explicit default values for a leaner codegen, .cursor variables for optimization
This commit is contained in:
parent
2b5e48d807
commit
f8bf24623c
7 changed files with 172 additions and 46 deletions
|
|
@ -686,42 +686,3 @@ proc constructCfg*(s: PSym; body: PNode): ControlFlowGraph =
|
|||
gen(c, body)
|
||||
genImplicitReturn(c)
|
||||
shallowCopy(result, c.code)
|
||||
|
||||
proc interpret(code: ControlFlowGraph; pc: int, state: seq[PSym], comesFrom: int; threadId: int): (seq[PSym], int) =
|
||||
var res = state
|
||||
var pc = pc
|
||||
while pc < code.len:
|
||||
#echo threadId, " ", code[pc].kind
|
||||
case code[pc].kind
|
||||
of goto:
|
||||
pc = pc + code[pc].dest
|
||||
of fork:
|
||||
let target = pc + code[pc].dest
|
||||
let (branchA, pcA) = interpret(code, pc+1, res, pc, threadId+1)
|
||||
let (branchB, _) = interpret(code, target, res, pc, threadId+2)
|
||||
# we add vars if they are in both branches:
|
||||
for v in branchB:
|
||||
if v in branchA:
|
||||
if v notin res:
|
||||
res.add v
|
||||
pc = pcA+1
|
||||
of join:
|
||||
let target = pc + code[pc].dest
|
||||
if comesFrom == target: return (res, pc)
|
||||
inc pc
|
||||
of use:
|
||||
let v = code[pc].sym
|
||||
if v notin res and v.kind != skParam:
|
||||
echo "attempt to read uninitialized variable ", v.name.s
|
||||
inc pc
|
||||
of def:
|
||||
let v = code[pc].sym
|
||||
if v notin res:
|
||||
res.add v
|
||||
inc pc
|
||||
return (res, pc)
|
||||
|
||||
proc dataflowAnalysis*(s: PSym; body: PNode) =
|
||||
let c = constructCfg(s, body)
|
||||
#echoCfg c
|
||||
discard interpret(c, 0, @[], -1, 1)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue