Skip to content

Commit 5f70390

Browse files
committed
1 parent f79afcf commit 5f70390

20 files changed

+1168
-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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,61 @@ 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("use_clang_cl", False) or env["CXX"] == "clang++" or (not env.get("is_msvc", False) and env["optimize"] != "none"):
52+
env.Append(CPPDEFINES=["LAUF_HAS_TAIL_CALL_ELIMINATION=1"])
53+
54+
lauf_env = env.Clone()
55+
56+
if lauf_env.get("is_msvc", False):
57+
lauf_env.Append(CXXFLAGS=["/WX", "/W3", "/D", "_CRT_SECURE_NO_WARNINGS"])
58+
if lauf_env.get("use_clang_cl"):
59+
lauf_env.Append(CXXFLAGS=["-Wno-return-type-c-linkage", "-fomit-frame-pointer"])
60+
lauf_env.Append(CXXFLAGS=["/wd5105"])
61+
else:
62+
lauf_env.Append(CXXFLAGS=["/Oy"])
63+
else:
64+
lauf_env.Append(CXXFLAGS=["-pedantic-errors", "-Werror", "-Wall", "-Wextra", "-Wconversion", "-Wsign-conversion", "-fomit-frame-pointer"])
65+
if lauf_env["CXX"] == "clang++":
66+
lauf_env.Append(CXXFLAGS=["-Wno-return-type-c-linkage"])
67+
lauf_env.Append(CXXFLAGS=["-Wno-shift-op-parentheses", "-Wno-parentheses-equality"])
68+
else:
69+
lauf_env.Append(
70+
CXXFLAGS=[
71+
"-Wno-parentheses",
72+
"-Wno-unused-local-typedefs",
73+
"-Wno-array-bounds", # , "-Wno-maybe-uninitialized", "-Wno-restrict"
74+
]
75+
)
76+
77+
lexy_include_path = "openvic-dataloader/deps/lexy/include"
78+
include_path = "lauf/include"
79+
source_path = "lauf/src"
80+
lauf_env.Append(CPPPATH=[[lauf_env.Dir(p) for p in [lexy_include_path, source_path, include_path]]])
81+
sources = lauf_env.GlobRecursive("*.cpp", [source_path])
82+
env.lauf_sources = sources
83+
84+
library_name = "liblauf" + env["LIBSUFFIX"]
85+
library = lauf_env.StaticLibrary(target=os.path.join(source_path, library_name), source=sources)
86+
Default(library)
87+
88+
include_dir = lauf_env.Dir(include_path)
89+
source_dir = lauf_env.Dir(source_path)
90+
91+
env.lauf = {}
92+
env.lauf["INCPATH"] = [env.Dir(include_path)]
93+
94+
env.Append(CPPPATH=env.lauf["INCPATH"])
95+
if env.get("is_msvc", False):
96+
env.Append(CXXFLAGS=["/external:I", include_dir, "/external:W0"])
97+
else:
98+
env.Append(CXXFLAGS=["-isystem", include_dir])
99+
env.Append(LIBPATH=[source_dir])
100+
env.Prepend(LIBS=[library_name])
101+
env.exposed_includes += env.lauf["INCPATH"]
102+
48103
def link_tbb(env):
49104
import sys
50105
if not env.get("is_msvc", False) and not env.get("use_mingw", False) and sys.platform != "darwin":
@@ -56,4 +111,5 @@ build_ordered_map(env)
56111
build_colony(env)
57112
build_function2(env)
58113
build_std_function(env)
114+
build_lauf(env)
59115
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+
#pragma once
2+
3+
#include "openvic-simulation/vm/Chunk.hpp"
4+
#include "openvic-simulation/vm/CodeBuilder.hpp"
5+
#include "openvic-simulation/vm/Function.hpp"
6+
#include "openvic-simulation/vm/Handler.hpp"
7+
#include "openvic-simulation/vm/Module.hpp"
8+
9+
#include <lauf/asm/builder.h>
10+
#include <lauf/asm/module.h>
11+
12+
namespace OpenVic::Vm {
13+
struct AsmBuilderRef : HandlerRef<lauf_asm_builder> {
14+
using HandlerRef::HandlerRef;
15+
16+
CodeBuilder& build(ModuleRef module, Function function) {
17+
_current_builder = { *this, module, function };
18+
return _current_builder;
19+
}
20+
21+
CodeBuilder& build(ModuleRef module, Chunk chunk, lauf_asm_signature signature) {
22+
_current_builder = { *this, module, chunk, signature };
23+
return _current_builder;
24+
}
25+
26+
CodeBuilder& current_builder() {
27+
return _current_builder;
28+
}
29+
30+
private:
31+
CodeBuilder _current_builder { *this };
32+
};
33+
34+
struct AsmBuilder : UniqueHandlerDerived<AsmBuilder, AsmBuilderRef> {
35+
using UniqueHandlerDerived::UniqueHandlerDerived;
36+
using UniqueHandlerDerived::operator=;
37+
38+
AsmBuilder(lauf_asm_build_options options = lauf_asm_default_build_options)
39+
: UniqueHandlerDerived(lauf_asm_create_builder(options)) {}
40+
41+
~AsmBuilder() {
42+
if (_handle == nullptr) {
43+
return;
44+
}
45+
lauf_asm_destroy_builder(*this);
46+
_handle = nullptr;
47+
}
48+
};
49+
}
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "CodeBuilder.hpp"
2+
3+
#include "openvic-simulation/vm/AsmBuilder.hpp"
4+
5+
#include <lauf/asm/builder.h>
6+
7+
using namespace OpenVic::Vm;
8+
9+
CodeBuilder::CodeBuilder(AsmBuilderRef builder) : HandlerRef(builder), _module(nullptr), _body({ .chunk { false, nullptr } }) {}
10+
11+
CodeBuilder::CodeBuilder(AsmBuilderRef builder, ModuleRef module, Function function)
12+
: HandlerRef(builder), _module(module), _body({ .function = { true, function } }) {
13+
lauf_asm_build(builder, _module, _body.function.value);
14+
}
15+
16+
CodeBuilder::CodeBuilder(AsmBuilderRef builder, ModuleRef module, Chunk chunk, lauf_asm_signature signature)
17+
: HandlerRef(builder), _module(module), _body({ .chunk = { false, chunk } }) {
18+
lauf_asm_build_chunk(builder, _module, _body.chunk.value, signature);
19+
}
20+
21+
AsmBuilderRef CodeBuilder::asm_builder() {
22+
return _handle;
23+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#pragma once
2+
3+
#include <bit>
4+
#include <string_view>
5+
6+
#include "openvic-simulation/vm/Handler.hpp"
7+
#include "openvic-simulation/vm/Local.hpp"
8+
#include "openvic-simulation/vm/Module.hpp"
9+
10+
#include <lauf/asm/builder.h>
11+
#include <lauf/asm/module.h>
12+
13+
namespace OpenVic::Vm {
14+
struct AsmBuilderRef;
15+
16+
struct CodeBuilder : HandlerRef<lauf_asm_builder> {
17+
AsmBuilderRef asm_builder();
18+
19+
ModuleRef module() {
20+
return _module;
21+
}
22+
23+
bool is_function() const {
24+
return _body.function.is_function;
25+
}
26+
27+
bool is_chunk() const {
28+
return !_body.chunk.is_function && _body.chunk.value != nullptr;
29+
}
30+
31+
bool can_build() const {
32+
return !_body.chunk.is_function && _body.chunk.value == nullptr;
33+
}
34+
35+
Function function() const {
36+
assert(is_function());
37+
return _body.function.value;
38+
}
39+
40+
Chunk chunk() const {
41+
assert(is_chunk());
42+
return _body.chunk.value;
43+
}
44+
45+
Global build_literal(const std::span<unsigned char> data) {
46+
return lauf_asm_build_data_literal(*this, data.data(), data.size_bytes());
47+
}
48+
49+
Global build_string(std::string_view string) {
50+
return lauf_asm_build_data_literal(
51+
*this, std::bit_cast<unsigned char*>(string.data()), string.size() * sizeof(char)
52+
);
53+
}
54+
55+
Global build_cstring(const char* string) {
56+
return lauf_asm_build_string_literal(*this, string);
57+
}
58+
59+
Local build_local(lauf_asm_layout layout) {
60+
return lauf_asm_build_local(*this, layout);
61+
}
62+
63+
void drop() && {
64+
_body.chunk.is_function = false;
65+
_body.chunk.value = nullptr;
66+
}
67+
68+
bool finish() && {
69+
bool result = lauf_asm_build_finish(*this);
70+
static_cast<CodeBuilder&&>(*this).drop();
71+
return result;
72+
}
73+
74+
~CodeBuilder() {
75+
if (_handle == nullptr || !can_build()) {
76+
return;
77+
}
78+
static_cast<CodeBuilder&&>(*this).finish();
79+
_handle = nullptr;
80+
}
81+
82+
protected:
83+
friend struct AsmBuilderRef;
84+
CodeBuilder(AsmBuilderRef builder);
85+
CodeBuilder(AsmBuilderRef builder, ModuleRef module, Function function);
86+
CodeBuilder(AsmBuilderRef builder, ModuleRef module, Chunk chunk, lauf_asm_signature signature);
87+
88+
ModuleRef _module;
89+
90+
union {
91+
struct {
92+
bool is_function;
93+
lauf_asm_function* value;
94+
} function;
95+
struct {
96+
bool is_function;
97+
lauf_asm_chunk* value;
98+
} chunk;
99+
} _body;
100+
};
101+
}
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)