2 Commits
12 changed files with 1074 additions and 121 deletions
+2 -2
View File
@@ -67,8 +67,8 @@
*
* asm volatile("nop" : : : reg_str(R_RA), "memory"); // clobber list */
#define rlit_stringfy(n) "$" stringify(n)
#define rlit_tmpl(n) rlit_stringfy(tmpl(n,Code))
#define rlit(n) rlit_tmpl(n)
#define rlit_tmpl(n) rlit_stringfy(tmpl(n,Code))
#define rlit(n) rlit_tmpl(n)
/* ------------------------------------------------------------------------ *
* rgcc(n) — GCC-specific bundle for register-variable declarations.
+50
View File
@@ -0,0 +1,50 @@
// Auto-generated by gen_atom_offsets.lua — DO NOT EDIT
// Source: C:\projects\Pikuma\ps1\code\duffle\lottes_tape.h
#ifndef LOTTES_TAPE_OFFSETS_H
#define LOTTES_TAPE_OFFSETS_H
#pragma region lottes_tape
// Override the placeholder atom_offset() to dispatch via token pasting.
#undef atom_offset
#define atom_offset(name) atom_offset_##name
// --- atom: sym (8 words) ---
// --- atom: tape_exit (2 words) ---
// --- atom: yield (4 words) ---
// --- atom: mips_flush_icache (13 words) ---
// --- atom: sync_prim_cursor (6 words) ---
// --- atom: set_gte_world (22 words) ---
// --- atom: rbind_cube_tri (6 words) ---
// --- atom: cube_tri (74 words) ---
// --- atom: rbind_floor_tri (6 words) ---
// --- atom: diag_yield (4 words) ---
// --- atom: diag_color (28 words) ---
// --- atom: diag_gte (34 words) ---
#pragma endregion lottes_tape
#endif // LOTTES_TAPE_OFFSETS_H
+16 -2
View File
@@ -5,6 +5,7 @@
# include "mips.h"
#endif
#pragma region ASM DSL
/* ============================================================================
* gte.h — Geometry Transformation Engine (COP2) for the PS1
* ============================================================================
@@ -332,7 +333,7 @@ enum { _C2_OPS_ = 0
| enc_gte_cmd(cmd) \
)
/* Pre-baked GTE command words for the common cases.
/* GTE command words for the common cases.
*
* These are pure compile-time integer constants — the C compiler
* constant-folds them into `.word` directives in .rodata. Use them
@@ -379,12 +380,17 @@ enum { _C2_OPS_ = 0
#define gte_cmdw_op (gte_cmd_base | enc_gte_cmd(gte_cmd_op ))
#define gte_cmdw_mvmva (gte_cmd_base | enc_gte_cmd(gte_cmd_mvmva))
#define gte_cmdw_rotate_translate_perspective_triple gte_cmdw_rtpt
/* PsyQ compatibility bits for AVSZ3 (Bits 20, 22, 24 must be set) */
#define gte_cmdw_psyq_avsz3_compat (0x15 << 20)
#define gte_cmd_avsz3 0x2D
#define gte_cmdw_avsz3 (gte_cmd_base | enc_gte_cmd(gte_cmd_avsz3) | gte_cmdw_psyq_avsz3_compat)
// Takes the three screen-space Z values of the triangle → averages them → writes the result into the OTZ register.
#define gte_avg_sort_z3 gte_cmdw_avsz3
/* AVSZ4 — average Z of 4 vertices (for quads) */
#define gte_cmd_avsz4 0x2E
#define gte_cmdw_avsz4 (gte_cmd_base | enc_gte_cmd(gte_cmd_avsz4) | gte_cmdw_psyq_avsz3_compat)
@@ -403,7 +409,7 @@ enum { _C2_OPS_ = 0
* asm_gte_load_v0(svector_ptr);
*/
/* Pre-baked lwc2 encoding helpers parameterized on the base GPR.
/* lwc2 encoding helpers parameterized on the base GPR.
*
* gte_lwc2_v0(base) → lwc2 $0, 0(base) ; C2_VXY0
* gte_lwc2_v0z(base) → lwc2 $1, 4(base) ; C2_VZ0
@@ -656,3 +662,11 @@ enum {
, r_use(r0) \
asm_clobber: clb_system, rlit(R_T4), rlit(R_T5), rlit(R_T6) \
)
#pragma endregion ASM DSL
#pragma region Reserved
#pragma endregion Reserved
+63 -59
View File
@@ -7,7 +7,10 @@
# include "memory.h"
#endif
typedef U4 const MipsCode;
#define MipsAtom_(sym) MipsCode tmpl(code,sym) [] align_(4) =
#pragma region Tape Drive
/* ---------------------------------------------------------------------------
* TAPE DRIVE ABI & REGISTER ALIASES
* ---------------------------------------------------------------------------
@@ -15,6 +18,7 @@
* The C compiler is completely unaware of these bindings.
* ---------------------------------------------------------------------------*/
enum {
R_AtomJmp = R_T9,
R_TapePtr = R_T8, /* The Instruction Stream Pointer */
R_PrimCur = R_T7, /* VRAM output cursor (primitive buffer) */
R_FaceCur = R_T4, /* Input data cursor (indices/faces) */
@@ -62,12 +66,14 @@ FI_ TapeBuilder tb_make( FArena* arena) { return (TapeBuilder){
#define tb_emit_(tb, atom) tb_emit(tb, tmpl(code,atom))
FI_ void tb_emit(TapeBuilder* tb, MipsCode* atom) { u4_r(tb->ptr)[tb->used] = u4_(atom); ++ tb->used; }
FI_ void tb_data(TapeBuilder* tb, U4 data) { u4_r(tb->ptr)[tb->used] = u4_(data); ++ tb->used; }
FI_ void tb_data(TapeBuilder* tb, U4 data) { u4_r(tb->ptr)[tb->used] = u4_(data); ++ tb->used; }
FI_ Slice_U4 tb_end (TapeBuilder* tb) { tb_emit(tb,code_tape_exit); return (Slice_U4){ C_(U4*,tb->ptr), tb->used }; }
FI_ Slice_U4 tb_slice(TapeBuilder tb) { return (Slice_U4){ C_(U4*,tb.ptr), tb.used }; }
#define tb_scope(tb) for(U4 tbs_once=0;tbs_once==0;++tbs_once,tb_emit(tb,code_tape_exit))
#pragma endregion Tape Drive
#pragma region Macro Mips Atom Components
/* ---------------------------------------------------------------------------
* MACRO ATOM Components (Reusable Assembly Components)
@@ -78,33 +84,30 @@ FI_ Slice_U4 tb_slice(TapeBuilder tb) { return (Sli
* Loads the next pointer from the tape, advances the tape, and jumps.
* Cost: ~ 4 cycles */
#define mac_yield() \
load_word(R_T9, R_TapePtr, 0) \
, add_ui_1( R_TapePtr, 4) \
, jump_reg( R_T9) \
load_word(R_AtomJmp, R_TapePtr, 0) \
, add_ui_1( R_TapePtr, 4) \
, jump_reg( R_AtomJmp) \
, nop
/* Loads 3 16-bit indices from the face array */
/* Words: 3; Loads 3 S2 indices from the face array */
#define mac_load_tri_indices(rId_0, rId_1, rId_2) \
load_half_u(rId_0, R_FaceCur, 0) \
, load_half_u(rId_1, R_FaceCur, 2) \
, load_half_u(rId_2, R_FaceCur, 4)
/* Translates indices to vertex addresses and pushes them to GTE */
/* Words: 18; Translates indices to vertex addresses and pushes them to GTE
R_AT = rId_[#] << 3;
R_AT += R_VertBase;
R_V0 = R_AT[0];
gte_mt(R_V0, V.xy[#]);
gte_mt(R_V1, V.z [#]);
*/
#define mac_load_tri_verts(rId_0, rId_1, rId_2) \
shift_ll(R_AT, rId_0, 3), add_u(R_AT, R_AT, R_VertBase), load_word(R_V0, R_AT, 0), load_word(R_V1, R_AT, 4), gte_mt(R_V0, C2_VXY0), gte_mt(R_V1, C2_VZ0) \
, shift_ll(R_AT, rId_1, 3), add_u(R_AT, R_AT, R_VertBase), load_word(R_V0, R_AT, 0), load_word(R_V1, R_AT, 4), gte_mt(R_V0, C2_VXY1), gte_mt(R_V1, C2_VZ1) \
, shift_ll(R_AT, rId_2, 3), add_u(R_AT, R_AT, R_VertBase), load_word(R_V0, R_AT, 0), load_word(R_V1, R_AT, 4), gte_mt(R_V0, C2_VXY2), gte_mt(R_V1, C2_VZ2)
/* Formats the primitive memory layout (Tag + Color + Coordinates) */
#define mac_format_prim_f3(color_hi, color_lo) \
store_word(R_0, R_PrimCur, 0) \
, load_ui(R_AT, color_hi), or_i(R_AT, R_AT, color_lo) \
, store_word(R_AT, R_PrimCur, 4) \
, gte_sw(C2_SXY0, R_PrimCur, 8) \
, gte_sw(C2_SXY1, R_PrimCur, 12) \
, gte_sw(C2_SXY2, R_PrimCur, 16)
/* Correctly inserts a primitive into the Ordering Table linked list */
/* Words: 11; Correctly inserts a primitive into the Ordering Table linked list */
#define mac_insert_ot_tag(r_otz, prim_length) \
shift_ll( R_T1, r_otz, 2) \
, add_u( R_T1, R_T1, R_OtBase) /* T1 = &OrderingTable[OTZ] */ \
@@ -136,7 +139,7 @@ FI_ void atombuilder_unroll(MipsAtomBuilder_R ab, Slice_MipsCode_R code) {
mem_copy(ab->start, u4_(code->ptr), code->len);
mem_bump(ab->start, ab->capacity, & ab->used, code->len);
}
#define atombuilder_unroll_mac(ab, mac)
#define atombuilder_unroll_mac(ab, mac) atombuilder_unroll(ab, slice_arg_from_array(Slice_MipsCode, mac))
// When done authoring, utilize this to cap-off the atom
FI_ void atombuilder_end(MipsAtomBuilder_R ab) {
@@ -152,14 +155,48 @@ FI_ void atombuilder_end(MipsAtomBuilder_R ab) {
#pragma region Baked Mips Atoms
// These atoms are resolved at compile time and are (usually) statically linked readonly data.
enum {
bios_flushcache = 0x44,
bios_table_addr = 0xA0,
};
/* Flushes the Instruction Cache (PSX A-function 0x44 via BIOS stub at 0xA0).
*
* Sequence (per MIPS ABI; arguments in arg registers, RA pushed to stack):
* 1. sp -= 8; sw $ra, 4($sp) ; save RA
* 2. $a0 = bios_flushcache (arg0)
* 3. $t0 = bios_table_addr ; t0 = &BIOS A-function table
* 4. jalr $t0, $ra ; call BIOS(flushcache)
* nop ; branch delay slot
* 5. lw $ra, 4($sp); jr $ra ; restore & return
* 6. sp += 8
*/
internal MipsAtom_(mips_flush_icache) {
add_ui(rstack_ptr, rstack_ptr, -8) /* sp -= 8 */
, store_word(rret_addr, rstack_ptr, 4) /* sw $ra, 4($sp) */
, add_ui(rret_0, rdiscard, bios_flushcache) /* addiu $a0, $0, 0x44 */
, add_ui(rtmp_0, rdiscard, bios_table_addr) /* addiu $t0, $0, 0xA0 */
, jump_link(rtmp_0, rret_addr) /* jalr $t0, $ra */
, nop /* BD slot */
, load_word(rret_addr, rstack_ptr, 4) /* lw $ra, 4($sp) */
, jump_reg(rret_addr) /* jr $ra */
, add_ui(rstack_ptr, rstack_ptr, 8) /* sp += 8 (BD) */
, mac_yield()
};
typedef Struct_(Binds_SyncPrimCursor) {
U4 PrimtiveArena_Used;
U4 PrimtiveBase;
};
internal MipsAtom_(sync_prim_cursor) {
/* Pop the C-struct address and base address from the tape */
load_word(R_AT, R_TapePtr, 0), /* AT = &pa->used */
load_word(R_T0, R_TapePtr, 4), /* T0 = prim_base */
add_ui_1( R_TapePtr, 8),
// Note(Ed): Argument shuffle....
load_word(R_AT, R_TapePtr, O_(Binds_SyncPrimCursor,PrimtiveArena_Used)),
load_word(R_T0, R_TapePtr, O_(Binds_SyncPrimCursor,PrimtiveBase)),
add_ui_1( R_TapePtr, S_(Binds_SyncPrimCursor)),
/* Calculate byte offset and store directly back to RAM */
sub_u(R_T0, R_PrimCur, R_T0),
store_word(R_T0, R_AT, 0),
sub_u(R_T0, R_PrimCur, R_T0), // R_T0 = PrimitiveArea_Used(R_AT) - R_PrimCur
store_word(R_T0, R_AT, 0), // PrimitiveBase(R_AT)[0] = R_T0
mac_yield()
};
@@ -195,6 +232,8 @@ internal MipsAtom_(rbind_cube_tri) {
load_word(R_VertBase, R_TapePtr, O_(Binds_CubeTri,VertBase)),
load_word(R_OtBase, R_TapePtr, O_(Binds_CubeTri,OtBase)),
add_ui_1( R_TapePtr, S_(Binds_CubeTri)),
// Note(Ed): This entire thing is argument shuffle?
// TODO(Ed): Eliminate
mac_yield()
};
@@ -345,46 +384,11 @@ internal MipsAtom_(rbind_floor_tri) {
load_word(R_VertBase, R_TapePtr, O_(Binds_FloorTri,VertBase)),
load_word(R_OtBase, R_TapePtr, O_(Binds_FloorTri,OtBase)),
add_ui_1( R_TapePtr, S_(Binds_FloorTri)),
// Note(Ed): This entire thing is argument shuffle?
// TODO(Ed): Eliminate
mac_yield()
};
internal MipsAtom_(floor_tri) {
mac_load_tri_indices(R_T0, R_T1, R_T2),
mac_load_tri_verts( R_T0, R_T1, R_T2),
/* 3. Execute Math */
nop, nop, gte_cmdw_rtpt,
nop, nop, gte_cmdw_nclip,
nop, nop,
/* 4. Culling (Branch forward 29 instructions if Backface) */
gte_mf(R_T0, C2_MAC0),
nop,
branch_le_zero(R_T0, 29),
nop,
/* 5. Format Primitive */
mac_format_prim_f3(0x20FF, 0xFFFF), /* High: 0x20/B, Low: G/R */
/* 6. Calculate Depth */
nop, nop, gte_cmdw_avsz3,
nop, nop,
gte_mf(R_T1, C2_OTZ),
/* 7. Bounds Check OTZ < 2048 (Branch forward 13 instructions to skip insertion) */
add_ui( R_AT, R_0, 2048),
slt_u( R_AT, R_T1, R_AT),
branch_equal(R_AT, R_0, 13),
nop,
/* 8. Insert into Ordering Table Linked List */
mac_insert_ot_tag(R_T1, 0x0400), /* Length = 4 words */
add_ui(R_PrimCur, R_PrimCur, 20), /* Advance Prim Cursor (5 words) */
/* 9. Advance Input Cursor & Yield (Both branch targets land here) */
add_ui(R_FaceCur, R_FaceCur, 8), /* Advance Face Cursor (4 * S2 = 8 bytes) */
mac_yield()
};
/* DIAGNOSTIC 1: Pure tape loop test */
internal MipsAtom_(diag_yield) { mac_yield() };
+18 -41
View File
@@ -69,6 +69,7 @@ enum {
/* Semantic Aliases for MIPS Registers (O32 ABI) */
, rdiscard = R_0 /* Hardwired to 0 */
, rasm_tmp = R_AT /* Assembler temporary (destroyed by some assembler pseudoinstructions!) */
, rret_0 = R_V0 /* Function return value */
, rret_1 = R_V1 /* Second return value (e.g., 64-bit) */
, rarg_0 = R_A0 /* First function argument */
@@ -112,21 +113,30 @@ enum {
, op_xori = 0x0E /* XOR Immediate */
, op_lui = 0x0F /* Load Upper Immediate */
, op_cop0 = 0x10 /* Coprocessor 0 (System) */
, op_cop1 = 0x11 /* Coprocessor 1 (Reserved, FP Unit, Omitted by Sony) */
, op_cop2 = 0x12 /* Coprocessor 2 (GTE) */
, op_la = 0
, op_li = 0
, op_cop3 = 0x13 /* Coprocessor 3 (Reserved, Unused)*/
/* 14-1F: N/A */
, op_lb = 0x20 /* Load Byte */
, op_lh = 0x21 /* Load Halfword */
, op_lwl = 0x22 /* Load Word (Left Bits) */
, op_lw = 0x23 /* Load Word */
, op_lbu = 0x24 /* Load Byte Unsigned */
, op_lhu = 0x25 /* Load Halfword Unsigned */
, op_lwr = 0x26 /* Load Word (Right Bits) */
/* 27: N/A */
, op_sb = 0x28 /* Store Byte */
, op_sh = 0x29 /* Store Halfword */
, op_swl = 0x2A /* Store Word (Left Bits) */
, op_sw = 0x2B /* Store Word */
/* 2C-2D: N/A */
, op_swr = 0x2E /* Store Word (Right Bits) */
/* 2F: N/A */
// , op_lwc0
, op_load_addr = op_la
, op_load_imm = op_li
// , op_load_addr = op_la
// , op_load_imm = op_li
, op_jump = op_j
, op_jump_nlink = op_jal
@@ -246,6 +256,10 @@ enum { _BitOffsets = 0
#define xor_i(rt, rs, imm) enc_i(op_xori, (rs), (rt), (imm))
#define load_ui(rt, imm) enc_i(op_lui, R_0, (rt), (imm))
#define load_u1 load_byte_u
#define load_u2 load_half_u
#define load_u4 load_word
// Ergonomic add to the same register.
#define add_ui_1(rt_rs, imm) enc_i(op_addiu, (rt_rs), (rt_rs), (imm))
@@ -468,39 +482,6 @@ enum { _BitOffsets = 0
} \
} while (0 )
// Binary Metaprogramming
typedef U4 const MipsCode;
#define MipsAtom_(sym) MipsCode tmpl(code,sym) [] align_(4) =
enum {
bios_flushcache = 0x44,
bios_table_addr = 0xA0,
};
/* Flushes the Instruction Cache (PSX A-function 0x44 via BIOS stub at 0xA0).
*
* Sequence (per MIPS ABI; arguments in arg registers, RA pushed to stack):
* 1. sp -= 8; sw $ra, 4($sp) ; save RA
* 2. $a0 = bios_flushcache (arg0)
* 3. $t0 = bios_table_addr ; t0 = &BIOS A-function table
* 4. jalr $t0, $ra ; call BIOS(flushcache)
* nop ; branch delay slot
* 5. lw $ra, 4($sp); jr $ra ; restore & return
* 6. sp += 8
*/
internal MipsAtom_(mips_flush_icache) {
add_ui(rstack_ptr, rstack_ptr, -8) /* sp -= 8 */
, store_word(rret_addr, rstack_ptr, 4) /* sw $ra, 4($sp) */
, add_ui(rret_0, rdiscard, bios_flushcache) /* addiu $a0, $0, 0x44 */
, add_ui(rtmp_0, rdiscard, bios_table_addr) /* addiu $t0, $0, 0xA0 */
, jump_link(rtmp_0, rret_addr) /* jalr $t0, $ra */
, nop /* BD slot */
, load_word(rret_addr, rstack_ptr, 4) /* lw $ra, 4($sp) */
, jump_reg(rret_addr) /* jr $ra */
, add_ui(rstack_ptr, rstack_ptr, 8) /* sp += 8 (BD) */
};
I_ void mips_flush_icache(void) { C_(VoidFn*, code_mips_flush_icache)(); }
/* Standard clobber list for pure-MIPS asm volatile blocks: caller-saved
* GPRs that the kernel treats as volatile (v0/v1/t0/t1/ra) plus the
@@ -519,7 +500,3 @@ I_ void mips_flush_icache(void) { C_(VoidFn*, code_mips_flush_icache)(); }
, jump_reg(rret_addr) \
, add_ui(rstack_ptr, rstack_ptr, 8) \
) asm_clobber: clb_system )
void test_mips_asm() {
asm_mips_flush_icache();
}
@@ -0,0 +1,18 @@
// Auto-generated by gen_atom_offsets.lua — DO NOT EDIT
// Source: C:\projects\Pikuma\ps1\code\gte_hello\hello_gte_tape.c
#ifndef HELLO_GTE_TAPE_OFFSETS_H
#define HELLO_GTE_TAPE_OFFSETS_H
#pragma region hello_gte_tape
// Override the placeholder atom_offset() to dispatch via token pasting.
#undef atom_offset
#define atom_offset(name) atom_offset_##name
// --- atom: floor_tri (49 words) ---
#define atom_offset_floor_tri_exit (17)
#pragma endregion hello_gte_tape
#endif // HELLO_GTE_TAPE_OFFSETS_H
+23 -17
View File
@@ -12,13 +12,14 @@
#include "duffle/mips.h"
#include "duffle/gp.h"
#include "duffle/gte.h"
#include "duffle/lottes_tape.h"
#include "hello_gte.h"
enum {
PrimitiveBuff_Len = 4096,
OrderingTbl_Len = 2048
};
# include "duffle/gen/lottes_tape.offsets.h"
#include "duffle/lottes_tape.h"
# include "tape_atom.metadata.h"
# include "gen/hello_gte_tape.offsets.h"
#include "hello_gte.h"
#include "hello_gte_tape.c"
typedef U4 OrderingTable_Buffer[OrderingTbl_Len];
typedef Array_(OrderingTable_Buffer, 2);
@@ -104,6 +105,8 @@ typedef Struct_(SMemory) {
Ent_Cube cube;
Ent_Floor floor;
};
global SMemory static_mem;
extern SMemory static_mem;
@@ -245,10 +248,10 @@ void update(PrimitiveArena* pa, U4* ordering_buf)
LP_ U4 mem_temp_tape[512]; FArena tape_arena; farena_init(& tape_arena, slice_ut_arr(mem_temp_tape));
TapeBuilder tb = tb_make(&tape_arena); tb_scope(& tb) {
tb_emit(& tb, code_rbind_cube_tri);
tb_data(& tb, prim_cursor);
tb_data(& tb, u4_(static_mem.cube.faces));
tb_data(& tb, u4_(static_mem.cube.verts));
tb_data(& tb, u4_(ordering_buf));
tb_data(& tb, prim_cursor);
tb_data(& tb, u4_(static_mem.cube.faces));
tb_data(& tb, u4_(static_mem.cube.verts));
tb_data(& tb, u4_(ordering_buf));
for (U4 i = 0; i < Cube_num_faces; i++) {
// Two triangles per quad face: (x,y,z) and (x,z,w)
@@ -256,8 +259,8 @@ void update(PrimitiveArena* pa, U4* ordering_buf)
}
tb_emit(& tb, code_sync_prim_cursor);
tb_data(& tb, u4_(& pa->used));
tb_data(& tb, prim_base);
tb_data(& tb, u4_(& pa->used));
tb_data(& tb, prim_base);
}
tape_run(tb_slice(tb));
@@ -334,21 +337,24 @@ void update(PrimitiveArena* pa, U4* ordering_buf)
LP_ U4 mem_temp_tape[512]; FArena tape_arena; farena_init(& tape_arena, slice_ut_arr(mem_temp_tape));
TapeBuilder tb = tb_make(&tape_arena); tb_scope(& tb) {
// Push "Protocol" to tape
tb_emit_(& tb, rbind_floor_tri);
tb_emit(& tb, code_rbind_floor_tri);
// Note(Ed): This is technically argument shuffle and would be better if we did a single reference at most to a global batch context.
// Note(Ed): We can technically allocate a single ptr with the global offset to the working context instead of utilizing the tape for this stack of refs.
tb_data(& tb, prim_cursor);
tb_data(& tb, u4_(static_mem.floor.faces));
tb_data(& tb, u4_(static_mem.floor.verts));
tb_data(& tb, u4_(ordering_buf));
tb_emit(& tb, code_set_gte_world);
tb_data(& tb, u4_(& static_mem.tform_world));
tb_data(& tb, u4_(& static_mem.tform_world));
for (U4 i = 0; i < Floor_num_faces; i++) {
tb_emit(& tb, code_floor_tri);
}
tb_emit(& tb, code_sync_prim_cursor);
tb_data(& tb, u4_(& pa->used));
tb_data(& tb, prim_base);
tb_data(& tb, u4_(& pa->used));
tb_data(& tb, prim_base);
}
// Fire off the tape.
+5
View File
@@ -5,6 +5,11 @@
# include "duffle/gp.h"
#endif
enum {
PrimitiveBuff_Len = 4096,
OrderingTbl_Len = 2048
};
typedef Struct_(DrawEnv_Packed) { U4 tag; U4 code[15]; };
typedef Struct_(DrawEnv) {
Rect_S2 clip_area;
+64
View File
@@ -0,0 +1,64 @@
#ifdef INTELLISENSE_DIRECTIVES
# include "duffle/lottes_tape.h"
# include "hello_gte.h"
# include "tape_atom.metadata.h"
# include "gen/hello_gte_tape.offsets.h"
#endif
#pragma region MACs
/* Words: 3; High: 0x20/B, Low: G/R */
#define mac_format_f3_color(color_hi, color_lo) \
load_ui(R_AT, color_hi), or_i(R_AT, R_AT, color_lo) \
, store_word(R_AT, R_PrimCur, O_(Poly_F3,color)) \
/* Words: 3 */
#define mac_gte_store_f3() \
gte_sw(C2_SXY0, R_PrimCur, O_(Poly_F3,p0)) \
, gte_sw(C2_SXY1, R_PrimCur, O_(Poly_F3,p1)) \
, gte_sw(C2_SXY2, R_PrimCur, O_(Poly_F3,p2))
#pragma endregion MACs
#pragma region Baked Atoms
internal MipsAtom_(floor_tri) {
// T0-T2 allocated
mac_load_tri_indices(R_T0, R_T1, R_T2),
mac_load_tri_verts( R_T0, R_T1, R_T2),
/* 3. Execute Math */
nop, nop, gte_cmdw_rotate_translate_perspective_triple,
nop, nop, gte_cmdw_nclip,
nop, nop,
/* 4. Culling (Branch forward 29 instructions if Backface) */
gte_mf(R_T0, C2_MAC0),
nop, branch_le_zero(R_T0, atom_offset(floor_tri_exit)),
nop,
/* 5. Format Primitive */
mac_format_f3_color(0x20FF, 0xFFFF),
mac_gte_store_f3(),
/* 6. Calculate Depth */
nop, nop, gte_avg_sort_z3,
nop, nop, gte_mf(R_T1, C2_OTZ),
/* 7. Bounds Check OTZ < 2048 (Branch forward 13 instructions to skip insertion) */
add_ui( R_AT, R_0, OrderingTbl_Len),
slt_u( R_AT, R_T1, R_AT),
branch_equal(R_AT, R_0, 13),
nop,
/* 8. Insert into Ordering Table Linked List */
mac_insert_ot_tag(R_T1, 0x0400),
add_ui(R_PrimCur, R_PrimCur, S_(Poly_F3)), /* Advance Prim Cursor (5 words) */
/* 9. Advance Input Cursor & Yield (Both branch targets land here) */
atom_label(floor_tri_exit)
add_ui(R_FaceCur, R_FaceCur, S_(S2) * 4), /* Advance Face Cursor (4 * S2 = 8 bytes) */
mac_yield()
};
#pragma endregion Baked Atoms
+39
View File
@@ -0,0 +1,39 @@
// tape_atom.metadata.h
// Single source of truth for instruction-word counts.
// Used by C (to define compile-time constants) AND Python (to count positions).
//
// Format: WORD_COUNT(MACRO_NAME, COUNT)
// One line per macro that appears in your atom sources.
//
// To regenerate: hand-count the instructions in each macro definition.
// (You'll only need to do this once per macro — they don't change often.)
#define WORD_COUNT(name, count) enum { words_##name = (count) };
WORD_COUNT(nop, 1)
WORD_COUNT(branch_le_zero, 1)
WORD_COUNT(branch_equal, 1)
WORD_COUNT(add_ui, 1)
WORD_COUNT(slt_u, 1)
WORD_COUNT(load_ui, 1)
WORD_COUNT(load_word, 1)
WORD_COUNT(load_half_u, 1)
WORD_COUNT(store_word, 1)
WORD_COUNT(gte_mf, 1)
WORD_COUNT(gte_mt, 1)
WORD_COUNT(gte_ct, 1)
WORD_COUNT(gte_sw, 1)
WORD_COUNT(gte_cmdw_rtpt, 1)
WORD_COUNT(gte_cmdw_nclip, 1)
WORD_COUNT(gte_avg_sort_z3, 1)
WORD_COUNT(mac_load_tri_indices, 3)
WORD_COUNT(mac_load_tri_verts, 18)
WORD_COUNT(mac_format_f3_color, 3)
WORD_COUNT(mac_gte_store_f3, 3)
WORD_COUNT(mac_insert_ot_tag, 11)
WORD_COUNT(mac_yield, 4)
#undef WORD_COUNT
// Used to define word markers for the lua metaprogram to calculate offsets from.
#define atom_label(sym)
// #define atom_offset(sym) // will be generated based on usage within a baked atom.
+51
View File
@@ -311,11 +311,60 @@ function build-graphis_hello {
}
# build-graphis_hello
function generate-TapeAtomOffsets {param(
[Parameter(Mandatory=$true)]
[string[]]$sources,
[Parameter(Mandatory=$true)]
[string]$metadata)
$gen_atom_offsets_script = join-path $path_scripts 'tape_attom.offset_gen.meta.lua'
$any_stale = $false
foreach ($src in $sources) {
$basename = [System.IO.Path]::GetFileNameWithoutExtension($src)
$dir = split-path -Path $src -Parent
$gen_dir = join-path $dir 'gen'
$out = join-path $gen_dir "$basename.offsets.h"
if (-not (test-path $out)) { $any_stale = $true; break }
$src_mtime = (get-item $src).LastWriteTimeUtc
$out_mtime = (get-item $out).LastWriteTimeUtc
$meta_mtime = (get-item $metadata).LastWriteTimeUtc
if (($src_mtime -gt $out_mtime) -or ($meta_mtime -gt $out_mtime)) {
$any_stale = $true
break
}
}
if (-not $any_stale) {
write-host "AtomOffs all $($sources.Count) source(s) up-to-date" -ForegroundColor DarkGray
return
}
write-host "AtomOffs $($sources.Count) source(s)" -ForegroundColor Magenta
& lua $gen_atom_offsets_script $metadata @sources
if ($LASTEXITCODE -ne 0) {
write-error "Atom offset generation failed. Aborting."
exit 1
}
}
function build-gte_hello {
$includes += @()
$path_module = join-path $path_code 'gte_hello'
$path_duffle = join-path $path_code 'duffle'
$path_gen = join-path $path_module 'gen'
$path_atom_metadata = join-path $path_module 'tape_atom.metadata.h'
$atom_sources = @(
(join-path $path_duffle 'mips.h'),
(join-path $path_duffle 'lottes_tape.h'),
(join-path $path_module 'hello_gte_tape.c')
)
generate-TapeAtomOffsets -sources $atom_sources -metadata $path_atom_metadata
$assemble_args = @()
$assemble_args += $f_debug
$assemble_args += $f_optimize_none
@@ -353,6 +402,8 @@ function build-gte_hello {
}
build-gte_hello
# NO idea if this works yet...
function Send-ToEmulator { param(
[string]$exePath
)
+725
View File
@@ -0,0 +1,725 @@
#!/usr/bin/env lua
-- gen_atom_offsets.lua
--
-- Finds every `MipsAtom_(name) { ... }` declaration in the given sources,
-- counts the words in each body using the WORD_COUNT manifest, computes
-- branch offsets for atom_label(name)/atom_offset(name) markers, and writes
-- one header per source into `<source_dir>/gen/<basename>.offsets.h`.
--
-- Usage:
-- lua gen_atom_offsets.lua <metadata.h> <source1> [source2 ...]
-- ============================================================
-- Character classification
-- ============================================================
local function is_space(c)
return c == " " or c == "\t" or c == "\n" or c == "\r" or c == "\v" or c == "\f"
end
local function is_alpha(c)
if not c or #c == 0 then return false end
if c >= "a" and c <= "z" then return true end
if c >= "A" and c <= "Z" then return true end
return c == "_"
end
local function is_digit(c)
return c and c >= "0" and c <= "9"
end
local function is_alnum(c)
return is_alpha(c) or is_digit(c)
end
-- ============================================================
-- I/O
-- ============================================================
local function read_file(path)
local f = io.open(path, "r")
if not f then error("Cannot open " .. path) end
local content = f:read("*a")
f:close()
return content
end
local function write_file(path, content)
local f = io.open(path, "w")
if not f then error("Cannot write " .. path) end
f:write(content)
f:close()
end
local function ensure_dir(path)
os.execute('mkdir -p "' .. path .. '"')
end
-- ============================================================
-- String primitives (no patterns)
-- ============================================================
local function trim(s)
local a = 1
while a <= #s and is_space(s:sub(a, a)) do a = a + 1 end
local b = #s
while b >= a and is_space(s:sub(b, b)) do b = b - 1 end
return s:sub(a, b)
end
local function starts_with(s, prefix)
if #s < #prefix then return false end
for i = 1, #prefix do
if s:sub(i, i) ~= prefix:sub(i, i) then return false end
end
return true
end
local function ends_with(s, suffix)
if #s < #suffix then return false end
local off = #s - #suffix
for i = 1, #suffix do
if s:sub(off + i, off + i) ~= suffix:sub(i, i) then return false end
end
return true
end
local function find_byte(haystack, target, start)
for i = start or 1, #haystack do
if haystack:sub(i, i) == target then return i end
end
return nil
end
local function dirname(path)
local last_sep = 0
for i = 1, #path do
local c = path:sub(i, i)
if c == "/" or c == "\\" then last_sep = i end
end
if last_sep == 0 then return "." end
return path:sub(1, last_sep - 1)
end
local function basename_no_ext(path)
local last_sep = 0
for i = 1, #path do
local c = path:sub(i, i)
if c == "/" or c == "\\" then last_sep = i end
end
local a = last_sep + 1
local last_dot = #path + 1
for i = #path, a, -1 do
if path:sub(i, i) == "." then last_dot = i; break end
end
return path:sub(a, last_dot - 1)
end
local function to_upper(s)
local out = ""
for i = 1, #s do
local code = string.byte(s, i)
if code >= 97 and code <= 122 then
out = out .. string.char(code - 32)
else
out = out .. s:sub(i, i)
end
end
return out
end
local function to_alnum_underscore(s)
local out = ""
for i = 1, #s do
local c = s:sub(i, i)
if is_alnum(c) then out = out .. c
else out = out .. "_" end
end
return out
end
local function pad_right(s, width)
while #s < width do s = s .. " " end
return s
end
-- ============================================================
-- Skip whitespace and comments
-- ============================================================
local function skip_ws_and_comments(source, i)
local len = #source
while i <= len do
local c = source:sub(i, i)
if is_space(c) then
i = i + 1
elseif c == "/" and source:sub(i+1, i+1) == "/" then
while i <= len and source:sub(i, i) ~= "\n" do i = i + 1 end
elseif c == "/" and source:sub(i+1, i+1) == "*" then
i = i + 2
while i <= len - 1 do
if source:sub(i, i) == "*" and source:sub(i+1, i+1) == "/" then
i = i + 2
break
end
i = i + 1
end
else
break
end
end
return i
end
-- ============================================================
-- Read identifier
-- ============================================================
local function read_ident(source, i)
if not is_alpha(source:sub(i, i)) then return nil, i end
local a = i
i = i + 1
while i <= #source and is_alnum(source:sub(i, i)) do i = i + 1 end
return source:sub(a, i - 1), i
end
-- ============================================================
-- Read balanced (open_char, close_char) group, return inner + new pos
-- Skips strings and comments inside.
-- ============================================================
local function read_balanced(source, open_char, close_char, i)
if source:sub(i, i) ~= open_char then return nil, i end
i = i + 1
local len = #source
local depth = 1
local a = i
while i <= len and depth > 0 do
local c = source:sub(i, i)
if c == open_char then
depth = depth + 1
i = i + 1
elseif c == close_char then
depth = depth - 1
if depth == 0 then break end
i = i + 1
elseif c == '"' then
i = i + 1
while i <= len do
if source:sub(i, i) == "\\" then i = i + 2
elseif source:sub(i, i) == '"' then i = i + 1; break
else i = i + 1 end
end
elseif c == "'" then
i = i + 1
while i <= len do
if source:sub(i, i) == "\\" then i = i + 2
elseif source:sub(i, i) == "'" then i = i + 1; break
else i = i + 1 end
end
elseif c == "/" and source:sub(i+1, i+1) == "/" then
while i <= len and source:sub(i, i) ~= "\n" do i = i + 1 end
elseif c == "/" and source:sub(i+1, i+1) == "*" then
i = i + 2
while i <= len - 1 do
if source:sub(i, i) == "*" and source:sub(i+1, i+1) == "/" then
i = i + 2
break
end
i = i + 1
end
else
i = i + 1
end
end
return source:sub(a, i - 1), i + 1
end
local function read_parens(source, i) return read_balanced(source, "(", ")", i) end
local function read_braces(source, i) return read_balanced(source, "{", "}", i) end
local function read_brackets(source, i) return read_balanced(source, "[", "]", i) end
-- ============================================================
-- Scan forward from `start`, skipping balanced (), [], {}, strings, comments.
-- Returns position of first occurrence of `target` char at top level, or nil.
-- ============================================================
local function scan_to_char(source, target, start)
local len = #source
local i = start
while i <= len do
local c = source:sub(i, i)
if c == target then
return i
elseif c == "(" then
local _, after = read_parens(source, i); i = after
elseif c == "{" then
local _, after = read_braces(source, i); i = after
elseif c == "[" then
local _, after = read_brackets(source, i); i = after
elseif c == '"' then
i = i + 1
while i <= len do
if source:sub(i, i) == "\\" then i = i + 2
elseif source:sub(i, i) == '"' then i = i + 1; break
else i = i + 1 end
end
elseif c == "'" then
i = i + 1
while i <= len do
if source:sub(i, i) == "\\" then i = i + 2
elseif source:sub(i, i) == "'" then i = i + 1; break
else i = i + 1 end
end
elseif c == "/" and source:sub(i+1, i+1) == "/" then
while i <= len and source:sub(i, i) ~= "\n" do i = i + 1 end
elseif c == "/" and source:sub(i+1, i+1) == "*" then
i = i + 2
while i <= len - 1 do
if source:sub(i, i) == "*" and source:sub(i+1, i+1) == "/" then
i = i + 2
break
end
i = i + 1
end
else
i = i + 1
end
end
return nil
end
-- ============================================================
-- Load WORD_COUNT manifest from metadata.h
-- ============================================================
local function load_word_counts(metadata_path)
local counts = {}
local content = read_file(metadata_path)
local len = #content
local i = 1
local prefix = "WORD_COUNT("
while i <= len do
local nl = find_byte(content, "\n", i)
local line_end = nl or (len + 1)
local line = content:sub(i, line_end - 1)
local trimmed = trim(line)
if starts_with(trimmed, prefix) and ends_with(trimmed, ")") then
local inner = trimmed:sub(#prefix + 1, #trimmed - 1)
local comma = find_byte(inner, ",", 1)
if comma then
local name = trim(inner:sub(1, comma - 1))
local cnt = trim(inner:sub(comma + 1))
counts[name] = tonumber(cnt)
end
end
i = line_end + 1
end
return counts
end
-- ============================================================
-- Count words for a single comma-separated token
-- ============================================================
local function word_count_of_token(token, word_counts)
local i = 1
local len = #token
while i <= len and is_space(token:sub(i, i)) do i = i + 1 end
if i > len then return 0 end
local name, after = read_ident(token, i)
if not name then return 1 end
local j = skip_ws_and_comments(token, after)
if token:sub(j, j) == "(" then
local wc = word_counts[name]
if wc then return wc end
io.stderr:write(" warning: unknown macro '" .. name .. "', assuming 1 word\n")
return 1
end
return 1
end
-- ============================================================
-- Split brace-body into top-level comma-separated tokens
-- ============================================================
local function split_top_level_commas(body)
local tokens = {}
local len = #body
local i = 1
local token_start = 1
while i <= len do
local c = body:sub(i, i)
if c == "(" then
local _, after = read_parens(body, i); i = after
elseif c == "{" then
local _, after = read_braces(body, i); i = after
elseif c == "[" then
local _, after = read_brackets(body, i); i = after
elseif c == '"' then
i = i + 1
while i <= len do
if body:sub(i, i) == "\\" then i = i + 2
elseif body:sub(i, i) == '"' then i = i + 1; break
else i = i + 1 end
end
elseif c == "'" then
i = i + 1
while i <= len do
if body:sub(i, i) == "\\" then i = i + 2
elseif body:sub(i, i) == "'" then i = i + 1; break
else i = i + 1 end
end
elseif c == "/" and body:sub(i+1, i+1) == "/" then
while i <= len and body:sub(i, i) ~= "\n" do i = i + 1 end
elseif c == "/" and body:sub(i+1, i+1) == "*" then
i = i + 2
while i <= len - 1 do
if body:sub(i, i) == "*" and body:sub(i+1, i+1) == "/" then
i = i + 2
break
end
i = i + 1
end
elseif c == "," then
table.insert(tokens, body:sub(token_start, i - 1))
i = i + 1
token_start = i
else
i = i + 1
end
end
local last = body:sub(token_start, len)
if trim(last) ~= "" then
table.insert(tokens, last)
end
return tokens
end
-- ============================================================
-- Scan an atom body for atom_label/atom_offset markers, count words
-- ============================================================
local function scan_for_atom_markers(token, at_pos, labels, branches)
local i = 1
local len = #token
while i <= len do
i = skip_ws_and_comments(token, i)
if i > len then break end
local c = token:sub(i, i)
if c == '"' then
-- Skip string literal
i = i + 1
while i <= len do
if token:sub(i, i) == "\\" then i = i + 2
elseif token:sub(i, i) == '"' then i = i + 1; break
else i = i + 1 end
end
elseif c == "'" then
-- Skip char literal
i = i + 1
while i <= len do
if token:sub(i, i) == "\\" then i = i + 2
elseif token:sub(i, i) == "'" then i = i + 1; break
else i = i + 1 end
end
elseif c == "/" and token:sub(i+1, i+1) == "/" then
while i <= len and token:sub(i, i) ~= "\n" do i = i + 1 end
elseif c == "/" and token:sub(i+1, i+1) == "*" then
i = i + 2
while i <= len - 1 do
if token:sub(i, i) == "*" and token:sub(i+1, i+1) == "/" then
i = i + 2
break
end
i = i + 1
end
elseif is_alpha(c) then
local ident, after = read_ident(token, i)
if ident == "atom_label" or ident == "atom_offset" then
local arg_start = skip_ws_and_comments(token, after)
if token:sub(arg_start, arg_start) == "(" then
local inner, after_paren = read_parens(token, arg_start)
local n = 1
while n <= #inner and is_space(inner:sub(n, n)) do n = n + 1 end
local ns = n
while n <= #inner and is_alnum(inner:sub(n, n)) do n = n + 1 end
local name = inner:sub(ns, n - 1)
if name ~= "" then
if ident == "atom_label" then
labels[name] = at_pos
else
table.insert(branches, {pos = at_pos, target = name})
end
end
i = after_paren
else
i = arg_start
end
else
i = after
end
else
-- Anything else (parens, brackets, braces, commas, operators) — walk past
i = i + 1
end
end
end
local function scan_atom_body(body, word_counts)
local pos = 0
local labels = {}
local branches = {}
for _, tok in ipairs(split_top_level_commas(body)) do
local k = 1
local tlen = #tok
while k <= tlen and is_space(tok:sub(k, k)) do k = k + 1 end
local leading_ident, leading_after = read_ident(tok, k)
if leading_ident == "atom_label" or leading_ident == "atom_offset" then
scan_for_atom_markers(tok, pos, labels, branches)
else
local words = word_count_of_token(tok, word_counts)
scan_for_atom_markers(tok, pos, labels, branches)
pos = pos + words
end
end
return labels, branches, pos
end
-- ============================================================
-- Token classification for atom detection
-- ============================================================
-- Skip past storage-class / qualifier noise. These appear before MipsCode
-- in raw expanded forms: `static`, `const`, the user's `internal`/`LP_`/
-- `global` macros (which all expand to `static`), `RO_` (which expands to
-- a section attribute), plus standard C qualifiers.
local function skip_qualifiers(source, i)
local keywords = {
["static"]=true, ["const"]=true, ["volatile"]=true,
["extern"]=true, ["register"]=true, ["auto"]=true,
["inline"]=true, ["typedef"]=true,
["internal"]=true, ["LP_"]=true, ["global"]=true, ["gkknown"]=true
}
while true do
i = skip_ws_and_comments(source, i)
local ident, after = read_ident(source, i)
if not ident then return i end
if keywords[ident] then
i = after
else
return i
end
end
end
-- Test whether `s` starts with literal `prefix` (no patterns).
local function has_prefix(s, prefix)
if #s < #prefix then return false end
for i = 1, #prefix do
if s:sub(i, i) ~= prefix:sub(i, i) then return false end
end
return true
end
-- ============================================================
-- Find every atom declaration in a source, both wrapped and raw
-- ============================================================
local function find_atoms(source_text)
local atoms = {}
local len = #source_text
local i = 1
-- First, scan inside source_text normally
local function try_wrapped_form(ident_pos)
local paren_pos = skip_ws_and_comments(source_text, ident_pos)
if source_text:sub(paren_pos, paren_pos) ~= "(" then
return nil -- not a MipsAtom_() call
end
local inner, after_paren = read_parens(source_text, paren_pos)
-- Extract name (first identifier from inner)
local n = 1
while n <= #inner and is_space(inner:sub(n, n)) do n = n + 1 end
local ns = n
while n <= #inner and is_alnum(inner:sub(n, n)) do n = n + 1 end
local name = inner:sub(ns, n - 1)
if name == "" then return nil end
local brace_pos = scan_to_char(source_text, "{", after_paren)
if not brace_pos then return nil end
local body, after_brace = read_braces(source_text, brace_pos)
return {name = name, body = body, after_brace = after_brace}
end
local function try_raw_form(after_type_pos)
local next_pos = skip_ws_and_comments(source_text, after_type_pos)
local next_ident, next_after = read_ident(source_text, next_pos)
if not next_ident then return nil end
if not has_prefix(next_ident, "code_") then return nil end
if #next_ident <= 5 then return nil end -- bare "code_" — not an atom
local atom_name = next_ident:sub(6)
local brace_pos = scan_to_char(source_text, "{", next_after)
if not brace_pos then return nil end
local body, after_brace = read_braces(source_text, brace_pos)
return {name = atom_name, body = body, after_brace = after_brace}
end
while i <= len do
i = skip_ws_and_comments(source_text, i)
if i > len then break end
-- Skip past storage-class noise
i = skip_qualifiers(source_text, i)
if i > len then break end
local ident, after = read_ident(source_text, i)
if not ident then
i = i + 1
elseif ident == "MipsAtom_" then
local atom = try_wrapped_form(after)
if atom then
table.insert(atoms, {name = atom.name, body = atom.body})
i = atom.after_brace
else
i = i + 1 -- not actually MipsAtom_(), skip and continue
end
elseif ident == "MipsCode" then
local atom = try_raw_form(after)
if atom then
table.insert(atoms, {name = atom.name, body = atom.body})
i = atom.after_brace
else
i = after -- some other MipsCode use; skip just this token
end
else
-- Anything else: skip just this identifier. The next loop
-- iteration will see whatever follows (might be more qualifiers,
-- another type keyword, etc.).
i = after
end
end
return atoms
end
-- ============================================================
-- Compute branch offsets: target - branch - 1
-- ============================================================
local function compute_offsets(labels, branches)
local results = {}
for _, br in ipairs(branches) do
local target = labels[br.target]
if not target then
error("Branch target '" .. br.target .. "' has no atom_label (at word " .. br.pos .. ")")
end
table.insert(results, {target = br.target, offset = target - br.pos - 1})
end
return results
end
-- ============================================================
-- Generate header for one source
-- ============================================================
local function generate_header(source_path, atoms_data)
local basename = basename_no_ext(source_path)
local guard = to_alnum_underscore(to_upper(basename)) .. "_OFFSETS_H"
local lines = {}
local function add(s) table.insert(lines, s) end
add("// Auto-generated by gen_atom_offsets.lua — DO NOT EDIT")
add("// Source: " .. source_path)
add("#ifndef " .. guard)
add("#define " .. guard)
add("")
add("#pragma region " .. basename)
add("")
add("// Override the placeholder atom_offset() to dispatch via token pasting.")
add("#undef atom_offset")
add("#define atom_offset(name) atom_offset_##name")
add("")
for _, atom in ipairs(atoms_data) do
add("// --- atom: " .. atom.name .. " (" .. atom.total_words .. " words) ---")
add("")
for _, r in ipairs(atom.offsets) do
local const_name = "atom_offset_" .. r.target
add("#define " .. pad_right(const_name, 40) .. " (" .. r.offset .. ")")
end
add("")
end
add("#pragma endregion " .. basename)
add("")
add("#endif // " .. guard)
return table.concat(lines, "\n") .. "\n"
end
-- ============================================================
-- Process one source
-- ============================================================
local function process_source(source_path, word_counts)
local source = read_file(source_path)
local atoms_raw = find_atoms(source)
if #atoms_raw == 0 then
io.stderr:write(" note: no MipsAtom_ declarations in " .. source_path .. "\n")
return
end
local atoms_data = {}
for _, atom in ipairs(atoms_raw) do
local labels, branches, total = scan_atom_body(atom.body, word_counts)
local offsets = compute_offsets(labels, branches)
table.insert(atoms_data, {
name = atom.name,
total_words = total,
offsets = offsets
})
end
local basename = basename_no_ext(source_path)
local out_dir = dirname(source_path) .. "/gen"
ensure_dir(out_dir)
local out_path = out_dir .. "/" .. basename .. ".offsets.h"
write_file(out_path, generate_header(source_path, atoms_data))
local total_branches = 0
for _, a in ipairs(atoms_data) do total_branches = total_branches + #a.offsets end
print(" " .. basename .. ": " .. #atoms_data .. " atom(s), " .. total_branches .. " branch(es)")
for _, a in ipairs(atoms_data) do
for _, r in ipairs(a.offsets) do
print(" " .. a.name .. " -> " .. r.target .. " : " .. r.offset)
end
end
end
-- ============================================================
-- Main
-- ============================================================
local function main(args)
if #args < 2 then
print("Usage: gen_atom_offsets.lua <metadata.h> <source1> [source2 ...]")
os.exit(1)
end
local metadata_path = args[1]
local sources = {}
for i = 2, #args do table.insert(sources, args[i]) end
local word_counts = load_word_counts(metadata_path)
for _, src in ipairs(sources) do process_source(src, word_counts) end
end
main({...})