about summary refs log tree commit diff stats
path: root/041jump_target.cc
blob: 8ccd56254887b144c22f6c7b8d08e59c0c5dfdf0 (plain) (blame)
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
//: Support jumps to special labels called 'targets'. Targets must be in the
//: same recipe as the jump, and must be unique in that recipe. Targets always
//: start with a '+'.
//:
//: We'll also treat 'break' and 'loop' as jumps. The choice of name is
//: just documentation about intent; use 'break' to indicate you're exiting
//: one or more loop nests, and 'loop' to indicate you're skipping to the next
//: iteration of some containing loop nest.

:(scenario jump_to_label)
def main [
  jump +target:label
  1:number <- copy 0
  +target
]
-mem: storing 0 in location 1

:(before "End Mu Types Initialization")
put(Type_ordinal, "label", 0);

:(before "End Instruction Modifying Transforms")
Transform.push_back(transform_labels);  // idempotent

:(code)
void transform_labels(const recipe_ordinal r) {
  map<string, int> offset;
  for (int i = 0; i < SIZE(get(Recipe, r).steps); ++i) {
    const instruction& inst = get(Recipe, r).steps.at(i);
    if (!inst.label.empty() && inst.label.at(0) == '+') {
      if (!contains_key(offset, inst.label)) {
        put(offset, inst.label, i);
      }
      else {
        raise << maybe(get(Recipe, r).name) << "duplicate label '" << inst.label << "'" << end();
        // have all jumps skip some random but noticeable and deterministic amount of code
        put(offset, inst.label, 9999);
      }
    }
  }
  for (int i = 0; i < SIZE(get(Recipe, r).steps); ++i) {
    instruction& inst = get(Recipe, r).steps.at(i);
    if (inst.name == "jump") {
      if (inst.ingredients.empty()) {
        raise << maybe(get(Recipe, r).name) << "'jump' expects an ingredient but got none\n" << end();
        return;
      }
      replace_offset(inst.ingredients.at(0), offset, i, r);
    }
    if (inst.name == "jump-if" || inst.name == "jump-unless") {
      if (SIZE(inst.ingredients) < 2) {
        raise << maybe(get(Recipe, r).name) << "'" << inst.name << "' expects 2 ingredients but got " << SIZE(inst.ingredients) << '\n' << end();
        return;
      }
      replace_offset(inst.ingredients.at(1), offset, i, r);
    }
    if ((inst.name == "loop" || inst.name == "break")
        && SIZE(inst.ingredients) >= 1) {
      replace_offset(inst.ingredients.at(0), offset, i, r);
    }
    if ((inst.name == "loop-if" || inst.name == "loop-unless"
            || inst.name == "break-if" || inst.name == "break-unless")
        && SIZE(inst.ingredients) >= 2) {
      replace_offset(inst.ingredients.at(1), offset, i, r);
    }
  }
}

:(code)
void replace_offset(reagent& x, /*const*/ map<string, int>& offset, const int current_offset, const recipe_ordinal r) {
  if (!is_literal(x)) {
    raise << maybe(get(Recipe, r).name) << "jump target must be offset or label but is '" << x.original_string << "'\n" << end();
    x.set_value(0);  // no jump by default
    return;
  }
  if (x.initialized) return;
  if (is_integer(x.name)) return;  // non-labels will be handled like other number operands
  if (!is_jump_target(x.name)) {
    raise << maybe(get(Recipe, r).name) << "can't jump to label '" << x.name << "'\n" << end();
    x.set_value(0);  // no jump by default
    return;
  }
  if (!contains_key(offset, x.name)) {
    raise << maybe(get(Recipe, r).name) << "can't find label '" << x.name << "'\n" << end();
    x.set_value(0);  // no jump by default
    return;
  }
  x.set_value(get(offset, x.name) - current_offset);
}

bool is_jump_target(string label) {
  return label.at(0) == '+';
}

:(scenario break_to_label)
def main [
  {
    {
      break +target:label
      1:number <- copy 0
    }
  }
  +target
]
-mem: storing 0 in location 1

:(scenario jump_if_to_label)
def main [
  {
    {
      jump-if 1, +target:label
      1:number <- copy 0
    }
  }
  +target
]
-mem: storing 0 in location 1

:(scenario loop_unless_to_label)
def main [
  {
    {
      loop-unless 0, +target:label  # loop/break with a label don't care about braces
      1:number <- copy 0
    }
  }
  +target
]
-mem: storing 0 in location 1

:(scenario jump_runs_code_after_label)
def main [
  # first a few lines of padding to exercise the offset computation
  1:number <- copy 0
  2:number <- copy 0
  3:number <- copy 0
  jump +target:label
  4:number <- copy 0
  +target
  5:number <- copy 0
]
+mem: storing 0 in location 5
-mem: storing 0 in location 4

:(scenario jump_fails_without_target)
% Hide_errors = true;
def main [
  jump
]
+error: main: 'jump' expects an ingredient but got none

