Skip to content

Commit 883b3e8

Browse files
committed
1 parent f79afcf commit 883b3e8

18 files changed

+1048
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@
2626
[submodule "tests/benchmarks/deps/nanobench"]
2727
path = tests/benchmarks/deps/nanobench
2828
url = https://github.com/Spartan322/nanobench
29+
[submodule "deps/lauf"]
30+
path = deps/lauf
31+
url = https://github.com/OpenVicProject/lauf

deps/SCsub

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,63 @@ def build_std_function(env):
4545
env.Append(CPPPATH=env.std_function["INCPATH"])
4646
env.exposed_includes += env.std_function["INCPATH"]
4747

48+
def build_lauf(env):
49+
import os
50+
51+
if (env.get("is_msvc", False) and not env.get("use_clang_cl")) or (env["CXX"] != "clang++" and env["optimize"] == "none"):
52+
env.Append(CPPDEFINES=["LAUF_HAS_TAIL_CALL_ELIMINATION=0"])
53+
else:
54+
env.Append(CPPDEFINES=["LAUF_HAS_TAIL_CALL_ELIMINATION=1"])
55+
56+
lauf_env = env.Clone()
57+
58+
if lauf_env.get("is_msvc", False):
59+
lauf_env.Append(CXXFLAGS=["/WX", "/W3", "/D", "_CRT_SECURE_NO_WARNINGS"])
60+
if lauf_env.get("use_clang_cl"):
61+
lauf_env.Append(CXXFLAGS=["-Wno-return-type-c-linkage", "-fomit-frame-pointer"])
62+
lauf_env.Append(CXXFLAGS=["/wd5105"])
63+
else:
64+
lauf_env.Append(CXXFLAGS=["/Oy"])
65+
else:
66+
lauf_env.Append(CXXFLAGS=["-pedantic-errors", "-Werror", "-Wall", "-Wextra", "-Wconversion", "-Wsign-conversion", "-fomit-frame-pointer"])
67+
if lauf_env["CXX"] == "clang++":
68+
lauf_env.Append(CXXFLAGS=["-Wno-return-type-c-linkage"])
69+
lauf_env.Append(CXXFLAGS=["-Wno-shift-op-parentheses", "-Wno-parentheses-equality"])
70+
else:
71+
lauf_env.Append(
72+
CXXFLAGS=[
73+
"-Wno-parentheses",
74+
"-Wno-unused-local-typedefs",
75+
"-Wno-array-bounds", # , "-Wno-maybe-uninitialized", "-Wno-restrict"
76+
]
77+
)
78+
79+
lexy_include_path = "openvic-dataloader/deps/lexy/include"
80+
include_path = "lauf/include"
81+
source_path = "lauf/src"
82+
lauf_env.Append(CPPPATH=[[lauf_env.Dir(p) for p in [lexy_include_path, source_path, include_path]]])
83+
sources = lauf_env.GlobRecursive("*.cpp", [source_path])
84+
env.lauf_sources = sources
85+
86+
library_name = "liblauf" + env["LIBSUFFIX"]
87+
library = lauf_env.StaticLibrary(target=os.path.join(source_path, library_name), source=sources)
88+
Default(library)
89+
90+
include_dir = lauf_env.Dir(include_path)
91+
source_dir = lauf_env.Dir(source_path)
92+
93+
env.lauf = {}
94+
env.lauf["INCPATH"] = [env.Dir(include_path)]
95+
96+
env.Append(CPPPATH=env.lauf["INCPATH"])
97+
if env.get("is_msvc", False):
98+
env.Append(CXXFLAGS=["/external:I", include_dir, "/external:W0"])
99+
else:
100+
env.Append(CXXFLAGS=["-isystem", include_dir])
101+
env.Append(LIBPATH=[source_dir])
102+
env.Prepend(LIBS=[library_name])
103+
env.exposed_includes += env.lauf["INCPATH"]
104+
48105
def link_tbb(env):
49106
import sys
50107
if not env.get("is_msvc", False) and not env.get("use_mingw", False) and sys.platform != "darwin":
@@ -56,4 +113,5 @@ build_ordered_map(env)
56113
build_colony(env)
57114
build_function2(env)
58115
build_std_function(env)
116+
build_lauf(env)
59117
link_tbb(env)

deps/lauf

