about summary refs log tree commit diff stats
path: root/themes/headache
blob: 5c06ca78f17a1d487da278b5928662f4f219d2b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[colours]
bkgnd=default
titlebar=magenta
titlebar.text=white
titlebar.brackets=white
titlebar.unencrypted=black
titlebar.encrypted=cyan
titlebar.untrusted=yellow
titlebar.trusted=cyan
titlebar.online=white
titlebar.offline=black
titlebar.away=white
titlebar.chat=white
titlebar.dnd=black
titlebar.xa=white
statusbar=default
statusbar.text=white
statusbar.brackets=red
statusbar.active=cyan
statusbar.new=white
main.text=blue
main.text.me=white
main.text.them=bold_white
main.splash=red
main.time=green
input.text=yellow
subscribed=bold_magenta
unsubscribed=bold_black
otr.started.trusted=cyan
otr.started.untrusted=yellow
otr.ended=blue
otr.trusted=cyan
otr.untrusted=yellow
online=red
away=cyan
chat=green
dnd=megenta
xa=cyan
offline=green
incoming=yellow
typing=magenta
gone=yellow
error=red
roominfo=white
roommention=bold_green
me=white
them=white
roster.header=bold_cyan
occupants.header=bold_cyan
receipt.sent=red
='/akkartik/mu/commit/subx/055trace.subx?h=hlt&id=33ad085125ccae96563207701e63d3a2112900ba'>33ad0851 ^
7ea0b532 ^














33ad0851 ^
7ea0b532 ^


33ad0851 ^
7ea0b532 ^














33ad0851 ^

7ea0b532 ^
33ad0851 ^
7ea0b532 ^
a8c6eda1 ^




7c397786 ^
33ad0851 ^
a8c6eda1 ^
a0467aa2 ^

7ea0b532 ^
a8c6eda1 ^
7ea0b532 ^
a8c6eda1 ^
7ea0b532 ^
33ad0851 ^


7ea0b532 ^
a8c6eda1 ^
33ad0851 ^
7ea0b532 ^
33ad0851 ^


a8c6eda1 ^

33ad0851 ^





7ea0b532 ^
33ad0851 ^


a8c6eda1 ^

33ad0851 ^





a0467aa2 ^
33ad0851 ^




a0467aa2 ^













































33ad0851 ^









c25d9aa4 ^
33ad0851 ^







8108f5b8 ^
33ad0851 ^

a0467aa2 ^


































8108f5b8 ^
a0467aa2 ^

7c397786 ^


























8108f5b8 ^
7c397786 ^

33ad0851 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314


                                                                                        
                                             
                                           
                                                      







                                                                                                                                                                           
                                                                        































                                                                                                                                               
                                    
















                                                                                                                                                                                    
                                                                                                                                                                            

           
                                   




                                                                                                                                                                     
             







                                                                                                                                                                           
                                      

               














                                                                                  
   


                                                                            
   














                                                                       

                                                                                                                                                                          
                                   
                                                                                                                                                                            
                      




                                                                                                                                                                      
                                    
                          
                                                                                                                                                                     

                                                                                                                                                                 
                            
                                                                                                                                                                            
                                        
                                                                                                                                                                           
                        


                                                                                                                                                                
                                  
                                                                                                                                                                          
                                                 
                                                                                      


                                                                                                                                                                          

                                                                                                                                                                             





                             
                                       


                                                                                                                                                                          

                                                                                                                                                                             





                     
               




                                                                                                                                                                     













































                                                                                                                                                                            









                                                                                                                                                                
                                        







                                                                                                                                                                    
       

           


































                                                                                                                                                                    
       

           


























                                                                                                                                                                    
       

           
                        
# helpers for emitting traces to a trace stream, and for tests to check the trace stream
#
# A trace stream looks like this:
#   read : int  # index that we've read until
#   write : int  # index at which writes go
#   data : (array byte)  # prefixed by length as usual
# In a real trace the data will be in a special segment set aside for the purpose.
#
# primitives for operating on traces:
#   - initialize-trace-stream (update global variable)
#   - trace: stream, string
#   - die: stream (exit(1) if using real trace)
#   - 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)
#   - rewind-reads: stream (resets read pointer)
#   - scan-to-next-line: stream (advance read pointer past next newline)
#
# Traces are very fundamental, so many of the helpers we create here won't be
# used elsewhere; we'll switch to more bounds-checked variants. But here we get
# bounds-checking for free; we allocate a completely disjoint segment for trace
# data, and overflowing it will generate a page fault.

== data

# We'll save the address of the trace segment here.
Trace-stream:
  00 00 00 00

# Fake trace-stream for tests.
# Also illustrates the layout of the real trace-stream (segment).
Test-trace-stream:
  # current write index
  00 00 00 00
  # current read index
  00 00 00 00
  # length (= 8)
  08 00 00 00
  # data
  00 00 00 00 00 00 00 00  # 8 bytes

== code

# instruction                     effective address                                                   operand     displacement    immediate
# op          subop               mod             rm32          base        index         scale       r32
# 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

