vmhooks.nim 2.0 KB

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