about summary refs log tree commit diff stats
path: root/035lookup.cc
diff options
context:
space:
mode:
Diffstat (limited to '035lookup.cc')
-rw-r--r--035lookup.cc106
1 files changed, 47 insertions, 59 deletions
diff --git a/035lookup.cc b/035lookup.cc
index 9708ce5b..a2647f5d 100644
--- a/035lookup.cc
+++ b/035lookup.cc
@@ -6,8 +6,7 @@
 :(scenario copy_indirect)
 def main [
   1:address:num <- copy 10/unsafe
-  # skip alloc id
-  11:num <- copy 34
+  10:num <- copy 34
   # This loads location 1 as an address and looks up *that* location.
   2:num <- copy 1:address:num/lookup
 ]
@@ -23,7 +22,7 @@ def main [
   1:address:num <- copy 10/unsafe
   1:address:num/lookup <- copy 34
 ]
-+mem: storing 34 in location 11
++mem: storing 34 in location 10
 
 :(before "End Preprocess write_memory(x, data)")
 canonize(x);
@@ -36,7 +35,7 @@ def main [
   1:address:num/lookup <- copy 34
 ]
 -mem: storing 34 in location 0
-+error: main: tried to lookup 0 in '1:address:num/lookup <- copy 34'
++error: can't write to location 0 in '1:address:num/lookup <- copy 34'
 
 //: attempts to /lookup address 0 always loudly fail
 :(scenario lookup_0_fails)
@@ -83,7 +82,7 @@ void lookup_memory(reagent& x) {
 }
 
 void lookup_memory_core(reagent& x, bool check_for_null) {
-  double address = x.value + /*skip alloc id in address*/1;
+  double address = x.value;
   double new_value = get_or_insert(Memory, address);
   trace("mem") << "location " << address << " contains " << no_scientific(new_value) << end();
   if (check_for_null && new_value == 0) {
@@ -95,18 +94,12 @@ void lookup_memory_core(reagent& x, bool check_for_null) {
       raise << "tried to lookup 0\n" << end();
     }
   }
-  x.set_value(new_value+/*skip alloc id in payload*/1);
+  x.set_value(new_value);
   drop_from_type(x, "address");
   drop_one_lookup(x);
 }
 
-:(after "Begin types_coercible(reagent to, reagent from)")
-if (!canonize_type(to)) return false;
-if (!canonize_type(from)) return false;
-:(after "Begin types_match(reagent to, reagent from)")
-if (!canonize_type(to)) return false;
-if (!canonize_type(from)) return false;
-:(after "Begin types_strictly_match(reagent to, reagent from)")
+:(before "End Preprocess types_strictly_match(reagent to, reagent from)")
 if (!canonize_type(to)) return false;
 if (!canonize_type(from)) return false;
 
