Skip to content

Commit e3acb2d

Browse files
authored
Cleanup DFA (#13173)
1 parent d245d4c commit e3acb2d

File tree

2 files changed

+16
-36
lines changed

2 files changed

+16
-36
lines changed

compiler/dfa.nim

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@ type
3737
InstrKind* = enum
3838
goto, fork, join, def, use
3939
Instr* = object
40-
n*: PNode
40+
n*: PNode # contains the def/use location.
4141
case kind*: InstrKind
42-
of def, use: sym*: PSym # 'sym' can also be 'nil' and
43-
# then 'n' contains the def/use location.
44-
# This is used so that we can track object
45-
# and tuple field accesses precisely.
4642
of goto, fork, join: dest*: int
43+
else: discard
4744

4845
ControlFlowGraph* = seq[Instr]
4946

@@ -595,7 +592,7 @@ proc genUse(c: var Con; orig: PNode) =
595592
n = n[1]
596593
else: break
597594
if n.kind == nkSym and n.sym.kind in InterestingSyms:
598-
c.code.add Instr(n: orig, kind: use, sym: if orig != n: nil else: n.sym)
595+
c.code.add Instr(n: orig, kind: use)
599596

600597
proc aliases*(obj, field: PNode): bool =
601598
var n = field
@@ -605,41 +602,24 @@ proc aliases*(obj, field: PNode): bool =
605602
while true:
606603
if sameTrees(obj, n): return true
607604
case n.kind
608-
of nkDotExpr, nkCheckedFieldExpr, nkHiddenSubConv, nkHiddenStdConv,
609-
nkObjDownConv, nkObjUpConv, nkHiddenAddr, nkAddr, nkBracketExpr,
610-
nkHiddenDeref, nkDerefExpr:
605+
of PathKinds0, PathKinds1:
611606
n = n[0]
612607
else:
613608
break
614-
return false
615609

616610
proc useInstrTargets*(ins: Instr; loc: PNode): bool =
617611
assert ins.kind == use
618-
if ins.sym != nil and loc.kind == nkSym:
619-
result = ins.sym == loc.sym
620-
else:
621-
result = ins.n == loc or sameTrees(ins.n, loc)
622-
if not result:
623-
# We can come here if loc is 'x.f' and ins.n is 'x' or the other way round.
624-
# def x.f; question: does it affect the full 'x'? No.
625-
# def x; question: does it affect the 'x.f'? Yes.
626-
# use x.f; question: does it affect the full 'x'? No.
627-
# use x; question does it affect 'x.f'? Yes.
628-
result = aliases(ins.n, loc) or aliases(loc, ins.n)
612+
sameTrees(ins.n, loc) or
613+
ins.n.aliases(loc) or loc.aliases(ins.n) # We can come here if loc is 'x.f' and ins.n is 'x' or the other way round.
614+
# use x.f; question: does it affect the full 'x'? No.
615+
# use x; question does it affect 'x.f'? Yes.
629616

630617
proc defInstrTargets*(ins: Instr; loc: PNode): bool =
631618
assert ins.kind == def
632-
if ins.sym != nil and loc.kind == nkSym:
633-
result = ins.sym == loc.sym
634-
else:
635-
result = ins.n == loc or sameTrees(ins.n, loc)
636-
if not result:
637-
# We can come here if loc is 'x.f' and ins.n is 'x' or the other way round.
638-
# def x.f; question: does it affect the full 'x'? No.
639-
# def x; question: does it affect the 'x.f'? Yes.
640-
# use x.f; question: does it affect the full 'x'? No.
641-
# use x; question does it affect 'x.f'? Yes.
642-
result = aliases(ins.n, loc)
619+
sameTrees(ins.n, loc) or
620+
ins.n.aliases(loc) # We can come here if loc is 'x.f' and ins.n is 'x' or the other way round.
621+
# def x.f; question: does it affect the full 'x'? No.
622+
# def x; question: does it affect the 'x.f'? Yes.
643623

644624
proc isAnalysableFieldAccess*(orig: PNode; owner: PSym): bool =
645625
var n = orig
@@ -695,9 +675,9 @@ proc genDef(c: var Con; n: PNode) =
695675
break
696676

697677
if n.kind == nkSym and n.sym.kind in InterestingSyms:
698-
c.code.add Instr(n: n, kind: def, sym: n.sym)
678+
c.code.add Instr(n: n, kind: def)
699679
elif isAnalysableFieldAccess(n, c.owner):
700-
c.code.add Instr(n: n, kind: def, sym: nil)
680+
c.code.add Instr(n: n, kind: def)
701681

702682
proc genCall(c: var Con; n: PNode) =
703683
gen(c, n[0])

compiler/injectdestructors.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@ proc initialized(code: ControlFlowGraph; pc: int,
168168
if comesFrom == target: return pc
169169
inc pc
170170
of use:
171-
let v = code[pc].sym
171+
let v = code[pc].n.sym
172172
if v.kind != skParam and v.id notin init:
173173
# attempt to read an uninit'ed variable
174174
uninit.incl v.id
175175
inc pc
176176
of def:
177-
let v = code[pc].sym
177+
let v = code[pc].n.sym
178178
init.incl v.id
179179
inc pc
180180
return pc

0 commit comments

Comments
 (0)