Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler: introduce an IR for the code generators #551

Merged
merged 21 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/ast/ast_query.nim
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ proc isSinkParam*(s: PSym): bool {.inline.} =
proc isSinkType*(t: PType): bool {.inline.} =
t.kind == tySink

const magicsThatCanRaise = {
const magicsThatCanRaise* = {
mNone, mSlurp, mStaticExec, mParseExprToAst, mParseStmtToAst, mEcho}

proc canRaiseConservative*(fn: PNode): bool =
Expand Down
42 changes: 0 additions & 42 deletions compiler/ast/ast_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -349,48 +349,6 @@ const
nkUsingStmt,
}

codegenExprNodeKinds* = {
nkEmpty,
nkSym,
nkType,

nkCharLit,
nkIntLit, nkInt8Lit, nkInt16Lit, nkInt32Lit, nkInt64Lit,
nkUIntLit, nkUInt8Lit, nkUInt16Lit, nkUInt32Lit, nkUInt64Lit,
nkFloatLit, nkFloat32Lit, nkFloat64Lit, nkFloat128Lit,
nkStrLit, nkRStrLit, nkTripleStrLit,
nkNilLit,

nkCall,

nkObjConstr, nkCurly, nkBracket,

nkBracketExpr, nkDotExpr, nkCheckedFieldExpr, nkDerefExpr,

nkHiddenStdConv, nkConv, nkCast, nkAddr, nkHiddenAddr,
nkHiddenDeref, nkObjDownConv, nkObjUpConv,

nkChckRangeF, nkChckRange64, nkChckRange, nkStringToCString,
nkCStringToString,

nkAsgn, nkFastAsgn,

nkAsmStmt, nkPragma,

nkIfStmt, nkWhileStmt, nkCaseStmt,

nkVarSection, nkLetSection,
nkTryStmt,

nkRaiseStmt, nkReturnStmt, nkBreakStmt, nkBlockStmt, nkDiscardStmt,

nkStmtList, nkStmtListExpr,

nkClosure,
nkTupleConstr,
nkNimNodeLit,
}

type
TSymFlag* = enum # 48 flags!
sfUsed ## read access of sym (for warnings) or simply used
Expand Down
13 changes: 7 additions & 6 deletions compiler/backend/backends.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import
lineinfos
],
compiler/backend/[
cgmeth
cgmeth,
cgir,
cgirgen
],
compiler/front/[
options
],
compiler/mir/[
astgen,
mirbridge,
mirconstr,
mirgen,
Expand Down Expand Up @@ -373,11 +374,11 @@ proc process(body: var MirFragment, ctx: PSym, graph: ModuleGraph,
## is used for the purpose of error reporting and debug tracing.
injectDestructorCalls(graph, idgen, ctx, body.tree, body.source)

proc generateAST*(graph: ModuleGraph, idgen: IdGenerator, owner: PSym,
code: sink MirFragment): PNode =
## Translates the MIR code provided by `code` into ``PNode`` AST and,
proc generateIR*(graph: ModuleGraph, idgen: IdGenerator, owner: PSym,
code: sink MirFragment): CgNode =
## Translates the MIR code provided by `code` into ``CgNode`` IR and,
## if enabled, echoes the result.
result = generateAST(graph, idgen, owner, code.tree, code.source)
result = generateIR(graph, idgen, owner, code.tree, code.source)
echoOutput(graph.config, owner, result)

# ------- handling of lifted globals ---------
Expand Down
20 changes: 13 additions & 7 deletions compiler/backend/cbackend.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,24 @@ import
tables
],
compiler/ast/[
ast,
ast_query,
ast_types,
lineinfos,
ndi
],
compiler/backend/[
backends,
cgen,
cgendata,
cgir,
compat,
extccomp
],
compiler/front/[
options
],
compiler/mir/[
mirbridge,
mirtrees
],
compiler/modules/[
Expand All @@ -67,6 +71,8 @@ import

import std/options as std_options

from compiler/ast/ast import id, newNode

# XXX: reports are a legacy facility that is going to be phased out. A
# note on how to move forward is left at each usage site in this
# module
Expand All @@ -78,7 +84,7 @@ type
InlineProc = object
## Information about an inline procedure.
sym: PSym
body: PNode
body: CgNode
## the fully processed body of the procedure

deps: PackedSet[uint32]
Expand Down Expand Up @@ -150,11 +156,11 @@ proc prepare(g: BModuleList, d: var DiscoveryData) =

# emit definitions for the lifted globals we've discovered:
for _, s in visit(d.globals):
defineGlobalVar(g.modules[moduleId(s)], newSymNode(s))
defineGlobalVar(g.modules[moduleId(s)], s)

for _, s in visit(d.threadvars):
let bmod = g.modules[moduleId(s)]
fillGlobalLoc(bmod, s, newSymNode(s))
fillGlobalLoc(bmod, s)
declareThreadVar(bmod, s, sfImportc in s.flags)

proc processEvent(g: BModuleList, inl: var InliningData, discovery: var DiscoveryData, partial: var Table[PSym, BProc], evt: sink BackendEvent) =
Expand Down Expand Up @@ -220,7 +226,7 @@ proc processEvent(g: BModuleList, inl: var InliningData, discovery: var Discover
p = startProc(g.modules[evt.module.int], evt.sym)
partial[evt.sym] = p

let body = generateAST(g.graph, bmod.idgen, evt.sym, evt.body)
let body = generateIR(g.graph, bmod.idgen, evt.sym, evt.body)
# emit into the procedure:
genStmts(p, body)

Expand All @@ -232,7 +238,7 @@ proc processEvent(g: BModuleList, inl: var InliningData, discovery: var Discover
# emit of the prototype in the case of self-recursion
bmod.declaredThings.incl(evt.sym.id)
let
body = generateAST(g.graph, bmod.idgen, evt.sym, evt.body)
body = generateIR(g.graph, bmod.idgen, evt.sym, evt.body)
p = startProc(bmod, evt.sym, body)

# we can't generate with ``genProc`` because we still need to output
Expand Down Expand Up @@ -326,7 +332,7 @@ proc generateCodeForMain(m: BModule, modules: ModuleList) =
generateTeardown(m.g.graph, modules, body)

# now generate the C code for the body:
genStmts(p, body)
genStmts(p, canonicalize(m.g.graph, m.idgen, m.module, body, {}))
var code: string
code.add(p.s(cpsLocals))
code.add(p.s(cpsInit))
Expand Down
Loading
Loading