Unify taking an identifier address
This commit is contained in:
parent
d85183c7a6
commit
1b31f532df
@ -4,9 +4,9 @@
|
|||||||
|
|
||||||
.global _is_alpha, _is_digit, _is_alnum, _is_upper, _is_lower
|
.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 _write_s, _read_file, _write_error, _write_c, _write_i, _print_i
|
||||||
.global _get, _memcmp, _memchr, _memmem, _memcpy
|
.global _memcmp, _memchr, _memmem, _memcpy, _mmap
|
||||||
.global _divide_by_zero_error, _exit, _mmap
|
.global _current, _get, _advance, _label_counter
|
||||||
.global _strings_index, _string_equal
|
.global _divide_by_zero_error, _exit, _strings_index, _string_equal
|
||||||
|
|
||||||
.section .rodata
|
.section .rodata
|
||||||
|
|
||||||
@ -598,3 +598,33 @@ _mmap:
|
|||||||
ecall
|
ecall
|
||||||
|
|
||||||
ret
|
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
|
||||||
|
221
boot/stage1.s
221
boot/stage1.s
@ -249,31 +249,47 @@ is_local_identifier:
|
|||||||
addi sp, sp, 16
|
addi sp, sp, 16
|
||||||
ret
|
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:
|
# Parameters:
|
||||||
# a0 - Pointer to the identifier.
|
# a0 - Symbol info pointer.
|
||||||
# a1 - Identifier length.
|
# a1 - Output buffer.
|
||||||
#
|
#
|
||||||
# Sets a0 to 1 if the identifier is a preserved register, otherwise to 0.
|
# Sets a0 to the length of register name written or 0.
|
||||||
.type is_register_identifier, @function
|
.type take_address, @function
|
||||||
is_register_identifier:
|
take_address:
|
||||||
# Prologue.
|
beqz a0, .Ltake_address_undefined
|
||||||
addi sp, sp, -8
|
lw t0, 0(a0)
|
||||||
sw ra, 4(sp)
|
|
||||||
sw s0, 0(sp)
|
|
||||||
addi s0, sp, 8
|
|
||||||
|
|
||||||
lbu a0, (a0)
|
li t1, INFO_PARAMETER
|
||||||
addi a1, a1, -2
|
beq t0, t1, .Ltake_address_parameter
|
||||||
seqz a1, a1
|
|
||||||
addi t0, a0, -'s'
|
|
||||||
seqz t0, t0
|
|
||||||
and a0, a1, t0
|
|
||||||
|
|
||||||
# Epilogue.
|
li t1, INFO_LOCAL
|
||||||
lw ra, 4(sp)
|
beq t0, t1, .Ltake_address_local
|
||||||
lw s0, 0(sp)
|
|
||||||
addi sp, sp, 8
|
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
|
ret
|
||||||
|
|
||||||
# Parameters:
|
# Parameters:
|
||||||
@ -298,18 +314,9 @@ compile_identifier_expression:
|
|||||||
beqz a0, .Lcompile_identifier_expression_by_name
|
beqz a0, .Lcompile_identifier_expression_by_name
|
||||||
lw t0, 0(a0)
|
lw t0, 0(a0)
|
||||||
|
|
||||||
li t1, INFO_PARAMETER
|
j .Lcompile_identifier_expression_by_address
|
||||||
beq t0, t1, .Lcompile_identifier_expression_parameter
|
|
||||||
|
|
||||||
li t1, INFO_LOCAL
|
|
||||||
beq t0, t1, .Lcompile_identifier_expression_local
|
|
||||||
|
|
||||||
.Lcompile_identifier_expression_by_name:
|
.Lcompile_identifier_expression_by_name:
|
||||||
mv a0, s1
|
|
||||||
lw a1, 20(sp)
|
|
||||||
call is_register_identifier
|
|
||||||
bnez a0, .Lcompile_identifier_expression_saved
|
|
||||||
|
|
||||||
# Global identifier.
|
# Global identifier.
|
||||||
lw t1, 16(sp)
|
lw t1, 16(sp)
|
||||||
li t0, 0x00202c00 # \0,_
|
li t0, 0x00202c00 # \0,_
|
||||||
@ -348,7 +355,7 @@ compile_identifier_expression:
|
|||||||
|
|
||||||
j .Lcompile_identifier_expression_end
|
j .Lcompile_identifier_expression_end
|
||||||
|
|
||||||
.Lcompile_identifier_expression_parameter:
|
.Lcompile_identifier_expression_by_address:
|
||||||
lw t1, 16(sp)
|
lw t1, 16(sp)
|
||||||
li t0, 0x00202c00 # \0,_
|
li t0, 0x00202c00 # \0,_
|
||||||
or t0, t0, t1
|
or t0, t0, t1
|
||||||
@ -363,56 +370,17 @@ compile_identifier_expression:
|
|||||||
lw a0, 8(a0)
|
lw a0, 8(a0)
|
||||||
call _write_i
|
call _write_i
|
||||||
|
|
||||||
li t0, 0x29307328 # (s0)
|
li a0, '('
|
||||||
sw t0, 8(sp)
|
|
||||||
li a0, 4
|
|
||||||
addi a1, sp, 8
|
|
||||||
call _write_s
|
|
||||||
li a0, '\n'
|
|
||||||
call _write_c
|
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, 12(sp)
|
||||||
lw a0, 8(a0)
|
addi a1, sp, 4
|
||||||
call _write_i
|
call take_address
|
||||||
|
|
||||||
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
|
addi a1, sp, 4
|
||||||
call _write_s
|
call _write_s
|
||||||
|
|
||||||
lw a0, 20(sp)
|
li a0, ')'
|
||||||
mv a1, s1
|
call _write_c
|
||||||
call _write_s
|
|
||||||
|
|
||||||
li a0, '\n'
|
li a0, '\n'
|
||||||
call _write_c
|
call _write_c
|
||||||
|
|
||||||
@ -552,81 +520,36 @@ compile_at_expression:
|
|||||||
lw a1, 32(sp)
|
lw a1, 32(sp)
|
||||||
call symbol_table_find
|
call symbol_table_find
|
||||||
sw a0, 20(sp)
|
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 # _
|
li t0, 0x20 # _
|
||||||
sw t0, 16(sp)
|
sb t0, 12(sp)
|
||||||
li t0, 0x2c707320 # _sp,
|
|
||||||
sw 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,
|
li t0, 0x2c006120 # _a\0,
|
||||||
sw t0, 8(sp)
|
sw t0, 8(sp)
|
||||||
sb a0, 10(sp)
|
sb t1, 10(sp)
|
||||||
li t0, 0x69646461 # addi
|
li t0, 0x69646461 # addi
|
||||||
sw t0, 4(sp)
|
sw t0, 4(sp)
|
||||||
li a0, 13
|
addi a0, a0, 9 # The length returned by take_address + the instruction.
|
||||||
addi a1, sp, 4
|
addi a1, sp, 4
|
||||||
call _write_s
|
call _write_s
|
||||||
|
|
||||||
|
li a0, ','
|
||||||
|
call _write_c
|
||||||
|
li a0, ' '
|
||||||
|
call _write_c
|
||||||
|
|
||||||
lw a0, 20(sp)
|
lw a0, 20(sp)
|
||||||
lw a0, 8(a0)
|
lw a0, 8(a0)
|
||||||
call _write_i
|
call _write_i
|
||||||
|
|
||||||
j .Lcompile_at_expression_end
|
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:
|
.Lcompile_at_expression_end:
|
||||||
li a0, '\n'
|
li a0, '\n'
|
||||||
call _write_c
|
call _write_c
|
||||||
@ -658,11 +581,6 @@ compile_designator_expression:
|
|||||||
call is_local_identifier
|
call is_local_identifier
|
||||||
bnez a0, .Lcompile_designator_expression_local
|
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:
|
.Lcompile_designator_expression_local:
|
||||||
li t0, 0x202c30 # 0,_
|
li t0, 0x202c30 # 0,_
|
||||||
sw t0, 12(sp)
|
sw t0, 12(sp)
|
||||||
@ -688,27 +606,6 @@ compile_designator_expression:
|
|||||||
|
|
||||||
j .Lcompile_designator_expression_end
|
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:
|
.Lcompile_designator_expression_end:
|
||||||
# Epilogue.
|
# Epilogue.
|
||||||
lw ra, 28(sp)
|
lw ra, 28(sp)
|
||||||
|
@ -1081,6 +1081,7 @@ proc _compile_label(loca84: Word)
|
|||||||
var
|
var
|
||||||
loca0: Word
|
loca0: Word
|
||||||
loca4: Word
|
loca4: Word
|
||||||
|
loca8: ^Byte
|
||||||
begin
|
begin
|
||||||
loca0 := _current();
|
loca0 := _current();
|
||||||
|
|
||||||
@ -1092,7 +1093,8 @@ begin
|
|||||||
if loca0 = 0x3b then
|
if loca0 = 0x3b then
|
||||||
loca4 := loca4 - 1
|
loca4 := loca4 - 1
|
||||||
end;
|
end;
|
||||||
_write_s(loca4, s1);
|
loca8 := _current();
|
||||||
|
_write_s(loca4, loca8);
|
||||||
|
|
||||||
_write_c(0x3a);
|
_write_c(0x3a);
|
||||||
_write_c(0x0a);
|
_write_c(0x0a);
|
||||||
@ -1115,6 +1117,7 @@ var
|
|||||||
loca12: Word
|
loca12: Word
|
||||||
loca16: Word
|
loca16: Word
|
||||||
loca20: Word
|
loca20: Word
|
||||||
|
loca24: Word
|
||||||
begin
|
begin
|
||||||
_advance(2);
|
_advance(2);
|
||||||
_skip_spaces();
|
_skip_spaces();
|
||||||
@ -1133,8 +1136,9 @@ begin
|
|||||||
_write_s(4, @loca12);
|
_write_s(4, @loca12);
|
||||||
_write_c(0x20);
|
_write_c(0x20);
|
||||||
|
|
||||||
|
loca24 := _label_counter(1);
|
||||||
_write_s(4, @loca16);
|
_write_s(4, @loca16);
|
||||||
_write_i(s2);
|
_write_i(loca24);
|
||||||
|
|
||||||
_write_c(0x0a);
|
_write_c(0x0a);
|
||||||
|
|
||||||
@ -1153,12 +1157,11 @@ begin
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
_write_s(4, @loca16);
|
_write_s(4, @loca16);
|
||||||
_write_i(s2);
|
_write_i(loca24);
|
||||||
|
|
||||||
loca12 := 0x0a3a0a3a;
|
loca12 := 0x0a3a0a3a;
|
||||||
_write_s(2, @loca12);
|
_write_s(2, @loca12);
|
||||||
|
|
||||||
s2 := s2 + 1;
|
|
||||||
_advance(4)
|
_advance(4)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -1445,16 +1448,6 @@ begin
|
|||||||
.Lcompile_end
|
.Lcompile_end
|
||||||
end
|
end
|
||||||
|
|
||||||
proc _current()
|
|
||||||
begin
|
|
||||||
return s1
|
|
||||||
end
|
|
||||||
|
|
||||||
proc _advance(loca84: Word)
|
|
||||||
begin
|
|
||||||
s1 := s1 + loca84
|
|
||||||
end
|
|
||||||
|
|
||||||
proc _front(loca84: ^Word)
|
proc _front(loca84: ^Word)
|
||||||
begin
|
begin
|
||||||
return _get(loca84) & 0xff
|
return _get(loca84) & 0xff
|
||||||
@ -1464,7 +1457,7 @@ proc _main()
|
|||||||
begin
|
begin
|
||||||
_read_file(source_code, SOURCE_BUFFER_SIZE);
|
_read_file(source_code, SOURCE_BUFFER_SIZE);
|
||||||
|
|
||||||
s2 := 1
|
_label_counter(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
Loading…
x
Reference in New Issue
Block a user