Skip to content

Commit 2a95289

Browse files
authored
Merge pull request #30 from emerge-lang/debuginfo
Optionally emit debug info
2 parents 5907bb6 + 6765341 commit 2a95289

67 files changed

Lines changed: 2167 additions & 738 deletions

File tree

Some content is hidden

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

backend-api/src/main/kotlin/io/github/tmarsteel/emerge/backend/api/ir/basetype.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ interface IrClass : IrBaseType {
4444

4545
val destructor: IrFunction
4646

47+
val declaredAt: IrSourceLocation
48+
4749
interface MemberVariable {
4850
val name: String
4951

@@ -59,6 +61,8 @@ interface IrClass : IrBaseType {
5961
*/
6062
val writeStrategy: AccessStrategy
6163

64+
val declaredAt: IrSourceLocation
65+
6266
sealed interface AccessStrategy {
6367
/**
6468
* The member variable is supposed to be read/written by directly accessing the [Field] with [Field.id] == [BareField.fieldId].

backend-api/src/main/kotlin/io/github/tmarsteel/emerge/backend/api/ir/executable.kt

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface IrCreateTemporaryValue : IrExecutable {
2929
* that the mutation of the reference counter cannot be observed by the input program.
3030
*/
3131
interface IrCreateStrongReferenceStatement : IrExecutable {
32-
/** the temporary holding the reference to the object whichs reference count needs to increased */
32+
/** the temporary holding the reference to the object whose reference count needs to be increased */
3333
val reference: IrCreateTemporaryValue
3434
}
3535

@@ -67,6 +67,38 @@ interface IrVariableDeclaration : IrExecutable {
6767
* [isSSA] implies [isReAssignable] `== false`.
6868
*/
6969
val isSSA: Boolean
70+
71+
val declaredAt: IrSourceLocation
72+
73+
/**
74+
* The [Scope] in which this variable is valid.
75+
*/
76+
val scope: Scope
77+
78+
interface Scope {
79+
interface Lexical : Scope {
80+
val beginMarker: BeginMarker
81+
val endMarker: EndMarker
82+
83+
/**
84+
* No-op executable that just marks the start of a [Scope].
85+
*/
86+
interface BeginMarker : IrExecutable {
87+
val scope: Scope
88+
}
89+
90+
/**
91+
* No-op executable that just marks the end of a [Scope].
92+
*/
93+
interface EndMarker : IrExecutable {
94+
val scope: Scope
95+
}
96+
}
97+
98+
interface File : Scope {
99+
val file: IrSourceFile
100+
}
101+
}
70102
}
71103

72104
interface IrAssignmentStatement : IrExecutable {

backend-api/src/main/kotlin/io/github/tmarsteel/emerge/backend/noop/NoopBackend.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class NoopBackend : EmergeBackend<NoopBackend.Config, Unit> {
1919
projectConfig: Unit
2020
): Iterable<ConfigModuleDefinition> {
2121
return listOf(ConfigModuleDefinition(
22-
EmergeConstants.PLATFORM_MODULE_NAME,
22+
EmergeConstants.PlatformModule.NAME,
2323
toolchainConfig.platformSources,
2424
))
2525
}

bazel/rules_emerge/binary.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ def _build_project_config_json(
55
path_from_project_config_toolchain_cwd,
66
target_triple,
77
output_directory,
8-
all_modules):
8+
all_modules,
9+
emit_debug_info):
910
return {
1011
"modules": [{
1112
"name": module.name,
@@ -15,6 +16,7 @@ def _build_project_config_json(
1516
"targets": {
1617
target_triple: {
1718
"output-directory": path_from_project_config_toolchain_cwd + output_directory,
19+
"emit-debug-info": emit_debug_info,
1820
},
1921
},
2022
}
@@ -65,6 +67,7 @@ emerge_binary = rule(
6567
implementation = _emerge_binary_impl,
6668
attrs = {
6769
"root_module": attr.label(mandatory = True, allow_files = False, allow_rules = ["emerge_module_internal"]),
70+
"emit_debug_info": attr.bool(default = True, doc = "If true, the executable will contain debugging symbols (variable names and types). Line numbers and function names are always included."),
6871
"_triple": attr.label(
6972
default = "//target:selected_target_triple",
7073
),

common/src/main/kotlin/io/github/tmarsteel/emerge/common/EmergeConstants.kt

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,42 @@ import io.github.tmarsteel.emerge.common.EmergeConstants.IterableContract.RANGE_
77
* Necessary, "magic" constants that are chosen arbitrarily
88
*/
99
data object EmergeConstants {
10-
val CORE_MODULE_NAME = CanonicalElementName.Package(listOf("emerge", "core"))
11-
val STD_MODULE_NAME = CanonicalElementName.Package(listOf("emerge", "std"))
12-
val PLATFORM_MODULE_NAME = CanonicalElementName.Package(listOf("emerge", "platform"))
10+
data object CoreModule {
11+
val NAME = CanonicalElementName.Package(listOf("emerge", "core"))
12+
val ANY_TYPE_NAME = CanonicalElementName.BaseType(NAME, "Any")
13+
val UNIT_TYPE_NAME = CanonicalElementName.BaseType(NAME, "Unit")
14+
val THROWABLE_TYPE_NAME = CanonicalElementName.BaseType(NAME, "Throwable")
15+
val ERROR_TYPE_NAME = CanonicalElementName.BaseType(NAME, "Error")
16+
val CAST_ERROR_TYPE_NAME = CanonicalElementName.BaseType(NAME, "CastError")
17+
val ARITHMETIC_ERROR_TYPE_NAME = CanonicalElementName.BaseType(NAME, "ArithmeticError")
1318

14-
val UNIT_TYPE_NAME = CanonicalElementName.BaseType(CORE_MODULE_NAME, "Unit")
15-
val THROWABLE_TYPE_NAME = CanonicalElementName.BaseType(CORE_MODULE_NAME, "Throwable")
16-
val ERROR_TYPE_NAME = CanonicalElementName.BaseType(CORE_MODULE_NAME, "Error")
17-
val CAST_ERROR_TYPE_NAME = CanonicalElementName.BaseType(CORE_MODULE_NAME, "CastError")
19+
val BOOL_TYPE_NAME = CanonicalElementName.BaseType(NAME, "Bool")
20+
val S8_TYPE_NAME = CanonicalElementName.BaseType(NAME, "S8")
21+
val U8_TYPE_NAME = CanonicalElementName.BaseType(NAME, "U8")
22+
val S16_TYPE_NAME = CanonicalElementName.BaseType(NAME, "S16")
23+
val U16_TYPE_NAME = CanonicalElementName.BaseType(NAME, "U16")
24+
val S32_TYPE_NAME = CanonicalElementName.BaseType(NAME, "S32")
25+
val U32_TYPE_NAME = CanonicalElementName.BaseType(NAME, "U32")
26+
val S64_TYPE_NAME = CanonicalElementName.BaseType(NAME, "S64")
27+
val U64_TYPE_NAME = CanonicalElementName.BaseType(NAME, "U64")
28+
val F32_TYPE_NAME = CanonicalElementName.BaseType(NAME, "F32")
29+
val F64_TYPE_NAME = CanonicalElementName.BaseType(NAME, "F64")
30+
val SWORD_TYPE_NAME = CanonicalElementName.BaseType(NAME, "SWord")
31+
val UWORD_TYPE_NAME = CanonicalElementName.BaseType(NAME, "UWord")
32+
}
33+
34+
data object ReflectionModule {
35+
val NAME = CoreModule.NAME + "reflection"
36+
val REFLECTION_BASE_TYPE_TYPE_NAME = CanonicalElementName.BaseType(NAME, "ReflectionBaseType")
37+
}
38+
39+
data object StdModule {
40+
val NAME = CanonicalElementName.Package(listOf("emerge", "std"))
41+
}
42+
43+
data object PlatformModule {
44+
val NAME = CanonicalElementName.Package(listOf("emerge", "platform"))
45+
}
1846

1947
/**
2048
* the identifier to use in the initialization expression for member variables to indicate that the
@@ -51,7 +79,7 @@ data object EmergeConstants {
5179
* range is empty.
5280
*/
5381
val EMPTY_RANGE_EXCEPTION_NAME = CanonicalElementName.BaseType(
54-
CORE_MODULE_NAME.plus("range"),
82+
CoreModule.NAME.plus("range"),
5583
"EmptyRangeException",
5684
)
5785

@@ -60,7 +88,7 @@ data object EmergeConstants {
6088
* one type parameter: `T : Any?` which denotes the type of the elements that can be iterated over.
6189
*/
6290
val ITERABLE_TYPE_NAME = CanonicalElementName.BaseType(
63-
CORE_MODULE_NAME.plus("range"),
91+
CoreModule.NAME.plus("range"),
6492
"Iterable",
6593
)
6694
}

common/src/main/kotlin/io/github/tmarsteel/emerge/common/config/ConfigModuleDefinition.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ data class ConfigModuleDefinition(
1818
val uses: Set<CanonicalElementName.Package> = emptySet()
1919
) {
2020
val implicitlyUses: Set<CanonicalElementName.Package> = when (this.name) {
21-
EmergeConstants.CORE_MODULE_NAME -> emptySet()
22-
EmergeConstants.STD_MODULE_NAME -> setOf(EmergeConstants.CORE_MODULE_NAME)
23-
else -> setOf(EmergeConstants.CORE_MODULE_NAME, EmergeConstants.STD_MODULE_NAME)
21+
EmergeConstants.CoreModule.NAME -> emptySet()
22+
EmergeConstants.StdModule.NAME -> setOf(EmergeConstants.CoreModule.NAME)
23+
else -> setOf(EmergeConstants.CoreModule.NAME, EmergeConstants.StdModule.NAME)
2424
}
2525
}

frontend/src/main/kotlin/compiler/ast/ASTSourceFile.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import io.github.tmarsteel.emerge.common.CanonicalElementName
3737
import io.github.tmarsteel.emerge.common.EmergeConstants
3838

3939
val DEFAULT_IMPORT_PACKAGES = listOf(
40-
EmergeConstants.CORE_MODULE_NAME,
41-
EmergeConstants.STD_MODULE_NAME,
40+
EmergeConstants.CoreModule.NAME,
41+
EmergeConstants.StdModule.NAME,
4242
)
4343

4444
/**

frontend/src/main/kotlin/compiler/ast/expression/AstCatchBlockExpression.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class AstCatchBlockExpression(
2727
ownership = null,
2828
name = throwableVariableNameToken,
2929
type = AstAbsoluteTypeReference(
30-
EmergeConstants.THROWABLE_TYPE_NAME,
30+
EmergeConstants.CoreModule.THROWABLE_TYPE_NAME,
3131
mutability = TypeMutability.MUTABLE,
3232
span = throwableVariableNameToken.span,
3333
),

frontend/src/main/kotlin/compiler/binding/BoundVariable.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import compiler.diagnostic.visibilityNotAllowedOnVariable
5454
import compiler.handleCyclicInvocation
5555
import compiler.lexer.Span
5656
import io.github.tmarsteel.emerge.backend.api.ir.IrExecutable
57+
import io.github.tmarsteel.emerge.backend.api.ir.IrSourceLocation
5758
import io.github.tmarsteel.emerge.backend.api.ir.IrType
5859
import io.github.tmarsteel.emerge.backend.api.ir.IrVariableDeclaration
5960

@@ -269,12 +270,16 @@ class BoundVariable(
269270
isExternallyInitialized
270271
}
271272
}
273+
val scope = context.parentScopeContext
274+
?: throw InternalCompilerError("No parent scope context for variable $name at ${declaration.span.fileLineColumnText}")
272275
IrVariableDeclarationImpl(
273276
name,
274277
typeAtDeclarationTime!!.toBackendIr(),
275278
isBorrowed = ownershipAtDeclarationTime == VariableOwnership.BORROWED,
276279
isReAssignable = isReAssignable,
277280
isSSA = isSSA,
281+
declaredAt = declaration.declaredAt,
282+
scope = scope.irScope,
278283
)
279284
}
280285

@@ -618,4 +623,6 @@ private class IrVariableDeclarationImpl(
618623
override val isBorrowed: Boolean,
619624
override val isReAssignable: Boolean,
620625
override val isSSA: Boolean,
626+
override val declaredAt: IrSourceLocation,
627+
override val scope: IrVariableDeclaration.Scope
621628
) : IrVariableDeclaration

frontend/src/main/kotlin/compiler/binding/BoundVisibility.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private fun validateCrossModuleAccess(contextOfAccessedElement: CTContext, acces
212212
}
213213

214214
// always allow accessing emerge.platform
215-
if (EmergeConstants.PLATFORM_MODULE_NAME.containsOrEquals(moduleOfAccessedElement.moduleName)) {
215+
if (EmergeConstants.PlatformModule.NAME.containsOrEquals(moduleOfAccessedElement.moduleName)) {
216216
return
217217
}
218218

0 commit comments

Comments
 (0)