Using nim 10.2 on my linux desktop and Macbook pro laptop asm code in templates fails. Here, https://github.com/winksaville/nim-asm-in-template, is a git repo with a Makefile.
The following code succeeds as x1 is a proc and outputs:
wink@desktop:~/prgs/nim/asm-in-template$ nim c -r test_asm.nim config/nim.cfg(45, 2) Hint: added path: '/home/wink/.babel/pkgs/' [Path] config/nim.cfg(46, 2) Hint: added path: '/home/wink/.nimble/pkgs/nimble-0.6.0' [Path] config/nim.cfg(46, 2) Hint: added path: '/home/wink/.nimble/pkgs/' [Path] Hint: used config file '/opt/nim/config/nim.cfg' [Conf] Hint: system [Processing] Hint: test_asm [Processing] CC: test_asm [Linking] Hint: operation successful (8765 lines compiled; 0.255 sec total; 14.148MB) [SuccessX] /home/wink/prgs/nim/asm-in-template/test_asm hi cycles=3659218470919
# This works when x1 is defined as a proc
proc x1(name: string) =
var hi, lo: uint32
asm """
rdtsc
:"=a"(`lo`), "=d"(`hi`)
"""
var cycles = int64(lo) or (int64(hi) shl 32)
echo name, " cycles=", cycles
when isMainModule:
x1("ok")
Where as changing x1 to a template:
template x1(name: string) =
var hi, lo: uint32
asm """
rdtsc
:"=a"(`lo`), "=d"(`hi`)
"""
var cycles = int64(lo) or (int64(hi) shl 32)
echo name, " cycles=", cycles
when isMainModule:
x1("ok")
Fails to compile because asm code is generated at the top level:
wink@desktop:~/prgs/nim/asm-in-template$ nim c --parallelBuild:1 test_asm.nim config/nim.cfg(45, 2) Hint: added path: '/home/wink/.babel/pkgs/' [Path] config/nim.cfg(46, 2) Hint: added path: '/home/wink/.nimble/pkgs/nimble-0.6.0' [Path] config/nim.cfg(46, 2) Hint: added path: '/home/wink/.nimble/pkgs/' [Path] Hint: used config file '/opt/nim/config/nim.cfg' [Conf] Hint: system [Processing] Hint: test_asm [Processing] test_asm.nim(1, 12) Warning: undeclared conditional symbol; use --symbol to declare it: use_proc [User] /home/wink/prgs/nim/asm-in-template/nimcache/test_asm.c:22:7: error: expected ‘)’ before ‘:’ token :"=a"(lo), "=d"(hi) ^ Error: execution of an external program failed