fixed compiler always exiting with 0

This commit is contained in:
Sven Vogel 2024-08-02 18:23:23 +02:00
parent 81e8c833e3
commit 34361bd834
7 changed files with 59 additions and 35 deletions

View File

@ -145,21 +145,21 @@ static void print_ast_to_file(AST_NODE_PTR ast, const TargetConfig *target) {
g_free(path); g_free(path);
} }
static void run_backend_codegen(const Module* module, const TargetConfig* target) { static int run_backend_codegen(const Module* module, const TargetConfig* target) {
DEBUG("initializing LLVM codegen backend..."); DEBUG("initializing LLVM codegen backend...");
llvm_backend_init(); llvm_backend_init();
DEBUG("initiializing backend for codegen..."); DEBUG("initiializing backend for codegen...");
BackendError err = init_backend(); BackendError err = init_backend();
if (err.kind != Success) { if (err.kind != Success) {
return; return EXIT_FAILURE;
} }
DEBUG("generating code..."); DEBUG("generating code...");
err = generate_code(module, target); err = generate_code(module, target);
if (err.kind != Success) { if (err.kind != Success) {
print_message(Error, "Backend failed: %s", err.impl.message); print_message(Error, "Backend failed: %s", err.impl.message);
return; return EXIT_FAILURE;
} }
print_message(Info, "Compilation finished successfully"); print_message(Info, "Compilation finished successfully");
@ -167,7 +167,10 @@ static void run_backend_codegen(const Module* module, const TargetConfig* target
err = deinit_backend(); err = deinit_backend();
if (err.kind != Success) { if (err.kind != Success) {
ERROR("Unable to deinit backend: %s", err.impl.message); ERROR("Unable to deinit backend: %s", err.impl.message);
return EXIT_FAILURE;
} }
return EXIT_SUCCESS;
} }
const char* get_absolute_import_path(const TargetConfig* config, const char* import_target_name) { const char* get_absolute_import_path(const TargetConfig* config, const char* import_target_name) {
@ -255,21 +258,27 @@ static int compile_module_with_dependencies(ModuleFileStack *unit, ModuleFile* f
* @param unit * @param unit
* @param target * @param target
*/ */
static void build_target(ModuleFileStack *unit, const TargetConfig *target) { static int build_target(ModuleFileStack *unit, const TargetConfig *target) {
int err = EXIT_SUCCESS;
print_message(Info, "Building target: %s", target->name); print_message(Info, "Building target: %s", target->name);
ModuleFile *file = push_file(unit, target->root_module); ModuleFile *file = push_file(unit, target->root_module);
AST_NODE_PTR root_module = AST_new_node(empty_location(file), AST_Module, NULL); AST_NODE_PTR root_module = AST_new_node(empty_location(file), AST_Module, NULL);
if (compile_module_with_dependencies(unit, file, target, root_module) == EXIT_SUCCESS) { err = compile_module_with_dependencies(unit, file, target, root_module);
if (err == EXIT_SUCCESS) {
if (root_module != NULL) { if (root_module != NULL) {
if (setup_target_environment(target) == 0) { err = setup_target_environment(target);
if (err == 0) {
print_ast_to_file(root_module, target); print_ast_to_file(root_module, target);
Module *module = create_set(root_module); Module *module = create_set(root_module);
if (module != NULL) { if (module != NULL) {
run_backend_codegen(module, target); err = run_backend_codegen(module, target);
} else {
err = EXIT_FAILURE;
} }
} }
@ -282,6 +291,8 @@ static void build_target(ModuleFileStack *unit, const TargetConfig *target) {
mem_purge_namespace(MemoryNamespaceSet); mem_purge_namespace(MemoryNamespaceSet);
print_file_statistics(file); print_file_statistics(file);
return err;
} }
/** /**
@ -289,7 +300,7 @@ static void build_target(ModuleFileStack *unit, const TargetConfig *target) {
* Creates a single target by the given command line arguments. * Creates a single target by the given command line arguments.
* @param unit * @param unit
*/ */
static void compile_file(ModuleFileStack *unit) { static int compile_file(ModuleFileStack *unit) {
INFO("compiling basic files..."); INFO("compiling basic files...");
TargetConfig *target = default_target_config_from_args(); TargetConfig *target = default_target_config_from_args();
@ -297,12 +308,14 @@ static void compile_file(ModuleFileStack *unit) {
if (target->root_module == NULL) { if (target->root_module == NULL) {
print_message(Error, "No input file specified."); print_message(Error, "No input file specified.");
delete_target_config(target); delete_target_config(target);
return; return EXIT_FAILURE;
} }
build_target(unit, target); int err = build_target(unit, target);
delete_target_config(target); delete_target_config(target);
return err;
} }
/** /**
@ -310,7 +323,9 @@ static void compile_file(ModuleFileStack *unit) {
* @param unit * @param unit
* @param config * @param config
*/ */
static void build_project_targets(ModuleFileStack *unit, const ProjectConfig *config) { static int build_project_targets(ModuleFileStack *unit, const ProjectConfig *config) {
int err = EXIT_SUCCESS;
if (is_option_set("all")) { if (is_option_set("all")) {
// build all targets in the project // build all targets in the project
GHashTableIter iter; GHashTableIter iter;
@ -320,9 +335,9 @@ static void build_project_targets(ModuleFileStack *unit, const ProjectConfig *co
char *key; char *key;
TargetConfig *val; TargetConfig *val;
while (g_hash_table_iter_next(&iter, (gpointer) &key, (gpointer) &val)) { while (g_hash_table_iter_next(&iter, (gpointer) &key, (gpointer) &val)) {
build_target(unit, val); err = build_target(unit, val);
} }
return; return err;
} }
// build all targets given in the arguments // build all targets given in the arguments
@ -333,7 +348,7 @@ static void build_project_targets(ModuleFileStack *unit, const ProjectConfig *co
const char *target_name = g_array_index(targets, const char*, i); const char *target_name = g_array_index(targets, const char*, i);
if (g_hash_table_contains(config->targets, target_name)) { if (g_hash_table_contains(config->targets, target_name)) {
build_target(unit, g_hash_table_lookup(config->targets, target_name)); err = build_target(unit, g_hash_table_lookup(config->targets, target_name));
} else { } else {
print_message(Error, "Unknown target: %s", target_name); print_message(Error, "Unknown target: %s", target_name);
} }
@ -343,39 +358,46 @@ static void build_project_targets(ModuleFileStack *unit, const ProjectConfig *co
} else { } else {
print_message(Error, "No targets specified."); print_message(Error, "No targets specified.");
} }
return err;
} }
/** /**
* @brief Build targets from project. Configuration is provided by command line arguments. * @brief Build targets from project. Configuration is provided by command line arguments.
* @param unit File storage * @param unit File storage
*/ */
static void build_project(ModuleFileStack *unit) { static int build_project(ModuleFileStack *unit) {
ProjectConfig *config = default_project_config(); ProjectConfig *config = default_project_config();
int err = load_project_config(config); int err = load_project_config(config);
if (err == PROJECT_OK) { if (err == PROJECT_OK) {
build_project_targets(unit, config); err = build_project_targets(unit, config);
} }
delete_project_config(config); delete_project_config(config);
return err;
} }
void run_compiler() { int run_compiler() {
ModuleFileStack files = new_file_stack(); ModuleFileStack files = new_file_stack();
int status = EXIT_SUCCESS;
if (is_option_set("build")) { if (is_option_set("build")) {
build_project(&files); status = build_project(&files);
} else if (is_option_set("compile")) { } else if (is_option_set("compile")) {
compile_file(&files); status = compile_file(&files);
} else { } else {
print_message(Error, "Invalid mode of operation. Rerun with --help."); print_message(Error, "Invalid mode of operation. Rerun with --help.");
} }
if (files.files == NULL) { if (files.files == NULL) {
print_message(Error, "No input files, nothing to do."); print_message(Error, "No input files, nothing to do.");
exit(1); } else {
print_unit_statistics(&files);
} }
print_unit_statistics(&files);
delete_files(&files); delete_files(&files);
return status;
} }

View File

@ -7,7 +7,8 @@
/** /**
* @brief Run the gemstone compiler with the provided command arguments. * @brief Run the gemstone compiler with the provided command arguments.
* @return status of compilation
*/ */
void run_compiler(); int run_compiler();
#endif //GEMSTONE_COMPILER_H #endif //GEMSTONE_COMPILER_H

View File

@ -74,11 +74,11 @@ int main(int argc, char *argv[]) {
exit(0); exit(0);
} }
run_compiler(); int status = run_compiler();
if (is_option_set("print-gc-stats")) { if (is_option_set("print-gc-stats")) {
print_memory_statistics(); print_memory_statistics();
} }
return 0; return status;
} }

View File

@ -85,6 +85,7 @@ def run_check_print_node():
54 deref 54 deref
55 ref 55 ref
56 value 56 value
57 value
""" == p.stdout """ == p.stdout

View File

@ -5,20 +5,20 @@ fun cstrlen(in cstr: str)(out u32: len) {
u32: idx = 0 as u32 u32: idx = 0 as u32
while !(str[idx] == 0) { while !(str[idx] == 0) {
idx = idx + 1 idx = idx + 1 as u32
} }
len = idx len = idx
} }
fun printcstr(in cstr: msg) { fun printcstr(in cstr: msg) {
u32: len = 0 u32: len = 0 as u32
cstrlen(msg)(len) cstrlen(msg)(len)
handle: stdout = 0 handle: stdout = 0 as u32
getStdoutHandle()(stdout) getStdoutHandle()(stdout)
u32: written = 0 u32: written = 0 as u32
writeBytes(stdout, msg, len)(written) writeBytes(stdout, msg, len)(written)
} }

View File

@ -5,20 +5,20 @@ fun cstrlen(in cstr: str)(out u32: len) {
u32: idx = 0 as u32 u32: idx = 0 as u32
while !(str[idx] == 0) { while !(str[idx] == 0) {
idx = idx + 1 idx = idx + 1 as u32
} }
len = idx len = idx
} }
fun printcstr(in cstr: msg) { fun printcstr(in cstr: msg) {
u32: len = 0 u32: len = 0 as u32
cstrlen(msg)(len) cstrlen(msg)(len)
handle: stdout = 0 handle: stdout = 0 as u32
getStdoutHandle()(stdout) getStdoutHandle()(stdout)
u32: written = 0 u32: written = 0 as u32
writeBytes(stdout, msg, len)(written) writeBytes(stdout, msg, len)(written)
} }

View File

@ -4,6 +4,6 @@ include(CTest)
# CTEST 1 # CTEST 1
# test if the program successfully reads the project config # test if the program successfully reads the project config
#add_test(NAME project_build add_test(NAME project_build
# WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/tests/project WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/tests/project
# COMMAND python ${GEMSTONE_TEST_DIR}/project/test_project.py) COMMAND python ${GEMSTONE_TEST_DIR}/project/test_project.py)