about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--002test.cc1
-rw-r--r--030container.cc17
-rw-r--r--032array.cc13
-rw-r--r--034exclusive_container.cc9
-rw-r--r--037call_reply.cc2
-rw-r--r--041name.cc22
6 files changed, 47 insertions, 17 deletions
diff --git a/002test.cc b/002test.cc
index 159932d6..a35f95bf 100644
--- a/002test.cc
+++ b/002test.cc
@@ -54,6 +54,7 @@ if (Run_tests) {
   time_t t; time(&t);
   cerr << "C tests: " << ctime(&t);
   for (size_t i=0; i < sizeof(Tests)/sizeof(Tests[0]); ++i) {
+//?     cerr << i << '\n'; //? 1
     run_test(i);
   }
   // End Tests
diff --git a/030container.cc b/030container.cc
index fbb0f87c..2bc540bb 100644
--- a/030container.cc
+++ b/030container.cc
@@ -14,11 +14,14 @@ Type[point].elements.push_back(i);
 //: Containers can be copied around with a single instruction just like
 //: numbers, no matter how large they are.
 
+//: Tests in this layer often explicitly setup memory before reading it as a
+//: container. Don't do this in general. I'm tagging exceptions with /raw to
+//: avoid warnings.
 :(scenario copy_multiple_locations)
 recipe main [
   1:number <- copy 34:literal
   2:number <- copy 35:literal
-  3:point <- copy 1:point
+  3:point <- copy 1:point/raw  # unsafe
 ]
 +mem: storing 34 in location 3
 +mem: storing 35 in location 4
@@ -42,7 +45,7 @@ recipe main [
   12:number <- copy 34:literal
   13:number <- copy 35:literal
   14:number <- copy 36:literal
-  15:point-number <- copy 12:point-number
+  15:point-number <- copy 12:point-number/raw  # unsafe
 ]
 +mem: storing 36 in location 17
 
@@ -57,7 +60,7 @@ recipe main [
   4:number <- copy 34:literal  # second
   5:number <- copy 35:literal
   6:number <- copy 36:literal
-  7:boolean <- equal 1:point-number, 4:point-number
+  7:boolean <- equal 1:point-number/raw, 4:point-number/raw  # unsafe
 ]
 +mem: storing 1 in location 7
 
@@ -69,7 +72,7 @@ recipe main [
   4:number <- copy 34:literal  # second
   5:number <- copy 35:literal
   6:number <- copy 37:literal  # different
-  7:boolean <- equal 1:point-number, 4:point-number
+  7:boolean <- equal 1:point-number/raw, 4:point-number/raw  # unsafe
 ]
 +mem: storing 0 in location 7
 
@@ -91,7 +94,7 @@ if (t.kind == container) {
 recipe main [
   12:number <- copy 34:literal
   13:number <- copy 35:literal
-  15:number <- get 12:point, 1:offset
+  15:number <- get 12:point/raw, 1:offset  # unsafe
 ]
 +mem: storing 35 in location 15
 
@@ -129,7 +132,7 @@ recipe main [
   12:number <- copy 34:literal
   13:number <- copy 35:literal
   14:number <- copy 36:literal
-  15:number <- get 12:point-number, 1:offset
+  15:number <- get 12:point-number/raw, 1:offset  # unsafe
 ]
 +mem: storing 36 in location 15
 
@@ -139,7 +142,7 @@ recipe main [
 recipe main [
   12:number <- copy 34:literal
   13:number <- copy 35:literal
-  15:address:number <- get-address 12:point, 1:offset
+  15:address:number <- get-address 12:point/raw, 1:offset  # unsafe
 ]
 +mem: storing 13 in location 15
 
diff --git a/032array.cc b/032array.cc
index 0d8efc5c..de9a05f1 100644
--- a/032array.cc
+++ b/032array.cc
@@ -5,6 +5,9 @@
 //: elements of a fixed size, so you can't create containers containing arrays.
 //: Create containers containing addresses to arrays instead.
 
+//: Tests in this layer often explicitly setup memory before reading it as an
+//: array. Don't do this in general. I'm tagging exceptions with /raw to
+//: avoid warnings.
 :(scenario copy_array)
 # Arrays can be copied around with a single instruction just like numbers,
 # no matter how large they are.
@@ -13,7 +16,7 @@ recipe main [
   2:number <- copy 14:literal
   3:number <- copy 15:literal
   4:number <- copy 16:literal
-  5:array:number <- copy 1:array:number
+  5:array:number <- copy 1:array:number/raw  # unsafe
 ]
 +mem: storing 3 in location 5
 +mem: storing 14 in location 6
@@ -52,7 +55,7 @@ recipe main [
   2:number <- copy 14:literal
   3:number <- copy 15:literal
   4:number <- copy 16:literal
-  5:number <- index 1:array:number, 0:literal
+  5:number <- index 1:array:number/raw, 0:literal  # unsafe
 ]
 +mem: storing 14 in location 5
 
@@ -63,7 +66,7 @@ recipe main [
   3:number <- copy 15:literal
   4:number <- copy 16:literal
   5:number <- copy 0:literal
-  6:number <- index 1:array:number, 5:number
+  6:number <- index 1:array:number/raw, 5:number  # unsafe
 ]
 +mem: storing 14 in location 6
 
@@ -105,7 +108,7 @@ recipe main [
   2:number <- copy 14:literal
   3:number <- copy 15:literal
   4:number <- copy 16:literal
-  5:number <- index-address 1:array:number, 0:literal
+  5:number <- index-address 1:array:number/raw, 0:literal  # unsafe
 ]
 +mem: storing 2 in location 5
 
@@ -148,7 +151,7 @@ recipe main [
   2:number <- copy 14:literal
   3:number <- copy 15:literal
   4:number <- copy 16:literal
-  5:number <- length 1:array:number
+  5:number <- length 1:array:number/raw  # unsafe
 ]
 +mem: storing 3 in location 5
 
diff --git a/034exclusive_container.cc b/034exclusive_container.cc
index 6d8e3932..8247cb42 100644
--- a/034exclusive_container.cc
+++ b/034exclusive_container.cc
@@ -25,13 +25,16 @@ Type[tmp].element_names.push_back("i");
 Type[tmp].element_names.push_back("p");
 }
 
+//: Tests in this layer often explicitly setup memory before reading it as an
+//: array. Don't do this in general. I'm tagging exceptions with /raw to
+//: avoid warnings.
 :(scenario copy_exclusive_container)
 # Copying exclusive containers copies all their contents and an extra location for the tag.
 recipe main [
   1:number <- copy 1:literal  # 'point' variant
   2:number <- copy 34:literal
   3:number <- copy 35:literal
-  4:number-or-point <- copy 1:number-or-point
+  4:number-or-point <- copy 1:number-or-point/raw  # unsafe
 ]
 +mem: storing 1 in location 4
 +mem: storing 34 in location 5
@@ -69,7 +72,7 @@ recipe main [
   12:number <- copy 1:literal
   13:number <- copy 35:literal
   14:number <- copy 36:literal
-  20:address:point <- maybe-convert 12:number-or-point, 1:variant
+  20:address:point <- maybe-convert 12:number-or-point/raw, 1:variant  # unsafe
 ]
 +mem: storing 13 in location 20
 
@@ -78,7 +81,7 @@ recipe main [
   12:number <- copy 1:literal
   13:number <- copy 35:literal
   14:number <- copy 36:literal
-  20:address:point <- maybe-convert 12:number-or-point, 0:variant
+  20:address:point <- maybe-convert 12:number-or-point/raw, 0:variant  # unsafe
 ]
 +mem: storing 0 in location 20
 
diff --git a/037call_reply.cc b/037call_reply.cc
index 4806c169..a6ec8c0c 100644
--- a/037call_reply.cc
+++ b/037call_reply.cc
@@ -51,7 +51,7 @@ recipe main [
 recipe f [
   12:number <- next-ingredient
   13:number <- copy 35:literal
-  reply 12:point
+  reply 12:point/raw  # unsafe
 ]
 +run: result 0 is [2, 35]
 +mem: storing 2 in location 3
diff --git a/041name.cc b/041name.cc
index 24ba4e00..eec6aa3e 100644
--- a/041name.cc
+++ b/041name.cc
@@ -31,6 +31,7 @@ void transform_names(const recipe_number r) {
   bool names_used = false;
   bool numeric_locations_used = false;
   map<string, long long int>& names = Name[r];
+  map<string, vector<type_number> > metadata;
   // store the indices 'used' so far in the map
   long long int& curr_idx = names[""];
   ++curr_idx;  // avoid using index 0, benign skip in some other cases
@@ -39,6 +40,7 @@ void transform_names(const recipe_number r) {
     // Per-recipe Transforms
     // map names to addresses
     for (long long int in = 0; in < SIZE(inst.ingredients); ++in) {
+      check_metadata(metadata, inst.ingredients.at(in), r);
       if (is_numeric_location(inst.ingredients.at(in))) numeric_locations_used = true;
       if (is_named_location(inst.ingredients.at(in))) names_used = true;
       if (disqualified(inst.ingredients.at(in))) continue;
@@ -48,6 +50,7 @@ void transform_names(const recipe_number r) {
       inst.ingredients.at(in).set_value(lookup_name(inst.ingredients.at(in), r));
     }
     for (long long int out = 0; out < SIZE(inst.products); ++out) {
+      check_metadata(metadata, inst.products.at(out), r);
       if (is_numeric_location(inst.products.at(out))) numeric_locations_used = true;
       if (is_named_location(inst.products.at(out))) names_used = true;
       if (disqualified(inst.products.at(out))) continue;
@@ -63,6 +66,15 @@ void transform_names(const recipe_number r) {
     raise << "mixing variable names and numeric addresses in " << Recipe[r].name << '\n';
 }
 
+void check_metadata(map<string, vector<type_number> >& metadata, const reagent& x, const recipe_number r) {
+  if (isa_literal(x)) return;
+  if (is_raw(x)) return;
+  if (metadata.find(x.name) == metadata.end())
+    metadata[x.name] = x.types;
+  if (metadata[x.name] != x.types)
+    raise << x.name << " used with multiple types in " << Recipe[r].name << '\n';
+}
+
 bool disqualified(/*mutable*/ reagent& x) {
   if (x.types.empty())
     raise << "missing type in " << x.to_string() << '\n';
@@ -180,6 +192,14 @@ recipe main [
 ]
 -warn: mixing variable names and numeric addresses in main
 
+:(scenario convert_names_warns_on_reusing_name_with_different_type)
+% Hide_warnings = true;
+recipe main [
+  x:number <- copy 1:literal
+  x:boolean <- copy 1:literal
+]
++warn: x used with multiple types in main
+
 //:: Support element names for containers in 'get' and 'get-address'.
 
 //: update our running example container for the next test
@@ -228,7 +248,7 @@ recipe main [
   12:number <- copy 1:literal
   13:number <- copy 35:literal
   14:number <- copy 36:literal
-  20:address:point <- maybe-convert 12:number-or-point, p:variant
+  20:address:point <- maybe-convert 12:number-or-point/raw, p:variant  # unsafe
 ]
 +name: variant p of type number-or-point has tag 1
 +mem: storing 13 in location 20
mmitter Kartik Agaram <vc@akkartik.com> 2018-11-30 10:54:42 -0800 4801' href='/akkartik/mu/commit/subx/055trace.subx?h=hlt&id=6030d7e2e56d445ca67c6a0e8c9cf33e46bc673c'>6030d7e2 ^
9d27e966 ^
6030d7e2 ^

9d27e966 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
1639687b ^
615a7e95 ^
6030d7e2 ^





9d27e966 ^
6030d7e2 ^
9d27e966 ^
6030d7e2 ^

9d27e966 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^


33ad0851 ^
ee9a9237 ^
6030d7e2 ^





ee9a9237 ^
6030d7e2 ^


33ad0851 ^
a0467aa2 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^


dd9ba09a ^
6030d7e2 ^




431627b2 ^
6030d7e2 ^
431627b2 ^
6030d7e2 ^

a0467aa2 ^
1639687b ^
6030d7e2 ^


431627b2 ^
6030d7e2 ^


a0467aa2 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^


a0467aa2 ^
9d27e966 ^
b3d8c144 ^
a0467aa2 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
c98d4b1c ^
ee9a9237 ^
6030d7e2 ^

c98d4b1c ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^


33ad0851 ^
a0467aa2 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
c98d4b1c ^
ee9a9237 ^
6030d7e2 ^

c98d4b1c ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^


a0467aa2 ^
7c397786 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
c98d4b1c ^
ee9a9237 ^
6030d7e2 ^

c98d4b1c ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^


7c397786 ^
9d27e966 ^
b3d8c144 ^


ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
52a2a21c ^
c98d4b1c ^
dd9ba09a ^
6030d7e2 ^
6030d7e2 ^

c98d4b1c ^
6030d7e2 ^

9d27e966 ^
dd9ba09a ^
9d27e966 ^
dd9ba09a ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
cf02c20b ^
ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^


8b0e960d ^


ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^







dd9ba09a ^
6030d7e2 ^
dd9ba09a ^
6030d7e2 ^
dd9ba09a ^
6030d7e2 ^
dd9ba09a ^
8b0e960d ^
1639687b ^
6030d7e2 ^

52a2a21c ^
6030d7e2 ^
52a2a21c ^
1639687b ^
6030d7e2 ^

1639687b ^
6030d7e2 ^
1639687b ^
6030d7e2 ^
1639687b ^
6030d7e2 ^

8b0e960d ^
ee9a9237 ^
6030d7e2 ^




ee9a9237 ^
6030d7e2 ^


b3d8c144 ^
52a2a21c ^














ee9a9237 ^
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
                                                                                                      
 
                                             
                                           
                                             
                                                      






                                                                                                                                                                           
                                                                        









                                                                               
           


                                                                 
                   
                         
           
                        


            

                                      

       


                                                                                                                                                 
 


                                                                                                           
                               
                   
                           
              
                               
                      


                                                                                                                                                                                 
                                          
                                                                                                                                                                              
             
 

                                                                                           
                                                   
              

                                                                                                                                                                       
                      






               
                                                                                                                                                                            
                
                                                                                                                                                                             




                                                                                                                                                                            
                   
               
                                  

                                                                                                                                                                              
                                 

                                                                                                                                                                              
              
                             
                      
                                                                                                                                                                  
                          
                               





                                                                                                                                                                        
                   
                          
                                  

                                                                                                                                                                              
                                 

                                                                                                                                                                              
              
                             
                      


                                                                                                                                                                       
           
                         





                 
              


                                                                                                                                                                       
 
                                                 
              

                                                                                                                                                                       
                      


               
                                                                                                                                                                            




                                                                                                                                                                              
                                                                                                                                                                    
                 
                                                                                                                                                                        

                                                                                                                                                                  
                         
                           


                                                                                                                                                                            
                                                                                                                                                                    


                                                                                                                                                                  
                        
                         

                 
              


                                                                                                                                                                       
 
         
 
                  
                                            
                   
                                     
              
                                      
                      

                                                                                                                                                                  
                   

                                     
              
                         
                      
                                                                                                                                                                  
                                                                               
                   

                                          
                                        

                                                                                                                                                                      
              
                                    
                      


                                                                                                                                                                  
 
                   
                                            
                   
                                     
              
                                      
                      

                                                                                                                                                                  
                   

                                     
              
                         
                      

                                                                                                                                                                  
                   

                                     
              
                         
                      
                                                                                                                                                                  
                                                                                       
                   

                                                 
                                        

                                                                                                                                                                      
              
                                    
                      


                                                                                                                                                                  
 
                      
                                            
                   
                                     
              
                                      
                      

                                                                                                                                                                  
                   

                                     
              
                         
                      
                                                                                                                                                                  
                                                         
                   

                                              
                                        

                                                                                                                                                                      
              
                                    
                      


                                                                                                                                                                  
 
           


                                                                                         
              

                                                                                                                                                                       
                      
               
                                                                    
                                  
                                                                                                                                                                             
                                                                                                                                                                        

                                                                                                                                                                             
                          

                                                                                                                                                                         
                     
                                                                                                                                                                      
                  
                                                                                                                                                                     
              
                             
                      
                                                                                                                                                                  
               
                         
                 
              


                                                                                                                                                                       


                                                                                                      
              

                                                                                                                                                                       
                      







                                
                                                                                                                                                                            
                  
                                                                                                                                                                             
              
                                                                                                                                                                             
                 
                                                                                                                                                                             
                
                            

                                                                                                                                                                            
                                                                  
                                                                                                                                                                            
                                                       
                

                                                                                                                                                                               
                          
                    
          
                    
           

                                  
               
                         




                 
              


                                                                                                                                                                       
 














                                                                                                                                                                  
                            
# primitives for emitting traces to a 'trace' stream, and for tests to make assertions on its contents
#
# A trace stream looks like a regular stream:
#   write : int  # index at which writes go
#   read : int  # index that we've read until
#   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)
#   - 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:
    0/imm32

# Fake trace-stream for tests.
# Also illustrates the layout of the real trace-stream (segment).
_test-trace-stream:
    # current write index
    0/imm32
    # current read index
    0/imm32
    # length
    8/imm32
    # data
    00 00 00 00 00 00 00 00  # 8 bytes

== code
#   instruction                     effective address                                                   register    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

# 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 args
    68/push  0x1000/imm32/N
    # . . call
    e8/call  new-segment/disp32
    # . . discard args
    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/subop/copy        1/mod/*+disp8   0/rm32/EAX    .           .             .           .           8/disp8         0xff4/imm32       # copy 0xff4 to *(EAX+8)
    c3/return

# Append a string to the given trace stream.
# Silently give up if it's already full. Or truncate the string if there isn't enough room.
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
    57/push-EDI
    # EDI = t
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         7/r32/EDI   8/disp8         .                 # copy *(EBP+8) to EDI
    # ESI = line
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         6/r32/ESI   0xc/disp8       .                 # copy *(EBP+12) to ESI
    # ECX = t->write
    8b/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           1/r32/ECX   .               .                 # copy *EDI to ECX
    # EDX = t->length
    8b/copy                         1/mod/*+disp8   7/rm32/EDI    .           .             .           2/r32/EDX   8/disp8         .                 # copy *(EDI+8) to EDX
    # EAX = _append-3(&t->data[t->write], &t->data[t->length], line)
    # . . push line
    56/push-ESI
    # . . push &t->data[t->length]
    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
    53/push-EBX
    # . . push &t->data[t->write]
    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
    53/push-EBX
    # . . call
    e8/call  _append-3/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
    # if (EAX == 0) return
    3d/compare-EAX-and  0/imm32
    74/jump-if-equal  $trace:end/disp8
    # t->write += EAX
    01/add                          0/mod/indirect  7/rm32/EDI    .           .             .           0/r32/EAX   .               .                 # add EAX to *EDI
    # refresh ECX = t->write
    8b/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           1/r32/ECX   .               .                 # copy *EDI to ECX
    # EAX = _append-3(&t->data[t->write], &t->data[t->length], line)
    # . . push line
    68/push  Newline/imm32
    # . . push &t->data[t->length]
    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
    53/push-EBX
    # . . push &t->data[t->write]
    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
    53/push-EBX
    # . . call
    e8/call  _append-3/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
    # t->write += EAX
    01/add                          0/mod/indirect  7/rm32/EDI    .           .             .           0/r32/EAX   .               .                 # add EAX to *EDI
$trace:end:
    # . restore registers
    5f/pop-to-EDI
    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   5/rm32/EBP    .           .                         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          0/subop/copy        0/mod/direct    0/rm32/EAX    .           .             .           .           .               0/imm32           # copy to *EAX
    # t->read = 0
    c7          0/subop/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
$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          0/subop/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

# - tests

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

# - helpers

# 3-argument variant of _append
_append-3:  # out : address, outend : address, s : (array byte) -> num_bytes_appended/EAX
    # . prolog
    55/push-EBP
    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
    # . save registers
    51/push-ECX
    # EAX = _append-4(out, outend, &s->data[0], &s->data[s->length])
    # . . push &s->data[s->length]
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .                         0/r32/EAX   0x10/disp8      .                 # copy *(EBP+16) to EAX
    8b/copy                         0/mod/indirect  0/rm32/EAX    .           .             .           1/r32/ECX   .               .                 # copy *EAX to ECX
    8d/copy-address                 1/mod/*+disp8   4/rm32/sib    0/base/EAX  1/index/ECX   .           1/r32/ECX   4/disp8         .                 # copy EAX+ECX+4 to ECX
    51/push-ECX
    # . . push &s->data[0]
    8d/copy-address                 1/mod/*+disp8   0/rm32/EAX    .           .             .           1/r32/ECX   4/disp8         .                 # copy EAX+4 to ECX
    51/push-ECX
    # . . push outend
    ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           0xc/disp8       .                 # push *(EBP+12)
    # . . push out
    ff          6/subop/push        1/mod/*+disp8   5/rm32/EBP    .           .             .           .           8/disp8         .                 # push *(EBP+8)
    # . . call
    e8/call  _append-4/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0x10/imm32        # add to ESP
$_append-3:end:
    # . restore registers
    59/pop-to-ECX
    # . epilog
    89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
    5d/pop-to-EBP
    c3/return

# 4-argument variant of _append
_append-4:  # out : address, outend : address, in : address, inend : address -> num_bytes_appended/EAX
    # . prolog
    55/push-EBP
    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
    # . save registers
    51/push-ECX
    52/push-EDX
    53/push-EBX
    56/push-ESI
    57/push-EDI
    # EAX/num_bytes_appended = 0
    b8/copy-to-EAX  0/imm32
    # EDI = out
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           7/r32/EDI   8/disp8         .                 # copy *(EBP+8) to EDI
    # EDX = outend
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           2/r32/EDX   0xc/disp8       .                 # copy *(EBP+12) to EDX
    # ESI = in
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           6/r32/ESI   0x10/disp8      .                 # copy *(EBP+16) to ESI
    # ECX = inend
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           1/r32/ECX   0x14/disp8      .                 # copy *(EBP+20) to ECX
$_append-4:loop:
    # if (in >= inend) break
    39/compare                      3/mod/direct    6/rm32/ESI    .           .             .           1/r32/ECX   .               .                 # compare ESI with ECX
    7d/jump-if-greater-or-equal  $_append-4:end/disp8
    # if (out >= outend) abort  # just to catch test failures fast
    39/compare                      3/mod/direct    7/rm32/EDI    .           .             .           2/r32/EDX   .               .                 # compare EDI with EDX
    7d/jump-if-greater-or-equal  $_append-4:abort/disp8
    # *out = *in
    8a/copy-byte                    0/mod/indirect  6/rm32/ESI    .           .             .           3/r32/BL    .               .                 # copy byte at *ESI to BL
    88/copy-byte                    0/mod/indirect  7/rm32/EDI    .           .             .           3/r32/BL    .               .                 # copy byte at BL to *EDI
    # ++num_bytes_appended
    40/increment-EAX
    # ++in
    46/increment-ESI
    # ++out
    47/increment-EDI
    eb/jump  $_append-4:loop/disp8
$_append-4:end:
    # . restore registers
    5f/pop-to-EDI
    5e/pop-to-ESI
    5b/pop-to-EBX
    5a/pop-to-EDX
    59/pop-to-ECX
    # . epilog
    89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
    5d/pop-to-EBP
    c3/return

$_append-4:abort:
    # . _write(2/stderr, error)
    # . . push args
    68/push  "stream overflow"/imm32
    68/push  2/imm32/stderr
    # . . call
    e8/call  _write/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    # . syscall(exit, 1)
    bb/copy-to-EBX  1/imm32
    b8/copy-to-EAX  1/imm32/exit
    cd/syscall  0x80/imm8
    # never gets here

# . . vim:nowrap:textwidth=0