-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.s
1037 lines (797 loc) · 17.6 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
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
%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]
%define NUM_PARAMS qword [rbp+(3+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
%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
; Make a literal of type %1
%macro MAKE_LITERAL 2
; followed by the definition %2
db %1
%2
%endmacro
%define MAKE_CONST_NIL \
db T_NIL
%define MAKE_CONST_VOID \
db T_VOID
%define MAKE_LITERAL_BOOL(val) \
MAKE_LITERAL T_BOOL, db val
; %macro MAKE_LITERAL_BOOL 1
; MAKE_LITERAL T_BOOL, db %1
; %endmacro
%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_STRING(val)
; MAKE_LITERAL T_STRING, 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
%define PARAM_COUNT qword [rbp + (3 * 8)]
%define PARAM_COUNT_OPT qword [rbp + (2 * 8)]
%define PARAM_COUNT_OPT_RSP qword [rsp + (2 * 8)]
;%1 = size of frame(constant)
%macro SHIFT_FRAME 1
push rax
mov rax, PARAM_COUNT ;PARAM_COUNT of father frame
add rax, 4 ;(not TODO) 4 cells if not magic , 5 if use of magic
%assign i 1
%rep %1
dec rax
push qword [rbp- i*WORD_SIZE]
pop qword [rbp+ rax*WORD_SIZE]
%assign i i+1
%endrep
pop rax
%endmacro
;%1 = size of frame(constant)
%macro SHIFT_FRAME_REGISTER 1
push rax
push rbx
push rdx
mov rax, PARAM_COUNT ;PARAM_COUNT of father frame
add rax, 4 ;(not TODO) 4 cells if not magic , 5 if use of magic
; %assign i 1
mov rbx, 1 ;from 1 to (7 + 5) = 12 include
%%start_loop:
; %rep %1
cmp rbx, %1
jg %%finish_loop
dec rax
mov rdx, rbp
mov rsi, rbx
shl rsi, 3
sub rdx, rsi ;; rbp - i * WORD_SIZE
push qword [rdx]
pop qword [rbp+ rax*WORD_SIZE]
; %assign i i+1
inc rbx
jmp %%start_loop
; %endrep
%%finish_loop:
pop rdx
pop rbx
pop rax
%endmacro
;%1 = size of frame(constant)
; %macro LAMBDA_OPT_SHIFT_FRAME 1
; push rax
; mov rax, PARAM_COUNT ;PARAM_COUNT of father frame
; add rax, 3 ;4+3=7
; %assign i 1
; %rep %1
; dec rax
; push qword [rbp- i*WORD_SIZE]
; pop qword [rbp+ rax*WORD_SIZE]
; %assign i i+1
; %endrep
; pop rax
; %endmacro
;get rsi shift gap
%macro LAMBDA_OPT_SHIFT_FRAME_UP 1
push rbp
mov rbp, rsp
add rbp, 8
push rax
push rbx
push rcx
push rdx
mov rax, PARAM_COUNT_OPT ;PARAM_COUNT of father frame
add rax, 2 ;4+2=6 is the place of the last cell
mov rcx, PARAM_COUNT_OPT ;PARAM_COUNT of father frame
add rcx, 3 ;4+3=7 is the num of iterations
;add rcx, 5 ;5 more iterations because of rbp and registers rcx=12
; %assign i 1
mov rbx, 1 ;from 1 to (7 + 5) = 12 include
%%start_loop:
; %rep %1
cmp rbx, rcx
jg %%finish_loop
push qword [rbp+ rax*WORD_SIZE]
mov rdx, %1 ;2
add rdx, rax ;8
pop qword [rbp+ (rdx*WORD_SIZE)]
dec rax
; %assign i i+1
inc rbx
jmp %%start_loop
; %endrep
%%finish_loop:
pop rdx
pop rcx
pop rbx
pop rax
pop rbp
%endmacro
; ;%1 = size of frame(constant)
; %macro SHIFT_FRAME_DOWN_BY_ONE_CELL 1
; ;this macro creates extra space for variadic when the variadic is empty list
; push rbp
; mov rbp, rsp
; ; add rbp, 8
; ; push rax
; ; push rcx
; mov rax, PARAM_COUNT_OPT ;PARAM_COUNT of father frame
; mov rcx, PARAM_COUNT_OPT ;PARAM_COUNT of father frame
; add rax, 2 ;rax = n+2 , 8 we have n+3 cells
; %assign i 0
; %rep %1
; ; mov rbx, [rbp + (i*WORD_SIZE)]
; ; mov qword [rbp + ((i*WORD_SIZE) - WORD_SIZE) ], rbx
; push qword [rbp + (i*WORD_SIZE)]
; pop qword [rbp + ((i*WORD_SIZE) - WORD_SIZE) ]
; %assign i i+1
; %endrep
; ;adding of nil to the new cell variadic
; shl rax, 3
; ;MAKE_PAIR(rdx,SOB_NIL_ADDRESS, SOB_NIL_ADDRESS)
; mov qword [rbp + rax], SOB_NIL_ADDRESS
; inc rcx
; mov qword [rbp + (2 * 8)] ,rcx ;change n to be (n+1) ==> args = n+1
; ; pop rcx
; ; pop rax
; pop rbp
; %endmacro
;%1 = size of frame(constant)
%macro SHIFT_FRAME_DOWN_BY_ONE_CELL 1
;this macro creates extra space for variadic when the variadic is empty list
push rbp
push rbp
push rax
push rcx
push rdi
mov rbp, rsp
add rbp, 5 * WORD_SIZE
; push rax
; push rcx
mov rax, qword [rbp + (2*WORD_SIZE)] ;PARAM_COUNT of father frame
mov rcx, qword [rbp + (2*WORD_SIZE)] ;PARAM_COUNT of father frame
add rax, 2 ;rax = n+2 , 8 we have n+3 cells
; add rax, 1 ;rax = n+2 , 8 we have n+3 cells
;%1 is the times we loop.
%assign i 0
%rep %1
mov rdi, qword [rbp + (i*WORD_SIZE)]
mov qword [rbp + ((i*WORD_SIZE) - WORD_SIZE ) ], rdi
; push qword [rbp + (i*WORD_SIZE)]
; pop qword [rbp + ((i*WORD_SIZE) - WORD_SIZE) ]
%assign i i+1
%endrep
;adding of nil to the new cell variadic
shl rax, 3
;MAKE_PAIR(rdx,SOB_NIL_ADDRESS, SOB_NIL_ADDRESS)
mov qword [rbp + rax], SOB_NIL_ADDRESS
inc rcx
mov qword [rbp + (1 * 8)] ,rcx ;change n to be (n+1) ==> args = n+1
pop rdi
pop rcx
pop rax
pop rbp
%endmacro
; %macro SHIFT_FRAME_DOWN_BY_ONE_CELL 1
; ;this macro creates extra space for variadic when the variadic is empty list
; ; %assign i 1
; mov rdx, 0 ;from 1 to (7 + 5) = 12 include
; %%start_loop:
; ; %rep %1
; cmp rdx, %1
; jg %%finish_loop
; mov rdi, [rsp + rdx*WORD_SIZE]
; mov qword [rsp + rdx*WORD_SIZE - WORD_SIZE ], rdi
; ; push qword [rbp + (i*WORD_SIZE)]
; ; pop qword [rbp + ((i*WORD_SIZE) - WORD_SIZE) ]
; ; %assign i i+1
; inc rdx
; jmp %%start_loop
; ; %endrep
; %%finish_loop:
; sub rsp, WORD_SIZE
; mov rdi, PARAM_COUNT_OPT_RSP ;PARAM_COUNT of father frame
; mov rsi, PARAM_COUNT_OPT_RSP ;PARAM_COUNT of father frame
; add rsi, 3 ;rax = n+2 , 8 we have n+3 cells
; ;MAKE_PAIR(rdx,SOB_NIL_ADDRESS, SOB_NIL_ADDRESS)
; mov r8, SOB_NIL_ADDRESS
; mov qword [rsp + rsi], r8
; inc rdi
; mov qword [rsp + (2 * 8)] ,rdi ;change n to be (n+1) ==> args = n+1
; %endmacro
; ;%1 = num of params to make a varidaic list
; %macro CREATE_VARIADIC_OPT_LIST 1
; push rax
; push rbx
; push rcx
; mov rax, PARAM_COUNT ;PARAM_COUNT of father frame
; add rax, 2 ;should be n+3 but we do one loop iteration outside.. so rax=n+2 last item, last cell of args
; ;first loop run
; mov rdx, qword [rbp+ rax*WORD_SIZE] ;from the lecture this is last param int=8
; MAKE_PAIR(rcx,rdx, SOB_NIL_ADDRESS) ;rcx hold first pair
; ;start from the second loop
; %assign i 2
; %rep %1
; ;we do the loop %1 minus 2 times
; mov rbx, rcx ;mov to rbx the old pair
; dec rax
; mov rdx, qword [rbp+ rax*WORD_SIZE] ;next arg on stack
; MAKE_PAIR(rcx,rdx, rbx)
; %assign i i+1
; %endrep
; mov qword [rbp+ rax*WORD_SIZE] , rcx ;put list in variadic cell
; pop rcx
; pop rbx
; pop rax
; %endmacro
;%1 = num of params to make a varidaic list
%macro CREATE_VARIADIC_OPT_LIST 1
push rbp
mov rbp, rsp
add rbp, 8
push rax
push rbx
push rcx
mov rax, PARAM_COUNT_OPT ;PARAM_COUNT_OPT of father frame
add rax, 2 ;should be n+3 but we do one loop iteration outside.. so rax=n+2 last item, last cell of args
;first loop run
mov rdx, qword [rbp+ rax*WORD_SIZE] ;from the lecture this is last param int=8
MAKE_PAIR(rcx,rdx, SOB_NIL_ADDRESS) ;rcx hold first pair
;start from the second loop
; %assign i 1
mov rdi, 2
%%start_loop:
; %rep %1
cmp rdi, %1
jg %%finish_loop
;we do the loop %1 minus 2 times
mov rbx, rcx ;mov to rbx the old pair
dec rax
mov rdx, qword [rbp+ rax*WORD_SIZE] ;next arg on stack
MAKE_PAIR(rcx,rdx, rbx)
; %assign i i+1
inc rdi
jmp %%start_loop
; %endrep
%%finish_loop:
mov qword [rbp+ rax*WORD_SIZE] , rcx ;put list in variadic cell
pop rcx
pop rbx
pop rax
pop rbp
%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