diff --git a/examples/main.ll b/examples/main.ll index 670239df7..450f898d8 100644 --- a/examples/main.ll +++ b/examples/main.ll @@ -20,7 +20,18 @@ %MSG = type {%HWND, i32, %WPARAM, %LPARAM, i32, %POINT} declare void @llvm.memmove.p0i8.p0i8.i64(i8*, i8*, i64, i32, i1) argmemonly nounwind +@constant = global i64 zeroinitializer @win32_perf_count_freq = global i64 zeroinitializer +define i64 @win32_get_perf_count_freq() { +entry.-.0: + %0 = alloca i64, align 8 ; r + store i64 zeroinitializer, i64* %0 + %1 = getelementptr inbounds i64, i64* %0 + %2 = call i32 @QueryPerformanceFrequency(i64* %1) + %3 = load i64, i64* %0, align 8 + ret i64 %3 +} + define double @time_now() { entry.-.0: %0 = load i64, i64* @win32_perf_count_freq, align 8 @@ -88,242 +99,8 @@ if.done.-.2: define void @main() { entry.-.0: - %0 = alloca %WNDCLASSEXA, align 8 ; wc - store %WNDCLASSEXA zeroinitializer, %WNDCLASSEXA* %0 - %1 = alloca %HINSTANCE, align 8 ; instance - store %HINSTANCE zeroinitializer, %HINSTANCE* %1 - %2 = call %HINSTANCE @GetModuleHandleA(i8* null) - store %HINSTANCE %2, %HINSTANCE* %1 - %3 = getelementptr inbounds i64, i64* @win32_perf_count_freq - %4 = call i32 @QueryPerformanceFrequency(i64* %3) - %5 = alloca i8*, align 8 ; class_name - store i8* zeroinitializer, i8** %5 - %6 = getelementptr inbounds [18 x i8], [18 x i8]* @.str2, i64 0, i64 0 - %7 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %7 - %8 = getelementptr inbounds %.string, %.string* %7, i64 0, i32 0 - %9 = getelementptr inbounds %.string, %.string* %7, i64 0, i32 1 - store i8* %6, i8** %8 - store i64 18, i64* %9 - %10 = load %.string, %.string* %7, align 8 - %11 = call i8* @main$to_c_string-0(%.string %10) - store i8* %11, i8** %5 - %12 = alloca i8*, align 8 ; title - store i8* zeroinitializer, i8** %12 - %13 = getelementptr inbounds [18 x i8], [18 x i8]* @.str3, i64 0, i64 0 - %14 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %14 - %15 = getelementptr inbounds %.string, %.string* %14, i64 0, i32 0 - %16 = getelementptr inbounds %.string, %.string* %14, i64 0, i32 1 - store i8* %13, i8** %15 - store i64 18, i64* %16 - %17 = load %.string, %.string* %14, align 8 - %18 = call i8* @main$to_c_string-0(%.string %17) - store i8* %18, i8** %12 - %19 = getelementptr inbounds %WNDCLASSEXA, %WNDCLASSEXA* %0, i64 0, i32 0 - store i32 80, i32* %19 - %20 = getelementptr inbounds %WNDCLASSEXA, %WNDCLASSEXA* %0, i64 0, i32 1 - store i32 3, i32* %20 - %21 = getelementptr inbounds %WNDCLASSEXA, %WNDCLASSEXA* %0, i64 0, i32 5 - %22 = load %HINSTANCE, %HINSTANCE* %1, align 8 - store %HINSTANCE %22, %HINSTANCE* %21 - %23 = getelementptr inbounds %WNDCLASSEXA, %WNDCLASSEXA* %0, i64 0, i32 10 - %24 = load i8*, i8** %5, align 8 - store i8* %24, i8** %23 - %25 = getelementptr inbounds %WNDCLASSEXA, %WNDCLASSEXA* %0, i64 0, i32 8 - %26 = inttoptr i64 1 to %.rawptr - store %HBRUSH %26, %HBRUSH* %25 - %27 = getelementptr inbounds %WNDCLASSEXA, %WNDCLASSEXA* %0, i64 0, i32 2 - store %WNDPROC @main$1, %WNDPROC* %27 - %28 = getelementptr inbounds %WNDCLASSEXA, %WNDCLASSEXA* %0 - %29 = call %ATOM @RegisterClassExA(%WNDCLASSEXA* %28) - %30 = icmp eq i16 %29, 0 - br i1 %30, label %if.then.-.1, label %if.done.-.2 - -if.then.-.1: + call void @__$startup_runtime() ret void - -if.done.-.2: - %31 = alloca %HWND, align 8 ; hwnd - store %HWND zeroinitializer, %HWND* %31 - %32 = load i8*, i8** %5, align 8 - %33 = load i8*, i8** %12, align 8 - %34 = load %HINSTANCE, %HINSTANCE* %1, align 8 - %35 = call %HWND @CreateWindowExA(i32 0, i8* %32, i8* %33, i32 281673728, i32 0, i32 0, i32 854, i32 480, %HWND null, %HMENU null, %HINSTANCE %34, %.rawptr null) - store %HWND %35, %HWND* %31 - %36 = load %HWND, %HWND* %31, align 8 - %37 = icmp eq %.rawptr %36, null - br i1 %37, label %if.then.-.3, label %if.done.-.4 - -if.then.-.3: - call void @win32_print_last_error() - ret void - -if.done.-.4: - %38 = alloca double, align 8 ; start_time - store double zeroinitializer, double* %38 - %39 = call double @time_now() - store double %39, double* %38 - %40 = alloca i1, align 1 ; running - store i1 zeroinitializer, i1* %40 - store i1 true, i1* %40 - %41 = alloca i64, align 8 ; tick_count - store i64 zeroinitializer, i64* %41 - store i64 0, i64* %41 - br label %for.loop.-.6 - -for.body.-.5: - %42 = alloca double, align 8 ; curr_time - store double zeroinitializer, double* %42 - %43 = call double @time_now() - store double %43, double* %42 - %44 = alloca double, align 8 ; dt - store double zeroinitializer, double* %44 - %45 = load double, double* %38, align 8 - %46 = load double, double* %42, align 8 - %47 = fsub double %46, %45 - store double %47, double* %44 - %48 = load double, double* %44, align 8 - %49 = fcmp ogt double %48, 0x4000000000000000 - br i1 %49, label %if.then.-.7, label %if.done.-.8 - -for.loop.-.6: - %50 = load i1, i1* %40, align 1 - br i1 %50, label %for.body.-.5, label %for.done.-.16 - -if.then.-.7: - store i1 false, i1* %40 - br label %if.done.-.8 - -if.done.-.8: - %51 = alloca %MSG, align 8 ; msg - store %MSG zeroinitializer, %MSG* %51 - br label %for.body.-.9 - -for.body.-.9: - %52 = alloca i1, align 1 ; ok - store i1 zeroinitializer, i1* %52 - %53 = getelementptr inbounds %MSG, %MSG* %51 - %54 = call %BOOL @PeekMessageA(%MSG* %53, %HWND null, i32 0, i32 0, i32 1) - %55 = icmp ne i32 %54, 0 - store i1 %55, i1* %52 - %56 = load i1, i1* %52, align 1 - br i1 %56, label %if.done.-.11, label %if.then.-.10 - -if.then.-.10: - br label %for.done.-.15 - -if.done.-.11: - %57 = getelementptr inbounds %MSG, %MSG* %51, i64 0, i32 1 - %58 = load i32, i32* %57, align 4 - %59 = icmp eq i32 %58, 18 - br i1 %59, label %if.then.-.12, label %if.else.-.13 - -if.then.-.12: - ret void - -if.else.-.13: - %60 = getelementptr inbounds %MSG, %MSG* %51 - %61 = call %BOOL @TranslateMessage(%MSG* %60) - %62 = getelementptr inbounds %MSG, %MSG* %51 - %63 = call %LRESULT @DispatchMessageA(%MSG* %62) - br label %if.done.-.14 - -if.done.-.14: - br label %for.body.-.9 - -for.done.-.15: - %64 = getelementptr inbounds [6 x i8], [6 x i8]* @.str4, i64 0, i64 0 - %65 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %65 - %66 = getelementptr inbounds %.string, %.string* %65, i64 0, i32 0 - %67 = getelementptr inbounds %.string, %.string* %65, i64 0, i32 1 - store i8* %64, i8** %66 - store i64 6, i64* %67 - %68 = load %.string, %.string* %65, align 8 - call void @print_string(%.string %68) - %69 = load i64, i64* %41, align 8 - call void @print_int(i64 %69) - %70 = load i64, i64* %41, align 8 - %71 = add i64 %70, 1 - store i64 %71, i64* %41 - call void @print_rune(i32 10) - call void @sleep_ms(i32 16) - br label %for.loop.-.6 - -for.done.-.16: - ret void -} - -define i8* @main$to_c_string-0(%.string %s) { -entry.-.0: - %0 = alloca %.string, align 8 ; s - store %.string zeroinitializer, %.string* %0 - store %.string %s, %.string* %0 - %1 = alloca i8*, align 8 ; c_str - store i8* zeroinitializer, i8** %1 - %2 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 1 - %3 = load i64, i64* %2, align 8 - %4 = add i64 %3, 1 - %5 = call %.rawptr @malloc(i64 %4) - %6 = bitcast %.rawptr %5 to i8* - store i8* %6, i8** %1 - %7 = load i8*, i8** %1, align 8 - %8 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 0 - %9 = load i8*, i8** %8, align 8 - %10 = getelementptr i8, i8* %9, i64 0 - %11 = getelementptr inbounds i8, i8* %10 - %12 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 1 - %13 = load i64, i64* %12, align 8 - %14 = call i32 @memcpy(%.rawptr %7, %.rawptr %11, i64 %13) - %15 = load i8*, i8** %1, align 8 - %16 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 1 - %17 = load i64, i64* %16, align 8 - %18 = getelementptr i8, i8* %15, i64 %17 - store i8 0, i8* %18 - %19 = load i8*, i8** %1, align 8 - ret i8* %19 -} - -define %LRESULT @main$1(%HWND %hwnd, i32 %msg, %WPARAM %wparam, %LPARAM %lparam) noinline { -entry.-.0: - %0 = alloca %HWND, align 8 ; hwnd - store %HWND zeroinitializer, %HWND* %0 - store %HWND %hwnd, %HWND* %0 - %1 = alloca i32, align 4 ; msg - store i32 zeroinitializer, i32* %1 - store i32 %msg, i32* %1 - %2 = alloca %WPARAM, align 8 ; wparam - store %WPARAM zeroinitializer, %WPARAM* %2 - store %WPARAM %wparam, %WPARAM* %2 - %3 = alloca %LPARAM, align 8 ; lparam - store %LPARAM zeroinitializer, %LPARAM* %3 - store %LPARAM %lparam, %LPARAM* %3 - %4 = load i32, i32* %1, align 4 - %5 = icmp eq i32 %4, 2 - br i1 %5, label %if.then.-.1, label %cmp-or.-.3 - -if.then.-.1: - call void @ExitProcess(i32 0) - ret %LRESULT 0 - -cmp-or.-.2: - %6 = load i32, i32* %1, align 4 - %7 = icmp eq i32 %6, 18 - br i1 %7, label %if.then.-.1, label %if.done.-.4 - -cmp-or.-.3: - %8 = load i32, i32* %1, align 4 - %9 = icmp eq i32 %8, 16 - br i1 %9, label %if.then.-.1, label %cmp-or.-.2 - -if.done.-.4: - %10 = load %HWND, %HWND* %0, align 8 - %11 = load i32, i32* %1, align 4 - %12 = load %WPARAM, %WPARAM* %2, align 8 - %13 = load %LPARAM, %LPARAM* %3, align 8 - %14 = call %LRESULT @DefWindowProcA(%HWND %10, i32 %11, %WPARAM %12, %LPARAM %13) - ret i64 %14 } define void @print_string(%.string %s) { @@ -700,7 +477,7 @@ for.body.-.5: %16 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 0 %17 = load i64, i64* %3, align 8 %18 = getelementptr i8, i8* %16, i64 %17 - %19 = getelementptr inbounds [64 x i8], [64 x i8]* @.str5, i64 0, i64 0 + %19 = getelementptr inbounds [64 x i8], [64 x i8]* @.str2, i64 0, i64 0 %20 = load i64, i64* %1, align 8 %21 = load i64, i64* %0, align 8 %22 = srem i64 %21, %20 @@ -842,7 +619,7 @@ for.body.-.5: %16 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 0 %17 = load i64, i64* %3, align 8 %18 = getelementptr i8, i8* %16, i64 %17 - %19 = getelementptr inbounds [64 x i8], [64 x i8]* @.str6, i64 0, i64 0 + %19 = getelementptr inbounds [64 x i8], [64 x i8]* @.str3, i64 0, i64 0 %20 = load i64, i64* %1, align 8 %21 = load i64, i64* %0, align 8 %22 = urem i64 %21, %20 @@ -934,7 +711,7 @@ entry.-.0: br i1 %1, label %if.then.-.1, label %if.else.-.2 if.then.-.1: - %2 = getelementptr inbounds [4 x i8], [4 x i8]* @.str7, i64 0, i64 0 + %2 = getelementptr inbounds [4 x i8], [4 x i8]* @.str4, i64 0, i64 0 %3 = alloca %.string, align 8 store %.string zeroinitializer, %.string* %3 %4 = getelementptr inbounds %.string, %.string* %3, i64 0, i32 0 @@ -946,7 +723,7 @@ if.then.-.1: br label %if.done.-.3 if.else.-.2: - %7 = getelementptr inbounds [5 x i8], [5 x i8]* @.str8, i64 0, i64 0 + %7 = getelementptr inbounds [5 x i8], [5 x i8]* @.str5, i64 0, i64 0 %8 = alloca %.string, align 8 store %.string zeroinitializer, %.string* %8 %9 = getelementptr inbounds %.string, %.string* %8, i64 0, i32 0 @@ -1239,10 +1016,15 @@ entry.-.0: @.str0 = global [14 x i8] c"GetLastError\3A\20" @.str1 = global [1 x i8] c"\0A" -@.str2 = global [18 x i8] c"Odin-Language-Demo" -@.str3 = global [18 x i8] c"Odin\20Language\20Demo" -@.str4 = global [6 x i8] c"Tick\3A\20" -@.str5 = global [64 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\40$" -@.str6 = global [64 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\40$" -@.str7 = global [4 x i8] c"true" -@.str8 = global [5 x i8] c"false" +@.str2 = global [64 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\40$" +@.str3 = global [64 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\40$" +@.str4 = global [4 x i8] c"true" +@.str5 = global [5 x i8] c"false" +define void @__$startup_runtime() { +entry.-.0: + %0 = call i64 @win32_get_perf_count_freq() + store i64 1, i64* @constant + store i64 %0, i64* @win32_perf_count_freq + ret void +} + diff --git a/examples/main.odin b/examples/main.odin index e0e479e45..3f9cdfa40 100644 --- a/examples/main.odin +++ b/examples/main.odin @@ -1,7 +1,15 @@ #load "basic.odin" #load "win32.odin" -win32_perf_count_freq: i64; +constant := 1; + +win32_perf_count_freq: i64 = win32_get_perf_count_freq(); +win32_get_perf_count_freq :: proc() -> i64 { + r: i64; + _ = QueryPerformanceFrequency(^r); + return r; +} + time_now :: proc() -> f64 { if win32_perf_count_freq == 0 { @@ -25,6 +33,7 @@ win32_print_last_error :: proc() { } main :: proc() { +/* wc: WNDCLASSEXA; instance := GetModuleHandleA(null); @@ -105,4 +114,5 @@ main :: proc() { sleep_ms(16); } +*/ } diff --git a/src/checker/expr.cpp b/src/checker/expr.cpp index ded60103c..f5b3d2158 100644 --- a/src/checker/expr.cpp +++ b/src/checker/expr.cpp @@ -1734,18 +1734,6 @@ ExpressionKind check_call_expr(Checker *c, Operand *operand, AstNode *call) { return Expression_Statement; } - if (curr_procedure(c) == NULL) { - AstNode *e = operand->expr; - gbString str = expr_to_string(e); - defer (gb_string_free(str)); - error(&c->error_collector, ast_node_token(e), "Can ony call procedure within a procedure: `%s`", str); - - operand->mode = Addressing_Invalid; - operand->expr = call; - - return Expression_Statement; - } - check_call_arguments(c, operand, proc_type, call); auto *proc = &proc_type->proc; diff --git a/src/codegen/codegen.cpp b/src/codegen/codegen.cpp index 4b9d943c4..15afec217 100644 --- a/src/codegen/codegen.cpp +++ b/src/codegen/codegen.cpp @@ -39,6 +39,11 @@ void ssa_gen_destroy(ssaGen *s) { gb_file_close(&s->output_file); } +struct ssaGlobalVariable { + ssaValue *var, *init; + DeclInfo *decl; +}; + void ssa_gen_code(ssaGen *s) { if (v_zero == NULL) { v_zero = ssa_make_value_constant(gb_heap_allocator(), t_int, make_exact_value_integer(0)); @@ -53,8 +58,9 @@ void ssa_gen_code(ssaGen *s) { ssaModule *m = &s->module; CheckerInfo *info = m->info; gbAllocator a = m->allocator; - ssaProcedure dummy_proc = {}; - dummy_proc.module = m; + gbArray(ssaGlobalVariable) global_variables; + gb_array_init(global_variables, gb_heap_allocator()); + defer (gb_array_free(global_variables)); gb_for_array(i, info->entities.entries) { auto *entry = &info->entities.entries[i]; @@ -71,15 +77,12 @@ void ssa_gen_code(ssaGen *s) { } break; case Entity_Variable: { - // ssaValue *value = ssa_build_expr(&dummy_proc, decl->init_expr); - // if (value->kind == ssaValue_Instr) { - // ssaInstr *i = &value->instr; - // if (i->kind == ssaInstr_Load) { - // value = i->load.address; - // } - // } // TODO(bill): global runtime initialization ssaValue *g = ssa_make_value_global(a, e, NULL); + ssaGlobalVariable var = {}; + var.var = g; + var.decl = decl; + gb_array_append(global_variables, var); map_set(&m->values, hash_pointer(e), g); map_set(&m->members, hash_string(name), g); } break; @@ -107,6 +110,58 @@ void ssa_gen_code(ssaGen *s) { ssa_build_proc(v, NULL); } + + + { // Startup Runtime + // Cleanup(bill): probably better way of doing code insertion + String name = make_string(SSA_STARTUP_RUNTIME_PROC_NAME); + Type *proc_type = make_type_proc(a, gb_alloc_item(a, Scope), + NULL, 0, + NULL, 0); + AstNode *body = gb_alloc_item(a, AstNode); + ssaValue *p = ssa_make_value_procedure(a, m, proc_type, NULL, body, name); + Token token = {}; + token.string = name; + Entity *e = make_entity_procedure(a, NULL, token, proc_type); + + map_set(&m->values, hash_pointer(e), p); + map_set(&m->members, hash_string(name), p); + + ssaProcedure *proc = &p->proc; + + ssa_begin_procedure_body(proc); + + // TODO(bill): Should do a dependency graph do check which order to initialize them in? + gb_for_array(i, global_variables) { + ssaGlobalVariable *var = &global_variables[i]; + if (var->decl->init_expr != NULL) { + var->init = ssa_build_expr(proc, var->decl->init_expr); + } + } + + // NOTE(bill): Initialize constants first + gb_for_array(i, global_variables) { + ssaGlobalVariable *var = &global_variables[i]; + if (var->init != NULL) { + if (var->init->kind == ssaValue_Constant) { + ssa_emit_store(proc, var->var, var->init); + } + } + } + + gb_for_array(i, global_variables) { + ssaGlobalVariable *var = &global_variables[i]; + if (var->init != NULL) { + if (var->init->kind != ssaValue_Constant) { + ssa_emit_store(proc, var->var, var->init); + } + } + } + + ssa_end_procedure_body(proc); + } + + // m->layout = make_string("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"); ssa_print_llvm_ir(&s->output_file, &s->module); diff --git a/src/codegen/print_llvm.cpp b/src/codegen/print_llvm.cpp index ae2ef5fb1..0f145d408 100644 --- a/src/codegen/print_llvm.cpp +++ b/src/codegen/print_llvm.cpp @@ -275,6 +275,10 @@ void ssa_print_instr(gbFile *f, ssaModule *m, ssaValue *value) { ssa_fprintf(f, "\t"); switch (instr->kind) { + case ssaInstr_StartupRuntime: { + ssa_fprintf(f, "call void @" SSA_STARTUP_RUNTIME_PROC_NAME "()\n"); + } break; + case ssaInstr_Local: { Type *type = instr->local.entity->type; ssa_fprintf(f, "%%%d = alloca ", value->id); diff --git a/src/codegen/ssa.cpp b/src/codegen/ssa.cpp index 3df8cbd81..7a0c587c5 100644 --- a/src/codegen/ssa.cpp +++ b/src/codegen/ssa.cpp @@ -53,6 +53,7 @@ struct ssaProcedure { ssaTargetList * target_list; }; +#define SSA_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime" #define SSA_INSTR_KINDS \ @@ -73,6 +74,7 @@ struct ssaProcedure { SSA_INSTR_KIND(ExtractElement), \ SSA_INSTR_KIND(InsertElement), \ SSA_INSTR_KIND(ShuffleVector), \ + SSA_INSTR_KIND(StartupRuntime), \ SSA_INSTR_KIND(Count), enum ssaInstrKind { @@ -192,6 +194,8 @@ struct ssaInstr { ssaValue *elem; ssaValue *index; } insert_element; + + struct {} startup_runtime; }; }; @@ -890,6 +894,7 @@ void ssa_end_procedure_body(ssaProcedure *proc) { case ssaInstr_Ret: case ssaInstr_Unreachable: case ssaInstr_CopyMemory: + case ssaInstr_StartupRuntime: continue; case ssaInstr_Call: if (instr->call.type == NULL) { @@ -2340,6 +2345,26 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { } } + +void ssa_emit_startup_runtime(ssaProcedure *proc) { + GB_ASSERT(proc->parent == NULL && are_strings_equal(proc->name, make_string("main"))); + + ssaValue *v = ssa_alloc_instr(proc->module->allocator, ssaInstr_StartupRuntime); + if (proc->curr_block) { + gb_array_append(proc->curr_block->values, v); + } + ssa_emit(proc, v); +} + +void ssa_insert_code_before_proc(ssaProcedure* proc, ssaProcedure *parent) { + if (parent == NULL) { + if (are_strings_equal(proc->name, make_string("main"))) { + ssa_emit_startup_runtime(proc); + } + } +} + + void ssa_build_proc(ssaValue *value, ssaProcedure *parent) { ssaProcedure *proc = &value->proc; @@ -2347,6 +2372,7 @@ void ssa_build_proc(ssaValue *value, ssaProcedure *parent) { if (proc->body != NULL) { ssa_begin_procedure_body(proc); + ssa_insert_code_before_proc(proc, parent); ssa_build_stmt(proc, proc->body); ssa_end_procedure_body(proc); }