summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2023-03-17 15:02:48 +0800
committerGitHub <noreply@github.com>2023-03-17 08:02:48 +0100
commitfd4e3ae3e4564525f901f2517711a1243535d2a2 (patch)
treedf5e43a9484f2e9ca11420533a65b56d66972557
parentb5ee81fd234c690f736a8f196a234c11a32e3910 (diff)
downloadNim-fd4e3ae3e4564525f901f2517711a1243535d2a2.tar.gz
add a prepass for codeReordering (#21513)
* add a prepass for codeReordering

* simplify

* fixes
-rw-r--r--compiler/pipelines.nim34
-rw-r--r--compiler/pragmas.nim2
-rw-r--r--tests/misc/tnoforward.nim1
-rw-r--r--tests/modules/t8665.nim1
-rw-r--r--tests/modules/treorder.nim2
-rw-r--r--tests/pragmas/treorder.nim1
6 files changed, 34 insertions, 7 deletions
diff --git a/compiler/pipelines.nim b/compiler/pipelines.nim
index b80681c4b..2bb0bc247 100644
--- a/compiler/pipelines.nim
+++ b/compiler/pipelines.nim
@@ -1,13 +1,13 @@
 import sem, cgen, modulegraphs, ast, llstream, parser, msgs,
        lineinfos, reorder, options, semdata, cgendata, modules, pathutils,
-       packages, syntaxes, depends, vm
+       packages, syntaxes, depends, vm, pragmas, idents, lookups
 
 import pipelineutils
 
 when not defined(leanCompiler):
   import jsgen, docgen2
 
-import std/[syncio, objectdollar, assertions, tables]
+import std/[syncio, objectdollar, assertions, tables, strutils]
 import renderer
 import ic/replayer
 
@@ -56,6 +56,34 @@ proc processImplicitImports(graph: ModuleGraph; implicits: seq[string], nodeKind
       if semNode == nil or processPipeline(graph, semNode, bModule) == nil:
         break
 
+proc prePass(c: PContext; n: PNode) =
+  for son in n:
+    if son.kind == nkPragma:
+      for s in son:
+        var key = if s.kind in nkPragmaCallKinds and s.len > 1: s[0] else: s
+        if key.kind in {nkBracketExpr, nkCast} or key.kind notin nkIdentKinds:
+          continue
+        let ident = whichKeyword(considerQuotedIdent(c, key))
+        case ident
+        of wReorder:
+          pragmaNoForward(c, s, flag = sfReorder)
+        of wExperimental:
+          if isTopLevel(c) and s.kind in nkPragmaCallKinds and s.len == 2:
+            let name = c.semConstExpr(c, s[1])
+            case name.kind
+            of nkStrLit, nkRStrLit, nkTripleStrLit:
+              try:
+                let feature = parseEnum[Feature](name.strVal)
+                if feature == codeReordering:
+                  c.features.incl feature
+                  c.module.flags.incl sfReorder
+              except ValueError:
+                discard
+            else:
+              discard
+        else:
+          discard
+
 proc processPipelineModule*(graph: ModuleGraph; module: PSym; idgen: IdGenerator;
                     stream: PLLStream): bool =
   if graph.stopCompile(): return true
@@ -133,6 +161,8 @@ proc processPipelineModule*(graph: ModuleGraph; module: PSym; idgen: IdGenerator
         var n = parseTopLevelStmt(p)
         if n.kind == nkEmpty: break
         sl.add n
+
+      prePass(ctx, sl)
       if sfReorder in module.flags or codeReordering in graph.config.features:
         sl = reorder(graph, sl, module)
       if graph.pipelinePass != EvalPass:
diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim
index 97182abab..e51eb054b 100644
--- a/compiler/pragmas.nim
+++ b/compiler/pragmas.nim
@@ -270,7 +270,7 @@ proc onOff(c: PContext, n: PNode, op: TOptions, resOptions: var TOptions) =
   if isTurnedOn(c, n): resOptions.incl op
   else: resOptions.excl op
 
-proc pragmaNoForward(c: PContext, n: PNode; flag=sfNoForward) =
+proc pragmaNoForward*(c: PContext, n: PNode; flag=sfNoForward) =
   if isTurnedOn(c, n):
     incl(c.module.flags, flag)
     c.features.incl codeReordering
diff --git a/tests/misc/tnoforward.nim b/tests/misc/tnoforward.nim
index 0d9b40c83..b6a71897a 100644
--- a/tests/misc/tnoforward.nim
+++ b/tests/misc/tnoforward.nim
@@ -1,5 +1,4 @@
 discard """
-  matrix: "--experimental:codeReordering"
   output: "10"
 """
 
diff --git a/tests/modules/t8665.nim b/tests/modules/t8665.nim
index 7cfdbdb00..74d31452f 100644
--- a/tests/modules/t8665.nim
+++ b/tests/modules/t8665.nim
@@ -1,5 +1,4 @@
 discard """
-  matrix: "--experimental:codeReordering"
   action: compile
 """
 
diff --git a/tests/modules/treorder.nim b/tests/modules/treorder.nim
index 626d9684b..286b50e22 100644
--- a/tests/modules/treorder.nim
+++ b/tests/modules/treorder.nim
@@ -1,5 +1,5 @@
 discard """
-  matrix: "--experimental:codeReordering -d:testdef"
+  matrix: "-d:testdef"
   output: '''works 34
 34
 defined
diff --git a/tests/pragmas/treorder.nim b/tests/pragmas/treorder.nim
index 72e8808b3..09a98ef6a 100644
--- a/tests/pragmas/treorder.nim
+++ b/tests/pragmas/treorder.nim
@@ -1,5 +1,4 @@
 discard """
-matrix: "--experimental:codeReordering"
 output:'''0
 1
 2