Skip to content

Commit f608e10

Browse files
Araqdemotomohiroringaboutarnetheduck
authored
massive refactoring for IC (#25282)
TODO: - [ ] test writing of .nif files - [x] implement loading of fields in PType/PSym that might not have been loaded - [ ] implement interface logic - [ ] implement pragma "replays" - [ ] implement special logic for `converter` - [ ] implement special logic for `method` - [ ] test the logic holds up for `export` - [ ] implement logic to free the memory of PSym/PType if memory pressure is high - [ ] implement logic to close memory mapped files if too many are open. --------- Co-authored-by: demotomohiro <gpuppur@gmail.com> Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com> Co-authored-by: Jacek Sieka <arnetheduck@gmail.com>
1 parent 4c6d9b6 commit f608e10

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+5226
-1628
lines changed

compiler/ast.nim

Lines changed: 537 additions & 1101 deletions
Large diffs are not rendered by default.

compiler/ast2nif.nim

Lines changed: 917 additions & 0 deletions
Large diffs are not rendered by default.

compiler/astdef.nim

Lines changed: 1033 additions & 0 deletions
Large diffs are not rendered by default.

compiler/ccgcalls.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ proc genArg(p: BProc, n: PNode, param: PSym; call: PNode; result: var Builder; n
369369
let needsIndirect = mapType(p.config, n[0].typ, mapTypeChooser(n[0]) == skParam) != ctArray
370370
if needsIndirect:
371371
n.typ() = n.typ.exactReplica
372-
n.typ.flags.incl tfVarIsPtr
372+
n.typ.incl tfVarIsPtr
373373
a = initLocExprSingleUse(p, n)
374374
a = withTmpIfNeeded(p, a, needsTmp)
375375
if needsIndirect: a.flags.incl lfIndirect

compiler/ccgexprs.nim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,8 +3363,9 @@ proc genConstSetup(p: BProc; sym: PSym): bool =
33633363
useHeader(m, sym)
33643364
if sym.loc.k == locNone:
33653365
fillBackendName(p.module, sym)
3366-
fillLoc(sym.loc, locData, sym.astdef, OnStatic)
3367-
if m.hcrOn: incl(sym.loc.flags, lfIndirect)
3366+
ensureMutable sym
3367+
fillLoc(sym.locImpl, locData, sym.astdef, OnStatic)
3368+
if m.hcrOn: incl(sym, lfIndirect)
33683369
result = lfNoDecl notin sym.loc.flags
33693370

33703371
proc genConstHeader(m, q: BModule; p: BProc, sym: PSym) =

compiler/ccgstmts.nim

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ proc genVarTuple(p: BProc, n: PNode) =
126126
let vn = n[i]
127127
let v = vn.sym
128128
if sfCompileTime in v.flags: continue
129+
ensureMutable v
129130
if sfGlobal in v.flags:
130131
assignGlobalVar(p, vn, "")
131-
genObjectInit(p, cpsInit, v.typ, v.loc, constructObj)
132+
genObjectInit(p, cpsInit, v.typ, v.locImpl, constructObj)
132133
registerTraverseProc(p, v)
133134
else:
134135
assignLocalVar(p, vn)
@@ -142,9 +143,9 @@ proc genVarTuple(p: BProc, n: PNode) =
142143
if t.n[i].kind != nkSym: internalError(p.config, n.info, "genVarTuple")
143144
mangleRecFieldName(p.module, t.n[i].sym)
144145
field.snippet = dotField(rtup, fieldName)
145-
putLocIntoDest(p, v.loc, field)
146+
putLocIntoDest(p, v.locImpl, field)
146147
if forHcr or isGlobalInBlock:
147-
hcrGlobals.add((loc: v.loc, tp: CNil))
148+
hcrGlobals.add((loc: v.locImpl, tp: CNil))
148149

149150
if forHcr:
150151
# end the block where the tuple gets initialized
@@ -460,7 +461,8 @@ proc genSingleVar(p: BProc, v: PSym; vn, value: PNode) =
460461
if value.kind != nkEmpty and valueAsRope.len == 0:
461462
genLineDir(targetProc, vn)
462463
if not isCppCtorCall:
463-
loadInto(targetProc, vn, value, v.loc)
464+
ensureMutable v
465+
loadInto(targetProc, vn, value, v.locImpl)
464466
if forHcr:
465467
endBlockWith(targetProc):
466468
finishBranch(p.s(cpsStmts), hcrInit)
@@ -736,7 +738,8 @@ proc genBlock(p: BProc, n: PNode, d: var TLoc) =
736738
# named block?
737739
assert(n[0].kind == nkSym)
738740
var sym = n[0].sym
739-
sym.loc.k = locOther
741+
ensureMutable sym
742+
sym.locImpl.k = locOther
740743
sym.position = p.breakIdx+1
741744
expr(p, n[1], d)
742745
endSimpleBlock(p, scope)
@@ -1250,7 +1253,8 @@ proc genTryCpp(p: BProc, t: PNode, d: var TLoc) =
12501253
initElifBranch(p.s(cpsStmts), ifStmt, orExpr)
12511254
if exvar != nil:
12521255
fillLocalName(p, exvar.sym)
1253-
fillLoc(exvar.sym.loc, locTemp, exvar, OnStack)
1256+
ensureMutable exvar.sym
1257+
fillLoc(exvar.sym.locImpl, locTemp, exvar, OnStack)
12541258
linefmt(p, cpsStmts, "$1 $2 = T$3_;$n", [getTypeDesc(p.module, exvar.sym.typ),
12551259
rdLoc(exvar.sym.loc), rope(etmp+1)])
12561260
# we handled the error:
@@ -1298,7 +1302,8 @@ proc genTryCpp(p: BProc, t: PNode, d: var TLoc) =
12981302
if isImportedException(typeNode.typ, p.config):
12991303
let exvar = t[i][j][2] # ex1 in `except ExceptType as ex1:`
13001304
fillLocalName(p, exvar.sym)
1301-
fillLoc(exvar.sym.loc, locTemp, exvar, OnStack)
1305+
ensureMutable exvar.sym
1306+
fillLoc(exvar.sym.locImpl, locTemp, exvar, OnStack)
13021307
startBlockWith(p):
13031308
lineCg(p, cpsStmts, "catch ($1& $2) {$n", [getTypeDesc(p.module, typeNode.typ), rdLoc(exvar.sym.loc)])
13041309
genExceptBranchBody(t[i][^1]) # exception handler body will duplicated for every type
@@ -1389,7 +1394,8 @@ proc genTryCppOld(p: BProc, t: PNode, d: var TLoc) =
13891394
if t[i][j].isInfixAs():
13901395
let exvar = t[i][j][2] # ex1 in `except ExceptType as ex1:`
13911396
fillLocalName(p, exvar.sym)
1392-
fillLoc(exvar.sym.loc, locTemp, exvar, OnUnknown)
1397+
ensureMutable exvar.sym
1398+
fillLoc(exvar.sym.locImpl, locTemp, exvar, OnUnknown)
13931399
startBlockWith(p):
13941400
lineCg(p, cpsStmts, "catch ($1& $2) {$n", [getTypeDesc(p.module, t[i][j][1].typ), rdLoc(exvar.sym.loc)])
13951401
else:

compiler/ccgtypes.nim

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ proc fillBackendName(m: BModule; s: PSym) =
8484
if m.hcrOn:
8585
result.add '_'
8686
result.add(idOrSig(s, m.module.name.s.mangle, m.sigConflicts, m.config))
87-
s.loc.snippet = result
87+
ensureMutable s
88+
s.locImpl.snippet = result
8889

8990
proc fillParamName(m: BModule; s: PSym) =
9091
if s.loc.snippet == "":
@@ -107,7 +108,8 @@ proc fillParamName(m: BModule; s: PSym) =
107108
# and a function called in main or proxy uses `socket` as a parameter name.
108109
# That would lead to either needing to reload `proxy` or to overwrite the
109110
# executable file for the main module, which is running (or both!) -> error.
110-
s.loc.snippet = res.rope
111+
ensureMutable s
112+
s.locImpl.snippet = res.rope
111113

112114
proc fillLocalName(p: BProc; s: PSym) =
113115
assert s.kind in skLocalVars+{skTemp}
@@ -122,7 +124,8 @@ proc fillLocalName(p: BProc; s: PSym) =
122124
elif s.kind != skResult:
123125
result.add "_" & rope(counter+1)
124126
p.sigConflicts.inc(key)
125-
s.loc.snippet = result
127+
ensureMutable s
128+
s.locImpl.snippet = result
126129

127130
proc scopeMangledParam(p: BProc; param: PSym) =
128131
## parameter generation only takes BModule, not a BProc, so we have to
@@ -155,9 +158,10 @@ proc getTypeName(m: BModule; typ: PType; sig: SigHash): Rope =
155158
else:
156159
break
157160
let typ = if typ.kind in {tyAlias, tySink, tyOwned}: typ.elementType else: typ
161+
ensureMutable typ
158162
if typ.loc.snippet == "":
159-
typ.typeName(typ.loc.snippet)
160-
typ.loc.snippet.add $sig
163+
typ.typeName(typ.locImpl.snippet)
164+
typ.locImpl.snippet.add $sig
161165
else:
162166
when defined(debugSigHashes):
163167
# check consistency:
@@ -300,12 +304,13 @@ proc addAbiCheck(m: BModule; t: PType, name: Rope) =
300304

301305

302306
proc fillResult(conf: ConfigRef; param: PNode, proctype: PType) =
303-
fillLoc(param.sym.loc, locParam, param, "Result",
307+
ensureMutable param.sym
308+
fillLoc(param.sym.locImpl, locParam, param, "Result",
304309
OnStack)
305310
let t = param.sym.typ
306311
if mapReturnType(conf, t) != ctArray and isInvalidReturnType(conf, proctype):
307-
incl(param.sym.loc.flags, lfIndirect)
308-
param.sym.loc.storage = OnUnknown
312+
incl(param.sym.locImpl.flags, lfIndirect)
313+
param.sym.locImpl.storage = OnUnknown
309314

310315
proc typeNameOrLiteral(m: BModule; t: PType, literal: string): Rope =
311316
if t.sym != nil and sfImportc in t.sym.flags and t.sym.magic == mNone:
@@ -524,14 +529,15 @@ proc genMemberProcParams(m: BModule; prc: PSym, superCall, rettype, name, params
524529
var types, names, args: seq[string] = @[]
525530
if not isCtor:
526531
var this = t.n[1].sym
532+
ensureMutable this
527533
fillParamName(m, this)
528-
fillLoc(this.loc, locParam, t.n[1],
534+
fillLoc(this.locImpl, locParam, t.n[1],
529535
this.paramStorageLoc)
530536
if this.typ.kind == tyPtr:
531-
this.loc.snippet = "this"
537+
this.locImpl.snippet = "this"
532538
else:
533-
this.loc.snippet = "(*this)"
534-
names.add this.loc.snippet
539+
this.locImpl.snippet = "(*this)"
540+
names.add this.locImpl.snippet
535541
types.add getTypeDescWeak(m, this.typ, check, dkParam)
536542

537543
let firstParam = if isCtor: 1 else: 2
@@ -545,21 +551,22 @@ proc genMemberProcParams(m: BModule; prc: PSym, superCall, rettype, name, params
545551
else:
546552
descKind = dkRefParam
547553
var typ, name: string
554+
ensureMutable param
548555
fillParamName(m, param)
549-
fillLoc(param.loc, locParam, t.n[i],
556+
fillLoc(param.locImpl, locParam, t.n[i],
550557
param.paramStorageLoc)
551558
if ccgIntroducedPtr(m.config, param, t.returnType) and descKind == dkParam:
552559
typ = getTypeDescWeak(m, param.typ, check, descKind) & "*"
553-
incl(param.loc.flags, lfIndirect)
554-
param.loc.storage = OnUnknown
560+
incl(param.locImpl.flags, lfIndirect)
561+
param.locImpl.storage = OnUnknown
555562
elif weakDep:
556563
typ = getTypeDescWeak(m, param.typ, check, descKind)
557564
else:
558565
typ = getTypeDescAux(m, param.typ, check, descKind)
559566
if sfNoalias in param.flags:
560567
typ.add("NIM_NOALIAS ")
561568

562-
name = param.loc.snippet
569+
name = param.locImpl.snippet
563570
types.add typ
564571
names.add name
565572
if sfCodegenDecl notin param.flags:
@@ -601,14 +608,15 @@ proc genProcParams(m: BModule; t: PType, rettype: var Rope, params: var Builder,
601608
else:
602609
descKind = dkRefParam
603610
if isCompileTimeOnly(param.typ): continue
611+
ensureMutable param
604612
fillParamName(m, param)
605-
fillLoc(param.loc, locParam, t.n[i],
613+
fillLoc(param.locImpl, locParam, t.n[i],
606614
param.paramStorageLoc)
607615
var typ: Rope
608616
if ccgIntroducedPtr(m.config, param, t.returnType) and descKind == dkParam:
609617
typ = ptrType(getTypeDescWeak(m, param.typ, check, descKind))
610-
incl(param.loc.flags, lfIndirect)
611-
param.loc.storage = OnUnknown
618+
incl(param.locImpl.flags, lfIndirect)
619+
param.locImpl.storage = OnUnknown
612620
elif weakDep:
613621
typ = (getTypeDescWeak(m, param.typ, check, descKind))
614622
else:
@@ -620,9 +628,9 @@ proc genProcParams(m: BModule; t: PType, rettype: var Rope, params: var Builder,
620628
var j = 0
621629
while arr.kind in {tyOpenArray, tyVarargs}:
622630
# this fixes the 'sort' bug:
623-
if param.typ.kind in {tyVar, tyLent}: param.loc.storage = OnUnknown
631+
if param.typ.kind in {tyVar, tyLent}: param.locImpl.storage = OnUnknown
624632
# need to pass hidden parameter:
625-
params.addParam(paramBuilder, name = param.loc.snippet & "Len_" & $j, typ = NimInt)
633+
params.addParam(paramBuilder, name = param.locImpl.snippet & "Len_" & $j, typ = NimInt)
626634
inc(j)
627635
arr = arr[0].skipTypes({tySink})
628636
if t.returnType != nil and isInvalidReturnType(m.config, t):
@@ -707,7 +715,8 @@ proc genRecordFieldsAux(m: BModule; n: PNode,
707715
if field.typ.kind == tyVoid: return
708716
#assert(field.ast == nil)
709717
let sname = mangleRecFieldName(m, field)
710-
fillLoc(field.loc, locField, n, unionPrefix & sname, OnUnknown)
718+
ensureMutable field
719+
fillLoc(field.locImpl, locField, n, unionPrefix & sname, OnUnknown)
711720
# for importcpp'ed objects, we only need to set field.loc, but don't
712721
# have to recurse via 'getTypeDescAux'. And not doing so prevents problems
713722
# with heavily templatized C++ code:
@@ -1155,7 +1164,8 @@ proc genMemberProcHeader(m: BModule; prc: PSym; result: var Builder; asPtr: bool
11551164
let isCtor = sfConstructor in prc.flags
11561165
var check = initIntSet()
11571166
fillBackendName(m, prc)
1158-
fillLoc(prc.loc, locProc, prc.ast[namePos], OnUnknown)
1167+
ensureMutable prc
1168+
fillLoc(prc.locImpl, locProc, prc.ast[namePos], OnUnknown)
11591169
var memberOp = "#." #only virtual
11601170
var typ: PType
11611171
if isCtor:
@@ -1187,7 +1197,7 @@ proc genMemberProcHeader(m: BModule; prc: PSym; result: var Builder; asPtr: bool
11871197
superCall = ""
11881198
else:
11891199
if not isCtor:
1190-
prc.loc.snippet = "$1$2(@)" % [memberOp, name]
1200+
prc.locImpl.snippet = "$1$2(@)" % [memberOp, name]
11911201
elif superCall != "":
11921202
superCall = " : " & superCall
11931203

@@ -1202,14 +1212,15 @@ proc genProcHeader(m: BModule; prc: PSym; result: var Builder; visibility: var D
12021212
# using static is needed for inline procs
12031213
var check = initIntSet()
12041214
fillBackendName(m, prc)
1205-
fillLoc(prc.loc, locProc, prc.ast[namePos], OnUnknown)
1215+
ensureMutable prc
1216+
fillLoc(prc.locImpl, locProc, prc.ast[namePos], OnUnknown)
12061217
var rettype: Snippet = ""
12071218
var desc = newBuilder("")
12081219
genProcParams(m, prc.typ, rettype, desc, check, true, false)
12091220
let params = extract(desc)
12101221
# handle the 2 options for hotcodereloading codegen - function pointer
12111222
# (instead of forward declaration) or header for function body with "_actual" postfix
1212-
var name = prc.loc.snippet
1223+
var name = prc.locImpl.snippet
12131224
if not asPtr and isReloadable(m, prc):
12141225
name.add("_actual")
12151226
# careful here! don't access ``prc.ast`` as that could reload large parts of
@@ -1449,7 +1460,7 @@ proc genObjectInfo(m: BModule; typ, origType: PType, name: Rope; info: TLineInfo
14491460
var t = typ.baseClass
14501461
while t != nil:
14511462
t = t.skipTypes(skipPtrs)
1452-
t.flags.incl tfObjHasKids
1463+
t.incl tfObjHasKids
14531464
t = t.baseClass
14541465

14551466
proc genTupleInfo(m: BModule; typ, origType: PType, name: Rope; info: TLineInfo) =
@@ -1645,8 +1656,8 @@ proc generateRttiDestructor(g: ModuleGraph; typ: PType; owner: PSym; kind: TType
16451656
n[bodyPos] = body
16461657
result.ast = n
16471658

1648-
incl result.flags, sfFromGeneric
1649-
incl result.flags, sfGeneratedOp
1659+
incl result.flagsImpl, sfFromGeneric
1660+
incl result.flagsImpl, sfGeneratedOp
16501661

16511662
proc genHook(m: BModule; t: PType; info: TLineInfo; op: TTypeAttachedOp; result: var Builder) =
16521663
let theProc = getAttachedOp(m.g.graph, t, op)

0 commit comments

Comments
 (0)