about summary refs log tree commit diff stats
path: root/linux/133subx-widths.subx
blob: 6d8ce8de4058b01c87c01a65a8bc19ca15a685b7 (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
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
# Helpers to compute widths of SubX arguments based on their metadata.
#   /imm8 => 1 byte,
#   /disp32 => 4 bytes,
#   and so on.

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

compute-width:  # word: (addr array byte) -> result/eax: int
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    51/push-ecx
    # eax = word
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           0/r32/eax   8/disp8         .                 # copy *(ebp+8) to ecx
    # var ecx: (addr byte) = &word[word->size]
    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
    # eax = word->data
    05/add-to-eax  4/imm32
    # var in/ecx: slice = {eax, ecx}
    51/push-ecx
    50/push-eax
    89/copy                         3/mod/direct    1/rm32/ecx    .           .             .           4/r32/esp   .               .                 # copy esp to ecx
    # return compute-width-of-slice(ecx)
    # . . push args
    51/push-ecx
    # . . call
    e8/call  compute-width-of-slice/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
$compute-width:end:
    # . reclaim locals
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # . restore registers
    59/pop-to-ecx
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

compute-width-of-slice:  # s: (addr slice) -> result/eax: int
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    51/push-ecx
    # ecx = s
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           1/r32/ecx   8/disp8         .                 # copy *(ebp+8) to ecx
    # if (has-metadata?(word, "imm32")) return 4
    # . eax = has-metadata?(word, "imm32")
    # . . push args
    68/push  "imm32"/imm32
    51/push-ecx
    # . . call
    e8/call  has-metadata?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # . if (eax != false) return 4
    3d/compare-eax-and  0/imm32/false
    b8/copy-to-eax  4/imm32         # ZF is set, so we can overwrite eax now
    75/jump-if-!=  $compute-width-of-slice:end/disp8
    # if (has-metadata?(word, "disp32")) return 4
    # . eax = has-metadata?(word, "disp32")
    # . . push args
    68/push  "disp32"/imm32
    51/push-ecx
    # . . call
    e8/call  has-metadata?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # . if (eax != false) return 4
    3d/compare-eax-and  0/imm32/false
    b8/copy-to-eax  4/imm32         # ZF is set, so we can overwrite eax now
    75/jump-if-!=  $compute-width-of-slice:end/disp8
    # if (has-metadata?(word, "imm16")) return 2
    # . eax = has-metadata?(word, "imm16")
    # . . push args
    68/push  "imm16"/imm32
    51/push-ecx
    # . . call
    e8/call  has-metadata?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # . if (eax != false) return 2
    3d/compare-eax-and  0/imm32/false
    b8/copy-to-eax  2/imm32         # ZF is set, so we can overwrite eax now
    75/jump-if-!=  $compute-width-of-slice:end/disp8
    # if (has-metadata?(word, "disp16")) return 2
    # . eax = has-metadata?(word, "disp16")
    # . . push args
    68/push  "disp16"/imm32
    51/push-ecx
    # . . call
    e8/call  has-metadata?/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # . if (eax != false) return 2
    3d/compare-eax-and  0/imm32/false
    b8/copy-to-eax  2/imm32         # ZF is set, so we can overwrite eax now
    75/jump-if-!=  $compute-width-of-slice:end/disp8
    # otherwise return 1
    b8/copy-to-eax  1/imm32
$compute-width-of-slice:end:
    # . restore registers
    59/pop-to-ecx
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

test-compute-width:
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
$test-compute-width:imm8:
    # eax = compute-width("0x2/imm8")
    # . . push args
    68/push  "0x2/imm8"/imm32
    # . . call
    e8/call  compute-width/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 1, msg)
    # . . push args
    68/push  "F - test-compute-width: 0x2/imm8"/imm32
    50/push-eax
    68/push  1/imm32
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
$test-compute-width:imm16:
    # eax = compute-width("4/imm16")
    # . . push args
    68/push  "4/imm16"/imm32
    # . . call
    e8/call  compute-width/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 2, msg)
    # . . push args
    68/push  "F - test-compute-width: 4/imm16"/imm32
    50/push-eax
    68/push  2/imm32
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
$test-compute-width:imm32:
    # eax = compute-width("4/imm32")
    # . . push args
    68/push  "4/imm32"/imm32
    # . . call
    e8/call  compute-width/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 4, msg)
    # . . push args
    68/push  "F - test-compute-width: 4/imm32"/imm32
    50/push-eax
    68/push  4/imm32
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
$test-compute-width:disp8:
    # eax = compute-width("foo/disp8")
    # . . push args
    68/push  "foo/disp8"/imm32
    # . . call
    e8/call  compute-width/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 1, msg)
    # . . push args
    68/push  "F - test-compute-width: foo/disp8"/imm32
    50/push-eax
    68/push  1/imm32
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
$test-compute-width:disp16:
    # eax = compute-width("foo/disp16")
    # . . push args
    68/push  "foo/disp16"/imm32
    # . . call
    e8/call  compute-width/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 2, msg)
    # . . push args
    68/push  "F - test-compute-width: foo/disp16"/imm32
    50/push-eax
    68/push  2/imm32
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
$test-compute-width:disp32:
    # eax = compute-width("foo/disp32")
    # . . push args
    68/push  "foo/disp32"/imm32
    # . . call
    e8/call  compute-width/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 4, msg)
    # . . push args
    68/push  "F - test-compute-width: foo/disp32"/imm32
    50/push-eax
    68/push  4/imm32
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
$test-compute-width:no-metadata:
    # eax = compute-width("45")
    # . . push args
    68/push  "45"/imm32
    # . . call
    e8/call  compute-width/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 1, msg)
    # . . push args
    68/push  "F - test-compute-width: 45 (no metadata)"/imm32
    50/push-eax
    68/push  1/imm32
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# . . vim:nowrap:textwidth=0