-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.ml
952 lines (889 loc) · 34.5 KB
/
generate.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
open Ast
open Whitespace
open Lib.Symbol
exception Spacebar_Exception
exception Duplicate_Function_Definition
exception Symbol_Not_Found
exception Function_Not_Found
(* An entry in the symbol table. *)
type symbol =
| Global of {scope: int; name: string; offset: int}
| Function of {scope: int; name: string; label: int}
| Argument of {scope: int; fn_name: string; name: string; offset: int}
| Variable of {scope: int; name: string; offset: int}
(* The current state of the code generator. Stores the list of generated opcodes
and the symbol table. The symbol table is a list of symbols, this isn't the
most efficient structure, but its easy. *)
type state =
{ ops: imp list
; symbol_table: symbol_table
; iter_stmt_start_label: int option
; iter_stmt_end_label: int option }
(* Wrap Symbol.add_label *)
let add_label_s state =
let label, symbol_table = add_label state.symbol_table in
({state with symbol_table}, label)
(* Wrap Symbol.push_scope *)
let push_scope_s state =
let symbol_table = push_scope state.symbol_table in
{state with symbol_table}
(* Wrap Symbol.pop_scope *)
let pop_scope_s state =
let symbol_table = pop_scope state.symbol_table in
{state with symbol_table}
(* Wrap Symbol.add_func *)
let add_func_s state name =
let label, symbol_table = add_func state.symbol_table name in
({state with symbol_table}, label)
(* Wrap Symbol.find_func *)
let find_func_s state name =
let label = find_func state.symbol_table name in
match label with Some l -> l | None -> raise Spacebar_Exception
(* Wrap Symbol.add_func_arg*)
let add_func_arg_s state func_name arg_name size =
let offset, symbol_table =
add_func_arg state.symbol_table func_name arg_name size
in
({state with symbol_table}, offset)
(* Wrap Symbol.add_local_var*)
let add_local_var_s state name size =
let offset, symbol_table = add_local_var state.symbol_table name size in
({state with symbol_table}, offset)
(* Wrap Symbol.find_offset*)
let find_offset_s state name =
let offset = find_offset state.symbol_table name in
offset
(* Emit a new opcode and return the resulting state. *)
let emit_opcode state opcode = {state with ops= opcode :: state.ops}
(* Try and reduce an assignment expression to a constant value. Used for e.g.
compile time determination of the size of arrays. TODO: This is terrible,
clean this up! *)
let constant_from_assignment_expr assignment_expr =
match assignment_expr with
| AssignmentConditionalExpression x -> (
match x with
| ContitionalLogicalOrExpression x -> (
match x with
| LogicalOrLogicalAndExpression x -> (
match x with
| InclusiveOrExpression x -> (
match x with
| ExclusiveOr x -> (
match x with
| AndExpression x -> (
match x with
| EqualityExpression x -> (
match x with
| RelationalExpression x -> (
match x with
| ShiftExpression x -> (
match x with
| AdditiveExpression x -> (
match x with
| MultiplicativeExpression x -> (
match x with
| CastExpression x -> (
match x with
| PostfixExpression x -> (
match x with
| PrimaryExpression x -> (
match x with
| Constant x -> x
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception
(* Return the identifier from a declarator. *)
let rec identifier (declarator : declarator) =
let rec match_direct_declarator (direct_declarator : direct_declarator) =
match direct_declarator with
| Identifier x -> x
| Declarator x -> identifier x
| ArrayDeclarator x -> match_direct_declarator x.direct_declarator
| FunctionDeclarator x -> match_direct_declarator x.direct_declarator
in
match_direct_declarator declarator.direct_declarator
(* Store a value on the stack relative to rsp. *)
let store_rsp_rel state =
let ops =
[ StackManipulation (Push 0)
; HeapAccess Retrieve
; StackManipulation Swap
; HeapAccess Store
; StackManipulation (Push 0)
; HeapAccess Retrieve
; StackManipulation (Push 1)
; Arithmetic Addtion
; StackManipulation (Push 0)
; StackManipulation Swap
; HeapAccess Store ]
in
List.fold_left emit_opcode state ops
(* Take a stack with top [offset, value] and store value in (rbp + offset). *)
let store_stack_offset state =
let ops =
[ StackManipulation (Push 1)
; HeapAccess Retrieve
; Arithmetic Addtion
; StackManipulation Swap
; HeapAccess Store ]
in
List.fold_left emit_opcode state ops
(* Store the value on top of the stack at [rbp + offset] *)
let store_rbp_rel state offset =
let ops =
[ StackManipulation (Push 1)
; HeapAccess Retrieve
; StackManipulation (Push offset)
; Arithmetic Addtion
; StackManipulation Swap
; HeapAccess Store ]
in
List.fold_left emit_opcode state ops
(* Load a value relative to rbp. *)
let load_rbp_rel state offset =
let ops =
[ StackManipulation (Push 1)
; HeapAccess Retrieve
; StackManipulation (Push offset)
; Arithmetic Addtion
; HeapAccess Retrieve ]
in
List.fold_left emit_opcode state ops
(* Load a value from memory using the offset at the top of the stack. *)
let load_rbp_rel_stack state =
let ops =
[ StackManipulation (Push 1)
; HeapAccess Retrieve
; Arithmetic Addtion
; HeapAccess Retrieve ]
in
List.fold_left emit_opcode state ops
(* Push a value offset relative to rbp onto the stack. *)
let push_rbp_rel_offset state offset =
let ops =
[ StackManipulation (Push 1)
; HeapAccess Retrieve
; StackManipulation (Push offset)
; Arithmetic Addtion ]
in
List.fold_left emit_opcode state ops
(* Store the current value of rsp in rbp. *)
let set_rsp state =
let ops =
[ StackManipulation (Push 0)
; HeapAccess Retrieve
; StackManipulation (Push 1)
; StackManipulation Swap
; HeapAccess Store ]
in
List.fold_left emit_opcode state ops
(* Push the value of rbp onto the stack. *)
let push_rbp state =
let state =
List.fold_left emit_opcode state
[StackManipulation (Push 1); HeapAccess Retrieve]
in
store_rsp_rel state
(* Load the value of rbp from the stack. This is executed as part of a function
epilogue, where the value of rbp is expected at (rsp - 1). *)
let pop_rbp state =
(* Load the value of rsp. *)
let state = load_rbp_rel state (-1) in
let ops =
[StackManipulation (Push 1); StackManipulation Swap; HeapAccess Store]
in
List.fold_left emit_opcode state ops
(* Adjust the stack pointer by `value`. *)
let add_rsp state value =
let ops =
[ StackManipulation (Push 0)
; HeapAccess Retrieve
; StackManipulation (Push value)
; StackManipulation Swap
; HeapAccess Store ]
in
List.fold_left emit_opcode state ops
(* Store the current value of rsp in rbp. *)
let store_rsp state =
let ops =
[ StackManipulation (Push 0)
; HeapAccess Retrieve
; StackManipulation (Push 1)
; StackManipulation Swap
; HeapAccess Store ]
in
List.fold_left emit_opcode state ops
(* Subtract a value from the stack pointer. *)
let sub_rsp state value =
let ops =
[ StackManipulation (Push 0)
; HeapAccess Retrieve
; StackManipulation (Push value)
; Arithmetic Subtraction
; StackManipulation (Push 0)
; StackManipulation Swap
; HeapAccess Store ]
in
List.fold_left emit_opcode state ops
(* Store the current value of rbp in rsp. *)
let restore_rsp state =
let ops =
[ StackManipulation (Push 1)
; HeapAccess Retrieve
; StackManipulation (Push 0)
; StackManipulation Swap
; HeapAccess Store ]
in
List.fold_left emit_opcode state ops
(* Push the absolute address of [rbp + offset]. This is functionally equivilent
to the x86 LEA instruction with the register fixed to rbp*)
let push_abs_addr state offset =
(* Load rbp to the top of the stack. *)
let ops =
[ StackManipulation (Push 1)
; HeapAccess Retrieve (* Load rbp *)
; StackManipulation (Push offset)
; Arithmetic Addtion ]
(* Add offset to get abs address. *)
in
List.fold_left emit_opcode state ops
(* Emit the built-in function geti and return the function label. *)
let emit_geti state =
(* Add the function to the symbol table and emit the function label. *)
let state, label = add_func_s state "geti" in
let state = emit_opcode state (FlowControl (Mark label)) in
(* Load the output location and read the number. *)
let state = load_rbp_rel state (-2) in
List.fold_left emit_opcode state [IO ReadNumber; FlowControl EndSubroutine]
(* Emit the built-in function puti and return the function label. *)
let emit_puti state =
(* Add the function to the symbol table and emit the function label. *)
let state, label = add_func_s state "puti" in
let state = emit_opcode state (FlowControl (Mark label)) in
(* Load and output the argument. *)
let state = load_rbp_rel state (-2) in
List.fold_left emit_opcode state [IO OutputNumber; FlowControl EndSubroutine]
(* Emit the built-in function putc and return the function label. *)
let emit_putc state =
(* Add the function to the symbol table and emit the function label. *)
let state, label = add_func_s state "putc" in
let state = emit_opcode state (FlowControl (Mark label)) in
(* Load and output the argument. *)
let state = load_rbp_rel state (-2) in
List.fold_left emit_opcode state
[IO OutputCharacter; FlowControl EndSubroutine]
(* Return an identifier from a postfix expression. *)
let id_from_postfix postfix_expr =
match postfix_expr with
| PrimaryExpression x -> (
match x with IdentifierExpr x' -> x' | _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception
(* Emit opcodes for a primary expression. *)
let rec emit_primary_expression state expr lvalue =
match expr with
| IdentifierExpr x ->
let offset = find_offset_s state x in
if lvalue then emit_opcode state (StackManipulation (Push offset))
else load_rbp_rel state offset
| Constant x -> emit_opcode state (StackManipulation (Push x))
| Expression x -> emit_expression state x
| _ -> state
(* Emit a postfix expression. *)
and emit_postfix_expression state expr lvalue =
match expr with
| PrimaryExpression x -> emit_primary_expression state x lvalue
| ArrayAccess x ->
(* Emit the index expression. *)
let state = emit_expression state x.expression in
(* Get the offset of the array start. *)
let offset =
unary_expr_offset state (PostfixExpression x.postfix_expression)
in
(* Add the index to the offset. *)
let state =
List.fold_left emit_opcode state
[StackManipulation (Push offset); Arithmetic Addtion]
in
if lvalue then state else load_rbp_rel_stack state
| FunctionCall x ->
(* Store the function arguments relative to the stack pointer. *)
let rec process_arguments state arguments =
match arguments with
| [] -> state
| h :: t ->
(* Compute the expression the place the result on top of the stack. *)
let state = emit_assignment_expression state h in
let state = store_rsp_rel state in
process_arguments state t
in
(* Assume our postfix expression is an identifier. *)
let function_name = id_from_postfix x.postfix_expression in
(* Push the function arguments on the stack. *)
let state =
process_arguments state (List.rev x.argument_expression_list)
in
(* Store the current frame pointer on the stack. *)
let state = push_rbp state in
(* Set the frame pointer for the callee. *)
let state = set_rsp state in
(* Find the function label and call the function. *)
let label = find_func_s state function_name in
let state = emit_opcode state (FlowControl (Call label)) in
(* Restore the frame and stack pointers.rbp and rsp *)
let state = restore_rsp state in
let state = pop_rbp state in
sub_rsp state 1
| PostfixIncrement x ->
let offset =
unary_expr_offset state (PostfixExpression x.postfix_expression)
in
let state = load_rbp_rel state offset in
let state =
List.fold_left emit_opcode state
[ StackManipulation Duplicate
; StackManipulation (Push 1)
; Arithmetic Addtion
; StackManipulation (Push offset) ]
in
store_stack_offset state
| PostfixDecrement x ->
let offset =
unary_expr_offset state (PostfixExpression x.postfix_expression)
in
let state = load_rbp_rel state offset in
let state =
List.fold_left emit_opcode state
[ StackManipulation Duplicate
; StackManipulation (Push 1)
; Arithmetic Subtraction
; StackManipulation (Push offset) ]
in
store_stack_offset state
| _ -> state
(* Lookup the offset of a unary expression in the symbol table. *)
and unary_expr_offset state expr =
match expr with
| PostfixExpression x -> (
match x with
| PrimaryExpression x' -> (
match x' with
| IdentifierExpr x'' -> find_offset_s state x''
| _ -> raise Spacebar_Exception )
| ArrayAccess {postfix_expression= expr; _} ->
unary_expr_offset state (PostfixExpression expr)
| _ -> raise Spacebar_Exception )
| _ -> raise Spacebar_Exception
and emit_unary_expression state expr lvalue =
match expr with
| PostfixExpression x -> emit_postfix_expression state x lvalue
| PrefixIncrement x ->
let offset = unary_expr_offset state x.unary_expression in
let state = load_rbp_rel state offset in
let state =
List.fold_left emit_opcode state
[ StackManipulation (Push 1)
; Arithmetic Addtion
; StackManipulation Duplicate
; StackManipulation (Push offset) ]
in
store_stack_offset state
| PrefixDecrement x ->
let offset = unary_expr_offset state x.unary_expression in
let state = load_rbp_rel state offset in
let state =
List.fold_left emit_opcode state
[ StackManipulation (Push 1)
; Arithmetic Subtraction
; StackManipulation Duplicate
; StackManipulation (Push offset) ]
in
store_stack_offset state
| UnaryOperator x -> (
match x.operator with
| AddressOf _ ->
(* Look up the offset of the lvalue expression from rbp. *)
let offset = unary_expr_offset state x.unary_expression in
(* Emit the absolute address of the expression. *)
push_abs_addr state offset
| PointerDereference _ ->
(* Evaluate the expression so the address is on top of the stack. *)
let state = emit_unary_expression state x.unary_expression lvalue in
(* Load the value at the absolute address. *)
emit_opcode state (HeapAccess Retrieve)
| UnaryPlus _ ->
(* noop *)
state
| UnaryMinus _ ->
let state = emit_opcode state (StackManipulation (Push 0)) in
let state = emit_unary_expression state x.unary_expression lvalue in
emit_opcode state (Arithmetic Subtraction)
| UnaryBitwiseNot _ -> state
| UnaryNot _ -> state )
and emit_multiplicative_expression state expr =
match expr with
| CastExpression x -> emit_unary_expression state x false
| MultiplicativeProduct x ->
let state =
emit_multiplicative_expression state x.multiplicative_expression
in
let state = emit_unary_expression state x.cast_expression false in
emit_opcode state (Arithmetic Multiplication)
| MultiplicativeDivision x ->
let state =
emit_multiplicative_expression state x.multiplicative_expression
in
let state = emit_unary_expression state x.cast_expression false in
emit_opcode state (Arithmetic Division)
| MultiplicativeRemainder x ->
let state =
emit_multiplicative_expression state x.multiplicative_expression
in
let state = emit_unary_expression state x.cast_expression false in
emit_opcode state (Arithmetic Modulo)
and emit_additive_expression state expr =
match expr with
| MultiplicativeExpression x -> emit_multiplicative_expression state x
| AdditiveAdditionExpression x ->
let state = emit_additive_expression state x.additive_expression in
let state =
emit_multiplicative_expression state x.multiplicative_expression
in
emit_opcode state (Arithmetic Addtion)
| AdditiveSubtractionExpression x ->
let state = emit_additive_expression state x.additive_expression in
let state =
emit_multiplicative_expression state x.multiplicative_expression
in
emit_opcode state (Arithmetic Subtraction)
and emit_shift_expression state expr =
match expr with
| AdditiveExpression x -> emit_additive_expression state x
| _ -> state
and emit_relational_expression state expr =
match expr with
| ShiftExpression x -> emit_shift_expression state x
| LessThanExpression x ->
(* True if (lhs - rhs) is negative. *)
let state, negative_label = add_label_s state in
let state, end_label = add_label_s state in
let state = emit_relational_expression state x.relational_expression in
let state = emit_shift_expression state x.shift_expression in
List.fold_left emit_opcode state
[ Arithmetic Subtraction
; FlowControl (JumpNegative negative_label)
; StackManipulation (Push 0)
; FlowControl (UnconditionalJump end_label)
; FlowControl (Mark negative_label)
; StackManipulation (Push 1)
; FlowControl (Mark end_label) ]
| GreaterThanExpression x ->
(* True if (lhs - rhs) is NOT negative. *)
let state, negative_label = add_label_s state in
let state, zero_label = add_label_s state in
let state, end_label = add_label_s state in
let state = emit_relational_expression state x.relational_expression in
let state = emit_shift_expression state x.shift_expression in
List.fold_left emit_opcode state
[ Arithmetic Subtraction
; StackManipulation Duplicate
; FlowControl (JumpNegative negative_label)
; FlowControl (JumpZero zero_label)
; StackManipulation (Push 1)
; FlowControl (UnconditionalJump end_label)
; FlowControl (Mark negative_label)
; StackManipulation Discard
; FlowControl (Mark zero_label)
; StackManipulation (Push 0)
; FlowControl (Mark end_label) ]
| LessThanEqualThanExpression x ->
let state = emit_relational_expression state x.relational_expression in
let state = emit_shift_expression state x.shift_expression in
state
| GreaterThanEqualExpression x ->
let state = emit_relational_expression state x.relational_expression in
let state = emit_shift_expression state x.shift_expression in
state
and emit_equality_expression state expr =
match expr with
| RelationalExpression x -> emit_relational_expression state x
| EqualToExpression x ->
let state, zero_label = add_label_s state in
let state, end_label = add_label_s state in
let state = emit_equality_expression state x.equality_expression in
let state = emit_relational_expression state x.relational_expression in
List.fold_left emit_opcode state
[ Arithmetic Subtraction
; FlowControl (JumpZero zero_label)
; StackManipulation (Push 0)
; FlowControl (UnconditionalJump end_label)
; FlowControl (Mark zero_label)
; StackManipulation (Push 1)
; FlowControl (Mark end_label) ]
| NotEqualToExpression x ->
let state, zero_label = add_label_s state in
let state, end_label = add_label_s state in
let state = emit_equality_expression state x.equality_expression in
let state = emit_relational_expression state x.relational_expression in
List.fold_left emit_opcode state
[ Arithmetic Subtraction
; FlowControl (JumpZero zero_label)
; StackManipulation (Push 1)
; FlowControl (UnconditionalJump end_label)
; FlowControl (Mark zero_label)
; StackManipulation (Push 0)
; FlowControl (Mark end_label) ]
and emit_and_expression state expr =
match expr with
| EqualityExpression x -> emit_equality_expression state x
| _ -> state
and emit_exclusive_or_expression state expr =
match expr with AndExpression x -> emit_and_expression state x | _ -> state
and emit_inclusive_or_expression state expr =
match expr with
| ExclusiveOr x -> emit_exclusive_or_expression state x
| _ -> state
and emit_logical_and_expression state expr =
match expr with
| InclusiveOrExpression x -> emit_inclusive_or_expression state x
| LogicalAndExpression x ->
let state, zero_label = add_label_s state in
let state, end_label = add_label_s state in
let state = emit_logical_and_expression state x.logical_and_expression in
let state = emit_opcode state (FlowControl (JumpZero zero_label)) in
let state =
emit_inclusive_or_expression state x.inclusive_or_expression
in
List.fold_left emit_opcode state
[ FlowControl (JumpZero zero_label)
; StackManipulation (Push 1)
; FlowControl (UnconditionalJump end_label)
; FlowControl (Mark zero_label)
; StackManipulation (Push 0)
; FlowControl (Mark end_label) ]
and emit_logical_or_expression state expr =
match expr with
| LogicalOrLogicalAndExpression x -> emit_logical_and_expression state x
| LogicalOrExpression x ->
let state, rhs_label = add_label_s state in
let state, end_label = add_label_s state in
let state = emit_logical_or_expression state x.logical_or_expression in
let state =
List.fold_left emit_opcode state
[ FlowControl (JumpZero rhs_label)
; StackManipulation (Push 1)
; FlowControl (UnconditionalJump end_label)
; FlowControl (Mark rhs_label) ]
in
let state = emit_logical_and_expression state x.logical_and_expression in
emit_opcode state (FlowControl (Mark end_label))
and emit_conditional_expression state expr =
match expr with
| ContitionalLogicalOrExpression x -> emit_logical_or_expression state x
| _ -> state
and emit_assignment_expression state expr =
match expr with
| AssignmentConditionalExpression x -> emit_conditional_expression state x
| AssignmentOperation x ->
let state =
match x.assignment_operator with
| Assign _ ->
(* Evalulate lhs expression to determine the l-value. Evaluation of
this expression should leave the address of the value on top of
the stack. At the moment, we can only assign to local variables. *)
(* Emit rhs first. *)
let state =
emit_assignment_expression state x.assignment_expression
in
(* Make sure stack pointer relative offset is on top of the current
stack*)
let state = emit_unary_expression state x.unary_expression true in
(* Store the value. *)
store_stack_offset state
in
state
and emit_expression state (expr : expression) =
match expr with AssignmentExpression x -> emit_assignment_expression state x
let rec emit_block_item state (block_item : block_item) =
match block_item with
| Declaration x -> emit_declaration state x
| Statement x -> emit_statement state x
and emit_statement state (statement : statement) =
match statement with
| LabeledStatement _ -> state
| CompoundStatement x ->
let state = push_scope_s state in
let state = List.fold_left emit_block_item state x in
pop_scope_s state
| ExpressionStatement x -> (
match x with Some x' -> emit_expression state x' | None -> state )
| SelectionStatement x -> (
match x with
| If x' ->
(* Label for conditional jump. *)
let state, skip_condition_label = add_label_s state in
(* Evaluate expression and jump if false. *)
let state = emit_expression state x'.expression in
let state =
emit_opcode state (FlowControl (JumpZero skip_condition_label))
in
(* Emit body of condition. *)
let state = emit_statement state x'.body in
(* Mark label for skipping conditional body. *)
emit_opcode state (FlowControl (Mark skip_condition_label))
| IfElse _ -> state
| Switch _ -> state )
| IterationStatement x -> (
match x with
| While x' ->
(* Labels for condition and end. *)
let state, condition_label = add_label_s state in
let state, end_label = add_label_s state in
let state =
{ state with
iter_stmt_end_label= Some end_label
; iter_stmt_start_label= Some condition_label }
in
(* Mark start of loop, before expression. *)
let state = emit_opcode state (FlowControl (Mark condition_label)) in
(* Evaluate expression. *)
let state = emit_expression state x'.expression in
let state = emit_opcode state (FlowControl (JumpZero end_label)) in
(* Emit body. *)
let state = emit_statement state x'.body in
(* Unconditional jump. *)
List.fold_left emit_opcode state
[ FlowControl (UnconditionalJump condition_label)
; FlowControl (Mark end_label) ]
| DoWhile x' ->
(* Labels for condition and end. *)
let state, body_label = add_label_s state in
let state, end_label = add_label_s state in
let state =
{ state with
iter_stmt_end_label= Some end_label
; iter_stmt_start_label= Some body_label }
in
(* Mark start of loop, before expression. *)
let state = emit_opcode state (FlowControl (Mark body_label)) in
(* Emit body. *)
let state = emit_statement state x'.body in
(* Evaluate expression. *)
let state = emit_expression state x'.expression in
List.fold_left emit_opcode state
[ FlowControl (JumpZero end_label)
; FlowControl (UnconditionalJump body_label)
; FlowControl (Mark end_label) ]
| For x' ->
(* Push a new block scope. *)
let state = push_scope_s state in
let state, loop_start_label = add_label_s state in
let state, loop_end_label = add_label_s state in
let state =
{ state with
iter_stmt_end_label= Some loop_end_label
; iter_stmt_start_label= Some loop_start_label }
in
(* Emit loop variable declaration. *)
let state =
match x'.init_clause with
| Some x'' -> (
match x'' with
| ForInitExpr x''' -> emit_expression state x'''
| ForInitDecl x''' -> emit_declaration state x''' )
| None -> state
in
(* Mark start of loop, before condition. *)
let state = emit_opcode state (FlowControl (Mark loop_start_label)) in
(* Evaluate condition. *)
let state =
match x'.condition with
| Some x'' -> emit_expression state x''
| None -> state
in
(* Jump if condition is not valid. *)
let state = emit_opcode state (FlowControl (JumpZero loop_end_label)) in
(* Emit body. *)
let state = emit_statement state x'.body in
(* Evaluate iteration statement. *)
let state =
match x'.iteration with
| Some x'' -> emit_expression state x''
| None -> state
in
(* Unconditional jump back to condition. *)
let state =
emit_opcode state (FlowControl (UnconditionalJump loop_start_label))
in
(* End label *)
let state = emit_opcode state (FlowControl (Mark loop_end_label)) in
(* Pop the loop block scope. *)
pop_scope_s state )
| JumpStatement x -> (
match x with
| Goto _ -> state
| Continue -> (
(* Unconditionally jump to iteration statement condition label. *)
match state.iter_stmt_start_label with
| Some label -> emit_opcode state (FlowControl (UnconditionalJump label))
| None -> raise Spacebar_Exception )
| Break -> (
(* Unconditionally jump to the currently active iteration statement end
label. *)
match state.iter_stmt_end_label with
| Some label -> emit_opcode state (FlowControl (UnconditionalJump label))
| None -> raise Spacebar_Exception )
| Return x' -> (
match x'.expression with
| Some x'' ->
let state = emit_expression state x'' in
emit_opcode state (FlowControl EndSubroutine)
| None -> emit_opcode state (FlowControl EndSubroutine) ) )
(* Every function gets a fixed size amount of stack space for locals. *)
and calc_func_stack_size = 8
and reserve_stack_space state size =
let rec do_reserve state size =
match size with
| 0 -> state
| n ->
let state = emit_opcode state (StackManipulation (Push 0)) in
let state = store_rsp_rel state in
do_reserve state (n - 1)
in
do_reserve state size
(* Emit a function definition. *)
and emit_fn_def state (fn_def : function_definition) =
(* Add the function to the symbol table. *)
let fn_name = identifier fn_def.declarator in
let state, label = add_func_s state fn_name in
(* Add the function arguments to the symbol table. *)
let state =
match fn_def.declarator.direct_declarator with
| FunctionDeclarator x ->
(* Extract argument names. *)
let args =
List.map
(fun (y : parameter_declaration) -> identifier y.declarator)
x.parameter_list
in
(* Add arguments to symbol table. *)
List.fold_left
(fun acc x ->
let state, _ = add_func_arg_s acc fn_name x 1 in
state )
state args
| _ -> state
in
(* Mark the function. *)
let state = emit_opcode state (FlowControl (Mark label)) in
(* Push a new scope and emit the function definition. *)
let state = push_scope_s state in
(* Calculate stack space required for local variables. *)
let state = reserve_stack_space state calc_func_stack_size in
(* Allocate stack space for local variables. *)
let state = List.fold_left emit_block_item state fn_def.compound_statement in
let state = emit_opcode state (FlowControl EndSubroutine) in
(* Pop the function scope. *)
pop_scope_s state
(* Return true if a declaration is an array declaration. *)
and is_array_decl (decl : init_declarator) =
match decl.declarator.direct_declarator with
| ArrayDeclarator _ -> true
| _ -> false
(* Emit a declaration, returning the new state of the generator. *)
and emit_declaration state declaration =
(* Emit a non-array declarator, returning the new state of the generator. *)
let emit_non_array_init_declarator state (init_declarator : init_declarator) =
(* Emit the (optional) initializer. *)
let state, has_init =
match init_declarator._initializer with
| Some x -> (emit_assignment_expression state x, true)
| None -> (state, false)
in
(* Add the identifier to the symbol table. *)
let id = identifier init_declarator.declarator in
let state, offset = add_local_var_s state id 1 in
(* Store the initial value if we had one. *)
if has_init then store_rbp_rel state offset else state
in
(* Emit an array declarator, returning the new state of the generator. *)
let emit_array_init_decl state (init_decl : init_declarator) =
(* Most Whitespace interpreters use a hash table like structure for tracking
stack memory and therefore don't do so well when reading memory locations
that have not previously been written. To work around this we 'allocate'
an array on the stack by zeroing out the amount of stack space we want. *)
let rec stack_allocate state size =
match size with
| 0 -> state
| n ->
let state = emit_opcode state (StackManipulation (Push 0)) in
let state = store_rsp_rel state in
stack_allocate state (n - 1)
in
let name = identifier init_decl.declarator in
(* Assume array sizes are constant primary expressions. *)
let size =
match init_decl.declarator.direct_declarator with
| ArrayDeclarator x ->
constant_from_assignment_expr x.assignment_expression
| _ ->
Printf.eprintf "Unsupported array size expression" ;
raise Spacebar_Exception
in
let state, _ = add_local_var_s state name size in
stack_allocate state size
in
let array_decl, non_array_decl =
List.partition (fun x -> is_array_decl x) declaration.init_declarator_list
in
(* Handle normal declarations. *)
let state =
List.fold_left emit_non_array_init_declarator state non_array_decl
in
(* Handle array declarations. *)
List.fold_left emit_array_init_decl state array_decl
let emit_external_declaration state ast =
match ast with
| FunctionDefinition x -> emit_fn_def state x
| Declaration x -> emit_declaration state x
(* Emit program prolog. Set up rsp and rbp, call main then end the program. *)
let emit_prog_prolog state =
let ops =
[ StackManipulation (Push 0)
; StackManipulation (Push 2)
; HeapAccess Store
; StackManipulation (Push 1)
; StackManipulation (Push 2)
; HeapAccess Store
; FlowControl (Call 0)
; FlowControl EndProgram ]
in
List.fold_left emit_opcode state ops
(* Emit bytecode for built in functions, whether we call them or not. *)
let emit_build_ins state =
let state = emit_puti state in
let state = emit_putc state in
emit_geti state
(* Generate Whitespace bytecode from an abstract syntax tree. *)
let generate (ast : external_declaration list) =
let state =
{ ops= []
; symbol_table= new_symbol_table
; iter_stmt_end_label= None
; iter_stmt_start_label= None }
in
let state = emit_prog_prolog state in
let state = emit_build_ins state in
let state = List.fold_left emit_external_declaration state ast in
List.rev state.ops