summary refs log tree commit diff stats
path: root/compiler/optimizer.nim
blob: 34e8ec80f4ec334335cd459c1555bba00c82b3ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#
#
#           The Nim Compiler
#        (c) Copyright 2020 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## Optimizer:
## - elide 'wasMoved(x); destroy(x)' pairs
## - recognize "all paths lead to 'wasMoved(x)'"

import
  ast, renderer, idents

from trees import exprStructuralEquivalent

import std/[strutils, intsets]

const
  nfMarkForDeletion = nfNone # faster than a lookup table

type
  BasicBlock = object
    wasMovedLocs: seq[PNode]
    kind: TNodeKind
    hasReturn, hasBreak: bool
    label: PSym # can be nil
    parent: ptr BasicBlock

  Con = object
    somethingTodo: bool
    inFinally: int

proc nestedBlock(parent: var BasicBlock; kind: TNodeKind): BasicBlock =
  BasicBlock(wasMovedLocs: @[], kind: kind, hasReturn: false, hasBreak: false,
    label: nil, parent: addr(parent))

proc breakStmt(b: var BasicBlock; n: PNode) =
  var it = addr(b)
  while it != nil:
    it.wasMovedLocs.setLen 0
    it.hasBreak = true

    if n.kind == nkSym:
      if it.label == n.sym: break
    else:
      # unnamed break leaves the block is nkWhileStmt or the like:
      if it.kind in {nkWhileStmt, nkBlockStmt, nkBlockExpr}: break

    it = it.parent

proc returnStmt(b: var BasicBlock) =
  b.hasReturn = true
  var it = addr(b)
  while it != nil:
    it.wasMovedLocs.setLen 0
    it = it.parent

proc mergeBasicBlockInfo(parent: var BasicBlock; this: BasicBlock) {.inline.} =
  if this.hasReturn:
    parent.wasMovedLocs.setLen 0
    parent.hasReturn = true

proc wasMovedTarget(matches: var IntSet; branch: seq[PNode]; moveTarget: PNode): bool =
  result = false
  for i in 0..<branch.len:
    if exprStructuralEquivalent(branch[i][1].skipHiddenAddr, moveTarget,
                                strictSymEquality = true):
      result = true
      matches.incl i

proc intersect(summary: var seq[PNode]; branch: seq[PNode]) =
  # keep all 'wasMoved(x)' calls in summary that are also in 'branch':
  var i = 0
  var matches = initIntSet()
  while i < summary.len:
    if wasMovedTarget(matches, branch, summary[i][1].skipHiddenAddr):
      inc i
    else:
      summary.del i
  for m in matches:
    summary.add branch[m]


proc invalidateWasMoved(c: var BasicBlock; x: PNode) =
  var i = 0
  while i < c.wasMovedLocs.len:
    if exprStructuralEquivalent(c.wasMovedLocs[i][1].skipHiddenAddr, x,
                                strictSymEquality = true):
      c.wasMovedLocs.del i
    else:
      inc i

proc wasMovedDestroyPair(c: var Con; b: var BasicBlock; d: PNode) =
  var i = 0
  while i < b.wasMovedLocs.len:
    if exprStructuralEquivalent(b.wasMovedLocs[i][1].skipHiddenAddr, d[1].skipHiddenAddr,
                                strictSymEquality = true):
      b.wasMovedLocs[i].flags.incl nfMarkForDeletion
      c.somethingTodo = true
      d.flags.incl nfMarkForDeletion
      b.wasMovedLocs.del i
    else:
      inc i

