This repository was archived by the owner on Apr 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheme.s
812 lines (673 loc) · 11.2 KB
/
scheme.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
;;; scheme.s
;;; Support for the Scheme compiler
%define T_UNDEFINED 0
%define T_VOID 1
%define T_NIL 2
%define T_INTEGER 3
%define T_FRACTION 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 T_VECTOR 11
%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 TYPE_BITS 4
%define WORD_SIZE 64
%define MAKE_LITERAL(type, lit) ((lit << TYPE_BITS) | type)
%define MAKE_LITERAL_SYMBOL(address) (((address - start_of_data) << TYPE_BITS) | T_SYMBOL)
%macro TYPE 1
and %1, ((1 << TYPE_BITS) - 1)
%endmacro
%macro DATA 1
sar %1, TYPE_BITS
%endmacro
%macro DATA_UPPER 1
shr %1, (((WORD_SIZE - TYPE_BITS) >> 1) + TYPE_BITS)
%endmacro
%macro DATA_LOWER 1
shl %1, ((WORD_SIZE - TYPE_BITS) >> 1)
DATA_UPPER %1
%endmacro
%define MAKE_LITERAL_PAIR(car, cdr) (((((car - start_of_data) << ((WORD_SIZE - TYPE_BITS) >> 1)) | (cdr - start_of_data)) << TYPE_BITS) | T_PAIR)
%macro CAR 1
DATA_UPPER %1
add %1, start_of_data
mov %1, qword [%1]
%endmacro
%macro CDR 1
DATA_LOWER %1
add %1, start_of_data
mov %1, qword [%1]
%endmacro
%define MAKE_LITERAL_FRACTION(numerator, denominator) (((((numerator - start_of_data) << ((WORD_SIZE - TYPE_BITS) >> 1)) | (denominator - start_of_data)) << TYPE_BITS) | T_FRACTION)
;;; MAKE_LITERAL_CLOSURE target, env, code
%macro MAKE_LITERAL_CLOSURE 3
push rax
push rbx
mov rax, %1
mov qword [rax], %2
sub qword [rax], start_of_data
shl qword [rax], ((WORD_SIZE - TYPE_BITS) >> 1)
lea rbx, [rax + 8]
sub rbx, start_of_data
or qword [rax], rbx
shl qword [rax], TYPE_BITS
or qword [rax], T_CLOSURE
mov qword [rax + 8], %3
pop rbx
pop rax
%endmacro
;;; MAKE_MALLOC_LITERAL_PAIR target-address, car-address, cdr-address
%macro MAKE_MALLOC_LITERAL_PAIR 3
push rax
push rbx
mov rax, %1
mov qword [rax], %2
sub qword [rax], start_of_data
shl qword [rax], ((WORD_SIZE - TYPE_BITS) >> 1)
mov rbx, %3
sub rbx, start_of_data
or qword [rax], rbx
shl qword [rax], TYPE_BITS
or qword [rax], T_PAIR
pop rbx
pop rax
%endmacro
%macro SYMBOL_STRING 1
mov rax, %1
DATA rax
add rax, start_of_data
mov rax, qword [rax]
%endmacro
%macro DENOMINATOR 1
mov rax, %1
mov rbx, rax
TYPE rbx
cmp rbx, T_INTEGER
je %%B_denominator_INT
CDR rax
DATA rax
jmp %%B_denominator_end
%%B_denominator_INT:
mov rax, 1
%%B_denominator_end:
sal rax, TYPE_BITS
sar rax, TYPE_BITS
%endmacro
%macro NUMERATOR 1
mov rax, %1
mov rbx, rax
TYPE rbx
cmp rbx, T_INTEGER
je %%B_numerator_end
CAR rax
%%B_numerator_end:
DATA rax
sal rax, TYPE_BITS
sar rax, TYPE_BITS
%endmacro
;;; GCD m, n
%macro GCD 2
mov rax, %1
cmp rax, 0
jge %%compute_rbx
neg rax
%%compute_rbx:
mov rbx, %2
cmp rbx, 0
jge %%loop
neg rbx
%%loop:
mov rdx, 0
div rbx
cmp rdx, 0
je %%leave
mov rax, rbx
mov rbx, rdx
jmp %%loop
%%leave:
mov rax, rbx
%endmacro
%macro ABS 1
cmp %1, 0
jge %%abs_end
neg %1
%%abs_end:
%endmacro
;;; REDUCE_FRAC_TO_INT target-address, numerator-address, denominator-address
%macro REDUCE_FRAC_TO_INT 3
mov rax, qword [%2]
sal rax, TYPE_BITS
sar rax, TYPE_BITS
mov rbx, qword [%3]
sal rbx, TYPE_BITS
sar rbx, TYPE_BITS
mov rcx, rax
cmp rbx, 0
jge %%cont_reduce
neg rax
neg rbx
mov rcx, rax
%%cont_reduce:
mov rdx, 0
div rbx
cmp rdx, 0 ; see if they divide exactly
jne %%make_frac
sal rax, TYPE_BITS
or rax, T_INTEGER
jmp %%REDUCE_END
%%make_frac: ; not an int, create write_sob_fraction
mov rax, rcx
sal rax, TYPE_BITS
or rax, T_INTEGER
mov qword [%2], rax
sal rbx, TYPE_BITS
or rbx, T_INTEGER
mov qword [%3], rbx
mov rax, %2
sub rax, start_of_data
shl rax, ((WORD_SIZE - TYPE_BITS) >> 1)
mov rbx, %3
sub rbx, start_of_data
or rax, rbx
shl rax, TYPE_BITS
or rax, T_FRACTION
%%REDUCE_END:
mov %1, rax
%endmacro
%macro CLOSURE_ENV 1
DATA_UPPER %1
add %1, start_of_data
%endmacro
%macro CLOSURE_CODE 1
DATA_LOWER %1
add %1, start_of_data
mov %1, qword [%1]
%endmacro
%macro MAKE_LITERAL_STRING 1+
dq (((((%%LstrEnd - %%Lstr) << ((WORD_SIZE - TYPE_BITS) >> 1)) | (%%Lstr - start_of_data)) << TYPE_BITS) | T_STRING)
%%Lstr:
db %1
%%LstrEnd:
%endmacro
%macro STRING_LENGTH 1
DATA_UPPER %1
%endmacro
%macro STRING_ELEMENTS 1
DATA_LOWER %1
add %1, start_of_data
%endmacro
;;; STRING_REF dest, src, index
%macro STRING_REF 3
push rax
mov rax, %2
STRING_ELEMENTS rax
add rax, %3
movzx %1, byte [rax]
pop rax
%endmacro
%macro MAKE_LITERAL_VECTOR 1+
dq ((((((%%VecEnd - %%Vec) >> 3) << ((WORD_SIZE - TYPE_BITS) >> 1)) | (%%Vec - start_of_data)) << TYPE_BITS) | T_VECTOR)
%%Vec:
dq %1
%%VecEnd:
%endmacro
%macro VECTOR_LENGTH 1
DATA_UPPER %1
%endmacro
%macro VECTOR_ELEMENTS 1
DATA_LOWER %1
add %1, start_of_data
%endmacro
;;; VECTOR_REF dest, src, index
%macro VECTOR_REF 3
mov %1, %2
VECTOR_ELEMENTS %1
lea %1, [%1 + %3*8]
mov %1, qword [%1]
mov %1, qword [%1]
%endmacro
%define SOB_UNDEFINED MAKE_LITERAL(T_UNDEFINED, 0)
%define SOB_VOID MAKE_LITERAL(T_VOID, 0)
%define SOB_FALSE MAKE_LITERAL(T_BOOL, 0)
%define SOB_TRUE MAKE_LITERAL(T_BOOL, 1)
%define SOB_NIL MAKE_LITERAL(T_NIL, 0)
%macro MY_MALLOC 1
mov rbx, malloc_pointer
mov rax, qword [rbx]
add qword [rbx], %1
%endmacro
section .bss
malloc_pointer:
resq 1
start_of_malloc:
resb 2^30
extern exit, printf, scanf
global main, write_sob, write_sob_if_not_void
section .text
write_sob_undefined:
push rbp
mov rbp, rsp
mov rax, 0
mov rdi, .undefined
call printf
leave
ret
section .data
.undefined:
db "#<undefined>", 0
write_sob_integer:
push rbp
mov rbp, rsp
mov rsi, qword [rbp + 8 + 1*8]
sar rsi, TYPE_BITS
mov rdi, .int_format_string
mov rax, 0
call printf
leave
ret
section .data
.int_format_string:
db "%ld", 0
write_sob_char:
push rbp
mov rbp, rsp
mov rsi, qword [rbp + 8 + 1*8]
DATA 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
leave
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%01x", 0
.regular:
db "#\%c", 0
write_sob_void:
push rbp
mov rbp, rsp
mov rax, 0
mov rdi, .void
call printf
leave
ret
section .data
.void:
db "#<void>", 0
write_sob_bool:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 8 + 1*8]
cmp rax, SOB_FALSE
je .sobFalse
mov rdi, .true
jmp .continue
.sobFalse:
mov rdi, .false
.continue:
mov rax, 0
call printf
leave
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
leave
ret
section .data
.nil:
db "()", 0
write_sob_string:
push rbp
mov rbp, rsp
mov rax, 0
mov rdi, .double_quote
call printf
mov rax, qword [rbp + 8 + 1*8]
mov rcx, rax
STRING_LENGTH rcx
STRING_ELEMENTS rax
.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_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
.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
leave
ret
section .data
.double_quote:
db '"', 0
.fs_simple_char:
db "%c", 0
.fs_hex_char:
db "\x%01x;", 0
.fs_tab:
db "\t", 0
.fs_page:
db "\f", 0
.fs_return:
db "\r", 0
.fs_newline:
db "\n", 0
write_sob_symbol:
push rbp
mov rbp, rsp
mov rax, 0
mov rax, qword [rbp + 8 + 1*8]
SYMBOL_STRING rax
mov rcx, rax
STRING_LENGTH rcx
STRING_ELEMENTS rax
.loop:
cmp rcx, 0
je .done
mov bl, byte [rax]
and rbx, 0xff
mov rdi, .fs_simple_char
mov rsi, rbx
push rax
push rcx
mov rax, 0
call printf
pop rcx
pop rax
dec rcx
inc rax
jmp .loop
.done:
leave
ret
section .data
.quote:
db "'", 0
.fs_simple_char:
db "%c", 0
write_sob_pair:
push rbp
mov rbp, rsp
mov rax, 0
mov rdi, .open_paren
call printf
mov rax, qword [rbp + 8 + 1*8]
CAR rax
push rax
call write_sob
add rsp, 1*8
mov rax, qword [rbp + 8 + 1*8]
CDR rax
push rax
call write_sob_pair_on_cdr
add rsp, 1*8
mov rdi, .close_paren
mov rax, 0
call printf
leave
ret
section .data
.open_paren:
db "(", 0
.close_paren:
db ")", 0
write_sob_pair_on_cdr:
push rbp
mov rbp, rsp
mov rbx, qword [rbp + 8 + 1*8]
mov rax, rbx
TYPE rbx
cmp rbx, T_NIL
je .done
cmp rbx, T_PAIR
je .cdrIsPair
push rax
mov rax, 0
mov rdi, .dot
call printf
call write_sob
add rsp, 1*8
jmp .done
.cdrIsPair:
mov rbx, rax
CDR rbx
push rbx
CAR rax
push rax
mov rax, 0
mov rdi, .space
call printf
call write_sob
add rsp, 1*8
call write_sob_pair_on_cdr
add rsp, 1*8
.done:
leave
ret
section .data
.space:
db " ", 0
.dot:
db " . ", 0
write_sob_vector:
push rbp
mov rbp, rsp
mov rax, 0
mov rdi, .fs_open_vector
call printf
mov rax, qword [rbp + 8 + 1*8]
mov rcx, rax
VECTOR_LENGTH rcx
cmp rcx, 0
je .done
VECTOR_ELEMENTS rax
push rcx
push rax
mov rax, qword [rax]
push qword [rax]
call write_sob
add rsp, 1*8
pop rax
pop rcx
dec rcx
add rax, 8
.loop:
cmp rcx, 0
je .done
push rcx
push rax
mov rax, 0
mov rdi, .fs_space
call printf
pop rax
push rax
mov rax, qword [rax]
push qword [rax]
call write_sob
add rsp, 1*8
pop rax
pop rcx
dec rcx
add rax, 8
jmp .loop
.done:
mov rax, 0
mov rdi, .fs_close_vector
call printf
leave
ret
section .data
.fs_open_vector:
db "#(", 0
.fs_close_vector:
db ")", 0
.fs_space:
db " ", 0
write_sob_fraction:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 8 + 1*8]
CAR rax
push rax
call write_sob
add rsp, 1*8
mov rax, qword [rbp + 8 + 1*8]
CDR rax
push rax
mov rax, 0
mov rdi, .frac_div
call printf
call write_sob
leave
ret
section .data
.frac_div:
db "/", 0
write_sob_closure:
push rbp
mov rbp, rsp
mov rsi, qword [rbp + 8 + 1*8]
mov rdx, rsi
CLOSURE_ENV rsi
CLOSURE_CODE rdx
mov rdi, .closure
mov rax, 0
call printf
leave
ret
section .data
.closure:
db "#<closure [env:%p, code:%p]>", 0
write_sob:
mov rax, qword [rsp + 1*8]
TYPE rax
jmp qword [.jmp_table + rax * 8]
section .data
.jmp_table:
dq write_sob_undefined, write_sob_void, write_sob_nil
dq write_sob_integer, write_sob_fraction, write_sob_bool
dq write_sob_char, write_sob_string, write_sob_symbol
dq write_sob_closure, write_sob_pair, write_sob_vector
section .text
write_sob_if_not_void:
mov rax, qword [rsp + 1*8]
cmp rax, SOB_VOID
je .continue
push rax
call write_sob
add rsp, 1*8
mov rax, 0
mov rdi, .newline
call printf
.continue:
ret
section .data
.newline:
db CHAR_NEWLINE, 0