Skip to content

Commit

Permalink
Use maps instead of arrays for branch lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
romainguy committed May 16, 2024
1 parent 2c58dcc commit 38d40e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@ class CodeBuilder(private val codeStyle: CodeStyle) {
var count = 0
instructionSet.instructions.forEach { instruction ->
val code = instruction.code
val index = code.indexOf(": ")
instructionSet.isa.branchInstructions.forEach out@ { opCode ->
if (code.startsWith(opCode, index + 2)) {
count++
return@out
}
val start = code.indexOf(": ") + 2
val end = code.indexOfFirst(start) { c -> !c.isLetter() }
val opCode = code.substring(start, end)
if (instructionSet.isa.branchInstructions.contains(opCode)) {
count++
}
}
return count
Expand Down Expand Up @@ -115,4 +114,14 @@ class CodeBuilder(private val codeStyle: CodeStyle) {
sb.append('\n')
line++
}
}
}

private inline fun CharSequence.indexOfFirst(start: Int, predicate: (Char) -> Boolean): Int {
val end = length
for (index in start..<end ) {
if (predicate(this[index])) {
return index
}
}
return end
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package dev.romainguy.kotlin.explorer.code

enum class ISA(val branchInstructions: Array<String>) {
ByteCode(arrayOf("if")),
Dex(arrayOf("if-")),
import androidx.collection.ScatterSet
import androidx.collection.scatterSetOf

enum class ISA(val branchInstructions: ScatterSet<String>) {
ByteCode(scatterSetOf("if")),
Dex(scatterSetOf("if")),
X86_64(
arrayOf(
scatterSetOf(
"je",
"jz",
"jne",
Expand All @@ -45,7 +48,7 @@ enum class ISA(val branchInstructions: Array<String>) {
"jna"
)
),
Arm64(arrayOf("b.", "b ", "bl", "cbz", "cbnz", "tbz", "tbnz"))
Arm64(scatterSetOf("b", "bl", "cbz", "cbnz", "tbz", "tbnz"))
}

data class Class(val header: String, val methods: List<Method>)
Expand Down

0 comments on commit 38d40e0

Please sign in to comment.