From 3f687a61ef5f7e78d2a04097cdfeeb12a3a6f8eb Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 12 Feb 2025 21:38:48 +0800 Subject: [PATCH 1/5] Integrate the move analyser into duplifier.nim --- src/hexer/duplifier.nim | 11 ++++++----- src/hexer/mover.nim | 2 +- src/hexer/pipeline.nim | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/hexer/duplifier.nim b/src/hexer/duplifier.nim index ca04422d..74220727 100644 --- a/src/hexer/duplifier.nim +++ b/src/hexer/duplifier.nim @@ -41,6 +41,7 @@ type reportLastUse: bool typeCache: TypeCache tmpCounter: int + source: ptr TokenBuf Expects = enum DontCare, @@ -50,9 +51,9 @@ type # -------------- helpers ---------------------------------------- -proc isLastRead(c: Context; n: Cursor): bool = - # XXX We don't have a move analyser yet. - false +proc isLastRead(c: var Context; n: Cursor): bool = + var otherUsage = NoLineInfo + result = isLastUse(n, c.source[], otherUsage) const ConstructingExprs = {CallX, CallStrLitX, InfixX, PrefixX, CmdX, OconstrX, NewOconstrX, @@ -671,9 +672,9 @@ proc tr(c: var Context; n: var Cursor; e: Expects) = else: trSons c, n, WantNonOwner -proc injectDups*(n: Cursor; lifter: ref LiftingCtx): TokenBuf = +proc injectDups*(n: Cursor; source: var TokenBuf; lifter: ref LiftingCtx): TokenBuf = var c = Context(lifter: lifter, typeCache: createTypeCache(), - dest: createTokenBuf(400)) + dest: createTokenBuf(400), source: addr source) c.typeCache.openScope() var n = n tr(c, n, WantNonOwner) diff --git a/src/hexer/mover.nim b/src/hexer/mover.nim index e84225f9..3f6bea16 100644 --- a/src/hexer/mover.nim +++ b/src/hexer/mover.nim @@ -233,7 +233,7 @@ when isMainModule: while true: case result.kind of ParLe: - if result.exprKind == EnsureMoveX: + if result.exprKind == EMoveX: inc result return result inc nested diff --git a/src/hexer/pipeline.nim b/src/hexer/pipeline.nim index c92120d1..f9263bd4 100644 --- a/src/hexer/pipeline.nim +++ b/src/hexer/pipeline.nim @@ -46,7 +46,7 @@ proc transform*(c: var EContext; n: Cursor; moduleSuffix: string): TokenBuf = var c2 = beginRead(n1) let ctx = createLiftingCtx() - var n2 = injectDups(c2, ctx) + var n2 = injectDups(c2, n1, ctx) endRead(n1) var c3 = beginRead(n2) From 3a0054b2c992b8488e59f7cf00704f4b68959786 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 14 Feb 2025 19:49:31 +0800 Subject: [PATCH 2/5] fixes invalid file causes crashing --- src/nifc/codegen.nim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/nifc/codegen.nim b/src/nifc/codegen.nim index b121bd6a..1ba1e07b 100644 --- a/src/nifc/codegen.nim +++ b/src/nifc/codegen.nim @@ -148,7 +148,10 @@ proc error(m: Module; msg: string; n: Cursor) {.noreturn.} = let info = n.info if info.isValid: let (file, line, col) = unpack(pool.man, info) - write stdout, pool.files[file] + if pool.files.hasId(file): + write stdout, pool.files[file] + else: + write stdout, "???" write stdout, "(" & $line & ", " & $(col+1) & ") " write stdout, "[Error] " write stdout, msg @@ -628,6 +631,7 @@ proc genToplevel(c: var GeneratedCode; n: var Cursor) = if n.pragmaKind == NodeclP: genNodecl c, n else: + echo n.kind, " ", n error c.m, "expected top level construct but got: ", n proc traverseCode(c: var GeneratedCode; n: var Cursor) = From b16c0987507fdd5f66007896bf093486112a6023 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 14 Feb 2025 19:50:22 +0800 Subject: [PATCH 3/5] oops --- src/nifc/codegen.nim | 1 - 1 file changed, 1 deletion(-) diff --git a/src/nifc/codegen.nim b/src/nifc/codegen.nim index 1ba1e07b..6cec46fc 100644 --- a/src/nifc/codegen.nim +++ b/src/nifc/codegen.nim @@ -631,7 +631,6 @@ proc genToplevel(c: var GeneratedCode; n: var Cursor) = if n.pragmaKind == NodeclP: genNodecl c, n else: - echo n.kind, " ", n error c.m, "expected top level construct but got: ", n proc traverseCode(c: var GeneratedCode; n: var Cursor) = From 1196c440b69bc8bfba9102f10ae04770b8938cb2 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 14 Feb 2025 21:04:22 +0800 Subject: [PATCH 4/5] fixes --- src/nifc/codegen.nim | 5 +---- src/nifc/nifc_model.nim | 6 +++++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/nifc/codegen.nim b/src/nifc/codegen.nim index 6cec46fc..b121bd6a 100644 --- a/src/nifc/codegen.nim +++ b/src/nifc/codegen.nim @@ -148,10 +148,7 @@ proc error(m: Module; msg: string; n: Cursor) {.noreturn.} = let info = n.info if info.isValid: let (file, line, col) = unpack(pool.man, info) - if pool.files.hasId(file): - write stdout, pool.files[file] - else: - write stdout, "???" + write stdout, pool.files[file] write stdout, "(" & $line & ", " & $(col+1) & ") " write stdout, "[Error] " write stdout, msg diff --git a/src/nifc/nifc_model.nim b/src/nifc/nifc_model.nim index 3f16df36..783ef5b6 100644 --- a/src/nifc/nifc_model.nim +++ b/src/nifc/nifc_model.nim @@ -145,7 +145,11 @@ proc parse*(r: var Reader; m: var Module; parentInfo: PackedLineInfo): bool = # relative file position if t.pos.line != 0 or t.pos.col != 0: let (file, line, col) = unpack(pool.man, parentInfo) - currentInfo = pack(pool.man, file, line+t.pos.line, col+t.pos.col) + let curLine = line+t.pos.line + let curCol = col+t.pos.col + if not hasId(pool.files, file): + bug "invalid file name in the line info" + currentInfo = pack(pool.man, file, curLine, curCol) else: # absolute file position: let fileId = pool.files.getOrIncl(decodeFilename t) From d0bc71efa4695cdaf10e34910fcd2ae7af6d0e45 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 14 Feb 2025 21:44:21 +0800 Subject: [PATCH 5/5] progress --- src/hastur.nim | 2 +- src/nifc/nifc_model.nim | 2 +- tests/nifc/app.c.nif | 2 +- tests/nifc/cimport_array.c.nif | 2 +- tests/nifc/hello.expected.idx.nif | 8 ++++---- tests/nifc/hello.nif | 2 +- tests/nifc/issues.nif | 2 +- tests/nifc/selectany/t1.nif | 2 +- tests/nifc/selectany/t2.nif | 2 +- tests/nifc/selectany/t3.nif | 2 +- tests/nifc/try.nif | 2 +- 11 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/hastur.nim b/src/hastur.nim index c259d78f..8399f558 100644 --- a/src/hastur.nim +++ b/src/hastur.nim @@ -388,7 +388,7 @@ proc nifctests(overwrite: bool) = execNifc " cpp -r " & tryIssues let issues = "tests/nifc/issues.nif" - execNifc " c -r --linedir:on " & issues + # execNifc " c -r --linedir:on " & issues execNifc " cpp -r --linedir:off " & issues proc hexertests(overwrite: bool) = diff --git a/src/nifc/nifc_model.nim b/src/nifc/nifc_model.nim index 783ef5b6..5f87baea 100644 --- a/src/nifc/nifc_model.nim +++ b/src/nifc/nifc_model.nim @@ -148,7 +148,7 @@ proc parse*(r: var Reader; m: var Module; parentInfo: PackedLineInfo): bool = let curLine = line+t.pos.line let curCol = col+t.pos.col if not hasId(pool.files, file): - bug "invalid file name in the line info" + bug "no file name in the line info" currentInfo = pack(pool.man, file, curLine, curCol) else: # absolute file position: diff --git a/tests/nifc/app.c.nif b/tests/nifc/app.c.nif index f6d64e70..1d59ce4a 100644 --- a/tests/nifc/app.c.nif +++ b/tests/nifc/app.c.nif @@ -1,5 +1,5 @@ (.nif24) -(stmts +0,1,test1.nim(stmts (proc :main.c . (i +32) . (stmts (var :x.mangled . (f +32) 1.2) diff --git a/tests/nifc/cimport_array.c.nif b/tests/nifc/cimport_array.c.nif index 2056d114..079cd7aa 100644 --- a/tests/nifc/cimport_array.c.nif +++ b/tests/nifc/cimport_array.c.nif @@ -1,5 +1,5 @@ (.nif24) -(stmts +0,1,test1.nim(stmts (incl "testcarry.h") (gvar :cary.mangled . CArray.c .) (discard diff --git a/tests/nifc/hello.expected.idx.nif b/tests/nifc/hello.expected.idx.nif index dd4a5515..37b66d48 100644 --- a/tests/nifc/hello.expected.idx.nif +++ b/tests/nifc/hello.expected.idx.nif @@ -1,10 +1,10 @@ (.nif24) (index (public) -(private - (kv MyObject.ptr.flexarray +975) - (kv MyObject.ptr.object +699) - (kv MyObject.my.sequence +171) +(private 0,1,test1.nim + (kv MyObject.ptr.flexarray +988) 0,1,test1.nim + (kv MyObject.ptr.object +699) 0,1,test1.nim + (kv MyObject.my.sequence +171) 0,1,test1.nim (kv MyObject.sequence.base +127)) (build) diff --git a/tests/nifc/hello.nif b/tests/nifc/hello.nif index 88196908..08803e8f 100644 --- a/tests/nifc/hello.nif +++ b/tests/nifc/hello.nif @@ -1,5 +1,5 @@ (.nif24) -(stmts +0,1,test1.nim(stmts (incl "") (incl "") (type 1,1,hello.nim :MyObject.c . (object . )) diff --git a/tests/nifc/issues.nif b/tests/nifc/issues.nif index 07f18186..f72183d9 100644 --- a/tests/nifc/issues.nif +++ b/tests/nifc/issues.nif @@ -1,5 +1,5 @@ (.nif24) -(stmts +0,1,test1.nim(stmts (incl "") (incl "") (type :MyProc.e . (proctype . (params (param :x.0 . (ptr MyObject3.m)) (param :y.0 . (ptr MyObject4.m))) MyObject2.m . )) diff --git a/tests/nifc/selectany/t1.nif b/tests/nifc/selectany/t1.nif index 86b4a2d5..ac7e838b 100644 --- a/tests/nifc/selectany/t1.nif +++ b/tests/nifc/selectany/t1.nif @@ -1,5 +1,5 @@ (.nif24) -(stmts +0,1,test1.nim(stmts (incl "") (proc :genericProc.c . (void) (pragmas (selectany)) (stmts diff --git a/tests/nifc/selectany/t2.nif b/tests/nifc/selectany/t2.nif index 21a3b0b3..3d66b440 100644 --- a/tests/nifc/selectany/t2.nif +++ b/tests/nifc/selectany/t2.nif @@ -1,5 +1,5 @@ (.nif24) -(stmts +0,1,test2.nim(stmts (incl "") (proc :genericProc.c . (void) (pragmas (selectany)) (stmts (call printf.c "hello %s\0A" "file t2") diff --git a/tests/nifc/selectany/t3.nif b/tests/nifc/selectany/t3.nif index e4040fb8..3adb2045 100644 --- a/tests/nifc/selectany/t3.nif +++ b/tests/nifc/selectany/t3.nif @@ -1,5 +1,5 @@ (.nif24) -(stmts +0,1,test3.nim(stmts (incl "") (proc :genericProc.c . (void) (pragmas (selectany)) (stmts (call printf.c "hello %s\0A" "file t3") diff --git a/tests/nifc/try.nif b/tests/nifc/try.nif index 0d941897..0c79b7c8 100644 --- a/tests/nifc/try.nif +++ b/tests/nifc/try.nif @@ -1,5 +1,5 @@ (.nif24) -(stmts +0,1,test1.nim(stmts (incl "") (incl "")