Skip to content

Commit

Permalink
feat: new base intrinsics setup
Browse files Browse the repository at this point in the history
  • Loading branch information
SrGaabriel committed Sep 7, 2024
1 parent 9cd4280 commit 0cf076e
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package me.gabriel.selene.backend.common

interface SeleneCompilerBackend {
import me.gabriel.selene.backend.common.intrinsic.IntrinsicFunctionRepository

interface SeleneCompilerBackend<Context : Any> {
val intrinsics: IntrinsicFunctionRepository<Context>

fun compile(
module: SeleneCompilerModule
): String
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package me.gabriel.selene.backend.common.intrinsic

abstract class IntrinsicFunctionExecutor<T : IntrinsicFunction>(
val function: T
) {
abstract fun execute(): String
abstract class IntrinsicFunctionExecutor<Context : Any> {
abstract fun onCall(context: Context)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.gabriel.selene.backend.common.intrinsic

abstract class IntrinsicFunctionRepository<Context : Any> {
abstract val intrinsics: MutableMap<String, IntrinsicFunctionExecutor<Context>>

fun register(name: String, executor: IntrinsicFunctionExecutor<Context>) {
intrinsics[name] = executor
}

fun find(name: String): IntrinsicFunctionExecutor<Context>? {
return intrinsics[name]
}

fun unregister(name: String) {
intrinsics.remove(name)
}
}
6 changes: 5 additions & 1 deletion backend/llvm/src/commonMain/kotlin/DragonCompilerBackend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import me.gabriel.ryujin.transcript.DefaultDragonIrTranscriber
import me.gabriel.ryujin.transcript.DragonIrTranscriber
import me.gabriel.selene.backend.common.SeleneCompilerBackend
import me.gabriel.selene.backend.common.SeleneCompilerModule
import me.gabriel.selene.backend.common.intrinsic.IntrinsicFunctionRepository
import me.gabriel.selene.backend.llvm.intrinsic.DragonIntrinsicFunctionRepository
import me.gabriel.selene.backend.llvm.session.SeleneDragonCompilingSession

class DragonCompilerBackend: SeleneCompilerBackend {
class DragonCompilerBackend: SeleneCompilerBackend<DragonHookContext> {
var irTranscriber: DragonIrTranscriber = DefaultDragonIrTranscriber()

override val intrinsics: IntrinsicFunctionRepository<DragonHookContext> = DragonIntrinsicFunctionRepository()

override fun compile(module: SeleneCompilerModule): String {
// TODO: remove
if (module.stdlib) return ""
Expand Down
13 changes: 13 additions & 0 deletions backend/llvm/src/commonMain/kotlin/DragonHookContext.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.gabriel.selene.backend.llvm

import me.gabriel.ryujin.DragonModule
import me.gabriel.ryujin.dsl.FunctionScopeDsl
import me.gabriel.ryujin.dsl.ModuleScopeDsl
import me.gabriel.ryujin.function.DragonFunction

class DragonHookContext(
val module: DragonModule,
val function: DragonFunction,
val moduleDsl: ModuleScopeDsl,
val functionDsl: FunctionScopeDsl
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.gabriel.selene.backend.llvm.intrinsic

import me.gabriel.selene.backend.common.intrinsic.IntrinsicFunctionExecutor
import me.gabriel.selene.backend.common.intrinsic.IntrinsicFunctionRepository
import me.gabriel.selene.backend.llvm.DragonHookContext

class DragonIntrinsicFunctionRepository: IntrinsicFunctionRepository<DragonHookContext>() {
override val intrinsics: MutableMap<String, IntrinsicFunctionExecutor<DragonHookContext>> = mutableMapOf(

)
}
20 changes: 20 additions & 0 deletions backend/llvm/src/commonMain/kotlin/intrinsic/Print.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.gabriel.selene.backend.llvm.intrinsic

import me.gabriel.ryujin.struct.Constant
import me.gabriel.ryujin.struct.DragonType
import me.gabriel.selene.backend.common.intrinsic.IntrinsicFunctionExecutor
import me.gabriel.selene.backend.llvm.DragonHookContext

class PrintlnIntrinsicFunctionExecutor : IntrinsicFunctionExecutor<DragonHookContext>() {
override fun onCall(context: DragonHookContext) {
context.functionDsl.run {
callExternal(
functionName = "printf",
returnType = DragonType.Pointer(DragonType.Int8),
arguments = listOf(
Constant.DeclaredConstantPtr("%s\\0A"),
)
)
}
}
}
2 changes: 1 addition & 1 deletion compiler/src/commonMain/kotlin/SeleneCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import me.gabriel.selene.tools.*

class SeleneCompiler(
private val platform: SeleneCompilerPlatform,
private val backend: SeleneCompilerBackend,
private val backend: SeleneCompilerBackend<*>,
private val cli: CommandLine
) {
private val errorFormatter = ErrorFormatter(platform.logger)
Expand Down
21 changes: 21 additions & 0 deletions ryujin/src/commonMain/kotlin/dsl/FunctionScopeDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package me.gabriel.ryujin.dsl

import me.gabriel.ryujin.function.DragonFunction
import me.gabriel.ryujin.statement.*
import me.gabriel.ryujin.struct.DragonType
import me.gabriel.ryujin.struct.Memory
import me.gabriel.ryujin.struct.Value

Expand All @@ -28,6 +29,26 @@ class FunctionScopeDsl(
fun add(left: Value, right: Value): AddStatement =
AddStatement(left, right)

fun call(
functionName: String,
returnType: DragonType,
arguments: List<Value>
): CallStatement =
CallStatement(functionName, returnType, arguments)

fun callExternal(
functionName: String,
returnType: DragonType,
arguments: List<Value>
): CallStatement {
module.dependOnFunction(
name = functionName,
returnType = returnType,
parameters = arguments.map { it.type }
)
return call(functionName, returnType, arguments)
}

fun load(target: Memory): LoadStatement =
LoadStatement(target)

Expand Down
18 changes: 18 additions & 0 deletions ryujin/src/commonMain/kotlin/dsl/ModuleScopeDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ class ModuleScopeDsl: DragonModule {
dsl.block(parameterMemoryUnits)
}

fun dependOnFunction(
name: String,
returnType: DragonType,
parameters: List<DragonType>,
thoroughlyCheckForDuplicates: Boolean = true
) {
if (thoroughlyCheckForDuplicates) {
// The dependencies is already a set, but what if the hash codes differs due to same name but different parameters, etc?
val alreadyDependsOnFunction = dependencies.any { it is Dependency.Function && it.name == name }
if (alreadyDependsOnFunction) return
}
dependencies.add(Dependency.Function(
name = name,
returnType = returnType,
parameters = parameters
))
}

fun virtualTable(
name: String,
values: Collection<Value>
Expand Down
16 changes: 16 additions & 0 deletions ryujin/src/commonMain/kotlin/statement/Functions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.gabriel.ryujin.statement

import me.gabriel.ryujin.struct.DragonType
import me.gabriel.ryujin.struct.Value

class CallStatement(
val functionName: String,
override val type: DragonType,
val arguments: List<Value>
) : TypedDragonStatement {
override val memoryDependencies: Set<Value> = arguments.toSet()

override fun llvm(): String {
return "call ${type.llvm} @${functionName}(${arguments.joinToString { "${it.type.llvm} ${it.llvm()}" }})"
}
}
2 changes: 1 addition & 1 deletion ryujin/src/commonMain/kotlin/struct/Value.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class Constant(override val type: DragonType): Value {
override fun llvm(): kotlin.String = "$value"
}

class FunctionPtr(
class DeclaredConstantPtr(
val name: kotlin.String,
): Constant(
type = DragonType.Ptr
Expand Down
4 changes: 2 additions & 2 deletions ryujin/src/commonTest/kotlin/General.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class GeneralTest {
val module = ryujinModule {
virtualTable(
"my_vtable", listOf(
Constant.FunctionPtr("test"),
Constant.FunctionPtr("test2")
Constant.DeclaredConstantPtr("test"),
Constant.DeclaredConstantPtr("test2")
)
)
}
Expand Down

0 comments on commit 0cf076e

Please sign in to comment.