-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Exercise_5_50.rkt
1408 lines (1288 loc) · 50.9 KB
/
Exercise_5_50.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/base
(require compatibility/mlist)
;; =====================================================
;; Metacircular evaluator
;; =====================================================
(define (true? x) (not (eq? x #f)))
(define (false? x) (eq? x #f))
(define (self-evaluating? exp)
(cond [(number? exp) #t]
[(string? exp) #t]
[(null? exp) #t]
[(boolean? exp) #t]
[else #f]))
(define (variable? exp) (symbol? exp))
(define (quoted? exp) (tagged-list? exp 'quote))
(define (text-of-quotation exp) (cadr exp))
(define (tagged-list? exp tag)
(cond [(pair? exp) (eq? (car exp) tag)]
[(mpair? exp) (eq? (mcar exp) tag)]
[else #f]))
(define (assignment? exp) (tagged-list? exp 'set!))
(define (assignment-variable exp) (cadr exp))
(define (assignment-value exp) (caddr exp))
(define (definition? exp)
(tagged-list? exp 'define))
(define (definition-variable exp)
(if (symbol? (cadr exp))
(cadr exp)
(caadr exp)))
(define (definition-value exp)
(if (symbol? (cadr exp))
(caddr exp)
(make-lambda (cdadr exp) ;; formal parameters
(cddr exp)))) ;; body
(define (lambda? exp) (tagged-list? exp 'lambda))
(define (lambda-parameters exp) (cadr exp))
(define (lambda-body exp) (cddr exp))
(define (make-lambda parameters body)
(cons 'lambda (cons parameters body)))
(define (if? exp) (tagged-list? exp 'if))
(define (if-predicate exp) (cadr exp))
(define (if-consequent exp) (caddr exp))
(define (if-alternative exp)
(if (not (null? (cdddr exp)))
(cadddr exp)
'false))
(define (make-if predicate consequent alternative)
(list 'if predicate consequent alternative))
(define (begin? exp) (tagged-list? exp 'begin))
(define (begin-actions exp) (cdr exp))
(define (last-exp? seq) (null? (cdr seq)))
(define (first-exp seq) (car seq))
(define (rest-exps seq) (cdr seq))
(define (sequence->exp seq)
(cond [(null? seq) seq]
[(last-exp? seq) (first-exp seq)]
[else (make-begin seq)]))
(define (make-begin seq) (cons 'begin seq))
(define (application? exp) (pair? exp))
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))
(define (cond? exp) (tagged-list? exp 'cond))
(define (cond-clauses exp) (cdr exp))
(define (cond-else-clause? clause)
(eq? (cond-predicate clause) 'else))
(define (cond-predicate clause) (car clause))
(define (cond-actions clause) (cdr clause))
(define (cond->if exp) (expand-clauses (cond-clauses exp)))
(define (expand-clauses clauses)
(if (null? clauses)
'false ;; no else clause
(let ([first (car clauses)]
[rest (cdr clauses)])
(if (cond-else-clause? first)
(if (null? rest)
(sequence->exp (cond-actions first))
(error "ELSE clause isn't last: COND->IF"
clauses))
(make-if (cond-predicate first)
(sequence->exp (cond-actions first))
(expand-clauses rest))))))
(define (let? exp) (tagged-list? exp 'let))
(define (let->combination exp)
(let* ([bindings (cadr exp)]
[var-list (map car bindings)]
[exp-list (map cadr bindings)]
[body (cddr exp)])
(cons (make-lambda var-list body) exp-list)))
(define (make-let bindings body)
(cons 'let (cons bindings body)))
(define (primitive-procedure? proc)
(tagged-list? proc 'primitive))
(define (primitive-implementation proc) (cadr proc))
(define apply-in-underlying-scheme apply)
(define (apply-primitive-procedure proc args)
(apply-in-underlying-scheme
(primitive-implementation proc) args))
(define primitive-procedures
(list (list 'car car)
(list 'cdr cdr)
(list 'caddr caddr)
(list 'cadddr cadddr)
(list 'caadr caadr)
(list 'cdadr cdadr)
(list 'cddr cddr)
(list 'cons cons)
(list 'null? null?)
(list 'list list)
(list '+ +)
(list '- -)
(list '* *)
(list '/ /)
(list '= =)
(list '> >)
(list '< <)
(list 'apply apply-primitive-procedure) ;; *** remove another 'primitive
(list 'cadr cadr)
(list 'length length)
(list 'mcons mcons)
(list 'map map)
(list 'eq? eq?)
(list 'mpair? mpair?)
(list 'mcar mcar)
(list 'mcdr mcdr)
(list 'list->mlist list->mlist)
(list 'mappend! mappend!)
(list 'mlist mlist)
(list 'number? number?)
(list 'string? string?)
(list 'symbol? symbol?)
(list 'pair? pair?)
(list 'error error)
(list 'newline newline)
(list 'display display)
(list 'read read)))
(define (primitive-procedure-names)
(map car primitive-procedures))
(define (primitive-procedure-objects)
(map (lambda (proc) (list 'primitive (cadr proc)))
primitive-procedures))
(define (enclosing-environment env) (cdr env))
(define (first-frame env) (car env))
(define the-empty-environment '())
(define (make-frame variables values)
(list->mlist (map mcons variables values))) ;; ***
(define (add-binding-to-frame! var val frame)
(mappend! frame (mlist (mcons var val))))
(define (frame-unit-variable unit) (mcar unit))
(define (frame-unit-value unit) (mcdr unit))
(define (travsersing-env end-frame-proc find-proc end-env-proc env var)
(define (env-loop env)
(define (scan pairs)
(let ([current-pair
(if (mpair? pairs)
(mcar pairs)
null)])
(cond [(null? current-pair)
(end-frame-proc env)]
[(eq? var (frame-unit-variable current-pair))
(find-proc current-pair)]
[else (scan (mcdr pairs))])))
(if (eq? env the-empty-environment)
(end-env-proc var)
(scan (first-frame env))))
(env-loop env))
(define (set-variable-value! var val env)
(travsersing-env
(lambda (env) (set-variable-value! var val (enclosing-environment env)))
(lambda (current-pair) (set-mcdr! current-pair val))
(lambda (var) (error "Unbound variable: SET!" var))
env
var))
(define (define-variable! var val env)
(travsersing-env
(lambda (env) (add-binding-to-frame! var val (first-frame env)))
(lambda (current-pair) (set-mcdr! current-pair val))
(lambda (var) (error "Empty environment" var))
env
var))
(define (lookup-variable-value var env)
(travsersing-env
(lambda (env) (lookup-variable-value var (enclosing-environment env)))
(lambda (current-pair) (frame-unit-value current-pair))
(lambda (var) (error "Unbound variable" var))
env
var))
(define (extend-environment vars vals base-env)
(if (= (length vars) (length vals))
(cons (make-frame vars vals) base-env)
(if (< (length vars) (length vals))
(error "Too many arguments supplied" vars vals)
(error "Too few arguments supplied" vars vals))))
(define (setup-environment)
(let ([initial-env
(extend-environment (primitive-procedure-names)
(primitive-procedure-objects)
the-empty-environment)])
(define-variable! 'true #t initial-env)
(define-variable! 'false #f initial-env)
(define-variable! 'null null initial-env) ;; ***
initial-env))
(define the-global-environment (setup-environment))
;; =====================================================
;; Register-Machine simulator
;; =====================================================
(define (make-machine register-names ops controller-text)
(let ([machine (make-new-machine)])
(for-each
(lambda (register-name)
((machine 'allocate-register) register-name))
register-names)
((machine 'install-operations) ops)
((machine 'install-instruction-sequence)
(assemble controller-text machine))
machine))
(define (make-register name)
(let ([contents '*unassigned*])
(define (dispatch message)
(cond [(eq? message 'get) contents]
[(eq? message 'set)
(lambda (value) (set! contents value))]
[else
(error "Unknown request: REGISTER" message)]))
dispatch))
(define (get-contents register) (register 'get))
(define (set-contents! register value)
((register 'set) value))
(define (make-stack)
(let ([s '()]
[number-pushes 0]
[max-depth 0]
[current-depth 0])
(define (push x)
(set! s (cons x s))
(set! number-pushes (+ 1 number-pushes))
(set! current-depth (+ 1 current-depth))
(set! max-depth (max current-depth max-depth)))
(define (pop)
(if (null? s)
(error "Empty stack: POP")
(let ([top (car s)])
(set! s (cdr s))
(set! current-depth (- current-depth 1))
top)))
(define (initialize)
(set! s '())
(set! number-pushes 0)
(set! max-depth 0)
(set! current-depth 0)
'done)
(define (print-statistics)
(newline)
(displayln (list 'total-pushes '= number-pushes
'maximum-depth '= max-depth)))
(define (dispatch message)
(cond [(eq? message 'push) push]
[(eq? message 'pop) (pop)]
[(eq? message 'initialize) (initialize)]
[(eq? message 'print-statistics)
(print-statistics)]
[else (error "Unknown request: STACK" message)]))
dispatch))
(define (pop stack) (stack 'pop))
(define (push stack value) ((stack 'push) value))
(define (make-new-machine)
(let ([pc (make-register 'pc)]
[flag (make-register 'flag)]
[stack (make-stack)]
[the-instruction-sequence '()])
(let ([the-ops
(list (list 'initialize-stack
(lambda () (stack 'initialize)))
(list 'print-stack-statistics
(lambda () (stack 'print-statistics))))]
[register-table
(list (list 'pc pc) (list 'flag flag))])
(define (allocate-register name)
(if (assoc name register-table)
(error "Multiply defined register: " name)
(set! register-table
(cons (list name (make-register name))
register-table)))
'register-allocated)
(define (lookup-register name)
(let ([val (assoc name register-table)])
(if val
(cadr val)
(error "Unknown register:" name))))
(define (execute)
(let ([insts (get-contents pc)])
(if (null? insts)
'done
(begin
((instruction-execution-proc (car insts)))
(execute)))))
(define (dispatch message)
(cond [(eq? message 'start)
(set-contents! pc the-instruction-sequence)
(execute)]
[(eq? message 'install-instruction-sequence)
(lambda (seq)
(set! the-instruction-sequence seq))]
[(eq? message 'allocate-register)
allocate-register]
[(eq? message 'get-register)
lookup-register]
[(eq? message 'install-operations)
(lambda (ops)
(set! the-ops (append the-ops ops)))]
[(eq? message 'stack) stack]
[(eq? message 'operations) the-ops]
[else (error "Unknown request: MACHINE"
message)]))
dispatch)))
(define (start machine) (machine 'start))
(define (get-register-contents machine register-name)
(get-contents (get-register machine register-name)))
(define (set-register-contents! machine register-name value)
(set-contents! (get-register machine register-name) value)
'done)
(define (get-register machine reg-name)
((machine 'get-register) reg-name))
(define (assemble controller-text machine)
(extract-labels
controller-text
(lambda (insts labels)
(update-insts! insts labels machine)
insts)))
(define (extract-labels text receive)
(if (null? text)
(receive '() '())
(extract-labels
(cdr text)
(lambda (insts labels)
(let ([next-inst (car text)])
(if (symbol? next-inst)
(begin
(duplicate-label? next-inst labels)
(receive insts
(cons (make-label-entry next-inst
insts)
labels)))
(receive (cons (make-instruction next-inst)
insts)
labels)))))))
(define (duplicate-label? label-name labels)
(when (assoc label-name labels)
(error "Duplicated label: ASSEMBLE"
label-name)))
(define (update-insts! insts labels machine)
(let ([pc (get-register machine 'pc)]
[flag (get-register machine 'flag)]
[stack (machine 'stack)]
[ops (machine 'operations)])
(for-each
(lambda (inst)
(set-instruction-execution-proc!
inst
(make-execution-procedure
(instruction-text inst)
labels machine pc flag stack ops)))
insts)))
(define (make-instruction text) (mcons text null))
(define (instruction-text inst) (mcar inst))
(define (instruction-execution-proc inst) (mcdr inst))
(define (set-instruction-execution-proc! inst proc)
(set-mcdr! inst proc))
(define (make-label-entry label-name insts)
(cons label-name insts))
(define (lookup-label labels label-name)
(let ([val (assoc label-name labels)])
(if val
(cdr val)
(error "Undefined label: ASSEMBLE"
label-name))))
(define (make-execution-procedure
inst labels machine pc flag stack ops)
(cond [(eq? (car inst) 'assign)
(make-assign inst machine labels ops pc)]
[(eq? (car inst) 'test)
(make-test inst machine labels ops flag pc)]
[(eq? (car inst) 'branch)
(make-branch inst machine labels flag pc)]
[(eq? (car inst) 'goto)
(make-goto inst machine labels pc)]
[(eq? (car inst) 'save)
(make-save inst machine stack pc)]
[(eq? (car inst) 'restore) (make-restore inst machine stack pc)]
[(eq? (car inst) 'perform)
(make-perform inst machine labels ops pc)]
[else
(error "Unknown instruction type: ASSEMBLE"
inst)]))
(define (make-assign inst machine labels operations pc)
(let ([target
(get-register machine (assign-reg-name inst))]
[value-exp (assign-value-exp inst)])
(let ([value-proc
(if (operation-exp? value-exp)
(make-operation-exp
value-exp machine labels operations)
(make-primitive-exp
(car value-exp) machine labels))])
(lambda () ; execution procedure for assign
(set-contents! target (value-proc))
(advance-pc pc)))))
(define (assign-reg-name assign-instruction)
(cadr assign-instruction))
(define (assign-value-exp assign-instruction)
(cddr assign-instruction))
(define (advance-pc pc)
(set-contents! pc (cdr (get-contents pc))))
(define (make-test inst machine labels operations flag pc)
(let ([condition (test-condition inst)])
(if (operation-exp? condition)
(let ([condition-proc
(make-operation-exp
condition machine labels operations)])
(lambda ()
(set-contents! flag (condition-proc))
(advance-pc pc)))
(error "Bad TEST instruction: ASSEMBLE" inst))))
(define (test-condition test-instruction)
(cdr test-instruction))
(define (make-branch inst machine labels flag pc)
(let ([dest (branch-dest inst)])
(if (label-exp? dest)
(let ([insts
(lookup-label
labels
(label-exp-label dest))])
(lambda ()
(if (get-contents flag)
(set-contents! pc insts)
(advance-pc pc))))
(error "Bad BRANCH instruction: ASSEMBLE" inst))))
(define (branch-dest branch-instruction)
(cadr branch-instruction))
(define (make-goto inst machine labels pc)
(let ([dest (goto-dest inst)])
(cond [(label-exp? dest)
(let ([insts (lookup-label
labels
(label-exp-label dest))])
(lambda () (set-contents! pc insts)))]
[(register-exp? dest)
(let ([reg (get-register
machine
(register-exp-reg dest))])
(lambda ()
(set-contents! pc (get-contents reg))))]
[else (error "Bad GOTO instruction: ASSEMBLE" inst)])))
(define (goto-dest goto-instruction)
(cadr goto-instruction))
(define (make-save inst machine stack pc)
(let ([reg (get-register machine
(stack-inst-reg-name inst))])
(lambda ()
(push stack (get-contents reg))
(advance-pc pc))))
(define (make-restore inst machine stack pc)
(let ([reg (get-register machine
(stack-inst-reg-name inst))])
(lambda ()
(set-contents! reg (pop stack))
(advance-pc pc))))
(define (stack-inst-reg-name stack-instruction)
(cadr stack-instruction))
(define (make-perform inst machine labels operations pc)
(let ([action (perform-action inst)])
(if (operation-exp? action)
(let ([action-proc
(make-operation-exp
action machine labels operations)])
(lambda () (action-proc) (advance-pc pc)))
(error "Bad PERFORM instruction: ASSEMBLE" inst))))
(define (perform-action inst) (cdr inst))
(define (make-primitive-exp exp machine labels)
(cond [(constant-exp? exp)
(let ([c (constant-exp-value exp)])
(lambda () c))]
[(label-exp? exp)
(let ([insts (lookup-label
labels
(label-exp-label exp))])
(lambda () insts))]
[(register-exp? exp)
(let ([r (get-register machine (register-exp-reg exp))])
(lambda () (get-contents r)))]
[else (error "Unknown expression type: ASSEMBLE" exp)]))
(define (register-exp? exp) (tagged-list? exp 'reg))
(define (register-exp-reg exp) (cadr exp))
(define (constant-exp? exp) (tagged-list? exp 'const))
(define (constant-exp-value exp) (cadr exp))
(define (label-exp? exp) (tagged-list? exp 'label))
(define (label-exp-label exp) (cadr exp))
(define (make-operation-exp exp machine labels operations)
(let ([op (lookup-prim (operation-exp-op exp)
operations)]
[aprocs
(map (lambda (e)
(make-primitive-exp e machine labels))
(operation-exp-operands exp))])
(lambda ()
(apply op (map (lambda (p) (p)) aprocs)))))
(define (operation-exp? exp)
(and (pair? exp) (tagged-list? (car exp) 'op)))
(define (operation-exp-op operation-exp)
(cadr (car operation-exp)))
(define (operation-exp-operands operation-exp)
(cdr operation-exp))
(define (lookup-prim symbol operations)
(let ([val (assoc symbol operations)])
(if val
(cadr val)
(error "Unknown operation: ASSEMBLE"
symbol))))
(define (empty-arglist) '())
(define (adjoin-arg arg arglist) (append arglist (list arg)))
(define (last-operand? ops) (null? (cdr ops)))
(define (get-global-environment) the-global-environment)
;; =====================================================
;; Compiler
;; =====================================================
(define (compile exp target linkage compile-env)
(cond [(self-evaluating? exp)
(compile-self-evaluating exp target linkage)]
[(quoted? exp) (compile-quoted exp target linkage)]
[(variable? exp)
(compile-variable exp target linkage compile-env)]
[(assignment? exp)
(compile-assignment exp target linkage compile-env)]
[(definition? exp)
(compile-definition exp target linkage compile-env)]
[(if? exp) (compile-if exp target linkage compile-env)]
[(lambda? exp) (compile-lambda exp target linkage compile-env)]
[(begin? exp)
(compile-sequence
(begin-actions exp) target linkage compile-env)]
[(cond? exp)
(compile (cond->if exp) target linkage compile-env)]
[(let? exp)
(compile (let->combination exp) target linkage compile-env)]
[(open-code? exp compile-env)
(compile-open-code exp target linkage compile-env)]
[(application? exp)
(compile-application exp target linkage compile-env)]
[else
(error "Unknown expression type: COMPILE" exp)]))
(define (make-instruction-sequence needs modifies statements)
(list needs modifies statements))
(define (empty-instruction-sequence)
(make-instruction-sequence '() '() '()))
(define (compile-linkage linkage)
(cond [(eq? linkage 'return)
(make-instruction-sequence '(continue) '()
'((goto (reg continue))))]
[(eq? linkage 'next)
(empty-instruction-sequence)]
[else
(make-instruction-sequence '() '()
`((goto (label ,linkage))))]))
(define (end-with-linkage linkage instruction-sequence)
(preserving '(continue)
instruction-sequence
(compile-linkage linkage)))
(define (compile-self-evaluating exp target linkage)
(end-with-linkage
linkage
(make-instruction-sequence
'()
(list target)
`((assign ,target (const ,exp))))))
(define (compile-quoted exp target linkage)
(end-with-linkage
linkage
(make-instruction-sequence
'()
(list target)
`((assign ,target (const ,(text-of-quotation exp)))))))
(define (compile-variable exp target linkage compile-env)
(let ([address (find-variable exp compile-env)])
(end-with-linkage
linkage
(if (eq? address 'not-found)
(make-instruction-sequence
'(env)
(list target)
`((assign ,target (op lookup-variable-value) (const ,exp) (reg env))))
(make-instruction-sequence
'(env)
(list target)
`((assign ,target
(op lexical-address-lookup)
(const ,address)
(reg env))))))))
(define (compile-assignment exp target linkage compile-env)
(let* ([var (assignment-variable exp)]
[get-value-code
(compile (assignment-value exp) 'val 'next compile-env)]
[address (find-variable var compile-env)])
(end-with-linkage
linkage
(preserving
'(env)
get-value-code
(if (eq? address 'not-found)
(make-instruction-sequence
'(env val)
(list target 'env)
`((assign env (op get-global-environment))
(perform (op set-variable-value!)
(const ,var)
(reg val)
(reg env))
(assign ,target (const ok))))
(make-instruction-sequence
'(env val)
(list target)
`((perform (op lexical-address-set!)
(const ,address)
(reg val)
(reg env))
(assign ,target (const ok)))))))))
(define (compile-definition exp target linkage compile-env)
(let ([var (definition-variable exp)]
[get-value-code
(compile (definition-value exp) 'val 'next compile-env)])
(end-with-linkage
linkage
(preserving
'(env)
get-value-code
(make-instruction-sequence
'(env val)
(list target)
`((perform (op define-variable!)
(const ,var)
(reg val)
(reg env))
(assign ,target (const ok))))))))
(define label-counter 0)
(define (new-label-number)
(set! label-counter (+ 1 label-counter))
label-counter)
(define (make-label name)
(string->symbol
(string-append (symbol->string name)
(number->string (new-label-number)))))
(define (compile-if exp target linkage compile-env)
(let ([t-branch (make-label 'true-branch)]
[f-branch (make-label 'false-branch)]
[after-if (make-label 'after-if)])
(let ([consequent-linkage
(if (eq? linkage 'next) after-if linkage)])
(let ([p-code (compile (if-predicate exp) 'val 'next compile-env)]
[c-code
(compile
(if-consequent exp) target consequent-linkage compile-env)]
[a-code
(compile (if-alternative exp) target linkage compile-env)])
(preserving '(env continue)
p-code
(append-instruction-sequences
(make-instruction-sequence '(val) '()
`((test (op false?) (reg val))
(branch (label ,f-branch))))
(parallel-instruction-sequences
(append-instruction-sequences t-branch c-code)
(append-instruction-sequences f-branch a-code))
after-if))))))
(define (compile-sequence seq target linkage compile-env)
(if (last-exp? seq)
(compile (first-exp seq) target linkage compile-env)
(preserving '(env continue)
(compile (first-exp seq) target 'next compile-env)
(compile-sequence
(rest-exps seq)
target
linkage
compile-env))))
(define (make-compiled-procedure entry env)
(list 'compiled-procedure entry env))
(define (compiled-procedure? proc)
(tagged-list? proc 'compiled-procedure))
(define (compiled-procedure-entry c-proc) (cadr c-proc))
(define (compiled-procedure-env c-proc) (caddr c-proc))
(define (compile-lambda exp target linkage compile-env)
(let ([proc-entry (make-label 'entry)]
[after-lambda (make-label 'after-lambda)])
(let ([lambda-linkage
(if (eq? linkage 'next) after-lambda linkage)])
(append-instruction-sequences
(tack-on-instruction-sequence
(end-with-linkage
lambda-linkage
(make-instruction-sequence
'(env)
(list target)
`((assign ,target
(op make-compiled-procedure)
(label ,proc-entry)
(reg env)))))
(compile-lambda-body exp proc-entry compile-env))
after-lambda))))
(define (compile-lambda-body exp proc-entry compile-env)
(let ([formals (lambda-parameters exp)])
(append-instruction-sequences
(make-instruction-sequence '(env proc argl) '(env)
`(,proc-entry
(assign env (op compiled-procedure-env) (reg proc))
(assign env
(op extend-environment)
(const ,formals)
(reg argl)
(reg env))))
(compile-sequence (scan-out-defines (lambda-body exp))
'val
'return
(cons formals compile-env)))))
(define (compile-application exp target linkage compile-env)
(let ([proc-code (compile (operator exp) 'proc 'next compile-env)]
[operand-codes
(map (lambda (operand) (compile operand 'val 'next compile-env))
(operands exp))])
(preserving '(env continue)
proc-code
(preserving '(proc continue)
(construct-arglist operand-codes)
(compile-procedure-call target linkage)))))
(define (construct-arglist operand-codes)
(let ([operand-codes (reverse operand-codes)])
(if (null? operand-codes)
(make-instruction-sequence '() '(argl)
'((assign argl (const ()))))
(let ([code-to-get-last-arg
(append-instruction-sequences
(car operand-codes)
(make-instruction-sequence '(val) '(argl)
'((assign argl (op list) (reg val)))))])
(if (null? (cdr operand-codes))
code-to-get-last-arg
(preserving '(env)
code-to-get-last-arg
(code-to-get-rest-args
(cdr operand-codes))))))))
(define (code-to-get-rest-args operand-codes)
(let ([code-for-next-arg
(preserving '(argl)
(car operand-codes)
(make-instruction-sequence
'(val argl)
'(argl)
'((assign argl
(op cons) (reg val) (reg argl)))))])
(if (null? (cdr operand-codes))
code-for-next-arg
(preserving '(env)
code-for-next-arg
(code-to-get-rest-args (cdr operand-codes))))))
(define (compile-procedure-call target linkage)
(let ([primitive-branch (make-label 'primitive-branch)]
[compiled-branch (make-label 'compiled-branch)]
[after-call (make-label 'after-call)])
(let ([compiled-linkage
(if (eq? linkage 'next) after-call linkage)])
(append-instruction-sequences
(make-instruction-sequence '(proc) '()
`((test (op primitive-procedure?) (reg proc))
(branch (label ,primitive-branch))))
(parallel-instruction-sequences
(append-instruction-sequences
compiled-branch
(compile-proc-appl target compiled-linkage))
(append-instruction-sequences
primitive-branch
(end-with-linkage
linkage
(make-instruction-sequence '(proc argl)
(list target)
`((assign ,target
(op apply-primitive-procedure)
(reg proc)
(reg argl)))))))
after-call))))
(define (compile-proc-appl target linkage)
(cond [(and (eq? target 'val) (not (eq? linkage 'return)))
(make-instruction-sequence '(proc) all-regs
`((assign continue (label ,linkage))
(assign val (op compiled-procedure-entry)
(reg proc))
(goto (reg val))))]
[(and (not (eq? target 'val))
(not (eq? linkage 'return)))
(let ([proc-return (make-label 'proc-return)])
(make-instruction-sequence '(proc) all-regs
`((assign continue (label ,proc-return))
(assign val (op compiled-procedure-entry)
(reg proc))
(goto (reg val))
,proc-return
(assign ,target (reg val))
(goto (label ,linkage)))))]
[(and (eq? target 'val) (eq? linkage 'return))
(make-instruction-sequence '(proc continue) all-regs
'((assign val (op compiled-procedure-entry)
(reg proc))
(goto (reg val))))]
[(and (not (eq? target 'val)) (eq? linkage 'return))
(error "return linkage, target not val -- COMPILE"
target)]))
(define all-regs '(env proc val argl continue arg1 arg2))
(define (registers-needed s)
(if (symbol? s) '() (car s)))
(define (registers-modified s)
(if (symbol? s) '() (cadr s)))
(define (statements s)
(if (symbol? s) (list s) (caddr s)))
(define (needs-register? seq reg)
(memq reg (registers-needed seq)))
(define (modifies-register? seq reg)
(memq reg (registers-modified seq)))
(define (append-instruction-sequences . seqs)
(define (append-2-sequences seq1 seq2)
(make-instruction-sequence
(list-union (registers-needed seq1)
(list-difference (registers-needed seq2)
(registers-modified seq1)))
(list-union (registers-modified seq1)
(registers-modified seq2))
(append (statements seq1) (statements seq2))))
(define (append-seq-list seqs)
(if (null? seqs)
(empty-instruction-sequence)
(append-2-sequences (car seqs)
(append-seq-list (cdr seqs)))))
(append-seq-list seqs))
(define (list-union s1 s2)
(cond [(null? s1) s2]
[(memq (car s1) s2) (list-union (cdr s1) s2)]
[else (cons (car s1) (list-union (cdr s1) s2))]))
(define (list-difference s1 s2)
(cond [(null? s1) '()]
[(memq (car s1) s2) (list-difference (cdr s1) s2)]
[else (cons (car s1)
(list-difference (cdr s1) s2))]))
(define (preserving regs seq1 seq2)
(if (null? regs)
(append-instruction-sequences seq1 seq2)
(let ([first-reg (car regs)])
(if (and (needs-register? seq2 first-reg)
(modifies-register? seq1 first-reg))
(preserving (cdr regs)
(make-instruction-sequence
(list-union (list first-reg)
(registers-needed seq1))
(list-difference (registers-modified seq1)
(list first-reg))
(append `((save ,first-reg))
(statements seq1)
`((restore ,first-reg))))
seq2)
(preserving (cdr regs) seq1 seq2)))))
(define (tack-on-instruction-sequence seq body-seq)
(make-instruction-sequence
(registers-needed seq)
(registers-modified seq)
(append (statements seq) (statements body-seq))))
(define (parallel-instruction-sequences seq1 seq2)
(make-instruction-sequence
(list-union (registers-needed seq1)
(registers-needed seq2))
(list-union (registers-modified seq1)
(registers-modified seq2))
(append (statements seq1) (statements seq2))))
;; =========================================
;; optimizations from previous exercises
;; =========================================
(define (spread-arguments operand-list compile-env)
(cons (compile (car operand-list) 'arg1 'next compile-env)
(map (lambda (operand)
(compile operand 'arg2 'next compile-env))
(cdr operand-list))))
(define (compile-open-code exp target linkage compile-env)
(define (compile-rest-open-codes operand-codes)
(if (null? (cdr operand-codes))
(preserving '(arg1)
(car operand-codes)
(make-instruction-sequence
'(arg1 arg2)
(list target)
`((assign ,target
(op ,(operator exp))
(reg arg1)
(reg arg2)))))
(preserving '(env arg1)
(car operand-codes)
(append-instruction-sequences
(make-instruction-sequence
'(arg1 arg2)
'(arg1)
`((assign arg1
(op ,(operator exp))
(reg arg1)
(reg arg2))))
(compile-rest-open-codes
(cdr operand-codes))))))
(let ([operand-codes (spread-arguments (operands exp) compile-env)])
(end-with-linkage
linkage
(preserving
'(env)
(car operand-codes)
(compile-rest-open-codes (cdr operand-codes))))))
(define (open-code? exp compile-env)
(let* ([var (operator exp)]
[address (find-variable var compile-env)])
(if (eq? address 'not-found)