https://github.com/akkartik/mu/blob/master/apps/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 translate init.linux 0*.subx apps/subx-params.subx apps/tests.subx  -o apps/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 is-label?(word-slice)
 84     #       if slice-starts-with?(word-slice, "test-")
 85     #         tests-found = true
 86     #         write(new-code-segment, "  e8/call  ")
 87     #         write-slice(new-code-segment, word-slice)
 88     #         write(new-code-segment, "/disp32\n")
 89     #     rewind-stream(line)
 90     #     write-stream-data(out, line)
 91     #   if tests-found
 92     #     write(new-code-segment, "  c3/return\n")
 93     #     write-stream-data(out, new-code-segment)
 94     #   flush(out)
 95     #
 96     # . prologue
 97     55/push-ebp
 98     89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
 99     # . save registers
100     50/push-eax
101     51/push-ecx
102     52/push-edx
103     53/push-ebx
104     56/push-esi
105     57/push-edi
106     # var line/ecx: (stream byte 512)
107     81          5/subop/subtract    3/mod/direct    4/rm32/esp    .           .             .           .           .               0x200/imm32       # subtract from esp
108     68/push  0x200/imm32/length
109     68/push  0/imm32/read
110     68/push  0/imm32/write
111     89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
112     # var word-slice/edx: slice
113     68/push  0/imm32/end
114     68/push  0/imm32/start
115     89/copy                         3/mod/direct    2/rm32/edx    .           .             .           4/r32/esp   .               .                 # copy esp to edx
116     # tests-found?/ebx = false
117     31/xor                          3/mod/direct    3/rm32/ebx    .           .             .           3/r32/ebx   .               .                 # clear ebx
118     # var new-code-stream-handle/esi: (handle stream byte)
119     68/push  0/imm32
120     68/push  0/imm32
121     89/copy                         3/mod/direct    6/rm32/esi    .           .             .           4/r32/esp   .               .                 # copy esp to esi
122     # new-stream(Heap, Segment-size, 1, new-code-stream-handle)
123     # . . push args
124     56/push-esi
125     68/push  1/imm32
126     ff          6/subop/push        0/mod/indirect  5/rm32/.disp32            .             .           .           Segment-size/disp32               # push *Segment-size
127     68/push  Heap/imm32
128     # . . call
129     e8/call  new-stream/disp32
130     # . . discard args
131     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x10/imm32        # add to esp
132     # var new-code-stream/edi: (addr stream byte) = lookup(*new-code-stream-handle)
133     # . eax = lookup(*new-code-stream-handle)
134     # . . push args
135     ff          6/subop/push        1/mod/*+disp8   6/rm32/esi    .           .             .           .           4/disp8         .                 # push *(esi+4)
136     ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
137     # . . call
138     e8/call  lookup/disp32
139     # . . discard args
140     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
141     # . new-code-stream = eax
142     89/copy                         3/mod/direct    7/rm32/edi    .           .             .           0/r32/eax   .               .                 # copy eax to edi
143     # write(new-code-segment, "\n== code\n")
144     # . . push args
145     68/push  "\n== code\n"/imm32
146     57/push-edi
147     # . . call
148     e8/call  write/disp32
149     # . . discard args
150     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
151     # write(new-code-segment, "run-tests:\n")
152     # . . push args
153     68/push  "run-tests:\n"/imm32
154     57/push-edi
155     # . . call
156     e8/call  write/disp32
157     # . . discard args
158     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
159 $subx-gen-run-tests:loop:
160     # clear-stream(line)
161     # . . push args
162     51/push-ecx
163     # . . call
164     e8/call  clear-stream/disp32
165     # . . discard args
166     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
167     # read-line-buffered(in, line)
168     # . . push args
169     51/push-ecx
170     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           8/disp8         .                 # push *(ebp+8)
171     # . . call
172     e8/call  read-line-buffered/disp32
173     # . . discard args
174     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
175 $subx-gen-run-tests:check0:
176     # if (line->write == 0) break
177     81          7/subop/compare     0/mod/indirect  1/rm32/ecx    .           .             .           .           .               0/imm32           # compare *ecx
178     0f 84/jump-if-=  $subx-gen-run-tests:break/disp32
179     # next-word(line, word-slice)
180     # . . push args
181     52/push-edx
182     51/push-ecx
183     # . . call
184     e8/call  next-word/disp32
185     # . . discard args
186     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
187 $subx-gen-run-tests:check-for-label:
188     # if (!is-label?(word-slice)) continue
189     # . eax = is-label?(word-slice)
190     # . . push args
191     52/push-edx
192     # . . call
193     e8/call  is-label?/disp32
194     # . . discard args
195     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
196     # . if (eax == false) continue
197     3d/compare-eax-and  0/imm32/false
198     74/jump-if-=  $subx-gen-run-tests:continue/disp8
199 $subx-gen-run-tests:check-label-prefix:
200     # strip trailing ':' from word-slice
201     ff          1/subop/decrement   1/mod/*+disp8   2/rm32/edx    .           .             .           .           4/disp8         .                 # decrement *(edx+4)
202     # if !slice-starts-with?(word-slice, "test-") continue
203     # . . push args
204     68/push  "test-"/imm32
205     52/push-edx
206     # . . call
207     e8/call  slice-starts-with?/disp32
208     # . . discard args
209     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
210     # . if (eax == false) break
211     3d/compare-eax-and  0/imm32/false
212     74/jump-if-=  $subx-gen-run-tests:continue/disp8
213 $subx-gen-run-tests:call-test-function:
214     # tests-found? = true
215     bb/copy-to-ebx  1/imm32/true
216     # write(new-code-segment, "  e8/call  ")
217     # . . push args
218     68/push  "  e8/call  "/imm32
219     57/push-edi
220     # . . call
221     e8/call  write/disp32
222     # . . discard args
223     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
224     # write-slice(new-code-segment, word-slice)
225     # . . push args
226     52/push-edx
227     57/push-edi
228     # . . call
229     e8/call  write-slice/disp32
230     # . . discard args
231     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
232     # write(new-code-segment, "/disp32\n")
233     # . . push args
234     68/push  "/disp32\n"/imm32
235     57/push-edi
236     # . . call
237     e8/call  write/disp32
238     # . . discard args
239     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
240 $subx-gen-run-tests:continue:
241     # rewind-stream(line)
242     # . . push args
243     51/push-ecx
244     # . . call
245     e8/call  rewind-stream/disp32
246     # . . discard args
247     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
248     # write-stream-data(out, line)
249     # . . push args
250     51/push-ecx
251     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
252     # . . call
253     e8/call  write-stream-data/disp32
254     # . . discard args
255     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
256     # loop
257     e9/jump  $subx-gen-run-tests:loop/disp32
258 $subx-gen-run-tests:break:
259     # if (!tests-found?) goto end
260     81          7/subop/compare     3/mod/direct    3/rm32/ebx    .           .             .           .           .               0/imm32/false     # compare ebx
261     74/jump-if-=  $subx-gen-run-tests:end/disp8
262     # write(new-code-segment, "  c3/return\n")
263     # . . push args
264     68/push  "  c3/return\n"/imm32
265     57/push-edi
266     # . . call
267     e8/call  write/disp32
268     # . . discard args
269     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
270     # write-stream-data(out, new-code-segment)
271     # . . push args
272     57/push-edi
273     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
274     # . . call
275     e8/call  write-stream-data/disp32
276     # . . discard args
277     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
278 $subx-gen-run-tests:end:
279     # flush(out)
280     # . . push args
281     ff          6/subop/push        1/mod/*+disp8   5/rm32/ebp    .           .             .           .           0xc/disp8       .                 # push *(ebp+12)
282     # . . call
283     e8/call  flush/disp32
284     # . . discard args
285     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
286     # . reclaim locals
287     81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0x21c/imm32       # add to esp
288     # . restore registers
289     5f/pop-to-edi
290     5e/pop-to-esi
291     5b/pop-to-ebx
292     5a/pop-to-edx
293     59/pop-to-ecx
294     58/pop-to-eax
295     # . epilogue
296     89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
297     5d/pop-to-ebp
298     c3/return
299 
300 # . . vim:nowrap:textwidth=0