summaryrefslogtreecommitdiff
path: root/boot/echo-boot.s
blob: a8faecb37c0a24f6f5db4dc47ad59b6bf30247c8 (plain)
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
.global _start
# s1 - Contains the current position in the source text.

.section .rodata
SOURCE_BUFFER_SIZE: .long 20480

.section .bss
.type source_code, @object
.size source_code, 20480
source_code: .zero 20480

.section .text

# Reads a token and returns its length in a0.
# _read_token doesn't change s1, it finds the length of the token s1 is pointing to.
.type _read_token, @function
_read_token:
	# Prologue.
	addi sp, sp, -16
	sw ra, 12(sp)
	sw s0, 8(sp)
	addi s0, sp, 16

	lbu t0, (s1) # t0 = Current character.
	sw zero, 4(sp)

	li t1, '.'
	beq t0, t1, .Ltoken_character_single

	li t1, ','
	beq t0, t1, .Ltoken_character_single

	li t1, ':'
	beq t0, t1, .Ltoken_character_colon

	li t1, ';'
	beq t0, t1, .Ltoken_character_single

	li t1, '('
	beq t0, t1, .Ltoken_character_single

	li t1, ')'
	beq t0, t1, .Ltoken_character_single

	li t1, '['
	beq t0, t1, .Ltoken_character_single

	li t1, ']'
	beq t0, t1, .Ltoken_character_single

.Ltoken_character_loop_do: # Expect an identifier or a number.
	lw t6, 4(sp)
	add t1, s1, t6
	lbu a0, (t1) # a0 = Current character.

	call is_alnum

	beqz a0, .Ltoken_character_end
	lw t6, 4(sp)
	addi t6, t6, 1
	sw t6, 4(sp)
	j .Ltoken_character_loop_do

.Ltoken_character_single:
	lw t6, 4(sp)
	addi t6, t6, 1
	sw t6, 4(sp)
	j .Ltoken_character_end

.Ltoken_character_colon:
	lbu t0, 1(s1) # t0 = The character after the colon.
	lw t6, 4(sp)
	addi t6, t6, 1
	sw t6, 4(sp)

	li t1, '='
	beq t0, t1, .Ltoken_character_single
	j .Ltoken_character_end

.Ltoken_character_end:
	lw a0, 4(sp)

	# Epilogue.
	lw ra, 12(sp)
	lw s0, 8(sp)
	addi sp, sp, 16
	ret

# Skips the spaces till the next non space character.
.type _skip_spaces, @function
_skip_spaces:
.Lspace_loop_do:
	lbu t0, (s1) # t0 = Current character.

	li t1, ' '
	beq t0, t1, .Lspace_loop_repeat
	li t1, '\t'
	beq t0, t1, .Lspace_loop_repeat
	li t1, '\n'
	beq t0, t1, .Lspace_loop_repeat
	li t1, '\r'
	beq t0, t1, .Lspace_loop_repeat

	j .Lspace_loop_end
.Lspace_loop_repeat:
	addi s1, s1, 1
	j .Lspace_loop_do

.Lspace_loop_end:
	ret

# Skips tabs at the line beginning.
.type _skip_indentation, @function
_skip_indentation:
.Lskip_indentation_do:
	lbu t0, (s1)

	li t1, '\t'
	beq t0, t1, .Lskip_indentation_skip

	j .Lskip_indentation_end

.Lskip_indentation_skip:
	addi s1, s1, 1
	j .Lskip_indentation_do

.Lskip_indentation_end:
	ret

# Parameters:
# a0 - Line length.
.type _skip_comment, @function
_skip_comment:
	add s1, s1, a0
	addi s1, s1, 1 # Skip the new line.
	ret

# Parameters:
# a0 - Line length.
.type _compile_assembly, @function
_compile_assembly:
	# Prologue.
	addi sp, sp, -16
	sw ra, 12(sp)
	sw s0, 8(sp)
	addi s0, sp, 16

	sw a0, 4(sp) # a0 - Line length.

	# Write the source to the standard output.
	mv a0, s1
	lw a1, 4(sp)
	call write_out

	lw t0, 4(sp)
	add s1, s1, t0

	li t0, '\n'
	sb t0, 0(sp)
	addi a0, sp, 0
	li a1, 1
	call write_out

	addi s1, s1, 1 # Skip the new line.

	# Epilogue.
	lw ra, 12(sp)
	lw s0, 8(sp)
	addi sp, sp, 16
	ret

