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