add a prepass for codeReordering (#21513)

* add a prepass for codeReordering

* simplify

* fixes
This commit is contained in:
ringabout
2023-03-17 15:02:48 +08:00
committed by GitHub
parent b5ee81fd23
commit fd4e3ae3e4
6 changed files with 34 additions and 7 deletions

View File

@@ -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:

View File

@@ -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

View File

@@ -1,5 +1,4 @@
discard """
matrix: "--experimental:codeReordering"
output: "10"
"""

View File

@@ -1,5 +1,4 @@
discard """
matrix: "--experimental:codeReordering"
action: compile
"""

View File

@@ -1,5 +1,5 @@
discard """
matrix: "--experimental:codeReordering -d:testdef"
matrix: "-d:testdef"
output: '''works 34
34
defined

View File

@@ -1,5 +1,4 @@
discard """
matrix: "--experimental:codeReordering"
output:'''0
1
2