Hello everyone,
Been playing around with the ESP8266 microcontroller lately and a couple days ago I decided to give Nim a try since it looks awesome and seems to be such a good match for embedded programming. I got it all mostly working (it's running Nim code right now!) however I have a couple questions:
2. in embedded programming it is often important to select in which linker section functions get placed, and there doesn't seem to be a convenient way to do this in Nim, the only way I found was with codegenDecl by doing e.g. this:
codegenDecl: "$# __attribute__((section(\".irom0.text\"))) $#$#"
which is atrociously verbose (and GCC/Clang only I suppose). Is there a better way to do this, and if not, is there any chance of a section: "mysection" pragma coming to Nim? From what I see it seems a lot of the pragmas are meant as a 'bag of tricks' to help with various specific scenarios so this wouldn't be out of place I would say and seems (to me) essential for a systems language.
I will probably have more questions in the future but these will be for a later thread. I look forward to your replies and to working with Nim.
Thanks
I think you can use the pragma pragma to simplify this case. Someone can probably improve my answer and create a pragma which takes an argument for the section instead of hard-coding it.
{.pragma:irom, codegenDecl: "$# $# __attribute__((section (\".rom0.text\")))".}
var a {.irom.} = 2
My $#'s may be in the wrong spots.
Figured out how to add a parameter if anyone is interested, it doesn't use a pragma pragma (couldn't figure out how to do it with a pragma pragma) but a macro:
macro section(param: string, body: untyped): stmt =
let sectionName = strVal(if param.kind == nnkSym: param.symbol.getImpl() else: param)
let codegenAttribute = "$# __attribute__((section(\"" & sectionName & "\"))) $#$#"
let pragma = body.findChild(it.kind == nnkPragma)
pragma.add(newNimNode(nnkExprColonExpr).add(newIdentNode("codegenDecl"),
newStrLitNode(codegenAttribute)))
return body
This macro supports either a literal string:
{. section: ".irom0.text" .}
or a constant containing a string:
const ROM = ".irom0.text"
# ...
{. section: ROM .}
I suppose there is a way to generalise it to make it work with any compile-time expression that evaluates to a string too :)