-
Notifications
You must be signed in to change notification settings - Fork 0
/
优化后中间代码.TXT
510 lines (492 loc) · 7.57 KB
/
优化后中间代码.TXT
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
const int _ = -100000
const int __ = 0
const int _2 = 2
const int _PoS_3 = 3
const int neg5_ = -5
const char add = '+'
const char sub = '-'
const char mul = '*'
const char _div = '/'
const int MAX_FIBO = 100
var int i
var int N
var int n
array int fibo 100
array int num 100
array char str 1000
int init_fibo()
i = 0
label_0:
$t0 = i
$t0 < 51
BZ label_1
$t2 = i
$t3 = i
$t4 = -$t3
fibo[$t2] = $t4
$t5 = i
$t6 = 99 - $t5
fibo[$t6] = 0
$t7 = i
$t8 = $t7 + 1
i = $t8
GOTO label_0
label_1:
ret
int fibonacci()
para int n
n == 0
BZ label_3
ret
label_3:
n == 1
BZ label_4
ret
label_4:
$t2 = fibo[n]
$t2 == 0
BZ label_5
$t4 = n - 1
push $t4
call fibonacci
$t5 = RET
$t6 = n - 2
push $t6
call fibonacci
$t7 = RET
$t8 = $t5 + $t7
fibo[n] = $t8
label_5:
$t9 = fibo[n]
ret
label_2:
ret
int factorial()
para int n
n == 0
BZ label_6
ret
label_6:
$t1 = n - 1
push $t1
call factorial
$t2 = RET
$t3 = n * $t2
ret
void swap()
para int i
para int j
var int tmp
$t0 = num[i]
tmp = $t0
$t1 = num[j]
num[i] = $t1
num[j] = tmp
ret
void sort()
para int left
para int right
var int i
var int j
left >= right
BZ label_7
ret
label_7:
i = left
$t1 = left + 1
j = $t1
label_8:
j <= right
BZ label_9
$t3 = num[j]
$t4 = num[left]
$t3 < $t4
BZ label_10
$t6 = i + 1
i = $t6
push i
push j
call swap
label_10:
$t7 = j + 1
j = $t7
GOTO label_8
label_9:
push left
push i
call swap
push left
$t8 = i - 1
push $t8
call sort
$t9 = i + 1
push $t9
push right
call sort
ret
int calculate()
para int a
para int b
para char op
op == '+'
BZ label_12
$t1 = a + b
ret
label_12:
op == '-'
BZ label_13
$t3 = a - b
ret
label_13:
op == '*'
BZ label_14
$t5 = a * b
ret
label_14:
op == '/'
BZ label_15
$t7 = a / b
ret
label_15:
label_11:
ret
int charcomp()
para char a
para char b
para int op
const int _true = 1
const int _false = 0
var int ret
ret = 0
op == 1
BZ label_17
a < b
BZ label_18
ret = 1
label_18:
GOTO label_16
label_17:
op == 2
BZ label_19
a <= b
BZ label_20
ret = 1
label_20:
GOTO label_16
label_19:
op == 3
BZ label_21
a > b
BZ label_22
ret = 1
label_22:
GOTO label_16
label_21:
op == 4
BZ label_23
a >= b
BZ label_24
ret = 1
label_24:
GOTO label_16
label_23:
op == 5
BZ label_25
a != b
BZ label_26
ret = 1
label_26:
GOTO label_16
label_25:
op == 6
BZ label_27
a == b
BZ label_28
ret = 1
label_28:
GOTO label_16
label_27:
label_16:
ret
void strrev()
para int i
para int j
var char c
label_29:
i < j
BZ label_30
$t1 = str[i]
c = $t1
$t2 = str[j]
str[i] = $t2
str[j] = c
$t3 = i + 1
i = $t3
$t4 = j - 1
j = $t4
GOTO label_29
label_30:
ret
void main()
const char x = 'x'
const char X = 'X'
var int result
var int a
var int b
var int nnn
var int i
var int tmp
var char c
var int fibo
push 5
call factorial
$t0 = RET
push 5
call factorial
$t1 = RET
$t2 = $t0 + $t1
$t3 = 10 + $t2
$t4 = 9 + $t3
$t5 = 8 + $t4
$t6 = 7 + $t5
$t7 = 6 + $t6
$t8 = 5 + $t7
$t9 = 7 + $t8
$t10 = 2 + $t9
$t11 = 1 + $t10
fibo = $t11
push "fibo: "
push fibo
call printf
push "Test start:"
call printf
push "Test printf:"
call printf
push "This is a String:"
call printf
push " !\\n_\\r_\\t }~"
call printf
push '_'
call printf
push -100000
call printf
push 'x'
call printf
push 'x'
call printf
push 'X'
call printf
push 'X'
call printf
push -1024
call printf
push "X*2+x/3 = "
push 216
call printf
push "Test scanf:"
call printf
push "Please input an integer nnn:"
call printf
push nnn
call scanf
push "nnn is "
push nnn
call printf
push "Please input a character x:"
call printf
push c
call scanf
push "x is "
push c
call printf
push "Please input 2 integers a, b and a character x:"
call printf
push a
push b
push c
call scanf
push "a is "
push a
call printf
push "b is "
push b
call printf
push "x is "
push c
call printf
push "Test charcomp:"
call printf
i = 1
label_31:
i <= 6
BZ label_32
push 'A'
push 'a'
push i
call charcomp
$t13 = RET
push $t13
call printf
push 'a'
push 'a'
push i
call charcomp
$t14 = RET
push $t14
call printf
push 'a'
push 'A'
push i
call charcomp
$t15 = RET
push $t15
call printf
$t16 = i + 1
i = $t16
GOTO label_31
label_32:
push "Test calculate:"
call printf
a = -56091
$t17 = a * 1
$t18 = -$t17
$t19 = $t18 + 0
$t20 = $t19 / 123
b = $t20
push "a: "
push a
call printf
push "b: "
push b
call printf
push a
push b
push '+'
call calculate
$t21 = RET
result = $t21
push "a b add: "
push result
call printf
push a
push b
push '-'
call calculate
$t22 = RET
result = $t22
push "a b sub: "
push result
call printf
push a
push b
push '*'
call calculate
$t23 = RET
result = $t23
push "a b mul: "
push result
call printf
push "a b div: "
push a
push b
push '/'
call calculate
$t24 = RET
push $t24
call printf
push "Test strrev:"
call printf
str[0] = 'H'
str[1] = 'e'
str[2] = 'l'
str[3] = 'l'
str[4] = 'o'
str[5] = '_'
str[6] = '1'
str[7] = '2'
str[8] = '3'
push "str before reverse:"
call printf
i = 0
label_33:
i < 9
BZ label_34
$t26 = str[i]
push $t26
call printf
$t27 = i + 1
i = $t27
GOTO label_33
label_34:
push 0
push 8
call strrev
push "str after reverse:"
call printf
i = 0
label_35:
i < 9
BZ label_36
$t29 = str[i]
push $t29
call printf
$t30 = i + 1
i = $t30
GOTO label_35
label_36:
push "Test factorial and fibonacci:"
call printf
push "Please input 2 integers N, n:"
call printf
push N
push n
call scanf
push "factorial N is "
$t31 = N
push $t31
call factorial
$t32 = RET
push $t32
call printf
call init_fibo
push "fibonacci n is "
$t33 = n
push $t33
call fibonacci
$t34 = RET
push $t34
call printf
push "Test sort:"
call printf
push "Please input 10 numbers:"
call printf
i = 0
label_37:
i < 10
BZ label_38
push tmp
call scanf
num[i] = tmp
$t36 = i + 1
i = $t36
GOTO label_37
label_38:
push 0
push 9
call sort
push "sort result:"
call printf
i = 0
label_39:
i < 10
BZ label_40
$t38 = num[i]
push $t38
call printf
$t39 = i + 1
i = $t39
GOTO label_39
label_40:
push "Test finished."
call printf
ret