tmemit.nim 627 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. discard """
  2. output: '''
  3. c_func
  4. 12
  5. '''
  6. """
  7. import macros, strutils
  8. # bug #1025
  9. macro foo(icname): untyped =
  10. let ic = newStrLitNode($icname)
  11. result = quote do:
  12. proc x* =
  13. proc private {.exportc: `ic`.} = discard
  14. echo `ic`
  15. private()
  16. foo(c_func)
  17. x()
  18. template volatileLoad[T](x: ptr T): T =
  19. var res: T
  20. {.emit: [res, " = (*(", type(x[]), " volatile*)", x, ");"].}
  21. res
  22. template volatileStore[T](x: ptr T; y: T) =
  23. {.emit: ["*((", type(x[]), " volatile*)(", x, ")) = ", y, ";"].}
  24. proc main =
  25. var st: int
  26. var foo: ptr int = addr st
  27. volatileStore(foo, 12)
  28. echo volatileLoad(foo)
  29. main()