.type _compile_program, @function
_compile_program:
	# Prologue.
	addi sp, sp, -32
	sw ra, 28(sp)
	sw s0, 24(sp)
	addi s0, sp, 32

	# .global _start
	li t0, 0x0a7472 # rt\n
	sw t0, 20(sp)
	li t0, 0x6174735f # _sta
	sw t0, 16(sp)
	li t0, 0x206c6162 # bal_
	sw t0, 12(sp)
	li t0, 0x6f6c672e # .glo
	sw t0, 8(sp)

	addi a0, sp, 8
	li a1, 16
	call write_out

	addi s1, s1, 8 # program\n.

	# Epilogue.
	lw ra, 28(sp)
	lw s0, 24(sp)
	addi sp, sp, 32
	ret

.type _compile_constant_section, @function
_compile_constant_section:
	# Prologue.
	addi sp, sp, -32
	sw ra, 28(sp)
	sw s0, 24(sp)
	addi s0, sp, 32

	# .section .rodata
	li t0, 0x0a # \n
	sw t0, 20(sp)
	li t0, 0x61746164 # data
	sw t0, 16(sp)
	li t0, 0x6f722e20 # _.ro
	sw t0, 12(sp)
	li t0, 0x6e6f6974 # tion
	sw t0, 8(sp)
	li t0, 0x6365732e # .sec
	sw t0, 4(sp)

	addi a0, sp, 4
	li a1, 17
	call write_out

	addi s1, s1, 6 # const\n.

.Lcompile_constant_section_item:
	call _skip_spaces
	lbu a0, (s1)
	call is_upper
	beqz a0, .Lcompile_constant_section_end

	call _compile_constant
	j .Lcompile_constant_section_item

.Lcompile_constant_section_end:
	# Epilogue.
	lw ra, 28(sp)
	lw s0, 24(sp)
	addi sp, sp, 32
	ret

.type _compile_constant, @function
_compile_constant:
	# Prologue.
	addi sp, sp, -16
	sw ra, 12(sp)
	sw s0, 8(sp)
	addi s0, sp, 16

	call _read_token

	mv a1, a0 # The identifier length from _read_token should be in a1.
	mv a0, s1 # Save the identifier pointer before advancing it.
	add s1, s1, a1
	call write_out

	call _skip_spaces
	call _read_token
	addi s1, s1, 2 # Skip the assignment sign.

	# : .long
	li t0, 0x20676e6f # ong_
	sw t0, 4(sp)
	li t0, 0x6c2e203a # : .l
	sw t0, 0(sp)
	mv a0, sp
	li a1, 8
	call write_out

	call _skip_spaces
	call _read_token

	mv a1, a0 # The literal length from _read_token should be in a1.
	mv a0, s1 # Save the literal pointer before advancing it.
	add s1, s1, a1
	call write_out

	li t0, '\n'
	sw t0, 4(sp)
	addi a0, sp, 4
	li a1, 1
	call write_out

	# Epilogue.
	lw ra, 12(sp)
	lw s0, 8(sp)
	addi sp, sp, 16
	ret

.type _compile_variable_section, @function
_compile_variable_section:
	# Prologue.
	addi sp, sp, -24
	sw ra, 20(sp)
	sw s0, 16(sp)
	addi s0, sp, 24

	# .section .bss
	li t0, 0x0a73 # s\n
	sw t0, 12(sp)
	li t0, 0x73622e20 # _.bs
	sw t0, 8(sp)
	li t0, 0x6e6f6974 # tion
	sw t0, 4(sp)
	li t0, 0x6365732e # .sec
	sw t0, 0(sp)

	addi a0, sp, 0
	li a1, 14
	call write_out

	addi s1, s1, 4 # var\n.

.Lcompile_variable_section_item:
	call _skip_spaces
	lbu a0, (s1)
	call is_lower
	beqz a0, .Lcompile_variable_section_end

	call _compile_variable
	j .Lcompile_variable_section_item

.Lcompile_variable_section_end:
	# Epilogue.
	lw ra, 20(sp)
	lw s0, 16(sp)
	addi sp, sp, 24
	ret

