vmhooks.nim 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. proc setResult*(a: VmArgs; v: (BiggestInt, BiggestInt)) =
  34. a.slots[a.ra].ensureKind(rkNode)
  35. var tuplen = newNode(nkTupleConstr)
  36. tuplen.add newIntNode(nkIntLit, v[0])
  37. tuplen.add newIntNode(nkIntLit, v[1])
  38. a.slots[a.ra].node = tuplen
  39. template getReg(a, i): untyped =
  40. doAssert i < a.rc-1
  41. a.slots[i+a.rb+1].unsafeAddr
  42. template getX(k, field): untyped {.dirty.} =
  43. let p = getReg(a, i)
  44. doAssert p.kind == k, $p.kind
  45. p.field
  46. proc numArgs*(a: VmArgs): int =
  47. result = a.rc-1
  48. proc getInt*(a: VmArgs; i: Natural): BiggestInt = getX(rkInt, intVal)
  49. proc getBool*(a: VmArgs; i: Natural): bool = getInt(a, i) != 0
  50. proc getFloat*(a: VmArgs; i: Natural): BiggestFloat = getX(rkFloat, floatVal)
  51. proc getNode*(a: VmArgs; i: Natural): PNode = getX(rkNode, node)
  52. proc getString*(a: VmArgs; i: Natural): string = getX(rkNode, node).strVal
  53. proc getVar*(a: VmArgs; i: Natural): PNode =
  54. let p = getReg(a, i)
  55. # depending on whether we come from top-level or proc scope, we need to consider 2 cases
  56. case p.kind
  57. of rkRegisterAddr: result = p.regAddr.node
  58. of rkNodeAddr: result = p.nodeAddr[]
  59. else: raiseAssert $p.kind
  60. proc getNodeAddr*(a: VmArgs; i: Natural): PNode =
  61. let nodeAddr = getX(rkNodeAddr, nodeAddr)
  62. doAssert nodeAddr != nil
  63. result = nodeAddr[]