Skip to content

Commit

Permalink
Add code size in bytes to method dumps in OAT format
Browse files Browse the repository at this point in the history
  • Loading branch information
romainguy committed Oct 14, 2024
1 parent b7b0ede commit fbe51b9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import androidx.collection.IntIntPair
import androidx.collection.IntObjectMap
import androidx.collection.mutableIntIntMapOf
import androidx.collection.mutableIntObjectMapOf
import androidx.compose.ui.util.fastForEach
import androidx.compose.ui.util.fastSumBy
import kotlin.math.max

fun buildCode(codeStyle: CodeStyle = CodeStyle(), builderAction: CodeBuilder.() -> Unit): CodeBuilder {
Expand Down Expand Up @@ -85,14 +87,17 @@ class CodeBuilder(private val codeStyle: CodeStyle) {
sb.append(" ".repeat(codeStyle.indent))
writeLine(method.header)

sb.append(" ".repeat(codeStyle.indent))
val indent = " ".repeat(codeStyle.indent)

sb.append(indent)
val codeSize = method.codeSize
val instructionCount = method.instructionSet.instructions.size
writeLine("-- $instructionCount instruction${if (instructionCount > 1) "s" else ""}")
writeLine("-- $instructionCount instruction${if (instructionCount > 1) "s" else ""} ${if (codeSize >= 0) "($codeSize bytes)" else ""}")

val (pre, post) = countBranches(method.instructionSet)
val branches = pre + post
if (branches > 0) {
sb.append(" ".repeat(codeStyle.indent))
sb.append(indent)
writeLine("-- $branches branch${if (branches > 1) "es" else ""} ($pre + $post)")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ enum class ISA(val branchInstructions: ScatterSet<String>, val returnInstruction

data class Class(val header: String, val methods: List<Method>, val builtIn: Boolean)

data class Method(val header: String, val instructionSet: InstructionSet, val index: Int = -1)
data class Method(val header: String, val instructionSet: InstructionSet, val index: Int = -1, val codeSize: Int = -1)

data class InstructionSet(
val isa: ISA,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import dev.romainguy.kotlin.explorer.code.*
import dev.romainguy.kotlin.explorer.code.CodeContent.Error
import dev.romainguy.kotlin.explorer.code.CodeContent.Success

private val CodeStartRegex = Regex("^\\s+CODE: \\(code_offset=0x[a-fA-F0-9]+ size=(?<codeSize>\\d+)\\)[.]{3}")

private val ClassNameRegex = Regex("^\\d+: L(?<class>[^;]+); \\(offset=0x$HexDigit+\\) \\(type_idx=\\d+\\).+")
private val MethodRegex = Regex("^\\s+\\d+:\\s+(?<method>.+)\\s+\\(dex_method_idx=(?<methodIndex>\\d+)\\)")
private val CodeRegex = Regex("^\\s+0x(?<address>$HexDigit+):\\s+$HexDigit+\\s+(?<code>.+)")
Expand Down Expand Up @@ -119,13 +121,15 @@ internal class OatDumpParser {
consumeUntil("DEX CODE:")
val methodReferences = readMethodReferences()

consumeUntil("CODE:")
val codeStart = consumeUntil(CodeStartRegex)
val codeSize = codeStart?.getValue("codeSize")?.toInt() ?: -1

val instructions = readNativeInstructions(jumpRegex, methodCallRegex, builtIn)

val method = match.getValue("method")
val index = match.getValue("methodIndex").toInt()

return Method(method, InstructionSet(isa, instructions, methodReferences), index)
return Method(method, InstructionSet(isa, instructions, methodReferences), index, codeSize)
}

private fun PeekingIterator<String>.readMethodReferences(): IntObjectMap<MethodReference> {
Expand Down

0 comments on commit fbe51b9

Please sign in to comment.