Skip to content

Commit

Permalink
fix computed goto
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Nov 8, 2024
1 parent 6be6f4c commit 53d1c39
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 10 additions & 5 deletions compiler/cbuilderstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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..<caseStmt.len:
var scope: ScopeBuilder
Expand Down Expand Up @@ -682,7 +682,7 @@ proc genComputedGoto(p: BProc; n: PNode) =

var a: TLoc = initLocExpr(p, caseStmt[0])
let ra = a.rdLoc
p.s(cpsStmts).addGoto(cDeref(subscript(tmp, ra)))
p.s(cpsStmts).addComputedGoto(subscript(tmp, ra))
endSimpleBlock(p, scope)

for j in casePos+1..<n.len:
Expand Down

0 comments on commit 53d1c39

Please sign in to comment.