Add language support and massively update Forth compiler

This commit is contained in:
Dominic Grimm 2023-04-01 19:20:32 +02:00
parent 635ae21540
commit df73353bc6
No known key found for this signature in database
GPG key ID: 12EFFCAEA9E620BF
31 changed files with 2217 additions and 331 deletions

View file

@ -4,24 +4,42 @@
.include "$lib/core.asm"
.include "$lib/std.asm"
.include "$lib/main.asm"
;.include "$lib/main.asm"
.define MEM_LOOP_I, CORE_MEM_MEM
.define MEM_LOOP_J, (MEM_LOOP_I + 1)
.define_eval prev, OFFSET
ts CORE_U16_MAX
tlr CORE_REG_PC
.define_eval diff, (OFFSET - prev)
.define_eval MAIN_JUMPER, (CORE_MEM_PRG_END - diff)
.org prev
.delete prev, diff
.define MEM_CALL_STACK_LEN, 16
.define MEM_CALL_STACK_PTR, (MEM_LOOP_J + 1)
.define MEM_CALL_STACK_END, (MEM_CALL_STACK_PTR + MEM_CALL_STACK_LEN)
.define_eval MAIN_JUMPER, (CORE_MEM_PRG_END - 3 - 1)
.define MEM_ALLOC_PTR, MEM_CALL_STACK_END
.define_eval MEM_LOOP_DEPTH, CORE_MEM_MEM
.define_eval MEM_LOOP_I, (MEM_LOOP_DEPTH + 1)
.define_eval MEM_LOOP_J, (MEM_LOOP_I + 1)
.define_eval MEM_CALL_STACK_LEN, 16
.define_eval MEM_CALL_STACK_PTR, (MEM_LOOP_J + 1)
.define_eval MEM_CALL_STACK_END, (MEM_CALL_STACK_PTR + MEM_CALL_STACK_LEN)
.define_eval MEM_ALLOC_PTR, (MEM_CALL_STACK_END + 1)
.macro stack_transfer_alu
.std_ld
tlr CORE_REG_B
.std_ld
tlr CORE_REG_A
.endmacro
.std_rset CORE_REG_A, MEM_CALL_STACK_PTR
.std_set MEM_CALL_STACK_PTR
.std_rset CORE_REG_A, (MEM_ALLOC_PTR + 1)
.std_rset CORE_REG_A, MEM_ALLOC_PTR
.std_set MEM_ALLOC_PTR
.jump_main
.std_jump MAIN_JUMPER
call_stack_jump:
.std_get MEM_CALL_STACK_PTR
@ -52,13 +70,6 @@ return_call_stack_jump:
get
tlr CORE_REG_PC
.macro stack_transfer_alu
.std_ld
tlr CORE_REG_B
.std_ld
tlr CORE_REG_A
.endmacro
.macro call_stack_jump, call_stack_jump_arg_0_label, call_stack_jump_arg_1_offset
.std_rset CORE_REG_C, call_stack_jump_arg_0_label
.std_rset CORE_REG_D, (call_stack_jump_arg_1_offset + 7)
@ -66,6 +77,48 @@ return_call_stack_jump:
tlr CORE_REG_PC
.endmacro
.macro call_stack_jump_cond_if, call_stack_jump_cond_if_arg_0_if_label, call_stack_jump_cond_if_arg_1_offset
.std_ld
tlr CORE_REG_A
.std_rset CORE_REG_C, call_stack_jump_cond_if_arg_0_if_label
.std_rset CORE_REG_D, (call_stack_jump_cond_if_arg_1_offset + 7)
ts call_stack_jump
tlrc CORE_REG_PC
.endmacro
.define_eval prev, OFFSET
.std_ld
tlr CORE_REG_A
.std_rset CORE_REG_C, CORE_U16_MAX
.std_rset CORE_REG_D, CORE_U16_MAX
ts call_stack_jump
tlrc CORE_REG_PC
.std_rset CORE_REG_C, CORE_U16_MAX
.std_rset CORE_REG_D, CORE_U16_MAX
ts call_stack_jump
tlr CORE_REG_PC
.define_eval CALL_STACK_JUMP_COND_IF_ELSE_SIZE, (OFFSET - prev)
.delete prev
.macro call_stack_jump_cond_if_else, call_stack_jump_cond_if_else_arg_0_if_label, call_stack_jump_cond_if_else_arg_1_else_label, call_stack_jump_cond_if_else_arg_2_offset
.define_eval prev, OFFSET
.std_ld
tlr CORE_REG_A
.std_rset CORE_REG_C, call_stack_jump_cond_if_else_arg_0_if_label
.std_rset CORE_REG_D, (call_stack_jump_cond_if_else_arg_2_offset)
ts call_stack_jump
tlrc CORE_REG_PC
.std_rset CORE_REG_C, call_stack_jump_cond_if_else_arg_1_else_label
.std_rset CORE_REG_D, (call_stack_jump_cond_if_else_arg_2_offset + 11)
ts call_stack_jump
tlr CORE_REG_PC
.delete prev
.endmacro
.macro return_call_stack_jump
.std_jump return_call_stack_jump
.endmacro
@ -89,13 +142,15 @@ return_call_stack_jump:
; condition: {{ c.id }}
conditions_if_{{ c.id }}:
{%- for ins in c.data.0 %}
{{ ins.to_code() }}
{{ ins }}
{%- endfor %}
.return_call_stack_jump
conditions_else_{{ c.id }}:
{%- for ins in c.data.0 %}
{{ ins.to_code() }}
{{ ins }}
{%- endfor %}
.return_call_stack_jump
{% endfor %}
; words
@ -104,15 +159,19 @@ return_call_stack_jump:
{% let name = snailquote::escape(w.data.0) -%}
; name : {% call escape_name(name) %}
{%- for node in w.data.1 %}
{{ node.to_code() }}
{{ node }}
{%- endfor %}
.return_call_stack_jump
.return_call_stack_jump
{% endfor %}
; main
.def_main
ts NULL ; reset tmp
{% for node in main %}
{{ node.to_code() }}
main:
.org MAIN_JUMPER
ts main
tlr CORE_REG_PC
.org main
{%- for node in main %}
{{ node }}
{%- endfor %}
.std_stop