diff --git a/compiler/ast.nim b/compiler/ast.nim index eee1e1ad25..3456177d30 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -666,6 +666,7 @@ type # (or not in symbol table) # for modules, an unique index corresponding # to the module's fileIdx + # for variables a slot index for the evaluator offset*: int # offset of record field loc*: TLoc diff --git a/compiler/evals.nim b/compiler/evals.nim index 8c05d57fc1..1689a37d8e 100644 --- a/compiler/evals.nim +++ b/compiler/evals.nim @@ -23,12 +23,12 @@ when hasFFI: type PStackFrame* = ref TStackFrame - TStackFrame*{.final.} = object - mapping*: TIdNodeTable # mapping from symbols to nodes - prc*: PSym # current prc; proc that is evaluated - call*: PNode - next*: PStackFrame # for stacking - params*: TNodeSeq # parameters passed to the proc + TStackFrame* = object + prc: PSym # current prc; proc that is evaluated + slots: TNodeSeq # parameters passed to the proc + locals; + # parameters come first + call: PNode + next: PStackFrame # for stacking TEvalMode* = enum ## reason for evaluation emRepl, ## evaluate because in REPL mode @@ -67,10 +67,9 @@ const # other idea: use a timeout! -> Wether code compiles depends on the machine # the compiler runs on then! Bad idea! -proc newStackFrame*(): PStackFrame = +proc newStackFrame*(): PStackFrame = new(result) - initIdNodeTable(result.mapping) - result.params = @[] + result.slots = @[] proc newEvalContext*(module: PSym, mode: TEvalMode): PEvalContext = new(result) @@ -291,6 +290,20 @@ proc evalVarValue(c: PEvalContext, n: PNode): PNode = result = evalAux(c, n, {}) if result.kind in {nkType..nkNilLit}: result = result.copyNode +proc setSlot(c: PStackFrame, sym: PSym, val: PNode) = + assert sym.owner == c.prc + var idx = sym.position + if idx == 0: + idx = c.slots.len + if idx == 0: idx = 1 + sym.position = idx + setLen(c.slots, max(idx+1, c.slots.len)) + c.slots[idx] = val + +proc setVar(c: PEvalContext, v: PSym, n: PNode) = + if sfGlobal notin v.flags: setSlot(c.tos, v, n) + else: IdNodeTablePut(c.globals, v, n) + proc evalVar(c: PEvalContext, n: PNode): PNode = for i in countup(0, sonsLen(n) - 1): let a = n.sons[i] @@ -305,7 +318,7 @@ proc evalVar(c: PEvalContext, n: PNode): PNode = return raiseCannotEval(c, n.info) for i in 0 .. a.len-3: var v = a.sons[i].sym - IdNodeTablePut(c.tos.mapping, v, result.sons[i]) + setVar(c, v, result.sons[i]) else: if a.sons[2].kind != nkEmpty: result = evalVarValue(c, a.sons[2]) @@ -314,7 +327,7 @@ proc evalVar(c: PEvalContext, n: PNode): PNode = result = getNullValue(a.sons[0].typ, a.sons[0].info) if a.sons[0].kind == nkSym: var v = a.sons[0].sym - IdNodeTablePut(c.tos.mapping, v, result) + setVar(c, v, result) else: # assign to a.sons[0]: var x = result @@ -339,21 +352,22 @@ proc aliasNeeded(n: PNode, flags: TEvalFlags): bool = result = efLValue in flags or n.typ == nil or n.typ.kind in {tyExpr, tyStmt, tyTypeDesc} -proc evalVariable(c: PStackFrame, sym: PSym, flags: TEvalFlags): PNode = +proc evalVariable(c: PStackFrame, sym: PSym, flags: TEvalFlags): PNode = # We need to return a node to the actual value, # which can be modified. + assert sym.position != 0 or skResult == sym.kind var x = c - while x != nil: - if sym.kind == skResult and x.params.len > 0: - result = x.params[0] - if result == nil: result = emptyNode + while x != nil: + if sym.owner == c.prc: + result = x.slots[sym.position] + assert result != nil + if not aliasNeeded(result, flags): + result = copyTree(result) return - result = IdNodeTableGet(x.mapping, sym) - if result != nil and not aliasNeeded(result, flags): - result = copyTree(result) - if result != nil: return x = x.next - #internalError(sym.info, "cannot eval " & sym.name.s) + debug sym.owner + debug c.prc + internalError(sym.info, "cannot eval " & sym.name.s & " " & $sym.position) result = raiseCannotEval(nil, sym.info) #result = emptyNode @@ -385,12 +399,12 @@ proc evalCall(c: PEvalContext, n: PNode): PNode = d.call = n var prc = n.sons[0] let isClosure = prc.kind == nkClosure - setlen(d.params, sonsLen(n) + ord(isClosure)) + setlen(d.slots, sonsLen(n) + ord(isClosure)) if isClosure: #debug prc result = evalAux(c, prc.sons[1], {efLValue}) if isSpecial(result): return - d.params[sonsLen(n)] = result + d.slots[sonsLen(n)] = result result = evalAux(c, prc.sons[0], {}) else: result = evalAux(c, prc, {}) @@ -408,21 +422,21 @@ proc evalCall(c: PEvalContext, n: PNode): PNode = for i in countup(1, sonsLen(n) - 1): result = evalAux(c, n.sons[i], {}) if isSpecial(result): return - d.params[i] = result - if n.typ != nil: d.params[0] = getNullValue(n.typ, n.info) + d.slots[i] = result + if n.typ != nil: d.slots[0] = getNullValue(n.typ, n.info) when hasFFI: if sfImportc in prc.sym.flags and allowFFI in c.features: var newCall = newNodeI(nkCall, n.info, n.len) newCall.sons[0] = evalGlobalVar(c, prc.sym, {}) for i in 1 ..