-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompiler.s
696 lines (545 loc) · 10.3 KB
/
compiler.s
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
%define T_UNDEFINED 0
%define T_VOID 1
%define T_NIL 2
%define T_RATIONAL 3
%define T_FLOAT 4
%define T_BOOL 5
%define T_CHAR 6
%define T_STRING 7
%define T_SYMBOL 8
%define T_CLOSURE 9
%define T_PAIR 10
%define TYPE_SIZE 1
%define WORD_SIZE 8
%define KB(n) n*1024
%define MB(n) 1024*KB(n)
%define GB(n) 1024*MB(n)
%define PARAM_COUNT qword[ rbp + 3 * WORD_SIZE ]
%macro SKIP_TYPE_TAG 2
mov %1, qword [%2+TYPE_SIZE]
%endmacro
%define NUMERATOR SKIP_TYPE_TAG
%macro DENOMINATOR 2
mov %1, qword [%2+TYPE_SIZE+WORD_SIZE]
%endmacro
%macro CHAR_VAL 2
movzx %1, byte [%2+TYPE_SIZE]
%endmacro
%define FLOAT_VAL SKIP_TYPE_TAG
%define STRING_LENGTH SKIP_TYPE_TAG
%define SYMBOL_VAL SKIP_TYPE_TAG
%macro STRING_ELEMENTS 2
lea %1, [%2+TYPE_SIZE+WORD_SIZE]
%endmacro
%define CAR SKIP_TYPE_TAG
%macro CDR 2
mov %1, qword [%2+TYPE_SIZE+WORD_SIZE]
%endmacro
%define CLOSURE_ENV CAR
%define CLOSURE_CODE CDR
%define PVAR(n) qword [rbp+(4+n)*WORD_SIZE]
; returns %2 allocated bytes in register %1
; Supports using with %1 = %2
%macro MALLOC 2
add qword [malloc_pointer], %2
push %2
mov %1, qword [malloc_pointer]
sub %1, [rsp]
add rsp, 8
%endmacro
; Creates a short SOB with the
; value %2
; Returns the result in register %1
%macro MAKE_CHAR_VALUE 2
MALLOC %1, 1+TYPE_SIZE
mov byte [%1], T_CHAR
mov byte [%1+TYPE_SIZE], %2
%endmacro
; Creates a long SOB with the
; value %2 and type %3.
; Returns the result in register %1
%macro MAKE_LONG_VALUE 3
MALLOC %1, TYPE_SIZE+WORD_SIZE
mov byte [%1], %3
mov qword [%1+TYPE_SIZE], %2
%endmacro
%define MAKE_FLOAT(r,val) MAKE_LONG_VALUE r, val, T_FLOAT
%define MAKE_CHAR(r,val) MAKE_CHAR_VALUE r, val
; Create a string of length %2
; from char %3.
; Stores result in register %1
%macro MAKE_STRING 3
lea %1, [%2+WORD_SIZE+TYPE_SIZE]
MALLOC %1, %1
mov byte [%1], T_STRING
mov qword [%1+TYPE_SIZE], %2
push rcx
add %1,WORD_SIZE+TYPE_SIZE
mov rcx, %2
cmp rcx, 0
%%str_loop:
jz %%str_loop_end
dec rcx
mov byte [%1+rcx], %3
jmp %%str_loop
%%str_loop_end:
pop rcx
sub %1, WORD_SIZE+TYPE_SIZE
%endmacro
;;; Creates a SOB with tag %2
;;; from two pointers %3 and %4
;;; Stores result in register %1
%macro MAKE_TWO_WORDS 4
MALLOC %1, TYPE_SIZE+WORD_SIZE*2
mov byte [%1], %2
mov qword [%1+TYPE_SIZE], %3
mov qword [%1+TYPE_SIZE+WORD_SIZE], %4
%endmacro
%macro MAKE_WORDS_LIT 3
db %1
dq %2
dq %3
%endmacro
%macro MAKE_LITERAL 2
db %1
%2
%endmacro
%define MAKE_RATIONAL(r, num, den) \
MAKE_TWO_WORDS r, T_RATIONAL, num, den
%define MAKE_LITERAL_RATIONAL(num, den) \
MAKE_WORDS_LIT T_RATIONAL, num, den
%define MAKE_PAIR(r, car, cdr) \
MAKE_TWO_WORDS r, T_PAIR, car, cdr
%define MAKE_LITERAL_PAIR(car, cdr) \
MAKE_WORDS_LIT T_PAIR, car, cdr
%define MAKE_CLOSURE(r, env, body) \
MAKE_TWO_WORDS r, T_CLOSURE, env, body
%define MAKE_LITERAL_CHAR(val) MAKE_LITERAL T_CHAR, db val
%define MAKE_LITERAL_FLOAT(val) MAKE_LITERAL T_FLOAT, dq val
%define MAKE_LITERAL_SYMBOL(val) MAKE_LITERAL T_SYMBOL, dq val
%macro MAKE_LITERAL_STRING 1+
db T_STRING
dq (%%end_str- %%str)
%%str:
db %1
%%end_str:
%endmacro
;;; Macros and routines for printing Scheme OBjects to STDOUT
%define CHAR_NUL 0
%define CHAR_TAB 9
%define CHAR_NEWLINE 10
%define CHAR_PAGE 12
%define CHAR_RETURN 13
%define CHAR_SPACE 32
%define CHAR_DOUBLEQUOTE 34
%define CHAR_BACKSLASH 92
%macro SHIFT_FRAME 1
push rax
mov rax, PARAM_COUNT
add rax, 4
%assign i 1
%rep %1
dec rax
push qword [rbp-WORD_SIZE*i]
pop qword [rbp+WORD_SIZE*rax]
%assign i i+1
%endrep
pop rax
%endmacro
extern printf, malloc
global write_sob, write_sob_if_not_void
write_sob_undefined:
push rbp
mov rbp, rsp
mov rax, qword 0
mov rdi, .undefined
call printf
pop rbp
ret
section .data
.undefined:
db "#<undefined>", 0
write_sob_rational:
push rbp
mov rbp, rsp
mov rdx, rsi
NUMERATOR rsi, rdx
DENOMINATOR rdx, rdx
cmp rdx, 1
jne .print_fraction
mov rdi, .int_format_string
jmp .print
.print_fraction:
mov rdi, .frac_format_string
.print:
mov rax, 0
call printf
pop rbp
ret
section .data
.int_format_string:
db "%ld", 0
.frac_format_string:
db "%ld/%ld", 0
write_sob_float:
push rbp
mov rbp, rsp
FLOAT_VAL rsi, rsi
movq xmm0, rsi
mov rdi, .float_format_string
mov rax, 1
;; printf-ing floats (among other things) requires the stack be 16-byte aligned
;; so align the stack *downwards* (take up some extra space) if needed before
;; calling printf for floats
and rsp, -16
call printf
;; move the stack back to the way it was, cause we messed it up in order to
;; call printf.
;; Note that the `leave` instruction does exactly this (reset the stack and pop
;; rbp). The instructions are explicitly layed out here for clarity.
mov rsp, rbp
pop rbp
ret
section .data
.float_format_string:
db "%f", 0
write_sob_char:
push rbp
mov rbp, rsp
CHAR_VAL rsi, rsi
cmp rsi, CHAR_NUL
je .Lnul
cmp rsi, CHAR_TAB
je .Ltab
cmp rsi, CHAR_NEWLINE
je .Lnewline
cmp rsi, CHAR_PAGE
je .Lpage
cmp rsi, CHAR_RETURN
je .Lreturn
cmp rsi, CHAR_SPACE
je .Lspace
jg .Lregular
mov rdi, .special
jmp .done
.Lnul:
mov rdi, .nul
jmp .done
.Ltab:
mov rdi, .tab
jmp .done
.Lnewline:
mov rdi, .newline
jmp .done
.Lpage:
mov rdi, .page
jmp .done
.Lreturn:
mov rdi, .return
jmp .done
.Lspace:
mov rdi, .space
jmp .done
.Lregular:
mov rdi, .regular
jmp .done
.done:
mov rax, 0
call printf
pop rbp
ret
section .data
.space:
db "#\space", 0
.newline:
db "#\newline", 0
.return:
db "#\return", 0
.tab:
db "#\tab", 0
.page:
db "#\page", 0
.nul:
db "#\nul", 0
.special:
db "#\x%02x", 0
.regular:
db "#\%c", 0
write_sob_void:
push rbp
mov rbp, rsp
mov rax, 0
mov rdi, .void
call printf
pop rbp
ret
section .data
.void:
db "#<void>", 0
write_sob_bool:
push rbp
mov rbp, rsp
cmp word [rsi], word T_BOOL
je .sobFalse
mov rdi, .true
jmp .continue
.sobFalse:
mov rdi, .false
.continue:
mov rax, 0
call printf
pop rbp
ret
section .data
.false:
db "#f", 0
.true:
db "#t", 0
write_sob_nil:
push rbp
mov rbp, rsp
mov rax, 0
mov rdi, .nil
call printf
pop rbp
ret
section .data
.nil:
db "()", 0
write_sob_string:
push rbp
mov rbp, rsp
push rsi
mov rax, 0
mov rdi, .double_quote
call printf
pop rsi
STRING_LENGTH rcx, rsi
STRING_ELEMENTS rax, rsi
.loop:
cmp rcx, 0
je .done
mov bl, byte [rax]
and rbx, 0xff
cmp rbx, CHAR_TAB
je .ch_tab
cmp rbx, CHAR_NEWLINE
je .ch_newline
cmp rbx, CHAR_PAGE
je .ch_page
cmp rbx, CHAR_RETURN
je .ch_return
cmp rbx, CHAR_DOUBLEQUOTE
je .ch_doublequote
cmp rbx, CHAR_BACKSLASH
je .ch_backslash
cmp rbx, CHAR_SPACE
jl .ch_hex
mov rdi, .fs_simple_char
mov rsi, rbx
jmp .printf
.ch_hex:
mov rdi, .fs_hex_char
mov rsi, rbx
jmp .printf
.ch_tab:
mov rdi, .fs_tab
mov rsi, rbx
jmp .printf
.ch_page:
mov rdi, .fs_page
mov rsi, rbx
jmp .printf
.ch_return:
mov rdi, .fs_return
mov rsi, rbx
jmp .printf
.ch_newline:
mov rdi, .fs_newline
mov rsi, rbx
jmp .printf
.ch_doublequote:
mov rdi, .fs_doublequote
mov rsi, rbx
jmp .printf
.ch_backslash:
mov rdi, .fs_backslash
mov rsi, rbx
.printf:
push rax
push rcx
mov rax, 0
call printf
pop rcx
pop rax
dec rcx
inc rax
jmp .loop
.done:
mov rax, 0
mov rdi, .double_quote
call printf
pop rbp
ret
section .data
.double_quote:
db CHAR_DOUBLEQUOTE, 0
.fs_simple_char:
db "%c", 0
.fs_hex_char:
db "\x%02x;", 0
.fs_tab:
db "\t", 0
.fs_page:
db "\f", 0
.fs_return:
db "\r", 0
.fs_newline:
db "\n", 0
.fs_doublequote:
db CHAR_BACKSLASH, CHAR_DOUBLEQUOTE, 0
.fs_backslash:
db CHAR_BACKSLASH, CHAR_BACKSLASH, 0
write_sob_pair:
push rbp
mov rbp, rsp
push rsi
mov rax, 0
mov rdi, .open_paren
call printf
mov rsi, [rsp]
CAR rsi, rsi
call write_sob
mov rsi, [rsp]
CDR rsi, rsi
call write_sob_pair_on_cdr
add rsp, 1*8
mov rdi, .close_paren
mov rax, 0
call printf
pop rbp
ret
section .data
.open_paren:
db "(", 0
.close_paren:
db ")", 0
write_sob_pair_on_cdr:
push rbp
mov rbp, rsp
mov bl, byte [rsi]
cmp bl, T_NIL
je .done
cmp bl, T_PAIR
je .cdrIsPair
push rsi
mov rax, 0
mov rdi, .dot
call printf
pop rsi
call write_sob
jmp .done
.cdrIsPair:
CDR rbx, rsi
push rbx
CAR rsi, rsi
push rsi
mov rax, 0
mov rdi, .space
call printf
pop rsi
call write_sob
pop rsi
call write_sob_pair_on_cdr
.done:
pop rbp
ret
section .data
.space:
db " ", 0
.dot:
db " . ", 0
write_sob_symbol:
push rbp
mov rbp, rsp
SYMBOL_VAL rsi, rsi
STRING_LENGTH rcx, rsi
STRING_ELEMENTS rax, rsi
mov rdx, rcx
.loop:
cmp rcx, 0
je .done
mov bl, byte [rax]
and rbx, 0xff
cmp rcx, rdx
jne .ch_simple
cmp rbx, '+'
je .ch_hex
cmp rbx, '-'
je .ch_hex
cmp rbx, 'A'
jl .ch_hex
.ch_simple:
mov rdi, .fs_simple_char
mov rsi, rbx
jmp .printf
.ch_hex:
mov rdi, .fs_hex_char
mov rsi, rbx
.printf:
push rax
push rcx
mov rax, 0
call printf
pop rcx
pop rax
dec rcx
inc rax
jmp .loop
.done:
pop rbp
ret
section .data
.fs_simple_char:
db "%c", 0
.fs_hex_char:
db "\x%02x;", 0
write_sob_closure:
push rbp
mov rbp, rsp
CLOSURE_CODE rdx, rsi
CLOSURE_ENV rsi, rsi
mov rdi, .closure
mov rax, 0
call printf
pop rbp
ret
section .data
.closure:
db "#<closure [env:%p, code:%p]>", 0
section .text
write_sob:
mov rbx, 0
mov bl, byte [rsi]
jmp qword [.jmp_table + rbx * 8]
section .data
.jmp_table:
dq write_sob_undefined, write_sob_void, write_sob_nil
dq write_sob_rational, write_sob_float, write_sob_bool
dq write_sob_char, write_sob_string, write_sob_symbol
dq write_sob_closure, write_sob_pair
section .text
write_sob_if_not_void:
mov rsi, rax
mov bl, byte [rsi]
cmp bl, T_VOID
je .continue
call write_sob
mov rax, 0
mov rdi, .newline
call printf
.continue:
ret
section .data
.newline:
db CHAR_NEWLINE, 0