1 # helpers for emitting traces to a trace stream, and for tests to check the trace stream
  2 #
  3 # A trace stream looks like this:
  4 #   read : int  # index that we've read until
  5 #   write : int  # index at which writes go
  6 #   data : (array byte)  # prefixed by length as usual
  7 # In a real trace the data will be in a special segment set aside for the purpose.
  8 #
  9 # primitives for operating on traces:
 10 #   - initialize-trace-stream (update global variable)
 11 #   - trace: stream, string
 12 #   - die: stream (exit(1) if using real trace)
 13 #   - check-trace-contains: stream, string/line, string/message (scans only from stream's read pointer, prints message to stderr on failure, updates stream's read pointer)
 14 #   - rewind-reads: stream (resets read pointer)
 15 #   - scan-to-next-line: stream (advance read pointer past next newline)
 16 #
 17 # Traces are very fundamental, so many of the helpers we create here won't be
 18 # used elsewhere; we'll switch to more bounds-checked variants. But here we get
 19 # bounds-checking for free; we allocate a completely disjoint segment for trace
 20 # data, and overflowing it will generate a page fault.
 21 
 22 == data
 23 
 24 # We'll save the address of the trace segment here.
 25 Trace-stream:
 26   00 00 00 00
 27 
 28 # Fake trace-stream for tests.
 29 # Also illustrates the layout of the real trace-stream (segment).
 30 Test-trace-stream:
 31   # current write index
 32   00 00 00 00
 33   # current read index
 34   00 00 00 00
 35   # length (= 8)
 36   08 00 00 00
 37   # data
 38   00 00 00 00 00 00 00 00  # 8 bytes
 39 
 40 == code
 41 
 42 # instruction                     effective address                                                   operand     displacement    immediate
 43 # op          subop               mod             rm32          base        index         scale       r32
 44 # 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
 45 
 46 # main:  (manual test if this is the last file loaded)
 47   e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 48   # syscall(exit, Num-test-failures)
 49   8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           1/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 50   b8/copy-to-EAX  1/imm32
 51   cd/syscall  0x80/imm8
 52 
 53 # Allocate a new segment for the trace stream, initialize its length, and save its address to Trace-stream.
 54 # The Trace-stream segment will consist of variable-length lines separated by newlines (0x0a)
 55 initialize-trace-stream:
 56   # EAX = new-segment(0x1000)
 57     # push arg
 58   68/push  0x1000/imm32/N
 59     # call
 60   e8/call  new-segment/disp32
 61     # discard arg
 62   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
 63   # copy EAX to *Trace-stream
 64   89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   Trace-stream/disp32               # copy EAX to *Trace-stream
 65   # Trace-stream.length = 0x1000/N - 12
 66   c7          0/copy              1/mod/*+disp8   0/rm32/EAX    .           .             .           .           8/disp8         0xff4/imm32       # copy 0xff4 to *(EAX+8)
 67   c3/return
 68 
 69 # Append to the given trace stream.
 70 trace:  # t : (address trace-stream), line : string
 71   # prolog
 72   55/push-EBP
 73   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 74   # save registers
 75   50/push-EAX
 76   51/push-ECX
 77   52/push-EDX
 78   53/push-EBX
 79   56/push-ESI
 80   57/push-EDI
 81   # EDI = t
 82   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              7/r32/EDI   8/disp8         .                 # copy *(EBP+8) to EDI
 83   # ESI = line
 84   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              6/r32/ESI   0xc/disp8       .                 # copy *(EBP+12) to ESI
 85   # ECX = t.write
 86   8b/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           1/r32/ECX   .               .                 # copy *EDI to ECX
 87   # EDX = t.length
 88   8b/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           2/r32/EDX   8/disp8         .                 # copy *(EDI+8) to EDX
 89   # EAX = _append(&t.data[t.write], &t.data[t.length], line)
 90     # push line
 91   56/push-ESI
 92     # push &t.data[t.length]
 93   8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/EDI  2/index/EDX   .           3/r32/EBX   0xc/disp8       .                 # copy EDI+EDX+12 to EBX
 94   53/push-EBX
 95     # push &t.data[t.write]
 96   8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/EDI  1/index/ECX   .           3/r32/EBX   0xc/disp8       .                 # copy EDI+ECX+12 to EBX
 97   53/push-EBX
 98     # call
 99   e8/call  _append/disp32
100     # discard args
101   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
102   # if EAX == 0 return
103   81          7/subop/compare     3/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # compare EDX
104   74/jump-if-equal  $trace:end/disp8
105   # t.write += EAX
106   01/add                          0/mod/indirect  7/rm32/EDI    .           .             .           0/r32/EAX   .               .                 # add EAX to *EDI
107   # refresh ECX = t.write
108   8b/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           1/r32/ECX   .               .                 # copy *EDI to ECX
109   # EAX = _append(&t.data[t.write], &t.data[t.length], line)
110     # push line
111   68/push  Newline/imm32
112     # push &t.data[t.length]
113   8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/EDI  2/index/EDX   .           3/r32/EBX   0xc/disp8       .                 # copy EDI+EDX+12 to EBX
114   53/push-EBX
115     # push &t.data[t.write]
116   8d/copy-address                 1/mod/*+disp8   4/rm32/sib    7/base/EDI  1/index/ECX   .           3/r32/EBX   0xc/disp8       .                 # copy EDI+ECX+12 to EBX
117   53/push-EBX
118     # call
119   e8/call  _append/disp32
120     # discard args
121   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
122   # t.write += EAX
123   01/add                          0/mod/indirect  7/rm32/EDI    .           .             .           0/r32/EAX   .               .                 # add EAX to *EDI
124 $trace:end:
125   # restore registers
126   5f/pop-to-EDI
127   5e/pop-to-ESI
128   5b/pop-to-EBX
129   5a/pop-to-EDX
130   59/pop-to-ECX
131   58/pop-to-EAX
132   # epilog
133   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
134   5d/pop-to-EBP
135   c3/return
136 
137 _append:  # out : address, outend : address, s : (array byte) -> num_bytes_appended/EAX
138   # prolog
139   55/push-EBP
140   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
141   # save registers
142   51/push-ECX
143   52/push-EDX
144   53/push-EBX
145   56/push-ESI
146   57/push-EDI
147   # EAX/num_bytes_appended = 0
148   b8/copy-to-EAX  0/imm32
149   # EDI = out
150   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              7/r32/EDI   0x8/disp8       .                 # copy *(EBP+8) to EDI
151   # EDX = outend
152   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              2/r32/EDX   0xc/disp8       .                 # copy *(EBP+12) to EDX
153   # ESI = s
154   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              6/r32/ESI   0x10/disp8      .                 # copy *(EBP+16) to ESI
155   # ECX = line.length
156   8b/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           1/r32/ECX   .               .                 # copy *ESI to ECX
157   # ECX/srcend = &line.data[line.length]
158   8d/copy-address                 1/mod/*+disp8   4/rm32/sib    6/base/ESI  1/index/ECX   .           1/r32/ECX   4/disp8         .                 # copy ESI+ECX+4 to ECX
159   # ESI/src = &line.data[0]
160   81          0/subop/add         3/mod/direct    6/rm32/ESI    .           .             .           .           .               4/imm32           # add to ESI
161 $_append:loop:
162   # if ESI/src >= ECX/srcend break
163   39/compare                      3/mod/direct    6/rm32/ESI    .           .             .           1/r32/ECX   .               .                 # compare ESI with ECX
164   7d/jump-if-greater-or-equal  $_append:end/disp8
165   # if EDI/out >= EDX/outend break  (for now silently ignore filled up trace buffer)
166   39/compare                      3/mod/direct    7/rm32/EDI    .           .             .           2/r32/EDX   .               .                 # compare EDI with EDX
167   7d/jump-if-greater-or-equal  $_append:end/disp8
168   # copy one byte from ESI/src to EDI/out
169   8a/copy-byte                    0/mod/indirect  6/rm32/ESI    .           .             .           3/r32/BL    .               .                 # copy byte at *ESI to BL
170   88/copy-byte                    0/mod/indirect  7/rm32/EDI    .           .             .           3/r32/BL    .               .                 # copy byte at BL to *EDI
171   # updates
172   40/increment-EAX
173   46/increment-ESI
174   47/increment-EDI
175   eb/jump  $_append:loop/disp8
176 $_append:end:
177   # restore registers
178   5f/pop-to-EDI
179   5e/pop-to-ESI
180   5b/pop-to-EBX
181   5a/pop-to-EDX
182   59/pop-to-ECX
183   # epilog
184   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
185   5d/pop-to-EBP
186   c3/return
187 
188 clear-trace-stream:  # t : (address trace-stream)
189   # prolog
190   55/push-EBP
191   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
192   # save registers
193   50/push-EAX
194   51/push-ECX
195   # EAX = t
196   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
197   # ECX = t.length
198   8b/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(EAX+8) to ECX
199   # ECX = &t.data[t.length]
200   8d/copy-address                 1/mod/*+disp8   4/rm32/sib    0/base/EAX  1/index/ECX   .           1/r32/ECX   0xc/disp8       .                 # copy EAX+ECX+12 to ECX
201   # t.write = 0
202   c7/copy                         0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # copy to *EAX
203   # t.read = 0
204   c7/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         0/imm32           # copy to *(EAX+4)
205   # EAX = t.data
206   81          0/subop/add         3/mod/direct    0/rm32/EAX    .           .             .           .           .               0xc/imm32         # add to EAX
207   # while (true)
208 $clear-trace-stream:loop:
209   # if EAX >= ECX break
210   39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # compare EAX with ECX
211   7d/jump-if-greater-or-equal  $clear-trace-stream:end/disp8
212   # *EAX = 0
213   c7/copy                         0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # copy to *EAX
214   # EAX += 4
215   81          0/subop/add         3/mod/direct    0/rm32/EAX    .           .             .           .           .               4/imm32           # add to EAX
216   eb/jump  $clear-trace-stream:loop/disp8
217 $clear-trace-stream:end:
218   # restore registers
219   59/pop-to-ECX
220   58/pop-to-EAX
221   # epilog
222   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
223   5d/pop-to-EBP
224   c3/return
225 
226 test-trace-single:
227   # clear-trace-stream(Test-trace-stream)
228     # push args
229   68/push  Test-trace-stream/imm32
230     # call
231   e8/call  clear-trace-stream/disp32
232     # discard args
233   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
234   # trace(Test-trace-stream, "Ab")
235     # push args
236   68/push  "Ab"/imm32
237   68/push  Test-trace-stream/imm32
238     # call
239   e8/call  trace/disp32
240     # discard args
241   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
242   # check-ints-equal(*Test-trace-stream.data, 41/A 62/b 0a/newline 00, msg)
243     # push args
244   68/push  "F - test-trace-single"/imm32
245   68/push  0x0a6241/imm32/Ab-newline
246     # push *Test-trace-stream.data
247   b8/copy-to-EAX  Test-trace-stream/imm32
248   ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
249     # call
250   e8/call  check-ints-equal/disp32
251     # discard args
252   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
253   # end
254   c3/return
255 
256 test-trace-appends:
257   # clear-trace-stream(Test-trace-stream)
258     # push args
259   68/push  Test-trace-stream/imm32
260     # call
261   e8/call  clear-trace-stream/disp32
262     # discard args
263   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
264   # trace(Test-trace-stream, "C")
265     # push args
266   68/push  "C"/imm32
267   68/push  Test-trace-stream/imm32
268     # call
269   e8/call  trace/disp32
270     # discard args
271   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
272   # trace(Test-trace-stream, "D")
273     # push args
274   68/push  "D"/imm32
275   68/push  Test-trace-stream/imm32
276     # call
277   e8/call  trace/disp32
278     # discard args
279   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
280   # check-ints-equal(*Test-trace-stream.data, 43/C 0a/newline 44/D 0a/newline, msg)
281     # push args
282   68/push  "F - test-trace-appends"/imm32
283   68/push  0x0a440a43/imm32/C-newline-D-newline
284     # push *Test-trace-stream.data
285   b8/copy-to-EAX  Test-trace-stream/imm32
286   ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
287     # call
288   e8/call  check-ints-equal/disp32
289     # discard args
290   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
291   # end
292   c3/return
293 
294 test-trace-empty-line:
295   # clear-trace-stream(Test-trace-stream)
296     # push args
297   68/push  Test-trace-stream/imm32
298     # call
299   e8/call  clear-trace-stream/disp32
300     # discard args
301   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
302   # trace(Test-trace-stream, "")
303     # push args
304   68/push  ""/imm32
305   68/push  Test-trace-stream/imm32
306     # call
307   e8/call  trace/disp32
308     # discard args
309   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
310   # check-ints-equal(*Test-trace-stream.data, 0, msg)
311     # push args
312   68/push  "F - test-trace-empty-line"/imm32
313   68/push  0/imm32
314     # push *Test-trace-stream.data
315   b8/copy-to-EAX  Test-trace-stream/imm32
316   ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
317     # call
318   e8/call  check-ints-equal/disp32
319     # discard args
320   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
321   # end
322   c3/return
323 
324 # vim:nowrap:textwidth=0