volatile.nim 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  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. result = default(T)
  21. {.emit: [result, " = (*(", typeof(src[]), " volatile*)", src, ");"].}
  22. template volatileStore*[T](dest: ptr T, val: T) =
  23. ## Generates a volatile store into the container `dest` of the value
  24. ## `val`. Note that this only effects code generation on `C` like
  25. ## backends.
  26. when nimvm:
  27. dest[] = val
  28. else:
  29. when defined(js):
  30. dest[] = val
  31. else:
  32. {.emit: ["*((", typeof(dest[]), " volatile*)(", dest, ")) = ", val, ";"].}