proc analyse(c: var Con; b: var BasicBlock; n: PNode) =
  case n.kind
  of nkCallKinds:
    var special = false
    var reverse = false
    if n[0].kind == nkSym:
      let s = n[0].sym
      let name = s.name.s.normalize
      if name == "=wasmoved":
        b.wasMovedLocs.add n
        special = true
      elif name == "=destroy":
        if c.inFinally > 0 and (b.hasReturn or b.hasBreak):
          discard "cannot optimize away the destructor"
        else:
          c.wasMovedDestroyPair b, n
        special = true
      elif name == "=sink":
        reverse = true

    if not special:
      if not reverse:
        for i in 0 ..< n.len:
          analyse(c, b, n[i])
      else:
        #[ Test destructor/tmatrix.test3:
        Prevent this from being elided. We should probably
        find a better solution...

            `=sink`(b, -
              let blitTmp = b;
              wasMoved(b);
              blitTmp + a)
            `=destroy`(b)

        ]#
        for i in countdown(n.len-1, 0):
          analyse(c, b, n[i])
      if canRaise(n[0]): returnStmt(b)

  of nkSym:
    # any usage of the location before destruction implies we
    # cannot elide the 'wasMoved(x)':
    b.invalidateWasMoved n

  of nkNone..pred(nkSym), succ(nkSym)..nkNilLit, nkTypeSection, nkProcDef, nkConverterDef,
      nkMethodDef, nkIteratorDef, nkMacroDef, nkTemplateDef, nkLambda, nkDo,
      nkFuncDef, nkConstSection, nkConstDef, nkIncludeStmt, nkImportStmt,
      nkExportStmt, nkPragma, nkCommentStmt, nkBreakState,
      nkTypeOfExpr, nkMixinStmt, nkBindStmt:
    discard "do not follow the construct"

  of nkAsgn, nkFastAsgn, nkSinkAsgn:
    # reverse order, see remark for `=sink`:
    analyse(c, b, n[1])
    analyse(c, b, n[0])

  of nkIfStmt, nkIfExpr:
    let isExhaustive = n[^1].kind in {nkElse, nkElseExpr}
    var wasMovedSet: seq[PNode] = @[]

    for i in 0 ..< n.len:
      var branch = nestedBlock(b, n[i].kind)

      analyse(c, branch, n[i])
      mergeBasicBlockInfo(b, branch)
      if isExhaustive:
        if i == 0:
          wasMovedSet = move(branch.wasMovedLocs)
        else:
          wasMovedSet.intersect(branch.wasMovedLocs)
    for i in 0..<wasMovedSet.len:
      b.wasMovedLocs.add wasMovedSet[i]

  of nkCaseStmt:
    let isExhaustive = skipTypes(n[0].typ,
      abstractVarRange-{tyTypeDesc}).kind notin {tyFloat..tyFloat128, tyString, tyCstring} or
      n[^1].kind == nkElse

    analyse(c, b, n[0])

    var wasMovedSet: seq[PNode] = @[]

    for i in 1 ..< n.len:
      var branch = nestedBlock(b, n[i].kind)

      analyse(c, branch, n[i])
      mergeBasicBlockInfo(b, branch)
      if isExhaustive:
        if i == 1:
          wasMovedSet = move(branch.wasMovedLocs)
        else:
          wasMovedSet.intersect(branch.wasMovedLocs)
    for i in 0..<wasMovedSet.len:
      b.wasMovedLocs.add wasMovedSet[i]

  of nkTryStmt:
    for i in 0 ..< n.len:
      var tryBody = nestedBlock(b, nkTryStmt)

      analyse(c, tryBody, n[i])
      mergeBasicBlockInfo(b, tryBody)

  of nkWhileStmt:
    analyse(c, b, n[0])
    var loopBody = nestedBlock(b, nkWhileStmt)
    analyse(c, loopBody, n[1])
    mergeBasicBlockInfo(b, loopBody)

  of nkBlockStmt, nkBlockExpr:
    var blockBody = nestedBlock(b, n.kind)
    if n[0].kind == nkSym:
      blockBody.label = n[0].sym
    analyse(c, blockBody, n[1])
    mergeBasicBlockInfo(b, blockBody)

  of nkBreakStmt:
    breakStmt(b, n[0])

  of nkReturnStmt, nkRaiseStmt:
    for child in n: analyse(c, b, child)
    returnStmt(b)

  of nkFinally:
    inc c.inFinally
    for child in n: analyse(c, b, child)
    dec c.inFinally

  else:
    for child in n: analyse(c, b, child)

proc opt(c: Con; n, parent: PNode; parentPos: int) =
  template recurse() =
    let x = shallowCopy(n)
    for i in 0 ..< n.len:
      opt(c, n[i], x, i)
    parent[parentPos] = x

  case n.kind
  of nkCallKinds:
    if nfMarkForDeletion in n.flags:
      parent[parentPos] = newNodeI(nkEmpty, n.info)
    else:
      recurse()

  of nkNone..nkNilLit, nkTypeSection, nkProcDef, nkConverterDef,
      nkMethodDef, nkIteratorDef, nkMacroDef, nkTemplateDef, nkLambda, nkDo,
      nkFuncDef, nkConstSection, nkConstDef, nkIncludeStmt, nkImportStmt,
      nkExportStmt, nkPragma, nkCommentStmt, nkBreakState, nkTypeOfExpr,
      nkMixinStmt, nkBindStmt:
    parent[parentPos] = n

  else:
    recurse()


proc optimize*(n: PNode): PNode =
  # optimize away simple 'wasMoved(x); destroy(x)' pairs.
  #[ Unfortunately this optimization is only really safe when no exceptions
     are possible, see for example:

  proc main(inp: string; cond: bool) =
    if cond:
      try:
        var s = ["hi", inp & "more"]
        for i in 0..4:
          use s
        consume(s)
        wasMoved(s)
      finally:
        destroy(s)

    Now assume 'use' raises, then we shouldn't do the 'wasMoved(s)'
  ]#
  var c: Con = Con()
  var b: BasicBlock = default(BasicBlock)
  analyse(c, b, n)
  if c.somethingTodo:
    result = shallowCopy(n)
    for i in 0 ..< n.safeLen:
      opt(c, n[i], result, i)
  else:
    result = n
