vmhooks.nim 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. template setX(k, field) {.dirty.} =
  10. var s: seq[TFullReg]
  11. move(s, cast[seq[TFullReg]](a.slots))
  12. if s[a.ra].kind != k:
  13. myreset(s[a.ra])
  14. s[a.ra].kind = k
  15. s[a.ra].field = v
  16. proc setResult*(a: VmArgs; v: BiggestInt) = setX(rkInt, intVal)
  17. proc setResult*(a: VmArgs; v: BiggestFloat) = setX(rkFloat, floatVal)
  18. proc setResult*(a: VmArgs; v: bool) =
  19. let v = v.ord
  20. setX(rkInt, intVal)
  21. proc setResult*(a: VmArgs; v: string) =
  22. var s: seq[TFullReg]
  23. move(s, cast[seq[TFullReg]](a.slots))
  24. if s[a.ra].kind != rkNode:
  25. myreset(s[a.ra])
  26. s[a.ra].kind = rkNode
  27. s[a.ra].node = newNode(nkStrLit)
  28. s[a.ra].node.strVal = v
  29. proc setResult*(a: VmArgs; n: PNode) =
  30. var s: seq[TFullReg]
  31. move(s, cast[seq[TFullReg]](a.slots))
  32. if s[a.ra].kind != rkNode:
  33. myreset(s[a.ra])
  34. s[a.ra].kind = rkNode
  35. s[a.ra].node = n
  36. proc setResult*(a: VmArgs; v: seq[string]) =
  37. var s: seq[TFullReg]
  38. move(s, cast[seq[TFullReg]](a.slots))
  39. if s[a.ra].kind != rkNode:
  40. myreset(s[a.ra])
  41. s[a.ra].kind = rkNode
  42. var n = newNode(nkBracket)
  43. for x in v: n.add newStrNode(nkStrLit, x)
  44. s[a.ra].node = n
  45. template getX(k, field) {.dirty.} =
  46. doAssert i < a.rc-1
  47. let s = cast[seq[TFullReg]](a.slots)
  48. doAssert s[i+a.rb+1].kind == k
  49. result = s[i+a.rb+1].field
  50. proc getInt*(a: VmArgs; i: Natural): BiggestInt = getX(rkInt, intVal)
  51. proc getBool*(a: VmArgs; i: Natural): bool = getInt(a, i) != 0
  52. proc getFloat*(a: VmArgs; i: Natural): BiggestFloat = getX(rkFloat, floatVal)
  53. proc getString*(a: VmArgs; i: Natural): string =
  54. doAssert i < a.rc-1
  55. let s = cast[seq[TFullReg]](a.slots)
  56. doAssert s[i+a.rb+1].kind == rkNode
  57. result = s[i+a.rb+1].node.strVal
  58. proc getNode*(a: VmArgs; i: Natural): PNode =
  59. doAssert i < a.rc-1
  60. let s = cast[seq[TFullReg]](a.slots)
  61. doAssert s[i+a.rb+1].kind == rkNode
  62. result = s[i+a.rb+1].node