mirror of
https://github.com/Ed94/pikuma_ps1.git
synced 2026-09-22 17:20:04 +00:00
Compare commits
17
Commits
de13bc3ce9
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5f49c071d1 | ||
|
|
e6cd2539d8 | ||
|
|
0e9034cbd4 | ||
|
|
1faf3539d8 | ||
|
|
6b3fbab387 | ||
|
|
2c38a73709 | ||
|
|
159ead625e | ||
|
|
52888015de | ||
|
|
a37ffe6f58 | ||
|
|
b61610d819 | ||
|
|
1b950ab5b5 | ||
|
|
b2858b3c73 | ||
|
|
f1801343e2 | ||
|
|
85b2205603 | ||
|
|
2d754650c9 | ||
|
|
e2ffe538b6 | ||
|
|
223d1832eb |
@@ -30,7 +30,7 @@
|
||||
"editorHoverWidget.background": "#2c334b"
|
||||
},
|
||||
"semanticTokenColors": {
|
||||
"comment": { "foreground": "#868686", "fontStyle": "italic" },
|
||||
"comment": { "foreground": "#868686", }, //"fontStyle": "italic" },
|
||||
"keyword": { "foreground": "#d8bd5b" },
|
||||
"string": { "foreground": "#d46a54" },
|
||||
"number": { "foreground": "#b5cea8" },
|
||||
@@ -81,7 +81,7 @@
|
||||
"tapeDelaySlot": { "foreground": "#ff5647" }
|
||||
},
|
||||
"tokenColors": [
|
||||
{ "scope": ["comment", "comment.block", "comment.line", "comment.block.documentation"], "settings": { "foreground": "#868686", "fontStyle": "italic" } },
|
||||
{ "scope": ["comment", "comment.block", "comment.line", "comment.block.documentation"], "settings": { "foreground": "#868686", } }, //"fontStyle": "italic" } },
|
||||
{ "scope": ["keyword", "keyword.control", "keyword.other"], "settings": { "foreground": "#d8bd5b" } },
|
||||
{ "scope": ["string", "string.quoted"], "settings": { "foreground": "#d46a54" } },
|
||||
{ "scope": ["string.quoted.other"], "settings": { "foreground": "#d69d85" } },
|
||||
|
||||
+74
-29
@@ -21,7 +21,7 @@ const TOKEN_TYPES = [
|
||||
"tapeGprRegister",
|
||||
"tapeCop2Register",
|
||||
"tapeDuffleType",
|
||||
"tapeAttribute",
|
||||
"tapeAt__ibute",
|
||||
"keyword",
|
||||
"macro",
|
||||
];
|
||||
@@ -33,23 +33,59 @@ const TOKEN_MODIFIER_INDEX = new Map(TOKEN_MODIFIERS.map((name, index) => [name,
|
||||
const ATOM_KEYWORDS = new Set(["MipsAtom_", "MipsAtom_Proc_"]);
|
||||
const COMPONENT_KEYWORDS = new Set(["MipsAtomComp_", "MipsAtomComp_Proc_"]);
|
||||
const ANNOTATIONS = new Set([
|
||||
"atom_info", "atom_bind", "atom_reads", "atom_writes", "atom_label",
|
||||
"atom_offset", "atom_reg", "atom_type", "atom_ctx", "atom_phase",
|
||||
"atom_auto_reg", "phase_auto_reg", "atom_dbg_skip",
|
||||
"atom_info",
|
||||
"atom_bind",
|
||||
"atom_reads",
|
||||
"atom_writes",
|
||||
"atom_label",
|
||||
"atom_offset",
|
||||
"atom_reg",
|
||||
"atom_type",
|
||||
"atom_ctx",
|
||||
"atom_phase",
|
||||
"atom_auto_reg",
|
||||
"phase_auto_reg",
|
||||
"atom_dbg_skip",
|
||||
]);
|
||||
|
||||
const DSL_KEYWORDS = new Set([
|
||||
"FI_", "I_", "NI_", "Relative_", "Struct_", "Enum_", "Union_", "Array_",
|
||||
"Slice_", "TypeR_", "TypeV_", "align_", "internal", "local_persist", "global",
|
||||
"RO_", "LP_", "gknown", "expect_", "cexpr_",
|
||||
"enum", "struct", "union",
|
||||
"offset_of", "static_assert", "typeof", "typeof_ptr", "typeof_same",
|
||||
"glue", "tmpl",
|
||||
"A_", "FI_", "I_", "NI_",
|
||||
"Array_", "Enum_", "Proc_", "Relative_", "Struct_", "Union_", "Slice_",
|
||||
|
||||
// "TypeR_", "TypeV_",
|
||||
|
||||
"align_",
|
||||
"internal", "local_persist", "global",
|
||||
|
||||
"RO_", "LP_",
|
||||
"gknown", "expect_", "cexpr_",
|
||||
|
||||
"O_", "OA_", "S_", "C_", "T_", "T_same", "R_", "V_",
|
||||
"r_", "v_", "rt_", "vt_",
|
||||
|
||||
"asm", "asm_words", "asm_rpins", "asm_clobber",
|
||||
"O_", "S_", "C_", "T_", "tmpl", "glue", "r_", "v_", "tr_", "tv_",
|
||||
"rgcc", "r_use", "r_set", "r_mod", "r_imm", "r_mem",
|
||||
"u1_", "u2_", "u4_", "u8_", "s1_", "s2_", "s4_", "s8_",
|
||||
"u1_r", "u2_r", "u4_r", "u8_r", "u1_v", "u2_v", "u4_v", "u8_v",
|
||||
|
||||
"b1_", "b2_", "b4_", "b8_",
|
||||
"u1_", "u2_", "u4_", "u8_",
|
||||
"s1_", "s2_", "s4_", "s8_",
|
||||
"b1_r", "b2_r", "b4_r", "b8_r",
|
||||
"b1_v", "b2_v", "b4_v", "b8_v",
|
||||
"u1_r", "u2_r", "u4_r", "u8_r",
|
||||
"u1_v", "u2_v", "u4_v", "u8_v",
|
||||
|
||||
"u4_lo", "u4_hi",
|
||||
]);
|
||||
|
||||
const DELAY_SLOT_KEYWORDS = new Set(["LdSlot_", "BdSlot_", "DmaSlot_", "GteDelay_"]);
|
||||
const DELAY_SLOT_KEYWORDS = new Set([
|
||||
"LdSlot_",
|
||||
"BdSlot_",
|
||||
"DmaSlot_",
|
||||
"GteDelay_"
|
||||
]);
|
||||
|
||||
const CONTROL_FLOW_PREFIXES = /^(?:branch_|jump_|call_)/;
|
||||
|
||||
@@ -100,10 +136,8 @@ function modifierMask(modifiers) {
|
||||
}
|
||||
|
||||
function isRegUseAccess(tokens, tokenIndex) {
|
||||
const prev = tokens[tokenIndex - 1];
|
||||
if (!prev || prev.text !== ".") return false;
|
||||
const prevPrev = tokens[tokenIndex - 2];
|
||||
if (!prevPrev || prevPrev.kind !== "identifier") return false;
|
||||
const prev = tokens[tokenIndex - 1]; if (!prev || prev.text !== ".") return false;
|
||||
const prevPrev = tokens[tokenIndex - 2]; if (!prevPrev || prevPrev.kind !== "identifier") return false;
|
||||
const next = tokens[tokenIndex + 1];
|
||||
if (next && next.text === ".") return false;
|
||||
if (prevPrev.text === "r") return true;
|
||||
@@ -120,8 +154,7 @@ function classifyDocument(source, filePath, workspaceIndex, shouldCancel = () =>
|
||||
|
||||
for (let tokenIndex = 0; tokenIndex < scanned.tokens.length; tokenIndex += 1) {
|
||||
if (shouldCancel()) break;
|
||||
const token = scanned.tokens[tokenIndex];
|
||||
if (token.kind !== "identifier") continue;
|
||||
const token = scanned.tokens[tokenIndex]; if (token.kind !== "identifier") continue;
|
||||
|
||||
let type = null;
|
||||
let modifiers = [];
|
||||
@@ -131,37 +164,49 @@ function classifyDocument(source, filePath, workspaceIndex, shouldCancel = () =>
|
||||
if (declaration) {
|
||||
type = ROLE_TO_TYPE[declaration.role] || null;
|
||||
modifiers = declaration.modifiers.slice();
|
||||
} else if (ATOM_KEYWORDS.has(token.text)) {
|
||||
}
|
||||
else if (ATOM_KEYWORDS.has(token.text)) {
|
||||
type = "tapeAtomKeyword";
|
||||
} else if (COMPONENT_KEYWORDS.has(token.text)) {
|
||||
}
|
||||
else if (COMPONENT_KEYWORDS.has(token.text)) {
|
||||
type = "keyword";
|
||||
} else if (ANNOTATIONS.has(token.text)) {
|
||||
}
|
||||
else if (ANNOTATIONS.has(token.text)) {
|
||||
type = "tapeAnnotation";
|
||||
} else if (context && context.callee === "atom_bind" && context.argIndex === 0) {
|
||||
}
|
||||
else if (context && context.callee === "atom_bind" && context.argIndex === 0) {
|
||||
type = "tapeBindType";
|
||||
} else if (context && context.callee === "atom_phase" && context.argIndex === 0) {
|
||||
}
|
||||
else if (context && context.callee === "atom_phase" && context.argIndex === 0) {
|
||||
type = "tapePhase";
|
||||
modifiers = ["declaration"];
|
||||
} else if (context && context.callee === "atom_ctx" && context.argIndex === 0) {
|
||||
}
|
||||
else if (context && context.callee === "atom_ctx" && context.argIndex === 0) {
|
||||
type = "tapeAtomName";
|
||||
} else if (context && context.callee === "atom_label" && context.argIndex === 0) {
|
||||
}
|
||||
else if (context && context.callee === "atom_label" && context.argIndex === 0) {
|
||||
type = "tapeLabel";
|
||||
modifiers = ["declaration"];
|
||||
} else if (context && context.callee === "atom_offset" && context.argIndex <= 1) {
|
||||
}
|
||||
else if (context && context.callee === "atom_offset" && context.argIndex <= 1) {
|
||||
type = "tapeLabel";
|
||||
} else if (context && context.callee === "atom_reads") {
|
||||
}
|
||||
else if (context && context.callee === "atom_reads") {
|
||||
type = registerType(token.text, index);
|
||||
if (type) modifiers = ["tapeRead"];
|
||||
} else if (context && context.callee === "atom_writes") {
|
||||
}
|
||||
else if (context && context.callee === "atom_writes") {
|
||||
type = registerType(token.text, index);
|
||||
if (type) modifiers = ["tapeWrite"];
|
||||
} else if (context && context.callee === "atom_auto_reg") {
|
||||
}
|
||||
else if (context && context.callee === "atom_auto_reg") {
|
||||
if (context.argIndex === 0) type = "tapeAtomName";
|
||||
if (context.argIndex === 1) {
|
||||
type = "tapeGprRegister";
|
||||
modifiers = ["declaration", "tapeAuto"];
|
||||
}
|
||||
} else if (context && context.callee === "phase_auto_reg") {
|
||||
}
|
||||
else if (context && context.callee === "phase_auto_reg") {
|
||||
if (context.argIndex === 0) type = "tapePhase";
|
||||
if (context.argIndex === 1) {
|
||||
type = "tapeGprRegister";
|
||||
|
||||
+7
-3
@@ -39,7 +39,8 @@ async function activate(context) {
|
||||
const result = scanSource(source, uri.fsPath);
|
||||
nextIndex = mergeIndexes(nextIndex, result.index);
|
||||
for (const error of result.errors) output.appendLine(formatError(uri.fsPath, error));
|
||||
} catch (error) {
|
||||
}
|
||||
catch (error) {
|
||||
output.appendLine(`${uri.fsPath}: ${error.stack || error.message || error}`);
|
||||
}
|
||||
}
|
||||
@@ -55,7 +56,9 @@ async function activate(context) {
|
||||
debounceHandle = setTimeout(() => {
|
||||
debounceHandle = null;
|
||||
rebuildIndex().catch((error) => output.appendLine(error.stack || String(error)));
|
||||
}, 100);
|
||||
},
|
||||
100
|
||||
);
|
||||
}
|
||||
|
||||
const provider = {
|
||||
@@ -77,7 +80,8 @@ async function activate(context) {
|
||||
output.appendLine(formatError(document.uri.fsPath || document.uri.toString(), error));
|
||||
}
|
||||
return builder.build();
|
||||
} catch (error) {
|
||||
}
|
||||
catch (error) {
|
||||
output.appendLine(`${document.uri}: ${error.stack || error.message || error}`);
|
||||
return new vscode.SemanticTokensBuilder(legend).build();
|
||||
}
|
||||
|
||||
Vendored
+14
-7
@@ -10,7 +10,8 @@ function isIdentifierContinue(code) {
|
||||
return isIdentifierStart(code) || (code >= 48 && code <= 57);
|
||||
}
|
||||
|
||||
function lex(source) {
|
||||
function lex(source)
|
||||
{
|
||||
if (typeof source !== "string") throw new TypeError("source must be a string");
|
||||
|
||||
const tokens = [];
|
||||
@@ -47,7 +48,8 @@ function lex(source) {
|
||||
});
|
||||
}
|
||||
|
||||
while (offset < source.length) {
|
||||
while (offset < source.length)
|
||||
{
|
||||
const ch = source[offset];
|
||||
|
||||
if (/\s/.test(ch)) {
|
||||
@@ -60,12 +62,14 @@ function lex(source) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch === "/" && source[offset + 1] === "*") {
|
||||
if (ch === "/" && source[offset + 1] === "*")
|
||||
{
|
||||
const start = offset;
|
||||
advance();
|
||||
advance();
|
||||
let closed = false;
|
||||
while (offset < source.length) {
|
||||
while (offset < source.length)
|
||||
{
|
||||
if (source[offset] === "*" && source[offset + 1] === "/") {
|
||||
advance();
|
||||
advance();
|
||||
@@ -78,12 +82,14 @@ function lex(source) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch === "\"" || ch === "'") {
|
||||
if (ch === "\"" || ch === "'")
|
||||
{
|
||||
const quote = ch;
|
||||
const start = offset;
|
||||
advance();
|
||||
let closed = false;
|
||||
while (offset < source.length) {
|
||||
while (offset < source.length)
|
||||
{
|
||||
if (source[offset] === "\\") {
|
||||
advance();
|
||||
if (offset < source.length) advance();
|
||||
@@ -134,7 +140,8 @@ function buildCallContexts(tokens) {
|
||||
if (token.text === ")") {
|
||||
if (stack.length === 0) {
|
||||
errors.push({ kind: "unmatched-close-paren", offset: token.start });
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
const frame = stack.pop();
|
||||
if (frame.callee !== null) calls.push({ ...frame, closeTokenIndex: tokenIndex });
|
||||
}
|
||||
|
||||
+20
-11
@@ -4,8 +4,10 @@ const path = require("node:path");
|
||||
const { buildCallContexts, lex, nearestCall } = require("./lexer");
|
||||
|
||||
const BASE_TYPES = [
|
||||
"B1", "B2", "B4", "B8", "F4", "F8", "S1", "S2", "S4", "S8",
|
||||
"U1", "U2", "U4", "U8", "MipsAtom", "MipsCode", "Reg",
|
||||
"B1", "B2", "B4", "B8",
|
||||
"F4", "F8", "S1", "S2", "S4", "S8",
|
||||
"U1", "U2", "U4", "U8",
|
||||
"MipsAtom", "MipsCode", "Reg",
|
||||
];
|
||||
|
||||
const C_BUILTINS = new Set([
|
||||
@@ -15,11 +17,13 @@ const C_BUILTINS = new Set([
|
||||
]);
|
||||
|
||||
const BASE_ATTRIBUTES = [
|
||||
"FI_", "I_", "NI_", "Relative_", "Struct_", "Enum_", "Union_", "Array_",
|
||||
"Slice_", "TypeR_", "TypeV_", "align_", "internal", "local_persist", "global",
|
||||
"RO_", "LP_", "gknown", "expect_", "cexpr_",
|
||||
"FI_", "I_", "NI_",
|
||||
"Relative_", "Struct_", "Enum_", "Union_", "Array_", "Slice_",
|
||||
"align_", "internal", "local_persist", "global",
|
||||
"RO_", "LP_",
|
||||
"gknown", "expect_", "cexpr_",
|
||||
"asm", "asm_words", "asm_rpins", "asm_clobber",
|
||||
"O_", "S_", "C_", "T_", "tmpl", "glue", "r_", "v_", "tr_", "tv_",
|
||||
"O_", "OA_", "S_", "C_", "T_", "tmpl", "glue", "r_", "v_", "rt_", "vt_",
|
||||
"rgcc", "r_use", "r_set", "r_mod", "r_imm", "r_mem",
|
||||
"u1_", "u2_", "u4_", "u8_", "s1_", "s2_", "s4_", "s8_",
|
||||
"u1_r", "u2_r", "u4_r", "u8_r", "u1_v", "u2_v", "u4_v", "u8_v",
|
||||
@@ -161,7 +165,8 @@ function findFunctionNameBefore(tokens, calleeTokenIndex) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function scanSource(source, filePath) {
|
||||
function scanSource(source, filePath)
|
||||
{
|
||||
const lexical = lex(source);
|
||||
const balanced = buildCallContexts(lexical.tokens);
|
||||
const tokens = lexical.tokens;
|
||||
@@ -191,7 +196,8 @@ function scanSource(source, filePath) {
|
||||
if (!index.macros.has(alias)) index.macros.set(alias, "component");
|
||||
}
|
||||
|
||||
for (let tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 1) {
|
||||
for (let tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 1)
|
||||
{
|
||||
const token = tokens[tokenIndex];
|
||||
if (token.kind !== "identifier") continue;
|
||||
|
||||
@@ -240,12 +246,14 @@ function scanSource(source, filePath) {
|
||||
mark(token, "gprRegister", ["declaration", "tapeAuto"]);
|
||||
}
|
||||
|
||||
if (token.text === "define" && tokens[tokenIndex - 1] && tokens[tokenIndex - 1].text === "#") {
|
||||
if (token.text === "define" && tokens[tokenIndex - 1] && tokens[tokenIndex - 1].text === "#")
|
||||
{
|
||||
const name = tokens[tokenIndex + 1];
|
||||
if (name && name.kind === "identifier" && name.line === token.line) {
|
||||
if (/^(?:RegUse_|Struct_|Enum_|Union_|TypeR_|TypeV_|Relative_|Binds_)/.test(name.text)) {
|
||||
index.types.add(name.text);
|
||||
} else if (/^(?:ac_|mac_)/.test(name.text)) {
|
||||
}
|
||||
else if (/^(?:ac_|mac_)/.test(name.text)) {
|
||||
const alias = name.text.startsWith("ac_") ? componentAlias(name.text) : name.text;
|
||||
const rest = [];
|
||||
for (let restIndex = tokenIndex + 2; restIndex < tokens.length && tokens[restIndex].line === name.line; restIndex += 1) {
|
||||
@@ -256,7 +264,8 @@ function scanSource(source, filePath) {
|
||||
index.macros.set(alias, prefixDomain(alias) || "component");
|
||||
if (rest.length) index.componentCallees.set(alias, rest);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
index.macros.set(name.text, domain || "utility");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
*
|
||||
* Annotation rules
|
||||
* ----------------
|
||||
* 1. atom_info(...) is OPTIONAL. Atoms without atom_info are silently skipped by the metaprogram.
|
||||
* 1. atom_info(...) is optional. Atoms without atom_info are silently skipped by the metaprogram.
|
||||
* 2. If present, atom_info takes up to three sub-calls, all order-independent within the arg list:
|
||||
* - atom_bind(Binds_X)
|
||||
* - atom_reads(...)
|
||||
@@ -160,6 +160,8 @@
|
||||
* ----------------------------------------------------------------------------*/
|
||||
#define atom_bind(binds_struct) /* atom_bind(binds_struct) */
|
||||
|
||||
#define Binds_(type) (tmpl(Binds,type)) // TODO(Ed): Do we want to use this?
|
||||
|
||||
/* ============================================================================
|
||||
* atom_label / atom_offset — branch target machinery
|
||||
*
|
||||
|
||||
+16
-5
@@ -29,8 +29,7 @@
|
||||
|
||||
#define asm __asm__
|
||||
|
||||
#define A_(data) (& data)
|
||||
#define align_(value) __attribute__((aligned (value))) // for easy alignment
|
||||
#define A_(data) (& (data))
|
||||
#define align_(value) __attribute__((aligned (value))) // for easy alignment
|
||||
#define C_(type,data) ((type)(data)) // for enforced precedence
|
||||
#define expect_(x, y) __builtin_expect(x, y) // so compiler knows the common path
|
||||
@@ -45,7 +44,8 @@
|
||||
#define R_ restrict
|
||||
#define V_ volatile
|
||||
|
||||
#pragma region Fictional //, used for intiution
|
||||
#pragma region Fictional
|
||||
//, used for intiution
|
||||
|
||||
#define EUB_ restrict // Execute Unit Bound: Data is siloed in the ALU Register File. The Load/Store Unit is bypassed. (Route to Execution Unit. Keep in registers)
|
||||
#define ISO_ restrict // Isolated Provenance: Alternative to Exu_. Guarantees electrical memory isolation,
|
||||
@@ -83,8 +83,8 @@
|
||||
|
||||
#define r_(ptr) C_(T_(ptr[0])*R_, ptr) // Constrain pointer to restrict
|
||||
#define v_(ptr) C_(T_(ptr[0])V_*, ptr) //
|
||||
#define tr_(type, ptr) C_(type *R_, ptr)
|
||||
#define tv_(type, ptr) C_(type V_*, ptr)
|
||||
#define rt_(type, ptr) C_(type *R_, ptr)
|
||||
#define vt_(type, ptr) C_(type V_*, ptr)
|
||||
|
||||
#define TypeR_(type) type *R_ type ## _R // type *restrict type_R
|
||||
#define TypeV_(type) type V_* type ## _V // type volatile* type_V
|
||||
@@ -120,6 +120,10 @@ typedef unsigned char TSet_(B1);
|
||||
typedef __UINT16_TYPE__ TSet_(B2);
|
||||
typedef __UINT32_TYPE__ TSet_(B4);
|
||||
|
||||
#define b1_(value) C_(B1, value)
|
||||
#define b2_(value) C_(B2, value)
|
||||
#define b4_(value) C_(B4, value)
|
||||
|
||||
#define u1_(value) C_(U1, value)
|
||||
#define u2_(value) C_(U2, value)
|
||||
#define u4_(value) C_(U4, value)
|
||||
@@ -127,6 +131,13 @@ typedef __UINT32_TYPE__ TSet_(B4);
|
||||
#define s2_(value) C_(S2, value)
|
||||
#define s4_(value) C_(S4, value)
|
||||
|
||||
#define b1_r(value) C_(B1*R_, value)
|
||||
#define b2_r(value) C_(B2*R_, value)
|
||||
#define b4_r(value) C_(B4*R_, value)
|
||||
#define b1_v(value) C_(B1 V_*, value)
|
||||
#define b2_v(value) C_(B2 V_*, value)
|
||||
#define b4_v(value) C_(B4 V_*, value)
|
||||
|
||||
#define u1_r(value) C_(U1 *R_, value)
|
||||
#define u2_r(value) C_(U2 *R_, value)
|
||||
#define u4_r(value) C_(U4 *R_, value)
|
||||
|
||||
@@ -79,14 +79,6 @@
|
||||
* Why bundle the `__asm__()` wrapper?
|
||||
* - The integer R_T4 (= 12, via R_T4_Code) already indicates the register.
|
||||
* - The string "$12" is derived from it via reg_str, so they cannot drift apart.
|
||||
* - Spelling `__asm__(reg_str(R_T4_Code))` at every call site is noise.
|
||||
*
|
||||
* tmpl defined in dsl.h (token-paste glue).
|
||||
* rgcc define here (gcc_asm.h) because the `__asm__` keyword is GCC-specific.
|
||||
* Anyone porting to a different compiler's asm dialect overrides rgcc,
|
||||
* and the integer→string derivation in rlit can be retargeted in one place.
|
||||
*
|
||||
* For clobber lists and asm-template strings, use the bare `rlit(R_T4_Code)`.
|
||||
* ------------------------------------------------------------------------ */
|
||||
#define rgcc(n) __asm__(rlit(n))
|
||||
|
||||
|
||||
+25
-18
@@ -13,7 +13,7 @@
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\gte.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\pad.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\dsl.atom.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\lottes_tape.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\tape.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\bios.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\psyq.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\pad.c
|
||||
@@ -35,6 +35,7 @@
|
||||
* These do NOT yield. They are expanded inline inside Tape Atoms.
|
||||
* ---------------------------------------------------------------------------*/
|
||||
// The 'Yield' sequence for Tape Atoms (mac_yield).
|
||||
// In Forth this is considered the "NEXT" mechanism.
|
||||
#define mac_yield(...) \
|
||||
load_word(R_AtomJmp, R_TapePtr, 0) \
|
||||
LdSlot_ \
|
||||
@@ -55,6 +56,12 @@ WORD_COUNT(mac_yield_load, 1)
|
||||
, BdSlot_ nop
|
||||
WORD_COUNT(mac_yield_tail, 3)
|
||||
|
||||
/* atom_dbg_skip */
|
||||
#define mac_yield_to(code_ptr) \
|
||||
jump_reg(code_ptr) \
|
||||
, BdSlot_ nop
|
||||
WORD_COUNT(mac_yield_to, 2)
|
||||
|
||||
/* atom_dbg_skip */
|
||||
#define mac_load_half_v3(tx, ty, tz, base, offset) \
|
||||
load_half(tx, base, offset + OA_(U2,[0])) \
|
||||
@@ -228,21 +235,21 @@ WORD_COUNT(mac_gte_op_cross_v3s4, 13)
|
||||
WORD_COUNT(mac_gte_store_f3, 3)
|
||||
|
||||
/* atom_dbg_skip */
|
||||
#define mac_gte_load_tri_verts(r_vert_base, r_v0, r_v1, r_v2) \
|
||||
shift_lleft(R_AT, r_v0, v3s2_byteoff) \
|
||||
, add_u_self(R_AT, r_vert_base) \
|
||||
#define mac_gte_load_tri_verts(vbase, v0, v1, v2) \
|
||||
shift_lleft(R_AT, v0, v3s2_byteoff) \
|
||||
, add_u_self(R_AT, vbase) \
|
||||
, load_word(R_V0, R_AT, O_(V3_S2,x)) \
|
||||
, load_word(R_V1, R_AT, O_(V3_S2,z)) \
|
||||
, LdSlot_ gte_mv_to_data_r(R_V0, C2_VXY0) \
|
||||
, gte_mv_to_data_r(R_V1, C2_VZ0) \
|
||||
, shift_lleft(R_AT, r_v1, v3s2_byteoff) \
|
||||
, add_u_self(R_AT, r_vert_base) \
|
||||
, shift_lleft(R_AT, v1, v3s2_byteoff) \
|
||||
, add_u_self(R_AT, vbase) \
|
||||
, load_word(R_V0, R_AT, O_(V3_S2,x)) \
|
||||
, load_word(R_V1, R_AT, O_(V3_S2,z)) \
|
||||
, LdSlot_ gte_mv_to_data_r(R_V0, C2_VXY1) \
|
||||
, gte_mv_to_data_r(R_V1, C2_VZ1) \
|
||||
, shift_lleft(R_AT, r_v2, v3s2_byteoff) \
|
||||
, add_u_self(R_AT, r_vert_base) \
|
||||
, shift_lleft(R_AT, v2, v3s2_byteoff) \
|
||||
, add_u_self(R_AT, vbase) \
|
||||
, load_word(R_V0, R_AT, O_(V3_S2,x)) \
|
||||
, load_word(R_V1, R_AT, O_(V3_S2,z)) \
|
||||
, LdSlot_ gte_mv_to_data_r(R_V0, C2_VXY2) \
|
||||
@@ -270,10 +277,10 @@ WORD_COUNT(mac_gte_store_g4_p3, 1)
|
||||
WORD_COUNT(mac_gte_sqr_v3, 8)
|
||||
|
||||
/* atom_dbg_skip */
|
||||
#define mac_gte_sqr_v3s4(r_sx, r_sy, r_sz, delay_slot) \
|
||||
gte_mv_to_data_r(r_sx, C2_IR1) \
|
||||
, gte_mv_to_data_r(r_sy, C2_IR2) \
|
||||
, gte_mv_to_data_r(r_sz, C2_IR3) \
|
||||
#define mac_gte_sqr_v3s4(sx, sy, sz, delay_slot) \
|
||||
gte_mv_to_data_r(sx, C2_IR1) \
|
||||
, gte_mv_to_data_r(sy, C2_IR2) \
|
||||
, gte_mv_to_data_r(sz, C2_IR3) \
|
||||
, delay_slot \
|
||||
, gte_cmdw_sqr
|
||||
WORD_COUNT(mac_gte_sqr_v3s4, 5)
|
||||
@@ -304,12 +311,12 @@ WORD_COUNT(mac_gte_gpf_scale, 13)
|
||||
WORD_COUNT(mac_trans_mt3s3s4, 6)
|
||||
|
||||
/* atom_dbg_skip */
|
||||
#define mac_lzcr_round_even_half_shift(r_shift, r_mag_sq, r_mag_sq_copy) \
|
||||
and_i(r_shift, r_shift, gte_lzcr_even_mask) \
|
||||
, or_u(r_mag_sq_copy, r_mag_sq, 0) \
|
||||
, li_s( r_mag_sq, 31) \
|
||||
, sub_s( r_mag_sq, r_mag_sq, r_shift) \
|
||||
, shift_aright(r_mag_sq, r_mag_sq, 1)
|
||||
#define mac_lzcr_round_even_half_shift(shift, mag_sq, mag_sq_copy) \
|
||||
and_i(shift, shift, gte_lzcr_even_mask) \
|
||||
, or_u(mag_sq_copy, mag_sq, 0) \
|
||||
, li_s( mag_sq, 31) \
|
||||
, sub_s( mag_sq, mag_sq, shift) \
|
||||
, shift_aright(mag_sq, mag_sq, 1)
|
||||
WORD_COUNT(mac_lzcr_round_even_half_shift, 5)
|
||||
|
||||
#define mac_gte_general_purpose_interopolation(to_ir0, to_ir1, to_ir2, to_ir3, fr_mac1, fr_mac2, fr_mac3, nop_slot1, nop_slot2) \
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\gte.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\pad.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\dsl.atom.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\lottes_tape.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\tape.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\bios.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\psyq.h
|
||||
// source: C:\projects\Pikuma\ps1\code\duffle\pad.c
|
||||
@@ -33,7 +33,7 @@ enum {
|
||||
atom_offset_example_atom_proc_skip = _atom_offset_example_atom_proc_skip,
|
||||
};
|
||||
|
||||
// --- atom: build_normalize_v3s4 (63 words) ---
|
||||
// --- atom: normalize_v3s4 (62 words) ---
|
||||
|
||||
#define _atom_offset_aligned_done_srav_path 3
|
||||
#define _atom_offset_srav_path_aligned_done 4
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
#ifdef INTELLISENSE_DIRECTIVES
|
||||
# include "dsl.h"
|
||||
# include "gp.h"
|
||||
# include "lottes_tape.h"
|
||||
# include "tape.h"
|
||||
#endif
|
||||
|
||||
ATOM_FILE_DEBUGGER_LINE_MARKER(gp_atom_c);
|
||||
|
||||
#pragma region MACs (Mips Atom Components)
|
||||
|
||||
FI_ Slice_MipsCode ac_gcmd_push(AtomBuilder_R ab, U4 cmd, U4 reg_transfer, U4 reg_base, U2 port)
|
||||
FI_ Slice_MipsCode ac_gcmd_push(AtomBuilder_R ab, U2 cmd, Reg reg_transfer, Reg reg_base, U2 port)
|
||||
atom_dbg_skip MipsAtomComp_Proc_(ab, {
|
||||
mac_load_word_imm(reg_transfer, cmd),
|
||||
store_word( reg_transfer, reg_base, port),
|
||||
|
||||
+20
-35
@@ -6,16 +6,6 @@
|
||||
* Primitive commands: gp0_cmd_poly_f3 = 0x20 (byte opcode)
|
||||
* Packed 32-bit cmd: gp0_word_poly_f3(r, g, b) (32-bit, shifted)
|
||||
*
|
||||
* Type ordering: domain?_(direction)?_action_target_modifier_type?
|
||||
* Examples: add_ui (add + unsigned + immediate)
|
||||
* add_s (add + signed, R-type implicit)
|
||||
* shift_lleft (shift + logical + left)
|
||||
* shift_aright (shift + arithmetic + right)
|
||||
* call_reg(rs) (call + register, $ra implicit)
|
||||
* gte_mv_to_data_r (gte + mv + to + data + register)
|
||||
* gte_lw_v0_xy(base) (gte + lw + v0 + xy)
|
||||
* load_upper_i (load-upper + immediate, unique verb)
|
||||
*
|
||||
* --- GPU-domain layer cake ---
|
||||
* Every gp.h macro follows the same 4-layer composition as mips.h and gte.h:
|
||||
* 4. Semantic encoders gp0_word_poly_f3(r,g,b)
|
||||
@@ -23,9 +13,6 @@
|
||||
* 2. Per-field encoders enc_gp0_color_r(r), enc_gp0_color_g(g), ...
|
||||
* 1. Bitfield layout consts gp0_color_red_pos = 0, gp0_color_red_width = 8
|
||||
* 0. Opcode IDs gp0_cmd_poly_f3 = 0x20
|
||||
*
|
||||
* Vendor mnemonics (gte_mtc2, gte_mfc2, etc.) are NOT in this header.
|
||||
* They live in the opt-in `gp_vendor_sym.h` for users who prefer the PSYQ-style names.
|
||||
* ============================================================================ */
|
||||
|
||||
#ifdef INTELLISENSE_DIRECTIVES
|
||||
@@ -391,17 +378,15 @@ enum {
|
||||
* Primitive structs (8 polygon variants + tag)
|
||||
* ============================================================================
|
||||
* Each struct follows the GPU-documented memory layout for the corresponding primitive command.
|
||||
* The PolyTag is the OT-link header; the rest of the struct is the primitive's body.
|
||||
* PolyTag is an OT-link header. Rest of the struct is the primitive's body.
|
||||
*
|
||||
* The current working layouts match the existing demo
|
||||
* (floor_tri uses Poly_F3; cube_tri uses Poly_G4).
|
||||
* They are NOT necessarily byte-identical to the PSX-SPX reference layout.
|
||||
* The demo layout uses color+vertex interleaving that doesn't match the standard PSX SDK file format.
|
||||
* For PSX-SDK file compatibility, the textured variants (FT*, GT*) would need layout adjustments.
|
||||
* ============================================================================ */
|
||||
|
||||
/* ---------- RGB8 (3-byte packed color) ---------- */
|
||||
typedef Struct_(RGB8) { B1 r; B1 g; B1 b; };
|
||||
typedef Struct_(RGB8) { U1 r; U1 g; U1 b; };
|
||||
#define rgb8(r,g,b) ((RGB8){r,g,b})
|
||||
|
||||
/* ---------- PolyTag (the OT-link header; 1 word) ---------- */
|
||||
@@ -431,7 +416,7 @@ typedef Struct_(PolyTag) {
|
||||
typedef Struct_(Poly_F3) {
|
||||
U4 tag;
|
||||
RGB8 color;
|
||||
B1 code;
|
||||
U1 code;
|
||||
union {
|
||||
struct { V2_S2 p0; V2_S2 p1; V2_S2 p2; };
|
||||
A3_V2_S2 points;
|
||||
@@ -442,7 +427,7 @@ typedef Struct_(Poly_F3) {
|
||||
typedef Struct_(Poly_F4) {
|
||||
U4 tag;
|
||||
RGB8 color;
|
||||
B1 code;
|
||||
U1 code;
|
||||
union {
|
||||
struct { V2_S2 p0; V2_S2 p1; V2_S2 p2; V2_S2 p3; };
|
||||
A4_V2_S2 points;
|
||||
@@ -451,18 +436,18 @@ typedef Struct_(Poly_F4) {
|
||||
|
||||
/* ---------- Poly_G3 (Gouraud Triangle; 7 words) ---------- */
|
||||
typedef Struct_(Poly_G3) {
|
||||
U4 tag; RGB8 c0; B1 code;
|
||||
V2_S2 p0; RGB8 c1; B1 pad1;
|
||||
V2_S2 p1; RGB8 c2; B1 pad2;
|
||||
U4 tag; RGB8 c0; U1 code;
|
||||
V2_S2 p0; RGB8 c1; U1 pad1;
|
||||
V2_S2 p1; RGB8 c2; U1 pad2;
|
||||
V2_S2 p2;
|
||||
};
|
||||
|
||||
/* ---------- Poly_G4 (Gouraud Quad; 9 words) ---------- */
|
||||
typedef Struct_(Poly_G4) {
|
||||
U4 tag; RGB8 c0; B1 code;
|
||||
V2_S2 p0; RGB8 c1; B1 pad1;
|
||||
V2_S2 p1; RGB8 c2; B1 pad2;
|
||||
V2_S2 p2; RGB8 c3; B1 pad3;
|
||||
U4 tag; RGB8 c0; U1 code;
|
||||
V2_S2 p0; RGB8 c1; U1 pad1;
|
||||
V2_S2 p1; RGB8 c2; U1 pad2;
|
||||
V2_S2 p2; RGB8 c3; U1 pad3;
|
||||
V2_S2 p3;
|
||||
};
|
||||
|
||||
@@ -471,7 +456,7 @@ typedef Struct_(Poly_G4) {
|
||||
typedef Struct_(Poly_FT3) {
|
||||
U4 tag;
|
||||
RGB8 color;
|
||||
B1 code;
|
||||
U1 code;
|
||||
U4 tpage;
|
||||
U4 clut;
|
||||
V2_S2 p0; U1 u0; U1 v0;
|
||||
@@ -483,7 +468,7 @@ typedef Struct_(Poly_FT3) {
|
||||
typedef Struct_(Poly_FT4) {
|
||||
U4 tag;
|
||||
RGB8 color;
|
||||
B1 code;
|
||||
U1 code;
|
||||
U4 tpage;
|
||||
U4 clut;
|
||||
V2_S2 p0; U1 u0; U1 v0;
|
||||
@@ -494,9 +479,9 @@ typedef Struct_(Poly_FT4) {
|
||||
|
||||
/* ---------- Poly_GT3 (Gouraud Textured Triangle) ---------- */
|
||||
typedef Struct_(Poly_GT3) {
|
||||
U4 tag; RGB8 c0; B1 code;
|
||||
V2_S2 p0; RGB8 c1; B1 pad1;
|
||||
V2_S2 p1; RGB8 c2; B1 pad2;
|
||||
U4 tag; RGB8 c0; U1 code;
|
||||
V2_S2 p0; RGB8 c1; U1 pad1;
|
||||
V2_S2 p1; RGB8 c2; U1 pad2;
|
||||
V2_S2 p2;
|
||||
U4 tpage;
|
||||
U4 clut;
|
||||
@@ -507,10 +492,10 @@ typedef Struct_(Poly_GT3) {
|
||||
|
||||
/* ---------- Poly_GT4 (Gouraud Textured Quad) ---------- */
|
||||
typedef Struct_(Poly_GT4) {
|
||||
U4 tag; RGB8 c0; B1 code;
|
||||
V2_S2 p0; RGB8 c1; B1 pad1;
|
||||
V2_S2 p1; RGB8 c2; B1 pad2;
|
||||
V2_S2 p2; RGB8 c3; B1 pad3;
|
||||
U4 tag; RGB8 c0; U1 code;
|
||||
V2_S2 p0; RGB8 c1; U1 pad1;
|
||||
V2_S2 p1; RGB8 c2; U1 pad2;
|
||||
V2_S2 p2; RGB8 c3; U1 pad3;
|
||||
V2_S2 p3;
|
||||
U4 tpage;
|
||||
U4 clut;
|
||||
|
||||
+120
-91
@@ -3,7 +3,8 @@
|
||||
# include "gen/offsets.h"
|
||||
# include "gte.h"
|
||||
# include "gp.h"
|
||||
# include "lottes_tape.h"
|
||||
# include "tape.h"
|
||||
# include "math.atom.h"
|
||||
#endif
|
||||
|
||||
ATOM_FILE_DEBUGGER_LINE_MARKER(gte_atom_c);
|
||||
@@ -63,10 +64,10 @@ FI_ Slice_MipsCode ac_gte_store_f3(AtomBuilder_R ab, U4 r_primitive_cursor) atom
|
||||
})
|
||||
|
||||
/* Words: 18; Translates indices to vertex addresses and pushes them to GTE */
|
||||
I_ Slice_MipsCode ac_gte_load_tri_verts(AtomBuilder_R ab, U4 r_vert_base, U4 r_v0, U4 r_v1, U4 r_v2) atom_dbg_skip MipsAtomComp_Proc_(ab, {
|
||||
shift_lleft(R_AT, r_v0, v3s2_byteoff), add_u_self(R_AT, r_vert_base), load_word(R_V0, R_AT, O_(V3_S2,x)), load_word(R_V1, R_AT, O_(V3_S2,z)), LdSlot_ gte_mv_to_data_r(R_V0, C2_VXY0), gte_mv_to_data_r(R_V1, C2_VZ0),
|
||||
shift_lleft(R_AT, r_v1, v3s2_byteoff), add_u_self(R_AT, r_vert_base), load_word(R_V0, R_AT, O_(V3_S2,x)), load_word(R_V1, R_AT, O_(V3_S2,z)), LdSlot_ gte_mv_to_data_r(R_V0, C2_VXY1), gte_mv_to_data_r(R_V1, C2_VZ1),
|
||||
shift_lleft(R_AT, r_v2, v3s2_byteoff), add_u_self(R_AT, r_vert_base), load_word(R_V0, R_AT, O_(V3_S2,x)), load_word(R_V1, R_AT, O_(V3_S2,z)), LdSlot_ gte_mv_to_data_r(R_V0, C2_VXY2), gte_mv_to_data_r(R_V1, C2_VZ2),
|
||||
I_ Slice_MipsCode ac_gte_load_tri_verts(AtomBuilder_R ab, Reg vbase, Reg v0, Reg v1, Reg v2) atom_dbg_skip MipsAtomComp_Proc_(ab, {
|
||||
shift_lleft(R_AT, v0, v3s2_byteoff), add_u_self(R_AT, vbase), load_word(R_V0, R_AT, O_(V3_S2,x)), load_word(R_V1, R_AT, O_(V3_S2,z)), LdSlot_ gte_mv_to_data_r(R_V0, C2_VXY0), gte_mv_to_data_r(R_V1, C2_VZ0),
|
||||
shift_lleft(R_AT, v1, v3s2_byteoff), add_u_self(R_AT, vbase), load_word(R_V0, R_AT, O_(V3_S2,x)), load_word(R_V1, R_AT, O_(V3_S2,z)), LdSlot_ gte_mv_to_data_r(R_V0, C2_VXY1), gte_mv_to_data_r(R_V1, C2_VZ1),
|
||||
shift_lleft(R_AT, v2, v3s2_byteoff), add_u_self(R_AT, vbase), load_word(R_V0, R_AT, O_(V3_S2,x)), load_word(R_V1, R_AT, O_(V3_S2,z)), LdSlot_ gte_mv_to_data_r(R_V0, C2_VXY2), gte_mv_to_data_r(R_V1, C2_VZ2),
|
||||
})
|
||||
|
||||
/* Words: 3; Stores the 3 transformed (V2_S2 screen) vertices of the
|
||||
@@ -96,11 +97,11 @@ FI_ Slice_MipsCode ac_gte_sqr_v3(AtomBuilder_R ab, U4 r_sx, U4 r_sy, U4 r_sz, U4
|
||||
})
|
||||
|
||||
/* ─── SQR FIRE — mtc2 3 GPRs into IR1/IR2/IR3, then fire SQR. ─── */
|
||||
FI_ Slice_MipsCode ac_gte_sqr_v3s4(AtomBuilder_R ab, Reg r_sx, Reg r_sy, Reg r_sz, MipsCode delay_slot)
|
||||
FI_ Slice_MipsCode ac_gte_sqr_v3s4(AtomBuilder_R ab, Reg sx, Reg sy, Reg sz, MipsCode delay_slot)
|
||||
atom_dbg_skip MipsAtomComp_Proc_(ab, {
|
||||
gte_mv_to_data_r(r_sx, C2_IR1),
|
||||
gte_mv_to_data_r(r_sy, C2_IR2),
|
||||
gte_mv_to_data_r(r_sz, C2_IR3),
|
||||
gte_mv_to_data_r(sx, C2_IR1),
|
||||
gte_mv_to_data_r(sy, C2_IR2),
|
||||
gte_mv_to_data_r(sz, C2_IR3),
|
||||
delay_slot, gte_cmdw_sqr,
|
||||
})
|
||||
|
||||
@@ -157,16 +158,13 @@ FI_ Slice_MipsCode ac_trans_mt3s3s4(AtomBuilder_R ab
|
||||
*
|
||||
* Note: C2_LZCR (cop2r31) is a fixed read-only C2 data register — the caller must read it via mfc2 from C2_LZCR;
|
||||
* there is no register choice at the hardware level. Only the GPR that holds the result is caller-determined. */
|
||||
FI_ Slice_MipsCode ac_lzcr_round_even_half_shift(AtomBuilder_R ab,
|
||||
U4 r_shift,
|
||||
U4 r_mag_sq,
|
||||
U4 r_mag_sq_copy)
|
||||
FI_ Slice_MipsCode ac_lzcr_round_even_half_shift(AtomBuilder_R ab, Reg shift, Reg mag_sq, Reg mag_sq_copy)
|
||||
atom_dbg_skip MipsAtomComp_Proc_(ab, {
|
||||
and_i(r_shift, r_shift, gte_lzcr_even_mask),
|
||||
or_u(r_mag_sq_copy, r_mag_sq, 0),
|
||||
li_s( r_mag_sq, 31),
|
||||
sub_s( r_mag_sq, r_mag_sq, r_shift),
|
||||
shift_aright(r_mag_sq, r_mag_sq, 1),
|
||||
and_i(shift, shift, gte_lzcr_even_mask),
|
||||
or_u(mag_sq_copy, mag_sq, 0),
|
||||
li_s( mag_sq, 31),
|
||||
sub_s( mag_sq, mag_sq, shift),
|
||||
shift_aright(mag_sq, mag_sq, 1),
|
||||
})
|
||||
|
||||
FI_ Slice_MipsCode ac_gte_general_purpose_interopolation(AtomBuilder_R ab
|
||||
@@ -186,59 +184,94 @@ MipsAtomComp_Proc_(ab, {
|
||||
gte_mv_from_data_r(fr_mac3, C2_MAC3),
|
||||
})
|
||||
|
||||
FI_ Slice_MipsCode ac_gte_mv_from_data_r_mac123(AtomBuilder_R ab
|
||||
, Reg fr_mac1, Reg fr_mac2, Reg fr_mac3)
|
||||
FI_ Slice_MipsCode ac_gte_mv_from_data_r_mac123(AtomBuilder_R ab, Reg fr_mac1, Reg fr_mac2, Reg fr_mac3)
|
||||
MipsAtomComp_Proc_(ab, {
|
||||
gte_mv_from_data_r(fr_mac1, C2_MAC1),
|
||||
gte_mv_from_data_r(fr_mac2, C2_MAC2),
|
||||
gte_mv_from_data_r(fr_mac3, C2_MAC3),
|
||||
})
|
||||
|
||||
|
||||
FI_ Slice_MipsCode ac_gte_mv_from_mac123_v3s4(AtomBuilder_R ab, Reg_(V3_S4) v) MipsAtomComp_ProcMap_(ab, mac_gte_mv_from_data_r_mac123(v.x, v.y, v.z))
|
||||
|
||||
#pragma endregion MACs (Mips Atom Components)
|
||||
|
||||
#pragma region Atom Procs
|
||||
|
||||
/* ─── Local copy of PSYQ's sqrtbl (1/sqrt lookup table for VectorNormal). ───
|
||||
* Source: PSYQ 4.7 libgte sqrtbl at 0x800185B4 in hello_camera.elf.
|
||||
* objdump -s --start-address=0x800185B4 --stop-address=0x800185F4 hello_camera.elf → 192 entries × 16-bit signed, in 1.12 fixed-point (max value 0x1000 = 1.0).
|
||||
/* Normalize V3_S4 using the PSYQ/libgte reciprocal-sqrt method:
|
||||
* |v|² = x² + y² + z²
|
||||
* LZCR determines the exponent of |v|².
|
||||
* Round that exponent even and shift |v|² into [1, 4).
|
||||
* sqrtbl approximates 1/sqrt(mantissa).
|
||||
* GPF multiplies v by that reciprocal-sqrt mantissa.
|
||||
* srav_shift restores the exponent scale.
|
||||
* Effectively: v_normalized = v * (1 / sqrt(|v|²)).
|
||||
*
|
||||
* ─── Local port of PSYQ's sqrtbl (1/sqrt lookup table for VectorNormal). ───
|
||||
* Source: PSYQ 4.7 libgte sqrtbl at 0x800185B4 in hello_camera.elf.
|
||||
* objdump -s --start-address=0x800185B4 --stop-address=0x800185F4 hello_camera.elf -> 192 entries x 16-bit signed, stored in 1.12 fixed point.
|
||||
* Data is identical to the libgte original (byte-for-byte verified).
|
||||
*
|
||||
* ─── Per-entry semantics (decoded from libgte msc02 VectorNormal) ───
|
||||
* Each entry is `1/sqrt(x)` in 1.12 fixed point (value / 4096).
|
||||
* The 192 entries span 4 octaves of the input magnitude, with 48 entries per octave:
|
||||
* Octave 0 (entries 0- 47): mantissa in [0x8000, 0x10000) output ~[1.000, 0.707]
|
||||
* Octave 1 (entries 48- 95): mantissa in [0x10000, 0x20000) output ~[0.707, 0.500]
|
||||
* Octave 2 (entries 96-143): mantissa in [0x20000, 0x40000) output ~[0.500, 0.354]
|
||||
* Octave 3 (entries144-191): mantissa in [0x40000, 0x80000) output ~[0.354, 0.251]
|
||||
* Within each octave, 8 sub-entries interpolate over the 8 fractional bits of the mantissa
|
||||
* (the byte `(0x80 | (i mod 8))` for the lower-byte of the aligned value).
|
||||
* Sampling the first value of each octave:
|
||||
* [0] 0x1000 = 1.0000 ; 1 / sqrt(1.0000)
|
||||
* [48] 0x0e4f = 0.8940 ; 1 / sqrt(1.2500)
|
||||
* [96] 0x0d10 = 0.8164 ; 1 / sqrt(1.5000)
|
||||
* [144] 0x0c0a = 0.7520 ; 1 / sqrt(1.7500)
|
||||
* And representative sub-entries within octave 0 (mantissa in [0x8000, 0x8100)):
|
||||
* [0] 0x1000 = 1.0000 ; 1 / sqrt(0x8000)
|
||||
* [1] 0x0fe0 = 0.9922 ; 1 / sqrt(0x8100)
|
||||
* [2] 0x0fc1 = 0.9846 ; 1 / sqrt(0x8200)
|
||||
* [3] 0x0fa3 = 0.9773 ; 1 / sqrt(0x8300)
|
||||
* [4] 0x0f85 = 0.9700 ; 1 / sqrt(0x8400)
|
||||
* [5] 0x0f68 = 0.9629 ; 1 / sqrt(0x8500)
|
||||
* [6] 0x0f4c = 0.9561 ; 1 / sqrt(0x8600)
|
||||
* [7] 0x0f30 = 0.9492 ; 1 / sqrt(0x8700)
|
||||
* ─── Table semantics ───
|
||||
* For table index i in [0, 192):
|
||||
* x = 1 + i / 64
|
||||
* tbl[i] = floor(4096 / sqrt(x))
|
||||
* Thus the table uniformly samples 1/sqrt(x) over:
|
||||
* x in [1.0, 4.0)
|
||||
* at steps of 1/64, with the result represented in 1.12 fixed point (0x1000 = 1.0).
|
||||
*
|
||||
* The algorithm's `addi -64 / sll 1 / lh` selects the entry at `(aligned - 64) * 2` for the case where `aligned` has its top bit at bit 24.
|
||||
* After the sllv/srav pair, `aligned` always lands in `[0x80, 0x100)`
|
||||
* (with top bit at bit 24 → after `sub $aligned - 64`, the index sits in `[0x40, 0x80) * 2 = [0x80, 0x100)` bytes = entries [64, 128) within the sqrtbl).
|
||||
* The earlier 64 entries (octave 0) are reached when the magnitude after shifting puts the top bit below bit 24 (the `sllv` branch),
|
||||
* and the load upper_halves of the table bracket the input range.
|
||||
* The later 64 entries (octaves 2-3) are the `srav` branch when the magnitude's top bit is well above bit 24.
|
||||
* Representative entries:
|
||||
* [ 0] 0x1000 = 1.000000 ; 1 / sqrt(1.000000)
|
||||
* [ 16] 0x0e4f = 0.894287 ; 1 / sqrt(1.250000)
|
||||
* [ 32] 0x0d10 = 0.816406 ; 1 / sqrt(1.500000)
|
||||
* [ 48] 0x0c18 = 0.755859 ; 1 / sqrt(1.750000)
|
||||
* [ 64] 0x0b50 = 0.707031 ; 1 / sqrt(2.000000)
|
||||
* [128] 0x093c = 0.577148 ; 1 / sqrt(3.000000)
|
||||
* [191] 0x0804 = 0.500977 ; 1 / sqrt(3.984375)
|
||||
*
|
||||
* Reproduced verbatim from libgte (verified against libpsn00b/psxgte/vector.s:100-123 — 24 rows × 8 halfwords, last entry 0x0804).
|
||||
* ─── How VectorNormal indexes it ───
|
||||
* Let:
|
||||
* mag_sq = x*x + y*y + z*z
|
||||
* lzcr = leading-zero count of mag_sq
|
||||
* For a non-zero magnitude, libgte first rounds LZCR down to an even number:
|
||||
* lzcr_even = lzcr & ~1
|
||||
*
|
||||
* It then shifts mag_sq so that its significant bits land in one of two
|
||||
* adjacent normalized ranges:
|
||||
* if lzcr_even >= 24:
|
||||
* aligned = mag_sq << (lzcr_even - 24)
|
||||
* else:
|
||||
* aligned = mag_sq >> (24 - lzcr_even)
|
||||
*
|
||||
* Because lzcr_even differs from the true LZCR by at most one bit:
|
||||
* raw LZCR even -> aligned in [0x80, 0x100)
|
||||
* raw LZCR odd -> aligned in [0x40, 0x080)
|
||||
* therefore:
|
||||
* aligned in [0x40, 0x100)
|
||||
*
|
||||
* Dividing this normalized integer by 64 gives exactly the table domain:
|
||||
* x = aligned / 64
|
||||
* x in [1.0, 4.0)
|
||||
*
|
||||
* The lookup is therefore:
|
||||
* index = aligned - 0x40
|
||||
* byte_offset = index * sizeof(S2)
|
||||
* inv_len = sqrtbl[index]
|
||||
* or equivalently, matching the libgte instructions:
|
||||
* addi aligned, -64
|
||||
* sll aligned, 1
|
||||
* lh inv_len, sqrtbl + aligned
|
||||
*
|
||||
* ─── Why the domain spans [1, 4) instead of [1, 2) ───
|
||||
* Square-root scaling depends on the parity of the exponent.
|
||||
* Rounding LZCR to even absorbs exponent changes in pairs of bits, leaving the lookup mantissa normalized over a factor-of-four interval [1, 4).
|
||||
*
|
||||
* The corresponding exponent correction is retained separately as:
|
||||
* srav_shift = (31 - lzcr_even) >> 1
|
||||
*
|
||||
* After GPF multiplies the original vector components by the table's reciprocal-square-root coefficient,
|
||||
* this shift restores the exponent scale and yields the normalized vector.
|
||||
*
|
||||
* Reproduced verbatim from libgte; also matches PSn00bSDK VectorNormalS _norm_table (24 rows x 8 halfwords, final entry 0x0804).
|
||||
**/
|
||||
internal S2 const gte_normalize_sqr_tbl[192] align_(2) = {
|
||||
0x1000, 0x0fe0, 0x0fc1, 0x0fa3, 0x0f85, 0x0f68, 0x0f4c, 0x0f30,
|
||||
@@ -267,28 +300,25 @@ internal S2 const gte_normalize_sqr_tbl[192] align_(2) = {
|
||||
0x0820, 0x081c, 0x0818, 0x0814, 0x0810, 0x080c, 0x0808, 0x0804,
|
||||
};
|
||||
|
||||
typedef Struct_(Binds_NormalizeV3S4) {
|
||||
U2 src_offset; /* offset of src V3_S4 within the BIOS scratchpad */
|
||||
U2 dst_offset; /* offset of dst V3_S4 within the BIOS scratchpad */
|
||||
};
|
||||
typedef Struct_(RegUse_build_normalize_v3s4) {
|
||||
typedef Struct_(Binds_normalize_v3s4) { U2 src_offset; U2 dst_offset; };
|
||||
typedef Struct_(RegUse_normalize_v3s4) {
|
||||
union { Reg_(V3_S4) res, src; };
|
||||
union { Reg t0, src_ptr, mac2; };
|
||||
union { Reg t1, dst_ptr; };
|
||||
union { Reg t2, dst_offset, mac1, v_sqr_aligned; };
|
||||
union { Reg t3, src_offset, btarget, shift_count, sqrtbl_index; };
|
||||
union { Reg t4, mac3, v_sqr_sum, half_shift_tmp, inv_len; };
|
||||
union { Reg t5, lzcr, half_shift; };
|
||||
union { Reg r0, src_ptr, mac2; };
|
||||
union { Reg r1, dst_ptr; };
|
||||
union { Reg r2, dst_offset, mac1, v_sqr_aligned, sqrtbl_byte_offset; };
|
||||
union { Reg r3, src_offset, align_delta, shift_count, sqrtbl_lookup; };
|
||||
union { Reg r4, mac3, v_sqr_sum, srav_shift; };
|
||||
union { Reg r5, lzcr_raw, lzcr_even, inv_len; };
|
||||
};
|
||||
/* ─── Full normalize (all 4 stages inline) ───
|
||||
* Generic 4-stage GTE normalize (SQR → sum+LZCR → align+sqrtbl → GPF+srav). */
|
||||
internal MipsAtom* build_normalize_v3s4(AtomArena_R aa, RegUse_build_normalize_v3s4 r)
|
||||
internal MipsAtom* normalize_v3s4(AtomArena_R aa, RegUse_normalize_v3s4 r)
|
||||
MipsAtom_Proc_(aa, {
|
||||
load_half(r.src_offset, R_TapePtr, O_(Binds_NormalizeV3S4, src_offset)),
|
||||
load_half(r.dst_offset, R_TapePtr, O_(Binds_NormalizeV3S4, dst_offset)),
|
||||
load_half(r.src_offset, R_TapePtr, O_(Binds_normalize_v3s4, src_offset)),
|
||||
load_half(r.dst_offset, R_TapePtr, O_(Binds_normalize_v3s4, dst_offset)),
|
||||
LdSlot_ add_u(r.src_ptr, R_ScratchBase, r.src_offset),
|
||||
LdSlot_ add_u(r.dst_ptr, R_ScratchBase, r.dst_offset),
|
||||
LdSlot_ add_ui_self(R_TapePtr, S_(Binds_NormalizeV3S4)),
|
||||
LdSlot_ add_ui_self(R_TapePtr, S_(Binds_normalize_v3s4)),
|
||||
|
||||
mac_load_v3s4(r.src, r.src_ptr, 0),
|
||||
|
||||
@@ -300,33 +330,32 @@ MipsAtom_Proc_(aa, {
|
||||
add_u_self( r.v_sqr_sum, r.mac1),
|
||||
add_u_self( r.v_sqr_sum, r.mac2),
|
||||
gte_mv_to_data_r( r.v_sqr_sum, C2_LZCS), GteDelay_ nop2,
|
||||
gte_mv_from_data_r(r.lzcr, C2_LZCR), GteDelay_ nop,
|
||||
gte_mv_from_data_r(r.lzcr_raw, C2_LZCR), GteDelay_ nop,
|
||||
|
||||
/* Stage 3: even(LZCR), half-shift, align |v|² to bit 24. */
|
||||
mac_lzcr_round_even_half_shift(r.lzcr, r.v_sqr_sum, r.v_sqr_aligned),
|
||||
add_si( r.btarget, r.lzcr, -24),
|
||||
branch_lt_zero(r.btarget, atom_offset(aligned_done, srav_path)), BdSlot_ nop, /* bltz → srav_path (LZCR < 24 path) */
|
||||
mac_lzcr_round_even_half_shift(r.lzcr_raw, r.v_sqr_sum, r.v_sqr_aligned),
|
||||
add_si( r.align_delta, r.lzcr_even, -24),
|
||||
branch_lt_zero(r.align_delta, atom_offset(aligned_done, srav_path)), BdSlot_ nop, /* bltz → srav_path (LZCR < 24 path) */
|
||||
jump_rel(atom_offset(srav_path, aligned_done)), /* b → aligned_done (LZCR >= 24 path) */
|
||||
BdSlot_ shift_lleft_var(r.v_sqr_aligned, r.v_sqr_aligned, r.btarget),
|
||||
BdSlot_ shift_lleft_var(r.v_sqr_aligned, r.v_sqr_aligned, r.align_delta),
|
||||
atom_label(srav_path)
|
||||
li_s( r.shift_count, 24),
|
||||
sub_s(r.shift_count, r.shift_count, r.lzcr),
|
||||
sub_s(r.shift_count, r.shift_count, r.lzcr_even),
|
||||
shift_aright_var(r.v_sqr_aligned, r.v_sqr_aligned, r.shift_count),
|
||||
atom_label(aligned_done)
|
||||
or_u(r.half_shift, r.half_shift_tmp, 0),
|
||||
add_si( r.v_sqr_aligned, r.v_sqr_aligned, -64),
|
||||
shift_lleft(r.v_sqr_aligned, r.v_sqr_aligned, 1),
|
||||
mac_load_word_imm(r.sqrtbl_index, & gte_normalize_sqr_tbl), add_u_self(r.sqrtbl_index, r.v_sqr_aligned),
|
||||
load_half(r.inv_len, r.sqrtbl_index, 0),
|
||||
add_si( r.sqrtbl_byte_offset, r.v_sqr_aligned, -64),
|
||||
shift_lleft(r.sqrtbl_byte_offset, r.sqrtbl_byte_offset, 1),
|
||||
mac_load_word_imm(r.sqrtbl_lookup, & gte_normalize_sqr_tbl), add_u_self(r.sqrtbl_lookup, r.sqrtbl_byte_offset),
|
||||
load_half(r.inv_len, r.sqrtbl_lookup, 0),
|
||||
LdSlot_ nop,
|
||||
|
||||
mac_gte_general_purpose_interopolation(r.inv_len,
|
||||
r.src.x, r.src.y, r.src.z,
|
||||
r.res.x, r.res.y, r.res.z,
|
||||
GteDelay_ load_word(R_AtomJmp, R_TapePtr, 0), LdSlot_ // ac_yield: word 1
|
||||
GteDelay_ add_ui_self( R_TapePtr, S_(MipsCode)) // ac_yield: word 2
|
||||
GteDelay_ add_ui_self( R_TapePtr, S_(MipsCode)) // ac_yield: word
|
||||
),
|
||||
mac_shift_aright_var_v3s4_self(r.res, r.half_shift),
|
||||
mac_shift_aright_var_v3s4_self(r.res, r.srav_shift),
|
||||
mac_store_v3s4(r.res, r.dst_ptr, 0),
|
||||
|
||||
jump_reg(R_AtomJmp), BdSlot_ nop // ac_yield: word 3-4
|
||||
@@ -340,21 +369,21 @@ typedef Struct_(Binds_gte_cross_v3s4) { V3_S4* src_a; V3_S4* src_b; V3_S4* out;
|
||||
typedef Struct_(RegUse_gte_cross_v3s4) {
|
||||
Reg_(V3_S4) a;
|
||||
Reg_(V3_S4) b;
|
||||
union { Reg out, t0; } x;
|
||||
union { Reg src_a, t1, rt11; } y;
|
||||
union { Reg src_b, t2, rt22; } z;
|
||||
Reg out;
|
||||
Reg src_a;
|
||||
Reg src_b;
|
||||
};
|
||||
internal MipsAtom* gte_cross_v3s4(AtomArena_R aa, RegUse_gte_cross_v3s4 r)
|
||||
atom_info(atom_bind(Binds_gte_cross_v3s4)) MipsAtom_Proc_(aa, {
|
||||
load_word(r.y.src_a, R_TapePtr, O_(Binds_gte_cross_v3s4,src_a)),
|
||||
load_word(r.z.src_b, R_TapePtr, O_(Binds_gte_cross_v3s4,src_b)),
|
||||
load_word(r.x.out, R_TapePtr, O_(Binds_gte_cross_v3s4,out)),
|
||||
load_word(r.src_a, R_TapePtr, O_(Binds_gte_cross_v3s4,src_a)),
|
||||
load_word(r.src_b, R_TapePtr, O_(Binds_gte_cross_v3s4,src_b)),
|
||||
load_word(r.out, R_TapePtr, O_(Binds_gte_cross_v3s4,out)),
|
||||
LdSlot_ add_ui_self(R_TapePtr, S_(Binds_gte_cross_v3s4)),
|
||||
|
||||
mac_load_v3s4(r.a, r.y.src_a, 0), LdSlot_
|
||||
mac_load_v3s4(r.b, r.z.src_b, 0), LdSlot_
|
||||
mac_load_v3s4(r.a, r.src_a, 0), LdSlot_
|
||||
mac_load_v3s4(r.b, r.src_b, 0), LdSlot_
|
||||
mac_gte_op_cross_v3s4(r.a, r.b), /* RT diagonal + IR + OP + MAC read + shift */
|
||||
mac_store_v3s4(r.a, r.x.out, 0),
|
||||
mac_store_v3s4(r.a, r.out, 0),
|
||||
|
||||
mac_yield()
|
||||
})
|
||||
|
||||
+1
-26
@@ -1,21 +1,10 @@
|
||||
/* ============================================================================
|
||||
* duffle DSL Suffix Conventions
|
||||
* ============================================================================
|
||||
*
|
||||
* Every mnemonic in this header follows the same suffix grammar:
|
||||
*
|
||||
* Primitive commands: gp0_cmd_poly_f3 = 0x20 (byte opcode)
|
||||
* Packed 32-bit cmd: gp0_word_poly_f3(r, g, b) (32-bit, shifted)
|
||||
*
|
||||
* Type ordering: domain?_(direction)?_action_target_modifier_type?
|
||||
* Examples: add_ui (add + unsigned + immediate)
|
||||
* add_s (add + signed, R-type implicit)
|
||||
* shift_lleft (shift + logical + left)
|
||||
* shift_aright (shift + arithmetic + right)
|
||||
* call_reg(rs) (call + register, $ra implicit)
|
||||
* gte_mv_to_data_r (gte + mv + to + data + register)
|
||||
* gte_lw_v0_xy(base) (gte + lw + v0 + xy)
|
||||
* load_upper_i (load-upper + immediate, unique verb)
|
||||
* ============================================================================ */
|
||||
|
||||
#ifdef INTELLISENSE_DIRECTIVES
|
||||
@@ -29,21 +18,7 @@
|
||||
/* ============================================================================
|
||||
* gte.h — Geometry Transformation Engine (COP2) for the PS1
|
||||
* ============================================================================
|
||||
*
|
||||
* Hand-rolled DSL for emitting GTE/MIPS instruction words from C.
|
||||
* No GCC inline-assembly string syntax in the code body.
|
||||
*
|
||||
* STYLE NOTES
|
||||
* -----------
|
||||
* - Per-field encoders are named `enc_gte_<field>(value)` and each one self-masks its argument before shifting.
|
||||
* Mirrors the `enc_op / enc_rs / enc_rt / ...` family in mips.h.
|
||||
* - The composite `enc_gte_cmdw(sf, mx, v, cv, lm, cmd)` is a flat OR of the per-field encoders, plus the COP2/CO base.
|
||||
* - Pre-baked shortcuts (`gte_cmd_rtpt`, `gte_cmd_rtps`, …) are defined for the common cases so call sites read like assembly source.
|
||||
* - All register/field values are enums (not `#define`s) so they show up in debugger symbol tables and IDE autocomplete.
|
||||
*
|
||||
* SEE ALSO
|
||||
* --------
|
||||
* - mips.h: The MIPS encoder layer this builds on.
|
||||
* DSL for emitting GTE/MIPS instruction words from C.
|
||||
*/
|
||||
|
||||
/* C2 data registers */
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
# include "gen/macs.h"
|
||||
# include "gen/offsets.h"
|
||||
# include "math.h"
|
||||
# include "lottes_tape.h"
|
||||
# include "tape.h"
|
||||
#endif
|
||||
|
||||
ATOM_FILE_DEBUGGER_LINE_MARKER(math_atom_c);
|
||||
|
||||
+16
-17
@@ -5,7 +5,7 @@
|
||||
|
||||
#define MEM_ALIGNMENT_DEFAULT 4
|
||||
|
||||
#define assert_bounds(point, start, end) for(;0;){ \
|
||||
#define assert_bounds(point, start, end) do{ \
|
||||
assert((start) <= (point)); \
|
||||
assert((point) <= (end)); \
|
||||
} while(0)
|
||||
@@ -23,10 +23,10 @@ FI_ void mem_bump(U4 cap, U4*R_ used, U4 amount) {
|
||||
used[0] += amount;
|
||||
}
|
||||
|
||||
FI_ U4 mem_copy (U4 dest, U4 src, U4 len) { return (U4)(__builtin_memcpy ((void*)dest, (void const*)src, len)); }
|
||||
FI_ U4 mem_copy_overlapping(U4 dest, U4 src, U4 len) { return (U4)(__builtin_memmove((void*)dest, (void const*)src, len)); }
|
||||
FI_ U4 mem_fill (U4 dest, U4 value, U4 len) { return (U4)(__builtin_memset ((void*)dest, (int) value, len)); }
|
||||
FI_ B4 mem_zero (U4 dest, U4 len) { if(dest == 0){return false;} mem_fill(dest, 0, len); return true; }
|
||||
FI_ U4 mem_copy (U1_R dest, U1_R src, U4 len) { return (U4)(__builtin_memcpy ((void*)dest, (void const*)src, len)); }
|
||||
FI_ U4 mem_copy_overlapping(U1* dest, U1* src, U4 len) { return (U4)(__builtin_memmove((void*)dest, (void const*)src, len)); }
|
||||
FI_ U4 mem_fill (U1_R dest, U4 value, U4 len) { return (U4)(__builtin_memset ((void*)dest, (int) value, len)); }
|
||||
FI_ B4 mem_zero (U1_R dest, U4 len) { if(dest == 0){return false;} mem_fill(dest, 0, len); return true; }
|
||||
|
||||
#pragma region DAG
|
||||
|
||||
@@ -58,31 +58,30 @@ typedef Struct_(Str8) { UTF8* ptr; U4 len; };
|
||||
typedef Struct_(Slice_Str8) { Str8* ptr; U4 len; };
|
||||
#define slit(string_literal) (Str8){ (UTF8*) string_literal, S_(string_literal) - 1 }
|
||||
|
||||
typedef Struct_(Slice) { B1* ptr; U4 len; }; // Untyped Slice (byte-addressable; .len in elements)
|
||||
FI_ Slice slice_ut_(U4 ptr, U4 len) { return (Slice){(B1*)ptr, len}; }
|
||||
typedef Struct_(Slice) { U1* ptr; U4 len; };
|
||||
FI_ Slice slice_ut_(U1* ptr, U4 len) { return (Slice){ptr, len}; }
|
||||
|
||||
#define Slice_(type) Struct_(tmpl(Slice,type)) { type* ptr; U4 len; }
|
||||
typedef Slice_(B1);
|
||||
#define slice_assert(s) do { assert((s).ptr != 0); assert((s).len > 0); } while(0)
|
||||
#define slice_end(slice) ((slice).ptr + S_slice(slice) / S_(B1)) /* byte-ptr arithmetic; .len is in elements per slice convention */
|
||||
#define slice_end(slice) ((slice).ptr + S_slice(slice))
|
||||
#define S_slice(s) ((s).len * S_((s).ptr[0]))
|
||||
|
||||
#define slice_ut(ptr,len) slice_ut_(u4_(ptr), u4_(len))
|
||||
#define slice_ut_arr(a) slice_ut_(u4_(a), S_(a))
|
||||
#define slice_to_ut(s) slice_ut_(u4_((s).ptr), S_slice(s))
|
||||
#define slice_ut(ptr,len) slice_ut_(C_(U1*,ptr), u4_(len))
|
||||
#define slice_ut_arr(a) slice_ut_(C_(U1*,a), S_(a))
|
||||
#define slice_to_ut(s) slice_ut_(C_(U1*,(s).ptr), S_slice(s))
|
||||
|
||||
#define slice_iter(container, iter) (T_((container).ptr) iter = (container).ptr; iter != slice_end(container); ++ iter)
|
||||
#define slice_arg_from_array(type, ...) & (tmpl(Slice,type)) { .ptr = Array_decl(type,__VA_ARGS__), .len = Array_len( Array_decl(type,__VA_ARGS__)) }
|
||||
#define slice_from_array(type, array) (tmpl(Slice,type)) { .ptr = array, .len = Array_len(array) }
|
||||
|
||||
FI_ void slice_zero_(Slice s) { slice_assert(s); mem_zero(u4_(s.ptr), s.len); }
|
||||
FI_ void slice_zero_(Slice s) { slice_assert(s); mem_zero(s.ptr, s.len); }
|
||||
#define slice_zero(s) slice_zero_(slice_to_ut(s))
|
||||
|
||||
FI_ void slice_copy_(Slice dest, Slice src) {
|
||||
assert(S_slice(dest) >= S_slice(src));
|
||||
slice_assert(dest);
|
||||
slice_assert(src);
|
||||
mem_copy(u4_(dest.ptr), u4_(src.ptr), S_slice(src));
|
||||
mem_copy(dest.ptr, src.ptr, S_slice(src));
|
||||
}
|
||||
#define slice_copy(dest, src) do { \
|
||||
static_assert(T_same(dest, src)); \
|
||||
@@ -95,6 +94,7 @@ FI_ Slice slice_bump(U4_R used, U4 start, U4 len, U4 amount) {
|
||||
return slice_ut(ptr, amount);
|
||||
}
|
||||
|
||||
typedef Slice_(B1);
|
||||
typedef Slice_(U1);
|
||||
typedef Slice_(U4);
|
||||
|
||||
@@ -117,7 +117,7 @@ I_ Slice farena_push(FArena_R arena, U4 amount, Opt_farena o) {
|
||||
U4 to_commit = align_pow2(desired, o.alignment ? o.alignment : MEM_ALIGNMENT_DEFAULT);
|
||||
U4 ptr = arena->start + arena->used;
|
||||
mem_bump(arena->capacity, & arena->used, to_commit);
|
||||
return (Slice){ (B1*)ptr, to_commit };
|
||||
return (Slice){ (U1*)ptr, to_commit };
|
||||
}
|
||||
FI_ void farena_reset (FArena_R arena) { arena->used = 0; }
|
||||
FI_ void farena_rewind(FArena_R arena, U4 save_point) {
|
||||
@@ -134,8 +134,7 @@ FI_ U4 farena_unused_start(FArena arena) { return arena.start + arena.used; }
|
||||
|
||||
#pragma region BIOS Scratchpad
|
||||
/* BIOS scratchpad location. 1 KB at 0x1F800000.
|
||||
* TapeHostFrame occupies the final 44 bytes while tape code executes.
|
||||
* Atom scratch is bounded by the TapeHostFrame_Loc declaration in lottes_tape.h. */
|
||||
* TapeHostFrame occupies the final 44 bytes while tape code executes. */
|
||||
enum {
|
||||
Scratchpad_Loc = 0x1F800000,
|
||||
Scratchpad_Len = 0x400, /* 1 KB */
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
# include "gen/offsets.h"
|
||||
# include "bios.h"
|
||||
# include "mips.h"
|
||||
# include "lottes_tape.h"
|
||||
# include "tape.h"
|
||||
#endif
|
||||
|
||||
ATOM_FILE_DEBUGGER_LINE_MARKER(mips_atom_c);
|
||||
|
||||
+1
-6
@@ -81,13 +81,8 @@ enum {
|
||||
* (e.g. for asm clobber lists and register-variable declarations via `rgcc(R_X)`).
|
||||
* The enum value is bound to the `#define` so the two forms cannot drift apart.
|
||||
*
|
||||
* Only registers that get stringified need a `_Code` form; the rest are plain enum values.
|
||||
* If you need to add a new one, follow the pattern:
|
||||
* #define R_T7_Code 15
|
||||
* R_T7 = R_T7_Code, // in the enum
|
||||
*
|
||||
* User code should always reference the enum form (`R_T4`) at arithmetic sites and let
|
||||
* `rlit(R_T4_Code)` / `rgcc(R_T4)` handle the stringify cases — never write the bare number `12`.
|
||||
* `rlit(R_T4_Code)` / `rgcc(R_T4)` handle the stringify cases
|
||||
* ============================================================================ */
|
||||
#define R_0_Code 0
|
||||
#define R_AT_Code 1
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
# include "gen/offsets.h"
|
||||
# include "mips.h"
|
||||
# include "dsl.atom.h"
|
||||
# include "lottes_tape.h"
|
||||
# include "tape.h"
|
||||
# include "pad.h"
|
||||
#endif
|
||||
|
||||
|
||||
+2
-11
@@ -6,16 +6,10 @@
|
||||
# include "pad.h"
|
||||
#endif
|
||||
|
||||
/* Uses ONE 8-byte frame allocated via the compiler's standard prologue.
|
||||
/* Uses an 8-byte frame allocated via the compiler's standard prologue.
|
||||
* 4 wasted-arg words for B(12h) InitPAD2 are at [SP+0..15] but are not explicitly allocated.
|
||||
* Compiler handles the MIPS O32 "wasted stack" convention for us by treating the B-call as a 4-arg call.
|
||||
*
|
||||
* The buffer pointers are passed as arguments so the compiler keeps them in callee-saved registers;
|
||||
* The B(12h) asm volatile block does NOT clobber those registers (it clobbers only the volatile GPRs + B-table arg registers explicitly).
|
||||
* The C-level writes after the call re-load the pointers from their callee-saved homes.
|
||||
*
|
||||
* The clobber list for both B-calls names the full BIOS destroy set documented in kernelbios.md:167-174 (R1..R15, R24..R25, R31, HI/LO).
|
||||
* The kernel-ABI "volatile GPRs" subset is clb_mem_drain; the rest of the destroy set is enumerated explicitly here. */
|
||||
*/
|
||||
NI_ void pad_bios_init_start(PadBiosRaw* raw0, PadBiosRaw* raw1)
|
||||
{
|
||||
/* Pin raw0 + raw1 to $a0 + $a1 via rgcc; the B(12h) call uses these directly.
|
||||
@@ -24,9 +18,6 @@ NI_ void pad_bios_init_start(PadBiosRaw* raw0, PadBiosRaw* raw1)
|
||||
register PadBiosRaw* p1 rgcc(R_A1) = raw1;
|
||||
(void)p0; (void)p1;
|
||||
|
||||
// TODO(Ed): Properly annotate the raw values in the inline asm instructions.
|
||||
// Use enums.
|
||||
|
||||
/* B(12h) InitPAD2(raw0, 0x22, raw1, 0x22)
|
||||
* $a0 = raw0 (rgcc-bound; survives the sequence below)
|
||||
* $a1 = raw1 (preserved into $a2 before $a1 is overwritten)
|
||||
|
||||
+2
-9
@@ -4,10 +4,7 @@
|
||||
# include "math.h"
|
||||
#endif
|
||||
|
||||
/* PSX button bit positions — 1:1 with PSX-SPX docs at docs/psx-spx/docs/controllersandmemorycards.md:405-421.
|
||||
* Wire is active-low (0 = pressed).
|
||||
* The decoder atom computes buttons = (~raw_buttons) & 0xFFFF;
|
||||
* active-low-to-active-high inversion is applied bit-by-bit. */
|
||||
// PSX button bit positions: PSX-SPX docs/psx-spx/docs/controllersandmemorycards.md:405-421.
|
||||
typedef Enum_(U2, PadBtns) {
|
||||
Bit_(Pad_Select, 0),
|
||||
Bit_(Pad_L3, 1),
|
||||
@@ -62,11 +59,7 @@ typedef Enum_(U4, PadStatus) {
|
||||
PadStatus_Invalid,
|
||||
};
|
||||
|
||||
/* Distinct from the game-facing PadStatus enum: PadRawStatus_Ok and PadRawStatus_Timeout are raw BIOS values;
|
||||
* PadStatus_* are game-facing post-decode states. PadUnknownId_Sentinel is written by the decoder
|
||||
* when the controller id does not match any known controller type.
|
||||
* PadAxisCentered_Word: Four-byte 0x80 pattern used to clear / center
|
||||
* four byte axes at PadState.left_x through PadState.right_y. */
|
||||
// Distinct from the game-facing PadStatus enum: PadRawStatus_Ok and PadRawStatus_Timeout are raw BIOS values
|
||||
typedef Enum_(U1, PadRawStatus) {
|
||||
PadRawStatus_Ok = 0x00,
|
||||
PadRawStatus_Timeout = 0xFF,
|
||||
|
||||
+1
-1
@@ -104,7 +104,7 @@ void gte_matrix_set_translation(MT3_S2S4* mat) asm("SetTransMatrix");
|
||||
|
||||
// Einheit, Metrication to unit vector. "Normalization", not Orthogonal "Normal, Normalis". Directionalization.
|
||||
// RGA(Lengyel): Normalize the bulk of a zero-weight direction. This is not finite-point unitization (which forces w=1).
|
||||
S4 normalize_v3s4(V3_S4* v0, V3_S4* v1) asm("VectorNormal");
|
||||
S4 psy_normalize_v3s4(V3_S4* v0, V3_S4* v1) asm("VectorNormal");
|
||||
|
||||
// RGA(Lengyel): Apply the matrix expansion of a rigid transformation.
|
||||
// Motor antiproduct is equivalent for unitized points; LA form is what GTE consumes.
|
||||
|
||||
@@ -13,56 +13,32 @@
|
||||
|
||||
#pragma region Tape Drive
|
||||
/* -----------------------------------------------------------------------------------------------------------
|
||||
* TAPE DRIVE ABI
|
||||
* THREADED ATOMS - TAPE EXECUTION & ABI
|
||||
* _________
|
||||
* | ___ |
|
||||
* | o___o | ,-----<-----.
|
||||
* |__/___\__| V ^
|
||||
* \_[Enter]_[A]->[A]->[A]->[A(B)]->[A]->[Exit]
|
||||
* -----------------------------------------------------------------------------------------------------------
|
||||
* Note(Ed): One of the main purposes of this codebase is to help me learn this,
|
||||
* as such the information below may not* be entirely realized or finalized conceptually.
|
||||
* -----------------------------------------------------------------------------------------------------------
|
||||
* This ABI and its associated legos were directly inspired by researching the work of
|
||||
* Timothy Lottes and Onat Türkçüoğlu; along with many others. It's the simplest bootstrap of a
|
||||
* directly executed chain of assemby arrays (Atoms) that terminate with a yield sequence to the next atom.
|
||||
* These eventually lead to a terminal atom for the tape which is defined below as "tape_exit".
|
||||
* This ABI and its associated legos were directly inspired by researching the work of Timothy Lottes and
|
||||
* Onat Türkçüoğlu; Forth, threaded code system, and various other people or programming techniques.
|
||||
*
|
||||
* The setup is simple:
|
||||
* A tape is a linear stream containing addresses of directly executable native-code fragments ("Atoms").
|
||||
* Most atoms terminate in a small yield sequence which loads the next atom address from the tape.
|
||||
* It's a runtime composed of directly executed native machine-code sequences (Atoms) that usually terminate
|
||||
* in a yield sequence to the next atom. These eventually lead to a terminal atom for the tape
|
||||
* which is defined below as "tape_exit". Traditionally referred to as Direct Threaded Execution.
|
||||
*
|
||||
* It behaves as one of the simplest runtime harnesses ontop of a host-enviornment's execution engine
|
||||
* to author and compose programs with. From here various conventions can be further applied.
|
||||
* To make things easier to understand it may be better to focus on what this ABI does not have.
|
||||
* It does not have have any branching within the tape but relative branches within atoms or between atoms.
|
||||
* Branching nearly is always downstream. Automatic stack usage is non-existent.
|
||||
* Push/Pop, FIFO, or Arena/Bump data structures are used by atoms explicitly.
|
||||
* In it's current form with the C11 macro DSL, the user also has fullfill manual register allocation per atom.
|
||||
*
|
||||
* One of the remarkable things about utilizing this ABI is its essentially interopable with CPUs, GPUs, FPGA,
|
||||
* or, basically anything from the 5th generation consoles and onward.
|
||||
* The ABI directly reflects how all computational hardware must be architected in order to execute
|
||||
* digital logic effectively on current era tech.
|
||||
* On the PS1 we don't have access to a few features like multi-threading, speculative execution, or L3 cache;
|
||||
* but, we can set the foundation for legoing whats required for eventually expanding this ABI's paradigm
|
||||
* and core atoms to take those newer hardware features into account. For example, you can easily expand
|
||||
* this to support wave-based execution model on a PS2 or PS3. Not having a stack or
|
||||
* automatic register allocation means the user cannot ignore excessive argument shuffle across workload or
|
||||
* waves and thier phases. Crossing ABI boundaries to other runtimes that do has obviouss penalties.
|
||||
*
|
||||
* Learning data-oriented code becomes a natural progression. Your not fighting a stack-based procedural
|
||||
* paradigm that wants to argument shuffle. There is no ambiguity due to the lack of constraints, for example,
|
||||
* on how the user may "call" a procedure in traditional random dispatch runtimes. The user does have to
|
||||
* hammer down "rules" or patterns for massaging the compiler to dissolve those call frames; just to get
|
||||
* the asesmbly into its desired form. The form is obvious, and once the user gets to author these compoonents
|
||||
* it becomes a game of tetris.
|
||||
*
|
||||
* Another feature is this ABI is very compatible with bootstrapping and developing simple toolchains built off
|
||||
* of bit-packed annotated command streams the user can directly author, maintatain, and immediately execute.
|
||||
* That being like a color forth, or maybe something more familar like an immediate mode library
|
||||
* for various systems such as GUIs. This can make the tetris less of a chore with some helpful policy
|
||||
* generation for allocation of registers, helping to choose resuable components, designing DSL on the fly, etc.
|
||||
* -----------------------------------------------------------------------------------------------------------
|
||||
* TODO(Ed): We need pretty ascii diagrams and proper guides, articles, etc.
|
||||
* -----------------------------------------------------------------------------------------------------------
|
||||
* For now this ideation has just started functioning. I'm abusing C11 & a lua metaprogram to help establish
|
||||
* a hybrid toolchain to ideate on a traditional text-based authoring UX for this paradigm.
|
||||
* If pcsx-redux provides viable hot-reload and persistent data storage beyond save-states
|
||||
* (just copying ram to filesystem), I can author a color forth to mess around with.
|
||||
* With either an editor in-emulator or on the actual machine itself. Assembly is tedius,
|
||||
* but I think this codebase most likely has a pretty ergonomic flavor worst case...
|
||||
* The tape itself does not have have any branching behavior.
|
||||
* Branches, loops, skips, or other control-flow policies must be implemented explicitly by atoms.
|
||||
* Push/Pop, FIFO, or Arena/Bump data structures are utilized by atoms explicitly.
|
||||
* There is no implicit call-stack, return stack, or per-atom stack-frame.
|
||||
* The user must also explictly handle register allocation per atom (by default).
|
||||
* However they could procedurally automate it using metaprogramming functionality.
|
||||
* */
|
||||
/* Register Allocation Info */
|
||||
enum {
|
||||
@@ -99,8 +75,8 @@ enum {
|
||||
R_Atom7 = R_T7,
|
||||
R_Atom8 = R_T8,
|
||||
R_Atom9 = R_T9,
|
||||
R_Atom10 = R_V0, // Tend to be used with gte DMAs
|
||||
R_Atom11 = R_V1, // Tend to be used with gte DMAs
|
||||
R_Atom10 = R_V0, // Tend to be used with gte moves
|
||||
R_Atom11 = R_V1, // Tend to be used with gte moves
|
||||
R_Atom12 = R_A0,
|
||||
R_Atom13 = R_A1,
|
||||
R_Atom14 = R_A2,
|
||||
@@ -121,7 +97,7 @@ typedef U2 Reg; // Register parameter used with atom or atom component procedure
|
||||
typedef U4 const MipsCode; // Underlying type to mips asm words.
|
||||
typedef Slice_(MipsCode);
|
||||
|
||||
typedef U4 const MipsAtom; // Underlying type to a mips atom defnition
|
||||
typedef U4 const MipsAtom; // Underlying type to a mips atom definition
|
||||
typedef Slice_(MipsAtom);
|
||||
|
||||
// Sometimes a user will define a bundle of atoms that represent a procedure of work as:
|
||||
@@ -163,6 +139,12 @@ typedef Slice_(MipsAtom);
|
||||
// Used for trivial mappings from one atom component proc to the command of a more baser (meant for type-mapping)
|
||||
#define MipsAtomComp_ProcMap_(ab, base_command) atom_dbg_skip MipsAtomComp_Proc_(ab, {base_command })
|
||||
|
||||
// WIP: Atoms Assocated closely with each other to form a tape procedure. (Maybe also a phase in a procedure/pipeline?)
|
||||
|
||||
#define AtomBundle_(name) Struct_(tmpl(AtomBundle,name))
|
||||
#define AtomBundle_Len(name) S_(tmpl(AtomBundle,name))/S_(MipsAtom*)
|
||||
#define AtomBundleEntry_(bundle,entry) tmpl(bundle,entry)
|
||||
|
||||
/* Line-table anchor: gcc only adds a file to the .debug_line file table when the contains line-numbered content.
|
||||
Files containing only atoms and atom components.
|
||||
Place `ATOM_FILE_LINE_MARKER();` once at file scope in any `.atom.c` that defines atoms.
|
||||
@@ -170,7 +152,7 @@ typedef Slice_(MipsAtom);
|
||||
The constant is in `.rodata` so the linker may eliminate it. */
|
||||
#define ATOM_FILE_DEBUGGER_LINE_MARKER(file_name) internal U4 const tmpl(atom_file_debugger_line_marker,file_name) = 0
|
||||
|
||||
typedef Slice_MipsAtom Tape;
|
||||
typedef Struct_(Tape) { union { MipsAtom* ptr; U4* inlaid_data; }; U4 len; };
|
||||
|
||||
typedef Struct_(TapeHostFrame) {
|
||||
U4 s0;
|
||||
@@ -236,15 +218,14 @@ FI_ void tape_run(Tape tape) { C_(TapeEntryFn*, tape_enter)(tape.ptr); }
|
||||
// Procedural authoring of tapes:
|
||||
typedef Relative_(FArena) Struct_(TapeBuilder) { U4 ptr; U4 capacity; U4 used; };
|
||||
FI_ void tb_init(TapeBuilder* tb, FArena* arena) { tb->ptr = arena->start; tb->used = 0; }
|
||||
FI_ TapeBuilder tb_make_old( FArena* arena) { return (TapeBuilder){ arena->start, 0 }; }
|
||||
FI_ TapeBuilder tb_make(Slice mem) { return (TapeBuilder){ u4_(mem.ptr), mem.len, 0 }; } /* capacity in elements (matches used units) */
|
||||
|
||||
FI_ void tb_emit(TapeBuilder* tb, MipsAtom* 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; }
|
||||
#define tb_emit_(atom) tb_emit(& tb, atom)
|
||||
#define tb_data_(field, data) tb_data(& tb, u4_(data))
|
||||
|
||||
FI_ void tb_emit_bundle(TapeBuilder_R tb, Slice_MipsAtom atoms) { mem_copy(u4_(tb->ptr), u4_(atoms.ptr), S_slice(atoms)); tb->used += atoms.len; }
|
||||
FI_ void tb_bind(TapeBuilder* tb, Slice data) { mem_copy(b1_r(tb->ptr + tb->used * S_(MipsCode)), data.ptr, data.len); tb->used += data.len / S_(MipsCode); }
|
||||
#define tb_bind_(tb,type,...) tb_bind(tb, (Slice){ (U1*)(& (type){__VA_ARGS__}), S_(type) }); static_assert(S_(type) % S_(MipsCode) == 0)
|
||||
|
||||
FI_ Tape tb_end (TapeBuilder* tb) { tb_emit(tb,tape_exit); return (Tape){ C_(U4*,tb->ptr), tb->used }; }
|
||||
FI_ Tape tb_slice(TapeBuilder tb) { return (Tape){ C_(U4*,tb.ptr), tb.used }; }
|
||||
@@ -252,6 +233,12 @@ FI_ Tape tb_slice(TapeBuilder tb) { return (Tape){ C_(U4
|
||||
|
||||
FI_ void tb_scope_run_end(TapeBuilder* tb) { tb_emit(tb,tape_exit); tape_run(tb_slice(tb[0])); }
|
||||
#define tb_scope_run(tb) for(U4 tbs_once=0;tbs_once==0;++tbs_once,tb_scope_run_end(tb))
|
||||
|
||||
|
||||
// NOTE(Ed): Wip still ideating convention. Possibly will never use a composite.
|
||||
#define tb_emit_wbind_(tb,atom,...) tb_emit(tb,atom); tb_bind_(tb,tmpl(Binds,atom),__VA_ARGS__)
|
||||
#define tb_emit_wbind2_(tb,atom,type,...) tb_emit(tb,atom); tb_bind_(tb,type,__VA_ARGS__)
|
||||
|
||||
#pragma endregion Tape Drive
|
||||
|
||||
#pragma region Macro Mips Atom Components
|
||||
@@ -261,6 +248,7 @@ FI_ void tb_scope_run_end(TapeBuilder* tb) { tb_emit(tb,tape_exit); tape_run(tb_
|
||||
* ---------------------------------------------------------------------------*/
|
||||
|
||||
// The 'Yield' sequence for Tape Atoms (mac_yield).
|
||||
// In Forth this is considered the "NEXT" mechanism.
|
||||
|
||||
atom_dbg_skip MipsAtomComp_(ac_yield) {
|
||||
load_word(R_AtomJmp, R_TapePtr, 0), LdSlot_
|
||||
@@ -289,7 +277,7 @@ typedef Relative_(FArena) Struct_(AtomBuilder) { U4 start; U4 capacity; U4 used;
|
||||
FI_ void atombuilder_push(AtomBuilder_R ab, Slice_MipsCode code) {
|
||||
assert(ab->capacity - ab->used - code.len);
|
||||
U4 dest = ab->start + ab->used * S_(MipsCode); U4 size = S_slice(code);
|
||||
mem_copy(dest, u4_(code.ptr), size); ab->used += size;
|
||||
mem_copy(b1_r(dest), b1_r(code.ptr), size); ab->used += size;
|
||||
}
|
||||
#define atombuilder_push_mac(ab, mac) atombuilder_push(ab, slice_arg_from_array(Slice_MipsCode, mac))
|
||||
|
||||
@@ -313,7 +301,7 @@ FI_ AtomArena atomarena_make(Slice mem) { AtomArena a; atomarena_init(& a, mem);
|
||||
FI_ MipsAtom* atomarena_push(AtomArena_R aa, Slice_MipsCode code) {
|
||||
assert(aa->capacity - aa->used - code.len);
|
||||
U4 dest = atomarena_unused_start(aa[0]); U4 size = S_slice(code);
|
||||
mem_copy(dest, u4_(code.ptr), size); aa->used += size;
|
||||
mem_copy(b1_r(dest), b1_r(code.ptr), size); aa->used += size;
|
||||
return C_(MipsAtom*, dest);
|
||||
}
|
||||
FI_ void atomarena_reset(AtomArena_R aa) { aa->used = 0; }
|
||||
@@ -344,24 +332,14 @@ internal Reg const regfile_alloc_order[] = {
|
||||
R_T8, R_T9,
|
||||
};
|
||||
|
||||
typedef Struct_(RegFile) {
|
||||
A2_U2 GPR;
|
||||
A2_U2 GTE;
|
||||
};
|
||||
typedef Struct_(RegFile) { A2_U2 GPR; };
|
||||
#define regfile(pin_mask) {.GPR={u4_lo(pin_mask), u4_hi(pin_mask)} }
|
||||
FI_ void regfile_init(RegFile_R rf) {
|
||||
/* pack the 32-bit ABI mask into the two U2s */
|
||||
rf->GPR[0] = u4_lo(regfile_abi_mask);
|
||||
rf->GPR[1] = u4_hi(regfile_abi_mask);
|
||||
rf->GTE[0] = rf->GTE[1] = 0;
|
||||
rf->GPR[0] = u4_lo(regfile_abi_mask); rf->GPR[1] = u4_hi(regfile_abi_mask);
|
||||
}
|
||||
FI_ RegFile regfile_make(void) { RegFile rf; regfile_init(& rf); return rf; }
|
||||
|
||||
typedef Struct_(RegFile_RInfo) {
|
||||
U2_R section;
|
||||
U2 mask;
|
||||
B2 occupied;
|
||||
};
|
||||
typedef Struct_(RegFile_RInfo) { U2_R section; U2 mask; B2 occupied; };
|
||||
FI_ RegFile_RInfo regfile_rinfo(A2_U2 file, Reg r_id) {
|
||||
U2 s_id = r_id >> 4;
|
||||
U2_R section = & file[s_id];
|
||||
@@ -371,19 +349,11 @@ FI_ RegFile_RInfo regfile_rinfo(A2_U2 file, Reg r_id) {
|
||||
}
|
||||
FI_ Reg regfile__alloc_helper(A2_U2 file, Reg r_id) {
|
||||
Reg result = 0; RegFile_RInfo info = regfile_rinfo(file, r_id);
|
||||
if (info.occupied == false) {
|
||||
info.section[0] |= info.mask;
|
||||
result = r_id;
|
||||
}
|
||||
if (info.occupied == false) { info.section[0] |= info.mask; result = r_id; }
|
||||
return result;
|
||||
}
|
||||
/* regfile_alloc picks the next free GPR from regfile_alloc_order.
|
||||
* The table is the first-fit allocation order: T0..T7, V0..V1, A0..A3,
|
||||
* S0..S7, T8..T9. The 24 entries leave room for the tape program to use
|
||||
* any of them while R0, R1, R26-R31 remain reserved. */
|
||||
I_ Reg regfile_alloc(RegFile_R rf) {
|
||||
Reg allocated = 0;
|
||||
for index_iter(U4, r_id, R_V0, <, R_T9) {
|
||||
Reg allocated = 0; for index_iter(U4, r_id, R_V0, <, R_T9) {
|
||||
allocated = regfile__alloc_helper(rf->GPR, r_id);
|
||||
Jmp_nZero_(allocated,resolved);
|
||||
}
|
||||
@@ -397,8 +367,7 @@ FI_ Reg regfile_pin(RegFile_R rf, Reg r_id) {
|
||||
return r_id;
|
||||
}
|
||||
FI_ void regfile_pin_mask(RegFile_R rf, U4 mask) {
|
||||
B4 occupied = u4_r(rf->GPR)[0] & mask;
|
||||
assert(occupied == false);
|
||||
B4 occupied = u4_r(rf->GPR)[0] & mask; assert(occupied == false);
|
||||
u4_r(rf->GPR)[0] |= mask;
|
||||
}
|
||||
FI_ void regfile_free_mask(RegFile_R rf, U4 mask) {
|
||||
@@ -406,21 +375,23 @@ FI_ void regfile_free_mask(RegFile_R rf, U4 mask) {
|
||||
u4_r(rf->GPR)[0] &= ~mask;
|
||||
}
|
||||
FI_ void regfile_free_reg(RegFile_R rf, Reg r_id) {
|
||||
/* never free the ABI set */
|
||||
if (regfile_abi_mask & (1u << r_id)) return;
|
||||
if (regfile_abi_mask & (1u << r_id)) return; // never free the ABI set
|
||||
RegFile_RInfo info = regfile_rinfo(rf->GPR, r_id);
|
||||
info.section[0] &= ~info.mask;
|
||||
}
|
||||
FI_ void regfile_reset(RegFile_R rf) {
|
||||
rf->GPR[0] = u4_lo(regfile_abi_mask);
|
||||
rf->GPR[1] = u4_hi(regfile_abi_mask);
|
||||
}
|
||||
FI_ void regfile_reset_to_mask(RegFile_R rf, U4 mask) {
|
||||
rf->GPR[0] = u4_lo(mask);
|
||||
rf->GPR[1] = u4_hi(mask);
|
||||
}
|
||||
FI_ void regfile_reset (RegFile_R rf) { rf->GPR[0] = u4_lo(regfile_abi_mask); rf->GPR[1] = u4_hi(regfile_abi_mask); }
|
||||
FI_ void regfile_reset_to_mask(RegFile_R rf, U4 mask) { rf->GPR[0] = u4_lo(mask); rf->GPR[1] = u4_hi(mask); }
|
||||
#pragma endregion RegFileArena (Register File Allocator)
|
||||
|
||||
#pragma region Mips Atom Components (Procedures)
|
||||
|
||||
// For doing direct-chaining of "atoms or fragments".
|
||||
FI_ Slice_MipsCode ac_yield_to(AtomBuilder_R ab, Reg code_ptr) atom_dbg_skip MipsAtomComp_Proc_(ab, {
|
||||
jump_reg(code_ptr), BdSlot_ nop,
|
||||
})
|
||||
|
||||
#pragma endregion Mips Atom Components (Procedures)
|
||||
|
||||
#pragma region Mips Atom Procs
|
||||
/* RegUse structs are a convention to organize register allocations for a mips atom procedure.
|
||||
Unlike the usual enum-based declarations, they provide a namespaced scope and have view types via union declarations. */
|
||||
@@ -441,7 +412,6 @@ FI_ void regfile_reset_to_mask(RegFile_R rf, U4 mask) {
|
||||
add_si(r.t1.view_3, r.usual_modifiable, 10),
|
||||
mac_yield(),
|
||||
})
|
||||
|
||||
#pragma endregion Mips Atom Procs
|
||||
|
||||
#pragma region Baked Mips Atoms
|
||||
@@ -26,7 +26,7 @@ enum {
|
||||
atom_offset_end_low_exit_stick = _atom_offset_end_low_exit_stick,
|
||||
};
|
||||
|
||||
// --- atom: pad_input_cam (40 words) ---
|
||||
// --- atom: pad_input_cam (39 words) ---
|
||||
|
||||
#define _atom_offset_left_x_exit_left_x 3
|
||||
#define _atom_offset_right_x_exit_right_x 3
|
||||
@@ -44,7 +44,7 @@ enum {
|
||||
atom_offset_circle_z_exit_circle_z = _atom_offset_circle_z_exit_circle_z,
|
||||
};
|
||||
|
||||
// --- atom: cube_g4_face (75 words) ---
|
||||
// --- atom: cube_g4_face (73 words) ---
|
||||
|
||||
#define _atom_offset_cull_cube_g4_face_exit 41
|
||||
#define _atom_offset_bounds_chk_cube_g4_face_exit 24
|
||||
@@ -54,7 +54,7 @@ enum {
|
||||
atom_offset_bounds_chk_cube_g4_face_exit = _atom_offset_bounds_chk_cube_g4_face_exit,
|
||||
};
|
||||
|
||||
// --- atom: floor_f3_face (58 words) ---
|
||||
// --- atom: floor_f3_face (56 words) ---
|
||||
|
||||
#define _atom_offset_culling_floor_f3_face_exit 25
|
||||
#define _atom_offset_bounds_chk_floor_f3_face_exit 16
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
# include "duffle/gen/macs.h"
|
||||
# include "duffle/gen/offsets.h"
|
||||
# include "duffle/dsl.atom.h"
|
||||
# include "duffle/lottes_tape.h"
|
||||
# include "duffle/tape.h"
|
||||
# include "duffle/mips.h"
|
||||
# include "duffle/gte.h"
|
||||
# include "duffle/gp.h"
|
||||
@@ -11,8 +11,8 @@
|
||||
# include "duffle/word_count.metadata.h"
|
||||
# include "duffle/psyq.h"
|
||||
# include "duffle/math.atom.h"
|
||||
# include "duffle/mips.atom.c"
|
||||
# include "duffle/gte.atom.c"
|
||||
# include "duffle/mips.atom.c"
|
||||
# include "duffle/gp.atom.c"
|
||||
# include "duffle/psyq.atom.c"
|
||||
# include "gen/offsets.h"
|
||||
@@ -21,7 +21,7 @@
|
||||
# include "hello_camera.h"
|
||||
#endif
|
||||
|
||||
ATOM_FILE_DEBUGGER_LINE_MARKER(hello_joypad_atom_c);
|
||||
ATOM_FILE_DEBUGGER_LINE_MARKER(hello_camera_atom_c);
|
||||
|
||||
#pragma region MACs (Mips Atom components)
|
||||
|
||||
@@ -92,11 +92,6 @@ MipsAtomComp_Proc_(ab, {
|
||||
#pragma endregion MACs
|
||||
|
||||
#pragma region Atom Procs
|
||||
// Modular Atoms
|
||||
|
||||
#define AtomBundle_(name) Struct_(tmpl(AtomBundle,name))
|
||||
#define AtomBundle_Len(name) S_(tmpl(AtomBundle,name))/S_(MipsAtom*)
|
||||
#define AtomBundleEntry_(bundle,entry) tmpl(bundle,entry)
|
||||
|
||||
#pragma region resolve_look_at
|
||||
/* ─── resolve_look_at bundle chain atoms ──────────────────────────── */
|
||||
@@ -108,7 +103,7 @@ typedef AtomBundle_(resolve_look_at) { MipsAtom
|
||||
*normalize_right_ux,
|
||||
*cross_to_up,
|
||||
*normalize_up_uy,
|
||||
*pop_mv_trans;
|
||||
*populate_mt3s4s2;
|
||||
};
|
||||
|
||||
typedef Struct_(ResolveLookAtScratch) {
|
||||
@@ -123,71 +118,56 @@ typedef Struct_(ResolveLookAtScratch) {
|
||||
V3_S4 up_in;
|
||||
};
|
||||
|
||||
/* Binds_ResolveLookAtSub — what the C side pushes onto the tape before input_and_sub.
|
||||
* The scratchpad base is no longer pushed because R_ScratchBase (= R_SP) is a tape carrier
|
||||
* preserved across atoms; the atom body reads 0x1F800000 directly from R_SP. */
|
||||
typedef Struct_(Binds_ResolveLookAt) {
|
||||
MT3_S2S4* look_at;
|
||||
P3_S4* eye;
|
||||
P3_S4* target;
|
||||
V3_S4* up_in;
|
||||
};
|
||||
typedef Struct_(Binds_ResolveLookAtSub) {
|
||||
P3_S4* target;
|
||||
P3_S4* eye;
|
||||
V3_S4* up_in;
|
||||
};
|
||||
typedef Struct_(RegUse_resolve_look_at_input_and_sub) {
|
||||
Reg target; Reg eye; Reg up_in;
|
||||
Reg t0; Reg t1; Reg t2; Reg t3; Reg t4;
|
||||
Reg target_ptr;
|
||||
Reg eye_ptr;
|
||||
Reg up_in_ptr;
|
||||
union { Reg_(V3_S4) r012, up_in, eye; };
|
||||
union { Reg_(V3_S4) r345, target, fwd; };
|
||||
};
|
||||
/* Atom 0 in the bundle: input_and_sub. Stages C-side inputs into the scratchpad and computes fwd = target - eye. */
|
||||
internal MipsAtom* AtomBundleEntry_(resolve_look_at,input_and_sub)(AtomArena_R aa, RegUse_resolve_look_at_input_and_sub r)
|
||||
atom_info(atom_bind(Binds_ResolveLookAtSub)) MipsAtom_Proc_(aa, {
|
||||
load_word(r.target, R_TapePtr, O_(Binds_ResolveLookAtSub,target)),
|
||||
load_word(r.eye, R_TapePtr, O_(Binds_ResolveLookAtSub,eye)),
|
||||
load_word(r.up_in, R_TapePtr, O_(Binds_ResolveLookAtSub,up_in)),
|
||||
load_word(r.target_ptr, R_TapePtr, O_(Binds_ResolveLookAtSub,target)),
|
||||
load_word(r.eye_ptr, R_TapePtr, O_(Binds_ResolveLookAtSub,eye)),
|
||||
load_word(r.up_in_ptr, R_TapePtr, O_(Binds_ResolveLookAtSub,up_in)),
|
||||
LdSlot_ add_ui_self( R_TapePtr, S_(Binds_ResolveLookAtSub)),
|
||||
|
||||
/* Stage up_in.x/y/z into the scratchpad. R_ScratchBase = R_SP = 0x1F800000. */
|
||||
mac_load_word_v3( r.t0, r.t1, r.t2, r.up_in, 0), LdSlot_
|
||||
mac_store_word_v3(r.t0, r.t1, r.t2, R_ScratchBase, O_(ResolveLookAtScratch,up_in)),
|
||||
mac_load_v3s4( r.up_in, r.up_in_ptr, 0), LdSlot_
|
||||
mac_store_v3s4(r.up_in, R_ScratchBase, O_(ResolveLookAtScratch,up_in)),
|
||||
|
||||
// Stage eye.x/y/z into the scratchpad (atom 6 reads these for the translation column).
|
||||
mac_load_word_v3( r.t0, r.t1, r.t2, r.eye, 0), LdSlot_
|
||||
mac_store_word_v3(r.t0, r.t1, r.t2, R_ScratchBase, O_(ResolveLookAtScratch,eye)),
|
||||
mac_load_v3s4( r.eye, r.eye_ptr, 0), LdSlot_
|
||||
mac_store_v3s4(r.eye, R_ScratchBase, O_(ResolveLookAtScratch,eye)),
|
||||
|
||||
/* Compute fwd = target - eye. */
|
||||
// mac_load_p3s4(t3, R_AT, t4, r.eye, 0),
|
||||
mac_load_word_v3(r.t3, R_AT, r.t4, r.target, 0), LdSlot_
|
||||
mac_sub_s_v3_self(
|
||||
r.t3, R_AT, r.t4,
|
||||
r.t0, r.t1, r.t2),
|
||||
mac_store_word_v3(r.t3, R_AT, r.t4, R_ScratchBase, O_(ResolveLookAtScratch,fwd)),
|
||||
mac_load_v3s4( r.target, r.target_ptr, 0), LdSlot_
|
||||
mac_sub_v3s4_self(r.fwd, r.eye),
|
||||
mac_store_v3s4( r.fwd, R_ScratchBase, O_(ResolveLookAtScratch,fwd)),
|
||||
|
||||
mac_yield()
|
||||
})
|
||||
|
||||
|
||||
typedef Struct_(Binds_ResolveLookAtPopMvTrans) {
|
||||
U4 look_at; /* MT3_S2S4* — destination matrix address */
|
||||
typedef Struct_(Binds_ResolveLookAt_PopulateMT3S4S2) {
|
||||
MT3_S2S4* look_at; /* MT3_S2S4* — destination matrix address */
|
||||
};
|
||||
typedef Struct_(RegUse_resolve_look_at__pop_mv_trans) {
|
||||
typedef Struct_(RegUse_resolve_look_at_populate_mt3s4s2) {
|
||||
Reg look_at;
|
||||
Reg_(V3_S4) row; /* populate phase: load ux/uy/uz */
|
||||
union { Reg ux, v_x; } t6; /* populate addr (canonical) → matrix_vector v_x */
|
||||
union { Reg uy, v_y; } t7; /* populate uy → matrix_vector v_y */
|
||||
union { Reg uz, v_z; } t8; /* populate uz → matrix_vector v_z */
|
||||
Reg eye; /* matrix_vector phase: load -eye */
|
||||
Reg_(V3_S4) row; /* populate phase: load ux/uy/uz */
|
||||
union { Reg r0, ux, vx; }; /* populate addr → matrix_vector v_x */
|
||||
union { Reg r1, uy, vy; }; /* populate uy → matrix_vector v_y */
|
||||
union { Reg r2, uz, vz; }; /* populate uz → matrix_vector v_z */
|
||||
};
|
||||
/* Atom 6 (fused): write look_at->m[][] from ux/uy/uz as packed S2 (populate),
|
||||
/* write look_at->m[][] from ux/uy/uz as packed S2 (populate),
|
||||
* ctc2 RT chain into C2[0..4] (matrix_vector), MVMVA RT*(-eye)>>12, store off
|
||||
* directly to look_at->t[] (trans_matrix). Replaces the previous 3 separate atoms
|
||||
* (populate + matrix_vector + trans_matrix).
|
||||
*
|
||||
* MT3_S2S4 { A3x3_S2 m; A3_S4 t; }
|
||||
* m[][] is S2 packed (9 × 2 = 18 bytes at offset 0)
|
||||
* t[0..2] is S4 (3 × 4 = 12 bytes at offset 18)
|
||||
* directly to look_at->t[] (trans_matrix).
|
||||
*
|
||||
* C11 ApplyMatrixLV semantics (gte.atom.c ac_apply_matrix_lv; libgte reference):
|
||||
* 1. ctc2 RT matrix (5 ctc2s to C2[0..4])
|
||||
@@ -197,82 +177,51 @@ typedef Struct_(RegUse_resolve_look_at__pop_mv_trans) {
|
||||
* 4. mtc2 to IR1/2/3, nop2, MVMVA pass2 (sf=1, mx=0, v=3, cv=3)
|
||||
* 5. mfc2 MACs → off
|
||||
* 6. store off to look_at->t[] (skip scratch.eye intermediate)
|
||||
*
|
||||
* GPR codes (assigned by resolve_look_at_init):
|
||||
* r_scratch : R_ResolveScratch (R_T4 carrier)
|
||||
* r_look_at : ralloc() — also serves as the off-dst in the trans_matrix phase
|
||||
* r_row : V3_S4, reused for ux/uy/uz loads in populate phase
|
||||
* r_eye : ralloc() — &scratch.eye, used for -eye load in matrix_vector phase
|
||||
* r_v_x/v_y/v_z : ralloc() — populate scratch addrs (ux/uy/uz), reused as
|
||||
* ctc2 transfer + MVMVA -eye temp in matrix_vector phase
|
||||
* (v_x/v_y/v_z alias ux/uy/uz via the union; lifetime ends for ux/uy/uz after
|
||||
* populate's mac_load_v3s4, so reusing for v.x/v.y/v.z is safe)
|
||||
* Pool cost: 1 carrier + 1 look_at + 3 row + 1 eye + 3 aliased = 9 GPRs
|
||||
*
|
||||
* Net word savings vs the previous 3-atom flow: ~15 words + 2 mac_yields + 1 tape pop.
|
||||
* - 2 mac_yields (trans_matrix's + matrix_vector's) → fused into one yield
|
||||
* - 1 redundant tb_data (look_at was pushed 2x; now once)
|
||||
* - mac_trans_mt3s3s4 (6 words) → replaced by direct mac_store_v3s4
|
||||
* - mac_store_v3s4 to scratch.eye (3 words intermediate) → eliminated
|
||||
* - add_si for r_off_ptr (2 words) → eliminated
|
||||
* - mac_store_v3s4 zero-store of t[] (3 words) → eliminated (matrix_vector writes
|
||||
* off directly; no consumer needed the zero first)
|
||||
* - 1 set_gte_mt3s2s4 ctc2 chain (13 baked words) → eliminated (matrix_vector
|
||||
* has its own ctc2 RT chain; cube rendering atoms reload C2 state themselves)
|
||||
*/
|
||||
internal MipsAtom* resolve_look_at__pop_mv_trans(AtomArena_R aa,
|
||||
RegUse_resolve_look_at__pop_mv_trans r
|
||||
) MipsAtom_Proc_(aa, {
|
||||
internal MipsAtom* AtomBundleEntry_(resolve_look_at,populate_mt3s4s2)(AtomArena_R aa, RegUse_resolve_look_at_populate_mt3s4s2 r)
|
||||
atom_info(atom_bind(Binds_ResolveLookAt_PopulateMT3S4S2)) MipsAtom_Proc_(aa, {
|
||||
/* --- Tape pop: look_at pointer --- */
|
||||
load_word(r.look_at, R_TapePtr, O_(Binds_ResolveLookAtPopMvTrans,look_at)),
|
||||
LdSlot_ add_ui_self(R_TapePtr, S_(Binds_ResolveLookAtPopMvTrans)),
|
||||
load_word(r.look_at, R_TapePtr, O_(Binds_ResolveLookAt_PopulateMT3S4S2,look_at)),
|
||||
LdSlot_ add_ui_self( R_TapePtr, S_(Binds_ResolveLookAt_PopulateMT3S4S2)),
|
||||
|
||||
/* --- Scratch addresses for ux/uy/uz/eye (populate phase; t6/t7/t8 alias ux/uy/uz).
|
||||
* R_ScratchBase (= R_SP) holds 0x1F800000; no per-atom bake is required because
|
||||
* R_SP is a tape carrier preserved across atoms. --- */
|
||||
add_si(r.t6.ux, R_ScratchBase, O_(ResolveLookAtScratch, ux)), LdSlot_
|
||||
add_si(r.t7.uy, R_ScratchBase, O_(ResolveLookAtScratch, uy)),
|
||||
add_si(r.t8.uz, R_ScratchBase, O_(ResolveLookAtScratch, uz)),
|
||||
add_si(r.ux, R_ScratchBase, O_(ResolveLookAtScratch, ux)), LdSlot_
|
||||
add_si(r.uy, R_ScratchBase, O_(ResolveLookAtScratch, uy)),
|
||||
add_si(r.uz, R_ScratchBase, O_(ResolveLookAtScratch, uz)),
|
||||
add_si(r.eye, R_ScratchBase, O_(ResolveLookAtScratch, eye)),
|
||||
|
||||
/* --- POPULATE phase: write look_at->m[][] from ux/uy/uz as packed S2 --- */
|
||||
mac_load_v3s4(r.row, r.t6.ux, 0), LdSlot_ mac_store_v3s2(r.row, r.look_at, O_(MT3_S2S4, m[0])),
|
||||
mac_load_v3s4(r.row, r.t7.uy, 0), LdSlot_ mac_store_v3s2(r.row, r.look_at, O_(MT3_S2S4, m[1])),
|
||||
mac_load_v3s4(r.row, r.t8.uz, 0), LdSlot_ mac_store_v3s2(r.row, r.look_at, O_(MT3_S2S4, m[2])),
|
||||
/* write look_at->m[][] from ux/uy/uz as packed S2 */
|
||||
mac_load_v3s4(r.row, r.ux, 0), LdSlot_ mac_store_v3s2(r.row, r.look_at, O_(MT3_S2S4, m[0])),
|
||||
mac_load_v3s4(r.row, r.uy, 0), LdSlot_ mac_store_v3s2(r.row, r.look_at, O_(MT3_S2S4, m[1])),
|
||||
mac_load_v3s4(r.row, r.uz, 0), LdSlot_ mac_store_v3s2(r.row, r.look_at, O_(MT3_S2S4, m[2])),
|
||||
|
||||
/* --- MATRIX-VECTOR phase: ctc2 RT chain + MVMVA RT*(-eye)>>12 --- */
|
||||
/* RT packing (per libgte ApplyMatrixLV convention; see gte.h:217-220 +
|
||||
* atom_6b_disasm_comparison.md:28-32):
|
||||
* C2[0] = (RT12<<16)|RT11 ← ctc2 RT11 from m[0][0..1] packed word
|
||||
* C2[1] = (RT21<<16)|RT13 ← ctc2 RT12 from m[0][2..3] packed word
|
||||
* C2[2] = (RT23<<16)|RT22 ← ctc2 RT13 from m[1][1..2] packed word
|
||||
* C2[3] = (RT32<<16)|RT31 ← ctc2 RT21 from m[2][0..1] packed word
|
||||
* C2[4] = (RT33<<16)|junk ← ctc2 RT22 from m[2][2] (half)
|
||||
* Each ctc2 writes a WHOLE 32-bit C2 slot; the "macro name" identifies
|
||||
* which C2 register, not which 16-bit half. */
|
||||
load_word( r.t6.v_x, r.look_at, O_(MT3_S2S4, m[0][0])), /* RT11|RT12 */ LdSlot_
|
||||
load_word( r.t7.v_y, r.look_at, O_(MT3_S2S4, m[0][2])), /* RT13|RT21 */ LdSlot_ gte_mv_to_ctrl_r(r.t6.v_x, gte_cr_RT11),
|
||||
load_word( r.t8.v_z, r.look_at, O_(MT3_S2S4, m[1][1])), /* RT22|RT23 */ LdSlot_ gte_mv_to_ctrl_r(r.t7.v_y, gte_cr_RT12),
|
||||
load_word( r.t6.v_x, r.look_at, O_(MT3_S2S4, m[2][0])), /* RT31|RT32 */ LdSlot_ gte_mv_to_ctrl_r(r.t8.v_z, gte_cr_RT13),
|
||||
load_half_u(r.t7.v_y, r.look_at, O_(MT3_S2S4, m[2][2])), /* RT33 */ LdSlot_ gte_mv_to_ctrl_r(r.t6.v_x, gte_cr_RT21),
|
||||
/* pos = -eye. The three loads also retire the last CTC2. */ gte_mv_to_ctrl_r(r.t7.v_y, gte_cr_RT22),
|
||||
GteDelay_ mac_load_word_v3(r.t6.v_x, r.t7.v_y, r.t8.v_z, r.eye, 0), LdSlot_
|
||||
mac_sub_s_v3(r.t6.v_x, r.t7.v_y, r.t8.v_z, R_0, R_0, R_0, r.t6.v_x, r.t7.v_y, r.t8.v_z),
|
||||
/* mtc2 pos (as S16) to IR1/2/3. The GTE takes low 16 bits. pos fits in S16. For negative pos, the 32-bit sign-extended value's low 16 bits = correct S16. */
|
||||
gte_mv_to_data_r(r.t6.v_x, C2_IR1),
|
||||
gte_mv_to_data_r(r.t7.v_y, C2_IR2),
|
||||
gte_mv_to_data_r(r.t8.v_z, C2_IR3),
|
||||
/* ctc2 RT chain + MVMVA RT * (-eye) >> 12 */
|
||||
/* C2[0] = (RT12<<16)|RT11 ← ctc2 RT11 from m[0][0..1]
|
||||
* C2[1] = (RT21<<16)|RT13 ← ctc2 RT12 from m[0][2..3]
|
||||
* C2[2] = (RT23<<16)|RT22 ← ctc2 RT13 from m[1][1..2]
|
||||
* C2[3] = (RT32<<16)|RT31 ← ctc2 RT21 from m[2][0..1]
|
||||
* C2[4] = (RT33<<16)|junk ← ctc2 RT22 from m[2][2] (half) */
|
||||
load_word( r.vx, r.look_at, O_(MT3_S2S4, m[0][0])), /* RT11|RT12 */ LdSlot_
|
||||
load_word( r.vy, r.look_at, O_(MT3_S2S4, m[0][2])), /* RT13|RT21 */ LdSlot_ gte_mv_to_ctrl_r(r.vx, gte_cr_RT11),
|
||||
load_word( r.vz, r.look_at, O_(MT3_S2S4, m[1][1])), /* RT22|RT23 */ LdSlot_ gte_mv_to_ctrl_r(r.vy, gte_cr_RT12),
|
||||
load_word( r.vx, r.look_at, O_(MT3_S2S4, m[2][0])), /* RT31|RT32 */ LdSlot_ gte_mv_to_ctrl_r(r.vz, gte_cr_RT13),
|
||||
load_half_u(r.vy, r.look_at, O_(MT3_S2S4, m[2][2])), /* RT33 */ LdSlot_ gte_mv_to_ctrl_r(r.vx, gte_cr_RT21),
|
||||
|
||||
GteDelay_ mac_load_word_v3(r.vx, r.vy, r.vz, r.eye, 0), LdSlot_
|
||||
mac_sub_s_v3(r.vx, r.vy, r.vz, R_0, R_0, R_0, r.vx, r.vy, r.vz),
|
||||
|
||||
gte_mv_to_data_r(r.vx, C2_IR1),
|
||||
gte_mv_to_data_r(r.vy, C2_IR2),
|
||||
gte_mv_to_data_r(r.vz, C2_IR3),
|
||||
GteDelay_ nop2,
|
||||
|
||||
/* MVMVA pass 2 — C11 ApplyMatrixLV command.
|
||||
* sf=1, mx=0 (RT), v=3 (IR), cv=3. Reads RT × IR >> 12. */
|
||||
gte_cmdw_mvmva_c11_pass2, GteDelay_ nop,
|
||||
mac_gte_mv_from_data_r_mac123(r.t6.v_x, r.t7.v_y, r.t8.v_z), GteDelay_ nop,
|
||||
/* MVMVA pass 2 — C11 ApplyMatrixLV command. sf=1, mx=0 (RT), v=3 (IR), cv=3. Reads RT × IR >> 12. */
|
||||
gte_cmdw_mvmva_c11_pass2, GteDelay_ load_word(R_AtomJmp, R_TapePtr, 0), // ac_yield: word 1
|
||||
mac_gte_mv_from_data_r_mac123(r.vx, r.vy, r.vz), GteDelay_ add_ui_self( R_TapePtr, S_(MipsCode)), // ac_yield: word 2
|
||||
|
||||
/* --- TRANS-MATRIX phase: store off directly to look_at->t[] (skip scratch.eye intermediate) --- */
|
||||
mac_store_word_v3(r.t6.v_x, r.t7.v_y, r.t8.v_z, r.look_at, O_(MT3_S2S4, t)),
|
||||
/* store off directly to look_at->t[] (skip scratch.eye intermediate) */
|
||||
mac_store_word_v3(r.vx, r.vy, r.vz, r.look_at, O_(MT3_S2S4, t)),
|
||||
|
||||
mac_yield()
|
||||
jump_reg(R_AtomJmp), BdSlot_ nop, // ac_yield: word 3-4
|
||||
})
|
||||
#pragma endregion resolve_look_at
|
||||
|
||||
@@ -288,8 +237,8 @@ enum {
|
||||
};
|
||||
//screen_env_init. Mirrors the libpsyx's SetDefDispEnv + SetDefDrawEnv + the manual enable_auto_clear / initial_bg_color writes.
|
||||
internal MipsAtom_(screen_env_init) atom_info(atom_phase(screen_init)
|
||||
, atom_reads(R_T0, R_ScreenX, R_ScreenY, R_ScreenBuf)
|
||||
, atom_writes(R_T0, R_ScreenX, R_ScreenY)
|
||||
, atom_reads(R_ScreenBuf)
|
||||
, atom_writes(R_ScreenBuf)
|
||||
) {
|
||||
/* display[0] = (0, 0, 320, 240); rest of struct zeroed. */
|
||||
add_ui(R_ScreenX, R_0, ScreenRes_X), add_ui(R_ScreenY, R_0, ScreenRes_Y),
|
||||
@@ -339,23 +288,6 @@ internal MipsAtom_(screen_env_init) atom_info(atom_phase(screen_init)
|
||||
mac_yield(),
|
||||
};
|
||||
|
||||
/* gp_screen_init's GPR setup. Tests the mixed user-pinning + auto-reg pattern:
|
||||
* - R_IO_BaseAddr = R_T4 (user-pinned via atom_reg; pre-existing)
|
||||
* - R_GP1_Offset = R_T2 (user-pinned via atom_reg; NEW -- for GPIO_PORT1_OFFSET)
|
||||
* - R_ScreenX = R_T5 (user-pinned via atom_reg; used as a transfer and GTE setup reg)
|
||||
* - R_GpTmp = auto-allocated by the lua pass and used for several GPU transfers;
|
||||
* the C preprocessor resolves it to the chosen free pool GPR.
|
||||
*
|
||||
* For gp_screen_init, the auto-reg pool exclusions are:
|
||||
* user_pinned (from the corpus register_alias_registry) : R_T0..R_T7 (all 8 user-pinned across hello_camera.atom.c)
|
||||
* body-parsed physical registers : aliases resolve through the registry;
|
||||
* the body uses R_ScreenX, not raw R_T5
|
||||
* source_pool after both subtractions : {R_V0, R_V1} only
|
||||
* R_GpTmp gets R_V0 (the first-fit choice). Its repeated GPU-transfer use proves that the
|
||||
* auto-reg allocation is active while the R_ScreenX references prove the pinned alias is used.
|
||||
* R_TapePtr (R_T9), R_AtomJmp (R_T8), R_AT are excluded from the POOL by construction in
|
||||
* passes/auto_reg.lua -- see the "obvious exclusions" comment block at the top of that file.
|
||||
*/
|
||||
enum {
|
||||
R_IO_BaseAddr = R_T4 atom_reg, /* Caller-pinned: IO_BASE_ADDR = 0x1F800000 */
|
||||
R_GP1_Offset = R_T2 atom_reg, /* Caller-pinned: GPIO_PORT1_OFFSET = 0x10 */
|
||||
@@ -509,7 +441,7 @@ typedef Struct_(Binds_PadInputCam) {
|
||||
Camera* cam;
|
||||
};
|
||||
internal MipsAtom_(pad_input_cam) atom_info(atom_bind(Binds_PadInputCam)
|
||||
, atom_reads( R_Cam, R_CamPadState, R_TapePtr)
|
||||
, atom_reads( R_Cam, R_CamPadState)
|
||||
, atom_writes(R_Cam)
|
||||
) {
|
||||
/* Bind pop: state → R_CamPadState (R_T5), cam → R_Cam (R_T4), advance R_TapePtr by 8. */
|
||||
@@ -522,7 +454,7 @@ internal MipsAtom_(pad_input_cam) atom_info(atom_bind(Binds_PadInputCam)
|
||||
load_word(R_T1, R_Cam, O_(Camera,pos.x)),
|
||||
|
||||
// D-pad Left → cam.pos.x -= 50. and_i fulfills BD-slot for load on R_Cam.
|
||||
LdSlot_ and_i(R_T3, R_T0, Pad_Left), branch_le_zero(R_T3, atom_offset(left_x, exit_left_x)), BdSlot_ mac_yield_load(), LdSlot_
|
||||
LdSlot_ and_i(R_T3, R_T0, Pad_Left), branch_le_zero(R_T3, atom_offset(left_x, exit_left_x)), BdSlot_ nop,
|
||||
add_si(R_T1, R_T1, -50), store_word(R_T1, R_Cam, O_(Camera,pos.x)),
|
||||
atom_label(exit_left_x)
|
||||
|
||||
@@ -544,16 +476,16 @@ internal MipsAtom_(pad_input_cam) atom_info(atom_bind(Binds_PadInputCam)
|
||||
|
||||
/* D-pad Cross → cam.pos.z -= 50. Load pos.z BEFORE the andi. */
|
||||
load_word(R_T1, R_Cam, O_(Camera,pos.z)), LdSlot_
|
||||
and_i(R_T3, R_T0, Pad_Cross), branch_le_zero(R_T3, atom_offset(cross_z, exit_cross_z)), BdSlot_ nop,
|
||||
and_i(R_T3, R_T0, Pad_Cross), branch_le_zero(R_T3, atom_offset(cross_z, exit_cross_z)), BdSlot_ load_word(R_AtomJmp, R_TapePtr, 0), LdSlot_ // ac_yield: word 1
|
||||
add_si(R_T1, R_T1, -50), store_word(R_T1, R_Cam, O_(Camera,pos.z)),
|
||||
atom_label(exit_cross_z)
|
||||
|
||||
/* D-pad Circle → cam.pos.z += 50. Reuses R_T1 from Cross. */
|
||||
and_i(R_T3, R_T0, Pad_Circle), branch_le_zero(R_T3, atom_offset(circle_z, exit_circle_z)), BdSlot_ nop,
|
||||
and_i(R_T3, R_T0, Pad_Circle), branch_le_zero(R_T3, atom_offset(circle_z, exit_circle_z)), BdSlot_ add_ui_self(R_TapePtr, S_(MipsCode)), // ac_yield: word 2
|
||||
add_si(R_T1, R_T1, 50), store_word(R_T1, R_Cam, O_(Camera,pos.z)),
|
||||
atom_label(exit_circle_z)
|
||||
|
||||
mac_yield_tail(),
|
||||
jump_reg(R_AtomJmp), BdSlot_ nop // ac_yield: word 3-4
|
||||
};
|
||||
|
||||
enum {
|
||||
@@ -567,20 +499,17 @@ enum {
|
||||
#define R_OtBase_Code R_T6_Code
|
||||
};
|
||||
typedef Struct_(Binds_CubeTri) {
|
||||
U4 PrimCursor;
|
||||
V4_S2* FaceCursor;
|
||||
V3_S2* VertBase;
|
||||
U4* OtBase;
|
||||
U1* prim_cursor;
|
||||
V4_S2* face_cursor;
|
||||
V3_S2* vert_base;
|
||||
U4* ot_base;
|
||||
};
|
||||
internal MipsAtom_(rbind_cube_g4_face) atom_info(atom_bind(Binds_CubeTri), atom_phase(cube_g4)
|
||||
, atom_reads(R_TapePtr)
|
||||
, atom_writes(R_PrimCursor, R_FaceCursor, R_VertBase, R_OtBase, R_TapePtr)
|
||||
){
|
||||
internal MipsAtom_(rbind_cube_g4_face) atom_info(atom_bind(Binds_CubeTri), atom_phase(cube_g4)){
|
||||
/* Pop 4 arguments from the tape directly into the workspace registers */
|
||||
load_word(R_PrimCursor, R_TapePtr, O_(Binds_CubeTri,PrimCursor)),
|
||||
load_word(R_FaceCursor, R_TapePtr, O_(Binds_CubeTri,FaceCursor)),
|
||||
load_word(R_VertBase, R_TapePtr, O_(Binds_CubeTri,VertBase)),
|
||||
load_word(R_OtBase, R_TapePtr, O_(Binds_CubeTri,OtBase)),
|
||||
load_word(R_PrimCursor, R_TapePtr, O_(Binds_CubeTri,prim_cursor)),
|
||||
load_word(R_FaceCursor, R_TapePtr, O_(Binds_CubeTri,face_cursor)),
|
||||
load_word(R_VertBase, R_TapePtr, O_(Binds_CubeTri,vert_base)),
|
||||
load_word(R_OtBase, R_TapePtr, O_(Binds_CubeTri,ot_base)),
|
||||
LdSlot_ add_ui_self( R_TapePtr, S_(Binds_CubeTri)),
|
||||
mac_yield()
|
||||
};
|
||||
@@ -596,11 +525,13 @@ MipsAtom_(cube_g4_face) atom_info(atom_phase(cube_g4),
|
||||
load_half_u(R_T2, R_FaceCursor, 2 * S_(S2)),
|
||||
// load_half_u(R_T3, R_FaceCursor, 3 * S_(S2)),
|
||||
|
||||
LdSlot_ mac_gte_load_tri_verts(R_VertBase, R_T0, R_T1, R_T2), GteDelay_ load_half_u(R_T3, R_FaceCursor, 3 * S_(S2)), LdSlot_
|
||||
GteDelay_ nop, gte_cmdw_rotate_translate_perspective_triple,
|
||||
LdSlot_ mac_gte_load_tri_verts(R_VertBase, R_T0, R_T1, R_T2),
|
||||
GteDelay_ load_half_u(R_T3, R_FaceCursor, 3 * S_(S2)), LdSlot_
|
||||
GteDelay_ load_word(R_AtomJmp, R_TapePtr, 0), LdSlot_ //ac_yield: word 1,
|
||||
gte_cmdw_rotate_translate_perspective_triple,
|
||||
gte_cmdw_nclip,
|
||||
|
||||
gte_mv_from_data_r(R_T0, C2_MAC0), GteDelay_ nop,
|
||||
gte_mv_from_data_r(R_T0, C2_MAC0), GteDelay_ add_ui_self(R_TapePtr, S_(MipsCode)), // ac_yield: word 2
|
||||
branch_le_zero(R_T0, atom_offset(cull, cube_g4_face_exit)),
|
||||
/* BD-slot: Write the prim tag (R_0=0; overwrites the legacy tag word in the prim_buffer).
|
||||
* If branch IS taken (face culled), the body is skipped and this 0-tag is stranded —
|
||||
@@ -630,16 +561,16 @@ MipsAtom_(cube_g4_face) atom_info(atom_phase(cube_g4),
|
||||
// end: branch(cull)
|
||||
|
||||
atom_label(cube_g4_face_exit)
|
||||
add_ui_self(R_PrimCursor, S_(Poly_G4)), /* 9 words = Poly_G4 */
|
||||
add_ui_self(R_FaceCursor, S_(S2) * 4), /* 4 × S2 = 8 bytes */
|
||||
mac_yield()
|
||||
add_ui_self(R_PrimCursor, S_(Poly_G4)), // 9 words = Poly_G4
|
||||
add_ui_self(R_FaceCursor, S_(S2) * 4), // 4 × S2 = 8 bytes
|
||||
jump_reg(R_AtomJmp), BdSlot_ nop // ac_yield: word 3-4
|
||||
};
|
||||
|
||||
typedef Struct_(Binds_FloorTri) {
|
||||
U4 PrimCursor;
|
||||
V3_S2* FaceCursor;
|
||||
V3_S2* VertBase;
|
||||
U4* OtBase;
|
||||
U1* prim_cursor;
|
||||
V3_S2* face_cursor;
|
||||
V3_S2* vert_base;
|
||||
U4* ot_base;
|
||||
};
|
||||
internal
|
||||
MipsAtom_(rbind_floor_f3_face) atom_info(atom_bind(Binds_FloorTri), atom_phase(floor_f3)
|
||||
@@ -647,10 +578,10 @@ MipsAtom_(rbind_floor_f3_face) atom_info(atom_bind(Binds_FloorTri), atom_phase(f
|
||||
, atom_writes(R_PrimCursor, R_FaceCursor, R_VertBase, R_OtBase, R_TapePtr)
|
||||
){
|
||||
/* Pop 4 arguments from the tape directly into the workspace registers */
|
||||
load_word(R_PrimCursor, R_TapePtr, O_(Binds_FloorTri,PrimCursor)),
|
||||
load_word(R_FaceCursor, R_TapePtr, O_(Binds_FloorTri,FaceCursor)),
|
||||
load_word(R_VertBase, R_TapePtr, O_(Binds_FloorTri,VertBase)),
|
||||
load_word(R_OtBase, R_TapePtr, O_(Binds_FloorTri,OtBase)),
|
||||
load_word(R_PrimCursor, R_TapePtr, O_(Binds_FloorTri,prim_cursor)),
|
||||
load_word(R_FaceCursor, R_TapePtr, O_(Binds_FloorTri,face_cursor)),
|
||||
load_word(R_VertBase, R_TapePtr, O_(Binds_FloorTri,vert_base)),
|
||||
load_word(R_OtBase, R_TapePtr, O_(Binds_FloorTri,ot_base)),
|
||||
LdSlot_ add_ui_self( R_TapePtr, S_(Binds_FloorTri)),
|
||||
mac_yield()
|
||||
};
|
||||
@@ -662,13 +593,13 @@ MipsAtom_(floor_f3_face) atom_info(atom_phase(floor_f3)
|
||||
, atom_writes(R_PrimCursor, R_FaceCursor)
|
||||
) {
|
||||
mac_load_tri_indices(R_FaceCursor, R_T0, R_T1, R_T2),
|
||||
mac_gte_load_tri_verts(R_VertBase, R_T0, R_T1, R_T2),
|
||||
nop2, gte_cmdw_rotate_translate_perspective_triple, // 2 nops retire the final cpu -> gte writes before RTPT
|
||||
mac_gte_load_tri_verts(R_VertBase, R_T0, R_T1, R_T2), GteDelay_ nop2,
|
||||
gte_cmdw_rotate_translate_perspective_triple, // 2 nops retire the final cpu -> gte writes before RTPT
|
||||
gte_cmdw_nclip,
|
||||
|
||||
/* Culling (Branch forward if Backface) */
|
||||
gte_mv_from_data_r(R_T0, C2_MAC0),
|
||||
nop, branch_le_zero(R_T0, atom_offset(culling, floor_f3_face_exit)), nop, // required gte -> cpu load-delay slot.
|
||||
gte_mv_from_data_r(R_T0, C2_MAC0), GteDelay_ load_word(R_AtomJmp, R_TapePtr, 0), // ac_yield: word 1
|
||||
branch_le_zero(R_T0, atom_offset(culling, floor_f3_face_exit)), BdSlot_ add_ui_self(R_TapePtr, S_(MipsCode)), // ac_yield: word 2
|
||||
/* Format Primitive */
|
||||
mac_gte_store_f3(R_PrimCursor),
|
||||
|
||||
@@ -678,7 +609,7 @@ MipsAtom_(floor_f3_face) atom_info(atom_phase(floor_f3)
|
||||
/* Bounds Check OTZ < 2048 (Branch forward to skip insertion) */
|
||||
add_ui( R_AT, R_0, OrderingTbl_Len),
|
||||
set_lt_u( R_AT, R_T1, R_AT),
|
||||
branch_equal(R_AT, R_0, atom_offset(bounds_chk, floor_f3_face_exit)), nop,
|
||||
branch_equal(R_AT, R_0, atom_offset(bounds_chk, floor_f3_face_exit)), BdSlot_ nop,
|
||||
mac_format_f3_color(R_PrimCursor, 0xFF, 0xFF, 0xFF), // RGB-form (R=FF, G=FF, B=FF = white)
|
||||
mac_insert_ot_tag(R_OtBase, R_PrimCursor, S_(Poly_F3)), /* Insert into Ordering Table Linked List */
|
||||
add_ui_self(R_PrimCursor, S_(Poly_F3)), /* Advance Prim Cursor (5 words) */
|
||||
@@ -689,13 +620,12 @@ MipsAtom_(floor_f3_face) atom_info(atom_phase(floor_f3)
|
||||
/* Advance Input Cursor & Yield (Both branch targets land here) */
|
||||
atom_label(floor_f3_face_exit)
|
||||
add_ui_self(R_FaceCursor, S_(S2) * 4), /* Advance Face Cursor (4 * S2 = 8 bytes) */
|
||||
mac_yield()
|
||||
jump_reg(R_AtomJmp), BdSlot_ nop // ac_yield: word 3-4
|
||||
};
|
||||
|
||||
typedef Struct_(Binds_SyncPrimitiveArena) { U4 used; U4 cursor; };
|
||||
typedef Struct_(Binds_SyncPrimitiveArena) { U4* used; U1* cursor; };
|
||||
internal MipsAtom_(sync_primitive_arena) atom_info(atom_bind(Binds_SyncPrimitiveArena)
|
||||
, atom_reads( R_TapePtr, R_PrimCursor)
|
||||
, atom_writes(R_TapePtr)
|
||||
, atom_reads(R_PrimCursor), atom_writes(R_AT)
|
||||
){
|
||||
load_word(R_AT, R_TapePtr, O_(Binds_SyncPrimitiveArena,used)),
|
||||
load_word(R_T0, R_TapePtr, O_(Binds_SyncPrimitiveArena,cursor)), LdSlot_
|
||||
|
||||
+113
-141
@@ -24,7 +24,7 @@
|
||||
#include "duffle/pad.h"
|
||||
|
||||
#include "duffle/dsl.atom.h"
|
||||
#include "duffle/lottes_tape.h"
|
||||
#include "duffle/tape.h"
|
||||
|
||||
#include "duffle/bios.h"
|
||||
#include "duffle/psyq.h"
|
||||
@@ -80,9 +80,6 @@ typedef Struct_(SMemory) {
|
||||
PadBiosRaw pad_raw[2];
|
||||
PadState pad[2];
|
||||
|
||||
// TODO(Ed): We don't need this we can just cast at any point an address to a desired view of scratchpad, we have the address.
|
||||
U4_V scratchpad; // d-cache
|
||||
|
||||
U1 ct_init_atom_mem[CT_InitAtomMem_Size];
|
||||
MipsAtom* normalize_v3s4;
|
||||
MipsAtom* gte_cross_v3s4;
|
||||
@@ -96,11 +93,11 @@ extern SMemory smem;
|
||||
#define pad0_btn_(btn) btn & smem.pad[0].buttons
|
||||
#define pad1_btn_(btn) btn & smem.pad[1].buttons
|
||||
|
||||
I_ B1* prim__alloc(U4 type_width, Str8 type_name) {
|
||||
I_ U1* prim__alloc(U4 type_width, Str8 type_name) {
|
||||
gknown PrimitiveArena* pa = & smem.primitives;
|
||||
gknown B1* buf = (B1*) r_(smem.primitives.buf)[smem.active_buf_id];
|
||||
gknown U1* buf = (U1*) r_(smem.primitives.buf)[smem.active_buf_id];
|
||||
assert(pa->used + type_width < PrimitiveBuff_Len);
|
||||
B1* next = buf + pa->used;
|
||||
U1* next = buf + pa->used;
|
||||
pa->used += type_width;
|
||||
return next;
|
||||
}
|
||||
@@ -114,10 +111,10 @@ I_ void resolve_look_at_c11(MT3_S2S4* look_at, P3_S4* eye, P3_S4* target, V3_S4*
|
||||
V3_S4 pos, off;
|
||||
|
||||
forward = target[0]; sub_v3s4(& forward, eye[0]); // RGA(Lengyel): Affine point - point = zero-weight direction.
|
||||
normalize_v3s4(& forward, & uz); // RGA(Lengyel): Normalize the direction bulk. Not finite-point unitization.
|
||||
psy_normalize_v3s4(& forward, & uz); // RGA(Lengyel): Normalize the direction bulk. Not finite-point unitization.
|
||||
|
||||
cross_v3s4(& uz, up_in, & right); normalize_v3s4(& right, & ux); // RGA(Lengyel): Complement(Wedge(forward, up_in)) -> right axis.
|
||||
cross_v3s4(& uz, & ux, & up); normalize_v3s4(& up, & uy); // RGA(Lengyel): Complement(Wedge(forward, right)) -> up axis.
|
||||
cross_v3s4(& uz, up_in, & right); psy_normalize_v3s4(& right, & ux); // RGA(Lengyel): Complement(Wedge(forward, up_in)) -> right axis.
|
||||
cross_v3s4(& uz, & ux, & up); psy_normalize_v3s4(& up, & uy); // RGA(Lengyel): Complement(Wedge(forward, right)) -> up axis.
|
||||
|
||||
// RGA(Lengyel): matrix expansion of the world-to-camera rotation (basis rows).
|
||||
look_at->m[0][0] = ux.x; look_at->m[0][1] = ux.y; look_at->m[0][2] = ux.z;
|
||||
@@ -143,21 +140,21 @@ internal void compile_init_atoms(void) {
|
||||
RegUse_(gte_cross_v3s4) {
|
||||
.a = ralloc_v3(),
|
||||
.b = ralloc_v3(),
|
||||
.x = ralloc(),
|
||||
.y = ralloc(),
|
||||
.z = ralloc(),
|
||||
.out = ralloc(),
|
||||
.src_a = ralloc(),
|
||||
.src_b = ralloc(),
|
||||
});
|
||||
regfile_reset(& rf);
|
||||
|
||||
smem.normalize_v3s4 = build_normalize_v3s4(& ab,
|
||||
RegUse_(build_normalize_v3s4) {
|
||||
smem.normalize_v3s4 = normalize_v3s4(& ab,
|
||||
RegUse_(normalize_v3s4) {
|
||||
.res = ralloc_v3(),
|
||||
.t0 = ralloc(),
|
||||
.t1 = ralloc(),
|
||||
.t2 = ralloc(),
|
||||
.t3 = ralloc(),
|
||||
.t4 = ralloc(),
|
||||
.t5 = ralloc(),
|
||||
.r0 = ralloc(),
|
||||
.r1 = ralloc(),
|
||||
.r2 = ralloc(),
|
||||
.r3 = ralloc(),
|
||||
.r4 = ralloc(),
|
||||
.r5 = ralloc(),
|
||||
});
|
||||
regfile_reset(& rf);
|
||||
|
||||
@@ -167,25 +164,19 @@ internal void compile_init_atoms(void) {
|
||||
}
|
||||
|
||||
internal void compile_resolve_look_at(void) {
|
||||
AtomArena ab = atomarena_make(slice_ut_arr(smem.resolve_look_at_mem));
|
||||
AtomBundle_resolve_look_at_R bundle = C_(void*, smem.resolve_look_at_bundle);
|
||||
|
||||
/* R_ScratchBase (= R_SP) is a tape carrier preserved across atoms; no carrier
|
||||
* pin is needed in the regfile. The standard 24-register pool is sufficient. */
|
||||
AtomArena ab = atomarena_make(slice_ut_arr(smem.resolve_look_at_mem));
|
||||
RegFile rf = regfile(regfile_abi_mask);
|
||||
#define ralloc() regfile_alloc(& rf)
|
||||
#define ralloc_v3() { ralloc(), ralloc(), ralloc() }
|
||||
|
||||
bundle->input_and_sub = AtomBundleEntry_(resolve_look_at, input_and_sub)(& ab,
|
||||
RegUse_(resolve_look_at_input_and_sub) {
|
||||
.target = ralloc(),
|
||||
.eye = ralloc(),
|
||||
.up_in = ralloc(),
|
||||
.t0 = ralloc(),
|
||||
.t1 = ralloc(),
|
||||
.t2 = ralloc(),
|
||||
.t3 = ralloc(),
|
||||
.t4 = ralloc(),
|
||||
.target_ptr = ralloc(),
|
||||
.eye_ptr = ralloc(),
|
||||
.up_in_ptr = ralloc(),
|
||||
.up_in = ralloc_v3(),
|
||||
.r012 = ralloc_v3(),
|
||||
.r345 = {ralloc(), R_AT, ralloc() },
|
||||
});
|
||||
regfile_reset(& rf);
|
||||
|
||||
@@ -195,85 +186,70 @@ internal void compile_resolve_look_at(void) {
|
||||
bundle->cross_to_up = smem.gte_cross_v3s4;
|
||||
bundle->normalize_up_uy = smem.normalize_v3s4;
|
||||
|
||||
bundle->pop_mv_trans = resolve_look_at__pop_mv_trans(& ab,
|
||||
RegUse_(resolve_look_at__pop_mv_trans){
|
||||
bundle->populate_mt3s4s2 = AtomBundleEntry_(resolve_look_at,populate_mt3s4s2)(& ab,
|
||||
RegUse_(resolve_look_at_populate_mt3s4s2){
|
||||
.look_at = ralloc(),
|
||||
.eye = ralloc(),
|
||||
.row = ralloc_v3(),
|
||||
.t6 = ralloc(),
|
||||
.t7 = ralloc(),
|
||||
.t8 = ralloc(),
|
||||
.r0 = ralloc(),
|
||||
.r1 = ralloc(),
|
||||
.r2 = ralloc(),
|
||||
});
|
||||
|
||||
/* Sanity check: arena didn't overflow. */
|
||||
assert(ab.used <= ResolveLookAtArena_Size);
|
||||
assert(ab.used <= ResolveLookAtArena_Size); // Sanity check: arena didn't overflow.
|
||||
#undef ralloc
|
||||
}
|
||||
|
||||
/* Emit the resolve_look_at bundle into the tape. Called once per frame from update(). */
|
||||
I_ void resolve_look_at(TapeBuilder_R tb
|
||||
, MT3_S2S4* look_at
|
||||
, P3_S4* eye
|
||||
, P3_S4* target
|
||||
, V3_S4* up_in
|
||||
){
|
||||
// Emit the resolve_look_at bundle into the tape. Called once per frame from update().
|
||||
I_ void resolve_look_at(TapeBuilder_R tb, MT3_S2S4* look_at, P3_S4* eye, P3_S4* target, V3_S4* up_in) {
|
||||
/* Typed view of the scratchpad for field-address arithmetic. */
|
||||
ResolveLookAtScratch* sp = C_scratch(ResolveLookAtScratch*);
|
||||
AtomBundle_resolve_look_at_R bundle = C_(void*, smem.resolve_look_at_bundle);
|
||||
tb_emit(tb, bundle->input_and_sub); tb_bind_(tb, Binds_ResolveLookAtSub,
|
||||
.target = target,
|
||||
.eye = eye,
|
||||
.up_in = up_in,
|
||||
);
|
||||
tb_emit(tb, bundle->normalize_fwd_uz); tb_bind_(tb, Binds_normalize_v3s4,
|
||||
.src_offset = O_(ResolveLookAtScratch,fwd),
|
||||
.dst_offset = O_(ResolveLookAtScratch,uz),
|
||||
);
|
||||
tb_emit(tb, bundle->cross_to_right); tb_bind_(tb, Binds_gte_cross_v3s4,
|
||||
.src_a = & sp->uz,
|
||||
.src_b = & sp->up_in,
|
||||
.out = & sp->right,
|
||||
);
|
||||
tb_emit(tb, bundle->normalize_right_ux); tb_bind_(tb, Binds_normalize_v3s4,
|
||||
.src_offset = O_(ResolveLookAtScratch,right),
|
||||
.dst_offset = O_(ResolveLookAtScratch,ux),
|
||||
);
|
||||
tb_emit(tb, bundle->cross_to_up); tb_bind_(tb, Binds_gte_cross_v3s4,
|
||||
.src_a = & sp->uz,
|
||||
.src_b = & sp->ux,
|
||||
.out = & sp->up,
|
||||
);
|
||||
tb_emit(tb, bundle->normalize_up_uy); tb_bind_(tb, Binds_normalize_v3s4,
|
||||
.src_offset = O_(ResolveLookAtScratch,up),
|
||||
.dst_offset = O_(ResolveLookAtScratch,uy),
|
||||
);
|
||||
tb_emit(tb, bundle->populate_mt3s4s2); tb_bind_(tb, Binds_ResolveLookAt_PopulateMT3S4S2,
|
||||
.look_at = look_at,
|
||||
);
|
||||
}
|
||||
FI_ void camera_look_at(TapeBuilder_R tb, Camera* c, P3_S4* target, V3_S4* up_in) { resolve_look_at(tb, & c->look_at, & c->pos, target, up_in); }
|
||||
|
||||
tb_emit(tb, bundle->input_and_sub); {
|
||||
tb_data(tb, u4_(target));
|
||||
tb_data(tb, u4_(eye));
|
||||
tb_data(tb, u4_(up_in));
|
||||
}
|
||||
tb_emit(tb, bundle->normalize_fwd_uz); {
|
||||
tb_data(tb, u4_(O_(ResolveLookAtScratch, fwd) | (O_(ResolveLookAtScratch, uz) << 16)));
|
||||
}
|
||||
tb_emit(tb, bundle->cross_to_right); {
|
||||
tb_data(tb, u4_(& sp->uz));
|
||||
tb_data(tb, u4_(& sp->up_in));
|
||||
tb_data(tb, u4_(& sp->right));
|
||||
}
|
||||
tb_emit(tb, bundle->normalize_right_ux); {
|
||||
tb_data(tb, u4_(O_(ResolveLookAtScratch, right) | (O_(ResolveLookAtScratch, ux) << 16)));
|
||||
}
|
||||
tb_emit(tb, bundle->cross_to_up); {
|
||||
tb_data(tb, u4_(& sp->uz));
|
||||
tb_data(tb, u4_(& sp->ux));
|
||||
tb_data(tb, u4_(& sp->up));
|
||||
}
|
||||
tb_emit(tb, bundle->normalize_up_uy); {
|
||||
tb_data(tb, u4_(O_(ResolveLookAtScratch, up) | (O_(ResolveLookAtScratch, uy) << 16)));
|
||||
}
|
||||
tb_emit(tb, bundle->pop_mv_trans); {
|
||||
tb_data(tb, u4_(look_at));
|
||||
}
|
||||
}
|
||||
|
||||
GCC_OPTIMIZATION_DISABLE
|
||||
void update(PrimitiveArena* pa, U4* ordering_buf)
|
||||
{
|
||||
TapeBuilder tb = tb_make(slice_ut_arr(smem.MemTape));
|
||||
|
||||
// Pad Input
|
||||
{
|
||||
/*Pad Input*/ {
|
||||
tb.used = 0; tb_scope_run(& tb) {
|
||||
// Grab latest state from bios.
|
||||
tb_emit_(pad_bios_snapshot);
|
||||
tb_data_(raw, & smem.pad_raw[0]);
|
||||
tb_data_(state, & smem.pad[0]);
|
||||
// tb_emit_(pad_bios_snapshot);
|
||||
// tb_data_(raw, & smem.pad_raw[1]);
|
||||
// tb_data_(state, & smem.pad[1]);
|
||||
|
||||
tb_data(& tb, u4_(& smem.pad_raw[0]));
|
||||
tb_data(& tb, u4_(& smem.pad[0]));
|
||||
tb_emit_(pad_input_cam);
|
||||
tb_data_(state, & smem.pad[0]);
|
||||
tb_data_(cam, & smem.cam);
|
||||
|
||||
// tb_emit_(pad_input_cube_rotation);
|
||||
// tb_data_(state, & smem.pad[0]);
|
||||
// tb_data_(cube_rot, & smem.cube.rot);
|
||||
// tb_data_(floor_rot, & smem.floor.rot);
|
||||
tb_data(& tb, u4_(& smem.pad[0]));
|
||||
tb_data(& tb, u4_(& smem.cam));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,7 +277,7 @@ void update(PrimitiveArena* pa, U4* ordering_buf)
|
||||
if (use_c11_path == false)
|
||||
{
|
||||
tb.used = 0; tb_scope_run(& tb) {
|
||||
resolve_look_at(& tb, & smem.cam.look_at, & smem.cam.pos, & smem.cube.pos, & v3s4(0, -fp_one, 0));
|
||||
camera_look_at(& tb, & smem.cam, & smem.cube.pos, & v3s4(0, -fp_one, 0));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,33 +287,30 @@ void update(PrimitiveArena* pa, U4* ordering_buf)
|
||||
mt3s2s4_rotation (& smem.cube.rot, & smem.tform_world);
|
||||
mt3s2s4_translation(& smem.tform_world, & smem.cube.pos);
|
||||
mt3s2s4_scale (& smem.tform_world, & smem.cube.scale);
|
||||
|
||||
// Combine world and look_at matrix.
|
||||
gte_comp_coord_m3s2(& smem.cam.look_at, & smem.tform_world, & smem.tform_view);
|
||||
gte_matrix_set_rotation (& smem.tform_view);
|
||||
gte_matrix_set_translation(& smem.tform_view);
|
||||
|
||||
U4 prim_base = u4_(pa->buf[smem.active_buf_id]);
|
||||
U4 prim_cursor = prim_base + pa->used;
|
||||
|
||||
tb.used = 0; tb_scope(& tb) {
|
||||
tb_emit(& tb, rbind_cube_g4_face);
|
||||
tb_data(& tb, prim_cursor);
|
||||
tb_data(& tb, u4_(smem.cube.faces));
|
||||
tb_data(& tb, u4_(smem.cube.verts));
|
||||
tb_data(& tb, u4_(ordering_buf));
|
||||
|
||||
// TODO(Ed): We should do a bounds check beforehand to confirm pa can hold all tris?
|
||||
// The tape atoms in-flight should not need to care.
|
||||
U1* prim_base = u1_r(pa->buf[smem.active_buf_id]);
|
||||
U1* prim_cursor = prim_base + pa->used;
|
||||
tb.used = 0; tb_scope_run(& tb) {
|
||||
tb_emit(& tb, rbind_cube_g4_face); tb_bind_(& tb, Binds_CubeTri,
|
||||
.prim_cursor = prim_cursor,
|
||||
.face_cursor = smem.cube.faces,
|
||||
.vert_base = smem.cube.verts,
|
||||
.ot_base = ordering_buf,
|
||||
);
|
||||
for (U4 i = 0; i < Cube_num_faces; i++) {
|
||||
// Two triangles per quad face: (x,y,z) and (x,z,w)
|
||||
tb_emit(& tb, cube_g4_face);
|
||||
tb_emit(& tb, cube_g4_face); // Two triangles per quad face: (x,y,z) and (x,z,w)
|
||||
}
|
||||
|
||||
tb_emit(& tb, sync_primitive_arena);
|
||||
tb_data(& tb, u4_(& pa->used));
|
||||
tb_data(& tb, prim_base);
|
||||
tb_emit(& tb, sync_primitive_arena); tb_bind_(& tb, Binds_SyncPrimitiveArena,
|
||||
.used = & pa->used,
|
||||
.cursor = prim_base,
|
||||
);
|
||||
}
|
||||
tape_run(tb_slice(tb));// Fire off the tape (bigger-clobber variant).
|
||||
|
||||
// smem.cube.rot.y += 30;
|
||||
}
|
||||
// Draw floor
|
||||
@@ -346,45 +319,35 @@ void update(PrimitiveArena* pa, U4* ordering_buf)
|
||||
mt3s2s4_rotation (& smem.floor.rot, & smem.tform_world);
|
||||
mt3s2s4_translation(& smem.tform_world, & smem.floor.pos);
|
||||
mt3s2s4_scale (& smem.tform_world, & smem.floor.scale);
|
||||
|
||||
// Combine world and look_at matrix.
|
||||
gte_comp_coord_m3s2(& smem.cam.look_at, & smem.tform_world, & smem.tform_view);
|
||||
|
||||
gte_matrix_set_rotation (& smem.tform_view);
|
||||
gte_matrix_set_translation(& smem.tform_view);
|
||||
|
||||
U4 prim_base = u4_(pa->buf[smem.active_buf_id]);
|
||||
U4 prim_cursor = prim_base + pa->used;
|
||||
|
||||
// TODO(Ed): We should do a bounds check beforehand to confirm pa can hold all tris?
|
||||
// The tape atoms in-flight should not need to care.
|
||||
|
||||
// Prepare the tape. (Push protocol to tape)
|
||||
tb.used = 0; tb_scope(& tb) {
|
||||
// tb_emit(& tb, set_gte_mt3s2s4);
|
||||
// tb_data(& tb, u4_(& smem.tform_view));
|
||||
|
||||
tb_emit(& tb, rbind_floor_f3_face);
|
||||
// TODO(Ed): Just use a single context struct ref?
|
||||
tb_data(& tb, prim_cursor);
|
||||
tb_data(& tb, u4_(smem.floor.faces));
|
||||
tb_data(& tb, u4_(smem.floor.verts));
|
||||
tb_data(& tb, u4_(ordering_buf));
|
||||
U1_R prim_base = u1_r(pa->buf[smem.active_buf_id]);
|
||||
U1_R prim_cursor = prim_base + pa->used;
|
||||
tb.used = 0; tb_scope_run(& tb) { // Prepare the tape. (Push protocol to tape)
|
||||
tb_emit(& tb, rbind_floor_f3_face); tb_bind_(& tb, Binds_FloorTri,
|
||||
.prim_cursor = prim_cursor,
|
||||
.face_cursor = smem.floor.faces,
|
||||
.vert_base = smem.floor.verts,
|
||||
.ot_base = ordering_buf,
|
||||
);
|
||||
for (U4 i = 0; i < Floor_num_faces; i++) {
|
||||
tb_emit(& tb, floor_f3_face);
|
||||
}
|
||||
// After floor_f3_face iterations complete, the primitive arena's used counter needs updating.
|
||||
tb_emit(& tb, sync_primitive_arena);
|
||||
tb_data(& tb, u4_(& pa->used));
|
||||
tb_data(& tb, prim_base);
|
||||
tb_emit(& tb, sync_primitive_arena); tb_bind_(& tb, Binds_SyncPrimitiveArena,
|
||||
.used = & pa->used,
|
||||
.cursor = prim_base,
|
||||
);
|
||||
}
|
||||
tape_run(tb_slice(tb));// Fire off the tape (bigger-clobber variant).
|
||||
|
||||
// C-side state (pa->used) has already been updated by the tape!
|
||||
// smem.floor.rot.y += 5;
|
||||
}
|
||||
}
|
||||
GCC_OPTIMIZATION_ENABLE
|
||||
|
||||
void render(void) {
|
||||
}
|
||||
@@ -401,12 +364,22 @@ void gp_display_frame(DoubleBuffer* screen_buf, S4* active_buf_id, U4* ordering_
|
||||
active_buf_id[0] = ! active_buf_id[0]; // Swap current buffer
|
||||
}
|
||||
|
||||
GCC_OPTIMIZATION_DISABLE
|
||||
int main(void)
|
||||
{
|
||||
smem = (SMemory){0};
|
||||
// TODO(Ed): remove this field we don't need it in smem.
|
||||
smem.scratchpad = C_(U4_V, Scratchpad_Loc);
|
||||
|
||||
B4 basic_sample = false; if (basic_sample) {
|
||||
// We will be defining the tape here along with its atom, then running the tape after.
|
||||
TapeBuilder tb = tb_make(slice_ut_arr(smem.MemTape));
|
||||
MipsCode add_one_to_R_T1[] = {
|
||||
add_ui_self(R_T1, 1),
|
||||
mac_yield(),
|
||||
};
|
||||
tb_emit(& tb, C_(MipsAtom*, add_one_to_R_T1));
|
||||
Tape tape = tb_end(& tb);
|
||||
tape_run(tape);
|
||||
}
|
||||
|
||||
// smem.primitives.used = 0;
|
||||
// smem.active_buf_id = 0;
|
||||
smem.cam.pos = v3s4(500, -1000, -1500);
|
||||
@@ -451,4 +424,3 @@ int main(void)
|
||||
};
|
||||
return 0;
|
||||
}
|
||||
GCC_OPTIMIZATION_ENABLE
|
||||
|
||||
@@ -24,7 +24,7 @@ enum {
|
||||
typedef U4 OrderingTable_Buffer[OrderingTbl_Len];
|
||||
typedef Array_(OrderingTable_Buffer, 2);
|
||||
|
||||
typedef B1 PrimitiveBuffer[PrimitiveBuff_Len];
|
||||
typedef U1 PrimitiveBuffer[PrimitiveBuff_Len];
|
||||
typedef Array_(PrimitiveBuffer, 2);
|
||||
typedef Struct_(PrimitiveArena) {
|
||||
A2_PrimitiveBuffer buf;
|
||||
@@ -54,14 +54,14 @@ I_ void ent_cube128_init(A8_V3_S2* verts, A6_V4_S2* faces) {
|
||||
{ 2, 3, 6, 7 },
|
||||
{ 3, 0, 7, 4 },
|
||||
};
|
||||
mem_copy(u4_(verts), u4_(& baked_verts), S_(A8_V3_S2) );
|
||||
mem_copy(u4_(faces), u4_(& baked_faces), S_(A6_V4_S2) );
|
||||
mem_copy(b1_r(verts), b1_r(& baked_verts), S_(A8_V3_S2) );
|
||||
mem_copy(b1_r(faces), b1_r(& baked_faces), S_(A6_V4_S2) );
|
||||
return;
|
||||
}
|
||||
typedef Struct_(Ent_Cube) {
|
||||
V3_S4 accel;
|
||||
V3_S4 vel;
|
||||
V3_S4 pos; // RGA(Lengyel): affine point with implicit weight one. Storage alias of V3_S4.
|
||||
V3_S4 pos;
|
||||
V3_S4 scale;
|
||||
V3_S2 rot;
|
||||
A8_V3_S2 verts;
|
||||
@@ -83,12 +83,12 @@ I_ void ent_floor_init(A4_V3_S2* verts, A2_V3_S2* faces) {
|
||||
{ 0, 1, 2 },
|
||||
{ 1, 3, 2 },
|
||||
};
|
||||
mem_copy(u4_(verts), u4_(& baked_verts), S_(A4_V3_S2));
|
||||
mem_copy(u4_(faces), u4_(& baked_faces), S_(A2_V3_S2));
|
||||
mem_copy(b1_r(verts), b1_r(& baked_verts), S_(A4_V3_S2));
|
||||
mem_copy(b1_r(faces), b1_r(& baked_faces), S_(A2_V3_S2));
|
||||
};
|
||||
typedef Struct_(Ent_Floor) {
|
||||
V3_S4 accel;
|
||||
V3_S4 pos; // RGA(Lengyel): affine point with implicit weight one. Storage alias of V3_S4.
|
||||
V3_S4 pos;
|
||||
V3_S4 scale;
|
||||
V3_S2 rot;
|
||||
A4_V3_S2 verts;
|
||||
@@ -96,7 +96,7 @@ typedef Struct_(Ent_Floor) {
|
||||
};
|
||||
|
||||
typedef Struct_(Camera) {
|
||||
P3_S4 pos; // RGA(Lengyel): affine point with implicit weight one. Storage alias of V3_S4.
|
||||
P3_S4 pos;
|
||||
V3_S2 rot;
|
||||
MT3_S2S4 look_at;
|
||||
};
|
||||
|
||||
+4
-6
@@ -31,9 +31,8 @@ local M = {} ---@type DuffleExport
|
||||
--- @field binds_by_name table<string, BindsEntry>
|
||||
--- @field atoms_by_name table<AtomName, AtomEntry>
|
||||
--- @field atom_infos AtomInfoEntry[]
|
||||
--- @field components table<string, ComponentDef>
|
||||
--- @field components table<string, Component>
|
||||
--- @field component_atom_infos AtomInfoEntry[]|nil
|
||||
--- @field component_body_index table<string, ComponentBodyEntry>
|
||||
--- @field tape_chains table<string, TapeChain>|nil
|
||||
--- @field source_order SourceFile[]
|
||||
--- @field collisions CorpusCollision[]
|
||||
@@ -70,7 +69,6 @@ function M.corpus_view(ctx)
|
||||
atom_infos = corpus.atom_infos or {},
|
||||
components = corpus.components or {},
|
||||
component_atom_infos = corpus.component_atom_infos or {},
|
||||
component_body_index = corpus.component_body_index or {},
|
||||
tape_chains = corpus.tape_chains or {},
|
||||
source_order = corpus.source_order or {},
|
||||
collisions = corpus.collisions or {},
|
||||
@@ -80,12 +78,12 @@ end
|
||||
--- @param rules CheckRule[]
|
||||
--- @param phase string
|
||||
--- @param item AtomEntry|SourceFile
|
||||
--- @param pipe_ctx PipeCtx
|
||||
--- @param findings CheckFinding[]
|
||||
--- @param pipe_ctx PassScratch
|
||||
--- @param findings Finding[]
|
||||
--- @return nil
|
||||
function M.run_check_rules(rules, phase, item, pipe_ctx, findings)
|
||||
for _, rule in ipairs(rules) do ---@type integer, CheckRule
|
||||
local fn = rule[phase] ---@type (fun(item: AtomEntry|SourceFile, pipe_ctx: PipeCtx, findings: CheckFinding[]): nil)|nil
|
||||
local fn = rule[phase] ---@type (fun(item: AtomEntry|SourceFile, pipe_ctx: PassScratch, findings: Finding[]): nil)|nil
|
||||
if fn then fn(item, pipe_ctx, findings) end
|
||||
end
|
||||
end
|
||||
|
||||
+69
-69
@@ -11,20 +11,9 @@ for k, v in pairs(isa) do M[k] = v end ---@type string, any
|
||||
-- Shared, memoized helpers: a single emitted-word event stream that every downstream pass reads from,
|
||||
-- built once from the pre-tokenized bodies.
|
||||
|
||||
--- @class ComponentBodyEntry
|
||||
--- @field body_tokens BodyToken[]
|
||||
--- @field body_off integer -- byte offset of body[1] in `source`
|
||||
--- @field line_of fun(pos:integer):integer -- byte-offset → 1-based line number in `source`
|
||||
--- @field source string -- absolute path of the source containing the declaration
|
||||
--- @field declaration integer -- 1-based line number of the MipsAtomComp_(ac_X) declaration
|
||||
--- @field kind string -- "comp_bare" | "comp_proc"
|
||||
--- @field arg_names string[]|nil
|
||||
--- @field sub_map table<string, string>|nil -- bag: formal -> substituted operand
|
||||
|
||||
--- @class EmissionWalkCtx
|
||||
--- @field component_index table<string, ComponentBodyEntry>
|
||||
--- @field components table<string, Component>
|
||||
--- @field word_counts WordCounts
|
||||
--- @field components table<string, ComponentDef>
|
||||
--- @field reg_use_schema RegUseSchema|nil
|
||||
--- @field reg_use_param string|nil
|
||||
--- @field atom_name AtomName|nil
|
||||
@@ -39,10 +28,12 @@ for k, v in pairs(isa) do M[k] = v end ---@type string, any
|
||||
--- @field atom_name AtomName|nil
|
||||
--- @field schema_name string|nil
|
||||
|
||||
-- Finding: see ps1_meta.lua
|
||||
|
||||
--- @class DuffleEmit
|
||||
|
||||
|
||||
-- The cross-source component-body index is owned by the corpus (`corpus.component_body_index`, populated by `passes/components.lua`).
|
||||
-- The cross-source component row is owned by the corpus (`corpus.components`, populated by `passes/components.lua`).
|
||||
-- Consumers (`passes/static_analysis.lua`, `passes/emission_model.lua`) read it directly; per-pass memoization helpers stay out of scope.
|
||||
|
||||
-- ASCII byte constants used by split_call_args (kept local to keep Section 8 self-contained).
|
||||
@@ -119,18 +110,18 @@ local E_MAC_PREFIX_LEN = 4 ---@type integer
|
||||
--- * Known `mac_X(...)` calls: Recursively expand the indexed component body, including nested components. Every event from the expansion carries:
|
||||
--- - `source` / `line` = the COMPONENT'S source path + the line of the token within the component body (i.e. "definition site").
|
||||
--- - `call_source` / `call_line` = the ROOT atom's source path + call-site line, PRESERVED across recursion so nested events still point at the original root.
|
||||
--- * Unknown `mac_X` (not in `component_index`): fall back to `word_counts[ident]` if present; otherwise emit one opaque event so the cycle budget accounts for the word.
|
||||
--- * Unknown `mac_X` (not in `components`): fall back to `word_counts[ident]` if present; otherwise emit one opaque event so the cycle budget accounts for the word.
|
||||
--- * Marker Tokens (`atom_label(...)` / `atom_offset(...)`): Zero events (they are pure metaprogram hints).
|
||||
---
|
||||
--- Cycle protection: a per-expansion `visiting` set tracks components currently on the expansion stack;
|
||||
--- a re-entry produces a deterministic `{kind = "cycle", ...}` error and aborts that branch (does NOT hang, does NOT recurse).
|
||||
--- a re-entry produces a deterministic `{check = "cycle", ...}` error and aborts that branch (does NOT hang, does NOT recurse).
|
||||
---
|
||||
--- Pure: reads `body_entry` / `component_index` / `word_counts`. Memoization is the caller's responsibility.
|
||||
--- Pure: reads `body_entry` / `components` / `word_counts`. Memoization is the caller's responsibility.
|
||||
--- Callers wanting `word_events` / `word_event_errors` precomputed for many atoms should memoize them per atom.
|
||||
--- @param body_entry ComponentBodyEntry
|
||||
--- @param component_index table<string, ComponentBodyEntry>
|
||||
--- @param body_entry Component
|
||||
--- @param components table<string, Component>
|
||||
--- @param word_counts WordCounts
|
||||
--- @return WordEvent[], EmitError[]
|
||||
--- @return WordEvent[], Finding[]
|
||||
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
-- Section 11: project_emission (per-atom emission projection)
|
||||
@@ -141,7 +132,7 @@ local E_MAC_PREFIX_LEN = 4 ---@type integer
|
||||
-- The items stream is the single ordered source of truth; `word_events` and `markers` are dense views over it.
|
||||
--
|
||||
-- The helper below operates on a body string (not a body_entry) so the pass can call it without depending on the older SourceScan / body_off conventions.
|
||||
-- component_index argument is reserved for recursive component expansion.
|
||||
-- `components` is the recursive expansion map (`corpus.components`).
|
||||
-- word_counts table is authored-metadata + current-component count table.
|
||||
|
||||
--- @class EmissionProjection
|
||||
@@ -149,8 +140,8 @@ local E_MAC_PREFIX_LEN = 4 ---@type integer
|
||||
--- @field word_events WordEvent[]
|
||||
--- @field markers EmissionMarker[]
|
||||
--- @field invocations InvocationRecord[] -- dense view of items where kind == "invoke_begin"|"invoke_end"
|
||||
--- @field errors EmitError[]
|
||||
--- @field warnings EmitWarning[]
|
||||
--- @field errors Finding[]
|
||||
--- @field warnings Finding[]
|
||||
|
||||
--- @class InvocationRecord
|
||||
--- Lives at `atom.paths.invocations[*]`. Constructed once at the single invocation-construction site
|
||||
@@ -171,7 +162,7 @@ local E_MAC_PREFIX_LEN = 4 ---@type integer
|
||||
--- @field end_word integer -- 1-based items index of the `invoke_end` item (set by `emit_invoke_end`)
|
||||
--- @field word_count integer -- Number of `word` items emitted between `start_word` and `end_word` (inclusive)
|
||||
--- @field debug_skip boolean -- `debug_skip` stamp; true iff `corpus.components[name].debug_skip` is true at construction. Always boolean (never `nil`).
|
||||
--- @field errors EmitError[]
|
||||
--- @field errors Finding[]
|
||||
|
||||
-- Internal recursive walker. The items stream holds every emitted event in order; `word_events`, `markers`,
|
||||
-- `invocations`, `errors`, `warnings` are dense views / side outputs appended alongside.
|
||||
@@ -187,9 +178,9 @@ local E_MAC_PREFIX_LEN = 4 ---@type integer
|
||||
-- * Unknown uncounted macros emit one opaque word + one warning. Unknown metadata-backed macros (entry in `word_counts`) emit the declared word count, no warning.
|
||||
-- * Cycle detection uses an active DFS stack (`visiting`); a cycle appends a construction error to BOTH the projection errors and the cycle invocation's own errors,
|
||||
-- then breaks out without recursing (the cycle entry still receives an invocation ID + paired `invoke_begin` / `invoke_end` items, so the boundary invariant is preserved).
|
||||
-- * Component declared-count mismatch (declared vs. measured) is a construction error (kind = "count_mismatch"); recorded on the invocation record and pass-level errors list.
|
||||
-- * Component declared-count mismatch (declared vs. measured) is a construction error (check = "count_mismatch"); recorded on the invocation record and pass-level errors list.
|
||||
-- * Final boundary check: if any invocation is still open at end of walk, surface a "unbalanced" construction error.
|
||||
--- @param root_body_entry ComponentBodyEntry
|
||||
--- @param root_body_entry Component
|
||||
--- @param ctx_table EmissionWalkCtx
|
||||
--- @return EmissionProjection
|
||||
local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
@@ -197,8 +188,8 @@ local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
local word_events = {} ---@type WordEvent[]
|
||||
local markers = {} ---@type EmissionMarker[]
|
||||
local invocations = {} ---@type InvocationRecord[]
|
||||
local errors = {} ---@type EmitError[]
|
||||
local warnings = {} ---@type EmitWarning[]
|
||||
local errors = {} ---@type Finding[]
|
||||
local warnings = {} ---@type Finding[]
|
||||
|
||||
local word_idx = 0 ---@type integer
|
||||
local invocation_stack = {} ---@type InvocationRecord[] -- stack of currently-open invocation records
|
||||
@@ -282,7 +273,8 @@ local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
gpr_keys[pos] = key
|
||||
if unresolved then
|
||||
errors[#errors + 1] = {
|
||||
kind = "reguse_unresolved",
|
||||
kind = "error",
|
||||
check = "reguse_unresolved",
|
||||
line = line,
|
||||
msg = string.format("RegUse operand %q does not resolve in schema %q",
|
||||
effective, (reg_use_schema and reg_use_schema.name) or "?"),
|
||||
@@ -294,7 +286,8 @@ local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
for _, wpos in ipairs(row.writes) do ---@type integer, integer
|
||||
if wpos == pos then
|
||||
errors[#errors + 1] = {
|
||||
kind = "reguse_const_write",
|
||||
kind = "error",
|
||||
check = "reguse_const_write",
|
||||
line = line,
|
||||
msg = string.format("RegUse slot %q is Reg const; %s writes it",
|
||||
slot, encoder),
|
||||
@@ -541,15 +534,14 @@ local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
-- The stamp is resolved from the `corpus.components[name]` registry (passed in via `ctx_table.components` by `emission_model.run`),
|
||||
-- Unmarked components stamp `false` (not `nil`) so consumers can dispatch on the boolean without nil checks.
|
||||
--
|
||||
-- The walker has already found the component body in `ctx_table.component_index[component_name]`, so the matching entry MUST exist in `ctx_table.components[component_name]`
|
||||
-- (both registries are populated from the same source by the components pass).
|
||||
-- The walker has already found the component body in `ctx_table.components[component_name]`.
|
||||
-- A missing entry is a corpus-plumbing bug; we fail loudly here rather than silently stamp `false` and mask the regression.
|
||||
local components = ctx_table.components ---@type table<string, ComponentDef>|nil
|
||||
local component_def = components and components[component_name] or nil ---@type ComponentDef|nil
|
||||
local components = ctx_table.components ---@type table<string, Component>|nil
|
||||
local component_def = components and components[component_name] or nil ---@type Component|nil
|
||||
if not component_def then
|
||||
error("duffle.emit_invoke_begin: component " .. string.format("%q", component_name)
|
||||
.. " is present in `component_index` (the walker matched a `mac_" .. component_name .. "()` call) but absent from `components` (the canonical corpus.components registry). "
|
||||
.. "This is a corpus-plumbing bug — the components pass must populate corpus.components[name] for every component it puts in corpus.component_body_index[name]. "
|
||||
.. " is present in the walk (the walker matched a `mac_" .. component_name .. "()` call) but absent from `components` (the canonical corpus.components registry). "
|
||||
.. "This is a corpus-plumbing bug — the components pass must populate corpus.components[name] for every expanded component. "
|
||||
.. "The emission pass refuses to silently stamp `debug_skip = false` for a missing registry entry."
|
||||
, 0
|
||||
)
|
||||
@@ -621,9 +613,10 @@ local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
local canon = M.gte_canon(ident) ---@type string
|
||||
if canon ~= ident and wc and wc[canon] then return wc[canon] end
|
||||
warnings[#warnings + 1] = {
|
||||
kind = "uncounted",
|
||||
kind = "warning",
|
||||
check = "uncounted",
|
||||
line = tok_line,
|
||||
msg = string.format("project_emission: opaque word emitted for %q (no entry in word_counts or component_index)",
|
||||
msg = string.format("project_emission: opaque word emitted for %q (no entry in word_counts or components)",
|
||||
ident),
|
||||
}
|
||||
return 1
|
||||
@@ -634,19 +627,19 @@ local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
-- walk_root_call_text: Outermost `mac_X(...)` token text (preserved across recursion).
|
||||
-- walk_immediate_call_text: IMMEDIATE outer `mac_X(...)` token text for words emitted in this body — nil for the root atom body.
|
||||
-- Two trackers are propagated as separate parameters so words deep inside nested expansions correctly identify both their immediate call site and the outermost call site.
|
||||
--- @param body_entry ComponentBodyEntry
|
||||
--- @param body_entry Component
|
||||
--- @param walk_parent_inv_id integer
|
||||
--- @param walk_root_call_text string|nil
|
||||
--- @param walk_immediate_call_text string|nil
|
||||
--- @param sub_map table<string, string>|nil
|
||||
--- @return nil
|
||||
local function walk_body_entry(body_entry, walk_parent_inv_id,
|
||||
walk_root_call_text, walk_immediate_call_text)
|
||||
walk_root_call_text, walk_immediate_call_text, sub_map)
|
||||
local tokens = body_entry.body_tokens or {} ---@type BodyToken[]
|
||||
local body_off = body_entry.body_off or 0 ---@type integer
|
||||
local line_of = body_entry.line_of or M.LineIndex("") ---@type LineIndexFn
|
||||
local def_source = body_entry.source or "" ---@type string
|
||||
local def_line = body_entry.declaration or 0 ---@type integer
|
||||
local sub_map = body_entry.sub_map ---@type table<string, string>|nil
|
||||
local def_line = body_entry.line or 0 ---@type integer
|
||||
-- Per-token dispatch: each matched branch returns; only the fall-through
|
||||
-- "opaque word" emit handles direct encoders + mac_X-without-component.
|
||||
--- @param bt BodyToken
|
||||
@@ -716,7 +709,7 @@ local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
end
|
||||
if ident:sub(1, 4) == "mac_" then
|
||||
local bare = ident:sub(5) ---@type string
|
||||
local comp = ctx_table.component_index[bare] ---@type ComponentBodyEntry|nil
|
||||
local comp = ctx_table.components[bare] ---@type Component|nil
|
||||
if comp then
|
||||
local invocation_root_call_text = walk_root_call_text or tok ---@type string
|
||||
if ctx_table.visiting[bare] then
|
||||
@@ -724,8 +717,9 @@ local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
local inv = emit_invoke_begin(comp.kind or "comp_bare", bare, tok, invocation_root_call_text, def_source, tok_line) ---@type InvocationRecord
|
||||
inv.parent_id = walk_parent_inv_id
|
||||
inv.call_text = tok
|
||||
local err = { ---@type EmitError
|
||||
kind = "cycle",
|
||||
local err = { ---@type Finding
|
||||
kind = "error",
|
||||
check = "cycle",
|
||||
msg = string.format("project_emission: component cycle detected: %q", bare),
|
||||
source = def_source,
|
||||
line = tok_line,
|
||||
@@ -741,12 +735,12 @@ local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
inv.parent_id = walk_parent_inv_id
|
||||
inv.call_text = tok
|
||||
inv.def_path = comp.source
|
||||
inv.def_line = comp.declaration
|
||||
inv.def_line = comp.line
|
||||
-- Propagate trackers into the recursive walk:
|
||||
-- immediate_call_text = this call's tok (the IMMEDIATE outer call for words emitted in this body)
|
||||
-- root_call_text = the OUTERMOST call (immutable across the recursion)
|
||||
local formal_names = ctx_table.component_index[bare] ---@type string[]|nil
|
||||
and ctx_table.component_index[bare].arg_names
|
||||
-- child_map = per-invocation formal substitution; stays on the walk stack
|
||||
local formal_names = comp.arg_names ---@type string[]|nil
|
||||
local child_map = nil ---@type table<string, string>|nil
|
||||
if formal_names then
|
||||
child_map = {}
|
||||
@@ -754,17 +748,11 @@ local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
child_map[fname] = apply_sub(sub_map, args[i])
|
||||
end
|
||||
end
|
||||
walk_body_entry({
|
||||
body_tokens = comp.body_tokens or {},
|
||||
body_off = comp.body_off or 0,
|
||||
line_of = comp.line_of,
|
||||
source = comp.source,
|
||||
declaration = comp.declaration,
|
||||
sub_map = child_map,
|
||||
},
|
||||
walk_body_entry(comp,
|
||||
inv.id,
|
||||
invocation_root_call_text,
|
||||
tok)
|
||||
tok,
|
||||
child_map)
|
||||
ctx_table.visiting[bare] = nil
|
||||
emit_invoke_end(inv)
|
||||
-- Count `word` items inside [start_word, end_word].
|
||||
@@ -780,8 +768,9 @@ local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
-- We compare against the measured word count.
|
||||
local declared = ctx_table.word_counts["mac_" .. bare] ---@type integer|nil
|
||||
if declared and wc_inside ~= declared then
|
||||
local err = { ---@type EmitError
|
||||
kind = "count_mismatch",
|
||||
local err = { ---@type Finding
|
||||
kind = "error",
|
||||
check = "count_mismatch",
|
||||
msg = string.format("project_emission: mac_%s declared=%d measured=%d", bare, declared, wc_inside),
|
||||
source = def_source,
|
||||
line = tok_line,
|
||||
@@ -791,7 +780,7 @@ local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
end
|
||||
return
|
||||
end
|
||||
-- mac_X NOT in component_index: fall through to opaque emit.
|
||||
-- mac_X NOT in components: fall through to opaque emit.
|
||||
end
|
||||
-- Direct encoder, or mac_X-without-component: resolve count + emit n words.
|
||||
-- Resolve_count may emit a warning if the count is unresolved.
|
||||
@@ -816,13 +805,14 @@ local function _project_emission_inner(root_body_entry, ctx_table)
|
||||
|
||||
-- Walk first; the pass caller stamps the root call site for direct words after the projection returns.
|
||||
-- For nested words the def_path / def_line already point at the component source and MUST be preserved (the stamping helper checks for that).
|
||||
walk_body_entry(root_body_entry, 0, nil, nil)
|
||||
walk_body_entry(root_body_entry, 0, nil, nil, nil)
|
||||
|
||||
-- Boundary check: every invoke_begin must have a matching invoke_end.
|
||||
-- If anything is still open, surface a hard error.
|
||||
if #invocation_stack > 0 then
|
||||
errors[#errors + 1] = {
|
||||
kind = "unbalanced",
|
||||
kind = "error",
|
||||
check = "unbalanced",
|
||||
msg = string.format("project_emission: invocation boundaries not balanced (%d unclosed invocation(s) at end of walk)", #invocation_stack),
|
||||
}
|
||||
end
|
||||
@@ -850,7 +840,7 @@ end
|
||||
--- * `mac_X(...)` calls: emit `invoke_begin` (zero-width), recurse into the component body, emit `invoke_end` (zero-width).
|
||||
--- The component body's words land between the begin/end pair; one invocation record is allocated per call (monotonic ID per atom).
|
||||
--- * Unknown uncounted macros emit 1 opaque word + one warning per occurrence.
|
||||
--- * Tokens whose count cannot be resolved (e.g. `mac_unknown` not in word_counts and not in component_index) surface one
|
||||
--- * Tokens whose count cannot be resolved (e.g. `mac_unknown` not in word_counts and not in components) surface one
|
||||
--- warning; cycle + count-mismatch + boundary violations are construction errors on `pass.errors`.
|
||||
---
|
||||
--- Every emitted `word` carries: `i` (0-based word index), `encoder`, `args` (top-level args), `def_path`, `def_line`,
|
||||
@@ -860,15 +850,15 @@ end
|
||||
--- for the open invocation stack at that word.
|
||||
---
|
||||
--- @param body_text string -- the raw atom body string
|
||||
--- @param component_index table<string, ComponentBodyEntry>
|
||||
--- @param component_index table<string, Component>
|
||||
--- @param word_counts WordCounts
|
||||
--- @param components table<string, ComponentDef>
|
||||
--- @param components table<string, Component>
|
||||
--- `invocation.debug_skip`. A missing or non-table `components` raises a fail-loud error rather than silently falling back.
|
||||
--- @return EmissionProjection
|
||||
--- @param reg_use_ctx RegUseCtx|nil
|
||||
function M.project_emission(body_text, component_index, word_counts, components, reg_use_ctx)
|
||||
-- The recursive walk delegates to `_project_emission_inner` so component bodies (which arrive as
|
||||
-- `{body_tokens, body_off, line_of, source, declaration}` records from `corpus.component_body_index`)
|
||||
-- The recursive walk delegates to `_project_emission_inner` so component bodies
|
||||
-- (the `corpus.components` row: body_tokens / body_off / line_of / source / line)
|
||||
-- re-enter the same walker with the same shared output state.
|
||||
--
|
||||
-- The walker is body-relative: it builds `line_of` from `body_text` and stamps body-relative line numbers (1..N)
|
||||
@@ -898,17 +888,17 @@ function M.project_emission(body_text, component_index, word_counts, components,
|
||||
end
|
||||
|
||||
local tokens = M.tokenize_body(body_text) ---@type BodyToken[]
|
||||
local comps = components or component_index or {} ---@type table<string, Component>
|
||||
return _project_emission_inner({
|
||||
body_tokens = tokens,
|
||||
body_off = 0,
|
||||
line_of = M.LineIndex(body_text),
|
||||
source = "",
|
||||
declaration = 0,
|
||||
line = 0,
|
||||
},
|
||||
{
|
||||
component_index = component_index or {},
|
||||
components = comps,
|
||||
word_counts = word_counts or {},
|
||||
components = components,
|
||||
reg_use_schema = reg_use_ctx and reg_use_ctx.reg_use_schema,
|
||||
reg_use_param = reg_use_ctx and reg_use_ctx.reg_use_param,
|
||||
atom_name = reg_use_ctx and reg_use_ctx.atom_name,
|
||||
@@ -1051,6 +1041,16 @@ function M.find_atom_proc_decl_for(source, before_pos, mips_atom_ptr_len)
|
||||
if source:sub(next_pos, next_pos) == "(" then
|
||||
local inner, after_paren = M.read_parens(source, next_pos) ---@type string|nil, integer
|
||||
if inner then
|
||||
if ident == "AtomBundleEntry_" then
|
||||
local tmpl = M.split_top_level_commas(inner) ---@type string[]
|
||||
if #tmpl ~= 2 then return nil end
|
||||
local name = M.trim(tmpl[1]) .. "_" .. M.trim(tmpl[2]) ---@type string
|
||||
local formals_pos = M.skip_ws_and_cmt(source, after_paren) ---@type integer
|
||||
if source:sub(formals_pos, formals_pos) ~= "(" then return nil end
|
||||
local real_inner, after_real = M.read_parens(source, formals_pos) ---@type string|nil, integer
|
||||
if not real_inner then return nil end
|
||||
return name, real_inner, name, after_real
|
||||
end
|
||||
return ident, inner, ident, after_paren
|
||||
end
|
||||
end
|
||||
|
||||
@@ -88,7 +88,14 @@
|
||||
--- @field visibility_kind string
|
||||
--- @field evidence HardwareRelationEvidence
|
||||
|
||||
--- @class GprRole
|
||||
--- @field name string
|
||||
--- @field pool boolean
|
||||
--- @field optional boolean
|
||||
--- @field carrier boolean
|
||||
|
||||
--- @class DuffleIsa
|
||||
--- @field GPR_ROLE table<string, GprRole>
|
||||
--- @field TAPE_ATOM_MACROS table<string, TapeAtomMacroRow>
|
||||
--- @field DELAY_MARKERS table<string, boolean>
|
||||
--- @field INSTRUCTION table<string, InstructionRow>
|
||||
@@ -111,6 +118,43 @@ local M = {} ---@type DuffleIsa
|
||||
-- Section 7: domain tables
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
-- One GprRole row per name. Construction order is the auto_reg pool order,
|
||||
-- then R_AT, then the three carriers. Index by name into M.GPR_ROLE.
|
||||
--- @type table<string, GprRole>
|
||||
M.GPR_ROLE = {
|
||||
{ name = "R_V0", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_V1", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_T0", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_T1", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_T2", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_T3", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_T4", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_T5", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_T6", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_T7", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_A0", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_A1", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_A2", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_A3", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_S0", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_S1", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_S2", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_S3", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_S4", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_S5", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_S6", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_S7", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_T8", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_T9", pool = true, optional = true, carrier = false },
|
||||
{ name = "R_AT", pool = false, optional = true, carrier = false },
|
||||
{ name = "R_TapePtr", pool = false, optional = true, carrier = true },
|
||||
{ name = "R_AtomJmp", pool = false, optional = true, carrier = true },
|
||||
{ name = "R_ScratchBase", pool = false, optional = true, carrier = true },
|
||||
}
|
||||
for _, row in ipairs(M.GPR_ROLE) do ---@type integer, GprRole
|
||||
M.GPR_ROLE[row.name] = row
|
||||
end
|
||||
|
||||
-- atom_info sub-calls: atom_bind, atom_reads, atom_writes, atom_view, atom_reg_types, atom_ctx, atom_phase.
|
||||
--- @type table<string, TapeAtomMacroRow>
|
||||
M.TAPE_ATOM_MACROS = {
|
||||
|
||||
@@ -59,6 +59,10 @@
|
||||
--- @field unity_root Path
|
||||
--- @field project_root Path
|
||||
|
||||
--- @class ExactResolveOptions
|
||||
--- @field sources string[]
|
||||
--- @field project_root Path
|
||||
|
||||
--- @alias LineIndexFn fun(query_pos: integer): integer
|
||||
|
||||
--- @class DuffleScan
|
||||
@@ -914,6 +918,61 @@ function M.resolve_source_corpus(options)
|
||||
}
|
||||
end
|
||||
|
||||
--- Exact `--source` corpus. No include expansion. Paths stay as given (normalized).
|
||||
--- Same return shape as resolve_source_corpus.
|
||||
--- @param options ExactResolveOptions
|
||||
--- @return Corpus
|
||||
function M.resolve_exact_sources(options)
|
||||
if type(options) ~= "table" then error("resolve_exact_sources requires options", 2) end
|
||||
if type(options.project_root) ~= "string" or options.project_root == "" then
|
||||
error("resolve_exact_sources requires options.project_root", 2)
|
||||
end
|
||||
local sources = options.sources ---@type string[]|nil
|
||||
if type(sources) ~= "table" then error("resolve_exact_sources requires options.sources", 2) end
|
||||
|
||||
local project_root = options.project_root ---@type Path
|
||||
local code_root = M.normalize_path(project_root .. "/code") ---@type Path
|
||||
local source_order = {} ---@type SourceFile[]
|
||||
local sources_by_path = {} ---@type table<Path, SourceFile>
|
||||
local resolver = { ---@type SourceResolver
|
||||
resolved = {},
|
||||
skipped = {},
|
||||
shadowed = {},
|
||||
}
|
||||
for _, input_path in ipairs(sources) do ---@type integer, string
|
||||
local path = M.normalize_path(input_path) ---@type string
|
||||
M.canonical_path_key(path)
|
||||
local source = { ---@type SourceFile
|
||||
path = path,
|
||||
text = M.read_file(path),
|
||||
dir = M.dirname(path),
|
||||
basename = M.basename_no_ext(path),
|
||||
}
|
||||
source_order[#source_order + 1] = source
|
||||
local key = M.canonical_path_key(path) ---@type string
|
||||
if not sources_by_path[key] then sources_by_path[key] = source end
|
||||
resolver.resolved[#resolver.resolved + 1] = {
|
||||
include_path = path,
|
||||
include_text = nil,
|
||||
root_source = path,
|
||||
root_line = 1,
|
||||
candidate_a = path,
|
||||
candidate_b = nil,
|
||||
selected_path = path,
|
||||
disposition = "exact",
|
||||
}
|
||||
end
|
||||
return {
|
||||
unity_root = nil,
|
||||
project_root = project_root,
|
||||
code_root = code_root,
|
||||
source_order = source_order,
|
||||
sources_by_path = sources_by_path,
|
||||
sources_by_dir = M.group_sources_by_dir(source_order),
|
||||
resolver = resolver,
|
||||
}
|
||||
end
|
||||
|
||||
-- Split a brace-body into top-level comma-separated tokens. Honors nested parens/braces/brackets and skips strings/comments.
|
||||
-- Splits at top-level NEWLINES and SEMICOLONS too, AND emits a token break after a top-level comment/string.
|
||||
-- Pure-comment / pure-string chunks contribute 0 words.
|
||||
|
||||
@@ -21,16 +21,8 @@ local duffle = dofile(_bootstrap_dir .. "../duffle_paths.lua")
|
||||
-- Type declarations
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
-- SourceFile, PassCtx, PassResult, PassShared, Corpus: see ps1_meta.lua
|
||||
-- SourceScan, AtomEntry, BindsEntry, RegTypeDefault, AtomViewEntry: see scan_source.lua
|
||||
|
||||
--- @class AtomAnnotation
|
||||
--- @field atom_name string -- Atom name (scan.atom_infos row)
|
||||
--- @field info_line integer -- Source line of the atom_info call
|
||||
--- @field binds string|nil -- Binds_X name if any
|
||||
--- @field reads string[] -- R_* names (read targets)
|
||||
--- @field writes string[] -- R_* names (write targets)
|
||||
--- @field errors string[]|nil -- Parse-time errors from scan_source (atom_info body malformed)
|
||||
-- SourceFile, PassCtx, PassResult, PassShared, Corpus, Finding: see ps1_meta.lua
|
||||
-- SourceScan, AtomEntry, AtomInfoEntry, BindsEntry, RegTypeDefault, AtomViewEntry: see scan_source.lua
|
||||
|
||||
--- @class RegTypeOccurrence
|
||||
--- @field reg string
|
||||
@@ -38,44 +30,30 @@ local duffle = dofile(_bootstrap_dir .. "../duffle_paths.lua")
|
||||
--- @field source_line integer
|
||||
|
||||
--- @class Findings
|
||||
--- @field errors PassFinding[]
|
||||
--- @field warnings PassFinding[]
|
||||
--- @field info PassFinding[]
|
||||
--- @field errors Finding[]
|
||||
--- @field warnings Finding[]
|
||||
--- @field info Finding[]
|
||||
|
||||
--- @class PipeCtx
|
||||
--- @field atom_index table<string, AtomEntry> -- raw_name or name -> scan.atoms row (kind atom/atom_proc)
|
||||
--- @field binds_index table<string, BindsEntry>
|
||||
--- @field annot_counts table<string, integer> -- bag: atom name -> annotation count
|
||||
--- @field types table<string, RegTypeDefault>
|
||||
--- @field atom_views table<string, AtomViewEntry>
|
||||
--- @field seen_defaults table<string, integer> -- bag: register ident -> occurrence count
|
||||
--- @field seen_field table<string, integer> -- bag: leftover field-count slot
|
||||
--- @field _scan SourceScan
|
||||
--- @field word_counts WordCounts|nil
|
||||
--- @field register_alias_registry table<string, AliasEntry>|nil
|
||||
--- @field type_name_registry table<string, TypeNameEntry>|nil
|
||||
--- @field type_occurrences RegTypeOccurrence[]|nil
|
||||
--- @field atom_infos_list AtomInfoEntry[]|nil
|
||||
--- @field binds_list BindsEntry[]|nil
|
||||
-- PassScratch: see ps1_meta.lua
|
||||
|
||||
--- @class AnnotatedResult
|
||||
--- @field atoms AtomEntry[]
|
||||
--- @field annots AtomAnnotation[]
|
||||
--- @field annots AtomInfoEntry[]
|
||||
--- @field macros MacroEntry[]
|
||||
--- @field binds BindsEntry[]
|
||||
--- @field errors PassFinding[]
|
||||
--- @field warnings PassFinding[]
|
||||
--- @field info PassFinding[]
|
||||
--- @field errors Finding[]
|
||||
--- @field warnings Finding[]
|
||||
--- @field info Finding[]
|
||||
--- @field source string|nil
|
||||
|
||||
--- @class CheckRule
|
||||
--- @field per_annot (fun(item: AtomAnnotation, pipe_ctx: PipeCtx, findings: Findings): nil)|nil
|
||||
--- @field per_annot (fun(item: AtomInfoEntry, pipe_ctx: PassScratch, findings: Findings): nil)|nil
|
||||
|
||||
--- @class SourceScan
|
||||
--- @field type_occurrences RegTypeOccurrence[]|nil
|
||||
|
||||
--- @class AnnotationPass
|
||||
--- @field validate fun(ctx: PassCtx, src: SourceFile, corpus_pipe_ctx: PipeCtx|nil): AnnotatedResult
|
||||
--- @field validate fun(ctx: PassCtx, src: SourceFile, corpus_pipe_ctx: PassScratch|nil): AnnotatedResult
|
||||
--- @field run fun(ctx: PassCtx): PassResult
|
||||
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
@@ -85,8 +63,8 @@ local duffle = dofile(_bootstrap_dir .. "../duffle_paths.lua")
|
||||
--- `macro_word_drift` writes errors[] for missing or mismatched metadata and info[] for a match.
|
||||
|
||||
--- Check: Every annotated atom must have a matching MipsAtom_(name) declaration.
|
||||
--- @param info AtomAnnotation
|
||||
--- @param pipe_ctx PipeCtx
|
||||
--- @param info AtomInfoEntry
|
||||
--- @param pipe_ctx PassScratch
|
||||
--- @param findings Findings
|
||||
--- @return nil
|
||||
local function check_atom_decl_exists(info, pipe_ctx, findings)
|
||||
@@ -100,8 +78,8 @@ end
|
||||
|
||||
--- Check: Every atom may have AT MOST ONE annotation.
|
||||
--- Post-loop: Needs full-corpus `annot_counts` from pipe_ctx.
|
||||
--- @param _item AtomAnnotation|nil
|
||||
--- @param pipe_ctx PipeCtx
|
||||
--- @param _item AtomInfoEntry|nil
|
||||
--- @param pipe_ctx PassScratch
|
||||
--- @param findings Findings
|
||||
--- @return nil
|
||||
local function check_unique_annotation(_item, pipe_ctx, findings)
|
||||
@@ -117,8 +95,8 @@ end
|
||||
|
||||
--- Check: BIND atoms must reference a real Binds_* struct.
|
||||
--- I keep this as a warning so the annotation pass can report the common test-fixture case; `check_abi_handoff` in static analysis supplies the build-stopping error.
|
||||
--- @param info AtomAnnotation
|
||||
--- @param pipe_ctx PipeCtx
|
||||
--- @param info AtomInfoEntry
|
||||
--- @param pipe_ctx PassScratch
|
||||
--- @param findings Findings
|
||||
--- @return nil
|
||||
local function check_binds_struct_exists(info, pipe_ctx, findings)
|
||||
@@ -135,7 +113,7 @@ end
|
||||
--- Check: TAPE_WORDS(mac_X, N) ↔ WORD_COUNT(mac_X, N) drift.
|
||||
--- Three outcomes: missing (error), mismatch (error), match (info).
|
||||
--- @param m MacroEntry
|
||||
--- @param pipe_ctx PipeCtx
|
||||
--- @param pipe_ctx PassScratch
|
||||
--- @param findings Findings
|
||||
--- @return nil
|
||||
local function check_macro_word_drift(m, pipe_ctx, findings)
|
||||
@@ -164,7 +142,7 @@ end
|
||||
--- Check: atom_dbg_reg_default(R_X, <type>) targets an alias in `pipe_ctx.register_alias_registry` and a type in `pipe_ctx.type_name_registry`.
|
||||
--- Pointer depth remains bounded to 0 or 1, and duplicate defaults remain errors.
|
||||
--- @param _src SourceFile -- unused (kept for the per_source shape)
|
||||
--- @param pipe_ctx PipeCtx
|
||||
--- @param pipe_ctx PassScratch
|
||||
--- @param findings Findings
|
||||
--- @return nil
|
||||
local function check_semantic_reg_defaults(_src, pipe_ctx, findings)
|
||||
@@ -215,7 +193,7 @@ end
|
||||
--- Check: atom_reg_types(R_X, <type>) entries target an alias in `pipe_ctx.register_alias_registry` and a type in `pipe_ctx.type_name_registry`.
|
||||
--- A bare `atom_reg` marker opts the `R_<n>` alias into GPR identity; references to R_T0..R_T3 require the same explicit marker.
|
||||
--- @param _src SourceFile
|
||||
--- @param pipe_ctx PipeCtx
|
||||
--- @param pipe_ctx PassScratch
|
||||
--- @param findings Findings
|
||||
--- @return nil
|
||||
local function check_atom_reg_types(_src, pipe_ctx, findings)
|
||||
@@ -247,7 +225,7 @@ end
|
||||
|
||||
--- Check: atom_view(Binds_X) entries reference a Binds_* struct with at least one field.
|
||||
--- @param _src SourceFile
|
||||
--- @param pipe_ctx PipeCtx
|
||||
--- @param pipe_ctx PassScratch
|
||||
--- @param findings Findings
|
||||
--- @return nil
|
||||
local function check_atom_view_layout(_src, pipe_ctx, findings)
|
||||
@@ -277,7 +255,7 @@ end
|
||||
|
||||
--- Check: Binds_* structs require unique field names because atom_view uses those names for typed-field lookup in gdb.
|
||||
--- @param _src SourceFile
|
||||
--- @param pipe_ctx PipeCtx
|
||||
--- @param pipe_ctx PassScratch
|
||||
--- @param findings Findings
|
||||
--- @return nil
|
||||
local function check_binds_no_duplicate_fields(_src, pipe_ctx, findings)
|
||||
@@ -310,7 +288,7 @@ end
|
||||
--- 6. unsupported target_kind -> marker precedes an unrelated declaration
|
||||
--- Valid markers stamp `debug_skip` on whole-atom, bare-component, and proc-component declaration records in scan_source.lua.
|
||||
--- @param marker DebugSkipMarker
|
||||
--- @param _pipe_ctx PipeCtx -- Unused; kept for consistency with per_annot
|
||||
--- @param _pipe_ctx PassScratch -- Unused; kept for consistency with per_annot
|
||||
--- @param findings Findings
|
||||
--- @return nil
|
||||
local function check_skip_marker(marker, _pipe_ctx, findings)
|
||||
@@ -368,7 +346,7 @@ end
|
||||
--- Warn when a source references an unregistered alias.
|
||||
--- When a source uses an unregistered R_X, this check emits one pass-level info entry for that source and directs C-ABI register names to explicit alias registration.
|
||||
--- @param _src SourceFile
|
||||
--- @param pipe_ctx PipeCtx
|
||||
--- @param pipe_ctx PassScratch
|
||||
--- @param findings Findings
|
||||
--- @return nil
|
||||
local function check_wave_context_migration(_src, pipe_ctx, findings)
|
||||
@@ -425,9 +403,9 @@ local CHECK_RULES = { ---@type CheckRule[]
|
||||
--- Builds one pass-wide pipe_ctx from the merged `corpus.*` registries and source-ordered `corpus.atom_infos`; per-source declarations and bodies remain in `src.scan`.
|
||||
--- The module ownership contract above requires callers to construct `ctx.shared.corpus` through `build_ctx`; the error message below enforces that gate.
|
||||
--- @param ctx PassCtx
|
||||
--- @return PipeCtx
|
||||
--- @return PassScratch
|
||||
local function build_corpus_pipe_ctx(ctx)
|
||||
local view = duffle.corpus_view(ctx) ---@type PipeCtx
|
||||
local view = duffle.corpus_view(ctx) ---@type PassScratch
|
||||
local annot_counts = {} ---@type table<string, integer> -- bag: atom name -> annotation count
|
||||
for _, info in ipairs(view.atom_infos) do ---@type integer, AtomInfoEntry
|
||||
if info and info.atom_name then
|
||||
@@ -443,7 +421,7 @@ end
|
||||
--- Validate one source against its pre-scanned SourceScan payload + the corpus-wide pipe_ctx.
|
||||
--- @param ctx PassCtx
|
||||
--- @param src SourceFile
|
||||
--- @param corpus_pipe_ctx PipeCtx|nil -- Built once per pass from corpus registries; nil builds the same projection here.
|
||||
--- @param corpus_pipe_ctx PassScratch|nil -- Built once per pass from corpus registries; nil builds the same projection here.
|
||||
--- @return AnnotatedResult
|
||||
local function validate(ctx, src, corpus_pipe_ctx)
|
||||
corpus_pipe_ctx = corpus_pipe_ctx or build_corpus_pipe_ctx(ctx)
|
||||
@@ -453,7 +431,7 @@ local function validate(ctx, src, corpus_pipe_ctx)
|
||||
local seen_defaults = {}; for reg, _ in pairs (scan.types or {}) do seen_defaults[reg] = (seen_defaults[reg] or 0) + 1 end ---@type table<string, integer> -- bag: register ident -> occurrence count
|
||||
local atom_infos_list = {}; for _, ai in ipairs(scan.atom_infos or {}) do atom_infos_list[#atom_infos_list + 1] = ai end ---@type AtomInfoEntry[]
|
||||
|
||||
local pipe_ctx = { ---@type PipeCtx
|
||||
local pipe_ctx = { ---@type PassScratch
|
||||
atom_index = {},
|
||||
binds_index = {},
|
||||
annot_counts = corpus_pipe_ctx.annot_counts,
|
||||
@@ -548,12 +526,12 @@ M.validate = validate
|
||||
--- @return PassResult
|
||||
function M.run(ctx)
|
||||
local outputs = {} ---@type PassOutputEntry[]
|
||||
local errors = {} ---@type PassFinding[]
|
||||
local warnings = {} ---@type PassFinding[]
|
||||
local errors = {} ---@type Finding[]
|
||||
local warnings = {} ---@type Finding[]
|
||||
|
||||
-- Build the shared pipe_ctx once for this run; every validate() call sees the same cross-source registries.
|
||||
-- The corpus owns the canonical cross-source registries; per-source scans retain body / declaration ownership.
|
||||
local corpus_pipe_ctx = build_corpus_pipe_ctx(ctx) ---@type PipeCtx
|
||||
local corpus_pipe_ctx = build_corpus_pipe_ctx(ctx) ---@type PassScratch
|
||||
local corpus = ctx.shared.corpus ---@type Corpus
|
||||
|
||||
-- Group `corpus.sources_by_dir` by module, validate every source in each bucket, and emit one errors.h per directory.
|
||||
@@ -562,17 +540,17 @@ function M.run(ctx)
|
||||
for dir, dir_sources in pairs(by_dir) do ---@type string, SourceFile[]
|
||||
local dir_basename = dir:match("([^/\\]+)$") or dir ---@type string
|
||||
local dir_atoms = 0 ---@type integer
|
||||
local dir_errors = {} ---@type PassFinding[]
|
||||
local dir_warnings = {} ---@type PassFinding[]
|
||||
local dir_errors = {} ---@type Finding[]
|
||||
local dir_warnings = {} ---@type Finding[]
|
||||
for _, src in ipairs(dir_sources) do ---@type integer, SourceFile
|
||||
local result = validate(ctx, src, corpus_pipe_ctx) ---@type AnnotatedResult
|
||||
result.source = src.path -- tag for downstream rendering
|
||||
dir_atoms = dir_atoms + #result.atoms
|
||||
for _, e in ipairs(result.errors) do ---@type integer, PassFinding
|
||||
for _, e in ipairs(result.errors) do ---@type integer, Finding
|
||||
dir_errors[#dir_errors + 1] = { line = e.line, msg = e.msg, source = src.path }
|
||||
errors [#errors + 1] = { line = e.line, msg = e.msg }
|
||||
end
|
||||
for _, w in ipairs(result.warnings) do ---@type integer, PassFinding
|
||||
for _, w in ipairs(result.warnings) do ---@type integer, Finding
|
||||
dir_warnings[#dir_warnings + 1] = { line = w.line, msg = w.msg }
|
||||
warnings [#warnings + 1] = { line = w.line, msg = w.msg }
|
||||
end
|
||||
|
||||
@@ -595,8 +595,8 @@ end
|
||||
--- @return PassResult
|
||||
function M.run(ctx)
|
||||
local outputs = {} ---@type PassOutputEntry[]
|
||||
local errors = {} ---@type PassFinding[]
|
||||
local warnings = {} ---@type PassFinding[]
|
||||
local errors = {} ---@type Finding[]
|
||||
local warnings = {} ---@type Finding[]
|
||||
|
||||
local corpus = ctx.shared and ctx.shared.corpus ---@type Corpus|nil
|
||||
if type(corpus) ~= "table" or type(corpus.source_order) ~= "table" then
|
||||
|
||||
+15
-17
@@ -28,8 +28,8 @@
|
||||
|
||||
--- @class AutoRegResult
|
||||
--- @field outputs AutoRegOutput[]
|
||||
--- @field errors PassFinding[]
|
||||
--- @field warnings PassFinding[]
|
||||
--- @field errors Finding[]
|
||||
--- @field warnings Finding[]
|
||||
|
||||
--- @class AutoRegPass
|
||||
--- @field run fun(ctx: PassCtx): AutoRegResult
|
||||
@@ -37,6 +37,7 @@
|
||||
|
||||
local _bootstrap_dir = debug.getinfo(1, "S").source:match("^@?(.*[/\\])") or "./" ---@type string
|
||||
local duffle = dofile(_bootstrap_dir .. "../duffle_paths.lua") ---@type DuffleExport
|
||||
local isa = require("duffle_isa") ---@type DuffleIsa
|
||||
|
||||
--- ════════════════════════════════════════════════════════════════════════════
|
||||
--- THE GPR ALLOCATION POOL — what's allocatable, and (more importantly) WHY
|
||||
@@ -54,15 +55,12 @@ local duffle = dofile(_bootstrap_dir .. "../duffle_paths.lua")
|
||||
--- R_K0/K1 (codes 26-27) — Kernel / interrupt handler reserves. Never touched by user code.
|
||||
--- R_GP/SP/FP/RA (codes 28-31) — R_SP/R_FP/R_RA are tape-runtime carriers between tape_enter and tape_exit; R_GP stays the host global pointer.
|
||||
---
|
||||
local POOL = { ---@type GprIdent[]
|
||||
"R_V0", "R_V1",
|
||||
"R_T0", "R_T1", "R_T2", "R_T3",
|
||||
"R_T4", "R_T5", "R_T6", "R_T7",
|
||||
"R_A0", "R_A1", "R_A2", "R_A3",
|
||||
"R_S0", "R_S1", "R_S2", "R_S3",
|
||||
"R_S4", "R_S5", "R_S6", "R_S7",
|
||||
"R_T8", "R_T9",
|
||||
}
|
||||
local POOL = {} ---@type GprIdent[]
|
||||
for _, row in ipairs(isa.GPR_ROLE) do ---@type integer, GprRole
|
||||
if row.pool then
|
||||
POOL[#POOL + 1] = row.name
|
||||
end
|
||||
end
|
||||
|
||||
-- Map from integer MIPS GPR code (the `code` field on AliasEntry) to the physical GPR ident in POOL.
|
||||
-- The standard MIPS O32 ABI register numbering matches mips.h's R_*_Code #defines (mips.h).
|
||||
@@ -94,7 +92,7 @@ end
|
||||
--- @param phase_label string
|
||||
--- @param decls table<string, string> -- bag: auto-reg symbol -> decl payload
|
||||
--- @return GprAllocMap
|
||||
--- @return PassFinding[]
|
||||
--- @return Finding[]
|
||||
local function allocate_phase(phase_label, decls)
|
||||
-- Deep-copy POOL into a fresh sequence table. The original `table.unpack and table.unpack(POOL) or { unpack(POOL) }`
|
||||
-- idiom wraps the unpacked values in a single inner table under LuaJIT 5.1 (`table.unpack` is nil; the `or` returns one value),
|
||||
@@ -102,7 +100,7 @@ local function allocate_phase(phase_label, decls)
|
||||
local pool = {} ---@type GprIdent[]
|
||||
for i = 1, #POOL do pool[i] = POOL[i] end ---@type integer
|
||||
local result = {} ---@type GprAllocMap
|
||||
local errors = {} ---@type PassFinding[]
|
||||
local errors = {} ---@type Finding[]
|
||||
for _, sym in ipairs(stable_sort_keys(decls)) do ---@type integer, string
|
||||
local next_gpr = table.remove(pool, 1) ---@type GprIdent|nil
|
||||
if not next_gpr then
|
||||
@@ -226,8 +224,8 @@ local M = {} ---@type AutoRegPass
|
||||
--- @return AutoRegResult
|
||||
function M.run(ctx)
|
||||
local outputs = {} ---@type AutoRegOutput[]
|
||||
local errors = {} ---@type PassFinding[]
|
||||
local warnings = {} ---@type PassFinding[]
|
||||
local errors = {} ---@type Finding[]
|
||||
local warnings = {} ---@type Finding[]
|
||||
|
||||
local corpus = ctx.shared and ctx.shared.corpus ---@type Corpus|nil
|
||||
if type(corpus) ~= "table" then
|
||||
@@ -244,12 +242,12 @@ function M.run(ctx)
|
||||
-- 1. Allocate phase pools first (phase declarations take precedence over per-atom declarations).
|
||||
local phase_allocations = {} ---@type table<string, GprAllocMap> -- bag: phase_label -> alloc map
|
||||
for phase_label, decls in pairs(corpus.phase_auto_regs or {}) do ---@type string, table<string, string>
|
||||
local mapping, errs = allocate_phase(phase_label, decls) ---@type GprAllocMap, PassFinding[]
|
||||
local mapping, errs = allocate_phase(phase_label, decls) ---@type GprAllocMap, Finding[]
|
||||
for sym, gpr in pairs(mapping) do ---@type string, GprIdent
|
||||
phase_allocations[phase_label] = phase_allocations[phase_label] or {}
|
||||
phase_allocations[phase_label][sym] = gpr
|
||||
end
|
||||
for _, e in ipairs(errs) do ---@type integer, PassFinding
|
||||
for _, e in ipairs(errs) do ---@type integer, Finding
|
||||
errors[#errors + 1] = e
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
--- passes/components.lua — Component-macro header generator.
|
||||
---
|
||||
--- Ownership: `corpus.word_counts`, `corpus.components`, and `corpus.component_body_index`.
|
||||
--- Ownership: `corpus.word_counts` and `corpus.components`.
|
||||
--- Scanner owns `declaration_comment` and `debug_skip` on each declaration record; this pass projects both forward.
|
||||
---
|
||||
--- Reads the pre-scanned SourceScan payload from `duffle.scan_source` for `MipsAtomComp_(ac_X)` and `MipsAtomComp_Proc_(ac_X, { body })` declarations (kind="comp_bare" / "comp_proc"),
|
||||
@@ -56,7 +56,6 @@ local MACS_FILENAME = "macs.h" ---@type string
|
||||
-- SourceScan, AtomEntry, CorpusCollision, CollisionSite: see scan_source.lua
|
||||
-- BodyToken: see emission_model.lua
|
||||
-- WordCounts: see word_count_eval.lua
|
||||
-- ComponentBodyEntry: see duffle_emit.lua
|
||||
-- InstructionRow, GteCommandRow: see duffle_isa.lua
|
||||
|
||||
--- @class Component
|
||||
@@ -70,6 +69,11 @@ local MACS_FILENAME = "macs.h" ---@type string
|
||||
--- @field comment string|nil -- Scanner-owned `declaration_comment`; the components pass reads it from the scanner record
|
||||
--- @field kind string -- "comp_bare" | "comp_proc" (atom_proc is NOT a component — see `project_components`)
|
||||
--- @field debug_skip boolean -- Mirror of `a.debug_skip` (scanner-owned); true iff a bare `atom_dbg_skip` marker immediately preceded the declaration
|
||||
--- @field path string|nil -- Slash-normalized source path (collision sites)
|
||||
--- @field source string|nil -- Absolute source path (emit)
|
||||
--- @field line_of (fun(pos: integer): integer)|nil
|
||||
--- @field cycle_cost integer|nil -- From metadata[c.name]; nil when the body was not costed
|
||||
--- @field gp0_contrib integer|nil -- From metadata[c.name]; nil when the body was not costed
|
||||
|
||||
--- @class ComponentMeta
|
||||
--- @field cycle_cost integer
|
||||
@@ -800,16 +804,7 @@ local function update_canonical_word_counts(corpus, components, counts)
|
||||
end
|
||||
end
|
||||
|
||||
--- @class ComponentDef
|
||||
--- @field name string -- Bare name (without ac_/mac_ prefix)
|
||||
--- @field line integer -- Definition source line (line of `MipsAtomComp_(ac_X)` / `MipsAtomComp_Proc_(ac_X, ...)`)
|
||||
--- @field path string -- Absolute source path of the definition
|
||||
--- @field kind string -- "comp_bare" | "comp_proc" (atom_proc is NOT a component)
|
||||
--- @field debug_skip boolean -- Mirror of the scanner-owned `a.debug_skip`; consumers read this directly
|
||||
--- @field cycle_cost integer|nil -- From metadata[c.name]; nil when the body was not costed
|
||||
--- @field gp0_contrib integer|nil -- From metadata[c.name]; nil when the body was not costed
|
||||
|
||||
--- (internal) Populate `corpus.components` with this source's components-by-name map.
|
||||
--- (internal) Populate `corpus.components` with this source's one component row per bare name.
|
||||
--- First declaration wins; later declarations of the same bare name are dropped and recorded as a collision via `corpus.collisions` (kind = "component").
|
||||
--- The pass does NOT write to `ctx.shared.components`.
|
||||
--- No parallel skip map is built here; consumers that need the per-component skip state read `corpus.components[name].debug_skip` directly.
|
||||
@@ -818,28 +813,29 @@ end
|
||||
--- @param src SourceFile
|
||||
--- @param components Component[]
|
||||
--- @param metadata ComponentMetaMap
|
||||
--- @param scan SourceScan
|
||||
--- @return nil
|
||||
local function update_canonical_components(corpus, src, components, metadata)
|
||||
local function update_canonical_components(corpus, src, components, metadata, scan)
|
||||
local rel_path = src.path:gsub("\\", "/") ---@type string
|
||||
local line_of = scan and scan.line_of ---@type (fun(pos: integer): integer)|nil
|
||||
for _, c in ipairs(components) do ---@type integer, Component
|
||||
-- Keyed by bare name (e.g. `yield`, `load_tri_indices`).
|
||||
-- The atoms_source_map pass looks up components by bare name from the corpus;
|
||||
-- `mac_` prefix lives at the call-site identifier and is stripped before lookup.
|
||||
local m = metadata and metadata[c.name] or nil ---@type ComponentMeta|nil
|
||||
if corpus.components[c.name] == nil then
|
||||
corpus.components[c.name] = {
|
||||
name = c.name,
|
||||
line = c.line,
|
||||
path = rel_path,
|
||||
kind = c.kind or "comp_bare",
|
||||
debug_skip = c.debug_skip == true,
|
||||
cycle_cost = m and m.cycle_cost or nil,
|
||||
gp0_contrib = m and m.gp0_contrib or nil,
|
||||
}
|
||||
c.path = rel_path
|
||||
c.source = src.path
|
||||
c.line_of = line_of
|
||||
c.kind = c.kind or "comp_bare"
|
||||
c.debug_skip = c.debug_skip == true
|
||||
c.cycle_cost = m and m.cycle_cost or nil
|
||||
c.gp0_contrib = m and m.gp0_contrib or nil
|
||||
corpus.components[c.name] = c
|
||||
else
|
||||
-- A second declaration of the same bare name: record a typed collision so static-analysis + the report can surface it.
|
||||
-- Identical-shape declarations (same path + line) reuse the first-wins entry without a collision record.
|
||||
local existing = corpus.components[c.name] ---@type ComponentDef
|
||||
local existing = corpus.components[c.name] ---@type Component
|
||||
if existing.path ~= rel_path or existing.line ~= c.line then
|
||||
local kind = c.kind or "comp_bare" ---@type string
|
||||
local first_kind = existing.kind or "comp_bare" ---@type string
|
||||
@@ -856,37 +852,12 @@ local function update_canonical_components(corpus, src, components, metadata)
|
||||
end
|
||||
end
|
||||
|
||||
--- (internal) Populate `corpus.component_body_index` with this source's body index entries.
|
||||
--- First declaration wins; later declarations are dropped (no separate collision record: the components collision is already surfaced by `update_canonical_components`).
|
||||
--- The pass writes to `corpus.component_body_index` only (the corpus owns this projection).
|
||||
--- @param corpus Corpus
|
||||
--- @param src SourceFile
|
||||
--- @param components Component[]
|
||||
--- @param scan SourceScan
|
||||
--- @return nil
|
||||
local function update_canonical_component_body_index(corpus, src, components, scan)
|
||||
local line_of = scan and scan.line_of ---@type (fun(pos: integer): integer)|nil
|
||||
for _, c in ipairs(components) do ---@type integer, Component
|
||||
if corpus.component_body_index[c.name] == nil then
|
||||
corpus.component_body_index[c.name] = {
|
||||
body_tokens = c.body_tokens,
|
||||
body_off = c.body_off,
|
||||
line_of = line_of,
|
||||
source = src.path,
|
||||
declaration = c.line,
|
||||
kind = c.kind,
|
||||
arg_names = c.arg_names,
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- @param ctx PassCtx
|
||||
--- @return PassResult
|
||||
function M.run(ctx)
|
||||
local outputs = {} ---@type MacsOutput[]
|
||||
local errors = {} ---@type PassFinding[]
|
||||
local warnings = {} ---@type PassFinding[]
|
||||
local errors = {} ---@type Finding[]
|
||||
local warnings = {} ---@type Finding[]
|
||||
|
||||
-- Corpus ownership gate.
|
||||
local corpus = ctx.shared and ctx.shared.corpus ---@type Corpus|nil
|
||||
@@ -904,8 +875,7 @@ function M.run(ctx)
|
||||
|
||||
-- Projection ownership:
|
||||
-- * `corpus.word_counts["mac_"..name]` — current component count
|
||||
-- * `corpus.components[name]` — bare-name component definition
|
||||
-- * `corpus.component_body_index[name]` — body / line_of / source index
|
||||
-- * `corpus.components[name]` — one row: body, line_of, source, cost
|
||||
-- The pass writes to the corpus only; consumers read from the corpus directly.
|
||||
|
||||
-- Per-directory aggregation: every source in the same directory contributes to one `gen/macs.h`.
|
||||
@@ -937,8 +907,7 @@ function M.run(ctx)
|
||||
for _, src in ipairs(sources) do ---@type integer, SourceFile
|
||||
local per_source = project_components(src.text, src.scan) or {} ---@type Component[]
|
||||
if #per_source > 0 then
|
||||
update_canonical_components(corpus, src, per_source, metadata_per_source[src])
|
||||
update_canonical_component_body_index(corpus, src, per_source, src.scan)
|
||||
update_canonical_components(corpus, src, per_source, metadata_per_source[src], src.scan)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -450,7 +450,7 @@ local DEFAULT_BASENAME = "hello_gte" ---@type string
|
||||
|
||||
--- DieSchema / FORM_WRITERS / atom-table records live on this file.
|
||||
--- SourceFile, Path, AtomName: see duffle.lua.
|
||||
--- Corpus, PassCtx, PassResult, PassFlags, PassShared, PassFinding: see ps1_meta.lua.
|
||||
--- Corpus, PassCtx, PassResult, PassFlags, PassShared, Finding: see ps1_meta.lua.
|
||||
--- AtomEntry, AliasEntry, TypeNameEntry, TypeField, AtomInfoEntry, BindsEntry,
|
||||
--- AtomViewEntry, AtomCtxEntry, AtomPhaseGroup, SourceScan, RegTypeOverride: see scan_source.lua.
|
||||
--- AtomPaths, WordEvent, BodyToken: see emission_model.lua.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
--- passes/emission_model.lua: Per-atom emission projection.
|
||||
---
|
||||
--- The `emission-model` pass owns `atom.paths`, the canonical per-atom mutable surface for atoms and raw atoms with bodies in `ctx.shared.corpus.source_order`.
|
||||
--- For each atom, the pass invokes `duffle.project_emission(body_text, component_index, word_counts, components)`.
|
||||
--- For each atom, the pass invokes `duffle.project_emission(body_text, components, word_counts, components)`.
|
||||
--- It stores the ordered `items` stream plus the dense `word_events` / `markers` / `invocations` views on `atom.paths`.
|
||||
---
|
||||
--- Public boundary:
|
||||
@@ -92,17 +92,7 @@
|
||||
--- @field consuming_encoder string|nil
|
||||
--- @field consuming_arg_pos integer|nil
|
||||
|
||||
--- @class EmitError
|
||||
--- @field kind string
|
||||
--- @field line integer|nil
|
||||
--- @field msg string
|
||||
--- @field source string|nil
|
||||
--- @field schema_name string|nil
|
||||
|
||||
--- @class EmitWarning
|
||||
--- @field kind string
|
||||
--- @field line integer|nil
|
||||
--- @field msg string
|
||||
-- Finding: see ps1_meta.lua
|
||||
|
||||
--- @class AtomPaths
|
||||
--- @field tokens BodyToken[]
|
||||
@@ -111,8 +101,8 @@
|
||||
--- @field word_events WordEvent[]
|
||||
--- @field markers EmissionMarker[]
|
||||
--- @field invocations InvocationRecord[]
|
||||
--- @field errors EmitError[]
|
||||
--- @field warnings EmitWarning[]
|
||||
--- @field errors Finding[]
|
||||
--- @field warnings Finding[]
|
||||
|
||||
--- @class EmissionModelPass
|
||||
--- @field run fun(ctx: PassCtx): PassResult
|
||||
@@ -136,7 +126,7 @@ local duffle = dofile(_bootstrap_dir .. "../duffle_paths.lua")
|
||||
-- * ROOT invocations (`inv.parent_id == 0`) receive body-relative `call_line` values directly from `M.LineIndex(body_text)` in the walker.
|
||||
-- The source `line_of` closure supplies physical lines at the close site, so this function converts each root value exactly once.
|
||||
-- * INNER invocations (`inv.parent_id ~= 0`) receive physical `call_line` values directly from the COMPONENT's `line_of` in the walker.
|
||||
-- Recursive descent forwards that closure through `corpus.component_body_index[name].line_of`; those values arrive physical and remain unchanged.
|
||||
-- Recursive descent forwards that closure through `corpus.components[name].line_of`; those values arrive physical and remain unchanged.
|
||||
--
|
||||
-- After this function, every `inv.call_line` is physical. DWARF and provenance output read it directly.
|
||||
-- The word-event loop forwards the already-physical `outer_inv.call_line` into `we.call_line` for words inside an invocation.
|
||||
@@ -155,7 +145,7 @@ local function stamp_root_provenance(projection, atom_record, src, corpus)
|
||||
-- The walker assigns line 2 to the body's first content line because line 1 is the trailing `\n` after `{`. Body-text line k therefore maps to `root_body_line + (k - 1)`.
|
||||
-- `body_off - 1` points at the opening `{`, whose line index identifies the header line. `body_off` points after `{` and would shift every word row forward by one line.
|
||||
local root_body_line = root_line_of(atom_record.body_off - 1) or atom_record.line or 0 ---@type integer
|
||||
local component_index = corpus.component_body_index or {} ---@type table<string, ComponentBodyEntry>
|
||||
local components = corpus.components or {} ---@type table<string, Component>
|
||||
local word_items = {} ---@type EmissionItem[]
|
||||
|
||||
for _, item in ipairs(projection.items) do ---@type integer, EmissionItem
|
||||
@@ -176,7 +166,7 @@ local function stamp_root_provenance(projection, atom_record, src, corpus)
|
||||
local inner_id = ids[#ids] ---@type integer
|
||||
local inner_inv = inner_id and projection.invocations[inner_id] ---@type InvocationRecord|nil
|
||||
if inner_inv then
|
||||
local component = component_index[inner_inv.component_name] ---@type ComponentBodyEntry|nil
|
||||
local component = components[inner_inv.component_name] ---@type Component|nil
|
||||
if component and component.line_of then
|
||||
-- Walker used `comp.line_of`, which is the source's physical LineIndex. item.line is already physical.
|
||||
return item.line or 0
|
||||
@@ -257,13 +247,13 @@ end
|
||||
local function project_atom(atom_record, src, corpus)
|
||||
local body = atom_record.body or "" ---@type string
|
||||
local wc = corpus.word_counts or {} ---@type WordCounts
|
||||
local cbi = corpus.component_body_index or {} ---@type table<string, ComponentBodyEntry>
|
||||
local comps = corpus.components or {} ---@type table<string, Component>
|
||||
local schema = nil ---@type RegUseSchema|nil
|
||||
if atom_record.reg_use_schema_name then
|
||||
schema = corpus.reg_use_schemas and corpus.reg_use_schemas[atom_record.reg_use_schema_name]
|
||||
end
|
||||
-- That construction site stamps `invocation.debug_skip` while appending each record to `proj.invocations`.
|
||||
local proj = duffle.project_emission(body, cbi, wc, corpus.components, { ---@type EmissionProjection
|
||||
local proj = duffle.project_emission(body, comps, wc, comps, { ---@type EmissionProjection
|
||||
reg_use_schema = schema,
|
||||
reg_use_param = atom_record.reg_use_param_name,
|
||||
atom_name = atom_record.name,
|
||||
@@ -271,13 +261,22 @@ local function project_atom(atom_record, src, corpus)
|
||||
})
|
||||
if atom_record.reg_use_schema_name and not schema then
|
||||
proj.errors[#proj.errors + 1] = {
|
||||
kind = "reguse_missing_schema",
|
||||
kind = "error",
|
||||
check = "reguse_missing_schema",
|
||||
msg = string.format("RegUse schema %q is missing", atom_record.reg_use_schema_name),
|
||||
schema_name = atom_record.reg_use_schema_name,
|
||||
}
|
||||
end
|
||||
for _, err in ipairs(corpus.reg_use_errors or {}) do ---@type integer, EmitError
|
||||
for _, err in ipairs(corpus.reg_use_errors or {}) do ---@type integer, RegUseError
|
||||
if err.schema_name == atom_record.reg_use_schema_name then
|
||||
proj.errors[#proj.errors + 1] = err
|
||||
proj.errors[#proj.errors + 1] = {
|
||||
kind = "error",
|
||||
check = err.kind,
|
||||
line = err.line or err.source_line or 0,
|
||||
msg = err.msg or "",
|
||||
source = err.source or err.source_file,
|
||||
schema_name = err.schema_name,
|
||||
}
|
||||
end
|
||||
end
|
||||
local paths = { ---@type AtomPaths
|
||||
@@ -303,8 +302,8 @@ end
|
||||
--- @return PassResult
|
||||
function M.run(ctx)
|
||||
local outputs = {} ---@type PassOutputEntry[]
|
||||
local errors = {} ---@type EmitError[]
|
||||
local warnings = {} ---@type EmitWarning[]
|
||||
local errors = {} ---@type Finding[]
|
||||
local warnings = {} ---@type Finding[]
|
||||
|
||||
local corpus = ctx and ctx.shared and ctx.shared.corpus ---@type Corpus|nil
|
||||
if type(corpus) ~= "table" then error("emission_model: ctx.shared.corpus is required (canonical projection)", 0) end
|
||||
@@ -322,18 +321,22 @@ function M.run(ctx)
|
||||
return
|
||||
end
|
||||
local proj = project_atom(atom, src, corpus) ---@type EmissionProjection
|
||||
for _, e in ipairs(proj.errors) do ---@type integer, EmitError
|
||||
-- Preserve `kind` (cycle / count_mismatch / unbalanced) so readers dispatch on the diagnostic class and leave the message string as display text.
|
||||
for _, e in ipairs(proj.errors) do ---@type integer, Finding
|
||||
-- Finding.kind is severity. Finding.check holds the diagnostic code
|
||||
-- (cycle / count_mismatch / unbalanced / reguse_*).
|
||||
errors[#errors + 1] = {
|
||||
kind = e.kind,
|
||||
kind = "error",
|
||||
check = e.check,
|
||||
line = e.line,
|
||||
msg = e.msg,
|
||||
source = e.source or src.path,
|
||||
schema_name = e.schema_name,
|
||||
}
|
||||
end
|
||||
for _, w in ipairs(proj.warnings) do ---@type integer, EmitWarning
|
||||
for _, w in ipairs(proj.warnings) do ---@type integer, Finding
|
||||
warnings[#warnings + 1] = {
|
||||
kind = w.kind,
|
||||
kind = "warning",
|
||||
check = w.check,
|
||||
line = w.line,
|
||||
msg = w.msg,
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ end
|
||||
--- missing `consuming_encoder` -> ERROR. A lone top-level `atom_offset` is not a branch.
|
||||
--- @param labels table<string, integer>
|
||||
--- @param branches OffsetBranch[]
|
||||
--- @param errors PassFinding[]
|
||||
--- @param errors Finding[]
|
||||
--- @return BranchOffset[]
|
||||
local function compute_offsets(labels, branches, errors)
|
||||
local results = {} ---@type BranchOffset[]
|
||||
@@ -259,7 +259,7 @@ local M = {} ---@type OffsetsPass
|
||||
--- @param ctx PassCtx
|
||||
--- @param dir string
|
||||
--- @param sources SourceFile[]
|
||||
--- @param errors PassFinding[]
|
||||
--- @param errors Finding[]
|
||||
--- @return string|nil
|
||||
local function process_directory(ctx, dir, sources, errors)
|
||||
local atoms_data = {} ---@type AtomData[]
|
||||
@@ -297,8 +297,8 @@ end
|
||||
--- @return PassResult
|
||||
function M.run(ctx)
|
||||
local outputs = {} ---@type OffsetOutput[]
|
||||
local errors = {} ---@type PassFinding[]
|
||||
local warnings = {} ---@type PassFinding[]
|
||||
local errors = {} ---@type Finding[]
|
||||
local warnings = {} ---@type Finding[]
|
||||
|
||||
local corpus = ctx.shared and ctx.shared.corpus ---@type Corpus|nil
|
||||
if type(corpus) ~= "table" then
|
||||
|
||||
+70
-42
@@ -54,36 +54,17 @@ local PASS_NAME = "report" ---@type string
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
-- SourceFile: see duffle.lua
|
||||
-- PassCtx, PassResult, PassFinding, PassOutputEntry: see ps1_meta.lua
|
||||
-- PassCtx, PassResult, Finding, PassOutputEntry: see ps1_meta.lua
|
||||
-- AtomEntry, SourceScan, BindsEntry, AtomInfoEntry, AliasEntry,
|
||||
-- AtomPhaseGroup, AtomViewEntry, AtomCtxEntry, CorpusCollision: see scan_source.lua
|
||||
-- CheckFinding, AtomAnalysis: see static_analysis.lua
|
||||
-- AtomAnalysis: see static_analysis.lua
|
||||
-- WordCounts: see word_count_eval.lua
|
||||
-- ComponentBodyEntry: see duffle_emit.lua
|
||||
-- Component: see components.lua
|
||||
-- AtomPaths, WordEvent: see emission_model.lua
|
||||
-- GprAllocMap: see auto_reg.lua
|
||||
|
||||
-- Shapes produced by `passes/annotation.lua`'s `M.validate()`.
|
||||
|
||||
--- @class AnnotEntry
|
||||
--- @field line integer -- Source line
|
||||
--- @field macro string -- Macro name (e.g. "atom_reads")
|
||||
--- @field name string -- Atom name (if a `name(...)` was given)
|
||||
--- @field kind string -- "atom_info" | "atom_bind" | ...
|
||||
--- @field binds string|nil -- Binds_X name if any
|
||||
--- @field reads string[] -- R_* names (read targets)
|
||||
--- @field writes string[] -- R_* names (write targets)
|
||||
--- @field error string|nil -- Error message if annotation was malformed
|
||||
|
||||
--- @class BindsField
|
||||
--- @field name string -- Field name
|
||||
--- @field offset integer -- Byte offset within the Binds_X struct
|
||||
|
||||
--- @class BindsStruct
|
||||
--- @field name string -- Struct name (e.g. "Binds_Floor")
|
||||
--- @field line integer -- Source line of the typedef
|
||||
--- @field bytes integer -- Total byte size
|
||||
--- @field fields BindsField[] -- The field list
|
||||
-- Binds_* rows are BindsEntry (scan_source.lua). Report reads src.scan.binds.
|
||||
|
||||
--- @class MacroEntry
|
||||
--- @field name string -- Macro name (e.g. "WORD_COUNT(my_macro, 4)")
|
||||
@@ -93,12 +74,12 @@ local PASS_NAME = "report" ---@type string
|
||||
--- @class AnnotationResult
|
||||
--- @field source string -- Set by this pass; original source path
|
||||
--- @field atoms AtomEntry[] -- Atom declarations in this source
|
||||
--- @field annots AnnotEntry[] -- Annotation entries
|
||||
--- @field annots AtomInfoEntry[] -- Annotation entries
|
||||
--- @field macros MacroEntry[] -- Macro word-count declarations
|
||||
--- @field binds BindsStruct[] -- Binds_* struct declarations
|
||||
--- @field errors PassFinding[] -- Errors from validation
|
||||
--- @field warnings PassFinding[] -- Warnings from validation
|
||||
--- @field info PassFinding[] -- Info summary (not rendered here)
|
||||
--- @field binds BindsEntry[] -- Binds_* struct declarations
|
||||
--- @field errors Finding[] -- Errors from validation
|
||||
--- @field warnings Finding[] -- Warnings from validation
|
||||
--- @field info Finding[] -- Info summary (not rendered here)
|
||||
|
||||
--- @class ModuleEntry
|
||||
--- @field dir string -- Absolute directory path
|
||||
@@ -196,7 +177,7 @@ local PASS_NAME = "report" ---@type string
|
||||
--- @field sources SourceFile[]
|
||||
--- @field decls AtomEntry[]
|
||||
--- @field schemas RegUseSchema[]
|
||||
--- @field findings CheckFinding[]
|
||||
--- @field findings Finding[]
|
||||
--- @field corpus Corpus
|
||||
|
||||
--- @class SectionRenderer
|
||||
@@ -425,10 +406,10 @@ end
|
||||
--- @return nil
|
||||
local function render_section_components(add, view)
|
||||
local rows = {} ---@type ComponentReportRow[]
|
||||
local index = (view.corpus and view.corpus.component_body_index) or {} ---@type table<string, ComponentBodyEntry>
|
||||
local index = (view.corpus and view.corpus.components) or {} ---@type table<string, Component>
|
||||
for _, a in ipairs(view.decls) do ---@type integer, AtomEntry
|
||||
if a.kind == "comp_bare" or a.kind == "comp_proc" then
|
||||
local idx = index[a.name] or {} ---@type ComponentBodyEntry
|
||||
local idx = index[a.name] or {} ---@type Component
|
||||
local args = idx.arg_names or {} ---@type string[]
|
||||
rows[#rows + 1] = {
|
||||
name = a.name,
|
||||
@@ -563,6 +544,50 @@ local function render_section_binds(add, view)
|
||||
if not wrote then add("_(none)_"); add("") end
|
||||
end
|
||||
|
||||
--- @param add fun(s: string): nil
|
||||
--- @param view ModuleView
|
||||
--- @return nil
|
||||
local function render_section_atom_bundles(add, view)
|
||||
local bundles = (view.corpus and view.corpus.atom_bundles) or {} ---@type table<string, AtomBundle>
|
||||
local names = {} ---@type string[]
|
||||
for name, bundle in pairs(bundles) do ---@type string, AtomBundle
|
||||
if path_in_module(bundle.path, view) then
|
||||
names[#names + 1] = name
|
||||
end
|
||||
end
|
||||
if #names == 0 then add("_(none)_"); add(""); return end
|
||||
table.sort(names)
|
||||
for _, name in ipairs(names) do ---@type integer, string
|
||||
local bundle = bundles[name] ---@type AtomBundle
|
||||
local entries = bundle.entries or {} ---@type table<string, string>
|
||||
add(string.format("### %s", name))
|
||||
for _, slot in ipairs(bundle.slots or {}) do ---@type integer, string
|
||||
local ident = entries[slot] ---@type string|nil
|
||||
if ident then
|
||||
add(string.format("- `%s` `%s`", slot, ident))
|
||||
else
|
||||
add(string.format("- `%s`", slot))
|
||||
end
|
||||
end
|
||||
add("")
|
||||
end
|
||||
end
|
||||
|
||||
--- @param add fun(s: string): nil
|
||||
--- @param view ModuleView
|
||||
--- @return nil
|
||||
local function render_section_tape_emits(add, view)
|
||||
local wrote = false ---@type boolean
|
||||
for _, src in ipairs(view.sources or {}) do ---@type integer, SourceFile
|
||||
for _, emit in ipairs((src.scan and src.scan.tape_emits) or {}) do ---@type integer, TapeEmit
|
||||
wrote = true
|
||||
add(string.format("- `%s` `%s`", emit.name or "?", emit.binds or "—"))
|
||||
end
|
||||
end
|
||||
if not wrote then add("_(none)_"); add(""); return end
|
||||
add("")
|
||||
end
|
||||
|
||||
--- @param add fun(s: string): nil
|
||||
--- @param view ModuleView
|
||||
--- @return nil
|
||||
@@ -698,8 +723,8 @@ end
|
||||
--- @param view ModuleView
|
||||
--- @return nil
|
||||
local function render_section_findings(add, view)
|
||||
local by_atom = {} ---@type table<string, CheckFinding[]>
|
||||
for _, f in ipairs(view.findings or {}) do ---@type integer, CheckFinding
|
||||
local by_atom = {} ---@type table<string, Finding[]>
|
||||
for _, f in ipairs(view.findings or {}) do ---@type integer, Finding
|
||||
local key = f.atom or "?" ---@type string
|
||||
by_atom[key] = by_atom[key] or {}
|
||||
by_atom[key][#by_atom[key] + 1] = f
|
||||
@@ -707,11 +732,11 @@ local function render_section_findings(add, view)
|
||||
if next(by_atom) == nil then add("_(none)_"); add(""); return end
|
||||
local seen = {} ---@type table<string, boolean> -- bag: atom name already emitted
|
||||
--- @param name string
|
||||
--- @param fs CheckFinding[]
|
||||
--- @param fs Finding[]
|
||||
--- @return nil
|
||||
local function emit(name, fs)
|
||||
add("### " .. name)
|
||||
for _, f in ipairs(fs) do ---@type integer, CheckFinding
|
||||
for _, f in ipairs(fs) do ---@type integer, Finding
|
||||
local msg = f.msg or "" ---@type string
|
||||
local slot = slot_suffix(f.gpr_key or f.producer_destination) ---@type string|nil
|
||||
if slot and not msg:find("(slot ", 1, true) then
|
||||
@@ -766,11 +791,12 @@ local HIDDEN_UNLESS_WRITTEN = { ---@type table<string, boolean> -- bag: GPR key
|
||||
R_AT = true, R_TapePtr = true, R_AtomJmp = true,
|
||||
}
|
||||
|
||||
local PHYSICAL_GPR = { ---@type table<string, boolean> -- bag: physical GPR alias -> true
|
||||
R_T0 = true, R_T1 = true, R_T2 = true, R_T3 = true,
|
||||
R_T4 = true, R_T5 = true, R_T6 = true, R_T7 = true,
|
||||
R_V0 = true, R_V1 = true,
|
||||
}
|
||||
local PHYSICAL_GPR = {} ---@type table<string, boolean> -- bag: physical GPR alias -> true
|
||||
for _, row in ipairs(duffle.GPR_ROLE) do ---@type integer, GprRole
|
||||
if row.pool then
|
||||
PHYSICAL_GPR[row.name] = true
|
||||
end
|
||||
end
|
||||
|
||||
--- @param atom AtomEntry
|
||||
--- @param key string
|
||||
@@ -913,6 +939,8 @@ local SECTION_RENDERERS = { ---@type SectionRenderer[]
|
||||
{ header = "## Annotations", render = render_section_annotations },
|
||||
{ header = "## Component annotations", render = render_section_component_annotations },
|
||||
{ header = "## Binds_* structs", render = render_section_binds },
|
||||
{ header = "## Atom bundles", render = render_section_atom_bundles },
|
||||
{ header = "## Tape emits", render = render_section_tape_emits },
|
||||
{ header = "## Phases / views / ctx", render = render_section_phases },
|
||||
{ header = "## Register aliases", render = render_section_aliases },
|
||||
{ header = "## Auto-reg", render = render_section_autoreg },
|
||||
@@ -945,7 +973,7 @@ local function render_module_meta_report(view)
|
||||
n_macros = n_macros + #((src.scan and src.scan.macros) or {})
|
||||
end
|
||||
local n_err, n_warn, n_info = 0, 0, 0 ---@type integer, integer, integer
|
||||
for _, f in ipairs(view.findings or {}) do ---@type integer, CheckFinding
|
||||
for _, f in ipairs(view.findings or {}) do ---@type integer, Finding
|
||||
if f.kind == "error" then n_err = n_err + 1
|
||||
elseif f.kind == "warning" then n_warn = n_warn + 1
|
||||
else n_info = n_info + 1
|
||||
@@ -1087,7 +1115,7 @@ function M.run(ctx)
|
||||
n_macros = n_macros + #((src.scan and src.scan.macros) or {})
|
||||
end
|
||||
local n_err, n_warn, n_info = 0, 0, 0 ---@type integer, integer, integer
|
||||
for _, f in ipairs(view.findings or {}) do ---@type integer, CheckFinding
|
||||
for _, f in ipairs(view.findings or {}) do ---@type integer, Finding
|
||||
if f.kind == "error" then n_err = n_err + 1
|
||||
elseif f.kind == "warning" then n_warn = n_warn + 1
|
||||
else n_info = n_info + 1
|
||||
|
||||
@@ -118,6 +118,7 @@ local parse_enum_int_literal ---@type fun(text: string, start: integer): (intege
|
||||
--- @field ctx_atom string|nil
|
||||
--- @field phase string|nil
|
||||
--- @field info_line integer
|
||||
--- @field errors string[]|nil -- parse-time atom_info errors
|
||||
|
||||
--- @class BindsEntry
|
||||
--- @field line integer
|
||||
@@ -126,6 +127,13 @@ local parse_enum_int_literal ---@type fun(text: string, start: integer): (intege
|
||||
--- @field body string
|
||||
--- @field bytes integer|nil
|
||||
|
||||
--- @class AtomBundle
|
||||
--- @field name string
|
||||
--- @field slots string[] -- typedef order
|
||||
--- @field line integer
|
||||
--- @field path string
|
||||
--- @field entries table<string, string>|nil -- slot → B_E when an AtomBundleEntry_ proc exists
|
||||
|
||||
--- @class AliasEntry
|
||||
--- @field name string
|
||||
--- @field code integer
|
||||
@@ -181,6 +189,14 @@ local parse_enum_int_literal ---@type fun(text: string, start: integer): (intege
|
||||
--- @class TapeChain
|
||||
--- @field [integer] string -- ordered atom names in one tb_emit chain
|
||||
|
||||
--- @class TapeEmit
|
||||
--- @field name string
|
||||
--- @field binds string|nil
|
||||
--- @field line integer
|
||||
--- @field path string
|
||||
--- @field slot string|nil
|
||||
--- @field data_words integer|nil
|
||||
|
||||
--- @class RegUseView
|
||||
--- @field names string[]
|
||||
--- @field lanes boolean
|
||||
@@ -207,6 +223,8 @@ local parse_enum_int_literal ---@type fun(text: string, start: integer): (intege
|
||||
--- @field reg_use_schemas table<string, RegUseSchema>
|
||||
--- @field reg_use_errors RegUseError[]
|
||||
--- @field tape_chains TapeChain[]
|
||||
--- @field tape_emits TapeEmit[]
|
||||
--- @field atom_bundles table<string, AtomBundle>
|
||||
--- @field _source_file string|nil
|
||||
--- @field _code_macros table<string, integer>|nil -- bag
|
||||
--- @field _code_macro_bodies table<string, string>|nil -- bag
|
||||
@@ -2438,11 +2456,76 @@ local function parse_typedef_array(source, pos, id2_end, line_of, out, after_typ
|
||||
return semi and (semi + 1) or after_paren
|
||||
end
|
||||
|
||||
--- Parse `MipsAtom *slot, …;` from an AtomBundle_ typedef body.
|
||||
--- Ignores the MipsAtom type token. Slot name is the ident after `*`.
|
||||
--- @param body string
|
||||
--- @return string[]
|
||||
local function parse_atom_bundle_slots(body)
|
||||
local slots = {} ---@type string[]
|
||||
local pos = 1 ---@type integer
|
||||
while pos <= #body do
|
||||
pos = duffle.skip_ws_and_cmt(body, pos)
|
||||
if pos > #body then break end
|
||||
local b = body:byte(pos) ---@type integer
|
||||
if b == BYTE_SEMI then
|
||||
break
|
||||
elseif b == BYTE_COMMA then
|
||||
pos = pos + 1
|
||||
elseif b == BYTE_STAR then
|
||||
local after_star = duffle.skip_ws_and_cmt(body, pos + 1) ---@type integer
|
||||
local ident, ident_end = duffle.read_ident(body, after_star) ---@type string|nil, integer
|
||||
if ident then
|
||||
slots[#slots + 1] = ident
|
||||
pos = ident_end
|
||||
else
|
||||
pos = pos + 1
|
||||
end
|
||||
else
|
||||
local ident, ident_end = duffle.read_ident(body, pos) ---@type string|nil, integer
|
||||
if ident then
|
||||
pos = ident_end
|
||||
else
|
||||
pos = pos + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return slots
|
||||
end
|
||||
|
||||
-- Shape 5: `typedef AtomBundle_(<name>) { MipsAtom *slot, … };`
|
||||
--- @param source string
|
||||
--- @param pos integer
|
||||
--- @param id2_end integer
|
||||
--- @param line_of fun(pos: integer): integer
|
||||
--- @param out SourceScan
|
||||
--- @param after_typedef integer
|
||||
--- @return integer
|
||||
local function parse_typedef_atom_bundle(source, pos, id2_end, line_of, out, after_typedef)
|
||||
local inner, after_paren, open_paren = read_parens_after(source, id2_end, id2_end) ---@type string|nil, integer, integer
|
||||
if not inner then return id2_end end
|
||||
local name = duffle.trim(inner) ---@type string
|
||||
|
||||
local body, after_brace = find_body_braces(source, after_paren, open_paren + 1) ---@type string|nil, integer
|
||||
if not body then return after_brace end
|
||||
if name ~= "" and out.atom_bundles[name] == nil then
|
||||
local bundle = { ---@type AtomBundle
|
||||
name = name,
|
||||
slots = parse_atom_bundle_slots(body),
|
||||
line = line_of(pos),
|
||||
path = out._source_file or "",
|
||||
}
|
||||
out.atom_bundles[name] = bundle
|
||||
end
|
||||
attach_debug_skip_marker(out, "unrelated")
|
||||
return after_brace
|
||||
end
|
||||
|
||||
local TYPE_FORMS = { ---@type table<string, fun(source: string, pos: integer, id2_end: integer, line_of: fun(pos: integer): integer, out: SourceScan, after_typedef: integer): integer>
|
||||
Struct_ = parse_typedef_struct,
|
||||
Enum_ = parse_typedef_enum,
|
||||
TSet_ = parse_typedef_tset,
|
||||
Array_ = parse_typedef_array,
|
||||
AtomBundle_ = parse_typedef_atom_bundle,
|
||||
}
|
||||
|
||||
--- Parse: `typedef` declarations.
|
||||
@@ -2826,6 +2909,23 @@ local function parse_addrs_assign(source, pos, ident_end, line_of, out)
|
||||
return rhs
|
||||
end
|
||||
|
||||
--- @param out SourceScan
|
||||
--- @param name string
|
||||
--- @param last string
|
||||
--- @param line integer
|
||||
--- @return nil
|
||||
local function push_tape_emit(out, name, last, line)
|
||||
out.tape_emits = out.tape_emits or {}
|
||||
out.tape_emits[#out.tape_emits + 1] = { ---@type TapeEmit
|
||||
name = name,
|
||||
binds = nil,
|
||||
line = line,
|
||||
path = out._source_file or "",
|
||||
slot = last:find("->", 1, true) and name or nil,
|
||||
data_words = 0,
|
||||
}
|
||||
end
|
||||
|
||||
--- @param source string
|
||||
--- @param pos integer
|
||||
--- @param ident_end integer
|
||||
@@ -2836,10 +2936,12 @@ local function parse_tb_emit_(source, pos, ident_end, line_of, out)
|
||||
local after = duffle.skip_ws_and_cmt(source, ident_end) ---@type integer
|
||||
if source:sub(after, after) ~= "(" then return ident_end end
|
||||
local inner, after_p = duffle.read_parens(source, after) ---@type string|nil, integer
|
||||
local name = duffle.trim(inner or ""):match("^([%w_]+)") ---@type string
|
||||
local last = duffle.trim(inner or "") ---@type string
|
||||
local name = last:match("^([%w_]+)") ---@type string
|
||||
if name then
|
||||
out._chain = out._chain or {}
|
||||
out._chain[#out._chain + 1] = name
|
||||
push_tape_emit(out, name, last, line_of(pos))
|
||||
end
|
||||
return after_p or (after + 1)
|
||||
end
|
||||
@@ -2864,6 +2966,49 @@ local function parse_tb_emit(source, pos, ident_end, line_of, out)
|
||||
if name then
|
||||
out._chain = out._chain or {}
|
||||
out._chain[#out._chain + 1] = name
|
||||
push_tape_emit(out, name, last, line_of(pos))
|
||||
end
|
||||
return after_p or (after + 1)
|
||||
end
|
||||
|
||||
--- @param source string
|
||||
--- @param pos integer
|
||||
--- @param ident_end integer
|
||||
--- @param line_of fun(pos: integer): integer
|
||||
--- @param out SourceScan
|
||||
--- @return integer
|
||||
local function parse_tb_bind_(source, pos, ident_end, line_of, out)
|
||||
local after = duffle.skip_ws_and_cmt(source, ident_end) ---@type integer
|
||||
if source:sub(after, after) ~= "(" then return ident_end end
|
||||
local inner, after_p = duffle.read_parens(source, after) ---@type string|nil, integer
|
||||
local args = duffle.split_top_level_commas(inner or "") ---@type string[]
|
||||
local typ = duffle.trim(args[2] or "") ---@type string
|
||||
if typ ~= "" then
|
||||
local emits = out.tape_emits or {} ---@type TapeEmit[]
|
||||
for i = #emits, 1, -1 do ---@type integer
|
||||
if emits[i].binds == nil then
|
||||
emits[i].binds = typ
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
return after_p or (after + 1)
|
||||
end
|
||||
|
||||
--- @param source string
|
||||
--- @param pos integer
|
||||
--- @param ident_end integer
|
||||
--- @param line_of fun(pos: integer): integer
|
||||
--- @param out SourceScan
|
||||
--- @return integer
|
||||
local function parse_tb_data(source, pos, ident_end, line_of, out)
|
||||
local after = duffle.skip_ws_and_cmt(source, ident_end) ---@type integer
|
||||
if source:sub(after, after) ~= "(" then return ident_end end
|
||||
local _, after_p = duffle.read_parens(source, after) ---@type string|nil, integer
|
||||
local emits = out.tape_emits or {} ---@type TapeEmit[]
|
||||
local last = emits[#emits] ---@type TapeEmit|nil
|
||||
if last then
|
||||
last.data_words = (last.data_words or 0) + 1
|
||||
end
|
||||
return after_p or (after + 1)
|
||||
end
|
||||
@@ -2871,6 +3016,8 @@ end
|
||||
local C_STMT_PARSERS = { ---@type table<string, fun(source: string, pos: integer, ident_end: integer, line_of: fun(pos: integer): integer, out: SourceScan): integer>
|
||||
tb_emit_ = parse_tb_emit_,
|
||||
tb_emit = parse_tb_emit,
|
||||
tb_bind_ = parse_tb_bind_,
|
||||
tb_data = parse_tb_data,
|
||||
addrs = parse_addrs_assign,
|
||||
}
|
||||
|
||||
@@ -2923,6 +3070,8 @@ local function scan_source(source, source_file, code_macros, code_macro_bodies)
|
||||
atoms = {},
|
||||
raw_atoms = {},
|
||||
binds = {},
|
||||
atom_bundles = {},
|
||||
tape_emits = {},
|
||||
atom_infos = {},
|
||||
component_atom_infos = {},
|
||||
macros = {},
|
||||
@@ -3237,6 +3386,8 @@ local function merge_corpus_registries(corpus)
|
||||
corpus.reg_use_schemas = corpus.reg_use_schemas or {}
|
||||
corpus.reg_use_errors = corpus.reg_use_errors or {}
|
||||
corpus.tape_chains = corpus.tape_chains or {}
|
||||
corpus.atom_bundles = corpus.atom_bundles or {}
|
||||
corpus.tape_emits = corpus.tape_emits or {}
|
||||
|
||||
-- Replace the existing corpus collections with empty tables so a re-run on the same corpus produces identical state (deterministic merge).
|
||||
-- This is safe because M.run is the only writer to these tables within a single orchestrator invocation.
|
||||
@@ -3254,6 +3405,8 @@ local function merge_corpus_registries(corpus)
|
||||
"reg_use_schemas",
|
||||
"reg_use_errors",
|
||||
"tape_chains",
|
||||
"atom_bundles",
|
||||
"tape_emits",
|
||||
}) do
|
||||
corpus[key] = {}
|
||||
end
|
||||
@@ -3361,6 +3514,55 @@ local function merge_corpus_registries(corpus)
|
||||
for _, chain in ipairs(scan.tape_chains or {}) do ---@type integer, TapeChain
|
||||
corpus.tape_chains[#corpus.tape_chains + 1] = chain
|
||||
end
|
||||
for _, emit in ipairs(scan.tape_emits or {}) do ---@type integer, TapeEmit
|
||||
corpus.tape_emits[#corpus.tape_emits + 1] = emit
|
||||
end
|
||||
|
||||
-- atom_bundles: keyed by typedef name. First-wins per name (like components).
|
||||
for name, bundle in pairs(scan.atom_bundles or {}) do ---@type string, AtomBundle
|
||||
if corpus.atom_bundles[name] == nil then
|
||||
corpus.atom_bundles[name] = bundle
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Join slot roles to AtomBundleEntry_ identities after catalogs and atoms exist.
|
||||
-- Do not invent a catalog from an entry that has no typedef.
|
||||
for _, bundle in pairs(corpus.atom_bundles) do ---@type string, AtomBundle
|
||||
local entries = {} ---@type table<string, string>
|
||||
for _, slot in ipairs(bundle.slots or {}) do ---@type integer, string
|
||||
local atom_name = bundle.name .. "_" .. slot ---@type string
|
||||
if corpus.atoms_by_name[atom_name] then
|
||||
entries[slot] = atom_name
|
||||
end
|
||||
end
|
||||
if next(entries) then
|
||||
bundle.entries = entries
|
||||
end
|
||||
end
|
||||
|
||||
-- Resolve tb_emit names: atom first, else unique catalog slot → entries[slot].
|
||||
-- Ambiguous slot or missing entry: leave the raw ident (no A/B later).
|
||||
for _, emit in ipairs(corpus.tape_emits) do ---@type integer, TapeEmit
|
||||
if corpus.atoms_by_name[emit.name] == nil then
|
||||
local slot = emit.slot or emit.name ---@type string
|
||||
local hit = nil ---@type AtomBundle|nil
|
||||
local n = 0 ---@type integer
|
||||
for _, bundle in pairs(corpus.atom_bundles) do ---@type string, AtomBundle
|
||||
for _, s in ipairs(bundle.slots or {}) do ---@type integer, string
|
||||
if s == slot then
|
||||
n = n + 1
|
||||
hit = bundle
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
local ident = hit and hit.entries and hit.entries[slot] ---@type string|nil
|
||||
if n == 1 and ident then
|
||||
emit.name = ident
|
||||
emit.slot = slot
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+532
-430
File diff suppressed because it is too large
Load Diff
@@ -4,13 +4,13 @@
|
||||
--- 1. **Public utility** `M.count_token_words(token, wc)`: Used by `passes/offsets.lua`, `passes/annotation.lua`, and other passes.
|
||||
--- 2. **Pass entry** `M.run(ctx)`: Loads the authored `word_count.metadata.h` into `ctx.shared.corpus.word_counts` for downstream passes.
|
||||
--- The generated `.macs.h` files are OUTPUT artifacts and are NOT inputs to this pass;
|
||||
--- Current component counts are owned by `passes/components.lua` (which populates `corpus.word_counts` and `corpus.component_body_index`
|
||||
--- Current component counts are owned by `passes/components.lua` (which populates `corpus.word_counts` and `corpus.components`
|
||||
--- AFTER computing each current count from the just-built body + `corpus.word_counts`).
|
||||
---
|
||||
--- **Canonical contract**:
|
||||
--- * `ctx.shared.corpus.word_counts` is the count table.
|
||||
--- * `corpus.word_counts` is the sole count table. Consumers read `corpus.word_counts` directly.
|
||||
--- * `ctx.shared.components` and `ctx.shared.component_body_index` are NOT created by this pass (projections only).
|
||||
--- * `ctx.shared.components` is NOT created by this pass (projections only).
|
||||
--- * No `.macs.h` recursive discovery (no `scan_dir`, no scan cache, no `_invalidate_scan_cache`).
|
||||
---
|
||||
--- **Conventions**: tabs (1/level), EmmyLua annotations, no regex,
|
||||
|
||||
+55
-62
@@ -76,8 +76,9 @@ local PASS_FLAG_DISPATCH_KEY = "__pass__" ---@type string
|
||||
--- @field atom_ctxs table<AtomName, AtomCtxEntry>
|
||||
--- @field atom_phases table<string, AtomPhaseGroup>
|
||||
--- @field word_counts WordCounts
|
||||
--- @field components table<string, ComponentDef>
|
||||
--- @field component_body_index table<string, ComponentBodyEntry>
|
||||
--- @field components table<string, Component>
|
||||
--- @field atom_bundles table<string, AtomBundle>|nil
|
||||
--- @field tape_emits TapeEmit[]|nil
|
||||
--- @field collisions CorpusCollision[]
|
||||
--- @field resolver SourceResolver
|
||||
--- @field component_atom_infos AtomInfoEntry[]|nil
|
||||
@@ -104,9 +105,43 @@ local PASS_FLAG_DISPATCH_KEY = "__pass__" ---@type string
|
||||
--- @field flags PassFlags -- CLI flags + per-pass stash
|
||||
--- @field verbose boolean -- If true, log diagnostic info
|
||||
|
||||
--- @class PassFinding
|
||||
--- @field line integer -- Source line (or 0 for pass-level)
|
||||
--- @field msg string -- Finding message
|
||||
--- CheckName: see static_analysis.lua. AtomName: see duffle.lua.
|
||||
--- @class Finding
|
||||
--- @field line integer
|
||||
--- @field msg string
|
||||
--- @field kind string|nil -- error | warning | info
|
||||
--- @field atom AtomName|nil
|
||||
--- @field check CheckName|nil
|
||||
--- @field source string|nil -- optional; emit/reguse path
|
||||
--- @field schema_name string|nil -- optional; emit/reguse
|
||||
|
||||
--- @class PassScratch
|
||||
--- @field corpus Corpus|nil
|
||||
--- @field info_by_atom table<string, AtomInfoEntry>|nil
|
||||
--- @field binds_index table<string, BindsEntry>|nil
|
||||
--- @field atom_index table<string, AtomEntry>|nil
|
||||
--- @field annot_counts table<string, integer>|nil -- bag
|
||||
--- @field types table<string, RegTypeDefault>|nil
|
||||
--- @field atom_views table<string, AtomViewEntry>|nil
|
||||
--- @field seen_defaults table<string, integer>|nil -- bag
|
||||
--- @field seen_field table<string, integer>|nil -- bag
|
||||
--- @field _scan SourceScan|nil
|
||||
--- @field word_counts WordCounts|nil
|
||||
--- @field register_alias_registry table<string, AliasEntry>|nil
|
||||
--- @field type_name_registry table<string, TypeNameEntry>|nil
|
||||
--- @field type_occurrences RegTypeOccurrence[]|nil
|
||||
--- @field atom_infos_list AtomInfoEntry[]|nil
|
||||
--- @field binds_list BindsEntry[]|nil
|
||||
--- @field unknown_seen table<string, integer>|nil -- bag
|
||||
--- @field atoms AtomEntry[]|nil
|
||||
--- @field components_by_name table<string, Component>|nil
|
||||
--- @field atoms_by_name table<string, AtomEntry>|nil
|
||||
--- @field tape_chains table<string, string[]>|nil
|
||||
--- @field source_order SourceFile[]|nil
|
||||
--- @field component_atom_infos AtomInfoEntry[]|nil
|
||||
--- @field atom_infos_all AtomInfoEntry[]|nil
|
||||
--- @field gte_cr_alias_groups GteCrAliasGroup[]|nil
|
||||
--- @field line_for_word_event (fun(ev: WordEvent): integer)|nil
|
||||
|
||||
--- @class PassOutputEntry
|
||||
--- @field kind string
|
||||
@@ -114,9 +149,9 @@ local PASS_FLAG_DISPATCH_KEY = "__pass__" ---@type string
|
||||
|
||||
--- @class PassResult
|
||||
--- @field outputs PassOutputEntry[]
|
||||
--- @field errors PassFinding[] -- Build-stops (per-pass kind policy)
|
||||
--- @field warnings PassFinding[] -- Informational
|
||||
--- @field info CheckFinding[]|nil -- static_analysis only
|
||||
--- @field errors Finding[] -- Build-stops (per-pass kind policy)
|
||||
--- @field warnings Finding[] -- Informational
|
||||
--- @field info Finding[]|nil -- static_analysis only
|
||||
|
||||
--- @class ParsedArgs
|
||||
--- @field requested_set string[] -- Pass names to run (explicit --all expanded)
|
||||
@@ -340,8 +375,7 @@ COMMON_FLAGS:
|
||||
--help Show this help and exit
|
||||
|
||||
EXIT CODES:
|
||||
0 All requested passes succeeded
|
||||
1 Validation errors found
|
||||
0 Ran. Findings print on stderr and in the report; they do not fail the process.
|
||||
2 Metaprogram internal error
|
||||
|
||||
EXAMPLES:
|
||||
@@ -604,56 +638,15 @@ local function build_ctx(args)
|
||||
end
|
||||
resolution = resolved
|
||||
else
|
||||
local source_order = {} ---@type SourceFile[]
|
||||
local sources_by_path = {} ---@type table<Path, SourceFile>
|
||||
local resolver = { ---@type SourceResolver
|
||||
resolved = {},
|
||||
skipped = {},
|
||||
shadowed = {},
|
||||
}
|
||||
for _, input_path in ipairs(args.sources) do ---@type integer, string
|
||||
local path = duffle.normalize_path(input_path) ---@type string
|
||||
local key_ok, key_or_error = pcall(duffle.canonical_path_key, path) ---@type boolean, string
|
||||
if not key_ok then
|
||||
error("ps1_meta: invalid --source " .. input_path .. ": " .. tostring(key_or_error), 0)
|
||||
end
|
||||
local file = io.open(path, "r") ---@type file*|nil
|
||||
if not file then
|
||||
io.stderr:write("ps1_meta: cannot open --source " .. input_path .. "\n")
|
||||
local ok_exact, exact = pcall(duffle.resolve_exact_sources, { ---@type boolean, Corpus|string
|
||||
sources = args.sources,
|
||||
project_root = project_root,
|
||||
})
|
||||
if not ok_exact then
|
||||
io.stderr:write("ps1_meta: cannot resolve --source: " .. tostring(exact) .. "\n")
|
||||
os.exit(EXIT_INTERNAL_ERROR)
|
||||
end
|
||||
local text = file:read("*a") ---@type string
|
||||
file:close()
|
||||
|
||||
local source = { ---@type SourceFile
|
||||
path = path,
|
||||
text = text,
|
||||
dir = duffle.dirname(path),
|
||||
basename = duffle.basename_no_ext(path),
|
||||
}
|
||||
source_order[#source_order + 1] = source
|
||||
local key = key_or_error ---@type string
|
||||
if not sources_by_path[key] then sources_by_path[key] = source end
|
||||
resolver.resolved[#resolver.resolved + 1] = {
|
||||
include_path = path,
|
||||
include_text = nil,
|
||||
root_source = nil,
|
||||
root_line = nil,
|
||||
candidate_a = path,
|
||||
candidate_b = nil,
|
||||
selected_path = path,
|
||||
disposition = "exact",
|
||||
}
|
||||
end
|
||||
resolution = {
|
||||
unity_root = nil,
|
||||
project_root = project_root,
|
||||
code_root = duffle.normalize_path(project_root .. "/code"),
|
||||
source_order = source_order,
|
||||
sources_by_path = sources_by_path,
|
||||
sources_by_dir = duffle.group_sources_by_dir(source_order),
|
||||
resolver = resolver,
|
||||
}
|
||||
resolution = exact
|
||||
end
|
||||
|
||||
local corpus = { ---@type Corpus
|
||||
@@ -673,7 +666,8 @@ local function build_ctx(args)
|
||||
atom_phases = {},
|
||||
word_counts = {},
|
||||
components = {},
|
||||
component_body_index = {},
|
||||
atom_bundles = {},
|
||||
tape_emits = {},
|
||||
collisions = {},
|
||||
resolver = resolution.resolver,
|
||||
}
|
||||
@@ -791,7 +785,7 @@ end
|
||||
local function report_validation_errors(pass_name, pass, result)
|
||||
local has_errors = result.errors and #result.errors > 0 ---@type boolean
|
||||
if not has_errors then return false end
|
||||
for _, e in ipairs(result.errors) do ---@type integer, PassFinding
|
||||
for _, e in ipairs(result.errors) do ---@type integer, Finding
|
||||
io.stderr:write(string.format("[%s] line %d: %s\n", pass_name, e.line or 0, e.msg or ""))
|
||||
end
|
||||
return PASS_KIND_STOP_ON_ERROR[pass.kind] == true
|
||||
@@ -825,8 +819,7 @@ local function main(argv)
|
||||
local requested = args.requested_set ---@type string[]
|
||||
local closed = topo_sort(PASSES, requested) ---@type string[]
|
||||
|
||||
local had_errors = dispatch_passes(ctx, closed) ---@type boolean
|
||||
if had_errors then os.exit(EXIT_VALIDATION_ERRORS) end
|
||||
dispatch_passes(ctx, closed)
|
||||
end)
|
||||
|
||||
if not ok then
|
||||
|
||||
Reference in New Issue
Block a user