From b80f5e137d7ed11154b8f973f24e97ecd011cb9a Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 2 Jan 2025 20:20:43 +0800 Subject: [PATCH 1/4] nifc: fixes top level var decls with non-literals --- src/nifc/codegen.nim | 23 ++++++++++++++++++----- tests/nimony/sysbasics/ttoplevelvars.nim | 17 +++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 tests/nimony/sysbasics/ttoplevelvars.nim diff --git a/src/nifc/codegen.nim b/src/nifc/codegen.nim index 0677f41f..b845d9fb 100644 --- a/src/nifc/codegen.nim +++ b/src/nifc/codegen.nim @@ -350,11 +350,24 @@ proc genVarDecl(c: var GeneratedCode; t: Tree; n: NodePos; vk: VarKind; toExtern if vis == StaticC: c.code.insert(Token(StaticKeyword), beforeDecl) if t[d.value].kind != Empty: - c.add AsgnOpr - if vk != IsLocal: inc c.inSimpleInit - genx c, t, d.value - if vk != IsLocal: dec c.inSimpleInit - c.add Semicolon + if vk == IsGlobal and + t[d.value].kind notin {IntLit, UIntLit, FloatLit, CharLit, StrLit, FalseC, TrueC}: + c.add Semicolon + moveToInitSection: + c.add name + c.add AsgnOpr + inc c.inSimpleInit + genx c, t, d.value + dec c.inSimpleInit + c.add Semicolon + else: + c.add AsgnOpr + if vk != IsLocal: inc c.inSimpleInit + genx c, t, d.value + if vk != IsLocal: dec c.inSimpleInit + c.add Semicolon + else: + c.add Semicolon else: error c.m, "expected SymbolDef but got: ", t, n diff --git a/tests/nimony/sysbasics/ttoplevelvars.nim b/tests/nimony/sysbasics/ttoplevelvars.nim new file mode 100644 index 00000000..faa57123 --- /dev/null +++ b/tests/nimony/sysbasics/ttoplevelvars.nim @@ -0,0 +1,17 @@ +var x = 1 +var y = 2 +x = y +var z = x + +const a = 1 +var m = a + +let s = x +let n = 3 + +proc foo = + var m = a + let g = x + const s = a + +foo() \ No newline at end of file From 742bfef95f3f29c81f20cbac242eeccd3bdb9a1b Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:09:31 +0800 Subject: [PATCH 2/4] adds AconstrC, OconstrC --- src/nifc/codegen.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nifc/codegen.nim b/src/nifc/codegen.nim index b845d9fb..070f09c8 100644 --- a/src/nifc/codegen.nim +++ b/src/nifc/codegen.nim @@ -351,7 +351,7 @@ proc genVarDecl(c: var GeneratedCode; t: Tree; n: NodePos; vk: VarKind; toExtern c.code.insert(Token(StaticKeyword), beforeDecl) if t[d.value].kind != Empty: if vk == IsGlobal and - t[d.value].kind notin {IntLit, UIntLit, FloatLit, CharLit, StrLit, FalseC, TrueC}: + t[d.value].kind notin {IntLit, UIntLit, FloatLit, CharLit, StrLit, FalseC, TrueC, AconstrC, OconstrC}: c.add Semicolon moveToInitSection: c.add name From beddf46bd893f688fbbaf7ba7021e6e284943c5f Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:13:32 +0800 Subject: [PATCH 3/4] fixes --- src/nifc/codegen.nim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nifc/codegen.nim b/src/nifc/codegen.nim index 070f09c8..e0198063 100644 --- a/src/nifc/codegen.nim +++ b/src/nifc/codegen.nim @@ -351,7 +351,9 @@ proc genVarDecl(c: var GeneratedCode; t: Tree; n: NodePos; vk: VarKind; toExtern c.code.insert(Token(StaticKeyword), beforeDecl) if t[d.value].kind != Empty: if vk == IsGlobal and - t[d.value].kind notin {IntLit, UIntLit, FloatLit, CharLit, StrLit, FalseC, TrueC, AconstrC, OconstrC}: + t[d.value].kind notin {IntLit, UIntLit, FloatLit, + CharLit, StrLit, FalseC, TrueC, AconstrC, + OconstrC, InfC, NegInfC, NanC, SufC}: c.add Semicolon moveToInitSection: c.add name From c5457009b1b277c154ac56ee49c8b6246af961f1 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 7 Jan 2025 22:01:59 +0800 Subject: [PATCH 4/4] implements `isLiteral` --- src/nifc/codegen.nim | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/nifc/codegen.nim b/src/nifc/codegen.nim index e0198063..d1ac09f5 100644 --- a/src/nifc/codegen.nim +++ b/src/nifc/codegen.nim @@ -331,6 +331,25 @@ type VarKind = enum IsLocal, IsGlobal, IsThreadlocal, IsConst +proc isLiteral(t: Tree; n: NodePos): bool = + case t[n].kind + of IntLit, UIntLit, FloatLit, + CharLit, StrLit, FalseC, TrueC, InfC, NegInfC, NanC, SufC: + result = true + of AconstrC: + result = true + for ch in sonsFromX(t, n): + if not isLiteral(t, ch): + return false + of OconstrC: + result = true + for ch in sonsFromX(t, n): + let (_, value) = sons2(t, ch) + if not isLiteral(t, value): + return false + else: + result = false + proc genVarDecl(c: var GeneratedCode; t: Tree; n: NodePos; vk: VarKind; toExtern = false) = let d = asVarDecl(t, n) genCLineDir(c, t, info(t, n)) @@ -350,10 +369,7 @@ proc genVarDecl(c: var GeneratedCode; t: Tree; n: NodePos; vk: VarKind; toExtern if vis == StaticC: c.code.insert(Token(StaticKeyword), beforeDecl) if t[d.value].kind != Empty: - if vk == IsGlobal and - t[d.value].kind notin {IntLit, UIntLit, FloatLit, - CharLit, StrLit, FalseC, TrueC, AconstrC, - OconstrC, InfC, NegInfC, NanC, SufC}: + if vk == IsGlobal and not isLiteral(t, d.value): c.add Semicolon moveToInitSection: c.add name