This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
forked from iambrj/imin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interp.rkt
1820 lines (1650 loc) · 74.5 KB
/
interp.rkt
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
#lang racket
(require racket/fixnum)
(require "utilities.rkt" (prefix-in runtime-config: "runtime-config.rkt"))
(provide interp-F1 interp-F2 interp-F3 interp-F4
interp-pseudo-x86-0 interp-x86-0
interp-pseudo-x86-1 interp-x86-1
interp-pseudo-x86-2 interp-x86-2
interp-pseudo-x86-3 interp-x86-3
interp-pseudo-x86-4 interp-x86-4
interp-pseudo-x86-5 interp-x86-5
interp-R6-class-alt
)
;; The interpreters in this file are for the intermediate languages
;; produced by the various passes of the compiler.
;;
;; The interpreters for the source languages (Rvar, Rif, ...)
;; and the C intermediate languages Cvar and Cif
;; are in separate files, e.g., interp-Rvar.rkt.
#;(define interp-R3-prime
(lambda (p)
((send (new interp-R3-class) interp-scheme '()) p)))
(define interp-F1
(lambda (p)
((send (new interp-R4-class) interp-F '()) p)))
(define interp-F2
(lambda (p)
((send (new interp-R5-class) interp-F '()) p)))
(define interp-F3
(lambda (p)
((send (new interp-R6-class-alt) interp-F '()) p)))
(define interp-F4
(lambda (p)
((send (new interp-R8-class) interp-F '()) p)))
;; Interpreters for C2 and C3.
#;(define interp-C2
(lambda (p)
(send (new interp-R3-class) interp-C p)))
#;(define interp-C3
(lambda (p)
(send (new interp-R4-class) interp-C p)))
#;(define interp-C4
(lambda (p)
(send (new interp-R5-class) interp-C p)))
#;(define interp-C5
(lambda (p)
(send (new interp-R6-class-alt) interp-C p)))
#;(define interp-C7
(lambda (p)
(send (new interp-R8-class) interp-C p)))
;; Interpreters for x86 with names that correspond to the book.
(define interp-pseudo-x86-0
(lambda (p)
((send (new interp-R1-class) interp-pseudo-x86 '()) p)))
(define interp-x86-0
(lambda (p)
((send (new interp-R1-class) interp-x86 '()) p)))
(define interp-pseudo-x86-1
(lambda (p)
((send (new interp-R2-class) interp-pseudo-x86 '()) p)))
(define interp-x86-1
(lambda (p)
((send (new interp-R2-class) interp-x86 '()) p)))
(define interp-pseudo-x86-2
(lambda (p)
((send (new interp-R3-class) interp-pseudo-x86 '()) p)))
;; The interp-x86-2 interpreter takes a program of the form
;; (X86Program info G)
;; Also, the info field must be an association list
;; with a key 'num-root-spills whose values is
;; the number of spills to the root stack.
(define interp-x86-2
(lambda (p)
((send (new interp-R3-class) interp-x86 '()) p)))
(define interp-pseudo-x86-3
(lambda (p)
((send (new interp-R4-class) interp-pseudo-x86 '()) p)))
;; The interp-x86-3 interpreter requires that the info field of the
;; Def struct be an association list with a key 'num-root-spills whose
;; values is the number of spills to the root stack.
(define interp-x86-3
(lambda (p)
((send (new interp-R4-class) interp-x86 '()) p)))
(define interp-pseudo-x86-4
(lambda (p)
((send (new interp-R6-class-alt) interp-pseudo-x86 '()) p)))
(define interp-x86-4
(lambda (p)
((send (new interp-R6-class-alt) interp-x86 '()) p)))
(define interp-pseudo-x86-5
(lambda (p)
((send (new interp-gradual-class) interp-pseudo-x86 '()) p)))
(define interp-x86-5
(lambda (p)
((send (new interp-gradual-class) interp-x86 '()) p)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Interpreters for R1: integer arithmetic and 'let'
(define interp-R1-class
(class object%
(super-new)
(field (result (gensym 'result)))
;; Hide details for debug output.
(define/public (observe-value v)
v)
(define/public (return-from-tail v env)
(cons (cons result v) env))
(define/public (is-return? e)
(match e
[(cons (cons res v) env) (equal? res result)]
[else #f]))
(define/public (primitives)
(set '+ '- 'read))
(define/public (interp-op op)
(match op
['+ fx+]
['- fx-]
['read read-fixnum]
[else (error "in interp-op R1, unmatched" op)]))
(define/public (interp-scheme-exp env)
(lambda (ast)
(define recur (interp-scheme-exp env))
(verbose "R1/interp-scheme-exp" ast)
(match ast
[(Var x)
(lookup x env)]
[(Int n) n]
[(Let x e body)
(define v (recur e))
((interp-scheme-exp (cons (cons x v) env)) body)]
[(Prim op args)
(apply (interp-op op)
(for/list ([e args]) (recur e)))]
[else
(error (format "R1/no match in interp-scheme-exp for ~a" ast))]
)))
(define/public (interp-scheme env)
(lambda (ast)
(verbose "R1/interp-scheme" ast)
(match ast
[(Program _ e)
((interp-scheme-exp '()) e)]
[else
(error (format "R1/no match in interp-scheme for ~a" ast))]
)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; C0
(define/public (interp-C-exp env)
(lambda (ast)
(define result
(match ast
[(Var x) (lookup x env)]
[(Int n) n]
[(Prim op args)
(apply (interp-op op) (map (interp-C-exp env) args))]
[else
(error "C0/interp-C-exp unhandled" ast)]
))
(verbose "C0/interp-C-exp" ast result)
result))
(define/public (interp-C-tail env)
(lambda (ast)
(match ast
[(Return e)
((interp-C-exp env) e)]
;; (return-from-tail v env) hmm -Jeremy
[(Seq s t)
(define new-env ((interp-C-stmt env) s))
((interp-C-tail new-env) t)]
[else
(error "interp-C-tail unhandled" ast)]
)))
(define/public (interp-C-stmt env)
(lambda (ast)
(verbose "C0/interp-C-stmt" ast)
(match ast
[(Assign (Var x) e)
(let ([v ((interp-C-exp env) e)])
(cons (cons x v) env))]
[(Prim op args)
((interp-C-exp env) ast)
env]
[else
(error "interp-C-stmt unhandled" ast)]
)))
(define/public (interp-C ast)
(debug "R1/interp-C" ast)
(match ast
[(CProgram info G)
(define start (dict-ref G 'start))
((interp-C-tail '()) start)]
[else (error "no match in interp-C for " ast)]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; psuedo-x86 and x86
;; s,d ::= (var x) | (int n) | (reg r) | (deref r n)
;; i ::= (movq s d) | (addq s d) | (subq s d) | (imulq s d)
;; | (negq d) | (callq f)
;; psuedo-x86 ::= (program info i ...)
(define/public (get-name ast)
(match ast
[(or (Var x) (Reg x)) x]
[(Deref 'rbp n) n]
[else
(error 'interp-R1-class/get-name "doesn't have a name: ~a" ast)]))
(field [x86-ops (make-immutable-hash
`((addq 2 ,+)
(imulq 2 ,*)
(subq 2 ,(lambda (s d) (- d s)))
(negq 1 ,-)))])
(define/public (interp-x86-op op)
(define (err)
(error 'interp-R1-class/interp-x86-op "unmatched ~a" op))
(cadr (hash-ref x86-ops op err)))
(define/public (interp-x86-exp env)
(lambda (ast)
(copious "interp-x86-exp" ast)
(define result
(match ast
[(or (Var x) (Reg x))
(lookup (get-name ast) env)]
[(Deref r n)
(lookup (get-name ast) env)]
[(Imm n) n]
[else
(error 'interp-R1-class/interp-x86-exp "unhandled ~a" ast)]))
(copious "R1/interp-x86-exp" (observe-value result))
result))
(define/public (interp-x86-instr env)
(lambda (ast)
(when (pair? ast)
(copious "R1/interp-x86-instr" (car ast)))
(match ast
['() env]
[(cons (Callq 'read_int _) ss)
(let ([v (read)])
(copious "read " v)
((interp-x86-instr (cons (cons 'rax v) env)) ss))]
[(cons (Instr 'movq (list s d)) ss)
(define x (get-name d))
(define v ((interp-x86-exp env) s))
(copious "move " (observe-value v))
((interp-x86-instr (cons (cons x v) env)) ss)]
[(cons (Jmp conclusion) ss)
#:when (string-suffix? (symbol->string conclusion) "conclusion")
env]
[(cons (Jmp label) ss)
((interp-x86-block env) (goto-label label))]
[(X86Program info ss)
(let ([env ((interp-x86-instr '()) ss)])
(lookup 'rax env))]
[(cons (Instr binary-op (list s d)) ss)
(let ([s ((interp-x86-exp env) s)]
[d ((interp-x86-exp env) d)]
[x (get-name d)]
[f (interp-x86-op binary-op)])
(let ([v (f d s)])
(copious "binary-op result " (observe-value v))
((interp-x86-instr (cons (cons x v) env)) ss)))]
[(cons (Instr unary-op (list d)) ss)
(let ([d ((interp-x86-exp env) d)]
[x (get-name d)]
[f (interp-x86-op unary-op)])
(let ([v (f d)])
(copious "unary-op result " (observe-value v))
((interp-x86-instr (cons (cons x v) env)) ss)))]
[else (error "R1/interp-x86-instr no match for" ast)]
)))
(define/public (interp-pseudo-x86 env)
(lambda (ast)
((interp-x86 env) ast)))
(define/public (interp-x86-block env)
(lambda (ast)
(match ast
[(Block info ss)
((interp-x86-instr env) ss)]
[else
(error "R1/interp-x86-block unhandled" ast)])))
(define/public (interp-x86 env)
(lambda (ast)
(when (pair? ast)
(copious "R1/interp-x86" (car ast)))
(match ast
[(X86Program info G)
(parameterize ([get-CFG G])
(define start-block (dict-ref G 'start))
(define result-env ((interp-x86-block '()) start-block))
(lookup 'rax result-env))]
[else (error "R1/interp-x86 no match in for" ast)]
)))
)) ;; class interp-R1-class
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Interpreters for R2: Booleans and conditionals
(define interp-R2-class
(class interp-R1-class
(super-new)
(inherit interp-x86-block observe-value)
(inherit-field x86-ops)
;; We do not include 'and' because it has a funky order of evaluation.
;; -Jeremy
(define/override (primitives)
(set-union (super primitives)
(set 'eq? 'not 'or '< '<= '> '>=)))
(define/override (interp-op op)
(match op
['eq? (lambda (v1 v2)
(cond [(and (fixnum? v1) (fixnum? v2)) (eq? v1 v2)]
[(and (boolean? v1) (boolean? v2)) (eq? v1 v2)]
[else (error 'interp-op "unhandled case")]))]
['and (lambda (v1 v2)
(cond [(and (boolean? v1) (boolean? v2))
(and v1 v2)]
[else (error 'interp-op "unhandled case")]
))]
['not (lambda (v) (match v
[#t #f] [#f #t]
[else (error 'interp-op "unhandled case")]))]
['or (lambda (v1 v2)
(cond [(and (boolean? v1) (boolean? v2))
(or v1 v2)]
[else (error 'interp-op "unhandled case")]))]
['< (lambda (v1 v2)
(cond [(and (fixnum? v1) (fixnum? v2)) (< v1 v2)]
[else (error 'interp-op "unhandled case")]
))]
['<= (lambda (v1 v2)
(cond [(and (fixnum? v1) (fixnum? v2)) (<= v1 v2)]
[else (error 'interp-op "unhandled case")]
))]
['> (lambda (v1 v2)
(cond [(and (fixnum? v1) (fixnum? v2)) (> v1 v2)]
[else (error 'interp-op "unhandled case")]
))]
['>= (lambda (v1 v2)
(cond [(and (fixnum? v1) (fixnum? v2)) (>= v1 v2)]
[else (error 'interp-op "unhandled case")]
))]
[else (super interp-op op)]))
(define/override (interp-scheme-exp env)
(lambda (ast)
(define recur (interp-scheme-exp env))
(verbose "R2/interp-scheme-exp" ast)
(match ast
[(HasType e t) (recur e)]
[(Bool b) b]
[(Prim 'and (list e1 e2))
(match (recur e1)
[#t (match (recur e2)
[#t #t] [#f #f])]
[#f #f])]
[(If cnd thn els)
(match (recur cnd)
[#t (recur thn)]
[#f (recur els)]
[else
(error 'interp-scheme-exp "R2 expected Boolean, not ~a" cnd)])]
[else ((super interp-scheme-exp env) ast)]
)))
(define/override (interp-C-exp env)
(lambda (ast)
(define result
(match ast
[(HasType e t) ((interp-C-exp env) e)]
[(Bool b) b]
[else ((super interp-C-exp env) ast)]
))
(copious "R2/interp-C-exp" ast result)
result))
(define/override (interp-C-tail env)
(lambda (ast)
(copious "R2/interp-C-tail" ast)
(match ast
[(IfStmt cnd thn els)
(if ((interp-C-exp env) cnd)
((interp-C-tail env) thn)
((interp-C-tail env) els))]
[(Goto label)
((interp-C-tail env) (goto-label label))]
[else ((super interp-C-tail env) ast)]
)))
(define/override (interp-C ast)
(copious "R2/interp-C" ast)
(match ast
[(CProgram info G)
(parameterize ([get-CFG G])
(super interp-C (CProgram info G)))]
[else (error "R2/interp-C unhandled" ast)]
))
(define byte2full-reg
(lambda (r)
(match r
['al 'rax]
['bl 'rbx]
['cl 'rcx]
['dl 'rdx]
)))
(define/override (get-name ast)
(match ast
[(ByteReg r)
(super get-name (Reg (byte2full-reg r)))]
[else (super get-name ast)]))
;; Extending the set of known operators is essentially the
;; same as overriding the interp-x86-op with new functionallity
(set! x86-ops (hash-set* x86-ops
'notq `(1 ,bitwise-not)
'andq `(2 ,bitwise-and)
'xorq `(2 ,bitwise-xor)))
(define/override (interp-x86-exp env)
(lambda (ast)
(copious "R2/interp-x86-exp" ast)
(define result
(match ast
[(ByteReg r)
((interp-x86-exp env) (Reg (byte2full-reg r)))]
[(Bool #t) 1]
[(Bool #f) 0]
[(Prim 'eq? (list e1 e2))
(if (eq? ((interp-x86-exp env) e1)
((interp-x86-exp env) e2))
1 0)]
[(Prim '< (list e1 e2))
(if (< ((interp-x86-exp env) e1)
((interp-x86-exp env) e2))
1 0)]
[(Prim '<= (list e1 e2))
(if (<= ((interp-x86-exp env) e1)
((interp-x86-exp env) e2))
1 0)]
[(Prim '> (list e1 e2))
(if (> ((interp-x86-exp env) e1)
((interp-x86-exp env) e2))
1 0)]
[(Prim '>= (list e1 e2))
(if (>= ((interp-x86-exp env) e1)
((interp-x86-exp env) e2))
1 0)]
[else ((super interp-x86-exp env) ast)]
))
(copious "R2/interp-x86-exp" (observe-value result))
result))
(define (eflags-status env cc)
(define eflags ((interp-x86-exp env) (Reg '__flag)))
(match cc
['e (if (equal? eflags 'equal) 1 0)]
['l (if (equal? eflags 'less) 1 0)]
['le
(if (or (eq? 1 (eflags-status env 'e))
(eq? 1 (eflags-status env 'l)))
1 0)]
['g
(if (not (eq? 1 (eflags-status env 'le)))
1 0)]
['ge
(if (not (eq? 1 (eflags-status env 'l)))
1 0)]))
(define/override (interp-x86-instr env)
(lambda (ast)
(when (pair? ast)
(copious "R2/interp-x86-instr" (car ast)))
(match ast
[(Block `(lives ,lives) ss)
((interp-x86-instr env) ss)]
[(Block `(lives ,lives) ss)
((interp-x86-instr env) ss)]
[(cons (Instr 'set (list cc d)) ss)
(define name (get-name d))
(define val (eflags-status env cc))
(verbose "set" cc val)
((interp-x86-instr (cons (cons name val) env)) ss)]
[(cons (IfStmt cnd thn els) ss)
(if (not (eq? 0 ((interp-x86-exp env) cnd)))
((interp-x86-instr env) (append thn ss))
((interp-x86-instr env) (append els ss)))]
;; Notice that the argument order of cmpq is confusing:
;; (cmpq ,s2 ,s1) (jl thn) (jmp els)
;; is eqivalent to
;; (if (< s1 s2) thn els)
[(cons (Instr 'cmpq (list s2 s1)) ss)
(let* ([v1 ((interp-x86-exp env) s1)]
[v2 ((interp-x86-exp env) s2)]
[eflags
(cond [(< v1 v2) 'less]
[(> v1 v2) 'greater]
[else 'equal])])
((interp-x86-instr (cons (cons '__flag eflags) env)) ss))]
[(cons (Instr 'movzbq (list s d)) ss)
(define x (get-name d))
(define v ((interp-x86-exp env) s))
((interp-x86-instr (cons (cons x v) env)) ss)]
[(cons (JmpIf cc label) ss)
(cond [(eq? (eflags-status env cc) 1)
((interp-x86-block env) (goto-label label))]
[else ((interp-x86-instr env) ss)])]
[else ((super interp-x86-instr env) ast)]
)))
));; class interp-R2-class
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Interpreters for R3: Vectors
(define interp-R3-class
(class interp-R2-class
(super-new)
(inherit get-name interp-x86-op interp-x86-block observe-value)
(inherit-field x86-ops)
;; The simulated global state of the program
;; define produces private fields
(define memory (box '()))
;; field is like define but public
(field [stack-size (runtime-config:rootstack-size)]
[heap-size (runtime-config:heap-size)]
[uninitialized 'uninitialized-value-from-memory]
[fromspace_begin (box uninitialized)]
[rootstack_end (box uninitialized)]
[free_ptr (box uninitialized)]
[fromspace_end (box uninitialized)]
[rootstack_begin (box uninitialized)]
[global-label-table
(make-immutable-hash
`((free_ptr . ,free_ptr)
(fromspace_begin . ,fromspace_begin)
(fromspace_end . ,fromspace_end)
(rootstack_begin . ,rootstack_begin)
(rootstack_end . ,rootstack_end)))])
(define/public (memory-read)
(lambda (addr)
(let-values ([(start stop name vect) (fetch-page addr)])
(let ([value (vector-ref vect (arithmetic-shift (- addr start) -3))])
(when (equal? value uninitialized)
(error 'interp-R3-class/memory-read
"read uninitialized memory at address ~s"
addr))
value))))
(define/public (memory-write!)
(lambda (addr value)
(let-values ([(start stop name vect) (fetch-page addr)])
(vector-set! vect (arithmetic-shift (- addr start) -3) value))))
(define/public (collect!)
(lambda (rootset bytes-requested)
(verbose "collect!" bytes-requested)
;; after a call to collect we must guarantee there is enough
;; memory to allocate the requested block of memory
(let double-heap ([hs heap-size])
(if (< hs bytes-requested)
(double-heap (* 2 hs))
(let ((h-begin (allocate-page! 'fromspace hs)))
;; I am only advancing the end of the heap because we
;; are not reclaiming memory
(set-box! fromspace_end (+ h-begin hs))
(set-box! free_ptr h-begin))))))
(define/public (initialize!)
(lambda (stack-length heap_length)
(verbose "initialize!")
(set-box! memory '())
(let* ([s-begin (allocate-page! 'rootstack stack-size)]
[h-begin (allocate-page! 'fromspace heap-size)])
(set-box! rootstack_begin s-begin)
(set-box! rootstack_end (+ s-begin stack-size))
(set-box! fromspace_begin h-begin)
(set-box! fromspace_end (+ h-begin heap-size))
(set-box! free_ptr h-begin))))
(define (allocate-page! name size)
(verbose "allocate-page!" name size)
(unless (and (fixnum? size)
(positive? size)
(= 0 (modulo size 8)))
(error 'allocate-page! "expected non-negative fixnum in ~a" size))
;; Find the last address
(define max-addr
(for/fold ([next 8])
([page (in-list (unbox memory))])
(match-let ([`(page ,_ ,stop ,_ ,_) page])
(max next stop))))
;; Allocate with a small pad 100 words so that it isn't likely to
;; accidentally use another region.
;; The randomness is to dispell any reliance on interp always allocating
;; the same way. -Andre
(define start-addr (+ max-addr 800))
;; The range is of valid addresses in memory are [start, stop)
(define stop-addr (+ start-addr size))
(define vect (make-vector (arithmetic-shift size -3) uninitialized))
(verbose "allocated" name start-addr stop-addr)
(set-box! memory (cons `(page ,start-addr ,stop-addr ,name ,vect)
(unbox memory)))
start-addr)
(define (free! addr)
(set-box! memory
(let loop ([memory (unbox memory)])
(match memory
[`() (error 'free "invalid address ~a, not currently allocated")]
[`(,(and page `(page ,ptr ,_ ,_ ,_)) . ,pages)
(if (= addr ptr)
pages
(cons page (loop pages)))]))))
(define (fetch-page addr)
;; Create a string containing
(define (fmt-err addr memory)
(apply
string-append
(cons (format "address ~a out of bounds\n\tcurrent memory regions:\n"
addr)
(for/list ([page (in-list (unbox memory))])
(match-let ([`(page ,start ,stop ,name ,_) page])
(format "\t\t~a\t\t[~a,~a)\n" name start stop))))))
(unless (fixnum? addr)
(error 'fetch-page "invalid address ~a, not a fixnum" addr))
(unless (positive? addr)
(error 'fetch-page "invalid address ~a, negative" addr))
(unless (= 0 (modulo addr 8))
(error 'fetch-page "invalid address ~a, not 8-byte aligned" addr))
(let search ([m (unbox memory)])
(match m
[`() (error 'fetch-page (fmt-err addr memory))]
[`((page ,min ,max ,name ,vect) . ,rest-memory)
;(copious "R3/fetch page" addr min max name vect)
; vect is too large to print, makes things hard to read.
;(copious "R3/fetch page" addr min max name)
(if (and (<= min addr) (< addr max))
(values min max name vect)
(search rest-memory))]
[other (error 'fetch-page "unmatched ~a" m)])))
(define/override (primitives)
(set-union (super primitives)
(set 'vector 'vector-ref 'vector-set! vector-length
;; todo: move the following to a different interpreter -Jeremy
'vector-proxy)))
(define/override (interp-op op)
(match op
['eq? (lambda (v1 v2)
(cond [(or (and (fixnum? v1) (fixnum? v2))
(and (boolean? v1) (boolean? v2))
(and (vector? v1) (vector? v2))
(and (void? v1) (void? v2)))
(eq? v1 v2)]))]
['vector-proxy
(lambda (vec rs ws)
`(vector-proxy ,vec ,rs ,ws))]
['vector vector]
['vector-length vector-length]
['vector-ref vector-ref]
['vector-set! vector-set!]
#;['vector-proxy-set! vector-set!]
[else (super interp-op op)]))
#;(define/public (scheme-vector-ref vec i)
(match vec
[`(vector-proxy ,v ,rs ,ws)
(define v^ (scheme-vector-ref v i))
(define r (vector-ref rs i))
(apply-fun (lambda (env) (interp-scheme-exp env))
r (list v^))]
[else
(vector-ref vec i)]))
#;(define/public (scheme-vector-set! vec i arg)
(match vec
[`(vector-proxy ,v ,rs ,ws)
(define w (vector-ref ws i))
(define arg^ (apply-fun (lambda (env) (interp-scheme-exp env))
w (list arg)))
(scheme-vector-set! v i arg^)]
[else
(vector-set! vec i arg)]))
(define/override (interp-scheme-exp env)
(lambda (ast)
(define recur (interp-scheme-exp env))
(verbose "R3/interp-scheme" ast)
(match ast
[(Void) (void)]
[(GlobalValue 'free_ptr)
(unbox free_ptr)]
[(GlobalValue 'fromspace_end)
(unbox fromspace_end)]
[(Allocate l ty) (build-vector l (lambda a uninitialized))]
[(AllocateClosure l ty arity)
(define vec (build-vector (add1 l) (lambda a uninitialized)))
(vector-set! vec l `(arity ,arity))
vec]
[(AllocateProxy ty) (build-vector 3 (lambda a uninitialized))]
[(Collect size)
(unless (exact-nonnegative-integer? size)
(error 'interp-C "invalid argument to collect in ~a" ast))
(void)]
#;[`(vector-ref ,e-vec ,e-i)
(define vec (recur e-vec))
(define i (recur e-i))
(scheme-vector-ref vec i)]
#;[`(vector-set! ,e-vec ,e-i ,e-arg)
(define vec (recur e-vec))
(define i (recur e-i))
(define arg (recur e-arg))
(scheme-vector-set! vec i arg)]
[else ((super interp-scheme-exp env) ast)]
)))
(define/override (interp-scheme env)
(lambda (ast)
(verbose "R3/interp-scheme" ast)
(match ast
[(Program info e)
((initialize!) runtime-config:rootstack-size
runtime-config:heap-size)
((interp-scheme-exp '()) e)]
[else ((super interp-scheme env) ast)]
)))
(define (mem-error message expr)
(lambda (who fmt . args)
(error who "~a in ~a raise error:\n~a"
message expr
(apply format (cons fmt args)))))
(define (global-value-err ast)
(lambda ()
(error 'interp-R3-class "global label is unknown in ~a" ast)))
(define/public (fetch-global label)
(let* ([err (global-value-err label)]
[ref (hash-ref global-label-table label err)]
[value (unbox ref)])
(when (equal? value uninitialized)
(debug "fetch" global-label-table)
(error 'interp-R3-class/fetch-global
"global value, ~a, used before initialization"
label))
value))
(define/override (interp-C-exp env)
(lambda (ast)
(define result
(match ast
[(Void) (void)]
[(GlobalValue 'free_ptr)
(unbox free_ptr)]
[(GlobalValue 'fromspace_end)
(unbox fromspace_end)]
[(CollectionNeeded? size)
(when (or (eq? (unbox free_ptr) uninitialized)
(eq? (unbox fromspace_end) uninitialized))
(error 'interp-C "uninitialized state in ~a" ast))
#t]
;; allocate a vector of length l and type t that is initialized.
[(Allocate l ty) (build-vector l (lambda a uninitialized))]
[(AllocateClosure l ty arity)
(define vec (build-vector (add1 l) (lambda a uninitialized)))
(vector-set! vec l `(arity ,arity))
vec]
#;[(AllocateClosure l ty arity)
(build-vector l (lambda a uninitialized))]
[(AllocateProxy ty) (build-vector 3 (lambda a uninitialized))]
[else
((super interp-C-exp env) ast)]
))
(copious "R3/interp-C-exp" ast result)
result))
(define/override (interp-C-stmt env)
(lambda (ast)
(copious "R3/interp-C-stmt" ast)
(match ast
;; Determine if a collection is needed.
;; Which it isn't because vectors stored in the environment
;; is the representation of the heap in the C language,
;; but collection is a no-op so we should check to see if
;; everything is well formed anyhow.
;; Collection isn't needed or possible in this representation
[(Collect size)
(unless (exact-nonnegative-integer? size)
(error 'interp-C "invalid argument to collect in ~a" ast))
env]
[else
((super interp-C-stmt env) ast)]
)))
(define/override (interp-C-tail env)
(lambda (ast)
(copious "R3/interp-C-tail" ast)
(match ast
[(Seq s t)
(define new-env ((interp-C-stmt env) s))
((interp-C-tail new-env) t)]
[else ((super interp-C-tail env) ast)])))
(define/override (interp-C ast)
(copious "R3/interp-C" ast)
(match ast
[(CProgram info G)
((initialize!) runtime-config:rootstack-size
runtime-config:heap-size)
(super interp-C (CProgram info G))]
[else (error "R3/interp-C unhandled" ast)]))
(define/override (interp-x86-exp env)
(lambda (ast)
(copious "interp-x86-exp" ast)
(define result
(match ast
[(Global label) (fetch-global label)]
[(Deref r i) #:when (not (eq? r 'rbp))
(define base ((interp-x86-exp env) (Reg r)))
(define addr (+ base i))
((memory-read) addr)]
[else ((super interp-x86-exp env) ast)]))
(copious "R3/interp-x86-exp" (observe-value result))
result))
(define/public (interp-x86-store env)
(lambda (ast value)
(copious "interp-x86-store" ast (observe-value value))
(match ast
[(Global label)
(define loc (hash-ref global-label-table label
(global-value-err ast)))
(set-box! loc value)
env]
[(Deref r i)
#:when (not (eq? r 'rbp))
(define base ((interp-x86-exp env) (Reg r)))
(define addr (+ base i))
((memory-write!) addr value)
env]
[dest
(define name (get-name dest))
(cons (cons name value) env)])))
(define (x86-binary-op? x)
(let ([val (hash-ref x86-ops x #f)])
(and val (= (car val) 2))))
(define (x86-unary-op? x)
(let ([val (hash-ref x86-ops x #f)])
(and val (= (car val) 1))))
(define/override (interp-x86-instr env)
(lambda (ast)
(when (pair? ast)
(copious "R3/interp-x86-instr" (car ast)))
(match ast
#;[(cons (Callq 'malloc) ss)
(define num-bytes ((interp-x86-exp env) (Reg 'rdi)))
((interp-x86-instr
`((rax . ,(allocate-page! 'malloc num-bytes)) . ,env))
ss)]
#;[(cons (Callq 'alloc) ss)
(define num-bytes ((interp-x86-exp env) (Reg 'rdi)))
((interp-x86-instr
`((rax . ,(allocate-page! 'alloc num-bytes)) . ,env))
ss)]
[(cons (Callq 'collect _) ss)
(define rootstack ((interp-x86-exp env) (Reg 'rdi)))
(define bytes-requested ((interp-x86-exp env) (Reg 'rsi)))
((collect!) rootstack bytes-requested)
((interp-x86-instr env) ss)]
[(cons (Instr 'movq (list s d)) ss)
(define value ((interp-x86-exp env) s))
(define new-env ((interp-x86-store env) d value))
((interp-x86-instr new-env) ss)]
[(cons (Instr (? x86-binary-op? binop) (list s d)) ss)
(define src ((interp-x86-exp env) s))
(define dst ((interp-x86-exp env) d))
(define op (interp-x86-op binop))
(define new-env ((interp-x86-store env) d (op src dst)))
((interp-x86-instr new-env) ss)]
[(cons (Instr (? x86-unary-op? unary-op) (list d)) ss)
(define dst ((interp-x86-exp env) d))
(define op (interp-x86-op unary-op))
(define new-env ((interp-x86-store env) d (op dst)))
((interp-x86-instr new-env) ss)]
[else
((super interp-x86-instr env) ast)]
)))
;; before register allocation
(define/override (interp-pseudo-x86 env)
(lambda (ast)
(copious "R3/interp-pseudo-x86" ast)
(match ast
[(X86Program info G)
;(define ty (dict-ref info 'type))
((initialize!) runtime-config:rootstack-size
runtime-config:heap-size)
(define env (cons (cons 'r15 (unbox rootstack_begin)) '()))
(parameterize ([get-CFG G])
(let ([env^ ((interp-x86-block env) (dict-ref G 'start))])
(lookup 'rax env^)))]
)))
;; after register allocation
(define/override (interp-x86 env)
(lambda (ast)
(copious "R3/interp-x86" ast)
(match ast
[(X86Program info G)
;;#:when (dict-has-key? info 'num-spills)
(define root-spills (dict-ref info 'num-root-spills))
(define variable-size 8) ;; ugh -Jeremy
(define root-space (* variable-size root-spills))
((initialize!) runtime-config:rootstack-size
runtime-config:heap-size)
(define env (cons (cons 'r15 (+ root-space (unbox rootstack_begin)))
'()))
(parameterize ([get-CFG G])
(let ([env^ ((interp-x86-block env) (dict-ref G 'start))])
(lookup 'rax env^)))]
)))
(set! x86-ops (hash-set* x86-ops
'sarq `(2 ,(lambda (n v) (arithmetic-shift v (- n))))
))
));; interp-R3-class
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Interpreters for R4: functions
(define interp-R4-class
(class interp-R3-class
(super-new)
(inherit primitives interp-op initialize!
return-from-tail interp-x86-block memory-read memory-write!
interp-x86-store)