Skip to content

Commit

Permalink
fix: Branching support for more types (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell committed Aug 8, 2024
1 parent 8f74e28 commit 5a5a0e7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
17 changes: 0 additions & 17 deletions .idea/gradle.xml

This file was deleted.

28 changes: 24 additions & 4 deletions src/main/kotlin/de/sirywell/handlehints/dfa/SsaAnalyzer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import de.sirywell.handlehints.foreign.MemoryLayoutHelper
import de.sirywell.handlehints.foreign.PathElementHelper
import de.sirywell.handlehints.mhtype.*
import de.sirywell.handlehints.type.*
import kotlin.reflect.KClass

class SsaAnalyzer(private val controlFlow: ControlFlow, val typeData: TypeData) {
companion object {
Expand Down Expand Up @@ -281,11 +282,22 @@ class SsaAnalyzer(private val controlFlow: ControlFlow, val typeData: TypeData)
noMatch()
}
}
// TODO is there a better way for this?
val resolver = HandleTypeResolver(this, block, BotMethodHandleType, MethodHandleType::class)
val resolver2 = HandleTypeResolver(this, block, BotVarHandleType, VarHandleType::class)
return withResolver(expression, block)
}

private fun withResolver(expression: PsiExpression, block: Block): TypeLatticeElement<*>? {
fun <T : TypeLatticeElement<T>> ter(block: Block, t: T, clazz: KClass<T>): TypeElementResolver<T> {
return TypeElementResolver(this, block, t, clazz)
}
val resolver = (when (expression.type) {
methodHandleType(expression) -> ter(block, BotMethodHandleType, MethodHandleType::class)
varHandleType(expression) -> ter(block, BotVarHandleType, VarHandleType::class)
pathElementType(expression) -> ter(block, BotPathElementType, PathElementType::class)
in memoryLayoutTypes(expression) -> ter(block, BotMemoryLayoutType, MemoryLayoutType::class)

else -> return noMatch()
})
expression.accept(resolver)
expression.accept(resolver2)
return resolver.result
}

Expand All @@ -301,10 +313,12 @@ class SsaAnalyzer(private val controlFlow: ControlFlow, val typeData: TypeData)
else if (arguments.size == 2) pathElementHelper.sequenceElement(arguments[0], arguments[1])
else noMatch()
}

"groupElement" -> {
if (arguments.size != 1) noMatch()
else pathElementHelper.groupElement(arguments[0])
}

else -> noMatch()
}
}
Expand All @@ -323,29 +337,35 @@ class SsaAnalyzer(private val controlFlow: ControlFlow, val typeData: TypeData)
if (arguments.size != 1 || qualifier == null) return noMatch()
memoryLayoutHelper.withName(qualifier, arguments[0], block)
}

"withOrder" -> qualifier?.type(block)
"withByteAlignment" -> {
if (arguments.size != 1 || qualifier == null) return noMatch()
memoryLayoutHelper.withByteAlignment(qualifier, arguments[0], block)
}

"structLayout" -> memoryLayoutHelper.structLayout(arguments, block)
"unionLayout" -> memoryLayoutHelper.unionLayout(arguments, block)
"sequenceLayout" -> {
if (arguments.size != 2) return noMatch()
memoryLayoutHelper.sequenceLayout(arguments[0], arguments[1], block)
}

"paddingLayout" -> {
if (arguments.size != 1) return noMatch()
memoryLayoutHelper.paddingLayout(arguments[0])
}

"scaleHandle" -> {
if (arguments.isNotEmpty()) return noMatch()
memoryLayoutHelper.scaleHandle()
}

"arrayElementVarHandle" -> {
if (qualifier == null) return noMatch()
memoryLayoutHelper.arrayElementVarHandle(qualifier, arguments, block)
}

"varHandle" -> {
if (qualifier == null) return noMatch()
memoryLayoutHelper.varHandle(qualifier, arguments, methodExpression, block)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import de.sirywell.handlehints.type.TypeLatticeElement
import kotlin.reflect.KClass
import kotlin.reflect.cast

class HandleTypeResolver<T : TypeLatticeElement<T>>(
class TypeElementResolver<T : TypeLatticeElement<T>>(
private val ssaAnalyzer: SsaAnalyzer,
private val block: SsaConstruction.Block,
private val bot: T,
private val clazz: KClass<T>
) :
JavaRecursiveElementVisitor() {
) : JavaRecursiveElementVisitor() {

var result: T? = null
private set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,11 @@ val WITHOUT_NAME = ExactLayoutName(null)

data class ExactLayoutName(val name: String?) : LayoutName {
override fun joinIdentical(other: LayoutName): Pair<LayoutName, TriState> {
if (other is ExactLayoutName && this.name == other.name) {
return this to TriState.YES
if (other is ExactLayoutName) {
if (this.name == other.name) {
return this to TriState.YES
}
return TopLayoutName to TriState.NO
}
if (other is TopLayoutName) return TopLayoutName to TriState.UNKNOWN
// BotLayoutName
Expand Down

0 comments on commit 5a5a0e7

Please sign in to comment.