Open aw opened 2 years ago
I've thought a macro system for quite awhile. It'd definitely be a nice feature to have! The main limitation are semantics and implementation: how exactly does it work and how do I accomplish it given the existing architecture?
The primitive expression evaluation in the assembler defers to python's builtin eval function. Maybe python could be further utilized for macros somehow? A macro could be written as a simply python function that takes a few args and generates assembler Items? But I'd like macros to resemble regular assembler syntax, not python. Part of me has always been curious about writing an S-expression based assembler...
I do want to implement some sort of simple macro system. Do you have any thoughts / ideas on what the syntax could look like for defining and using macros?
My experience with Assembly macros is limited, but I think a basic syntax similar to this would be a good start:
macro mycode addr size
li t0, %addr
lw a0, 0(t0)
addi a0, a0, %size
endmacro
In GNU as
, they use .macro
and .endm
and variables are prefixed with slash: \var
.
I guess what you need is a way to detect a start/end keyword for the macro, and a way to identify variables within the macro itself. Once that code is pre-processed into actual Assembly, then you can just append it where the macro is used:
mycode 0x2000, 16
would end up appending:
li t0, 0x2000
lw a0, 0(t0)
addi a0, a0, 16
... something like that 🤣
Would it be possible to use separate, stand-alone, preprocessor as a make step before assembly? There are plenty available although I haven't checked which ones preserve lines and indentation properly. Even if finding something suitable is difficult, it should be a relatively straight-forward task to write one if it doesn't have to integrate with the internals of bronzebeard.
That's possible! Do you know of any high-quality, python-based preprocessors? Pulling something in off the shelf could be a nice path to success. I could write something simple myself but I'd like to avoid reinventing the wheel if possible.
Using a non-python, standalone preprocessor sounds less ideal since it'd require an implicit dependency on external programs: something that bronzebeard tries to avoid.
I'd like to avoid reinventing the wheel if possible.
Says the person who wrote their own assembler and forth interpreter :roll_eyes: but I get the point. I'll have a look around, too.
Hah, yeah good point. I suppose it's more about being picky about wheels? Either way, if there is solid option that exists, I'd consider integration.
Hi @theandrew168 ,
Would it be possible to add support for writing basic macros in the assembly programs? I wanted to see how
derzforth
would change in size if I inlined all thej next
instructions.For the moment my hack in
derzforth.asm
is to replace<4 spaces>j next
withinclude next.asm
, which contains:The binary size increased by exactly 200 bytes, which I think is worth it for the performance gain.
Either way, I think it would be interesting to have a real macro syntax or something similar to what
GAS
andNASM
provide... doesn't have to be as complex/powerful though. What do you think?