From 0dd17fe84308bba09c6d2ba15f8b8504bbcf5c89 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 15 Aug 2016 21:22:44 +0100 Subject: [PATCH] Win32 Window Test --- examples/basic.odin | 57 +--- examples/main.ll | 682 +++++++++++++++++++++++++++---------- examples/main.odin | 141 +++++--- examples/runtime.odin | 52 +++ examples/win32.odin | 103 ++++++ run.bat | 8 +- src/checker/checker.cpp | 9 +- src/checker/expr.cpp | 38 +-- src/checker/stmt.cpp | 2 +- src/codegen/codegen.cpp | 19 +- src/codegen/print_llvm.cpp | 92 +++-- src/codegen/ssa.cpp | 35 +- src/parser.cpp | 201 +++++------ src/tokenizer.cpp | 1 + 14 files changed, 989 insertions(+), 451 deletions(-) create mode 100644 examples/runtime.odin create mode 100644 examples/win32.odin diff --git a/examples/basic.odin b/examples/basic.odin index 12418553a..7d5a70f95 100644 --- a/examples/basic.odin +++ b/examples/basic.odin @@ -1,15 +1,6 @@ -// CRT -putchar :: proc(c: i32) -> i32 #foreign - -heap_alloc :: proc(sz: int) -> rawptr #foreign "malloc" -heap_free :: proc(ptr: rawptr) #foreign "free" - -mem_compare :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memcmp" -mem_copy :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memcpy" -mem_move :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memmove" - -debug_trap :: proc() #foreign "llvm.debugtrap" +#load "runtime.odin" +TWO_HEARTS :: '💕'; print_string :: proc(s: string) { for i := 0; i < len(s); i++ { @@ -137,47 +128,3 @@ print_bool :: proc(b : bool) { } } - - -// Runtime procedures - -__string_eq :: proc(a, b : string) -> bool { - if len(a) != len(b) { - return false; - } - if ^a[0] == ^b[0] { - return true; - } - return mem_compare(^a[0], ^b[0], len(a)) == 0; -} - -__string_ne :: proc(a, b : string) -> bool { - return !__string_eq(a, b); -} - -__string_cmp :: proc(a, b : string) -> int { - min_len := len(a); - if len(b) < min_len { - min_len = len(b); - } - for i := 0; i < min_len; i++ { - x := a[i]; - y := b[i]; - if x < y { - return -1; - } else if x > y { - return +1; - } - } - if len(a) < len(b) { - return -1; - } else if len(a) > len(b) { - return +1; - } - return 0; -} - -__string_lt :: proc(a, b : string) -> bool { return __string_cmp(a, b) < 0; } -__string_gt :: proc(a, b : string) -> bool { return __string_cmp(a, b) > 0; } -__string_le :: proc(a, b : string) -> bool { return __string_cmp(a, b) <= 0; } -__string_ge :: proc(a, b : string) -> bool { return __string_cmp(a, b) >= 0; } diff --git a/examples/main.ll b/examples/main.ll index 85637b0a6..670239df7 100644 --- a/examples/main.ll +++ b/examples/main.ll @@ -1,56 +1,330 @@ %.string = type {i8*, i64} ; Basic_string %.rawptr = type i8* ; Basic_rawptr +%HANDLE = type %.rawptr +%HWND = type %.rawptr +%HDC = type %.rawptr +%HINSTANCE = type %.rawptr +%HICON = type %.rawptr +%HCURSOR = type %.rawptr +%HMENU = type %.rawptr +%HBRUSH = type %.rawptr +%WPARAM = type i64 +%LPARAM = type i64 +%LRESULT = type i64 +%ATOM = type i16 +%POINT = type {i32, i32} +%BOOL = type i32 +%WNDPROC = type %LRESULT (%HWND, i32, %WPARAM, %LPARAM)* +%WNDCLASSEXA = type {i32, i32, %WNDPROC, i32, i32, %HINSTANCE, %HICON, %HCURSOR, %HBRUSH, i8*, i8*, %HICON} +%MSG = type {%HWND, i32, %WPARAM, %LPARAM, i32, %POINT} declare void @llvm.memmove.p0i8.p0i8.i64(i8*, i8*, i64, i32, i1) argmemonly nounwind +@win32_perf_count_freq = global i64 zeroinitializer +define double @time_now() { +entry.-.0: + %0 = load i64, i64* @win32_perf_count_freq, align 8 + %1 = icmp eq i64 %0, 0 + br i1 %1, label %if.then.-.1, label %if.done.-.2 + +if.then.-.1: + call void @llvm.debugtrap() + br label %if.done.-.2 + +if.done.-.2: + %2 = alloca i64, align 8 ; counter + store i64 zeroinitializer, i64* %2 + %3 = getelementptr inbounds i64, i64* %2 + %4 = call i32 @QueryPerformanceCounter(i64* %3) + %5 = alloca double, align 8 ; result + store double zeroinitializer, double* %5 + %6 = load i64, i64* @win32_perf_count_freq, align 8 + %7 = sitofp i64 %6 to double + %8 = load i64, i64* %2, align 8 + %9 = sitofp i64 %8 to double + %10 = fdiv double %9, %7 + store double %10, double* %5 + %11 = load double, double* %5, align 8 + ret double %11 +} + +define void @win32_print_last_error() { +entry.-.0: + %0 = alloca i64, align 8 ; err_code + store i64 zeroinitializer, i64* %0 + %1 = call i32 @GetLastError() + %2 = zext i32 %1 to i64 + store i64 %2, i64* %0 + %3 = load i64, i64* %0, align 8 + %4 = icmp ne i64 %3, 0 + br i1 %4, label %if.then.-.1, label %if.done.-.2 + +if.then.-.1: + %5 = getelementptr inbounds [14 x i8], [14 x i8]* @.str0, i64 0, i64 0 + %6 = alloca %.string, align 8 + store %.string zeroinitializer, %.string* %6 + %7 = getelementptr inbounds %.string, %.string* %6, i64 0, i32 0 + %8 = getelementptr inbounds %.string, %.string* %6, i64 0, i32 1 + store i8* %5, i8** %7 + store i64 14, i64* %8 + %9 = load %.string, %.string* %6, align 8 + call void @print_string(%.string %9) + %10 = load i64, i64* %0, align 8 + call void @print_int(i64 %10) + %11 = getelementptr inbounds [1 x i8], [1 x i8]* @.str1, i64 0, i64 0 + %12 = alloca %.string, align 8 + store %.string zeroinitializer, %.string* %12 + %13 = getelementptr inbounds %.string, %.string* %12, i64 0, i32 0 + %14 = getelementptr inbounds %.string, %.string* %12, i64 0, i32 1 + store i8* %11, i8** %13 + store i64 1, i64* %14 + %15 = load %.string, %.string* %12, align 8 + call void @print_string(%.string %15) + br label %if.done.-.2 + +if.done.-.2: + ret void +} + define void @main() { entry.-.0: - %0 = getelementptr inbounds [8 x i8], [8 x i8]* @.str1, i64 0, i64 0 - %1 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %1 - %2 = getelementptr inbounds %.string, %.string* %1, i64 0, i32 0 - %3 = getelementptr inbounds %.string, %.string* %1, i64 0, i32 1 - store i8* %0, i8** %2 - store i64 8, i64* %3 - %4 = load %.string, %.string* %1, align 8 - call void @print_string(%.string %4) - call void @"main$\E4\B8\96\E7\95\8C-1"() - ret void -} + %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 -define void @main$nl-0() { -entry.-.0: +if.then.-.1: + 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 void @"main$\E4\B8\96\E7\95\8C-1"() { +define i8* @main$to_c_string-0(%.string %s) { entry.-.0: - %0 = getelementptr inbounds [9 x i8], [9 x i8]* @.str0, i64 0, i64 0 - %1 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %1 - %2 = getelementptr inbounds %.string, %.string* %1, i64 0, i32 0 - %3 = getelementptr inbounds %.string, %.string* %1, i64 0, i32 1 - store i8* %0, i8** %2 - store i64 9, i64* %3 - %4 = load %.string, %.string* %1, align 8 - call void @print_string(%.string %4) - ret void + %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 } -declare i32 @putchar(i32 %c) ; foreign procedure +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 -declare %.rawptr @malloc(i64 %sz) ; foreign procedure +if.then.-.1: + call void @ExitProcess(i32 0) + ret %LRESULT 0 -declare void @free(%.rawptr %ptr) ; foreign procedure +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 -declare i32 @memcmp(%.rawptr %dst, %.rawptr %src, i64 %len) ; foreign procedure +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 -declare i32 @memcpy(%.rawptr %dst, %.rawptr %src, i64 %len) ; foreign procedure - -declare i32 @memmove(%.rawptr %dst, %.rawptr %src, i64 %len) ; foreign procedure - -declare void @llvm.debugtrap() ; foreign procedure +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) { entry.-.0: @@ -63,30 +337,30 @@ for.init.-.1: %1 = alloca i64, align 8 ; i store i64 zeroinitializer, i64* %1 store i64 0, i64* %1 - br label %for.loop.-.2 + br label %for.loop.-.3 -for.loop.-.2: - %2 = load i64, i64* %1, align 8 - %3 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 1 - %4 = load i64, i64* %3, align 8 - %5 = icmp slt i64 %2, %4 - br i1 %5, label %for.body.-.4, label %for.done.-.5 +for.body.-.2: + %2 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 0 + %3 = load i8*, i8** %2, align 8 + %4 = load i64, i64* %1, align 8 + %5 = getelementptr i8, i8* %3, i64 %4 + %6 = load i8, i8* %5, align 1 + %7 = zext i8 %6 to i32 + %8 = call i32 @putchar(i32 %7) + br label %for.post.-.4 -for.post.-.3: - %6 = load i64, i64* %1, align 8 - %7 = add i64 %6, 1 - store i64 %7, i64* %1 - br label %for.loop.-.2 +for.loop.-.3: + %9 = load i64, i64* %1, align 8 + %10 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 1 + %11 = load i64, i64* %10, align 8 + %12 = icmp slt i64 %9, %11 + br i1 %12, label %for.body.-.2, label %for.done.-.5 -for.body.-.4: - %8 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 0 - %9 = load i8*, i8** %8, align 8 - %10 = load i64, i64* %1, align 8 - %11 = getelementptr i8, i8* %9, i64 %10 - %12 = load i8, i8* %11, align 1 - %13 = zext i8 %12 to i32 - %14 = call i32 @putchar(i32 %13) - br label %for.post.-.3 +for.post.-.4: + %13 = load i64, i64* %1, align 8 + %14 = add i64 %13, 1 + store i64 %14, i64* %1 + br label %for.loop.-.3 for.done.-.5: ret void @@ -108,49 +382,49 @@ for.init.-.1: %4 = alloca i64, align 8 ; i store i64 zeroinitializer, i64* %4 store i64 0, i64* %4 - br label %for.loop.-.2 + br label %for.loop.-.3 -for.loop.-.2: - %5 = load i64, i64* %4, align 8 - %6 = load i64, i64* %1, align 8 - %7 = sdiv i64 %6, 2 - %8 = icmp slt i64 %5, %7 - br i1 %8, label %for.body.-.4, label %for.done.-.5 +for.body.-.2: + %5 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 + %6 = load i8*, i8** %5, align 8 + %7 = load i64, i64* %4, align 8 + %8 = getelementptr i8, i8* %6, i64 %7 + %9 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 + %10 = load i8*, i8** %9, align 8 + %11 = load i64, i64* %4, align 8 + %12 = load i64, i64* %1, align 8 + %13 = sub i64 %12, 1 + %14 = sub i64 %13, %11 + %15 = getelementptr i8, i8* %10, i64 %14 + %16 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 + %17 = load i8*, i8** %16, align 8 + %18 = load i64, i64* %4, align 8 + %19 = load i64, i64* %1, align 8 + %20 = sub i64 %19, 1 + %21 = sub i64 %20, %18 + %22 = getelementptr i8, i8* %17, i64 %21 + %23 = load i8, i8* %22, align 1 + %24 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 + %25 = load i8*, i8** %24, align 8 + %26 = load i64, i64* %4, align 8 + %27 = getelementptr i8, i8* %25, i64 %26 + %28 = load i8, i8* %27, align 1 + store i8 %23, i8* %8 + store i8 %28, i8* %15 + br label %for.post.-.4 -for.post.-.3: - %9 = load i64, i64* %4, align 8 - %10 = add i64 %9, 1 - store i64 %10, i64* %4 - br label %for.loop.-.2 +for.loop.-.3: + %29 = load i64, i64* %4, align 8 + %30 = load i64, i64* %1, align 8 + %31 = sdiv i64 %30, 2 + %32 = icmp slt i64 %29, %31 + br i1 %32, label %for.body.-.2, label %for.done.-.5 -for.body.-.4: - %11 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 - %12 = load i8*, i8** %11, align 8 - %13 = load i64, i64* %4, align 8 - %14 = getelementptr i8, i8* %12, i64 %13 - %15 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 - %16 = load i8*, i8** %15, align 8 - %17 = load i64, i64* %4, align 8 - %18 = load i64, i64* %1, align 8 - %19 = sub i64 %18, 1 - %20 = sub i64 %19, %17 - %21 = getelementptr i8, i8* %16, i64 %20 - %22 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 - %23 = load i8*, i8** %22, align 8 - %24 = load i64, i64* %4, align 8 - %25 = load i64, i64* %1, align 8 - %26 = sub i64 %25, 1 - %27 = sub i64 %26, %24 - %28 = getelementptr i8, i8* %23, i64 %27 - %29 = load i8, i8* %28, align 1 - %30 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 - %31 = load i8*, i8** %30, align 8 - %32 = load i64, i64* %4, align 8 - %33 = getelementptr i8, i8* %31, i64 %32 - %34 = load i8, i8* %33, align 1 - store i8 %29, i8* %14 - store i8 %34, i8* %21 - br label %for.post.-.3 +for.post.-.4: + %33 = load i64, i64* %4, align 8 + %34 = add i64 %33, 1 + store i64 %34, i64* %4 + br label %for.loop.-.3 for.done.-.5: ret void @@ -420,32 +694,32 @@ if.then.-.3: br label %if.done.-.4 if.done.-.4: - br label %for.loop.-.5 + br label %for.loop.-.6 -for.loop.-.5: - %16 = load i64, i64* %0, align 8 - %17 = icmp sgt i64 %16, 0 - br i1 %17, label %for.body.-.6, label %for.done.-.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 + %20 = load i64, i64* %1, align 8 + %21 = load i64, i64* %0, align 8 + %22 = srem i64 %21, %20 + %23 = getelementptr i8, i8* %19, i64 %22 + %24 = load i8, i8* %23, align 1 + store i8 %24, i8* %18 + %25 = load i64, i64* %3, align 8 + %26 = add i64 %25, 1 + store i64 %26, i64* %3 + %27 = load i64, i64* %1, align 8 + %28 = load i64, i64* %0, align 8 + %29 = sdiv i64 %28, %27 + store i64 %29, i64* %0 + br label %for.loop.-.6 -for.body.-.6: - %18 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 0 - %19 = load i64, i64* %3, align 8 - %20 = getelementptr i8, i8* %18, i64 %19 - %21 = getelementptr inbounds [64 x i8], [64 x i8]* @.str2, i64 0, i64 0 - %22 = load i64, i64* %1, align 8 - %23 = load i64, i64* %0, align 8 - %24 = srem i64 %23, %22 - %25 = getelementptr i8, i8* %21, i64 %24 - %26 = load i8, i8* %25, align 1 - store i8 %26, i8* %20 - %27 = load i64, i64* %3, align 8 - %28 = add i64 %27, 1 - store i64 %28, i64* %3 - %29 = load i64, i64* %1, align 8 +for.loop.-.6: %30 = load i64, i64* %0, align 8 - %31 = sdiv i64 %30, %29 - store i64 %31, i64* %0 - br label %for.loop.-.5 + %31 = icmp sgt i64 %30, 0 + br i1 %31, label %for.body.-.5, label %for.done.-.7 for.done.-.7: %32 = load i1, i1* %4, align 1 @@ -562,32 +836,32 @@ if.then.-.3: br label %if.done.-.4 if.done.-.4: - br label %for.loop.-.5 + br label %for.loop.-.6 -for.loop.-.5: - %16 = load i64, i64* %0, align 8 - %17 = icmp ugt i64 %16, 0 - br i1 %17, label %for.body.-.6, label %for.done.-.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 + %20 = load i64, i64* %1, align 8 + %21 = load i64, i64* %0, align 8 + %22 = urem i64 %21, %20 + %23 = getelementptr i8, i8* %19, i64 %22 + %24 = load i8, i8* %23, align 1 + store i8 %24, i8* %18 + %25 = load i64, i64* %3, align 8 + %26 = add i64 %25, 1 + store i64 %26, i64* %3 + %27 = load i64, i64* %1, align 8 + %28 = load i64, i64* %0, align 8 + %29 = udiv i64 %28, %27 + store i64 %29, i64* %0 + br label %for.loop.-.6 -for.body.-.6: - %18 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 0 - %19 = load i64, i64* %3, align 8 - %20 = getelementptr i8, i8* %18, i64 %19 - %21 = getelementptr inbounds [64 x i8], [64 x i8]* @.str3, i64 0, i64 0 - %22 = load i64, i64* %1, align 8 - %23 = load i64, i64* %0, align 8 - %24 = urem i64 %23, %22 - %25 = getelementptr i8, i8* %21, i64 %24 - %26 = load i8, i8* %25, align 1 - store i8 %26, i8* %20 - %27 = load i64, i64* %3, align 8 - %28 = add i64 %27, 1 - store i64 %28, i64* %3 - %29 = load i64, i64* %1, align 8 +for.loop.-.6: %30 = load i64, i64* %0, align 8 - %31 = udiv i64 %30, %29 - store i64 %31, i64* %0 - br label %for.loop.-.5 + %31 = icmp ugt i64 %30, 0 + br i1 %31, label %for.body.-.5, label %for.done.-.7 for.done.-.7: %32 = load i1, i1* %4, align 1 @@ -660,7 +934,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]* @.str4, i64 0, i64 0 + %2 = getelementptr inbounds [4 x i8], [4 x i8]* @.str7, i64 0, i64 0 %3 = alloca %.string, align 8 store %.string zeroinitializer, %.string* %3 %4 = getelementptr inbounds %.string, %.string* %3, i64 0, i32 0 @@ -672,7 +946,7 @@ if.then.-.1: br label %if.done.-.3 if.else.-.2: - %7 = getelementptr inbounds [5 x i8], [5 x i8]* @.str5, i64 0, i64 0 + %7 = getelementptr inbounds [5 x i8], [5 x i8]* @.str8, i64 0, i64 0 %8 = alloca %.string, align 8 store %.string zeroinitializer, %.string* %8 %9 = getelementptr inbounds %.string, %.string* %8, i64 0, i32 0 @@ -687,6 +961,43 @@ if.done.-.3: ret void } +declare %HANDLE @GetStdHandle(i32 %h) ; foreign +declare i32 @CloseHandle(%HANDLE %h) ; foreign +declare i32 @WriteFileA(%HANDLE %h, %.rawptr %buf, i32 %len, i32* %written_result, %.rawptr %overlapped) ; foreign +declare i32 @GetLastError() ; foreign +declare void @ExitProcess(i32 %exit_code) ; foreign +declare %HWND @GetDesktopWindow() ; foreign +declare i32 @GetCursorPos(%POINT* %p) ; foreign +declare i32 @ScreenToClient(%HWND %h, %POINT* %p) ; foreign +declare %HINSTANCE @GetModuleHandleA(i8* %module_name) ; foreign +declare i32 @QueryPerformanceFrequency(i64* %result) ; foreign +declare i32 @QueryPerformanceCounter(i64* %result) ; foreign +define void @sleep_ms(i32 %ms) { +entry.-.0: + %0 = alloca i32, align 4 ; ms + store i32 zeroinitializer, i32* %0 + store i32 %ms, i32* %0 + %1 = load i32, i32* %0, align 4 + %2 = call i32 @Sleep(i32 %1) + ret void +} + +declare i32 @Sleep(i32 %ms) declare void @OutputDebugStringA(i8* %c_str) ; foreign +declare %ATOM @RegisterClassExA(%WNDCLASSEXA* %wc) ; foreign +declare %HWND @CreateWindowExA(i32 %ex_style, i8* %class_name, i8* %title, i32 %style, i32 %x, i32 %y, i32 %w, i32 %h, %HWND %parent, %HMENU %menu, %HINSTANCE %instance, %.rawptr %param) ; foreign +declare %BOOL @ShowWindow(%HWND %hwnd, i32 %cmd_show) ; foreign +declare %BOOL @UpdateWindow(%HWND %hwnd) ; foreign +declare %BOOL @PeekMessageA(%MSG* %msg, %HWND %hwnd, i32 %msg_filter_min, i32 %msg_filter_max, i32 %remove_msg) ; foreign +declare %BOOL @TranslateMessage(%MSG* %msg) ; foreign +declare %LRESULT @DispatchMessageA(%MSG* %msg) ; foreign +declare %LRESULT @DefWindowProcA(%HWND %hwnd, i32 %msg, %WPARAM %wparam, %LPARAM %lparam) ; foreign +declare i32 @putchar(i32 %c) ; foreign +declare %.rawptr @malloc(i64 %sz) ; foreign +declare void @free(%.rawptr %ptr) ; foreign +declare i32 @memcmp(%.rawptr %dst, %.rawptr %src, i64 %len) ; foreign +declare i32 @memcpy(%.rawptr %dst, %.rawptr %src, i64 %len) ; foreign +declare i32 @memmove(%.rawptr %dst, %.rawptr %src, i64 %len) ; foreign +declare void @llvm.debugtrap() ; foreign define i1 @__string_eq(%.string %a, %.string %b) { entry.-.0: %0 = alloca %.string, align 8 ; a @@ -783,48 +1094,48 @@ for.init.-.3: %11 = alloca i64, align 8 ; i store i64 zeroinitializer, i64* %11 store i64 0, i64* %11 - br label %for.loop.-.4 + br label %for.loop.-.5 -for.loop.-.4: - %12 = load i64, i64* %11, align 8 - %13 = load i64, i64* %2, align 8 - %14 = icmp slt i64 %12, %13 - br i1 %14, label %for.body.-.6, label %for.done.-.12 - -for.post.-.5: +for.body.-.4: + %12 = alloca i8, align 1 ; x + store i8 zeroinitializer, i8* %12 + %13 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 0 + %14 = load i8*, i8** %13, align 8 %15 = load i64, i64* %11, align 8 - %16 = add i64 %15, 1 - store i64 %16, i64* %11 - br label %for.loop.-.4 + %16 = getelementptr i8, i8* %14, i64 %15 + %17 = load i8, i8* %16, align 1 + store i8 %17, i8* %12 + %18 = alloca i8, align 1 ; y + store i8 zeroinitializer, i8* %18 + %19 = getelementptr inbounds %.string, %.string* %1, i64 0, i32 0 + %20 = load i8*, i8** %19, align 8 + %21 = load i64, i64* %11, align 8 + %22 = getelementptr i8, i8* %20, i64 %21 + %23 = load i8, i8* %22, align 1 + store i8 %23, i8* %18 + %24 = load i8, i8* %12, align 1 + %25 = load i8, i8* %18, align 1 + %26 = icmp ult i8 %24, %25 + br i1 %26, label %if.then.-.7, label %if.else.-.8 -for.body.-.6: - %17 = alloca i8, align 1 ; x - store i8 zeroinitializer, i8* %17 - %18 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 0 - %19 = load i8*, i8** %18, align 8 - %20 = load i64, i64* %11, align 8 - %21 = getelementptr i8, i8* %19, i64 %20 - %22 = load i8, i8* %21, align 1 - store i8 %22, i8* %17 - %23 = alloca i8, align 1 ; y - store i8 zeroinitializer, i8* %23 - %24 = getelementptr inbounds %.string, %.string* %1, i64 0, i32 0 - %25 = load i8*, i8** %24, align 8 - %26 = load i64, i64* %11, align 8 - %27 = getelementptr i8, i8* %25, i64 %26 - %28 = load i8, i8* %27, align 1 - store i8 %28, i8* %23 - %29 = load i8, i8* %17, align 1 - %30 = load i8, i8* %23, align 1 - %31 = icmp ult i8 %29, %30 - br i1 %31, label %if.then.-.7, label %if.else.-.8 +for.loop.-.5: + %27 = load i64, i64* %11, align 8 + %28 = load i64, i64* %2, align 8 + %29 = icmp slt i64 %27, %28 + br i1 %29, label %for.body.-.4, label %for.done.-.12 + +for.post.-.6: + %30 = load i64, i64* %11, align 8 + %31 = add i64 %30, 1 + store i64 %31, i64* %11 + br label %for.loop.-.5 if.then.-.7: ret i64 -1 if.else.-.8: - %32 = load i8, i8* %17, align 1 - %33 = load i8, i8* %23, align 1 + %32 = load i8, i8* %12, align 1 + %33 = load i8, i8* %18, align 1 %34 = icmp ugt i8 %32, %33 br i1 %34, label %if.then.-.9, label %if.done.-.10 @@ -835,7 +1146,7 @@ if.done.-.10: br label %if.done.-.11 if.done.-.11: - br label %for.post.-.5 + br label %for.post.-.6 for.done.-.12: %35 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 1 @@ -926,9 +1237,12 @@ entry.-.0: ret i1 %5 } -@.str0 = global [9 x i8] c"\E6\97\A5\E6\9C\AC\E8\AA\9E" -@.str1 = global [8 x i8] c"Hellope\0A" -@.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" +@.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" diff --git a/examples/main.odin b/examples/main.odin index 3a9566233..e0e479e45 100644 --- a/examples/main.odin +++ b/examples/main.odin @@ -1,49 +1,108 @@ #load "basic.odin" +#load "win32.odin" -TWO_HEARTS :: '💕'; +win32_perf_count_freq: i64; -main :: proc() { - nl :: proc() { print_rune('\n'); } - 世界 :: proc() { print_string(`日本語`); } - - print_string("Hellope\n"); - 世界(); - - -/* - DATA_SIZE :: 100; - data := malloc(DATA_SIZE); - - slice := (data as ^u8)[:0:DATA_SIZE]; - for i := 0; i < cap(slice); i++ { - ok := append(^slice, (i*i) as u8 % 8); +time_now :: proc() -> f64 { + if win32_perf_count_freq == 0 { + debug_trap(); } - for i := 0; i < len(slice); i++ { - print_int(slice[i] as int); - print_string(", "); - if (i+1) % 8 == 0 { - print_string("\n"); - } - } - - print_string("\n"); - free(data); -*/ + counter: i64; + _ = QueryPerformanceCounter(^counter); + result := counter as f64 / win32_perf_count_freq as f64; + return result; } -// print_hello :: proc() { -// print_string("Chinese - 你好世界\n"); -// print_string("Dutch - Hello wereld\n"); -// print_string("English - Hello world\n"); -// print_string("French - Bonjour monde\n"); -// print_string("German - Hallo Welt\n"); -// print_string("Greek - γειά σου κόσμος\n"); -// print_string("Italian - Ciao mondo\n"); -// print_string("Japanese - こんにちは世界\n"); -// print_string("Korean - 여보세요 세계\n"); -// print_string("Portuguese - Olá mundo\n"); -// print_string("Russian - Здравствулте мир\n"); -// print_string("Spanish - Hola mundo\n"); -// } +win32_print_last_error :: proc() { + err_code := GetLastError() as int; + if err_code != 0 { + print_string("GetLastError: "); + print_int(err_code); + print_string("\n"); + } +} + +main :: proc() { + wc: WNDCLASSEXA; + instance := GetModuleHandleA(null); + + // Init time info + _ = QueryPerformanceFrequency(^win32_perf_count_freq); + + // Yuck! + to_c_string :: proc(s: string) -> ^u8 { + c_str := heap_alloc(len(s)+1) as ^u8; + mem_copy(c_str, ^s[0], len(s)); + c_str[len(s)] = 0; + return c_str; + } + + class_name := to_c_string("Odin-Language-Demo"); + title := to_c_string("Odin Language Demo"); + + wc.cbSize = size_of(WNDCLASSEXA) as u32; + wc.style = CS_VREDRAW | CS_HREDRAW; + wc.hInstance = instance; + wc.className = class_name; + wc.hbrBackground = COLOR_BACKGROUND as HBRUSH; + + wc.wndProc = proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #no_inline { + if msg == WM_DESTROY || msg == WM_CLOSE || msg == WM_QUIT { + ExitProcess(0); + return 0; + } + return DefWindowProcA(hwnd, msg, wparam, lparam); + }; + + if RegisterClassExA(^wc) == 0 { + return; + } + + + hwnd := CreateWindowExA(0, + class_name, title, + WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, + 0, 0, 854, 480, + null, null, instance, null); + + + if hwnd == null { + win32_print_last_error(); + return; + } + + start_time := time_now(); + running := true; + tick_count := 0; + for running { + curr_time := time_now(); + dt := curr_time - start_time; + if dt > 2.0 { + running = false; + } + + msg: MSG; + for { + ok := PeekMessageA(^msg, null, 0, 0, PM_REMOVE) != 0; + if !ok { + break; + } + + if msg.message == WM_QUIT { + return; + } else { + _ = TranslateMessage(^msg); + _ = DispatchMessageA(^msg); + } + } + + print_string("Tick: "); + print_int(tick_count); + tick_count++; + print_rune('\n'); + + sleep_ms(16); + } +} diff --git a/examples/runtime.odin b/examples/runtime.odin new file mode 100644 index 000000000..17f6ca17b --- /dev/null +++ b/examples/runtime.odin @@ -0,0 +1,52 @@ +putchar :: proc(c: i32) -> i32 #foreign + +heap_alloc :: proc(sz: int) -> rawptr #foreign "malloc" +heap_free :: proc(ptr: rawptr) #foreign "free" + +mem_compare :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memcmp" +mem_copy :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memcpy" +mem_move :: proc(dst, src : rawptr, len: int) -> i32 #foreign "memmove" + +debug_trap :: proc() #foreign "llvm.debugtrap" + + +__string_eq :: proc(a, b : string) -> bool { + if len(a) != len(b) { + return false; + } + if ^a[0] == ^b[0] { + return true; + } + return mem_compare(^a[0], ^b[0], len(a)) == 0; +} + +__string_ne :: proc(a, b : string) -> bool { + return !__string_eq(a, b); +} + +__string_cmp :: proc(a, b : string) -> int { + min_len := len(a); + if len(b) < min_len { + min_len = len(b); + } + for i := 0; i < min_len; i++ { + x := a[i]; + y := b[i]; + if x < y { + return -1; + } else if x > y { + return +1; + } + } + if len(a) < len(b) { + return -1; + } else if len(a) > len(b) { + return +1; + } + return 0; +} + +__string_lt :: proc(a, b : string) -> bool { return __string_cmp(a, b) < 0; } +__string_gt :: proc(a, b : string) -> bool { return __string_cmp(a, b) > 0; } +__string_le :: proc(a, b : string) -> bool { return __string_cmp(a, b) <= 0; } +__string_ge :: proc(a, b : string) -> bool { return __string_cmp(a, b) >= 0; } diff --git a/examples/win32.odin b/examples/win32.odin new file mode 100644 index 000000000..14d779f4d --- /dev/null +++ b/examples/win32.odin @@ -0,0 +1,103 @@ +STD_INPUT_HANDLE :: -10; +STD_OUTPUT_HANDLE :: -11; +STD_ERROR_HANDLE :: -12; + +CS_VREDRAW :: 1; +CS_HREDRAW :: 2; +CW_USEDEFAULT :: 0x80000000; + +WS_OVERLAPPED :: 0; +WS_MAXIMIZEBOX :: 0x00010000; +WS_MINIMIZEBOX :: 0x00020000; +WS_THICKFRAME :: 0x00040000; +WS_SYSMENU :: 0x00080000; +WS_CAPTION :: 0x00C00000; +WS_VISIBLE :: 0x10000000; +WS_OVERLAPPEDWINDOW :: WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX|WS_MAXIMIZEBOX; + +WM_DESTROY :: 0x02; +WM_CLOSE :: 0x10; +WM_QUIT :: 0x12; + +PM_REMOVE :: 1; + +COLOR_BACKGROUND : rawptr : 1; // NOTE(bill): cast to HBRUSH when needed + + +type HANDLE: rawptr; +type HWND: HANDLE; +type HDC: HANDLE; +type HINSTANCE: HANDLE; +type HICON: HANDLE; +type HCURSOR: HANDLE; +type HMENU: HANDLE; +type HBRUSH: HANDLE; +type WPARAM: uint; +type LPARAM: int; +type LRESULT: int; +type ATOM: i16; +type POINT: struct { x, y: i32 } +type BOOL: i32; + +type WNDPROC: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT; + +type WNDCLASSEXA: struct { + cbSize, style: u32, + wndProc: WNDPROC, + cbClsExtra, cbWndExtra: i32, + hInstance: HINSTANCE, + hIcon: HICON, + hCursor: HCURSOR, + hbrBackground: HBRUSH, + menuName, className: ^u8, + hIconSm: HICON, +} + +type MSG: struct { + hwnd: HWND, + message: u32, + wparam: WPARAM, + lparam: LPARAM, + time: u32, + pt: POINT, +} + + +GetStdHandle :: proc(h: i32) -> HANDLE #foreign +CloseHandle :: proc(h: HANDLE) -> i32 #foreign +WriteFileA :: proc(h: HANDLE, buf: rawptr, len: i32, written_result: ^i32, overlapped: rawptr) -> i32 #foreign +GetLastError :: proc() -> i32 #foreign +ExitProcess :: proc(exit_code: u32) #foreign +GetDesktopWindow :: proc() -> HWND #foreign +GetCursorPos :: proc(p: ^POINT) -> i32 #foreign +ScreenToClient :: proc(h: HWND, p: ^POINT) -> i32 #foreign + +GetModuleHandleA :: proc(module_name: ^u8) -> HINSTANCE #foreign + +QueryPerformanceFrequency :: proc(result: ^i64) -> i32 #foreign +QueryPerformanceCounter :: proc(result: ^i64) -> i32 #foreign + +sleep_ms :: proc(ms: i32) { + Sleep :: proc(ms: i32) -> i32 #foreign + Sleep(ms); +} + +OutputDebugStringA :: proc(c_str: ^u8) #foreign + + +RegisterClassExA :: proc(wc: ^WNDCLASSEXA) -> ATOM #foreign +CreateWindowExA :: proc(ex_style: u32, + class_name, title: ^u8, + style: u32, + x, y, w, h: i32, + parent: HWND, menu: HMENU, instance: HINSTANCE, + param: rawptr) -> HWND #foreign + +ShowWindow :: proc(hwnd: HWND, cmd_show: i32) -> BOOL #foreign +UpdateWindow :: proc(hwnd: HWND) -> BOOL #foreign +PeekMessageA :: proc(msg: ^MSG, hwnd: HWND, + msg_filter_min, msg_filter_max, remove_msg: u32) -> BOOL #foreign +TranslateMessage :: proc(msg: ^MSG) -> BOOL #foreign +DispatchMessageA :: proc(msg: ^MSG) -> LRESULT #foreign + +DefWindowProcA :: proc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT #foreign diff --git a/run.bat b/run.bat index 55767d498..62818dc8c 100644 --- a/run.bat +++ b/run.bat @@ -3,9 +3,13 @@ rem call clang -c -emit-llvm -DGB_IMPLEMENTATION -DGB_DEF=GB_DLL_EXPORT ..\src\gb\gb.h +pushd ..\examples call ..\bin\odin.exe ..\examples/main.odin ^ - && ..\misc\llvm-bin\opt.exe -mem2reg ..\examples/main.ll -o ..\examples/main.bc ^ - && ..\misc\llvm-bin\lli.exe ..\examples/main.bc + && opt -mem2reg main.ll -o main.bc ^ + && clang main.bc -o main.exe ^ + -Wno-override-module -lkernel32.lib -luser32.lib ^ + && main.exe +popd rem && llvm-dis ..\examples/main.bc -o - ^ rem call ..\misc\llvm-bin\opt.exe -mem2reg ..\examples/output.ll > ..\examples/main.bc diff --git a/src/checker/checker.cpp b/src/checker/checker.cpp index 4b2ba78fc..a27a54f87 100644 --- a/src/checker/checker.cpp +++ b/src/checker/checker.cpp @@ -452,7 +452,8 @@ void add_type_and_value(CheckerInfo *i, AstNode *expression, AddressingMode mode if (mode == Addressing_Constant) { GB_ASSERT(value.kind != ExactValue_Invalid); - GB_ASSERT(type == t_invalid || is_type_constant_type(type)); + GB_ASSERT_MSG(type != t_invalid || is_type_constant_type(type), + "type: %s", type_to_string(type)); } TypeAndValue tv = {}; @@ -473,7 +474,11 @@ void add_entity(Checker *c, Scope *scope, AstNode *identifier, Entity *entity) { if (!are_strings_equal(entity->token.string, make_string("_"))) { Entity *insert_entity = scope_insert_entity(scope, entity); if (insert_entity) { - error(&c->error_collector, entity->token, "Redeclared entity in this scope: %.*s", LIT(entity->token.string)); + error(&c->error_collector, entity->token, + "Redeclararation of `%.*s` in this scope\n" + "\tat %.*s(%td:%td)", + LIT(entity->token.string), + LIT(entity->token.pos.file), entity->token.pos.line, entity->token.pos.column); return; } } diff --git a/src/checker/expr.cpp b/src/checker/expr.cpp index 204d27819..f7c380603 100644 --- a/src/checker/expr.cpp +++ b/src/checker/expr.cpp @@ -843,19 +843,19 @@ b32 check_castable_to(Checker *c, Operand *operand, Type *y) { return true; } - // untyped integers -> pointers - if (is_type_untyped(xb) && is_type_integer(xb)) { - if (is_type_pointer(yb)) - return true; - } + // // untyped integers -> pointers + // if (is_type_untyped(xb) && is_type_integer(xb)) { + // if (is_type_pointer(yb)) + // return true; + // } // (u)int <-> pointer - if (is_type_pointer(xb) || is_type_int_or_uint(xb)) { + if (is_type_pointer(xb) || (is_type_int_or_uint(xb) && !is_type_untyped(xb))) { if (is_type_pointer(yb)) return true; } if (is_type_pointer(xb)) { - if (is_type_pointer(yb) || is_type_int_or_uint(yb)) + if (is_type_pointer(yb) || (is_type_int_or_uint(yb) && !is_type_untyped(yb))) return true; } @@ -888,9 +888,9 @@ void check_binary_expr(Checker *c, Operand *x, AstNode *node) { b32 can_convert = false; if (is_const_expr && is_type_constant_type(type)) { - Type *t = get_base_type(type); - if (t->kind == Type_Basic) { - if (check_value_is_expressible(c, x->value, t, &x->value)) { + Type *base_type = get_base_type(type); + if (base_type->kind == Type_Basic) { + if (check_value_is_expressible(c, x->value, base_type, &x->value)) { can_convert = true; } } @@ -1807,7 +1807,7 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ case Token_Float: t = t_untyped_float; break; case Token_String: t = t_untyped_string; break; case Token_Rune: t = t_untyped_rune; break; - default: GB_PANIC("Unknown literal"); break; + default: GB_PANIC("Unknown literal"); break; } o->mode = Addressing_Constant; o->type = t; @@ -1815,16 +1815,16 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ case_end; case_ast_node(pl, ProcLit, node); + auto curr_context = c->context; + c->context.scope = c->global_scope; + check_open_scope(c, pl->type); + c->context.decl = make_declaration_info(c->allocator, c->context.scope); + defer ({ + check_close_scope(c); + c->context = curr_context; + }); Type *proc_type = check_type(c, pl->type); if (proc_type != NULL) { - auto context = c->context; - c->context.scope = c->global_scope; - check_open_scope(c, pl->type); - c->context.decl = make_declaration_info(c->allocator, c->context.scope); - defer ({ - c->context = context; - check_close_scope(c); - }); check_proc_body(c, empty_token, c->context.decl, proc_type, pl->body); o->mode = Addressing_Value; o->type = proc_type; diff --git a/src/checker/stmt.cpp b/src/checker/stmt.cpp index 9b127b7aa..2fbe8e1d3 100644 --- a/src/checker/stmt.cpp +++ b/src/checker/stmt.cpp @@ -351,7 +351,7 @@ void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init_e e->type = t; } - Operand operand = {Addressing_Invalid}; + Operand operand = {}; if (init_expr) check_expr(c, &operand, init_expr); check_init_constant(c, e, &operand); diff --git a/src/codegen/codegen.cpp b/src/codegen/codegen.cpp index 43957ba76..4b9d943c4 100644 --- a/src/codegen/codegen.cpp +++ b/src/codegen/codegen.cpp @@ -71,14 +71,15 @@ 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; - } - } - ssaValue *g = ssa_make_value_global(a, e, value); + // 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); map_set(&m->values, hash_pointer(e), g); map_set(&m->members, hash_string(name), g); } break; @@ -91,6 +92,8 @@ void ssa_gen_code(ssaGen *s) { name = pd->foreign_name; } ssaValue *p = ssa_make_value_procedure(a, m, e->type, decl->type_expr, body, name); + p->proc.tags = pd->tags; + map_set(&m->values, hash_pointer(e), p); map_set(&m->members, hash_string(name), p); } break; diff --git a/src/codegen/print_llvm.cpp b/src/codegen/print_llvm.cpp index 97cf168b9..ae2ef5fb1 100644 --- a/src/codegen/print_llvm.cpp +++ b/src/codegen/print_llvm.cpp @@ -174,21 +174,22 @@ void ssa_print_type(gbFile *f, BaseTypeSizes s, Type *t) { ssa_fprintf(f, "}"); } break; - case Type_Proc: + case Type_Proc: { if (t->proc.result_count == 0) { ssa_fprintf(f, "void"); } else { ssa_print_type(f, s, t->proc.results); } ssa_fprintf(f, " ("); + auto *params = &t->proc.params->tuple; for (isize i = 0; i < t->proc.param_count; i++) { if (i > 0) { ssa_fprintf(f, ", "); } - ssa_print_type(f, s, &t->proc.params[i]); + ssa_print_type(f, s, params->variables[i]->type); } ssa_fprintf(f, ")*"); - break; + } break; } } @@ -208,9 +209,9 @@ void ssa_print_exact_value(gbFile *f, ssaModule *m, ExactValue value, Type *type ssa_print_escape_string(f, value.value_string, false); ssa_fprintf(f, "\""); } break; - case ExactValue_Integer: + case ExactValue_Integer: { ssa_fprintf(f, "%lld", value.value_integer); - break; + } break; case ExactValue_Float: { u64 u = *cast(u64*)&value.value_float; if (is_type_float(type) && type->basic.kind == Basic_f32) { @@ -506,7 +507,7 @@ void ssa_print_instr(gbFile *f, ssaModule *m, ssaValue *value) { case ssaInstr_Call: { auto *call = &instr->call; - Type *result_type = call->type->proc.results; + Type *result_type = call->type; if (result_type) { ssa_fprintf(f, "%%%d = ", value->id); } @@ -521,18 +522,22 @@ void ssa_print_instr(gbFile *f, ssaModule *m, ssaValue *value) { ssa_fprintf(f, "("); - auto *params = &call->type->proc.params->tuple; - for (isize i = 0; i < call->arg_count; i++) { - Entity *e = params->variables[i]; - GB_ASSERT(e != NULL); - Type *t = e->type; - if (i > 0) { - ssa_fprintf(f, ", "); + if (call->arg_count > 0) { + Type *proc_type = get_base_type(ssa_value_type(call->value)); + GB_ASSERT(proc_type->kind == Type_Proc); + auto *params = &proc_type->proc.params->tuple; + for (isize i = 0; i < call->arg_count; i++) { + Entity *e = params->variables[i]; + GB_ASSERT(e != NULL); + Type *t = e->type; + if (i > 0) { + ssa_fprintf(f, ", "); + } + ssa_print_type(f, m->sizes, t); + ssa_fprintf(f, " "); + ssaValue *arg = call->args[i]; + ssa_print_value(f, m, arg, t); } - ssa_print_type(f, m->sizes, t); - ssa_fprintf(f, " "); - ssaValue *arg = call->args[i]; - ssa_print_value(f, m, arg, t); } ssa_fprintf(f, ")\n"); @@ -650,9 +655,18 @@ void ssa_print_proc(gbFile *f, ssaModule *m, ssaProcedure *proc) { ssa_fprintf(f, ") "); - if (proc->body == NULL) { - ssa_fprintf(f, "; foreign procedure\n\n"); - } else { + if (proc->tags != 0) { + if (proc->tags & ProcTag_inline) + ssa_fprintf(f, "alwaysinline "); + if (proc->tags & ProcTag_no_inline) + ssa_fprintf(f, "noinline "); + + + if (proc->tags & ProcTag_foreign) + ssa_fprintf(f, "; foreign\n"); + } + + if (proc->body != NULL) { ssa_fprintf(f, "{\n"); gb_for_array(i, proc->blocks) { ssaBlock *block = proc->blocks[i]; @@ -682,6 +696,7 @@ void ssa_print_type_name(gbFile *f, ssaModule *m, ssaValue *v) { ssa_fprintf(f, "\n"); } + void ssa_print_llvm_ir(gbFile *f, ssaModule *m) { if (m->layout.len > 0) { ssa_fprintf(f, "target datalayout = \"%.*s\"\n", LIT(m->layout)); @@ -695,17 +710,6 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) { ssa_print_encoded_local(f, make_string(".rawptr")); ssa_fprintf(f, " = type i8* ; Basic_rawptr\n\n"); - ssa_fprintf(f, "declare void @llvm.memmove.p0i8.p0i8."); - ssa_print_type(f, m->sizes, t_int); - ssa_fprintf(f, "(i8*, i8*, "); - ssa_print_type(f, m->sizes, t_int); - ssa_fprintf(f, ", i32, i1) argmemonly nounwind \n\n"); - - gb_for_array(i, m->nested_type_names) { - ssaValue *v = m->nested_type_names[i]; - ssa_print_type_name(f, m, v); - } - gb_for_array(member_index, m->members.entries) { auto *entry = &m->members.entries[member_index]; ssaValue *v = entry->value; @@ -716,7 +720,27 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) { ssa_print_type(f, m->sizes, get_base_type(v->type_name.type)); ssa_fprintf(f, "\n"); } break; + } + } + gb_for_array(i, m->nested_type_names) { + ssaValue *v = m->nested_type_names[i]; + ssa_print_type_name(f, m, v); + } + + + ssa_fprintf(f, "declare void @llvm.memmove.p0i8.p0i8."); + ssa_print_type(f, m->sizes, t_int); + ssa_fprintf(f, "(i8*, i8*, "); + ssa_print_type(f, m->sizes, t_int); + ssa_fprintf(f, ", i32, i1) argmemonly nounwind \n\n"); + + + + gb_for_array(member_index, m->members.entries) { + auto *entry = &m->members.entries[member_index]; + ssaValue *v = entry->value; + switch (v->kind) { case ssaValue_Global: { auto *g = &v->global; ssa_print_encoded_global(f, g->entity->token.string); @@ -729,7 +753,11 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) { ssa_print_type(f, m->sizes, get_base_type(g->entity->type)); ssa_fprintf(f, " "); - ssa_print_value(f, m, g->value, g->entity->type); + if (g->value != NULL) { + ssa_print_value(f, m, g->value, g->entity->type); + } else { + ssa_fprintf(f, "zeroinitializer"); + } ssa_fprintf(f, "\n"); } break; diff --git a/src/codegen/ssa.cpp b/src/codegen/ssa.cpp index 1ccbcf5d6..3df8cbd81 100644 --- a/src/codegen/ssa.cpp +++ b/src/codegen/ssa.cpp @@ -46,6 +46,7 @@ struct ssaProcedure { Type * type; AstNode * type_expr; AstNode * body; + u64 tags; gbArray(ssaBlock *) blocks; ssaBlock * curr_block; @@ -318,13 +319,13 @@ Type *ssa_instr_type(ssaInstr *instr) { case ssaInstr_Select: return ssa_value_type(instr->select.true_value); case ssaInstr_Call: { - Type *pt = instr->call.type; - GB_ASSERT(pt->kind == Type_Proc); - auto *tuple = &pt->proc.results->tuple; - if (tuple->variable_count != 1) - return pt->proc.results; - else - return tuple->variables[0]->type; + Type *pt = get_base_type(instr->call.type); + if (pt != NULL) { + if (pt->kind == Type_Tuple && pt->tuple.variable_count == 1) + return pt->tuple.variables[0]->type; + return pt; + } + return NULL; } case ssaInstr_CopyMemory: return t_int; @@ -891,7 +892,7 @@ void ssa_end_procedure_body(ssaProcedure *proc) { case ssaInstr_CopyMemory: continue; case ssaInstr_Call: - if (instr->call.type->proc.results == NULL) { + if (instr->call.type == NULL) { continue; } break; @@ -1194,6 +1195,7 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) { return value; } + Type *src = get_base_type(src_type); Type *dst = get_base_type(t); if (are_types_identical(t, src_type)) @@ -1208,6 +1210,10 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) { // } else if (is_type_integer(dst)) { ev = exact_value_to_integer(ev); + } else if (is_type_pointer(dst)) { + // IMPORTANT NOTE(bill): LLVM doesn't support pointer constants expect `null` + ssaValue *i = ssa_make_value_constant(proc->module->allocator, t_uint, ev); + return ssa_emit(proc, ssa_make_instr_conv(proc, ssaConv_inttoptr, i, t_uint, dst)); } return ssa_make_value_constant(proc->module->allocator, t, ev); } @@ -1459,6 +1465,8 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue ssaValue *value = ssa_make_value_procedure(proc->module->allocator, proc->module, type, pl->type, pl->body, name); + value->proc.tags = pl->tags; + gb_array_append(proc->children, &value->proc); ssa_build_proc(value, proc); @@ -1693,8 +1701,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue } } - ssaValue *call = ssa_make_instr_call(proc, value, args, arg_count, tv->type); - ssa_value_set_type(call, proc_type_); + ssaValue *call = ssa_make_instr_call(proc, value, args, arg_count, type->results); return ssa_emit(proc, call); case_end; @@ -1725,6 +1732,7 @@ ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr) { ssaValue *elem = ssa_array_elem(proc, array); return ssa_emit_load(proc, ssa_emit_string(proc, elem, ssa_array_len(proc, array))); } + return ssa_make_value_constant(proc->module->allocator, tv->type, tv->value); } @@ -2043,6 +2051,8 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { if (proc->children == NULL) { gb_array_init(proc->children, gb_heap_allocator()); } + + if (pd->body != NULL) { // NOTE(bill): Generate a new name // parent$name-guid @@ -2059,6 +2069,8 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { ssaValue *value = ssa_make_value_procedure(proc->module->allocator, proc->module, e->type, pd->type, pd->body, name); + value->proc.tags = pd->tags; + ssa_module_add_value(proc->module, e, value); gb_array_append(proc->children, &value->proc); ssa_build_proc(value, proc); @@ -2273,7 +2285,7 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { proc->curr_block = init; ssa_build_stmt(proc, fs->init); } - ssaBlock *body = ssa__make_block(proc, node, make_string("for.body")); + ssaBlock *body = ssa_add_block(proc, node, make_string("for.body")); ssaBlock *done = ssa__make_block(proc, node, make_string("for.done")); // NOTE(bill): Append later ssaBlock *loop = body; @@ -2289,7 +2301,6 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { proc->curr_block = loop; if (loop != body) { ssa_build_cond(proc, fs->cond, body, done); - gb_array_append(proc->blocks, body); proc->curr_block = body; } diff --git a/src/parser.cpp b/src/parser.cpp index 088d2072c..5818875bb 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -87,6 +87,7 @@ enum ProcTag { AST_NODE_KIND(ProcLit, struct { \ AstNode *type; \ AstNode *body; \ + u64 tags; \ }) \ AST_NODE_KIND(CompoundLit, struct { \ AstNode *type; \ @@ -559,23 +560,24 @@ gb_inline AstNode *make_basic_lit(AstFile *f, Token basic_lit) { return result; } -gb_inline AstNode *make_identifier(AstFile *f, Token token, AstEntity *entity = NULL) { +gb_inline AstNode *make_ident(AstFile *f, Token token, AstEntity *entity = NULL) { AstNode *result = make_node(f, AstNode_Ident); result->Ident.token = token; result->Ident.entity = entity; return result; } -gb_inline AstNode *make_procedure_literal(AstFile *f, AstNode *type, AstNode *body) { +gb_inline AstNode *make_proc_lit(AstFile *f, AstNode *type, AstNode *body, u64 tags) { AstNode *result = make_node(f, AstNode_ProcLit); result->ProcLit.type = type; result->ProcLit.body = body; + result->ProcLit.tags = tags; return result; } -gb_inline AstNode *make_compound_literal(AstFile *f, AstNode *type, AstNode *elem_list, isize elem_count, - Token open, Token close) { +gb_inline AstNode *make_compound_lit(AstFile *f, AstNode *type, AstNode *elem_list, isize elem_count, + Token open, Token close) { AstNode *result = make_node(f, AstNode_CompoundLit); result->CompoundLit.type = type; result->CompoundLit.elem_list = elem_list; @@ -845,16 +847,12 @@ gb_internal void add_ast_entity(AstFile *f, AstScope *scope, AstNode *declaratio AstEntity *insert_entity = ast_scope_insert(scope, *entity); if (insert_entity != NULL && !is_blank_ident(insert_entity->token.string)) { ast_file_err(f, entity->token, - "There is already a previous declaration of `%.*s` in the current scope at\n" - "\t%.*s(%td:%td)", + "There is already a previous declaration of `%.*s` in the current scope\n" + "at \t%.*s(%td:%td)", LIT(insert_entity->token.string), LIT(insert_entity->token.pos.file), insert_entity->token.pos.line, insert_entity->token.pos.column); - - gb_printf_err("Hashes\n"); - gb_printf_err("%16llx - %.*s\n", hash_string(insert_entity->token.string), LIT(insert_entity->token.string)); - gb_printf_err("%16llx - %.*s\n\n", hash_string(entity->token.string), LIT(entity->token.string)); } } } @@ -911,7 +909,7 @@ AstNode *parse_identifier(AstFile *f) { token.string = make_string("_"); expect_token(f, Token_Identifier); } - return make_identifier(f, token); + return make_ident(f, token); } AstNode *parse_tag_expr(AstFile *f, AstNode *expression) { @@ -971,7 +969,7 @@ AstNode *parse_literal_value(AstFile *f, AstNode *type) { f->expr_level--; Token close = expect_token(f, Token_CloseBrace); - return make_compound_literal(f, type, element_list, element_count, open, close); + return make_compound_lit(f, type, element_list, element_count, open, close); } AstNode *parse_value(AstFile *f) { @@ -984,6 +982,93 @@ AstNode *parse_value(AstFile *f) { AstNode *parse_identifier_or_type(AstFile *f); + +void check_proc_add_tag(AstFile *f, AstNode *tag_expr, u64 *tags, ProcTag tag, String tag_name) { + if (*tags & tag) { + ast_file_err(f, ast_node_token(tag_expr), "Procedure tag already used: %.*s", LIT(tag_name)); + } + *tags |= tag; +} + +b32 is_foreign_name_valid(String name) { + // TODO(bill): is_foreign_name_valid + if (name.len == 0) + return false; + isize offset = 0; + while (offset < name.len) { + Rune rune; + isize remaining = name.len - offset; + isize width = gb_utf8_decode(name.text+offset, remaining, &rune); + if (rune == GB_RUNE_INVALID && width == 1) { + return false; + } else if (rune == GB_RUNE_BOM && remaining > 0) { + return false; + } + + if (offset == 0) { + switch (rune) { + case '-': + case '$': + case '.': + case '_': + break; + default: + if (!rune_is_letter(rune)) + return false; + break; + } + } else { + switch (rune) { + case '-': + case '$': + case '.': + case '_': + break; + default: + if (!rune_is_letter(rune) && !rune_is_digit(rune)) { + return false; + } + break; + } + } + + offset += width; + } + + return true; +} + +void parse_proc_tags(AstFile *f, u64 *tags, String *foreign_name) { + // TODO(bill): Add this to procedure literals too + while (f->cursor[0].kind == Token_Hash) { + AstNode *tag_expr = parse_tag_expr(f, NULL); + ast_node(te, TagExpr, tag_expr); + String tag_name = te->name.string; + if (are_strings_equal(tag_name, make_string("foreign"))) { + check_proc_add_tag(f, tag_expr, tags, ProcTag_foreign, tag_name); + if (f->cursor[0].kind == Token_String) { + *foreign_name = f->cursor[0].string; + // TODO(bill): Check if valid string + if (!is_foreign_name_valid(*foreign_name)) { + ast_file_err(f, ast_node_token(tag_expr), "Invalid alternative foreign procedure name"); + } + + next_token(f); + } + } else if (are_strings_equal(tag_name, make_string("inline"))) { + check_proc_add_tag(f, tag_expr, tags, ProcTag_inline, tag_name); + } else if (are_strings_equal(tag_name, make_string("no_inline"))) { + check_proc_add_tag(f, tag_expr, tags, ProcTag_no_inline, tag_name); + } else { + ast_file_err(f, ast_node_token(tag_expr), "Unknown procedure tag"); + } + } + + if ((*tags & ProcTag_inline) && (*tags & ProcTag_no_inline)) { + ast_file_err(f, f->cursor[0], "You cannot apply both `inline` and `no_inline` to a procedure"); + } +} + AstNode *parse_operand(AstFile *f, b32 lhs) { AstNode *operand = NULL; // Operand switch (f->cursor[0].kind) { @@ -1024,6 +1109,13 @@ AstNode *parse_operand(AstFile *f, b32 lhs) { AstScope *scope = NULL; AstNode *type = parse_proc_type(f, &scope); + u64 tags = 0; + String foreign_name = {}; + parse_proc_tags(f, &tags, &foreign_name); + if (tags & ProcTag_foreign) { + ast_file_err(f, f->cursor[0], "#foreign cannot be applied to procedure literals"); + } + if (f->cursor[0].kind != Token_OpenBrace) { return type; } else { @@ -1036,7 +1128,7 @@ AstNode *parse_operand(AstFile *f, b32 lhs) { f->expr_level--; f->curr_scope = curr_scope; - return make_procedure_literal(f, type, body); + return make_proc_lit(f, type, body, tags); } } @@ -1605,60 +1697,7 @@ AstNode *parse_body(AstFile *f, AstScope *scope) { return make_block_stmt(f, statement_list, statement_list_count, open, close); } -b32 is_foreign_name_valid(String name) { - // TODO(bill): is_foreign_name_valid - if (name.len == 0) - return false; - isize offset = 0; - while (offset < name.len) { - Rune rune; - isize remaining = name.len - offset; - isize width = gb_utf8_decode(name.text+offset, remaining, &rune); - if (rune == GB_RUNE_INVALID && width == 1) { - return false; - } else if (rune == GB_RUNE_BOM && remaining > 0) { - return false; - } - if (offset == 0) { - switch (rune) { - case '-': - case '$': - case '.': - case '_': - break; - default: - if (!rune_is_letter(rune)) - return false; - break; - } - } else { - switch (rune) { - case '-': - case '$': - case '.': - case '_': - break; - default: - if (!rune_is_letter(rune) && !rune_is_digit(rune)) { - return false; - } - break; - } - } - - offset += width; - } - - return true; -} - -void check_proc_add_tag(AstFile *f, AstNode *tag_expr, u64 *tags, ProcTag tag, String tag_name) { - if (*tags & tag) { - ast_file_err(f, ast_node_token(tag_expr), "Procedure tag already used: %.*s", LIT(tag_name)); - } - *tags |= tag; -} AstNode *parse_proc_decl(AstFile *f, Token proc_token, AstNode *name) { AstNode *param_list = NULL; @@ -1673,36 +1712,8 @@ AstNode *parse_proc_decl(AstFile *f, Token proc_token, AstNode *name) { AstNode *body = NULL; u64 tags = 0; String foreign_name = {}; - while (f->cursor[0].kind == Token_Hash) { - AstNode *tag_expr = parse_tag_expr(f, NULL); - ast_node(te, TagExpr, tag_expr); - String tag_name = te->name.string; - if (are_strings_equal(tag_name, make_string("foreign"))) { - check_proc_add_tag(f, tag_expr, &tags, ProcTag_foreign, tag_name); - if (f->cursor[0].kind == Token_String) { - foreign_name = f->cursor[0].string; - // TODO(bill): Check if valid string - if (!is_foreign_name_valid(foreign_name)) { - ast_file_err(f, ast_node_token(tag_expr), "Invalid alternative foreign procedure name"); - } - next_token(f); - } - } else if (are_strings_equal(tag_name, make_string("inline"))) { - check_proc_add_tag(f, tag_expr, &tags, ProcTag_inline, tag_name); - } else if (are_strings_equal(tag_name, make_string("no_inline"))) { - check_proc_add_tag(f, tag_expr, &tags, ProcTag_no_inline, tag_name); - } else { - ast_file_err(f, ast_node_token(tag_expr), "Unknown procedure tag"); - } - } - - b32 is_inline = (tags & ProcTag_inline) != 0; - b32 is_no_inline = (tags & ProcTag_no_inline) != 0; - - if (is_inline && is_no_inline) { - ast_file_err(f, f->cursor[0], "You cannot apply both `inline` and `no_inline` to a procedure"); - } + parse_proc_tags(f, &tags, &foreign_name); if (f->cursor[0].kind == Token_OpenBrace) { if ((tags & ProcTag_foreign) != 0) { diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index d4cfac52c..8d7848d53 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -85,6 +85,7 @@ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \ TOKEN_KIND(Token_continue, "continue"), \ TOKEN_KIND(Token_fallthrough, "fallthrough"), \ TOKEN_KIND(Token_case, "case"), \ + TOKEN_KIND(Token_then, "then"), \ TOKEN_KIND(Token_if, "if"), \ TOKEN_KIND(Token_else, "else"), \ TOKEN_KIND(Token_for, "for"), \