From d65101268835b4dcda054bdd0687f2527b65eee0 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Mon, 23 Jan 2017 11:22:14 +0100 Subject: template+emit supports volatileRead and volatileWrite ops --- compiler/ccgstmts.nim | 2 ++ compiler/semtempl.nim | 4 +++- tests/macros/tmemit.nim | 24 +++++++++++++++++++++--- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index 123d627b5..b3d21c35e 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -973,6 +973,8 @@ proc genAsmOrEmitStmt(p: BProc, t: PNode, isAsmStmt=false): Rope = r = mangleName(p.module, sym) sym.loc.r = r # but be consequent! res.add($r) + of nkTypeOfExpr: + res.add($getTypeDesc(p.module, t.sons[i].typ)) else: var a: TLoc initLocExpr(p, t.sons[i], a) diff --git a/compiler/semtempl.nim b/compiler/semtempl.nim index e016e689c..b63928807 100644 --- a/compiler/semtempl.nim +++ b/compiler/semtempl.nim @@ -445,7 +445,9 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode = of nkPostfix: result.sons[1] = semTemplBody(c, n.sons[1]) of nkPragma: - result = onlyReplaceParams(c, n) + for x in n: + if x.kind == nkExprColonExpr: + x.sons[1] = semTemplBody(c, x.sons[1]) of nkBracketExpr: result = newNodeI(nkCall, n.info) result.add newIdentNode(getIdent("[]"), n.info) diff --git a/tests/macros/tmemit.nim b/tests/macros/tmemit.nim index e5aed3172..3a3b8734b 100644 --- a/tests/macros/tmemit.nim +++ b/tests/macros/tmemit.nim @@ -1,15 +1,16 @@ discard """ output: '''HELLO WORLD -c_func''' +c_func +12''' """ import macros, strutils -emit("echo " & '"' & "hello world".toUpper & '"') +emit("echo " & '"' & "hello world".toUpperAscii & '"') # bug #1025 -macro foo(icname): stmt = +macro foo(icname): untyped = let ic = newStrLitNode($icname) result = quote do: proc x* = @@ -19,3 +20,20 @@ macro foo(icname): stmt = foo(c_func) 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() -- cgit 1.4.1-2-gfad0 ue='search'/>
path: root/tests/overload/toverl4.nim
blob: 5379256747ac795dda76b77cf0a72bd3ce84ded9 (plain) (tree)