-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstage3b_verify.txt
2933 lines (2670 loc) · 98.7 KB
/
stage3b_verify.txt
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
============================================
vis/zequnm.out
17:34:10_Monday_22_May_2017
============================================
COMP90045 project - Test of semantic analysis and code generation.
User: zequnm
--- Makefile found ---
--- Running Makefile ---
ocamlopt -g -c snick_ast.ml
ocamlyacc snick_parse.mly
ocamlc -g -c snick_ast.ml
ocamlc -c snick_parse.mli
ocamlopt -g -c snick_parse.ml
ocamllex snick_lex.mll
96 states, 5679 transitions, table size 23292 bytes
ocamlopt -g -c snick_lex.ml
ocamlopt -g -c snick_pprint.ml
ocamlopt -g -c snick_br_ast.ml
ocamlopt -g -c snick_symbol.ml
ocamlopt -g -c snick_err.ml
ocamlopt -g -c snick_optimizer.ml
ocamlopt -g -c snick_analyze.ml
ocamlopt -g -c snick_codegen.ml
ocamlopt -g -c snick.ml
ocamlopt -g -o snick snick_ast.cmx snick_lex.cmx snick_parse.cmx snick_pprint.cmx snick_br_ast.cmx snick_symbol.cmx snick_err.cmx snick_optimizer.cmx snick_analyze.cmx snick_codegen.cmx snick.cmx
--- Succeeded ---
PLEASE NOTE: These cases are very basic and are not intended to be
comprehensive. Passing these does not guarantee a correct compiler!
--- Running basic test of each milestone ---
Milestone 1: Expressions and write statement
PASS
Milestone 2: Read and assignment statements
PASS
Milestone 3: If and while statements
PASS
Milestone 4: Procedure arguments and calls (by-value only)
PASS
Milestone 5: By-reference arguments
PASS
Milestone 6: Structures
PASS
Milestone 7: Semantic errors
PASS:
Compiler output:
Fatal error: exception Failure("Semantic Error: Arguement types mismatch for calling proc 'p' in proc: main")
--- Running simple tests ---
vis/*.snick)
bell: PASS
gcd: PASS
hail: PASS
power: PASS
sort: PASS
stddev: PASS
trig: PASS
tute_question_38: PASS
tute_question_40: PASS
tute_question_41: PASS
tute_question_42: PASS
11/11 cases passed
--- Running peer-contributed tests ---
(contributed/*.snick)
#classTeamName=metajoke: PASS
123: PASS
GGWP: PASS
Mainframe: PASS
OHarrytheCaml: PASS
PSZ: PASS
Shimada Brothers: PASS
SuperCaptain: PASS
candidate: PASS
cantparseus: PASS
eclipse: PASS
nechiko: PASS
snark: PASS
teamFoobar: PASS
zz: PASS
15/15 cases passed
--- End of testing for zequnm ---\n
============================================
src/snick_br_ast.ml
17:33:48_Monday_22_May_2017
============================================
(*
** File: snick_br_ast.ml
** Description: Specification of the abstract syntax tree for Snick,
** also provides functions for printin out a brill program.
** Last Modified: Mon. 15th May 2017
**
** Group name: Mainframe
**
** Member names | usernames
** Xianzhuo REN | xianzhuor
** Haoyu LIN | haoyul3
** Zequn MA | zequnm
*)
open Format
(* Available operations in Brill *)
type opType =
| OpCall of string
| OpHalt
| OpReturn
| OpIntConst of (int * int)
| OpRealConst of (int * float)
| OpStringConst of (int * string)
| OpDebugReg of int
| OpDebugSlot of int
| OpDebugStack
(*unop*)
| OpPush of int
| OpPop of int
| OpBranchUncond of int
(*binop*)
| OpLoad of (int * int)
| OpStore of (int * int)
| OpLoadAddress of (int * int)
| OpLoadIndirect of (int * int)
| OpStoreIndirect of (int * int)
| OpBranchOnTrue of (int * int)
| OpBranchOnFalse of (int * int)
| OpIntToReal of (int * int)
| OpNot of (int * int)
(*triop*)
| OpOr of (int * int * int)
| OpAnd of (int * int * int)
| OpAddInt of (int * int * int)
| OpSubInt of (int * int * int)
| OpMulInt of (int * int * int)
| OpDivInt of (int * int * int)
| OpCmpEqInt of (int * int * int)
| OpCmpNeInt of (int * int * int)
| OpCmpLtInt of (int * int * int)
| OpCmpLeInt of (int * int * int)
| OpCmpGtInt of (int * int * int)
| OpCmpGeInt of (int * int * int)
| OpAddReal of (int * int * int)
| OpSubReal of (int * int * int)
| OpMulReal of (int * int * int)
| OpDivReal of (int * int * int)
| OpCmpEqReal of (int * int * int)
| OpCmpNeReal of (int * int * int)
| OpCmpLtReal of (int * int * int)
| OpCmpLeReal of (int * int * int)
| OpCmpGtReal of (int * int * int)
| OpCmpGeReal of (int * int * int)
| OpSubOffset of (int * int * int)
(* Types of builtin calls *)
type bltInType =
| BltInReadInt
| BltInReadReal
| BltInReadBool
| BltInPrintInt
| BltInPrintReal
| BltInPrintBool
| BltInPrintString
(* Representation of a line in a brill program *)
type brLine =
| BrProc of string
| BrOp of opType
| BrLabel of int
| BrBltIn of bltInType
| BrComment of string
type brLines = brLine list option
type brProg = brLines
let indent = " "
let width = -19
(* Print lines of brill program *)
let rec print_prog prog = List.iter print_line prog
(* Print a single line depending on its type *)
and print_line = function
| BrProc(proc_id) -> print_br_proc proc_id
| BrOp(brOp) -> print_br_op brOp
| BrLabel(nlabel) -> print_br_label nlabel
| BrBltIn(brBltIn) -> print_br_bltin brBltIn
| BrComment(brComment) -> print_br_comment brComment
(* Print procedure name as a label *)
and print_br_proc proc_id =
fprintf std_formatter "proc_%s:\n" proc_id
(* Print lines for brill operations *)
and print_br_op = function
| OpCall(proc_id) ->
fprintf std_formatter "%s%*s proc_%s\n"
indent width "call" proc_id
| OpHalt ->
fprintf std_formatter "%shalt\n"
indent
| OpPush(frame_size) ->
fprintf std_formatter "%s%*s %d\n"
indent width "push_stack_frame" frame_size
| OpPop(frame_size) ->
fprintf std_formatter "%s%*s %d\n"
indent width "pop_stack_frame" frame_size
| OpBranchUncond(nlabel) ->
fprintf std_formatter "%s%*s label%d\n"
indent width "branch_uncond" nlabel
| OpLoad(nreg,nslot) ->
fprintf std_formatter "%s%*s r%d, %d\n"
indent width "load" nreg nslot
| OpStore(nslot,nreg) ->
fprintf std_formatter "%s%*s %d, r%d\n"
indent width "store" nslot nreg
| OpLoadAddress(nreg,nslot) ->
fprintf std_formatter "%s%*s r%d, %d\n"
indent width "load_address" nreg nslot
| OpLoadIndirect(nreg1,nreg2) ->
fprintf std_formatter "%s%*s r%d, r%d\n"
indent width "load_indirect" nreg1 nreg2
| OpStoreIndirect(nreg1,nreg2) ->
fprintf std_formatter "%s%*s r%d, r%d\n"
indent width "store_indirect" nreg1 nreg2
| OpBranchOnTrue(nreg,nlabel) ->
fprintf std_formatter "%s%*s r%d, label%d\n"
indent width "branch_on_true" nreg nlabel
| OpBranchOnFalse(nreg,nlabel) ->
fprintf std_formatter "%s%*s r%d, label%d\n"
indent width "branch_on_false" nreg nlabel
| OpIntToReal(nreg_dest,nreg_scr) ->
fprintf std_formatter "%s%*s r%d, r%d\n"
indent width "int_to_real" nreg_dest nreg_scr
| OpNot(nreg_dest,nreg_scr) ->
fprintf std_formatter "%s%*s r%d, r%d\n"
indent width "not" nreg_dest nreg_scr
| OpOr(nreg_dest,nreg1,nreg2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "or" nreg_dest nreg1 nreg2
| OpAnd(nreg_dest,nreg1,nreg2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "and" nreg_dest nreg1 nreg2
| OpAddInt(nreg_dest,nreg_int1,nreg_int2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "add_int" nreg_dest nreg_int1 nreg_int2
| OpSubInt(nreg_dest,nreg_int1,nreg_int2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "sub_int" nreg_dest nreg_int1 nreg_int2
| OpMulInt(nreg_dest,nreg_int1,nreg_int2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "mul_int" nreg_dest nreg_int1 nreg_int2
| OpDivInt(nreg_dest,nreg_int1,nreg_int2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "div_int" nreg_dest nreg_int1 nreg_int2
| OpCmpEqInt(nreg_dest,nreg_int1,nreg_int2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "cmp_eq_int" nreg_dest nreg_int1 nreg_int2
| OpCmpNeInt(nreg_dest,nreg_int1,nreg_int2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "cmp_ne_int" nreg_dest nreg_int1 nreg_int2
| OpCmpLtInt(nreg_dest,nreg_int1,nreg_int2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "cmp_lt_int" nreg_dest nreg_int1 nreg_int2
| OpCmpLeInt(nreg_dest,nreg_int1,nreg_int2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "cmp_le_int" nreg_dest nreg_int1 nreg_int2
| OpCmpGtInt(nreg_dest,nreg_int1,nreg_int2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "cmp_gt_int" nreg_dest nreg_int1 nreg_int2
| OpCmpGeInt(nreg_dest,nreg_int1,nreg_int2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "cmp_ge_int" nreg_dest nreg_int1 nreg_int2
| OpAddReal(nreg_dest,nreg_real1,nreg_real2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "add_real" nreg_dest nreg_real1 nreg_real2
| OpSubReal(nreg_dest,nreg_real1,nreg_real2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "sub_real" nreg_dest nreg_real1 nreg_real2
| OpMulReal(nreg_dest,nreg_real1,nreg_real2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "mul_real" nreg_dest nreg_real1 nreg_real2
| OpDivReal(nreg_dest,nreg_real1,nreg_real2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "div_real" nreg_dest nreg_real1 nreg_real2
| OpCmpEqReal(nreg_dest,nreg_real1,nreg_real2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "cmp_eq_real" nreg_dest nreg_real1 nreg_real2
| OpCmpNeReal(nreg_dest,nreg_real1,nreg_real2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "cmp_ne_real" nreg_dest nreg_real1 nreg_real2
| OpCmpLtReal(nreg_dest,nreg_real1,nreg_real2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "cmp_lt_real" nreg_dest nreg_real1 nreg_real2
| OpCmpLeReal(nreg_dest,nreg_real1,nreg_real2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "cmp_le_real" nreg_dest nreg_real1 nreg_real2
| OpCmpGtReal(nreg_dest,nreg_real1,nreg_real2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "cmp_gt_real" nreg_dest nreg_real1 nreg_real2
| OpCmpGeReal(nreg_dest,nreg_real1,nreg_real2) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "cmp_ge_real" nreg_dest nreg_real1 nreg_real2
| OpSubOffset(nreg_dest,nreg_addr,nreg_offset) ->
fprintf std_formatter "%s%*s r%d, r%d, r%d\n"
indent width "sub_offset" nreg_dest nreg_addr nreg_offset
| OpReturn ->
fprintf std_formatter "%sreturn\n"
indent
| OpIntConst(nreg,int_const) ->
fprintf std_formatter "%s%*s r%d, %d\n"
indent width "int_const" nreg int_const
| OpRealConst(nreg,real_const) ->
fprintf std_formatter "%s%*s r%d, %f\n"
indent width "real_const" nreg real_const
| OpStringConst(nreg,string_const) ->
fprintf std_formatter "%s%*s r%d, %s\n"
indent width "string_const" nreg string_const
| OpDebugReg(nreg) ->
fprintf std_formatter "%s%*s r%d\n"
indent width "debug_reg" nreg
| OpDebugSlot(nslot) ->
fprintf std_formatter "%s%*s %d\n"
indent width "debug_slot" nslot
| OpDebugStack ->
fprintf std_formatter "%s%*s\n"
indent width "debug_stack"
(* Print a brill label *)
and print_br_label nlabel =
fprintf std_formatter "label%d:\n" nlabel
(* Print brill builtin functions *)
and print_br_bltin = function
| BltInReadInt ->
fprintf std_formatter "%s%*s read_int\n"
indent width "call_builtin"
| BltInReadReal ->
fprintf std_formatter "%s%*s read_real\n"
indent width "call_builtin"
| BltInReadBool ->
fprintf std_formatter "%s%*s read_bool\n"
indent width "call_builtin"
| BltInPrintInt ->
fprintf std_formatter "%s%*s print_int\n"
indent width "call_builtin"
| BltInPrintReal ->
fprintf std_formatter "%s%*s print_real\n"
indent width "call_builtin"
| BltInPrintBool ->
fprintf std_formatter "%s%*s print_bool\n"
indent width "call_builtin"
| BltInPrintString ->
fprintf std_formatter "%s%*s print_string\n"
indent width "call_builtin"
(* Print brill comments *)
and print_br_comment brComment =
fprintf std_formatter "# %s\n" brComment
============================================
src/snick_pprint.ml
17:33:48_Monday_22_May_2017
============================================
(*
** File: snick_pprint.ml
** Description: Pretty-printer converts from snick source
** code to well-formed style.
** Last Modified: Sun. 9th April 2017
**
** Group name: Mainframe
**
** Member names | usernames
** Xianzhuo REN | xianzhuor
** Haoyu LIN | haoyul3
** Zequn MA | zequnm
*)
open Snick_ast
open Format
(* Print program as list of procedures. *)
let rec print_program fmtr prog = print_procs fmtr prog
(* Print list of procedures. *)
and print_procs fmtr = function
| [] -> ()
| x::[] -> fprintf fmtr "%a@," print_proc x
| x::xs -> fprintf fmtr "%a@,%a" print_proc x print_procs xs
(* Print a single procedure. *)
and print_proc fmtr (header, body) =
fprintf fmtr "@[<v>proc %a@;<0 4>@[<v>%a@]@,end@]@."
print_proc_header header print_proc_body body
(* Print procedure header. *)
and print_proc_header fmtr (ident, params) =
fprintf fmtr "%s (%a)" ident print_params params
(* Print the list of parameters in header. *)
and print_params fmtr = function
| [] -> ()
| x :: [] -> fprintf fmtr "%a" print_param x
| x :: xs -> fprintf fmtr "%a, %a" print_param x print_params xs
(* Print a single procedure parameter. *)
and print_param fmtr (indicator, param_type, ident) =
fprintf fmtr "%a %a %s"
print_param_indc indicator print_type param_type ident
(* Print the indicator of a procedure parameter. *)
and print_param_indc fmtr = function
| Val -> fprintf fmtr "%s" "val"
| Ref -> fprintf fmtr "%s" "ref"
(* Print the type of a procedure parameter. *)
and print_type fmtr = function
| Bool -> fprintf fmtr "%s" "bool"
| Int -> fprintf fmtr "%s" "int"
| Float -> fprintf fmtr "%s" "float"
(* Print procedure body as a list of declarations
** followed by a list of statements. *)
and print_proc_body fmtr prog_body =
fprintf fmtr "%a@,%a" print_decls prog_body.decls
print_stmts prog_body.stmts
(* Print the list of declarations. *)
and print_decls fmtr = function
| [] -> ()
| x :: [] -> fprintf fmtr "%a@," print_decl x
| x :: xs -> fprintf fmtr "%a@,%a" print_decl x print_decls xs
(* Print the list of statements. *)
and print_stmts fmtr = function
| [] -> ()
| x :: [] -> fprintf fmtr "%a" print_stmt x
| x :: xs -> fprintf fmtr "%a@,%a" print_stmt x print_stmts xs
(* Print a single declaration. *)
and print_decl fmtr (var_type, variable) =
fprintf fmtr "%a %a;" print_type var_type print_var variable
(* Print a variable. *)
and print_var fmtr = function
| Variable (ident, None) -> fprintf fmtr "%s" ident
| Variable (ident, Some itvls) -> fprintf fmtr "%s[%a]"
ident print_itvls itvls
(* Print list of intervals. *)
and print_itvls fmtr = function
| [] -> ()
| x::[] -> fprintf fmtr "%a" print_itvl x
| x::xs -> fprintf fmtr "%a,%a" print_itvl x print_itvls xs
(* Print a single interval *)
and print_itvl fmtr (st_pnt, end_pnt) =
fprintf fmtr "%d..%d" st_pnt end_pnt
(* Print statement. *)
and print_stmt fmtr = function
| Assign (elem, expr) -> fprintf fmtr "%a := %a;" print_elem elem
print_expr expr
| Read elem -> fprintf fmtr "read %a;" print_elem elem
| Write expr ->
begin
match expr with
| Expr wexpr -> fprintf fmtr "write %a;" print_expr wexpr
| String str -> fprintf fmtr "write %s;" str
end
| Call (ident, exprs) -> fprintf fmtr "%s(%a);" ident print_exprs exprs
| If_then (expr, stmts) -> fprintf fmtr "if %a then@;<0 4>@[<v>%a@]@,fi"
print_expr expr print_stmts stmts
| If_then_else (expr, then_stmts, else_stmts) ->
fprintf fmtr "if %a then@;<0 4>@[<v>%a@]@,else@;<0 4>@[<v>%a@]@,fi"
print_expr expr print_stmts then_stmts print_stmts else_stmts
| While (expr, stmts) -> fprintf fmtr "while %a do@;<0 4>@[<v>%a@]@,od"
print_expr expr print_stmts stmts
(* Print element to be assigned to or be read / written. *)
and print_elem fmtr = function
| Elem (ident, None) -> fprintf fmtr "%s" ident
| Elem (ident, Some idxs) -> fprintf fmtr "%s[%a]" ident print_exprs idxs
(* Print an expression. *)
and print_expr fmtr = function
| Eelem elem -> fprintf fmtr "%a" print_elem elem
| Ebool bool_const -> fprintf fmtr "%B" bool_const
| Eint int_const -> fprintf fmtr "%d" int_const
| Efloat float_const -> fprintf fmtr "%f" float_const
(* Parentheses to be printed (or removed) in other functions,
so only print the expression within the parenthesis.
*)
| Eparen expr -> fprintf fmtr "%a" print_expr (strip_paren expr)
| Ebinop bin_expr -> fprintf fmtr "%a" print_binop bin_expr
| Eunop un_expr -> fprintf fmtr "%a" print_unop un_expr
(* Print expressions. *)
and print_exprs fmtr = function
| [] -> ()
| x::[] -> fprintf fmtr "%a" print_expr x
| x::xs -> fprintf fmtr "%a, %a" print_expr x print_exprs xs
(* Print binary operations.
** Parenthese around an operation expression on LHS with higher
** or equal precedence will be removed.
** Parenthese around an operation expression on RHS with higher
** precedence will be removed.
*)
and print_binop fmtr = function
| (Eparen lexpr_inside, optr, Eparen rexpr_inside) ->
begin
let
lexpr_inside_strip = strip_paren lexpr_inside
and
rexpr_inside_strip = strip_paren rexpr_inside
in
match lexpr_inside_strip with
| Ebinop _ | Eunop _ ->
begin
match rexpr_inside_strip with
| Ebinop _ | Eunop _ ->
let
lcmpr_result = cmpr_prec lexpr_inside_strip optr
and
rcmpr_result = cmpr_prec rexpr_inside_strip optr
in
if lcmpr_result>=0 && rcmpr_result>0 then
fprintf fmtr "%a %a %a"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
else if lcmpr_result>=0 && rcmpr_result<=0 then
fprintf fmtr "%a %a (%a)"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
else if lcmpr_result<0 && rcmpr_result>0 then
fprintf fmtr "(%a) %a %a"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
else
fprintf fmtr "(%a) %a (%a)"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
| _ ->
let
lcmpr_result = cmpr_prec lexpr_inside_strip optr
in
if lcmpr_result>=0 then
fprintf fmtr "%a %a %a"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
else
fprintf fmtr "(%a) %a %a"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
end
| _ ->
begin
match rexpr_inside_strip with
| Ebinop _ | Eunop _ ->
let
rcmpr_result = cmpr_prec rexpr_inside_strip optr
in
if rcmpr_result>0 then
fprintf fmtr "%a %a %a"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
else
fprintf fmtr "%a %a (%a)"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
| _ ->
fprintf fmtr "%a %a %a"
print_expr lexpr_inside_strip
print_optr optr
print_expr rexpr_inside_strip
end
end
| (Eparen lexpr_inside, optr, rexpr) ->
begin
let
lexpr_inside_strip = strip_paren lexpr_inside
in
match lexpr_inside_strip with
| Ebinop _ | Eunop _ ->
let
lcmpr_result = cmpr_prec lexpr_inside_strip optr
in
if lcmpr_result>=0 then
fprintf fmtr "%a %a %a"
print_expr lexpr_inside_strip
print_optr optr print_expr rexpr
else
fprintf fmtr "(%a) %a %a"
print_expr lexpr_inside_strip
print_optr optr print_expr rexpr
| _ ->
fprintf fmtr "%a %a %a"
print_expr lexpr_inside_strip
print_optr optr print_expr rexpr
end
| (lexpr, optr, Eparen rexpr_inside) ->
begin
let
rexpr_inside_strip = strip_paren rexpr_inside
in
match rexpr_inside_strip with
| Ebinop _ | Eunop _ ->
let
rcmpr_result = cmpr_prec rexpr_inside_strip optr
in
if rcmpr_result>0 then
fprintf fmtr "%a %a %a"
print_expr lexpr print_optr optr
print_expr rexpr_inside_strip
else
fprintf fmtr "%a %a (%a)"
print_expr lexpr print_optr optr
print_expr rexpr_inside_strip
| _ ->
fprintf fmtr "%a %a %a"
print_expr lexpr print_optr optr
print_expr rexpr_inside_strip
end
| (lexpr, optr, rexpr) ->
begin
fprintf fmtr "%a %a %a"
print_expr lexpr print_optr optr print_expr rexpr
end
(* Print unary operations.
** Parenthese around an operation expression with higher precedence
** will be removed. *)
and print_unop fmtr = function
| (optr, Eparen expr_inside) ->
begin
let
expr_inside_strip = strip_paren expr_inside
in
match expr_inside_strip with
| Ebinop _ | Eunop _ ->
let
cmpr_result = cmpr_prec expr_inside_strip optr
in
if cmpr_result<0 then
fprintf fmtr "%a (%a)"
print_optr optr print_expr expr_inside_strip
else
fprintf fmtr "%a %a"
print_optr optr print_expr expr_inside_strip
| _ ->
fprintf fmtr "%a %a"
print_optr optr print_expr expr_inside_strip
end
| (optr, expr) ->
begin
fprintf fmtr "%a %a"
print_optr optr print_expr expr
end
(* compare operator precedence *)
and cmpr_prec exp optr = match exp with
| Ebinop (_, exp_binoptr, _) -> (get_prec exp_binoptr) - (get_prec optr)
| Eunop (exp_unoptr, _) -> (get_prec exp_unoptr) - (get_prec optr)
| _ -> raise (Failure "Impossible!")
(* get the precedence of an operator *)
and get_prec optr = match optr with
| Op_minus -> 7
| Op_mul | Op_div -> 6
| Op_add | Op_sub -> 5
| Op_eq | Op_ne | Op_lt | Op_gt | Op_le | Op_ge -> 4
| Op_not -> 3
| Op_and -> 2
| Op_or -> 1
(* print operator symbol *)
and print_optr fmtr optr = match optr with
| Op_add -> fprintf fmtr "%s" "+"
| Op_sub -> fprintf fmtr "%s" "-"
| Op_mul -> fprintf fmtr "%s" "*"
| Op_div -> fprintf fmtr "%s" "/"
| Op_eq -> fprintf fmtr "%s" "="
| Op_ne -> fprintf fmtr "%s" "!="
| Op_lt -> fprintf fmtr "%s" "<"
| Op_gt -> fprintf fmtr "%s" ">"
| Op_le -> fprintf fmtr "%s" "<="
| Op_ge -> fprintf fmtr "%s" ">="
| Op_and -> fprintf fmtr "%s" "and"
| Op_or -> fprintf fmtr "%s" "or"
| Op_not -> fprintf fmtr "%s" "not"
| Op_minus -> fprintf fmtr "%s" "-"
(* Strips parentheses in case there are multiple pairs of parentheses
** around an expression.
*)
and strip_paren expr = match expr with
| Eparen paren_expr -> strip_paren paren_expr
| _ -> expr
============================================
src/Makefile.depend
17:33:48_Monday_22_May_2017
============================================
snick.cmo : snick_pprint.cmi snick_parse.cmi snick_lex.cmo snick_codegen.cmo
snick.cmx : snick_pprint.cmx snick_parse.cmx snick_lex.cmx snick_codegen.cmx
snick.cmo : snick_pprint.cmi snick_parse.cmi snick_lex.cmo snick_codegen.cmo
snick.cmx : snick_pprint.cmx snick_parse.cmx snick_lex.cmx snick_codegen.cmx
snick_analyze.cmo : snick_symbol.cmo snick_optimizer.cmo snick_err.cmo \
snick_ast.cmo
snick_analyze.cmx : snick_symbol.cmx snick_optimizer.cmx snick_err.cmx \
snick_ast.cmx
snick_ast.cmo :
snick_ast.cmx :
snick_br_ast.cmo :
snick_br_ast.cmx :
snick_codegen.cmo : snick_symbol.cmo snick_optimizer.cmo snick_br_ast.cmo \
snick_ast.cmo snick_analyze.cmo
snick_codegen.cmx : snick_symbol.cmx snick_optimizer.cmx snick_br_ast.cmx \
snick_ast.cmx snick_analyze.cmx
snick_err.cmo : snick_symbol.cmo
snick_err.cmx : snick_symbol.cmx
snick_lex.cmo : snick_parse.cmi
snick_lex.cmx : snick_parse.cmx
snick_optimizer.cmo : snick_symbol.cmo snick_ast.cmo
snick_optimizer.cmx : snick_symbol.cmx snick_ast.cmx
snick_parse.cmo : snick_ast.cmo snick_parse.cmi
snick_parse.cmx : snick_ast.cmx snick_parse.cmi
snick_parse.cmi : snick_ast.cmo
snick_pprint.cmo : snick_ast.cmo snick_pprint.cmi
snick_pprint.cmx : snick_ast.cmx snick_pprint.cmi
snick_pprint.cmi : snick_ast.cmo
snick_symbol.cmo : snick_ast.cmo
snick_symbol.cmx : snick_ast.cmx
============================================
src/snick_symbol.ml
17:33:48_Monday_22_May_2017
============================================
(*
** File: snick_symbol.ml
** Description: Defines the data structure and data types for Snick
** also provides getters of Scope objects.
** Last Modified: Mon. 15th May 2017
**
** Group name: Mainframe
**
** Member names | usernames
** Xianzhuo REN | xianzhuor
** Haoyu LIN | haoyul3
** Zequn MA | zequnm
*)
open Snick_ast
type symKind =
| SYM_LOCAL
| SYM_PARAM_VAL
| SYM_PARAM_REF
type symType =
| SYM_BOOL
| SYM_REAL
| SYM_INT
type nslot = int
type bound = interval
(* Representation of a single symbol *)
type symbol = (symKind * symType * nslot * bound list option)
(* Symbol table for a particular scope *)
type htScopeSt = (string, symbol) Hashtbl.t
(* Description of a scope *)
(* identifier of scope (proc), symbol table of this scope, parameters, size*)
type scope = Scope of (ident * htScopeSt * param list * nslot)
let get_scope_id (Scope(id,_,_,_)) = id
let get_scope_st (Scope(_,ht_st,_,_)) = ht_st
let get_scope_params (Scope(_,_,params,_)) = params
let get_scope_nslot (Scope(_,_,_,nslot)) = nslot
let sym_type_from_ast_type = function
| Bool -> SYM_BOOL
| Int -> SYM_INT
| Float -> SYM_REAL
let sym_kind_from_ast_indc = function
| Val -> SYM_PARAM_VAL
| Ref -> SYM_PARAM_REF
let ast_type_from_sym_type = function
| SYM_BOOL -> Bool
| SYM_INT -> Int
| SYM_REAL -> Float============================================
src/snick.ml
17:33:48_Monday_22_May_2017
============================================
(*
** File: snick.ml
** Description: Main file for Snick compiler,
** modified based on given sample.
** Last Modified: Sun. 9th April 2017
**
** Group name: Mainframe
**
** Member names | usernames
** Xianzhuo REN | xianzhuor
** Haoyu LIN | haoyul3
** Zequn MA | zequnm
*)
module P = Snick_parse
(* Argument parsing code *)
let infile_name = ref None
type compiler_mode = PrettyPrint | Compile
let mode = ref Compile
(* print current position of lexbuf *)
let err_pos lexbuf =
let pos = Lexing.lexeme_start_p lexbuf in
Format.sprintf ": line %d, col %d."
(pos.Lexing.pos_lnum)
(pos.Lexing.pos_cnum - pos.Lexing.pos_bol + 1)
(* --------------------------------------------- *)
(* Specification for command-line options *)
(* --------------------------------------------- *)
let (speclist:(Arg.key * Arg.spec * Arg.doc) list) =
["-p",
Arg.Unit(fun () -> mode := PrettyPrint),
" Run the compiler in pretty-printer mode"
]
let main () =
(* Parse the command-line arguments *)
Arg.parse speclist
(begin fun fname -> infile_name := Some fname end)
"snick [-p] [snick source]" ;
(* Open the input file *)
let infile = match !infile_name with
| None -> stdin
| Some fname -> open_in fname in
(* Initialize lexing buffer *)
let lexbuf = Lexing.from_channel infile in
(* Call the parser *)
try
let prog = Snick_parse.program Snick_lex.token lexbuf in
match !mode with
| PrettyPrint ->
Snick_pprint.print_program Format.std_formatter prog
| Compile ->
Snick_codegen.compile prog
with
(* Handle failure from lexer, print error position. *)
| Snick_lex.LexErr ->
failwith ("Lexing Error" ^ (err_pos lexbuf))
(* Handle error from parser, print error position. *)
| Parsing.Parse_error ->
failwith ("Parsing Error" ^ (err_pos lexbuf))
| Failure x ->
failwith ("Semantic Error: "^x)
let _ = main ()
============================================
src/snick_codegen.ml
17:33:48_Monday_22_May_2017
============================================
(*
** File: snick_codegen.ml
** Description: Module to generate brill code from a parsed snick program.
** Last Modified: Wed. 18th May 2017
**
** Group name: Mainframe
**
** Member names | usernames
** Xianzhuo REN | xianzhuor
** Haoyu LIN | haoyul3
** Zequn MA | zequnm
*)
open Snick_ast
open Snick_symbol
open Snick_err
open Snick_analyze
open Snick_br_ast
open Snick_optimizer
open Format
let brprog = ref [] (* Brill program to be generated *)
let out_of_bounds_label = 0
let div_by_zero_label = 1
let next_label = ref 2
(* Strip any parentheses around a expression *)
let rec strip_paren expr = match expr with
| Eparen paren_expr -> strip_paren paren_expr
| _ -> expr
(* Start compiling program *)
let rec compile prog =
analyse prog; (* First analyse the program into symbol table *)
(* Generate brill program *)
gen_br_program (simplify_prog prog);
(* Print brill program *)
print_prog !brprog
(* Check if indices are static, i.e. all indices are int
** e.g. A[1,2] *)
and is_idxs_all_static idxs =
List.for_all
(fun idx ->
match idx with
| Eint(_) -> true
| _ -> false
)
idxs
(* Calculate the offset for static indices *)
and calc_static_offset idxs bases bounds =
let (lo_bound, _) = List.hd bounds in
match idxs with
| [] -> error_undefined ""
| idx::[] ->
(
match idx with
| Eint(int_idx) -> int_idx - lo_bound
| _ -> error_undefined ""
)
| idx::idxs_tail ->
(
let offset = calc_static_offset
(idxs_tail) (List.tl bases) (List.tl bounds) in
match idx with
| Eint(int_idx) -> (int_idx - lo_bound) * (List.hd bases) + offset
| _ -> error_undefined ""
)
(* Brill program generation *)
and gen_br_program prog =
gen_call "main";
gen_halt "";
gen_br_out_of_bounds "";
gen_br_div_by_zero "";
List.iter gen_br_proc prog
(* Create a label and instructions for the index out of bound error *)
and gen_br_out_of_bounds _ =
gen_label out_of_bounds_label;
gen_string_const 0 "\"ARRAY INDEXING OUT OF BOUND\\n\"";
(* gen_string_const 0 "\"[FATAL]: array element out of bounds!\\n\""; *)
gen_call_builtin "print_string";
gen_halt ""
(* Create a label and instructions for the division by zero error *)
and gen_br_div_by_zero _ =
gen_label div_by_zero_label;
gen_string_const 0 "\"DIVIDE BY ZERO\\n\"";
(* gen_string_const 0 "\"[FATAL]: division by zero!\\n\""; *)
gen_call_builtin "print_string";
gen_halt ""
(* Generate a block of brill instructions for a snick procedure *)
and gen_br_proc ((proc_id,params),proc_body) =
let scope = Hashtbl.find ht_scopes proc_id
in
(
(* generate label *)
gen_proc_label proc_id;
(* generate prologue *)
gen_br_prologue scope params proc_body.decls;
(* generate instructions of statments in procedure *)
gen_br_stmts scope proc_body.stmts;
(* generate epilogue *)
gen_br_epilogue scope
)
(* Generate prologue of procedure *)
and gen_br_prologue scope params decls =
gen_comment "prologue"; (* mark with comment *)
(* push this scope to stack *)