template+emit supports volatileRead and volatileWrite ops
This commit is contained in:
parent
93068f1ba6
commit
d651012688
3 changed files with 26 additions and 4 deletions
|
|
@ -973,6 +973,8 @@ proc genAsmOrEmitStmt(p: BProc, t: PNode, isAsmStmt=false): Rope =
|
||||||
r = mangleName(p.module, sym)
|
r = mangleName(p.module, sym)
|
||||||
sym.loc.r = r # but be consequent!
|
sym.loc.r = r # but be consequent!
|
||||||
res.add($r)
|
res.add($r)
|
||||||
|
of nkTypeOfExpr:
|
||||||
|
res.add($getTypeDesc(p.module, t.sons[i].typ))
|
||||||
else:
|
else:
|
||||||
var a: TLoc
|
var a: TLoc
|
||||||
initLocExpr(p, t.sons[i], a)
|
initLocExpr(p, t.sons[i], a)
|
||||||
|
|
|
||||||
|
|
@ -445,7 +445,9 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode =
|
||||||
of nkPostfix:
|
of nkPostfix:
|
||||||
result.sons[1] = semTemplBody(c, n.sons[1])
|
result.sons[1] = semTemplBody(c, n.sons[1])
|
||||||
of nkPragma:
|
of nkPragma:
|
||||||
result = onlyReplaceParams(c, n)
|
for x in n:
|
||||||
|
if x.kind == nkExprColonExpr:
|
||||||
|
x.sons[1] = semTemplBody(c, x.sons[1])
|
||||||
of nkBracketExpr:
|
of nkBracketExpr:
|
||||||
result = newNodeI(nkCall, n.info)
|
result = newNodeI(nkCall, n.info)
|
||||||
result.add newIdentNode(getIdent("[]"), n.info)
|
result.add newIdentNode(getIdent("[]"), n.info)
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,16 @@
|
||||||
discard """
|
discard """
|
||||||
output: '''HELLO WORLD
|
output: '''HELLO WORLD
|
||||||
c_func'''
|
c_func
|
||||||
|
12'''
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import macros, strutils
|
import macros, strutils
|
||||||
|
|
||||||
emit("echo " & '"' & "hello world".toUpper & '"')
|
emit("echo " & '"' & "hello world".toUpperAscii & '"')
|
||||||
|
|
||||||
# bug #1025
|
# bug #1025
|
||||||
|
|
||||||
macro foo(icname): stmt =
|
macro foo(icname): untyped =
|
||||||
let ic = newStrLitNode($icname)
|
let ic = newStrLitNode($icname)
|
||||||
result = quote do:
|
result = quote do:
|
||||||
proc x* =
|
proc x* =
|
||||||
|
|
@ -19,3 +20,20 @@ macro foo(icname): stmt =
|
||||||
|
|
||||||
foo(c_func)
|
foo(c_func)
|
||||||
x()
|
x()
|
||||||
|
|
||||||
|
|
||||||
|
template volatileLoad[T](x: ptr T): T =
|
||||||
|
var res: T
|
||||||
|
{.emit: [res, " = (*(", type(x[]), " volatile*)", x, ");"].}
|
||||||
|
res
|
||||||
|
|
||||||
|
template volatileStore[T](x: ptr T; y: T) =
|
||||||
|
{.emit: ["*((", type(x[]), " volatile*)(", x, ")) = ", y, ";"].}
|
||||||
|
|
||||||
|
proc main =
|
||||||
|
var st: int
|
||||||
|
var foo: ptr int = addr st
|
||||||
|
volatileStore(foo, 12)
|
||||||
|
echo volatileLoad(foo)
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue