mirror of
https://github.com/Ed94/pikuma_ps1.git
synced 2026-08-25 02:20:33 +00:00
Reviewing. Successfuly reworked register allocation for tape runs.
This commit is contained in:
+16
-16
@@ -2,7 +2,7 @@
|
||||
local scan = require("duffle_scan")
|
||||
local isa = require("duffle_isa")
|
||||
local emit = require("duffle_emit")
|
||||
local M = {}
|
||||
local M = {}
|
||||
|
||||
local function merge(src, label)
|
||||
for k, v in pairs(src) do
|
||||
@@ -18,30 +18,30 @@ merge(isa, "duffle_isa")
|
||||
merge(emit, "duffle_emit")
|
||||
|
||||
function M.corpus_view(ctx)
|
||||
local corpus = ctx and ctx.shared and ctx.shared.corpus
|
||||
local corpus = ctx and ctx.shared and ctx.shared.corpus
|
||||
if not corpus then error("requires ctx.shared.corpus", 0) end
|
||||
return {
|
||||
register_alias_registry = corpus.register_alias_registry or {},
|
||||
type_name_registry = corpus.type_name_registry or {},
|
||||
atom_views = corpus.atom_views or {},
|
||||
atom_ctxs = corpus.atom_ctxs or {},
|
||||
atom_phases = corpus.atom_phases or {},
|
||||
binds_by_name = corpus.binds_by_name or {},
|
||||
atoms_by_name = corpus.atoms_by_name or {},
|
||||
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 {},
|
||||
type_name_registry = corpus.type_name_registry or {},
|
||||
atom_views = corpus.atom_views or {},
|
||||
atom_ctxs = corpus.atom_ctxs or {},
|
||||
atom_phases = corpus.atom_phases or {},
|
||||
binds_by_name = corpus.binds_by_name or {},
|
||||
atoms_by_name = corpus.atoms_by_name or {},
|
||||
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 {},
|
||||
}
|
||||
end
|
||||
|
||||
function M.run_check_rules(rules, phase, item, pipe_ctx, findings)
|
||||
for _, rule in ipairs(rules) do
|
||||
local fn = rule[phase]
|
||||
if fn then fn(item, pipe_ctx, findings) end
|
||||
if fn then fn(item, pipe_ctx, findings) end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+18
-21
@@ -74,11 +74,11 @@ end
|
||||
--- @param tok string
|
||||
--- @return string, string[]
|
||||
local function token_ident_and_args(tok)
|
||||
local ident, after = M.read_ident(tok, 1)
|
||||
local ident, after = M.read_ident(tok, 1)
|
||||
if not ident then return "?", {} end
|
||||
local paren_pos = M.skip_ws_and_cmt(tok, after)
|
||||
if tok:sub(paren_pos, paren_pos) ~= "(" then return ident, {} end
|
||||
local inner = M.read_parens(tok, paren_pos)
|
||||
local inner = M.read_parens(tok, paren_pos)
|
||||
if not inner then return ident, {} end
|
||||
return ident, split_call_args(inner)
|
||||
end
|
||||
@@ -104,9 +104,9 @@ local E_MAC_PREFIX_LEN = 4
|
||||
---
|
||||
--- Pure: reads `body_entry` / `component_index` / `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 table -- `{body_tokens, body_off, line_of, source, declaration}` (declaration = root atom's atom.line)
|
||||
--- @param component_index table -- the bare-name → ComponentBodyEntry map from M.get_component_body_index
|
||||
--- @param word_counts table -- macro name → emitted-word count (from `ctx.shared.word_counts`)
|
||||
--- @param body_entry table -- `{body_tokens, body_off, line_of, source, declaration}` (declaration = root atom's atom.line)
|
||||
--- @param component_index table -- the bare-name → ComponentBodyEntry map from M.get_component_body_index
|
||||
--- @param word_counts table -- macro name → emitted-word count (from `ctx.shared.word_counts`)
|
||||
--- @return WordEvent[], WordEventError[]
|
||||
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
@@ -775,18 +775,16 @@ end
|
||||
-------------------------------------------------------------------------------
|
||||
-- find_function_decl_for — backward walk for MipsAtomComp_Proc_ name extraction.
|
||||
--
|
||||
-- After the `sym` arg was dropped from MipsAtomComp_Proc_, the component name
|
||||
-- is derived from the preceding `FI_ Slice_MipsCode ac_X(args)` function
|
||||
-- declaration. This function walks backward from `before_pos` to find it.
|
||||
-- After the `sym` arg was dropped from MipsAtomComp_Proc_, the component name is derived from the preceding
|
||||
-- `FI_ Slice_MipsCode ac_X(args)` function declaration. This function walks backward from `before_pos` to find it.
|
||||
--
|
||||
-- Returns (raw_name, args_inner) or (nil, nil).
|
||||
-- raw_name — e.g. "ac_load_word_imm"
|
||||
-- args_inner — e.g. "AtomBuilder_R ab, Reg dst, U4 imm"
|
||||
--
|
||||
-- The walk finds the LAST "Slice_MipsCode" before before_pos, then skips
|
||||
-- whitespace + qualifiers (FI_, atom_dbg_skip, comments) until it finds an
|
||||
-- ident followed by "(". That ident is the function name; the parens contents
|
||||
-- are the args.
|
||||
-- The walk finds the LAST "Slice_MipsCode" before before_pos, then skips whitespace + qualifiers
|
||||
-- (FI_, atom_dbg_skip, comments) until it finds an ident followed by "(".
|
||||
-- That ident is the function name; the parens contents are the args.
|
||||
-------------------------------------------------------------------------------
|
||||
function M.find_function_decl_for(source, before_pos, slice_mips_code_len)
|
||||
local search_pos = 1
|
||||
@@ -819,13 +817,13 @@ function M.find_function_decl_for(source, before_pos, slice_mips_code_len)
|
||||
end
|
||||
-- skip block comments
|
||||
if source:sub(pos, pos + 1) == "/*" then
|
||||
local close = source:find("*/", pos + 2, true)
|
||||
local close = source:find("*/", pos + 2, true)
|
||||
if not close then break end
|
||||
pos = close + 2
|
||||
goto continue
|
||||
end
|
||||
-- try to read an ident
|
||||
local ident, ident_end = M.read_ident(source, pos)
|
||||
local ident, ident_end = M.read_ident(source, pos)
|
||||
if not ident then break end
|
||||
-- check if the next non-ws char after ident is "("
|
||||
local next_pos = M.skip_ws_and_cmt(source, ident_end)
|
||||
@@ -853,16 +851,15 @@ end
|
||||
-- args_inner — e.g. "AtomArena_R aa, U4 r_scratch, ..."
|
||||
-- after_paren — source position after the function `)`
|
||||
--
|
||||
-- The walk finds the LAST "MipsAtom*" before before_pos, then skips
|
||||
-- whitespace + qualifiers (internal, I_, FI_, comments) until it finds an
|
||||
-- ident followed by "(". That ident is the name. The parens contents are the args.
|
||||
-- The walk finds the LAST "MipsAtom*" before before_pos, then skips whitespace + qualifiers (internal, I_, FI_, comments)
|
||||
-- until it finds an ident followed by "(".
|
||||
-------------------------------------------------------------------------------
|
||||
function M.find_atom_proc_decl_for(source, before_pos, mips_atom_ptr_len)
|
||||
local search_pos = 1
|
||||
local last_match = nil
|
||||
while true do
|
||||
-- plain=true: "*" is literal, no escaping needed
|
||||
local found = source:find("MipsAtom*", search_pos, true)
|
||||
local found = source:find("MipsAtom*", search_pos, true)
|
||||
if not found or found >= before_pos then break end
|
||||
last_match = found
|
||||
search_pos = found + mips_atom_ptr_len
|
||||
@@ -889,13 +886,13 @@ function M.find_atom_proc_decl_for(source, before_pos, mips_atom_ptr_len)
|
||||
end
|
||||
-- skip block comments
|
||||
if source:sub(pos, pos + 1) == "/*" then
|
||||
local close = source:find("*/", pos + 2, true)
|
||||
local close = source:find("*/", pos + 2, true)
|
||||
if not close then break end
|
||||
pos = close + 2
|
||||
goto continue
|
||||
end
|
||||
-- try to read an ident
|
||||
local ident, ident_end = M.read_ident(source, pos)
|
||||
local ident, ident_end = M.read_ident(source, pos)
|
||||
if not ident then break end
|
||||
-- check if the next non-ws char after ident is "("
|
||||
local next_pos = M.skip_ws_and_cmt(source, ident_end)
|
||||
@@ -912,4 +909,4 @@ function M.find_atom_proc_decl_for(source, before_pos, mips_atom_ptr_len)
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
return M
|
||||
return M
|
||||
|
||||
+228
-1118
File diff suppressed because it is too large
Load Diff
@@ -22,12 +22,10 @@ local M = {}
|
||||
local CACHE_KEY = "__duffle_repo_root__"
|
||||
|
||||
--- Resolve the repo root from this script's own path. Zero shell spawn.
|
||||
--- `duffle_paths.lua` always lives at `<repo>/scripts/duffle_paths.lua`, so the repo root is the
|
||||
--- parent of the directory containing this script. We derive it directly from `debug.getinfo(1, "S").source`
|
||||
--- (returns `@<path>` for the currently-running chunk).
|
||||
--- `duffle_paths.lua` always lives at `<repo>/scripts/duffle_paths.lua`, so the repo root is the parent of the directory containing this script.
|
||||
--- We derive it directly from `debug.getinfo(1, "S").source` (returns `@<path>` for the currently-running chunk).
|
||||
---
|
||||
--- If `debug.getinfo` can't parse this script's path (shouldn't happen — dofile always populates source),
|
||||
--- return nil and let `M.setup()` fail loud.
|
||||
--- If `debug.getinfo` can't parse this script's path (shouldn't happen — dofile always populates source), return nil and let `M.setup()` fail loud.
|
||||
--- @return string|nil
|
||||
local function find_repo_root()
|
||||
if package.loaded[CACHE_KEY] then return package.loaded[CACHE_KEY] end
|
||||
@@ -51,17 +49,13 @@ end
|
||||
---
|
||||
--- This script does NOT touch the OS environment: no `os.setenv`, no `os.putenv`, no `$PATH` mods.
|
||||
--- It just sets `package.path` and `package.cpath` (the standard Lua way to register module search dirs).
|
||||
--- lpeg is built by `update_deps.ps1` to `toolchain/lpeg/`,
|
||||
--- which we wire into `package.cpath` here (so `require("lpeg")` from `duffle.lua` resolves without any global state).
|
||||
--- lpeg is built by `update_deps.ps1` to `toolchain/lpeg/`, which we wire into `package.cpath` here (so `require("lpeg")` from `duffle.lua` resolves without any global state).
|
||||
function M.setup()
|
||||
local repo_root = find_repo_root()
|
||||
if not repo_root then
|
||||
-- Unreachable in practice: find_repo_root() derives the repo root from this script's
|
||||
-- own source path via debug.getinfo(1, "S").source (no subprocess, no git CLI, <1ms).
|
||||
-- A nil return means the source path did not match the expected
|
||||
-- <repo>/scripts/duffle_paths.lua layout — a packaging bug, not a "missing git repo"
|
||||
-- condition. os.exit(2) is retained so a real failure surfaces loud rather than
|
||||
-- silently producing an unconfigured module table.
|
||||
-- Unreachable in practice: find_repo_root() derives the repo root from this script's own source path via debug.getinfo(1, "S").source (no subprocess, no git CLI, <1ms).
|
||||
-- A nil return means the source path did not match the expected <repo>/scripts/duffle_paths.lua layout — a packaging bug, not a "missing git repo" condition.
|
||||
-- os.exit(2) is retained so a real failure surfaces loud rather than silently producing an unconfigured module table.
|
||||
os.exit(2)
|
||||
end
|
||||
|
||||
@@ -86,6 +80,6 @@ end
|
||||
-- Run the setup as a side effect.
|
||||
M.setup()
|
||||
|
||||
-- Now that package.path includes scripts/, `require("duffle")` resolves. Return the duffle module
|
||||
-- so callers can do `local duffle = dofile(...duffle_paths.lua)` in one line.
|
||||
-- Now that package.path includes scripts/, `require("duffle")` resolves.
|
||||
-- Return the duffle module so callers can do `local duffle = dofile(...duffle_paths.lua)` in one line.
|
||||
return require("duffle")
|
||||
|
||||
+22
-23
@@ -422,10 +422,10 @@ end
|
||||
--- should use this directly rather than going through `read_form_value`,
|
||||
--- which only exposes the low 4 bytes to preserve its existing (value, next_pos) return shape.
|
||||
--- @param buf string
|
||||
--- @param pos integer -- zero-based wire offset
|
||||
--- @return integer -- low 4 bytes (LE), the type signature
|
||||
--- @return integer -- high 4 bytes (LE), the offset within the matching type unit
|
||||
--- @return integer -- cursor after the 8-byte value
|
||||
--- @param pos integer -- zero-based wire offset
|
||||
--- @return integer -- low 4 bytes (LE), the type signature
|
||||
--- @return integer -- high 4 bytes (LE), the offset within the matching type unit
|
||||
--- @return integer -- cursor after the 8-byte value
|
||||
function M.read_ref_sig8(buf, pos)
|
||||
return M.read_u32_le(buf, pos), M.read_u32_le(buf, pos + 4), pos + 8
|
||||
end
|
||||
@@ -440,10 +440,10 @@ end
|
||||
--- unit_length(4) + version(2) + unit_type(1) + address_size(1) + debug_abbrev_offset(4)
|
||||
--- followed by type_unit_specific fields: type_signature(8) + type_offset(4)
|
||||
--- The type_signature is at byte offset 8 of the body (right after debug_abbrev_offset).
|
||||
--- @param info string -- the .debug_info section bytes
|
||||
--- @param target_sig_lo integer -- low 4 bytes (LE) of the desired signature
|
||||
--- @param target_sig_hi integer -- high 4 bytes (LE) of the desired signature
|
||||
--- @return integer|nil, integer|nil -- unit offset, type_offset within the unit
|
||||
--- @param info string -- the .debug_info section bytes
|
||||
--- @param target_sig_lo integer -- low 4 bytes (LE) of the desired signature
|
||||
--- @param target_sig_hi integer -- high 4 bytes (LE) of the desired signature
|
||||
--- @return integer|nil, integer|nil -- unit offset, type_offset within the unit
|
||||
function M.find_type_unit_by_signature(info, target_sig_lo, target_sig_hi)
|
||||
local pos = 0
|
||||
local section_len = #info
|
||||
@@ -459,24 +459,23 @@ function M.find_type_unit_by_signature(info, target_sig_lo, target_sig_hi)
|
||||
return nil, nil -- malformed
|
||||
end
|
||||
-- Per DWARF5 §7.5.6, the type_unit (DW_UT_type = 0x02) body layout is:
|
||||
-- 0: version (2)
|
||||
-- 2: unit_type (1) -- DW_UT_type = 0x02
|
||||
-- 3: address_size (1)
|
||||
-- 4: debug_abbrev_offset (4)
|
||||
-- 8: type_signature (8)
|
||||
-- 16: type_offset (4)
|
||||
-- 20: <children>
|
||||
-- 0: version (2)
|
||||
-- 2: unit_type (1) -- DW_UT_type = 0x02
|
||||
-- 3: address_size (1)
|
||||
-- 4: debug_abbrev_offset (4)
|
||||
-- 8: type_signature (8)
|
||||
-- 16: type_offset (4)
|
||||
-- 20: <children>
|
||||
if body_end - body_start >= 20 then
|
||||
-- read_ref_sig8 / write_u32_le / etc. are 1-indexed (string:byte);
|
||||
-- pos / body_start / body_end are 0-based wire offsets, so the
|
||||
-- 1-indexed byte at 0-based wire offset X is string:byte(X + 1).
|
||||
-- pos / body_start / body_end are 0-based wire offsets, so the 1-indexed byte at 0-based wire offset X is string:byte(X + 1).
|
||||
-- Per DWARF5 §7.5.6, the type_unit body is laid out as:
|
||||
-- byte 0-1: version (2)
|
||||
-- byte 2: unit_type (1) -- DW_UT_type = 0x02
|
||||
-- byte 3: address_size (1)
|
||||
-- byte 4-7: debug_abbrev_offset (4)
|
||||
-- byte 8-15: type_signature (8)
|
||||
-- byte 16-19: type_offset (4)
|
||||
-- byte 0-1: version (2)
|
||||
-- byte 2: unit_type (1) -- DW_UT_type = 0x02
|
||||
-- byte 3: address_size (1)
|
||||
-- byte 4-7: debug_abbrev_offset (4)
|
||||
-- byte 8-15: type_signature (8)
|
||||
-- byte 16-19: type_offset (4)
|
||||
local unit_type = info:byte(body_start + 2 + 1) -- 0-based +2 = unit_type in 1-indexed
|
||||
if unit_type == 0x02 then -- DW_UT_type
|
||||
local sig_lo, sig_hi, _ = M.read_ref_sig8(info, body_start + 8) -- 0-based +8 = type_signature in 1-indexed
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
# scripts/launch_pcsx_debug.ps1
|
||||
#
|
||||
# One-shot launcher for debug sessions: starts pcsx-redux with the .ps-exe
|
||||
# loaded, the gdb stub enabled, AND the pcsx_debug_helper Lua plugin loaded
|
||||
# so external CLI tools (gdb's `shell` command, etc.)
|
||||
# can read GTE state via http://localhost:8080/api/v1/lua/gte
|
||||
# (the gdb stub doesn't expose COP2 at all).
|
||||
# One-shot launcher for debug sessions:
|
||||
# Starts pcsx-redux with the .ps-exe loaded, the gdb stub enabled,
|
||||
# AND the pcsx_debug_helper Lua plugin loaded so external CLI tools (gdb's `shell` command, etc.)
|
||||
# can read GTE state via http://localhost:8080/api/v1/lua/gte (the gdb stub doesn't expose COP2 at all).
|
||||
#
|
||||
# usage:
|
||||
# .\scripts\launch_pcsx_debug.ps1
|
||||
@@ -19,11 +18,11 @@
|
||||
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[string]$PcsxPath = (Join-Path $PSScriptRoot '..\toolchain\pcsx-redux\vsprojects\x64\Release\pcsx-redux.exe'),
|
||||
[string]$ExePath = (Join-Path $PSScriptRoot '..\build\hello_gte.ps-exe'),
|
||||
[string]$PcsxPath = (Join-Path $PSScriptRoot '..\toolchain\pcsx-redux\vsprojects\x64\Release\pcsx-redux.exe'),
|
||||
[string]$ExePath = (Join-Path $PSScriptRoot '..\build\hello_gte.ps-exe'),
|
||||
[string]$HelperZip = (Join-Path $PSScriptRoot 'pcsx_debug_helper.zip'),
|
||||
[int] $GdbPort = 3333,
|
||||
[int] $WebPort = 8080
|
||||
[int] $GdbPort = 3333,
|
||||
[int] $WebPort = 8080
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
@@ -84,7 +83,8 @@ try {
|
||||
$r = Invoke-WebRequest -Uri "http://localhost:$WebPort/api/v1/lua/gte" -UseBasicParsing -TimeoutSec 5
|
||||
$firstLine = ([System.Text.Encoding]::UTF8.GetString($r.Content) -split "`n")[0]
|
||||
Write-Host "GTE handler OK: $firstLine" -ForegroundColor Green
|
||||
} catch {
|
||||
}
|
||||
catch {
|
||||
Write-Warning "GTE handler NOT responding: $_"
|
||||
Write-Host "Check the pcsx-redux Lua Console for debug cli messages." -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
+249
-261
@@ -27,50 +27,34 @@
|
||||
local _bootstrap_dir = debug.getinfo(1, "S").source:match("^@?(.*[/\\])") or "./"
|
||||
local duffle = dofile(_bootstrap_dir .. "../duffle_paths.lua")
|
||||
|
||||
--- ════════════════════════════════════════════════════════════════════════════
|
||||
--- THE GPR ALLOCATION POOL — what is allocatable, and (more importantly) WHY
|
||||
--- ════════════════════════════════════════════════════════════════════════════
|
||||
---
|
||||
--- The auto-reg pass picks physical GPRs for `atom_auto_reg(...)` / `phase_auto_reg(...)` markers.
|
||||
--- It allocates from a FIXED 10-register pool.
|
||||
--- This comment block makes the inclusion AND exclusion criteria obvious so a reader doesn't have
|
||||
--- to grep lottes_tape.h + mips.h to understand the design.
|
||||
---
|
||||
--- ── WHAT'S IN THE POOL (10 GPRs, all caller-trash per the O32 ABI) ────────
|
||||
--- R_T0..R_T7 (GPR codes 8..15), R_V0..R_V1 (GPR codes 2..3)
|
||||
--- The workhorse of every atom body. The uesr should be aware of atom allocation across atoms they chain.
|
||||
--- If they have a collision it means either they didn't saturate the register file optimally for a phase,
|
||||
--- or the may have made the workload to large for the run.
|
||||
---
|
||||
--- ── WHAT'S NOT IN THE POOL — and WHY (the "obvious exclusions") ────────────
|
||||
--- R_T9 (GPR code 25) — R_TapePtr, the tape instruction stream pointer.
|
||||
--- Owned by the tape runtime (in tape_run / tape_run_a02_s07).
|
||||
--- `rgcc(R_TapePtr)` register-variable ties the C compiler's view to $t9 across the whole tape_run.
|
||||
--- The auto-reg pass MUST NOT clobber this; doing so would desync the C-side tape pointer from the
|
||||
--- hardware pointer and crash on the next tape_run.
|
||||
---
|
||||
--- R_T8 (GPR code 24) — R_AtomJmp, the atom-jump register used by the 4-word yield handshake.
|
||||
--- Every `mac_yield()` / `mac_yield_tail` does `load_word R_AtomJmp, R_TapePtr, 0` then
|
||||
--- `jump_reg R_AtomJmp`. The auto-reg pass MUST NOT clobber this either, or the atom dispatcher breaks.
|
||||
--- Owned by the tape runtime, same family as R_TapePtr.
|
||||
---
|
||||
--- R_AT (GPR code 1) — Assembler temporary. Reserved by the MIPS O32 ABI for pseudoinstruction expansion
|
||||
--- (lottes_tape.h:86, mips.h:93). The ISA's psuedo instructions use it as a scratch temporary.
|
||||
---
|
||||
--- R_A0..A3 (codes 4..7) — Function arguments. Used in tape_run_a02_s07, see below.
|
||||
--- R_S0..S7 (codes 16..23) — Callee-saved. Preserved across C-ABI calls by convention.
|
||||
--- The `tape_run_a02_s07` variant clobbers them deliberately, but the default `tape_run` does NOT.
|
||||
--- Kept out of POOL to preserve the conservative default.
|
||||
--- Add them in a separate "big clobber" pool if/when needed.
|
||||
---
|
||||
--- R_K0/K1 (codes 26..27) — Kernel / interrupt handler reserves. Never touched by user code; OS-internal.
|
||||
--- R_GP/SP/FP/RA (codes 28..31) — Stack frame + return-address. Owned by the C compiler; never allocatable.
|
||||
--- R_0 (code 0) — Hardwired zero. Cannot be written.
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
-- THE GPR ALLOCATION POOL — what's allocatable, and (more importantly) WHY
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
--
|
||||
-- The auto-reg pass picks physical GPRs for `atom_auto_reg(...)` / `phase_auto_reg(...)` markers.
|
||||
-- The 24-register pool covers R2-R25 (the user/atom allocatable surface):
|
||||
-- R_T0..R_T7, R_V0..R_V1, R_A0..A3, R_S0..S7, R_T8..T9.
|
||||
-- Excluded (and never added to the pool):
|
||||
-- R_0 (code 0) — hardwired zero. Cannot be written.
|
||||
-- R_AT (code 1) — assembler temporary. Reserved by the MIPS O32 ABI.
|
||||
-- R_A0..A3 — explicitly omitted above even though their integer codes
|
||||
-- map to POOL entries; the pool-construction loop below
|
||||
-- only references the POOL string literals, never the
|
||||
-- integer codes, so they are NOT auto-allocated by default.
|
||||
-- (A0-A3 become available when the user adds them to
|
||||
-- POOL or hardcodes an R_A0 reference in the atom body.)
|
||||
-- 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 = {
|
||||
"R_T0", "R_T1", "R_T2", "R_T3",
|
||||
"R_T4", "R_T5", "R_T6", "R_T7",
|
||||
"R_V0", "R_V1",
|
||||
"R_T0", "R_T1", "R_T2", "R_T3",
|
||||
"R_T4", "R_T5", "R_T6", "R_T7",
|
||||
"R_V0", "R_V1",
|
||||
"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",
|
||||
}
|
||||
|
||||
-- Map from integer MIPS GPR code (the `code` field on AliasEntry) to the physical GPR ident in POOL.
|
||||
@@ -79,44 +63,48 @@ local POOL = {
|
||||
-- (R_AT=1, R_A0..A3=4..7, R_T8=24, R_T9=25, R_K0/K1=26..27, R_GP/SP/FP/RA=28..31)
|
||||
-- are deliberately omitted — see the comment block above for the WHY of each exclusion.
|
||||
local INT_CODE_TO_POOL_GPR = {
|
||||
[2] = "R_V0", [3] = "R_V1",
|
||||
[8] = "R_T0", [9] = "R_T1", [10] = "R_T2", [11] = "R_T3",
|
||||
[12] = "R_T4", [13] = "R_T5", [14] = "R_T6", [15] = "R_T7",
|
||||
[2] = "R_V0", [3] = "R_V1",
|
||||
[4] = "R_A0", [5] = "R_A1", [6] = "R_A2", [7] = "R_A3",
|
||||
[8] = "R_T0", [9] = "R_T1", [10] = "R_T2", [11] = "R_T3",
|
||||
[12] = "R_T4", [13] = "R_T5", [14] = "R_T6", [15] = "R_T7",
|
||||
[16] = "R_S0", [17] = "R_S1", [18] = "R_S2", [19] = "R_S3",
|
||||
[20] = "R_S4", [21] = "R_S5", [22] = "R_S6", [23] = "R_S7",
|
||||
[24] = "R_T8", [25] = "R_T9",
|
||||
}
|
||||
|
||||
-- Stable sort for deterministic allocation order.
|
||||
local function stable_sort_keys(tbl)
|
||||
local keys = {}
|
||||
for k in pairs(tbl) do keys[#keys + 1] = k end
|
||||
table.sort(keys)
|
||||
return keys
|
||||
local keys = {}
|
||||
for k in pairs(tbl) do keys[#keys + 1] = k end
|
||||
table.sort(keys)
|
||||
return keys
|
||||
end
|
||||
|
||||
-- Allocate one phase's auto-reg mappings.
|
||||
-- Returns (allocated_map, errors). On pool exhaustion, errors is populated and the function halts.
|
||||
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),
|
||||
-- which corrupts the pool into `{ {R_T0, R_T1, ...} }` — making `table.remove(pool, 1)` return the inner table on iteration.
|
||||
local pool = {}
|
||||
for i = 1, #POOL do pool[i] = POOL[i] end
|
||||
local result = {}
|
||||
local errors = {}
|
||||
for _, sym in ipairs(stable_sort_keys(decls)) do
|
||||
local next_gpr = table.remove(pool, 1)
|
||||
if not next_gpr then
|
||||
errors[#errors + 1] = {
|
||||
line = 0,
|
||||
msg = string.format("phase_register_pool_exhausted: "
|
||||
.. "phase '%s' requested symbol '%s' but the pool has no remaining registers "
|
||||
.. "(max 10 per phase: R_T0..R_T7 + R_V0..R_V1). Split the phase or use hardcoded GPRs."
|
||||
, phase_label, sym),
|
||||
}
|
||||
return result, errors
|
||||
end
|
||||
result[sym] = next_gpr
|
||||
end
|
||||
return result, errors
|
||||
-- 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),
|
||||
-- which corrupts the pool into `{ {R_T0, R_T1, ...} }` — making `table.remove(pool, 1)` return the inner table on iteration.
|
||||
local pool = {}
|
||||
for i = 1, #POOL do pool[i] = POOL[i] end
|
||||
local result = {}
|
||||
local errors = {}
|
||||
for _, sym in ipairs(stable_sort_keys(decls)) do
|
||||
local next_gpr = table.remove(pool, 1)
|
||||
if not next_gpr then
|
||||
errors[#errors + 1] = {
|
||||
line = 0,
|
||||
msg = string.format("phase_register_pool_exhausted: "
|
||||
.. "phase '%s' requested symbol '%s' but the pool has no remaining registers "
|
||||
.. "(max 24 per phase: R_T0..R_T7 + R_V0..R_V1 + R_A0..R_A3 + R_S0..R_S7 + R_T8..R_T9). Split the phase or use hardcoded GPRs."
|
||||
, phase_label, sym),
|
||||
}
|
||||
return result, errors
|
||||
end
|
||||
result[sym] = next_gpr
|
||||
end
|
||||
return result, errors
|
||||
end
|
||||
|
||||
-- Build two projections from corpus.register_alias_registry:
|
||||
@@ -128,19 +116,19 @@ end
|
||||
-- Aliases whose `code` points to a non-POOL GPR (e.g. R_S0, R_T8, R_K1) are ignored —
|
||||
-- they don't affect the auto_reg pool, and they're already excluded from POOL above.
|
||||
local function build_user_pins(corpus)
|
||||
local user_pinned = {}
|
||||
local alias_to_gpr = {}
|
||||
if not corpus.register_alias_registry then return user_pinned, alias_to_gpr end
|
||||
for alias_name, alias_entry in pairs(corpus.register_alias_registry) do
|
||||
if alias_entry.has_atom_reg and alias_entry.code then
|
||||
local gpr = INT_CODE_TO_POOL_GPR[alias_entry.code]
|
||||
if gpr then
|
||||
user_pinned[gpr] = true
|
||||
alias_to_gpr[alias_name] = gpr
|
||||
end
|
||||
end
|
||||
end
|
||||
return user_pinned, alias_to_gpr
|
||||
local user_pinned = {}
|
||||
local alias_to_gpr = {}
|
||||
if not corpus.register_alias_registry then return user_pinned, alias_to_gpr end
|
||||
for alias_name, alias_entry in pairs(corpus.register_alias_registry) do
|
||||
if alias_entry.has_atom_reg and alias_entry.code then
|
||||
local gpr = INT_CODE_TO_POOL_GPR[alias_entry.code]
|
||||
if gpr then
|
||||
user_pinned[gpr] = true
|
||||
alias_to_gpr[alias_name] = gpr
|
||||
end
|
||||
end
|
||||
end
|
||||
return user_pinned, alias_to_gpr
|
||||
end
|
||||
|
||||
-- Find every physical GPR referenced in the atom body, via EITHER:
|
||||
@@ -151,57 +139,57 @@ end
|
||||
-- original find_hardcoded_rn shape so callers can switch without churn.
|
||||
-- The alias pattern is sorted lexicographically to keep the regex deterministic.
|
||||
local function find_used_gprs(body_text, alias_to_gpr)
|
||||
local found = {}
|
||||
-- (a) Hardcoded physical GPRs (R_T0..R_T7, R_V0..R_V1, R_A0..R_A3, R_S0..R_S7).
|
||||
for gpr in body_text:gmatch("(R_T%d+|R_V%d+|R_A%d+|R_S%d+)") do
|
||||
found[gpr] = (found[gpr] or 0) + 1
|
||||
end
|
||||
-- (b) Alias references (R_<Alias>) resolved to physical GPRs via the registry.
|
||||
-- Sorted by name so the regex is byte-stable across runs.
|
||||
if alias_to_gpr and next(alias_to_gpr) then
|
||||
local aliases = {}
|
||||
for alias_name in pairs(alias_to_gpr) do
|
||||
aliases[#aliases + 1] = alias_name
|
||||
end
|
||||
table.sort(aliases)
|
||||
local pattern = "(" .. table.concat(aliases, "|") .. ")"
|
||||
for alias_name in body_text:gmatch(pattern) do
|
||||
local gpr = alias_to_gpr[alias_name]
|
||||
if gpr and not found[gpr] then
|
||||
found[gpr] = 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return found
|
||||
local found = {}
|
||||
-- (a) Hardcoded physical GPRs (R_T0..R_T7, R_V0..R_V1, R_A0..R_A3, R_S0..R_S7).
|
||||
for gpr in body_text:gmatch("(R_T%d+|R_V%d+|R_A%d+|R_S%d+)") do
|
||||
found[gpr] = (found[gpr] or 0) + 1
|
||||
end
|
||||
-- (b) Alias references (R_<Alias>) resolved to physical GPRs via the registry.
|
||||
-- Sorted by name so the regex is byte-stable across runs.
|
||||
if alias_to_gpr and next(alias_to_gpr) then
|
||||
local aliases = {}
|
||||
for alias_name in pairs(alias_to_gpr) do
|
||||
aliases[#aliases + 1] = alias_name
|
||||
end
|
||||
table.sort(aliases)
|
||||
local pattern = "(" .. table.concat(aliases, "|") .. ")"
|
||||
for alias_name in body_text:gmatch(pattern) do
|
||||
local gpr = alias_to_gpr[alias_name]
|
||||
if gpr and not found[gpr] then
|
||||
found[gpr] = 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return found
|
||||
end
|
||||
|
||||
-- Emit one gen/auto_reg.h header per directory.
|
||||
local function emit_auto_reg_h(out_dir, dir, sources, mappings)
|
||||
if not mappings or next(mappings) == nil then return end
|
||||
local out_path = out_dir .. "/" .. "auto_reg.h"
|
||||
duffle.ensure_dir(out_dir)
|
||||
local lines = {
|
||||
"#ifdef INTELLISENSE_DIRECTIVES",
|
||||
"#pragma once",
|
||||
"#endif",
|
||||
"// Auto-generated by ps1_meta.lua (passes/auto_reg.lua) — DO NOT EDIT",
|
||||
"// Directory: " .. dir:gsub("/", "\\"),
|
||||
}
|
||||
for _, src in ipairs(sources) do
|
||||
lines[#lines + 1] = "// source: " .. src.path
|
||||
end
|
||||
lines[#lines + 1] = "// Per-phase register allocations resolved by the lua pass."
|
||||
lines[#lines + 1] = "// R_<Sym>_Code = <chosen GPR's _Code constant> for every marker in this directory."
|
||||
lines[#lines + 1] = ""
|
||||
for _, sym in ipairs(stable_sort_keys(mappings)) do
|
||||
local gpr = mappings[sym]
|
||||
local gpr_code = gpr .. "_Code"
|
||||
lines[#lines + 1] = "#define " .. sym .. "_Code " .. gpr_code
|
||||
end
|
||||
lines[#lines + 1] = ""
|
||||
duffle.write_file_lf(out_path, table.concat(lines, "\n") .. "\n")
|
||||
print(" -> " .. out_path)
|
||||
return out_path
|
||||
if not mappings or next(mappings) == nil then return end
|
||||
local out_path = out_dir .. "/" .. "auto_reg.h"
|
||||
duffle.ensure_dir(out_dir)
|
||||
local lines = {
|
||||
"#ifdef INTELLISENSE_DIRECTIVES",
|
||||
"#pragma once",
|
||||
"#endif",
|
||||
"// Auto-generated by ps1_meta.lua (passes/auto_reg.lua) — DO NOT EDIT",
|
||||
"// Directory: " .. dir:gsub("/", "\\"),
|
||||
}
|
||||
for _, src in ipairs(sources) do
|
||||
lines[#lines + 1] = "// source: " .. src.path
|
||||
end
|
||||
lines[#lines + 1] = "// Per-phase register allocations resolved by the lua pass."
|
||||
lines[#lines + 1] = "// R_<Sym>_Code = <chosen GPR's _Code constant> for every marker in this directory."
|
||||
lines[#lines + 1] = ""
|
||||
for _, sym in ipairs(stable_sort_keys(mappings)) do
|
||||
local gpr = mappings[sym]
|
||||
local gpr_code = gpr .. "_Code"
|
||||
lines[#lines + 1] = "#define " .. sym .. "_Code " .. gpr_code
|
||||
end
|
||||
lines[#lines + 1] = ""
|
||||
duffle.write_file_lf(out_path, table.concat(lines, "\n") .. "\n")
|
||||
print(" -> " .. out_path)
|
||||
return out_path
|
||||
end
|
||||
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
@@ -213,143 +201,143 @@ local M = {}
|
||||
--- @param ctx PassCtx
|
||||
--- @return AutoRegResult
|
||||
function M.run(ctx)
|
||||
local outputs = {}
|
||||
local errors = {}
|
||||
local warnings = {}
|
||||
local outputs = {}
|
||||
local errors = {}
|
||||
local warnings = {}
|
||||
|
||||
local corpus = ctx.shared and ctx.shared.corpus
|
||||
if type(corpus) ~= "table" then
|
||||
error("auto_reg.run requires ctx.shared.corpus", 0)
|
||||
end
|
||||
local corpus = ctx.shared and ctx.shared.corpus
|
||||
if type(corpus) ~= "table" then
|
||||
error("auto_reg.run requires ctx.shared.corpus", 0)
|
||||
end
|
||||
|
||||
-- 0. Build the user-pinned GPR exclusion set + alias-to-GPR resolution map.
|
||||
-- Wave-context carriers (e.g. `R_ResolveScratch = R_T4 atom_reg` in hello_camera.atom.c)
|
||||
-- MUST NOT be allocated to any auto-reg marker — they're preserved across atoms by the wave-context discipline.
|
||||
-- The corpus's register_alias_registry is the source of truth for these opt-in pins.
|
||||
-- Body references to those aliases (via alias_to_gpr) are also excluded on a per-atom basis in step 2 below.
|
||||
local user_pinned, alias_to_gpr = build_user_pins(corpus)
|
||||
-- 0. Build the user-pinned GPR exclusion set + alias-to-GPR resolution map.
|
||||
-- Wave-context carriers (e.g. `R_ResolveScratch = R_T4 atom_reg` in hello_camera.atom.c)
|
||||
-- MUST NOT be allocated to any auto-reg marker — they're preserved across atoms by the wave-context discipline.
|
||||
-- The corpus's register_alias_registry is the source of truth for these opt-in pins.
|
||||
-- Body references to those aliases (via alias_to_gpr) are also excluded on a per-atom basis in step 2 below.
|
||||
local user_pinned, alias_to_gpr = build_user_pins(corpus)
|
||||
|
||||
-- 1. Allocate phase pools first (phase declarations take precedence over per-atom declarations).
|
||||
local phase_allocations = {}
|
||||
for phase_label, decls in pairs(corpus.phase_auto_regs or {}) do
|
||||
local mapping, errs = allocate_phase(phase_label, decls)
|
||||
for sym, gpr in pairs(mapping) do
|
||||
phase_allocations[phase_label] = phase_allocations[phase_label] or {}
|
||||
phase_allocations[phase_label][sym] = gpr
|
||||
end
|
||||
for _, e in ipairs(errs) do
|
||||
errors[#errors + 1] = e
|
||||
end
|
||||
end
|
||||
-- 1. Allocate phase pools first (phase declarations take precedence over per-atom declarations).
|
||||
local phase_allocations = {}
|
||||
for phase_label, decls in pairs(corpus.phase_auto_regs or {}) do
|
||||
local mapping, errs = allocate_phase(phase_label, decls)
|
||||
for sym, gpr in pairs(mapping) do
|
||||
phase_allocations[phase_label] = phase_allocations[phase_label] or {}
|
||||
phase_allocations[phase_label][sym] = gpr
|
||||
end
|
||||
for _, e in ipairs(errs) do
|
||||
errors[#errors + 1] = e
|
||||
end
|
||||
end
|
||||
|
||||
-- 2. Allocate per-atom auto-regs. If the atom scope matches a phase, reuse the phase pool.
|
||||
-- Otherwise, allocate a private pool for the atom.
|
||||
-- The phase membership is in `corpus.atom_phases[phase_label].atoms` (an array of atom names declared via `atom_phase(<phase>)`
|
||||
-- in the atom's `atom_info` line). Build a reverse map `atom_name -> phase_label` so the lookup is O(1) per atom scope.
|
||||
local atom_name_to_phase = {}
|
||||
for phase_label, entry in pairs(corpus.atom_phases or {}) do
|
||||
for _, atom_name in ipairs(entry.atoms or {}) do
|
||||
atom_name_to_phase[atom_name] = phase_label
|
||||
end
|
||||
end
|
||||
-- 2. Allocate per-atom auto-regs. If the atom scope matches a phase, reuse the phase pool.
|
||||
-- Otherwise, allocate a private pool for the atom.
|
||||
-- The phase membership is in `corpus.atom_phases[phase_label].atoms` (an array of atom names declared via `atom_phase(<phase>)`
|
||||
-- in the atom's `atom_info` line). Build a reverse map `atom_name -> phase_label` so the lookup is O(1) per atom scope.
|
||||
local atom_name_to_phase = {}
|
||||
for phase_label, entry in pairs(corpus.atom_phases or {}) do
|
||||
for _, atom_name in ipairs(entry.atoms or {}) do
|
||||
atom_name_to_phase[atom_name] = phase_label
|
||||
end
|
||||
end
|
||||
|
||||
local atom_allocations = {}
|
||||
for atom_scope, decls in pairs(corpus.atom_auto_regs or {}) do
|
||||
local phase_label = atom_name_to_phase[atom_scope]
|
||||
-- Build the atom's source pool: start with the full POOL, subtract:
|
||||
-- (a) every GPR already committed (phase allocations + prior atom allocations)
|
||||
-- (b) every USER-PINNED GPR (wave-context carriers + file-scope pinned aliases)
|
||||
-- (c) every GPR referenced in the atom's body — either hardcoded R_X or alias R_Xxx
|
||||
-- (the latter resolved via alias_to_gpr; this catches cases where the user wrote R_ResolveScratch instead of R_T4 directly)
|
||||
-- Atoms whose scope matches a phase share the global pool with the phase allocations;
|
||||
-- the original `source_pool = phase_allocations[phase_label]` form used the phase
|
||||
-- allocation MAP as a pool, but that map has no array part, so `table.remove(source_pool, 1)`
|
||||
-- returned nil and every atom-with-phase marker errored with `phase_register_pool_exhausted`.
|
||||
local used = {}
|
||||
for _, m in pairs(phase_allocations) do for _, gpr in pairs(m) do used[gpr] = true end end
|
||||
for _, m in pairs(atom_allocations) do for _, gpr in pairs(m) do used[gpr] = true end end
|
||||
-- (c) Body references — scan the atom body for hardcoded + alias-resolved GPRs.
|
||||
-- Folded into `used` so the source_pool exclusion is a single check.
|
||||
local atom = corpus.atoms_by_name and corpus.atoms_by_name[atom_scope]
|
||||
if atom and atom.body then
|
||||
local body_used = find_used_gprs(atom.body, alias_to_gpr)
|
||||
for gpr in pairs(body_used) do used[gpr] = true end
|
||||
end
|
||||
local source_pool = {}
|
||||
for _, gpr in ipairs(POOL) do
|
||||
-- Exclude (a) prior commitments, (b) USER-PINNED GPRs (wave-context carriers
|
||||
-- declared via atom_reg + _Code defs, preserved across atoms globally).
|
||||
if not used[gpr] and not user_pinned[gpr] then
|
||||
source_pool[#source_pool + 1] = gpr
|
||||
end
|
||||
end
|
||||
local result = {}
|
||||
for _, sym in ipairs(stable_sort_keys(decls)) do
|
||||
local next_gpr = table.remove(source_pool, 1)
|
||||
if not next_gpr then
|
||||
errors[#errors + 1] = {
|
||||
line = 0,
|
||||
msg = string.format("phase_register_pool_exhausted: atom '%s' requested symbol '%s' "
|
||||
.. "but no free registers remain in its scope pool."
|
||||
, atom_scope, sym),
|
||||
}
|
||||
else
|
||||
result[sym] = next_gpr
|
||||
end
|
||||
end
|
||||
atom_allocations[atom_scope] = result
|
||||
end
|
||||
local atom_allocations = {}
|
||||
for atom_scope, decls in pairs(corpus.atom_auto_regs or {}) do
|
||||
local phase_label = atom_name_to_phase[atom_scope]
|
||||
-- Build the atom's source pool: start with the full POOL, subtract:
|
||||
-- (a) every GPR already committed (phase allocations + prior atom allocations)
|
||||
-- (b) every USER-PINNED GPR (wave-context carriers + file-scope pinned aliases)
|
||||
-- (c) every GPR referenced in the atom's body — either hardcoded R_X or alias R_Xxx
|
||||
-- (the latter resolved via alias_to_gpr; this catches cases where the user wrote R_ResolveScratch instead of R_T4 directly)
|
||||
-- Atoms whose scope matches a phase share the global pool with the phase allocations;
|
||||
-- the original `source_pool = phase_allocations[phase_label]` form used the phase
|
||||
-- allocation MAP as a pool, but that map has no array part, so `table.remove(source_pool, 1)`
|
||||
-- returned nil and every atom-with-phase marker errored with `phase_register_pool_exhausted`.
|
||||
local used = {}
|
||||
for _, m in pairs(phase_allocations) do for _, gpr in pairs(m) do used[gpr] = true end end
|
||||
for _, m in pairs(atom_allocations) do for _, gpr in pairs(m) do used[gpr] = true end end
|
||||
-- (c) Body references — scan the atom body for hardcoded + alias-resolved GPRs.
|
||||
-- Folded into `used` so the source_pool exclusion is a single check.
|
||||
local atom = corpus.atoms_by_name and corpus.atoms_by_name[atom_scope]
|
||||
if atom and atom.body then
|
||||
local body_used = find_used_gprs(atom.body, alias_to_gpr)
|
||||
for gpr in pairs(body_used) do used[gpr] = true end
|
||||
end
|
||||
local source_pool = {}
|
||||
for _, gpr in ipairs(POOL) do
|
||||
-- Exclude (a) prior commitments, (b) USER-PINNED GPRs (wave-context carriers
|
||||
-- declared via atom_reg + _Code defs, preserved across atoms globally).
|
||||
if not used[gpr] and not user_pinned[gpr] then
|
||||
source_pool[#source_pool + 1] = gpr
|
||||
end
|
||||
end
|
||||
local result = {}
|
||||
for _, sym in ipairs(stable_sort_keys(decls)) do
|
||||
local next_gpr = table.remove(source_pool, 1)
|
||||
if not next_gpr then
|
||||
errors[#errors + 1] = {
|
||||
line = 0,
|
||||
msg = string.format("phase_register_pool_exhausted: atom '%s' requested symbol '%s' "
|
||||
.. "but no free registers remain in its scope pool."
|
||||
, atom_scope, sym),
|
||||
}
|
||||
else
|
||||
result[sym] = next_gpr
|
||||
end
|
||||
end
|
||||
atom_allocations[atom_scope] = result
|
||||
end
|
||||
|
||||
-- 3. Conflict-with-hardcoded detection (defensive — should be unreachable now).
|
||||
-- The source_pool exclusion in step 2 (b) + (c) already accounts for both user-pinned GPRs
|
||||
-- and body-referenced GPRs (hardcoded R_Tn OR alias R_<Alias>).
|
||||
-- An auto-reg allocation that matched an existing body reference would be impossible by construction.
|
||||
-- This warning is kept as a defensive safety net for cases the body scanner might miss
|
||||
-- (e.g. macros that expand to register references the scanner cannot resolve).
|
||||
-- For each resolved (scope, sym) -> R_Tn mapping, scan the atom body source for used GPRs.
|
||||
for atom_scope, decls in pairs(atom_allocations) do
|
||||
local atom = corpus.atoms_by_name and corpus.atoms_by_name[atom_scope]
|
||||
if atom and atom.body then
|
||||
local used_in_body = find_used_gprs(atom.body, alias_to_gpr)
|
||||
for sym, allocated_gpr in pairs(decls) do
|
||||
if used_in_body[allocated_gpr] and used_in_body[allocated_gpr] > 0 then
|
||||
warnings[#warnings + 1] = {
|
||||
line = atom.line or 0,
|
||||
msg = string.format("phase_register_clash: atom '%s' has hardcoded '%s' in its body AND an auto-reg marker '%s' "
|
||||
.. "that was allocated to '%s' (same phase). Resolve by removing the hardcoded reference or renaming the auto-reg."
|
||||
, atom_scope, allocated_gpr, sym, allocated_gpr),
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
-- 3. Conflict-with-hardcoded detection (defensive — should be unreachable now).
|
||||
-- The source_pool exclusion in step 2 (b) + (c) already accounts for both user-pinned GPRs
|
||||
-- and body-referenced GPRs (hardcoded R_Tn OR alias R_<Alias>).
|
||||
-- An auto-reg allocation that matched an existing body reference would be impossible by construction.
|
||||
-- This warning is kept as a defensive safety net for cases the body scanner might miss
|
||||
-- (e.g. macros that expand to register references the scanner cannot resolve).
|
||||
-- For each resolved (scope, sym) -> R_Tn mapping, scan the atom body source for used GPRs.
|
||||
for atom_scope, decls in pairs(atom_allocations) do
|
||||
local atom = corpus.atoms_by_name and corpus.atoms_by_name[atom_scope]
|
||||
if atom and atom.body then
|
||||
local used_in_body = find_used_gprs(atom.body, alias_to_gpr)
|
||||
for sym, allocated_gpr in pairs(decls) do
|
||||
if used_in_body[allocated_gpr] and used_in_body[allocated_gpr] > 0 then
|
||||
warnings[#warnings + 1] = {
|
||||
line = atom.line or 0,
|
||||
msg = string.format("phase_register_clash: atom '%s' has hardcoded '%s' in its body AND an auto-reg marker '%s' "
|
||||
.. "that was allocated to '%s' (same phase). Resolve by removing the hardcoded reference or renaming the auto-reg."
|
||||
, atom_scope, allocated_gpr, sym, allocated_gpr),
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- 4. Emit per-directory gen/auto_reg.h.
|
||||
-- For each source directory that has atom_auto_regs or phase_auto_regs entries, emit one header.
|
||||
local sources_by_dir = corpus.sources_by_dir or {}
|
||||
for dir, sources in pairs(sources_by_dir) do
|
||||
local per_dir_mappings = {}
|
||||
for _, src in ipairs(sources) do
|
||||
-- Collect every (sym -> gpr) entry that originated from a source in this directory.
|
||||
-- `src.scan.atom_auto_regs` is keyed by ATOM SCOPE NAME; `pairs(t)` iterates KEYS so `scope_name` here is the scope ident (e.g. "cube_g4_face").
|
||||
-- The previous `for _, scan_atom_auto` form silently assigned the VALUE (a `{sym = sym}` table) to the variable,
|
||||
-- which made `atom_allocations[scan_atom_auto]` a table-indexed lookup that never resolved.
|
||||
for scope_name in pairs(src.scan and src.scan.atom_auto_regs or {}) do
|
||||
for sym, gpr in pairs(atom_allocations[scope_name] or {}) do
|
||||
per_dir_mappings[sym] = gpr
|
||||
end
|
||||
end
|
||||
for scope_name in pairs(src.scan and src.scan.phase_auto_regs or {}) do
|
||||
for sym, gpr in pairs(phase_allocations[scope_name] or {}) do
|
||||
per_dir_mappings[sym] = gpr
|
||||
end
|
||||
end
|
||||
end
|
||||
local out_dir = dir .. "/gen"
|
||||
local out_path = emit_auto_reg_h(out_dir, dir, sources, per_dir_mappings)
|
||||
if out_path then outputs[#outputs + 1] = { auto_reg_h = out_path } end
|
||||
end
|
||||
return { outputs = outputs, errors = errors, warnings = warnings }
|
||||
-- 4. Emit per-directory gen/auto_reg.h.
|
||||
-- For each source directory that has atom_auto_regs or phase_auto_regs entries, emit one header.
|
||||
local sources_by_dir = corpus.sources_by_dir or {}
|
||||
for dir, sources in pairs(sources_by_dir) do
|
||||
local per_dir_mappings = {}
|
||||
for _, src in ipairs(sources) do
|
||||
-- Collect every (sym -> gpr) entry that originated from a source in this directory.
|
||||
-- `src.scan.atom_auto_regs` is keyed by ATOM SCOPE NAME; `pairs(t)` iterates KEYS so `scope_name` here is the scope ident (e.g. "cube_g4_face").
|
||||
-- The previous `for _, scan_atom_auto` form silently assigned the VALUE (a `{sym = sym}` table) to the variable,
|
||||
-- which made `atom_allocations[scan_atom_auto]` a table-indexed lookup that never resolved.
|
||||
for scope_name in pairs(src.scan and src.scan.atom_auto_regs or {}) do
|
||||
for sym, gpr in pairs(atom_allocations[scope_name] or {}) do
|
||||
per_dir_mappings[sym] = gpr
|
||||
end
|
||||
end
|
||||
for scope_name in pairs(src.scan and src.scan.phase_auto_regs or {}) do
|
||||
for sym, gpr in pairs(phase_allocations[scope_name] or {}) do
|
||||
per_dir_mappings[sym] = gpr
|
||||
end
|
||||
end
|
||||
end
|
||||
local out_dir = dir .. "/gen"
|
||||
local out_path = emit_auto_reg_h(out_dir, dir, sources, per_dir_mappings)
|
||||
if out_path then outputs[#outputs + 1] = { auto_reg_h = out_path } end
|
||||
end
|
||||
return { outputs = outputs, errors = errors, warnings = warnings }
|
||||
end
|
||||
|
||||
return M
|
||||
|
||||
@@ -2465,8 +2465,8 @@ local function check_binds_no_substruct_deref(_src, pipe_ctx, findings)
|
||||
local body_line = a.line + (line_in_body[tokens[ti].rel] or 0)
|
||||
|
||||
local type_entry = resolve_type_with_fields(type_name, type_registry, 1)
|
||||
local no_fields = not type_entry or not type_entry.fields or #type_entry.fields == 0
|
||||
local raw_entry = type_registry[type_name]
|
||||
local no_fields = not type_entry or not type_entry.fields or #type_entry.fields == 0
|
||||
local raw_entry = type_registry[type_name]
|
||||
local is_typedef_to_struct = raw_entry
|
||||
and raw_entry.kind == "typedef"
|
||||
and raw_entry.underlying_type
|
||||
@@ -2506,13 +2506,11 @@ local function check_binds_no_substruct_deref(_src, pipe_ctx, findings)
|
||||
end
|
||||
|
||||
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
-- GTE control-register alias + RT-diagonal + TR-naming helpers and checks
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
--- Resolve a `gte_cr_<Alias>` ident to its alias-group entry, or nil if the alias
|
||||
--- is in a distinct-slot group (or the alias name is not a known C2 control-register alias).
|
||||
--- Resolve a `gte_cr_<Alias>` ident to its alias-group entry, or nil if the alias is in a distinct-slot group (or the alias name is not a known C2 control-register alias).
|
||||
--- Reads `M.GTE_CR_ALIAS_GROUPS` from `duffle.lua`.
|
||||
local function find_alias_pair_for(alias_name, duffle)
|
||||
local groups = (duffle and duffle.GTE_CR_ALIAS_GROUPS) or {}
|
||||
@@ -2531,11 +2529,9 @@ local function is_ctrl_r_transfer(c)
|
||||
return c.ident == "gte_mv_to_ctrl_r" or c.ident == "gte_mv_from_ctrl_r"
|
||||
end
|
||||
|
||||
-- Resolve a token's source line. The per-token `line` is the body-relative
|
||||
-- line; `atom.line` is the source line of the atom declaration; `line_in_body`
|
||||
-- (atom.paths) maps a body-relative line to its source line. The arithmetic
|
||||
-- `atom.line + line_in_body[tok.rel] - 1` matches the convention used by
|
||||
-- check_abi_handoff and check_control_transfer_delay_slot_use elsewhere.
|
||||
-- Resolve a token's source line.
|
||||
-- The per-token `line` is the body-relative line; `atom.line` is the source line of the atom declaration; `line_in_body` (atom.paths) maps a body-relative line to its source line.
|
||||
-- The arithmetic `atom.line + line_in_body[tok.rel] - 1` matches the convention used by check_abi_handoff and check_control_transfer_delay_slot_use elsewhere.
|
||||
local function atom_body_token_source_line(atom, token, line_in_body)
|
||||
if line_in_body == nil or token == nil or token.rel == nil then
|
||||
return atom.line or 0
|
||||
@@ -2584,14 +2580,11 @@ local function ctrl_writes_in_atom(atom)
|
||||
end
|
||||
|
||||
-- Check #N: gte_cr_alias_writes
|
||||
-- Fires one warning per atom per alias-group when the atom body touches two
|
||||
-- distinct aliases from the same group. Aliases within a group write to the
|
||||
-- same C2 control-register slot on real silicon; cross-alias writes inside
|
||||
-- one atom body silently clobber each other.
|
||||
-- Fires one warning per atom per alias-group when the atom body touches two distinct aliases from the same group.
|
||||
-- Aliases within a group write to the same C2 control-register slot on real silicon; cross-alias writes inside one atom body silently clobber each other.
|
||||
--
|
||||
-- Severity: warning. Build continues. The libgte outer-product convention
|
||||
-- uses only RT-row aliases (which are NOT in `M.GTE_CR_ALIAS_GROUPS`), so
|
||||
-- the canonical convention does not trigger this check.
|
||||
-- Severity: warning. Build continues.
|
||||
-- The libgte outer-product convention uses only RT-row aliases (which are NOT in `M.GTE_CR_ALIAS_GROUPS`), so the canonical convention does not trigger this check.
|
||||
local function check_gte_cr_alias_writes(atom, pipe_ctx, findings)
|
||||
local groups = pipe_ctx.gte_cr_alias_groups or {}
|
||||
if not next(groups) then return end
|
||||
@@ -2650,14 +2643,11 @@ end
|
||||
|
||||
-- Check #N+1: rtdiagonal_completeness
|
||||
-- Fires one info per atom body when the bare `gte_cmdw_mvmva` macro is used.
|
||||
-- The bare macro encodes only the cmd field; the canonical libgte-2-pass
|
||||
-- shape uses `gte_cmdw_mvmva_c11_pass2_exact = 0x4A49E012` (gte.h:430).
|
||||
-- The bare macro encodes only the cmd field; the canonical libgte-2-pass shape uses `gte_cmdw_mvmva_c11_pass2_exact = 0x4A49E012` (gte.h:430).
|
||||
--
|
||||
-- Severity: info by default. Escalates to warning when
|
||||
-- `GTE_RT_DIAGONAL_STRICT=1` env var is set (CI / production builds).
|
||||
-- Severity: info by default. Escalates to warning when `GTE_RT_DIAGONAL_STRICT=1` env var is set (CI / production builds).
|
||||
--
|
||||
-- The bare macro IS the right call for the canonical libgte outer-product
|
||||
-- convention, so this is an opt-out hint rather than a hard warning.
|
||||
-- The bare macro IS the right call for the canonical libgte outer-product convention, so this is an opt-out hint rather than a hard warning.
|
||||
local function check_rtdiagonal_completeness(atom, _pipe_ctx, findings)
|
||||
local tokens = atom.paths and atom.paths.tokens or {}
|
||||
local tc = atom.paths and atom.paths.tok_class or {}
|
||||
|
||||
+14
-25
@@ -91,11 +91,9 @@ if (-not (Test-Path -LiteralPath $path_pcsx_packages)) {
|
||||
New-Item -ItemType Directory -Path $path_pcsx_packages -Force | Out-Null
|
||||
}
|
||||
|
||||
# Download anything missing. Skip the package entirely if its dir already has
|
||||
# any contents (the legacy packages.config style means the targets file
|
||||
# location varies per package — `luajit.native` puts it at build/native/,
|
||||
# `glfw` puts it elsewhere — so we can't probe a specific path; just check
|
||||
# whether the dir is non-empty).
|
||||
# Download anything missing.
|
||||
# Skip the package entirely if its dir already has any contents (the legacy packages.config style means the targets file location varies per package
|
||||
# — `luajit.native` puts it at build/native/, `glfw` puts it elsewhere — so we can't probe a specific path; just check whether the dir is non-empty).
|
||||
Add-Type -AssemblyName System.IO.Compression.FileSystem
|
||||
foreach ($pkg in $required_packages.Values) {
|
||||
$pkgDir = Join-Path $path_pcsx_packages ('{0}.{1}' -f $pkg.id, $pkg.version)
|
||||
@@ -122,24 +120,18 @@ foreach ($pkg in $required_packages.Values) {
|
||||
}
|
||||
}
|
||||
|
||||
# ════════════════════════════════════════════════════════════════════════════
|
||||
# isoffi.lua size guard — `core.vcxproj` #includes src/core/isoffi.lua into
|
||||
# luaiso.cc via the `-- lualoader, R"EOF(...)EOF"` trick. The raw string
|
||||
# literal between R"EOF(-- and -- )EOF" must stay under ~16,379 bytes or
|
||||
# MSVC (19.44) fails with C2026 (its actual raw-string limit is 16,384,
|
||||
# minus 5 bytes for the `-- lualoader, ` prefix). If the upstream file
|
||||
# grows past that, trim it: remove license header, trailing whitespace,
|
||||
# blank separators, inline comments, and shrink 4-space indent to 2-space.
|
||||
# ════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
|
||||
# isoffi.lua size guard — `core.vcxproj` #includes src/core/isoffi.lua into luaiso.cc via the `-- lualoader, R"EOF(...)EOF"` trick.
|
||||
# The raw string literal between R"EOF(-- and -- )EOF" must stay under ~16,379 bytes or MSVC (19.44) fails with C2026 (its actual raw-string limit is 16,384, minus 5 bytes for the `-- lualoader, ` prefix).
|
||||
# If the upstream file grows past that, trim it: remove license header, trailing whitespace, blank separators, inline comments, and shrink 4-space indent to 2-space.
|
||||
# Idempotent — only writes when the raw string exceeds the limit.
|
||||
# ════════════════════════════════════════════════════════════════════════════
|
||||
# ════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
|
||||
$path_isoffi = join-path $path_pcsx_redux 'src\core\isoffi.lua'
|
||||
if (Test-Path -LiteralPath $path_isoffi) {
|
||||
$content = Get-Content -LiteralPath $path_isoffi -Raw -Encoding utf8
|
||||
$startMarker = $content.IndexOf('R"EOF(--')
|
||||
$endMarker = $content.IndexOf('-- )EOF"')
|
||||
$literalLen = if ($startMarker -ge 0 -and $endMarker -gt $startMarker) {
|
||||
$endMarker - ($startMarker + 8)
|
||||
} else { -1 }
|
||||
$literalLen = if ($startMarker -ge 0 -and $endMarker -gt $startMarker) { $endMarker - ($startMarker + 8) } else { -1 }
|
||||
# Effective MSVC raw-string limit for the lualoader prefix is 16379 bytes.
|
||||
if ($literalLen -gt 16379) {
|
||||
Write-Host "isoffi.lua raw string is $literalLen bytes (>16379); trimming for MSVC C2026 limit."
|
||||
@@ -168,8 +160,7 @@ if (Test-Path -LiteralPath $path_isoffi) {
|
||||
$newLines += $line
|
||||
}
|
||||
($newLines -join "`n") | Out-File -LiteralPath $path_isoffi -Encoding utf8 -NoNewline
|
||||
$newLen = ((Get-Content -LiteralPath $path_isoffi -Raw -Encoding utf8) `
|
||||
-replace '.*R"EOF\(--', '' -replace '-- \)EOF".*', '').Length
|
||||
$newLen = ((Get-Content -LiteralPath $path_isoffi -Raw -Encoding utf8) -replace '.*R"EOF\(--', '' -replace '-- \)EOF".*', '').Length
|
||||
Write-Host "isoffi.lua trimmed: $literalLen -> $newLen bytes of raw string content."
|
||||
}
|
||||
}
|
||||
@@ -231,13 +222,11 @@ $lfs_dll_import = join-path $luajit_lib_dir 'libluajit-5.1.dll.a'
|
||||
|
||||
$path_openbios = join-path $path_pcsx_redux 'src\mips\openbios'
|
||||
|
||||
# Wipe stale *.dep files across src\mips. These cache absolute paths to the
|
||||
# GCC headers directory; if the toolchain was upgraded (e.g. v14.2.0 → v16.1.0)
|
||||
# Wipe stale *.dep files across src\mips.
|
||||
# These cache absolute paths to the GCC headers directory; if the toolchain was upgraded (e.g. v14.2.0 → v16.1.0)
|
||||
# Make reads the stale paths and aborts with "no rule to make target .../stddef.h".
|
||||
# `make clean` in openbios only clears its own dir — subdirs like
|
||||
# common/crt0/, modplayer/, and shell/ keep their stale .dep files. Easier to
|
||||
# just delete the lot before each build than to teach every Makefile about
|
||||
# deepclean recursion.
|
||||
# `make clean` in openbios only clears its own dir — subdirs like common/crt0/, modplayer/, and shell/ keep their stale .dep files.
|
||||
# Easier to just delete the lot before each build than to teach every Makefile about deepclean recursion.
|
||||
Get-ChildItem -Path (join-path $path_pcsx_redux 'src\mips') -Recurse -Filter '*.dep' -ErrorAction SilentlyContinue |
|
||||
ForEach-Object { Remove-Item -LiteralPath $_.FullName -Force }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user