-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx86_codegen.ml
More file actions
executable file
·1333 lines (1223 loc) · 50.7 KB
/
x86_codegen.ml
File metadata and controls
executable file
·1333 lines (1223 loc) · 50.7 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
(*
*
* Generate x86 code.
*
* ----------------------------------------------------------------
*
* @begin[license]
* Copyright (C) Kai Chen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* @end[license]
*)
open Field_table
open Symbol
open Fj_fir
open Fj_fir_exn
open Fj_fir_pos
open Fj_fir_env
open Fj_fir_type
open Fj_fir_check
open X86_inst
open X86_frame
open X86_frame.X86Frame
module Pos = MakePos (struct let name = "X86_codegen" end)
open Pos
module type CodegenSig =
sig
val build_prog : Fj_fir.prog -> X86_frame.prog
end
module X86Codegen : CodegenSig =
struct
(************************************************************************
* UTILITIES
************************************************************************)
(*
* Build a block from the instruction list.
* We'll compute the jumps later.
*)
let block_of_insts label insts =
{ block_label = label;
block_code = Listbuf.to_list insts;
block_jumps = []
}
(*
* "Block" types correspond to pointers.
*)
let is_pointer_type env pos ty =
match expand_type env pos ty with
TyUnit
| TyBool
| TyChar
| TyInt
| TyFloat ->
false
| TyNil
| TyString
| TyArray _
| TyFun _
| TyMethod _
| TyRecord _
| TyId _
| TyNames _ ->
true
(*
* test if i is power of 2
*
* Implemented by: turtles
*)
let is_power2 i =
let rec search k =
if k > i || k = (1 lsl 30) then
false
else
k = i || search (k lsl 1)
in
search 1
(*
* compute log2 i
*
* Implemented by: turtles
*)
let log2 i =
let rec search j =
let k = 1 lsl j in
if k = i then
j
else
search (succ j)
in
search 0
(************************************************************************
* FUNCTION TABLES
************************************************************************)
(*
* Information we keep for each function.
*)
type fun_info =
{ fun_closure : var;
fun_vars : var list;
fun_pointers : var list;
fun_reserve : int
}
(*
* Environment for codegeneration. We save:
* 1. The GC info for each function
* 2. Label offsets
*)
type info =
{ info_funs : fun_info SymbolTable.t;
info_labels : int SymbolTable.t;
info_rttd : (label * int list) SymbolTable.t
}
(*
* Fetch info.
*)
let info_mem_fun info f =
SymbolTable.mem info.info_funs f
let info_lookup_fun info pos f =
try SymbolTable.find info.info_funs f with
Not_found ->
raise (FirException (pos, UnboundVar f))
let info_lookup_closure info f =
try (SymbolTable.find info.info_funs f).fun_closure with
Not_found ->
f
let info_lookup_label info pos label =
try SymbolTable.find info.info_labels label with
Not_found ->
raise (FirException (pos, UnboundLabel label))
let info_lookup_rttd info pos label =
try SymbolTable.find info.info_rttd label with
Not_found ->
raise (FirException (pos, UnboundLabel label))
(*
* Scan the types for record definitions, and save the offset
* of each label.
*)
let rec label_type env pos (renv, lenv) ty =
match ty with
TyUnit
| TyNil
| TyBool
| TyChar
| TyString
| TyInt
| TyFloat
| TyNames _
| TyId _ ->
renv, lenv
| TyArray ty ->
label_type env pos (renv, lenv) ty
| TyFun (ty_vars, ty_res) ->
List.fold_left (label_type env pos) (renv, lenv) (ty_res :: ty_vars)
| TyMethod (ty_this, ty_vars, ty_res) ->
List.fold_left (label_type env pos) (renv, lenv) (ty_this :: ty_res :: ty_vars)
| TyRecord (rclass, fields) ->
let pos = string_pos "label_type" pos in
let off =
match rclass with
RecordFrame
| RecordObject ->
1
| RecordClass
| RecordMethods ->
0
in
(* Build the RTTD and label environment *)
let fields = FieldTable.to_list fields in
let rttd, lenv, _ =
List.fold_left (fun (rttd, lenv, off) (v, ty) ->
let rttd =
if is_pointer_type env pos ty then
off :: rttd
else
rttd
in
let lenv = SymbolTable.add lenv v off in
let off = succ off in
rttd, lenv, off) ([], lenv, off) fields
in
(* Save the RTTD at each label *)
let renv =
match fields with
(v, _) :: _ ->
let label = new_symbol_string (Symbol.to_string v ^ "_rttd") in
SymbolTable.add renv v (label, rttd)
| [] ->
renv
in
renv, lenv
let label_types env tenv =
SymbolTable.fold (fun rlenv v ty ->
let pos = string_pos "label_types" (var_exp_pos v) in
label_type env pos rlenv ty) (SymbolTable.empty, SymbolTable.empty) tenv
(*
* Count up the maximum allocation in the body
* of the function.
*)
let sizeof_vobject = sizeof_header + 2 * sizeof_field
let sizeof_closure = sizeof_header + 2 * sizeof_field
let sizeof_maxint_string = sizeof_header + 12
let sizeof_strcat_pair = sizeof_header + 2 * sizeof_field
let rec alloc_exp pos e =
match e with
TailCall _
| MethodCall _ ->
0
| LetAtom (_, _, _, e)
| LetUnop (_, _, _, _, e)
| LetBinop (_, _, _, _, _, e)
| LetSubscript (_, _, _, _, e)
| SetSubscript (_, _, _, _, e)
| LetProject (_, _, _, _, e)
| SetProject (_, _, _, _, e) ->
alloc_exp pos e
| IfThenElse (_, e1, e2) ->
let size1 = alloc_exp pos e1 in
let size2 = alloc_exp pos e2 in
max size1 size2
| IfType (_, _, _, e1, e2) ->
(* Add size of a vobject allocation to the first branch *)
let size1 = alloc_exp pos e1 + sizeof_vobject in
let size2 = alloc_exp pos e2 in
max size1 size2
| LetArray (_, _, [AtomInt i], _, e) ->
(* Array includes a header *)
alloc_exp pos e + i * sizeof_field + sizeof_header
| LetArray (_, _, _, _, e) ->
raise (FirException (pos, StringError "multimensional and variable-sized arrays not implemented"))
| LetRecord (_, _, _, fields, e) ->
alloc_exp pos e + (succ (FieldTable.cardinal fields) * sizeof_field) + sizeof_header
| LetClosure (_, _, _, _, e) ->
alloc_exp pos e + sizeof_closure
| LetExt (_, _, s, _, _, e) ->
let size = alloc_exp pos e in
(match s with
"itoa" -> size + sizeof_maxint_string
| "strcat" -> size + sizeof_strcat_pair
| _ -> size)
| LetFuns _
| LetVar _
| SetVar _ ->
raise (FirException (pos, FirLevel2))
(*
* For each function, save:
* 1. the params of the fun
* 2. the params that are pointers
* 3. the max allocation (in words)
*)
let build_info prog =
let { prog_types = tenv;
prog_funs = funs
} = prog
in
let env = env_of_prog prog in
let renv, lenv = label_types env tenv in
let fenv =
SymbolTable.mapi (fun f (_, ty, vars, body) ->
let pos = string_pos "build_fenv" (var_exp_pos f) in
let ty_vars, _ = dest_fun_or_method_type env pos ty in
let pointers =
List.fold_left2 (fun pointers v ty ->
if is_pointer_type env pos ty then
v :: pointers
else
pointers) [] vars ty_vars
in
let size = alloc_exp pos body in
{ fun_closure = new_symbol_string (Symbol.to_string f ^ "_closure");
fun_vars = vars;
fun_pointers = pointers;
fun_reserve = size
}) funs
in
{ info_labels = lenv;
info_rttd = renv;
info_funs = fenv
}
(************************************************************************
* CODE GENERATION
************************************************************************)
(*
* A global initializer.
* Implemented by: jyh
*)
let build_init info a =
match a with
AtomUnit
| AtomNil ->
InitNumber 0
| AtomBool b ->
InitNumber (if b then 1 else 0)
| AtomChar c ->
InitNumber (Char.code c)
| AtomInt i ->
InitNumber i
| AtomFloat _ ->
raise (Failure "code_atom: floats not implemented")
| AtomVar v ->
(* Functions get mapped *)
InitLabel (info_lookup_closure info v)
(*
* Operand for an atom.
* Implemented by: jyh
*)
let build_atom a =
match a with
AtomUnit
| AtomNil ->
ImmediateNumber 0
| AtomBool b ->
ImmediateNumber (if b then 1 else 0)
| AtomChar c ->
ImmediateNumber (Char.code c)
| AtomInt i ->
ImmediateNumber i
| AtomFloat _ ->
raise (Failure "code_atom: floats not implemented")
| AtomVar v ->
Register v
(*
* Build the assembly for an expression.
* Implemented by: jyh
*)
let rec build_exp info blocks insts e =
let pos = string_pos "build_exp" (exp_pos e) in
let insts = Listbuf.add insts (CommentFIR e) in
match e with
LetAtom (v, ty, a, e) ->
build_atom_exp info blocks insts pos v a e
| LetUnop (v, ty, op, a, e) ->
build_unop_exp info blocks insts pos v op a e
| LetBinop (v, ty, op, a1, a2, e) ->
build_binop_exp info blocks insts pos v op a1 a2 e
| IfThenElse (a, e1, e2) ->
build_if_exp info blocks insts pos a e1 e2
| IfType (a, name, v, e1, e2) ->
build_iftype_exp info blocks insts pos a name v e1 e2
| LetSubscript (v, ty, a1, a2, e) ->
build_subscript_exp info blocks insts pos v a1 a2 e
| SetSubscript (a1, a2, _, a3, e) ->
build_set_subscript_exp info blocks insts pos a1 a2 a3 e
| LetProject (v, ty, a, label, e) ->
build_project_exp info blocks insts pos v a label e
| SetProject (a1, label, _, a2, e) ->
build_set_project_exp info blocks insts pos a1 label a2 e
| TailCall (f, args) ->
build_tailcall_exp info blocks insts pos f args
| MethodCall (f, a, args) ->
build_methodcall_exp info blocks insts pos f a args
| LetClosure (v, _, f, a, e) ->
build_closure_exp info blocks insts pos v f a e
| LetRecord (v, _, rclass, fields, e) ->
build_record_exp info blocks insts pos v rclass fields e
| LetArray (v, _, dimens, a, e) ->
build_array_exp info blocks insts pos v dimens a e
| LetExt (v, _, s, _, args, e) ->
build_ext_exp info blocks insts pos v s args e
| LetFuns _
| LetVar _
| SetVar _ ->
raise (FirException (pos, FirLevel2))
(*
* An atom assignment.
* Implemented by: jyh
*)
and build_atom_exp info blocks insts pos v a e =
let pos = string_pos "build_atom_exp" pos in
let insts = Listbuf.add insts (MOV (Register v, build_atom a)) in
build_exp info blocks insts e
(*
* A unary operation.
* Implemented by: jyh
*)
and build_unop_exp info blocks insts pos v op a e =
let pos = string_pos "build_unop_exp" pos in
let a = build_atom a in
let insts = Listbuf.add insts (MOV (Register v, a)) in
let inst =
match op with
UMinusIntOp ->
NEG (Register v)
| UNotIntOp
| UNotBoolOp ->
(* modified by turtles to flip only the LSB *)
XOR (Register v, ImmediateNumber 1)
| UCharOfInt ->
AND (Register v, ImmediateNumber 255)
| UIntOfChar ->
(*
* Interprete as unsigned characters.
* This NOP is silly. The code can be restructured to
* remove it, but optimization will remove it anyway.
*)
NOP
| UMinusFloatOp
| UIntOfFloat
| UFloatOfInt ->
raise (FirException (pos, StringError "floats not implemented"))
in
let insts = Listbuf.add insts inst in
build_exp info blocks insts e
(*
* A comparison.
*
* For some architectures, SET only works on real registers.
* So we first SET ecx and then move the result to dst.
* Since SET only sets the lowest 8 bits, we need to clear ecx first
*
* Implemented by: turtles
*)
and build_compare insts dst op a1 a2 =
let tmp = new_symbol_string "cmp_tmp" in
let insts = Listbuf.add insts (XOR (Register ecx, Register ecx)) in
let insts = Listbuf.add insts (MOV (Register tmp, a1)) in
let insts = Listbuf.add insts (CMP (Register tmp, a2)) in
let insts = Listbuf.add insts (SET (op, Register ecx)) in
let insts = Listbuf.add insts (MOV (dst, Register ecx)) in
insts
(*
* A shift. Variable shifts have to use cl for the shift.
* Implemented by: jyh
*)
and build_shift insts dst op a1 a2 =
match a2 with
ImmediateNumber _ ->
let insts = Listbuf.add insts (MOV (dst, a1)) in
let insts = Listbuf.add insts (op dst a2) in
insts
| _ ->
let insts = Listbuf.add insts (MOV (dst, a1)) in
let insts = Listbuf.add insts (MOV (Register ecx, a2)) in
let insts = Listbuf.add insts (op dst (Register ecx)) in
insts
(*
* A binary operation.
* This part includes some simple strength reduction.
*
* Implemented by: turtles
*)
and build_binop_exp info blocks insts pos v op a1 a2 e =
let pos = string_pos "build_binop_exp" pos in
let a1 = build_atom a1 in
let a2 = build_atom a2 in
let dst = Register v in
let insts =
match op with
(* reduce addition by 0 *)
AddIntOp ->
(match a1, a2 with
(* if a1 = 0, dst <- a2 *)
ImmediateNumber 0, _ ->
Listbuf.add insts (MOV (dst, a2))
(* if a2 = 0, dst <- a1 *)
| _, ImmediateNumber 0 ->
Listbuf.add insts (MOV (dst, a1))
(* otherwise, we do not do any reduction *)
| _ ->
let insts = Listbuf.add insts (MOV (dst, a1)) in
let insts = Listbuf.add insts (ADD (dst, a2)) in
insts)
(* reduce subtraction by 0 *)
| SubIntOp ->
(match a2 with
(* if a2 = 0, dst <- a1 *)
ImmediateNumber 0 ->
Listbuf.add insts (MOV (dst, a1))
(* otherwise, no reduction *)
| _ ->
let insts = Listbuf.add insts (MOV (dst, a1)) in
let insts = Listbuf.add insts (SUB (dst, a2)) in
insts)
(* reduce multiply by 0, by 1, or by power of 2 *)
| MulIntOp ->
(* dst of IMUL must be register *)
let unreduced_insts = Listbuf.add insts (MOV (Register ecx, a1)) in
let unreduced_insts = Listbuf.add unreduced_insts (IMUL (Register ecx, a2)) in
let unreduced_insts = Listbuf.add unreduced_insts (MOV (dst, Register ecx)) in
(match a1, a2 with
(* if a1 = 0, or a2 = 0 *)
ImmediateNumber 0, _
| _, ImmediateNumber 0 ->
Listbuf.add insts (MOV (dst, ImmediateNumber 0))
(* if a1 = 1 *)
| ImmediateNumber 1, _ ->
Listbuf.add insts (MOV (dst, a2))
(* if a2 = 1 *)
| _, ImmediateNumber 1 ->
Listbuf.add insts (MOV (dst, a1))
(* now test for power of 2 case *)
| ImmediateNumber i, a2 ->
(* if a1 is power2, shift a2 left by log2 a1 *)
if is_power2 i then
let x = log2 i in
let op = fun r1 r2 -> SHL (r1, r2) in
build_shift insts dst op a2 (ImmediateNumber x)
else
(match a2 with
ImmediateNumber i ->
(* if a2 is power2, shift a1 left by log2 a2 *)
if is_power2 i then
let x = log2 i in
let op = fun r1 r2 -> SHL (r1, r2) in
build_shift insts dst op a1 (ImmediateNumber x)
(* no reduction *)
else
unreduced_insts
(* no reduction *)
| _ ->
unreduced_insts)
| a1, ImmediateNumber i ->
(* if a2 is power2, shift a1 left by log2 a2 *)
if is_power2 i then
let x = log2 i in
let op = fun r1 r2 -> SHL (r1, r2) in
build_shift insts dst op a1 (ImmediateNumber x)
(* no reduction *)
else
unreduced_insts
(* no reduction *)
| _, _ ->
unreduced_insts)
(* reduce a1=0, a2=1, a2=-1, or a1>0 && a2 power2 *)
| DivIntOp ->
let tmp = new_symbol_string "div_op2" in
let unreduced_insts = Listbuf.add insts (MOV (Register eax, a1)) in
let unreduced_insts = Listbuf.add unreduced_insts CDQ in
let unreduced_insts = Listbuf.add unreduced_insts (MOV (Register tmp, a2)) in
let unreduced_insts = Listbuf.add unreduced_insts (IDIV (Register tmp)) in
let unreduced_insts = Listbuf.add unreduced_insts (MOV (dst, Register eax)) in
(match a1, a2 with
(* in this case, we know it's divide by 0 exception *)
_, ImmediateNumber 0 ->
raise (Failure "divide by 0 exception")
(* if a1 = 0, dst <- 0
* this may get by some divide by 0 exception, but we
* can't tell at compile time. *)
| ImmediateNumber 0, _ ->
Listbuf.add insts (MOV (dst, ImmediateNumber 0))
(* if a2 = 1, dst <- a1 *)
| _, ImmediateNumber 1 ->
Listbuf.add insts (MOV (dst, a1))
(* if a2 = -1, dst <- -a1, this gives fewer instructions *)
| _, ImmediateNumber -1 ->
let insts = Listbuf.add insts (MOV (dst, a1)) in
let insts = Listbuf.add insts (NEG dst) in
insts
(* be careful, idiv->sar reduction only works for positive numbers *)
| ImmediateNumber i, ImmediateNumber j ->
(* use SAR for efficiency *)
if i > 0 && is_power2 j then
let x = log2 j in
let op = fun r1 r2 -> SAR (r1, r2) in
build_shift insts dst op a1 (ImmediateNumber x)
(* otherwise no reduction *)
else
unreduced_insts
(* otherwise no reduction *)
| _, _ ->
unreduced_insts)
(* reduce a1=0, a2=1, a2=-1, or a1>0 && a2 power2 *)
| RemIntOp ->
let tmp = new_symbol_string "rem_op2" in
let unreduced_insts = Listbuf.add insts (MOV (Register eax, a1)) in
let unreduced_insts = Listbuf.add unreduced_insts CDQ in
let unreduced_insts = Listbuf.add unreduced_insts (MOV (Register tmp, a2)) in
let unreduced_insts = Listbuf.add unreduced_insts (IDIV (Register tmp)) in
let unreduced_insts = Listbuf.add unreduced_insts (MOV (dst, Register edx)) in
(match a1, a2 with
(* in this case, we know it's divide by 0 exception *)
_, ImmediateNumber 0 ->
raise (Failure "divide by 0 exception")
(* if a1 = 0, dst <- 0.
* this may get by some divide by 0 exception, but we
* can't tell at compile time. *)
| ImmediateNumber 0, _ ->
Listbuf.add insts (MOV (dst, ImmediateNumber 0))
(* if a2 = 1 or a2 = -1, dst <- 0 *)
| _, ImmediateNumber 1
| _, ImmediateNumber -1 ->
Listbuf.add insts (MOV (dst, ImmediateNumber 0))
(* if a2 is power2, we can use AND a1, a2-1 *)
| ImmediateNumber i, ImmediateNumber j ->
if i > 0 && is_power2 j then
let insts = Listbuf.add insts (MOV (dst, a1)) in
let insts = Listbuf.add insts (AND (dst, ImmediateNumber (pred j))) in
insts
else
unreduced_insts
(* no reduction *)
| _, _ ->
unreduced_insts)
(* reduce a1 = 0, a2 = 0, a1 = -1, a2 = -1 *)
| AndIntOp ->
(match a1, a2 with
(* if a1 = 0, or a2 = 0, dst <- 0 *)
ImmediateNumber 0, _
| _, ImmediateNumber 0 ->
Listbuf.add insts (MOV (dst, ImmediateNumber 0))
(* if a1 = -1, dst <- a2 *)
| ImmediateNumber -1, _ ->
Listbuf.add insts (MOV (dst, a2))
(* if a2 = -1, dst <- a1 *)
| _, ImmediateNumber -1 ->
Listbuf.add insts (MOV (dst, a1))
(* otherwise, no reduction *)
| _, _ ->
let insts = Listbuf.add insts (MOV (dst, a1)) in
let insts = Listbuf.add insts (AND (dst, a2)) in
insts)
(* reduce a1 = 0, a2 = 0, a1 = -1, a2 = -1 *)
| OrIntOp ->
(match a1, a2 with
(* if a1 = -1 or a2 = -1, dst <- -1 *)
ImmediateNumber -1, _
| _, ImmediateNumber -1 ->
Listbuf.add insts (MOV (dst, ImmediateNumber (-1)))
(* if a1 = 0, dst <- a2 *)
| ImmediateNumber 0, _ ->
Listbuf.add insts (MOV (dst, a2))
(* if a2 = 0, dst <- a1 *)
| _, ImmediateNumber 0 ->
Listbuf.add insts (MOV (dst, a1))
(* otherwise, no reduction *)
| _, _ ->
let insts = Listbuf.add insts (MOV (dst, a1)) in
let insts = Listbuf.add insts (OR (dst, a2)) in
insts)
(* reduce a1 = 0, a2 = 0 *)
| LslIntOp ->
(match a1, a2 with
(* if either a1 or a2 is 0, SHL is nop *)
ImmediateNumber 0, _
| _, ImmediateNumber 0 ->
Listbuf.add insts (MOV (dst, a1))
(* otherwise no reduction *)
| _, _ ->
let op = fun r1 r2 -> SHL (r1, r2) in
build_shift insts dst op a1 a2)
(* reduce a1 = 0, a2 = 0 *)
| LsrIntOp ->
(match a1, a2 with
(* if either a1 or a2 is 0, SHR is nop *)
| ImmediateNumber 0, _
| _, ImmediateNumber 0 ->
Listbuf.add insts (MOV (dst, a1))
(* otherwise no reduction *)
| _, _ ->
let op = fun r1 r2 -> SHR (r1, r2) in
build_shift insts dst op a1 a2)
(* reduce a1 = 0, a1 = -1, a2 = 0 *)
| AsrIntOp ->
(match a1, a2 with
(* if a1 = 0, or a1 = -1, or a2 = 0, SAR is nop *)
| ImmediateNumber 0, _
| ImmediateNumber (-1), _
| _, ImmediateNumber 0 ->
Listbuf.add insts (MOV (dst, a1))
(* otherwise no reduction *)
| _, _ ->
let op = fun r1 r2 -> SAR (r1, r2) in
build_shift insts dst op a1 a2)
(* reduce a1 = 0, a2 = 0 *)
| XorIntOp ->
(match a1, a2 with
(* if either a1 or a2 is 0, XOR is nop *)
ImmediateNumber 0, _ ->
Listbuf.add insts (MOV (dst, a2))
| _, ImmediateNumber 0 ->
Listbuf.add insts (MOV (dst, a1))
(* otherwise no reduction *)
| _, _ ->
let insts = Listbuf.add insts (MOV (dst, a1)) in
let insts = Listbuf.add insts (XOR (dst, a2)) in
insts)
| EqIntOp
| EqBoolOp ->
build_compare insts dst EQ a1 a2
| NeqIntOp
| NeqBoolOp ->
build_compare insts dst NEQ a1 a2
| LeIntOp ->
build_compare insts dst LE a1 a2
| LtIntOp ->
build_compare insts dst LT a1 a2
| GtIntOp ->
build_compare insts dst GT a1 a2
| GeIntOp ->
build_compare insts dst GE a1 a2
| AddFloatOp
| SubFloatOp
| MulFloatOp
| DivFloatOp
| RemFloatOp
| EqFloatOp
| NeqFloatOp
| LeFloatOp
| LtFloatOp
| GtFloatOp
| GeFloatOp ->
raise (FirException (pos, StringError "floats not implemented"))
| EqPtrOp
| NeqPtrOp ->
raise (FirException (pos, StringError "ptr not implemented"))
in
build_exp info blocks insts e
(*
* For a condition, test the atom and branch.
* Implemented by: turtles
*)
and build_if_exp info blocks insts pos a e1 e2 =
let pos = string_pos "build_if_exp" pos in
let a = build_atom a in
(* build the branches *)
let true_label = new_symbol_string "true_case" in
let false_label = new_symbol_string "false_case" in
let blocks, true_insts = build_exp info blocks Listbuf.empty e1 in
let blocks, false_insts = build_exp info blocks Listbuf.empty e2 in
let true_block = block_of_insts true_label true_insts in
let false_block = block_of_insts false_label false_insts in
let blocks = true_block :: false_block :: blocks in
(* test the atom and branch *)
let tmp = new_symbol_string "if_tmp" in
let insts = Listbuf.add insts (MOV (Register tmp, a)) in
let insts = Listbuf.add insts (CMP (Register tmp, ImmediateNumber 0)) in
let insts = Listbuf.add insts (JCC (NEQ, true_label)) in
let insts = Listbuf.add insts (JMP (false_label)) in
blocks, insts
(*
* The iftype requires searching the name table.
* This function is especially difficult; we have to construct
* a loop that walks through the name table.
* Implemented by: jyh
*)
and build_iftype_exp info blocks insts pos a name v e1 e2 =
let pos = string_pos "build_iftype_exp" pos in
let a = build_atom a in
(* Build the branches *)
let label1 = new_symbol_string "true_case" in
let label2 = new_symbol_string "false_case" in
let blocks, insts1 = build_exp info blocks Listbuf.empty e1 in
let blocks, insts2 = build_exp info blocks Listbuf.empty e2 in
let block1 = block_of_insts label1 insts1 in
let block2 = block_of_insts label2 insts2 in
let blocks = block1 :: block2 :: blocks in
(* The match case *)
let match_label = new_symbol_string "match" in
let names_reg = new_symbol_string "names_ptr" in
let name_reg = new_symbol_string "name" in
let insts_match = Listbuf.empty in
let insts_match = Listbuf.add insts_match (MOV (Register name_reg, MemRegOff (names_reg, sizeof_field))) in
let insts_match = Listbuf.add insts_match (CMP (Register name_reg, ImmediateNumber 0)) in
let insts_match = Listbuf.add insts_match (JCC (EQ, label1)) in
let insts_match = Listbuf.add insts_match (LEA (Register v, MemRegOff (mem_next, sizeof_header))) in
let insts_match = Listbuf.add insts_match (MOV (MemRegOff (v, header_off), ImmediateNumber headerof_vobject)) in
let insts_match = Listbuf.add insts_match (MOV (MemReg v, Register name_reg)) in
let insts_match = Listbuf.add insts_match (MOV (MemRegOff (v, sizeof_field), a)) in
let insts_match = Listbuf.add insts_match (ADD (Register mem_next, ImmediateNumber sizeof_vobject)) in
let insts_match = Listbuf.add insts_match (JMP label1) in
let blocks = block_of_insts match_label insts_match :: blocks in
(* The name loop *)
let loop_label = new_symbol_string "names_loop" in
let insts_loop = Listbuf.empty in
let insts_loop = Listbuf.add insts_loop (MOV (Register name_reg, MemReg names_reg)) in
let insts_loop = Listbuf.add insts_loop (CMP (Register name_reg, ImmediateCLabel name)) in
let insts_loop = Listbuf.add insts_loop (JCC (EQ, match_label)) in
let insts_loop = Listbuf.add insts_loop (CMP (Register name_reg, ImmediateNumber 0)) in
let insts_loop = Listbuf.add insts_loop (JCC (EQ, label2)) in
let insts_loop = Listbuf.add insts_loop (ADD (Register names_reg, ImmediateNumber (2 * sizeof_field))) in
let insts_loop = Listbuf.add insts_loop (JMP loop_label) in
let blocks = block_of_insts loop_label insts_loop :: blocks in
(* Generate the test *)
let insts = Listbuf.add insts (MOV (Register v, a)) in
let insts = Listbuf.add insts (MOV (Register names_reg, MemRegOff (v, sizeof_field))) in
let insts = Listbuf.add insts (MOV (Register names_reg, MemReg names_reg)) in
let insts = Listbuf.add insts (JMP loop_label) in
blocks, insts
(*
* Array subscripting.
* Check that the array is not nil, and that the index is in bounds.
* Implemented by: turtles
*)
and build_subscript_exp info blocks insts pos v a1 a2 e =
let pos = string_pos "build_subscript_exp" pos in
let a1 = build_atom a1 in
let a2 = build_atom a2 in
let v_array = new_symbol_string "let_sub_array" in
let v_index = new_symbol_string "let_sub_index" in
let size = new_symbol_string "let_sub_size" in
(* check that array is not nil *)
let insts = Listbuf.add insts (MOV (Register v_array, a1)) in
let insts = Listbuf.add insts (CMP (Register v_array, ImmediateNumber 0)) in
let insts = Listbuf.add insts (JCC (EQ, seg_fault_label)) in
(* check if the index is in bounds *)
let insts = Listbuf.add insts (MOV (Register size, MemRegOff(v_array, header_off))) in
let insts = Listbuf.add insts (SHR (Register size, ImmediateNumber size_shift)) in
let insts = Listbuf.add insts (AND (Register size, ImmediateNumber size_mask)) in
let insts = Listbuf.add insts (MOV (Register v_index, a2)) in
(* size is in bytes, but index is in words ... so we need to divide size by 4
* the following is a hack for efficiency. we rely on the assumption that sizeof_field
* is a power of 2 *)
let insts = Listbuf.add insts (SAR (Register size, ImmediateNumber (log2 sizeof_field))) in
let insts = Listbuf.add insts (CMP (Register size, Register v_index)) in
let insts = Listbuf.add insts (JCC (ULE, seg_fault_label)) in
(* fetch array element *)
let insts = Listbuf.add insts (MOV (Register v, MemRegRegOffMul(v_array, v_index, 0, sizeof_field))) in
build_exp info blocks insts e
(*
* Assign a subscript.
* Check that the array is not nil, and that the index is in bounds.
* Implemented by: turtles
*)
and build_set_subscript_exp info blocks insts pos a1 a2 a3 e =
let pos = string_pos "build_set_subscript_exp" pos in
let a1 = build_atom a1 in
let a2 = build_atom a2 in
let a3 = build_atom a3 in
let v_array = new_symbol_string "set_sub_array" in
let v_index = new_symbol_string "set_sub_index" in
let size = new_symbol_string "set_sub_size" in
(* check that array is not nil *)
let insts = Listbuf.add insts (MOV (Register v_array, a1)) in
let insts = Listbuf.add insts (CMP (Register v_array, ImmediateNumber 0)) in
let insts = Listbuf.add insts (JCC (EQ, seg_fault_label)) in
(* check if the index is in bounds *)
let insts = Listbuf.add insts (MOV (Register size, MemRegOff(v_array, header_off))) in
let insts = Listbuf.add insts (SHR (Register size, ImmediateNumber size_shift)) in
let insts = Listbuf.add insts (AND (Register size, ImmediateNumber size_mask)) in
let insts = Listbuf.add insts (MOV (Register v_index, a2)) in
(* same here, need to divide size by 4 first *)
let insts = Listbuf.add insts (SAR (Register size, ImmediateNumber (log2 sizeof_field))) in
let insts = Listbuf.add insts (CMP (Register size, Register v_index)) in
let insts = Listbuf.add insts (JCC (ULE, seg_fault_label)) in
(* set array element *)
(* let tmp = new_symbol_string "set_sub_tmp" in *)
(* let insts = Listbuf.add insts (MOV (Register tmp, a3)) in *)
let insts = Listbuf.add insts (MOV (MemRegRegOffMul(v_array, v_index, 0, sizeof_field), a3)) in
build_exp info blocks insts e
(*
* Record projection.
* Check that the record is not nil.
* Don't have to worry about bounds checking.
* Note that you can get the index into the block with
* the function info_lookup_label.
*
* Implemented by: turtles
*)
and build_project_exp info blocks insts pos v a label e =
let pos = string_pos "build_project_exp" pos in
let a = build_atom a in
let index = info_lookup_label info pos label in
let v_record = new_symbol_string "let_proj_record" in
(* check that the record is not nil *)
let insts = Listbuf.add insts (MOV (Register v_record, a)) in
let insts = Listbuf.add insts (CMP (Register v_record, ImmediateNumber 0)) in
let insts = Listbuf.add insts (JCC (EQ, seg_fault_label)) in
(* fetch record field *)
let insts = Listbuf.add insts (MOV (Register v, MemRegOff(v_record, index * sizeof_field))) in
build_exp info blocks insts e
(*
* Record projection.
* Check that the record is not nil.
* Implemented by: turtles
*)
and build_set_project_exp info blocks insts pos a1 label a2 e =
let pos = string_pos "build_set_project_exp" pos in
let a1 = build_atom a1 in
let a2 = build_atom a2 in
let index = info_lookup_label info pos label in
let v_record = new_symbol_string "set_proj_record" in
(* check that the record is not nil *)
let insts = Listbuf.add insts (MOV (Register v_record, a1)) in
let insts = Listbuf.add insts (CMP (Register v_record, ImmediateNumber 0)) in
let insts = Listbuf.add insts (JCC (EQ, seg_fault_label)) in
(* set a record field *)
let tmp = new_symbol_string "set_proj_tmp" in
let insts = Listbuf.add insts (MOV (Register tmp, a2)) in
let insts = Listbuf.add insts (MOV (MemRegOff(v_record, index * sizeof_field), Register tmp)) in
build_exp info blocks insts e
(*
* A function call. If this is not a real function,
* it has to be a closure. In this case, we put the arguments
* in the "standard" places, including the environment as the
* first arg.
*)
and build_tailcall_exp info blocks insts pos f args =
let pos = string_pos "build_tailcall_exp" pos in
if info_mem_fun info f then
build_normal_call info blocks insts pos f args
else
build_closure_call info blocks insts pos f args
(*
* For a normal tailcall, we generate the MOV instructions
* to put the args in the right place.
* Implemented by: turtles
*)
and build_normal_call info blocks insts pos f args =
let pos = string_pos "build_normal_call" pos in
let {fun_vars = vars} = info_lookup_fun info pos f in
let args = List.map build_atom args in
(* allocate a list of tmp registers *)
let tmp_vars =
let rec build_list tmp_vars i =
if i = List.length args then
tmp_vars
else
build_list (new_symbol_string "tmp_var" :: tmp_vars) (succ i)
in
build_list [] 0
in
(* we first move all arguments to tmp registers *)
let insts =
List.fold_left2 (fun insts tmp_var a ->
Listbuf.add insts (MOV (Register tmp_var, a))) insts tmp_vars args
in
(* now move arguments to the right place *)
let insts =
List.fold_left2 (fun insts v tmp_var ->
Listbuf.add insts (MOV (Register v, Register tmp_var))) insts vars tmp_vars
in
(* and jump, here we use direct jump *)