Skip to content

Commit

Permalink
bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Oct 29, 2023
1 parent 0f194a4 commit a0ad76b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
14 changes: 8 additions & 6 deletions compiler/nir/ast2ir.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type
types: TypesCon
module*: PSym
graph*: ModuleGraph
symnames*: SymNames
nativeIntId, nativeUIntId: TypeId
strPayloadId: (TypeId, TypeId)
idgen: IdGenerator
Expand Down Expand Up @@ -72,10 +71,11 @@ proc initModuleCon*(graph: ModuleGraph; config: ConfigRef; idgen: IdGenerator; m
result.strPayloadId = strPayloadPtrType(result.types, result.nirm.types)

proc initProcCon*(m: ModuleCon; prc: PSym; config: ConfigRef): ProcCon =
ProcCon(m: m, sm: initSlotManager({}), prc: prc, config: config,
result = ProcCon(m: m, sm: initSlotManager({}), prc: prc, config: config,
lit: m.nirm.lit, idgen: m.idgen,
options: if prc != nil: prc.options
else: config.options)
result.exitLabel = newLabel(result.labelGen)

proc toLineInfo(c: var ProcCon; i: TLineInfo): PackedLineInfo =
var val: LitId
Expand Down Expand Up @@ -716,7 +716,7 @@ proc genBinaryOp(c: var ProcCon; n: PNode; d: var Value; opc: Opcode) =
template body(target) =
buildTyped target, info, opc, t:
if optOverflowCheck in c.options and opc in {CheckedAdd, CheckedSub, CheckedMul, CheckedDiv, CheckedMod}:
c.code.addLabel info, CheckedGoto, c.exitLabel
target.addLabel info, CheckedGoto, c.exitLabel
copyTree target, tmp
copyTree target, tmp2
intoDest d, info, t, body
Expand Down Expand Up @@ -2154,7 +2154,7 @@ proc genVarSection(c: var ProcCon; n: PNode) =
#assert t.int >= 0, typeToString(s.typ) & (c.config $ n.info)
let symId = toSymId(c, s)
c.code.addSummon toLineInfo(c, a.info), symId, t, opc
c.m.symnames[symId] = c.lit.strings.getOrIncl(s.name.s)
c.m.nirm.symnames[symId] = c.lit.strings.getOrIncl(s.name.s)
if a[2].kind != nkEmpty:
genAsgn2(c, vn, a[2])
else:
Expand Down Expand Up @@ -2361,7 +2361,7 @@ proc genParams(c: var ProcCon; params: PNode; prc: PSym) =
assert t.int != -1, typeToString(s.typ)
let symId = toSymId(c, s)
c.code.addSummon toLineInfo(c, params[i].info), symId, t, SummonParam
c.m.symnames[symId] = c.lit.strings.getOrIncl(s.name.s)
c.m.nirm.symnames[symId] = c.lit.strings.getOrIncl(s.name.s)

proc addCallConv(c: var ProcCon; info: PackedLineInfo; callConv: TCallingConvention) =
template ann(s: untyped) = c.code.addPragmaId info, s
Expand Down Expand Up @@ -2394,7 +2394,7 @@ proc genProc(cOuter: var ProcCon; prc: PSym) =
build c.code, info, ProcDecl:
let symId = toSymId(c, prc)
addSymDef c.code, info, symId
c.m.symnames[symId] = c.lit.strings.getOrIncl(prc.name.s)
c.m.nirm.symnames[symId] = c.lit.strings.getOrIncl(prc.name.s)
addCallConv c, info, prc.typ.callConv
if sfCompilerProc in prc.flags:
build c.code, info, PragmaPair:
Expand Down Expand Up @@ -2426,6 +2426,8 @@ proc genProc(cOuter: var ProcCon; prc: PSym) =
genParams(c, prc.typ.n, prc)
gen(c, body)
patch c, body, c.exitLabel
build c.code, info, Ret:
discard

#copyTree cOuter.code, c.code
dec cOuter.m.inProc
Expand Down
17 changes: 11 additions & 6 deletions compiler/nir/nirvm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -936,13 +936,15 @@ proc eval(c: Bytecode; pc: CodePos; s: StackFrame; result: pointer; size: int) =
of UInt16Id: impl uint16
of UInt32Id: impl uint32
of UInt64Id: impl uint64
of Float32Id: impl float32
of Float64Id: impl float64
else:
case c.m.types[tid].kind
of ProcTy, UPtrTy, APtrTy, AArrayPtrTy, UArrayPtrTy:
# the VM always uses 64 bit pointers:
impl uint64
else:
raiseAssert "cannot happen"
raiseAssert "cannot happen: " & $c.m.types[tid].kind
else:
#debug c, c.debug[pc.int]
raiseAssert "cannot happen: " & $c.code[pc].kind
Expand Down Expand Up @@ -986,7 +988,7 @@ proc evalBuiltin(c: Bytecode; pc: CodePos; s: StackFrame; prc: CodePos; didEval:
else: discard
echo "running compilerproc: ", c.m.lit.strings[lit]
didEval = true
of PragmaIdM: discard
of PragmaIdM, AllocLocals: discard
else: break
next c, prc
result = prc
Expand Down Expand Up @@ -1027,10 +1029,10 @@ proc exec(c: Bytecode; pc: CodePos; u: ref Universe) =
for a in sonsFrom1(c, callInstr):
assert c[prc].kind == SummonParamM
let paramAddr = c[prc].operand
assert c[prc.firstSon].kind == ImmediateValM
let paramSize = c[prc.firstSon].operand.int
eval(c, a, s2, s2.locals +! paramAddr, paramSize)
next c, prc
assert c[prc].kind == ImmediateValM
let paramSize = c[prc].operand.int
eval(c, a, s2, s2.locals +! paramAddr, paramSize)
next c, prc
s = s2
pc = prc
Expand All @@ -1046,14 +1048,17 @@ proc exec(c: Bytecode; pc: CodePos; u: ref Universe) =
of ProcDeclM:
next c, pc
else:
raiseAssert "unreachable"
#debug c, c.debug[pc.int]
raiseAssert "unreachable: " & $c.code[pc].kind

proc execCode*(bc: var Bytecode; t: Tree; n: NodePos) =
traverseTypes bc
var c = Preprocessing(u: nil, thisModule: 1'u32)
let start = CodePos(bc.code.len)
var pc = n
while pc.int < t.len:
#echo "RUnning: "
#debug bc, t, pc
preprocess c, bc, t, pc, {}
next t, pc
exec bc, start, nil

0 comments on commit a0ad76b

Please sign in to comment.