Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq committed Oct 29, 2023
1 parent a0ad76b commit 8959603
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion compiler/nir/nirinsts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ proc addNilVal*(t: var Tree; info: PackedLineInfo; typ: TypeId) =
proc store*(r: var RodFile; t: Tree) = storeSeq r, t.nodes
proc load*(r: var RodFile; t: var Tree) = loadSeq r, t.nodes

proc escapeToNimLit(s: string; result: var string) =
proc escapeToNimLit*(s: string; result: var string) =
result.add '"'
for c in items s:
if c < ' ' or int(c) >= 128:
Expand Down
58 changes: 54 additions & 4 deletions compiler/nir/nirvm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type
TypedM, # with type ID
PragmaIdM, # with Pragma ID, possible values: see PragmaKey enum
NilValM,
AllocLocals,
SummonParamM,
GotoM,
CheckedGotoM, # last atom

Expand All @@ -43,8 +45,6 @@ type
SelectListM, # (values...)
SelectValueM, # (value)
SelectRangeM, # (valueA..valueB)
AllocLocals,
SummonParamM,

AddrOfM,
ArrayAtM, # (elemSize, addr(a), i)
Expand Down Expand Up @@ -312,6 +312,50 @@ iterator triples*(bc: Bytecode; n: CodePos): (uint32, int, CodePos) =
yield (offset, size, val)
nextChild bc, pos

proc toString*(t: Bytecode; pos: CodePos;
r: var string; nesting = 0) =
if r.len > 0 and r[r.len-1] notin {' ', '\n', '(', '[', '{'}:
r.add ' '

case t[pos].kind
of ImmediateValM:
r.add $t[pos].operand
of IntValM:
r.add "IntVal "
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:
r.add $t[pos].kind
r.add ' '
r.add $t[pos].operand
of PragmaIdM:
r.add $cast[PragmaKey](t[pos].operand)
of TypedM:
r.add "T<"
r.add $t[pos].operand
r.add ">"
of NilValM:
r.add "NilVal"
of GotoM, CheckedGotoM:
r.add $t[pos].kind
r.add " L"
r.add $t[pos].operand
else:
r.add $t[pos].kind
r.add "{\n"
for i in 0..<(nesting+1)*2: r.add ' '
for p in sons(t, pos):
toString t, p, r, nesting+1
r.add "\n"
for i in 0..<nesting*2: r.add ' '
r.add "}"

proc debug(b: Bytecode; pos: CodePos) =
var buf = ""
toString(b, pos, buf)
echo buf

type
Preprocessing = object
u: ref Universe
Expand Down Expand Up @@ -549,8 +593,9 @@ 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, dest, {WantAddr})
for ch in sons(t, src): preprocess(c, bc, t, ch, {WantAddr})
for ch in sonsFrom1(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})
Expand Down Expand Up @@ -650,6 +695,11 @@ proc preprocess(c: var Preprocessing; bc: var Bytecode; t: Tree; n: NodePos; fla
bc.add info, AllocLocals, 0'u32
for ch in sons(t, n): preprocess(c2, bc, t, ch, {})
bc.code[toPatch] = toIns(AllocLocals, c2.localsAddr)
when false:
if here.int == 40192:
debug bc, t, n
debug bc, here

of PragmaPair:
recurse PragmaPairM

Expand Down Expand Up @@ -745,7 +795,7 @@ proc evalAddr(c: Bytecode; pc: CodePos; s: StackFrame): pointer =
proc `div`(x, y: float32): float32 {.inline.} = x / y
proc `div`(x, y: float64): float64 {.inline.} = x / y

from math import `mod`
from std / math import `mod`

template binop(opr) {.dirty.} =
template impl(typ) {.dirty.} =
Expand Down

0 comments on commit 8959603

Please sign in to comment.