:(scenario jump_fails_without_target_2)
% Hide_errors = true;
def main [
  jump-if 1/true
]
+error: main: 'jump-if' expects 2 ingredients but got 1

:(scenario recipe_fails_on_duplicate_jump_target)
% Hide_errors = true;
def main [
  +label
  1:number <- copy 0
  +label
  2:number <- copy 0
]
+error: main: duplicate label '+label'

:(scenario jump_ignores_nontarget_label)
% Hide_errors = true;
def main [
  # first a few lines of padding to exercise the offset computation
  1:number <- copy 0
  2:number <- copy 0
  3:number <- copy 0
  jump $target:label
  4:number <- copy 0
  $target
  5:number <- copy 0
]
+error: main: can't jump to label '$target'
trace.mu?h=hlt&id=c93ff8ffb3811a690c8aafed55e7a6eb2d8a3544'>c93ff8ff ^
89933230 ^

333a2d3f ^
89933230 ^
333a2d3f ^
89933230 ^
333a2d3f ^
b87f7bf5 ^




378ffc9f ^


82181e99 ^
285e0963 ^
82181e99 ^
bf2ee4a4 ^



82181e99 ^
bf2ee4a4 ^
c93ff8ff ^
76911c57 ^
c93ff8ff ^
333a2d3f ^
378ffc9f ^
c93ff8ff ^







96fb6d34 ^
82181e99 ^
c93ff8ff ^
378ffc9f ^

333a2d3f ^
c93ff8ff ^
333a2d3f ^
89933230 ^
333a2d3f ^
c93ff8ff ^
333a2d3f ^

82181e99 ^
333a2d3f ^

378ffc9f ^

9dc37aff ^

82181e99 ^
9dc37aff ^

















378ffc9f ^
0ad92f3b ^

82181e99 ^
0ad92f3b ^

0ad92f3b ^



















82181e99 ^
0ad92f3b ^






82181e99 ^
0ad92f3b ^

96fb6d34 ^

c2719c3d ^
96fb6d34 ^


d54c3dda ^




8328a326 ^



96fb6d34 ^

c2719c3d ^
96fb6d34 ^


d92d7fbb ^

8328a326 ^

d92d7fbb ^
96fb6d34 ^


8328a326 ^

c2719c3d ^

96fb6d34 ^
333a2d3f ^
0ad92f3b ^


9dc37aff ^


82181e99 ^
9dc37aff ^


333a2d3f ^
9dc37aff ^
333a2d3f ^


9dc37aff ^
333a2d3f ^

9dc37aff ^
82181e99 ^

333a2d3f ^



9dc37aff ^
333a2d3f ^


9dc37aff ^
333a2d3f ^



9dc37aff ^




82181e99 ^
9dc37aff ^

333a2d3f ^



9dc37aff ^

82181e99 ^
9dc37aff ^


82181e99 ^

0ad92f3b ^




dc380332 ^


4e64c541 ^
dc380332 ^
















82181e99 ^
dc380332 ^
82181e99 ^
dc380332 ^
82181e99 ^
dc380332 ^



82181e99 ^
dc380332 ^


8a06a34a ^


c93ff8ff ^














8a06a34a ^
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















































                                                                                                                        

                                                    




































                                                                                                                    

                                                                                                                           









                                                                                                        

                                                                                                               








                                                                                      
                                         

                                                              
                                      



                                                               
                                                                 
                                                         



                                                               
                                         
                                             
                                                     
                                      

                                                               







                                                                                             

                                                             



                                          
                                                               
                                                                       
                                                         
                     


                                                               
                                                                                
                                                         
                                                                  









                                                                                           
                           

                                                             


                                                                                                       
                                                                             
                                                                 

                                                                 
                                                         
                                                                    




                                          

                                                               
                                 
                                             
                                             
                                      

                                                               







                                                                                             



                                                                    



                                          
                                                               
                                                                       
                                                         
                     


                                                               
                                                                                
                                                         
                                                                

  









                                                                                                            
                 
                         
                                                                        


                                                                                                          



                                                                                      
                
                                                                


                                                                                                                        




                                                                        
                                                     
                                         

                                          
                                                                                
                                   
                                                                                             
                                       
                                                                                                                                       











                                                                              
                                      
         
                                                                                                      
                                 






                                                                            
                                                     
                                         

                                          
                                                                                
                                   
                                                                                                                                       
                                          
                                                                                       













                                                                                                        
         

                                                                             






                                                                                 
                                                     

                                         
                                                                                                      





                                                  











                                                                             

                                                                        
                                                     















                                                                                         

                                                                        
                                                     

                                         
                                                                                                      
                                     






                                                                                                         

                                                                                    
           
                                                                                                   
                                      
                                                                         
     




                                                                          


                                                                        
                                                     
                                         
                                                                                                    



                                                                        
                                                     
                                         
                                     
                                                                                      
                                                                   
                                    
         







                                                                                                                            
                                                                    
                                                                                                                          
                                                              

          
                             
                                      
                                      
                                             
         
                                                                                             

                                        
                                                                

          

  

                                                                        
                                                     

















                                                                                                                                           
                                                  

                                                                        
                                                     

                                         



















                                                                                 
                                                              






                                                         
                                                                

                       

                     
                                                            


                                                        




                                                                                          



                                                                                                                           

         
                                                              


                                                            

                                                                                        

                                                                                 
                                 


                                                                  

                                                                                                                           

                                                                  
   
                                      


                                                                


                                                                    
                                                                                                                         


                                                 
           
                                                                                      


                                      
                                                                                                 

                                                    
                                                           

                                                                                                                      



                                      
                                                                                                           


                                              
                                                                                                   



                                                      




                                                                                                                                         
                                                                                                                                

                



                                                   

                                                                                                                                     
                                                                                                                


                                                                      

                                                                                                          




                       


                                                                        
                                       
















                                                                                   
                                                                                                                             
               
                                                                             
         
                                                                                                          



                                        
                                                                


              


                                                                        














                                                         
  
