-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.S
371 lines (325 loc) · 6.14 KB
/
parse.S
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
366
367
368
369
370
371
/*
* struct token:
* - value: i64
* - op: u64 (padded char), -1 - end
*/
token_value_size = 8
token_op_size = 8
token_size = token_value_size + token_op_size
.section .text
/*
* tokenize() - tokenize an expression
*
* Args: %rsi - string
* %rdi - tokens[]
*
* Returns: %rax - error code
*
* Error codes: -1 - unexpected symbol, pointed to by %rsi
* -2 - parentheses don't match
*
* Modifies: %rax, %rbx, %rsi, %rdi, %r10, %r11, %r12
* + stoi64
*/
.global tokenize
tokenize:
xor %r11, %r11 # '(' counter
xor %r12, %r12 # ')' counter
.tokenize_next:
xor %rbx, %rbx
call skip_ws
# peek at the next char
movb (%rsi), %bl # %rsi to be incremented later
test %bl, %bl # \0, end of string
jz .tok_done
/* check if it is an operator */
cmp $'+', %bl
je .tok_op
cmp $'-', %bl
je .tok_op
cmp $'*', %bl
je .tok_op
cmp $'/', %bl
je .tok_op
cmp $'^', %bl
je .tok_op
inc %r11
cmp $'(', %bl
je .tok_op
dec %r11
inc %r12
cmp $')', %bl
je .tok_op
dec %r12
cmp $'x', %bl
je .tok_op
jmp .tok_int
.tok_op:
# write an operator token
movq %rbx, (%rdi)
movq $0, token_op_size(%rdi)
add $token_size, %rdi
# next token
inc %rsi
jmp .tokenize_next
.tok_int:
# get an integer
mov $10, %rbx
mov $-1, %r10
call stoi64
test %rbx, %rbx # check for stoi64 errors
jz 1f # success
mov $-1, %rax
ret
1:
# write an integer token
movq $0, (%rdi)
movq %rax, token_op_size(%rdi)
add $token_size, %rdi
# next token
jmp .tokenize_next
.tok_done:
cmp %r11, %r12 # check if all parentheses match
je 1f
mov $-2, %rax
ret
1:
# terminate the array with a -1 "operator"
movq $-1, (%rdi)
movq $0, token_op_size(%rdi)
add $token_size, %rdi
xor %rax, %rax
ret
/*
* transform_postfix() - transform the tokens into a postfix form in place
*
* Postfix form description: an array of i64
* : if [n] != 0 then [n] - number
* , else
* : if [n + 1] == 0 then ([n], [n + 1]) == 0
* , if [n + 1] in {+, -, *, /, ^} then ([n], [n + 1]) - operator
* , if [n + 1] == -1 then end of array
* , else - error
*
* Args: %rsi - tokens[]
*/
.global transform_postfix
transform_postfix:
# setup the stack frame
push %rbp
mov %rsp, %rbp
# %rsi - next token
# %rdi - next output position
mov %rsi, %rdi
.next_token:
# operator vs integer
movq (%rsi), %rax # token.op
test %rax, %rax
jnz .op
/* integer */
movq token_op_size(%rsi), %rax # get the value
movq %rax, (%rdi) # integer is simply written to output
add $8, %rdi
add $token_size, %rsi
test %rax, %rax # | check for zero, because ...
jnz .next_token # |
movq $0, (%rdi) # | ... zero has to be 2 bytes long
add $8, %rdi # |
jmp .next_token
/* operator */
.op:
add $token_size, %rsi
# end of the array
cmp $-1, %rax
je .pop_rest
# x: special case of an operator, just write it to output
cmp $'x', %rax
jne .not_x
movq $0, (%rdi)
movq $'x', 8(%rdi)
add $16, %rdi
jmp .next_token
.not_x:
cmp $'^', %rax
je .pow_pop
cmp $'*', %rax
je .mul_pop
cmp $'/', %rax
je .mul_pop
cmp $'+', %rax
je .add_pop
cmp $'-', %rax
je .add_pop
cmp $'(', %rax
je .push_op
cmp $')', %rax
je .rparen_pop
hlt # can not be reachable if tokenization worked correctly
/* *_pop: pop operators of >= precedence */
.pow_pop:
cmp %rsp, %rbp
je .push_op
mov (%rsp), %rbx
cmp $'^', %rbx # only pops ^
jne .push_op
add $8, %rsp # pop the op
movq $0, (%rdi)
movq %rbx, 8(%rdi)
add $16, %rdi
jmp .pow_pop
.mul_pop:
cmp %rsp, %rbp
je .push_op
mov (%rsp), %rbx
cmp $'+', %rbx # | don't pop these
je .push_op # |
cmp $'-', %rbx # |
je .push_op # |
cmp $'(', %rbx # |
je .push_op # |
cmp $')', %rbx # |
je .push_op # |
add $8, %rsp # pop the operator
movq $0, (%rdi)
movq %rbx, 8(%rdi)
add $16, %rdi
jmp .mul_pop
.add_pop:
cmp %rsp, %rbp
je .push_op
mov (%rsp), %rbx
cmp $'(', %rbx # | don't pop these
je .push_op # |
cmp $')', %rbx # |
je .push_op # |
add $8, %rsp # pop the operator
movq $0, (%rdi)
movq %rbx, 8(%rdi)
add $16, %rdi
jmp .add_pop
# pop everything until '('
.rparen_pop:
pop %rbx
cmp $'(', %rbx
je .next_token
movq $0, (%rdi)
movq %rbx, 8(%rdi)
add $16, %rdi
jmp .rparen_pop
.push_op:
push %rax
jmp .next_token
.pop_rest:
# pop the rest of the stack
cmp %rsp, %rbp # stack is empty,
je .post_exit # leave
pop %rbx
movq $0, (%rdi)
movq %rbx, 8(%rdi)
add $16, %rdi
jmp .pop_rest
.post_exit:
# terminate the array with a -1 "operator"
movq $0, (%rdi)
movq $-1, 8(%rdi)
add $16, %rdi
leave
ret
/*
* exec_postfix() - execute a postfix expression
*
* Args: %rsi - postfix expression
* %rdi - x
*
* Returns: %rax - result
*/
.global exec_postfix
exec_postfix:
# setup the stack frame
push %rbp
mov %rsp, %rbp
.exec_next:
mov (%rsi), %rbx
add $8, %rsi
test %rbx, %rbx # | 2-byte value: either 0, or an operator
jz .complex # |
push %rbx # all ints are pushed to be used as operands later
jmp .exec_next
.complex:
mov (%rsi), %rbx
add $8, %rsi
test %rbx, %rbx
jnz .check_x
push %rbx # it is a 2-byte 0
jmp .exec_next
.check_x:
# if it is 'x', push the passed x value
cmp $'x', %rbx
jne .exec_op
push %rdi
jmp .exec_next
/* execute the operator and push the result to the stack */
.exec_op:
cmp $-1, %rbx # end of the expression
je .exec_exit
pop %rcx # | operands
pop %rax # |
cmp $'+', %rbx
jne 1f
add %rcx, %rax
push %rax
jmp .exec_next
1:
cmp $'-', %rbx
jne 1f
sub %rcx, %rax
push %rax
jmp .exec_next
1:
cmp $'*', %rbx
jne 1f
imul %rcx
push %rax
jmp .exec_next
1:
cmp $'/', %rbx
jne 1f
cqo
idiv %rcx
push %rax
jmp .exec_next
1:
cmp $'^', %rbx
jne 1f
call pow
push %rax
jmp .exec_next
1:
.exec_exit:
movq (%rsp), %rax
mov %rbp, %rbx # | the stack should only contain 1 value,
sub %rsp, %rbx # | the result
cmp $8, %rbx # |
jne .exec_err # |
add $8, %rsp
leave
ret
.exec_err:
hlt
/* VERY BAD!!! AWFUL CODE!!!
* pow() - raise a to the power of b (a^b)
*
* Args: %rax - a
* %rcx - b
*
* Uses: %rbx
*
* Returns: %rax - a^b
*/
pow:
dec %rcx
mov %rax, %rbx
1:
imul %rbx
loop 1b
ret