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
Prev Previous commit
Next Next commit
progress
  • Loading branch information
Araq committed Oct 31, 2023
commit e46fef30dc43b746ac187eedec98f5beef0b5077
16 changes: 8 additions & 8 deletions compiler/nir/ast2ir.nim
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ proc xjmp(c: var ProcCon; n: PNode; jk: JmpKind; v: Value): LabelId =
c.code.copyTree Tree(v)
build c.code, info, SelectPair:
build c.code, info, SelectValue:
c.code.boolVal(info, jk == opcTJmp)
c.code.boolVal(c.lit.numbers, info, jk == opcTJmp)
c.code.gotoLabel info, Goto, result

proc patch(c: var ProcCon; n: PNode; L: LabelId) =
Expand Down Expand Up @@ -361,7 +361,7 @@ template buildCond(useNegation: bool; cond: typed; body: untyped) =
c.code.copyTree cond
build c.code, info, SelectPair:
build c.code, info, SelectValue:
c.code.boolVal(info, useNegation)
c.code.boolVal(c.lit.numbers, info, useNegation)
c.code.gotoLabel info, Goto, lab

body
Expand All @@ -380,7 +380,7 @@ template buildIfThenElse(cond: typed; then, otherwise: untyped) =
c.code.copyTree cond
build c.code, info, SelectPair:
build c.code, info, SelectValue:
c.code.boolVal(info, false)
c.code.boolVal(c.lit.numbers, info, false)
c.code.gotoLabel info, Goto, lelse