(and-record trace [
  label:string-address
  contents:string-address
])
(address trace-address (trace))
(array trace-address-array (trace-address))
(address trace-address-array-address (trace-address-array))
(address trace-address-array-address-address (trace-address-array-address))

(and-record instruction-trace [
  call-stack:string-address-array-address
  pc:string-address  ; should be integer?
  instruction:string-address
  children:trace-address-array-address
])
(address instruction-trace-address (instruction-trace))
(array instruction-trace-address-array (instruction-trace-address))
(address instruction-trace-address-array-address (instruction-trace-address-array))

(function parse-traces [  ; stream-address -> instruction-trace-address-array-address
  (default-space:space-address <- new space:literal 30:literal)
;?   ($print (("parse-traces\n" literal))) ;? 1
  (in:stream-address <- next-input)
  (result:buffer-address <- init-buffer 30:literal)
  (curr-tail:instruction-trace-address <- copy nil:literal)
  (ch:buffer-address <- init-buffer 5:literal)  ; accumulator for traces between instructions
  (run:string-address/const <- new "run")
  ; reading each line from 'in'
  { begin
    next-line
    (done?:boolean <- end-of-stream? in:stream-address)
;?     ($print done?:boolean) ;? 1
;?     ($print (("\n" literal))) ;? 1
    (break-if done?:boolean)
    ; parse next line as a generic trace
    (line:string-address <- read-line in:stream-address)
;?     (print-string nil:literal/terminal line:string-address) ;? 1
    (f:trace-address <- parse-trace line:string-address)
    (l:string-address <- get f:trace-address/deref label:offset)
    { begin
      ; if it's an instruction trace with label 'run'
      (inst?:boolean <- string-equal l:string-address run:string-address/const)
      (break-unless inst?:boolean)
      ; add accumulated traces to curr-tail
      { begin
        (break-unless curr-tail:instruction-trace-address)
        (c:trace-address-array-address-address <- get-address curr-tail:instruction-trace-address/deref children:offset)
        (c:trace-address-array-address-address/deref <- to-array ch:buffer-address)
        ; clear 'ch'
        (ch:buffer-address <- init-buffer 5:literal)
      }
      ; append a new curr-tail to result
      (curr-tail:instruction-trace-address <- parse-instruction-trace f:trace-address)
      (result:buffer-address <- append result:buffer-address curr-tail:instruction-trace-address)
      (jump next-line:offset)  ; loop
    }
    ; otherwise accumulate trace
    (loop-unless curr-tail:instruction-trace-address)
    (ch:buffer-address <- append ch:buffer-address f:trace-address)
    (loop)
  }
  ; add accumulated traces to final curr-tail
  ; todo: test
  { begin
    (break-unless curr-tail:instruction-trace-address)
    (c:trace-address-array-address-address <- get-address curr-tail:instruction-trace-address/deref children:offset)
    (c:trace-address-array-address-address/deref <- to-array ch:buffer-address)
  }
  (s:instruction-trace-address-array-address <- to-array result:buffer-address)
  (reply s:instruction-trace-address-array-address)
])

