Skip to content

Commit

Permalink
self_hosted_compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
littlewhitecloud authored Jul 12, 2024
1 parent ccda81f commit 3f1622f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
9 changes: 8 additions & 1 deletion self_hosted/create_llvm_ir.jou
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ def file_defines_global_var(ast: AstFile*, name: byte*) -> bool:
return False


def create_llvm_ir(ast: AstFile*, ft: FileTypes*) -> LLVMModule*:
def create_llvm_ir(ast: AstFile*, ft: FileTypes*, checkmain: bool) -> LLVMModule*:
module = LLVMModuleCreateWithName(ast->path)
LLVMSetTarget(module, target.triple)
LLVMSetDataLayout(module, target.data_layout)
Expand All @@ -874,9 +874,12 @@ def create_llvm_ir(ast: AstFile*, ft: FileTypes*) -> LLVMModule*:
builder = LLVMCreateBuilder(),
file_types = ft
}
mainflag: bool = False

for s = ast->body.statements; s < &ast->body.statements[ast->body.nstatements]; s++:
if s->kind == AstStatementKind::Function and s->function.body.nstatements > 0:
if strcmp((&s->function.signature)->name, "main") == 0:
mainflag = True
a2i.define_function_or_method(&s->function, NULL)
elif s->kind == AstStatementKind::Class:
classdef = &s->classdef
Expand All @@ -889,5 +892,9 @@ def create_llvm_ir(ast: AstFile*, ft: FileTypes*) -> LLVMModule*:
# TODO: need to handle some others?
pass

if not mainflag and checkmain:
l: Location = Location{path = ast->path, lineno = 0}
fail(l, "missing `main` function to execute the program")

LLVMDisposeBuilder(a2i.builder)
return module
6 changes: 5 additions & 1 deletion self_hosted/main.jou
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,14 @@ class Compiler:
paths = self->get_object_file_paths()

for i = 0; i < self->nfiles; i++:
checkmain: bool = False
if self->verbosity >= 1:
printf("Build LLVM IR: %s\n", self->files[i].ast.path)

module = create_llvm_ir(&self->files[i].ast, &self->files[i].typectx)
if strcmp(self->args->main_path, self->files[i].ast.path) == 0:
checkmain = True

module = create_llvm_ir(&self->files[i].ast, &self->files[i].typectx, checkmain)
if self->verbosity >= 2:
# Don't want to use LLVMDumpModule() because it dumps to stdout.
# When redirected, stdout and stderr tend to get mixed up into a weird order.
Expand Down
1 change: 1 addition & 0 deletions self_hosted/runs_wrong.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ tests/syntax_error/assign_to_None.jou
tests/syntax_error/None_as_value.jou
tests/should_succeed/method_by_value.jou
tests/wrong_type/self_annotation.jou
tests/should_succeed/file.jou

0 comments on commit 3f1622f

Please sign in to comment.