-
Notifications
You must be signed in to change notification settings - Fork 6
/
coverage.html
1773 lines (1618 loc) · 66.2 KB
/
coverage.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>mugo: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/benhoyt/mugo/gofuncs.go (75.0%)</option>
<option value="file1">github.com/benhoyt/mugo/mugo.go (95.3%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package main
import (
"bufio"
"os"
)
var stdin = bufio.NewReader(os.Stdin)
func getc() int <span class="cov8" title="1">{
b, err := stdin.ReadByte()
if err != nil </span><span class="cov8" title="1">{
return -1
}</span>
<span class="cov8" title="1">return int(b)</span>
}
func log(s string) <span class="cov0" title="0">{
os.Stderr.WriteString(s)
}</span>
func print(s string) <span class="cov8" title="1">{
os.Stdout.WriteString(s)
}</span>
func exit(code int) <span class="cov0" title="0">{
os.Exit(code)
}</span>
func char(ch int) string <span class="cov8" title="1">{
return string([]byte{byte(ch)})
}</span>
</pre>
<pre class="file" id="file1" style="display: none">// 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() <span class="cov8" title="1">{
if c == '\n' </span><span class="cov8" title="1">{
line = line + 1
col = 0
}</span>
<span class="cov8" title="1">c = getc()
col = col + 1</span>
}
func itoa(n int) string <span class="cov8" title="1">{
if n < 0 </span><span class="cov8" title="1">{
return "-" + itoa(-n)
}</span>
<span class="cov8" title="1">if n < 10 </span><span class="cov8" title="1">{
return char(n + '0')
}</span>
<span class="cov8" title="1">return itoa(n/10) + itoa(n%10)</span>
}
func error(msg string) <span class="cov0" title="0">{
log("\n" + itoa(line) + ":" + itoa(col) + ": " + msg + "\n")
exit(1)
}</span>
func isDigit(ch int) bool <span class="cov8" title="1">{
return ch >= '0' && ch <= '9'
}</span>
func isAlpha(ch int) bool <span class="cov8" title="1">{
return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z'
}</span>
func find(names []string, name string) int <span class="cov8" title="1">{
i := 0
for i < len(names) </span><span class="cov8" title="1">{
if names[i] == name </span><span class="cov8" title="1">{
return i
}</span>
<span class="cov8" title="1">i = i + 1</span>
}
<span class="cov8" title="1">return -1</span>
}
func expectChar(ch int) <span class="cov8" title="1">{
if c != ch </span><span class="cov0" title="0">{
error("expected '" + char(ch) + "' not '" + char(c) + "'")
}</span>
<span class="cov8" title="1">nextChar()</span>
}
func tokenChoice(oneCharToken int, secondCh int, twoCharToken int) <span class="cov8" title="1">{
nextChar()
if c == secondCh </span><span class="cov8" title="1">{
nextChar()
token = twoCharToken
}</span> else<span class="cov8" title="1"> {
token = oneCharToken
}</span>
}
func next() <span class="cov8" title="1">{
// Skip whitespace and comments, and look for / operator
for c == '/' || c == ' ' || c == '\t' || c == '\r' || c == '\n' </span><span class="cov8" title="1">{
if c == '/' </span><span class="cov8" title="1">{
nextChar()
if c != '/' </span><span class="cov8" title="1">{
token = tDivide
return
}</span>
<span class="cov8" title="1">nextChar()
// Comment, skip till end of line
for c >= 0 && c != '\n' </span><span class="cov8" title="1">{
nextChar()
}</span>
} else<span class="cov8" title="1"> if c == '\n' </span><span class="cov8" title="1">{
nextChar()
// Semicolon insertion: golang.org/ref/spec#Semicolons
if token == tIdent || token == tIntLit || token == tStrLit ||
token == tReturn || token == tRParen ||
token == tRBracket || token == tRBrace </span><span class="cov8" title="1">{
token = tSemicolon
return
}</span>
} else<span class="cov8" title="1"> {
nextChar()
}</span>
}
<span class="cov8" title="1">if c < 0 </span><span class="cov8" title="1">{
// End of file
token = tEOF
return
}</span>
// Integer literal
<span class="cov8" title="1">if isDigit(c) </span><span class="cov8" title="1">{
tokenInt = c - '0'
nextChar()
for isDigit(c) </span><span class="cov8" title="1">{
tokenInt = tokenInt*10 + c - '0'
nextChar()
}</span>
<span class="cov8" title="1">token = tIntLit
return</span>
}
// Character literal
<span class="cov8" title="1">if c == '\'' </span><span class="cov8" title="1">{
nextChar()
if c == '\n' </span><span class="cov0" title="0">{
error("newline not allowed in character literal")
}</span>
<span class="cov8" title="1">if c == '\\' </span><span class="cov8" title="1">{
// Escape character
nextChar()
if c == '\'' </span><span class="cov8" title="1">{
tokenInt = '\''
}</span> else<span class="cov8" title="1"> if c == '\\' </span><span class="cov8" title="1">{
tokenInt = '\\'
}</span> else<span class="cov8" title="1"> if c == 't' </span><span class="cov8" title="1">{
tokenInt = '\t'
}</span> else<span class="cov8" title="1"> if c == 'r' </span><span class="cov8" title="1">{
tokenInt = '\r'
}</span> else<span class="cov8" title="1"> if c == 'n' </span><span class="cov8" title="1">{
tokenInt = '\n'
}</span> else<span class="cov0" title="0"> {
error("unexpected escape '\\" + char(c) + "'")
}</span>
<span class="cov8" title="1">nextChar()</span>
} else<span class="cov8" title="1"> {
tokenInt = c
nextChar()
}</span>
<span class="cov8" title="1">expectChar('\'')
token = tIntLit
return</span>
}
// String literal
<span class="cov8" title="1">if c == '"' </span><span class="cov8" title="1">{
nextChar()
tokenStr = ""
for c >= 0 && c != '"' </span><span class="cov8" title="1">{
if c == '\n' </span><span class="cov0" title="0">{
error("newline not allowed in string")
}</span>
<span class="cov8" title="1">if c == '\\' </span><span class="cov8" title="1">{
// Escape character
nextChar()
if c == '"' </span><span class="cov8" title="1">{
c = '"'
}</span> else<span class="cov8" title="1"> if c == '\\' </span><span class="cov8" title="1">{
c = '\\'
}</span> else<span class="cov8" title="1"> if c == 't' </span><span class="cov0" title="0">{
c = '\t'
}</span> else<span class="cov8" title="1"> if c == 'r' </span><span class="cov0" title="0">{
c = '\r'
}</span> else<span class="cov8" title="1"> if c == 'n' </span><span class="cov8" title="1">{
c = '\n'
}</span> else<span class="cov0" title="0"> {
error("unexpected escape \"\\" + char(c) + "\"")
}</span>
}
<span class="cov8" title="1">tokenStr = tokenStr + char(c)
nextChar()</span>
}
<span class="cov8" title="1">expectChar('"')
token = tStrLit
return</span>
}
// Keyword or identifier
<span class="cov8" title="1">if isAlpha(c) || c == '_' </span><span class="cov8" title="1">{
tokenStr = char(c)
nextChar()
for isAlpha(c) || isDigit(c) || c == '_' </span><span class="cov8" title="1">{
tokenStr = tokenStr + char(c)
nextChar()
}</span>
<span class="cov8" title="1">index := find(tokens, tokenStr)
if index >= tIf && index <= tPackage </span><span class="cov8" title="1">{
// Keyword
token = index
}</span> else<span class="cov8" title="1"> {
// Otherwise it's an identifier
token = tIdent
}</span>
<span class="cov8" title="1">return</span>
}
// Single-character tokens (token is ASCII value)
<span class="cov8" title="1">if c == '+' || c == '-' || c == '*' || c == '%' || c == ';' ||
c == ',' || c == '(' || c == ')' || c == '{' || c == '}' ||
c == '[' || c == ']' </span><span class="cov8" title="1">{
token = c
nextChar()
return
}</span>
// One or two-character tokens
<span class="cov8" title="1">if c == '=' </span><span class="cov8" title="1">{
tokenChoice(tAssign, '=', tEq)
return
}</span> else<span class="cov8" title="1"> if c == '<' </span><span class="cov8" title="1">{
tokenChoice(tLess, '=', tLessEq)
return
}</span> else<span class="cov8" title="1"> if c == '>' </span><span class="cov8" title="1">{
tokenChoice(tGreater, '=', tGreaterEq)
return
}</span> else<span class="cov8" title="1"> if c == '!' </span><span class="cov8" title="1">{
tokenChoice(tNot, '=', tNotEq)
return
}</span> else<span class="cov8" title="1"> if c == ':' </span><span class="cov8" title="1">{
tokenChoice(tColon, '=', tDeclAssign)
return
}</span>
// Two-character tokens
<span class="cov8" title="1">if c == '|' </span><span class="cov8" title="1">{
nextChar()
expectChar('|')
token = tOr
return
}</span> else<span class="cov8" title="1"> if c == '&' </span><span class="cov8" title="1">{
nextChar()
expectChar('&')
token = tAnd
return
}</span>
<span class="cov0" title="0">error("unexpected '" + char(c) + "'")</span>
}
// Escape given string; use "delim" as quote character.
func escape(s string, delim string) string <span class="cov8" title="1">{
i := 0
quoted := delim
for i < len(s) </span><span class="cov8" title="1">{
if s[i] == '"' </span><span class="cov8" title="1">{
quoted = quoted + "\\\""
}</span> else<span class="cov8" title="1"> if s[i] == '\\' </span><span class="cov8" title="1">{
quoted = quoted + "\\\\"
}</span> else<span class="cov8" title="1"> if s[i] == '\t' </span><span class="cov0" title="0">{
quoted = quoted + "\\t"
}</span> else<span class="cov8" title="1"> if s[i] == '\r' </span><span class="cov0" title="0">{
quoted = quoted + "\\r"
}</span> else<span class="cov8" title="1"> if s[i] == '\n' </span><span class="cov8" title="1">{
quoted = quoted + "\\n"
}</span> else<span class="cov8" title="1"> if s[i] == '`' </span><span class="cov8" title="1">{
quoted = quoted + "\\`"
}</span> else<span class="cov8" title="1"> {
quoted = quoted + char(int(s[i]))
}</span>
<span class="cov8" title="1">i = i + 1</span>
}
<span class="cov8" title="1">return quoted + delim</span>
}
func tokenName(t int) string <span class="cov0" title="0">{
if t > ' ' </span><span class="cov0" title="0">{
return char(t)
}</span>
<span class="cov0" title="0">return tokens[t]</span>
}
// Code generator functions
func genProgramStart() <span class="cov8" title="1">{
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")
}</span>
func genConst(name string, value int) <span class="cov8" title="1">{
print(name + " equ " + itoa(value) + "\n")
}</span>
func genIntLit(n int) <span class="cov8" title="1">{
print("push qword " + itoa(n) + "\n")
}</span>
func genStrLit(s string) <span class="cov8" title="1">{
// Add string to strs and strAddrs tables
index := find(strs, s)
if index < 0 </span><span class="cov8" title="1">{
// Haven't seen this string constant before, add a new one
index = len(strs)
strs = append(strs, s)
}</span>
// Push string struct: length and then address (by label)
<span class="cov8" title="1">print("push qword " + itoa(len(s)) + "\n")
print("push qword str" + itoa(index) + "\n")</span>
}
func typeName(typ int) string <span class="cov0" title="0">{
return types[typ]
}</span>
func typeSize(typ int) int <span class="cov8" title="1">{
return typeSizes[typ]
}</span>
// Return offset of local variable from rbp (including arguments).
func localOffset(index int) int <span class="cov8" title="1">{
funcIndex := find(funcs, curFunc)
sigIndex := funcSigIndexes[funcIndex]
numArgs := funcSigs[sigIndex+1]
if index < numArgs </span><span class="cov8" title="1">{
// Function argument local (add to rbp; args are on stack in reverse)
offset := 16
i := numArgs - 1
for i > index </span><span class="cov8" title="1">{
offset = offset + typeSize(localTypes[i])
i = i - 1
}</span>
<span class="cov8" title="1">return offset</span>
} else<span class="cov8" title="1"> {
// Declared local (subtract from rbp)
offset := 0
i := numArgs
for i <= index </span><span class="cov8" title="1">{
offset = offset - typeSize(localTypes[i])
i = i + 1
}</span>
<span class="cov8" title="1">return offset</span>
}
}
func genFetchInstrs(typ int, addr string) <span class="cov8" title="1">{
if typ == typeInt </span><span class="cov8" title="1">{
print("push qword [" + addr + "]\n")
}</span> else<span class="cov8" title="1"> if typ == typeString </span><span class="cov8" title="1">{
print("push qword [" + addr + "+8]\n")
print("push qword [" + addr + "]\n")
}</span> else<span class="cov8" title="1"> { // slice
print("push qword [" + addr + "+16]\n")
print("push qword [" + addr + "+8]\n")
print("push qword [" + addr + "]\n")
}</span>
}
func genLocalFetch(index int) int <span class="cov8" title="1">{
offset := localOffset(index)
typ := localTypes[index]
genFetchInstrs(typ, "rbp+"+itoa(offset))
return typ
}</span>
func genGlobalFetch(index int) int <span class="cov8" title="1">{
name := globals[index]
typ := globalTypes[index]
genFetchInstrs(typ, name)
return typ
}</span>
func genConstFetch(index int) int <span class="cov8" title="1">{
name := consts[index]
print("push qword " + name + "\n")
return typeInt
}</span>
func genIdentifier(name string) int <span class="cov8" title="1">{
localIndex := find(locals, name)
if localIndex >= 0 </span><span class="cov8" title="1">{
return genLocalFetch(localIndex)
}</span>
<span class="cov8" title="1">globalIndex := find(globals, name)
if globalIndex >= 0 </span><span class="cov8" title="1">{
return genGlobalFetch(globalIndex)
}</span>
<span class="cov8" title="1">constIndex := find(consts, name)
if constIndex >= 0 </span><span class="cov8" title="1">{
return genConstFetch(constIndex)
}</span>
<span class="cov8" title="1">funcIndex := find(funcs, name)
if funcIndex >= 0 </span><span class="cov8" title="1">{
sigIndex := funcSigIndexes[funcIndex]
return funcSigs[sigIndex] // result type
}</span>
<span class="cov0" title="0">error("identifier " + escape(name, "\"") + " not defined")
return 0</span>
}
func genAssignInstrs(typ int, addr string) <span class="cov8" title="1">{
if typ == typeInt </span><span class="cov8" title="1">{
print("pop qword [" + addr + "]\n")
}</span> else<span class="cov8" title="1"> if typ == typeString </span><span class="cov8" title="1">{
print("pop qword [" + addr + "]\n")
print("pop qword [" + addr + "+8]\n")
}</span> else<span class="cov8" title="1"> { // slice
print("pop qword [" + addr + "]\n")
print("pop qword [" + addr + "+8]\n")
print("pop qword [" + addr + "+16]\n")
}</span>
}
func genLocalAssign(index int) <span class="cov8" title="1">{
offset := localOffset(index)
genAssignInstrs(localTypes[index], "rbp+"+itoa(offset))
}</span>
func genGlobalAssign(index int) <span class="cov8" title="1">{
name := globals[index]
genAssignInstrs(globalTypes[index], name)
}</span>
func genAssign(name string) <span class="cov8" title="1">{
localIndex := find(locals, name)
if localIndex >= 0 </span><span class="cov8" title="1">{
genLocalAssign(localIndex)
return
}</span>
<span class="cov8" title="1">globalIndex := find(globals, name)
if globalIndex >= 0 </span><span class="cov8" title="1">{
genGlobalAssign(globalIndex)
return
}</span>
<span class="cov0" title="0">error("identifier " + escape(name, "\"") + " not defined (or not assignable)")</span>
}
func varType(name string) int <span class="cov8" title="1">{
localIndex := find(locals, name)
if localIndex >= 0 </span><span class="cov8" title="1">{
return localTypes[localIndex]
}</span>
<span class="cov8" title="1">globalIndex := find(globals, name)
if globalIndex >= 0 </span><span class="cov8" title="1">{
return globalTypes[globalIndex]
}</span>
<span class="cov0" title="0">error("identifier " + escape(name, "\"") + " not defined")
return 0</span>
}
func genSliceAssign(name string) <span class="cov8" title="1">{
typ := varType(name)
print("pop rax\n") // value (addr if string type)
if typ == typeSliceStr </span><span class="cov8" title="1">{
print("pop rbx\n") // value (len)
print("pop rcx\n") // index * 2
print("add rcx, rcx\n")
}</span> else<span class="cov8" title="1"> {
print("pop rcx\n")
}</span>
<span class="cov8" title="1">localIndex := find(locals, name)
if localIndex >= 0 </span><span class="cov8" title="1">{
offset := localOffset(localIndex)
print("mov rdx, [rbp+" + itoa(offset) + "]\n")
}</span> else<span class="cov8" title="1"> {
print("mov rdx, [" + name + "]\n")
}</span>
<span class="cov8" title="1">print("mov [rdx+rcx*8], rax\n")
if typ == typeSliceStr </span><span class="cov8" title="1">{
print("mov [rdx+rcx*8+8], rbx\n")
}</span>
}
func genCall(name string) int <span class="cov8" title="1">{
print("call " + name + "\n")
index := find(funcs, name)
sigIndex := funcSigIndexes[index]
resultType := funcSigs[sigIndex]
if resultType == typeInt </span><span class="cov8" title="1">{
print("push rax\n")
}</span> else<span class="cov8" title="1"> if resultType == typeString </span><span class="cov8" title="1">{
print("push rbx\n")
print("push rax\n")
}</span> else<span class="cov8" title="1"> if resultType == typeSliceInt || resultType == typeSliceStr </span><span class="cov8" title="1">{
print("push rcx\n")
print("push rbx\n")
print("push rax\n")
}</span>
<span class="cov8" title="1">return resultType</span>
}
func genFuncStart(name string) <span class="cov8" title="1">{
print("\n")
print(name + ":\n")
print("push rbp\n")
print("mov rbp, rsp\n")
print("sub rsp, " + itoa(localSpace) + "\n") // space for locals
}</span>
// Return size (in bytes) of current function's arguments.
func argsSize() int <span class="cov8" title="1">{
i := find(funcs, curFunc)
sigIndex := funcSigIndexes[i]
numArgs := funcSigs[sigIndex+1]
size := 0
i = 0
for i < numArgs </span><span class="cov8" title="1">{
size = size + typeSize(funcSigs[sigIndex+2+i])
i = i + 1
}</span>
<span class="cov8" title="1">return size</span>
}
// Return size (in bytes) of current function's locals (excluding arguments).
func localsSize() int <span class="cov8" title="1">{
i := find(funcs, curFunc)
sigIndex := funcSigIndexes[i]
numArgs := funcSigs[sigIndex+1]
size := 0
i = numArgs
for i < len(locals) </span><span class="cov8" title="1">{
size = size + typeSize(localTypes[i])
i = i + 1
}</span>
<span class="cov8" title="1">return size</span>
}
func genFuncEnd() <span class="cov8" title="1">{
size := localsSize()
if size > localSpace </span><span class="cov0" title="0">{
error(curFunc + "'s locals too big (" + itoa(size) + " > " + itoa(localSpace) + ")\n")
}</span>
<span class="cov8" title="1">print("mov rsp, rbp\n")
print("pop rbp\n")
size = argsSize()
if size > 0 </span><span class="cov8" title="1">{
print("ret " + itoa(size) + "\n")
}</span> else<span class="cov8" title="1"> {
print("ret\n")
}</span>
}
func genDataSections() <span class="cov8" title="1">{
print("\n")
print("section .data\n")
print("_strOutOfMem: db `out of memory\\n`\n")
// String constants
i := 0
for i < len(strs) </span><span class="cov8" title="1">{
print("str" + itoa(i) + ": db " + escape(strs[i], "`") + "\n")
i = i + 1
}</span>
// Global variables
<span class="cov8" title="1">print("align 8\n")
i = 0
for i < len(globals) </span><span class="cov8" title="1">{
typ := globalTypes[i]
if typ == typeInt </span><span class="cov8" title="1">{
print(globals[i] + ": dq 0\n")
}</span> else<span class="cov8" title="1"> if typ == typeString </span><span class="cov8" title="1">{
print(globals[i] + ": dq 0, 0\n") // string: address, length
}</span> else<span class="cov8" title="1"> {
print(globals[i] + ": dq 0, 0, 0\n") // slice: address, length, capacity
}</span>
<span class="cov8" title="1">i = i + 1</span>
}
// "Heap" (used for strings and slice appends)
<span class="cov8" title="1">print("\n")