.type _compile_variable, @function
_compile_variable:
	# Prologue.
	addi sp, sp, -40
	sw ra, 36(sp)
	sw s0, 32(sp)
	addi s0, sp, 40

	call _read_token

	# Save the identifier on the stack since it should emitted multiple times.
	sw s1, 28(sp)
	sw a0, 24(sp)
	add s1, s1, a0

	call _skip_spaces
	addi s1, s1, 1 # Skip the colon in front of the type.

	call _skip_spaces
	addi s1, s1, 1 # Skip the opening bracket.

	call _read_token

	# Save the array size on the stack since it has to be emitted multiple times.
	sw s1, 20(sp)
	sw a0, 16(sp)
	add s1, s1, a0

	call _skip_spaces
	addi s1, s1, 1 # Skip the closing bracket.

	call _skip_spaces
	call _read_token
	add s1, s1, a0 # Skip the type.

	# .type identifier, @object
	li t0, 0x2065 # e_
	sw t0, 12(sp)
	li t0, 0x7079742e # .typ
	sw t0, 8(sp)
	addi a0, sp, 8
	li a1, 6
	call write_out

	lw a0, 28(sp)
	lw a1, 24(sp)
	call write_out

	li t0, 0x0a74 # t\n
	sw t0, 12(sp)
	li t0, 0x63656a62 # bjec
	sw t0, 8(sp)
	li t0, 0x6f40202c # , @o
	sw t0, 4(sp)
	addi a0, sp, 4
	li a1, 10
	call write_out

	# .size identifier, size
	li t0, 0x2065 # e_
	sw t0, 12(sp)
	li t0, 0x7a69732e # .siz
	sw t0, 8(sp)
	addi a0, sp, 8
	li a1, 6
	call write_out

	lw a0, 28(sp)
	lw a1, 24(sp)
	call write_out

	li t0, 0x202c # ,
	sw t0, 12(sp)
	addi a0, sp, 12
	li a1, 2
	call write_out

	lw a0, 20(sp)
	lw a1, 16(sp)
	call write_out

	li t0, 0x0a # \n
	sw t0, 12(sp)
	addi a0, sp, 12
	li a1, 1
	call write_out

	# identifier: .zero size
	lw a0, 28(sp)
	lw a1, 24(sp)
	call write_out

	li t0, 0x206f7265 # ero_
	sw t0, 12(sp)
	li t0, 0x7a2e203a # : .z
	sw t0, 8(sp)
	addi a0, sp, 8
	li a1, 8
	call write_out

	lw a0, 20(sp)
	lw a1, 16(sp)
	call write_out

	li t0, 0x0a # \n
	sw t0, 12(sp)
	addi a0, sp, 12
	li a1, 1
	call write_out

	# Epilogue.
	lw ra, 36(sp)
	lw s0, 32(sp)
	addi sp, sp, 40
	ret

.type _compile_procedure, @function
_compile_procedure:
	# Prologue.
	addi sp, sp, -32
	sw ra, 28(sp)
	sw s0, 24(sp)
	addi s0, sp, 32

	addi s1, s1, 5 # Skip proc_
	call _read_token
	sw s1, 20(sp)
	sw a0, 16(sp)
	add s1, s1, a0

	# .type identifier, @function
	li t0, 0x2065 # e_
	sw t0, 12(sp)
	li t0, 0x7079742e # .typ
	sw t0, 8(sp)
	addi a0, sp, 8
	li a1, 6
	call write_out

	lw a0, 20(sp)
	lw a1, 16(sp)
	call write_out

	li t0, 0x0a6e6f69 # ion\n
	sw t0, 12(sp)
	li t0, 0x74636e75 # unct
	sw t0, 8(sp)
	li t0, 0x6640202c # , @f
	sw t0, 4(sp)
	addi a0, sp, 4
	li a1, 12
	call write_out

	lw a0, 20(sp)
	lw a1, 16(sp)
	call write_out

	li t0, 0x0a3a # :\n
	sw t0, 12(sp)
	addi a0, sp, 12
	li a1, 2
	call write_out

	call _skip_spaces
	addi s1, s1, 1 # Skip opening argument paren.
	call _skip_spaces
	addi s1, s1, 1 # Skip closing argument paren.
	call _skip_spaces

	# Epilogue.
	lw ra, 28(sp)
	lw s0, 24(sp)
	addi sp, sp, 32
	ret