# main:  (manual test if this is the last file loaded)
  e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
  # syscall(exit, Num-test-failures)
  8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           1/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
  b8/copy-to-EAX  1/imm32
  cd/syscall  0x80/imm8

# Allocate a new segment for the trace stream, initialize its length, and save its address to Trace-stream.
# The Trace-stream segment will consist of variable-length lines separated by newlines (0x0a)
initialize-trace-stream:
  # EAX = new-segment(0x1000)
    # push arg
  68/push  0x1000/imm32/N
    # call
  e8/call  new-segment/disp32
    # discard arg
  81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
  # copy EAX to *Trace-stream
  89/copy                         0/mod/indirect  5/rm32/.disp32            .             .           0/r32/EAX   Trace-stream/disp32               # copy EAX to *Trace-stream
  # Trace-stream.length = 0x1000/N - 12
  c7          0/copy              1/mod/*+disp8   0/rm32/EAX    .           .             .           .           8/disp8         0xff4/imm32       # copy 0xff4 to *(EAX+8)
  c3/return

# Append to the given trace stream.
trace:  # t : (address trace-stream), line : string
  # prolog
  55/push-EBP
  89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
  # save registers
  50/push-EAX
  51/push-ECX
  52/push-EDX
  53/push-EBX
  56/push-ESI
  # EAX = t
  8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
  # EBX = line
  8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              3/r32/EBX   0xc/disp8       .                 # copy *(EBP+12) to EBX
  # append line to t.data from t.write
  #
  # pseudocode:
  #   destend = &t.data[t.length]
  #   oldw = t.write
  #   if line.length == 0 return
  #   t.write += line.length + 1  # for newline
  #   dest = &t.data[oldw]
  #   srcend = &line.data[line.length]
  #   src = &line.data[0]
  #   while true:
  #     if src >= srcend break
  #     if dest >= destend break  # for now silently ignore filled up trace buffer
  #     *dest = *src
  #     ++src
  #     ++dest
  #   if dest >= destend return
  #   *dest = 10/newline
  #
  # key registers to set up for the loop:
  #   EAX/dest, ECX/destend, EBX/src, ESI/srcend
  # we save EDX for byte operations (has to be one of the first 4 registers)
  #
  # register setup before the loop:
  #   EAX = *(EBP+8)    # t
  #   EBX = *(EBP+12)   # line
  #   ECX = *(EAX+8)    # t.length
  #   ECX = EAX+12+ECX  # destend = &t.data[t.length]
  #   ESI = *EAX        # oldw = t.write
  #   EDX = *EBX        # line.length
  #   *EAX = *EAX + EDX # update t.write  (allowed to go past t.length)
  #                     # do this here just because it's convenient
  #   ++ *EAX  # for the newline
  #   EAX = EAX+12+ESI  # dest = &t.data[t.write]
  #   ESI = EBX+4+EDX   # srcend = &line.data[line.length]
  #   EBX = EBX+4       # src = &line.data[0]
  #
  # EAX/t and EBX/line are already initialized
  # ECX = t.length
  8b/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(EAX+8) to ECX
  # ECX/destend = &t.data[t.length]
  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
  # ESI/oldw = t.write
  8b/copy                         0/mod/indirect  0/rm32/EAX    .           .             .           6/r32/ESI   .               .                 # copy *EAX to ESI
  # EDX = line.length
  8b/copy                         0/mod/indirect  3/rm32/EBX    .           .             .           2/r32/EDX   .               .                 # copy *EBX to EDX
  # if EDX == 0 return
  81          7/subop/compare     3/mod/direct    2/rm32/EDX    .           .             .           .           .               0/imm32           # compare EDX
  74/jump-if-equal  $trace:end/disp8
  # t.write += line.length
  01/add                          0/mod/indirect  0/rm32/EAX    .           .             .           2/r32/EDX   .               .                 # add EDX to *EAX
  # t.write++ (for the newline we'll append below)
  81          0/subop/add         0/mod/indirect  0/rm32/EAX    .           .             .           .           .               1/imm32           # add to *EAX
  # EAX/dest = &t.data[oldw]
  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
  # ESI/srcend = &line.data[line.length]
  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
  # EBX/src = &line.data
  81          0/subop/add         3/mod/direct    3/rm32/EBX    .           .             .           .           .               4/imm32           # add to EBX
  # while (true)
$trace:loop:
  # if EBX/src >= ESI/srcend break
  39/compare                      3/mod/direct    3/rm32/EBX    .           .             .           6/r32/ESI   .               .                 # compare EBX with ESI
  7d/jump-if-greater-or-equal  $trace:break/disp8
  # if EAX/dest >= ECX/destend break  (for now silently ignore filled up trace buffer)
  39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # compare EAX with ECX
  7d/jump-if-greater-or-equal  $trace:break/disp8
  # copy one byte
  8a/copy-byte                    0/mod/indirect  3/rm32/EBX    .           .             .           2/r32/DL    .               .                 # copy byte at *EBX to DL
  88/copy-byte                    0/mod/indirect  0/rm32/EAX    .           .             .           2/r32/DL    .               .                 # copy byte at DL to *EAX
  # updates
  40/increment-EAX
  43/increment-EBX
  eb/jump  $trace:loop/disp8
$trace:break:
  # finally, append a newline
    # if EAX/dest >= ECX/destend return
  39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # compare EAX with ECX
  7d/jump-if-greater-or-equal  $trace:end/disp8
    # append
  ba/copy-to-EDX  0x0a/imm32
  88/copy-byte                    0/mod/indirect  0/rm32/EAX    .           .             .           2/r32/DL    .               .                 # copy byte at DL to *EAX
$trace:end:
  # restore registers
  5e/pop-to-ESI
  5b/pop-to-EBX
  5a/pop-to-EDX
  59/pop-to-ECX
  58/pop-to-EAX
  # epilog
  89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
  5d/pop-to-EBP
  c3/return

clear-trace-stream:  # t : (address trace-stream)
  # prolog
  55/push-EBP
  89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
  # save registers
  50/push-EAX
  51/push-ECX
  # EAX = t
  8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none              0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
  # ECX = t.length
  8b/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   8/disp8         .                 # copy *(EAX+8) to ECX
  # ECX = &t.data[t.length]
  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
  # t.write = 0
  c7/copy                         0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # copy to *EAX
  # t.read = 0
  c7/copy                         1/mod/*+disp8   0/rm32/EAX    .           .             .           .           4/disp8         0/imm32           # copy to *(EAX+4)
  # EAX = t.data
  81          0/subop/add         3/mod/direct    0/rm32/EAX    .           .             .           .           .               0xc/imm32         # add to EAX
  # while (true)
$clear-trace-stream:loop:
  # if EAX >= ECX break
  39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # compare EAX with ECX
  7d/jump-if-greater-or-equal  $clear-trace-stream:end/disp8
  # *EAX = 0
  c7/copy                         0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # copy to *EAX
  # EAX += 4
  81          0/subop/add         3/mod/direct    0/rm32/EAX    .           .             .           .           .               4/imm32           # add to EAX
  eb/jump  $clear-trace-stream:loop/disp8
$clear-trace-stream:end:
  # restore registers
  59/pop-to-ECX
  58/pop-to-EAX
  # epilog
  89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
  5d/pop-to-EBP
  c3/return

test-trace-single:
  # clear-trace-stream(Test-trace-stream)
    # push args
  68/push  Test-trace-stream/imm32
    # call
  e8/call  clear-trace-stream/disp32
    # discard args
  81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
  # trace(Test-trace-stream, "Ab")
    # push args
  68/push  "Ab"/imm32
  68/push  Test-trace-stream/imm32
    # call
  e8/call  trace/disp32
    # discard args
  81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
  # check-ints-equal(*Test-trace-stream.data, 41/A 62/b 0a/newline 00, msg)
    # push args
  68/push  "F - test-trace-single"/imm32
  68/push  0x0a6241/imm32/Ab-newline
    # push *Test-trace-stream.data
  b8/copy-to-EAX  Test-trace-stream/imm32
  ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
    # call
  e8/call  check-ints-equal/disp32
    # discard args
  81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
  # end
  c3/return

test-trace-appends:
  # clear-trace-stream(Test-trace-stream)
    # push args
  68/push  Test-trace-stream/imm32
    # call
  e8/call  clear-trace-stream/disp32
    # discard args
  81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
  # trace(Test-trace-stream, "C")
    # push args
  68/push  "C"/imm32
  68/push  Test-trace-stream/imm32
    # call
  e8/call  trace/disp32
    # discard args
  81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
  # trace(Test-trace-stream, "D")
    # push args
  68/push  "D"/imm32
  68/push  Test-trace-stream/imm32
    # call
  e8/call  trace/disp32
    # discard args
  81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
  # check-ints-equal(*Test-trace-stream.data, 43/C 0a/newline 44/D 0a/newline, msg)
    # push args
  68/push  "F - test-trace-appends"/imm32
  68/push  0x0a440a43/imm32/C-newline-D-newline
    # push *Test-trace-stream.data
  b8/copy-to-EAX  Test-trace-stream/imm32
  ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
    # call
  e8/call  check-ints-equal/disp32
    # discard args
  81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
  # end
  c3/return

test-trace-empty-line:
  # clear-trace-stream(Test-trace-stream)
    # push args
  68/push  Test-trace-stream/imm32
    # call
  e8/call  clear-trace-stream/disp32
    # discard args
  81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               4/imm32           # add to ESP
  # trace(Test-trace-stream, "")
    # push args
  68/push  ""/imm32
  68/push  Test-trace-stream/imm32
    # call
  e8/call  trace/disp32
    # discard args
  81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
  # check-ints-equal(*Test-trace-stream.data, 0, msg)
    # push args
  68/push  "F - test-trace-empty-line"/imm32
  68/push  0/imm32
    # push *Test-trace-stream.data
  b8/copy-to-EAX  Test-trace-stream/imm32
  ff          6/subop/push        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           0xc/disp8       .                 # push *(EAX+12)
    # call
  e8/call  check-ints-equal/disp32
    # discard args
  81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
  # end
  c3/return

# vim:nowrap:textwidth=0