hence/henceforth/templates/default.asm
2023-04-04 20:22:36 +02:00

283 lines
6.7 KiB
NASM

; Generated by Henceforth {{ env!("CARGO_PKG_VERSION") }} ({{ env!("CARGO_PKG_REPOSITORY") }})
.include "$lib/core.asm"
.include "$lib/std.asm"
.define PLACEHOLDER_U8, CORE_U8_MAX
.define PLACEHOLDER_U16, CORE_U16_MAX
.define_eval prev, OFFSET
ts PLACEHOLDER_U16
tlr CORE_REG_PC
.define_eval diff, (OFFSET - prev)
.define_eval MAIN_JUMPER, (CORE_MEM_PRG_END - diff)
.org prev
.delete prev, diff
.define_eval prev, OFFSET
{%- for c in conditions %}
.bytes PLACEHOLDER_U16{% if !c.data.1.is_empty() %}, PLACEHOLDER_U16{% endif %}
{%- endfor %}
.define_eval JUMP_TABLE_CONDITIONS_SIZE, (OFFSET - prev)
.org prev
.delete prev
.define_eval JUMP_TABLE_WORDS_SIZE, ({{ words.len() }} * 2)
.define_eval JUMP_TABLE_PTR, (MAIN_JUMPER - JUMP_TABLE_CONDITIONS_SIZE - JUMP_TABLE_WORDS_SIZE)
.debug JUMP_TABLE_CONDITIONS_SIZE, JUMP_TABLE_WORDS_SIZE, (JUMP_TABLE_CONDITIONS_SIZE + JUMP_TABLE_WORDS_SIZE / 2), JUMP_TABLE_PTR
.define_eval counter, 0
; jump table
; conditions
{%- for c in conditions %}
.define_eval jump_table_conditions_if_{{ c.id }}, (JUMP_TABLE_PTR + counter)
.define_eval counter, (counter + 2)
{%- if !c.data.1.is_empty() %}
.define_eval jump_table_conditions_else_{{ c.id }}, (JUMP_TABLE_PTR + counter)
.define_eval counter, (counter + 2)
{%- endif %}
{%- endfor %}
; words
{%- for w in words %}
.define_eval jump_table_words_{{ w.id }}, (JUMP_TABLE_PTR + counter)
.define_eval counter, (counter + 1)
{%- endfor %}
.delete counter
.define_eval MEM_JUMP_TABLE_LEN, (JUMP_TABLE_CONDITIONS_SIZE + JUMP_TABLE_WORDS_SIZE / 2)
.define_eval MEM_JUMP_TABLE_PTR, CORE_MEM_MEM
.define_eval MEM_JUMP_TABLE_END, (MEM_JUMP_TABLE_PTR + MEM_JUMP_TABLE_LEN)
.define_eval MEM_LOOP_DEPTH, (MEM_JUMP_TABLE_END + 1)
.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
.macro jump_table_pull_ptr, jump_table_pull_ptr_arg_0_ptr, jump_table_pull_ptr_arg_1_counter
ts jump_table_pull_ptr_arg_0_ptr
tls
ts 8
tlr CORE_REG_B
.std_get jump_table_pull_ptr_arg_0_ptr
tlr CORE_REG_A
ts CORE_ALU_LSH
alu
tlr CORE_REG_A
.std_get (jump_table_pull_ptr_arg_0_ptr + 1)
tlr CORE_REG_B
ts CORE_ALU_OR
alu
tlr CORE_REG_A
tls
dbg
pop
pop
.endmacro
.define_eval counter, 0
; setup jump table
; conditions
{%- for c in conditions %}
.jump_table_pull_ptr jump_table_conditions_if_{{ c.id }}, counter
.define_eval counter, (counter + 1)
{%- if !c.data.1.is_empty() %}
.jump_table_pull_ptr jump_table_conditions_else_{{ c.id }}, counter
.define_eval counter, (counter + 1)
{%- endif %}
{%- endfor %}
; words
{%- for w in words %}
.jump_table_pull_ptr jump_table_words_{{ w.id }}, counter
.define_eval counter, (counter + 1)
{%- endfor %}
.delete counter
.delete_macro jump_table_pull_ptr
.macro self_ref, self_ref_arg_0_ptr
.std_rset CORE_REG_A, self_ref_arg_0_ptr
.std_set self_ref_arg_0_ptr
.endmacro
; reference MEM_CALL_STACK_PTR to itself
.self_ref MEM_CALL_STACK_PTR
; reference MEM_ALLOC_PTR to itself
.self_ref MEM_ALLOC_PTR
.delete_macro self_ref
.std_jump MAIN_JUMPER
call_stack_jump:
.std_get MEM_CALL_STACK_PTR
tlr CORE_REG_A
.std_rset CORE_REG_B, 1
.std_alu CORE_ALU_ADD
tlr CORE_REG_A
tlr CORE_REG_B
.std_set MEM_CALL_STACK_PTR
tsr CORE_REG_D
tlr CORE_REG_A
tsr CORE_REG_B
set
tsr CORE_REG_C
tlr CORE_REG_PC
return_call_stack_jump:
.std_get MEM_CALL_STACK_PTR
tlr CORE_REG_A
tlr CORE_REG_C
.std_rset CORE_REG_B, 1
.std_alu CORE_ALU_SUB
tlr CORE_REG_A
.std_set MEM_CALL_STACK_PTR
tsr CORE_REG_C
get
tlr CORE_REG_PC
.macro call_stack_jump, call_stack_jump_arg_0_label
.std_rset CORE_REG_C, call_stack_jump_arg_0_label
.std_rset CORE_REG_D, (OFFSET + 7)
ts call_stack_jump
tlr CORE_REG_PC
.endmacro
.macro call_stack_jump_cond_if, call_stack_jump_cond_if_arg_0_if_label
.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, (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, PLACEHOLDER_U16
.std_rset CORE_REG_D, PLACEHOLDER_U16
ts call_stack_jump
tlrc CORE_REG_PC
.std_rset CORE_REG_C, PLACEHOLDER_U16
.std_rset CORE_REG_D, PLACEHOLDER_U16
ts call_stack_jump
tlr CORE_REG_PC
.define_eval CALL_STACK_JUMP_COND_IF_ELSE_SIZE, (OFFSET - prev)
.org 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
.define_eval prev, OFFSET
.define offset_till_end, (OFFSET - prev * -1 + CALL_STACK_JUMP_COND_IF_ELSE_SIZE)
.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, (OFFSET + offset_till_end)
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, (OFFSET + offset_till_end)
ts call_stack_jump
tlr CORE_REG_PC
.delete prev, offset_till_end
.endmacro
.macro return_call_stack_jump
.std_jump return_call_stack_jump
.endmacro
; data
; strings
{%- for s in strings %}
{% let bytes = s.data.as_bytes() -%}
data_strings_{{ s.id }}:
{%- for byte in bytes %}
.bytes {{ "0x{:02x}"|format(byte) }}
{%- endfor %}
data_strings_end_{{ s.id }}:
{% endfor %}
; conditions
{%- for c in conditions %}
; condition: {{ c.id }}
conditions_if_{{ c.id }}:
{%- for ins in c.data.0 %}
{{ ins }}
{%- endfor %}
.return_call_stack_jump
{%- if !c.data.1.is_empty() %}
conditions_else_{{ c.id }}:
{%- for ins in c.data.1 %}
{{ ins }}
{%- endfor %}
.return_call_stack_jump
{%- endif %}
{% endfor %}
; words
{%- for w in words %}
words_{{ w.id }}:
{%- for node in w.data.1 %}
{{ node }}
{%- endfor %}
.return_call_stack_jump
{% endfor %}
; static jump table
.define_eval prev, OFFSET
; conditions
{%- for c in conditions %}
; condition: {{ c.id }}
.org jump_table_conditions_if_{{ c.id }}
.bytes conditions_if_{{ c.id }}
{%- if !c.data.1.is_empty() %}
.org jump_table_conditions_else_{{ c.id }}
.bytes conditions_else_{{ c.id }}
{%- endif %}
{% endfor %}
; words
{%- for w in words %}
.org jump_table_words_{{ w.id }}
.bytes words_{{ w.id }}
{% endfor %}
.org prev
.delete prev
; main
main:
.org MAIN_JUMPER
ts main
tlr CORE_REG_PC
.org main
{%- for node in main %}
{{ node }}
{%- endfor %}
.std_stop