From 6f1fc1b5ba7f74fde21467f5f0bfd294ad9292f3 Mon Sep 17 00:00:00 2001 From: Araq Date: Thu, 22 Aug 2013 08:38:47 +0200 Subject: [PATCH] fixed and documented computedGoto pragma --- compiler/ccgstmts.nim | 5 +++- compiler/nimsets.nim | 27 +++++--------------- compiler/vm.nim | 1 - compiler/vmgen.nim | 42 +++++++++++++++++++++++------- doc/manual.txt | 45 +++++++++++++++++++++++++++++++++ doc/nimrodc.txt | 2 +- tests/compile/tcomputedgoto.nim | 5 ++-- web/news.txt | 2 ++ 8 files changed, 94 insertions(+), 35 deletions(-) diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index 4d52e3aab4..01c734524f 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -322,6 +322,7 @@ proc genComputedGoto(p: BProc; n: PNode) = gotoArray.appf("&&TMP$#};$n", (id+arraySize).toRope) line(p, cpsLocals, gotoArray) + for j in 0 .. casePos-1: genStmts(p, n.sons[j]) let caseStmt = n.sons[casePos] var a: TLoc initLocExpr(p, caseStmt.sons[0], a) @@ -329,6 +330,7 @@ proc genComputedGoto(p: BProc; n: PNode) = lineF(p, cpsStmts, "goto *$#[$#];$n", tmp, a.rdLoc) for i in 1 .. `_ diff --git a/tests/compile/tcomputedgoto.nim b/tests/compile/tcomputedgoto.nim index 661f70743e..2c3b4bbd4a 100644 --- a/tests/compile/tcomputedgoto.nim +++ b/tests/compile/tcomputedgoto.nim @@ -31,7 +31,8 @@ proc vm() = var pc = 0 while true: {.computedGoto.} - case instructions[pc] + let instr = instructions[pc] + case instr of enumA: echo "yeah A" of enumC, enumD: @@ -39,7 +40,7 @@ proc vm() = of enumB: echo "yeah B" of enumE: - return + break inc(pc) vm() diff --git a/web/news.txt b/web/news.txt index 56919d56cd..896cf9941a 100644 --- a/web/news.txt +++ b/web/news.txt @@ -37,6 +37,8 @@ Compiler Additions - The compiler now enforces the ``not nil`` constraint. - The compiler now supports a ``codegenDecl`` pragma for even more control over the generated code. +- The compiler now supports a ``computedGoto`` pragma to support very fast + dispatching for interpreters and the like. Language Additions