Skip to content

Conversation

@NiceAndPeter
Copy link
Owner

No description provided.

Changed FuncState operator method signatures to accept enum types:
- prefix(UnOpr op, ...) - was prefix(int op, ...)
- infix(BinOpr op, ...) - was infix(int op, ...)
- posfix(BinOpr op, ...) - was posfix(int op, ...)

Impact:
- Eliminated 6 redundant static_cast operations (enum→int→enum roundtrip)
- Improved type safety - prevents passing invalid operator values
- Self-documenting function signatures
- Zero performance impact - enums compile to same underlying type

Files changed:
- src/compiler/lparser.h (function signatures)
- src/compiler/lcode.cpp (implementations)
- src/compiler/parser.cpp (call sites - removed casts)

Performance: 4.49s avg (baseline 4.20s, variance 4.01-4.95s)
All tests passing: "final OK !!!"
Moved luaP_opmodes array access into InstructionView methods:
- Added getOpMode() method - returns OpMode enum
- Added testAMode() method - tests if instruction sets register A
- Added testTMode() method - tests if instruction is a test/jump
- Added testITMode() method - tests if instruction uses top from previous
- Added testOTMode() method - tests if instruction sets top for next
- Added testMMMode() method - tests if instruction is metamethod call

Impact:
- Better encapsulation - opcode properties accessed through InstructionView
- Cleaner code - view.testTMode() instead of testTMode(view.opcode())
- Eliminated redundant InstructionView creations at call sites
- Zero-cost abstraction - all methods are inline

Files changed:
- src/compiler/lopcodes.h (method declarations and implementations)
- src/compiler/lopcodes.cpp (luaP_isIT, luaP_isOT - use view methods)
- src/compiler/lcode.cpp (getjumpcontrol, negatecondition - use view methods)
- src/core/ldebug.cpp (findsetreg - use view methods, eliminated duplicate views)

Performance: 4.33s avg (baseline 4.20s, exactly at target!)
All tests passing: "final OK !!!"
Converted 7 internal predicate functions from int to bool:

Compiler predicates (lcode.cpp):
- isKint() - checks if expression is literal integer
- isCint() - checks if integer fits in register C
- isSCint() - checks if integer fits in register sC
- isSCnumber() - checks if number fits in register with output params
- validop() - validates if constant folding operation is safe

Test-only predicates (ltests.cpp):
- testobjref1() - tests GC object reference invariants
- testobjref() - wrapper that prints failed invariants

Impact:
- Clearer intent - bool instead of int (0/1) for predicates
- Better type safety - prevents arithmetic on boolean results
- Self-documenting code - function signatures show boolean nature
- Zero functional change - same compiled code

All return statements updated: 0 → false, 1 → true

Performance: 4.73s avg (baseline 4.20s, within variance)
All tests passing: "final OK !!!"
Created TYPE_MODERNIZATION_ANALYSIS.md documenting:
- Completed work: Phases 112-113 (operator type safety, InstructionView
  encapsulation, boolean returns)
- Static cast analysis: ~170 instances across 25+ files categorized
- Int overuse analysis: 600+ instances identified and assessed
- Remaining opportunities: Boolean returns (8), loop counters (400),
  size variables (30), status codes (25), token types (20)
- Detailed risk/benefit analysis for each category
- Recommendations: Complete boolean returns OR stop here

Key findings:
- Most remaining opportunities have high risk or low value
- Lua's int-based API design is intentional (C compatibility)
- Loop counter conversion not recommended (massive scope, minimal benefit)
- Size variable conversion not recommended (underflow risk)
- Current design is pragmatic for Lua's problem domain

Document provides roadmap for future type system modernization work.
@NiceAndPeter NiceAndPeter merged commit cb5a5a0 into main Nov 21, 2025
18 of 19 checks passed
@NiceAndPeter NiceAndPeter deleted the claude/refactor-static-cast-01Mrj8MFXifWmTNwn6dYYCwF branch November 21, 2025 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants