This repository has been 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
/
runtime-support.scm
1834 lines (1687 loc) · 35.9 KB
/
runtime-support.scm
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 set-up-global-env
"
mov rdi, 8
call malloc
mov r12, rax
")
(define not-assembly
(string-append "\n;; not? ;;\n" set-up-global-env "
CL_not:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_not
jmp L_not
B_not:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
cmp rax, SOB_FALSE
je B_not_false
mov rax, SOB_FALSE
jmp B_not_ret
B_not_false:
mov rax, SOB_TRUE
B_not_ret:
leave
ret
L_not:
mov rax, qword [rax]
mov qword [fvar_0], rax
mov rax, SOB_VOID
"))
(define car-assembly
(string-append "\n;; car ;;\n" set-up-global-env
"
CL_car:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_car
jmp L_car
B_car:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
CAR rax
leave
ret
L_car:
mov rax, qword [rax]
mov qword [fvar_1], rax
mov rax, SOB_VOID
"))
(define cdr-assembly
(string-append "\n;; cdr ;;\n" set-up-global-env
"
CL_cdr:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_cdr
jmp L_cdr
B_cdr:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
CDR rax
leave
ret
L_cdr:
mov rax, qword [rax]
mov qword [fvar_2], rax
mov rax, SOB_VOID
"))
(define char?-assembly
(string-append "\n;; char? ;;\n" set-up-global-env
"
CL_char:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_char
jmp L_char
B_char:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
TYPE rax
cmp rax, T_CHAR
je B_char_T
mov rax, SOB_FALSE
jmp B_char_end
B_char_T:
mov rax, SOB_TRUE
B_char_end:
leave
ret
L_char:
mov rax, qword [rax]
mov qword [fvar_3], rax
mov rax, SOB_VOID
"))
(define integer?-assembly
(string-append "\n;; integer? ;;\n" set-up-global-env
"
CL_integer:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_integer
jmp L_integer
B_integer:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
TYPE rax
cmp rax, T_INTEGER
je B_integer_T
mov rax, SOB_FALSE
jmp B_integer_end
B_integer_T:
mov rax, SOB_TRUE
B_integer_end:
leave
ret
L_integer:
mov rax, qword [rax]
mov qword [fvar_4], rax
mov rax, SOB_VOID
"))
(define boolean?-assembly
(string-append "\n;; boolean? ;;\n" set-up-global-env
"
CL_bool:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_bool
jmp L_bool
B_bool:
push rbp
mov rbp, rsp
mov rax, qword[rbp + 4*8]
TYPE rax
cmp rax, T_BOOL
je B_bool_true
mov rax, qword[const_2]
jmp B_bool_end
B_bool_true:
mov rax, qword[const_3]
B_bool_end:
leave
ret
L_bool:
mov rax, qword [rax]
mov qword [fvar_20], rax
mov rax, SOB_VOID
"))
(define null?-assembly
(string-append "\n;; null? ;;\n" set-up-global-env
"
CL_null:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_null
jmp L_null
B_null:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
TYPE rax
cmp rax, T_NIL
je B_null_T
mov rax, SOB_FALSE
jmp B_null_end
B_null_T:
mov rax, SOB_TRUE
B_null_end:
leave
ret
L_null:
mov rax, qword [rax]
mov qword [fvar_5], rax
mov rax, SOB_VOID
"))
(define number?-assembly
(string-append "\n;; number? ;;\n" set-up-global-env
"
CL_number:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_number
jmp L_number
B_number:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
TYPE rax
cmp rax, T_INTEGER
je B_number_T
cmp rax, T_FRACTION
je B_number_T
mov rax, SOB_FALSE
jmp B_number_end
B_number_T:
mov rax, SOB_TRUE
B_number_end:
leave
ret
L_number:
mov rax, qword [rax]
mov qword [fvar_6], rax
mov rax, SOB_VOID
"))
(define pair?-assembly
(string-append "\n;; pair? ;;\n" set-up-global-env
"
CL_pair:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_pair
jmp L_pair
B_pair:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
TYPE rax
cmp rax, T_PAIR
je B_pair_T
mov rax, SOB_FALSE
jmp B_pair_end
B_pair_T:
mov rax, SOB_TRUE
B_pair_end:
leave
ret
L_pair:
mov rax, qword [rax]
mov qword [fvar_7], rax
mov rax, SOB_VOID
"))
(define procedure?-assembly
(string-append "\n;; procedure? ;;\n" set-up-global-env
"
CL_procedure:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_procedure
jmp L_procedure
B_procedure:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
TYPE rax
cmp rax, T_CLOSURE
je B_procedure_T
mov rax, SOB_FALSE
jmp B_procedure_end
B_procedure_T:
mov rax, SOB_TRUE
B_procedure_end:
leave
ret
L_procedure:
mov rax, qword [rax]
mov qword [fvar_8], rax
mov rax, SOB_VOID
"))
(define string?-assembly
(string-append "\n;; string? ;;\n" set-up-global-env
"
CL_string:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_string
jmp L_string
B_string:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
TYPE rax
cmp rax, T_STRING
je B_string_T
mov rax, SOB_FALSE
jmp B_string_end
B_string_T:
mov rax, SOB_TRUE
B_string_end:
leave
ret
L_string:
mov rax, qword [rax]
mov qword [fvar_9], rax
mov rax, SOB_VOID
"))
(define symbol?-assembly
(string-append "\n;; symbol? ;;\n" set-up-global-env
"
CL_symbol:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_symbol
jmp L_symbol
B_symbol:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
TYPE rax
cmp rax, T_SYMBOL
je B_symbol_T
mov rax, SOB_FALSE
jmp B_symbol_end
B_symbol_T:
mov rax, SOB_TRUE
B_symbol_end:
leave
ret
L_symbol:
mov rax, qword [rax]
mov qword [fvar_10], rax
mov rax, SOB_VOID
"))
(define vector?-assembly
(string-append "\n;; vector? ;;\n" set-up-global-env
"
CL_vector:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_vector
jmp L_vector
B_vector:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
TYPE rax
cmp rax, T_VECTOR
je B_vector_T
mov rax, SOB_FALSE
jmp B_vector_end
B_vector_T:
mov rax, SOB_TRUE
B_vector_end:
leave
ret
L_vector:
mov rax, qword [rax]
mov qword [fvar_11], rax
mov rax, SOB_VOID
"))
(define set-car!-assembly
(string-append "\n;; set-car! ;;\n" set-up-global-env
"
CL_set_car:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_set_car
jmp L_set_car
B_set_car:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
TYPE rax
cmp rax, T_PAIR
jne END
mov rbx, qword [rbp + 5*8]
mov rax, qword [rbp + 4*8]
DATA_UPPER rax
add rax, start_of_data
mov qword [rax], rbx
mov rax, SOB_VOID
leave
ret
L_set_car:
mov rax, qword [rax]
mov qword [fvar_12], rax
mov rax, SOB_VOID
"))
(define set-cdr!-assembly
(string-append "\n;; set-cdr! ;;\n" set-up-global-env
"
CL_set_cdr:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_set_cdr
jmp L_set_cdr
B_set_cdr:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
TYPE rax
cmp rax, T_PAIR
jne END
mov rbx, qword [rbp + 5*8]
mov rax, qword [rbp + 4*8]
DATA_LOWER rax
add rax, start_of_data
mov qword [rax], rbx
mov rax, SOB_VOID
leave
ret
L_set_cdr:
mov rax, qword [rax]
mov qword [fvar_13], rax
mov rax, SOB_VOID
"))
(define equals-assembly
(string-append "\n;; = ;;\n" set-up-global-env
"
CL_equals:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_equals
jmp L_equals
B_equals:
push rbp
mov rbp, rsp
mov r8, qword [rbp + 3*8] ; get actual num of params
sub r8, 1 ; ignore '() param
mov rax, 0
mov r9, qword [rbp + 4*8]
equals_loop:
inc rax
cmp rax, r8
je equals_B_true
mov rbx, rax
add rbx, 4
shl rbx, 3
mov r10, qword [rbp + rbx] ; rbx = (rax + 4)*8
cmp r9, r10
jne equals_B_false
jmp equals_loop
equals_B_true:
mov rax, SOB_TRUE
jmp equals_B_end
equals_B_false:
mov rax, SOB_FALSE
equals_B_end:
leave
ret
L_equals:
mov rax, qword [rax]
mov qword [fvar_14], rax
mov rax, SOB_VOID
"))
(define greater-than-assembly
(string-append set-up-global-env
"
CL_gt:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_gt
jmp L_gt
B_gt:
push rbp
mov rbp, rsp
mov r8, qword [rbp + 3*8] ; get actual num of params
sub r8, 1 ; ignore '() param
mov r11, 0
mov r9, qword [rbp + 4*8]
gt_loop:
inc r11
cmp r11, r8
je gt_B_true
mov rbx, r11
add rbx, 4
shl rbx, 3
mov r10, qword [rbp + rbx] ; rbx = (rax + 4)*8
; r9 = a/b, r10 = c/d, cmp a*d < b*c
mov r12, r9
NUMERATOR r12 ; r12 = a
mov r12, rax
mov rbx, r10
DENOMINATOR rbx ; rbx = d
mul r12
mov r12, rax ; r12 = a * d
mov r13, r9
DENOMINATOR r13
mov r13, rax ; r13 = b
mov rbx, r10
NUMERATOR rbx ; rbx = c
mul r13
mov r13, rax ; r13 = b * c
cmp r12, r13
jle gt_B_false
mov r9, r10
jmp gt_loop
gt_B_true:
mov rax, SOB_TRUE
jmp gt_B_end
gt_B_false:
mov rax, SOB_FALSE
gt_B_end:
leave
ret
L_gt:
mov rax, qword [rax]
mov qword [fvar_15], rax
mov rax, SOB_VOID
"))
(define less-than-assembly
(string-append "\n;; < ;;\n" set-up-global-env
"
CL_lt:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_lt
jmp L_lt
B_lt:
push rbp
mov rbp, rsp
mov r8, qword [rbp + 3*8] ; get actual num of params
sub r8, 1 ; ignore '() param
mov r11, 0
mov r9, qword [rbp + 4*8]
lt_loop:
inc r11
cmp r11, r8
je gt_B_true
mov rbx, r11
add rbx, 4
shl rbx, 3
mov r10, qword [rbp + rbx] ; rbx = (rax + 4)*8
; r9 = a/b, r10 = c/d, cmp a*d < b*c
mov r12, r9
NUMERATOR r12 ; r12 = a
mov r12, rax
mov rbx, r10
DENOMINATOR rbx ; rbx = d
mul r12
mov r12, rax ; r12 = a * d
mov r13, r9
DENOMINATOR r13
mov r13, rax ; r13 = b
mov rbx, r10
NUMERATOR rbx ; rbx = c
mul r13
mov r13, rax ; r13 = b * c
cmp r12, r13
jge lt_B_false
mov r9, r10
jmp lt_loop
lt_B_true:
mov rax, SOB_TRUE
jmp lt_B_end
lt_B_false:
mov rax, SOB_FALSE
lt_B_end:
leave
ret
L_lt:
mov rax, qword [rax]
mov qword [fvar_16], rax
mov rax, SOB_VOID
"))
(define plus-assembly
(string-append "\n;; + ;;\n" set-up-global-env
"
CL_plus:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_plus
jmp L_plus
B_plus:
push rbp
mov rbp, rsp
mov r9, qword [rbp + 4*8]
TYPE r9,
cmp r9, T_NIL
je plus_zero
mov r9, qword [rbp + 4*8]
mov rax, r9
mov r14, qword [rbp + 3*8]
sub r14, 1
mov r15, 0
plus_loop:
inc r15
cmp r15, r14
je plus_end
mov rbx, r15
add rbx, 4
shl rbx, 3
mov r8, qword [rbp + rbx] ; rbx = (r15 + 4)*8
; r9 = a/b, r8 = c/d
NUMERATOR r9
mov r10, rax ; r10 = a
DENOMINATOR r8
mul r10
mov r10, rax ; r10 = a*d
NUMERATOR r8
mov r11, rax ; r11 = c
DENOMINATOR r9
mul r11
mov r11, rax ; r11 = b*c
DENOMINATOR r8
mov r12, rax
DENOMINATOR r9
mul r12
mov r12, rax ; r12 = b*d = solution denominator
add r10, r11 ; r10 = solution numerator
cmp r12, 0
jge B_plus_cont
neg r10
neg r12
B_plus_cont:
GCD r10, r12
mov r11, rax ; r11 = gcd(numerator, denominator)
mov rax, r10
cmp r10, 0
jge B_plus_reduce_numerator
mov rdx, -1
B_plus_reduce_numerator:
idiv r11
mov r13, rax ; r13 = numerator after reduction
mov rax, r12
div r11
mov r12, rax ; r12 = denominator after reduction
mov rdi, 8
call malloc
mov r8, r13
mov r13, rax
mov qword [r13], r8
mov rdi, 8
call malloc
mov r9, r12
mov r12, rax
mov qword [r12], r9
mov rdi, 8
REDUCE_FRAC_TO_INT r10, r13, r12 ; r10 = pointer to dest, r8 - numerator, r9 - denominator
mov r9, r10
jmp plus_loop
plus_zero:
mov rax, 0
sal rax, TYPE_BITS
or rax, T_INTEGER
plus_end:
leave
ret
L_plus:
mov rax, qword [rax]
mov qword [fvar_17], rax
mov rax, SOB_VOID
"))
(define numerator-assembly
(string-append "\n;; numerator ;;\n" set-up-global-env
"
CL_numerator:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_numerator
jmp L_numerator
B_numerator:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
NUMERATOR rax
sal rax, TYPE_BITS
or rax, T_INTEGER
leave
ret
L_numerator:
mov rax, qword [rax]
mov qword [fvar_18], rax
mov rax, SOB_VOID
"))
(define denominator-assembly
(string-append "\n;; denominator ;;\n" set-up-global-env
"
CL_denominator:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_denominator
jmp L_denominator
B_denominator:
push rbp
mov rbp, rsp
mov rax, qword [rbp + 4*8]
DENOMINATOR rax
sal rax, TYPE_BITS
or rax, T_INTEGER
leave
ret
L_denominator:
mov rax, qword [rax]
mov qword [fvar_19], rax
mov rax, SOB_VOID
"))
(define mul-assembly
(string-append "\n;; mul ;;\n" set-up-global-env
"
CL_mul:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_mul
jmp L_mul
B_mul:
push rbp
mov rbp, rsp
mov r9, qword [rbp + 4*8]
TYPE r9,
cmp r9, T_NIL
je mul_one
mov r9, qword [rbp + 4*8]
mov rax, r9
mov r14, qword [rbp + 3*8]
sub r14, 1
mov r15, 0
mul_loop:
inc r15
cmp r15, r14
je mul_end
mov rbx, r15
add rbx, 4
shl rbx, 3
mov r8, qword [rbp + rbx] ; rbx = (r15 + 4)*8
; r9 = a/b, r8 = c/d
NUMERATOR r9
mov r10, rax ; r10 = a
NUMERATOR r8
mul r10
mov r10, rax ; r10 = a*c = solution numerator
DENOMINATOR r8
mov r11, rax ; r11 = b
DENOMINATOR r9
mul r11
mov r11, rax ; r11 = b*d = solution denominator
GCD r10, r11
mov r12, rax ; r12 = gcd(numerator, denominator)
mov rax, r10
div r12
mov r13, rax ; r13 = numerator after reduction
mov rax, r11
div r12
mov r12, rax ; r12 = denominator after reduction
mov rdi, 8
call malloc
mov r8, r13
mov r13, rax
mov qword [r13], r8
mov rdi, 8
call malloc
mov r9, r12
mov r12, rax
mov qword [r12], r9
mov rdi, 8
REDUCE_FRAC_TO_INT r10, r13, r12 ; r10 = pointer to dest, r8 - numerator, r9 - denominator
mov r9, r10
jmp mul_loop
mul_one:
mov rax, 1
sal rax, TYPE_BITS
or rax, T_INTEGER
mul_end:
leave
ret
L_mul:
mov rax, qword [rax]
mov qword [fvar_21], rax
mov rax, SOB_VOID
"))
(define div-assembly
(string-append "\n;; / ;;\n" set-up-global-env
"
CL_div:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_div
jmp L_div
B_div:
push rbp
mov rbp, rsp
mov r9, qword [rbp + 4*8]
TYPE r9,
cmp r9, T_NIL
je END ; Dividing with no parameters is invalid
mov r14, qword [rbp + 3*8]
cmp r14, 3
jl div_with_1
mov r9, qword [rbp + 4*8]
mov rax, r9
mov r15, 1
jmp div_loop
div_with_1:
mov rax, 1
sal rax, TYPE_BITS
or rax, T_INTEGER
mov r9, rax
mov r15, 0
div_loop:
inc r15
cmp r15, r14
je div_end
mov rbx, r15
add rbx, 3
shl rbx, 3
mov r8, qword [rbp + rbx] ; rbx = (r15 + 3)*8
; r9 = a/b, r8 = c/d
NUMERATOR r9
mov r10, rax ; r10 = a
DENOMINATOR r8
mul r10
mov r10, rax ; r10 = a*d = solution numerator
DENOMINATOR r9
mov r11, rax ; r11 = b
NUMERATOR r8
mul r11
mov r11, rax ; r11 = b*c = solution denominator
cmp r11, 0
jge B_div_cont
neg r10
neg r11
B_div_cont:
GCD r10, r11
mov r12, rax ; r12 = gcd(numerator, denominator)
mov rax, r10
div r12
mov r13, rax ; r13 = numerator after reduction
mov rax, r11
div r12
mov r12, rax ; r12 = denominator after reduction
mov rdi, 8
call malloc
mov r8, r13
mov r13, rax
mov qword [r13], r8
mov rdi, 8
call malloc
mov r9, r12
mov r12, rax
mov qword [r12], r9
mov rdi, 8
REDUCE_FRAC_TO_INT r10, r13, r12 ; r10 = pointer to dest, r8 - numerator, r9 - denominator
mov r9, r10
jmp div_loop
div_end:
; check that value is not integer
mov r15, rax
TYPE r15
xor r15,T_FRACTION
cmp r15,0
jne ENDDivide
; if Denom is negative, multiply Denom and Numer by -1
mov r15, rax
DENOMINATOR r15
cmp rax,0
jge DenomWasPositive
; denom was negative, need to multiply
mov r13, rax
CAR rax
mov r15, rax
mov rax,-1
mov r14, qword [r15]
mul r14
mov qword [r15], r14
mov rax, r13
CDR rax
mov r15, rax
mov rax,-1
mov r14, qword [r15]
mul r14
mov qword [r15], r14
mov rax, r13
jmp ENDDivide
DenomWasPositive:
mov rax, r15
ENDDivide:
leave
ret
L_div:
mov rax, qword [rax]
mov qword [fvar_22], rax
mov rax, SOB_VOID
"))
(define subtract-assembly
(string-append "\n;; - ;;\n" set-up-global-env
"
CL_sub:
mov rdi, 16
call malloc
MAKE_LITERAL_CLOSURE rax, rbx, B_sub
jmp L_sub
B_sub:
push rbp
mov rbp, rsp
mov r9, qword [rbp + 4*8]
TYPE r9
cmp r9, T_NIL
je END ; Subtracting with no parameters is invalid
mov r14, qword [rbp + 3*8]
cmp r14, 3
jl sub_with_0
mov r9, qword [rbp + 4*8]
mov rax, r9
mov r15, 1
jmp sub_loop
sub_with_0:
mov rax, 0
sal rax, TYPE_BITS
or rax, T_INTEGER
mov r9, rax
mov r15, 0
sub_loop:
inc r15
cmp r15, r14
je sub_end
mov rbx, r15
add rbx, 3
shl rbx, 3
mov r8, qword [rbp + rbx] ; rbx = (r15 + 3)*8
; r9 = a/b, r8 = c/d
NUMERATOR r9
mov r10, rax ; r10 = a
DENOMINATOR r8
mul r10
mov r10, rax ; r10 = a*d
NUMERATOR r8
mov r11, rax ; r11 = c
DENOMINATOR r9
mul r11
mov r11, rax ; r11 = b*c
DENOMINATOR r8
mov r12, rax
DENOMINATOR r9
mul r12
mov r12, rax ; r12 = b*d = solution denominator
sub r10, r11 ; r10 = solution numerator
cmp r12, 0