Skip to content

Transpiler: support complex array index expressions #225

@jonathanpeppers

Description

@jonathanpeppers

Follow-up from PR #211 (crypto.c sample port).

The transpiler currently only supports simple array index expressions where the index is a local variable or constant. Complex expressions like:

collision_top_left[(x >> 3) + ((y >> 3) << 4)]

fail with: Array element access requires the index to be a local variable or constant. Complex index expressions are not supported.

This pattern is common in NES games for tile map lookups. The C code equivalent is straightforward array indexing that the 6502 handles with indexed addressing modes.

Workaround

Compute the index into a temp variable first:

byte idx = (byte)((x >> 3) + ((y >> 3) << 4));
byte result = collision_top_left[idx];

Implementation

The HandleLdelemU1 method in IL2NESWriter.ArrayHandling.cs needs to handle more complex IL patterns before the ldelem.u1 instruction, such as shift/add/sub operations on the index.

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions