about summary refs log tree commit diff stats
path: root/tools/treeshake_all
blob: bdf14c906a363c4e4af35e1751b725244b38a44c (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
#!/bin/sh
# Build minimal-size versions of all apps.
# Hacky; only intended for some stats at the moment.

set -e

[ ! -f tools/treeshake ] && {
  echo building tools/treeshake
  c++ -g -O3 tools/treeshake.cc -o tools/treeshake
}

export OS=${OS:-linux}

process() {
  app=$1
  tools/treeshake_translate init.$OS 0*.subx apps/subx-params.subx apps/$app.subx
  echo "LoC $(cat apps/$app.subx |wc -l) => $(grep -vh '^\s*$\|^\s*#' apps/$app.subx |tools/treeshake |wc -l)"
  echo "LoC including common libraries: $(cat a.in |wc -l) => $(cat a.treeshake |wc -l)"
  echo "binary size: $(ls -lh apps/$app |column 5) => $(ls -lh a.elf |column 5)"
}

if [ $# -gt 0 ]
then
  process $1
  exit 0
fi

echo "== deleting dead code"
for app in factorial crenshaw2-1 crenshaw2-1b handle hex survey pack dquotes assort tests sigils calls braces
do
  echo "- $app"
  process $app
  mv a.in apps/$app.in
  mv a.treeshake apps/$app.treeshake
  mv a.elf apps/$app.treeshake.bin
done

echo "== testing treeshaken binaries"
for app in factorial crenshaw2-1 crenshaw2-1b
do
  echo $app
  tools/test_treeshake_translate init.$OS 0[0-8]*.subx apps/$app.subx
  diff apps/$app a.elf
done

for app in hex survey pack assort dquotes tests
do
  echo $app
  tools/test_treeshake_translate init.$OS 0[0-8]*.subx apps/subx-params.subx apps/$app.subx
  diff apps/$app a.elf
done

for app in sigils calls braces
do
  echo $app
  tools/test_treeshake_translate init.$OS 0*.subx apps/subx-params.subx apps/$app.subx
  diff apps/$app a.elf
done

app=mu
echo $app
tools/test_treeshake_translate init.$OS [0-9]*.subx apps/$app.subx
diff apps/$app a.elf
revision' href='/akkartik/mu/blame/shell/macroexpand.mu?h=hlt&id=1648a04cc0e92c0583c9d283ed6b6108120f820f'>^
4692c48f ^




a1cfadc7 ^







4692c48f ^
a1cfadc7 ^
edae464d ^


a1cfadc7 ^


edae464d ^

4692c48f ^
a1cfadc7 ^
edae464d ^


a1cfadc7 ^








4692c48f ^
a1cfadc7 ^








4692c48f ^
e2f18e88 ^








4692c48f ^
a1cfadc7 ^

f791cd9d ^
a1cfadc7 ^

















64d3e8e2 ^






a1cfadc7 ^


af6fc799 ^














a1cfadc7 ^

4692c48f ^
57fd40d5 ^
f791cd9d ^
57fd40d5 ^








4692c48f ^
7105b73f ^





33311443 ^

e9d2f00e ^





7105b73f ^


4692c48f ^
















bb1ccae2 ^



57fd40d5 ^

bb1ccae2 ^
57fd40d5 ^





bb1ccae2 ^
af6fc799 ^





bb1ccae2 ^
af6fc799 ^






57fd40d5 ^

4692c48f ^
f791cd9d ^
57fd40d5 ^











af6fc799 ^














57fd40d5 ^

4692c48f ^
57fd40d5 ^



a1cfadc7 ^
edae464d ^
f791cd9d ^




edae464d ^
f791cd9d ^






57fd40d5 ^









61a68452 ^
0dda332c ^
91b7b0be ^
af6fc799 ^














f791cd9d ^





4692c48f ^
f791cd9d ^

4692c48f ^





f791cd9d ^

64d3e8e2 ^


f791cd9d ^








edae464d ^
91b7b0be ^
af6fc799 ^














57fd40d5 ^
9156823c ^
a1cfadc7 ^
e9d2f00e ^

































33311443 ^
e9d2f00e ^
33311443 ^



e9d2f00e ^

























a1cfadc7 ^






bb1ccae2 ^
0dda332c ^
a1cfadc7 ^





f691782d ^






76ef912e ^
a1cfadc7 ^



e2ab1b30 ^
a1cfadc7 ^

e2ab1b30 ^
f691782d ^
a1cfadc7 ^


f691782d ^
a1cfadc7 ^









bb1ccae2 ^
0dda332c ^
a1cfadc7 ^





f691782d ^






a1cfadc7 ^




e2ab1b30 ^
a1cfadc7 ^

e2ab1b30 ^
f691782d ^
a1cfadc7 ^

f691782d ^
a1cfadc7 ^

f791cd9d ^







bb1ccae2 ^
0dda332c ^
f791cd9d ^





f691782d ^






f791cd9d ^




e2ab1b30 ^
f791cd9d ^

e2ab1b30 ^
f691782d ^
f791cd9d ^


f691782d ^
f791cd9d ^

7105b73f ^
e9d2f00e ^



d2094133 ^
e9d2f00e ^

d2094133 ^
e9d2f00e ^









d2094133 ^
e9d2f00e ^








e9d2f00e ^

7105b73f ^






bb1ccae2 ^
0dda332c ^
7105b73f ^





f691782d ^







7105b73f ^



e2ab1b30 ^
7105b73f ^

e2ab1b30 ^
f691782d ^
7105b73f ^

f691782d ^
7105b73f ^









bb1ccae2 ^
0dda332c ^
7105b73f ^





f691782d ^






26e9387d ^
7105b73f ^



e2ab1b30 ^
7105b73f ^

e2ab1b30 ^
f691782d ^
26e9387d ^
7105b73f ^

f691782d ^
7105b73f ^



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
                                                                                               

                                    

                                                         



                                                                



                                                                          


                              
                   

                                     
                          
   


                                                



                                                                          
                    

                           

                                                         



                                                                



                                                                          


                              

 

                                                                                                                       
                                                      




                                          

                                      

                                                         



                                                                



                                                                          


                              
                   
                             
                                 

                                              




                                       







                                      
                             
   


                                                   


                                       

                  
                             
                                     


                                                        








                                               
                             








                                             
                             








                                             
                             

                                                              
                                         

















                                                                               






                                                    


                      














                                                                            

                 
                             
                                            
                            








                                                       
                             





                                                           

                                                     





                                                                             


                      
















                                                                 



                                                             

              
                                     





                                                                            
                                    





                                                                  
                               






                                                                            

                       
                             
                          











                                                                            














                                                                            

                       
                             



                                                          
                                                          
   




                                                                       
              






                                                                 









                                                                            
                          
                                                                                                                                 
                      














                                                                            





                                                               
                               

                            





                                                 

                                                                             


                                                








                                                
   
                    














                                                                          
               
 
 

































                                                                           
                                 
                                              



                                                 

























                                                                  






                                                                 
                                                                                  
                                                       





                                                               






                                                                      
                                                                



                                                   
                                                                        

                                                                    
                                                               
                                            


                                                       
                                                                        









                                                                 
                                                                                  
                                                       





                                                               






                                                                      




                                                     
                                                                        

                                                                    
                                                               
                                            

                                                      
                                                                        

                                                             







                                                                 
                                                                                  
                                                       





                                                               






                                                                      




                                                     
                                                                        

                                                                    
                                                               
                                            


                                                       
                                                                        

                                                        
 



                                                                 
                                                           

                                                            
                                                  









                                                                      
                                                                         








                                                                                          

 






                                                                 
                                                                                  
                                                       





                                                                    







                                                                         



                                                                    
                                                                        

                                                                    
                                                               
                                            

                                                      
                                                                        









                                                                  
                                                                                  
                                                       





                                                                          






                                                                                
                                                     



                                                                          
                                                                        

                                                                    
                                                               
                                            
                                                       

                                                      
                                                                        



                                                                         
fn macroexpand expr-ah: (addr handle cell), globals: (addr global-table), trace: (addr trace) {
  # trace "macroexpand " expr-ah {{{
  {
    var should-trace?/eax: boolean <- should-trace? trace
    compare should-trace?, 0/false
    break-if-=
    var stream-storage: (stream byte 0x200)
    var stream/ecx: (addr stream byte) <- address stream-storage
    write stream, "macroexpand "
    var nested-trace-storage: trace
    var nested-trace/edi: (addr trace) <- address nested-trace-storage
    initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
    print-cell expr-ah, stream, nested-trace
    trace trace, "mac", stream
  }
  # }}}
  trace-lower trace
#?   clear-screen 0
#?   set-cursor-position 0, 0x20 0x20
  # loop until convergence
  {
    var error?/eax: boolean <- has-errors? trace
    compare error?, 0/false
    break-if-!=
    var expanded?/eax: boolean <- macroexpand-iter expr-ah, globals, trace
    compare expanded?, 0/false
    loop-if-!=
  }
  trace-higher trace
  # trace "=> " expr-ah {{{
  {
    var should-trace?/eax: boolean <- should-trace? trace
    compare should-trace?, 0/false
    break-if-=
    var stream-storage: (stream byte 0x200)
    var stream/ecx: (addr stream byte) <- address stream-storage
    write stream, "=> "
    var nested-trace-storage: trace
    var nested-trace/edi: (addr trace) <- address nested-trace-storage
    initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
    print-cell expr-ah, stream, nested-trace
    trace trace, "mac", stream
  }
  # }}}
}

# return true if we found any macros
fn macroexpand-iter _expr-ah: (addr handle cell), globals: (addr global-table), trace: (addr trace) -> _/eax: boolean {
  var expr-ah/esi: (addr handle cell) <- copy _expr-ah
  {
    compare expr-ah, 0
    break-if-!=
    abort "macroexpand-iter: NULL expr-ah"
  }
  # trace "macroexpand-iter " expr {{{
  {
    var should-trace?/eax: boolean <- should-trace? trace
    compare should-trace?, 0/false
    break-if-=
    var stream-storage: (stream byte 0x200)
    var stream/ecx: (addr stream byte) <- address stream-storage
    write stream, "macroexpand-iter "
    var nested-trace-storage: trace
    var nested-trace/edi: (addr trace) <- address nested-trace-storage
    initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
    print-cell expr-ah, stream, nested-trace
    trace trace, "mac", stream
  }
  # }}}
  trace-lower trace
  debug-print "a", 7/fg, 0/bg
  # if expr is a non-pair, return
  var expr/eax: (addr cell) <- lookup *expr-ah
  {
    compare expr, 0
    break-if-!=
    abort "macroexpand-iter: NULL expr"
  }
  {
    var nil?/eax: boolean <- nil? expr
    compare nil?, 0/false
    break-if-=
    # nil is a literal
    trace-text trace, "mac", "nil"
    trace-higher trace
    return 0/false
  }
  debug-print "b", 7/fg, 0/bg
  {
    var expr-type/eax: (addr int) <- get expr, type
    compare *expr-type, 0/pair
    break-if-=
    # non-pairs are literals
    trace-text trace, "mac", "non-pair"
    trace-higher trace
    return 0/false
  }
  debug-print "c", 7/fg, 0/bg
  # if expr is a literal pair, return
  var first-ah/ebx: (addr handle cell) <- get expr, left
  var rest-ah/ecx: (addr handle cell) <- get expr, right
  var first/eax: (addr cell) <- lookup *first-ah
  {
    var litfn?/eax: boolean <- litfn? first
    compare litfn?, 0/false
    break-if-=
    # litfn is a literal
    trace-text trace, "mac", "literal function"
    trace-higher trace
    return 0/false
  }
  debug-print "d", 7/fg, 0/bg
  {
    var litmac?/eax: boolean <- litmac? first
    compare litmac?, 0/false
    break-if-=
    # litmac is a literal
    trace-text trace, "mac", "literal macro"
    trace-higher trace
    return 0/false
  }
  debug-print "e", 7/fg, 0/bg
  {
    var litimg?/eax: boolean <- litimg? first
    compare litimg?, 0/false
    break-if-=
    # litimg is a literal
    trace-text trace, "mac", "literal image"
    trace-higher trace
    return 0/false
  }
  debug-print "f", 7/fg, 0/bg
  var result/edi: boolean <- copy 0/false
  # for each builtin, expand only what will later be evaluated
  $macroexpand-iter:anonymous-function: {
    var fn?/eax: boolean <- fn? first
    compare fn?, 0/false
    break-if-=
    # fn: expand every expression in the body
    trace-text trace, "mac", "anonymous function"
    # skip parameters
    var rest/eax: (addr cell) <- lookup *rest-ah
    {
      rest-ah <- get rest, right
      rest <- lookup *rest-ah
      {
        var done?/eax: boolean <- nil? rest
        compare done?, 0/false
      }
      break-if-!=
      var curr-ah/eax: (addr handle cell) <- get rest, left
      var macro-found?/eax: boolean <- macroexpand-iter curr-ah, globals, trace
      result <- or macro-found?
      {
        var error?/eax: boolean <- has-errors? trace
        compare error?, 0/false
        break-if-=
        trace-higher trace
        return result
      }
      loop
    }
    trace-higher trace
    # trace "fn=> " _expr-ah {{{
    {
      var should-trace?/eax: boolean <- should-trace? trace
      compare should-trace?, 0/false
      break-if-=
      var stream-storage: (stream byte 0x200)
      var stream/ecx: (addr stream byte) <- address stream-storage
      write stream, "fn=> "
      var nested-trace-storage: trace
      var nested-trace/edi: (addr trace) <- address nested-trace-storage
      initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
      print-cell _expr-ah, stream, nested-trace
      trace trace, "mac", stream
    }
    # }}}
    return result
  }
  debug-print "g", 7/fg, 0/bg
  # builtins with "special" evaluation rules
  $macroexpand-iter:quote: {
    # trees starting with single quote create literals
    var quote?/eax: boolean <- symbol-equal? first, "'"
    compare quote?, 0/false
    break-if-=
    #
    trace-text trace, "mac", "quote"
    trace-higher trace
    return 0/false
  }
  debug-print "h", 7/fg, 0/bg
  $macroexpand-iter:backquote: {
    # nested backquote not supported for now
    var backquote?/eax: boolean <- symbol-equal? first, "`"
    compare backquote?, 0/false
    break-if-=
    #
#?     set-cursor-position 0/screen, 0x40/x 0x10/y
#?     dump-cell-from-cursor-over-full-screen rest-ah
    var double-unquote-found?/eax: boolean <- look-for-double-unquote rest-ah
    compare double-unquote-found?, 0/false
    {
      break-if-=
      error trace, "double unquote not supported yet"
    }
    trace-higher trace
    return 0/false
  }
  $macroexpand-iter:unquote: {
    # nested backquote not supported for now
    var unquote?/eax: boolean <- symbol-equal? first, ","
    compare unquote?, 0/false
    break-if-=
    error trace, "unquote (,) must be within backquote (`)"
    return 0/false
  }
  $macroexpand-iter:unquote-splice: {
    # nested backquote not supported for now
    var unquote-splice?/eax: boolean <- symbol-equal? first, ",@"
    compare unquote-splice?, 0/false
    break-if-=
    error trace, "unquote (,@) must be within backquote (`)"
    return 0/false
  }
  debug-print "i", 7/fg, 0/bg
  $macroexpand-iter:define: {
    # trees starting with "define" define globals
    var define?/eax: boolean <- symbol-equal? first, "define"
    compare define?, 0/false
    break-if-=
    #
    trace-text trace, "mac", "define"
    var rest/eax: (addr cell) <- lookup *rest-ah
    rest-ah <- get rest, right  # skip name
    rest <- lookup *rest-ah
    var val-ah/edx: (addr handle cell) <- get rest, left
    var macro-found?/eax: boolean <- macroexpand-iter val-ah, globals, trace
    trace-higher trace
    # trace "define=> " _expr-ah {{{
    {
      var should-trace?/eax: boolean <- should-trace? trace
      compare should-trace?, 0/false
      break-if-=
      var stream-storage: (stream byte 0x200)
      var stream/ecx: (addr stream byte) <- address stream-storage
      write stream, "define=> "
      var nested-trace-storage: trace
      var nested-trace/edi: (addr trace) <- address nested-trace-storage
      initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
      print-cell _expr-ah, stream, nested-trace
      trace trace, "mac", stream
    }
    # }}}
    return macro-found?
  }
  debug-print "j", 7/fg, 0/bg
  $macroexpand-iter:set: {
    # trees starting with "set" mutate bindings
    var set?/eax: boolean <- symbol-equal? first, "set"
    compare set?, 0/false
    break-if-=
    #
    trace-text trace, "mac", "set"
    var rest/eax: (addr cell) <- lookup *rest-ah
    rest-ah <- get rest, right  # skip name
    rest <- lookup *rest-ah
    var val-ah/edx: (addr handle cell) <- get rest, left
    var macro-found?/eax: boolean <- macroexpand-iter val-ah, globals, trace
    trace-higher trace
    # trace "set=> " _expr-ah {{{
    {
      var should-trace?/eax: boolean <- should-trace? trace
      compare should-trace?, 0/false
      break-if-=
      var stream-storage: (stream byte 0x200)
      var stream/ecx: (addr stream byte) <- address stream-storage
      write stream, "set=> "
      var nested-trace-storage: trace
      var nested-trace/edi: (addr trace) <- address nested-trace-storage
      initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
      print-cell _expr-ah, stream, nested-trace
      trace trace, "mac", stream
    }
    # }}}
    return macro-found?
  }
  debug-print "k", 7/fg, 0/bg
  # 'and' is like a function for macroexpansion purposes
  # 'or' is like a function for macroexpansion purposes
  # 'if' is like a function for macroexpansion purposes
  # 'while' is like a function for macroexpansion purposes
  # if car(expr) is a symbol defined as a macro, expand it
  {
    var definition-h: (handle cell)
    var definition-ah/edx: (addr handle cell) <- address definition-h
    maybe-lookup-symbol-in-globals first, definition-ah, globals, trace
    var definition/eax: (addr cell) <- lookup *definition-ah
    compare definition, 0
    break-if-=
    # definition found
    {
      var definition-type/eax: (addr int) <- get definition, type
      compare *definition-type, 0/pair
    }
    break-if-!=
    # definition is a pair
    {
      var definition-car-ah/eax: (addr handle cell) <- get definition, left
      var definition-car/eax: (addr cell) <- lookup *definition-car-ah
      var macro?/eax: boolean <- litmac? definition-car
      compare macro?, 0/false
    }
    break-if-=
    # definition is a macro
    var macro-definition-ah/eax: (addr handle cell) <- get definition, right
    # TODO: check car(macro-definition) is litfn
#?     turn-on-debug-print
    apply macro-definition-ah, rest-ah, expr-ah, globals, trace, 0/no-screen, 0/no-keyboard, 0/definitions-created, 0/call-number
    trace-higher trace
    # trace "1=> " _expr-ah {{{
    {
      var should-trace?/eax: boolean <- should-trace? trace
      compare should-trace?, 0/false
      break-if-=
      var stream-storage: (stream byte 0x200)
      var stream/ecx: (addr stream byte) <- address stream-storage
      write stream, "1=> "
      var nested-trace-storage: trace
      var nested-trace/edi: (addr trace) <- address nested-trace-storage
      initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
      print-cell _expr-ah, stream, nested-trace
      trace trace, "mac", stream
    }
    # }}}
    return 1/true
  }
  # no macro found; process any macros within args
  trace-text trace, "mac", "recursing into function definition"
  var curr-ah/ebx: (addr handle cell) <- copy first-ah
  $macroexpand-iter:loop: {
    debug-print "l", 7/fg, 0/bg
#?     clear-screen 0/screen
#?     dump-trace trace
    {
      var foo/eax: (addr cell) <- lookup *curr-ah
      compare foo, 0
      break-if-!=
      abort "macroexpand-iter: NULL in loop"
    }
    var macro-found?/eax: boolean <- macroexpand-iter curr-ah, globals, trace
    result <- or macro-found?
    var error?/eax: boolean <- has-errors? trace
    compare error?, 0/false
    break-if-!=
    var rest/eax: (addr cell) <- lookup *rest-ah
    {
      var nil?/eax: boolean <- nil? rest
      compare nil?, 0/false
    }
    break-if-!=
    curr-ah <- get rest, left
    rest-ah <- get rest, right
    loop
  }
  trace-higher trace
  # trace "=> " _expr-ah {{{
  {
    var should-trace?/eax: boolean <- should-trace? trace
    compare should-trace?, 0/false
    break-if-=
    var stream-storage: (stream byte 0x200)
    var stream/ecx: (addr stream byte) <- address stream-storage
    write stream, "=> "
    var nested-trace-storage: trace
    var nested-trace/edi: (addr trace) <- address nested-trace-storage
    initialize-trace nested-trace, 1/only-errors, 0x10/capacity, 0/visible
    print-cell _expr-ah, stream, nested-trace
    trace trace, "mac", stream
  }
  # }}}
  return result
}

fn look-for-double-unquote _expr-ah: (addr handle cell) -> _/eax: boolean {
  # if expr is a non-pair, return false
  var expr-ah/eax: (addr handle cell) <- copy _expr-ah
  var expr/eax: (addr cell) <- lookup *expr-ah
  {
    var nil?/eax: boolean <- nil? expr
    compare nil?, 0/false
    break-if-=
    return 0/false
  }
  {
    var expr-type/eax: (addr int) <- get expr, type
    compare *expr-type, 0/pair
    break-if-=
    return 0/false
  }
  var cdr-ah/ecx: (addr handle cell) <- get expr, right
  var car-ah/ebx: (addr handle cell) <- get expr, left
  var car/eax: (addr cell) <- lookup *car-ah
  # if car is unquote or unquote-splice, check if cadr is unquote or
  # unquote-splice.
  $look-for-double-unquote:check: {
    # if car is not an unquote, break
    {
      {
        var unquote?/eax: boolean <- symbol-equal? car, ","
        compare unquote?, 0/false
      }
      break-if-!=
      var unquote-splice?/eax: boolean <- symbol-equal? car, ",@"
      compare unquote-splice?, 0/false
      break-if-!=
      break $look-for-double-unquote:check
    }
    # if cdr is not a pair, break
    var cdr/eax: (addr cell) <- lookup *cdr-ah
    var cdr-type/ecx: (addr int) <- get cdr, type
    compare *cdr-type, 0/pair
    break-if-!=
    # if cadr is not an unquote, break
    var cadr-ah/eax: (addr handle cell) <- get cdr, left
    var cadr/eax: (addr cell) <- lookup *cadr-ah
    {
      {
        var unquote?/eax: boolean <- symbol-equal? cadr, ","
        compare unquote?, 0/false
      }
      break-if-!=
      var unquote-splice?/eax: boolean <- symbol-equal? cadr, ",@"
      compare unquote-splice?, 0/false
      break-if-!=
      break $look-for-double-unquote:check
    }
    # error
    return 1/true
  }
  var result/eax: boolean <- look-for-double-unquote car-ah
  compare result, 0/false
  {
    break-if-=
    return result
  }
  result <- look-for-double-unquote cdr-ah
  return result
}

fn test-macroexpand {
  var globals-storage: global-table
  var globals/edx: (addr global-table) <- address globals-storage
  initialize-globals globals
  # new macro: m
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "(define m (litmac litfn () (a b) `(+ ,a ,b)))"
  edit-sandbox sandbox, 0x13/ctrl-s, globals, 0/no-disk
  # invoke macro
  initialize-sandbox-with sandbox, "(m 3 4)"
  var gap-ah/ecx: (addr handle gap-buffer) <- get sandbox, data
  var gap/eax: (addr gap-buffer) <- lookup *gap-ah
  var result-h: (handle cell)
  var result-ah/ebx: (addr handle cell) <- address result-h
  var trace-storage: trace
  var trace/ecx: (addr trace) <- address trace-storage
  initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
  read-cell gap, result-ah, trace
  var dummy/eax: boolean <- macroexpand-iter result-ah, globals, trace
  var error?/eax: boolean <- has-errors? trace
  check-not error?, "F - test-macroexpand/error"
#?   dump-cell-from-cursor-over-full-screen result-ah, 4/fg 0/bg
  var _result/eax: (addr cell) <- lookup *result-ah
  var result/edi: (addr cell) <- copy _result
  # expected
  initialize-sandbox-with sandbox, "(+ 3 4)"
  var expected-gap-ah/edx: (addr handle gap-buffer) <- get sandbox, data
  var expected-gap/eax: (addr gap-buffer) <- lookup *expected-gap-ah
  var expected-h: (handle cell)
  var expected-ah/edx: (addr handle cell) <- address expected-h
  read-cell expected-gap, expected-ah, trace
#?   dump-cell-from-cursor-over-full-screen expected-ah
  var expected/eax: (addr cell) <- lookup *expected-ah
  #
  var assertion/eax: boolean <- cell-isomorphic? result, expected, trace
  check assertion, "F - test-macroexpand"
}

fn test-macroexpand-inside-anonymous-fn {
  var globals-storage: global-table
  var globals/edx: (addr global-table) <- address globals-storage
  initialize-globals globals
  # new macro: m
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "(define m (litmac litfn () (a b) `(+ ,a ,b)))"
  edit-sandbox sandbox, 0x13/ctrl-s, globals, 0/no-disk
  # invoke macro
  initialize-sandbox-with sandbox, "(fn() (m 3 4))"
  var gap-ah/ecx: (addr handle gap-buffer) <- get sandbox, data
  var gap/eax: (addr gap-buffer) <- lookup *gap-ah
  var result-h: (handle cell)
  var result-ah/ebx: (addr handle cell) <- address result-h
  var trace-storage: trace
  var trace/ecx: (addr trace) <- address trace-storage
  initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
  read-cell gap, result-ah, trace
  var dummy/eax: boolean <- macroexpand-iter result-ah, globals, trace
  var error?/eax: boolean <- has-errors? trace
  check-not error?, "F - test-macroexpand-inside-anonymous-fn/error"
#?   dump-cell-from-cursor-over-full-screen result-ah
  var _result/eax: (addr cell) <- lookup *result-ah
  var result/edi: (addr cell) <- copy _result
  # expected
  initialize-sandbox-with sandbox, "(fn() (+ 3 4))"
  var expected-gap-ah/edx: (addr handle gap-buffer) <- get sandbox, data
  var expected-gap/eax: (addr gap-buffer) <- lookup *expected-gap-ah
  var expected-h: (handle cell)
  var expected-ah/edx: (addr handle cell) <- address expected-h
  read-cell expected-gap, expected-ah, trace
  var expected/eax: (addr cell) <- lookup *expected-ah
  #
  var assertion/eax: boolean <- cell-isomorphic? result, expected, trace
  check assertion, "F - test-macroexpand-inside-anonymous-fn"
}

fn test-macroexpand-inside-fn-call {
  var globals-storage: global-table
  var globals/edx: (addr global-table) <- address globals-storage
  initialize-globals globals
  # new macro: m
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "(define m (litmac litfn () (a b) `(+ ,a ,b)))"
  edit-sandbox sandbox, 0x13/ctrl-s, globals, 0/no-disk
  # invoke macro
  initialize-sandbox-with sandbox, "((fn() (m 3 4)))"
  var gap-ah/ecx: (addr handle gap-buffer) <- get sandbox, data
  var gap/eax: (addr gap-buffer) <- lookup *gap-ah
  var result-h: (handle cell)
  var result-ah/ebx: (addr handle cell) <- address result-h
  var trace-storage: trace
  var trace/ecx: (addr trace) <- address trace-storage
  initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
  read-cell gap, result-ah, trace
  var dummy/eax: boolean <- macroexpand-iter result-ah, globals, trace
  var error?/eax: boolean <- has-errors? trace
  check-not error?, "F - test-macroexpand-inside-fn-call/error"
#?   dump-cell-from-cursor-over-full-screen result-ah
  var _result/eax: (addr cell) <- lookup *result-ah
  var result/edi: (addr cell) <- copy _result
  # expected
  initialize-sandbox-with sandbox, "((fn() (+ 3 4)))"
  var expected-gap-ah/edx: (addr handle gap-buffer) <- get sandbox, data
  var expected-gap/eax: (addr gap-buffer) <- lookup *expected-gap-ah
  var expected-h: (handle cell)
  var expected-ah/edx: (addr handle cell) <- address expected-h
  read-cell expected-gap, expected-ah, trace
#?   dump-cell-from-cursor-over-full-screen expected-ah
  var expected/eax: (addr cell) <- lookup *expected-ah
  #
  var assertion/eax: boolean <- cell-isomorphic? result, expected, trace
  check assertion, "F - test-macroexpand-inside-fn-call"
}

fn test-macroexpand-repeatedly-with-backquoted-arg {
  var globals-storage: global-table
  var globals/edx: (addr global-table) <- address globals-storage
  initialize-globals globals
  # macroexpand an expression with a backquote but no macro
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "(cons 1 `(3))"
  var gap-ah/ecx: (addr handle gap-buffer) <- get sandbox, data
  var gap/eax: (addr gap-buffer) <- lookup *gap-ah
  var result-h: (handle cell)
  var result-ah/ebx: (addr handle cell) <- address result-h
  var trace-storage: trace
  var trace/ecx: (addr trace) <- address trace-storage
  initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
  read-cell gap, result-ah, trace
  var dummy/eax: boolean <- macroexpand-iter result-ah, globals, trace
  var error?/eax: boolean <- has-errors? trace
  check-not error?, "F - test-macroexpand-repeatedly-with-backquoted-arg"
  {
    compare error?, 0/false
    break-if-=
    # we need space to display traces, so just stop rendering future tests on failure here
    dump-trace trace
    {
      loop
    }
  }
}

fn pending-test-macroexpand-inside-backquote-unquote {
  var globals-storage: global-table
  var globals/edx: (addr global-table) <- address globals-storage
  initialize-globals globals
  # new macro: m
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "(define m (litmac litfn () (a b) `(+ ,a ,b)))"
  edit-sandbox sandbox, 0x13/ctrl-s, globals, 0/no-disk
  # invoke macro
  initialize-sandbox-with sandbox, "`(print [result is ] ,(m 3 4)))"
  var gap-ah/ecx: (addr handle gap-buffer) <- get sandbox, data
  var gap/eax: (addr gap-buffer) <- lookup *gap-ah
  var result-h: (handle cell)
  var result-ah/ebx: (addr handle cell) <- address result-h
  var trace-storage: trace
  var trace/ecx: (addr trace) <- address trace-storage
  initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
  read-cell gap, result-ah, trace
  var dummy/eax: boolean <- macroexpand-iter result-ah, globals, trace
  var error?/eax: boolean <- has-errors? trace
  check-not error?, "F - test-macroexpand-inside-backquote-unquote/error"
#?   dump-cell-from-cursor-over-full-screen result-ah
  var _result/eax: (addr cell) <- lookup *result-ah
  var result/edi: (addr cell) <- copy _result
  # expected
  initialize-sandbox-with sandbox, "`(print [result is ] ,(+ 3 4)))"
  var expected-gap-ah/edx: (addr handle gap-buffer) <- get sandbox, data
  var expected-gap/eax: (addr gap-buffer) <- lookup *expected-gap-ah
  var expected-h: (handle cell)
  var expected-ah/edx: (addr handle cell) <- address expected-h
  read-cell expected-gap, expected-ah, trace
  var expected/eax: (addr cell) <- lookup *expected-ah
  #
  var assertion/eax: boolean <- cell-isomorphic? result, expected, trace
  check assertion, "F - test-macroexpand-inside-backquote-unquote"
}

fn pending-test-macroexpand-inside-nested-backquote-unquote {
  var globals-storage: global-table
  var globals/edx: (addr global-table) <- address globals-storage
  initialize-globals globals
  # new macro: m
  var sandbox-storage: sandbox
  var sandbox/esi: (addr sandbox) <- address sandbox-storage
  initialize-sandbox-with sandbox, "(define m (litmac litfn () (a b) `(+ ,a ,b)))"
  edit-sandbox sandbox, 0x13/ctrl-s, globals, 0/no-disk
  # invoke macro
  initialize-sandbox-with sandbox, "`(a ,(m 3 4) `(b ,(m 3 4) ,,(m 3 4)))"
  var gap-ah/ecx: (addr handle gap-buffer) <- get sandbox, data
  var gap/eax: (addr gap-buffer) <- lookup *gap-ah
  var result-h: (handle cell)
  var result-ah/ebx: (addr handle cell) <- address result-h
  var trace-storage: trace
  var trace/ecx: (addr trace) <- address trace-storage
  initialize-trace trace, 1/only-errors, 0x10/capacity, 0/visible
  read-cell gap, result-ah, trace
  var dummy/eax: boolean <- macroexpand-iter result-ah, globals, trace
  var error?/eax: boolean <- has-errors? trace
  check-not error?, "F - test-macroexpand-inside-nested-backquote-unquote/error"
#?   dump-cell-from-cursor-over-full-screen result-ah
  var _result/eax: (addr cell) <- lookup *result-ah
  var result/edi: (addr cell) <- copy _result
  # expected
  initialize-sandbox-with sandbox, "`(a ,(+ 3 4) `(b ,(m 3 4) ,,(+ 3 4)))"
  var expected-gap-ah/edx: (addr handle gap-buffer) <- get sandbox, data
  var expected-gap/eax: (addr gap-buffer) <- lookup *expected-gap-ah
  var expected-h: (handle cell)
  var expected-ah/edx: (addr handle cell) <- address expected-h
  read-cell expected-gap, expected-ah, trace
#?   dump-cell-from-cursor-over-full-screen expected-ah
  var expected/eax: (addr cell) <- lookup *expected-ah
  #
  var assertion/eax: boolean <- cell-isomorphic? result, expected, trace
  check assertion, "F - test-macroexpand-inside-nested-backquote-unquote"
}

# TODO: unquote-splice, nested and unnested