volatile.nim 963 B

12345678910111213141516171819202122232425262728293031
  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 defined(js):
  15. src[]
  16. else:
  17. var res: T
  18. {.emit: [res, " = (*(", type(src[]), " volatile*)", src, ");"].}
  19. res
  20. template volatileStore*[T](dest: ptr T, val: T) =
  21. ## Generates a volatile store into the container `dest` of the value
  22. ## `val`. Note that this only effects code generation on `C` like
  23. ## backends
  24. when defined(js):
  25. dest[] = val
  26. else:
  27. {.emit: ["*((", type(dest[]), " volatile*)(", dest, ")) = ", val, ";"].}