-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimply_arm.py
257 lines (247 loc) · 5.21 KB
/
simply_arm.py
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
simply_arm_template = """/*************************
* Generated with SimPly *
*************************/
/************************
* Original source code *
************************/
// File : {filename:}
{source:}
/************************
* Abstract Syntax Tree *
************************/
{ast:}
/**********************
* Control Flow Graph *
**********************/
{cfg:}
/*********************
* ARM assembly code *
*********************/
.global _start
_start:
/* Start of compiled part */
{arm_code:}
/* End of compiled part */
end:
MOV R7, #1 // syscall 1 : exit
SWI 0
/* print "Functions" */
.printbool:
CMP R0, #0
BEQ .printfalse
// print
MOV R7, #4 // syscall 4 : write screen R0,R1,R2
MOV R0, #1 // dest
LDR R1, =true // pchar
MOV R2, #5 // len of mess
SWI 0 // syscall
BX LR
.printfalse:
// print
MOV R7, #4 // syscall 4 : write screen R0,R1,R2
MOV R0, #1 // dest
LDR R1, =false // pchar
MOV R2, #6 // len of mess
SWI 0 // syscall
BX LR
.printint:
// zero case
CMP R0, #0
BNE .nonzero
// print 0
MOV R0, #48 // '0'
LDR R1, =char
STR R0, [R1]
// print
MOV R7, #4 // syscall 4 : write screen R0,R1,R2
MOV R0, #1 // dest
LDR R1, =char // pchar
MOV R2, #1 // len of mess
SWI 0 // syscall
B .loopprintend
.nonzero:
CMP R0, #0
BPL .positive
MOV R1, #-1
MUL R0, R1
PUSH {{R0}}
// print -
MOV R0, #45 // '-'
LDR R1, =char
STR R0, [R1]
// print
MOV R7, #4 // syscall 4 : write screen R0,R1,R2
MOV R0, #1 // dest
LDR R1, =char // pchar
MOV R2, #1 // len of mess
SWI 0 // syscall
POP {{R0}}
.positive:
// R1 : max n so 10^n<=R0
// R2 : corresponding 10^n
// R3 : 10^(n+1)
MOV R1, #0
MOV R2, #1
MOV R3, #10
.loopexpcond:
CMP R0, R3
BMI .loopexpend
// next candidate is OK :
ADD R1, #1
MOV R2, R3
// R3 <- R3*10
LSL R3, #2
ADD R3, R2
LSL R3, #1
B .loopexpcond
.loopexpend:
.loopprintcond:
CMP R1, #0
BMI .loopprintend
// R3 : next digit
MOV R3, #0
.loopdigitcond:
CMP R0, R2
BMI .loopdigitend
SUB R0, R2
ADD R3, #1
B .loopdigitcond
.loopdigitend:
PUSH {{R0,R1}}
// R3 is computed, put it in char
ADD R3, #48 // add '0'
LDR R1, =char
STR R3, [R1]
// print
MOV R7, #4 // syscall 4 : write screen R0,R1,R2
MOV R0, #1 // dest
LDR R1, =char // pchar
MOV R2, #1 // len of mess
SWI 0 // syscall
POP {{R0,R1}}
SUB R1, #1
MOV R2, #1
MOV R3, #0
.loopreexpcond:
CMP R3, R1
BPL .loopreexpend
// R2 <- R2*10
MOV R4, R2
LSL R2, #2
ADD R2, R4
LSL R2, #1
ADD R3, #1
B .loopreexpcond
.loopreexpend:
B .loopprintcond
.loopprintend:
MOV R0, #10 // newline
LDR R1, =char
STR R0, [R1]
// print
MOV R7, #4 // syscall 4 : write screen R0,R1,R2
MOV R0, #1 // dest
LDR R1, =char // pchar
MOV R2, #1 // len of mess
SWI 0 // syscall
BX LR
.printstr:
// len(str) in R0
// &str in R1
MOV R2, R0
MOV R7, #4 // syscall 4 : write screen R0,R1,R2
MOV R0, #1 // dest
SWI 0 // syscall
BX LR
/* Data zone */
.data
char:
.ascii " "
true:
.ascii "True\\n"
false:
.ascii "False\\n"
zerodivisionerror:
.ascii "ZeroDivisionError: integer division or modulo by zero\\n"
{arm_data:}"""
arm_fast_multiplication_template = """
// Fast multiplication
// R0 : result
// R1 : operand 1 (a)
// R2 : operand 2 (b)
MOV R0, #0 // z
// test b>=0
CMP R2, #0
BPL .{label:}_mul_loop_condition
// if b<0, a=-a and b=-b
SUB R1, R0, R1
SUB R2, R0, R2
.{label:}_mul_loop_condition:
CMP R2, #0 // while b!=0
BEQ .{label:}_mul_loop_end
MOV R3, R2 // cpy b
AND R3, #1 // b%2
CMP R3, #1
BEQ .{label:}_mul_branch_else_odd // b is odd
LSR R2, #1 // b = b//2
LSL R1, #1 // a = a*2
B .{label:}_mul_branch_end_even
.{label:}_mul_branch_else_odd:
SUB R2, #1 // b = b-1
ADD R0, R1 // z = z+a
.{label:}_mul_branch_end_even:
B .{label:}_mul_loop_condition
.{label:}_mul_loop_end:
"""
arm_fast_division_template = """
// Fast division
// R0 : result
// R1 : operand 1 (a) -> will become the remainder
// R2 : operand 2 (b)
MOV R0, #0 // z
// Testing div/0 error
CMP R2, #0
BNE .{label:}_div_nonzero
MOV R0, #54
LDR R1, =zerodivisionerror
BL .printstr
B end
.{label:}_div_nonzero:
// b negative?
CMP R2, #0
BPL .{label:}_div_b_positive
SUB R1, R0, R1 // still z=0
SUB R2, R0, R2 // for -()
.{label:}_div_b_positive:
// a negative?
MOV R4, #1 // R4 = both_positive
CMP R1, #0
BPL .{label:}_div_a_positive
MOV R4, #0 // R4 = must be inverted
SUB R1, R0, R1
.{label:}_div_a_positive:
MOV R3, R2 // c = b
.{label:}_div_loop1_condition:
CMP R1, R3 // a >= c?
BMI .{label:}_div_loop1_end // a<c
LSL R3, #1 // c <<= 1
B .{label:}_div_loop1_condition
.{label:}_div_loop1_end:
LSR R3, #1 // c >>= 1
.{label:}_div_loop2_condition:
CMP R3, R2 // c>=b?
BMI .{label:}_div_loop2_end // c<b
LSL R0, #1 // z <<=1
CMP R1, R3 // if a>=c?
BMI .{label:}_div_else // a<c
SUB R1, R3 // a = a-c
ADD R0, #1 // z = z+1
.{label:}_div_else:
LSR R3, #1 // c >>=1
B .{label:}_div_loop2_condition
.{label:}_div_loop2_end:
CMP R4, #1
BEQ .{label:}_div_both_positive
SUB R0, R4, R0 // R4 is 0, so...
.{label:}_div_both_positive:
"""