https://github.com/akkartik/mu/blob/main/linux/tests.subx
  1 # Generate code for a new function called 'run-tests' which calls in sequence
  2 # all functions starting with 'test-'.
  3 #
  4 # To build:
  5 #   $ bootstrap/bootstrap translate [01]*.subx subx-params.subx tests.subx  -o tests
  6 
  7 == code
  8 #   instruction                     effective address                                                   register    displacement    immediate
  9 # . op          subop               mod             rm32          base        index         scale       r32
 10 # . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes
 11 
 12 Entry:  # run tests if necessary, convert stdin if not
 13     # . prologue
 14     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 15 
 16     # initialize heap
 17     # . Heap = new-segment(Heap-size)
 18     # . . push args
 19     68/push  Heap/imm32
 20     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Heap-size/disp32                  # push *Heap-size
 21     # . . call
 22     e8/call  new-segment/disp32
 23     # . . discard args
 24     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 25     # initialize-trace-stream(256KB)
 26     # . . push args
 27     68/push  0x40000/imm32/256KB
 28     # . . call
 29     e8/call  initialize-trace-stream/disp32
 30     # . . discard args
 31     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
 32 
 33     # - if argc > 1 and argv[1] == "test", then return run_tests()
 34     # if (argc <= 1) goto run-main
 35     81          7/subop/compare     1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0/disp8         1/imm32           # compare *ebp
 36     7e/jump-if-<=  $subx-tests-main:interactive/disp8
 37     # if (!kernel-string-equal?(argv[1], "test")) goto run-main
 38     # . eax = kernel-string-equal?(argv[1], "test")
 39     # . . push args
 40     68/push  "test"/imm32
 41     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
 42     # . . call
 43     e8/call  kernel-string-equal?/disp32
 44     # . . discard args
 45     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 46     # . if (eax == false) goto run-main
 47     3d/compare-eax-and  0/imm32/false
 48     74/jump-if-=  $subx-tests-main:interactive/disp8
 49     # run-tests()
 50     e8/call  run-tests/disp32
 51     # syscall(exit, *Num-test-failures)
 52     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/ebx   Num-test-failures/disp32          # copy *Num-test-failures to ebx
 53     eb/jump  $subx-tests-main:end/disp8
 54 $subx-tests-main:interactive:
 55     # - otherwise convert stdin
 56     # subx-gen-run-tests(Stdin, Stdout)
 57     # . . push args
 58     68/push  Stdout/imm32
 59     68/push  Stdin/imm32
 60     # . . call
 61     e8/call  subx-gen-run-tests/disp32
 62     # . . discard args
 63     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
 64     # syscall(exit, 0)
 65     bb/copy-to-ebx  0/imm32
 66 $subx-tests-main:end:
 67     e8/call  syscall_exit/disp32
 68 
 69 subx-gen-run-tests:  # in: (addr buffered-file), out: (addr buffered-file)
 70     # pseudocode
 71     #   boolean tests-found = false
 72     #   var line: (stream byte 512)
 73     #   var new-code-segment-handle: (handle stream byte)
 74     #   new-stream(Segment-size, 1, new-code-stream-handle)
 75     #   var new-code-segment: (addr stream byte) = lookup(new-code-stream-handle)
 76     #   write(new-code-segment, "\n== code\n")
 77     #   write(new-code-segment, "run-tests:\n")
 78     #   while true
 79     #     clear-stream(line)
 80     #     read-line-buffered(in, line)
 81     #     if (line->write == 0) break               # end of file
 82     #     var word-slice = next-word(line)
 83     #     if slice-empty?(word-slice)               # empty line
 84     #       continue
 85     #     if label?(word-slice)
 86     #       if slice-starts-with?(word-slice, "test-")
 87     #         tests-found = true
 88     #         write(new-code-segment, "  e8/call  ")
 89     #         write-slice(new-code-segment, word-slice)
 90     #         write(new-code-segment, "/disp32\n")
 91     #     rewind-stream(line)
 92     #     write-stream-data(out, line)
 93     #   if tests-found
 94     #     write(new-code-segment, "  c3/return\n")
 95     #     write-stream-data(out, new-code-segment)
 96     #   flush(out)
 97     #
 98     # . prologue
 99     55/push-ebp
