diff options
| -rw-r--r-- | asm/aoc23_1a.atk16 | 554 | ||||
| -rw-r--r-- | asm/aoc23_1a.py | 48 | ||||
| -rw-r--r-- | asm/atk16.py | 12 | ||||
| -rw-r--r-- | asm/test_py_src.pydis | 63 | ||||
| -rw-r--r-- | digital/atk16_mem.dig | 2 | ||||
| -rw-r--r-- | src/ast_compiler.py | 18 |
6 files changed, 629 insertions, 68 deletions
diff --git a/asm/aoc23_1a.atk16 b/asm/aoc23_1a.atk16 new file mode 100644 index 0000000..78c4622 --- /dev/null +++ b/asm/aoc23_1a.atk16 @@ -0,0 +1,554 @@ +;; BEGIN BOOTSTRAP + +@use ext_std:* + +@let sp RH ; stack pointer (points to empty slot at top of stack) +@let fp RG ; frame pointer (points to base of currently active frame) + +@let vector_table 0x10 +@let vt_ISR0 0x10 ; ISR0 +@let vt_ISR1 0x11 ; ISR1 +@let vt_ISR2 0x12 ; ISR2 +@let vt_ISR3 0x13 ; ISR3 +@let vt_stack_addr 0x14 ; Stack address +@let vt_term_pp_addr 0x15 ; Terminal peripheral address +@let vt_kb_pp_addr 0x16 ; Keyboard peripheral address +@let vt_gr_mode_addr 0x17 ; Graphics mode setting address +@let vt_sprite_mem 0x18 ; Sprite memory address +@let vt_text_mem 0x19 ; Text memory buffer address + +@let stack_segment 0x8000 +@let mmio_segment 0xE000 +@let terminal_addr 0xE000 +@let keyboard_addr 0xE001 +@let gr_mode_addr 0xE002 +@let sprite_mem 0xE800 +@let text_mem 0xF800 + +@let gr_disabled_mode 0b00 +@let gr_text_mode 0b01 +@let gr_sprite_mode 0b10 + +@address 0x0 + ldi vt_stack_addr RA + ldr RA SP + mov SP FP + jpi program_segment + +@address vector_table + keyboard_isr ; 0x10 + hlt_isr ; 0x11 + hlt_isr ; 0x12 + hlt_isr ; 0x13 + stack_segment ; 0x14 + terminal_addr ; 0x15 + keyboard_addr ; 0x16 + gr_mode_addr ; 0x17 + sprite_mem ; 0x18 + text_mem ; 0x19 + +@label hlt_isr + hlt + +@label keyboard_isr + spu RA + spu RB + ldi vt_kb_pp_addr RA + ldr RA RA + ldr RA RA + ldi vt_term_pp_addr RB + ldr RB RB + str RA RB + spo RB + spo RA + rti + +@label program_segment + ldi vt_gr_mode_addr RA + ldr RA RA + ldi gr_disabled_mode RB + str RB RA + + jpi main + +;; END BOOTSTRAP + +@label int_0 + 8 +@label graphics_no_mode + 0 +@label graphics_text_mode + 1 +@label graphics_sprite_mode + 2 +@label graphics_mode_pp + 23 +@label text_mem_pp + 25 +@label input_p + 36864 +@label input_elem_size + 256 +@label int_1 + 49 +@label int_2 + 97 +@label int_3 + 98 +@label int_4 + 99 +@label int_5 + 50 + + + +@label main +; stack frame with offsets +; graphics_mode_p 0 +; text_mem_p 1 +; row_p 2 +; i 3 +; first 4 +; last 5 +; char_p 6 +; char 7 + ldi int_0 RA + ldr RA RA + add SP RA SP +; <ast.Assign object at 0x104353640> +; assigning graphics_mode_p at FP + 0 +; evaluating value to be assigned +; <ast.Call object at 0x1043535e0> +; Builtin call load [<ast.Name object at 0x104353550>] +; <ast.Name object at 0x104353550> (GRAPHICS_MODE_PP) + ldi graphics_mode_pp RC + ldr RC RC + spu RC +; (load) dereferencing top of stack + spo RC + ldr RC RC + spu RC +; assigning stack address (FP + 0) := top of stack + spo RA + ldi 0 RB + add RB FP RB + str RA RB +; <ast.Assign object at 0x104353520> +; assigning text_mem_p at FP + 1 +; evaluating value to be assigned +; <ast.Call object at 0x1043534c0> +; Builtin call load [<ast.Name object at 0x104353430>] +; <ast.Name object at 0x104353430> (TEXT_MEM_PP) + ldi text_mem_pp RC + ldr RC RC + spu RC +; (load) dereferencing top of stack + spo RC + ldr RC RC + spu RC +; assigning stack address (FP + 1) := top of stack + spo RA + ldi 1 RB + add RB FP RB + str RA RB +; <ast.Call object at 0x1043533d0> +; Builtin call store [<ast.Name object at 0x104353340>, <ast.Name object at 0x104353310>] +; <ast.Name object at 0x104353340> (graphics_mode_p) + ldi 0 RA + add FP RA RA + ldr RA RA + spu RA +; <ast.Name object at 0x104353310> (GRAPHICS_TEXT_MODE) + ldi graphics_text_mode RA + ldr RA RA + spu RA + spo RB + spo RA + str RB RA + ldi 0 RA + spu RA +; popping free-standing Expr result from stack + spo RA +; <ast.Call object at 0x1043530d0> +; Builtin call store [<ast.BinOp object at 0x104352a40>, <ast.Constant object at 0x104352d40>] +; <ast.Add object at 0x10430ef80> +; BinOp lhs <ast.BinOp object at 0x104352a40> +; <ast.Name object at 0x104352da0> (INPUT_P) + ldi input_p RA + ldr RA RA + spu RA +; BinOp rhs <ast.BinOp object at 0x104352a40> +; <ast.Constant object at 0x104352d70> + ldi 0 RA + spu RA + spo RB + spo RA + add RA RB RA + spu RA +; <ast.Constant object at 0x104352d40> + ldi int_1 RA + ldr RA RA ; int_1 = 49 + spu RA + spo RB + spo RA + str RB RA + ldi 0 RA + spu RA +; popping free-standing Expr result from stack + spo RA +; <ast.Call object at 0x104352cb0> +; Builtin call store [<ast.BinOp object at 0x104352e60>, <ast.Constant object at 0x104352dd0>] +; <ast.Add object at 0x10430ef80> +; BinOp lhs <ast.BinOp object at 0x104352e60> +; <ast.Name object at 0x104352e30> (INPUT_P) + ldi input_p RA + ldr RA RA + spu RA +; BinOp rhs <ast.BinOp object at 0x104352e60> +; <ast.Constant object at 0x104352e00> + ldi 1 RA + spu RA + spo RB + spo RA + add RA RB RA + spu RA +; <ast.Constant object at 0x104352dd0> + ldi int_2 RA + ldr RA RA ; int_2 = 97 + spu RA + spo RB + spo RA + str RB RA + ldi 0 RA + spu RA +; popping free-standing Expr result from stack + spo RA +; <ast.Call object at 0x104352770> +; Builtin call store [<ast.BinOp object at 0x1043526b0>, <ast.Constant object at 0x104352620>] +; <ast.Add object at 0x10430ef80> +; BinOp lhs <ast.BinOp object at 0x1043526b0> +; <ast.Name object at 0x104352950> (INPUT_P) + ldi input_p RA + ldr RA RA + spu RA +; BinOp rhs <ast.BinOp object at 0x1043526b0> +; <ast.Constant object at 0x104352980> + ldi 2 RA + spu RA + spo RB + spo RA + add RA RB RA + spu RA +; <ast.Constant object at 0x104352620> + ldi int_3 RA + ldr RA RA ; int_3 = 98 + spu RA + spo RB + spo RA + str RB RA + ldi 0 RA + spu RA +; popping free-standing Expr result from stack + spo RA +; <ast.Call object at 0x1043525c0> +; Builtin call store [<ast.BinOp object at 0x104352530>, <ast.Constant object at 0x1043524a0>] +; <ast.Add object at 0x10430ef80> +; BinOp lhs <ast.BinOp object at 0x104352530> +; <ast.Name object at 0x104352500> (INPUT_P) + ldi input_p RA + ldr RA RA + spu RA +; BinOp rhs <ast.BinOp object at 0x104352530> +; <ast.Constant object at 0x1043524d0> + ldi 3 RA + spu RA + spo RB + spo RA + add RA RB RA + spu RA +; <ast.Constant object at 0x1043524a0> + ldi int_4 RA + ldr RA RA ; int_4 = 99 + spu RA + spo RB + spo RA + str RB RA + ldi 0 RA + spu RA +; popping free-standing Expr result from stack + spo RA +; <ast.Call object at 0x104352440> +; Builtin call store [<ast.BinOp object at 0x1043523b0>, <ast.Constant object at 0x1043522f0>] +; <ast.Add object at 0x10430ef80> +; BinOp lhs <ast.BinOp object at 0x1043523b0> +; <ast.Name object at 0x104352380> (INPUT_P) + ldi input_p RA + ldr RA RA + spu RA +; BinOp rhs <ast.BinOp object at 0x1043523b0> +; <ast.Constant object at 0x104352350> + ldi 4 RA + spu RA + spo RB + spo RA + add RA RB RA + spu RA +; <ast.Constant object at 0x1043522f0> + ldi int_5 RA + ldr RA RA ; int_5 = 50 + spu RA + spo RB + spo RA + str RB RA + ldi 0 RA + spu RA +; popping free-standing Expr result from stack + spo RA +; <ast.Call object at 0x1043521d0> +; Builtin call store [<ast.BinOp object at 0x104352110>, <ast.Constant object at 0x104353040>] +; <ast.Add object at 0x10430ef80> +; BinOp lhs <ast.BinOp object at 0x104352110> +; <ast.Name object at 0x1043520e0> (INPUT_P) + ldi input_p RA + ldr RA RA + spu RA +; BinOp rhs <ast.BinOp object at 0x104352110> +; <ast.Constant object at 0x104352ec0> + ldi 5 RA + spu RA + spo RB + spo RA + add RA RB RA + spu RA +; <ast.Constant object at 0x104353040> + ldi 0 RA + spu RA + spo RB + spo RA + str RB RA + ldi 0 RA + spu RA +; popping free-standing Expr result from stack + spo RA +; <ast.Assign object at 0x104353010> +; assigning row_p at FP + 2 +; evaluating value to be assigned +; <ast.Name object at 0x104352fb0> (INPUT_P) + ldi input_p RC + ldr RC RC + spu RC +; assigning stack address (FP + 2) := top of stack + spo RA + ldi 2 RB + add RB FP RB + str RA RB +; <ast.Assign object at 0x104352f80> +; assigning i at FP + 3 +; evaluating value to be assigned +; <ast.Constant object at 0x104352f20> + ldi 0 RC + spu RC +; assigning stack address (FP + 3) := top of stack + spo RA + ldi 3 RB + add RB FP RB + str RA RB +; <ast.Assign object at 0x104352ef0> +; assigning first at FP + 4 +; evaluating value to be assigned +; <ast.Call object at 0x104352830> +; Builtin call load [<ast.Name object at 0x104352650>] +; <ast.Name object at 0x104352650> (row_p) + ldi 2 RC + add FP RC RC + ldr RC RC + spu RC +; (load) dereferencing top of stack + spo RC + ldr RC RC + spu RC +; assigning stack address (FP + 4) := top of stack + spo RA + ldi 4 RB + add RB FP RB + str RA RB +; <ast.Assign object at 0x104352680> +; assigning last at FP + 5 +; evaluating value to be assigned +; <ast.Constant object at 0x104352b60> + ldi 0 RC + spu RC +; assigning stack address (FP + 5) := top of stack + spo RA + ldi 5 RB + add RB FP RB + str RA RB +; <ast.While object at 0x104352920> +@label while_test_6 +; <ast.Constant object at 0x1043526e0> + ldi 1 RB + spu RB + spo RA + addi RA 0 RA + bri zero While_else_7 +; <ast.Assign object at 0x1042d85b0> +; assigning char_p at FP + 6 +; evaluating value to be assigned +; <ast.Add object at 0x10430ef80> +; BinOp lhs <ast.BinOp object at 0x1042d8670> +; <ast.Name object at 0x1042da800> (INPUT_P) + ldi input_p RC + ldr RC RC + spu RC +; BinOp rhs <ast.BinOp object at 0x1042d8670> +; <ast.Name object at 0x1042d9570> (i) + ldi 3 RC + add FP RC RC + ldr RC RC + spu RC + spo RD + spo RC + add RC RD RC + spu RC +; assigning stack address (FP + 6) := top of stack + spo RA + ldi 6 RB + add RB FP RB + str RA RB +; <ast.Assign object at 0x1042da4a0> +; assigning char at FP + 7 +; evaluating value to be assigned +; <ast.Call object at 0x1045e1840> +; Builtin call load [<ast.Name object at 0x1045e17b0>] +; <ast.Name object at 0x1045e17b0> (char_p) + ldi 6 RC + add FP RC RC + ldr RC RC + spu RC +; (load) dereferencing top of stack + spo RC + ldr RC RC + spu RC +; assigning stack address (FP + 7) := top of stack + spo RA + ldi 7 RB + add RB FP RB + str RA RB +; <ast.If object at 0x1045e1780> +; <ast.Compare object at 0x1045e1750> +; <ast.Name object at 0x1045e1720> (char) + ldi 7 RA + add FP RA RA + ldr RA RA + spu RA +; <ast.Constant object at 0x1045e16f0> + ldi 0 RA + spu RA +; Comparing <ast.Name object at 0x1045e1720> <ast.Eq object at 0x10430e890> <ast.Constant object at 0x1045e16f0> + spo RA + addi RA 0 RA + bri zero If_false_branch_11 +; <ast.Break object at 0x1045e16c0> + jpi While_end_8 + jpi If_end_branch_12 +@label if_false_branch_11 +@label if_end_branch_12 +; <ast.Assign object at 0x1045e1690> +; assigning last at FP + 5 +; evaluating value to be assigned +; <ast.Name object at 0x1045e1630> (char) + ldi 7 RC + add FP RC RC + ldr RC RC + spu RC +; assigning stack address (FP + 5) := top of stack + spo RA + ldi 5 RB + add RB FP RB + str RA RB +; <ast.Assign object at 0x104352260> +; assigning i at FP + 3 +; evaluating value to be assigned +; <ast.Add object at 0x10430ef80> +; BinOp lhs <ast.BinOp object at 0x104352230> +; <ast.Name object at 0x1045e15d0> (i) + ldi 3 RC + add FP RC RC + ldr RC RC + spu RC +; BinOp rhs <ast.BinOp object at 0x104352230> +; <ast.Constant object at 0x1045e15a0> + ldi 1 RC + spu RC + spo RD + spo RC + add RC RD RC + spu RC +; assigning stack address (FP + 3) := top of stack + spo RA + ldi 3 RB + add RB FP RB + str RA RB + jpi While_test_6 +@label while_else_7 +@label while_end_8 +; <ast.Call object at 0x1045e1540> +; Builtin call store [<ast.BinOp object at 0x1045e14b0>, <ast.Name object at 0x1045e1420>] +; <ast.Add object at 0x10430ef80> +; BinOp lhs <ast.BinOp object at 0x1045e14b0> +; <ast.Name object at 0x1045e1480> (text_mem_p) + ldi 1 RA + add FP RA RA + ldr RA RA + spu RA +; BinOp rhs <ast.BinOp object at 0x1045e14b0> +; <ast.Constant object at 0x1045e1450> + ldi 0 RA + spu RA + spo RB + spo RA + add RA RB RA + spu RA +; <ast.Name object at 0x1045e1420> (first) + ldi 4 RA + add FP RA RA + ldr RA RA + spu RA + spo RB + spo RA + str RB RA + ldi 0 RA + spu RA +; popping free-standing Expr result from stack + spo RA +; <ast.Call object at 0x1045e13c0> +; Builtin call store [<ast.BinOp object at 0x1045e1330>, <ast.Name object at 0x1045e12a0>] +; <ast.Add object at 0x10430ef80> +; BinOp lhs <ast.BinOp object at 0x1045e1330> +; <ast.Name object at 0x1045e1300> (text_mem_p) + ldi 1 RA + add FP RA RA + ldr RA RA + spu RA +; BinOp rhs <ast.BinOp object at 0x1045e1330> +; <ast.Constant object at 0x1045e12d0> + ldi 1 RA + spu RA + spo RB + spo RA + add RA RB RA + spu RA +; <ast.Name object at 0x1045e12a0> (last) + ldi 5 RA + add FP RA RA + ldr RA RA + spu RA + spo RB + spo RA + str RB RA + ldi 0 RA + spu RA +; popping free-standing Expr result from stack + spo RA + mov FP SP + hlt diff --git a/asm/aoc23_1a.py b/asm/aoc23_1a.py new file mode 100644 index 0000000..12e3953 --- /dev/null +++ b/asm/aoc23_1a.py @@ -0,0 +1,48 @@ +import atk16 + +GRAPHICS_NO_MODE: atk16.ConstWord16 = 0 +GRAPHICS_TEXT_MODE: atk16.ConstWord16 = 1 +GRAPHICS_SPRITE_MODE: atk16.ConstWord16 = 2 + +GRAPHICS_MODE_PP: atk16.ConstWord16 = 0x17 +TEXT_MEM_PP: atk16.ConstWord16 = 0x19 + +graphics_mode_p = atk16.load(GRAPHICS_MODE_PP) +text_mem_p = atk16.load(TEXT_MEM_PP) + +atk16.store(graphics_mode_p, GRAPHICS_TEXT_MODE) + +INPUT_P: atk16.ConstWord16 = 0x9000 +INPUT_ELEM_SIZE: atk16.ConstWord16 = 256 + +# 1a example + +atk16.store(INPUT_P + 0, "1") +atk16.store(INPUT_P + 1, "a") +atk16.store(INPUT_P + 2, "b") +atk16.store(INPUT_P + 3, "c") +atk16.store(INPUT_P + 4, "2") +atk16.store(INPUT_P + 5, "\0") +# atk16.store_const_vec(INPUT_P + 0, "1abc2\0") +# atk16.store_const_vec(INPUT_P + 256, "pqr3stu8vwx\0") +# atk16.store_const_vec(INPUT_P + 512, "a1b2c3d4e5f\0") +# atk16.store_const_vec(INPUT_P + 768, "treb7uchet\0") + +row_p = INPUT_P +i = 0 + +first = atk16.load(row_p) +last = 0 + +while True: + char_p = INPUT_P + i + char = atk16.load(char_p) + + if char == "\0": + break + + last = char + i += 1 + +atk16.store(text_mem_p + 0, first) +atk16.store(text_mem_p + 1, last)
\ No newline at end of file diff --git a/asm/atk16.py b/asm/atk16.py index 2a1c0a1..9aa3c55 100644 --- a/asm/atk16.py +++ b/asm/atk16.py @@ -4,7 +4,17 @@ Void = Literal[0] Word16 = int ConstWord16 = int -def store(p: Word16, value: Word16) -> Void: +def store(p: Word16, value: Word16 | Char) -> Void: + raise NotImplementedError + +@overload +def store_const_vec(p: Word16, value: list[Word16]) -> Void: ... +@overload +def store_const_vec(p: Word16, value: list[Char]) -> Void: ... +@overload +def store_const_vec(p: Word16, value: str) -> Void: ... + +def store_const_vec(p, value) -> Void: raise NotImplementedError def load(p: Word16) -> Word16: diff --git a/asm/test_py_src.pydis b/asm/test_py_src.pydis deleted file mode 100644 index f4e427f..0000000 --- a/asm/test_py_src.pydis +++ /dev/null @@ -1,63 +0,0 @@ -### Constants -(0, ('*',), 63488, 'H', 63489, 'E', 63490, 'L', 63491, 63492, 'O', None) - -### Names -('atk16', 'atk_enable_text_mode', 'atk_store') - -### Instructions -RESUME 0 -LOAD_CONST 0 -LOAD_CONST 1 -IMPORT_NAME 0 -IMPORT_STAR None -PUSH_NULL None -LOAD_NAME 1 -PRECALL 0 -CALL 0 -POP_TOP None -PUSH_NULL None -LOAD_NAME 2 -LOAD_CONST 2 -LOAD_CONST 3 -PRECALL 2 -CALL 2 -POP_TOP None -PUSH_NULL None -LOAD_NAME 2 -LOAD_CONST 4 -LOAD_CONST 5 -PRECALL 2 -CALL 2 -POP_TOP None -PUSH_NULL None -LOAD_NAME 2 -LOAD_CONST 6 -LOAD_CONST 7 -PRECALL 2 -CALL 2 -POP_TOP None -PUSH_NULL None -LOAD_NAME 2 -LOAD_CONST 8 -LOAD_CONST 7 -PRECALL 2 -CALL 2 -POP_TOP None -PUSH_NULL None -LOAD_NAME 2 -LOAD_CONST 9 -LOAD_CONST 10 -PRECALL 2 -CALL 2 -POP_TOP None -LOAD_CONST 11 -RETURN_VALUE None -warn: cannot serialize <class 'tuple'> (('*',)), emitting zero -warn: cannot serialize <class 'NoneType'> (None), emitting zero -Skipping not implemented opcode: PRECALL -Skipping not implemented opcode: PRECALL -Skipping not implemented opcode: PRECALL -Skipping not implemented opcode: PRECALL -Skipping not implemented opcode: PRECALL -Skipping not implemented opcode: PRECALL -Skipping not implemented opcode: RETURN_VALUE diff --git a/digital/atk16_mem.dig b/digital/atk16_mem.dig index 5f5e2e6..6ff9810 100644 --- a/digital/atk16_mem.dig +++ b/digital/atk16_mem.dig @@ -34,7 +34,7 @@ </entry> <entry> <string>Data</string> - <data>4014,2e00,1dc0,6027,8000,8000,8000,8000,8000,8000,8000,8000,8000,8000,8000,8000,001c,001a,001a,001a,8000,e000,e001,e002,e800,f800,4855,f000,31c0,1fc8,31c8,1fc8,4016,2000,2000,4215,2240,3040,1fc9,23c0,1fc9,21c0,e000,4017,2000,4200,3008,6018,0001,0017,0019,0041,001a,4433,2480,31d0,1fc8,4400,0590,2480,31d0,1fc8,1fc9,27c0,1fc9,25c0,0498,31d0,1fc8,1d89,2380,5040,1fd8,4431,2480,31d0,1fc8,1fc9,25c0,2480,31d0,1fc8,1fc9,21c0,4200,0270,3040,4432,2480,31d0,1fc8,1fc9,25c0,2480,31d0,1fc8,1fc9,21c0,4201,0270,3040,4000,0180,2000,31c0,1fc8,4030,2000,31c0,1fc8,1fc9,23c0,1fc9,21c0,3008,4000,31c0,1fc8,1fc9,21c0,4400,31d0,1fc8,1fc9,21c0,4202,0270,3040,4202,0388,2240,31c8,1fc8,4234,2240,31c8,1fc8,1fc9,25c0,1fc9,23c0,0251,8004,4200,31c8,1fc8,6003,4201,31c8,1fc8,1fc9,21c0,1000,8447,4001,0180,2000,31c0,1fc8,4002,0180,2000,31c0,1fc8,1fc9,23c0,1fc9,21c0,0008,31c0,1fc8,31f0,1fc8,4000,31c0,1fc8,4002,0180,2000,31c0,1fc8,1dd1,9000,1020,3180,1d88,617a,1fc9,21c0,1f80,1fc9,2dc0,31c0,1fc8,1fc9,23c0,1fc9,21c0,3008,4000,31c0,1fc8,1fc9,21c0,4402,0590,2480,31d0,1fc8,4401,31d0,1fc8,1fc9,27c0,1fc9,25c0,0498,31d0,1fc8,1fc9,21c0,4202,0270,3040,619f,4201,31c8,1fc8,1fc9,21c0,1000,8401,61f8,1f80,f000</data> + <data>4014,2e00,1dc0,6026,8000,8000,8000,8000,8000,8000,8000,8000,8000,8000,8000,8000,001b,001a,001a,001a,8000,e000,e001,e002,e800,f800,f000,31c0,1fc8,31c8,1fc8,4016,2000,2000,4215,2240,3040,1fc9,23c0,1fc9,21c0,e000,4017,2000,4200,3008,600d,0008,0000,0001,0002,0017,0019,9000,0100,0031,0061,0062,0063,0032,402f,2000,0fc0,4433,2480,31d0,1fc8,1fc9,25c0,2480,31d0,1fc8,1fc9,21c0,4200,0270,3040,4434,2480,31d0,1fc8,1fc9,25c0,2480,31d0,1fc8,1fc9,21c0,4201,0270,3040,4000,0180,2000,31c0,1fc8,4031,2000,31c0,1fc8,1fc9,23c0,1fc9,21c0,3008,4000,31c0,1fc8,1fc9,21c0,4035,2000,31c0,1fc8,4000,31c0,1fc8,1fc9,23c0,1fc9,21c0,0008,31c0,1fc8,4037,2000,31c0,1fc8,1fc9,23c0,1fc9,21c0,3008,4000,31c0,1fc8,1fc9,21c0,4035,2000,31c0,1fc8,4001,31c0,1fc8,1fc9,23c0,1fc9,21c0,0008,31c0,1fc8,4038,2000,31c0,1fc8,1fc9,23c0,1fc9,21c0,3008,4000,31c0,1fc8,1fc9,21c0,4035,2000,31c0,1fc8,4002,31c0,1fc8,1fc9,23c0,1fc9,21c0,0008,31c0,1fc8,4039,2000,31c0,1fc8,1fc9,23c0,1fc9,21c0,3008,4000,31c0,1fc8,1fc9,21c0,4035,2000,31c0,1fc8,4003,31c0,1fc8,1fc9,23c0,1fc9,21c0,0008,31c0,1fc8,403a,2000,31c0,1fc8,1fc9,23c0,1fc9,21c0,3008,4000,31c0,1fc8,1fc9,21c0,4035,2000,31c0,1fc8,4004,31c0,1fc8,1fc9,23c0,1fc9,21c0,0008,31c0,1fc8,403b,2000,31c0,1fc8,1fc9,23c0,1fc9,21c0,3008,4000,31c0,1fc8,1fc9,21c0,4035,2000,31c0,1fc8,4005,31c0,1fc8,1fc9,23c0,1fc9,21c0,0008,31c0,1fc8,4000,31c0,1fc8,1fc9,23c0,1fc9,21c0,3008,4000,31c0,1fc8,1fc9,21c0,4435,2480,31d0,1fc8,1fc9,21c0,4202,0270,3040,4400,31d0,1fc8,1fc9,21c0,4203,0270,3040,4402,0590,2480,31d0,1fc8,1fc9,25c0,2480,31d0,1fc8,1fc9,21c0,4204,0270,3040,4400,31d0,1fc8,1fc9,21c0,4205,0270,3040,4201,31c8,1fc8,1fc9,21c0,1000,8451,4435,2480,31d0,1fc8,4403,0590,2480,31d0,1fc8,1fc9,27c0,1fc9,25c0,0498,31d0,1fc8,1fc9,21c0,4206,0270,3040,4406,0590,2480,31d0,1fc8,1fc9,25c0,2480,31d0,1fc8,1fc9,21c0,4207,0270,3040,4007,0180,2000,31c0,1fc8,4000,31c0,1fc8,1fc9,21c0,1000,8402,6020,6000,4407,0590,2480,31d0,1fc8,1fc9,21c0,4205,0270,3040,4403,0590,2480,31d0,1fc8,4401,31d0,1fc8,1fc9,27c0,1fc9,25c0,0498,31d0,1fc8,1fc9,21c0,4203,0270,3040,61a8,4001,0180,2000,31c0,1fc8,4000,31c0,1fc8,1fc9,23c0,1fc9,21c0,0008,31c0,1fc8,4004,0180,2000,31c0,1fc8,1fc9,23c0,1fc9,21c0,3008,4000,31c0,1fc8,1fc9,21c0,4001,0180,2000,31c0,1fc8,4001,31c0,1fc8,1fc9,23c0,1fc9,21c0,0008,31c0,1fc8,4005,0180,2000,31c0,1fc8,1fc9,23c0,1fc9,21c0,3008,4000,31c0,1fc8,1fc9,21c0,1f80,f000</data> </entry> </elementAttributes> <pos x="740" y="100" /> diff --git a/src/ast_compiler.py b/src/ast_compiler.py index 99b4176..fe6f5a6 100644 --- a/src/ast_compiler.py +++ b/src/ast_compiler.py @@ -195,6 +195,18 @@ class Compiler(ast.NodeVisitor): self.emit(f"spu {reg}") + def emit_add_imm(self, reg1: str, addend: int, target_reg: str): + if addend >= 0 and addend < 8: + self.emit(f"addi {reg1} {addend} {target_reg}") + else: + name = self.get_unique_name("int") + self.assign_const(name, addend) + + with self.allocated_reg() as addend_reg: + self.emit(f"ldi {name} {addend_reg}") + self.emit(f"ldr {addend_reg} {addend_reg}") + self.emit(f"add {reg1} {addend_reg} {target_reg}") + def visit_Module(self, node: ast.Module): stmts = node.body frame_names = self.collect_local_variables(stmts) @@ -204,7 +216,7 @@ class Compiler(ast.NodeVisitor): self.local_bindings[name] = offset self.emit(f"; {name} {offset}") - self.emit(f"addi SP {len(frame_names)} SP") + self.emit_add_imm("SP", len(frame_names), "SP") for stmt in stmts: if type(stmt) == ast.Expr and type(stmt.value) != ast.Call: @@ -510,7 +522,7 @@ class Compiler(ast.NodeVisitor): # Move stack pointer to accommodate local variables if len(local_var_names) > 0: - self.emit(f"addi SP {len(local_var_names)} SP") + self.emit_add_imm("SP", len(local_var_names), "SP") prev_local_bindings = self.local_bindings self.local_bindings = {} @@ -666,7 +678,7 @@ class Compiler(ast.NodeVisitor): target=ast.Name(name), annotation=ast.Attribute( value=ast.Name(id="atk16"), - attr="ConstInt"), + attr="ConstWord16"), value=ast.Constant(value) # TODO: constant folding ): if type(value) == int: |
