hence/henceforth/templates/default.asm

224 lines
5.1 KiB
NASM

{%- macro escape_name(name) -%}
{% if name.starts_with('"') %}{{ name }}{% else %}"{{ name }}"{% endif %}
{%- endmacro -%}
; Generated by Henceforth {{ env!("CARGO_PKG_VERSION") }} ({{ env!("CARGO_PKG_REPOSITORY") }})
.include "$lib/core.asm"
.include "$lib/std.asm"
.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_eval prev, OFFSET
{%- for c in conditions %}
.bytes NULL
{% if !c.data.1.is_empty() %}.bytes NULL{% endif %}
{% endfor %}
.define_eval INDEX_CONDITIONS_SIZE, (OFFSET - prev)
.org prev
.delete prev
.debug INDEX_CONDITIONS_SIZE
; index
{%- for c in conditions %}
index_conditions_if_{{ c.id }}:
{% endfor %}
.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
.std_set MEM_ALLOC_PTR
.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
.define_eval prev, OFFSET
.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_SIZE, (OFFSET - prev)
.org prev
.delete prev
.macro call_stack_jump, call_stack_jump_arg_0_label
.define_eval prev, OFFSET
.define offset_till_end, (OFFSET - prev * -1 + CALL_STACK_JUMP_COND_IF_ELSE_SIZE)
.std_rset CORE_REG_C, call_stack_jump_arg_0_label
.std_rset CORE_REG_D, (OFFSET + offset_till_end)
ts call_stack_jump
tlr CORE_REG_PC
.delete prev, offset_till_end
.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
.define_eval CALL_STACK_JUMP_COND_IF_SIZE, (OFFSET - prev)
.org prev
.delete prev
.macro call_stack_jump_cond_if, call_stack_jump_cond_if_arg_0_if_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_arg_0_if_label
.std_rset CORE_REG_D, (OFFSET + offset_till_end)
ts call_stack_jump
tlrc CORE_REG_PC
.delete prev, offset_till_end
.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)
.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 }}:
{% let name = snailquote::escape(s.data) -%}
; original : {% call escape_name(name) %}
; size : {{ bytes.len() }}
{%- 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
conditions_else_{{ c.id }}:
{%- for ins in c.data.1 %}
{{ ins }}
{%- endfor %}
.return_call_stack_jump
{% endfor %}
; words
{%- for w in words %}
words_{{ w.id }}:
{% let name = snailquote::escape(w.data.0) -%}
; name : {% call escape_name(name) %}
{%- for node in w.data.1 %}
{{ node }}
{%- endfor %}
.return_call_stack_jump
{% endfor %}
; main
main:
.org MAIN_JUMPER
ts main
tlr CORE_REG_PC
.org main
{%- for node in main %}
{{ node }}
{%- endfor %}
.std_stop