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. import pathutils
  10. when defined(nimPreviewSlimSystem):
  11. import std/assertions
  12. template setX(k, field) {.dirty.} =
  13. a.slots[a.ra].ensureKind(k)
  14. a.slots[a.ra].field = v
  15. proc setResult*(a: VmArgs; v: BiggestInt) = setX(rkInt, intVal)
  16. proc setResult*(a: VmArgs; v: BiggestFloat) = setX(rkFloat, floatVal)
  17. proc setResult*(a: VmArgs; v: bool) =
  18. let v = v.ord
  19. setX(rkInt, intVal)
  20. proc setResult*(a: VmArgs; v: string) =
  21. a.slots[a.ra].ensureKind(rkNode)
  22. a.slots[a.ra].node = newNode(nkStrLit)
  23. a.slots[a.ra].node.strVal = v
  24. proc setResult*(a: VmArgs; n: PNode) =
  25. a.slots[a.ra].ensureKind(rkNode)
  26. a.slots[a.ra].node = n
  27. proc setResult*(a: VmArgs; v: AbsoluteDir) = setResult(a, v.string)
  28. proc setResult*(a: VmArgs; v: seq[string]) =
  29. a.slots[a.ra].ensureKind(rkNode)
  30. var n = newNode(nkBracket)
  31. for x in v: n.add newStrNode(nkStrLit, x)
  32. a.slots[a.ra].node = n
  33. template getReg(a, i): untyped =
  34. doAssert i < a.rc-1
  35. a.slots[i+a.rb+1].unsafeAddr
  36. template getX(k, field): untyped {.dirty.} =
  37. let p = getReg(a, i)
  38. doAssert p.kind == k, $p.kind
  39. p.field
  40. proc numArgs*(a: VmArgs): int =
  41. result = a.rc-1
  42. proc getInt*(a: VmArgs; i: Natural): BiggestInt = getX(rkInt, intVal)
  43. proc getBool*(a: VmArgs; i: Natural): bool = getInt(a, i) != 0
  44. proc getFloat*(a: VmArgs; i: Natural): BiggestFloat = getX(rkFloat, floatVal)
  45. proc getNode*(a: VmArgs; i: Natural): PNode = getX(rkNode, node)
  46. proc getString*(a: VmArgs; i: Natural): string = getX(rkNode, node).strVal
  47. proc getVar*(a: VmArgs; i: Natural): PNode =
  48. let p = getReg(a, i)
  49. # depending on whether we come from top-level or proc scope, we need to consider 2 cases
  50. case p.kind
  51. of rkRegisterAddr: result = p.regAddr.node
  52. of rkNodeAddr: result = p.nodeAddr[]
  53. else: doAssert false, $p.kind
  54. proc getNodeAddr*(a: VmArgs; i: Natural): PNode =
  55. let nodeAddr = getX(rkNodeAddr, nodeAddr)
  56. doAssert nodeAddr != nil
  57. result = nodeAddr[]