>comesFrom) var info = c.debug[pc] # we now use the same format as in system/except.nim var s = toFilename(info) var line = toLineNumber(info) if line > 0: add(s, '(') add(s, $line) add(s, ')') if x.prc != nil: for k in 1..max(1, 25-s.len): add(s, ' ') add(s, x.prc.name.s) MsgWriteln(s) proc stackTrace(c: PCtx, tos: PStackFrame, pc: int, msg: TMsgKind, arg = "") = MsgWriteln("stack trace: (most recent call last)") stackTraceAux(c, tos, pc) LocalError(c.debug[pc], msg, arg) proc bailOut(c: PCtx; tos: PStackFrame) = stackTrace(c, tos, c.exceptionInstr, errUnhandledExceptionX, c.currentExceptionA.sons[2].strVal) when not defined(nimHasInterpreterLoop): {.pragma: interpreterLoop.} template inc(pc: ptr TInstr, diff = 1) = inc cast[TAddress](pc), TInstr.sizeof * diff proc myreset(n: PNode) = when defined(system.reset): var oldInfo = n.info reset(n[]) n.info = oldInfo template ensureKind(k: expr) {.immediate, dirty.} = if regs[ra].kind != k: myreset(regs[ra]) regs[ra].kind = k template decodeB(k: expr) {.immediate, dirty.} = let rb = instr.regB ensureKind(k) template decodeBC(k: expr) {.immediate, dirty.} = let rb = instr.regB let rc = instr.regC ensureKind(k) template declBC() {.immediate, dirty.} = let rb = instr.regB let rc = instr.regC template decodeBImm(k: expr) {.immediate, dirty.} = let rb = instr.regB let imm = instr.regC - byteExcess ensureKind(k) template decodeBx(k: expr) {.immediate, dirty.} = let rbx = instr.regBx - wordExcess ensureKind(k) template move(a, b: expr) = system.shallowCopy(a, b) # XXX fix minor 'shallowCopy' overloading bug in compiler proc asgnRef(x, y: PNode) = myreset(x) x.kind = y.kind x.typ = y.typ case x.kind of nkCharLit..nkInt64Lit: x.intVal = y.intVal of nkFloatLit..nkFloat64Lit: x.floatVal = y.floatVal of nkStrLit..nkTripleStrLit: x.strVal = y.strVal of nkIdent: x.ident = y.ident of nkSym: x.sym = y.sym else: if x.kind notin {nkEmpty..nkNilLit}: move(x.sons, y.sons) proc asgnComplex(x, y: PNode) = myreset(x) x.kind = y.kind x.typ = y.typ case x.kind of nkCharLit..nkInt64Lit: x.intVal = y.intVal of nkFloatLit..nkFloat64Lit: x.floatVal = y.floatVal of nkStrLit..nkTripleStrLit: x.strVal = y.strVal of nkIdent: x.ident = y.ident of nkSym: x.sym = y.sym else: if x.kind notin {nkEmpty..nkNilLit}: let y = y.copyTree for i in countup(0, sonsLen(y) - 1): addSon(x, y.sons[i]) template getstr(a: expr): expr = (if a.kind == nkStrLit: a.strVal else: $chr(int(a.intVal))) proc pushSafePoint(f: PStackFrame; pc: int) = if f.safePoints.isNil: f.safePoints = @[] f.safePoints.add(pc) proc popSafePoint(f: PStackFrame) = discard f.safePoints.pop() proc cleanUpOnException(c: PCtx; tos: PStackFrame; regs: TNodeSeq): int = let raisedType = c.currentExceptionA.typ.skipTypes(abstractPtrs) var f = tos while true: while f.safePoints.isNil or f.safePoints.len == 0: f = f.next if f.isNil: return -1 var pc2 = f.safePoints[f.safePoints.high] var nextExceptOrFinally = -1 if c.code[pc2].opcode == opcExcept: nextExceptOrFinally = pc2 + c.code[pc2].regBx - wordExcess inc pc2 while c.code[pc2].opcode == opcExcept: let exceptType = c.types[c.code[pc2].regBx-wordExcess].skipTypes( abstractPtrs) if inheritanceDiff(exceptType, raisedType) <= 0: # mark exception as handled but keep it in B for # the getCurrentException() builtin: c.currentExceptionB = c.currentExceptionA c.currentExceptionA = nil # execute the corresponding handler: return pc2 inc pc2 if nextExceptOrFinally >= 0: pc2 = nextExceptOrFinally if c.code[pc2].opcode == opcFinally: # execute the corresponding handler, but don't quit walking the stack: return pc2 # not the right one: discard f.safePoints.pop proc cleanUpOnReturn(c: PCtx; f: PStackFrame): int = if f.safePoints.isNil: return -1 for s in f.safePoints: var pc = s while c.code[pc].opcode == opcExcept: pc = pc + c.code[pc].regBx - wordExcess if c.code[pc].opcode == opcFinally: return pc return -1 proc opConv*(dest, src: PNode, typ: PType): bool = if typ.kind == tyString: if dest.kind != nkStrLit: myreset(dest) dest.kind = nkStrLit case src.typ.skipTypes(abstractRange).kind of tyEnum: dest.strVal = ordinalValToString(src) of tyInt..tyInt64, tyUInt..tyUInt64: dest.strVal = $src.intVal of tyBool: dest.strVal = if src.intVal == 0: "false" else: "true" of tyFloat..tyFloat128: dest.strVal = $src.floatVal of tyString, tyCString: dest.strVal = src.strVal of tyChar: dest.strVal = $chr(src.intVal) else: internalError("cannot convert to string " & typ.typeToString) else: case skipTypes(typ, abstractRange).kind of tyInt..tyInt64: if dest.kind != nkIntLit: myreset(dest); dest.kind = nkIntLit case skipTypes(src.typ, abstractRange).kind of tyFloat..tyFloat64: dest.intVal = system.toInt(src.floatVal) else: dest.intVal = src.intVal if dest.intVal < firstOrd(typ) or dest.intVal > lastOrd(typ): return true of tyUInt..tyUInt64: if dest.kind != nkIntLit: myreset(dest); dest.kind = nkIntLit case skipTypes(src.typ, abstractRange).kind of tyFloat..tyFloat64: dest.intVal = system.toInt(src.floatVal) else: dest.intVal = src.intVal and ((1 shl typ.size)-1) of tyFloat..tyFloat64: if dest.kind != nkFloatLit: myreset(dest); dest.kind = nkFloatLit case skipTypes(src.typ, abstractRange).kind of tyInt..tyInt64, tyUInt..tyUInt64, tyEnum, tyBool, tyChar: dest.floatVal = toFloat(src.intVal.int) else: dest.floatVal = src.floatVal else: asgnComplex(dest, src) proc compile(c: PCtx, s: PSym): int = result = vmgen.genProc(c, s) #c.echoCode proc execute(c: PCtx, start: int) = var pc = start var regs: TNodeSeq # alias to tos.slots for performance var tos: PStackFrame newSeq(regs, c.prc.maxSlots) while true: {.interpreterLoop.} let instr = c.code[pc] let ra = instr.regA #echo "PC ", pc, " ", c.code[pc].opcode, " ra ", ra case instr.opcode of opcEof: break of opcRet: # XXX perform any cleanup actions pc = tos.comesFrom tos = tos.next if tos.isNil: return let retVal = regs[0] move(regs, tos.slots) assert c.code[pc].opcode in {opcIndCall, opcIndCallAsgn} if c.code[pc].opcode == opcIndCallAsgn: regs[c.code[pc].regA] = retVal of opcYldYoid: assert false of opcYldVal: assert false of opcAsgnInt: decodeB(nkIntLit) regs[ra].intVal = regs[rb].intVal of opcAsgnStr: decodeB(nkStrLit) regs[ra].strVal = regs[rb].strVal of opcAsgnFloat: decodeB(nkFloatLit) regs[ra].floatVal = regs[rb].floatVal of opcAsgnComplex: asgnComplex(regs[ra], regs[instr.regB]) of opcAsgnRef: asgnRef(regs[ra], regs[instr.regB]) of opcWrGlobalRef: asgnRef(c.globals[instr.regBx-wordExcess-1], regs[ra]) of opcWrGlobal: asgnComplex(c.globals.sons[instr.regBx-wordExcess-1], regs[ra]) of opcLdArr: # a = b[c] let rb = instr.regB let rc = instr.regC let idx = regs[rc].intVal # XXX what if the array is not 0-based? -> codegen should insert a sub regs[ra] = regs[rb].sons[idx.int] of opcLdStrIdx: decodeBC(nkIntLit) let idx = regs[rc].intVal regs[ra].intVal = regs[rb].strVal[idx.int].ord of opcWrArr: # a[b] = c let rb = instr.regB let rc = instr.regC let idx = regs[rb].intVal asgnComplex(regs[ra].sons[idx.int], regs[rc]) of opcWrArrRef: let rb = instr.regB let rc = instr.regC let idx = regs[rb].intVal asgnRef(regs[ra].sons[idx.int], regs[rc]) of opcLdObj: # a = b.c let rb = instr.regB let rc = instr.regC # XXX this creates a wrong alias #Message(c.debug[pc], warnUser, $regs[rb].len & " " & $rc) asgnComplex(regs[ra], regs[rb].sons[rc]) of opcWrObj: # a.b = c let rb = instr.regB let rc = instr.regC asgnComplex(regs[ra].sons[rb], regs[rc]) of opcWrObjRef: let rb = instr.regB let rc = instr.regC asgnRef(regs[ra].sons[rb], regs[rc]) of opcWrStrIdx: decodeBC(nkStrLit) let idx = regs[rb].intVal.int regs[ra].strVal[idx] = chr(regs[rc].intVal) of opcAddr: decodeB(nkRefTy) if regs[ra].len == 0: regs[ra].add regs[rb] else: regs[ra].sons[0] = regs[rb] of opcDeref: # a = b[] let rb = instr.regB if regs[rb].kind == nkNilLit: stackTrace(c, tos, pc, errNilAccess) assert regs[rb].kind == nkRefTy regs[ra] = regs[rb].sons[0] of opcAddInt: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal + regs[rc].intVal of opcAddImmInt: decodeBImm(nkIntLit) regs[ra].intVal = regs[rb].intVal + imm of opcSubInt: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal - regs[rc].intVal of opcSubImmInt: decodeBImm(nkIntLit) regs[ra].intVal = regs[rb].intVal - imm of opcLenSeq: decodeBImm(nkIntLit) #assert regs[rb].kind == nkBracket # also used by mNLen regs[ra].intVal = regs[rb].len - imm of opcLenStr: decodeBImm(nkIntLit) assert regs[rb].kind == nkStrLit regs[ra].intVal = regs[rb].strVal.len - imm of opcIncl: decodeB(nkCurly) if not inSet(regs[ra], regs[rb]): addSon(regs[ra], copyTree(regs[rb])) of opcExcl: decodeB(nkCurly) # XXX arg we need types here :-( var b = newNodeIT(nkCurly, regs[rb].info, regs[rb].typ) addSon(b, regs[rb]) var r = diffSets(regs[ra], b) discardSons(regs[ra]) for i in countup(0, sonsLen(r) - 1): addSon(regs[ra], r.sons[i]) of opcCard: decodeB(nkIntLit) regs[ra].intVal = nimsets.cardSet(regs[rb]) of opcMulInt: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal * regs[rc].intVal of opcDivInt: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal div regs[rc].intVal of opcModInt: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal mod regs[rc].intVal of opcAddFloat: decodeBC(nkFloatLit) regs[ra].floatVal = regs[rb].floatVal + regs[rc].floatVal of opcSubFloat: decodeBC(nkFloatLit) regs[ra].floatVal = regs[rb].floatVal - regs[rc].floatVal of opcMulFloat: decodeBC(nkFloatLit) regs[ra].floatVal = regs[rb].floatVal * regs[rc].floatVal of opcDivFloat: decodeBC(nkFloatLit) regs[ra].floatVal = regs[rb].floatVal / regs[rc].floatVal of opcShrInt: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal shr regs[rc].intVal of opcShlInt: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal shl regs[rc].intVal of opcBitandInt: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal and regs[rc].intVal of opcBitorInt: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal or regs[rc].intVal of opcBitxorInt: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal xor regs[rc].intVal of opcAddu: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal +% regs[rc].intVal of opcSubu: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal -% regs[rc].intVal of opcMulu: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal *% regs[rc].intVal of opcDivu: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal /% regs[rc].intVal of opcModu: decodeBC(nkIntLit) regs[ra].intVal = regs[rb].intVal %% regs[rc].intVal of opcEqInt: decodeBC(nkIntLit) regs[ra].intVal = ord(regs[rb].intVal == regs[rc].intVal) of opcLeInt: decodeBC(nkIntLit) regs[ra].intVal = ord(regs[rb].intVal <= regs[rc].intVal) of opcLtInt: decodeBC(nkIntLit) regs[ra].intVal = ord(regs[rb].intVal < regs[rc].intVal) of opcEqFloat: decodeBC(nkIntLit) regs[ra].intVal = ord(regs[rb].floatVal == regs[rc].floatVal) of opcLeFloat: decodeBC(nkIntLit) regs[ra].intVal = ord(regs[rb].floatVal <= regs[rc].floatVal) of opcLtFloat: decodeBC(nkIntLit) regs[ra].intVal = ord(regs[rb].floatVal < regs[rc].floatVal) of opcLeu: decodeBC(nkIntLit) regs[ra].intVal = ord(regs[rb].intVal <=% regs[rc].intVal) of opcLtu: decodeBC(nkIntLit) regs[ra].intVal = ord(regs[rb].intVal <% regs[rc].intVal) of opcEqRef: decodeBC(nkIntLit) regs[ra].intVal = ord((regs[rb].kind == nkNilLit and regs[rc].kind == nkNilLit) or regs[rb].sons == regs[rc].sons) of opcXor: decodeBC(nkIntLit) regs[ra].intVal = ord(regs[rb].intVal != regs[rc].intVal) of opcNot: decodeB(nkIntLit) assert regs[rb].kind == nkIntLit regs[ra].intVal = 1 - regs[rb].intVal of opcUnaryMinusInt: decodeB(nkIntLit) assert regs[rb].kind == nkIntLit regs[ra].intVal = -regs[rb].intVal of opcUnaryMinusFloat: decodeB(nkFloatLit) assert regs[rb].kind == nkFloatLit regs[ra].floatVal = -regs[rb].floatVal of opcBitnotInt: decodeB(nkIntLit) assert regs[rb].kind == nkIntLit regs[ra].intVal = not regs[rb].intVal of opcEqStr: decodeBC(nkIntLit) regs[ra].intVal = Ord(regs[rb].strVal == regs[rc].strVal) of opcLeStr: decodeBC(nkIntLit) regs[ra].intVal = Ord(regs[rb].strVal <= regs[rc].strVal) of opcLtStr: decodeBC(nkIntLit) regs[ra].intVal = Ord(regs[rb].strVal < regs[rc].strVal) of opcLeSet: decodeBC(nkIntLit) regs[ra].intVal = Ord(containsSets(regs[rb], regs[rc])) of opcEqSet: decodeBC(nkIntLit) regs[ra].intVal = Ord(equalSets(regs[rb], regs[rc])) of opcLtSet: decodeBC(nkIntLit) let a = regs[rb] let b = regs[rc] regs[ra].intVal = Ord(containsSets(a, b) and not equalSets(a, b)) of opcMulSet: decodeBC(nkCurly) move(regs[ra].sons, nimsets.intersectSets(regs[rb], regs[rc]).sons) of opcPlusSet: decodeBC(nkCurly) move(regs[ra].sons, nimsets.unionSets(regs[rb], regs[rc]).sons) of opcMinusSet: decodeBC(nkCurly) move(regs[ra].sons, nimsets.diffSets(regs[rb], regs[rc]).sons) of opcSymDiffSet: decodeBC(nkCurly) move(regs[ra].sons, nimsets.symdiffSets(regs[rb], regs[rc]).sons) of opcConcatStr: decodeBC(nkStrLit) regs[ra].strVal = getstr(regs[rb]) for i in rb+1..rb+rc-1: regs[ra].strVal.add getstr(regs[i]) of opcAddStrCh: decodeB(nkStrLit) regs[ra].strVal.add(regs[rb].intVal.chr) of opcAddStrStr: decodeB(nkStrLit) regs[ra].strVal.add(regs[rb].strVal) of opcAddSeqElem: decodeB(nkBracket) regs[ra].add(copyTree(regs[rb])) of opcEcho: let rb = instr.regB for i in ra..ra+rb-1: if regs[i].kind != nkStrLit: debug regs[i] write(stdout, regs[i].strVal) writeln(stdout, "") of opcContainsSet: decodeBC(nkIntLit) regs[ra].intVal = Ord(inSet(regs[rb], regs[rc])) of opcSubStr: decodeBC(nkStrLit) inc pc assert c.code[pc].opcode == opcSubStr let rd = c.code[pc].regA regs[ra].strVal = substr(regs[rb].strVal, regs[rc].intVal.int, regs[rd].intVal.int) of opcRangeChck: let rb = instr.regB let rc = instr.regC if not (leValueConv(regs[rb], regs[ra]) and leValueConv(regs[ra], regs[rc])): stackTrace(c, tos, pc, errGenerated, msgKindToString(errIllegalConvFromXtoY) % [ "unknown type" , "unknown type"]) of opcIndCall, opcIndCallAsgn: # dest = call regStart, n; where regStart = fn, arg1, ... let rb = instr.regB let rc = instr.regC let prc = regs[rb].sym let newPc = compile(c, prc) var newFrame = PStackFrame(prc: prc, comesFrom: pc, next: tos) newSeq(newFrame.slots, prc.position) if not isEmptyType(prc.typ.sons[0]): newFrame.slots[0] = getNullValue(prc.typ.sons[0], prc.info) # pass every parameter by var (the language definition allows this): for i in 1 .. rc-1: newFrame.slots[i] = regs[rb+i] # allocate the temporaries: for i in rc .. <prc.position: newFrame.slots[i] = newNode(nkEmpty) tos = newFrame move(regs, newFrame.slots) # -1 for the following 'inc pc' pc = newPc-1 of opcTJmp: # jump Bx if A != 0 let rbx = instr.regBx - wordExcess - 1 # -1 for the following 'inc pc' if regs[ra].intVal != 0: inc pc, rbx of opcFJmp: # jump Bx if A == 0 let rbx = instr.regBx - wordExcess - 1 # -1 for the following 'inc pc' if regs[ra].intVal == 0: inc pc, rbx of opcJmp: # jump Bx let rbx = instr.regBx - wordExcess - 1 # -1 for the following 'inc pc' inc pc, rbx of opcBranch: # we know the next instruction is a 'jmp': let branch = c.constants[instr.regBx-wordExcess] var cond = false for j in countup(0, sonsLen(branch) - 2): if overlap(regs[ra], branch.sons[j]): cond = true break assert c.code[pc+1].opcode == opcJmp inc pc # we skip this instruction so that the final 'inc(pc)' skips # the following jump if cond: let instr2 = c.code[pc] let rbx = instr2.regBx - wordExcess - 1 # -1 for the following 'inc pc' inc pc, rbx of opcTry: let rbx = instr.regBx - wordExcess tos.pushSafePoint(pc + rbx) of opcExcept: # just skip it; it's followed by a jump; # we'll execute in the 'raise' handler discard of opcFinally: # just skip it; it's followed by the code we need to execute anyway tos.popSafePoint() of opcFinallyEnd: if c.currentExceptionA != nil: # we are in a cleanup run: pc = cleanupOnException(c, tos, regs)-1 if pc < 0: bailOut(c, tos) return of opcRaise: let raised = regs[ra] c.currentExceptionA = raised c.exceptionInstr = pc # -1 because of the following 'inc' pc = cleanupOnException(c, tos, regs) - 1 if pc < 0: bailOut(c, tos) return of opcNew: let typ = c.types[instr.regBx - wordExcess] regs[ra] = getNullValue(typ, regs[ra].info) of opcNewSeq: let typ = c.types[instr.regBx - wordExcess] inc pc ensureKind(nkBracket) let instr2 = c.code[pc] let rb = instr2.regA regs[ra].typ = typ newSeq(regs[ra].sons, rb) for i in 0 .. <rb: regs[ra].sons[i] = getNullValue(typ, regs[ra].info) of opcNewStr: decodeB(nkStrLit) regs[ra].strVal = newString(regs[rb].intVal.int) of opcLdImmInt: # dest = immediate value decodeBx(nkIntLit) regs[ra].intVal = rbx of opcLdNull: let typ = c.types[instr.regBx - wordExcess] regs[ra] = getNullValue(typ, c.debug[pc]) of opcLdConst: regs[ra] = c.constants.sons[instr.regBx - wordExcess] of opcAsgnConst: let rb = instr.regBx - wordExcess if regs[ra].isNil: regs[ra] = copyTree(c.constants.sons[rb]) else: asgnComplex(regs[ra], c.constants.sons[rb]) of opcNBindSym: # trivial implementation: let rb = instr.regB regs[ra] = regs[rb].sons[1] of opcNChild: let rb = instr.regB let rc = instr.regC regs[ra] = regs[rb].sons[regs[rc].intVal.int] of opcNSetChild: let rb = instr.regB let rc = instr.regC regs[ra].sons[regs[rb].intVal.int] = regs[rc] of opcNAdd: declBC() regs[rb].add(regs[rb]) regs[ra] = regs[rb] of opcNAddMultiple: declBC() let x = regs[rc] # XXX can be optimized: for i in 0.. <x.len: regs[rb].add(x.sons[i]) regs[ra] = regs[rb] of opcNKind: decodeB(nkIntLit) regs[ra].intVal = ord(regs[rb].kind) of opcNIntVal: decodeB(nkIntLit) let a = regs[rb] case a.kind of nkCharLit..nkInt64Lit: regs[ra].intVal = a.intVal else: stackTrace(c, tos, pc, errFieldXNotFound, "intVal") of opcNFloatVal: decodeB(nkFloatLit) let a = regs[rb] case a.kind of nkFloatLit..nkFloat64Lit: regs[ra].floatVal = a.floatVal else: stackTrace(c, tos, pc, errFieldXNotFound, "floatVal") of opcNSymbol: let rb = instr.regB if regs[rb].kind != nkSym: stackTrace(c, tos, pc, errFieldXNotFound, "symbol") regs[ra] = regs[rb] of opcNIdent: let rb = instr.regB if regs[rb].kind != nkIdent: stackTrace(c, tos, pc, errFieldXNotFound, "ident") regs[ra] = regs[rb] of opcNGetType: InternalError(c.debug[pc], "unknown opcode " & $instr.opcode) of opcNStrVal: decodeB(nkStrLit) let a = regs[rb] case a.kind of nkStrLit..nkTripleStrLit: regs[ra].strVal = a.strVal else: stackTrace(c, tos, pc, errFieldXNotFound, "strVal") of opcSlurp: decodeB(nkStrLit) regs[ra].strVal = opSlurp(regs[rb].strVal, c.debug[pc], c.module) of opcGorge: decodeBC(nkStrLit) regs[ra].strVal = opGorge(regs[rb].strVal, regs[rc].strVal) of opcNError: stackTrace(c, tos, pc, errUser, regs[ra].strVal) of opcNWarning: Message(c.debug[pc], warnUser, regs[ra].strVal) of opcNHint: Message(c.debug[pc], hintUser, regs[ra].strVal) of opcParseExprToAst: let rb = instr.regB # c.debug[pc].line.int - countLines(regs[rb].strVal) ? let ast = parseString(regs[rb].strVal, c.debug[pc].toFilename, c.debug[pc].line.int) if sonsLen(ast) != 1: GlobalError(c.debug[pc], errExprExpected, "multiple statements") regs[ra] = ast.sons[0] of opcParseStmtToAst: let rb = instr.regB let ast = parseString(regs[rb].strVal, c.debug[pc].toFilename, c.debug[pc].line.int) regs[ra] = ast of opcCallSite: if c.callsite != nil: regs[ra] = c.callsite else: stackTrace(c, tos, pc, errFieldXNotFound, "callsite") of opcNLineInfo: let rb = instr.regB let n = regs[rb] regs[ra] = newStrNode(nkStrLit, n.info.toFileLineCol) regs[ra].info = c.debug[pc] of opcEqIdent: decodeBC(nkIntLit) if regs[rb].kind == nkIdent and regs[rc].kind == nkIdent: regs[ra].intVal = ord(regs[rb].ident.id == regs[rc].ident.id) else: regs[ra].intVal = 0 of opcStrToIdent: let rb = instr.regB if regs[rb].kind notin {nkStrLit..nkTripleStrLit}: stackTrace(c, tos, pc, errFieldXNotFound, "strVal") else: regs[ra] = newNodeI(nkIdent, c.debug[pc]) regs[ra].ident = getIdent(regs[rb].strVal) of opcIdentToStr: let rb = instr.regB let a = regs[rb] regs[ra] = newNodeI(nkStrLit, c.debug[pc]) if a.kind == nkSym: regs[ra].strVal = a.sym.name.s elif a.kind == nkIdent: regs[ra].strVal = a.ident.s else: stackTrace(c, tos, pc, errFieldXNotFound, "ident") of opcSetType: regs[ra].typ = c.types[instr.regBx - wordExcess] of opcConv: let rb = instr.regB inc pc let typ = c.types[c.code[pc].regBx - wordExcess] if opConv(regs[ra], regs[rb], typ): stackTrace(c, tos, pc, errGenerated, msgKindToString(errIllegalConvFromXtoY) % [ "unknown type" , "unknown type"]) of opcNSetIntVal: let rb = instr.regB if regs[ra].kind in {nkCharLit..nkInt64Lit} and regs[rb].kind in {nkCharLit..nkInt64Lit}: regs[ra].intVal = regs[rb].intVal else: stackTrace(c, tos, pc, errFieldXNotFound, "intVal") of opcNSetFloatVal: let rb = instr.regB if regs[ra].kind in {nkFloatLit..nkFloat64Lit} and regs[rb].kind in {nkFloatLit..nkFloat64Lit}: regs[ra].floatVal = regs[rb].floatVal else: stackTrace(c, tos, pc, errFieldXNotFound, "floatVal") of opcNSetSymbol: let rb = instr.regB if regs[ra].kind == nkSym and regs[rb].kind == nkSym: regs[ra].sym = regs[rb].sym else: stackTrace(c, tos, pc, errFieldXNotFound, "symbol") of opcNSetIdent: let rb = instr.regB if regs[ra].kind == nkIdent and regs[rb].kind == nkIdent: regs[ra].ident = regs[rb].ident else: stackTrace(c, tos, pc, errFieldXNotFound, "ident") of opcNSetType: let b = regs[instr.regB] InternalAssert b.kind == nkSym and b.sym.kind == skType regs[ra].typ = b.sym.typ of opcNSetStrVal: let rb = instr.regB if regs[ra].kind in {nkStrLit..nkTripleStrLit} and regs[rb].kind in {nkStrLit..nkTripleStrLit}: regs[ra].strVal = regs[rb].strVal else: stackTrace(c, tos, pc, errFieldXNotFound, "strVal") of opcNNewNimNode: let rb = instr.regB let rc = instr.regC var k = regs[rb].intVal if k < 0 or k > ord(high(TNodeKind)): internalError(c.debug[pc], "request to create a NimNode with invalid kind") regs[ra] = newNodeI(TNodeKind(int(k)), if regs[rc].kind == nkNilLit: c.debug[pc] else: regs[rc].info) of opcNCopyNimNode: let rb = instr.regB regs[ra] = copyNode(regs[rb]) of opcNCopyNimTree: let rb = instr.regB regs[ra] = copyTree(regs[rb]) else: InternalError(c.debug[pc], "unknown opcode " & $instr.opcode) inc pc proc eval*(c: PCtx, n: PNode): PNode = ## eval never returns nil! This simplifies the code a lot and ## makes it faster too. let start = genStmt(c, n) # execute new instructions; this redundant opcEof check saves us lots # of allocations in 'execute': if c.code[start].opcode != opcEof: execute(c, start) result = emptyNode proc myOpen(module: PSym): PPassContext = #var c = newEvalContext(module, emRepl) #c.features = {allowCast, allowFFI, allowInfiniteLoops} #pushStackFrame(c, newStackFrame()) result = newCtx(module) var oldErrorCount: int proc myProcess(c: PPassContext, n: PNode): PNode = # don't eval errornous code: if oldErrorCount == msgs.gErrorCounter: result = eval(PCtx(c), n) else: result = n oldErrorCount = msgs.gErrorCounter const vmPass* = makePass(myOpen, nil, myProcess, myProcess)