Submodule lauf added at 8f3b2dc
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "AsmBuilder.hpp"
2+
3+
#include <lauf/asm/builder.h>
4+
5+
using namespace OpenVic::Vm;
6+
7+
CodeBuilder::CodeBuilder(AsmBuilderRef builder, ModuleRef module, Function function)
8+
: HandlerRef(builder), _module(module), _body({ .function = { true, function } }) {
9+
lauf_asm_build(builder, _module, _body.function.value);
10+
}
11+
12+
CodeBuilder::CodeBuilder(AsmBuilderRef builder, ModuleRef module, Chunk chunk, lauf_asm_signature signature)
13+
: HandlerRef(builder), _module(module), _body({ .chunk = { false, chunk } }) {
14+
lauf_asm_build_chunk(builder, _module, _body.chunk.value, signature);
15+
}
16+
17+
AsmBuilderRef CodeBuilder::asm_builder() {
18+
return _handle;
19+
}
20+
21+
void CodeBuilder::CodeBuilder::set_for(Function function) {
22+
set_for(_module, function);
23+
}
24+
25+
void CodeBuilder::set_for(ModuleRef module, Function function) {
26+
_module = module;
27+
_body.function.is_function = true;
28+
_body.function.value = function;
29+
lauf_asm_build(*this, _module, _body.function.value);
30+
}
31+
32+
void CodeBuilder::set_for(Chunk chunk) {
33+
set_for(_module, chunk);
34+
}
35+
36+
void CodeBuilder::set_for(ModuleRef module, Chunk chunk) {
37+
set_for(module, chunk, chunk.signature());
38+
}
39+
40+
void CodeBuilder::set_for(Chunk chunk, lauf_asm_signature signature) {
41+
set_for(_module, chunk, signature);
42+
}
43+
44+
void CodeBuilder::set_for(ModuleRef module, Chunk chunk, lauf_asm_signature signature) {
45+
_module = module;
46+
_body.chunk.is_function = false;
47+
_body.chunk.value = chunk;
48+
lauf_asm_build_chunk(*this, _module, _body.chunk.value, signature);
49+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#pragma once
2+
3+
#include <bit>
4+
#include <string_view>
5+
6+
#include "openvic-simulation/vm/Chunk.hpp"
7+
#include "openvic-simulation/vm/Function.hpp"
8+
#include "openvic-simulation/vm/Handler.hpp"
9+
#include "openvic-simulation/vm/Module.hpp"
10+
11+
#include <lauf/asm/builder.h>
12+
#include <lauf/asm/module.h>
13+
14+
namespace OpenVic::Vm {
15+
struct AsmBuilderRef;
16+
17+
struct CodeBuilder : HandlerRef<lauf_asm_builder> {
18+
AsmBuilderRef asm_builder();
19+
20+
ModuleRef module() {
21+
return _module;
22+
}
23+
24+
bool is_function() const {
25+
return _body.function.is_function;
26+
}
27+
28+
bool is_chunk() const {
29+
return !_body.chunk.is_function && _body.chunk.value != nullptr;
30+
}
31+
32+
bool can_build() const {
33+
return !_body.chunk.is_function && _body.chunk.value == nullptr;
34+
}
35+
36+
Function function() const {
37+
assert(is_function());
38+
return _body.function.value;
39+
}
40+
41+
Chunk chunk() const {
42+
assert(is_chunk());
43+
return _body.chunk.value;
44+
}
45+
46+
void set_for(Function function);
47+
void set_for(ModuleRef module, Function function);
48+
void set_for(Chunk chunk);
49+
void set_for(ModuleRef module, Chunk chunk);
50+
void set_for(Chunk chunk, lauf_asm_signature signature);
51+
void set_for(ModuleRef module, Chunk chunk, lauf_asm_signature signature);
52+
53+
Global build_literal(const std::span<unsigned char> data) {
54+
return lauf_asm_build_data_literal(*this, data.data(), data.size_bytes());
55+
}
56+
57+
Global build_string(std::string_view string) {
58+
return lauf_asm_build_data_literal(
59+
*this, std::bit_cast<unsigned char*>(string.data()), string.size() * sizeof(char)
60+
);
61+
}
62+
63+
Global build_cstring(const char* string) {
64+
return lauf_asm_build_string_literal(*this, string);
65+
}
66+
67+
bool finish() {
68+
bool result = lauf_asm_build_finish(*this);
69+
_body.chunk.is_function = false;
70+
_body.chunk.value = nullptr;
71+
return result;
72+
}
73+
74+
~CodeBuilder() {
75+
if (_handle == nullptr || !can_build()) {
76+
return;
77+
}
78+
finish();
79+
_handle = nullptr;
80+
}
81+
82+
protected:
83+
friend struct AsmBuilderRef;
84+
CodeBuilder(AsmBuilderRef builder, ModuleRef module, Function function);
85+
CodeBuilder(AsmBuilderRef builder, ModuleRef module, Chunk chunk, lauf_asm_signature signature);
86+
87+
ModuleRef _module;
88+
89+
union {
90+
struct {
91+
bool is_function;
92+
lauf_asm_function* value;
93+
} function;
94+
struct {
95+
bool is_function;
96+
lauf_asm_chunk* value;
97+
} chunk;
98+
} _body;
99+
};
100+
101+
struct AsmBuilderRef : HandlerRef<lauf_asm_builder> {
102+
using HandlerRef::HandlerRef;
103+
104+
CodeBuilder build(ModuleRef module, Function function) {
105+
return { *this, module, function };
106+
}
107+
108+
CodeBuilder build(ModuleRef module, Chunk chunk, lauf_asm_signature signature) {
109+
return { *this, module, chunk, signature };
110+
}
111+
};
112+
113+
struct AsmBuilder : UniqueHandlerDerived<AsmBuilder, AsmBuilderRef> {
114+
using UniqueHandlerDerived::UniqueHandlerDerived;
115+
using UniqueHandlerDerived::operator=;
116+
117+
AsmBuilder(lauf_asm_build_options options = lauf_asm_default_build_options)
118+
: UniqueHandlerDerived(lauf_asm_create_builder(options)) {}
119+
120+
~AsmBuilder() {
121+
if (_handle == nullptr) {
122+
return;
123+
}
124+
lauf_asm_destroy_builder(*this);
125+
_handle = nullptr;
126+
}
127+
};
128+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include "openvic-simulation/vm/Handler.hpp"
4+
5+
#include <lauf/asm/module.h>
6+
7+
namespace OpenVic::Vm {
8+
struct Chunk : HandlerRef<lauf_asm_chunk> {
9+
using HandlerRef::HandlerRef;
10+
11+
lauf_asm_signature signature() const {
12+
return lauf_asm_chunk_signature(*this);
13+
}
14+
15+
bool is_empty() const {
16+
return lauf_asm_chunk_is_empty(*this);
17+
}
18+
};
19+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include <cassert>
4+
#include <string_view>
5+
6+
#include "openvic-simulation/vm/Handler.hpp"
7+
8+
#include <lauf/asm/module.h>
9+
10+
namespace OpenVic::Vm {
11+
struct Function : HandlerRef<lauf_asm_function> {
12+
using HandlerRef::HandlerRef;
13+
14+
std::string_view name() const {
15+
return lauf_asm_function_name(*this);
16+
}
17+
18+
lauf_asm_signature signature() const {
19+
return lauf_asm_function_signature(*this);
20+
}
21+
22+
bool has_definition() const {
23+
return lauf_asm_function_has_definition(*this);
24+
}
25+
26+
size_t get_instruction_by_index(lauf_asm_inst const* ip) const {
27+
return lauf_asm_get_instruction_index(*this, ip);
28+
}
29+
30+
void set_export() {
31+
lauf_asm_export_function(*this);
32+
}
33+
};
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <string_view>
4+
5+
#include "openvic-simulation/vm/Handler.hpp"
6+
7+
#include <lauf/asm/module.h>
8+
#include <lauf/asm/type.h>
9+
10+
namespace OpenVic::Vm {
11+
struct Global : HandlerRef<lauf_asm_global> {
12+
using HandlerRef::HandlerRef;
13+
14+
std::string_view debug_name() const {
15+
const char* name = lauf_asm_global_debug_name(*this);
16+
return name == nullptr ? std::string_view {} : name;
17+
}
18+
19+
lauf_asm_layout layout() const {
20+
return lauf_asm_global_layout(*this);
21+
}
22+
23+
bool has_definition() const {
24+
return lauf_asm_global_has_definition(*this);
25+
}
26+
};
27+
}

0 commit comments

Comments
 (0)