1 # Generate code for a new function called 'run-tests' which calls in sequence
2 # all functions starting with 'test-'.
5 # $ bootstrap/bootstrap translate [01]*.subx subx-params.subx tests.subx -o tests
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
12 Entry: # run tests if necessary, convert stdin if not
14 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
17 # . Heap = new-segment(Heap-size)
20 ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Heap-size/disp32 # push *Heap-size
22 e8/call new-segment/disp32
24 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
25 # initialize-trace-stream(256KB)
27 68/push 0x40000/imm32/256KB
29 e8/call initialize-trace-stream/disp32
31 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
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")
41 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
43 e8/call kernel-string-equal?/disp32
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
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)
61 e8/call subx-gen-run-tests/disp32
63 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
65 bb/copy-to-ebx 0/imm32
67 e8/call syscall_exit/disp32
69 subx-gen-run-tests: # in: (addr buffered-file), out: (addr buffered-file)
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")
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
85 # if label?(word-slice)
86 # if slice-starts-with?(word-slice, "test-")
88 # write(new-code-segment, " e8/call ")
89 # write-slice(new-code-segment, word-slice)
90 # write(new-code-segment, "/disp32\n")
92 # write-stream-data(out, line)
94 # write(new-code-segment, " c3/return\n")
95 # write-stream-data(out, new-code-segment)
100 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
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
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
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)
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)
128 ff 6/subop/push 0/mod/indirect 5/rm32/.disp32 . . . Segment-size/disp32 # push *Segment-size
131 e8/call new-stream/disp32
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)
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
140 e8/call lookup/disp32
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")
147 68/push "\n== code\n"/imm32
152 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
153 # write(new-code-segment, "run-tests:\n")
155 68/push "run-tests:\n"/imm32
160 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
161 $subx-gen-run-tests:loop:
166 e8/call clear-stream/disp32
168 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
169 # read-line-buffered(in, line)
172 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
174 e8/call read-line-buffered/disp32
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)
186 e8/call next-word/disp32
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)
193 e8/call slice-empty?/disp32
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)
205 e8/call label?/disp32
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
216 68/push "test-"/imm32
219 e8/call slice-starts-with?/disp32
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 ")
230 68/push " e8/call "/imm32
235 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
236 # write-slice(new-code-segment, word-slice)
241 e8/call write-slice/disp32
243 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
244 # write(new-code-segment, "/disp32\n")
246 68/push "/disp32\n"/imm32
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)
257 e8/call rewind-stream/disp32
259 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
260 # write-stream-data(out, line)
263 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12)
265 e8/call write-stream-data/disp32
267 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
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")
276 68/push " c3/return\n"/imm32
281 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
282 # write-stream-data(out, new-code-segment)
285 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12)
287 e8/call write-stream-data/disp32
289 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
290 $subx-gen-run-tests:end:
293 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12)
297 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
299 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x21c/imm32 # add to esp
300 # . restore registers
308 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
312 # . . vim:nowrap:textwidth=0