Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
e9127b2
adds Nim's AST to/from NIF
demotomohiro Oct 23, 2025
25ca7cc
encodes/decodes itemId.module
demotomohiro Oct 27, 2025
5eeede3
uses PSym.item and PType.item as id in NIF
demotomohiro Oct 27, 2025
51eccea
saves/loads PSym guard, bitsize and alignment
demotomohiro Oct 28, 2025
8a4d921
saves/loads PNode and PSym line info
demotomohiro Oct 29, 2025
6f237be
saves/loads symbol magic, options and offset
demotomohiro Oct 29, 2025
b50c453
adds procedure test code and fix bugs
demotomohiro Oct 29, 2025
3ceb223
adds more procedure test code
demotomohiro Oct 29, 2025
10885d4
adds statements test code
demotomohiro Oct 29, 2025
6a74316
fixes assertion defect when encoding empty object type definition
demotomohiro Oct 29, 2025
467a279
adds pragma test code
demotomohiro Oct 30, 2025
66d7a8e
adds generics test code and fixes bug
demotomohiro Oct 30, 2025
3c599ed
adds expression test code
demotomohiro Oct 30, 2025
91db9ac
uses ItemId instead of PSym or PType for the set of decoded syms and …
demotomohiro Oct 30, 2025
9f73412
saves/loads float Inf, NaN and NegInf literals
demotomohiro Oct 31, 2025
f3178e4
saves/loads flags of nkIdent node
demotomohiro Nov 1, 2025
5f3be1e
makes tencode_node2node.nim faster
demotomohiro Nov 1, 2025
ab13900
saves/loads PSym.ast, constraint and instantiatedFrom
demotomohiro Nov 1, 2025
54aa552
refers imported symbols like Nimony
demotomohiro Nov 4, 2025
43ff7b1
loads modules imported by system module and create module sym before …
demotomohiro Nov 5, 2025
116c220
fixes #25263; provides a new switch `mangle:nim/cpp` for debug name m…
ringabout Nov 6, 2025
155b06a
Add `heaptrack` support (#25257)
arnetheduck Nov 6, 2025
6d3254a
fixes #25251; SIGBUS with iterator over const Table lookup - prematur…
ringabout Nov 7, 2025
a866bf2
VM: optimize 'return' slots; saves millions of node allocations for N…
Araq Nov 7, 2025
6a2f4d2
wip
Araq Nov 7, 2025
dc2fb81
Merge branch 'devel' into demotomohiro-add-ast2nif2
Araq Nov 7, 2025
b4c942c
wip
Araq Nov 7, 2025
e6a1c14
progress
Araq Nov 7, 2025
940ce87
prepare for dependency tracking
Araq Nov 8, 2025
0bf8bbf
progress
Araq Nov 8, 2025
e173a6a
added TLoc writing
Araq Nov 8, 2025
2cb1c53
wip
Araq Nov 8, 2025
44f3dcc
wip
Araq Nov 8, 2025
cf4ac37
progress
Araq Nov 9, 2025
7cb0676
progress
Araq Nov 9, 2025
a9bc213
progress
Araq Nov 9, 2025
eb53434
make CI happy
Araq Nov 9, 2025
908c4e4
progress
Araq Nov 9, 2025
1ab4f1f
make explicit
Araq Nov 10, 2025
16480c4
experiment: big refactoring
Araq Nov 11, 2025
13d30b9
progress
Araq Nov 11, 2025
820a51e
stupid typo
Araq Nov 11, 2025
d7543ed
make nimsuggest compile again
Araq Nov 11, 2025
1872360
make test green
Araq Nov 11, 2025
0df59d7
remove tests
Araq Nov 11, 2025
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
421 changes: 341 additions & 80 deletions compiler/ast.nim

Large diffs are not rendered by default.

895 changes: 895 additions & 0 deletions compiler/ast2nif.nim

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3359,8 +3359,9 @@ proc genConstSetup(p: BProc; sym: PSym): bool =
useHeader(m, sym)
if sym.loc.k == locNone:
fillBackendName(p.module, sym)
fillLoc(sym.loc, locData, sym.astdef, OnStatic)
if m.hcrOn: incl(sym.loc.flags, lfIndirect)
ensureMutable sym
fillLoc(sym.locImpl, locData, sym.astdef, OnStatic)
if m.hcrOn: incl(sym, lfIndirect)
result = lfNoDecl notin sym.loc.flags

proc genConstHeader(m, q: BModule; p: BProc, sym: PSym) =
Expand Down
22 changes: 14 additions & 8 deletions compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ proc genVarTuple(p: BProc, n: PNode) =
let vn = n[i]
let v = vn.sym
if sfCompileTime in v.flags: continue
ensureMutable v
if sfGlobal in v.flags:
assignGlobalVar(p, vn, "")
genObjectInit(p, cpsInit, v.typ, v.loc, constructObj)
genObjectInit(p, cpsInit, v.typ, v.locImpl, constructObj)
registerTraverseProc(p, v)
else:
assignLocalVar(p, vn)
Expand All @@ -142,9 +143,9 @@ proc genVarTuple(p: BProc, n: PNode) =
if t.n[i].kind != nkSym: internalError(p.config, n.info, "genVarTuple")
mangleRecFieldName(p.module, t.n[i].sym)
field.snippet = dotField(rtup, fieldName)
putLocIntoDest(p, v.loc, field)
putLocIntoDest(p, v.locImpl, field)
if forHcr or isGlobalInBlock:
hcrGlobals.add((loc: v.loc, tp: CNil))
hcrGlobals.add((loc: v.locImpl, tp: CNil))

if forHcr:
# end the block where the tuple gets initialized
Expand Down Expand Up @@ -460,7 +461,8 @@ proc genSingleVar(p: BProc, v: PSym; vn, value: PNode) =
if value.kind != nkEmpty and valueAsRope.len == 0:
genLineDir(targetProc, vn)
if not isCppCtorCall:
loadInto(targetProc, vn, value, v.loc)
ensureMutable v
loadInto(targetProc, vn, value, v.locImpl)
if forHcr:
endBlockWith(targetProc):
finishBranch(p.s(cpsStmts), hcrInit)
Expand Down Expand Up @@ -736,7 +738,8 @@ proc genBlock(p: BProc, n: PNode, d: var TLoc) =
# named block?
assert(n[0].kind == nkSym)
var sym = n[0].sym
sym.loc.k = locOther
ensureMutable sym
sym.locImpl.k = locOther
sym.position = p.breakIdx+1
expr(p, n[1], d)
endSimpleBlock(p, scope)
Expand Down Expand Up @@ -1250,7 +1253,8 @@ proc genTryCpp(p: BProc, t: PNode, d: var TLoc) =
initElifBranch(p.s(cpsStmts), ifStmt, orExpr)
if exvar != nil:
fillLocalName(p, exvar.sym)
fillLoc(exvar.sym.loc, locTemp, exvar, OnStack)
ensureMutable exvar.sym
fillLoc(exvar.sym.locImpl, locTemp, exvar, OnStack)
linefmt(p, cpsStmts, "$1 $2 = T$3_;$n", [getTypeDesc(p.module, exvar.sym.typ),
rdLoc(exvar.sym.loc), rope(etmp+1)])
# we handled the error:
Expand Down Expand Up @@ -1298,7 +1302,8 @@ proc genTryCpp(p: BProc, t: PNode, d: var TLoc) =
if isImportedException(typeNode.typ, p.config):
let exvar = t[i][j][2] # ex1 in `except ExceptType as ex1:`
fillLocalName(p, exvar.sym)
fillLoc(exvar.sym.loc, locTemp, exvar, OnStack)
ensureMutable exvar.sym
fillLoc(exvar.sym.locImpl, locTemp, exvar, OnStack)
startBlockWith(p):
lineCg(p, cpsStmts, "catch ($1& $2) {$n", [getTypeDesc(p.module, typeNode.typ), rdLoc(exvar.sym.loc)])
genExceptBranchBody(t[i][^1]) # exception handler body will duplicated for every type
Expand Down Expand Up @@ -1389,7 +1394,8 @@ proc genTryCppOld(p: BProc, t: PNode, d: var TLoc) =
if t[i][j].isInfixAs():
let exvar = t[i][j][2] # ex1 in `except ExceptType as ex1:`
fillLocalName(p, exvar.sym)
fillLoc(exvar.sym.loc, locTemp, exvar, OnUnknown)
ensureMutable exvar.sym
fillLoc(exvar.sym.locImpl, locTemp, exvar, OnUnknown)
startBlockWith(p):
lineCg(p, cpsStmts, "catch ($1& $2) {$n", [getTypeDesc(p.module, t[i][j][1].typ), rdLoc(exvar.sym.loc)])
else:
Expand Down
62 changes: 36 additions & 26 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ proc fillBackendName(m: BModule; s: PSym) =
if m.hcrOn:
result.add '_'
result.add(idOrSig(s, m.module.name.s.mangle, m.sigConflicts, m.config))
s.loc.snippet = result
ensureMutable s
s.locImpl.snippet = result

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

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

proc scopeMangledParam(p: BProc; param: PSym) =
## parameter generation only takes BModule, not a BProc, so we have to
Expand Down Expand Up @@ -300,12 +303,13 @@ proc addAbiCheck(m: BModule; t: PType, name: Rope) =


proc fillResult(conf: ConfigRef; param: PNode, proctype: PType) =
fillLoc(param.sym.loc, locParam, param, "Result",
ensureMutable param.sym
fillLoc(param.sym.locImpl, locParam, param, "Result",
OnStack)
let t = param.sym.typ
if mapReturnType(conf, t) != ctArray and isInvalidReturnType(conf, proctype):
incl(param.sym.loc.flags, lfIndirect)
param.sym.loc.storage = OnUnknown
incl(param.sym.locImpl.flags, lfIndirect)
param.sym.locImpl.storage = OnUnknown

proc typeNameOrLiteral(m: BModule; t: PType, literal: string): Rope =
if t.sym != nil and sfImportc in t.sym.flags and t.sym.magic == mNone:
Expand Down Expand Up @@ -524,14 +528,15 @@ proc genMemberProcParams(m: BModule; prc: PSym, superCall, rettype, name, params
var types, names, args: seq[string] = @[]
if not isCtor:
var this = t.n[1].sym
ensureMutable this
fillParamName(m, this)
fillLoc(this.loc, locParam, t.n[1],
fillLoc(this.locImpl, locParam, t.n[1],
this.paramStorageLoc)
if this.typ.kind == tyPtr:
this.loc.snippet = "this"
this.locImpl.snippet = "this"
else:
this.loc.snippet = "(*this)"
names.add this.loc.snippet
this.locImpl.snippet = "(*this)"
names.add this.locImpl.snippet
types.add getTypeDescWeak(m, this.typ, check, dkParam)

let firstParam = if isCtor: 1 else: 2
Expand All @@ -545,21 +550,22 @@ proc genMemberProcParams(m: BModule; prc: PSym, superCall, rettype, name, params
else:
descKind = dkRefParam
var typ, name: string
ensureMutable param
fillParamName(m, param)
fillLoc(param.loc, locParam, t.n[i],
fillLoc(param.locImpl, locParam, t.n[i],
param.paramStorageLoc)
if ccgIntroducedPtr(m.config, param, t.returnType) and descKind == dkParam:
typ = getTypeDescWeak(m, param.typ, check, descKind) & "*"
incl(param.loc.flags, lfIndirect)
param.loc.storage = OnUnknown
incl(param.locImpl.flags, lfIndirect)
param.locImpl.storage = OnUnknown
elif weakDep:
typ = getTypeDescWeak(m, param.typ, check, descKind)
else:
typ = getTypeDescAux(m, param.typ, check, descKind)
if sfNoalias in param.flags:
typ.add("NIM_NOALIAS ")

name = param.loc.snippet
name = param.locImpl.snippet
types.add typ
names.add name
if sfCodegenDecl notin param.flags:
Expand Down Expand Up @@ -601,14 +607,15 @@ proc genProcParams(m: BModule; t: PType, rettype: var Rope, params: var Builder,
else:
descKind = dkRefParam
if isCompileTimeOnly(param.typ): continue
ensureMutable param
fillParamName(m, param)
fillLoc(param.loc, locParam, t.n[i],
fillLoc(param.locImpl, locParam, t.n[i],
param.paramStorageLoc)
var typ: Rope
if ccgIntroducedPtr(m.config, param, t.returnType) and descKind == dkParam:
typ = ptrType(getTypeDescWeak(m, param.typ, check, descKind))
incl(param.loc.flags, lfIndirect)
param.loc.storage = OnUnknown
incl(param.locImpl.flags, lfIndirect)
param.locImpl.storage = OnUnknown
elif weakDep:
typ = (getTypeDescWeak(m, param.typ, check, descKind))
else:
Expand All @@ -620,9 +627,9 @@ proc genProcParams(m: BModule; t: PType, rettype: var Rope, params: var Builder,
var j = 0
while arr.kind in {tyOpenArray, tyVarargs}:
# this fixes the 'sort' bug:
if param.typ.kind in {tyVar, tyLent}: param.loc.storage = OnUnknown
if param.typ.kind in {tyVar, tyLent}: param.locImpl.storage = OnUnknown
# need to pass hidden parameter:
params.addParam(paramBuilder, name = param.loc.snippet & "Len_" & $j, typ = NimInt)
params.addParam(paramBuilder, name = param.locImpl.snippet & "Len_" & $j, typ = NimInt)
inc(j)
arr = arr[0].skipTypes({tySink})
if t.returnType != nil and isInvalidReturnType(m.config, t):
Expand Down Expand Up @@ -707,7 +714,8 @@ proc genRecordFieldsAux(m: BModule; n: PNode,
if field.typ.kind == tyVoid: return
#assert(field.ast == nil)
let sname = mangleRecFieldName(m, field)
fillLoc(field.loc, locField, n, unionPrefix & sname, OnUnknown)
ensureMutable field
fillLoc(field.locImpl, locField, n, unionPrefix & sname, OnUnknown)
# for importcpp'ed objects, we only need to set field.loc, but don't
# have to recurse via 'getTypeDescAux'. And not doing so prevents problems
# with heavily templatized C++ code:
Expand Down Expand Up @@ -1155,7 +1163,8 @@ proc genMemberProcHeader(m: BModule; prc: PSym; result: var Builder; asPtr: bool
let isCtor = sfConstructor in prc.flags
var check = initIntSet()
fillBackendName(m, prc)
fillLoc(prc.loc, locProc, prc.ast[namePos], OnUnknown)
ensureMutable prc
fillLoc(prc.locImpl, locProc, prc.ast[namePos], OnUnknown)
var memberOp = "#." #only virtual
var typ: PType
if isCtor:
Expand Down Expand Up @@ -1187,7 +1196,7 @@ proc genMemberProcHeader(m: BModule; prc: PSym; result: var Builder; asPtr: bool
superCall = ""
else:
if not isCtor:
prc.loc.snippet = "$1$2(@)" % [memberOp, name]
prc.locImpl.snippet = "$1$2(@)" % [memberOp, name]
elif superCall != "":
superCall = " : " & superCall

Expand All @@ -1202,14 +1211,15 @@ proc genProcHeader(m: BModule; prc: PSym; result: var Builder; visibility: var D
# using static is needed for inline procs
var check = initIntSet()
fillBackendName(m, prc)
fillLoc(prc.loc, locProc, prc.ast[namePos], OnUnknown)
ensureMutable prc
fillLoc(prc.locImpl, locProc, prc.ast[namePos], OnUnknown)
var rettype: Snippet = ""
var desc = newBuilder("")
genProcParams(m, prc.typ, rettype, desc, check, true, false)
let params = extract(desc)
# handle the 2 options for hotcodereloading codegen - function pointer
# (instead of forward declaration) or header for function body with "_actual" postfix
var name = prc.loc.snippet
var name = prc.locImpl.snippet
if not asPtr and isReloadable(m, prc):
name.add("_actual")
# careful here! don't access ``prc.ast`` as that could reload large parts of
Expand Down Expand Up @@ -1645,8 +1655,8 @@ proc generateRttiDestructor(g: ModuleGraph; typ: PType; owner: PSym; kind: TType
n[bodyPos] = body
result.ast = n

incl result.flags, sfFromGeneric
incl result.flags, sfGeneratedOp
incl result.flagsImpl, sfFromGeneric
incl result.flagsImpl, sfGeneratedOp

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