New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace __LINE__ and __FILE__ with something that handles inclusions and repetitions #1120
Comments
Doing If you want to use real filenames in a macro, you could instead put |
In the case of "just writing your own line identifier every time", you'd need some sort of preprocessor to change the literal 840 every time a line is added or deleted above line 840 so that the value remains correct. |
Rather than un-deprecating these, let’s add new versions that act the way we want? Specifically a line constant that tracks includes and repetitions |
I have to admit this is probably a better course of action. And it would remove the need for weird workarounds. |
For functions, a simple "unique name" is, by necessity, the function's: SECTION "MyFunction", ROM0 ; (Aside: using this pattern for ROM0 functions is a good idea, but ROMX is not.)
MyFunction::
; snip
ret For those debug messages, and for a unique name across the whole program, $ bat a.asm b.asm common.inc
───────┬───────────────────────────────────────────────────
│ File: a.asm
───────┼───────────────────────────────────────────────────
1 │ INCLUDE "common.inc"
───────┴───────────────────────────────────────────────────
───────┬───────────────────────────────────────────────────
│ File: b.asm
───────┼───────────────────────────────────────────────────
1 │ INCLUDE "common.inc"
───────┴───────────────────────────────────────────────────
───────┬───────────────────────────────────────────────────
│ File: common.inc
───────┼───────────────────────────────────────────────────
1 │ MACRO make_section
2 │ def anon_file\@ equs __FILE__
3 │ SECTION "Anonymous section {anon_file\@}", \#
4 │ purge anon_file\@
5 │ ENDM
6 │
7 │ make_section ROM0
───────┴───────────────────────────────────────────────────
$ rgbasm a.asm -o a.o && rgbasm b.asm -o b.o && rgblink a.o b.o -m /dev/stdout
warning: a.asm(1) -> common.inc(7) -> common.inc::make_section(2): [-Wobsolete]
`__FILE__` is deprecated
warning: b.asm(1) -> common.inc(7) -> common.inc::make_section(2): [-Wobsolete]
`__FILE__` is deprecated
error: Section name "Anonymous section common.inc" is already in use What would be useful would be the output file name... and even then, it's iffy. (The root source file name may not be enough, for example if I run |
These preprocessor symbols are a lot more useful than #1068 and #1072 would have you believe. Here are a few examples*:
* Tested with RGBDS 0.5.2, so they might not work exactly the same in other versions. Also note that many of the macros here require that a symbol named
DEBUG
be defined (otherwise they function as no-ops).Anonymous Sections
With anonymous sections, you can create a section without having to give it a name. It's a lot more convenient than having to maintain unique section names. This is especially true if you like to put each function into its own section so that the linker can rearrange things more effectively.
This macro will create a section with an auto-generated name. The name is generated using
__FILE__
, among other preprocessor symbols.Example usage:
This results in the creation a section with a name something like
__anonymousSection_engine/entity.z80_u38
.Also note the required indentation before
makeSection
(it is a macro, after all).Debug Logging
Emulators such as BGB allow you to log messages to the emulator's internal debug console. This is done by using a special instruction/data sequence. Here are some macros for outputting to BGB's debug console. It makes use of anonymous sections so that the actual string data gets stored elsewhere in a non-fixed bank.
Example usage:
Ouput:
This is a debug message.
Error Handling
Here, anonymous sections and
debug_writeLine
are used together with__FILE__
and__LINE__
to print an error message to the debug console and then hang the program. Another variant ofdebug_fail
exists with a condition code argument, but I feel it's a little too verbose to include here.Example usage:
Output:
Error at "Engine/Map.z80" line 681: Oops! Something went wrong.
Note that the line number preprocessor symbol must be written out at the call site as shown above. Otherwise the line number reported by the error message may not be correct. This is the one caveat of using this method.
... anyway, that's all for now.
The text was updated successfully, but these errors were encountered: