BartmanAbyss / vscode-amiga-debug

One-stop Visual Studio Code Extension to compile, debug and profile Amiga C/C++ programs compiled by the bundled gcc 12.2 with the bundled WinUAE/FS-UAE.
GNU General Public License v3.0
314 stars 39 forks source link

Small improvement for INCBIN: report included file's size #77

Closed ggnkua closed 2 years ago

ggnkua commented 2 years ago

Hi,

So as the subject says, I wanted to know the sizes of the various files I include using the INCBIN or INCBIN_CHIP macros. Here's my solution, it's not perfect but at least it works for me! Perhaps if you find it useful you can improve my ignorant hacks!

#define INCBIN(name, file) INCBIN_SECTION(name, file, ".rodata", "")
#define INCBIN_CHIP(name, file) INCBIN_SECTION(name, file, ".INCBIN.MEMF_CHIP", "aw")
#define INCBIN_SECTION(name, file, section, flags) \
    __asm__(".pushsection " #section ", " #flags "\n" \
            ".global incbin_" #name "_start\n" \
            ".type incbin_" #name "_start, @object\n" \
            ".balign 2\n" \
            "incbin_" #name "_start:\n" \
            ".incbin \"" file "\"\n" \
            \
            ".global incbin_" #name "_end\n" \
            ".type incbin_" #name "_end, @object\n" \
            ".size incbin_" #name "_start, incbin_" #name "_end - incbin_" #name "_start\n" \
            ".balign 1\n" \
            "incbin_" #name "_end:\n" \
            ".global incbin_" #name "_size\n" \
            ".type incbin_" #name "_size, @object\n" \
            ".equ incbin_" #name "_size, incbin_" #name "_end - incbin_" #name "_start\n" \
            ".byte 0\n" \
            ".popsection\n" \
    ); \
    extern const __attribute__((aligned(2))) char incbin_ ## name ## _start[1024*1024]; \
    extern const void* incbin_ ## name ## _end;\
    ULONG *incbin_ ## name ## _size; \
    const void* name = &incbin_ ## name ## _start;

So what does this get us? Well, incbin_name_size seems to have the correct value, but no matter what type I would try, make it a pointer or not, it would assume it's a pointer. This was the only compromise I could find, and then using it in the code is also bizarre. For example:

sfx_struct_click.sfx_len        = (ULONG)&incbin_sfx_click_size / 2;

I.e. I take the pointer, take its address, then cast it to an integer and then I have the value! (I think if I wrote ULONG *incbin_ ## name ## _size; then I couldn't even do that, it still assumed it's a pointer).

Anyway, IMO it's a worthy addition to the macros but I'm at the moment unsure how to make this more proper than the above version!

BartmanAbyss commented 2 years ago

This works fine:

INCBIN(songData,"edrum.regdump") 
KPrintF("%ld", (unsigned)&incbin_songData_end - (unsigned)&incbin_songData_start);

You can easily turn this into a macro.