then()
Expand Down Expand Up @@ -969,7 +969,7 @@ proc genInSet(c: var ProcCon; n: PNode; d: var Value) =
if ex == nil:
let info = toLineInfo(c, n.info)
template body(target) =
boolVal target, info, false
boolVal target, c.lit.numbers, info, false
intoDest d, info, Bool8Id, body
else:
gen c, ex, d
Expand Down Expand Up @@ -1057,7 +1057,7 @@ proc beginCountLoop(c: var ProcCon; info: PackedLineInfo; first, last: int): (Sy
c.code.addIntVal c.lit.numbers, info, c.m.nativeIntId, last
build c.code, info, SelectPair:
build c.code, info, SelectValue:
c.code.boolVal(info, false)
c.code.boolVal(c.lit.numbers, info, false)
c.code.gotoLabel info, Goto, result[2]

proc beginCountLoop(c: var ProcCon; info: PackedLineInfo; first, last: Value): (SymId, LabelId, LabelId) =
Expand All @@ -1075,7 +1075,7 @@ proc beginCountLoop(c: var ProcCon; info: PackedLineInfo; first, last: Value): (
copyTree c.code, last
build c.code, info, SelectPair:
build c.code, info, SelectValue:
c.code.boolVal(info, false)
c.code.boolVal(c.lit.numbers, info, false)
c.code.gotoLabel info, Goto, result[2]

proc endLoop(c: var ProcCon; info: PackedLineInfo; s: SymId; back, exit: LabelId) =
Expand Down Expand Up @@ -1121,7 +1121,7 @@ proc genLeSet(c: var ProcCon; n: PNode; d: var Value) =
c.code.copyTree d
build c.code, info, SelectPair:
build c.code, info, SelectValue:
c.code.boolVal(info, false)
c.code.boolVal(c.lit.numbers, info, false)
c.code.gotoLabel info, Goto, endLabel

endLoop(c, info, idx, backLabel, endLabel)
Expand Down Expand Up @@ -1534,7 +1534,7 @@ proc genMove(c: var ProcCon; n: PNode; d: var Value) =

build c.code, info, SelectPair:
build c.code, info, SelectValue:
c.code.boolVal(info, true)
c.code.boolVal(c.lit.numbers, info, true)
c.code.gotoLabel info, Goto, lab1

gen(c, n[3])
Expand Down
7 changes: 4 additions & 3 deletions compiler/nir/nirinsts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,6 @@ proc addNewLabel*(t: var Tree; labelGen: var int; info: PackedLineInfo; k: Opcod
t.nodes.add Instr(x: toX(k, uint32(result)), info: info)
inc labelGen

proc boolVal*(t: var Tree; info: PackedLineInfo; b: bool) =
t.nodes.add Instr(x: toX(ImmediateVal, uint32(b)), info: info)

proc gotoLabel*(t: var Tree; info: PackedLineInfo; k: Opcode; L: LabelId) =
assert k in {Goto, GotoLoop, CheckedGoto}
t.nodes.add Instr(x: toX(k, uint32(L)), info: info)
Expand Down Expand Up @@ -384,6 +381,10 @@ proc addIntVal*(t: var Tree; integers: var BiTable[int64]; info: PackedLineInfo;
buildTyped t, info, NumberConv, typ:
t.nodes.add Instr(x: toX(IntVal, uint32(integers.getOrIncl(x))), info: info)

proc boolVal*(t: var Tree; integers: var BiTable[int64]; info: PackedLineInfo; b: bool) =
buildTyped t, info, NumberConv, Bool8Id:
t.nodes.add Instr(x: toX(IntVal, uint32(integers.getOrIncl(ord b))), info: info)

proc addStrVal*(t: var Tree; strings: var BiTable[string]; info: PackedLineInfo; s: string) =
t.nodes.add Instr(x: toX(StrVal, uint32(strings.getOrIncl(s))), info: info)

Expand Down
54 changes: 32 additions & 22 deletions compiler/nir/nirvm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -814,10 +814,6 @@ proc evalAddr(c: Bytecode; pc: CodePos; s: StackFrame): pointer =
var idx: int = 0
eval(c, i, s, addr idx, sizeof(int))
result = cast[ptr pointer](p)[] +! (uint32(idx) * elemSize)
of LoadM:
let (_, arg) = sons2(c.code, pc)
let p = evalAddr(c, arg, s)
result = cast[ptr pointer](p)[]
of LoadGlobalM:
result = c.globalData +! c.code[pc].operand
else:
Expand Down Expand Up @@ -928,23 +924,29 @@ proc evalSelect(c: Bytecode; pc: CodePos; s: StackFrame): CodePos =
for pair in sonsFrom2(c, pc):
assert c.code[pair].kind == SelectPairM
let (values, action) = sons2(c.code, pair)
assert c.code[values].kind == SelectListM
for v in sons(c, values):
case c.code[v].kind
of SelectValueM:
var a = default(typ)
eval c, v.firstSon, s, addr a, sizeof(typ)
if selector == a:
return CodePos c.code[action].operand
of SelectRangeM:
let (va, vb) = sons2(c.code, v)
var a = default(typ)
eval c, va, s, addr a, sizeof(typ)
var b = default(typ)
eval c, vb, s, addr a, sizeof(typ)
if a <= selector and selector <= b:
return CodePos c.code[action].operand
else: raiseAssert "unreachable"
if c.code[values].kind == SelectValueM:
var a = default(typ)
eval c, values.firstSon, s, addr a, sizeof(typ)
if selector == a:
return CodePos c.code[action].operand
else:
assert c.code[values].kind == SelectListM, $c.code[values].kind
for v in sons(c, values):
case c.code[v].kind
of SelectValueM:
var a = default(typ)
eval c, v.firstSon, s, addr a, sizeof(typ)
if selector == a:
return CodePos c.code[action].operand
of SelectRangeM:
let (va, vb) = sons2(c.code, v)
var a = default(typ)
eval c, va, s, addr a, sizeof(typ)
var b = default(typ)
eval c, vb, s, addr a, sizeof(typ)
if a <= selector and selector <= b:
return CodePos c.code[action].operand
else: raiseAssert "unreachable"
result = CodePos(-1)

let (t, sel) = sons2(c.code, pc)
Expand All @@ -965,12 +967,16 @@ proc eval(c: Bytecode; pc: CodePos; s: StackFrame; result: pointer; size: int) =
of LoadLocalM:
let src = s.locals +! c.code[pc].operand
copyMem result, src, size
of FieldAtM, DerefFieldAtM, ArrayAtM, DerefArrayAtM, LoadM, LoadGlobalM:
of FieldAtM, DerefFieldAtM, ArrayAtM, DerefArrayAtM, LoadGlobalM:
let src = evalAddr(c, pc, s)
copyMem result, src, size
of LoadProcM:
let procAddr = c.code[pc].operand
cast[ptr pointer](result)[] = cast[pointer](procAddr)
of LoadM:
let (_, arg) = sons2(c.code, pc)
let src = evalAddr(c, arg, s)
copyMem result, src, size
of CheckedAddM: checkedBinop `+`
of CheckedSubM: checkedBinop `-`
of CheckedMulM: checkedBinop `*`
Expand Down Expand Up @@ -1090,6 +1096,10 @@ proc exec(c: Bytecode; pc: CodePos; u: ref Universe) =
var pc = pc
var frame = StackFrame(u: u)
while pc.int < c.code.len:
when false: # c.interactive:
echo "running: "
debug c, pc

case c.code[pc].kind
of GotoM:
pc = CodePos(c.code[pc].operand)
Expand Down