100     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
101     # . save registers
102     50/push-eax
103     51/push-ecx
104     52/push-edx
105     53/push-ebx
106     56/push-esi
107     57/push-edi
108     # var line/ecx: (stream byte 512)
109     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0x200/imm32       # subtract from esp
110     68/push  0x200/imm32/length
111     68/push  0/imm32/read
112     68/push  0/imm32/write
113     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
114     # var word-slice/edx: slice
115     68/push  0/imm32/end
116     68/push  0/imm32/start
117     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
118     # tests-found?/ebx = false
119     31/xor                          3/mod/direct    3/rm32/ebx    .           .             .           3/r32/ebx   .               .                 # clear ebx
120     # var new-code-stream-handle/esi: (handle stream byte)
121     68/push  0/imm32
122     68/push  0/imm32
123     89/copy                         3/mod/direct    6/rm32/esi    .           .             .           4/r32/esp   .               .                 # copy esp to esi
124     # new-stream(Heap, Segment-size, 1, new-code-stream-handle)
125     # . . push args
126     56/push-esi
127     68/push  1/imm32
128     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Segment-size/disp32               # push *Segment-size
129     68/push  Heap/imm32
130     # . . call
131     e8/call  new-stream/disp32
132     # . . discard args
133     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
134     # var new-code-stream/edi: (addr stream byte) = lookup(*new-code-stream-handle)
135     # . eax = lookup(*new-code-stream-handle)
136     # . . push args
137     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
138     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
139     # . . call
140     e8/call  lookup/disp32
141     # . . discard args
142     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
143     # . new-code-stream = eax
144     89/copy                         3/mod/direct    7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy eax to edi
145     # write(new-code-segment, "\n== code\n")
146     # . . push args
147     68/push  "\n== code\n"/imm32
148     57/push-edi
149     # . . call
150     e8/call  write/disp32
151     # . . discard args
152     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
153     # write(new-code-segment, "run-tests:\n")
154     # . . push args
155     68/push  "run-tests:\n"/imm32
156     57/push-edi
157     # . . call
158     e8/call  write/disp32
159     # . . discard args
160     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
161 $subx-gen-run-tests:loop:
162     # clear-stream(line)
163     # . . push args
164     51/push-ecx
165     # . . call
166     e8/call  clear-stream/disp32
167     # . . discard args
168     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
169     # read-line-buffered(in, line)
170     # . . push args
171     51/push-ecx
172     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
173     # . . call
174     e8/call  read-line-buffered/disp32
175     # . . discard args
176     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
177 $subx-gen-run-tests:check0:
178     # if (line->write == 0) break
179     81          7/subop/compare     0/mod/indirect  1/rm32/ecx    .           .             .           .           .               0/imm32           # compare *ecx
180     0f 84/jump-if-=  $subx-gen-run-tests:break/disp32
181     # next-word(line, word-slice)
182     # . . push args
183     52/push-edx
184     51/push-ecx
185     # . . call
186     e8/call  next-word/disp32
187     # . . discard args
188     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
189 $subx-gen-run-tests:check-empty:
190     # if slice-empty?(word-slice) break
191     # . eax = slice-empty?(word-slice)
192     52/push-edx
193     e8/call  slice-empty?/disp32
194     # . . discard args
195     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
196     # . if (eax != false) break
197     3d/compare-eax-and  0/imm32/false
198     75/jump-if-!=  $subx-gen-run-tests:loop/disp8
199 $subx-gen-run-tests:check-for-label:
200     # if (!label?(word-slice)) continue
201     # . eax = label?(word-slice)
202     # . . push args
203     52/push-edx
204     # . . call
205     e8/call  label?/disp32
206     # . . discard args
207     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
208     # . if (eax == false) continue
209     3d/compare-eax-and  0/imm32/false
210     74/jump-if-=  $subx-gen-run-tests:continue/disp8
211 $subx-gen-run-tests:check-label-prefix:
212     # strip trailing ':' from word-slice
213     ff          1/subop/decrement   1/mod/*+disp8   2/rm32/edx    .           .             .           .           4/disp8         .                 # decrement *(edx+4)
214     # if !slice-starts-with?(word-slice, "test-") continue
215     # . . push args
216     68/push  "test-"/imm32
217     52/push-edx
218     # . . call
219     e8/call  slice-starts-with?/disp32
220     # . . discard args
221     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
222     # . if (eax == false) break
223     3d/compare-eax-and  0/imm32/false
224     74/jump-if-=  $subx-gen-run-tests:continue/disp8
225 $subx-gen-run-tests:call-test-function:
226     # tests-found? = true
227     bb/copy-to-ebx  1/imm32/true
228     # write(new-code-segment, "  e8/call  ")
229     # . . push args
230     68/push  "  e8/call  "/imm32
231     57/push-edi
232     # . . call
233     e8/call  write/disp32
234     # . . discard args
235     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
236     # write-slice(new-code-segment, word-slice)
237     # . . push args
238     52/push-edx
239     57/push-edi
240     # . . call
241     e8/call  write-slice/disp32
242     # . . discard args
243     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
244     # write(new-code-segment, "/disp32\n")
245     # . . push args
246     68/push  "/disp32\n"/imm32
247     57/push-edi
248     # . . call
249     e8/call  write/disp32
250     # . . discard args
251     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
252 $subx-gen-run-tests:continue:
253     # rewind-stream(line)
254     # . . push args
255     51/push-ecx
256     # . . call
257     e8/call  rewind-stream/disp32
258     # . . discard args
259     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
260     # write-stream-data(out, line)
261     # . . push args
262     51/push-ecx
263     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
264     # . . call
265     e8/call  write-stream-data/disp32
266     # . . discard args
267     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
268     # loop
269     e9/jump  $subx-gen-run-tests:loop/disp32
270 $subx-gen-run-tests:break:
271     # if (!tests-found?) goto end
272     81          7/subop/compare     3/mod/direct    3/rm32/ebx    .           .             .           .           .               0/imm32/false     # compare ebx
273     74/jump-if-=  $subx-gen-run-tests:end/disp8
274     # write(new-code-segment, "  c3/return\n")
275     # . . push args
276     68/push  "  c3/return\n"/imm32
277     57/push-edi
278     # . . call
279     e8/call  write/disp32
280     # . . discard args
281     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
282     # write-stream-data(out, new-code-segment)
283     # . . push args
284     57/push-edi
285     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
286     # . . call
287     e8/call  write-stream-data/disp32
288     # . . discard args
289     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
290 $subx-gen-run-tests:end:
291     # flush(out)
292     # . . push args
293     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
294     # . . call
295     e8/call  flush/disp32
296     # . . discard args
297     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
298     # . reclaim locals
299     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x21c/imm32       # add to esp
300     # . restore registers
301     5f/pop-to-edi
302     5e/pop-to-esi
303     5b/pop-to-ebx
304     5a/pop-to-edx
305     59/pop-to-ecx
306     58/pop-to-eax
307     # . epilogue
308     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
309     5d/pop-to-ebp
310     c3/return
311 
312 # . . vim:nowrap:textwidth=0