-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompiler.patch
More file actions
2459 lines (2370 loc) · 97 KB
/
compiler.patch
File metadata and controls
2459 lines (2370 loc) · 97 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..4fcc1fe 100644
--- a/code-gen.ml
+++ b/code-gen.ml
@@ -1,14 +1,590 @@
#use "semantic-analyser.ml";;
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 * string)) list
+ val generate : (constant * (int * string)) list -> (string * (int * string)) list -> expr' -> string
+ val get_const_address: constant -> (constant * (int * 'a)) list -> string
+ val get_free_var_index: string -> (string * (int * string)) list -> int
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;;
+ let label_counter = ref 0;;
+
+ let primitives = ["boolean?"; "float?"; "integer?"; "pair?"; "null?"; "char?"; "vector?"; "string?"; "procedure?"; "symbol?"; "string-length"; "string-ref";
+ "string-set!"; "make-string"; "vector-length"; "vector-ref"; "vector-set!"; "make-vector"; "symbol->string"; "char->integer"; "integer->char"; "eq?"; "+"; "*"; "-"; "/"; "<"; "=";
+ "car"; "cdr"; "cons"; "set-car!"; "set-cdr!"; "apply"]
+
+ let magic = "push 0xffffffffffffffff\n"
+
+ let int_of_bool b =
+ match b with
+ | false -> 0
+ | true -> 1;;
+
+ (* Compare constants - can be Void os sexpr *)
+ let const_eq c1 c2 =
+ match c1 with
+ | Void -> if (c2 = Void)
+ then true
+ else false
+ | Sexpr(x) -> (match c2 with
+ | Void -> false
+ | Sexpr(y) -> sexpr_eq x y);;
+
+(* Chapter 6 page 32.1 *)
+(* Build the first const table, consists of constants and had duplicates *)
+let rec naive_const_table lin lout =
+ match lin with
+ | [] -> lout
+ | hd::tl -> (let res = match hd with
+ | Const'(c) -> lout@[c]
+ | BoxSet'(v, e) -> naive_const_table [e] lout
+ | If'(test, caseT, caseF) -> naive_const_table [test; caseT; caseF] lout
+ | Seq'(l) -> naive_const_table l lout
+ | Set'(e_var, e_val) -> naive_const_table [e_var; e_val] lout
+ | Def'(e_var, e_val) -> naive_const_table [e_var; e_val] lout
+ | Or'(l) -> naive_const_table l lout
+ | LambdaSimple'(params, body) -> naive_const_table [body] lout
+ | LambdaOpt'(params, opt, body) -> naive_const_table [body] lout
+ | Applic'(proc, args) -> (naive_const_table ([proc]@args) lout)
+ | ApplicTP'(proc, args) -> (naive_const_table ([proc]@args) lout)
+ | _ -> lout
+ in naive_const_table tl res);;
+
+(* Chapter 6 page 32.3 *)
+(* Handle compound constants *)
+let rec open_complex l =
+ match l with
+ | [] -> []
+ | hd::tl -> (match hd with
+ | Sexpr(Symbol(s)) -> [Sexpr(String(s))]@[hd]@(open_complex tl)
+ | Sexpr(Pair(car, cdr)) -> (open_complex [Sexpr(car); Sexpr(cdr)])@[hd]@(open_complex tl)
+ | Sexpr(Vector(v)) -> (open_complex (List.map (fun s -> Sexpr(s)) v))@[hd]@(open_complex tl)
+ | _ -> [hd]@(open_complex tl))
+
+let rec sexpr_mem member l =
+ match l with
+ | [] -> false
+ | hd::tl -> if (const_eq member hd)
+ then true
+ else sexpr_mem member tl;;
+
+let rec remove_dups l ans =
+ match l with
+ | [] -> ans
+ | hd::tl -> if (sexpr_mem hd ans)
+ then remove_dups tl ans
+ else remove_dups tl (ans@[hd]) ;;
+
+ let rec find_offset c table =
+ match table with
+ | [] -> raise X_this_should_not_happen
+ | hd::tl -> let (const, (offset, str)) = hd in
+ if (const_eq const c)
+ then offset
+ else find_offset c tl;;
+
+ let get_const_address c table = "const_tbl+" ^ (string_of_int (find_offset c table));;
+
+ let rec make_const_table constants_list (position : int) table =
+ match constants_list with
+ | [] -> table
+ | hd::tl -> (match hd with
+ | Void -> make_const_table tl (position + 1) (table@[(Void, (position, "MAKE_VOID"))])
+ | Sexpr(Bool(b)) -> make_const_table tl (position + 2) (table@[(Sexpr(Bool(b)), (position, "MAKE_BOOL(" ^ (string_of_int (int_of_bool b)) ^ ")"))])
+ | Sexpr(Nil) -> make_const_table tl (position + 1) (table@[(Sexpr(Nil), (position, "MAKE_NIL"))])
+ | Sexpr(Number(Int(i))) -> make_const_table tl (position + 9) (table@[(Sexpr(Number(Int(i))), (position, "MAKE_LITERAL_INT(" ^ (string_of_int i) ^ ")"))])
+ | Sexpr(Number(Float(f))) -> make_const_table tl (position + 9) (table@[(Sexpr(Number(Float(f))), (position, "MAKE_LITERAL_FLOAT(" ^ (string_of_float f) ^ ")"))])
+ | Sexpr(Char(ch)) -> make_const_table tl (position + 2) (table@[(Sexpr(Char(ch)), (position, "MAKE_LITERAL_CHAR(" ^ string_of_int (int_of_char ch) ^ ")" ))])
+ | Sexpr(String(s)) -> make_const_table tl (position + 9 + (String.length s)) (table@[(Sexpr(String(s)), (position, "MAKE_LITERAL_STRING \"" ^ s ^ "\""))])
+ | Sexpr(Symbol(s)) -> make_const_table tl (position + 9) (table@[(Sexpr(Symbol(s)), (position, "MAKE_LITERAL_SYMBOL(const_tbl+" ^ (string_of_int (find_offset (Sexpr(String(s))) table)) ^ ")"))])
+ | Sexpr(Pair(s1, s2)) -> make_const_table tl (position + 17) (table@[(Sexpr(Pair(s1, s2)), (position, "MAKE_LITERAL_PAIR (const_tbl+" ^ (string_of_int (find_offset (Sexpr(s1)) table)) ^ ", const_tbl+" ^ (string_of_int (find_offset (Sexpr(s2)) table)) ^ ")"))])
+ | Sexpr(Vector(l)) -> let (vectorstring, vectoroffset) = make_vector_string l table in
+ make_const_table tl (position+vectoroffset) (table@[(Sexpr(Vector(l)), (position, vectorstring))]))
+ and make_vector_string v table =
+ let length_v = List.length v in
+ if (length_v = 0)
+ then ("MAKE_LITERAL_VECTOR", 1)
+ else(
+ let hd = "const_tbl+" ^ (string_of_int (find_offset (Sexpr(List.hd v)) table)) ^ " " in
+ let tl = List.tl v in
+ let offset_list = List.fold_left (fun acc curr -> acc ^ ", const_tbl+" ^ (string_of_int (find_offset (Sexpr(curr)) table))) "" tl in
+ ("MAKE_LITERAL_VECTOR " ^ hd ^ offset_list ^ "", (9+8*length_v)));;
+
+let wrapper_make_const_table t = make_const_table t 0 [];;
+
+ (* Chapter 6 page 32.2 + 32.4 *)
+let final_table l = remove_dups (open_complex (remove_dups (naive_const_table l []) [])) [Void; Sexpr(Nil); Sexpr(Bool(false)); Sexpr(Bool(true))];;
+
+ let rec naive_free_var_table lin lout =
+ match lin with
+ | [] -> lout
+ | hd::tl -> (let res = match hd with
+ | Var'(VarFree(fv)) -> lout@[fv]
+ | Box'(v) -> naive_free_var_table [Var'(v)] lout
+ | BoxGet'(v) -> naive_free_var_table [Var'(v)] lout
+ | BoxSet'(v, e) -> naive_free_var_table [Var'(v); e] lout
+ | If'(test, caseT, caseF) -> naive_free_var_table [test; caseT; caseF] lout
+ | Seq'(l) -> naive_free_var_table l lout
+ | Set'(e_var, e_val) -> naive_free_var_table [e_var; e_val] lout
+ | Def'(e_var, e_val) -> naive_free_var_table [e_var; e_val] lout
+ | Or'(l) -> naive_free_var_table l lout
+ | LambdaSimple'(params, body) -> naive_free_var_table [body] lout
+ | LambdaOpt'(params, opt, body) -> naive_free_var_table [body] lout
+ | Applic'(proc, args) -> naive_free_var_table ([proc]@args) lout
+ | ApplicTP'(proc, args) -> naive_free_var_table ([proc]@args) lout
+ | _ -> lout in
+ naive_free_var_table tl res)
+
+ let rec remove_dups_strings l ans =
+ match l with
+ | [] -> ans
+ | hd::tl -> if (List.mem hd ans)
+ then remove_dups_strings tl ans
+ else remove_dups_strings tl (ans@[hd]) ;;
+
+ let rec make_free_val_table freeValList index table =
+ match freeValList with
+ | [] -> table
+ | hd::tl -> (make_free_val_table tl (index+1) (table@[(hd, (index, "MAKE_UNDEF"))]));;
+
+let rec get_free_var_index v_name table =
+ match table with
+ | [] -> raise X_this_should_not_happen
+ | hd::tl -> let (name, ((index: int), something)) = hd in
+ if (name = v_name)
+ then index
+ else get_free_var_index v_name tl;;
+
+let rec semi_generate const_table fv_table e index =
+ match e with
+ | Const'(c) -> make_const const_table c
+ | Var'(v) -> make_get_var const_table fv_table v index
+ | Box'(v) -> make_box const_table fv_table v index
+ | BoxGet'(v) -> make_box_get const_table fv_table v index
+ | BoxSet'(v, exp) -> make_box_set const_table fv_table v exp index
+ | If'(test, caseT, caseF) -> make_if const_table fv_table test caseT caseF index
+ | Seq'(l) -> make_seq const_table fv_table l index
+ | Set'(e_var, e_val) -> make_set const_table fv_table e_var e_val index
+ | Def'(e_var, e_val) -> make_def const_table fv_table e_var e_val index
+ | Or'(l) -> make_or const_table fv_table l index
+ | LambdaSimple'(params, body) -> make_simple_lambda const_table fv_table params body (index+1)
+ | LambdaOpt'(params, opt, body) -> make_opt_lambda const_table fv_table params opt body (index+1)
+ | Applic'(proc, args) -> make_applic const_table fv_table proc args index
+ | ApplicTP'(proc, args) -> make_applicTP const_table fv_table proc args index
+
+ and make_const const_table c =
+ ";MAKE_CONST \n" ^" mov rax, " ^ (get_const_address c const_table) ^ "\n"
+
+ and make_get_var const_table fv_table v index =(*let comment = ";MAKE_GET_VAR\n" in*)
+ match v with
+ | VarFree(name) -> "mov rax, qword FVAR(" ^ (string_of_int (get_free_var_index name fv_table)) ^ ")\n "
+ | VarParam(name, minor) -> "mov rax, qword [rbp + " ^ (string_of_int (8*(4+minor))) ^ "]\n"
+ (* Return here and check it's correct *)
+ | VarBound(name, major, minor) ->
+ "mov rax, qword [rbp + 16]
+ mov rax, qword [rax + " ^ (string_of_int (8*major)) ^ "]
+ mov rax, qword [rax + " ^ (string_of_int (8*minor)) ^ "]\n"
+
+ and make_box const_table fv_table v index =
+ let generated_var = semi_generate const_table fv_table (Var'(v)) index in
+ "MALLOC r15, 8 \n" ^ generated_var ^
+ "mov qword [r15], rax \n
+ mov rax, r15 \n"
+
+ and make_box_get const_table fv_table v index =
+ let generated_var = semi_generate const_table fv_table (Var'(v)) index in
+ generated_var ^ "mov rax, qword [rax] \n"
+
+ and make_box_set const_table fv_table v exp index =
+ let generated_exp = semi_generate const_table fv_table exp index in
+ let generated_var = semi_generate const_table fv_table (Var'(v)) index in
+ generated_exp ^ "push rax \n" ^ generated_var ^ "pop qword [rax] \n
+ mov rax, SOB_VOID_ADDRESS \n"
+
+ and make_if const_table fv_table test caseT caseF index =
+ let () = (label_counter := !label_counter+1) in
+ let current_label_counter = !label_counter in
+ ";MAKE_IF\n" ^ (semi_generate const_table fv_table test index) ^ "cmp rax, SOB_FALSE_ADDRESS \n je Lelse" ^ (string_of_int current_label_counter) ^ " \n" ^ (semi_generate const_table fv_table caseT index) ^ "jmp Lexit" ^ (string_of_int current_label_counter) ^ " \n Lelse" ^ (string_of_int current_label_counter) ^ ":" ^ (semi_generate const_table fv_table caseF index) ^ "Lexit" ^ (string_of_int current_label_counter) ^ ":\n"
+
+ and make_seq const_table fv_table l index = ";MAKE_SEQ\n" ^ (List.fold_left (fun acc curr -> acc ^ (semi_generate const_table fv_table curr index)) "" l)
+
+ and make_set const_table fv_table e_var e_val index = (*let comment = ";MAKE_SET\n" in*)
+ match e_var with
+ | Var'(VarFree(name)) -> (semi_generate const_table fv_table e_val index ) ^ "mov qword FVAR(" ^ (string_of_int (get_free_var_index name fv_table)) ^ "), rax \n mov rax, SOB_VOID_ADDRESS \n"
+ | Var'(VarParam(name, minor)) -> (semi_generate const_table fv_table e_val index ) ^ "mov qword [rbp + " ^ (string_of_int (8*(4+minor))) ^ "], rax \n mov rax, SOB_VOID_ADDRESS \n"
+ | Var'(VarBound(name, major, minor)) -> (semi_generate const_table fv_table e_val index ) ^
+ "mov rbx, qword [rbp + 16]
+ mov rbx, qword [rbx + " ^ (string_of_int (8*major)) ^ "]
+ mov qword [rbx + " ^ (string_of_int (8*minor)) ^ "], rax
+ mov rax, SOB_VOID_ADDRESS \n"
+ | _ -> raise X_syntax_error
+
+ and make_def const_table fv_table e_var e_val index = let comment = ";MAKE_DEF \n" in
+ comment ^ (make_set const_table fv_table e_var e_val index)
+
+ and make_or const_table fv_table l index =
+ let () = (label_counter := !label_counter+1) in
+ let current_label_counter = !label_counter in
+ ";MAKE_OR\n" ^ (List.fold_left (fun acc curr -> acc ^ (semi_generate const_table fv_table curr index) ^ " cmp rax, SOB_FALSE_ADDRESS \n jne Lexit" ^ (string_of_int current_label_counter) ^ " \n") "" l) ^ "Lexit" ^ (string_of_int current_label_counter) ^ ":\n"
+
+ and make_simple_lambda const_table fv_table params body index =
+ if (index = 1)
+ then make_simple_lambda_base const_table fv_table params body index
+ else make_simple_lambda_not_base const_table fv_table params body index
+
+ (* Create an empty env *)
+ and make_simple_lambda_base const_table fv_table params body index =
+ let () = (label_counter := !label_counter+1) in
+ let current_label_counter = !label_counter in
+ let comment = Printf.sprintf ";MAKE_LAMBDA_SIMPLE_BASE%i \n" index in
+ let code = Printf.sprintf
+ "MAKE_CLOSURE(rax, SOB_NIL_ADDRESS, CLOSURELABEL%i)
+ jmp ENDOFMAKECLOSURE%i
+
+ CLOSURELABEL%i:
+ push rbp
+ mov rbp,rsp
+ %s
+ leave
+ ret
+
+ ENDOFMAKECLOSURE%i:
+ "current_label_counter current_label_counter current_label_counter (semi_generate const_table fv_table body index) current_label_counter in
+ comment ^ code
+
+ (* We extend at least one env *)
+ and make_simple_lambda_not_base const_table fv_table params body index =
+ let () = (label_counter := !label_counter+1) in
+ let current_label_counter = !label_counter in
+ let comment = Printf.sprintf ";MAKE_LAMBDA_SIMPLE_NOT_BASE%i \n" index in
+ let allocating_commands = "MALLOC rax, "^(string_of_int (8*(index-1))) ^ "; We allocate a pointer to the extended env of size |env+1| \n" in
+ let code = Printf.sprintf " mov rcx, %i
+ mov r10, rax ; r10 now holds the pointer to the start of the env
+ add rax, 8 ; we increment the poitner from malloc to skip env[0]
+ mov r8, qword [rbp+16] ;r8 contains the start of the last env
+ cmp rcx, 1
+ je skip_env_loop%i
+ start_of_env_loop%i:
+ mov r9, qword [r8] ;we use r9 to dereference.
+ mov qword [rax], r9
+ add rax, 8 ;we increment the pointer to env[i]
+ add r8, 8 ; we increment the pointer to env[j]
+
+ loop start_of_env_loop%i
+
+ skip_env_loop%i:
+ push r10
+
+ mov r15, qword [rbp+8*3]
+ CHECK_MAGIC rdx
+ JNE .case_no_magic1
+ inc r15
+ .case_no_magic1:
+ shl r15, 3
+ MALLOC rax, r15 ; we allocate a pointer to env0
+ shr r15, 3
+ pop r10 ; we take back the pointer to where the pointer to env0 is stored in memory
+ mov qword [r10], rax ;we hang our pointer to env0 in place.
+
+ ;this loop assigns each parameter to it's place in the env0
+ mov qword [rax], SOB_NIL_ADDRESS
+ mov rcx, qword [rbp+8*3]
+ CHECK_MAGIC rdx
+ JNE .case_no_magic2
+ inc rcx
+ .case_no_magic2:
+ cmp rcx, 0 ;we make sure there's some parameters otherwise we skip the assignment
+ je end_of_params_loop%i
+ mov r11, rbp
+ add r11, 8*4 ; now r11 points to the first argument.
+ start_of_env0_loop%i: ;start of assignement loop
+ mov r12, qword [r11]
+ mov qword [rax], r12;
+ add rax, 8 ; we increment i in extenv[0][i]
+ add r11, 8 ; we increment the parameters pointer.
+
+ loop start_of_env0_loop%i
+ end_of_params_loop%i:
+
+ push r10
+ ;MALLOC rax, 17 ; allocate memory for closure.
+ pop r10
+ MAKE_CLOSURE(rax, r10, CLOSURELABEL%i)
+ jmp ENDOFMAKECLOSURE%i
+
+ CLOSURELABEL%i:
+ push rbp
+ mov rbp,rsp
+ %s
+ leave
+ ret
+
+ ENDOFMAKECLOSURE%i:
+ " (index-1) current_label_counter current_label_counter current_label_counter current_label_counter current_label_counter current_label_counter current_label_counter current_label_counter current_label_counter current_label_counter current_label_counter (semi_generate const_table fv_table body index) current_label_counter in
+ comment ^ allocating_commands ^ code
+
+ and make_opt_lambda const_table fv_table params opt body index =
+ if (index = 1)
+ then make_opt_lambda_base const_table fv_table params body index
+ else make_opt_lambda_not_base const_table fv_table params body index
+
+ (* Create an empty env *)
+ and make_opt_lambda_base const_table fv_table params body index =
+ let () = (label_counter := !label_counter+1) in
+ let current_label_counter = !label_counter in
+ let comment = Printf.sprintf ";MAKE_LAMBDA_OPT_BASE%i \n" index in
+ let expected_params = ((List.length params) + 1) in
+ let make_list_out_of_remain = Printf.sprintf
+ "mov r15, PARAM_COUNT
+ mov r14, %i
+ MAKE_PAIR (r13, SOB_NIL_ADDRESS, SOB_NIL_ADDRESS)
+ mov rcx, r15
+ sub rcx, r14
+ mov r10, r13 ; save pointer to the start of the list
+ mov rdi, r10
+ add rdi, 9
+
+ ; prepare r12 with first argument to put in the list
+ mov r12, r14
+ add r12, 3
+ shl r12, 3
+ add r12, rbp
+
+ MAKELIST%i:
+ mov r8, qword [r12]
+ mov qword [r13+1], r8 ; Pair(0) = args(m)
+ add r12, 8 ; m++
+ MAKE_PAIR (r11, SOB_NIL_ADDRESS, SOB_NIL_ADDRESS)
+ mov qword [r13+9], r11
+ add r13, 17
+ mov rdx, qword [rdi]
+ add rdi, 17
+
+ loop MAKELIST%i
+ inc r13
+ mov r8, qword [r12]
+ mov qword [r13], r8
+ mov PVAR(%i), r10
+
+ \n" expected_params current_label_counter current_label_counter (expected_params - 1) in
+ let code1 = (Printf.sprintf
+ "
+ MAKE_CLOSURE(rax, SOB_NIL_ADDRESS, CLOSURELABEL%i)
+ jmp ENDOFMAKECLOSURE%i
+
+ CLOSURELABEL%i:
+ push rbp
+ mov rbp,rsp
+ mov r9, PARAM_COUNT
+ cmp r9, %i
+ je NOSTACKMANIPULATION%i
+ jb USEMAGIC%i\n" current_label_counter current_label_counter current_label_counter expected_params current_label_counter current_label_counter) in
+ let code2 = (Printf.sprintf
+ "jmp BEGINFUNCBODY%i
+
+ USEMAGIC%i:
+ mov rdx, qword [rbp+8*3]
+ add rdx, 4
+ shl rdx, 3
+ add rdx, rbp
+ mov qword [rdx], SOB_NIL_ADDRESS
+ jmp BEGINFUNCBODY%i
+
+ NOSTACKMANIPULATION%i:
+ mov r15, PVAR(%i)
+ MAKE_PAIR (r14, r15, SOB_NIL_ADDRESS)
+ mov PVAR(%i), r14
+
+ BEGINFUNCBODY%i:
+
+ %s
+ leave
+ ret
+
+ ENDOFMAKECLOSURE%i:
+ " current_label_counter current_label_counter current_label_counter current_label_counter (expected_params-1) (expected_params-1) current_label_counter (semi_generate const_table fv_table body index) current_label_counter ) in
+ comment ^ code1 ^ make_list_out_of_remain ^ code2
+
+ (* We extend at least one env *)
+ and make_opt_lambda_not_base const_table fv_table params body index =
+ let () = (label_counter := !label_counter+1) in
+ let current_label_counter = !label_counter in
+ let comment = Printf.sprintf ";MAKE_LAMBDA_OPT_NOT_BASE%i \n" index in
+ let allocating_commands = "MALLOC rax, "^(string_of_int (8*(index-1))) ^ "\n" in
+ let expected_params = ((List.length params) + 1) in
+ let env_code = Printf.sprintf " mov rcx, %i
+ mov r10, rax ; r10 now holds the pointer to the start of the env
+ add rax, 8 ; we increment the poitner from malloc to skip env[0]
+ mov r8, qword [rbp+16] ;r8 contains the start of the last env
+ cmp rcx, 1
+ je skip_env_loop%i
+ start_of_env_loop%i:
+ mov r9, qword [r8] ;we use r9 to dereference.
+ mov qword [rax], r9
+ add rax, 8 ;we increment the pointer to env[i]
+ add r8, 8 ; we increment the pointer to env[j]
+
+ loop start_of_env_loop%i
+
+ skip_env_loop%i:
+ push r10
+
+ mov r15, qword [rbp+8*3]
+ CHECK_MAGIC rdx
+ JNE .case_no_magic1
+ inc r15
+ .case_no_magic1:
+ shl r15, 3
+ MALLOC rax, r15 ; we allocate a pointer to env0
+ shr r15, 3
+ pop r10 ; we take back the pointer to where the pointer to env0 is stored in memory
+ mov qword [r10], rax ;we hang our pointer to env0 in place.
+
+ ;this loop assigns each parameter to it's place in the env0
+ mov qword [rax], SOB_NIL_ADDRESS
+ mov rcx, qword [rbp+8*3]
+ CHECK_MAGIC rdx
+ JNE .case_no_magic2
+ inc rcx
+ .case_no_magic2:
+ cmp rcx, 0 ;we make sure there's some parameters otherwise we skip the assignment
+ je end_of_params_loop%i
+ mov r11, rbp
+ add r11, 8*4 ; now r11 points to the first argument.
+ start_of_env0_loop%i: ;start of assignement loop
+ mov r12, qword [r11]
+ mov qword [rax], r12;
+ add rax, 8 ; we increment i in extenv[0][i]
+ add r11, 8 ; we increment the parameters pointer.
+
+ loop start_of_env0_loop%i
+ end_of_params_loop%i:
+
+ push r10
+ ;MALLOC rax, 17 ; allocate memory for closure.
+ pop r10" (index-1) current_label_counter current_label_counter current_label_counter current_label_counter current_label_counter current_label_counter current_label_counter current_label_counter in
+ let make_list_out_of_remain = Printf.sprintf
+ "mov r15, PARAM_COUNT
+ mov r14, %i
+ MAKE_PAIR (r13, SOB_NIL_ADDRESS, SOB_NIL_ADDRESS)
+ mov rcx, r15
+ sub rcx, r14
+ mov r10, r13 ; save pointer to the start of the list
+ mov rdi, r10
+ add rdi, 9
+
+ ; prepare r12 with first argument to put in the list
+ mov r12, r14
+ add r12, 3
+ shl r12, 3
+ add r12, rbp
+
+ MAKELIST%i:
+ mov r8, qword [r12]
+ mov qword [r13+1], r8 ; Pair(0) = args(m)
+ add r12, 8 ; m++
+ MAKE_PAIR (r11, SOB_NIL_ADDRESS, SOB_NIL_ADDRESS)
+ mov qword [r13+9], r11
+ add r13, 17
+ mov rdx, qword [rdi]
+ add rdi, 17
+
+ loop MAKELIST%i
+ inc r13
+ mov r8, qword [r12]
+ mov qword [r13], r8
+ mov PVAR(%i), r10
+
+ \n" expected_params current_label_counter current_label_counter (expected_params - 1) in
+ let code1 = (Printf.sprintf
+ "
+ MAKE_CLOSURE(rax, r10, CLOSURELABEL%i)
+ jmp ENDOFMAKECLOSURE%i
+
+ CLOSURELABEL%i:
+ push rbp
+ mov rbp,rsp
+ mov r9, PARAM_COUNT
+ cmp r9, %i
+ je NOSTACKMANIPULATION%i
+ jb USEMAGIC%i\n" current_label_counter current_label_counter current_label_counter expected_params current_label_counter current_label_counter) in
+ let code2 = (Printf.sprintf
+ "jmp BEGINFUNCBODY%i
+
+ USEMAGIC%i:
+ mov rdx, qword [rbp+8*3]
+ add rdx, 4
+ shl rdx, 3
+ add rdx, rbp
+ mov qword [rdx], SOB_NIL_ADDRESS
+ jmp BEGINFUNCBODY%i
+
+ NOSTACKMANIPULATION%i:
+ mov r15, PVAR(%i)
+ MAKE_PAIR (r14, r15, SOB_NIL_ADDRESS)
+ mov PVAR(%i), r14
+
+ BEGINFUNCBODY%i:
+
+ %s
+ leave
+ ret
+
+ ENDOFMAKECLOSURE%i:
+ " current_label_counter current_label_counter current_label_counter current_label_counter (expected_params-1) (expected_params-1) current_label_counter (semi_generate const_table fv_table body index) current_label_counter ) in
+ comment ^ allocating_commands ^ env_code ^ code1 ^ make_list_out_of_remain ^ code2
+
+ and make_applic const_table fv_table proc args index =
+ let comment = ";MAKE_APPLIC \n" in
+ let push_args = List.fold_left (fun curr acc -> acc ^ curr) "" (List.map (fun arg -> (semi_generate const_table fv_table arg index) ^ " \n push rax \n") args) in
+ let push_num_of_args = Printf.sprintf "push %i\n" (List.length args) in
+ let generated_proc = semi_generate const_table fv_table proc index in
+ comment ^ magic ^ push_args ^ push_num_of_args ^ generated_proc ^ "
+ CLOSURE_ENV r8, rax ; Now r8 holds the start of our closure.
+ push r8
+ CLOSURE_CODE r8, rax
+ call r8 ; ??
+ add rsp , 8*1 ; pop env
+ pop rbx ; pop arg count
+ shl rbx , 3 ; rbx = rbx * 8
+ add rsp , rbx ; pop args
+ add rsp, 8 ; pop magic \n"
+
+ and make_applicTP const_table fv_table proc args index =
+ let num_of_args = List.length args in
+ let comment = ";MAKE_APPLICTP \n" in
+ let generated_args = List.fold_left (fun curr acc -> acc ^ curr) "" (List.map (fun arg -> (semi_generate const_table fv_table arg index) ^ " \n push rax \n") args) in
+ let push_num_of_args = Printf.sprintf "push %i\n" num_of_args in
+ let generated_proc = (semi_generate const_table fv_table proc index) in
+ let code = Printf.sprintf
+ "
+ CLOSURE_ENV r8, rax ; Now r8 holds the start of our closure.
+ push r8
+
+ push qword [rbp + 8 * 1] ; old ret addr
+ ;mov r14, qword [rbp]
+ ;mov rbp, qword [rbp]
+ mov rsi, [rbp]
+ SHIFT_FRAME %i
+
+ CLOSURE_CODE r8, rax
+ add rsp, 8 ;WE HAVE TO POP ONCE BECAUSE WE FUCKED UP EARLIER AND WE ARE IN TOO DEEP.
+ mov rbp, rsi
+ jmp r8
+
+
+ \n" (5+num_of_args)
+
+ in comment ^ magic ^ generated_args ^ push_num_of_args ^ generated_proc ^ code;;
+
+ let naive_table_no_dups e = remove_dups_strings (naive_free_var_table e primitives) [];;
+
+ let wrapper_make_free_val_table e = make_free_val_table (naive_table_no_dups e) 0 [];;
+
+ let make_consts_tbl asts = make_const_table (final_table asts) 0 [];;
+ let make_fvars_tbl asts = wrapper_make_free_val_table asts;;
+ let generate consts fvars e = semi_generate consts fvars e 0 ;;
+end;;
\ No newline at end of file
diff --git a/compiler.ml b/compiler.ml
index e724266..420d309 100644
--- a/compiler.ml
+++ b/compiler.ml
@@ -1,11 +1,20 @@
+(*
+* TODO:
+* delete all the unwanted labels
+*)
+
#use "code-gen.ml";;
+(* Open the scheme file and read from it *)
let file_to_string f =
let ic = open_in f in
let s = really_input_string ic (in_channel_length ic) in
close_in ic;
s;;
+(* Transform the scheme code (comes as a string) to expr' using reader, tag parser and semantic analyser
+The result is a list of expr'
+*)
let string_to_asts s = List.map Semantics.run_semantics
(Tag_Parser.tag_parse_expressions
(Reader.read_sexprs s));;
@@ -18,12 +27,12 @@ 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"
-(* you can add yours here *)];;
+ "+", "bin_add"; "*", "bin_mul"; "-", "bin_sub"; "/", "bin_div"; "<", "bin_lt"; "=", "bin_equ";
+ "car", "mycar"; "cdr", "mycdr"; "cons", "mycons"; "set-car!", "set_car"; "set-cdr!", "set_cdr"; "apply", "myapply"];;
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_address const consts_tbl in
+ let get_fvar_address const = "fvar_tbl + 8 * " ^ (string_of_int (Code_Gen.get_free_var_index const fvars_tbl)) in
let make_primitive_closure (prim, label) =
" MAKE_CLOSURE(rax, SOB_NIL_ADDRESS, " ^ label ^ ")
mov [" ^ (get_fvar_address prim) ^ "], rax" in
@@ -46,8 +55,9 @@ const_tbl:
;;; 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_FALSE_ADDRESS " ^ get_const_address (Sexpr (Bool false)) ^ "
+%define SOB_TRUE_ADDRESS " ^ get_const_address (Sexpr (Bool true)) ^ "
+%define FVAR(i) [fvar_tbl+i*WORD_SIZE]
fvar_tbl:
" ^ (String.concat "\n" (List.map (fun _ -> "dq T_UNDEFINED") fvars_tbl)) ^ "
@@ -55,6 +65,10 @@ fvar_tbl:
global main
section .text
main:
+
+ push rbp
+ mov rbp, rsp
+
;; set up the heap
mov rdi, GB(4)
call malloc
@@ -84,7 +98,8 @@ code_fragment:
";;
-let epilogue = raise X_not_yet_implemented;;
+(* **************IMPLEMENT!!!!!************** *)
+let epilogue = "exit_program_label: \n ret";;
exception X_missing_input_file;;
@@ -102,7 +117,7 @@ try
let provided_primitives = file_to_string "prims.s" in
print_string ((make_prologue consts_tbl fvars_tbl) ^
- code_fragment ^
+ code_fragment ^ "\n debug_exit_label: \n jmp exit_program_label" ^
provided_primitives ^ "\n" ^ epilogue)
with Invalid_argument(x) -> raise X_missing_input_file;;
diff --git a/compiler.s b/compiler.s
index 5c418e8..6cbe598 100644
--- a/compiler.s
+++ b/compiler.s
@@ -63,6 +63,7 @@
%define CLOSURE_CODE CDR
%define PVAR(n) qword [rbp+(4+n)*WORD_SIZE]
+%define PARAM_COUNT qword [rbp+3*WORD_SIZE]
%define SOB_UNDEFINED T_UNDEFINED
%define SOB_NIL T_NIL
@@ -98,10 +99,24 @@
mov qword [%1+TYPE_SIZE], %2
%endmacro
+
+%define MAKE_LITERAL_INT(val) MAKE_LITERAL T_INTEGER, dq val
+%define MAKE_LITERAL_FLOAT(val) MAKE_LITERAL T_FLOAT, dq val
+%define MAKE_LITERAL_CHAR(val) MAKE_LITERAL T_CHAR, db val
+%define MAKE_LITERAL_SYMBOL(val) MAKE_LITERAL T_SYMBOL, dq 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_INT(r,val) MAKE_LONG_VALUE r, val, T_INTEGER
%define MAKE_FLOAT(r,val) MAKE_LONG_VALUE r, val, T_FLOAT
%define MAKE_CHAR(r,val) MAKE_CHAR_VALUE r, val
+%macro MAKE_LITERAL 2
+ db %1
+ %2
+%endmacro
+
+
; Create a string of length %2
; from char %3.
; Stores result in register %1
@@ -124,6 +139,23 @@
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 CHECK_MAGIC 1
+ mov %1, qword [rbp+8*3]
+ add %1, 4
+ shl %1, 3
+ add %1, rbp
+ mov %1, qword [%1]
+ cmp %1, SOB_NIL_ADDRESS
+%endmacro
+
; Create a vector of length %2
; from SOB at %3.
; Stores result in register %1
@@ -146,14 +178,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,7 +212,53 @@
%define MAKE_CLOSURE(r, env, body) \
MAKE_TWO_WORDS r, T_CLOSURE, env, body
-
+; m is the number of new params
+; n is old
+; %1 = size of frame (constant), we will loop that amount of times
+; PARAM_COUNT is the number of params in old frame
+; each loop we decrease rax and increase i
+%macro SHIFT_FRAME 1
+ push rax ; save rax
+ mov rax, PARAM_COUNT
+ add rax, 5 ; now rax hold the size of the old frame (no.params + 5 (4+magic))
+ mov r13, %1
+ sub r13, rax
+%assign i 1
+%rep %1
+ dec rax
+ mov r15, [rbp-WORD_SIZE*i]
+ mov [rbp+WORD_SIZE*rax], r15 ; shift the frame downwards, put the end of the new frame at the end of the old frame and so on
+%assign i i+1
+%endrep
+ pop rax ; return rax
+ shl r13, 3 ; r13 = (m-n)*8
+ mov r14, rbp
+ sub r14, r13
+ mov rsp, r14
+
+%endmacro
+
+%macro SHIFT_FRAME_APPLY 2
+ push rax ; save rax
+ mov rax, PARAM_COUNT
+ add rax, 5 ; now rax hold the size of the old frame (no.params + 5 (4+magic))
+ mov r13, %1
+ sub r13, rax
+%assign i %0
+%rep i
+ dec rax
+ mov r15, [rbp-WORD_SIZE*i]
+ mov [rbp+WORD_SIZE*rax], r15 ; shift the frame downwards, put the end of the new frame at the end of the old frame and so on
+%assign i i+1
+%endrep
+ pop rax ; return rax
+ shl r13, 3 ; r13 = (m-n)*8
+ mov r14, rbp
+ sub r14, r13
+ mov rsp, r14
+
+%endmacro
+
extern exit, printf, malloc
global write_sob, write_sob_if_not_void
diff --git a/prims.s b/prims.s
index bd9d118..2715ade 100644
--- a/prims.s
+++ b/prims.s
@@ -1,3 +1,6 @@
+
+
+
is_boolean:
push rbp
mov rbp, rsp
@@ -7,7 +10,7 @@ is_boolean:
cmp sil, T_BOOL
jne .wrong_type
- mov rax, sob_true
+ mov rax, SOB_TRUE_ADDRESS
jmp .return
.wrong_type:
@@ -25,7 +28,7 @@ is_float:
cmp sil, T_FLOAT
jne .wrong_type
- mov rax, sob_true
+ mov rax, SOB_TRUE_ADDRESS
jmp .return
.wrong_type:
@@ -43,7 +46,7 @@ is_integer:
cmp sil, T_INTEGER
jne .wrong_type
- mov rax, sob_true
+ mov rax, SOB_TRUE_ADDRESS
jmp .return
.wrong_type:
@@ -61,7 +64,7 @@ is_pair:
cmp sil, T_PAIR
jne .wrong_type
- mov rax, sob_true
+ mov rax, SOB_TRUE_ADDRESS
jmp .return
.wrong_type:
@@ -79,7 +82,7 @@ is_null:
cmp sil, T_NIL
jne .wrong_type
- mov rax, sob_true
+ mov rax, SOB_TRUE_ADDRESS
jmp .return
.wrong_type:
@@ -97,7 +100,7 @@ is_char:
cmp sil, T_CHAR
jne .wrong_type
- mov rax, sob_true
+ mov rax, SOB_TRUE_ADDRESS
jmp .return
.wrong_type:
@@ -115,7 +118,7 @@ is_vector:
cmp sil, T_VECTOR
jne .wrong_type
- mov rax, sob_true
+ mov rax, SOB_TRUE_ADDRESS
jmp .return
.wrong_type:
@@ -133,7 +136,7 @@ is_string:
cmp sil, T_STRING
jne .wrong_type
- mov rax, sob_true
+ mov rax, SOB_TRUE_ADDRESS
jmp .return
.wrong_type:
@@ -151,7 +154,7 @@ is_procedure:
cmp sil, T_CLOSURE
jne .wrong_type
- mov rax, sob_true
+ mov rax, SOB_TRUE_ADDRESS
jmp .return
.wrong_type:
@@ -169,7 +172,7 @@ is_symbol:
cmp sil, T_SYMBOL
jne .wrong_type
- mov rax, sob_true
+ mov rax, SOB_TRUE_ADDRESS
jmp .return
.wrong_type:
@@ -390,6 +393,7 @@ is_eq:
leave
ret
+
bin_add:
push rbp
mov rbp, rsp
@@ -470,6 +474,7 @@ bin_add:
leave
ret
+
bin_mul:
push rbp
mov rbp, rsp
@@ -774,8 +779,8 @@ bin_lt:
cmpltsd xmm0, xmm1
pop r8
- cmp r8, 3
- jne .return_float
+ cmp r8, 0
+ je .return_float
cvttsd2si rsi, xmm0
MAKE_INT(rax, rsi)
@@ -866,8 +871,8 @@ bin_equ:
cmpeqsd xmm0, xmm1
pop r8
- cmp r8, 3
- jne .return_float
+ cmp r8, 0
+ je .return_float
cvttsd2si rsi, xmm0
MAKE_INT(rax, rsi)
@@ -894,3 +899,163 @@ bin_equ:
leave
ret
+mycar:
+push rbp
+mov rbp, rsp
+
+mov rsi, PVAR(0)
+mov rax, qword [rsi+1]
+
+leave
+ret
+
+mycdr:
+push rbp
+mov rbp, rsp
+
+mov rsi, PVAR(0)
+mov rax, qword [rsi+9]
+
+leave
+ret
+
+mycons:
+
+push rbp
+mov rbp, rsp
+
+mov rsi, PVAR(0)
+mov rdi, PVAR(1)
+MAKE_PAIR(rax, rsi, rdi)