-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.s
900 lines (727 loc) · 15.1 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
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
%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)
%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
; I added
%macro MAKE_LITERAL 2
db %1
%2
%endmacro
%define MAKE_LITERAL_FLOAT(val) \
MAKE_LITERAL T_FLOAT, dq val
%macro MAKE_LITERAL_STRING 1+
db T_STRING
dq (%%end_str - %%str)
%%str:
db %1
%%end_str:
%endmacro
%define MAKE_LITERAL_CHAR(val) \
MAKE_LITERAL T_CHAR, db val
%define MAKE_LITERAL_SYMBOL(val) \
MAKE_LITERAL T_SYMBOL, dq val
; until here
%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
;1- index 2- the new array
%macro COPY_ARGS 2
mov rsi, %1
mov rbx, %2
%%start_copy_args:
cmp rsi, 0
je %%end_copy_args
dec rsi
mov rdx, qword[rbp+ 8*(4+rsi)]
mov qword[rbx + 8*rsi], rdx ;changed from rax to rdx
jmp %%start_copy_args
%%end_copy_args:
%endmacro
;1- depth (d according to the new env) 2- new env array 3- old env array
%macro COPY_ENV 3
mov rsi, %1
dec rsi
mov r8, rsi
dec r8
mov rdi, %2
mov rdx, %3
%%start_copy_env:
cmp rsi, 0
je %%end_copy_env
mov r9, qword[rdx+8*r8]
mov qword [rdi+8*rsi], r9
dec rsi
dec r8
jmp %%start_copy_env
%%end_copy_env:
%endmacro
;1- number of arguments in the old frame (n) 2- number of arguments in the new frame (m) don't touch rbx!!
%macro UPDATE_FRAME 2
mov rsi, %1 ;number of arguments in the old frame
mov rdi, %2 ;number of arguments in the new frame (m)
mov rcx, rbp ; save the pointer to the lower rbp, where we want to copy from
mov r11, qword[rbp + 8 * 3] ;number of arguments in the old frame, we dont remove 1 fo n-1 because we want to get over the magic as well
;dec r11 ; we want n-1
sub rsi, rdi
mov r9, 8
mov r12, rcx ; get the rbp before the change, the lower one
mov rax, r11 ; save the place where we want to start copy to (the place of the n-1 argument)
add rax , 4 ; add 4 for rbp+8*(4+minor)
imul r9 ; now the mul
add r12, rax ;add rhe result of 8*(4+minor) so the pointer wiil be to the An-1, start copy to this place
mov rbp, qword[rbp] ;save the old rbp
mov rax, 1
add rdi, 5 ;because we want to copy the magic as well (m+3+1+1) 1for the magic and 1 to stop the loop
%%start_update_frame:
cmp rax, rdi
je %%end_update_frame
mov r8, rcx
mov r9, 8
mov r10, rax ; save the value of rax before mul
imul r9
sub r8, rax ; the place of the info that we want to copy
mov r8, qword[r8] ; the actual info
mov qword[r12], r8 ; remove the info to it's new location
mov rax, r10 ; restore the value
add rax, 1
sub r12, 8 ; the next place we want to copy to
jmp %%start_update_frame
%%end_update_frame:
;sub rax, 1
add rax, rsi ;according to 29:10 in lecture 15
mov rdx, rax
%%start_clean_loop:
cmp rdx, 0
jbe %%end_macro
pop rax
sub rdx, 1
jmp %%start_clean_loop
%%end_macro:
%endmacro
%macro LAMBDA_OPT 1
push rbp
mov rbp, rsp
mov rbx, qword[rbp + 8*3] ;number of parms in stack
mov rsi, qword[rbp + 8*3] ;number of parms in stack
mov rcx, %1 ;number of parms in lambda opt
cmp rbx, rcx
jg %%more_lambdaopt ;if the optional param isn't empty
mov rsi, SOB_NIL_ADDRESS ;create empty list
mov qword[rbp+8*(4+rbx)],rsi ;insert the empty list as 4 param
jmp %%end_lambdaopt
%%more_lambdaopt:
sub rbx, rcx ;get the number of optional parms
mov r9, rsi
dec r9
dec rbx
mov r10, rcx
dec r10
%%update_n:
mov rax, rsi ;rax << old n-num of optional params+1
sub rax, rbx
mov qword[rbp+8*3],rax ;update n
dec rsi
mov rcx, SOB_NIL_ADDRESS
%%add_parm_lambdaopt:
mov rax, qword[rbp+8*(4+rsi)] ;rax << stack param
;mov qword[rcx+8*rbx], rax ;insert the param to the list
MAKE_PAIR (r11, rax, rcx)
mov rcx, r11
dec rbx
dec rsi
cmp rbx, 0
jge %%add_parm_lambdaopt
mov qword[rbp+8*(4+r9)], rcx
dec r9
%%up_stack_lambdaOpt:
cmp r10, 0
jl %%remove_lambdaOpt
mov rax, qword[rbp+8*(4+r10)]
mov qword[rbp+8*(4+r9)],rax
dec r9
dec r10
jmp %%up_stack_lambdaOpt
%%remove_lambdaOpt:
mov rax, qword[rbp+8*3]
mov qword[rbp+8*(4+r9)], rax
dec r9
mov rax, qword[rbp+8*2]
mov qword[rbp+8*(4+r9)], rax
dec r9
mov rax, qword[rbp+8*1]
mov qword[rbp+8*(4+r9)], rax
dec r9
mov rax, qword[rbp]
mov qword[rbp+8*(4+r9)], rax
dec r9
add r9, 4
mov rax, r9
inc rax
mov rcx, 8
imul rcx
%%pops_lambdaOpt:
cmp r9, 0
jl %%fix_rbp_lambdaOpt
pop rbx
dec r9
jmp %%pops_lambdaOpt
%%fix_rbp_lambdaOpt:
add rbp, rax
%%end_lambdaopt:
mov rsp, rbp
pop rbp
%endmacro
%macro APPLY 0
mov rbx, qword[rbp+8*3] ;num of args
mov rsi, 0 ;num of args in the list
dec rbx
lea rdx, [rbp+8*(4+rbx)] ;the list
mov rdx, [rdx]
mov rcx, SOB_NIL_ADDRESS ;reverse list
push SOB_NIL_ADDRESS ;magic
%%list_args_apply:
cmp rdx, const_tbl+1
je %%push_apply
CAR rax, rdx
MAKE_PAIR (rdi, rax, rcx)
mov rcx, rdi
inc rsi
CDR rdx,rdx
jmp %%list_args_apply
%%push_apply:
cmp rcx, const_tbl+1
je %%push_opt_apply
CAR rax, rcx
push rax
CDR rcx,rcx
jmp %%push_apply
%%push_opt_apply:
cmp rbx, 1 ;without proc
je %%applicTP_apply
dec rbx
mov rax, qword[rbp+8*(4+rbx)]
push rax
inc rsi
jmp %%push_opt_apply
%%applicTP_apply:
push rsi ;push the new num of args
mov rbx, qword[rbp+8*4]
CLOSURE_ENV rax, rbx
push rax ;push lex-env
push qword[rbp+8] ;push ret-add
mov rcx, qword[rbp+8*3] ;old num of args
mov rax, rsi
UPDATE_FRAME rcx, rax
CLOSURE_CODE rax, rbx
jmp rax
%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
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