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   # EAX = t
 81   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
 82   # EBX = line
 83   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              3/r32/EBX   0xc/disp8       .                 # copy *(EBP+12) to EBX
 84   # append line to t.data from t.write
 85   #
 86   # pseudocode:
 87   #   destend = &t.data[t.length]
 88   #   oldw = t.write
 89   #   if line.length == 0 return
 90   #   t.write += line.length + 1  # for newline
 91   #   dest = &t.data[oldw]
 92   #   srcend = &line.data[line.length]
 93   #   src = &line.data[0]
 94   #   while true:
 95   #     if src >= srcend break
 96   #     if dest >= destend break  # for now silently ignore filled up trace buffer
 97   #     *dest = *src
 98   #     ++src
 99   #     ++dest
100   #   if dest >= destend return
101   #   *dest = 10/newline
102   #
103   # key registers to set up for the loop:
104   #   EAX/dest, ECX/destend, EBX/src, ESI/srcend
105   # we save EDX for byte operations (has to be one of the first 4 registers)
106   #
107   # register setup before the loop:
108   #   EAX = *(EBP+8)    # t
109   #   EBX = *(EBP+12)   # line
110   #   ECX = *(EAX+8)    # t.length
111   #   ECX = EAX+12+ECX  # destend = &t.data[t.length]
112   #   ESI = *EAX        # oldw = t.write
113   #   EDX = *EBX        # line.length
114   #   *EAX = *EAX + EDX # update t.write  (allowed to go past t.length)
115   #                     # do this here just because it's convenient
116   #   ++ *EAX  # for the newline
117   #   EAX = EAX+12+ESI  # dest = &t.data[oldw]
118   #   ESI = EBX+4+EDX   # srcend = &line.data[line.length]
119   #   EBX = EBX+4       # src = &line.data[0]
120   #
121   # EAX/t and EBX/line are already initialized
122   # ECX = t.length
123   8b/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(EAX+8) to ECX
124   # ECX/destend = &t.data[t.length]
125   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
126   # ESI/oldw = t.write
127   8b/copy                         0/mod/indirect  0/rm32/EAX    .           .             .           6/r32/ESI   .               .                 # copy *EAX to ESI
128   # EDX = line.length
129   8b/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           2/r32/EDX   .               .                 # copy *EBX to EDX
130   # if EDX == 0 return
131   81          7/subop/compare     3/mod/direct    2/rm32/EDX    .           .             .           .           .               0/imm32           # compare EDX
132   74/jump-if-equal  $trace:end/disp8
133   # t.write += line.length
134   01/add                          0/mod/indirect  0/rm32/EAX    .           .             .           2/r32/EDX   .               .                 # add EDX to *EAX
135   # t.write++ (for the newline we'll append below)
136   81          0/subop/add         0/mod/indirect  0/rm32/EAX    .           .             .           .           .               1/imm32           # add to *EAX
137   # EAX/dest = &t.data[oldw]
138   8d/copy-address                 1/mod/*+disp8   4/rm32/sib    0/base/EAX  6/index/ESI   .           0/r32/EAX   0xc/disp8       .                 # copy EAX+ESI+12 to EAX
139   # ESI/srcend = &line.data[line.length]
140   8d/copy-address                 1/mod/*+disp8   4/rm32/sib    3/base/EBX  2/index/EDX   .           6/r32/ESI   4/disp8         .                 # copy EBX+EDX+4 to ESI
141   # EBX/src = &line.data[0]
142   81          0/subop/add         3/mod/direct    3/rm32/EBX    .           .             .           .           .               4/imm32           # add to EBX
143   # while (true)
144 $trace:loop:
145   # if EBX/src >= ESI/srcend break
146   39/compare                      3/mod/direct    3/rm32/EBX    .           .             .           6/r32/ESI   .               .                 # compare EBX with ESI
147   7d/jump-if-greater-or-equal  $trace:break/disp8
148   # if EAX/dest >= ECX/destend break  (for now silently ignore filled up trace buffer)
149   39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # compare EAX with ECX
150   7d/jump-if-greater-or-equal  $trace:break/disp8
151   # copy one byte
152   8a/copy-byte                    0/mod/indirect  3/rm32/EBX    .           .             .           2/r32/DL    .               .                 # copy byte at *EBX to DL
153   88/copy-byte                    0/mod/indirect  0/rm32/EAX    .           .             .           2/r32/DL    .               .                 # copy byte at DL to *EAX
154   # updates
155   40/increment-EAX
156   43/increment-EBX
157   eb/jump  $trace:loop/disp8
158 $trace:break:
159   # finally, append a newline
160     # if EAX/dest >= ECX/destend return
161   39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # compare EAX with ECX
162   7d/jump-if-greater-or-equal  $trace:end/disp8
163     # append
164   ba/copy-to-EDX  0x0a/imm32
165   88/copy-byte                    0/mod/indirect  0/rm32/EAX    .           .             .           2/r32/DL    .               .                 # copy byte at DL to *EAX
166 $trace:end:
167   # restore registers
168   5e/pop-to-ESI
169   5b/pop-to-EBX
170   5a/pop-to-EDX
171   59/pop-to-ECX
172   58/pop-to-EAX
173   # epilog
174   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
175   5d/pop-to-EBP
176   c3/return
177 
178 clear-trace-stream:  # t : (address trace-stream)
179   # prolog
180   55/push-EBP
181   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
182   # save registers
183   50/push-EAX
184   51/push-ECX
185   # EAX = t
186   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
187   # ECX = t.length
188   8b/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(EAX+8) to ECX
189   # ECX = &t.data[t.length]
190   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
191   # t.write = 0
192   c7/copy                         0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # copy to *EAX
193   # t.read = 0
194   c7/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         0/imm32           # copy to *(EAX+4)
195   # EAX = t.data
196   81          0/subop/add         3/mod/direct    0/rm32/EAX    .           .             .           .           .               0xc/imm32         # add to EAX
197   # while (true)
198 $clear-trace-stream:loop:
199   # if EAX >= ECX break
200   39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # compare EAX with ECX
201   7d/jump-if-greater-or-equal  $clear-trace-stream:end/disp8
202   # *EAX = 0
203   c7/copy                         0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # copy to *EAX
204   # EAX += 4
205   81          0/subop/add         3/mod/direct    0/rm32/EAX    .           .             .           .           .               4/imm32           # add to EAX
206   eb/jump  $clear-trace-stream:loop/disp8
207 $clear-trace-stream:end:
208   # restore registers
209   59/pop-to-ECX
210   58/pop-to-EAX
211   # epilog
212   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
213   5d/pop-to-EBP
214   c3/return
215 
216 test-trace-single:
217   # clear-trace-stream(Test-trace-stream)
218     # push args
219   68/push  Test-trace-stream/imm32
220     # call
221   e8/call  clear-trace-stream/disp32
222     # discard args
223   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
224   # trace(Test-trace-stream, "Ab")
225     # push args
226   68/push  "Ab"/imm32
227   68/push  Test-trace-stream/imm32
228     # call
229   e8/call  trace/disp32
230     # discard args
231   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
232   # check-ints-equal(*Test-trace-stream.data, 41/A 62/b 0a/newline 00, msg)
233     # push args
234   68/push  "F - test-trace-single"/imm32
235   68/push  0x0a6241/imm32/Ab-newline
236     # push *Test-trace-stream.data
237   b8/copy-to-EAX  Test-trace-stream/imm32
238   ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
239     # call
240   e8/call  check-ints-equal/disp32
241     # discard args
242   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
243   # end
244   c3/return
245 
246 test-trace-appends:
247   # clear-trace-stream(Test-trace-stream)
248     # push args
249   68/push  Test-trace-stream/imm32
250     # call
251   e8/call  clear-trace-stream/disp32
252     # discard args
253   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
254   # trace(Test-trace-stream, "C")
255     # push args
256   68/push  "C"/imm32
257   68/push  Test-trace-stream/imm32
258     # call
259   e8/call  trace/disp32
260     # discard args
261   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
262   # trace(Test-trace-stream, "D")
263     # push args
264   68/push  "D"/imm32
265   68/push  Test-trace-stream/imm32
266     # call
267   e8/call  trace/disp32
268     # discard args
269   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
270   # check-ints-equal(*Test-trace-stream.data, 43/C 0a/newline 44/D 0a/newline, msg)
271     # push args
272   68/push  "F - test-trace-appends"/imm32
273   68/push  0x0a440a43/imm32/C-newline-D-newline
274     # push *Test-trace-stream.data
275   b8/copy-to-EAX  Test-trace-stream/imm32
276   ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
277     # call
278   e8/call  check-ints-equal/disp32
279     # discard args
280   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
281   # end
282   c3/return
283 
284 test-trace-empty-line:
285   # clear-trace-stream(Test-trace-stream)
286     # push args
287   68/push  Test-trace-stream/imm32
288     # call
289   e8/call  clear-trace-stream/disp32
290     # discard args
291   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
292   # trace(Test-trace-stream, "")
293     # push args
294   68/push  ""/imm32
295   68/push  Test-trace-stream/imm32
296     # call
297   e8/call  trace/disp32
298     # discard args
299   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
300   # check-ints-equal(*Test-trace-stream.data, 0, msg)
301     # push args
302   68/push  "F - test-trace-empty-line"/imm32
303   68/push  0/imm32
304     # push *Test-trace-stream.data
305   b8/copy-to-EAX  Test-trace-stream/imm32
306   ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
307     # call
308   e8/call  check-ints-equal/disp32
309     # discard args
310   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
311   # end
312   c3/return
313 
314 # vim:nowrap:textwidth=0