Skip to content

Commit 10d9f8d

Browse files
authored
Tuples and type improvement (#11)
* Tuples and type improvement * Fix tuples, add context to NodeTerm codegen for sret * Remove build command
1 parent 4c86085 commit 10d9f8d

File tree

23 files changed

+2024
-1537
lines changed

23 files changed

+2024
-1537
lines changed

a.out

32.8 KB
Binary file not shown.

examples/test_2

32.8 KB
Binary file not shown.

examples/test_2.gom

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import io;
22

3+
// struct
34
type Point = {
45
x: int,
56
y: int

examples/test_2.s

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
.section __TEXT,__text,regular,pure_instructions
2+
.build_version macos, 14, 0
3+
.globl _square ; -- Begin function square
4+
.p2align 2
5+
_square: ; @square
6+
.cfi_startproc
7+
; %bb.0: ; %entry
8+
sub sp, sp, #16
9+
.cfi_def_cfa_offset 16
10+
mov w8, w0
11+
mul w0, w0, w0
12+
str w8, [sp, #12]
13+
add sp, sp, #16
14+
ret
15+
.cfi_endproc
16+
; -- End function
17+
.globl _add ; -- Begin function add
18+
.p2align 2
19+
_add: ; @add
20+
.cfi_startproc
21+
; %bb.0: ; %entry
22+
sub sp, sp, #16
23+
.cfi_def_cfa_offset 16
24+
mov w8, w0
25+
add w0, w0, w1
26+
stp w1, w8, [sp, #8]
27+
add sp, sp, #16
28+
ret
29+
.cfi_endproc
30+
; -- End function
31+
.globl _distance ; -- Begin function distance
32+
.p2align 2
33+
_distance: ; @distance
34+
.cfi_startproc
35+
; %bb.0: ; %entry
36+
sub sp, sp, #48
37+
stp x20, x19, [sp, #16] ; 16-byte Folded Spill
38+
stp x29, x30, [sp, #32] ; 16-byte Folded Spill
39+
.cfi_def_cfa_offset 48
40+
.cfi_offset w30, -8
41+
.cfi_offset w29, -16
42+
.cfi_offset w19, -24
43+
.cfi_offset w20, -32
44+
sub w8, w0, w2
45+
stp w0, w1, [sp, #8]
46+
mov w0, w8
47+
stp w2, w3, [sp]
48+
bl _square
49+
ldr w8, [sp, #12]
50+
mov w19, w0
51+
ldr w9, [sp, #4]
52+
sub w0, w8, w9
53+
bl _square
54+
add w0, w19, w0
55+
ldp x29, x30, [sp, #32] ; 16-byte Folded Reload
56+
ldp x20, x19, [sp, #16] ; 16-byte Folded Reload
57+
add sp, sp, #48
58+
ret
59+
.cfi_endproc
60+
; -- End function
61+
.globl _main ; -- Begin function main
62+
.p2align 2
63+
_main: ; @main
64+
.cfi_startproc
65+
; %bb.0: ; %entry
66+
sub sp, sp, #96
67+
stp x22, x21, [sp, #48] ; 16-byte Folded Spill
68+
stp x20, x19, [sp, #64] ; 16-byte Folded Spill
69+
stp x29, x30, [sp, #80] ; 16-byte Folded Spill
70+
.cfi_def_cfa_offset 96
71+
.cfi_offset w30, -8
72+
.cfi_offset w29, -16
73+
.cfi_offset w19, -24
74+
.cfi_offset w20, -32
75+
.cfi_offset w21, -40
76+
.cfi_offset w22, -48
77+
mov x8, #1
78+
mov x9, #3
79+
movk x8, #2, lsl #32
80+
movk x9, #4, lsl #32
81+
mov w0, #1
82+
mov w1, #2
83+
mov w2, #3
84+
mov w3, #4
85+
stp x9, x8, [sp, #32]
86+
bl _distance
87+
mov w19, w0
88+
str w0, [sp, #28]
89+
Lloh0:
90+
adrp x0, l_.strliteral@PAGE
91+
Lloh1:
92+
add x0, x0, l_.strliteral@PAGEOFF
93+
bl _printf
94+
Lloh2:
95+
adrp x20, l_fmt.int@PAGE
96+
str x19, [sp]
97+
Lloh3:
98+
add x20, x20, l_fmt.int@PAGEOFF
99+
mov x0, x20
100+
bl _printf
101+
Lloh4:
102+
adrp x19, l_newline@PAGE
103+
Lloh5:
104+
add x19, x19, l_newline@PAGEOFF
105+
mov x0, x19
106+
bl _printf
107+
ldp w0, w1, [sp, #40]
108+
ldp w2, w3, [sp, #32]
109+
stp w0, w1, [sp, #8]
110+
stp w2, w3, [sp, #16]
111+
bl _distance
112+
mov w21, w0
113+
Lloh6:
114+
adrp x0, l_.strliteral.1@PAGE
115+
Lloh7:
116+
add x0, x0, l_.strliteral.1@PAGEOFF
117+
bl _printf
118+
mov x0, x20
119+
str x21, [sp]
120+
bl _printf
121+
mov x0, x19
122+
bl _printf
123+
ldp x29, x30, [sp, #80] ; 16-byte Folded Reload
124+
ldp x20, x19, [sp, #64] ; 16-byte Folded Reload
125+
ldp x22, x21, [sp, #48] ; 16-byte Folded Reload
126+
add sp, sp, #96
127+
ret
128+
.loh AdrpAdd Lloh6, Lloh7
129+
.loh AdrpAdd Lloh4, Lloh5
130+
.loh AdrpAdd Lloh2, Lloh3
131+
.loh AdrpAdd Lloh0, Lloh1
132+
.cfi_endproc
133+
; -- End function
134+
.section __TEXT,__cstring,cstring_literals
135+
l_.strliteral: ; @.strliteral
136+
.asciz "Distance between p1 and p2: "
137+
138+
l_fmt.int: ; @fmt.int
139+
.asciz "%d"
140+
141+
l_newline: ; @newline
142+
.asciz "\n"
143+
144+
l_.strliteral.1: ; @.strliteral.1
145+
.asciz "Distance between l.p1 and l.p2: "
146+
147+
.subsections_via_symbols

examples/test_3

32.7 KB
Binary file not shown.

examples/test_3.gom

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import io;
2+
3+
// tuple
4+
type HttpResponse = { int, bool };
5+
6+
7+
fn process_http(url: str): HttpResponse {
8+
if(url == "http://www.example.com") {
9+
return { 200, true };
10+
}
11+
12+
return { 401, false };
13+
}
14+
15+
fn process_http_retry(url: str, retries: int): HttpResponse {
16+
let i = 0;
17+
for(i = retries; i > 0; i = i - 1) {
18+
io.log("Round: ", retries - i + 1);
19+
let resp = process_http(url);
20+
if(resp.1) {
21+
return resp;
22+
}
23+
}
24+
25+
return { 500, false };
26+
}
27+
28+
fn main() {
29+
let resp = process_http_retry("http://www.example.com", 10);
30+
io.log("Status: ", resp.0, " Success: ", resp.1);
31+
}

examples/test_3.ll

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
; ModuleID = 'mod'
2+
source_filename = "mod"
3+
4+
@.strliteral = private unnamed_addr constant [23 x i8] c"http://www.example.com\00", align 1
5+
@.strliteral.1 = private unnamed_addr constant [8 x i8] c"Round: \00", align 1
6+
@fmt.int = private unnamed_addr constant [3 x i8] c"%d\00", align 1
7+
@newline = private unnamed_addr constant [2 x i8] c"\0A\00", align 1
8+
@.strliteral.2 = private unnamed_addr constant [23 x i8] c"http://www.example.com\00", align 1
9+
@.strliteral.3 = private unnamed_addr constant [9 x i8] c"Status: \00", align 1
10+
@.strliteral.4 = private unnamed_addr constant [11 x i8] c" Success: \00", align 1
11+
@fmt.bool = private unnamed_addr constant [3 x i8] c"%d\00", align 1
12+
13+
declare i32 @printf(i8*, ...)
14+
15+
define void @process_http({ i32, i1 }* "noalias" "sret" %0, i8* %1) {
16+
entry:
17+
%2 = alloca i8*, align 8
18+
store i8* %1, i8** %2, align 8
19+
%url.load = load i8*, i8** %2, align 8
20+
%eqtmp = icmp eq i8* %url.load, getelementptr inbounds ([23 x i8], [23 x i8]* @.strliteral, i32 0, i32 0)
21+
br i1 %eqtmp, label %then, label %else
22+
23+
then: ; preds = %entry
24+
%fieldptr = getelementptr { i32, i1 }, { i32, i1 }* %0, i32 0, i32 0
25+
store i32 200, i32* %fieldptr, align 4
26+
%fieldptr1 = getelementptr { i32, i1 }, { i32, i1 }* %0, i32 0, i32 1
27+
store i1 true, i1* %fieldptr1, align 1
28+
ret void
29+
br label %merge
30+
31+
else: ; preds = %entry
32+
br label %merge
33+
34+
merge: ; preds = %else, %then
35+
%fieldptr2 = getelementptr { i32, i1 }, { i32, i1 }* %0, i32 0, i32 0
36+
store i32 401, i32* %fieldptr2, align 4
37+
%fieldptr3 = getelementptr { i32, i1 }, { i32, i1 }* %0, i32 0, i32 1
38+
store i1 false, i1* %fieldptr3, align 1
39+
ret void
40+
}
41+
42+
define void @process_http_retry({ i32, i1 }* "noalias" "sret" %0, i8* %1, i32 %2) {
43+
entry:
44+
%3 = alloca i8*, align 8
45+
store i8* %1, i8** %3, align 8
46+
%4 = alloca i32, align 4
47+
store i32 %2, i32* %4, align 4
48+
%i = alloca i32, align 4
49+
store i32 0, i32* %i, align 4
50+
store i32 0, i32* %i, align 4
51+
%retries.load = load i32, i32* %4, align 4
52+
store i32 %retries.load, i32* %i, align 4
53+
br label %loop
54+
55+
loop: ; preds = %loopupdate, %entry
56+
%i.load = load i32, i32* %i, align 4
57+
%gttmp = icmp sgt i32 %i.load, 0
58+
br i1 %gttmp, label %loopbody, label %afterloop
59+
60+
loopbody: ; preds = %loop
61+
%retries.load1 = load i32, i32* %4, align 4
62+
%i.load2 = load i32, i32* %i, align 4
63+
%subtmp = sub i32 %retries.load1, %i.load2
64+
%addtmp = add i32 %subtmp, 1
65+
%calltmp0 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.strliteral.1, i32 0, i32 0))
66+
%calltmp1 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @fmt.int, i32 0, i32 0), i32 %addtmp)
67+
%newline = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @newline, i32 0, i32 0))
68+
%resp = alloca { i32, i1 }, align 8
69+
%url.load = load i8*, i8** %3, align 8
70+
call void @process_http({ i32, i1 }* %resp, i8* %url.load)
71+
%fieldptr = getelementptr { i32, i1 }, { i32, i1 }* %resp, i32 0, i32 1
72+
%fieldload = load i1, i1* %fieldptr, align 1
73+
br i1 %fieldload, label %then, label %else
74+
75+
loopupdate: ; preds = %merge
76+
%i.load3 = load i32, i32* %i, align 4
77+
%subtmp4 = sub i32 %i.load3, 1
78+
store i32 %subtmp4, i32* %i, align 4
79+
br label %loop
80+
81+
afterloop: ; preds = %loop
82+
%fieldptr5 = getelementptr { i32, i1 }, { i32, i1 }* %0, i32 0, i32 0
83+
store i32 500, i32* %fieldptr5, align 4
84+
%fieldptr6 = getelementptr { i32, i1 }, { i32, i1 }* %0, i32 0, i32 1
85+
store i1 false, i1* %fieldptr6, align 1
86+
ret void
87+
88+
then: ; preds = %loopbody
89+
%resp.load = load { i32, i1 }, { i32, i1 }* %resp, align 4
90+
store { i32, i1 } %resp.load, { i32, i1 }* %0, align 4
91+
ret void
92+
br label %merge
93+
94+
else: ; preds = %loopbody
95+
br label %merge
96+
97+
merge: ; preds = %else, %then
98+
br label %loopupdate
99+
}
100+
101+
define void @main() {
102+
entry:
103+
%resp = alloca { i32, i1 }, align 8
104+
call void @process_http_retry({ i32, i1 }* %resp, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.strliteral.2, i32 0, i32 0), i32 10)
105+
%fieldptr = getelementptr { i32, i1 }, { i32, i1 }* %resp, i32 0, i32 0
106+
%fieldload = load i32, i32* %fieldptr, align 4
107+
%fieldptr1 = getelementptr { i32, i1 }, { i32, i1 }* %resp, i32 0, i32 1
108+
%fieldload2 = load i1, i1* %fieldptr1, align 1
109+
%calltmp0 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.strliteral.3, i32 0, i32 0))
110+
%calltmp1 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @fmt.int, i32 0, i32 0), i32 %fieldload)
111+
%calltmp2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.strliteral.4, i32 0, i32 0))
112+
%calltmp3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @fmt.bool, i32 0, i32 0), i1 %fieldload2)
113+
%newline = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @newline, i32 0, i32 0))
114+
ret void
115+
}

package-lock.json

Lines changed: 37 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)