aboutsummaryrefslogtreecommitdiffstats
path: root/asm
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2023-11-07 16:29:52 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2023-11-07 16:29:52 +0200
commit5a0e57601a9489d9f56602e0184e722c85ffc310 (patch)
treed217f13caa628bba83411da15a027fecbbb7244c /asm
parent7309d563c6661f6e17c1ed3577b40b9646a85bb4 (diff)
WIP tree walking compiler
Diffstat (limited to 'asm')
-rw-r--r--asm/ast_compiler_bootstrap.atk1674
-rw-r--r--asm/atk16.py23
-rw-r--r--asm/test_py_src.atk16235
-rw-r--r--asm/test_py_src.atk16_optimized115
-rw-r--r--asm/test_py_src.py23
-rw-r--r--asm/test_py_src.pydis2
6 files changed, 340 insertions, 132 deletions
diff --git a/asm/ast_compiler_bootstrap.atk16 b/asm/ast_compiler_bootstrap.atk16
new file mode 100644
index 0000000..ad401f1
--- /dev/null
+++ b/asm/ast_compiler_bootstrap.atk16
@@ -0,0 +1,74 @@
+;; BEGIN BOOTSTRAP
+
+@opt stack_pointer RG
+@opt csr_scratch RH
+
+@use ext_std:*
+
+@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 __STACK_POINTER
+ 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
+ ldi 0x55 RE
+ 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 \ No newline at end of file
diff --git a/asm/atk16.py b/asm/atk16.py
index 143dd52..f1a2c95 100644
--- a/asm/atk16.py
+++ b/asm/atk16.py
@@ -1,11 +1,26 @@
-def atk_store(addr: int, value: int):
+from typing import Literal, Any, TypeVar, NewType, Never
+a = ["A", "B", "C"]
+Char = Literal['\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\t', '\n', '\x0b', '\x0c', '\r', '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '\x7f', '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87', '\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f', '\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97', '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f', '\xa0', '¡', '¢', '£', '¤', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '\xad', '®', '¯', '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ']
+ConstInt = int
+
+def store(addr: int, value: int | Char):
+ return NotImplemented
+
+def load(addr: int) -> int:
+ return NotImplemented
+
+def put_char(c: int):
return NotImplemented
-def atk_load(addr: int) -> int:
+def set_graphics_mode(mode: Literal[0, 1, 2]):
return NotImplemented
-def atk_put_char(c: int):
+T = TypeVar("T")
+def call_inline(expr: T) -> T:
+ """Inline the function call `expr` at the callsite.
+ This can lead to a larger output size but avoids having to do a subroutine call."""
return NotImplemented
-def atk_enable_text_mode():
+def asm(asm: str):
+ """Inject ATK16 assembly `asm` into the compiled output."""
return NotImplemented
diff --git a/asm/test_py_src.atk16 b/asm/test_py_src.atk16
index af8489b..c1bf1af 100644
--- a/asm/test_py_src.atk16
+++ b/asm/test_py_src.atk16
@@ -1,165 +1,160 @@
+;; BEGIN BOOTSTRAP
+
@opt stack_pointer RG
@opt csr_scratch RH
+
@use ext_std:*
-@include bootstrap
-@label py_consts
-; 0: 0
- 0
-; 1: ('*',)
- 0
-; 2: 63488
+
+@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 __STACK_POINTER
+ 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
+ ldi 0x55 RE
+ 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 TEXT_MODE
+ 1
+@label int_0
+ 23
+@label int_1
63488
-; 3: 72
+@label int_2
72
-; 4: 63489
+@label int_3
63489
-; 5: 69
+@label int_4
69
-; 6: 63490
+@label int_5
63490
-; 7: 76
+@label int_6
76
-; 8: 63491
+@label int_7
63491
-; 9: 63492
+@label int_8
+ 76
+@label int_9
63492
-; 10: 79
+@label int_10
79
-; 11: None
- 0
-@label atk_store
- str RB RA
- rsr
-@label atk_enable_text_mode
- set_graphics_mode gr_text_mode
- rsr
+
@label main
-; PUSH_NULL None
- ldi 0 RA
- spu RA
-; LOAD_NAME 1
- ldi atk_enable_text_mode RA
+; builtin call store [<ast.Constant object at 0x1045a3d90>, <ast.Name object at 0x1045a3d60>]
+ ldi int_0 RA
+ ldr RA RA
spu RA
-; PRECALL 0
-; CALL 0
- spo RA
- csr RA
-; POP_TOP None
+ spo RB
spo RA
-; PUSH_NULL None
- ldi 0 RA
- spu RA
-; LOAD_NAME 2
- ldi atk_store RA
- spu RA
-; LOAD_CONST 2
- ldi ${py_consts + 2} RA
+ str RB RA
+; builtin call store [<ast.Constant object at 0x1045a3c70>, <ast.Constant object at 0x1045a3c40>]
+ ldi int_1 RA
ldr RA RA
spu RA
-; LOAD_CONST 3
- ldi ${py_consts + 3} RA
+ ldi int_2 RA
ldr RA RA
spu RA
-; PRECALL 2
-; CALL 2
spo RB
spo RA
- spo RC
- csr RC
-; POP_TOP None
- spo RA
-; PUSH_NULL None
- ldi 0 RA
- spu RA
-; LOAD_NAME 2
- ldi atk_store RA
- spu RA
-; LOAD_CONST 4
- ldi ${py_consts + 4} RA
+ str RB RA
+; builtin call store [<ast.Constant object at 0x1045a3b50>, <ast.Constant object at 0x1045a3b20>]
+ ldi int_3 RA
ldr RA RA
spu RA
-; LOAD_CONST 5
- ldi ${py_consts + 5} RA
+ ldi int_4 RA
ldr RA RA
spu RA
-; PRECALL 2
-; CALL 2
spo RB
spo RA
- spo RC
- csr RC
-; POP_TOP None
- spo RA
-; PUSH_NULL None
- ldi 0 RA
- spu RA
-; LOAD_NAME 2
- ldi atk_store RA
- spu RA
-; LOAD_CONST 6
- ldi ${py_consts + 6} RA
+ str RB RA
+; builtin call store [<ast.Constant object at 0x1045a3a30>, <ast.Constant object at 0x1045a3a00>]
+ ldi int_5 RA
ldr RA RA
spu RA
-; LOAD_CONST 7
- ldi ${py_consts + 7} RA
+ ldi int_6 RA
ldr RA RA
spu RA
-; PRECALL 2
-; CALL 2
spo RB
spo RA
- spo RC
- csr RC
-; POP_TOP None
- spo RA
-; PUSH_NULL None
- ldi 0 RA
- spu RA
-; LOAD_NAME 2
- ldi atk_store RA
- spu RA
-; LOAD_CONST 8
- ldi ${py_consts + 8} RA
+ str RB RA
+; builtin call store [<ast.Constant object at 0x1045a3910>, <ast.Constant object at 0x1045a38e0>]
+ ldi int_7 RA
ldr RA RA
spu RA
-; LOAD_CONST 7
- ldi ${py_consts + 7} RA
+ ldi int_8 RA
ldr RA RA
spu RA
-; PRECALL 2
-; CALL 2
spo RB
spo RA
- spo RC
- csr RC
-; POP_TOP None
- spo RA
-; PUSH_NULL None
- ldi 0 RA
- spu RA
-; LOAD_NAME 2
- ldi atk_store RA
- spu RA
-; LOAD_CONST 9
- ldi ${py_consts + 9} RA
+ str RB RA
+; builtin call store [<ast.Constant object at 0x1045a37f0>, <ast.Constant object at 0x1045a37c0>]
+ ldi int_9 RA
ldr RA RA
spu RA
-; LOAD_CONST 10
- ldi ${py_consts + 10} RA
+ ldi int_10 RA
ldr RA RA
spu RA
-; PRECALL 2
-; CALL 2
spo RB
spo RA
- spo RC
- csr RC
-; POP_TOP None
- spo RA
-; LOAD_CONST 11
- ldi ${py_consts + 11} RA
- ldr RA RA
- spu RA
-; RETURN_VALUE None
-@label keep_alive
- jpi keep_alive \ No newline at end of file
+ str RB RA
+; builtin call asm [<ast.Constant object at 0x1045a2c20>]
+ ldi 1 RA \ No newline at end of file
diff --git a/asm/test_py_src.atk16_optimized b/asm/test_py_src.atk16_optimized
new file mode 100644
index 0000000..a6a77ab
--- /dev/null
+++ b/asm/test_py_src.atk16_optimized
@@ -0,0 +1,115 @@
+@opt stack_pointer RG
+@opt csr_scratch RH
+@use ext_std:*
+@let vector_table 0x10
+@let vt_ISR0 0x10
+@let vt_ISR1 0x11
+@let vt_ISR2 0x12
+@let vt_ISR3 0x13
+@let vt_stack_addr 0x14
+@let vt_term_pp_addr 0x15
+@let vt_kb_pp_addr 0x16
+@let vt_gr_mode_addr 0x17
+@let vt_sprite_mem 0x18
+@let vt_text_mem 0x19
+@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 __STACK_POINTER
+ jpi program_segment
+@address vector_table
+ keyboard_isr
+ hlt_isr
+ hlt_isr
+ hlt_isr
+ stack_segment
+ terminal_addr
+ keyboard_addr
+ gr_mode_addr
+ sprite_mem
+ text_mem
+@label hlt_isr
+ ldi 0x55 RE
+ 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
+@label TEXT_MODE
+ 1
+@label int_0
+ 23
+@label int_1
+ 63488
+@label int_2
+ 72
+@label int_3
+ 63489
+@label int_4
+ 69
+@label int_5
+ 63490
+@label int_6
+ 76
+@label int_7
+ 63491
+@label int_8
+ 76
+@label int_9
+ 63492
+@label int_10
+ 79
+@label main
+ ldi int_0 RA
+ ldr RA RB
+ spo RA
+ str RB RA
+ ldi int_1 RA
+ ldr RA RA
+ ldi int_2 RB
+ ldr RB RB
+ str RB RA
+ ldi int_3 RA
+ ldr RA RA
+ ldi int_4 RB
+ ldr RB RB
+ str RB RA
+ ldi int_5 RA
+ ldr RA RA
+ ldi int_6 RB
+ ldr RB RB
+ str RB RA
+ ldi int_7 RA
+ ldr RA RA
+ ldi int_8 RB
+ ldr RB RB
+ str RB RA
+ ldi int_9 RA
+ ldr RA RA
+ ldi int_10 RB
+ ldr RB RB
+ str RB RA
+ ldi 1 RA \ No newline at end of file
diff --git a/asm/test_py_src.py b/asm/test_py_src.py
index 89658f8..a1eca3f 100644
--- a/asm/test_py_src.py
+++ b/asm/test_py_src.py
@@ -1,8 +1,17 @@
-from atk16 import *
+import atk16
-atk_enable_text_mode()
-atk_store(0xF800, 65 + 7) # H
-atk_store(0xF801, 65 + 4) # E
-atk_store(0xF802, 65 + 11) # L
-atk_store(0xF803, 65 + 11) # L
-atk_store(0xF804, 65 + 14) # O
+TEXT_MODE: atk16.ConstInt = 1
+GRAPHICS_MODE_ADDR: atk16.ConstInt = 0x17
+
+atk16.store(GRAPHICS_MODE_ADDR, TEXT_MODE)
+atk16.store(0xF800, 'H')
+atk16.store(0xF801, 'E')
+atk16.store(0xF802, 'L')
+atk16.store(0xF803, 'L')
+atk16.store(0xF804, 'O')
+
+a = atk16.call_inline(
+ atk16.load(0xF000)
+)
+
+atk16.asm("ldi 1 RA")
diff --git a/asm/test_py_src.pydis b/asm/test_py_src.pydis
index 483f073..f4e427f 100644
--- a/asm/test_py_src.pydis
+++ b/asm/test_py_src.pydis
@@ -1,5 +1,5 @@
### Constants
-(0, ('*',), 63488, 72, 63489, 69, 63490, 76, 63491, 63492, 79, None)
+(0, ('*',), 63488, 'H', 63489, 'E', 63490, 'L', 63491, 63492, 'O', None)
### Names
('atk16', 'atk_enable_text_mode', 'atk_store')