gbdev / gbdev/rgbds

[Feature request] Link-time conditionals for identically sized branches

Open
#822 9 comments 0 reactions 0 assignees View on GitHub
enhancement rgbasm
Dominant language
C++
Stars
1.6k
Forks
193
Avg merge
22h 17m
Merged PRs (30d)
26

Description

Generating code with macros, like [pokecrystal16](https://github.com/aaaaaa123456789/pokecrystal16/blob/expand-move-ID/engine/16/macros.asm), can require different optimizations depending on where labels end up being placed. (Note the various `db`s in that file.) However, `if` needs to be evaluated at assembly time, and an eventual ternary operator would only work within `db`s, requiring people to encode instructions as raw bytes. (For instance, `db (HIGH(\1EntriesEnd) & 1) ? $28 : $20, .search_loop - (@ + 1)`, would eventually work, but `(HIGH(\1EntriesEnd) & 1) ? : `, or `jr <(HIGH(\1EntriesEnd) & 1) ? nz : z>, .search_loop`, cannot.)

A solution could be link-time conditionals. These would fail to assemble if all their branches are not already the same size, or if one branch defines a label but another does not. (Two branches defining a label in different relative locations should be okay, I think, but if it isn't that would be fine as a limitation too.)

Example syntax (chosen in contrast with `static_assert`):

```
dynamic_if HIGH(\1EntriesEnd) & 1
jr nz, .search_loop
else
jr z, .search_loop
endc
```

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the link-time conditional proposal and the example macro in pokecrystal16, then trace the assembler and linker entry points that would need to preserve both branches. Define how equal branch sizes, labels, and the dynamic_if/else/endc syntax should be validated before implementation; the issue does not name specific repository files or tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.