-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerator.ml
1187 lines (1179 loc) · 45.2 KB
/
generator.ml
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
open Printf
open Helper
open Parser
open Analyzer
let appfmt buf fmt =
ksprintf (fun str -> Buffer.add_string buf (str ^ "\n")) fmt
let appstr buf str = Buffer.add_string buf (str ^ "\n")
let escape_string str =
let buf = Buffer.create (String.length str) in
let rec aux i =
if i < String.length str then (
( match str.[i] with
| '\n' -> Buffer.add_string buf "\\n"
| '\r' -> Buffer.add_string buf "\\r"
| '\t' -> Buffer.add_string buf "\\t"
| '\\' -> Buffer.add_string buf "\\\\"
| '"' -> Buffer.add_string buf "\\\""
| ch -> Buffer.add_char buf ch ) ;
aux (i + 1) )
in
aux 0 ; Buffer.contents buf
let make_label () = make_id ".L"
type gen_environment = {offset: int; varoffset: (string, int) Hashmap.t}
type ctype = CTyInt | CTyUnit | CTyPtr
type tail_recursive = Tail | NonTail
let rec generate (letfuncs, strings, typedefs, exps) =
let stack_size = ref 0 in
let new_offset env size =
let offset = env.offset - (8 * size) in
stack_size := max !stack_size (-offset) ;
offset
in
let records_idx = Hashtbl.create 16 in
let ctors_id = Hashtbl.create 16 in
List.iter
(function
| DefTypeAlias _ -> ()
| DefVariant (_, typename, ctornames) ->
List.iteri
(fun i (ctorname, _) -> Hashtbl.add ctors_id (typename, ctorname) i)
ctornames
| DefRecord (typename, fields) ->
List.iteri
(fun i (fieldname, _) ->
Hashtbl.add records_idx (typename, fieldname) i )
fields)
typedefs ;
let exps_id = Hashtbl.create 16 in
List.iter
(fun expname -> Hashtbl.add exps_id expname @@ Hashtbl.length exps_id)
exps ;
let reg_of_index idx =
[|"rax"; "rbx"; "rdi"; "rsi"; "rdx"; "rcx"; "r8"; "r9"; "r12"; "r13"|].(idx)
in
let tag_int reg = sprintf "lea %s, [%s + %s + 1]" reg reg reg in
let untag_int reg = sprintf "sar %s, 1" reg in
let tagged_int num = (num lsl 1) lor 1 in
let gen_call_with_aligned_rsp callee =
let buf = Buffer.create 128 in
let just_call_label = make_label () in
let exit_label = make_label () in
appstr buf "mov rax, rsp" ;
appstr buf "and rax, 0x0f" ;
appstr buf "cmp rax, 0" ;
appfmt buf "je %s" just_call_label ;
(* rsp is not 16-byte aligned BUT 8-byte aligned *)
appstr buf "sub rsp, 8" ;
appfmt buf "call %s" callee ;
appstr buf "add rsp, 8" ;
appfmt buf "jmp %s" exit_label ;
appfmt buf "%s:" just_call_label ;
appfmt buf "call %s" callee ;
appfmt buf "%s:" exit_label ;
Buffer.contents buf
in
let save_rsp = "mov [rip + aqaml_current_rsp], rsp" in
let rec gen_alloc_block size color tag =
(* allocated block address is in rax *)
let buf = Buffer.create 128 in
appstr buf save_rsp ;
appfmt buf "mov rdi, %d" size ;
appfmt buf "mov rsi, %d" color ;
appfmt buf "mov rdx, %d" tag ;
appstr buf @@ gen_call_with_aligned_rsp "aqaml_alloc_block@PLT" ;
(* appstr buf "call aqaml_alloc_block@PLT" ; *)
Buffer.contents buf
in
let rec gen_assign_pattern env exp_label = function
| UnitValue | EmptyList -> gen_assign_pattern env exp_label @@ IntValue 0
| IntValue num ->
let buf = Buffer.create 128 in
let exit_label = make_label () in
appstr buf "pop rax" ;
appfmt buf "cmp rax, %d" @@ tagged_int num ;
appfmt buf "je %s" exit_label ;
appfmt buf "jmp %s" exp_label ;
appfmt buf "%s:" exit_label ;
Buffer.contents buf
| CharValue ch ->
gen_assign_pattern env exp_label @@ IntValue (Char.code ch)
| StringValue (id, str) ->
let buf = Buffer.create 128 in
appstr buf "pop rax" ;
appfmt buf "lea rbx, [rip + %s]" id ;
appstr buf "call aqaml_structural_equal" ;
appstr buf "cmp rax, 1" ;
appfmt buf "je %s" exp_label ;
Buffer.contents buf
| Var varname ->
let buf = Buffer.create 128 in
let offset = Hashmap.find varname env.varoffset in
appstr buf "pop rax" ;
appfmt buf "mov [rbp + %d], rax" offset ;
Buffer.contents buf
| Cons (car, cdr) ->
let buf = Buffer.create 128 in
appstr buf "pop rax" ;
appstr buf "cmp rax, 1" ;
appfmt buf "je %s" exp_label ;
appstr buf "push QWORD PTR [rax]" ;
appstr buf "push QWORD PTR [rax + 8]" ;
appstr buf @@ gen_assign_pattern env exp_label cdr ;
appstr buf @@ gen_assign_pattern env exp_label car ;
Buffer.contents buf
| TupleValue values ->
let buf = Buffer.create 128 in
appstr buf "pop rax" ;
List.iteri
(fun i _ -> appfmt buf "push QWORD PTR [rax + %d]" (i * 8))
values ;
List.iter
(fun x -> appstr buf @@ gen_assign_pattern env exp_label x)
(List.rev values) ;
Buffer.contents buf
| CtorApp (Some typename, ctorname, None) ->
gen_assign_pattern env exp_label
@@ IntValue
( try Hashtbl.find ctors_id (typename, ctorname)
with Not_found -> Hashtbl.find exps_id typename )
| CtorApp (Some typename, ctorname, Some arg) ->
let buf = Buffer.create 128 in
let id =
try Hashtbl.find ctors_id (typename, ctorname) with Not_found ->
Hashtbl.find exps_id typename
in
appfmt buf "pop rax" ;
appstr buf "mov rdi, rax" ;
appstr buf "and rax, 1" ;
appstr buf "cmp rax, 0" ;
appfmt buf "jne %s" exp_label ;
appstr buf "mov rax, [rdi - 8]" ;
appstr buf "and rax, 0xff" ;
appfmt buf "cmp rax, %d" id ;
appfmt buf "jne %s" exp_label ;
appstr buf "push [rdi]" ;
appstr buf @@ gen_assign_pattern env exp_label arg ;
Buffer.contents buf
| PtnAlias (ptn, Var varname) ->
let buf = Buffer.create 128 in
let offset = Hashmap.find varname env.varoffset in
appstr buf "pop rax" ;
appfmt buf "mov [rbp + %d], rax" offset ;
appstr buf "push rax" ;
appstr buf @@ gen_assign_pattern env exp_label ptn ;
Buffer.contents buf
| PtnOr (lhs, rhs) ->
let next_label = make_label () in
let exit_label = make_label () in
let saved_rsp_offset = new_offset env 1 in
let env = {env with offset= saved_rsp_offset} in
let buf = Buffer.create 128 in
appfmt buf "mov [rbp + %d], rsp" saved_rsp_offset ;
appstr buf "pop rax" ;
appstr buf "push rax" ;
appstr buf "push rax" ;
appstr buf @@ gen_assign_pattern env next_label lhs ;
appstr buf "pop rax" ;
appfmt buf "jmp %s" exit_label ;
appfmt buf "%s:" next_label ;
appfmt buf "mov rsp, [rbp + %d]" saved_rsp_offset ;
appstr buf @@ gen_assign_pattern env exp_label rhs ;
appfmt buf "%s:" exit_label ;
Buffer.contents buf
| PtnRange (bg, ed) ->
let buf = Buffer.create 128 in
let exit0_label = make_label () in
let exit1_label = make_label () in
appstr buf "pop rax" ;
appfmt buf "cmp rax, %d" @@ tagged_int @@ Char.code bg ;
appfmt buf "jge %s" exit0_label ;
appfmt buf "jmp %s" exp_label ;
appfmt buf "%s:" exit0_label ;
appfmt buf "cmp rax, %d" @@ tagged_int @@ Char.code ed ;
appfmt buf "jle %s" exit1_label ;
appfmt buf "jmp %s" exp_label ;
appfmt buf "%s:" exit1_label ;
Buffer.contents buf
| _ -> raise Unexpected_ast
in
let rec gen_assign_pattern_or_raise env ptn =
let exp_label = make_label () in
let exit_label = make_label () in
let assign_code = gen_assign_pattern env exp_label ptn in
let buf = Buffer.create 256 in
appstr buf assign_code ;
appfmt buf "jmp %s" exit_label ;
appfmt buf "%s:" exp_label ;
(* TODO: arguments of Match_failure *)
appstr buf "mov rax, 1" ;
appstr buf @@ gen_raise_exp_of "Stdlib.Match_failure" true ;
appfmt buf "%s:" exit_label ;
Buffer.contents buf
and gen_raise () =
let buf = Buffer.create 128 in
(* Raise. Thanks to:
* https://github.com/ocamllabs/ocaml-multicore/wiki/Native-code-notes *)
appstr buf "mov rsp, r14" ;
appstr buf "pop r14" ;
appstr buf "ret" ;
Buffer.contents buf
and gen_raise_exp_of expname has_arguments =
let buf = Buffer.create 128 in
appstr buf "/* raise */" ;
if not has_arguments then
appfmt buf "mov rax, %d" @@ tagged_int @@ Hashtbl.find exps_id expname
else (
(* Assume that the argument is stored in rax *)
appstr buf "mov rbx, rax" ;
appstr buf @@ gen_alloc_block 1 0 @@ Hashtbl.find exps_id expname ;
appstr buf "mov [rax], rbx" ) ;
appstr buf @@ gen_raise () ;
Buffer.contents buf
and gen_pattern_match_cases env cases istail exp_body =
(* Assume that the target value is in stack top *)
let buf = Buffer.create 128 in
let saved_rsp_offset = new_offset env 1 in
let env = {env with offset= saved_rsp_offset} in
appfmt buf "mov [rbp + %d], rsp" saved_rsp_offset ;
let exit_label = make_label () in
let exp_label =
List.fold_left
(fun this_label (ptn, whn, case) ->
let varnames = varnames_in_pattern ptn in
let offset = new_offset env @@ List.length varnames in
let env =
{ offset
; varoffset=
(let rec aux i varoffset = function
| varname :: varnames ->
aux (i + 1)
(Hashmap.add varname (env.offset - (i * 8)) varoffset)
varnames
| [] -> varoffset
in
aux 1 env.varoffset varnames) }
in
let next_label = make_label () in
appfmt buf "%s:" this_label ;
appfmt buf "mov rsp, [rbp + %d]" saved_rsp_offset ;
appstr buf "pop rax" ;
appstr buf "push rax" ;
appstr buf "push rax" ;
appstr buf @@ gen_assign_pattern env next_label ptn ;
( match whn with
| None -> ()
| Some expr ->
appstr buf @@ aux env (NonTail, expr) ;
appstr buf "pop rax" ;
appfmt buf "cmp rax, %d" @@ tagged_int 0 ;
appfmt buf "je %s" next_label ) ;
appstr buf "pop rax" ;
appstr buf @@ aux env (istail, case) ;
appfmt buf "jmp %s /* exit label */" exit_label ;
next_label )
(make_label ()) cases
in
appfmt buf "%s:" exp_label ;
appstr buf "/* match failed */" ;
appstr buf exp_body ;
appfmt buf "%s:" exit_label ;
Buffer.contents buf
and aux env = function
| _, Nope -> "push 0 /* dummy */"
| _, IntValue num -> sprintf "push %d" (tagged_int num)
| istail, CharValue ch -> aux env (istail, IntValue (Char.code ch))
| istail, (UnitValue | EmptyList) -> aux env (istail, IntValue 0)
| _, StringValue (id, _) -> sprintf "lea rax, [rip + %s]\npush rax" id
| _, Cons (car, cdr) ->
let buf = Buffer.create 128 in
appstr buf "/* Cons BEGIN */" ;
appstr buf @@ aux env (NonTail, cdr) ;
appstr buf @@ aux env (NonTail, car) ;
appstr buf @@ gen_alloc_block 2 0 0 ;
appstr buf "pop rdi /* car */" ;
appstr buf "mov [rax], rdi" ;
appstr buf "pop rdi /* cdr */" ;
appstr buf "mov [rax + 8], rdi" ;
appstr buf "push rax" ;
appstr buf "/* Cons END */" ;
Buffer.contents buf
| _, TupleValue values ->
let size = List.length values in
let buf = Buffer.create 128 in
appstr buf @@ "/* TupleValue BEGIN */" ;
List.iter
(fun x -> appstr buf @@ aux env (NonTail, x))
(List.rev values) ;
appstr buf @@ gen_alloc_block size 0 1 ;
List.iteri
(fun i _ -> appfmt buf "pop rdi\nmov [rax + %d], rdi" (i * 8))
values ;
appstr buf @@ "push rax" ;
appstr buf @@ "/* TupleValue END */" ;
Buffer.contents buf
| _, ArrayValue values ->
let size = List.length values in
let buf = Buffer.create 128 in
appstr buf @@ "/* ArrayValue BEGIN */" ;
List.iter
(fun x -> appstr buf @@ aux env (NonTail, x))
(List.rev values) ;
appstr buf @@ gen_alloc_block size 0 1 ;
List.iteri
(fun i _ -> appfmt buf "pop rdi\nmov [rax + %d], rdi" (i * 8))
values ;
appstr buf @@ "push rax" ;
appstr buf @@ "/* ArrayValue END */" ;
Buffer.contents buf
| _, RecordValue (Some typename, fields) ->
let offset = new_offset env 1 in
let env = {env with offset} in
let buf = Buffer.create 128 in
appfmt buf "/* RecordValue %s BEGIN */" typename ;
appstr buf @@ gen_alloc_block (List.length fields) 0 1 ;
appfmt buf "mov [rbp + %d], rax" offset ;
List.iter
(fun (fieldname, ast) ->
appstr buf @@ aux env (NonTail, ast) ;
appstr buf "pop rax" ;
appfmt buf "mov rdi, [rbp + %d]" offset ;
let idx = Hashtbl.find records_idx (typename, fieldname) in
appfmt buf "mov [rdi + %d], rax" (idx * 8) )
fields ;
appfmt buf "push [rbp + %d]" offset ;
appfmt buf "/* RecordValue %s END */" typename ;
Buffer.contents buf
| _, RecordValueWith (Some typename, base, fields, Some comp_fieldnames) ->
let offset = new_offset env 1 in
let env = {env with offset} in
let buf = Buffer.create 128 in
appfmt buf "/* RecordValueWith %s BEGIN */" typename ;
appstr buf
@@ gen_alloc_block
(List.length fields + List.length comp_fieldnames)
0 1 ;
appfmt buf "mov [rbp + %d], rax" offset ;
List.iter
(fun (fieldname, ast) ->
appstr buf @@ aux env (NonTail, ast) ;
appstr buf "pop rax" ;
appfmt buf "mov rdi, [rbp + %d]" offset ;
let idx = Hashtbl.find records_idx (typename, fieldname) in
appfmt buf "mov [rdi + %d], rax" (idx * 8) )
fields ;
appstr buf @@ aux env (NonTail, base) ;
appstr buf "pop rax" ;
appfmt buf "mov rdi, [rbp + %d]" offset ;
List.iter
(fun fieldname ->
let idx = Hashtbl.find records_idx (typename, fieldname) in
appfmt buf "mov rsi, [rax + %d]" (idx * 8) ;
appfmt buf "mov [rdi + %d], rsi" (idx * 8) )
comp_fieldnames ;
appstr buf "push rdi" ;
appfmt buf "/* RecordValueWith %s END */" typename ;
Buffer.contents buf
| _, RecordDotAccess (Some typename, ast, fieldname) ->
let idx = Hashtbl.find records_idx (typename, fieldname) in
let buf = Buffer.create 128 in
appfmt buf "/* RecordDotAccess %s %s */" typename fieldname ;
appstr buf @@ aux env (NonTail, ast) ;
appstr buf "pop rax" ;
appfmt buf "push [rax + %d]" (idx * 8) ;
Buffer.contents buf
| _, Add (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf @@ "pop rdi" ;
appstr buf @@ untag_int "rdi" ;
appstr buf @@ "pop rax" ;
appstr buf @@ untag_int "rax" ;
appstr buf @@ "add rax, rdi" ;
appstr buf @@ tag_int "rax" ;
appstr buf @@ "push rax" ;
Buffer.contents buf
| _, Sub (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf @@ "pop rdi" ;
appstr buf @@ untag_int "rdi" ;
appstr buf @@ "pop rax" ;
appstr buf @@ untag_int "rax" ;
appstr buf @@ "sub rax, rdi" ;
appstr buf @@ tag_int "rax" ;
appstr buf @@ "push rax" ;
Buffer.contents buf
| _, Mul (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf @@ "pop rdi" ;
appstr buf @@ untag_int "rdi" ;
appstr buf @@ "pop rax" ;
appstr buf @@ untag_int "rax" ;
appstr buf @@ "imul rax, rdi" ;
appstr buf @@ tag_int "rax" ;
appstr buf @@ "push rax" ;
Buffer.contents buf
| _, Div (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rdi" ;
appstr buf @@ untag_int "rdi" ;
appstr buf "pop rax" ;
appstr buf @@ untag_int "rax" ;
appstr buf "cqo" ;
appstr buf "idiv rdi" ;
appstr buf @@ tag_int "rax" ;
appstr buf "push rax" ;
Buffer.contents buf
| _, Rem (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rdi" ;
appstr buf @@ untag_int "rdi" ;
appstr buf "pop rax" ;
appstr buf @@ untag_int "rax" ;
appstr buf "cqo" ;
appstr buf "idiv rdi" ;
appstr buf @@ tag_int "rdx" ;
appstr buf "push rdx" ;
Buffer.contents buf
| _, LogicalLeftShift (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rcx" ;
appstr buf @@ untag_int "rcx" ;
appstr buf "pop rax" ;
appstr buf @@ untag_int "rax" ;
appstr buf "shl rax, cl" ;
appstr buf @@ tag_int "rax" ;
appstr buf "push rax" ;
Buffer.contents buf
| _, LogicalRightShift (lhs, rhs) ->
(* Note that the size of int is 63bit, not 64bit. *)
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rcx" ;
appstr buf @@ untag_int "rcx" ;
appstr buf "pop rax" ;
appstr buf "shr rax, cl" ;
appstr buf "or rax, 1" ;
appstr buf "push rax" ;
Buffer.contents buf
| _, ArithmeticRightShift (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rcx" ;
appstr buf @@ untag_int "rcx" ;
appstr buf "pop rax" ;
appstr buf @@ untag_int "rax" ;
appstr buf "sar rax, cl" ;
appstr buf @@ tag_int "rax" ;
appstr buf "push rax" ;
Buffer.contents buf
| _, BitwiseAnd (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rdi" ;
appstr buf "pop rax" ;
appstr buf "and rax, rdi" ;
appstr buf "push rax" ;
Buffer.contents buf
| _, BitwiseOr (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rdi" ;
appstr buf "pop rax" ;
appstr buf "or rax, rdi" ;
appstr buf "push rax" ;
Buffer.contents buf
| _, StringConcat (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rbx" ;
appstr buf "pop rax" ;
appstr buf "call aqaml_concat_string" ;
appstr buf "push rax" ;
Buffer.contents buf
| _, ListConcat (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rbx" ;
appstr buf "pop rax" ;
appstr buf "call aqaml_concat_list" ;
appstr buf "push rax" ;
Buffer.contents buf
| _, RefAssign (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rbx" ;
appstr buf "pop rax" ;
appstr buf "mov [rax], rbx" ;
(* push unit value *)
appstr buf "push 1" ;
Buffer.contents buf
| _, RecordAssign (Some typename, lhs, fieldname, rhs) ->
let idx = Hashtbl.find records_idx (typename, fieldname) in
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rbx" ;
appstr buf "pop rax" ;
appfmt buf "mov [rax + %d], rbx" (idx * 8) ;
(* push unit value *)
appstr buf "push 1" ;
Buffer.contents buf
| _, Deref ast ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, ast) ;
appstr buf "pop rax" ;
appstr buf "push [rax]" ;
Buffer.contents buf
| _, Positate ast -> ""
| _, Negate ast ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, ast) ;
appstr buf "pop rax" ;
appstr buf @@ untag_int "rax" ;
appstr buf "neg rax" ;
appstr buf @@ tag_int "rax" ;
appstr buf "push rax" ;
Buffer.contents buf
| _, StructEqual (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf @@ "pop rbx" ;
appstr buf @@ "pop rax" ;
appstr buf @@ "call aqaml_structural_equal" ;
appstr buf @@ "push rax" ;
Buffer.contents buf
| _, StructInequal (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rbx" ;
appstr buf "pop rax" ;
appstr buf "call aqaml_structural_inequal" ;
appstr buf "push rax" ;
Buffer.contents buf
| _, LessThan (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rdi" ;
appstr buf "pop rax" ;
appstr buf "cmp rax, rdi" ;
appstr buf "setl al" ;
appstr buf "movzx rax, al" ;
appstr buf @@ tag_int "rax" ;
appstr buf "push rax" ;
Buffer.contents buf
| _, LessThanEqual (lhs, rhs) ->
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rdi" ;
appstr buf "pop rax" ;
appstr buf "cmp rax, rdi" ;
appstr buf "setle al" ;
appstr buf "movzx rax, al" ;
appstr buf @@ tag_int "rax" ;
appstr buf "push rax" ;
Buffer.contents buf
| _, LogicalAnd (lhs, rhs) ->
let false_label = make_label () in
let exit_label = make_label () in
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf "pop rax" ;
appfmt buf "cmp rax, %d" @@ tagged_int 0 ;
appfmt buf "je %s" false_label ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rax" ;
appfmt buf "cmp rax, %d" @@ tagged_int 0 ;
appfmt buf "je %s" false_label ;
appfmt buf "push %d" @@ tagged_int 1 ;
appfmt buf "jmp %s" exit_label ;
appfmt buf "%s:" false_label ;
appfmt buf "push %d" @@ tagged_int 0 ;
appfmt buf "%s:" exit_label ;
Buffer.contents buf
| _, LogicalOr (lhs, rhs) ->
let true_label = make_label () in
let exit_label = make_label () in
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf "pop rax" ;
appfmt buf "cmp rax, %d" @@ tagged_int 1 ;
appfmt buf "je %s" true_label ;
appstr buf @@ aux env (NonTail, rhs) ;
appstr buf "pop rax" ;
appfmt buf "cmp rax, %d" @@ tagged_int 1 ;
appfmt buf "je %s" true_label ;
appfmt buf "push %d" @@ tagged_int 0 ;
appfmt buf "jmp %s" exit_label ;
appfmt buf "%s:" true_label ;
appfmt buf "push %d" @@ tagged_int 1 ;
appfmt buf "%s:" exit_label ;
Buffer.contents buf
| istail, IfThenElse (cond, then_body, else_body) ->
let false_label = make_label () in
let exit_label = make_label () in
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, cond) ;
appstr buf "pop rax" ;
appstr buf "cmp rax, 1" (* if rax = 0 then then_body else else_body *) ;
appfmt buf "je %s" false_label ;
appstr buf @@ aux env (istail, then_body) ;
appfmt buf "jmp %s" exit_label ;
appfmt buf "%s:" false_label ;
( match else_body with
| None ->
appstr buf @@ aux env (istail, IntValue 0)
(* unit value is IntValue 0 *)
| Some else_body -> appstr buf @@ aux env (istail, else_body) ) ;
appfmt buf "%s:" exit_label ;
Buffer.contents buf
| istail, ExprSeq exprs ->
String.concat "\npop rax\n"
(List.mapi
(fun i x ->
aux env
((if i = List.length exprs - 1 then istail else NonTail), x)
)
exprs)
| _, Var varname -> (
try
let offset = Hashmap.find varname env.varoffset in
let buf = Buffer.create 128 in
appfmt buf "mov rax, [rbp + %d]" offset ;
appstr buf "push rax" ;
Buffer.contents buf
with Not_found ->
failwith (sprintf "not found in code generation: %s" varname) )
| istail, CtorApp (Some typename, ctorname, None) ->
aux env
@@ ( istail
, IntValue
( try Hashtbl.find ctors_id (typename, ctorname)
with Not_found -> Hashtbl.find exps_id typename ) )
| _, CtorApp (Some typename, ctorname, Some arg) ->
let buf = Buffer.create 128 in
appstr buf
@@ gen_alloc_block 1 0
( try Hashtbl.find ctors_id (typename, ctorname)
with Not_found -> Hashtbl.find exps_id typename ) ;
appstr buf "push rax" ;
appstr buf @@ aux env (NonTail, arg) ;
appstr buf "pop rdi" ;
appstr buf "pop rax" ;
appfmt buf "mov [rax], rdi" ;
appstr buf "push rax" ;
Buffer.contents buf
| istail, AppDir (funcname, args) ->
let buf = Buffer.create 128 in
List.iter
(fun arg -> appstr buf @@ aux env (NonTail, arg))
(List.rev args) ;
List.iteri
(fun index reg ->
if index < List.length args then appfmt buf "pop %s" reg )
["rax"; "rbx"; "rdi"; "rsi"; "rdx"; "rcx"; "r8"; "r9"; "r12"; "r13"] ;
( match istail with
| NonTail ->
appfmt buf "call %s" funcname ;
appstr buf "push rax"
| Tail ->
(* TODO: arguments passed via stack *)
appstr buf "mov rsp, rbp" ;
appstr buf "pop rbp" ;
appfmt buf "jmp %s" funcname ) ;
Buffer.contents buf
| istail, AppCls (func, args) ->
(* call aqaml_appcls *)
(* TODO: Any better way exists? *)
(* TODO: only 9 or less arguments are allowed *)
if List.length args > 9 then
failwith "only 9 or less arguments are allowed (not implemented)" ;
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, func) ;
List.iter
(fun arg -> appstr buf @@ aux env (NonTail, arg))
(List.rev args) ;
List.iteri
(fun index reg ->
if index < List.length args then appfmt buf "pop %s" reg )
["rbx"; "rdi"; "rsi"; "rdx"; "rcx"; "r8"; "r9"; "r12"; "r13"] ;
appstr buf "pop rax" ;
( match istail with
| NonTail ->
appfmt buf "call aqaml_appcls%d" @@ List.length args ;
appstr buf "push rax"
| Tail ->
(* TODO: arguments passed via stack *)
appstr buf "mov rsp, rbp" ;
appstr buf "pop rbp" ;
appfmt buf "jmp aqaml_appcls%d" @@ List.length args ) ;
Buffer.contents buf
| _, ForLoop (dir, indexname, expr1, expr2, expr3) ->
let loop_label = make_label () in
let exit_label = make_label () in
let offset = new_offset env 1 in
let env' =
{offset; varoffset= Hashmap.add indexname offset env.varoffset}
in
let buf = Buffer.create 128 in
appstr buf @@ aux env (NonTail, expr2) ;
appstr buf @@ aux env (NonTail, expr1) ;
appstr buf "pop rax" ;
appfmt buf "mov [rbp + %d], rax" offset ;
appstr buf "pop rax" ;
appfmt buf "cmp [rbp + %d], rax" offset ;
appfmt buf "%s %s"
(match dir with ForTo -> "jg" | ForDownto -> "jl")
exit_label ;
appstr buf "push rax" ;
appfmt buf "%s:" loop_label ;
appfmt buf "pop rax" ;
appfmt buf "cmp [rbp + %d], rax" offset ;
appfmt buf "%s %s"
(match dir with ForTo -> "jg" | ForDownto -> "jl")
exit_label ;
appstr buf "push rax" ;
appstr buf @@ aux env' (NonTail, expr3) ;
appstr buf "pop rax /* pop unit value */" ;
( match dir with
| ForTo -> appfmt buf "add QWORD PTR [rbp + %d], 2" offset
| ForDownto -> appfmt buf "sub QWORD PTR [rbp + %d], 2" offset ) ;
appfmt buf "jmp %s" loop_label ;
appfmt buf "%s:" exit_label ;
appfmt buf "push %d /* push unit value */" @@ tagged_int 0 ;
Buffer.contents buf
| istail, LetAndAnalyzed (lets, rhs_of_in) ->
let buf = Buffer.create 256 in
let aux' env' = function
| LetVar (false, bind, lhs) ->
let varnames = varnames_in_pattern bind in
let offset = new_offset env' @@ List.length varnames in
let env' =
{ offset
; varoffset=
integrate env'.varoffset @@ hashmap_of_list
@@ List.mapi
(fun i n -> (n, env'.offset - ((i + 1) * 8)))
varnames }
in
appstr buf @@ aux env (NonTail, lhs) ;
appstr buf @@ gen_assign_pattern_or_raise env' bind ;
env'
| LetFunc (_, funcname, _, _, _) ->
let offset = new_offset env' 1 in
let env' =
{offset; varoffset= Hashmap.add funcname offset env'.varoffset}
in
env'
| _ -> raise Unexpected_ast
in
let env' = List.fold_left (fun env le -> aux' env le) env lets in
appstr buf @@ aux env' (istail, rhs_of_in) ;
Buffer.contents buf
| _, MakeCls (funcname, nargs, freevars) ->
let buf = Buffer.create 128 in
appstr buf @@ gen_alloc_block (List.length freevars + 2) 0 247 ;
appfmt buf "lea rdi, [rip + %s]" funcname ;
appstr buf "mov [rax], rdi" ;
appfmt buf "mov QWORD PTR [rax + 8], %d" @@ nargs ;
List.iteri
(fun i var ->
let offset = Hashmap.find var env.varoffset in
appfmt buf "mov rdi, [rbp + %d]" offset ;
appfmt buf "mov [rax + %d], rdi" ((i + 2) * 8) )
freevars ;
appstr buf "push rax" ;
Buffer.contents buf
| istail, MatchWith (cond, cases) ->
let buf = Buffer.create 256 in
appstr buf "/* MatchWith BEGIN */" ;
appstr buf @@ aux env (NonTail, cond) ;
appstr buf
@@ gen_pattern_match_cases env cases istail
(let buf = Buffer.create 128 in
appstr buf "mov rax, 1" ;
(* TODO: arguments for Match_failure *)
appstr buf @@ gen_raise_exp_of "Stdlib.Match_failure" true ;
Buffer.contents buf) ;
appstr buf "/* MatchWith END */" ;
Buffer.contents buf
| istail, TryWith (cond, cases) ->
let offset = new_offset env 1 in
let env = {env with offset} in
let exp_label = make_label () in
let exit_label = make_label () in
let buf = Buffer.create 256 in
appstr buf "/* TryWith BEGIN */" ;
(* set an exception handler *)
appfmt buf "lea r13, [rip + %s]" exp_label ;
appstr buf "push rbp" ;
appstr buf "push r13" ;
appstr buf "push r14" ;
appstr buf "mov r14, rsp" ;
appstr buf @@ aux env (NonTail, cond) ;
appstr buf "pop rax" ;
appstr buf "pop r14 /* pop for r14 */" ;
appstr buf "pop rbx /* pop for r13 */" ;
appstr buf "pop rbx /* pop for rbp */" ;
appstr buf "push rax" ;
appfmt buf "jmp %s" exit_label ;
appfmt buf "%s:" exp_label ;
appstr buf "pop rbp" ;
appfmt buf "mov [rbp + %d], rax" offset ;
appstr buf "push rax" ;
appstr buf
@@ gen_pattern_match_cases env cases istail
(let buf = Buffer.create 128 in
appfmt buf "mov rax, [rbp + %d]" offset ;
appstr buf @@ gen_raise () ;
Buffer.contents buf) ;
appfmt buf "%s:" exit_label ;
appstr buf "/* TryWith END */" ;
Buffer.contents buf
| _ -> raise Unexpected_ast
in
let strings_code =
let buf = Buffer.create 80 in
appfmt buf ".data" ;
List.iter
(function
| StringValue (id, str) ->
let size = (String.length str / 8) + 1 in
let space = 7 - (String.length str mod 8) in
appfmt buf ".quad %d" ((size lsl 10) lor (0 lsl 8) lor 252) ;
appfmt buf "%s:" id ;
appfmt buf ".ascii \"%s\"" (escape_string str) ;
if space <> 0 then appfmt buf ".space %d" space ;
appfmt buf ".byte %d\n" space
| _ -> raise Unexpected_ast)
strings ;
appfmt buf ".text\n" ;
Buffer.contents buf
in
let letfuncs_code =
String.concat "\n"
(List.map
(function
| LetFunc (recursive, funcname, args, func, freevars) ->
let varnames =
List.flatten @@ List.map varnames_in_pattern args
in
let env =
{ offset= List.length varnames * -8
; varoffset=
integrate Hashmap.empty @@ hashmap_of_list
@@ List.mapi (fun i arg -> (arg, -8 * (i + 1))) varnames
}
in
(* if closured then freevars are stored on the stack *)
let env =
{ offset= env.offset - (8 * List.length freevars)
; varoffset=
integrate env.varoffset @@ hashmap_of_list
@@ List.mapi
(fun i var -> (var, env.offset - (8 * (i + 1))))
freevars }
in
stack_size := -env.offset ;
let code = aux env (Tail, func) in
let buf = Buffer.create 256 in
appfmt buf "/* %s(%d) */"
(if recursive then "recursive" else "")
(List.length args) ;
appstr buf @@ funcname ^ ":" ;
appstr buf @@ "push rbp" ;
appstr buf @@ "mov rbp, rsp" ;
appfmt buf "sub rsp, %d" !stack_size ;
(* push arguments in order *)
(* first the value for closure *)
if List.length freevars <> 0 then
appfmt buf "push %s" @@ reg_of_index @@ List.length args ;
(* then real arguments *)
appstr buf @@ String.concat "\n" @@ List.rev
@@ List.mapi
(fun i _ -> sprintf "push %s" (reg_of_index i))
args ;
(* process real arguments *)
appstr buf @@ String.concat "\n"
@@ List.map
(fun ptn -> gen_assign_pattern_or_raise env ptn)
args ;
(* process for closure *)
if List.length freevars > 0 then (
appstr buf "pop rax" ;
List.iteri
(fun i var ->
appfmt buf "mov rdi, [rax + %d]" (i * 8) ;
appfmt buf "mov [rbp + %d], rdi"
@@ Hashmap.find var env.varoffset )
freevars ) ;
appstr buf code ;
appstr buf "pop rax" ;
appstr buf "mov rsp, rbp" ;
appstr buf "pop rbp" ;
appstr buf "ret\n\n" ;
Buffer.contents buf
| _ -> failwith "LetFunc should be here")
letfuncs)
in
let main_code =
let buf = Buffer.create 512 in
let gen_c_func funcname argument_types ret_type =
appfmt buf "%s:" funcname ;
appstr buf save_rsp ;
List.iteri
(fun i -> function
| CTyInt | CTyUnit -> appstr buf @@ untag_int @@ reg_of_index i
| _ -> () )
argument_types ;
for i = List.length argument_types - 1 downto 0 do
appfmt buf "mov %s"
@@
match i with
| 0 -> "rdi, rax"
| 1 -> "rsi, rbx"
| 2 -> "rdx, rdi"
| 3 -> "rcx, rsi"
| 4 -> "r8, rdx"
| 5 -> "r9, rcx"
| _ ->
failwith "C function with more than 6 arguments can't be handled."
done ;
appstr buf @@ gen_call_with_aligned_rsp @@ sprintf "%s_detail@PLT" funcname ;
(* appfmt buf "call %s_detail@PLT" funcname ; *)
( match ret_type with
| CTyInt -> appstr buf @@ tag_int "rax"
| CTyUnit -> appfmt buf "mov rax, %d" @@ tagged_int 0
| CTyPtr -> () ) ;
appstr buf "ret" ; appstr buf ""
in
gen_c_func "aqaml_structural_equal" [CTyPtr; CTyPtr] CTyInt ;
gen_c_func "aqaml_concat_string" [CTyPtr; CTyPtr] CTyPtr ;
gen_c_func "aqaml_concat_list" [CTyPtr; CTyPtr] CTyPtr ;
gen_c_func "aqaml_string_length" [CTyPtr] CTyInt ;
gen_c_func "aqaml_string_get" [CTyPtr; CTyInt] CTyInt ;
gen_c_func "aqaml_string_set" [CTyPtr; CTyInt; CTyInt] CTyInt ;
gen_c_func "aqaml_array_get" [CTyPtr; CTyInt] CTyPtr ;
gen_c_func "aqaml_array_length" [CTyPtr] CTyInt ;
gen_c_func "aqaml_string_create" [CTyInt] CTyPtr ;
gen_c_func "aqaml_string_blit"
[CTyPtr; CTyInt; CTyPtr; CTyInt; CTyInt]
CTyUnit ;
gen_c_func "aqaml_string_sub" [CTyPtr; CTyInt; CTyInt] CTyPtr ;
gen_c_func "aqaml_string_make" [CTyInt; CTyInt] CTyPtr ;
gen_c_func "aqaml_string_of_int" [CTyInt] CTyPtr ;
gen_c_func "aqaml_print_string" [CTyPtr] CTyUnit ;
gen_c_func "aqaml_prerr_string" [CTyPtr] CTyUnit ;
gen_c_func "aqaml_printf_ksprintf" [CTyPtr; CTyPtr] CTyPtr ;
appstr buf
".global aqaml_printf_ksprintf1, aqaml_printf_ksprintf2, \
aqaml_printf_ksprintf3, aqaml_printf_ksprintf4, aqaml_printf_ksprintf5" ;