volatile.nim 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2017 Jeff Ciesielski
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module contains code for generating volatile loads and stores,
  10. ## which are useful in embedded and systems programming.
  11. template volatileLoad*[T](src: ptr T): T =
  12. ## Generates a volatile load of the value stored in the container `src`.
  13. ## Note that this only effects code generation on `C` like backends.
  14. when nimvm:
  15. src[]
  16. else:
  17. when defined(js):
  18. src[]
  19. else:
  20. var res: T
  21. {.emit: [res, " = (*(", typeof(src[]), " volatile*)", src, ");"].}
  22. res
  23. template volatileStore*[T](dest: ptr T, val: T) =
  24. ## Generates a volatile store into the container `dest` of the value
  25. ## `val`. Note that this only effects code generation on `C` like
  26. ## backends.
  27. when nimvm:
  28. dest[] = val
  29. else:
  30. when defined(js):
  31. dest[] = val
  32. else:
  33. {.emit: ["*((", typeof(dest[]), " volatile*)(", dest, ")) = ", val, ";"].}