kevinpt / opbasm

Open PicoBlaze Assembler
http://kevinpt.github.io/opbasm/
MIT License
60 stars 13 forks source link

defining constants with evalh/d fails #16

Closed nrother closed 7 years ago

nrother commented 7 years ago

The following code (from the manual) fails

constant cname, evalh(20 + 6)

Error message:

m4:stdin:25: ERROR: end of file in argument list

The same happens when using evald instead of evalh. Using eval works, but produces the wrong result, obviously.

m4 (GNU M4) 1.4.17
OPBASM version 1.3.3
kevinpt commented 7 years ago

You should use the built-in m4 eval() macro in this situation. You can tell it to generate zero-padded hex output with additional parameters:

constant cname, eval(20+6, 16, 2)

evalh() is really just a convenience macro that does the same internally as well as masking the result to guarantee it won't exceed two digits:

define(`evalh', `eval((const2m4($1)) & 0xFF, 16, 2) ; $1')

The reason why evalh() and related don't work is that constants are converted into a call to the hidden const() macro which exposes Picoblaze level constant values to the m4 processor. evalh() appends a comment after the generated value showing the original expression to make assembled listings easier to interpret. That prevents it from being nested within other macros because m4 chokes on the comment when it is passed into const(). This needs to be documented since it isn't obvious that another macro is being invoked for the constant definition.

nrother commented 7 years ago

Ah, that's tricky. Thanks for documenting this!

I'm not sure if m4 supports this, but in this situation support for block comments would be great. This way you could embed the comment as intended, but without eating up the rest of the line.

kevinpt commented 7 years ago

You can configure m4 for block comments but not at the same time as an inline comment. It may be possible to craft the const() macro to strip off a comment in its value argument.