-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.s
440 lines (379 loc) · 6.54 KB
/
runtime.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
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
#
# Using this file then there is no need to use
# sysspim.s or to compile runtime.c
#
#
# NOTE: Do not rename labels because they names are
# defined in Tiger Standard Library
#
# NOTE: https://godbolt.org/ could be used
# to translate C to MIPS assembler
.text
# int *initArray(int size, int init) {
# int i;
# int *a = (int *) malloc(size *sizeof(int));
#
# for (i = 0; i < size; i++)
# a[i] = init;
#
# return a;
# }
initArray:
move $a3, $a0
addiu $a0, $a0, 1
sll $a0, $a0, 2
li $v0, 9
syscall
move $a2, $v0
sw $a3, ($a2)
sub $a0, $a0, 4
add $a2, $a2, 4
b Lrun1
Lrunt2:
sw $a1, ($a2)
sub $a0, $a0, 4
add $a2, $a2, 4
Lrun1:
bgtz $a0, Lrunt2
jr $ra
# int *allocRecord(int size) {
# int i;
# int *p, *a;
#
# p = a = (int *) malloc(size);
#
# for (i = 0; i < size; i += sizeof(int))
# *p++ = 0;
#
# return a;
# }
allocRecord:
li $v0, 9
syscall
move $a2, $v0
b Lrunt4
Lrunt3:
sw $0, ($a2)
sub $a0, $a0, 4
add $a2, $a2, 4
Lrunt4:
bgtz $a0, Lrunt3
jr $ra
# struct string {
# int length;
# unsigned char chars[1];
# };
# int stringEqual(struct string *s, struct string *t) {
# int i;
# if (s == t) return 1;
#
# if (s->length != t->length) return 0;
#
# for (i = 0; i < s->length; i++)
# if (s->chars[i] != t->chars[i]) return 0;
#
# return 1;
# }
stringEqual:
beq $a0, $a1, Lrunt10
lw $a2, ($a0)
lw $a3, ($a1)
addiu $a0, $a0, 4
addiu $a1, $a1, 4
beq $a2, $a3, Lrunt11
Lrunt13:
li $v0, 0
j $ra
Lrunt12:
lbu $t0, ($a0)
lbu $t1, ($a1)
bne $t0, $t1, Lrunt13
addiu $a0, $a0, 1
addiu $a1, $a1, 1
addiu $a2, $a2, -1
Lrunt11:
bgez $a2, Lrunt12
Lrunt10:
li $v0, 1
j $ra
# void print(struct string *s) {
# int i;
# unsigned char *p = s->chars;
#
# for (i = 0; i < s->length; i++, p++)
# putchar(*p);
# }
.data
.align 4
putchar_buf: .byte 0 0
.text
print:
lw $t1, 0($a0)
beqz $t1, Lrunt56
add $t0, $a0, 4
la $a0, putchar_buf
Lrunt57:
lbu $t3, ($t0)
sb $t3, 0($a0)
li $v0, 4
syscall
addiu $t0, $t0, 1
addiu $t1, $t1, -1
bgtz $t1, Lrunt57
Lrunt56:
jr $ra
# void flush() {
# fflush(stdout);
# }
flush:
jr $ra
# struct string consts[256];
#
# int main() {
# int i;
# for (i = 0; i < 256; i++) {
# consts[i].length = 1;
# consts[i].chars[0] = i;
# }
#
# return __main(0 /* static link!? */);
# }
.data
.align 4
Runtconsts: .space 2048
Runtempty: .word 0
.text
main:
li $a0, 0
la $a1, Runtconsts
li $a2, 1
Lrunt20:
sw $a2, ($a1)
sb $a0, 4($a1)
addiu $a1, $a1, 8
addiu $a0, $a0, 1
slt $a3, $a0, 256
bnez $a3, Lrunt20
li $a0, 0
# See how main method is defined in FrontEnd.fs
j __main
# int ord(struct string *s) {
#
# if (s->length == 0)
# return -1;
#
# else
# return s->chars[0];
# }
ord:
lw $a1, ($a0)
li $v0, -1
beqz $a1, Lrunt5
lbu $v0, 4($a0)
Lrunt5:
jr $ra
# struct string empty = { 0, "" };
#
# struct string *chr(int i) {
# if (i < 0 || i >= 256) {
# printf("chr(%d) out of range\n", i);
# exit(1);
# }
#
# return consts + i;
# }
.data
Lrunt30: .asciiz "character out of range\n"
.text
chr:
srl $a1, $a0, 8
bnez $a1, Lrunt31
sll $a0, $a0, 3
la $v0, Runtconsts($a0)
j $ra
Lrunt31:
li $v0, 4
la $a0, Lrunt30
syscall
li $v0, 10
syscall
# int size(struct string *s) {
# return s->length;
# }
size:
lw $v0, ($a0)
jr $ra
# struct string *substring(struct string *s, int first, int n) {
#
# if (first < 0 || first + n > s->length) {
# printf("substring([%d], %d, %d) out of range\n", s->length, first, n);
# exit(1);
# }
#
# if (n == 1) return consts + s->chars[first]; {
#
# struct string *t = (struct string *) malloc(sizeof(int) + n);
#
# int i;
# t->length = n;
#
# for (i = 0; i < n; i++)
# t->chars[i] = s->chars[first + i];
#
# return t;
# }
# }
.data
Lrunt40: .asciiz "substring out of bounds\n"
.text
substring:
lw $t1, ($a0)
bltz $a1, Lrunt41
add $t2, $a1, $a2
sgt $t3, $t2, $t1
bnez $t3, Lrunt41
add $t1, $a0, $a1
addiu $t1, $t1, 4
bne $a2, 1, Lrunt42
lbu $a0, ($t1)
b chr
Lrunt42:
bnez $a2, Lrunt43
la $v0, Runtempty
jr $ra
Lrunt43:
addi $a0, $a2, 4
li $v0, 9
syscall
move $t2, $v0
sw $a2, ($t2)
addiu $t2, $t2, 4
Lrunt44:
lbu $t3, ($t1)
sb $t3, ($t2)
addiu $t1, $t1, 1
addiu $t2, $t2, 1
addiu $a2, $a2, -1
bgtz $a2 ,Lrunt44
jr $ra
Lrunt41:
li $v0, 4
la $a0, Lrunt40
syscall
li $v0, 10
syscall
# struct string *concat(struct string *a, struct string *b) {
#
# if (a->length == 0)
# return b;
#
# else if (b->length == 0)
# return a;
#
# else {
# int i, n = a->length + b->length;
#
# struct string *t = (struct string * ) malloc(sizeof(int) + n);
#
# t->length = n;
#
# for (i = 0; i < a->length; i++)
# t->chars[i] = a->chars[i];
#
# for (i = 0; i < b->length; i++)
# t->chars[i + a->length] = b->chars[i];
#
# return t;
# }
# }
concat:
lw $t0, ($a0)
lw $t1, ($a1)
beqz $t0, Lrunt50
beqz $t1, Lrunt51
addiu $t2, $a0, 4
addiu $t3, $a1, 4
add $t4, $t0, $t1
addiu $a0, $t4, 4
li $v0,9
syscall
addiu $t5, $v0, 4
sw $t4, ($v0)
Lrunt52:
lbu $a0, ($t2)
sb $a0, ($t5)
addiu $t2, $t2, 1
addiu $t5, $t5, 1
addiu $t0, $t0, -1
bgtz $t0, Lrunt52
Lrunt53:
lbu $a0, ($t3)
sb $a0, ($t5)
addiu $t3, $t3, 1
addiu $t5, $t5, 1
addiu $t1, $t1, -1
bgtz $t1, Lrunt53
j $ra
Lrunt50:
move $v0, $a1
j $ra
Lrunt51:
move $v0, $a0
j $ra
# int not(int i){
# return !i;
# }
_not:
seq $v0, $a0, 0
jr $ra
# # undef getchar
#
# struct string *getchar() {
# int i = getc(stdin);
#
# if (i == EOF)
# return ∅
# else
# return consts + i;
# }
.data
.align 4
getchar_buf: .byte 0 0
.text
getchar:
la $a0, getchar_buf
li $a1, 2
li $v0, 8
syscall
lb $v0, ($a0)
bnez $v0, Lrunt58
la $v0, Runtempty
jr $ra
Lrunt58:
sll $v0, $v0, 3
la $v0, Runtconsts($v0)
jr $ra
# void checkArrayBounds(int *a, int i) {
# if (*a < 0 || *a >= i) {
# printf("index %d out of range\n", i);
# exit(1);
# }
# }
.data
Lrunt54: .asciiz "index out of range\n"
.text
checkArrayBounds:
lw $a2, ($a0)
bltz $a1, Lrunt55
bge $a1, $a2, Lrunt55
jr $ra
Lrunt55:
li $v0, 4
la $a0, Lrunt54
syscall
li $v0, 10
syscall
# void exit(int i)
exit:
li $v0, 17
syscall