@@ -164,33 +157,30 @@ void drop_one_lookup(reagent& r) {
 :(scenario get_indirect)
 def main [
   1:address:point <- copy 10/unsafe
-  # skip alloc id
-  11:num <- copy 34
-  12:num <- copy 35
-  20:num <- get 1:address:point/lookup, 0:offset
+  10:num <- copy 34
+  11:num <- copy 35
+  2:num <- get 1:address:point/lookup, 0:offset
 ]
-+mem: storing 34 in location 20
++mem: storing 34 in location 2
 
 :(scenario get_indirect2)
 def main [
   1:address:point <- copy 10/unsafe
-  # skip alloc id
-  11:num <- copy 94
-  12:num <- copy 95
-  20:address:num <- copy 30/unsafe
-  20:address:num/lookup <- get 1:address:point/lookup, 0:offset
+  10:num <- copy 34
+  11:num <- copy 35
+  2:address:num <- copy 20/unsafe
+  2:address:num/lookup <- get 1:address:point/lookup, 0:offset
 ]
-+mem: storing 94 in location 31
++mem: storing 34 in location 20
 
 :(scenario include_nonlookup_properties)
 def main [
   1:address:point <- copy 10/unsafe
-  # skip alloc id
-  11:num <- copy 34
-  12:num <- copy 35
-  20:num <- get 1:address:point/lookup/foo, 0:offset
+  10:num <- copy 34
+  11:num <- copy 35
+  2:num <- get 1:address:point/lookup/foo, 0:offset
 ]
-+mem: storing 34 in location 20
++mem: storing 34 in location 2
 
 :(after "Update GET base in Check")
 if (!canonize_type(base)) break;
@@ -202,12 +192,11 @@ canonize(base);
 :(scenario put_indirect)
 def main [
   1:address:point <- copy 10/unsafe
-  # skip alloc id
-  11:num <- copy 34
-  12:num <- copy 35
+  10:num <- copy 34
+  11:num <- copy 35
   1:address:point/lookup <- put 1:address:point/lookup, 0:offset, 36
 ]
-+mem: storing 36 in location 11
++mem: storing 36 in location 10
 
 :(after "Update PUT base in Check")
 if (!canonize_type(base)) break;
@@ -252,7 +241,7 @@ def main [
   11:num <- copy 14
   12:num <- copy 15
   13:num <- copy 16
-  1:address:array:num <- copy 9/unsafe/skip-alloc-id
+  1:address:array:num <- copy 10/unsafe
   2:array:num <- copy 1:address:array:num/lookup
 ]
 +mem: storing 3 in location 2
@@ -265,7 +254,7 @@ def main [
   1:address:array:num:3 <- copy 1000/unsafe  # pretend allocation
   1:address:array:num:3/lookup <- create-array
 ]
-+mem: storing 3 in location 1001
++mem: storing 3 in location 1000
 
 :(after "Update CREATE_ARRAY product in Check")
 if (!canonize_type(product)) break;
@@ -278,7 +267,7 @@ def main [
   11:num <- copy 14
   12:num <- copy 15
   13:num <- copy 16
-  1:address:array:num <- copy 9/unsafe/skip-alloc-id
+  1:address:array:num <- copy 10/unsafe
   2:num <- index 1:address:array:num/lookup, 1
 ]
 +mem: storing 15 in location 2
@@ -301,7 +290,7 @@ def main [
   11:num <- copy 14
   12:num <- copy 15
   13:num <- copy 16
-  1:address:array:num <- copy 9/unsafe/skip-alloc-id
+  1:address:array:num <- copy 10/unsafe
   1:address:array:num/lookup <- put-index 1:address:array:num/lookup, 1, 34
 ]
 +mem: storing 34 in location 12
@@ -312,7 +301,7 @@ def main [
   2:num <- copy 14
   3:num <- copy 15
   4:num <- copy 16
-  5:address:num <- copy 9/unsafe/skip-alloc-id
+  5:address:num <- copy 10/unsafe
   10:num <- copy 1
   1:array:num:3 <- put-index 1:array:num:3, 5:address:num/lookup, 34
 ]
@@ -325,7 +314,7 @@ def main [
   11:num <- copy 14
   12:num <- copy 15
   13:num <- copy 16
-  1:address:array:num <- copy 9/unsafe/skip-alloc-id
+  1:address:array:num <- copy 10/unsafe
   1:address:array:num <- put-index 1:address:array:num/lookup, 1, 34
 ]
 +error: main: product of 'put-index' must be first ingredient '1:address:array:num/lookup', but got '1:address:array:num'
@@ -348,7 +337,7 @@ def main [
   *5:address:num <- copy 34
   6:num <- copy *5:address:num
 ]
-+run: creating array from 7 locations
++run: creating array of size 4
 +mem: storing 34 in location 6
 
 :(before "Update PUT_INDEX base in Check")
@@ -369,7 +358,7 @@ def main [
   11:num <- copy 14
   12:num <- copy 15
   13:num <- copy 16
-  1:address:array:num <- copy 9/unsafe/skip-alloc-id
+  1:address:array:num <- copy 10/unsafe
   2:num <- length 1:address:array:num/lookup
 ]
 +mem: storing 3 in location 2
@@ -381,8 +370,8 @@ canonize(array);
 
 :(scenario maybe_convert_indirect)
 def main [
-  11:number-or-point <- merge 0/number, 34
-  1:address:number-or-point <- copy 10/unsafe/skip-alloc-id
+  10:number-or-point <- merge 0/number, 34
+  1:address:number-or-point <- copy 10/unsafe
   2:num, 3:bool <- maybe-convert 1:address:number-or-point/lookup, i:variant
 ]
 +mem: storing 1 in location 3
@@ -390,23 +379,23 @@ def main [
 
 :(scenario maybe_convert_indirect_2)
 def main [
-  11:number-or-point <- merge 0/number, 34
-  1:address:number-or-point <- copy 10/unsafe/skip-alloc-id
-  3:address:num <- copy 20/unsafe
-  3:address:num/lookup, 5:bool <- maybe-convert 1:address:number-or-point/lookup, i:variant
+  10:number-or-point <- merge 0/number, 34
+  1:address:number-or-point <- copy 10/unsafe
+  2:address:num <- copy 20/unsafe
+  2:address:num/lookup, 3:bool <- maybe-convert 1:address:number-or-point/lookup, i:variant
 ]
-+mem: storing 1 in location 5
-+mem: storing 34 in location 21
++mem: storing 1 in location 3
++mem: storing 34 in location 20
 
 :(scenario maybe_convert_indirect_3)
 def main [
-  11:number-or-point <- merge 0/number, 34
-  1:address:number-or-point <- copy 10/unsafe/skip-alloc-id
-  3:address:bool <- copy 20/unsafe
-  5:num, 3:address:bool/lookup <- maybe-convert 1:address:number-or-point/lookup, i:variant
+  10:number-or-point <- merge 0/number, 34
+  1:address:number-or-point <- copy 10/unsafe
+  2:address:bool <- copy 20/unsafe
+  3:num, 2:address:bool/lookup <- maybe-convert 1:address:number-or-point/lookup, i:variant
 ]
-+mem: storing 1 in location 21
-+mem: storing 34 in location 5
++mem: storing 1 in location 20
++mem: storing 34 in location 3
 
 :(before "Update MAYBE_CONVERT base in Check")
 if (!canonize_type(base)) break;
@@ -427,9 +416,8 @@ def main [
   1:address:number-or-point <- copy 10/unsafe
   1:address:number-or-point/lookup <- merge 0/number, 34
 ]
-# skip alloc id
-+mem: storing 0 in location 11
-+mem: storing 34 in location 12
++mem: storing 0 in location 10
++mem: storing 34 in location 11
 
 :(before "Update size_mismatch Check for MERGE(x)
 canonize(x);
@@ -439,7 +427,7 @@ canonize(x);
 :(scenario lookup_abbreviation)
 def main [
   1:address:number <- copy 10/unsafe
-  11:number <- copy 34
+  10:number <- copy 34
   3:number <- copy *1:address:number
 ]
 +parse: ingredient: {1: ("address" "number"), "lookup": ()}
>^
db268970 ^



71ee78f2 ^

6030d7e2 ^
71ee78f2 ^

6030d7e2 ^
71ee78f2 ^
ee9a9237 ^
db268970 ^
6030d7e2 ^



ee9a9237 ^
6030d7e2 ^


d11372f6 ^
9d27e966 ^
d11372f6 ^
03d50cc8 ^
71ee78f2 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
71ee78f2 ^
ee9a9237 ^
6030d7e2 ^
9d27e966 ^
ee9a9237 ^
6030d7e2 ^


ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^

d11372f6 ^
03d50cc8 ^
71ee78f2 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
71ee78f2 ^
ee9a9237 ^
6030d7e2 ^
9d27e966 ^
ee9a9237 ^
6030d7e2 ^


ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^

d11372f6 ^
03d50cc8 ^
71ee78f2 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
71ee78f2 ^
ee9a9237 ^
6030d7e2 ^
9d27e966 ^
ee9a9237 ^
6030d7e2 ^


ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^

d11372f6 ^
03d50cc8 ^
71ee78f2 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
71ee78f2 ^
ee9a9237 ^
6030d7e2 ^
9d27e966 ^
ee9a9237 ^
6030d7e2 ^


ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^

ed0e64a9 ^
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


                                              


                                                                                                                                                 
 
       
                 
                                                
                                                                                                                                         
                                      
                                                                                                                                                                                      
                                
                         
 
                                                                                     
                 









                                                   
                   
     

                
                 



                  
     







                                                                                                                                                                       




                                                                                                                                                                             
                          
                                                                                                                                                                        

                                                 
                                                                                                                                                                             
                                                    







                                                                                                                                                                  
                    
                                
                                                                                                                                                                            
                                                          





                                                                                                                                                                               
                                                    

              



              

                                      
                           

                                     
                           
                   
                         
                 



                 
              


                                                                                                                                                                       
 
         
 
                                     
                                 
                   

                     
              
                                 
                      
                                                                                                                                                                  
                                   
                   


                                                             
              
                                    
                      

                                                                                                                                                                  
 
                                                                                  
                                    
                   

                        
              
                                 
                      
                                                                                                                                                                  
                                   
                   


                                                                 
              
                                    
                      

                                                                                                                                                                  
 
                           
                                       
                   

                        
              
                                 
                      
                                                                                                                                                                  
                                   
                   


                                                   
              
                                    
                      

                                                                                                                                                                  
 
                                           
                                       
                   

                        
              
                                 
                      
                                                                                                                                                                  
                                   
                   


                                                                   
              
                                    
                      

                                                                                                                                                                  
 
                            
# Comparing 'regular' length-prefixed strings.

== 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

# main:
    # run-tests()
#?     e8/call test-compare-equal-strings/disp32
    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            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
    b8/copy-to-EAX  1/imm32/exit
    cd/syscall  0x80/imm8

string-equal?:  # s : (address string), benchmark : (address string) -> EAX : boolean
    # pseudocode:
    #   lens = s->length
    #   if (lens != benchmark->length) return false
    #   i = 0
    #   currs = s->data
    #   currb = benchmark->data
    #   while (i < s->length)
    #     c1 = *currs
    #     c2 = *currb
    #     if (c1 != c2) return false
    #     ++i, ++currs, ++currb
    #   return true
    #
    # registers:
    #   i: ECX
    #   lens: EDX
    #   currs: ESI
    #   currb: EDI
    #   c1: EAX
    #   c2: EBX
    #
    # . 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
    # ESI = s
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           6/r32/ESI   8/disp8         .                 # copy *(EBP+8) to ESI
    # EDI = benchmark
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           7/r32/EDI   0xc/disp8       .                 # copy *(EBP+12) to EDI
    # lens/EDX = s->length
    8b/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           2/r32/EDX   .               .                 # copy *ESI to EDX
$string-equal?:lengths:
    # if (lens != benchmark->length) return false
    39/compare                      0/mod/indirect  7/rm32/EDI    .           .             .           2/r32/EDX   .               .                 # compare *EDI with EDX
    75/jump-if-not-equal  $string-equal?:false/disp8
    # currs/ESI = s->data
    81          0/subop/add         3/mod/direct    6/rm32/ESI    .           .             .           .           .               4/imm32           # add to ESI
    # currb/EDI = benchmark->data
    81          0/subop/add         3/mod/direct    7/rm32/EDI    .           .             .           .           .               4/imm32           # add to EDI
    # i/ECX = c1/EAX = c2/EBX = 0
    31/xor                          3/mod/direct    1/rm32/ECX    .           .             .           1/r32/ECX   .               .                 # clear ECX
    31/xor                          3/mod/direct    0/rm32/EAX    .           .             .           0/r32/EAX   .               .                 # clear EAX
    31/xor                          3/mod/direct    3/rm32/EBX    .           .             .           3/r32/EBX   .               .                 # clear EBX
$string-equal?:loop:
    # if (i >= lens) return true
    39/compare                      3/mod/direct    1/rm32/ECX    .           .             .           2/r32/EDX   .               .                 # compare ECX with EDX
    7d/jump-if-greater-or-equal  $string-equal?:true/disp8
    # c1 = *currs
    8a/copy-byte                    0/mod/indirect  6/rm32/ESI    .           .             .           0/r32/AL    .               .                 # copy byte at *ESI to AL
    # c2 = *currb
    8a/copy-byte                    0/mod/indirect  7/rm32/EDI    .           .             .           3/r32/BL    .               .                 # copy byte at *EDI to BL
    # if (c1 != c2) return false
    39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           3/r32/EBX   .               .                 # compare EAX with EBX
    75/jump-if-not-equal  $string-equal?:false/disp8
    # ++i
    41/inc-ECX
    # ++currs
    46/inc-ESI
    # ++currb
    47/inc-EDI
    eb/jump  $string-equal?:loop/disp8
$string-equal?:true:
    b8/copy-to-EAX  1/imm32
    eb/jump  $string-equal?:end/disp8
$string-equal?:false:
    b8/copy-to-EAX  0/imm32
$string-equal?: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

# - tests

test-compare-empty-with-empty-string:
    # EAX = string-equal?("", "")
    # . . push args
    68/push  ""/imm32
    68/push  ""/imm32
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    # check-ints-equal(EAX, 1, msg)
    # . . push args
    68/push  "F - test-compare-empty-with-empty-string"/imm32
    68/push  1/imm32/true
    50/push-EAX
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
    c3/return

test-compare-empty-with-non-empty-string:  # also checks length-mismatch code path
    # EAX = string-equal?("", "Abc")
    # . . push args
    68/push  "Abc"/imm32
    68/push  ""/imm32
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    # check-ints-equal(EAX, 0, msg)
    # . . push args
    68/push  "F - test-compare-empty-with-non-empty-string"/imm32
    68/push  0/imm32/false
    50/push-EAX
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
    c3/return

test-compare-equal-strings:
    # EAX = string-equal?("Abc", "Abc")
    # . . push args
    68/push  "Abc"/imm32
    68/push  "Abc"/imm32
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    # check-ints-equal(EAX, 1, msg)
    # . . push args
    68/push  "F - test-compare-equal-strings"/imm32
    68/push  1/imm32/true
    50/push-EAX
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
    c3/return

test-compare-inequal-strings-equal-lengths:
    # EAX = string-equal?("Abc", "Adc")
    # . . push args
    68/push  "Adc"/imm32
    68/push  "Abc"/imm32
    # . . call
    e8/call  string-equal?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    # check-ints-equal(EAX, 0, msg)
    # . . push args
    68/push  "F - test-compare-inequal-strings-equal-lengths"/imm32
    68/push  0/imm32/false
    50/push-EAX
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
    c3/return

# . . vim:nowrap:textwidth=0