Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

so close... #22885

Merged
merged 11 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
so close...
  • Loading branch information
Araq committed Oct 29, 2023
commit 49c1ed26f31101934435bb8a5c574b7ee339ac2c
5 changes: 5 additions & 0 deletions compiler/nir/ast2ir.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,11 @@ proc genParams(c: var ProcCon; params: PNode; prc: PSym) =
let res = resNode.sym # get result symbol
c.code.addSummon toLineInfo(c, res.info), toSymId(c, res),
typeToIr(c.m, res.typ), SummonResult
elif prc.typ.len > 0 and not isEmptyType(prc.typ[0]) and not isCompileTimeOnly(prc.typ[0]):
# happens for procs without bodies:
let t = typeToIr(c.m, prc.typ[0])
let tmp = allocTemp(c, t)
c.code.addSummon toLineInfo(c, params.info), tmp, t, SummonResult

for i in 1..<params.len:
let s = params[i].sym
Expand Down
13 changes: 13 additions & 0 deletions compiler/nir/nirinsts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ proc next*(tree: Tree; pos: var NodePos) {.inline.} = nextChild tree, int(pos)

template firstSon*(n: NodePos): NodePos = NodePos(n.int+1)

template skipTyped*(n: NodePos): NodePos = NodePos(n.int+2)

iterator sons*(tree: Tree; n: NodePos): NodePos =
var pos = n.int
assert tree.nodes[pos].kind > LastAtomicValue
Expand All @@ -259,6 +261,17 @@ iterator sonsFrom1*(tree: Tree; n: NodePos): NodePos =
yield NodePos pos
nextChild tree, pos

iterator sonsFrom2*(tree: Tree; n: NodePos): NodePos =
var pos = n.int
assert tree.nodes[pos].kind > LastAtomicValue
let last = pos + tree.nodes[pos].rawSpan
inc pos
nextChild tree, pos
nextChild tree, pos
while pos < last:
yield NodePos pos
nextChild tree, pos

template `[]`*(t: Tree; n: NodePos): Instr = t.nodes[n.int]

proc span(tree: Tree; pos: int): int {.inline.} =
Expand Down
19 changes: 11 additions & 8 deletions compiler/nir/nirvm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ proc toString*(t: Bytecode; pos: CodePos;
r.add $t.m.lit.numbers[LitId t[pos].operand]
of StrValM:
escapeToNimLit(t.m.lit.strings[LitId t[pos].operand], r)
of LoadLocalM, LoadGlobalM, LoadProcM, AllocLocals:
of LoadLocalM, LoadGlobalM, LoadProcM, AllocLocals, SummonParamM:
r.add $t[pos].kind
r.add ' '
r.add $t[pos].operand
Expand Down Expand Up @@ -593,14 +593,14 @@ proc preprocess(c: var Preprocessing; bc: var Bytecode; t: Tree; n: NodePos; fla
if t[src].kind in {Call, IndirectCall}:
# No support for return values, these are mapped to `var T` parameters!
build bc, info, CallM:
preprocess(c, bc, t, src.firstSon, {WantAddr})
preprocess(c, bc, t, src.skipTyped, {WantAddr})
preprocess(c, bc, t, dest, {WantAddr})
for ch in sonsFrom1(t, src): preprocess(c, bc, t, ch, {WantAddr})
for ch in sonsFrom2(t, src): preprocess(c, bc, t, ch, {WantAddr})
elif t[src].kind in {CheckedCall, CheckedIndirectCall}:
build bc, info, CheckedCallM:
preprocess(c, bc, t, src.firstSon, {WantAddr})
preprocess(c, bc, t, src.skipTyped, {WantAddr})
preprocess(c, bc, t, dest, {WantAddr})
for ch in sonsFrom1(t, src): preprocess(c, bc, t, ch, {WantAddr})
for ch in sonsFrom2(t, src): preprocess(c, bc, t, ch, {WantAddr})
elif t[dest].kind == Load:
let (typ, a) = sons2(t, dest)
let s = computeSize(bc, tid)[0]
Expand Down Expand Up @@ -696,7 +696,7 @@ proc preprocess(c: var Preprocessing; bc: var Bytecode; t: Tree; n: NodePos; fla
for ch in sons(t, n): preprocess(c2, bc, t, ch, {})
bc.code[toPatch] = toIns(AllocLocals, c2.localsAddr)
when false:
if here.int == 40192:
if here.int == 39850:
debug bc, t, n
debug bc, here

Expand Down Expand Up @@ -934,9 +934,12 @@ proc eval(c: Bytecode; pc: CodePos; s: StackFrame; result: pointer; size: int) =
of LoadLocalM:
let dest = s.locals +! c.code[pc].operand
copyMem dest, result, size
of FieldAtM, ArrayAtM, LoadM:
of FieldAtM, ArrayAtM, LoadM, LoadGlobalM:
let dest = evalAddr(c, pc, s)
copyMem dest, result, size
of LoadProcM:
let procAddr = c.code[pc].operand
cast[ptr pointer](result)[] = cast[pointer](procAddr)
of CheckedAddM: checkedBinop `+`
of CheckedSubM: checkedBinop `-`
of CheckedMulM: checkedBinop `*`
Expand Down Expand Up @@ -1082,8 +1085,8 @@ proc exec(c: Bytecode; pc: CodePos; u: ref Universe) =
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
eval(c, a, s2, s2.locals +! paramAddr, paramSize)
s = s2
pc = prc
of RetM:
Expand Down