From 53d1c39119e590f17fc04ccd45d0789128d03597 Mon Sep 17 00:00:00 2001 From: metagn Date: Fri, 1 Nov 2024 19:53:06 +0300 Subject: [PATCH] fix computed goto --- compiler/cbuilderstmts.nim | 15 ++++++++++----- compiler/ccgstmts.nim | 4 ++-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/compiler/cbuilderstmts.nim b/compiler/cbuilderstmts.nim index a4014628ad91..2099d45d1969 100644 --- a/compiler/cbuilderstmts.nim +++ b/compiler/cbuilderstmts.nim @@ -257,24 +257,29 @@ proc addLabel(builder: var Builder, name: TLabel) = proc addReturn(builder: var Builder) = builder.add("return;\n") -proc addReturn(builder: var Builder, value: string) = +proc addReturn(builder: var Builder, value: Snippet) = builder.add("return ") builder.add(value) builder.add(";\n") -template addGoto(builder: var Builder, label: TLabel) = +proc addGoto(builder: var Builder, label: TLabel) = builder.add("goto ") builder.add(label) builder.add(";\n") -template addBreak(builder: var Builder) = +proc addComputedGoto(builder: var Builder, value: Snippet) = + builder.add("goto *") + builder.add(value) + builder.add(";\n") + +proc addBreak(builder: var Builder) = builder.add("break;") -template addIncr(builder: var Builder, val: Snippet) = +proc addIncr(builder: var Builder, val: Snippet) = builder.add(val) builder.add("++;\n") -template addDecr(builder: var Builder, val: Snippet) = +proc addDecr(builder: var Builder, val: Snippet) = builder.add(val) builder.add("--;\n") diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index a384b6b0313b..054770459781 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -644,7 +644,7 @@ proc genComputedGoto(p: BProc; n: PNode) = var a: TLoc = initLocExpr(p, caseStmt[0]) let ra = a.rdLoc # first goto: - p.s(cpsStmts).addGoto(cDeref(subscript(tmp, ra))) + p.s(cpsStmts).addComputedGoto(subscript(tmp, ra)) for i in 1..