summary refs log tree commit diff stats
path: root/compiler/vmhooks.nim
blob: 39e435e4bff4c863337fdb0053d1d191efedef01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #66
#
#
#           The Nim Compiler
#        (c) Copyright 2015 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

import pathutils

template setX(k, field) {.dirty.} =
  var s: seq[TFullReg]
  move(s, cast[seq[TFullReg]](a.slots))
  if s[a.ra].kind != k:
    myreset(s[a.ra])
    s[a.ra].kind = k
  s[a.ra].field = v

proc setResult*(a: VmArgs; v: BiggestInt) = setX(rkInt, intVal)
proc setResult*(a: VmArgs; v: BiggestFloat) = setX(rkFloat, floatVal)
proc setResult*(a: VmArgs; v: bool) =
  let v = v.ord
  setX(rkInt, intVal)

proc setResult*(a: VmArgs; v: string) =
  var s: seq[TFullReg]
  move(s, cast[seq[TFullReg]](a.slots))
  if s[a.ra].kind != rkNode:
    myreset(s[a.ra])
    s[a.ra].kind = rkNode
  s[a.ra].node = newNode(nkStrLit)
  s[a.ra].node.strVal = v

proc setResult*(a: VmArgs; n: PNode) =
  var s: seq[TFullReg]
  move(s, cast[seq[TFullReg]](a.slots))
  if s[a.ra].kind != rkNode:
    myreset(s[a.ra])
    s[a.ra].kind = rkNode
  s[a.ra].node = n

proc setResult*(a: VmArgs; v: AbsoluteDir) = setResult(a, v.string)

proc setResult*(a: VmArgs; v: seq[string]) =
  var s: seq[TFullReg]
  move(s, cast[seq[TFullReg]](a.slots))
  if s[a.ra].kind != rkNode:
    myreset(s[a.ra])
    s[a.ra].kind = rkNode
  var n = newNode(nkBracket)
  for x in v: n.add newStrNode(nkStrLit, x)
  s[a.ra].node = n

template getX(k, field) {.dirty.} =
  doAssert i < a.rc-1
  let s = cast[seq[TFullReg]](a.slots)
  doAssert s[i+a.rb+1].kind == k
  result = s[i+a.rb+1].field

proc getInt*(a: VmArgs; i: Natural): BiggestInt = getX(rkInt, intVal)
proc getBool*(a: VmArgs; i: Natural): bool = getInt(a, i) != 0
proc getFloat*(a: VmArgs; i: Natural): BiggestFloat = getX(rkFloat, floatVal)
proc getString*(a: VmArgs; i: Natural): string =
  doAssert i < a.rc-1
  let s = cast[seq[TFullReg]](a.slots)
  doAssert s[i+a.rb+1].kind == rkNode
  result = s[i+a.rb+1].node.strVal

proc getNode*(a: VmArgs; i: Natural): PNode =
  doAssert i < a.rc-1
  let s = cast[seq[TFullReg]](a.slots)
  doAssert s[i+a.rb+1].kind == rkNode
  result = s[i+a.rb+1].node