Skip to content

Commit

Permalink
Print a little bit more with -v/--verbose (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli authored Mar 13, 2023
1 parent 40c0f05 commit d3c7453
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Makefile.posix
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ config.jou:
echo " return \"$(shell $(LLVM_CONFIG) --bindir)/clang\"" >> config.jou

self_hosted_compiler: jou config.jou $(wildcard self_hosted/*.jou)
./jou -v -o $@ --linker-flags "$(LDFLAGS)" self_hosted/main.jou
./jou -o $@ --linker-flags "$(LDFLAGS)" self_hosted/main.jou

.PHONY: clean
clean:
Expand Down
2 changes: 1 addition & 1 deletion Makefile.windows
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ config.jou:
echo " return NULL" >> config.jou

self_hosted_compiler.exe: jou.exe config.jou $(wildcard self_hosted/*.jou)
./jou.exe -v -o $@ --linker-flags "$(LDFLAGS)" self_hosted/main.jou
./jou.exe -o $@ --linker-flags "$(LDFLAGS)" self_hosted/main.jou

.PHONY: clean
clean:
Expand Down
55 changes: 26 additions & 29 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,15 @@ static void parse_file(struct CompileState *compst, const char *filename, const

struct FileState fs = { .path = strdup(filename) };

if(command_line_args.verbosity >= 2)
if(command_line_args.verbosity >= 1)
printf("Tokenizing %s\n", filename);
FILE *f = open_the_file(fs.path, import_location);
Token *tokens = tokenize(f, fs.path);
fclose(f);
if(command_line_args.verbosity >= 2)
print_tokens(tokens);

if(command_line_args.verbosity >= 2)
if(command_line_args.verbosity >= 1)
printf("Parsing %s\n", filename);
fs.ast = parse(tokens, compst->stdlib_path);
free_tokens(tokens);
Expand All @@ -234,13 +234,10 @@ static void parse_all_pending_files(struct CompileState *compst)
free(compst->parse_queue.ptr);
}

static void compile_ast_to_llvm(struct FileState *fs)
static char *compile_ast_to_object_file(struct FileState *fs)
{
if (command_line_args.verbosity >= 1)
printf("Compile to LLVM IR: %s\n", fs->path);

if (command_line_args.verbosity >= 2)
printf("Building CFG: %s\n", fs->path);
printf("Building Control Flow Graphs: %s\n", fs->path);

CfGraphFile cfgfile = build_control_flow_graphs(fs->ast, &fs->types);
for (AstToplevelNode *imp = fs->ast; imp->kind == AST_TOPLEVEL_IMPORT; imp++)
Expand All @@ -250,32 +247,38 @@ static void compile_ast_to_llvm(struct FileState *fs)
if(command_line_args.verbosity >= 2)
print_control_flow_graphs(&cfgfile);

if(command_line_args.verbosity >= 1)
printf("Analyzing CFGs: %s\n", fs->path);
simplify_control_flow_graphs(&cfgfile);
if(command_line_args.verbosity >= 2)
print_control_flow_graphs(&cfgfile);

if (command_line_args.verbosity >= 2)
printf("Build LLVM IR: %s\n", fs->path);
if (command_line_args.verbosity >= 1)
printf("Building LLVM IR: %s\n", fs->path);

fs->module = codegen(&cfgfile, &fs->types);
LLVMModuleRef mod = codegen(&cfgfile, &fs->types);
free_control_flow_graphs(&cfgfile);

if (command_line_args.verbosity >= 2)
print_llvm_ir(fs->module, false);
print_llvm_ir(mod, false);

/*
If this fails, it is not just users writing dumb code, it is a bug in this compiler.
This compiler should always fail with an error elsewhere, or generate valid LLVM IR.
*/
LLVMVerifyModule(fs->module, LLVMAbortProcessAction, NULL);
LLVMVerifyModule(mod, LLVMAbortProcessAction, NULL);

if (command_line_args.optlevel) {
if (command_line_args.verbosity >= 2)
if (command_line_args.verbosity >= 1)
printf("Optimizing %s (level %d)\n", fs->path, command_line_args.optlevel);
optimize(fs->module, command_line_args.optlevel);
optimize(mod, command_line_args.optlevel);
if(command_line_args.verbosity >= 2)
print_llvm_ir(fs->module, true);
print_llvm_ir(mod, true);
}

char *objpath = compile_to_object_file(mod);
LLVMDisposeModule(mod);
return objpath;
}

static char *find_stdlib()
Expand Down Expand Up @@ -433,9 +436,6 @@ int main(int argc, char **argv)
return 0;
}

if (command_line_args.verbosity >= 1)
printf("Parsing Jou files...\n");

include_special_stdlib_file(&compst, "_assert_fail.jou");
#ifdef _WIN32
include_special_stdlib_file(&compst, "_windows_startup.jou");
Expand All @@ -448,31 +448,28 @@ int main(int argc, char **argv)
printf("Type-checking...\n");

for (struct FileState *fs = compst.files.ptr; fs < End(compst.files); fs++) {
if (command_line_args.verbosity >= 2)
printf("Typecheck stage 1: %s\n", fs->path);
if (command_line_args.verbosity >= 1)
printf(" stage 1: %s\n", fs->path);
fs->pending_exports = typecheck_stage1_create_types(&fs->types, fs->ast);
}
add_imported_symbols(&compst);
for (struct FileState *fs = compst.files.ptr; fs < End(compst.files); fs++) {
if (command_line_args.verbosity >= 2)
printf("Typecheck stage 2: %s\n", fs->path);
if (command_line_args.verbosity >= 1)
printf(" stage 2: %s\n", fs->path);
fs->pending_exports = typecheck_stage2_signatures_globals_structbodies(&fs->types, fs->ast);
}
add_imported_symbols(&compst);
for (struct FileState *fs = compst.files.ptr; fs < End(compst.files); fs++) {
if (command_line_args.verbosity >= 2)
printf("Typecheck stage 3: %s\n", fs->path);
if (command_line_args.verbosity >= 1)
printf(" stage 3: %s\n", fs->path);
typecheck_stage3_function_and_method_bodies(&fs->types, fs->ast);
}

char **objpaths = calloc(sizeof objpaths[0], compst.files.len + 1);
for (struct FileState *fs = compst.files.ptr; fs < End(compst.files); fs++) {
compile_ast_to_llvm(fs);
objpaths[fs - compst.files.ptr] = compile_to_object_file(fs->module);
}
for (struct FileState *fs = compst.files.ptr; fs < End(compst.files); fs++)
objpaths[fs - compst.files.ptr] = compile_ast_to_object_file(fs);

for (struct FileState *fs = compst.files.ptr; fs < End(compst.files); fs++) {
LLVMDisposeModule(fs->module);
free_ast(fs->ast);
fs->ast = NULL;
free(fs->path);
Expand Down

0 comments on commit d3c7453

Please sign in to comment.