# Parameters:
# a0 - Line length.
.type _compile_line, @function
_compile_line:
	# Prologue.
	addi sp, sp, -32
	sw ra, 28(sp)
	sw s0, 24(sp)
	addi s0, sp, 32

	sw a0, 20(sp) # a0 - Line length.

	beqz a0, .Lcompile_line_empty # Skip an empty line.

	lbu t0, (s1)
	li t1, '#'
	beq t0, t1, .Lcompile_line_comment

	li t0, 0x0a6d6172 # ram\n
	sw t0, 16(sp)
	li t0, 0x676f7270 # prog
	sw t0, 12(sp)
	mv a0, s1
	addi a1, sp, 12
	li a2, 8
	call memcmp
	beqz a0, .Lcompile_line_program

	li t0, 0x0a74 # t\n
	sw t0, 16(sp)
	li t0, 0x736e6f63 # cons
	sw t0, 12(sp)
	mv a0, s1
	addi a1, sp, 12
	li a2, 6
	call memcmp
	beqz a0, .Lcompile_line_const

	li t0, 0x0a726176 # var\n
	sw t0, 16(sp)
	mv a0, s1
	addi a1, sp, 16
	li a2, 4
	call memcmp
	beqz a0, .Lcompile_line_var

	li t0, 0x20 # _
	sw t0, 16(sp)
	li t0, 0x636f7270 # proc
	sw t0, 12(sp)
	mv a0, s1
	addi a1, sp, 12
	li a2, 5
	call memcmp
	beqz a0, .Lcompile_line_procedure

	j .Lcompile_line_unchanged # Else.

.Lcompile_line_const:
	call _compile_constant_section
	j .Lcompile_line_end

.Lcompile_line_procedure:
	call _compile_procedure
	j .Lcompile_line_end

.Lcompile_line_var:

	/* DEBUG
	mv a0, s1
	li a1, 20
	call write_error */

	call _compile_variable_section
	j .Lcompile_line_end

.Lcompile_line_program:
	call _compile_program
	j .Lcompile_line_end

.Lcompile_line_comment:
	lw a0, 20(sp)
	call _skip_comment
	j .Lcompile_line_end

.Lcompile_line_empty:
	addi s1, s1, 1
	j .Lcompile_line_end

.Lcompile_line_unchanged:
	lw a0, 20(sp)
	call _compile_assembly
	j .Lcompile_line_end

.Lcompile_line_end:
	# Epilogue.
	lw ra, 28(sp)
	lw s0, 24(sp)
	addi sp, sp, 32
	ret

# Finds the end of the line and returns its length in a0.
.type _read_line, @function
_read_line:
	mv t0, s1 # Local position in the source text.

.Lread_line_do:
	lbu t1, (t0) # t1 = Current character.
	beqz t1, .Lread_line_end # Exit the loop on the NUL character.
	li t2, '\n'
	beq t1, t2, .Lread_line_end # Exit the loop on the new line.

	addi t0, t0, 1
	j .Lread_line_do

.Lread_line_end:
	sub a0, t0, s1 # Return the line length.
	ret

.type _compile, @function
_compile:
	# Prologue.
	addi sp, sp, -8
	sw ra, 4(sp)
	sw s0, 0(sp)
	addi s0, sp, 8

.Lcompile_do:
	lbu t0, (s1) # t0 = Current character.
	beqz t0, .Lcompile_end # Exit the loop on the NUL character.

	call _skip_indentation
	call _read_line
	call _compile_line

	j .Lcompile_do
.Lcompile_end:

	# Epilogue.
	lw ra, 4(sp)
	lw s0, 0(sp)
	addi sp, sp, 8
	ret

# Entry point.
.type _start, @function
_start:
	# Read the source from the standard input.
	la a0, source_code
	la a1, SOURCE_BUFFER_SIZE # Buffer size.
	lw a1, (a1)
	call read_file

	la s1, source_code # s1 = Source code position.
	call _compile

	# Call exit.
	li a0, 0   # Use 0 return code.
	call exit