I want to share some progress on my custom meta-assembler. The core focus here is automating the creation of Virtual Method Tables (VTables). This is essential when developing drivers or system components where data structures and methods must be tightly coupled but remain easily extensible.
1. Type Declaration (Layout)
At the source level, we define the data structure and method signatures. This allows the assembler to pre-calculate field offsets and method indices for the dispatch table.
Lisp
(deftype vec3 (structure)
((x int16) ;; offset 0
(y int16) ;; offset 2
(z int16)) ;; offset 4
(:methods
(new (int16 int16) none) ;; Method ID 0
(len () int16))) ;; Method ID 1
2. Method Implementation
Next, we write the actual method code. By using the $ prefix, the meta-assembler automatically binds these labels to the specific type's method slots.
Lisp
(org #x9000)
$vec3::new
(ret)
$vec3::len
(ret)
3. VTable Generation
The vtable command is a powerful meta-instruction. It inspects the vec3 type, locates all implemented methods, and arranges them in the correct order. It also handles "holes" (sparse tables) if certain methods aren't overridden in a hierarchy.
Lisp
(align 16)
(vtable position vec3)
Compilation Result (Listing)
The assembler doesn't just allocate space; it generates a functional jump table (using the jp opcode 0xC3 for the Z80).
Plaintext
9000 | -------- | org 9000h 9000 | | vec3::new: 9000 | C9 | ret 9001 | | vec3::len: 9001 | C9 | ret 9002 | -------- | align 10h 9010 | C3 00 90 00 00 00 00 00 | .vtable position, vec3, (quote default) 9010 | 00 00 00 00 00 00 00 00 | 9010 | 00 00 00 00 00 00 00 00 | 9010 | 00 00 00 C3 01 90 |
Key Benefits:
- Type Safety: Manually calculating table indices is error-prone; the assembler now handles this automatically.
- Flexibility: If you add a method to the
deftype, the VTable is rebuilt and re-aligned on the next pass. - Clarity: The listing clearly shows exactly where each jump leads, making debugging significantly easier.
Next step: Implementing high-level object declarations and automating method dispatch (dynamic dispatchers).
h2w
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.