copy(...)

This commit is contained in:
gingerBill
2016-08-10 20:07:24 +01:00
parent 153c27c755
commit 4c467b118d
10 changed files with 710 additions and 337 deletions
+19 -17
View File
@@ -14,17 +14,18 @@ string_byte_reverse :: proc(s: string) {
} }
} }
encode_rune :: proc(buf : []u8, r : rune) -> int { encode_rune :: proc(r : rune) -> ([4]u8, int) {
buf : [4]u8;
i := cast(u32)r; i := cast(u32)r;
mask : u8 : 0x3f; mask : u8 : 0x3f;
if i <= 1<<7-1 { if i <= 1<<7-1 {
buf[0] = cast(u8)r; buf[0] = cast(u8)r;
return 1; return buf, 1;
} }
if i <= 1<<11-1 { if i <= 1<<11-1 {
buf[0] = 0xc0 | cast(u8)(r>>6); buf[0] = 0xc0 | cast(u8)(r>>6);
buf[1] = 0x80 | cast(u8)(r)&mask; buf[1] = 0x80 | cast(u8)(r)&mask;
return 2; return buf, 2;
} }
// Invalid or Surrogate range // Invalid or Surrogate range
@@ -37,26 +38,28 @@ encode_rune :: proc(buf : []u8, r : rune) -> int {
buf[0] = 0xe0 | cast(u8)(r>>12); buf[0] = 0xe0 | cast(u8)(r>>12);
buf[1] = 0x80 | cast(u8)(r>>6)&mask; buf[1] = 0x80 | cast(u8)(r>>6)&mask;
buf[2] = 0x80 | cast(u8)(r)&mask; buf[2] = 0x80 | cast(u8)(r)&mask;
return 3; return buf, 3;
} }
buf[0] = 0xf0 | cast(u8)(r>>18); buf[0] = 0xf0 | cast(u8)(r>>18);
buf[1] = 0x80 | cast(u8)(r>>12)&mask; buf[1] = 0x80 | cast(u8)(r>>12)&mask;
buf[2] = 0x80 | cast(u8)(r>>6)&mask; buf[2] = 0x80 | cast(u8)(r>>6)&mask;
buf[3] = 0x80 | cast(u8)(r)&mask; buf[3] = 0x80 | cast(u8)(r)&mask;
return 4; return buf, 4;
} }
print_rune :: proc(r : rune) { print_rune :: proc(r : rune) {
buf : [4]u8; buf, n := encode_rune(r);
n := encode_rune(buf[:], r);
str := cast(string)buf[:n]; str := cast(string)buf[:n];
print_string(str); print_string(str);
} }
print_int :: proc(i, base : int) { print_int :: proc(i : int) {
print_int_base(i, 10);
}
print_int_base :: proc(i, base : int) {
NUM_TO_CHAR_TABLE :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$"; NUM_TO_CHAR_TABLE :: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@$";
NEG :: "-";
buf: [21]u8; buf: [21]u8;
len := 0; len := 0;
@@ -65,17 +68,16 @@ print_int :: proc(i, base : int) {
negative = true; negative = true;
i = -i; i = -i;
} }
if i > 0 { if i == 0 {
for i > 0 {
c : u8 = NUM_TO_CHAR_TABLE[i % base];
buf[len] = c;
len++;
i /= base;
}
} else {
buf[len] = '0'; buf[len] = '0';
len++; len++;
} }
for i > 0 {
c : u8 = NUM_TO_CHAR_TABLE[i % base];
buf[len] = c;
len++;
i /= base;
}
if negative { if negative {
buf[len] = '-'; buf[len] = '-';
+394 -225
View File
@@ -2,56 +2,197 @@
%.rawptr = type i8* ; Basic_rawptr %.rawptr = type i8* ; Basic_rawptr
define {i64, i64} @tuple() { declare void @llvm.memmove.p0i8.p0i8.i64(i8*, i8*, i64, i32, i1)
"entry - 0":
%0 = alloca {i64, i64}, align 8
store {i64, i64} zeroinitializer, {i64, i64}* %0
%1 = getelementptr inbounds {i64, i64}, {i64, i64}* %0, i64 0, i32 0
store i64 1, i64* %1
%2 = getelementptr inbounds {i64, i64}, {i64, i64}* %0, i64 0, i32 1
store i64 2, i64* %2
%3 = load {i64, i64}, {i64, i64}* %0
ret {i64, i64} %3
}
define void @main() { define void @main() {
"entry - 0": "entry - 0":
%0 = alloca i64, align 8 ; a %0 = alloca [4 x i64], align 8 ; dst
store i64 zeroinitializer, i64* %0 store [4 x i64] zeroinitializer, [4 x i64]* %0
%1 = alloca i64, align 8 ; b %1 = alloca [2 x i64], align 8 ; src
store i64 zeroinitializer, i64* %1 store [2 x i64] zeroinitializer, [2 x i64]* %1
%2 = call {i64, i64} @tuple() %2 = getelementptr inbounds [2 x i64], [2 x i64]* %1, i64 0, i64 0
%3 = alloca {i64, i64}, align 8 %3 = getelementptr i64, i64* %2, i64 0
store {i64, i64} zeroinitializer, {i64, i64}* %3 store i64 7, i64* %3
store {i64, i64} %2, {i64, i64}* %3 %4 = getelementptr inbounds [2 x i64], [2 x i64]* %1, i64 0, i64 0
%4 = getelementptr inbounds {i64, i64}, {i64, i64}* %3, i64 0, i32 0 %5 = getelementptr i64, i64* %4, i64 1
%5 = load i64, i64* %4 store i64 5, i64* %5
%6 = getelementptr inbounds {i64, i64}, {i64, i64}* %3, i64 0, i32 1 %6 = sub i64 4, 1
%7 = load i64, i64* %6 %7 = sub i64 4, 1
store i64 %5, i64* %0 %8 = getelementptr inbounds [4 x i64], [4 x i64]* %0, i64 0, i64 0
store i64 %7, i64* %1 %9 = getelementptr i64, i64* %8, i64 1
%8 = load i64, i64* %0 %10 = alloca {i64*, i64, i64}, align 8
call void @print_int(i64 %8, i64 10) store {i64*, i64, i64} zeroinitializer, {i64*, i64, i64}* %10
%9 = getelementptr inbounds [1 x i8], [1 x i8]* @.str0, i64 0, i64 0 %11 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %10, i64 0, i32 0
%10 = alloca %.string, align 8 store i64* %9, i64** %11
store %.string zeroinitializer, %.string* %10 %12 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %10, i64 0, i32 1
%11 = getelementptr inbounds %.string, %.string* %10, i64 0, i32 0 store i64 %6, i64* %12
%12 = getelementptr inbounds %.string, %.string* %10, i64 0, i32 1 %13 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %10, i64 0, i32 2
store i8* %9, i8** %11 store i64 %7, i64* %13
store i64 1, i64* %12 %14 = sub i64 2, 0
%13 = load %.string, %.string* %10 %15 = sub i64 2, 0
call void @print_string(%.string %13) %16 = getelementptr inbounds [2 x i64], [2 x i64]* %1, i64 0, i64 0
%14 = load i64, i64* %1 %17 = getelementptr i64, i64* %16, i64 0
call void @print_int(i64 %14, i64 10) %18 = alloca {i64*, i64, i64}, align 8
%15 = getelementptr inbounds [1 x i8], [1 x i8]* @.str1, i64 0, i64 0 store {i64*, i64, i64} zeroinitializer, {i64*, i64, i64}* %18
%16 = alloca %.string, align 8 %19 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %18, i64 0, i32 0
store %.string zeroinitializer, %.string* %16 store i64* %17, i64** %19
%17 = getelementptr inbounds %.string, %.string* %16, i64 0, i32 0 %20 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %18, i64 0, i32 1
%18 = getelementptr inbounds %.string, %.string* %16, i64 0, i32 1 store i64 %14, i64* %20
store i8* %15, i8** %17 %21 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %18, i64 0, i32 2
store i64 1, i64* %18 store i64 %15, i64* %21
%19 = load %.string, %.string* %16 %22 = getelementptr inbounds {i64*, i64, i64}, {i64*, i64, i64}* %10, i64 0, i32 0
call void @print_string(%.string %19) %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":
ret void ret void
} }
@@ -159,143 +300,164 @@ define void @string_byte_reverse(%.string %s) {
ret void ret void
} }
define i64 @encode_rune({i8*, i64, i64} %buf, i32 %r) { define {[4 x i8], i64} @encode_rune(i32 %r) {
"entry - 0": "entry - 0":
%0 = alloca {i8*, i64, i64}, align 8 ; buf %0 = alloca i32, align 4 ; r
store {i8*, i64, i64} zeroinitializer, {i8*, i64, i64}* %0 store i32 zeroinitializer, i32* %0
store {i8*, i64, i64} %buf, {i8*, i64, i64}* %0 store i32 %r, i32* %0
%1 = alloca i32, align 4 ; r %1 = alloca [4 x i8], align 1 ; buf
store i32 zeroinitializer, i32* %1 store [4 x i8] zeroinitializer, [4 x i8]* %1
store i32 %r, i32* %1
%2 = alloca i32, align 4 ; i %2 = alloca i32, align 4 ; i
store i32 zeroinitializer, i32* %2 store i32 zeroinitializer, i32* %2
%3 = load i32, i32* %1 %3 = load i32, i32* %0
store i32 %3, i32* %2 store i32 %3, i32* %2
%4 = load i32, i32* %2 %4 = load i32, i32* %2
%5 = icmp ule i32 %4, 127 %5 = icmp ule i32 %4, 127
br i1 %5, label %"if.then - 1", label %"if.done - 2" br i1 %5, label %"if.then - 1", label %"if.done - 2"
"if.then - 1": "if.then - 1":
%6 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %6 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%7 = load i8*, i8** %6 %7 = getelementptr i8, i8* %6, i64 0
%8 = getelementptr i8, i8* %7, i64 0 %8 = load i32, i32* %0
%9 = load i32, i32* %1 %9 = trunc i32 %8 to i8
%10 = trunc i32 %9 to i8 store i8 %9, i8* %7
store i8 %10, i8* %8 %10 = alloca {[4 x i8], i64}, align 8
ret i64 1 store {[4 x i8], i64} zeroinitializer, {[4 x i8], i64}* %10
%11 = load [4 x i8], [4 x i8]* %1
%12 = getelementptr inbounds {[4 x i8], i64}, {[4 x i8], i64}* %10, i64 0, i32 0
store [4 x i8] %11, [4 x i8]* %12
%13 = getelementptr inbounds {[4 x i8], i64}, {[4 x i8], i64}* %10, i64 0, i32 1
store i64 1, i64* %13
%14 = load {[4 x i8], i64}, {[4 x i8], i64}* %10
ret {[4 x i8], i64} %14
"if.done - 2": "if.done - 2":
%11 = load i32, i32* %2 %15 = load i32, i32* %2
%12 = icmp ule i32 %11, 2047 %16 = icmp ule i32 %15, 2047
br i1 %12, label %"if.then - 3", label %"if.done - 4" br i1 %16, label %"if.then - 3", label %"if.done - 4"
"if.then - 3": "if.then - 3":
%13 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %17 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%14 = load i8*, i8** %13 %18 = getelementptr i8, i8* %17, i64 0
%15 = getelementptr i8, i8* %14, i64 0 %19 = load i32, i32* %0
%16 = load i32, i32* %1 %20 = lshr i32 %19, 6
%17 = lshr i32 %16, 6 %21 = trunc i32 %20 to i8
%18 = trunc i32 %17 to i8 %22 = or i8 192, %21
%19 = or i8 192, %18 store i8 %22, i8* %18
store i8 %19, i8* %15 %23 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%20 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %24 = getelementptr i8, i8* %23, i64 1
%21 = load i8*, i8** %20 %25 = load i32, i32* %0
%22 = getelementptr i8, i8* %21, i64 1 %26 = trunc i32 %25 to i8
%23 = load i32, i32* %1 %27 = and i8 %26, 63
%24 = trunc i32 %23 to i8 %28 = or i8 128, %27
%25 = and i8 %24, 63 store i8 %28, i8* %24
%26 = or i8 128, %25 %29 = alloca {[4 x i8], i64}, align 8
store i8 %26, i8* %22 store {[4 x i8], i64} zeroinitializer, {[4 x i8], i64}* %29
ret i64 2 %30 = load [4 x i8], [4 x i8]* %1
%31 = getelementptr inbounds {[4 x i8], i64}, {[4 x i8], i64}* %29, i64 0, i32 0
store [4 x i8] %30, [4 x i8]* %31
%32 = getelementptr inbounds {[4 x i8], i64}, {[4 x i8], i64}* %29, i64 0, i32 1
store i64 2, i64* %32
%33 = load {[4 x i8], i64}, {[4 x i8], i64}* %29
ret {[4 x i8], i64} %33
"if.done - 4": "if.done - 4":
%27 = load i32, i32* %2 %34 = load i32, i32* %2
%28 = icmp ugt i32 %27, 1114111 %35 = icmp ugt i32 %34, 1114111
br i1 %28, label %"if.then - 5", label %"cmp-or - 6" br i1 %35, label %"if.then - 5", label %"cmp-or - 6"
"if.then - 5": "if.then - 5":
store i32 65533, i32* %1 store i32 65533, i32* %0
br label %"if.done - 8" br label %"if.done - 8"
"cmp-or - 6": "cmp-or - 6":
%29 = load i32, i32* %2 %36 = load i32, i32* %2
%30 = icmp uge i32 %29, 55296 %37 = icmp uge i32 %36, 55296
br i1 %30, label %"cmp-and - 7", label %"if.done - 8" br i1 %37, label %"cmp-and - 7", label %"if.done - 8"
"cmp-and - 7": "cmp-and - 7":
%31 = load i32, i32* %2 %38 = load i32, i32* %2
%32 = icmp ule i32 %31, 57343 %39 = icmp ule i32 %38, 57343
br i1 %32, label %"if.then - 5", label %"if.done - 8" br i1 %39, label %"if.then - 5", label %"if.done - 8"
"if.done - 8": "if.done - 8":
%33 = load i32, i32* %2 %40 = load i32, i32* %2
%34 = icmp ule i32 %33, 65535 %41 = icmp ule i32 %40, 65535
br i1 %34, label %"if.then - 9", label %"if.done - 10" br i1 %41, label %"if.then - 9", label %"if.done - 10"
"if.then - 9": "if.then - 9":
%35 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %42 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%36 = load i8*, i8** %35 %43 = getelementptr i8, i8* %42, i64 0
%37 = getelementptr i8, i8* %36, i64 0 %44 = load i32, i32* %0
%38 = load i32, i32* %1 %45 = lshr i32 %44, 12
%39 = lshr i32 %38, 12 %46 = trunc i32 %45 to i8
%40 = trunc i32 %39 to i8 %47 = or i8 224, %46
%41 = or i8 224, %40 store i8 %47, i8* %43
store i8 %41, i8* %37 %48 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%42 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %49 = getelementptr i8, i8* %48, i64 1
%43 = load i8*, i8** %42 %50 = load i32, i32* %0
%44 = getelementptr i8, i8* %43, i64 1 %51 = lshr i32 %50, 6
%45 = load i32, i32* %1 %52 = trunc i32 %51 to i8
%46 = lshr i32 %45, 6 %53 = and i8 %52, 63
%47 = trunc i32 %46 to i8 %54 = or i8 128, %53
%48 = and i8 %47, 63 store i8 %54, i8* %49
%49 = or i8 128, %48 %55 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
store i8 %49, i8* %44 %56 = getelementptr i8, i8* %55, i64 2
%50 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %57 = load i32, i32* %0
%51 = load i8*, i8** %50 %58 = trunc i32 %57 to i8
%52 = getelementptr i8, i8* %51, i64 2 %59 = and i8 %58, 63
%53 = load i32, i32* %1 %60 = or i8 128, %59
%54 = trunc i32 %53 to i8 store i8 %60, i8* %56
%55 = and i8 %54, 63 %61 = alloca {[4 x i8], i64}, align 8
%56 = or i8 128, %55 store {[4 x i8], i64} zeroinitializer, {[4 x i8], i64}* %61
store i8 %56, i8* %52 %62 = load [4 x i8], [4 x i8]* %1
ret i64 3 %63 = getelementptr inbounds {[4 x i8], i64}, {[4 x i8], i64}* %61, i64 0, i32 0
store [4 x i8] %62, [4 x i8]* %63
%64 = getelementptr inbounds {[4 x i8], i64}, {[4 x i8], i64}* %61, i64 0, i32 1
store i64 3, i64* %64
%65 = load {[4 x i8], i64}, {[4 x i8], i64}* %61
ret {[4 x i8], i64} %65
"if.done - 10": "if.done - 10":
%57 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %66 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%58 = load i8*, i8** %57 %67 = getelementptr i8, i8* %66, i64 0
%59 = getelementptr i8, i8* %58, i64 0 %68 = load i32, i32* %0
%60 = load i32, i32* %1 %69 = lshr i32 %68, 18
%61 = lshr i32 %60, 18 %70 = trunc i32 %69 to i8
%62 = trunc i32 %61 to i8 %71 = or i8 240, %70
%63 = or i8 240, %62 store i8 %71, i8* %67
store i8 %63, i8* %59 %72 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%64 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %73 = getelementptr i8, i8* %72, i64 1
%65 = load i8*, i8** %64 %74 = load i32, i32* %0
%66 = getelementptr i8, i8* %65, i64 1 %75 = lshr i32 %74, 12
%67 = load i32, i32* %1 %76 = trunc i32 %75 to i8
%68 = lshr i32 %67, 12 %77 = and i8 %76, 63
%69 = trunc i32 %68 to i8 %78 = or i8 128, %77
%70 = and i8 %69, 63 store i8 %78, i8* %73
%71 = or i8 128, %70 %79 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
store i8 %71, i8* %66 %80 = getelementptr i8, i8* %79, i64 2
%72 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %81 = load i32, i32* %0
%73 = load i8*, i8** %72 %82 = lshr i32 %81, 6
%74 = getelementptr i8, i8* %73, i64 2 %83 = trunc i32 %82 to i8
%75 = load i32, i32* %1 %84 = and i8 %83, 63
%76 = lshr i32 %75, 6 %85 = or i8 128, %84
%77 = trunc i32 %76 to i8 store i8 %85, i8* %80
%78 = and i8 %77, 63 %86 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%79 = or i8 128, %78 %87 = getelementptr i8, i8* %86, i64 3
store i8 %79, i8* %74 %88 = load i32, i32* %0
%80 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %0, i64 0, i32 0 %89 = trunc i32 %88 to i8
%81 = load i8*, i8** %80 %90 = and i8 %89, 63
%82 = getelementptr i8, i8* %81, i64 3 %91 = or i8 128, %90
%83 = load i32, i32* %1 store i8 %91, i8* %87
%84 = trunc i32 %83 to i8 %92 = alloca {[4 x i8], i64}, align 8
%85 = and i8 %84, 63 store {[4 x i8], i64} zeroinitializer, {[4 x i8], i64}* %92
%86 = or i8 128, %85 %93 = load [4 x i8], [4 x i8]* %1
store i8 %86, i8* %82 %94 = getelementptr inbounds {[4 x i8], i64}, {[4 x i8], i64}* %92, i64 0, i32 0
ret i64 4 store [4 x i8] %93, [4 x i8]* %94
%95 = getelementptr inbounds {[4 x i8], i64}, {[4 x i8], i64}* %92, i64 0, i32 1
store i64 4, i64* %95
%96 = load {[4 x i8], i64}, {[4 x i8], i64}* %92
ret {[4 x i8], i64} %96
} }
define void @print_rune(i32 %r) { define void @print_rune(i32 %r) {
@@ -307,59 +469,59 @@ define void @print_rune(i32 %r) {
store [4 x i8] zeroinitializer, [4 x i8]* %1 store [4 x i8] zeroinitializer, [4 x i8]* %1
%2 = alloca i64, align 8 ; n %2 = alloca i64, align 8 ; n
store i64 zeroinitializer, i64* %2 store i64 zeroinitializer, i64* %2
%3 = sub i64 4, 0 %3 = load i32, i32* %0
%4 = sub i64 4, 0 %4 = call {[4 x i8], i64} @encode_rune(i32 %3)
%5 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0 %5 = extractvalue {[4 x i8], i64} %4, 0
%6 = getelementptr i8, i8* %5, i64 0 %6 = extractvalue {[4 x i8], i64} %4, 1
%7 = alloca {i8*, i64, i64}, align 8 store [4 x i8] %5, [4 x i8]* %1
store {i8*, i64, i64} zeroinitializer, {i8*, i64, i64}* %7 store i64 %6, i64* %2
%8 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %7, i64 0, i32 0 %7 = alloca %.string, align 8 ; str
store i8* %6, i8** %8 store %.string zeroinitializer, %.string* %7
%9 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %7, i64 0, i32 1 %8 = load i64, i64* %2
store i64 %3, i64* %9 %9 = sub i64 %8, 0
%10 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %7, i64 0, i32 2 %10 = sub i64 4, 0
store i64 %4, i64* %10 %11 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%11 = load {i8*, i64, i64}, {i8*, i64, i64}* %7 %12 = getelementptr i8, i8* %11, i64 0
%12 = load i32, i32* %0 %13 = alloca {i8*, i64, i64}, align 8
%13 = call i64 @encode_rune({i8*, i64, i64} %11, i32 %12) store {i8*, i64, i64} zeroinitializer, {i8*, i64, i64}* %13
store i64 %13, i64* %2 %14 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %13, i64 0, i32 0
%14 = alloca %.string, align 8 ; str store i8* %12, i8** %14
store %.string zeroinitializer, %.string* %14 %15 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %13, i64 0, i32 1
%15 = load i64, i64* %2 store i64 %9, i64* %15
%16 = sub i64 %15, 0 %16 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %13, i64 0, i32 2
%17 = sub i64 4, 0 store i64 %10, i64* %16
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0 %17 = load {i8*, i64, i64}, {i8*, i64, i64}* %13
%19 = getelementptr i8, i8* %18, i64 0 %18 = alloca {i8*, i64, i64}, align 8
%20 = alloca {i8*, i64, i64}, align 8 store {i8*, i64, i64} zeroinitializer, {i8*, i64, i64}* %18
store {i8*, i64, i64} zeroinitializer, {i8*, i64, i64}* %20 store {i8*, i64, i64} %17, {i8*, i64, i64}* %18
%21 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %20, i64 0, i32 0 %19 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %18, i64 0, i32 0
store i8* %19, i8** %21 %20 = load i8*, i8** %19
%22 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %20, i64 0, i32 1 %21 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %18, i64 0, i32 1
store i64 %16, i64* %22 %22 = load i64, i64* %21
%23 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %20, i64 0, i32 2 %23 = alloca %.string, align 8
store i64 %17, i64* %23 store %.string zeroinitializer, %.string* %23
%24 = load {i8*, i64, i64}, {i8*, i64, i64}* %20 %24 = getelementptr inbounds %.string, %.string* %23, i64 0, i32 0
%25 = alloca {i8*, i64, i64}, align 8 %25 = getelementptr inbounds %.string, %.string* %23, i64 0, i32 1
store {i8*, i64, i64} zeroinitializer, {i8*, i64, i64}* %25 store i8* %20, i8** %24
store {i8*, i64, i64} %24, {i8*, i64, i64}* %25 store i64 %22, i64* %25
%26 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %25, i64 0, i32 0 %26 = load %.string, %.string* %23
%27 = load i8*, i8** %26 store %.string %26, %.string* %7
%28 = getelementptr inbounds {i8*, i64, i64}, {i8*, i64, i64}* %25, i64 0, i32 1 %27 = load %.string, %.string* %7
%29 = load i64, i64* %28 call void @print_string(%.string %27)
%30 = alloca %.string, align 8
store %.string zeroinitializer, %.string* %30
%31 = getelementptr inbounds %.string, %.string* %30, i64 0, i32 0
%32 = getelementptr inbounds %.string, %.string* %30, i64 0, i32 1
store i8* %27, i8** %31
store i64 %29, i64* %32
%33 = load %.string, %.string* %30
store %.string %33, %.string* %14
%34 = load %.string, %.string* %14
call void @print_string(%.string %34)
ret void ret void
} }
define void @print_int(i64 %i, i64 %base) { define void @print_int(i64 %i) {
"entry - 0":
%0 = alloca i64, align 8 ; i
store i64 zeroinitializer, i64* %0
store i64 %i, i64* %0
%1 = load i64, i64* %0
call void @print_int_base(i64 %1, i64 10)
ret void
}
define void @print_int_base(i64 %i, i64 %base) {
"entry - 0": "entry - 0":
%0 = alloca i64, align 8 ; i %0 = alloca i64, align 8 ; i
store i64 zeroinitializer, i64* %0 store i64 zeroinitializer, i64* %0
@@ -388,26 +550,26 @@ define void @print_int(i64 %i, i64 %base) {
"if.done - 2": "if.done - 2":
%9 = load i64, i64* %0 %9 = load i64, i64* %0
%10 = icmp sgt i64 %9, 0 %10 = icmp eq i64 %9, 0
br i1 %10, label %"if.then - 3", label %"if.else - 4" br i1 %10, label %"if.then - 3", label %"if.done - 4"
"if.then - 3": "if.then - 3":
br label %"for.loop - 6"
"if.else - 4":
%11 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0 %11 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0
%12 = load i64, i64* %3 %12 = load i64, i64* %3
%13 = getelementptr i8, i8* %11, i64 %12 %13 = getelementptr i8, i8* %11, i64 %12
store i8 0, i8* %13 store i8 48, i8* %13
%14 = load i64, i64* %3 %14 = load i64, i64* %3
%15 = add i64 %14, 1 %15 = add i64 %14, 1
store i64 %15, i64* %3 store i64 %15, i64* %3
br label %"if.done - 8" br label %"if.done - 4"
"if.done - 4":
br label %"for.loop - 6"
"for.body - 5": "for.body - 5":
%16 = alloca i8, align 1 ; c %16 = alloca i8, align 1 ; c
store i8 zeroinitializer, i8* %16 store i8 zeroinitializer, i8* %16
%17 = getelementptr inbounds [64 x i8], [64 x i8]* @.str2, i64 0, i64 0 %17 = getelementptr inbounds [64 x i8], [64 x i8]* @.strc, i64 0, i64 0
%18 = load i64, i64* %1 %18 = load i64, i64* %1
%19 = load i64, i64* %0 %19 = load i64, i64* %0
%20 = srem i64 %19, %18 %20 = srem i64 %19, %18
@@ -434,23 +596,20 @@ define void @print_int(i64 %i, i64 %base) {
br i1 %33, label %"for.body - 5", label %"for.done - 7" br i1 %33, label %"for.body - 5", label %"for.done - 7"
"for.done - 7": "for.done - 7":
br label %"if.done - 8"
"if.done - 8":
%34 = load i1, i1* %4 %34 = load i1, i1* %4
br i1 %34, label %"if.then - 9", label %"if.done - 10" br i1 %34, label %"if.then - 8", label %"if.done - 9"
"if.then - 9": "if.then - 8":
%35 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0 %35 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0
%36 = load i64, i64* %3 %36 = load i64, i64* %3
%37 = getelementptr i8, i8* %35, i64 %36 %37 = getelementptr i8, i8* %35, i64 %36
store i8 0, i8* %37 store i8 45, i8* %37
%38 = load i64, i64* %3 %38 = load i64, i64* %3
%39 = add i64 %38, 1 %39 = add i64 %38, 1
store i64 %39, i64* %3 store i64 %39, i64* %3
br label %"if.done - 10" br label %"if.done - 9"
"if.done - 10": "if.done - 9":
%40 = alloca %.string, align 8 ; str %40 = alloca %.string, align 8 ; str
store %.string zeroinitializer, %.string* %40 store %.string zeroinitializer, %.string* %40
%41 = load i64, i64* %3 %41 = load i64, i64* %3
@@ -489,6 +648,16 @@ define void @print_int(i64 %i, i64 %base) {
ret void ret void
} }
@.str0 = global [1 x i8] c"\0A" @.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 [1 x i8] c"\0A" @.str1 = global [26 x i8] c"Dutch\20\20\20\20\20\20-\20Hello\20wereld\0A"
@.str2 = global [64 x i8] c"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\40$" @.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$"
+23 -23
View File
@@ -2,31 +2,31 @@ import "basic"
TWO_HEARTS :: '💕'; TWO_HEARTS :: '💕';
tuple :: proc() -> (int, int) {
return 1, 2;
}
main :: proc() { main :: proc() {
a, b : int = tuple(); dst : [4]int;
src : [2]int;
src[0] = 7;
src[1] = 5;
print_int(a, 10); _ = copy(dst[1:], src[:]);
print_string("\n");
print_int(b, 10);
print_string("\n");
/* for i := 0; i < len(dst); i++ {
print_string("Chinese - \n"); print_int(dst[i]);
print_string("Dutch - Hello wereld\n"); }
print_string("English - Hello world\n");
print_string("French - Bonjour monde\n"); if false {
print_string("German - Hallo Welt\n"); print_string("Chinese - 你好世界\n");
print_string("Greek - γειά σου κόσμος\n"); print_string("Dutch - Hello wereld\n");
print_string("Italian - Ciao mondo\n"); print_string("English - Hello world\n");
print_string("Japanese - \n"); print_string("French - Bonjour monde\n");
print_string("Korean - \n"); print_string("German - Hallo Welt\n");
print_string("Portuguese - Olá mundo\n"); print_string("Greek - γειά σου κόσμος\n");
print_string("Russian - Здравствулте мир\n"); print_string("Italian - Ciao mondo\n");
print_string("Spanish - Hola mundo\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");
}
} }
+8
View File
@@ -522,6 +522,14 @@ void pop_procedure(Checker *c) {
gb_array_pop(c->proc_stack); gb_array_pop(c->proc_stack);
} }
Type *const curr_procedure(Checker *c) {
isize count = gb_array_count(c->proc_stack);
if (count > 0) {
return c->proc_stack[count-1];
}
return NULL;
}
void add_curr_ast_file(Checker *c, AstFile *file) { void add_curr_ast_file(Checker *c, AstFile *file) {
TokenPos zero_pos = {}; TokenPos zero_pos = {};
c->error_collector.prev = zero_pos; c->error_collector.prev = zero_pos;
+12
View File
@@ -1553,6 +1553,7 @@ ExpressionKind check_call_expr(Checker *c, Operand *operand, AstNode *call) {
return Expression_Statement; return Expression_Statement;
} }
if (operand->mode == Addressing_Builtin) { if (operand->mode == Addressing_Builtin) {
i32 id = operand->builtin_id; i32 id = operand->builtin_id;
if (!check_builtin_procedure(c, operand, call, id)) if (!check_builtin_procedure(c, operand, call, id))
@@ -1574,6 +1575,17 @@ ExpressionKind check_call_expr(Checker *c, Operand *operand, AstNode *call) {
return Expression_Statement; return Expression_Statement;
} }
if (curr_procedure(c) == NULL) {
AstNode *e = operand->expr;
gbString str = expr_to_string(e);
defer (gb_string_free(str));
error(&c->error_collector, ast_node_token(e), "Can ony call procedure within a procedure: `%s`", str);
operand->mode = Addressing_Invalid;
operand->expr = call;
return Expression_Statement;
}
check_call_arguments(c, operand, proc_type, call); check_call_arguments(c, operand, proc_type, call);
+10 -1
View File
@@ -46,6 +46,8 @@ void ssa_gen_code(ssaGen *s) {
ssaModule *m = &s->module; ssaModule *m = &s->module;
CheckerInfo *info = m->info; CheckerInfo *info = m->info;
gbAllocator a = m->allocator; gbAllocator a = m->allocator;
ssaProcedure dummy_proc = {};
dummy_proc.module = m;
gb_for_array(i, info->entities.entries) { gb_for_array(i, info->entities.entries) {
auto *entry = &info->entities.entries[i]; auto *entry = &info->entities.entries[i];
@@ -61,7 +63,14 @@ void ssa_gen_code(ssaGen *s) {
} break; } break;
case Entity_Variable: { case Entity_Variable: {
ssaValue *g = ssa_make_value_global(a, e, NULL); 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);
map_set(&m->values, hash_pointer(e), g); map_set(&m->values, hash_pointer(e), g);
map_set(&m->members, hash_string(name), g); map_set(&m->members, hash_string(name), g);
} break; } break;
+53 -4
View File
@@ -312,8 +312,9 @@ void ssa_print_instr(gbFile *f, ssaModule *m, ssaValue *value) {
case ssaInstr_GetElementPtr: { case ssaInstr_GetElementPtr: {
Type *et = instr->get_element_ptr.elem_type; Type *et = instr->get_element_ptr.elem_type;
ssa_fprintf(f, "%%%d = getelementptr ", value->id); ssa_fprintf(f, "%%%d = getelementptr ", value->id);
if (instr->get_element_ptr.inbounds) if (instr->get_element_ptr.inbounds) {
ssa_fprintf(f, "inbounds "); ssa_fprintf(f, "inbounds ");
}
ssa_print_type(f, m->sizes, et); ssa_print_type(f, m->sizes, et);
ssa_fprintf(f, ", "); ssa_fprintf(f, ", ");
@@ -331,6 +332,16 @@ void ssa_print_instr(gbFile *f, ssaModule *m, ssaValue *value) {
ssa_fprintf(f, "\n"); ssa_fprintf(f, "\n");
} break; } break;
case ssaInstr_ExtractValue: {
Type *et = instr->extract_value.elem_type;
ssa_fprintf(f, "%%%d = extractvalue ", value->id);
ssa_print_type(f, m->sizes, et);
ssa_fprintf(f, " ");
ssa_print_value(f, m, instr->extract_value.address, et);
ssa_fprintf(f, ", %d\n", instr->extract_value.index);
} break;
case ssaInstr_Br: { case ssaInstr_Br: {
ssa_fprintf(f, "br "); ssa_fprintf(f, "br ");
if (instr->br.cond != NULL) { if (instr->br.cond != NULL) {
@@ -494,7 +505,41 @@ void ssa_print_instr(gbFile *f, ssaModule *m, ssaValue *value) {
} break; } break;
case ssaInstr_Select: {
ssa_fprintf(f, "%%%d = select i1 ", value->id);
ssa_print_value(f, m, instr->select.cond, t_bool);
ssa_fprintf(f, ", ");
ssa_print_type(f, m->sizes, ssa_value_type(instr->select.true_value));
ssa_fprintf(f, " ");
ssa_print_value(f, m, instr->select.true_value, ssa_value_type(instr->select.true_value));
ssa_fprintf(f, ", ");
ssa_print_type(f, m->sizes, ssa_value_type(instr->select.false_value));
ssa_fprintf(f, " ");
ssa_print_value(f, m, instr->select.false_value, ssa_value_type(instr->select.false_value));
ssa_fprintf(f, "\n");
} break;
case ssaInstr_CopyMemory: {
ssa_fprintf(f, "call void @llvm.memmove.p0i8.p0i8.");
ssa_print_type(f, m->sizes, t_int);
ssa_fprintf(f, "(i8* ");
ssa_print_value(f, m, instr->copy_memory.dst, t_rawptr);
ssa_fprintf(f, ", i8* ");
ssa_print_value(f, m, instr->copy_memory.src, t_rawptr);
ssa_fprintf(f, ", ");
ssa_print_type(f, m->sizes, t_int);
ssa_fprintf(f, " ");
ssa_print_value(f, m, instr->copy_memory.len, t_int);
char *vol_str = "false";
if (instr->copy_memory.is_volatile) {
vol_str = "true";
}
ssa_fprintf(f, ", i32 %d, i1 %s)\n", instr->copy_memory.align, vol_str);
} break;
default: default:
GB_PANIC("<unknown instr> %d\n", instr->kind);
ssa_fprintf(f, "; <unknown instr> %d\n", instr->kind); ssa_fprintf(f, "; <unknown instr> %d\n", instr->kind);
break; break;
} }
@@ -511,8 +556,12 @@ void ssa_print_llvm_ir(gbFile *f, ssaModule *m) {
ssa_fprintf(f, "} ; Basic_string\n\n"); ssa_fprintf(f, "} ; Basic_string\n\n");
ssa_print_encoded_local(f, make_string(".rawptr")); ssa_print_encoded_local(f, make_string(".rawptr"));
ssa_fprintf(f, " = type i8*"); ssa_fprintf(f, " = type i8* ; Basic_rawptr\n\n");
ssa_fprintf(f, " ; 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)\n\n");
gb_for_array(member_index, m->members.entries) { gb_for_array(member_index, m->members.entries) {
auto *entry = &m->members.entries[member_index]; auto *entry = &m->members.entries[member_index];
+178 -66
View File
@@ -60,12 +60,15 @@ struct ssaProcedure {
SSA_INSTR_KIND(Store), \ SSA_INSTR_KIND(Store), \
SSA_INSTR_KIND(Load), \ SSA_INSTR_KIND(Load), \
SSA_INSTR_KIND(GetElementPtr), \ SSA_INSTR_KIND(GetElementPtr), \
SSA_INSTR_KIND(ExtractValue), \
SSA_INSTR_KIND(Conv), \ SSA_INSTR_KIND(Conv), \
SSA_INSTR_KIND(Br), \ SSA_INSTR_KIND(Br), \
SSA_INSTR_KIND(Ret), \ SSA_INSTR_KIND(Ret), \
SSA_INSTR_KIND(Select), \
SSA_INSTR_KIND(Unreachable), \ SSA_INSTR_KIND(Unreachable), \
SSA_INSTR_KIND(BinaryOp), \ SSA_INSTR_KIND(BinaryOp), \
SSA_INSTR_KIND(Call), \ SSA_INSTR_KIND(Call), \
SSA_INSTR_KIND(CopyMemory), \
SSA_INSTR_KIND(Count), SSA_INSTR_KIND(Count),
enum ssaInstrKind { enum ssaInstrKind {
@@ -135,6 +138,12 @@ struct ssaInstr {
isize index_count; isize index_count;
b32 inbounds; b32 inbounds;
} get_element_ptr; } get_element_ptr;
struct {
ssaValue *address;
Type * result_type;
Type * elem_type;
i32 index;
} extract_value;
struct { struct {
ssaConvKind kind; ssaConvKind kind;
ssaValue *value; ssaValue *value;
@@ -147,6 +156,11 @@ struct ssaInstr {
} br; } br;
struct { ssaValue *value; } ret; struct { ssaValue *value; } ret;
struct {} unreachable; struct {} unreachable;
struct {
ssaValue *cond;
ssaValue *true_value;
ssaValue *false_value;
} select;
struct { struct {
Type *type; Type *type;
Token op; Token op;
@@ -158,6 +172,12 @@ struct ssaInstr {
ssaValue **args; ssaValue **args;
isize arg_count; isize arg_count;
} call; } call;
struct {
ssaValue *dst, *src;
ssaValue *len;
i32 align;
b32 is_volatile;
} copy_memory;
}; };
}; };
@@ -280,10 +300,14 @@ Type *ssa_instr_type(ssaInstr *instr) {
return instr->load.type; return instr->load.type;
case ssaInstr_GetElementPtr: case ssaInstr_GetElementPtr:
return instr->get_element_ptr.result_type; return instr->get_element_ptr.result_type;
case ssaInstr_ExtractValue:
return instr->extract_value.result_type;
case ssaInstr_BinaryOp: case ssaInstr_BinaryOp:
return instr->binary_op.type; return instr->binary_op.type;
case ssaInstr_Conv: case ssaInstr_Conv:
return instr->conv.to; return instr->conv.to;
case ssaInstr_Select:
return ssa_value_type(instr->select.true_value);
case ssaInstr_Call: { case ssaInstr_Call: {
Type *pt = instr->call.type; Type *pt = instr->call.type;
GB_ASSERT(pt->kind == Type_Proc); GB_ASSERT(pt->kind == Type_Proc);
@@ -293,6 +317,8 @@ Type *ssa_instr_type(ssaInstr *instr) {
else else
return tuple->variables[0]->type; return tuple->variables[0]->type;
} }
case ssaInstr_CopyMemory:
return t_int;
} }
return NULL; return NULL;
} }
@@ -311,6 +337,9 @@ void ssa_instr_set_type(ssaInstr *instr, Type *type) {
case ssaInstr_GetElementPtr: case ssaInstr_GetElementPtr:
instr->get_element_ptr.result_type = type; instr->get_element_ptr.result_type = type;
break; break;
case ssaInstr_ExtractValue:
instr->extract_value.result_type = type;
break;
case ssaInstr_BinaryOp: case ssaInstr_BinaryOp:
instr->binary_op.type = type; instr->binary_op.type = type;
break; break;
@@ -459,6 +488,22 @@ ssaValue *ssa_make_instr_get_element_ptr(ssaProcedure *p, ssaValue *address,
return v; return v;
} }
ssaValue *ssa_make_instr_extract_value(ssaProcedure *p, ssaValue *address, i32 index, Type *result_type) {
ssaValue *v = ssa_alloc_instr(p->module->allocator, ssaInstr_ExtractValue);
ssaInstr *i = &v->instr;
i->extract_value.address = address;
i->extract_value.index = index;
i->extract_value.result_type = result_type;
Type *et = ssa_value_type(address);
i->extract_value.elem_type = et;
GB_ASSERT(et->kind == Type_Structure || et->kind == Type_Array || et->kind == Type_Tuple);
if (p->curr_block) {
gb_array_append(p->curr_block->values, v);
}
return v;
}
ssaValue *ssa_make_instr_binary_op(ssaProcedure *p, Token op, ssaValue *left, ssaValue *right) { ssaValue *ssa_make_instr_binary_op(ssaProcedure *p, Token op, ssaValue *left, ssaValue *right) {
ssaValue *v = ssa_alloc_instr(p->module->allocator, ssaInstr_BinaryOp); ssaValue *v = ssa_alloc_instr(p->module->allocator, ssaInstr_BinaryOp);
ssaInstr *i = &v->instr; ssaInstr *i = &v->instr;
@@ -500,6 +545,17 @@ ssaValue *ssa_make_instr_ret(ssaProcedure *p, ssaValue *value) {
return v; return v;
} }
ssaValue *ssa_make_instr_select(ssaProcedure *p, ssaValue *cond, ssaValue *t, ssaValue *f) {
ssaValue *v = ssa_alloc_instr(p->module->allocator, ssaInstr_Select);
v->instr.select.cond = cond;
v->instr.select.true_value = t;
v->instr.select.false_value = f;
if (p->curr_block) {
gb_array_append(p->curr_block->values, v);
}
return v;
}
ssaValue *ssa_make_instr_call(ssaProcedure *p, ssaValue *value, ssaValue **args, isize arg_count, Type *result_type) { ssaValue *ssa_make_instr_call(ssaProcedure *p, ssaValue *value, ssaValue **args, isize arg_count, Type *result_type) {
ssaValue *v = ssa_alloc_instr(p->module->allocator, ssaInstr_Call); ssaValue *v = ssa_alloc_instr(p->module->allocator, ssaInstr_Call);
v->instr.call.value = value; v->instr.call.value = value;
@@ -512,6 +568,19 @@ ssaValue *ssa_make_instr_call(ssaProcedure *p, ssaValue *value, ssaValue **args,
return v; return v;
} }
ssaValue *ssa_make_instr_copy_memory(ssaProcedure *p, ssaValue *dst, ssaValue *src, ssaValue *len, i32 align, b32 is_volatile) {
ssaValue *v = ssa_alloc_instr(p->module->allocator, ssaInstr_CopyMemory);
v->instr.copy_memory.dst = dst;
v->instr.copy_memory.src = src;
v->instr.copy_memory.len = len;
v->instr.copy_memory.align = align;
v->instr.copy_memory.is_volatile = is_volatile;
if (p->curr_block) {
gb_array_append(p->curr_block->values, v);
}
return v;
}
ssaValue *ssa_make_instr_conv(ssaProcedure *p, ssaConvKind kind, ssaValue *value, Type *from, Type *to) { ssaValue *ssa_make_instr_conv(ssaProcedure *p, ssaConvKind kind, ssaValue *value, Type *from, Type *to) {
ssaValue *v = ssa_alloc_instr(p->module->allocator, ssaInstr_Conv); ssaValue *v = ssa_alloc_instr(p->module->allocator, ssaInstr_Conv);
v->instr.conv.kind = kind; v->instr.conv.kind = kind;
@@ -557,7 +626,6 @@ ssaValue *ssa_make_value_block(ssaProcedure *proc, AstNode *node, Scope *scope,
return v; return v;
} }
b32 ssa_is_blank_ident(AstNode *node) { b32 ssa_is_blank_ident(AstNode *node) {
if (node->kind == AstNode_Ident) { if (node->kind == AstNode_Ident) {
ast_node(i, Ident, node); ast_node(i, Ident, node);
@@ -578,15 +646,24 @@ ssaInstr *ssa_get_last_instr(ssaBlock *block) {
} }
b32 ssa_is_instr_terminating(ssaInstr *i) {
if (i != NULL) {
switch (i->kind) {
case ssaInstr_Ret:
case ssaInstr_Unreachable:
return true;
}
}
return false;
}
ssaValue *ssa_emit(ssaProcedure *proc, ssaValue *instr) { ssaValue *ssa_emit(ssaProcedure *proc, ssaValue *instr) {
ssaBlock *b = proc->curr_block; ssaBlock *b = proc->curr_block;
instr->instr.parent = b; instr->instr.parent = b;
if (b) { if (b) {
ssaInstr *i = ssa_get_last_instr(b); ssaInstr *i = ssa_get_last_instr(b);
if (i && (i->kind == ssaInstr_Ret || i->kind == ssaInstr_Unreachable)) { if (!ssa_is_instr_terminating(i)) {
// NOTE(bill): any instruction in the current block after a `ret`
// or an `unreachable`, is never executed
} else {
gb_array_append(b->instrs, instr); gb_array_append(b->instrs, instr);
} }
} }
@@ -598,7 +675,9 @@ ssaValue *ssa_emit_store(ssaProcedure *p, ssaValue *address, ssaValue *value) {
ssaValue *ssa_emit_load(ssaProcedure *p, ssaValue *address) { ssaValue *ssa_emit_load(ssaProcedure *p, ssaValue *address) {
return ssa_emit(p, ssa_make_instr_load(p, address)); return ssa_emit(p, ssa_make_instr_load(p, address));
} }
ssaValue *ssa_emit_select(ssaProcedure *p, ssaValue *cond, ssaValue *t, ssaValue *f) {
return ssa_emit(p, ssa_make_instr_select(p, cond, t, f));
}
ssaValue *ssa_add_local(ssaProcedure *proc, Entity *e) { ssaValue *ssa_add_local(ssaProcedure *proc, Entity *e) {
@@ -764,6 +843,7 @@ void ssa_end_procedure_body(ssaProcedure *proc) {
case ssaInstr_Br: case ssaInstr_Br:
case ssaInstr_Ret: case ssaInstr_Ret:
case ssaInstr_Unreachable: case ssaInstr_Unreachable:
case ssaInstr_CopyMemory:
continue; continue;
case ssaInstr_Call: case ssaInstr_Call:
if (instr->call.type->proc.results == NULL) { if (instr->call.type->proc.results == NULL) {
@@ -859,6 +939,18 @@ ssaValue *ssa_emit_struct_gep(ssaProcedure *proc, ssaValue *s, ssaValue *index,
return ssa_emit(proc, gep); return ssa_emit(proc, gep);
} }
ssaValue *ssa_emit_struct_gep(ssaProcedure *proc, ssaValue *s, i32 index, Type *result_type) {
ssaValue *i = ssa_make_value_constant(proc->module->allocator, t_i32, make_exact_value_integer(index));
return ssa_emit_struct_gep(proc, s, i, result_type);
}
ssaValue *ssa_emit_struct_ev(ssaProcedure *proc, ssaValue *s, i32 index, Type *result_type) {
// NOTE(bill): For some weird legacy reason in LLVM, structure elements must be accessed as an i32
return ssa_emit(proc, ssa_make_instr_extract_value(proc, s, index, result_type));
}
ssaValue *ssa_array_elem(ssaProcedure *proc, ssaValue *array) { ssaValue *ssa_array_elem(ssaProcedure *proc, ssaValue *array) {
Type *t = ssa_value_type(array); Type *t = ssa_value_type(array);
@@ -967,7 +1059,7 @@ ssaValue *ssa_emit_slice(ssaProcedure *proc, Type *slice_type, ssaValue *base, s
gep = ssa_emit_struct_gep(proc, slice, v_two32, t_int); gep = ssa_emit_struct_gep(proc, slice, v_two32, t_int);
ssa_emit_store(proc, gep, cap); ssa_emit_store(proc, gep, cap);
return ssa_emit_load(proc, slice); return slice;
} }
ssaValue *ssa_emit_substring(ssaProcedure *proc, ssaValue *base, ssaValue *low, ssaValue *high) { ssaValue *ssa_emit_substring(ssaProcedure *proc, ssaValue *base, ssaValue *low, ssaValue *high) {
@@ -981,20 +1073,21 @@ ssaValue *ssa_emit_substring(ssaProcedure *proc, ssaValue *base, ssaValue *low,
} }
Token op_sub = {Token_Sub}; Token op_sub = {Token_Sub};
ssaValue *len = ssa_emit_arith(proc, op_sub, high, low, t_int); ssaValue *elem, *len;
len = ssa_emit_arith(proc, op_sub, high, low, t_int);
ssaValue *elem = ssa_string_elem(proc, base); elem = ssa_string_elem(proc, base);
elem = ssa_emit_ptr_offset(proc, elem, low); elem = ssa_emit_ptr_offset(proc, elem, low);
ssaValue *str = ssa_add_local_generated(proc, t_string); ssaValue *str, *gep;
ssaValue *gep = NULL; str = ssa_add_local_generated(proc, t_string);
gep = ssa_emit_struct_gep(proc, str, v_zero32, ssa_value_type(elem)); gep = ssa_emit_struct_gep(proc, str, v_zero32, ssa_value_type(elem));
ssa_emit_store(proc, gep, elem); ssa_emit_store(proc, gep, elem);
gep = ssa_emit_struct_gep(proc, str, v_one32, t_int); gep = ssa_emit_struct_gep(proc, str, v_one32, t_int);
ssa_emit_store(proc, gep, len); ssa_emit_store(proc, gep, len);
return ssa_emit_load(proc, str); return str;
} }
@@ -1012,9 +1105,7 @@ ssaValue *ssa_add_global_string_array(ssaProcedure *proc, ExactValue value) {
token.string = name; token.string = name;
Type *type = make_type_array(a, t_u8, value.value_string.len); Type *type = make_type_array(a, t_u8, value.value_string.len);
Entity *entity = make_entity_constant(a, NULL, token, type, value); Entity *entity = make_entity_constant(a, NULL, token, type, value);
ssaValue *v = ssa_make_value_constant(a, type, value); ssaValue *g = ssa_make_value_global(a, entity, ssa_make_value_constant(a, type, value));
ssaValue *g = ssa_make_value_global(a, entity, v);
map_set(&proc->module->values, hash_pointer(entity), g); map_set(&proc->module->values, hash_pointer(entity), g);
map_set(&proc->module->members, hash_string(name), g); map_set(&proc->module->members, hash_string(name), g);
@@ -1033,7 +1124,7 @@ ssaValue *ssa_emit_string(ssaProcedure *proc, ssaValue *elem, ssaValue *len) {
ssaValue *str_len = ssa_emit_struct_gep(proc, str, v_one32, t_int); ssaValue *str_len = ssa_emit_struct_gep(proc, str, v_one32, t_int);
ssa_emit_store(proc, str_elem, elem); ssa_emit_store(proc, str_elem, elem);
ssa_emit_store(proc, str_len, len); ssa_emit_store(proc, str_len, len);
return ssa_emit_load(proc, str); return str;
} }
@@ -1120,15 +1211,14 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) {
ssa_emit_store(proc, slice, value); ssa_emit_store(proc, slice, value);
ssaValue *elem = ssa_slice_elem(proc, slice); ssaValue *elem = ssa_slice_elem(proc, slice);
ssaValue *len = ssa_slice_len(proc, slice); ssaValue *len = ssa_slice_len(proc, slice);
return ssa_emit_string(proc, elem, len); return ssa_emit_load(proc, ssa_emit_string(proc, elem, len));
} }
if (is_type_string(src) && is_type_u8_slice(dst)) { if (is_type_string(src) && is_type_u8_slice(dst)) {
ssaValue *str = ssa_add_local_generated(proc, src); ssaValue *str = ssa_add_local_generated(proc, src);
ssa_emit_store(proc, str, value); ssa_emit_store(proc, str, value);
ssaValue *elem = ssa_string_elem(proc, str); ssaValue *elem = ssa_string_elem(proc, str);
ssaValue *len = ssa_string_len(proc, str); ssaValue *len = ssa_string_len(proc, str);
ssaValue *v = ssa_emit_slice(proc, dst, elem, v_zero, len, len); return ssa_emit_load(proc, ssa_emit_slice(proc, dst, elem, v_zero, len, len));
return v;
} }
@@ -1261,6 +1351,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
Entity *e = *found; Entity *e = *found;
switch (e->builtin.id) { switch (e->builtin.id) {
case BuiltinProc_len: { case BuiltinProc_len: {
// len :: proc(Type) -> int
// NOTE(bill): len of an array is a constant expression // NOTE(bill): len of an array is a constant expression
ssaValue *v = ssa_lvalue_address(ssa_build_addr(proc, ce->arg_list), proc); ssaValue *v = ssa_lvalue_address(ssa_build_addr(proc, ce->arg_list), proc);
Type *t = get_base_type(ssa_value_type(v)); Type *t = get_base_type(ssa_value_type(v));
@@ -1270,25 +1361,54 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
return ssa_slice_len(proc, v); return ssa_slice_len(proc, v);
} break; } break;
case BuiltinProc_cap: { case BuiltinProc_cap: {
// cap :: proc(Type) -> int
// NOTE(bill): cap of an array is a constant expression // NOTE(bill): cap of an array is a constant expression
ssaValue *v = ssa_lvalue_address(ssa_build_addr(proc, ce->arg_list), proc); ssaValue *v = ssa_lvalue_address(ssa_build_addr(proc, ce->arg_list), proc);
Type *t = get_base_type(ssa_value_type(v)); Type *t = get_base_type(ssa_value_type(v));
return ssa_slice_cap(proc, v); return ssa_slice_cap(proc, v);
} break; } break;
case BuiltinProc_copy: { case BuiltinProc_copy: {
GB_PANIC("TODO(bill): BuiltinProc_copy"); // copy :: proc(dst, src: []Type) -> int
// TODO(bill): Should this be llvm.memmove internally? AstNode *dst_node = ce->arg_list;
// http://llvm.org/docs/LangRef.html#llvm-memmove-intrinsic AstNode *src_node = ce->arg_list->next;
// declare void @llvm.memmove.p0i8.p0i8.i32(i8* <dest>, i8* <src>, i32 <len>, i32 <align>, i1 <isvolatile>) ssaValue *dst_slice = ssa_lvalue_address(ssa_build_addr(proc, dst_node), proc);
// declare void @llvm.memmove.p0i8.p0i8.i64(i8* <dest>, i8* <src>, i64 <len>, i32 <align>, i1 <isvolatile>) ssaValue *src_slice = ssa_lvalue_address(ssa_build_addr(proc, src_node), proc);
Type *slice_type = get_base_type(ssa_value_type(dst_slice));
GB_ASSERT(slice_type->kind == Type_Slice);
Type *elem_type = slice_type->slice.elem;
i64 size_of_elem = type_size_of(proc->module->sizes, proc->module->allocator, elem_type);
ssaValue *dst = ssa_emit_conv(proc, ssa_slice_elem(proc, dst_slice), t_rawptr);
ssaValue *src = ssa_emit_conv(proc, ssa_slice_elem(proc, src_slice), t_rawptr);
ssaValue *len_dst = ssa_slice_len(proc, dst_slice);
ssaValue *len_src = ssa_slice_len(proc, src_slice);
Token lt = {Token_Lt};
ssaValue *cond = ssa_emit_comp(proc, lt, len_dst, len_src);
ssaValue *len = ssa_emit_select(proc, cond, len_dst, len_src);
Token mul = {Token_Mul};
ssaValue *elem_size = ssa_make_value_constant(proc->module->allocator, t_int,
make_exact_value_integer(size_of_elem));
ssaValue *byte_count = ssa_emit_arith(proc, mul, len, elem_size, t_int);
i32 align = cast(i32)type_align_of(proc->module->sizes, proc->module->allocator, elem_type);
b32 is_volatile = false;
ssa_emit(proc, ssa_make_instr_copy_memory(proc, dst, src, byte_count, align, is_volatile));
return len;
} break; } break;
case BuiltinProc_append: { case BuiltinProc_append: {
// copy :: proc(s: ^[]Type, value: Type) -> bool
GB_PANIC("TODO(bill): BuiltinProc_append"); GB_PANIC("TODO(bill): BuiltinProc_append");
} break; } break;
case BuiltinProc_print: { case BuiltinProc_print: {
// print :: proc(...)
GB_PANIC("TODO(bill): BuiltinProc_print"); GB_PANIC("TODO(bill): BuiltinProc_print");
} break; } break;
case BuiltinProc_println: { case BuiltinProc_println: {
// println :: proc(...)
GB_PANIC("TODO(bill): BuiltinProc_println"); GB_PANIC("TODO(bill): BuiltinProc_println");
} break; } break;
} }
@@ -1310,13 +1430,9 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
ssaValue *a = ssa_build_expr(proc, arg); ssaValue *a = ssa_build_expr(proc, arg);
Type *at = ssa_value_type(a); Type *at = ssa_value_type(a);
if (at->kind == Type_Tuple) { if (at->kind == Type_Tuple) {
ssaValue *tuple = ssa_add_local_generated(proc, at);
ssa_emit_store(proc, tuple, a);
for (isize i = 0; i < at->tuple.variable_count; i++) { for (isize i = 0; i < at->tuple.variable_count; i++) {
Entity *e = at->tuple.variables[i]; Entity *e = at->tuple.variables[i];
ssaValue *index = ssa_make_value_constant(proc->module->allocator, t_i32, make_exact_value_integer(i)); ssaValue *v = ssa_emit_struct_ev(proc, a, i, e->type);
ssaValue *v = ssa_emit_struct_gep(proc, tuple, index, e->type);
v = ssa_emit_load(proc, v);
args[arg_index++] = v; args[arg_index++] = v;
} }
} else { } else {
@@ -1330,28 +1446,7 @@ ssaValue *ssa_build_single_expr(ssaProcedure *proc, AstNode *expr, TypeAndValue
case_end; case_end;
case_ast_node(se, SliceExpr, expr); case_ast_node(se, SliceExpr, expr);
ssaValue *low = NULL; return ssa_emit_load(proc, ssa_lvalue_address(ssa_build_addr(proc, expr), proc));
ssaValue *high = NULL;
ssaValue *max = NULL;
if (se->low != NULL) low = ssa_build_expr(proc, se->low);
if (se->high != NULL) high = ssa_build_expr(proc, se->high);
if (se->triple_indexed) max = ssa_build_expr(proc, se->max);
switch (tv->type->kind) {
case Type_Slice:
case Type_Array: {
ssaValue *base = ssa_lvalue_address(ssa_build_addr(proc, se->expr), proc);
return ssa_emit_slice(proc, tv->type, base, low, high, max);
} break;
case Type_Basic: {
// NOTE(bill): max is not needed
ssaValue *base = ssa_lvalue_address(ssa_build_addr(proc, se->expr), proc);
return ssa_emit_substring(proc, base, low, high);
} break;
}
GB_PANIC("Unknown slicable type");
case_end; case_end;
case_ast_node(ie, IndexExpr, expr); case_ast_node(ie, IndexExpr, expr);
@@ -1372,9 +1467,10 @@ ssaValue *ssa_build_expr(ssaProcedure *proc, AstNode *expr) {
if (tv->value.kind != ExactValue_Invalid) { if (tv->value.kind != ExactValue_Invalid) {
if (tv->value.kind == ExactValue_String) { if (tv->value.kind == ExactValue_String) {
// TODO(bill): Optimize by not allocating everytime
ssaValue *array = ssa_add_global_string_array(proc, tv->value); ssaValue *array = ssa_add_global_string_array(proc, tv->value);
ssaValue *elem = ssa_array_elem(proc, array); ssaValue *elem = ssa_array_elem(proc, array);
return ssa_emit_string(proc, elem, ssa_array_len(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); return ssa_make_value_constant(proc->module->allocator, tv->type, tv->value);
} }
@@ -1424,8 +1520,7 @@ ssaLvalue ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
e = ssa_emit_load(proc, e); e = ssa_emit_load(proc, e);
} }
ssaValue *index = ssa_make_value_constant(proc->module->allocator, t_i32, make_exact_value_integer(field_index)); ssaValue *v = ssa_emit_struct_gep(proc, e, field_index, entity->type);
ssaValue *v = ssa_emit_struct_gep(proc, e, index, entity->type);
return ssa_make_lvalue_address(v, expr); return ssa_make_lvalue_address(v, expr);
case_end; case_end;
@@ -1465,6 +1560,32 @@ ssaLvalue ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
return ssa_make_lvalue_address(v, expr); return ssa_make_lvalue_address(v, expr);
case_end; case_end;
case_ast_node(se, SliceExpr, expr);
ssaValue *low = NULL;
ssaValue *high = NULL;
ssaValue *max = NULL;
if (se->low != NULL) low = ssa_build_expr(proc, se->low);
if (se->high != NULL) high = ssa_build_expr(proc, se->high);
if (se->triple_indexed) max = ssa_build_expr(proc, se->max);
Type *type = type_of_expr(proc->module->info, expr);
switch (type->kind) {
case Type_Slice:
case Type_Array: {
ssaValue *base = ssa_lvalue_address(ssa_build_addr(proc, se->expr), proc);
return ssa_make_lvalue_address(ssa_emit_slice(proc, type, base, low, high, max), expr);
} break;
case Type_Basic: {
// NOTE(bill): max is not needed
ssaValue *base = ssa_lvalue_address(ssa_build_addr(proc, se->expr), proc);
return ssa_make_lvalue_address(ssa_emit_substring(proc, base, low, high), expr);
} break;
}
GB_PANIC("Unknown slicable type");
case_end;
case_ast_node(de, DerefExpr, expr); case_ast_node(de, DerefExpr, expr);
ssaValue *e = ssa_emit_load(proc, ssa_lvalue_address(ssa_build_addr(proc, de->expr), proc)); ssaValue *e = ssa_emit_load(proc, ssa_lvalue_address(ssa_build_addr(proc, de->expr), proc));
ssaValue *gep = ssa_make_instr_get_element_ptr(proc, e, NULL, NULL, 0, false); ssaValue *gep = ssa_make_instr_get_element_ptr(proc, e, NULL, NULL, 0, false);
@@ -1596,13 +1717,9 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) {
ssaValue *init = ssa_build_expr(proc, value); ssaValue *init = ssa_build_expr(proc, value);
Type *t = ssa_value_type(init); Type *t = ssa_value_type(init);
if (t->kind == Type_Tuple) { if (t->kind == Type_Tuple) {
ssaValue *tuple = ssa_add_local_generated(proc, t);
ssa_emit_store(proc, tuple, init);
for (isize i = 0; i < t->tuple.variable_count; i++) { for (isize i = 0; i < t->tuple.variable_count; i++) {
Entity *e = t->tuple.variables[i]; Entity *e = t->tuple.variables[i];
ssaValue *index = ssa_make_value_constant(proc->module->allocator, t_i32, make_exact_value_integer(i)); ssaValue *v = ssa_emit_struct_ev(proc, init, i, e->type);
ssaValue *v = ssa_emit_struct_gep(proc, tuple, index, e->type);
v = ssa_emit_load(proc, v);
gb_array_append(inits, v); gb_array_append(inits, v);
} }
} else { } else {
@@ -1678,13 +1795,9 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) {
Type *t = ssa_value_type(init); Type *t = ssa_value_type(init);
// TODO(bill): refactor for code reuse as this is repeated a bit // TODO(bill): refactor for code reuse as this is repeated a bit
if (t->kind == Type_Tuple) { if (t->kind == Type_Tuple) {
ssaValue *tuple = ssa_add_local_generated(proc, t);
ssa_emit_store(proc, tuple, init);
for (isize i = 0; i < t->tuple.variable_count; i++) { for (isize i = 0; i < t->tuple.variable_count; i++) {
Entity *e = t->tuple.variables[i]; Entity *e = t->tuple.variables[i];
ssaValue *index = ssa_make_value_constant(proc->module->allocator, t_i32, make_exact_value_integer(i)); ssaValue *v = ssa_emit_struct_ev(proc, init, i, e->type);
ssaValue *v = ssa_emit_struct_gep(proc, tuple, index, e->type);
v = ssa_emit_load(proc, v);
gb_array_append(inits, v); gb_array_append(inits, v);
} }
} else { } else {
@@ -1750,8 +1863,7 @@ void ssa_build_stmt(ssaProcedure *proc, AstNode *node) {
Entity *e = return_type_tuple->variables[i]; Entity *e = return_type_tuple->variables[i];
ssaValue *res = ssa_build_expr(proc, r); ssaValue *res = ssa_build_expr(proc, r);
ssa_value_set_type(res, e->type); ssa_value_set_type(res, e->type);
ssaValue *index = ssa_make_value_constant(proc->module->allocator, t_int, make_exact_value_integer(i)); ssaValue *field = ssa_emit_struct_gep(proc, v, i, e->type);
ssaValue *field = ssa_emit_struct_gep(proc, v, index, e->type);
ssa_emit_store(proc, field, res); ssa_emit_store(proc, field, res);
} }
v = ssa_emit_load(proc, v); v = ssa_emit_load(proc, v);
+6 -1
View File
@@ -87,7 +87,12 @@ ExactValue make_exact_value_from_basic_literal(Token token) {
case Token_String: return make_exact_value_string(token.string); case Token_String: return make_exact_value_string(token.string);
case Token_Integer: return make_exact_value_integer(token.string); case Token_Integer: return make_exact_value_integer(token.string);
case Token_Float: return make_exact_value_float(token.string); case Token_Float: return make_exact_value_float(token.string);
case Token_Rune: return make_exact_value_integer(token.string); case Token_Rune: {
Rune r = GB_RUNE_INVALID;
gb_utf8_decode(token.string.text, token.string.len, &r);
// gb_printf("%.*s rune: %d\n", LIT(token.string), r);
return make_exact_value_integer(r);
}
default: default:
GB_PANIC("Invalid token for basic literal"); GB_PANIC("Invalid token for basic literal");
break; break;
+7
View File
@@ -6625,6 +6625,8 @@ gb_global gbUtf8AcceptRange const gb__utf8_accept_ranges[] = {
isize gb_utf8_decode(u8 const *str, isize str_len, Rune *codepoint_out) { isize gb_utf8_decode(u8 const *str, isize str_len, Rune *codepoint_out) {
isize width = 0; isize width = 0;
Rune codepoint = GB_RUNE_INVALID; Rune codepoint = GB_RUNE_INVALID;
@@ -6639,6 +6641,11 @@ isize gb_utf8_decode(u8 const *str, isize str_len, Rune *codepoint_out) {
width = 1; width = 1;
goto end; goto end;
} }
if (s0 < 0x80) {
codepoint = s0;
width = 1;
goto end;
}
sz = x&7; sz = x&7;
accept = gb__utf8_accept_ranges[x>>4]; accept = gb__utf8_accept_ranges[x>>4];