-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.patch
More file actions
2493 lines (2446 loc) · 91.1 KB
/
compiler.patch
File metadata and controls
2493 lines (2446 loc) · 91.1 KB
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
diff --git a/code-gen.ml b/code-gen.ml
index 0d1442c..7a19c41 100644
--- a/code-gen.ml
+++ b/code-gen.ml
@@ -1,14 +1,541 @@
#use "semantic-analyser.ml";;
+exception X_not_found_in_fvar;;
+exception X_not_found_in_def;;
+exception X_not_found_in_const;;
+open List;;
+
+let sa s = Semantics.run_semantics (tp s);;
module type CODE_GEN = sig
- val make_consts_tbl : expr' list -> (constant * ('a * string)) list
- val make_fvars_tbl : expr' list -> (string * 'a) list
- val generate : (constant * ('a * string)) list -> (string * 'a) list -> expr' -> string
+ val make_consts_tbl : expr' list -> (constant * (int * string)) list
+ val make_fvars_tbl : expr' list -> (string * int) list
+ val generate : (constant * (int * string)) list -> (string * int) list -> expr' -> string
+ val get_const_idx : constant -> (constant * (int * string)) list -> int
+ val get_fvar_idx : string -> (string * int) list -> int
+ val analyze_string: string -> expr'
end;;
module Code_Gen : CODE_GEN = struct
- let make_consts_tbl asts = raise X_not_yet_implemented;;
- let make_fvars_tbl asts = raise X_not_yet_implemented;;
- let generate consts fvars e = raise X_not_yet_implemented;;
+
+(* end;; *)
+
+(* This function turns a string into an expr' *)
+let analyze_string s = Semantics.run_semantics (tp s);;
+
+(* ****************************************** *)
+let const_idx = ref(0)
+
+let get_const_size c =
+ match c with
+ | Void -> 1
+ | (Sexpr (Bool _)) -> 2
+ | (Sexpr (Nil)) -> 1
+ | (Sexpr (Number _ )) -> 9
+ | (Sexpr (Char _)) -> 2
+ | (Sexpr (Symbol _)) -> 9
+ | (Sexpr (String s)) -> 9 + (String.length s)
+ | (Sexpr (Vector slist)) -> 9 + ((length slist) * 8)
+ | (Sexpr (Pair (_, _))) -> 17
+
+let generate_const_idx c =
+ let x = !const_idx in
+ const_idx := !const_idx + (get_const_size c);
+ x;;
+
+let rec add_to_base_const_tbl ast tbl =
+ match ast with
+ | Const' c -> (match c with
+ | (Sexpr (Symbol s)) -> tbl @ [Sexpr (String s) ; c]
+ | (Sexpr (Pair (a, b))) -> fold_right add_to_base_const_tbl [Const'(Sexpr a);Const'(Sexpr b)] tbl @ [c]
+ | (Sexpr (Vector slist)) -> let slist = map (fun a-> Const'(Sexpr a)) slist in
+ fold_right add_to_base_const_tbl slist tbl @ [c]
+ | _ -> tbl @ [c] )
+ | BoxSet'(v, e) -> add_to_base_const_tbl e tbl
+ | If' (e1, e2, e3) -> fold_right add_to_base_const_tbl [e1; e2; e3] tbl
+ | Seq' slist -> fold_right add_to_base_const_tbl slist tbl
+ | Set' (e1, e2) -> fold_right add_to_base_const_tbl [ e1; e2] tbl
+ | Def' (e1, e2) -> add_to_base_const_tbl e2 tbl
+ | Or' slist -> fold_right add_to_base_const_tbl slist tbl
+ | LambdaSimple' (slist, e) -> let slist = map (fun a-> Const'(Sexpr (String a))) slist in
+ fold_right add_to_base_const_tbl (slist @ [e]) tbl
+ | LambdaOpt' (slist, opt, e) -> let slist = map (fun a-> Const'(Sexpr (String a))) (opt::slist) in
+ fold_right add_to_base_const_tbl (slist @ [e]) tbl
+ | Applic' (op, elist) -> fold_right add_to_base_const_tbl ([op] @ elist) tbl
+ | ApplicTP' (op, elist) -> fold_right add_to_base_const_tbl ([op] @ elist) tbl
+ | _ -> tbl
+
+let make_base_const_list asts = fold_right add_to_base_const_tbl (rev asts) [Void; Sexpr(Nil); Sexpr(Bool false); Sexpr(Bool true)];;
+
+let rec list_const_remove_dup tbl =
+ if (List.length tbl) = 0 then tbl else
+ let c1 = (List.hd tbl) in
+ let (_, parted) = partition (fun c2 -> (expr_eq (Const c1) (Const c2))) tbl in
+ [c1] @ (list_const_remove_dup parted);;
+
+let get_const_idx c1 tbl = try
+ let const = List.find (fun (c2, (_, _)) -> expr_eq (Const c2) (Const c1)) tbl in
+ let (_, (offset, _)) = const in offset
+ with Not_found -> raise X_not_found_in_const
+
+
+let rec make_vector_cmd tbl vec str =
+ if (length vec = 0) then str^" ;" else
+ let hd = (hd vec) in
+ if (length vec = 1) then
+ str ^ "const_tbl+"^string_of_int (get_const_idx (Sexpr hd) tbl)^" ;" else
+ let new_str = str ^ "const_tbl+"^string_of_int (get_const_idx (Sexpr hd) tbl)^", " in
+ make_vector_cmd tbl (tl vec) new_str
+
+(* let replace_special_char c =
+ let cnum = int_of_char c in
+ if cnum < 33 then "',"^cnum^", '"
+ else c *)
+
+let add_to_const_tbl tbl c =
+ let idx = generate_const_idx c in
+ let sidx = string_of_int idx in
+ let remove_special s =
+ let s = String.concat ("\", 10, \"") (String.split_on_char '\n' s) in
+ String.concat ("\", 13, \"") (String.split_on_char '\r' s) in
+ let switch_special c =
+ let ascii = (int_of_char c) in
+ if (ascii < 32) then (string_of_int ascii) else
+ "\""^(String.make 1 c)^"\"" in
+ (* if (c = '\n') then "10" else
+ if (c = '\r') then "13" else
+ if (c = '\t') then "9" else *)
+ match c with
+ | Void -> tbl @ [(c, (idx, "MAKE_VOID ;"^sidx))]
+ | (Sexpr (Bool false)) -> tbl @ [(c, (idx, "MAKE_BOOL(0) ;"^sidx))]
+ | (Sexpr (Bool true)) -> tbl @ [(c, (idx, "MAKE_BOOL(1) ;"^sidx))]
+ | (Sexpr (Nil)) -> tbl @ [(c, (idx, "MAKE_NIL ;"^sidx))]
+ | (Sexpr (Number (Int i) )) -> tbl @ [(c, (idx, "MAKE_LITERAL_INT("^(string_of_int i)^") ;"^sidx))]
+ | (Sexpr (Number (Float f) )) -> tbl @ [(c, (idx, Printf.sprintf "MAKE_LITERAL_FLOAT(%f) ;" f^sidx))]
+ | (Sexpr (Char ch)) -> tbl @ [(c, (idx, "MAKE_LITERAL_CHAR("^switch_special ch^") ;"^sidx))]
+ | (Sexpr (String s)) -> tbl @ [(c, (idx, "MAKE_NEW_LITERAL_STRING \""^remove_special s^"\" ;"^sidx))]
+ | (Sexpr (Symbol s)) -> tbl @ [(c, (idx, "MAKE_LITERAL_SYMBOL(const_tbl+"^string_of_int (get_const_idx (Sexpr (String s)) tbl)^") ;"^sidx))]
+ | (Sexpr (Pair (a, b))) -> tbl @ [(c, (idx, "MAKE_LITERAL_PAIR(const_tbl+"^string_of_int (get_const_idx (Sexpr a) tbl)^", const_tbl+"^string_of_int (get_const_idx (Sexpr b) tbl)^") ;"^sidx))]
+ | (Sexpr (Vector elist)) -> tbl @ [(c, (idx, (make_vector_cmd tbl elist "MAKE_LITERAL_VECTOR "^sidx)))]
+
+ let make_consts_tbl asts =
+ (* [(Sexpr(Number(Int(5)), (5, "ggg"))] *)
+ let dup_base_tbl = make_base_const_list asts in
+ let base_tbl = list_const_remove_dup dup_base_tbl in
+ fold_left add_to_const_tbl [] base_tbl
+
+(* ********************************************************************** *)
+
+let fvar_idx = ref(0)
+
+let generate_nat_idx idx =
+ let x = !idx in
+ idx := !idx + 1;
+ x;;
+
+let rec add_to_base_fvar_tbl ast tbl =
+ match ast with
+ | Var'(VarFree v) -> tbl @ [v]
+ | Box'(VarFree v) -> tbl @ [v]
+ | BoxGet'(VarFree v) -> tbl @ [v]
+ | BoxSet'((VarFree v), e) -> add_to_base_fvar_tbl e (tbl @ [v])
+ | BoxSet'(v, e) -> add_to_base_fvar_tbl e tbl
+ | If' (e1, e2, e3) -> fold_right add_to_base_fvar_tbl [e1; e2; e3] tbl
+ | Seq' slist -> fold_right add_to_base_fvar_tbl slist tbl
+ | Set' (e1, e2) -> fold_right add_to_base_fvar_tbl [ e1; e2] tbl
+ | Def' (e1, e2) -> fold_right add_to_base_fvar_tbl [ e1; e2] tbl
+ | Or' slist -> fold_right add_to_base_fvar_tbl slist tbl
+ | LambdaSimple' (slist, e) -> add_to_base_fvar_tbl e tbl
+ | LambdaOpt' (slist, opt, e) -> add_to_base_fvar_tbl e tbl
+ | Applic' (op, elist) -> fold_right add_to_base_fvar_tbl ([op] @ elist) tbl
+ | ApplicTP' (op, elist) -> fold_right add_to_base_fvar_tbl ([op] @ elist) tbl
+ | _ -> tbl
+
+let make_base_fvar_list asts = fold_right add_to_base_fvar_tbl asts [];;
+
+let rec list_remove_dup tbl =
+ if (List.length tbl) = 0 then tbl else
+ let e1 = (List.hd tbl) in
+ let (_, parted) = partition (fun e2 -> (e1 = e2)) tbl in
+ [e1] @ (list_remove_dup parted);;
+
+let get_fvar_idx f1 tbl = try
+ let fvar = List.find (fun (f2, _) -> (f2) = (f1)) tbl in
+ let (_, offset) = fvar in offset
+ with Not_found -> raise X_not_found_in_fvar
+
+let gets_fvar_idx f1 tbl = try string_of_int (get_fvar_idx f1 tbl)
+ with X_not_found_in_fvar -> raise X_not_found_in_def
+
+let add_to_fvar_tbl var = (var, generate_nat_idx fvar_idx)
+
+let make_fvars_tbl asts =
+ let dup_fbase_tbl = make_base_fvar_list asts in
+ let fbase_tbl = list_remove_dup dup_fbase_tbl in
+ map add_to_fvar_tbl fbase_tbl
+
+(* ****************************************************************************** *)
+
+let if_idx = ref(1)
+let or_idx = ref(1)
+let lam_idx = ref(1)
+let app_idx = ref(1)
+
+let word_size = 8
+
+let inc env_s = (string_of_int ((int_of_string env_s) + 1))
+let repeat_str s n = String.concat "" (Array.to_list (Array.make n s))
+
+let generate_str_idx i = string_of_int (generate_nat_idx i)
+
+ let rec gen_e consts fvars lam_hist e =
+ let (depth, parent_params_num) = lam_hist in
+ "\n" ^
+ (match e with
+ | Const'(c) -> gen_const consts c
+ | Var'(v) -> gen_var consts fvars lam_hist v
+ | Box' (v) -> gen_box consts fvars lam_hist v
+ | BoxGet' (v) -> gen_box_get consts fvars lam_hist v
+ | BoxSet' (v, e) -> gen_box_set consts fvars lam_hist v e
+ | If' (e1, e2, e3) -> (gen_if consts fvars lam_hist e1 e2 e3)
+ | Seq' (elist) ->
+ "; Sequence starts:" ^ (gen_seq consts fvars lam_hist elist) ^ "\n; :Sequence ends"
+ | Set' (e1, e2) -> gen_set consts fvars lam_hist e1 e2
+ | Def' (e1, e2)-> gen_def consts fvars lam_hist e1 e2
+ | Or' (elist) -> (gen_or consts fvars lam_hist elist)
+ | LambdaSimple' (slist, body) -> gen_lamSim consts fvars (depth + 1) parent_params_num slist body
+ | LambdaOpt' (slist, opt_s, body) -> gen_lamOpt consts fvars (depth + 1) parent_params_num slist body
+ | Applic' (op, elist) -> gen_applic consts fvars lam_hist op elist
+ (* TEMPORARILY TREATED AS A NORMAL APPLIC: *)
+ | ApplicTP' (op, elist) -> "; this is an ApplicTP! \n" ^ gen_applic_tp consts fvars lam_hist op elist
+ )
+
+and gen_applic_tp consts fvars lam_hist op elist =
+ let gen = gen_e consts fvars lam_hist in
+ let idx = generate_str_idx app_idx in
+ let argcount = string_of_int (length elist) in
+ let new_frame_size = (length elist) + 4 in
+ let gen_arg arg s = s ^ gen arg ^ "\n push rax" in
+" ApplicTP"^idx^":
+mov r9, 8875
+push r9 ; push the magic param first
+" ^ (fold_right gen_arg elist "") ^ "
+mov r9, "^argcount^"
+push r9 ; This is the number of the parameters
+"^gen op^" ; rax has the operator. We assume it's a lambda.
+add rax, TYPE_SIZE
+push qword[rax] ; = push rax + TYPE_SIZE; pushing the env
+push qword[ rbp + 8 * 1 ] ; push the OLD return address. We won't come back.
+mov rbx, [rbp + 8 * 0] ; rbx = old rbp. The lambda getting it will never know we've been here
+; SHIFT_FRAME "^string_of_int new_frame_size^" ; shift frame uses current rbp & rcx
+
+ shifty"^idx^":
+ mov rcx, [rbp + WORD_SIZE * 3]
+ add rcx, 5
+ mov rsi, rcx
+%assign i 1
+%rep "^string_of_int new_frame_size^"
+ dec rcx
+ mov r8, [rbp-WORD_SIZE*i]
+ mov r9, [rbp+WORD_SIZE*rcx]
+ mov [rbp+WORD_SIZE*rcx], r8
+%assign i i+1
+%endrep
+
+mov rbp, rbx
+shl rsi, 3 ; this is the actual size of the old env. The amount we need to fix
+add rsp, rsi ; clear the new frame and fix the stack
+
+add rax, WORD_SIZE
+jmp [rax] ; = call rax + TYPE_SIZE + WORD_SIZE, jump to the body and never come back. Goodbye!
+; :::end of ApplicTP"^idx
+
+and gen_lamOpt consts fvars extended_environment_depth parent_parameters_number current_params body =
+ let gen_e = gen_e consts fvars (extended_environment_depth, ((length current_params) + 1)) in
+ let idx = generate_str_idx lam_idx in
+ (* creation or extension of environment *)
+ let environment_creation =
+ " ; creation or extension of environment\n"^
+ (if (extended_environment_depth = 0)
+ then
+ ("mov rbx, SOB_NIL_ADDRESS ; no address required, we are in the global environment\n")
+ else
+ (if (extended_environment_depth = 1)
+ then
+ ("MALLOC rbx, 8 ; rbx = address of new env\n"^
+ (if(parent_parameters_number = 0)
+ then
+ ("mov qword [rbx], SOB_NIL_ADDRESS ; empty vector: no arguments on stack\n")
+ else
+ ("MALLOC rdx, "^string_of_int (8 * parent_parameters_number)^" ; rdx = address of new vector\n"^
+ "mov [rbx], rdx"^
+ (params_to_vector parent_parameters_number 0))
+ ))
+ else
+ ("MALLOC rbx, "^(string_of_int (8*extended_environment_depth))^" ; rbx = address of new env \n"^
+ "mov rcx, rbx \n"^
+ "add rcx, 8 ; rcx will move through the new env \n"^
+ "mov rdi, qword[rbp + 8*2] ; rdi = address of old env \n"^
+ (copy_old_env_to_new_env (extended_environment_depth-1) 0)^
+ (if(parent_parameters_number = 0)
+ then
+ ("mov qword [rbx], SOB_NIL_ADDRESS ; empty vector: no arguments on stack\n")
+ else
+ ("MALLOC rdx, "^string_of_int (8 * parent_parameters_number)^" ; rdx = address of new vector \n"^
+ "mov [rbx], rdx \n"^
+ (params_to_vector parent_parameters_number 0))))
+ )) in
+ let closure_creation =
+ "MAKE_CLOSURE(rax, rbx, lambda_body_"^idx^")\n" in
+ let body_creation =
+ "jmp lambda_end_body_"^idx^"
+ lambda_body_"^idx^":
+ push rbp
+ mov rbp, rsp
+
+ mov rcx, qword[rbp + 8*3]
+ mov rbx, "^string_of_int (length current_params)^"
+ cmp rbx, rcx ; how many optional arguments are practically in stack?
+ je no_optionals_parameters"^idx^"
+
+ ; turn the optional parameters to list
+ mov rcx, qword[rbp + 8*3]
+ sub rcx, "^string_of_int (length current_params)^" ; rcx = number of optional parameters
+ dec rcx
+ mov rdx, qword[rbp + 8*(4+"^string_of_int (length current_params)^"+rcx)] ; rdx = last optional parameter
+ mov rbx, rdx ; rbx = our future list
+ MAKE_PAIR(rax, rbx, const_tbl+1) ; make improper list: (rbx, nil)
+ mov rbx, rax
+ params_to_list_loop_"^idx^":
+ dec rcx
+ cmp rcx, -1
+ je end_params_to_list_loop_"^idx^"
+ mov rdx, qword[rbp + 8*(4+"^string_of_int (length current_params)^"+rcx)] ; rdx = an optional parameter
+ MAKE_PAIR(rax, rdx, rbx) ; make pair: (rdx, rbx)
+ mov rbx, rax
+ jmp params_to_list_loop_"^idx^"
+ end_params_to_list_loop_"^idx^":
+ mov qword[rbp + 8*(4+"^string_of_int (length current_params)^")], rax ; replace the first optional parameter with the optional list
+ jmp body_cont"^idx^"
+
+ no_optionals_parameters"^idx^":
+ mov rcx, const_tbl+1
+ mov qword[rbp + 8*(4+"^(string_of_int (length current_params))^")], rcx
+ jmp body_cont"^idx^"
+
+ body_cont"^idx^":
+ "^(gen_e body)^"
+ leave
+ ret
+ lambda_end_body_"^idx^":\n" in
+
+ "LambdaOpt"^idx^":\n"^environment_creation^closure_creation^body_creation^";:::end of LambdaOpt"^idx^":"
+
+
+and gen_box_set consts fvars lam_hist v e =
+ let gen_e = gen_e consts fvars lam_hist in
+ let gen_var = gen_var consts fvars lam_hist in
+ "; Setting a boxed variable:
+ "^gen_e e^"
+ push rax ; the stack has now the value we need
+ "^gen_var v^" ; rax = v's box
+ pop qword[rax] ; Now v's box points the new value
+ mov rax, sob_void
+ "
+
+and gen_box_get consts fvars lam_hist v =
+ "; Getting a boxed variable:
+ "^gen_var consts fvars lam_hist v^"
+ mov rax, qword[rax] ; the value is one pointer inside the box
+ "
+
+and gen_box consts fvars lam_hist v =
+ "; Boxing is needed. We need to replace the var with a pointer to it.
+ MALLOC rbx, 8 ; rbx will hold the pointer. Now we'll get the var to rax:
+ "^gen_var consts fvars lam_hist v^"
+ mov qword[rbx], rax ; now rbx is a pointer to the var!
+ mov rax, rbx\n"
+
+and gen_set consts fvars lam_hist var e =
+ let gen_e = gen_e consts fvars lam_hist in
+ ";; Set!
+ "^ gen_e e ^"; Now let's set var to rax:\n"^
+ (match var with
+ | Var' (VarParam (name, minor)) ->
+" mov qword [rbp + 8 * (4 + "^string_of_int minor^")], rax ; setting param "^name^"
+ mov rax, sob_void"
+ | Var' (VarBound (name, major, minor)) ->
+" mov rbx, qword [rbp + 8 * 2] ; current environment
+ mov rbx, qword [rbx + 8 * "^string_of_int major^"] ; a vector
+ mov qword [rbx + 8 * "^string_of_int minor^"], rax ; a value from rax to bound "^name^"
+ mov rax, sob_void"
+ | Var' (VarFree name) -> let idx = gets_fvar_idx name fvars in
+ gen_e e ^ "
+ mov qword FVAR("^idx^"), rax ; defining "^name^"
+ mov rax, sob_void"
+ | _ -> "who's the fool who's defining a non-var?"
+)
+
+and gen_applic consts fvars lam_hist op elist =
+ let gen = gen_e consts fvars lam_hist in
+ let idx = generate_str_idx app_idx in
+ let argcount = string_of_int (length elist) in
+ let gen_arg arg s = s ^ gen arg ^ "\n push rax" in
+" Applic"^idx^":
+mov r9, 8875
+push r9 ; push the magic param first"
+ ^ (fold_right gen_arg elist "") ^ "
+mov r9, "^argcount^"
+push r9 ; This is the number of the parameters
+"^gen op^" ; That was the operator. We assume it's a lambda.
+add rax, TYPE_SIZE
+push qword[rax] ; = push rax + TYPE_SIZE; pushing the env
+add rax, WORD_SIZE
+call [rax] ; = call rax + TYPE_SIZE + WORD_SIZE, call the body
+
+add rsp, WORD_SIZE ; pop the env
+pop rbx ; rbx has argcount now
+add rbx, 1 ; rbx = argcount + magic
+shl rbx, 3 ; rbx = rbx * 8
+add rsp, rbx ; pop all args + magic, back to normal
+; :::end of Applic"^idx
+
+(* Generation of lambda: *)
+and params_to_vector parent_parameters_number i =
+ if(i < parent_parameters_number)
+ then "
+ mov rsi, qword[rbp + 8*(4+ "^string_of_int i^")]
+ mov [rdx], rsi
+ add rdx, 8
+ "^(params_to_vector parent_parameters_number (i+1))
+ else
+ ""
+
+and copy_old_env_to_new_env depth i =
+ if (i <= depth)
+ then
+ "mov rsi, [rdi + "^(string_of_int (i*8))^"]
+ mov [rcx], rsi
+ add rcx, 8\n"
+ ^(copy_old_env_to_new_env depth (i+1))
+ else
+ ("")
+
+and gen_lamSim consts fvars extended_environment_depth parent_parameters_number current_params body =
+ let gen_e = gen_e consts fvars (extended_environment_depth, (length current_params)) in
+ let idx = generate_str_idx lam_idx in
+ (* creation or extension of environment *)
+ let environment_creation =
+ " ; creation or extension of environment\n"^
+ (if (extended_environment_depth = 0)
+ then
+ ("mov rbx, SOB_NIL_ADDRESS ; no address required, we are in the global environment\n")
+ else
+ (if (extended_environment_depth = 1)
+ then
+ ("MALLOC rbx, 8 ; rbx = address of new env\n"^
+ (if(parent_parameters_number = 0)
+ then
+ ("mov qword [rbx], SOB_NIL_ADDRESS ; empty vector: no arguments on stack\n")
+ else
+ ("MALLOC rdx, "^string_of_int (8 * parent_parameters_number)^" ; rdx = address of new vector\n"^
+ "mov [rbx], rdx"^
+ (params_to_vector parent_parameters_number 0))
+ ))
+ else
+ ("MALLOC rbx, "^(string_of_int (8*extended_environment_depth))^" ; rbx = address of new env \n"^
+ "mov rcx, rbx \n"^
+ "add rcx, 8 ; rcx will move through the new env \n"^
+ "mov rdi, qword[rbp + 8*2] ; rdi = address of old env \n"^
+ (copy_old_env_to_new_env (extended_environment_depth-1) 0)^
+ (if(parent_parameters_number = 0)
+ then
+ ("mov qword [rbx], SOB_NIL_ADDRESS ; empty vector: no arguments on stack\n")
+ else
+ ("MALLOC rdx, "^string_of_int (8 * parent_parameters_number)^" ; rdx = address of new vector \n"^
+ "mov [rbx], rdx \n"^
+ (params_to_vector parent_parameters_number 0))))
+ )) in
+ let closure_creation =
+ "MAKE_CLOSURE(rax, rbx, lambda_body_"^idx^")\n" in
+ let body_creation =
+ "jmp lambda_end_body_"^idx^"
+ lambda_body_"^idx^":
+ push rbp
+ mov rbp, rsp
+ "^(gen_e body)^"
+ aftecode"^idx^":
+ leave
+ afteleave"^idx^":
+ ret
+ lambda_end_body_"^idx^":\n" in
+
+ "Lambda"^idx^":\n"^environment_creation^closure_creation^body_creation^";:::end of Lambda"^idx^":"
+
+
+and gen_def consts fvars lam_hist var e =
+ match var with
+ | Var' (VarFree name) -> (
+ let gen_e = gen_e consts fvars lam_hist in
+ let idx = gets_fvar_idx name fvars in
+ gen_e e ^ "
+ mov qword FVAR("^idx^"), rax ; defining "^name^"
+ mov rax, sob_void")
+ | _ -> "who's the fool who's defining a non-freevar?"
+
+and gen_var consts fvars lam_hist v =
+ (* let gen_e = gen_e consts fvars in *)
+ match v with
+ | VarFree(n) -> let idx = string_of_int (get_fvar_idx n fvars) in
+ "mov rax, FVAR("^idx^") ; Get the freevar "^n
+ | VarParam(name, min) ->
+ " ;; Get_param "^name^":
+ ;Param "^name^":
+ mov r8, "^string_of_int min^"
+ add r8, 4
+ shl r8, 3
+ add r8, rbp
+ mov rax, qword [r8] ;; mov rax, qword [rbp + WORD_SIZE * (4 + "^string_of_int min^")]"
+ | VarBound(name, major, minor) -> let maj, min = string_of_int major, string_of_int minor in
+ " ;Get_bound "^name^":
+ mov rax, qword [rbp + 8 * 2] ; current environment
+ mov rax, qword [rax + 8 * "^maj^"] ; a vector
+ mov rax, qword [rax + 8 * "^min^"] ; a value
+ "
+
+and gen_or consts fvars lam_hist elist =
+ let gen_e = gen_e consts fvars lam_hist in
+ let idx = generate_str_idx or_idx in
+ let lexit = "LexitOr" ^ idx in
+ let rec add_or elist = let e, rest = (hd elist), (tl elist) in
+ (* the first case is unnecessary anyways *)
+ if ((length elist) = 0) then (gen_e (Const'(Sexpr(Bool false)))) else
+ if ((length elist) = 1) then (gen_e e) ^"\n" ^ lexit^":\n" else
+ gen_e (hd elist) ^ "\n cmp rax, sob_false \njne " ^lexit^"\n" ^ (add_or rest)
+ in
+ "; Or"^idx ^ add_or elist ^ "; :::end of Or"^idx
+
+and gen_if consts fvars lam_hist eif etn els =
+ let gen_e = gen_e consts fvars lam_hist in
+ let cif = gen_e eif in
+ let ctn = gen_e etn in
+ let cls = gen_e els in
+ let ifidx = generate_str_idx if_idx in
+ let lelse = "Lelse" ^ ifidx in
+ let lexit = "lexitIf" ^ ifidx in
+ "; If" ^ ifidx ^
+ cif ^ "\ncmp rax, sob_false \nje " ^ lelse ^ ctn ^ "\njmp " ^ lexit ^ "\n" ^ lelse ^ ":\n" ^ cls ^ "\n" ^ lexit ^":\n"
+ ^ "; :::end of If" ^ ifidx
+
+and gen_seq consts fvars lam_hist elist = fold_left (fun s e -> s^"\n"^(gen_e consts fvars lam_hist e)) "" elist
+
+and gen_const consts c = let idx = get_const_idx c consts in
+ "mov rax, const_tbl+"^(string_of_int idx)
+
+ let generate consts fvars e = gen_e consts fvars (-1, -1) e
+ (* This function takes the constants table, the free-variables table and a single exprג€™ and
+returns a string containing the assembly code representing the evaluation of the exprג€™. *)
end;;
+(* ****************************************************************************************** *)
+(* ****************************** ************************************ *)
+(* ****************************************************************************************** *)
diff --git a/compiler.ml b/compiler.ml
index e724266..f05d7c6 100644
--- a/compiler.ml
+++ b/compiler.ml
@@ -10,6 +10,8 @@ let string_to_asts s = List.map Semantics.run_semantics
(Tag_Parser.tag_parse_expressions
(Reader.read_sexprs s));;
+(* a mapping from free-variable names of primitive library procedures
+ to their corresponding assembly code labels *)
let primitive_names_to_labels =
["boolean?", "is_boolean"; "float?", "is_float"; "integer?", "is_integer"; "pair?", "is_pair";
"null?", "is_null"; "char?", "is_char"; "vector?", "is_vector"; "string?", "is_string";
@@ -18,15 +20,22 @@ let primitive_names_to_labels =
"vector-length", "vector_length"; "vector-ref", "vector_ref"; "vector-set!", "vector_set";
"make-vector", "make_vector"; "symbol->string", "symbol_to_string";
"char->integer", "char_to_integer"; "integer->char", "integer_to_char"; "eq?", "is_eq";
- "+", "bin_add"; "*", "bin_mul"; "-", "bin_sub"; "/", "bin_div"; "<", "bin_lt"; "=", "bin_equ"
+ "+", "bin_add"; "*", "bin_mul"; "binsub", "bin_sub"; "/", "bin_div"; "<", "bin_lt"; "=", "bin_equ";
+
+ "boolean?", "is_boolean"; "car", "car_prim"; "cdr", "cdr_prim"; "cons", "cons_prim"; "set-car!",
+ "set_car_prim"; "set-cdr!", "set_cdr_prim"; "apply", "apply_prim"
(* you can add yours here *)];;
let make_prologue consts_tbl fvars_tbl =
- let get_const_address const = raise X_not_yet_implemented in
- let get_fvar_address const = raise X_not_yet_implemented in
+ let get_const_address const = Code_Gen.get_const_idx const consts_tbl in
+ (* This function takes a single constant and returns a string representing its absolute address. *)
+ let get_fvar_address fvar = Code_Gen.get_fvar_idx fvar fvars_tbl in
+ (* This function takes a single free-variable name and returns a string representing its absolute address. *)
let make_primitive_closure (prim, label) =
+ (* a mapping from free-variable names of primitive library procedures to their corresponding assembly code labels *)
" MAKE_CLOSURE(rax, SOB_NIL_ADDRESS, " ^ label ^ ")
- mov [" ^ (get_fvar_address prim) ^ "], rax" in
+ mov qword FVAR("^string_of_int(get_fvar_address prim)^"), rax"
+ in
let make_constant (c, (a, s)) = s in
"
@@ -44,16 +53,20 @@ const_tbl:
;;; These macro definitions are required for the primitive
;;; definitions in the epilogue to work properly
-%define SOB_VOID_ADDRESS " ^ get_const_address Void ^ "
-%define SOB_NIL_ADDRESS " ^ get_const_address (Sexpr Nil) ^ "
-%define SOB_FALSE_ADDRESS " ^ get_const_address (Sexpr (Bool true)) ^ "
-%define SOB_TRUE_ADDRESS " ^ get_const_address (Sexpr (Bool false)) ^ "
+%define SOB_VOID_ADDRESS const_tbl+" ^ string_of_int (get_const_address Void) ^ "
+%define SOB_NIL_ADDRESS const_tbl+" ^ string_of_int (get_const_address (Sexpr Nil)) ^ "
+%define SOB_FALSE_ADDRESS const_tbl+" ^ string_of_int (get_const_address (Sexpr (Bool false))) ^ "
+%define SOB_TRUE_ADDRESS const_tbl+" ^ string_of_int (get_const_address (Sexpr (Bool true))) ^ "
+%define sob_void SOB_VOID_ADDRESS
+%define sob_nil SOB_NIL_ADDRESS
+%define sob_false SOB_FALSE_ADDRESS
+%define sob_true SOB_TRUE_ADDRESS
fvar_tbl:
" ^ (String.concat "\n" (List.map (fun _ -> "dq T_UNDEFINED") fvars_tbl)) ^ "
-global main
section .text
+global main
main:
;; set up the heap
mov rdi, GB(4)
@@ -62,18 +75,20 @@ main:
;; Set up the dummy activation frame
;; The dummy return address is T_UNDEFINED
- ;; (which a is a macro for 0) so that returning
+ ;; (which a is a macro for 5578) so that returning
;; from the top level (which SHOULD NOT HAPPEN
;; AND IS A BUG) will cause a segfault.
push 0
push qword SOB_NIL_ADDRESS
push qword T_UNDEFINED
push rsp
+ mov rbp, rsp
call code_fragment
add rsp, 4*8
ret
+
code_fragment:
;; Set up the primitive stdlib fvars:
;; Since the primtive procedures are defined in assembly,
@@ -81,10 +96,13 @@ code_fragment:
;; This is where we emulate the missing (define ...) expressions
;; for all the primitive procedures.
" ^ (String.concat "\n" (List.map make_primitive_closure primitive_names_to_labels)) ^ "
-
+
+actual_code:
";;
-let epilogue = raise X_not_yet_implemented;;
+let epilogue = ";; That's all, folks!";;
+let prim_start =
+"ret \n\n;***************************************\n ;; ********* Prim Functions: ********** \n;***************************************\n\n"
exception X_missing_input_file;;
@@ -93,16 +111,17 @@ try
let code = (file_to_string "stdlib.scm") ^ (file_to_string infile) in
let asts = string_to_asts code in
let consts_tbl = Code_Gen.make_consts_tbl asts in
- let fvars_tbl = Code_Gen.make_fvars_tbl asts in
+ (* NOTE: I "contaminate" the asts here with the names of all the primitive functions *)
+ let fvars_tbl = Code_Gen.make_fvars_tbl (asts @ (map (fun (a, b)-> (Code_Gen.analyze_string a)) primitive_names_to_labels)) in
let generate = Code_Gen.generate consts_tbl fvars_tbl in
- let code_fragment = String.concat "\n\n"
+ let code_fragment = (String.concat "\n\n"
(List.map
- (fun ast -> (generate ast) ^ "\n call write_sob_if_not_void")
- asts) in
+ (fun ast -> (generate ast) ^ "\n;;before_print:\n call write_sob_if_not_void \n")
+ asts)) ^ prim_start in
let provided_primitives = file_to_string "prims.s" in
print_string ((make_prologue consts_tbl fvars_tbl) ^
code_fragment ^
provided_primitives ^ "\n" ^ epilogue)
-
+
with Invalid_argument(x) -> raise X_missing_input_file;;
diff --git a/compiler.s b/compiler.s
index 5c418e8..ba9de1f 100644
--- a/compiler.s
+++ b/compiler.s
@@ -1,4 +1,4 @@
-%define T_UNDEFINED 0
+%define T_UNDEFINED 5578
%define T_VOID 1
%define T_NIL 2
%define T_INTEGER 3
@@ -63,7 +63,8 @@
%define CLOSURE_CODE CDR
%define PVAR(n) qword [rbp+(4+n)*WORD_SIZE]
-
+%define FVAR(i) [fvar_tbl+i*WORD_SIZE]
+
%define SOB_UNDEFINED T_UNDEFINED
%define SOB_NIL T_NIL
%define SOB_VOID T_VOID
@@ -79,6 +80,12 @@
sub %1, [rsp]
add rsp, 8
%endmacro
+
+%macro MAKE_LITERAL 2
+; Make a literal of type %1 ; followed by the definition %2
+ db %1
+ %2
+%endmacro
; Creates a short SOB with the
; value %2
@@ -102,6 +109,13 @@
%define MAKE_FLOAT(r,val) MAKE_LONG_VALUE r, val, T_FLOAT
%define MAKE_CHAR(r,val) MAKE_CHAR_VALUE r, val
+%define MAKE_NIL db T_NIL
+%define MAKE_VOID db T_VOID
+%define MAKE_BOOL(val) MAKE_LITERAL T_BOOL, db val
+%define MAKE_LITERAL_INT(val) MAKE_LITERAL T_INTEGER, dq val
+%define MAKE_LITERAL_CHAR(val) MAKE_LITERAL T_CHAR, db val
+%define MAKE_LITERAL_FLOAT(val) MAKE_LITERAL T_FLOAT, dq val
+
; Create a string of length %2
; from char %3.
; Stores result in register %1
@@ -124,6 +138,30 @@
sub %1, WORD_SIZE+TYPE_SIZE
%endmacro
+%macro MAKE_LITERAL_STRING 1
+ db T_STRING
+ dq (%%end_str - %%str)
+%%str:
+ db %1
+%%end_str:
+%endmacro
+
+%macro MAKE_NEW_LITERAL_STRING 0-*
+ db T_STRING
+ dq (%%end_str - %%str)
+%%str:
+%rep %0
+ db %1
+%rotate 1
+%endrep
+%%end_str:
+%endmacro
+
+%macro MAKE_LITERAL_SYMBOL 1
+ db T_SYMBOL
+ dq %1
+%endmacro
+
; Create a vector of length %2
; from SOB at %3.
; Stores result in register %1
@@ -146,14 +184,23 @@
pop rcx
%endmacro
+%macro MAKE_LITERAL_VECTOR 0-*
+ db T_VECTOR
+ dq %0
+%rep %0
+ dq %1
+%rotate 1
+%endrep
+%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_BYTES*2
+ MALLOC %1, TYPE_SIZE+WORD_SIZE*2
mov byte [%1], %2
mov qword [%1+TYPE_SIZE], %3
- mov qword [%1+TYPE_SIZE+WORD_BYTES], %4
+ mov qword [%1+TYPE_SIZE+WORD_SIZE], %4
%endmacro
%macro MAKE_WORDS_LIT 3
@@ -171,6 +218,37 @@
%define MAKE_CLOSURE(r, env, body) \
MAKE_TWO_WORDS r, T_CLOSURE, env, body
+%define FST_PARAM qword [rbp + WORD_SIZE * 4]
+%define PARAM_COUNT qword [rbp + WORD_SIZE * 3]
+%define LEX_ENV qword [rbp + WORD_SIZE * 2]
+
+%macro SHIFT_FRAME 1
+; %1 = size of frame (constant)
+ mov rcx, [rbp + WORD_SIZE * 3]
+ add rcx, 5
+%assign i 1
+%rep %1
+ dec rcx
+ mov r8, [rbp-WORD_SIZE*i]
+ mov [rbp+WORD_SIZE*rcx], r8
+%assign i i+1
+%endrep
+%endmacro
+
+;; Shifts the current frame over the previous one.
+;; assumes no one uses rcx currently (used to get size of new frame)
+;; %1 = size of frame (constant)
+%macro SHIFT_FRAMEe 1
+ mov rcx, qword [rbp + WORD_SIZE * 2]
+ add rcx, 5
+%assign i 1
+%rep %1
+ dec rcx
+ mov r8, [rbp - WORD_SIZE * i]
+ mov[rbp + WORD_SIZE*rcx], r8
+ %assign i i+1
+%endrep
+%endmacro
extern exit, printf, malloc
global write_sob, write_sob_if_not_void
@@ -728,4 +806,4 @@ write_sob_if_not_void:
ret
section .data
.newline:
- db CHAR_NEWLINE, 0
+ db CHAR_NEWLINE, 0
\ No newline at end of file
diff --git a/prims.s b/prims.s
index bd9d118..57ccab3 100644
--- a/prims.s
+++ b/prims.s
@@ -894,3 +894,182 @@ bin_equ:
leave
ret
+;; *************************************************************************************** ;;
+
+car_prim:
+ push rbp
+ mov rbp, rsp
+
+ mov rsi, PVAR(0)
+ add rsi, TYPE_SIZE
+ mov rax, [rsi]
+
+ leave
+ ret
+
+cdr_prim:
+ push rbp
+ mov rbp, rsp
+
+ mov rsi, PVAR(0)
+ add rsi, TYPE_SIZE
+ add rsi, WORD_SIZE
+ mov rax, [rsi]
+
+ leave
+ ret
+
+cons_prim:
+ push rbp
+ mov rbp, rsp
+
+ mov rsi, PVAR(0)
+ mov rdi, PVAR(1)
+ MAKE_PAIR(rax, rsi, rdi)
+
+ leave
+ ret
+
+set_car_prim:
+ push rbp
+ mov rbp, rsp
+
+ mov rsi, PVAR(0)
+ add rsi, TYPE_SIZE
+ mov rdi, PVAR(1)
+ mov [rsi], rdi
+ mov rax, SOB_VOID_ADDRESS
+
+ leave
+ ret
+
+set_cdr_prim:
+ push rbp
+ mov rbp, rsp
+
+ mov rsi, PVAR(0)
+ add rsi, TYPE_SIZE
+ add rsi, WORD_SIZE
+ mov rdi, PVAR(1)
+ mov [rsi], rdi
+ mov rax, SOB_VOID_ADDRESS
+
+ leave
+ ret
+
+apply_prim:
+ mov rbx, [rsp + (8 * 3)] ; rbx = proc
+ mov rcx, [rsp + (8 * 2)] ; rcx = # of params
+ dec rcx ; rcx = # of params without the proc (but including the list)
+ mov rdx, [rbx + TYPE_SIZE] ; rdx = environment of the proc
+ mov rdi, [rsp + (8 * 0)] ; rdx = return address
+
+ mov rsi, rcx
+ add rsi, 3
+ shl rsi, 3
+ mov rsi, [rsp + rsi]
+ ; mov rsi, [rbp - (8 * 3)] ; rsi = last argument
+ cmp byte[rsi], T_PAIR ; if last argument is a list
+ je apply_handlelist
+
+ ; not a list:
+ cmp byte[rsi], T_NIL ; if last argument is null, it's a special case!
+ je apply_handle_nil
+
+ mov qword [rsp + (8 * 3)], rcx ; # of params = previous # - 1 (of the proc)
+ mov qword [rsp + (8 * 2)], rdx ; move environment in stack
+ mov qword [rsp + (8 * 1)], rdi ; move return address in stack
+ add rsp, 8 ; fix stack
+ jmp apply_lambda
+
+ apply_handle_nil:
+ cmp rcx, 1
+ je apply_no_args ; if we have nothing but an empty list, it's another special case
+ dec rcx ; even the list doesn't count anymore. rcx = true # of args
+ ; ignore that nil. shift stack one down over it and over the proc
+ mov r15, rsp
+ add r15, 16 ; r15 = head of the new stack head
+ mov r10, 0 ; r10 = index
+ apply_shift_argsinstack_nil:
+ cmp r10, rcx
+ je end_apply_shift_argsinstack_nil
+ mov r11, [rsp + (8 * (4 + r10))] ; r11 = parameter
+ mov [r15 + (8 * (3 + r10))], r11 ; put the parameter in its new place in stack
+ inc r10
+ jmp apply_shift_argsinstack_nil
+ end_apply_shift_argsinstack_nil:
+ mov [r15 + (8 * 2)], rcx ; move new # of params in stack
+ mov [r15 + (8 * 1)], rdx ; move environment in stack
+ mov [r15 + (8 * 0)], rdi ; move return address in stack
+ mov rsp, r15
+ jmp apply_lambda
+
+ apply_handlelist:
+ mov r8, 1
+ mov r12, [rsi + TYPE_SIZE + WORD_SIZE] ; r12 = cdr
+ apply_loop_pairlength:
+ cmp byte[r12], T_NIL
+ je end_apply_loop_pairlength
+ inc r8
+ inc rcx ; update rcx with the newly found argument!
+ mov r12, [r12 + TYPE_SIZE + WORD_SIZE] ; r12 = cddr
+ jmp apply_loop_pairlength
+ end_apply_loop_pairlength: ; now r8 = length of arglist
+
+ ; make room for the new params. shift (r8 - 1) (we don't care of the old list)
+ mov r15, rsp
+ mov r14, r8
+ sub r14, 2
+ shl r14, 3
+ sub r15, r14 ; r15 = head of the new stack head
+ mov [r15 + (8 * 0)], rdi ; move return address in stack
+ mov [r15 + (8 * 1)], rdx ; move environment in stack
+ mov [r15 + (8 * 2)], rcx ; move new # of params in stack
+ mov r9, rcx
+ sub r9, r8 ; r9 = # of params not in list
+ mov r10, 0 ; r10 = index
+ apply_shift_argsinstack:
+ cmp r10, r9
+ je end_apply_shift_argsinstack
+ mov r11, [rsp + (8 * (4 + r10))] ; r11 = parameter
+ mov [r15 + (8 * (3 + r10))], r11 ; put the parameter to its new place in stack
+ inc r10
+ jmp apply_shift_argsinstack
+ end_apply_shift_argsinstack:
+ add rsi, TYPE_SIZE ; rsi = address of car
+ mov r12, rsi
+ add r12, WORD_SIZE ; r12 = address of cdr (pair)
+ apply_loop_copylist:
+ cmp r8, 1
+ je end_apply_loop_copylist
+ mov r11, [rsi] ; r11 = car
+ mov [r15 + (8 * (3 + r10))], r11
+ mov r12, [r12] ; r12 = pair
+ mov rsi, r12
+ add rsi, TYPE_SIZE ; rsi = address of car
+ add r12, TYPE_SIZE
+ add r12, WORD_SIZE ; r12 = address of pair
+ inc r10
+ dec r8
+ jmp apply_loop_copylist
+
+ end_apply_loop_copylist:
+ mov r11, [rsi]
+ mov [r15 + (8 * (3 + r10))], r11
+ mov rsp, r15
+ jmp apply_lambda