gbdev / gbdev/rgbds

[Feature request] Output the assembly code after macro expansion

Open
#1,895 3 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

I am looking through some gameboy projects where some pretty big macros are being used. these macros use conditional statements to be expanded and i would really like to see a feature where this can be expanded to the actual resulting code.

I am suggesting an addition to the `-s` flag for `rgbasm` called `expand` which expands the macros in the file provided.
Another addition that would come with this would be `expand_comment` where the original line is added as an extra comment before the expanded statement

these examples from the docs would be expanded to this
```
DEF NUM EQU 10
MACRO lb
ld \1, (\2) << 8 | (\3)
ENDM
lb hl, 20, 18 ; Expands to "ld hl, ((20) << 8) | (18)"
lb de, 3 + 1, NUM**2 ; Expands to "ld de, ((3 + 1) << 8) | (NUM**2)"

; rgbasm -s expand:macro_test_0.dump.asm macro_test.asm
ld hl, ((20) << 8) | (18)
ld de, ((3 + 1) << 8) | (10**2)

; rgbasm -s expand_comment:macro_test_1.dump.asm macro_test.asm
; lb hl, 20, 18 ; Expands to "ld hl, ((20) << 8) | (18)"
ld hl, ((20) << 8) | (18)
; lb de, 3 + 1, NUM**2 ; Expands to "ld de, ((3 + 1) << 8) | (NUM**2)"
ld de, ((3 + 1) << 8) | (10**2)
```
```
DEF NUM EQU 10
IF NUM < 0
PRINTLN "NUM < 0"
ELIF NUM == 0
PRINTLN "NUM == 0"
ELSE
PRINTLN "NUM > 0"
ENDC

; rgbasm -s expand:if_test_0.dump.asm if_test.asm
PRINTLN "NUM > 0"

; rgbasm -s expand_comment:if_test_1.dump.asm if_test.asm
; PRINTLN "NUM > 0"
PRINTLN "NUM > 0"
```
```
INCLUDE "macro_test.asm"
INCLUDE "if_test.asm"

; do note the double expansion, first the INCLUDE, then the actual expansion of that file

; rgbasm -s expand:include_test_0.dump.asm include_test.asm
ld hl, ((20) << 8) | (18)
ld de, ((3 + 1) << 8) | (10**2)
PRINTLN "NUM > 0"

; rgbasm -s expand_comment:include_test_1.dump.asm include_test.asm
; lb hl, 20, 18 ; Expands to "ld hl, ((20) << 8) | (18)"
ld hl, ((20) << 8) | (18)
; lb de, 3 + 1, NUM**2 ; Expands to "ld de, ((3 + 1) << 8) | (NUM**2)"
ld de, ((3 + 1) << 8) | (10**2)
; PRINTLN "NUM > 0"
PRINTLN "NUM > 0"
```

the resulting files should be able to be passed to `rgbasm` in a way that it can still be correctly parsed.

this feature would be similar to `gcc -E`, `clang -E` or `msvc /P`

Contributor guide

Open the contributing guide

Research direction

Start at rgbasm's -s flag and compare the requested expand and expand_comment output with the macro, conditional, and INCLUDE examples in the issue. Done means the generated files remain parseable by rgbasm and match the shown expansion behavior, including nested expansion and preserved comments.

Written by the indexing model from the issue text.

Assessment

Domain
cli, compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.