-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ryujin): virtual tables, analyzers, more statements, etc...
- Loading branch information
1 parent
aec92c9
commit 8d59af2
Showing
18 changed files
with
307 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package me.gabriel.ryujin.analyzer | ||
|
||
import me.gabriel.ryujin.statement.DragonStatement | ||
|
||
data class RyujinAnalysisError( | ||
val statement: DragonStatement, | ||
val message: String | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package me.gabriel.ryujin.analyzer | ||
|
||
import me.gabriel.ryujin.DragonModule | ||
import me.gabriel.ryujin.statement.DragonStatement | ||
|
||
/** | ||
* This analyzer is responsible for catching obvious flaws in LLVM IR. | ||
* It should be used to catch errors that are either not caught by the parser, or way too obvious to go unnoticed. | ||
*/ | ||
interface RyujinAnalyzer { | ||
fun analyze(module: DragonModule): List<RyujinAnalysisError > | ||
} | ||
|
40 changes: 40 additions & 0 deletions
40
ryujin/src/commonMain/kotlin/analyzer/impl/BalancedRyujinAnalyzer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package me.gabriel.ryujin.analyzer.impl | ||
|
||
import me.gabriel.ryujin.DragonModule | ||
import me.gabriel.ryujin.analyzer.RyujinAnalysisError | ||
import me.gabriel.ryujin.analyzer.RyujinAnalyzer | ||
import me.gabriel.ryujin.statement.DragonStatement | ||
import me.gabriel.ryujin.statement.StoreStatement | ||
import me.gabriel.ryujin.struct.DragonType | ||
|
||
/** | ||
* This is a balanced analyzer: neither too thorough nor too shallow, as to | ||
* provide a good balance between performance and error detection. | ||
*/ | ||
class BalancedRyujinAnalyzer: RyujinAnalyzer { | ||
override fun analyze(module: DragonModule): List<RyujinAnalysisError> { | ||
val errors = mutableListOf<RyujinAnalysisError>() | ||
module.functions.asSequence().flatMap { it.statements }.forEach { | ||
errors.addAll(analyzeStatement(it)) | ||
} | ||
return errors | ||
} | ||
|
||
private fun analyzeStatement(statement: DragonStatement): List<RyujinAnalysisError> = when (statement) { | ||
is StoreStatement -> analyzeStoreStatement(statement) | ||
else -> emptyList() | ||
} | ||
|
||
private fun analyzeStoreStatement(statement: StoreStatement): List<RyujinAnalysisError> { | ||
val errors = mutableListOf<RyujinAnalysisError>() | ||
if (statement.target.type !is DragonType.Pointer) { | ||
errors.add( | ||
RyujinAnalysisError( | ||
statement = statement, | ||
message = "Target is not a pointer: ${statement.target.type}" | ||
) | ||
) | ||
} | ||
return errors | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,46 @@ | ||
package me.gabriel.ryujin.dsl | ||
|
||
import me.gabriel.ryujin.function.DragonFunction | ||
import me.gabriel.ryujin.statement.DragonStatement | ||
import me.gabriel.ryujin.statement.* | ||
import me.gabriel.ryujin.struct.Memory | ||
import me.gabriel.ryujin.struct.Value | ||
|
||
class FunctionScopeDsl( | ||
val module: ModuleScopeDsl, | ||
val function: DragonFunction | ||
) { | ||
var register = function.parameters.size | ||
|
||
fun statement(statement: DragonStatement) { | ||
if (!statement.isValid()) { | ||
throw IllegalArgumentException("Invalid statement: $statement") | ||
} | ||
function.statements.add(statement) | ||
} | ||
|
||
fun load(target: Memory): LoadStatement = | ||
LoadStatement(target) | ||
|
||
fun add(left: Value, right: Value): AddStatement = | ||
AddStatement(left, right) | ||
|
||
fun assignment(target: Memory, value: TypedDragonStatement) { | ||
statement(AssignStatement(target, value)) | ||
} | ||
|
||
fun assign(value: (FunctionScopeDsl) -> TypedDragonStatement): Memory = | ||
value(this).assign() | ||
|
||
fun TypedDragonStatement.ignore() = | ||
statement(this) | ||
|
||
fun TypedDragonStatement.assign(): Memory { | ||
val memory = Memory.Sized( | ||
type = type, | ||
size = type.size, | ||
register = register++ | ||
) | ||
statement(AssignStatement(memory, this)) | ||
return memory | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package me.gabriel.ryujin.statement | ||
|
||
import me.gabriel.ryujin.struct.DragonType | ||
import me.gabriel.ryujin.struct.Value | ||
|
||
class AddStatement( | ||
val left: Value, | ||
val right: Value | ||
) : TypedDragonStatement { | ||
override val memoryDependencies: Set<Value> | ||
get() = setOf(left, right) | ||
|
||
override fun isValid(): Boolean { | ||
return left.type == right.type | ||
} | ||
|
||
override val type: DragonType get() = left.type | ||
|
||
override fun llvm(): String = | ||
"add ${type.llvm} ${left.llvm()}, ${right.llvm()}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.