From 1b31f532dfb9987788565c90db0558db6f8bed30 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Mon, 12 May 2025 23:07:02 +0200 Subject: [PATCH] Unify taking an identifier address --- boot/common-boot.s | 36 +++++++- boot/stage1.s | 221 ++++++++++++--------------------------------- boot/stage2.elna | 23 ++--- 3 files changed, 100 insertions(+), 180 deletions(-) diff --git a/boot/common-boot.s b/boot/common-boot.s index 0c2ef6b..f61321e 100644 --- a/boot/common-boot.s +++ b/boot/common-boot.s @@ -4,9 +4,9 @@ .global _is_alpha, _is_digit, _is_alnum, _is_upper, _is_lower .global _write_s, _read_file, _write_error, _write_c, _write_i, _print_i -.global _get, _memcmp, _memchr, _memmem, _memcpy -.global _divide_by_zero_error, _exit, _mmap -.global _strings_index, _string_equal +.global _memcmp, _memchr, _memmem, _memcpy, _mmap +.global _current, _get, _advance, _label_counter +.global _divide_by_zero_error, _exit, _strings_index, _string_equal .section .rodata @@ -598,3 +598,33 @@ _mmap: ecall ret + +# Sets the a0 to the current position in the source text (s1). +.type _current, @function +_current: + mv a0, s1 + ret + +# Advances the position of the source text. +# +# Parameters: +# a0 - The number of bytes to advance. +.type _advance, @function +_advance: + add s1, s1, a0 + ret + +# Advances the global label counter by 1 setting a0 to the previous value. +# +# Parameters: +# a0 - If it is 0, resets the counter to 1. +.type _label_counter, @function +_label_counter: + bnez a0, .Llabel_counter_advance + li s2, 0 + +.Llabel_counter_advance: + mv a0, s2 + addi s2, s2, 1 + + ret diff --git a/boot/stage1.s b/boot/stage1.s index 8258075..15e3d20 100644 --- a/boot/stage1.s +++ b/boot/stage1.s @@ -249,31 +249,47 @@ is_local_identifier: addi sp, sp, 16 ret -# Checks whether the given identifier is a saved register name, like s1 or s2. +# Looks for a register that can be used to calculate a symbol address. Writes it +# as string, like sp or s0 into the provided buffer. +# # Parameters: -# a0 - Pointer to the identifier. -# a1 - Identifier length. +# a0 - Symbol info pointer. +# a1 - Output buffer. # -# Sets a0 to 1 if the identifier is a preserved register, otherwise to 0. -.type is_register_identifier, @function -is_register_identifier: - # Prologue. - addi sp, sp, -8 - sw ra, 4(sp) - sw s0, 0(sp) - addi s0, sp, 8 +# Sets a0 to the length of register name written or 0. +.type take_address, @function +take_address: + beqz a0, .Ltake_address_undefined + lw t0, 0(a0) - lbu a0, (a0) - addi a1, a1, -2 - seqz a1, a1 - addi t0, a0, -'s' - seqz t0, t0 - and a0, a1, t0 + li t1, INFO_PARAMETER + beq t0, t1, .Ltake_address_parameter - # Epilogue. - lw ra, 4(sp) - lw s0, 0(sp) - addi sp, sp, 8 + li t1, INFO_LOCAL + beq t0, t1, .Ltake_address_local + + j .Ltake_address_undefined + +.Ltake_address_parameter: + li t0, 0x3073 # s0 + sh t0, (a1) + + li a0, 2 + + j .Ltake_address_end + +.Ltake_address_local: + li t0, 0x7073 # (sp) + sh t0, (a1) + + li a0, 2 + + j .Ltake_address_end + +.Ltake_address_undefined: + li a0, 0 + +.Ltake_address_end: ret # Parameters: @@ -298,18 +314,9 @@ compile_identifier_expression: beqz a0, .Lcompile_identifier_expression_by_name lw t0, 0(a0) - li t1, INFO_PARAMETER - beq t0, t1, .Lcompile_identifier_expression_parameter - - li t1, INFO_LOCAL - beq t0, t1, .Lcompile_identifier_expression_local + j .Lcompile_identifier_expression_by_address .Lcompile_identifier_expression_by_name: - mv a0, s1 - lw a1, 20(sp) - call is_register_identifier - bnez a0, .Lcompile_identifier_expression_saved - # Global identifier. lw t1, 16(sp) li t0, 0x00202c00 # \0,_ @@ -348,7 +355,7 @@ compile_identifier_expression: j .Lcompile_identifier_expression_end -.Lcompile_identifier_expression_parameter: +.Lcompile_identifier_expression_by_address: lw t1, 16(sp) li t0, 0x00202c00 # \0,_ or t0, t0, t1 @@ -363,56 +370,17 @@ compile_identifier_expression: lw a0, 8(a0) call _write_i - li t0, 0x29307328 # (s0) - sw t0, 8(sp) - li a0, 4 - addi a1, sp, 8 - call _write_s - li a0, '\n' + li a0, '(' call _write_c - j .Lcompile_identifier_expression_end - -.Lcompile_identifier_expression_local: - lw t1, 16(sp) - li t0, 0x00202c00 # \0,_ - or t0, t0, t1 - sw t0, 8(sp) - li t0, 0x6120776c # lw a - sw t0, 4(sp) - li a0, 7 - addi a1, sp, 4 - call _write_s - lw a0, 12(sp) - lw a0, 8(a0) - call _write_i - - li t0, 0x29707328 # (sp) - sw t0, 8(sp) - li a0, 4 - addi a1, sp, 8 - call _write_s - li a0, '\n' - call _write_c - - j .Lcompile_identifier_expression_end - -.Lcompile_identifier_expression_saved: - li t0, 0x00202c00 # \0,_ - lw t1, 16(sp) - or t0, t0, t1 - sw t0, 8(sp) - li t0, 0x6120766d # mv a - sw t0, 4(sp) - li a0, 7 + addi a1, sp, 4 + call take_address addi a1, sp, 4 call _write_s - lw a0, 20(sp) - mv a1, s1 - call _write_s - + li a0, ')' + call _write_c li a0, '\n' call _write_c @@ -552,81 +520,36 @@ compile_at_expression: lw a1, 32(sp) call symbol_table_find sw a0, 20(sp) - lw t0, 0(a0) - li t1, INFO_PARAMETER - beq t0, t1, .Lcompile_at_expression_parameter - - li t1, INFO_LOCAL - beq t0, t1, .Lcompile_at_expression_local - - unimp - -.Lcompile_at_expression_local: - lw a0, 36(sp) li t0, 0x20 # _ - sw t0, 16(sp) - li t0, 0x2c707320 # _sp, - sw t0, 12(sp) + sb t0, 12(sp) + + # lw a0, 28(sp) + lw a1, 32(sp) + addi a1, sp, 13 + call take_address + + lw t1, 36(sp) li t0, 0x2c006120 # _a\0, sw t0, 8(sp) - sb a0, 10(sp) + sb t1, 10(sp) li t0, 0x69646461 # addi sw t0, 4(sp) - li a0, 13 + addi a0, a0, 9 # The length returned by take_address + the instruction. addi a1, sp, 4 call _write_s + li a0, ',' + call _write_c + li a0, ' ' + call _write_c + lw a0, 20(sp) lw a0, 8(a0) call _write_i j .Lcompile_at_expression_end -.Lcompile_at_expression_parameter: - lw a0, 36(sp) - li t0, 0x20 # _ - sw t0, 16(sp) - li t0, 0x2c307320 # _s0, - sw t0, 12(sp) - li t0, 0x2c006120 # _a\0, - sw t0, 8(sp) - sb a0, 10(sp) - li t0, 0x69646461 # addi - sw t0, 4(sp) - li a0, 13 - addi a1, sp, 4 - call _write_s - - lw a0, 20(sp) - lw a0, 8(a0) - call _write_i - - j .Lcompile_at_expression_end - -.Lcompile_at_expression_undefined: - lw a0, 36(sp) - li t0, 0x20 # _ - sw t0, 16(sp) - li t0, 0x2c707320 # _sp, - sw t0, 12(sp) - li t0, 0x2c006120 # _a\0, - sw t0, 8(sp) - sb a0, 10(sp) - li t0, 0x69646461 # addi - sw t0, 4(sp) - li a0, 13 - addi a1, sp, 4 - call _write_s - - lw a0, 32(sp) - lw a1, 28(sp) - addi a0, a1, -4 # Skip the "loca" variable prefix. - addi a1, a0, 4 # Skip the "loca" variable prefix. - call _write_s - - j .Lcompile_at_expression_end - .Lcompile_at_expression_end: li a0, '\n' call _write_c @@ -658,11 +581,6 @@ compile_designator_expression: call is_local_identifier bnez a0, .Lcompile_designator_expression_local - lw a0, 20(sp) - lw a1, 16(sp) - call is_register_identifier - bnez a0, .Lcompile_designator_expression_saved - .Lcompile_designator_expression_local: li t0, 0x202c30 # 0,_ sw t0, 12(sp) @@ -688,27 +606,6 @@ compile_designator_expression: j .Lcompile_designator_expression_end -.Lcompile_designator_expression_saved: - li t0, 0x20766d # mv_ - sw t0, 12(sp) - li a0, 3 - addi a1, sp, 12 - call _write_s - - lw a0, 16(sp) - lw a1, 20(sp) - call _write_s - - li t0, 0x0a # \n - sw t0, 12(sp) - li t0, 0x3061202c # , a0 - sw t0, 8(sp) - li a0, 5 - addi a1, sp, 8 - call _write_s - - j .Lcompile_designator_expression_end - .Lcompile_designator_expression_end: # Epilogue. lw ra, 28(sp) diff --git a/boot/stage2.elna b/boot/stage2.elna index 59bb42a..e38874c 100644 --- a/boot/stage2.elna +++ b/boot/stage2.elna @@ -1081,6 +1081,7 @@ proc _compile_label(loca84: Word) var loca0: Word loca4: Word + loca8: ^Byte begin loca0 := _current(); @@ -1092,7 +1093,8 @@ begin if loca0 = 0x3b then loca4 := loca4 - 1 end; - _write_s(loca4, s1); + loca8 := _current(); + _write_s(loca4, loca8); _write_c(0x3a); _write_c(0x0a); @@ -1115,6 +1117,7 @@ var loca12: Word loca16: Word loca20: Word + loca24: Word begin _advance(2); _skip_spaces(); @@ -1133,8 +1136,9 @@ begin _write_s(4, @loca12); _write_c(0x20); + loca24 := _label_counter(1); _write_s(4, @loca16); - _write_i(s2); + _write_i(loca24); _write_c(0x0a); @@ -1153,12 +1157,11 @@ begin end; _write_s(4, @loca16); - _write_i(s2); + _write_i(loca24); loca12 := 0x0a3a0a3a; _write_s(2, @loca12); - s2 := s2 + 1; _advance(4) end @@ -1445,16 +1448,6 @@ begin .Lcompile_end end -proc _current() -begin - return s1 -end - -proc _advance(loca84: Word) -begin - s1 := s1 + loca84 -end - proc _front(loca84: ^Word) begin return _get(loca84) & 0xff @@ -1464,7 +1457,7 @@ proc _main() begin _read_file(source_code, SOURCE_BUFFER_SIZE); - s2 := 1 + _label_counter(0) end begin