-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex5.scrbl
3288 lines (2806 loc) · 99.1 KB
/
ex5.scrbl
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 scribble/manual
@require[scribble/lp]
@require[scribble/examples]
@require["eval.rkt"]
@title[#:version "" #:style 'toc]{Chapter 5}
@define[ev @make-eval[]]
@section[#:tag "c5e0"]{Note on diagrams}
I don't think creating diagrams for the data paths is a good
use of my time (they're hard to work with and confuse me
more than help me -- sorry!). This programming note is included
because exercise 5.1 is solely a diagramming question, and I
want the section and exercise numbers to line up.
@section[#:tag "c5e2"]{Exercise 5.2}
This is a literal translation of the iterative factorial
algorithm. The product and counter are both initialized to
@tt{1} at the start, mimicking the initial call to the inner
recursive function. Then, in every iteration, we check
whether the counter is greater than the original input
@tt{n}. If it is, we finish, with the final result being
stored in the @tt{product} register. If it isn't, then we
update the product and increment the counter before starting
again.
Here's an implementation of the given factorial function:
@racketblock[
(controller
(assign product (const 1))
(assign counter (const 1))
test-counter
(test (op >) (reg counter) (reg n))
(branch (label factorial-done))
(assign t (op *) (reg product) (reg counter))
(assign product (reg t))
(assign counter (op +) (const 1) (reg counter))
(goto (label test-counter))
factorial-done)
]
@section[#:tag "c5e3"]{Exercise 5.3}
Consider the following function for computing square roots using
Newton's method:
@racketblock[
(define (sqrt x)
(define (good-enough? guess)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess)
(average guess (/ x guess)))
(define (sqrt-iter guess)
(if (good-enough? guess)
guess
(sqrt-iter (improve guess))))
(sqrt-iter 1.0))
]
We're asked to translate this into a register machine -- first with
the @tt{good-enough?} and @tt{improve} operations presumed to exist
beforehand, and again with the operations defined ourselves.
First, here is a simple machine for computing square roots with these
operations used as primitives.
@racketblock[
(controller
(assign x (op read))
(assign guess (const 1.0))
sqrt-iter
(test (op good-enough?) (reg guess) (reg x))
(branch (label sqrt-done))
(assign guess (op improve) (reg guess) (reg x))
(goto (label sqrt-iter))
sqrt-done
(perform (op print) (reg guess)))
]
Next, here is an expanded version with those functions expanded.
@racketblock[
(controller
(assign x (op read))
(assign guess (const 1.0))
test-guess
(assign guess-temp (op *) (reg guess) (reg guess))
(assign guess-temp (op - ) (reg guess-temp) (reg x))
(assign guess-temp (op abs) (reg guess-temp))
(test (op <) (reg guess-temp) (const 0.001))
(branch (label sqrt-done))
improve-guess
(assign next-term (op /) (reg x) (reg guess))
(assign guess (op +) (reg guess) (reg next-term))
(assign guess (op /) (reg guess) (const 2))
(goto (label test-guess))
sqrt-done
(perform (op print) (reg guess)))
]
@section[#:tag "c5e4"]{Exercise 5.4}
A controller instruction sequence for recursive exponentiation:
@racketblock[
(controller
(assign continue (label expt-done))
expt-loop
(test (op =) (reg n) (const 0))
(branch (label base-case))
(save continue)
(assign n (op -) (reg n) (const 1))
(save n)
(assign continue (label after-expt))
(goto (label expt-loop))
after-expt
(restore n)
(restore continue)
(assign val (op *) (reg b) (reg val))
(goto (reg continue))
base-case
(assign val (const 1))
(goto (reg continue))
expt-done)
]
A controller instruction sequence for iterative exponentiation:
@racketblock[
(controller
(assign counter (reg n))
(assign product (const 1))
expt-iter
(test (op =) (reg counter) (const 0))
(branch (label after-expt))
(assign counter (op -) (reg counter) (const 1))
(assign product (op *) (reg b) (reg product))
(goto (label expt-iter))
after-expt
(assign val (reg product))
(goto (label expt-done))
expt-done)
]
As we've seen before, the recursive version will place vlaues for future
work on the stack, rather than continually channeling them through the
input values of the inner subroutine. However, the cost of doing this
is now more explicit, because you can no longer rely on the implicit call
stack to do this.
@section[#:tag "c5e5"]{Exercise 5.5}
@subsection{Factorial machine}
@racketblock[
(controller
(assign continue (label fact-done)) ; set up final return address
fact-loop
(test (op =) (reg n) (const 1))
(branch (label base-case))
(save continue)
(save n)
(assign n (op -) (reg n) (const 1))
(assign continue (label after-fact))
(goto (label fact-loop))
after-fact
(restore n)
(restore continue)
(assign val (op *) (reg n) (reg val)) ; val now contains n(n - 1)!
(goto (reg continue)) ; return to caller
base-case
(assign val (const 1)) ; base case: 1! = 1
(goto (reg continue)) ; return to caller
fact-done)
]
After each instruction, if any register values have changed, they all will be
shown in a table, like this:
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "2"))
]
The stack will also be shown when it is updated, like this:
@tabular[
(list (list @bold{Stack value})
(list "<end>"))
]
@racketblock[
(assign continue (label fact-done))
]
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "2")
(list "continue" "(label fact-done)"))
]
@racketblock[
(test (op =) (reg n) (const 1))
(branch (label base-case))
]
This evaluates to false, so we don't branch.
@racketblock[
(save continue)
(save n)
]
This adds two values to the stack:
@tabular[
(list (list @bold{Stack value})
(list "2")
(list "(label fact-done)")
(list "<end>"))
]
@racketblock[
(assign n (op -) (reg n) (const 1))
(assign continue (label after-fact))
(goto (label fact-loop))
]
This updates the values of @tt{n} and @tt{continue} for the next recursive
call, and jumps to the beginning of the loop.
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "1")
(list "continue" "(label after-fact)"))
]
@racketblock[
(test (op =) (reg n) (const 1))
(branch (label base-case))
]
In this case, we do take this branch, moving to the @tt{base-case} label.
@racketblock[
(assign val (const 1))
(goto (reg continue))
]
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "1")
(list "continue" "(label after-fact)")
(list "val" "1"))
]
At this point, we have first assigned to the @tt{val} register where the
final result will be stored, and will jump to the instruction currently pointed
at by the @tt{continue} register -- that is, the @tt{after-fact} label.
@racketblock[
(restore n)
(restore continue)
]
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "2")
(list "continue" "(label fact-done)")
(list "val" "1"))
]
@tabular[
(list (list @bold{Stack value})
(list "<end>"))
]
@racketblock[
(assign val (op *) (reg n) (reg val))
(goto (reg continue))
]
We assign the final @tt{val} and will presently jump to @tt{fact-done}, ending
the procedure.
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "2")
(list "continue" "(label fact-done)")
(list "val" "2"))
]
@subsection{Fibonacci machine}
@racketblock[
(controller
(assign continue (label fib-done))
fib-loop
(test (op <) (reg n) (const 2))
(branch (label immediate-answer))
(save continue)
(assign continue (label afterfib-n-1))
(save n) ; save old value of n
(assign n (op -) (reg n) (const 1)); clobber n to n - 1
(goto (label fib-loop)) ; perform recursive call
afterfib-n-1 ; upon return, val contains Fib(n - 1)
(restore n)
(restore continue)
(assign n (op -) (reg n) (const 2))
(save continue)
(assign continue (label afterfib-n-2))
(save val) ; save Fib(n - 1)
(goto (label fib-loop))
afterfib-n-2 ; upon return, val contains Fib(n - 2)
(assign n (reg val)) ; n now contains Fib(n - 2)
(restore val) ; val now contains Fib(n - 1)
(restore continue)
(assign val ; Fib(n - 1) + Fib(n - 2)
(op +) (reg val) (reg n))
(goto (reg continue)) ; return to caller, answer is in val
immediate-answer
(assign val (reg n)) ; base case: Fib(n) = n
(goto (reg continue))
fib-done)
]
Our register values will start out like this:
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "3"))
]
And our stack is empty:
@tabular[
(list (list @bold{Stack value})
(list "<end>"))
]
@racketblock[
(assign continue (label fib-done))
]
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "3")
(list "continue" "(label fact-done)"))
]
@racketblock[
(test (op <) (reg n) (const 2))
(branch (label immediate-answer))
]
This is false, so we don't branch.
@racketblock[
(save continue)
]
@tabular[
(list (list @bold{Stack value})
(list "(label fib-done)")
(list "<end>"))
]
@racketblock[
(assign continue (label afterfib-n-1))
]
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "3")
(list "continue" "(label afterfib-n-1)"))
]
@racketblock[
(save n)
]
@tabular[
(list (list @bold{Stack value})
(list "3")
(list "(label fib-done)")
(list "<end>"))
]
@racketblock[
(assign n (op -) (reg n) (const 1))
(goto (label fib-loop))
]
We decrement @tt{n} and move on to the next loop.
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "2")
(list "continue" "(label afterfib-n-1)"))
]
@racketblock[
(test (op <) (reg n) (const 2))
(branch (label immediate-answer))
]
This is false, so we don't branch.
@racketblock[
(save continue)
]
@tabular[
(list (list @bold{Stack value})
(list "(label afterfib-n-1)")
(list "3")
(list "(label fib-done)")
(list "<end>"))
]
@racketblock[
(assign continue (label afterfib-n-1))
]
This happens to do nothing.
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "2")
(list "continue" "(label afterfib-n-1)"))
]
@racketblock[
(save n)
]
@tabular[
(list (list @bold{Stack value})
(list "2")
(list "(label afterfib-n-1)")
(list "3")
(list "(label fib-done)")
(list "<end>"))
]
@racketblock[
(assign n (op -) (reg n) (const 1))
(goto (label fib-loop))
]
We decrement @tt{n} and move on to the next loop.
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "1")
(list "continue" "(label afterfib-n-1)"))
]
@racketblock[
(test (op <) (reg n) (const 2))
(branch (label immediate-answer))
]
This is now true, so we will branch.
@racketblock[
(assign val (reg n))
(goto (reg continue))
]
We write the base case value into the @tt{val} register and will presently
jump to @tt{afterfib-n-1}.
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "1")
(list "continue" "(label afterfib-n-1)")
(list "val" "1"))
]
@racketblock[
(restore n)
(restore continue)
]
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "2")
(list "continue" "(label afterfib-n-1)")
(list "val" "1"))
]
@tabular[
(list (list @bold{Stack value})
(list "3")
(list "(label fib-done)")
(list "<end>"))
]
@racketblock[
(assign n (op -) (reg n) (const 2))
]
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "0")
(list "continue" "(label afterfib-n-1)")
(list "val" "1"))
]
@racketblock[
(save continue)
]
@tabular[
(list (list @bold{Stack value})
(list "(label afterfib-n-1)")
(list "3")
(list "(label fib-done)")
(list "<end>"))
]
@racketblock[
(assign continue (label afterfib-n-2))
]
We prepare to jump into the other recursive branch for the first time.
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "0")
(list "continue" "(label afterfib-n-2)")
(list "val" "1"))
]
@racketblock[
(save val)
(goto (label fib-loop))
]
We also save the value of Fib(1) -- we're going to need it later.
@tabular[
(list (list @bold{Stack value})
(list "1")
(list "(label afterfib-n-1)")
(list "3")
(list "(label fib-done)")
(list "<end>"))
]
After this, we will begin looping again.
@racketblock[
(test (op <) (reg n) (const 2))
(branch (label immediate-answer))
]
This is true, so we branch.
@racketblock[
(assign val (reg n))
(goto (reg continue))
]
We write the base case value into the @tt{val} register (overwriting the value that we
saved to the stack earlier) and will presently jump to @tt{afterfib-n-1}.
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "0")
(list "continue" "(label afterfib-n-2)")
(list "val" "0"))
]
@racketblock[
(assign n (reg val))
]
This happens to do nothing.
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "0")
(list "continue" "(label afterfib-n-2)")
(list "val" "0"))
]
@racketblock[
(restore val)
(restore continue)
]
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "0")
(list "continue" "(label afterfib-n-1)")
(list "val" "1"))
]
@tabular[
(list (list @bold{Stack value})
(list "3")
(list "(label fib-done)")
(list "<end>"))
]
@racketblock[
(assign val (op +) (reg val) (reg n))
(goto (reg continue))
]
This also happens to do nothing. After this, we will jump to @tt{afterfib-n-1}
for another recursive go-around.
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "0")
(list "continue" "(label afterfib-n-1)")
(list "val" "1"))
]
@racketblock[
(restore n)
(restore continue)
]
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "3")
(list "continue" "(label fib-done)")
(list "val" "1"))
]
@tabular[
(list (list @bold{Stack value})
(list "<end>"))
]
@racketblock[
(assign n (op -) (reg n) (const 2))
]
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "1")
(list "continue" "(label fib-done)")
(list "val" "1"))
]
@racketblock[
(save continue)
]
Notice how we have popped this label off of the stack and immediately put it back.
@tabular[
(list (list @bold{Stack value})
(list "(label fact-done)")
(list "<end>"))
]
@racketblock[
(assign continue (label afterfib-n-2))
]
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "1")
(list "continue" "(label afterfib-n-2)")
(list "val" "1"))
]
@racketblock[
(save val)
(goto (label fib-loop))
]
@tabular[
(list (list @bold{Stack value})
(list "1")
(list "(label fact-done)")
(list "<end>"))
]
@racketblock[
(test (op <) (reg n) (const 2))
(branch (label immediate-answer))
]
This is true, so we will branch.
@racketblock[
(assign val (reg n))
(goto (reg continue))
]
This happens to do nothing.
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "1")
(list "continue" "(label afterfib-n-2)")
(list "val" "1"))
]
@racketblock[
(assign n (reg val))
]
This happens to do nothing.
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "1")
(list "continue" "(label afterfib-n-2)")
(list "val" "1"))
]
@racketblock[
(restore val)
(restore continue)
]
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "1")
(list "continue" "(label fact-done)")
(list "val" "1"))
]
@tabular[
(list (list @bold{Stack value})
(list "<end>"))
]
@racketblock[
(assign val (op +) (reg val) (reg n))
(goto (reg continue))
]
With this final addition, we reach our final answer and exit the procedure.
@tabular[#:sep @hspace[5]
(list (list @bold{Register} @bold{Value})
(list "n" "1")
(list "continue" "(label fact-done)")
(list "val" "2"))
]
@section[#:tag "c5e6"]{Exercise 5.6}
In the Fibonacci program earlier, @tt{afterfib-n-1} ran the following
instructions in order:
@racketblock[
(restore continue)
(assign n (op -) (reg n) (const 2))
(save continue)
]
Immediately popping a value off of the stack into the @tt{continue} register,
computing a value entirely unrelated to it, and then pushing the value in the
@tt{continue} register back onto the stack is obviously an unnecessary
@tt{restore}/@tt{save} pair. Both of these instructions can be deleted without
consequence.
@section[#:tag "c5e7"]{Exercise 5.7}
We can simulate the recursive and iterative exponentiation machines by wrapping
them in @tt{make-machine} declarations. This can be done mechanically.
First, the recursive machine:
@racketblock[
(define recursive-exponentiation-machine
(make-machine
'(b n continue val)
(list (list '= =) (list '- -) (list '* *))
'(start-machine
(assign continue (label expt-done))
expt-loop
(test (op =) (reg n) (const 0))
(branch (label base-case))
(save continue)
(assign n (op -) (reg n) (const 1))
(save n)
(assign continue (label after-expt))
(goto (label expt-loop))
after-expt
(restore n)
(restore continue)
(assign val (op *) (reg b) (reg val))
(goto (reg continue))
base-case
(assign val (const 1))
(goto (reg continue))
expt-done)))
]
Verifying that it works:
@verbatim{
> (set-register-contents! recursive-exponentiation-machine 'b 3)
'done
> (set-register-contents! recursive-exponentiation-machine 'n 10)
'done
> (start recursive-exponentiation-machine)
'done
> (get-register-contents recursive-exponentiation-machine 'val)
59049
}
Next, the iterative machine:
@racketblock[
(define iterative-exponentiation-machine
(make-machine
'(b n val counter product)
(list (list '= =) (list '- -) (list '* *))
'(start-machine
(assign counter (reg n))
(assign product (const 1))
expt-iter
(test (op =) (reg counter) (const 0))
(branch (label after-expt))
(assign counter (op -) (reg counter) (const 1))
(assign product (op *) (reg b) (reg product))
(goto (label expt-iter))
after-expt
(assign val (reg product))
(goto (label expt-done))
expt-done)))
]
Verifying that it also works:
@verbatim{
> (set-register-contents! iterative-exponentiation-machine 'b 3)
'done
> (set-register-contents! iterative-exponentiation-machine 'n 10)
'done
> (start iterative-exponentiation-machine )
'done
> (get-register-contents iterative-exponentiation-machine 'val)
59049
}
Of course, if we use the builtin @tt{expt} function, we can see that this
result is correct:
@verbatim{
> (expt 3 10)
59049
}
@section[#:tag "c5e8"]{Exercise 5.8}
Consider the following program with an ambiguous label:
@racketblock[
start
(goto (label here))
here
(assign a (const 3))
(goto (label there))
here
(assign a (const 4))
(goto (label there))
there
]
We can see in @tt{extract-labels} that new labels are prepended to the
list of existing labels. However, the instructions are iterated through in
reverse order -- the fact that @tt{extract-labels} calls @tt{(extract-labels (cdr text) ...)} is
a tell-tale clue of this. This means that the first declaration of the @tt{here}
label will be first in the list. Since @tt{lookup-label} uses @tt{assoc}, it will
therefore return the @italic{first} instance of the @tt{here} label in code.
We can verify this by simulation:
@racketblock[
(define broken-machine
(make-machine
'(a)
'()
'(start
(goto (label here))
here
(assign a (const 3))
(goto (label there))
here
(assign a (const 4))
(goto (label there))
there)))
]
@verbatim{
> (start broken-machine )
'done
> (get-register-contents broken-machine 'a)
3
}
We can fix this by forcing @tt{extract-labels} to verify that a label with
a given name doesn't already exist in the list of labels. It's not very
efficient, but @tt{assoc} will do the job here with minimal changes:
@racketblock[
(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)
(if (assoc next-inst labels)
(error "Duplicate label -- ASSEMBLE" next-inst)
(receive insts
(cons (make-label-entry next-inst insts)
labels)))
(receive (cons (make-instruction next-inst)
insts)
labels)))))))
]
@section[#:tag "c5e9"]{Exercise 5.9}
Currently, the machine does not reject operations that are performed on
labels. For example, this example program is assembled correctly:
@racketblock[
(define operating-on-label
(make-machine
'(a)
(list (list '+ +))
'(start
(assign a (op +) (label start) (const 1)))))
]
Naturally, this program doesn't actually work -- instead, it throws a
runtime error when trying to apply the @tt{+} operation.
@verbatim{
> (start operating-on-label )
; +: contract violation
; expected: number?
; given: (mcons (mcons (mcons 'assign (mcons 'a (mcons (mcons 'op (mcons '+
; '())) (mcons (mcons 'label (mcons 'start '())) (mcons (mcons 'const
; (mcons 1 '())) '()))))) #<procedure:...ch/5/machine.rkt:198:6>) '())
; argument position: 1st
}
Since performing operations on labels is (probably) not a good idea, we can
revise the machine to reject these programs. All that we need to do is insert
a check in @tt{make-operation-exp} to verify that primitive expressions are not
labels:
@racketblock[
(define (make-operation-exp exp machine labels operations)
(let ((op (lookup-prim (operation-exp-op exp) operations))
(aprocs
(map (lambda (e)
(if (label-exp? e)
(error "Cannot perform operations on label -- ASSEMBLE" e)
(make-primitive-exp e machine labels)))
(operation-exp-operands exp))))
(lambda ()
(apply op (map (lambda (p) (p)) aprocs)))))
]
It's a little ugly, but I think this is a reasonable place to check the expression
type. Obviously we want to check the expression itself rather than the expression
procedure, so it's convenient to do so in a location where each operand expression
is already in scope. And we don't want to push down any new logic into
@tt{make-primitive-exp}, because labels @italic{are} primitives. As an alternative,
you might also consider reporting on @italic{all} of the operands that are labels.
For now, we can verify that what we've written rejects the program above:
@verbatim{
Cannot perform operations on label -- ASSEMBLE {label start}
}
@section[#:tag "c5e10"]{Exercise 5.10}
There are a couple of areas in the syntax where I think readability can be
improved by relying on a few more conventions and assumptions.
For example: The explicit declaration of @tt{reg}, @tt{label}, and @tt{const}
expressions is kind of tiresome. We can make this simpler by distinguishing
these by the forms of the expressions themselves:
@itemlist[
@item{The only primitives that the interpreter will support are numbers, strings,