From 51eccea9106a9e68ce92b8f657dd7c0f383c7ded Mon Sep 17 00:00:00 2001 From: demotomohiro Date: Tue, 28 Oct 2025 14:40:05 +0900 Subject: [PATCH] saves/loads PSym guard, bitsize and alignment --- compiler/icnif/nifdecoder.nim | 39 +++++++++++++++---------- compiler/icnif/nifencoder.nim | 13 ++++++--- tests/icnif/tencode_node2node.nim | 15 +++++++++- tests/icnif/testcode/modtestpragmas.nim | 4 +-- 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/compiler/icnif/nifdecoder.nim b/compiler/icnif/nifdecoder.nim index 567277302a..8ec3d19a0d 100644 --- a/compiler/icnif/nifdecoder.nim +++ b/compiler/icnif/nifdecoder.nim @@ -89,30 +89,18 @@ proc fromNifSymDef(c: var DecodeContext; n: var Cursor): PSym = let itemId = pool.integers[n.intId].int32 incExpect n, Ident let ident = c.graph.cache.getIdent(pool.strings[n.litId]) + incExpect n, {Ident, DotToken} + let flags = if n.kind == Ident: pool.strings[n.litId].parseSymFlags else: {} + incExpect n, IntLit + let disamb = pool.integers[n.intId].int32 incExpect n, ParLe let kind = parseSymKind(pool.tags[n.tagId]) - # TODO: add kind specific data - inc n - skipParRi n - expect n, {Ident, DotToken} - let flags = if n.kind == Ident: pool.strings[n.litId].parseSymFlags else: {} - inc n - var position = if kind == skModule: - c.fromNifModuleId(n)[0].int - else: - expect n, IntLit - let p = pool.integers[n.intId] - inc n - p - expect n, IntLit - let disamb = pool.integers[n.intId].int32 inc n result = PSym(itemId: ItemId(module: itemIdModule.int32, item: itemId), kind: kind, name: ident, flags: flags, - position: position, disamb: disamb) # PNode, PSym or PType type fields in PSym can have cycles. @@ -122,6 +110,25 @@ proc fromNifSymDef(c: var DecodeContext; n: var Cursor): PSym = assert nifItemId notin c.symbols c.symbols[nifItemId] = result + case kind + of skLet, skVar, skField, skForVar: + result.guard = c.fromNifSymbol n + expect n, IntLit + result.bitsize = pool.integers[n.intId] + incExpect n, IntLit + result.alignment = pool.integers[n.intId] + inc n + else: + discard + skipParRi n + result.position = if kind == skModule: + c.fromNifModuleId(n)[0].int + else: + expect n, IntLit + let p = pool.integers[n.intId] + inc n + p + result.typ = c.fromNifType n result.setOwner(c.fromNifSymbol n) diff --git a/compiler/icnif/nifencoder.nim b/compiler/icnif/nifencoder.nim index 1a6d9cb0b4..bd35c988e2 100644 --- a/compiler/icnif/nifencoder.nim +++ b/compiler/icnif/nifencoder.nim @@ -52,15 +52,20 @@ proc toNifDef(c: var EncodeContext; sym: PSym) = c.toNifModuleId sym.itemId.module c.dest.addIntLit sym.itemId.item c.dest.addIdent sym.name.s - c.dest.buildTree sym.kind.toNifTag: - # TODO: add kind specific data - discard c.dest.writeFlags sym.flags + c.dest.addIntLit sym.disamb + c.dest.buildTree sym.kind.toNifTag: + case sym.kind + of skLet, skVar, skField, skForVar: + c.toNif sym.guard + c.dest.addIntLit sym.bitsize + c.dest.addIntLit sym.alignment + else: + discard if sym.kind == skModule: c.toNifModuleId sym.position else: c.dest.addIntLit sym.position - c.dest.addIntLit sym.disamb c.toNif sym.typ c.toNif sym.owner c.dest.addIdent toNifTag(sym.loc.k) diff --git a/tests/icnif/tencode_node2node.nim b/tests/icnif/tencode_node2node.nim index 21875c1623..2fb35785ef 100644 --- a/tests/icnif/tencode_node2node.nim +++ b/tests/icnif/tencode_node2node.nim @@ -149,7 +149,20 @@ proc eql(x, y: PSym; c: var EqlContext): bool = debug(y.owner) result = false else: - result = true + if x.kind in {skLet, skVar, skField, skForVar}: + if not eql(x.guard, y.guard, c): + echo "symbol guard mismatch" + result = false + elif x.bitsize != y.bitsize: + echo "symbol bitsize mismatch: ", x.bitsize, "/", y.bitsize + result = false + elif x.alignment != y.alignment: + echo "symbol alignment mismatch: ", x.alignment, "/", y.alignment + result = false + else: + result = true + else: + result = true discard c.symStack.pop proc eql(x, y: PType; c: var EqlContext): bool = diff --git a/tests/icnif/testcode/modtestpragmas.nim b/tests/icnif/testcode/modtestpragmas.nim index 7b8e22ccdf..c20c87fefc 100644 --- a/tests/icnif/testcode/modtestpragmas.nim +++ b/tests/icnif/testcode/modtestpragmas.nim @@ -1,3 +1,3 @@ -#var exportcTest {.exportc.}: int +var exportcTest {.exportc.}: int var importcTest {.importc.}: int -#var y* {.importc, header: "test.h".}: int +var y* {.importc, header: "test.h".}: int