-
Notifications
You must be signed in to change notification settings - Fork 6
/
mugo.go
1634 lines (1496 loc) · 35.2 KB
/
mugo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Mugo: compiler for a (micro) subset of Go
package main
var (
// Lexer variables
c int // current lexer byte
line int // current line and column
col int
// Parser-compiler variables
token int // current parser token
tokenInt int // integer value of current token (if applicable)
tokenStr string // string value of current token (if applicable)
curFunc string // current function name, or "" if not in a func
tokens []string // token names
types []string // type names
typeSizes []int // type sizes in bytes
labelNum int // current label number
consts []string // constant names
globals []string // global names and types
globalTypes []int
locals []string // local names and types
localTypes []int
funcs []string // function names
funcSigIndexes []int // indexes into funcSigs
funcSigs []int // for each func: retType N arg1Type ... argNType
strs []string // string constants
)
const (
localSpace int = 64 // max space for locals declared with := (not arguments)
heapSize int = 1048576 // 1MB "heap"
// Types
typeVoid int = 1 // only used as return "type"
typeInt int = 2
typeString int = 3
typeSliceInt int = 4
typeSliceStr int = 5
// Keywords
tIf int = 1
tElse int = 2
tFor int = 3
tVar int = 4
tConst int = 5
tFunc int = 6
tReturn int = 7
tPackage int = 8
// Literals, identifiers, and EOF
tIntLit int = 9
tStrLit int = 10
tIdent int = 11
tEOF int = 12
// Two-character tokens
tOr int = 13
tAnd int = 14
tEq int = 15
tNotEq int = 16
tLessEq int = 17
tGreaterEq int = 18
tDeclAssign int = 19
// Single-character tokens (these use the ASCII value)
tPlus int = '+'
tMinus int = '-'
tTimes int = '*'
tDivide int = '/'
tModulo int = '%'
tComma int = ','
tSemicolon int = ';'
tColon int = ':'
tAssign int = '='
tNot int = '!'
tLess int = '<'
tGreater int = '>'
tLParen int = '('
tRParen int = ')'
tLBrace int = '{'
tRBrace int = '}'
tLBracket int = '['
tRBracket int = ']'
)
// Lexer
func nextChar() {
if c == '\n' {
line = line + 1
col = 0
}
c = getc()
col = col + 1
}
func itoa(n int) string {
if n < 0 {
return "-" + itoa(-n)
}
if n < 10 {
return char(n + '0')
}
return itoa(n/10) + itoa(n%10)
}
func error(msg string) {
log("\n" + itoa(line) + ":" + itoa(col) + ": " + msg + "\n")
exit(1)
}
func isDigit(ch int) bool {
return ch >= '0' && ch <= '9'
}
func isAlpha(ch int) bool {
return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'
}
func find(names []string, name string) int {
i := 0
for i < len(names) {
if names[i] == name {
return i
}
i = i + 1
}
return -1
}
func expectChar(ch int) {
if c != ch {
error("expected '" + char(ch) + "' not '" + char(c) + "'")
}
nextChar()
}
func tokenChoice(oneCharToken int, secondCh int, twoCharToken int) {
nextChar()
if c == secondCh {
nextChar()
token = twoCharToken
} else {
token = oneCharToken
}
}
func next() {
// Skip whitespace and comments, and look for / operator
for c == '/' || c == ' ' || c == '\t' || c == '\r' || c == '\n' {
if c == '/' {
nextChar()
if c != '/' {
token = tDivide
return
}
nextChar()
// Comment, skip till end of line
for c >= 0 && c != '\n' {
nextChar()
}
} else if c == '\n' {
nextChar()
// Semicolon insertion: golang.org/ref/spec#Semicolons
if token == tIdent || token == tIntLit || token == tStrLit ||
token == tReturn || token == tRParen ||
token == tRBracket || token == tRBrace {
token = tSemicolon
return
}
} else {
nextChar()
}
}
if c < 0 {
// End of file
token = tEOF
return
}
// Integer literal
if isDigit(c) {
tokenInt = c - '0'
nextChar()
for isDigit(c) {
tokenInt = tokenInt*10 + c - '0'
nextChar()
}
token = tIntLit
return
}
// Character literal
if c == '\'' {
nextChar()
if c == '\n' {
error("newline not allowed in character literal")
}
if c == '\\' {
// Escape character
nextChar()
if c == '\'' {
tokenInt = '\''
} else if c == '\\' {
tokenInt = '\\'
} else if c == 't' {
tokenInt = '\t'
} else if c == 'r' {
tokenInt = '\r'
} else if c == 'n' {
tokenInt = '\n'
} else {
error("unexpected escape '\\" + char(c) + "'")
}
nextChar()
} else {
tokenInt = c
nextChar()
}
expectChar('\'')
token = tIntLit
return
}
// String literal
if c == '"' {
nextChar()
tokenStr = ""
for c >= 0 && c != '"' {
if c == '\n' {
error("newline not allowed in string")
}
if c == '\\' {
// Escape character
nextChar()
if c == '"' {
c = '"'
} else if c == '\\' {
c = '\\'
} else if c == 't' {
c = '\t'
} else if c == 'r' {
c = '\r'
} else if c == 'n' {
c = '\n'
} else {
error("unexpected escape \"\\" + char(c) + "\"")
}
}
tokenStr = tokenStr + char(c)
nextChar()
}
expectChar('"')
token = tStrLit
return
}
// Keyword or identifier
if isAlpha(c) || c == '_' {
tokenStr = char(c)
nextChar()
for isAlpha(c) || isDigit(c) || c == '_' {
tokenStr = tokenStr + char(c)
nextChar()
}
index := find(tokens, tokenStr)
if index >= tIf && index <= tPackage {
// Keyword
token = index
} else {
// Otherwise it's an identifier
token = tIdent
}
return
}
// Single-character tokens (token is ASCII value)
if c == '+' || c == '-' || c == '*' || c == '%' || c == ';' ||
c == ',' || c == '(' || c == ')' || c == '{' || c == '}' ||
c == '[' || c == ']' {
token = c
nextChar()
return
}
// One or two-character tokens
if c == '=' {
tokenChoice(tAssign, '=', tEq)
return
} else if c == '<' {
tokenChoice(tLess, '=', tLessEq)
return
} else if c == '>' {
tokenChoice(tGreater, '=', tGreaterEq)
return
} else if c == '!' {
tokenChoice(tNot, '=', tNotEq)
return
} else if c == ':' {
tokenChoice(tColon, '=', tDeclAssign)
return
}
// Two-character tokens
if c == '|' {
nextChar()
expectChar('|')
token = tOr
return
} else if c == '&' {
nextChar()
expectChar('&')
token = tAnd
return
}
error("unexpected '" + char(c) + "'")
}
// Escape given string; use "delim" as quote character.
func escape(s string, delim string) string {
i := 0
quoted := delim
for i < len(s) {
if s[i] == '"' {
quoted = quoted + "\\\""
} else if s[i] == '\\' {
quoted = quoted + "\\\\"
} else if s[i] == '\t' {
quoted = quoted + "\\t"
} else if s[i] == '\r' {
quoted = quoted + "\\r"
} else if s[i] == '\n' {
quoted = quoted + "\\n"
} else if s[i] == '`' {
quoted = quoted + "\\`"
} else {
quoted = quoted + char(int(s[i]))
}
i = i + 1
}
return quoted + delim
}
func tokenName(t int) string {
if t > ' ' {
return char(t)
}
return tokens[t]
}
// Code generator functions
func genProgramStart() {
print("global _start\n")
print("section .text\n")
print("\n")
// Initialize and call main.
print("_start:\n")
print("xor rax, rax\n") // ensure heap is zeroed
print("mov rdi, _heap\n")
print("mov rcx, " + itoa(heapSize/8) + "\n")
print("rep stosq\n")
print("mov rax, _heap\n")
print("mov [_heapPtr], rax\n")
print("call main\n")
print("mov rax, 60\n") // system call for "exit"
print("mov rdi, 0\n") // exit code 0
print("syscall\n")
print("\n")
// Write a string to stdout.
print("print:\n")
print("push rbp\n") // rbp ret addr len
print("mov rbp, rsp\n")
print("mov rax, 1\n") // system call for "write"
print("mov rdi, 1\n") // file handle 1 is stdout
print("mov rsi, [rbp+16]\n") // address
print("mov rdx, [rbp+24]\n") // length
print("syscall\n")
print("pop rbp\n")
print("ret 16\n")
print("\n")
// Write a string to stderr.
print("log:\n")
print("push rbp\n") // rbp ret addr len
print("mov rbp, rsp\n")
print("mov rax, 1\n") // system call for "write"
print("mov rdi, 2\n") // file handle 2 is stderr
print("mov rsi, [rbp+16]\n") // address
print("mov rdx, [rbp+24]\n") // length
print("syscall\n")
print("pop rbp\n")
print("ret 16\n")
print("\n")
// Read a single byte from stdin, or return -1 on EOF.
print("getc:\n")
print("push qword 0\n")
print("mov rax, 0\n") // system call for "read"
print("mov rdi, 0\n") // file handle 0 is stdin
print("mov rsi, rsp\n") // address
print("mov rdx, 1\n") // length
print("syscall\n")
print("cmp rax, 1\n")
print("je _getc1\n")
print("mov qword [rsp], -1\n")
print("_getc1:\n")
print("pop rax\n")
print("ret\n")
print("\n")
// Like os.Exit().
print("exit:\n")
print("mov rdi, [rsp+8]\n") // code
print("mov rax, 60\n") // system call for "exit"
print("syscall\n")
print("\n")
// No-op int() for use in escape(), to satisfy Go's type checker.
print("int:\n")
print("mov rax, [rsp+8]\n") // value
print("ret 8\n")
print("\n")
// Return concatenation of two strings.
print("_strAdd:\n")
print("push rbp\n") // rbp ret addr1 len1 addr0 len0
print("mov rbp, rsp\n")
// Allocate len0+len1 bytes
print("mov rax, [rbp+24]\n") // len1
print("add rax, [rbp+40]\n") // len1 + len0
print("push rax\n")
print("call _alloc\n")
// Move len0 bytes from addr0 to addrNew
print("mov rsi, [rbp+32]\n")
print("mov rdi, rax\n")
print("mov rcx, [rbp+40]\n")
print("rep movsb\n")
// Move len1 bytes from addr1 to addrNew+len0
print("mov rsi, [rbp+16]\n")
print("mov rdi, rax\n")
print("add rdi, [rbp+40]\n")
print("mov rcx, [rbp+24]\n")
print("rep movsb\n")
// Return addrNew len0+len1 (addrNew already in rax)
print("mov rbx, [rbp+24]\n")
print("add rbx, [rbp+40]\n")
print("pop rbp\n")
print("ret 32\n")
print("\n")
// Return true if strings are equal.
print("_strEq:\n")
print("push rbp\n") // rbp ret addr1 len1 addr0 len0
print("mov rbp, rsp\n")
print("mov rcx, [rbp+40]\n")
print("cmp rcx, [rbp+24]\n")
print("jne _strEqNotEqual\n")
print("mov rsi, [rbp+16]\n")
print("mov rdi, [rbp+32]\n")
print("rep cmpsb\n")
print("jne _strEqNotEqual\n")
print("mov rax, 1\n")
print("pop rbp\n")
print("ret 32\n")
// Return addrNew len0+len1 (addrNew already in rax)
print("_strEqNotEqual:\n")
print("xor rax, rax\n")
print("pop rbp\n")
print("ret 32\n")
print("\n")
// Return new 1-byte string from integer character.
print("char:\n")
print("push rbp\n") // rbp ret ch
print("mov rbp, rsp\n")
// Allocate 1 byte
print("push 1\n")
print("call _alloc\n")
// Move byte to destination
print("mov rbx, [rbp+16]\n")
print("mov [rax], bl\n")
// Return addrNew 1 (addrNew already in rax)
print("mov rbx, 1\n")
print("pop rbp\n")
print("ret 8\n")
print("\n")
// Simple bump allocator (with no GC!). Takes allocation size in bytes,
// returns pointer to allocated memory.
print("_alloc:\n")
print("push rbp\n") // rbp ret size
print("mov rbp, rsp\n")
print("mov rax, [_heapPtr]\n")
print("mov rbx, [rbp+16]\n")
print("add rbx, [_heapPtr]\n")
print("cmp rbx, _heapEnd\n")
print("jg _outOfMem\n")
print("mov [_heapPtr], rbx\n")
print("pop rbp\n")
print("ret 8\n")
print("_outOfMem:\n")
print("push qword 14\n") // len("out of memory\n")
print("push _strOutOfMem\n")
print("call log\n")
print("push qword 1\n")
print("call exit\n")
print("\n")
// Append single integer to []int, allocating and copying as necessary.
print("_appendInt:\n")
print("push rbp\n") // rbp ret value addr len cap
print("mov rbp, rsp\n")
// Ensure capacity is large enough
print("mov rax, [rbp+32]\n") // len
print("mov rbx, [rbp+40]\n") // cap
print("cmp rax, rbx\n") // if len >= cap, resize
print("jl _appendInt1\n")
print("add rbx, rbx\n") // double in size
print("jnz _appendInt2\n") // if it's zero, allocate minimum size
print("inc rbx\n")
print("_appendInt2:\n")
print("mov [rbp+40], rbx\n") // update cap
// Allocate newCap*8 bytes
print("lea rbx, [rbx*8]\n")
print("push rbx\n")
print("call _alloc\n")
// Move from old array to new
print("mov rsi, [rbp+24]\n")
print("mov rdi, rax\n")
print("mov [rbp+24], rax\n") // update addr
print("mov rcx, [rbp+32]\n")
print("rep movsq\n")
// Set addr[len] = value
print("_appendInt1:\n")
print("mov rax, [rbp+24]\n") // addr
print("mov rbx, [rbp+32]\n") // len
print("mov rdx, [rbp+16]\n") // value
print("mov [rax+rbx*8], rdx\n")
// Return addr len+1 cap (in rax rbx rcx)
print("inc rbx\n")
print("mov rcx, [rbp+40]\n")
print("pop rbp\n")
print("ret 32\n")
print("\n")
// Append single string to []string, allocating and copying as necessary.
print("_appendString:\n")
print("push rbp\n") // rbp ret 16strAddr 24strLen 32addr 40len 48cap
print("mov rbp, rsp\n")
// Ensure capacity is large enough
print("mov rax, [rbp+40]\n") // len
print("mov rbx, [rbp+48]\n") // cap
print("cmp rax, rbx\n") // if len >= cap, resize
print("jl _appendInt3\n")
print("add rbx, rbx\n") // double in size
print("jnz _appendInt4\n") // if it's zero, allocate minimum size
print("inc rbx\n")
print("_appendInt4:\n")
print("mov [rbp+48], rbx\n") // update cap
// Allocate newCap*16 bytes
print("add rbx, rbx\n")
print("lea rbx, [rbx*8]\n")
print("push rbx\n")
print("call _alloc\n")
// Move from old array to new
print("mov rsi, [rbp+32]\n")
print("mov rdi, rax\n")
print("mov [rbp+32], rax\n") // update addr
print("mov rcx, [rbp+40]\n")
print("add rcx, rcx\n")
print("rep movsq\n")
// Set addr[len] = strValue
print("_appendInt3:\n")
print("mov rax, [rbp+32]\n") // addr
print("mov rbx, [rbp+40]\n") // len
print("add rbx, rbx\n")
print("mov rdx, [rbp+16]\n") // strAddr
print("mov [rax+rbx*8], rdx\n")
print("mov rdx, [rbp+24]\n") // strLen
print("mov [rax+rbx*8+8], rdx\n")
// Return addr len+1 cap (in rax rbx rcx)
print("mov rbx, [rbp+40]\n")
print("inc rbx\n")
print("mov rcx, [rbp+48]\n")
print("pop rbp\n")
print("ret 40\n")
// Return string length
print("len:\n")
print("push rbp\n") // rbp ret addr len
print("mov rbp, rsp\n")
print("mov rax, [rbp+24]\n")
print("pop rbp\n")
print("ret 16\n")
print("\n")
// Return slice length
print("_lenSlice:\n")
print("push rbp\n") // rbp ret addr len cap
print("mov rbp, rsp\n")
print("mov rax, [rbp+24]\n")
print("pop rbp\n")
print("ret 24\n")
print("\n")
}
func genConst(name string, value int) {
print(name + " equ " + itoa(value) + "\n")
}
func genIntLit(n int) {
print("push qword " + itoa(n) + "\n")
}
func genStrLit(s string) {
// Add string to strs and strAddrs tables
index := find(strs, s)
if index < 0 {
// Haven't seen this string constant before, add a new one
index = len(strs)
strs = append(strs, s)
}
// Push string struct: length and then address (by label)
print("push qword " + itoa(len(s)) + "\n")
print("push qword str" + itoa(index) + "\n")
}
func typeName(typ int) string {
return types[typ]
}
func typeSize(typ int) int {
return typeSizes[typ]
}
// Return offset of local variable from rbp (including arguments).
func localOffset(index int) int {
funcIndex := find(funcs, curFunc)
sigIndex := funcSigIndexes[funcIndex]
numArgs := funcSigs[sigIndex+1]
if index < numArgs {
// Function argument local (add to rbp; args are on stack in reverse)
offset := 16
i := numArgs - 1
for i > index {
offset = offset + typeSize(localTypes[i])
i = i - 1
}
return offset
} else {
// Declared local (subtract from rbp)
offset := 0
i := numArgs
for i <= index {
offset = offset - typeSize(localTypes[i])
i = i + 1
}
return offset
}
}
func genFetchInstrs(typ int, addr string) {
if typ == typeInt {
print("push qword [" + addr + "]\n")
} else if typ == typeString {
print("push qword [" + addr + "+8]\n")
print("push qword [" + addr + "]\n")
} else { // slice
print("push qword [" + addr + "+16]\n")
print("push qword [" + addr + "+8]\n")
print("push qword [" + addr + "]\n")
}
}
func genLocalFetch(index int) int {
offset := localOffset(index)
typ := localTypes[index]
genFetchInstrs(typ, "rbp+"+itoa(offset))
return typ
}
func genGlobalFetch(index int) int {
name := globals[index]
typ := globalTypes[index]
genFetchInstrs(typ, name)
return typ
}
func genConstFetch(index int) int {
name := consts[index]
print("push qword " + name + "\n")
return typeInt
}
func genIdentifier(name string) int {
localIndex := find(locals, name)
if localIndex >= 0 {
return genLocalFetch(localIndex)
}
globalIndex := find(globals, name)
if globalIndex >= 0 {
return genGlobalFetch(globalIndex)
}
constIndex := find(consts, name)
if constIndex >= 0 {
return genConstFetch(constIndex)
}
funcIndex := find(funcs, name)
if funcIndex >= 0 {
sigIndex := funcSigIndexes[funcIndex]
return funcSigs[sigIndex] // result type
}
error("identifier " + escape(name, "\"") + " not defined")
return 0
}
func genAssignInstrs(typ int, addr string) {
if typ == typeInt {
print("pop qword [" + addr + "]\n")
} else if typ == typeString {
print("pop qword [" + addr + "]\n")
print("pop qword [" + addr + "+8]\n")
} else { // slice
print("pop qword [" + addr + "]\n")
print("pop qword [" + addr + "+8]\n")
print("pop qword [" + addr + "+16]\n")
}
}
func genLocalAssign(index int) {
offset := localOffset(index)
genAssignInstrs(localTypes[index], "rbp+"+itoa(offset))
}
func genGlobalAssign(index int) {
name := globals[index]
genAssignInstrs(globalTypes[index], name)
}
func genAssign(name string) {
localIndex := find(locals, name)
if localIndex >= 0 {
genLocalAssign(localIndex)
return
}
globalIndex := find(globals, name)
if globalIndex >= 0 {
genGlobalAssign(globalIndex)
return
}
error("identifier " + escape(name, "\"") + " not defined (or not assignable)")
}
func varType(name string) int {
localIndex := find(locals, name)
if localIndex >= 0 {
return localTypes[localIndex]
}
globalIndex := find(globals, name)
if globalIndex >= 0 {
return globalTypes[globalIndex]
}
error("identifier " + escape(name, "\"") + " not defined")
return 0
}
func genSliceAssign(name string) {
typ := varType(name)
print("pop rax\n") // value (addr if string type)
if typ == typeSliceStr {
print("pop rbx\n") // value (len)
print("pop rcx\n") // index * 2
print("add rcx, rcx\n")
} else {
print("pop rcx\n")
}
localIndex := find(locals, name)
if localIndex >= 0 {
offset := localOffset(localIndex)
print("mov rdx, [rbp+" + itoa(offset) + "]\n")
} else {
print("mov rdx, [" + name + "]\n")
}
print("mov [rdx+rcx*8], rax\n")
if typ == typeSliceStr {
print("mov [rdx+rcx*8+8], rbx\n")
}
}
func genCall(name string) int {
print("call " + name + "\n")
index := find(funcs, name)
sigIndex := funcSigIndexes[index]
resultType := funcSigs[sigIndex]
if resultType == typeInt {
print("push rax\n")
} else if resultType == typeString {
print("push rbx\n")
print("push rax\n")
} else if resultType == typeSliceInt || resultType == typeSliceStr {
print("push rcx\n")
print("push rbx\n")
print("push rax\n")
}
return resultType
}
func genFuncStart(name string) {
print("\n")
print(name + ":\n")
print("push rbp\n")
print("mov rbp, rsp\n")
print("sub rsp, " + itoa(localSpace) + "\n") // space for locals
}
// Return size (in bytes) of current function's arguments.
func argsSize() int {
i := find(funcs, curFunc)
sigIndex := funcSigIndexes[i]
numArgs := funcSigs[sigIndex+1]
size := 0
i = 0
for i < numArgs {
size = size + typeSize(funcSigs[sigIndex+2+i])
i = i + 1
}
return size
}
// Return size (in bytes) of current function's locals (excluding arguments).
func localsSize() int {
i := find(funcs, curFunc)
sigIndex := funcSigIndexes[i]
numArgs := funcSigs[sigIndex+1]
size := 0
i = numArgs
for i < len(locals) {
size = size + typeSize(localTypes[i])
i = i + 1
}
return size
}
func genFuncEnd() {
size := localsSize()
if size > localSpace {
error(curFunc + "'s locals too big (" + itoa(size) + " > " + itoa(localSpace) + ")\n")
}
print("mov rsp, rbp\n")
print("pop rbp\n")
size = argsSize()
if size > 0 {
print("ret " + itoa(size) + "\n")
} else {
print("ret\n")
}
}
func genDataSections() {
print("\n")
print("section .data\n")
print("_strOutOfMem: db `out of memory\\n`\n")
// String constants
i := 0
for i < len(strs) {
print("str" + itoa(i) + ": db " + escape(strs[i], "`") + "\n")
i = i + 1
}
// Global variables
print("align 8\n")
i = 0
for i < len(globals) {
typ := globalTypes[i]
if typ == typeInt {
print(globals[i] + ": dq 0\n")
} else if typ == typeString {
print(globals[i] + ": dq 0, 0\n") // string: address, length
} else {
print(globals[i] + ": dq 0, 0, 0\n") // slice: address, length, capacity
}
i = i + 1
}
// "Heap" (used for strings and slice appends)
print("\n")
print("section .bss\n")
print("_heapPtr: resq 1\n")
print("_heap: resb " + itoa(heapSize) + "\n")
print("_heapEnd:\n")
}
func genUnary(op int, typ int) {
if typ != typeInt {
error("unary operator not allowed on type " + typeName(typ))
}
print("pop rax\n")
if op == tMinus {
print("neg rax\n")
} else if op == tNot {
print("cmp rax, 0\n")
print("mov rax, 0\n")
print("setz al\n")
}
print("push rax\n")
}
func genBinaryString(op int) int {
if op == tPlus {
print("call _strAdd\n")
print("push rbx\n")
print("push rax\n")
return typeString
} else if op == tEq {
print("call _strEq\n")
print("push rax\n")
return typeInt
} else if op == tNotEq {
print("call _strEq\n")
print("cmp rax, 0\n")
print("mov rax, 0\n")
print("setz al\n")
print("push rax\n")
return typeInt
} else {
error("operator " + tokenName(op) + " not allowed on strings")
return 0
}
}
func genBinaryInt(op int) int {
print("pop rbx\n")
print("pop rax\n")
if op == tPlus {
print("add rax, rbx\n")
} else if op == tMinus {
print("sub rax, rbx\n")
} else if op == tTimes {
print("imul rbx\n")
} else if op == tDivide {
print("cqo\n")
print("idiv rbx\n")
} else if op == tModulo {
print("cqo\n")
print("idiv rbx\n")
print("mov rax, rdx\n")
} else if op == tEq {
print("cmp rax, rbx\n")
print("mov rax, 0\n")
print("sete al\n")
} else if op == tNotEq {
print("cmp rax, rbx\n")
print("mov rax, 0\n")
print("setne al\n")
} else if op == tLess {
print("cmp rax, rbx\n")
print("mov rax, 0\n")
print("setl al\n")
} else if op == tLessEq {
print("cmp rax, rbx\n")
print("mov rax, 0\n")
print("setle al\n")
} else if op == tGreater {
print("cmp rax, rbx\n")
print("mov rax, 0\n")
print("setg al\n")
} else if op == tGreaterEq {
print("cmp rax, rbx\n")
print("mov rax, 0\n")
print("setge al\n")
} else if op == tAnd {
print("and rax, rbx\n")
} else if op == tOr {
print("or rax, rbx\n")
}
print("push rax\n")
return typeInt
}
func genBinary(op int, typ1 int, typ2 int) int {
if typ1 != typ2 {
error("binary operands must be the same type")
}
if typ1 == typeString {
return genBinaryString(op)
} else {
return genBinaryInt(op)
}
}
func genReturn(typ int) {
if typ == typeInt {
print("pop rax\n")