Skip to content

Commit 48990d7

Browse files
zerbinasaem
andauthored
implement a new CGIR and C code generator (#1625)
## Summary Add an all new CGIR, together with a new code generation architecture using it and a new C code generator. ## Details ### New CGIR Core design decisions for the language: * structured control-flow: makes translation to Wasm/`asm.js` easier * syntax that resembles NimSkull/C: familiar to the someone that already knows NimSkull and its AST * statically and strongly typed: makes debugging and analysis easier * narrow(er) operations: keep decision making out of the code generators * built-in simple exception handling Core design decisions for the IR: * packed AST for storage; terminals are stored out-of-band and are interned * every node carries source location information, to make high precision debug information possible * one IR for everything: types, statements, expressions The IR also comes with a grammar and type checker, to help with debugging, troubleshooting, and codifying the static semantics. It's always built into the compiler, but due to its overhead, has to be enabled at run-time by passing `-d:validateCgir` to the compiler. The old CGIR is still used for the JS and VM code generators, and thus has to be kept for now. ### New Architecture The intertwined MIR -> CGIR -> C translation is replaced with separate MIR -> CGIR and CGIR -> C translation steps. This allows reusing the MIR -> CGIR parts for other code generators. Code generator may only support a subset/dialect of the CGIR, which the MIR -> CGIR facilitates by accepting a set of code generator capabilities. As a preparation for incremental compilation, the CGIR has support for being split multiple units (i.e., modules), though this feature is not actually used right now. ### Breaking Changes * features: TLS emulation is not supported anymore. The switch still exists, but enabling emulation now causes an error * features: header generation is not supported anymore * C FFI: field symbols cannot be used in `emit` statements anymore ### Changes To The Produced C Code * C scopes reflect the NimSkull-level scopes * immutable pass-by-reference parameters are marked with `NIM_NOALIAS` * error handling in `.compilerprocs` is not omitted anymore * except for returning array values, out parameters are not used anymore * large set operations are implemented as runtime procedures * updating the `TFrame` instances doesn't use C macros anymore * non-inline routines are emitted in the order they're processed by sem (which is roughly the order they appear in the source code) * RTTI for nominal types is defined in the nominal type's home module * RTTI for structural types is defined in the project's entry module * RTTI is initialized using static initializer expressions ### Tests * split up `thard_alignment.nim` so that the parts not requiring AVX can be run on non-amd64 systems * change `thard_alignment.nim` to use `-mavx` instead of `-march=native` , so that the test is reproducible --------- Co-authored-by: Saem Ghani <[email protected]>
1 parent ba2b4d1 commit 48990d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+9194
-6746
lines changed

compiler/ast/ast_types.nim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,6 @@ const
732732

733733
tfUnion* = tfNoSideEffect
734734
tfGcSafe* = tfThread
735-
tfObjHasKids* = tfEnumHasHoles
736735
tfReturnsNew* = tfInheritable
737736
skError* = skUnknown
738737

compiler/ast/ndi.nim

Lines changed: 0 additions & 80 deletions
This file was deleted.

compiler/ast/report_enums.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ type
639639
rsemRequiresDeepCopyEnabled
640640
rsemDisallowedOfForPureObjects
641641
rsemCannotCodegenCompiletimeProc
642+
rsemNameCollision
642643

643644
# Pragma
644645
rsemInvalidPragma
@@ -849,6 +850,8 @@ type
849850
rbackCannotWriteMappingFile ## Cannot write module compilation mapping
850851
## file to cache directory
851852
rbackTargetNotSupported ## C compiler does not support requested target
853+
rbackTlsEmulationNotImplemented
854+
rbackHeaderGenerationNotImplemented
852855
rbackJsTooCaseTooLarge
853856
rbackJsonScriptMismatch # ??? used in `extccomp.nim`, TODO figure out
854857
# what the original mesage was responsible for exactly

compiler/ast/types.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,10 @@ proc isPassByRef*(conf: ConfigRef; s: PSym, retType: PType): bool =
16561656
# XXX: this is a C code generator implementation detail leaking into the
16571657
# language semantics
16581658
result = true
1659+
of tySet:
1660+
# XXX: also a C code generator implementation detail
1661+
# passed by reference when its a large set (which is an array underneath)
1662+
result = lengthOrd(conf, pt) > 64
16591663
else:
16601664
result = false
16611665

0 commit comments

Comments
 (0)