diff --git a/examples/basic.odin b/examples/basic.odin index c088b85f9..e0863188a 100644 --- a/examples/basic.odin +++ b/examples/basic.odin @@ -2,29 +2,28 @@ putchar :: proc(c: i32) -> i32 #foreign print_string :: proc(s: string) { for i := 0; i < len(s); i++ { - c := cast(i32)s[i]; - putchar(c); + putchar(cast(i32)s[i]); } } -string_byte_reverse :: proc(s: string) { - n := len(s); +byte_reverse :: proc(b: []byte) { + n := len(b); for i := 0; i < n/2; i++ { - s[i], s[n-1-i] = s[n-1-i], s[i]; + b[i], b[n-1-i] = b[n-1-i], b[i]; } } -encode_rune :: proc(r : rune) -> ([4]u8, int) { - buf : [4]u8; +encode_rune :: proc(r : rune) -> ([4]byte, int) { + buf : [4]byte; i := cast(u32)r; - mask : u8 : 0x3f; + mask : byte : 0x3f; if i <= 1<<7-1 { - buf[0] = cast(u8)r; + buf[0] = cast(byte)r; return buf, 1; } if i <= 1<<11-1 { - buf[0] = 0xc0 | cast(u8)(r>>6); - buf[1] = 0x80 | cast(u8)(r)&mask; + buf[0] = 0xc0 | cast(byte)(r>>6); + buf[1] = 0x80 | cast(byte)(r)&mask; return buf, 2; } @@ -35,16 +34,16 @@ encode_rune :: proc(r : rune) -> ([4]u8, int) { } if i <= 1<<16-1 { - buf[0] = 0xe0 | cast(u8)(r>>12); - buf[1] = 0x80 | cast(u8)(r>>6)&mask; - buf[2] = 0x80 | cast(u8)(r)&mask; + buf[0] = 0xe0 | cast(byte)(r>>12); + buf[1] = 0x80 | cast(byte)(r>>6)&mask; + buf[2] = 0x80 | cast(byte)(r)&mask; return buf, 3; } - buf[0] = 0xf0 | cast(u8)(r>>18); - buf[1] = 0x80 | cast(u8)(r>>12)&mask; - buf[2] = 0x80 | cast(u8)(r>>6)&mask; - buf[3] = 0x80 | cast(u8)(r)&mask; + buf[0] = 0xf0 | cast(byte)(r>>18); + buf[1] = 0x80 | cast(byte)(r>>12)&mask; + buf[2] = 0x80 | cast(byte)(r>>6)&mask; + buf[3] = 0x80 | cast(byte)(r)&mask; return buf, 4; } @@ -59,9 +58,8 @@ print_int :: proc(i : int) { } print_int_base :: proc(i, base : int) { NUM_TO_CHAR_TABLE :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$"; - NEG :: "-"; - buf: [21]u8; + buf: [21]byte; len := 0; negative := false; if i < 0 { @@ -73,8 +71,7 @@ print_int_base :: proc(i, base : int) { len++; } for i > 0 { - c : u8 = NUM_TO_CHAR_TABLE[i % base]; - buf[len] = c; + buf[len] = NUM_TO_CHAR_TABLE[i % base]; len++; i /= base; } @@ -84,7 +81,6 @@ print_int_base :: proc(i, base : int) { len++; } - str := cast(string)buf[:len]; - string_byte_reverse(str); - print_string(str); + byte_reverse(buf[:len]); + print_string(cast(string)buf[:len]); } diff --git a/examples/main.ll b/examples/main.ll index 412f9c49b..fce46ccd9 100644 --- a/examples/main.ll +++ b/examples/main.ll @@ -4,198 +4,43 @@ declare void @llvm.memmove.p0i8.p0i8.i64(i8*, i8*, i64, i32, i1) +define void @thing(i64 ()* %p) { +"entry - 0": + %0 = alloca i64 ()*, align 8 ; p + store i64 ()* zeroinitializer, i64 ()** %0 + store i64 ()* %p, i64 ()** %0 + %1 = load i64 ()*, i64 ()** %0 + %2 = call i64 %1() + call void @print_int(i64 %2) + call void @print_rune(i32 10) + ret void +} + define void @main() { "entry - 0": - %0 = alloca [4 x i64], align 8 ; dst - store [4 x i64] zeroinitializer, [4 x i64]* %0 - %1 = alloca [2 x i64], align 8 ; src - store [2 x i64] zeroinitializer, [2 x i64]* %1 - %2 = getelementptr inbounds [2 x i64], [2 x i64]* %1, i64 0, i64 0 - %3 = getelementptr i64, i64* %2, i64 0 - store i64 7, i64* %3 - %4 = getelementptr inbounds [2 x i64], [2 x i64]* %1, i64 0, i64 0 - %5 = getelementptr i64, i64* %4, i64 1 - store i64 5, i64* %5 - %6 = sub i64 4, 1 - %7 = sub i64 4, 1 - %8 = getelementptr inbounds [4 x i64], [4 x i64]* %0, i64 0, i64 0 - %9 = getelementptr i64, i64* %8, i64 1 - %10 = alloca {i64*, i64, i64}, align 8 - store {i64*, i64, i64} zeroinitializer, {i64*, i64, i64}* %10 - %11 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %10, i64 0, i32 0 - store i64* %9, i64** %11 - %12 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %10, i64 0, i32 1 - store i64 %6, i64* %12 - %13 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %10, i64 0, i32 2 - store i64 %7, i64* %13 - %14 = sub i64 2, 0 - %15 = sub i64 2, 0 - %16 = getelementptr inbounds [2 x i64], [2 x i64]* %1, i64 0, i64 0 - %17 = getelementptr i64, i64* %16, i64 0 - %18 = alloca {i64*, i64, i64}, align 8 - store {i64*, i64, i64} zeroinitializer, {i64*, i64, i64}* %18 - %19 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %18, i64 0, i32 0 - store i64* %17, i64** %19 - %20 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %18, i64 0, i32 1 - store i64 %14, i64* %20 - %21 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %18, i64 0, i32 2 - store i64 %15, i64* %21 - %22 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %10, i64 0, i32 0 - %23 = load i64*, i64** %22 - %24 = bitcast i64* %23 to %.rawptr - %25 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %18, i64 0, i32 0 - %26 = load i64*, i64** %25 - %27 = bitcast i64* %26 to %.rawptr - %28 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %10, i64 0, i32 1 - %29 = load i64, i64* %28 - %30 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %18, i64 0, i32 1 - %31 = load i64, i64* %30 - %32 = icmp slt i64 %29, %31 - %33 = select i1 %32, i64 %29, i64 %31 - %34 = mul i64 %33, 8 - call void @llvm.memmove.p0i8.p0i8.i64(i8* %24, i8* %27, i64 %34, i32 8, i1 false) - %35 = alloca i64, align 8 ; i - store i64 zeroinitializer, i64* %35 - store i64 0, i64* %35 - br label %"for.loop - 2" - -"for.body - 1": - %36 = getelementptr inbounds [4 x i64], [4 x i64]* %0, i64 0, i64 0 - %37 = load i64, i64* %35 - %38 = getelementptr i64, i64* %36, i64 %37 - %39 = load i64, i64* %38 - call void @print_int(i64 %39) - br label %"for.post - 3" - -"for.loop - 2": - %40 = load i64, i64* %35 - %41 = icmp slt i64 %40, 4 - br i1 %41, label %"for.body - 1", label %"for.done - 4" - -"for.post - 3": - %42 = load i64, i64* %35 - %43 = add i64 %42, 1 - store i64 %43, i64* %35 - br label %"for.loop - 2" - -"for.done - 4": - br i1 false, label %"if.then - 5", label %"if.done - 6" - -"if.then - 5": - %44 = getelementptr inbounds [26 x i8], [26 x i8]* @.str0, i64 0, i64 0 - %45 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %45 - %46 = getelementptr inbounds %.string, %.string* %45, i64 0, i32 0 - %47 = getelementptr inbounds %.string, %.string* %45, i64 0, i32 1 - store i8* %44, i8** %46 - store i64 26, i64* %47 - %48 = load %.string, %.string* %45 - call void @print_string(%.string %48) - %49 = getelementptr inbounds [26 x i8], [26 x i8]* @.str1, i64 0, i64 0 - %50 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %50 - %51 = getelementptr inbounds %.string, %.string* %50, i64 0, i32 0 - %52 = getelementptr inbounds %.string, %.string* %50, i64 0, i32 1 - store i8* %49, i8** %51 - store i64 26, i64* %52 - %53 = load %.string, %.string* %50 - call void @print_string(%.string %53) - %54 = getelementptr inbounds [25 x i8], [25 x i8]* @.str2, i64 0, i64 0 - %55 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %55 - %56 = getelementptr inbounds %.string, %.string* %55, i64 0, i32 0 - %57 = getelementptr inbounds %.string, %.string* %55, i64 0, i32 1 - store i8* %54, i8** %56 - store i64 25, i64* %57 - %58 = load %.string, %.string* %55 - call void @print_string(%.string %58) - %59 = getelementptr inbounds [27 x i8], [27 x i8]* @.str3, i64 0, i64 0 - %60 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %60 - %61 = getelementptr inbounds %.string, %.string* %60, i64 0, i32 0 - %62 = getelementptr inbounds %.string, %.string* %60, i64 0, i32 1 - store i8* %59, i8** %61 - store i64 27, i64* %62 - %63 = load %.string, %.string* %60 - call void @print_string(%.string %63) - %64 = getelementptr inbounds [24 x i8], [24 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 24, i64* %67 - %68 = load %.string, %.string* %65 - call void @print_string(%.string %68) - %69 = getelementptr inbounds [42 x i8], [42 x i8]* @.str5, i64 0, i64 0 - %70 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %70 - %71 = getelementptr inbounds %.string, %.string* %70, i64 0, i32 0 - %72 = getelementptr inbounds %.string, %.string* %70, i64 0, i32 1 - store i8* %69, i8** %71 - store i64 42, i64* %72 - %73 = load %.string, %.string* %70 - call void @print_string(%.string %73) - %74 = getelementptr inbounds [24 x i8], [24 x i8]* @.str6, i64 0, i64 0 - %75 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %75 - %76 = getelementptr inbounds %.string, %.string* %75, i64 0, i32 0 - %77 = getelementptr inbounds %.string, %.string* %75, i64 0, i32 1 - store i8* %74, i8** %76 - store i64 24, i64* %77 - %78 = load %.string, %.string* %75 - call void @print_string(%.string %78) - %79 = getelementptr inbounds [35 x i8], [35 x i8]* @.str7, i64 0, i64 0 - %80 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %80 - %81 = getelementptr inbounds %.string, %.string* %80, i64 0, i32 0 - %82 = getelementptr inbounds %.string, %.string* %80, i64 0, i32 1 - store i8* %79, i8** %81 - store i64 35, i64* %82 - %83 = load %.string, %.string* %80 - call void @print_string(%.string %83) - %84 = getelementptr inbounds [33 x i8], [33 x i8]* @.str8, i64 0, i64 0 - %85 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %85 - %86 = getelementptr inbounds %.string, %.string* %85, i64 0, i32 0 - %87 = getelementptr inbounds %.string, %.string* %85, i64 0, i32 1 - store i8* %84, i8** %86 - store i64 33, i64* %87 - %88 = load %.string, %.string* %85 - call void @print_string(%.string %88) - %89 = getelementptr inbounds [24 x i8], [24 x i8]* @.str9, i64 0, i64 0 - %90 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %90 - %91 = getelementptr inbounds %.string, %.string* %90, i64 0, i32 0 - %92 = getelementptr inbounds %.string, %.string* %90, i64 0, i32 1 - store i8* %89, i8** %91 - store i64 24, i64* %92 - %93 = load %.string, %.string* %90 - call void @print_string(%.string %93) - %94 = getelementptr inbounds [45 x i8], [45 x i8]* @.stra, i64 0, i64 0 - %95 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %95 - %96 = getelementptr inbounds %.string, %.string* %95, i64 0, i32 0 - %97 = getelementptr inbounds %.string, %.string* %95, i64 0, i32 1 - store i8* %94, i8** %96 - store i64 45, i64* %97 - %98 = load %.string, %.string* %95 - call void @print_string(%.string %98) - %99 = getelementptr inbounds [24 x i8], [24 x i8]* @.strb, i64 0, i64 0 - %100 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %100 - %101 = getelementptr inbounds %.string, %.string* %100, i64 0, i32 0 - %102 = getelementptr inbounds %.string, %.string* %100, i64 0, i32 1 - store i8* %99, i8** %101 - store i64 24, i64* %102 - %103 = load %.string, %.string* %100 - call void @print_string(%.string %103) - br label %"if.done - 6" - -"if.done - 6": + %0 = alloca i8, align 1 ; a + store i8 zeroinitializer, i8* %0 + store i8 123, i8* %0 + %1 = load i8, i8* %0 + %2 = zext i8 %1 to i64 + call void @print_int(i64 %2) + call void @print_rune(i32 128149) + call void @print_rune(i32 10) + call void @thing(i64 ()* @main$0) ret void } +define i64 @main$0() { +"entry - 0": + %0 = alloca i64, align 8 ; a + store i64 zeroinitializer, i64* %0 + store i64 1337, i64* %0 + call void @print_rune(i32 128149) + call void @print_rune(i32 10) + %1 = load i64, i64* %0 + ret i64 %1 +} + declare i32 @putchar(i32 %c) ; foreign procedure define void @print_string(%.string %s) { @@ -209,44 +54,40 @@ define void @print_string(%.string %s) { br label %"for.loop - 2" "for.body - 1": - %2 = alloca i32, align 4 ; c - store i32 zeroinitializer, i32* %2 - %3 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 0 - %4 = load i8*, i8** %3 - %5 = load i64, i64* %1 - %6 = getelementptr i8, i8* %4, i64 %5 - %7 = load i8, i8* %6 - %8 = zext i8 %7 to i32 - store i32 %8, i32* %2 - %9 = load i32, i32* %2 - %10 = call i32 @putchar(i32 %9) + %2 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 0 + %3 = load i8*, i8** %2 + %4 = load i64, i64* %1 + %5 = getelementptr i8, i8* %3, i64 %4 + %6 = load i8, i8* %5 + %7 = zext i8 %6 to i32 + %8 = call i32 @putchar(i32 %7) br label %"for.post - 3" "for.loop - 2": - %11 = load i64, i64* %1 - %12 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 1 - %13 = load i64, i64* %12 - %14 = icmp slt i64 %11, %13 - br i1 %14, label %"for.body - 1", label %"for.done - 4" + %9 = load i64, i64* %1 + %10 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 1 + %11 = load i64, i64* %10 + %12 = icmp slt i64 %9, %11 + br i1 %12, label %"for.body - 1", label %"for.done - 4" "for.post - 3": - %15 = load i64, i64* %1 - %16 = add i64 %15, 1 - store i64 %16, i64* %1 + %13 = load i64, i64* %1 + %14 = add i64 %13, 1 + store i64 %14, i64* %1 br label %"for.loop - 2" "for.done - 4": ret void } -define void @string_byte_reverse(%.string %s) { +define void @byte_reverse({i8*, i64, i64} %b) { "entry - 0": - %0 = alloca %.string, align 8 ; s - store %.string zeroinitializer, %.string* %0 - store %.string %s, %.string* %0 + %0 = alloca {i8*, i64, i64}, align 8 ; b + store {i8*, i64, i64} zeroinitializer, {i8*, i64, i64}* %0 + store {i8*, i64, i64} %b, {i8*, i64, i64}* %0 %1 = alloca i64, align 8 ; n store i64 zeroinitializer, i64* %1 - %2 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 1 + %2 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 1 %3 = load i64, i64* %2 store i64 %3, i64* %1 %4 = alloca i64, align 8 ; i @@ -255,18 +96,18 @@ define void @string_byte_reverse(%.string %s) { br label %"for.loop - 2" "for.body - 1": - %5 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 0 + %5 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %6 = load i8*, i8** %5 %7 = load i64, i64* %4 %8 = getelementptr i8, i8* %6, i64 %7 - %9 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 0 + %9 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %10 = load i8*, i8** %9 %11 = load i64, i64* %4 %12 = load i64, i64* %1 %13 = sub i64 %12, 1 %14 = sub i64 %13, %11 %15 = getelementptr i8, i8* %10, i64 %14 - %16 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 0 + %16 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %17 = load i8*, i8** %16 %18 = load i64, i64* %4 %19 = load i64, i64* %1 @@ -274,7 +115,7 @@ define void @string_byte_reverse(%.string %s) { %21 = sub i64 %20, %18 %22 = getelementptr i8, i8* %17, i64 %21 %23 = load i8, i8* %22 - %24 = getelementptr inbounds %.string, %.string* %0, i64 0, i32 0 + %24 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %25 = load i8*, i8** %24 %26 = load i64, i64* %4 %27 = getelementptr i8, i8* %25, i64 %26 @@ -567,97 +408,90 @@ define void @print_int_base(i64 %i, i64 %base) { br label %"for.loop - 6" "for.body - 5": - %16 = alloca i8, align 1 ; c - store i8 zeroinitializer, i8* %16 - %17 = getelementptr inbounds [64 x i8], [64 x i8]* @.strc, i64 0, i64 0 - %18 = load i64, i64* %1 - %19 = load i64, i64* %0 - %20 = srem i64 %19, %18 - %21 = getelementptr i8, i8* %17, i64 %20 - %22 = load i8, i8* %21 - store i8 %22, i8* %16 - %23 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0 - %24 = load i64, i64* %3 - %25 = getelementptr i8, i8* %23, i64 %24 - %26 = load i8, i8* %16 - store i8 %26, i8* %25 - %27 = load i64, i64* %3 - %28 = add i64 %27, 1 - store i64 %28, i64* %3 - %29 = load i64, i64* %1 - %30 = load i64, i64* %0 - %31 = sdiv i64 %30, %29 - store i64 %31, i64* %0 + %16 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0 + %17 = load i64, i64* %3 + %18 = getelementptr i8, i8* %16, i64 %17 + %19 = getelementptr inbounds [64 x i8], [64 x i8]* @.str0, i64 0, i64 0 + %20 = load i64, i64* %1 + %21 = load i64, i64* %0 + %22 = srem i64 %21, %20 + %23 = getelementptr i8, i8* %19, i64 %22 + %24 = load i8, i8* %23 + store i8 %24, i8* %18 + %25 = load i64, i64* %3 + %26 = add i64 %25, 1 + store i64 %26, i64* %3 + %27 = load i64, i64* %1 + %28 = load i64, i64* %0 + %29 = sdiv i64 %28, %27 + store i64 %29, i64* %0 br label %"for.loop - 6" "for.loop - 6": - %32 = load i64, i64* %0 - %33 = icmp sgt i64 %32, 0 - br i1 %33, label %"for.body - 5", label %"for.done - 7" + %30 = load i64, i64* %0 + %31 = icmp sgt i64 %30, 0 + br i1 %31, label %"for.body - 5", label %"for.done - 7" "for.done - 7": - %34 = load i1, i1* %4 - br i1 %34, label %"if.then - 8", label %"if.done - 9" + %32 = load i1, i1* %4 + br i1 %32, label %"if.then - 8", label %"if.done - 9" "if.then - 8": - %35 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0 + %33 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0 + %34 = load i64, i64* %3 + %35 = getelementptr i8, i8* %33, i64 %34 + store i8 45, i8* %35 %36 = load i64, i64* %3 - %37 = getelementptr i8, i8* %35, i64 %36 - store i8 45, i8* %37 - %38 = load i64, i64* %3 - %39 = add i64 %38, 1 - store i64 %39, i64* %3 + %37 = add i64 %36, 1 + store i64 %37, i64* %3 br label %"if.done - 9" "if.done - 9": - %40 = alloca %.string, align 8 ; str - store %.string zeroinitializer, %.string* %40 - %41 = load i64, i64* %3 - %42 = sub i64 %41, 0 - %43 = sub i64 21, 0 - %44 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0 - %45 = getelementptr i8, i8* %44, i64 0 - %46 = alloca {i8*, i64, i64}, align 8 - store {i8*, i64, i64} zeroinitializer, {i8*, i64, i64}* %46 - %47 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %46, i64 0, i32 0 - store i8* %45, i8** %47 - %48 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %46, i64 0, i32 1 - store i64 %42, i64* %48 - %49 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %46, i64 0, i32 2 - store i64 %43, i64* %49 - %50 = load {i8*, i64, i64}, {i8*, i64, i64}* %46 - %51 = alloca {i8*, i64, i64}, align 8 - store {i8*, i64, i64} zeroinitializer, {i8*, i64, i64}* %51 - store {i8*, i64, i64} %50, {i8*, i64, i64}* %51 - %52 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %51, i64 0, i32 0 - %53 = load i8*, i8** %52 - %54 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %51, i64 0, i32 1 - %55 = load i64, i64* %54 - %56 = alloca %.string, align 8 - store %.string zeroinitializer, %.string* %56 - %57 = getelementptr inbounds %.string, %.string* %56, i64 0, i32 0 - %58 = getelementptr inbounds %.string, %.string* %56, i64 0, i32 1 - store i8* %53, i8** %57 - store i64 %55, i64* %58 - %59 = load %.string, %.string* %56 - store %.string %59, %.string* %40 - %60 = load %.string, %.string* %40 - call void @string_byte_reverse(%.string %60) - %61 = load %.string, %.string* %40 - call void @print_string(%.string %61) + %38 = load i64, i64* %3 + %39 = sub i64 %38, 0 + %40 = sub i64 21, 0 + %41 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0 + %42 = getelementptr i8, i8* %41, i64 0 + %43 = alloca {i8*, i64, i64}, align 8 + store {i8*, i64, i64} zeroinitializer, {i8*, i64, i64}* %43 + %44 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %43, i64 0, i32 0 + store i8* %42, i8** %44 + %45 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %43, i64 0, i32 1 + store i64 %39, i64* %45 + %46 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %43, i64 0, i32 2 + store i64 %40, i64* %46 + %47 = load {i8*, i64, i64}, {i8*, i64, i64}* %43 + call void @byte_reverse({i8*, i64, i64} %47) + %48 = load i64, i64* %3 + %49 = sub i64 %48, 0 + %50 = sub i64 21, 0 + %51 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0 + %52 = getelementptr i8, i8* %51, i64 0 + %53 = alloca {i8*, i64, i64}, align 8 + store {i8*, i64, i64} zeroinitializer, {i8*, i64, i64}* %53 + %54 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %53, i64 0, i32 0 + store i8* %52, i8** %54 + %55 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %53, i64 0, i32 1 + store i64 %49, i64* %55 + %56 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %53, i64 0, i32 2 + store i64 %50, i64* %56 + %57 = load {i8*, i64, i64}, {i8*, i64, i64}* %53 + %58 = alloca {i8*, i64, i64}, align 8 + store {i8*, i64, i64} zeroinitializer, {i8*, i64, i64}* %58 + store {i8*, i64, i64} %57, {i8*, i64, i64}* %58 + %59 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %58, i64 0, i32 0 + %60 = load i8*, i8** %59 + %61 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %58, i64 0, i32 1 + %62 = load i64, i64* %61 + %63 = alloca %.string, align 8 + store %.string zeroinitializer, %.string* %63 + %64 = getelementptr inbounds %.string, %.string* %63, i64 0, i32 0 + %65 = getelementptr inbounds %.string, %.string* %63, i64 0, i32 1 + store i8* %60, i8** %64 + store i64 %62, i64* %65 + %66 = load %.string, %.string* %63 + call void @print_string(%.string %66) ret void } -@.str0 = global [26 x i8] c"Chinese\20\20\20\20-\20\E4\BD\A0\E5\A5\BD\E4\B8\96\E7\95\8C\0A" -@.str1 = global [26 x i8] c"Dutch\20\20\20\20\20\20-\20Hello\20wereld\0A" -@.str2 = global [25 x i8] c"English\20\20\20\20-\20Hello\20world\0A" -@.str3 = global [27 x i8] c"French\20\20\20\20\20-\20Bonjour\20monde\0A" -@.str4 = global [24 x i8] c"German\20\20\20\20\20-\20Hallo\20Welt\0A" -@.str5 = global [42 x i8] c"Greek\20\20\20\20\20\20-\20\CE\B3\CE\B5\CE\B9\CE\AC\20\CF\83\CE\BF\CF\85\20\CE\BA\CF\8C\CF\83\CE\BC\CE\BF\CF\82\0A" -@.str6 = global [24 x i8] c"Italian\20\20\20\20-\20Ciao\20mondo\0A" -@.str7 = global [35 x i8] c"Japanese\20\20\20-\20\E3\81\93\E3\82\93\E3\81\AB\E3\81\A1\E3\81\AF\E4\B8\96\E7\95\8C\0A" -@.str8 = global [33 x i8] c"Korean\20\20\20\20\20-\20\EC\97\AC\EB\B3\B4\EC\84\B8\EC\9A\94\20\EC\84\B8\EA\B3\84\0A" -@.str9 = global [24 x i8] c"Portuguese\20-\20Ol\C3\A1\20mundo\0A" -@.stra = global [45 x i8] c"Russian\20\20\20\20-\20\D0\97\D0\B4\D1\80\D0\B0\D0\B2\D1\81\D1\82\D0\B2\D1\83\D0\BB\D1\82\D0\B5\20\D0\BC\D0\B8\D1\80\0A" -@.strb = global [24 x i8] c"Spanish\20\20\20\20-\20Hola\20mundo\0A" -@.strc = global [64 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\40$" +@.str0 = global [64 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\40$" diff --git a/examples/main.odin b/examples/main.odin index 1a6d9b443..1263a77eb 100644 --- a/examples/main.odin +++ b/examples/main.odin @@ -2,31 +2,38 @@ import "basic" TWO_HEARTS :: '💕'; -main :: proc() { - dst : [4]int; - src : [2]int; - src[0] = 7; - src[1] = 5; - - _ = copy(dst[1:], src[:]); - - for i := 0; i < len(dst); i++ { - print_int(dst[i]); - } - - if false { - 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"); - } +thing :: proc(p : proc() -> int) { + print_int(p()); + print_rune('\n'); } +main :: proc() { + a : u8 = 123; + print_int(cast(int)a); + print_rune(TWO_HEARTS); + print_rune('\n'); + thing(proc() -> int { + a : int = 1337; + print_rune(TWO_HEARTS); + print_rune('\n'); + return a; + }); +} + +/* +if false { + 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"); +} +*/ + diff --git a/src/checker/checker.cpp b/src/checker/checker.cpp index 6a4d4266e..5eb28eba1 100644 --- a/src/checker/checker.cpp +++ b/src/checker/checker.cpp @@ -37,7 +37,7 @@ struct DeclInfo { AstNode *type_expr; AstNode *init_expr; - AstNode *proc_decl; // AstNode_ProcedureDeclaration + AstNode *proc_decl; // AstNode_ProcDecl Map deps; // Key: Entity * i32 mark; diff --git a/src/checker/expr.cpp b/src/checker/expr.cpp index 2fc55a294..dc5dc15cc 100644 --- a/src/checker/expr.cpp +++ b/src/checker/expr.cpp @@ -598,7 +598,7 @@ void check_is_expressible(Checker *c, Operand *o, Type *type) { GB_ASSERT(type->kind == Type_Basic); GB_ASSERT(o->mode == Addressing_Constant); if (!check_value_is_expressible(c, o->value, type, &o->value)) { - gbString a = type_to_string(o->type); + gbString a = expr_to_string(o->expr); gbString b = type_to_string(type); defer (gb_string_free(a)); defer (gb_string_free(b)); @@ -606,7 +606,7 @@ void check_is_expressible(Checker *c, Operand *o, Type *type) { if (!is_type_integer(o->type) && is_type_integer(type)) { error(&c->error_collector, ast_node_token(o->expr), "`%s` truncated to `%s`", a, b); } else { - error(&c->error_collector, ast_node_token(o->expr), "`%s` overflows to `%s`", a, b); + error(&c->error_collector, ast_node_token(o->expr), "`%s` overflows `%s`", a, b); } } else { error(&c->error_collector, ast_node_token(o->expr), "Cannot convert `%s` to `%s`", a, b); @@ -1736,9 +1736,16 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ case_end; case_ast_node(pl, ProcLit, node); - Scope *origin_curr_scope = c->context.scope; 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/type.cpp b/src/checker/type.cpp index 4b22296d0..368e276af 100644 --- a/src/checker/type.cpp +++ b/src/checker/type.cpp @@ -13,10 +13,8 @@ enum BasicKind { Basic_u32, Basic_u64, Basic_u128, - Basic_f16, Basic_f32, Basic_f64, - Basic_f128, Basic_int, Basic_uint, Basic_rawptr, @@ -30,7 +28,7 @@ enum BasicKind { Basic_Count, - // Basic_byte = Basic_u8, + Basic_byte = Basic_u8, Basic_rune = Basic_i32, }; @@ -246,10 +244,8 @@ gb_global Type basic_types[] = { {Type_Basic, {Basic_u32, BasicFlag_Integer | BasicFlag_Unsigned, STR_LIT("u32")}}, {Type_Basic, {Basic_u64, BasicFlag_Integer | BasicFlag_Unsigned, STR_LIT("u64")}}, {Type_Basic, {Basic_u128, BasicFlag_Integer | BasicFlag_Unsigned, STR_LIT("u128")}}, - {Type_Basic, {Basic_f16, BasicFlag_Float, STR_LIT("f16")}}, {Type_Basic, {Basic_f32, BasicFlag_Float, STR_LIT("f32")}}, {Type_Basic, {Basic_f64, BasicFlag_Float, STR_LIT("f64")}}, - {Type_Basic, {Basic_f128, BasicFlag_Float, STR_LIT("f128")}}, {Type_Basic, {Basic_int, BasicFlag_Integer, STR_LIT("int")}}, {Type_Basic, {Basic_uint, BasicFlag_Integer | BasicFlag_Unsigned, STR_LIT("uint")}}, {Type_Basic, {Basic_rawptr, BasicFlag_Pointer, STR_LIT("rawptr")}}, @@ -263,7 +259,7 @@ gb_global Type basic_types[] = { }; gb_global Type basic_type_aliases[] = { - // {Type_Basic, {Basic_byte, BasicFlag_Integer | BasicFlag_Unsigned, STR_LIT("byte")}}, + {Type_Basic, {Basic_byte, BasicFlag_Integer | BasicFlag_Unsigned, STR_LIT("byte")}}, {Type_Basic, {Basic_rune, BasicFlag_Integer, STR_LIT("rune")}}, }; @@ -273,14 +269,14 @@ gb_global Type *t_i8 = &basic_types[Basic_i8]; gb_global Type *t_i16 = &basic_types[Basic_i16]; gb_global Type *t_i32 = &basic_types[Basic_i32]; gb_global Type *t_i64 = &basic_types[Basic_i64]; +gb_global Type *t_i128 = &basic_types[Basic_i128]; gb_global Type *t_u8 = &basic_types[Basic_u8]; gb_global Type *t_u16 = &basic_types[Basic_u16]; gb_global Type *t_u32 = &basic_types[Basic_u32]; gb_global Type *t_u64 = &basic_types[Basic_u64]; -gb_global Type *t_f16 = &basic_types[Basic_f16]; +gb_global Type *t_u128 = &basic_types[Basic_u128]; gb_global Type *t_f32 = &basic_types[Basic_f32]; gb_global Type *t_f64 = &basic_types[Basic_f64]; -gb_global Type *t_f128 = &basic_types[Basic_f128]; gb_global Type *t_int = &basic_types[Basic_int]; gb_global Type *t_uint = &basic_types[Basic_uint]; gb_global Type *t_rawptr = &basic_types[Basic_rawptr]; @@ -291,7 +287,7 @@ gb_global Type *t_untyped_float = &basic_types[Basic_UntypedFloat]; gb_global Type *t_untyped_pointer = &basic_types[Basic_UntypedPointer]; gb_global Type *t_untyped_string = &basic_types[Basic_UntypedString]; gb_global Type *t_untyped_rune = &basic_types[Basic_UntypedRune]; -// gb_global Type *t_byte = &basic_type_aliases[Basic_byte]; +gb_global Type *t_byte = &basic_type_aliases[Basic_byte]; gb_global Type *t_rune = &basic_type_aliases[Basic_rune]; diff --git a/src/codegen/codegen.cpp b/src/codegen/codegen.cpp index cde7951df..678013385 100644 --- a/src/codegen/codegen.cpp +++ b/src/codegen/codegen.cpp @@ -36,11 +36,13 @@ void ssa_gen_destroy(ssaGen *s) { 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)); - v_one = ssa_make_value_constant(gb_heap_allocator(), t_int, make_exact_value_integer(1)); - v_zero32 = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(0)); - v_one32 = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(1)); - v_two32 = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(2)); + v_zero = ssa_make_value_constant(gb_heap_allocator(), t_int, make_exact_value_integer(0)); + v_one = ssa_make_value_constant(gb_heap_allocator(), t_int, make_exact_value_integer(1)); + v_zero32 = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(0)); + v_one32 = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(1)); + v_two32 = ssa_make_value_constant(gb_heap_allocator(), t_i32, make_exact_value_integer(2)); + v_false = ssa_make_value_constant(gb_heap_allocator(), t_bool, make_exact_value_bool(false)); + v_true = ssa_make_value_constant(gb_heap_allocator(), t_bool, make_exact_value_bool(true)); } ssaModule *m = &s->module; @@ -76,7 +78,8 @@ void ssa_gen_code(ssaGen *s) { } break; case Entity_Procedure: { - ssaValue *p = ssa_make_value_procedure(a, e, decl, m); + AstNode *body = decl->proc_decl->ProcDecl.body; + ssaValue *p = ssa_make_value_procedure(a, m, e->type, decl->type_expr, body, e->token.string); map_set(&m->values, hash_pointer(e), p); map_set(&m->members, hash_string(name), p); } break; @@ -87,7 +90,7 @@ void ssa_gen_code(ssaGen *s) { auto *entry = &m->members.entries[i]; ssaValue *v = entry->value; if (v->kind == ssaValue_Proc) - ssa_build_proc(v); + ssa_build_proc(v, NULL); } // m->layout = make_string("e-p:64:64:64"); diff --git a/src/codegen/print_llvm.cpp b/src/codegen/print_llvm.cpp index ce84cb45a..a9645cca1 100644 --- a/src/codegen/print_llvm.cpp +++ b/src/codegen/print_llvm.cpp @@ -102,10 +102,8 @@ void ssa_print_type(gbFile *f, BaseTypeSizes s, Type *t) { case Basic_u32: ssa_fprintf(f, "i32"); break; case Basic_u64: ssa_fprintf(f, "i64"); break; case Basic_u128: ssa_fprintf(f, "u128"); break; - case Basic_f16: ssa_fprintf(f, "half"); break; case Basic_f32: ssa_fprintf(f, "float"); break; case Basic_f64: ssa_fprintf(f, "double"); break; - case Basic_f128: ssa_fprintf(f, "fp128"); break; case Basic_rawptr: ssa_fprintf(f, "%%.rawptr"); break; case Basic_string: ssa_fprintf(f, "%%.string"); break; case Basic_uint: ssa_fprintf(f, "i%lld", word_bits); break; @@ -199,20 +197,16 @@ void ssa_print_exact_value(gbFile *f, ssaModule *m, ExactValue value, Type *type ssa_fprintf(f, "%lld", value.value_integer); break; case ExactValue_Float: { - u64 u = 0; + u64 u = *cast(u64*)&value.value_float; if (is_type_float(type) && type->basic.kind == Basic_f32) { // IMPORTANT NOTE(bill): LLVM requires all floating point constants to be // a 64 bit number if bits_of(float type) <= 64. - // To overcome this problem, fill the "bottom" 32 bits with zeros + // For some bizarre reason, you need to clear the bottom 28 bits // https://groups.google.com/forum/#!topic/llvm-dev/IlqV3TbSk6M - f32 fp = cast(f32)value.value_float; - u = *cast(u32 *)&fp; - u <<= 32; - - } else { - u = *cast(u64 *)&value.value_float; + u >>= 28; + u <<= 28; } - ssa_fprintf(f, "0x%llx", u); + ssa_fprintf(f, "0x%016llx", u); } break; case ExactValue_Pointer: if (value.value_float == NULL) { @@ -253,7 +247,7 @@ void ssa_print_value(gbFile *f, ssaModule *m, ssaValue *value, Type *type_hint) ssa_print_encoded_local(f, value->param.entity->token.string); break; case ssaValue_Proc: - ssa_print_encoded_global(f, value->proc.entity->token.string); + ssa_print_encoded_global(f, value->proc.name); break; case ssaValue_Instr: ssa_fprintf(f, "%%%d", value->id); @@ -545,8 +539,66 @@ void ssa_print_instr(gbFile *f, ssaModule *m, ssaValue *value) { } } +void ssa_print_proc(gbFile *f, ssaModule *m, ssaProcedure *proc) { + if (proc->body == NULL) { + ssa_fprintf(f, "declare "); + } else { + ssa_fprintf(f, "define "); + } + + auto *proc_type = &proc->type->proc; + + if (proc_type->result_count == 0) { + ssa_fprintf(f, "void"); + } else { + ssa_print_type(f, m->sizes, proc_type->results); + } + + ssa_fprintf(f, " "); + + ssa_print_encoded_global(f, proc->name); + ssa_fprintf(f, "("); + + if (proc_type->param_count > 0) { + auto *params = &proc_type->params->tuple; + for (isize i = 0; i < params->variable_count; i++) { + Entity *e = params->variables[i]; + if (i > 0) + + ssa_fprintf(f, ", "); + ssa_print_type(f, m->sizes, e->type); + ssa_fprintf(f, " %%%.*s", LIT(e->token.string)); + } + } + + ssa_fprintf(f, ") "); + + if (proc->body == NULL) { + ssa_fprintf(f, "\t; foreign procedure\n\n"); + } else { + ssa_fprintf(f, "{\n"); + gb_for_array(i, proc->blocks) { + ssaBlock *block = proc->blocks[i]; + + if (i > 0) ssa_fprintf(f, "\n"); + ssa_print_block_name(f, block); + ssa_fprintf(f, ":\n"); + + gb_for_array(j, block->instrs) { + ssaValue *value = block->instrs[j]; + ssa_print_instr(f, m, value); + } + } + ssa_fprintf(f, "}\n\n"); + } + + gb_for_array(i, proc->anon_procs) { + ssa_print_proc(f, m, proc->anon_procs[i]); + } +} + void ssa_print_llvm_ir(gbFile *f, ssaModule *m) { - if (m->layout.len > 0) { +if (m->layout.len > 0) { ssa_fprintf(f, "target datalayout = %.*s\n", LIT(m->layout)); } @@ -591,58 +643,7 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) { } break; case ssaValue_Proc: { - ssaProcedure *proc = &v->proc; - if (proc->body == NULL) { - ssa_fprintf(f, "declare "); - } else { - ssa_fprintf(f, "define "); - } - - auto *proc_type = &proc->entity->type->proc; - - if (proc_type->result_count == 0) { - ssa_fprintf(f, "void"); - } else { - ssa_print_type(f, m->sizes, proc_type->results); - } - - ssa_fprintf(f, " "); - - ssa_print_encoded_global(f, proc->name); - ssa_fprintf(f, "("); - - if (proc_type->param_count > 0) { - auto *params = &proc_type->params->tuple; - for (isize i = 0; i < params->variable_count; i++) { - Entity *e = params->variables[i]; - if (i > 0) - ssa_fprintf(f, ", "); - ssa_print_type(f, m->sizes, e->type); - ssa_fprintf(f, " %%%.*s", LIT(e->token.string)); - } - } - - ssa_fprintf(f, ") "); - - if (proc->body == NULL) { - ssa_fprintf(f, "\t; foreign procedure\n\n"); - } else { - ssa_fprintf(f, "{\n"); - gb_for_array(i, proc->blocks) { - ssaBlock *block = proc->blocks[i]; - - if (i > 0) ssa_fprintf(f, "\n"); - ssa_print_block_name(f, block); - ssa_fprintf(f, ":\n"); - - gb_for_array(j, block->instrs) { - ssaValue *value = block->instrs[j]; - ssa_print_instr(f, m, value); - } - } - ssa_fprintf(f, "}\n\n"); - } - + ssa_print_proc(f, m, &v->proc); } break; } } diff --git a/src/codegen/ssa.cpp b/src/codegen/ssa.cpp index 90adf2c7d..a126ae5f2 100644 --- a/src/codegen/ssa.cpp +++ b/src/codegen/ssa.cpp @@ -5,10 +5,10 @@ struct ssaValue; struct ssaModule { - CheckerInfo *info; + CheckerInfo * info; BaseTypeSizes sizes; - gbArena arena; - gbAllocator allocator; + gbArena arena; + gbAllocator allocator; String layout; @@ -31,25 +31,25 @@ struct ssaBlock { struct ssaTargetList { ssaTargetList *prev; - ssaBlock *break_; - ssaBlock *continue_; - ssaBlock *fallthrough_; + ssaBlock * break_; + ssaBlock * continue_; + ssaBlock * fallthrough_; }; struct ssaProcedure { - ssaModule *module; - String name; - Entity *entity; - Type *type; - DeclInfo *decl; - AstNode *type_expr; - AstNode *body; + ssaProcedure *parent; + ssaModule * module; + String name; + Entity * entity; + Type * type; + AstNode * type_expr; + AstNode * body; gbArray(ssaBlock *) blocks; - ssaBlock *curr_block; - ssaTargetList *target_list; + ssaBlock * curr_block; + ssaTargetList * target_list; - gbArray(ssaValue *) anonymous_procedures; + gbArray(ssaProcedure *) anon_procs; }; @@ -233,6 +233,8 @@ gb_global ssaValue *v_one = NULL; gb_global ssaValue *v_zero32 = NULL; gb_global ssaValue *v_one32 = NULL; gb_global ssaValue *v_two32 = NULL; +gb_global ssaValue *v_false = NULL; +gb_global ssaValue *v_true = NULL; enum ssaLvalueKind { ssaLvalue_Blank, @@ -397,6 +399,7 @@ ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr); ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv); ssaLvalue ssa_build_addr(ssaProcedure *proc, AstNode *expr); ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *a_type); +void ssa_build_proc(ssaValue *value, ssaProcedure *parent); @@ -603,13 +606,13 @@ ssaValue *ssa_make_value_constant(gbAllocator a, Type *type, ExactValue value) { return v; } -ssaValue *ssa_make_value_procedure(gbAllocator a, Entity *e, DeclInfo *decl, ssaModule *m) { +ssaValue *ssa_make_value_procedure(gbAllocator a, ssaModule *m, Type *type, AstNode *type_expr, AstNode *body, String name) { ssaValue *v = ssa_alloc_value(a, ssaValue_Proc); v->proc.module = m; - v->proc.entity = e; - v->proc.type = e->type; - v->proc.decl = decl; - v->proc.name = e->token.string; + v->proc.type = type; + v->proc.type_expr = type_expr; + v->proc.body = body; + v->proc.name = name; return v; } @@ -752,9 +755,10 @@ void ssa_emit_defer_stmts(ssaProcedure *proc, ssaBlock *block) { // IMPORTANT TODO(bill): ssa defer - Place where needed!!! +#if 0 Scope *curr_scope = block->scope; if (curr_scope == NULL) { - GB_PANIC("No scope found for deferred statements"); + // GB_PANIC("No scope found for deferred statements"); } for (Scope *s = curr_scope; s != NULL; s = s->parent) { @@ -763,6 +767,7 @@ void ssa_emit_defer_stmts(ssaProcedure *proc, ssaBlock *block) { ssa_build_stmt(proc, s->deferred_stmts[i]); } } +#endif } void ssa_emit_unreachable(ssaProcedure *proc) { @@ -1183,7 +1188,7 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) { } if (is_type_integer(src) && is_type_float(dst)) { ssaConvKind kind = ssaConv_sitofp; - if (is_type_unsigned(dst)) { + if (is_type_unsigned(src)) { kind = ssaConv_uitofp; } return ssa_emit(proc, ssa_make_instr_conv(proc, kind, value, src, dst)); @@ -1331,7 +1336,29 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue case_end; case_ast_node(pl, ProcLit, expr); - GB_PANIC("TODO(bill): ssa_build_single_expr ProcLit"); + if (proc->anon_procs == NULL) { + // TODO(bill): Cleanup + gb_array_init(proc->anon_procs, gb_heap_allocator()); + } + // NOTE(bill): Generate a new name + // parent$count + isize name_len = proc->name.len + 1 + 8 + 1; + u8 *name_text = gb_alloc_array(proc->module->allocator, u8, name_len); + name_len = gb_snprintf(cast(char *)name_text, name_len, "%.*s$%d", LIT(proc->name), cast(i32)gb_array_count(proc->anon_procs)); + String name = make_string(name_text, name_len-1); + + + // auto **found = map_get(&proc->module->info->definitions, + // hash_pointer(expr)) + Type *type = type_of_expr(proc->module->info, expr); + ssaValue *value = ssa_make_value_procedure(proc->module->allocator, + proc->module, type, pl->type, pl->body, name); + ssaProcedure *np = &value->proc; + + gb_array_append(proc->anon_procs, np); + ssa_build_proc(value, proc); + + return value; // TODO(bill): Is this correct? case_end; @@ -1673,6 +1700,7 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { if (!ssa_is_blank_ident(name)) { ssa_add_local_for_identifier(proc, name); lval = ssa_build_addr(proc, name); + GB_ASSERT(lval.address.value != NULL); } gb_array_append(lvals, lval); @@ -1685,8 +1713,10 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { gb_for_array(i, inits) { - ssaValue *v = ssa_emit_conv(proc, inits[i], ssa_lvalue_type(lvals[i])); - ssa_lvalue_store(lvals[i], proc, v); + if (lvals[i].kind != ssaLvalue_Blank) { + ssaValue *v = ssa_emit_conv(proc, inits[i], ssa_lvalue_type(lvals[i])); + ssa_lvalue_store(lvals[i], proc, v); + } } } else if (vd->value_count == 0) { // declared and zero-initialized @@ -1736,6 +1766,10 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { } case_end; + case_ast_node(pd, ProcDecl, node); + + case_end; + case_ast_node(ids, IncDecStmt, node); Token op = ids->op; if (op.kind == Token_Increment) { @@ -1957,18 +1991,10 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) { } } -void ssa_build_proc(ssaValue *value) { +void ssa_build_proc(ssaValue *value, ssaProcedure *parent) { ssaProcedure *proc = &value->proc; - AstNode *proc_decl = proc->decl->proc_decl; - switch (proc_decl->kind) { - case_ast_node(pd, ProcDecl, proc_decl); - proc->type_expr = pd->type; - proc->body = pd->body; - case_end; - default: - return; - } + proc->parent = parent; if (proc->body != NULL) { ssa_begin_procedure_body(proc); diff --git a/src/common.cpp b/src/common.cpp index 2a0cd8371..02d4f4866 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -33,7 +33,7 @@ i64 next_pow2(i64 n) { } -#define gb_for_array(index_, array_) for (isize index_ = 0; index_ < gb_array_count(array_); index_++) +#define gb_for_array(index_, array_) for (isize index_ = 0; (array_) != NULL && index_ < gb_array_count(array_); index_++) // Doubly Linked Lists diff --git a/src/parser.cpp b/src/parser.cpp index ec4918430..026ceac03 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1018,10 +1018,13 @@ AstNode *parse_operand(AstFile *f, b32 lhs) { return type; } else { AstNode *body; + AstScope *curr_scope = f->curr_scope; + f->curr_scope = scope; f->expr_level++; body = parse_body(f, scope); f->expr_level--; + f->curr_scope = curr_scope; return make_procedure_literal(f, type, body); }