volatile.nim 1.1 KB

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