Skip to content

Commit 97cf52b

Browse files
committed
Test cleanup.
1 parent 58854da commit 97cf52b

File tree

12 files changed

+154
-130
lines changed

12 files changed

+154
-130
lines changed

premake5.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ workspace "sigma"
1313

1414
-- buildoptions { "-fsanitize=address" }
1515
-- linkoptions { "-fsanitize=address" }
16-
-- debugformat "C7"
16+
-- debugformat "C7"
1717

1818
filter "configurations:Release"
1919
defines { "NDEBUG" }
@@ -185,7 +185,7 @@ project "compiler"
185185
kind "ConsoleApp"
186186
location "source/compiler"
187187

188-
debugargs { "compile", "./test/main.s" }
188+
debugargs { "compile", "./test/main.s", "-e", "./test/a.obj" }
189189

190190
files {
191191
"source/compiler/**.h",

source/compiler/compiler/compiler.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ namespace sigma {
2020
: m_description(description) {}
2121

2222
auto compiler::compile() -> utility::result<void> {
23-
utility::console::print("compiling file: {}\n", m_description.path);
23+
utility::console::print("compiling file: {} ({})\n", m_description.source_path, m_description.emit_path);
2424

25-
ASSERT(m_description.emit != emit_target::EXECUTABLE, "executable support not implemented");
26-
TRY(verify_file(m_description.path));
25+
TRY(verify_file(m_description.source_path));
26+
TRY(m_emit_target, get_emit_target_from_path(m_description.emit_path));
2727

2828
// frontend
2929
// TODO: the entire frontend can be multi-threaded
3030
frontend_context frontend;
3131

3232
// generate the AST
33-
TRY(const std::string file, utility::fs::file<std::string>::load(m_description.path));
34-
TRY(tokenizer::tokenize(file, &m_description.path, frontend));
33+
TRY(const std::string file, utility::fs::file<std::string>::load(m_description.source_path));
34+
TRY(tokenizer::tokenize(file, &m_description.source_path, frontend));
3535
TRY(parser::parse(frontend));
3636

3737
// backend
@@ -47,9 +47,8 @@ namespace sigma {
4747
backend.module.compile();
4848

4949
//emit as an object file
50-
if(m_description.emit == OBJECT) {
51-
const filepath object_path = get_object_file_path();
52-
emit_object_file(backend.module, object_path);
50+
if(m_emit_target == emit_target::OBJECT) {
51+
emit_object_file(backend.module, m_description.emit_path);
5352
}
5453

5554
return SUCCESS;
@@ -80,10 +79,23 @@ namespace sigma {
8079
};
8180

8281
const char* format = object_formats[static_cast<u8>(m_description.target.get_system())];
83-
return m_description.path.get_parent_path() / (name + format);
82+
return m_description.source_path.get_parent_path() / (name + format);
8483
}
8584

8685
void compiler::emit_object_file(ir::module& module, const filepath& path) {
8786
utility::fs::file<utility::contiguous_container<utility::byte>>::save(path, module.generate_object_file());
8887
}
88+
89+
auto compiler::get_emit_target_from_path(const filepath& path) -> utility::result<emit_target> {
90+
if(path.get_extension() == ".exe") {
91+
return emit_target::EXECUTABLE;
92+
}
93+
94+
if(path.get_extension() == ".obj" || path.get_extension() == ".o") {
95+
return emit_target::OBJECT;
96+
}
97+
98+
NOT_IMPLEMENTED();
99+
return emit_target::NONE;
100+
}
89101
} // namespace sigma

source/compiler/compiler/compiler.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ namespace sigma {
2323
class module;
2424
} // namespace sigma::ir
2525

26-
enum emit_target : u8 {
26+
enum class emit_target : u8 {
2727
NONE,
2828
OBJECT,
2929
EXECUTABLE
3030
};
3131

3232
struct compiler_description {
33-
filepath path;
33+
filepath source_path;
34+
filepath emit_path;
3435
ir::target target;
35-
36-
emit_target emit;
3736
};
3837

3938
class compiler {
@@ -47,8 +46,12 @@ namespace sigma {
4746

4847
static auto verify_file(const filepath& path) -> utility::result<void>;
4948
static void emit_object_file(ir::module& module, const filepath& path);
49+
50+
static auto get_emit_target_from_path(const filepath& path) -> utility::result<emit_target>;
5051
private:
5152
compiler_description m_description;
53+
54+
emit_target m_emit_target;
5255
};
5356
} // namespace sigma
5457

source/compiler/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ using namespace utility::types;
55

66
i32 compile(const parametric::parameters& params) {
77
const sigma::compiler_description description {
8-
.path = params.get<filepath>("file"),
8+
.source_path = params.get<filepath>("file"),
9+
.emit_path = params.get<filepath>("emit"),
10+
911
// default to x64 win for now
1012
.target = { sigma::ir::arch::X64, sigma::ir::system::WINDOWS },
11-
.emit = params.get<sigma::emit_target>("emit")
1213
};
1314

1415
// compile the specified description, check for errors after we finish
@@ -43,9 +44,9 @@ auto main(i32 argc, char* argv[]) -> i32 {
4344
auto& compile_command = program.add_command("compile", "compile the specified source file", compile);
4445

4546
compile_command.add_positional_argument<filepath>("file", "source file to compile");
47+
compile_command.add_flag<filepath>("emit", "filepath to emit to", "e", "./a.obj");
4648
compile_command.add_flag<sigma::ir::arch>("arch", "CPU architecture to compile for [x64]");
4749
compile_command.add_flag<sigma::ir::system>("system", "operating system to compile for [windows, linux]");
48-
compile_command.add_flag<sigma::emit_target>("emit", "file the compiler should emit [object, executable]", "e", sigma::emit_target::OBJECT);
4950

5051
// TODO: add support for emitting multiple files at once
5152

source/compiler/test/main.s

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,28 @@
99
// - add namespaces to error messages, whenever applicable (ie. x::y::test)
1010
// - cleanup ir gen alignment sizes (u64 vs u32 vs u16)
1111
// - set crashes with more than 4(?) parameters
12+
// - implicit returns for non-void functions should be a thing
1213

1314
i32 main() {
14-
bool a = true;
15-
printf("%d\n", a);
16-
printf("%d\n", !a);
15+
i32* memory = cast<i32*>(malloc(100));
16+
17+
memory[0] = 1;
18+
memory[1] = 2;
1719

18-
bool b = 1;
19-
printf("%d\n", b);
20-
printf("%d\n", !b);
20+
printf("%d %d\n", memory[0], memory[1]);
21+
22+
ret 0;
2123
}
24+
25+
26+
// THIS CRASHES
27+
// i32 main() {
28+
// i32* memory = cast<i32*>(malloc(100));
29+
//
30+
// memory[0] = 1;
31+
// memory[1] = 2;
32+
//
33+
// printf("%d %d\n", memory[0], memory[1]);
34+
//
35+
// ret 0; a
36+
// }

source/tests/app_STDERR.txt

Whitespace-only changes.

source/tests/app_STDOUT.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
200

0 commit comments

Comments
 (0)