(function parse-instruction-trace [  ; trace-address -> instruction-trace-address
  (default-space:space-address <- new space:literal 30:literal)
;?   ($print (("parse-instruction-trace\n" literal))) ;? 1
  (in:trace-address <- next-input)
  (buf:string-address <- get in:trace-address/deref contents:offset)
;?   (print-string nil:literal buf:string-address) ;? 1
;?   ($print (("\n" literal))) ;? 1
  (result:instruction-trace-address <- new instruction-trace:literal)
  (f1:string-address rest:string-address <- split-first buf:string-address ((#\space literal)))
;?   ($print (("call-stack: " literal))) ;? 1
;?   (print-string nil:literal f1:string-address) ;? 1
;?   ($print (("\n" literal))) ;? 1
  (cs:string-address-array-address-address <- get-address result:instruction-trace-address/deref call-stack:offset)
  (cs:string-address-array-address-address/deref <- split f1:string-address ((#\/ literal)))
  (p:string-address-address <- get-address result:instruction-trace-address/deref pc:offset)
  (delim:string-address <- new ": ")
  (p:string-address-address/deref rest:string-address <- split-first-at-substring rest:string-address delim:string-address)
  (inst:string-address-address <- get-address result:instruction-trace-address/deref instruction:offset)
  (inst:string-address-address/deref <- copy rest:string-address)
  (reply result:instruction-trace-address)
])

(function parse-trace [  ; string-address -> trace-address
  (default-space:space-address <- new space:literal 30:literal)
;?   ($print (("parse-trace\n" literal))) ;? 1
  (in:string-address <- next-input)
  (result:trace-address <- new trace:literal)
  (delim:string-address <- new ": ")
  (first:string-address rest:string-address <- split-first-at-substring in:string-address delim:string-address)
  (l:string-address-address <- get-address result:trace-address/deref label:offset)
  (l:string-address-address/deref <- copy first:string-address)
  (c:string-address-address <- get-address result:trace-address/deref contents:offset)
  (c:string-address-address/deref <- copy rest:string-address)
  (reply result:trace-address)
])

(function print-trace [
  (default-space:space-address <- new space:literal 30:literal)
  (screen:terminal-address <- next-input)
  (x:trace-address <- next-input)
  (l:string-address <- get x:trace-address/deref label:offset)
  (clear-line screen:terminal-address)
  (print-string screen:terminal-address l:string-address)
  (print-character screen:terminal-address ((#\space literal)))
  (print-character screen:terminal-address ((#\: literal)))
  (print-character screen:terminal-address ((#\space literal)))
  (c:string-address <- get x:trace-address/deref contents:offset)
  (print-string screen:terminal-address c:string-address)
])

(function print-instruction-trace [
  (default-space:space-address <- new space:literal 30:literal)
  (screen:terminal-address <- next-input)
  (x:instruction-trace-address <- next-input)
  (0:space-address/names:browser-state <- next-input)
  (clear-line screen:terminal-address)
  (print-character screen:terminal-address ((#\- literal)))
  (print-character screen:terminal-address ((#\space literal)))
  ; print call stack
  (c:string-address-array-address <- get x:instruction-trace-address/deref call-stack:offset)
  (i:integer <- copy 0:literal)
  (len:integer <- length c:string-address-array-address/deref)
  { begin
    (done?:boolean <- greater-or-equal i:integer len:integer)
    (break-if done?:boolean)
    (s:string-address <- index c:string-address-array-address/deref i:integer)
    (print-string screen:terminal-address s:string-address)
    (print-character screen:terminal-address ((#\/ literal)))
    (i:integer <- add i:integer 1:literal)
    (loop)
  }
  ; print pc
  (print-character screen:terminal-address ((#\space literal)))
  (p:string-address <- get x:instruction-trace-address/deref pc:offset)
  (print-string screen:terminal-address p:string-address)
  ; print instruction
  (print-character screen:terminal-address ((#\space literal)))
  (print-character screen:terminal-address ((#\: literal)))
  (print-character screen:terminal-address ((#\space literal)))
  (i:string-address <- get x:instruction-trace-address/deref instruction:offset)
  (print-string screen:terminal-address i:string-address)
  (add-line 0:space-address/browser-state screen:terminal-address)
  ; print children
  (ch:trace-address-array-address <- get x:instruction-trace-address/deref children:offset)
  (i:integer <- copy 0:literal)
  { begin
    ; todo: test
    (break-if ch:trace-address-array-address)
    (reply)
  }
  (len:integer <- length ch:trace-address-array-address/deref)
  { begin
    ; until done with trace
    (done?:boolean <- greater-or-equal i:integer len:integer)
    (break-if done?:boolean)
    ; or screen ends
    (screen-done?:boolean <- greater-or-equal cursor-row:integer/space:1 screen-height:integer/space:1)
    (break-if screen-done?:boolean)
    (t:trace-address <- index ch:trace-address-array-address/deref i:integer)
    (print-character screen:terminal-address ((#\space literal)))
    (print-character screen:terminal-address ((#\space literal)))
    (print-character screen:terminal-address ((#\space literal)))
    (print-trace screen:terminal-address t:trace-address)
    (add-line 0:space-address/browser-state screen:terminal-address)
    (i:integer <- add i:integer 1:literal)
    (loop)
  }
])

(function print-instruction-trace-collapsed [
  (default-space:space-address <- new space:literal 30:literal)
  (screen:terminal <- next-input)
  (x:instruction-trace-address <- next-input)
  (browser-state:space-address <- next-input)
  (clear-line screen:terminal-address)
  (print-character screen:terminal-address ((#\+ literal)))
  (print-character screen:terminal-address ((#\space literal)))
  ; print call stack
  (c:string-address-array-address <- get x:instruction-trace-address/deref call-stack:offset)
  (i:integer <- copy 0:literal)
  (len:integer <- length c:string-address-array-address/deref)
  { begin
    (done?:boolean <- greater-or-equal i:integer len:integer)
    (break-if done?:boolean)
    (s:string-address <- index c:string-address-array-address/deref i:integer)
    (print-string screen:terminal-address s:string-address)
;?     (print-character screen:terminal-address ((#\space literal)))
    (print-character screen:terminal-address ((#\/ literal)))
;?     (print-character screen:terminal-address ((#\space literal)))
    (i:integer <- add i:integer 1:literal)
    (loop)
  }
  ; print pc
  (print-character screen:terminal-address ((#\space literal)))
  (p:string-address <- get x:instruction-trace-address/deref pc:offset)
  (print-string screen:terminal-address p:string-address)
  ; print instruction
  (print-character screen:terminal-address ((#\space literal)))
  (print-character screen:terminal-address ((#\: literal)))
  (print-character screen:terminal-address ((#\space literal)))
  (i:string-address <- get x:instruction-trace-address/deref instruction:offset)
  (print-string screen:terminal-address i:string-address)
  (add-line browser-state:space-address screen:terminal-address)
])

(function instruction-trace-num-children [
  (default-space:space-address <- new space:literal 30:literal)
  (traces:instruction-trace-address-array-address <- next-input)
  (index:integer <- next-input)
  (tr:instruction-trace-address <- index traces:instruction-trace-address-array-address/deref index:integer)
  (tr-children:trace-address-array-address <- get tr:instruction-trace-address/deref children:offset)
  (n:integer <- length tr-children:instruction-trace-address-array-address/deref)
  (reply n:integer)
])

;; data structure
(function browser-state [
  (default-space:space-address <- new space:literal 30:literal/capacity)
  ; trace state
  (traces:instruction-trace-address-array-address <- next-input)  ; the ground truth being rendered
  (expanded-index:integer <- copy -1:literal)  ; currently trace browser only ever shows one item expanded
  (first-index-on-page:integer <- copy 0:literal)  ; 'outer' line with label 'run'
  (first-subindex-on-page:integer <- copy 0:literal)  ; 'inner' line with other labels
  (last-index-on-page:integer <- copy 0:literal)
  (last-subindex-on-page:integer <- copy 0:literal)
  ; screen state
  (screen-height:integer <- next-input)  ; 'hardware' limitation
  (app-height:integer <- copy 0:literal)  ; area of the screen we're responsible for; can't be larger than screen-height
  (printed-height:integer <- copy 0:literal)  ; part of screen that currently has text; can't be larger than app-height
  (cursor-row:integer <- copy 0:literal)  ; position of cursor on screen; can't be larger than printed-height + 1
  (reply default-space:space-address)
])

(function down [
  (default-space:space-address <- new space:literal 30:literal/capacity)
  (0:space-address/names:browser-state <- next-input)
  (screen:terminal-address <- next-input)
  ; if at expanded, skip past nested lines
  { begin
    (no-expanded?:boolean <- less-than expanded-index:integer/space:1 0:literal)
    (break-if no-expanded?:boolean)
    (at-expanded?:boolean <- equal cursor-row:integer/space:1 expanded-index:integer/space:1)
    (break-unless at-expanded?:boolean)
    (n:integer <- instruction-trace-num-children traces:instruction-trace-address-array-address/space:1 expanded-index:integer/space:1)
    (n:integer <- add n:integer 1:literal)
    (i:integer <- copy 0:literal)
    { begin
      (done?:boolean <- greater-or-equal i:integer n:integer)
      (break-if done?:boolean)
      (cursor-row:integer/space:1 <- add cursor-row:integer/space:1 1:literal)
      (cursor-down screen:terminal-address)
      (i:integer <- add i:integer 1:literal)
      (loop)
    }
    (reply)
  }
  ; if not at bottom, move cursor down
  { begin
    (at-bottom?:boolean <- greater-or-equal cursor-row:integer/space:1 printed-height:integer/space:1)
    (break-if at-bottom?:boolean)
    (cursor-row:integer/space:1 <- add cursor-row:integer/space:1 1:literal)
    (cursor-down screen:terminal-address)
  }
])

(function up [
  (default-space:space-address <- new space:literal 30:literal/capacity)
  (0:space-address/names:browser-state <- next-input)
  (screen:terminal-address <- next-input)
  ; if at expanded, skip past nested lines
  { begin
    (no-expanded?:boolean <- less-than expanded-index:integer/space:1 0:literal)
    (break-if no-expanded?:boolean)
    (n:integer <- instruction-trace-num-children traces:instruction-trace-address-array-address/space:1 expanded-index:integer/space:1)
    (n:integer <- add n:integer 1:literal)
    (cursor-row-below-expanded:integer <- add expanded-index:integer/space:1 n:integer)
    (just-below-expanded?:boolean <- equal cursor-row:integer/space:1 cursor-row-below-expanded:integer)
    (break-unless just-below-expanded?:boolean)
    (i:integer <- copy 0:literal)
    { begin
      (done?:boolean <- greater-or-equal i:integer n:integer)
      (break-if done?:boolean)
      (cursor-row:integer/space:1 <- subtract cursor-row:integer/space:1 1:literal)
      (cursor-up screen:terminal-address)
      (i:integer <- add i:integer 1:literal)
      (loop)
    }
    (reply)
  }
  ; if not at top, move cursor up
  { begin
    (at-top?:boolean <- lesser-or-equal cursor-row:integer/space:1 0:literal)
    (break-if at-top?:boolean)
    (cursor-row:integer/space:1 <- subtract cursor-row:integer/space:1 1:literal)
    (cursor-up screen:terminal-address)
  }
])

(function to-bottom [
  (default-space:space-address <- new space:literal 30:literal/capacity)
  (0:space-address/names:browser-state <- next-input)
  (screen:terminal-address <- next-input)
  { begin
    (at-bottom?:boolean <- greater-or-equal cursor-row:integer/space:1 printed-height:integer/space:1)
    (break-if at-bottom?:boolean)
    (down 0:space-address screen:terminal-address)
    (loop)
  }
])

(function to-top [
  (default-space:space-address <- new space:literal 30:literal/capacity)
  (0:space-address/names:browser-state <- next-input)
  (screen:terminal-address <- next-input)
  { begin
    (at-top?:boolean <- lesser-or-equal cursor-row:integer/space:1 0:literal)
    (break-if at-top?:boolean)
    (up 0:space-address screen:terminal-address)
    (loop)
  }
])

(function back-to [
  (default-space:space-address <- new space:literal 30:literal/capacity)
  (0:space-address/names:browser-state <- next-input)
  (screen:terminal-address <- next-input)
  (target-row:integer <- next-input)
  { begin
    (below-target?:boolean <- greater-than cursor-row:integer/space:1 target-row:integer)
    (break-unless below-target?:boolean)
    (up 0:space-address screen:terminal-address)
    (loop)
  }
  { begin
    (above-target?:boolean <- less-than cursor-row:integer/space:1 target-row:integer)
    (break-unless above-target?:boolean)
    (down 0:space-address screen:terminal-address)
    (loop)
  }
])

(function add-line [  ; move down, adding line if necessary
  (default-space:space-address <- new space:literal 30:literal/capacity)
  (0:space-address/names:browser-state <- next-input)
  (screen:terminal-address <- next-input)
  { begin
    (at-bottom?:boolean <- greater-or-equal cursor-row:integer/space:1 printed-height:integer/space:1)
    (break-unless at-bottom?:boolean)
    { begin
      (screen-full?:boolean <- greater-or-equal app-height:integer/space:1 screen-height:integer/space:1)
      (break-unless screen-full?:boolean)
      (cursor-to-next-line screen:terminal-address)
      (cursor-up screen:terminal-address)
      (reply)
    }
    (printed-height:integer/space:1 <- add printed-height:integer/space:1 1:literal)
    ; update app-height if necessary
    { begin
      (grow-max?:boolean <- greater-than printed-height:integer/space:1 app-height:integer/space:1)
      (break-unless grow-max?:boolean)
      (app-height:integer/space:1 <- copy printed-height:integer/space:1)
    }
  }
  (cursor-row:integer/space:1 <- add cursor-row:integer/space:1 1:literal)
  (cursor-to-next-line screen:terminal-address)
])

;; initial screen state
(function print-traces-collapsed [
  (default-space:space-address <- new space:literal 30:literal/capacity)
  (0:space-address/names:browser-state <- next-input)
  (screen:terminal-address <- next-input)
  (print-traces-collapsed-from 0:space-address/browser-state screen:terminal-address 0:literal/from)
])

(function print-traces-collapsed-from [
  (default-space:space-address <- new space:literal 30:literal/capacity)
  (0:space-address/names:browser-state <- next-input)
  (screen:terminal-address <- next-input)
  (trace-index:integer <- next-input)
  (len:integer <- length traces:instruction-trace-address-array-address/space:1/deref)
  (height:integer <- min len:integer screen-height:integer/space:1)
  ; print remaining traces collapsed
  { begin
    ; until trace ends
    (trace-done?:boolean <- greater-or-equal trace-index:integer len:integer)
    (break-if trace-done?:boolean)
    ; or screen ends
    (screen-done?:boolean <- greater-or-equal cursor-row:integer/space:1 screen-height:integer/space:1)
    (break-if screen-done?:boolean)
    ; continue printing trace lines
    (tr:instruction-trace-address <- index traces:instruction-trace-address-array-address/space:1/deref trace-index:integer)
    (last-index-on-page:integer/space:1 <- copy trace-index:integer)
    (print-instruction-trace-collapsed screen:terminal-address tr:instruction-trace-address 0:space-address/browser-state)
    (trace-index:integer <- add trace-index:integer 1:literal)
    (loop)
  }
  ; empty any remaining lines
;?   ($print trace-index:integer) ;? 1
;?   ($print ((#\space literal))) ;? 1
;?   ($print app-height:integer/space:1) ;? 1
  { begin
    (done?:boolean <- greater-or-equal cursor-row:integer/space:1 app-height:integer/space:1)
    (break-if done?:boolean)
    (clear-line screen:terminal-address)
    (down 0:space-address/browser-state screen:terminal-address)
    (loop)
  }
])

(function cursor-row-to-trace-index [
  (default-space:space-address <- new space:literal 30:literal/capacity)
  (0:space-address/names:browser-state <- next-input)
  (n:integer/screen <- next-input)
  ; no row expanded? no munging needed
  { begin
    (has-expanded?:boolean <- greater-or-equal expanded-index:integer/space:1 0:literal)
    (break-if has-expanded?:boolean)
    (reply n:integer/index)
  }
  ; expanded row is below cursor-row? no munging needed
  { begin
    (above-expanded?:boolean <- lesser-or-equal cursor-row:integer/space:1/screen expanded-index:integer/space:1 )
    (break-unless above-expanded?:boolean)
    (reply n:integer/index)
  }
  (k:integer/index <- instruction-trace-num-children traces:instruction-trace-address-array-address/space:1 expanded-index:integer/space:1)
  (result:integer/index <- subtract n:integer/screen k:integer/index)
  (reply result:integer/index)
])

;; modify screen state in response to a single key
(function process-key [
  (default-space:space-address <- new space:literal 30:literal/capacity)
  (0:space-address/names:browser-state <- next-input)
  (k:keyboard-address <- next-input)
  (screen:terminal-address <- next-input)
  (c:character <- read-key k:keyboard-address silent:literal/terminal)
  { begin
    ; no key yet
    (break-if c:character)
    (reply nil:literal)
  }
  { begin
    ; user quit
    (q-pressed?:boolean <- equal c:character ((#\q literal)))
    (end-of-fake-keyboard-input?:boolean <- equal c:character ((#\null literal)))
    (quit?:boolean <- or q-pressed?:boolean end-of-fake-keyboard-input?:boolean)
    (break-unless quit?:boolean)
    (reply t:literal)
  }
  ; up/down navigation
  { begin
    (up?:boolean <- equal c:character ((up literal)))
    (k?:boolean <- equal c:character ((#\k literal)))
    (up?:boolean <- or up?:boolean k?:boolean)
    (break-unless up?:boolean)
    (up 0:space-address/browser-state screen:terminal-address)
    (reply nil:literal)
  }
  { begin
    (down?:boolean <- equal c:character ((down literal)))
    (j?:boolean <- equal c:character ((#\j literal)))
    (down?:boolean <- or down?:boolean j?:boolean)
    (break-unless down?:boolean)
    (down 0:space-address/browser-state screen:terminal-address)
    (reply nil:literal)
  }
  ; page up/page down
  { begin
    (page-up?:boolean <- equal c:character ((pgup literal)))
    (K?:boolean <- equal c:character ((#\K literal)))
    (page-up?:boolean <- or page-up?:boolean K?:boolean)
    (break-unless page-up?:boolean)
    (first-page?:boolean <- lesser-or-equal first-index-on-page:integer/space:1 0:literal)
    (break-if first-page?:boolean)
    ; move cursor to top of screen
    (to-top 0:space-address/browser-state screen:terminal-address)
    ; start drawing from previous page
    (first-index-on-page:integer/space:1 <- subtract first-index-on-page:integer/space:1 screen-height:integer/space:1)
    ; don't need to check for less than zero because page-up/page-down won't
    ; currently allow it
    (print-traces-collapsed-from 0:space-address/browser-state screen:terminal-address first-index-on-page:integer/space:1)
  }
  { begin
    (page-down?:boolean <- equal c:character ((pgdn literal)))
    (J?:boolean <- equal c:character ((#\J literal)))
    (page-down?:boolean <- or page-down?:boolean J?:boolean)
    (break-unless page-down?:boolean)
    ; if we're not past end of trace
    (len:integer <- length traces:instruction-trace-address-array-address/space:1/deref)
    (next-page-start:integer <- add last-index-on-page:integer/space:1 1:literal)
    (last-page?:boolean <- greater-or-equal next-page-start:integer len:integer)
    (break-if last-page?:boolean)
    ; move cursor to top of screen
    (to-top 0:space-address/browser-state screen:terminal-address)
    ; start drawing from next page
    (first-index-on-page:integer/space:1 <- copy next-page-start:integer)
    (print-traces-collapsed-from 0:space-address/browser-state screen:terminal-address first-index-on-page:integer/space:1)
    ; move cursor back to top of screen
    (to-top 0:space-address/browser-state screen:terminal-address)
  }
  ; enter: expand/collapse current row
  { begin
    (toggle?:boolean <- equal c:character ((#\newline literal)))
    (break-unless toggle?:boolean)
    (original-cursor-row:integer <- copy cursor-row:integer/space:1)
;?     ($print original-cursor-row:integer) ;? 1
;?     ($print (("\n" literal))) ;? 1
    (original-trace-index:integer <- cursor-row-to-trace-index 0:space-address/browser-state original-cursor-row:integer)
;?     ($print original-trace-index:integer) ;? 1
;?     ($print (("\n" literal))) ;? 1
    ; is expanded-index already set?
    { begin
      (expanded?:boolean <- greater-or-equal expanded-index:integer/space:1 0:literal)
      (break-unless expanded?:boolean)
      { begin
        ; are we at the expanded row?
        (at-expanded?:boolean <- equal cursor-row:integer/space:1 expanded-index:integer/space:1)
        (break-unless at-expanded?:boolean)
        ; print remaining lines collapsed and return
        (expanded-index:integer/space:1 <- copy -1:literal)
        (print-traces-collapsed-from 0:space-address/browser-state screen:terminal-address cursor-row:integer/space:1)
        (back-to 0:space-address/browser-state screen:terminal-address original-cursor-row:integer)
        (reply)
      }
      ; are we below the expanded row?
      { begin
        (below-expanded?:boolean <- greater-than cursor-row:integer/space:1 expanded-index:integer/space:1)
        (break-unless below-expanded?:boolean)
        ; scan up to expanded row
        { begin
          (at-expanded?:boolean <- equal cursor-row:integer/space:1 expanded-index:integer/space:1)
          (break-if at-expanded?:boolean)
          (up 0:space-address screen:terminal-address)
          (loop)
        }
        ; print traces collapsed until just before original row
        { begin
          (done?:boolean <- greater-or-equal cursor-row:integer/space:1 original-trace-index:integer)
          (break-if done?:boolean)
          (tr:instruction-trace-address <- index traces:instruction-trace-address-array-address/space:1/deref cursor-row:integer/space:1)
          (print-instruction-trace-collapsed screen:terminal-address tr:instruction-trace-address 0:space-address/browser-state)
          (loop)
        }
        ; fall through
      }
    }
    ; expand original row and print traces below it
    (expanded-index:integer/space:1 <- copy original-trace-index:integer)
    (tr:instruction-trace-address <- index traces:instruction-trace-address-array-address/space:1/deref original-trace-index:integer)
    (print-instruction-trace screen:terminal-address tr:instruction-trace-address 0:space-address/browser-state)
    (next-index:integer <- add original-trace-index:integer 1:literal)
;?     ($print next-index:integer) ;? 1
;?     ($print (("\n" literal))) ;? 1
    (print-traces-collapsed-from 0:space-address/browser-state screen:terminal-address next-index:integer)
    (back-to 0:space-address/browser-state screen:terminal-address original-trace-index:integer)
    (reply nil:literal)
  }
  (reply nil:literal)
])

(function browse-trace [
  (default-space:space-address <- new space:literal 30:literal/capacity)
  (x:string-address <- next-input)
  (screen-height:integer <- next-input)
;?   ($start-tracing) ;? 1
;?   (x:string-address <- new
;? "schedule: main
;? run: main 0: (((1 integer)) <- ((copy)) ((1 literal)))
;? run: main 0: 1 => ((1 integer))
;? mem: ((1 integer)): 1 <= 1
;? run: main 1: (((2 integer)) <- ((copy)) ((3 literal)))
;? run: main 1: 3 => ((2 integer))
;? mem: ((2 integer)): 2 <= 3
;? run: main 2: (((3 integer)) <- ((add)) ((1 integer)) ((2 integer)))
;? mem: ((1 integer)) => 1
;? mem: ((2 integer)) => 3
;? run: main 2: 4 => ((3 integer))
;? mem: ((3 integer)): 3 <= 4
;? schedule:  done with routine")
  (s:stream-address <- init-stream x:string-address)
  (traces:instruction-trace-address-array-address <- parse-traces s:stream-address)
  (0:space-address/names:browser-state <- browser-state traces:instruction-trace-address-array-address screen-height:integer)
  (cursor-mode)
  (print-traces-collapsed 0:space-address/browser-state nil:literal/terminal)
  { begin
    (quit?:boolean <- process-key 0:space-address/browser-state nil:literal/keyboard nil:literal/terminal)
    (break-if quit?:boolean)
    (loop)
  }
  ; move cursor to bottom before exiting
  (to-bottom 0:space-address/browser-state nil:literal/terminal)
  (retro-mode)
])

(function main [
  (default-space:space-address <- new space:literal 30:literal/capacity)
  (x:string-address <- new
"run: main 0: a b c
mem: 0
run: main 1: d e f
mem: 1
mem: 1
mem: 1
run: main 2: g hi
run: main 3: j
mem: 3
run: main 4: k
run: main 5: l
run: main 6: m
run: main 7: n
run: main 8: o")
  (browse-trace x:string-address 5:literal/screen-height)
])