Skip to content

Instantly share code, notes, and snippets.

@xudifsd
Last active December 29, 2023 11:57
Show Gist options
  • Save xudifsd/7906589 to your computer and use it in GitHub Desktop.
Save xudifsd/7906589 to your computer and use it in GitHub Desktop.
output of smali AST tree
package com.example;
public class Complete {
public int i;
private String s;
public Complete(int i, String s) {
this.i = i;
this.s = s;
}
public void throwsEx() throws Exception {
if (i == 1)
throw new Exception("i is 1");
}
public int withWhile() {
int sum = 0;
int i = 0;
while (i < 10)
sum += i;
return sum;
}
public int withSwitch() {
int sum = 0;
switch (i) {
case 1:
return i;
case 2:
return i * 100;
case 3:
sum = i + 1;
/* fall through */
case 4:
sum = i + 2;
break;
default:
return i;
}
return sum;
}
}
.class public Lcom/example/Complete;
.super Ljava/lang/Object;
.source "Complete.java"
# instance fields
.field public i:I
.field private s:Ljava/lang/String;
# direct methods
.method public constructor <init>(ILjava/lang/String;)V
.locals 0
.parameter
.parameter
.prologue
.line 7
invoke-direct {p0}, Ljava/lang/Object;-><init>()V
.line 8
iput p1, p0, Lcom/example/Complete;->i:I
.line 9
iput-object p2, p0, Lcom/example/Complete;->s:Ljava/lang/String;
.line 10
return-void
.end method
# virtual methods
.method public throwsEx()V
.locals 2
.annotation system Ldalvik/annotation/Throws;
value = {
Ljava/lang/Exception;
}
.end annotation
.prologue
.line 13
iget v0, p0, Lcom/example/Complete;->i:I
const/4 v1, 0x1
if-ne v0, v1, :cond_0
.line 14
new-instance v0, Ljava/lang/Exception;
const-string v1, "i is 1"
invoke-direct {v0, v1}, Ljava/lang/Exception;-><init>(Ljava/lang/String;)V
throw v0
.line 15
:cond_0
return-void
.end method
.method public withSwitch()I
.locals 1
.prologue
.line 26
.line 27
iget v0, p0, Lcom/example/Complete;->i:I
packed-switch v0, :pswitch_data_0
.line 39
iget v0, p0, Lcom/example/Complete;->i:I
.line 41
:goto_0
return v0
.line 29
:pswitch_0
iget v0, p0, Lcom/example/Complete;->i:I
goto :goto_0
.line 31
:pswitch_1
iget v0, p0, Lcom/example/Complete;->i:I
mul-int/lit8 v0, v0, 0x64
goto :goto_0
.line 33
:pswitch_2
iget v0, p0, Lcom/example/Complete;->i:I
add-int/lit8 v0, v0, 0x1
.line 36
:pswitch_3
iget v0, p0, Lcom/example/Complete;->i:I
add-int/lit8 v0, v0, 0x2
.line 37
goto :goto_0
.line 27
nop
:pswitch_data_0
.packed-switch 0x1
:pswitch_0
:pswitch_1
:pswitch_2
:pswitch_3
.end packed-switch
.end method
.method public withWhile()I
.locals 0
.prologue
.line 18
.line 21
:goto_0
goto/32 :goto_0
.end method
package com.example;
public class Main {
public static void main(String[] args) {
Complete com = new Complete(1, "Hello world");
try {
com.throwsEx();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(com.withWhile());
System.out.println(com.withSwitch());
}
}
.class public Lcom/example/Main;
.super Ljava/lang/Object;
.source "Main.java"
# direct methods
.method public constructor <init>()V
.locals 0
.prologue
.line 3
invoke-direct {p0}, Ljava/lang/Object;-><init>()V
return-void
.end method
.method public static main([Ljava/lang/String;)V
.locals 3
.parameter
.prologue
.line 5
new-instance v0, Lcom/example/Complete;
const/4 v1, 0x1
const-string v2, "Hello world"
invoke-direct {v0, v1, v2}, Lcom/example/Complete;-><init>(ILjava/lang/String;)V
.line 7
:try_start_0
invoke-virtual {v0}, Lcom/example/Complete;->throwsEx()V
:try_end_0
.catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0
.line 11
:goto_0
sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream;
invoke-virtual {v0}, Lcom/example/Complete;->withWhile()I
move-result v2
invoke-virtual {v1, v2}, Ljava/io/PrintStream;->println(I)V
.line 12
sget-object v1, Ljava/lang/System;->out:Ljava/io/PrintStream;
invoke-virtual {v0}, Lcom/example/Complete;->withSwitch()I
move-result v0
invoke-virtual {v1, v0}, Ljava/io/PrintStream;->println(I)V
.line 13
return-void
.line 8
:catch_0
move-exception v1
.line 9
invoke-virtual {v1}, Ljava/lang/Exception;->printStackTrace()V
goto :goto_0
.end method
(I_CLASS_DEF
Lcom/example/Complete;
(I_ACCESS_LIST public)
(I_SUPER Ljava/lang/Object;)
(I_SOURCE "Complete.java")
(I_METHODS
(I_METHOD
<init>
(I_METHOD_PROTOTYPE
(I_METHOD_RETURN_TYPE V)
I Ljava/lang/String;)
(I_ACCESS_LIST public constructor)
(I_LOCALS 0)
I_LABELS
I_PACKED_SWITCH_DECLARATIONS
I_SPARSE_SWITCH_DECLARATIONS
(I_STATEMENTS
(I_STATEMENT_FORMAT35c_METHOD
invoke-direct
(I_REGISTER_LIST p0)
Ljava/lang/Object;
<init>
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE V)))
(I_STATEMENT_FORMAT22c_FIELD iput p1 p0 Lcom/example/Complete; i I)
(I_STATEMENT_FORMAT22c_FIELD iput-object p2 p0 Lcom/example/Complete; s Ljava/lang/String;)
(I_STATEMENT_FORMAT10x return-void))
I_CATCHES
(I_PARAMETERS (I_PARAMETER I_ANNOTATIONS) (I_PARAMETER I_ANNOTATIONS))
(I_ORDERED_DEBUG_DIRECTIVES (I_PROLOGUE 0) (I_LINE 7 0) (I_LINE 8 3) (I_LINE 9 5) (I_LINE 10 7))
I_ANNOTATIONS)
(I_METHOD throwsEx (I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE V))
(I_ACCESS_LIST public)
(I_LOCALS 2)
(I_LABELS (I_LABEL cond_0 13))
I_PACKED_SWITCH_DECLARATIONS
I_SPARSE_SWITCH_DECLARATIONS
(I_STATEMENTS
(I_STATEMENT_FORMAT22c_FIELD iget v0 p0 Lcom/example/Complete; i I)
(I_STATEMENT_FORMAT11n const/4 v1 0x1)
(I_STATEMENT_FFORMAT22t if-ne v0 v1 cond_0)
(I_STATEMENT_FORMAT21c new-instance v0 Ljava/lang/Exception;)
(I_STATEMENT_FORMAT21c_STRING const-string v1 "i is 1")
(I_STATEMENT_FORMAT35c_METHOD
invoke-direct
(I_REGISTER_LIST v0 v1)
Ljava/lang/Exception;
<init>
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE V) Ljava/lang/String;))
(I_STATEMENT_FORMAT11x throw v0)
(I_STATEMENT_FORMAT10x return-void))
I_CATCHES
I_PARAMETERS
(I_ORDERED_DEBUG_DIRECTIVES (I_PROLOGUE 0) (I_LINE 13 0) (I_LINE 14 5) (I_LINE 15 13))
(I_ANNOTATIONS
(I_ANNOTATION system (I_SUBANNOTATION Ldalvik/annotation/Throws; (I_ANNOTATION_ELEMENT value (I_ENCODED_ARRAY Ljava/lang/Exception;))))))
(I_METHOD
withSwitch
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE I))
(I_ACCESS_LIST public)
(I_LOCALS 1)
(I_LABELS (I_LABEL goto_0 7) (I_LABEL pswitch_0 8) (I_LABEL pswitch_1 11) (I_LABEL pswitch_2 16) (I_LABEL pswitch_3 20) (I_LABEL pswitch_data_0 26))
(I_PACKED_SWITCH_DECLARATIONS (I_PACKED_SWITCH_DECLARATION 2 pswitch_data_0))
I_SPARSE_SWITCH_DECLARATIONS
(I_STATEMENTS
(I_STATEMENT_FORMAT22c_FIELD iget v0 p0 Lcom/example/Complete; i I)
(I_STATEMENT_FORMAT31t packed-switch v0 pswitch_data_0)
(I_STATEMENT_FORMAT22c_FIELD iget v0 p0 Lcom/example/Complete; i I)
(I_STATEMENT_FORMAT11x return v0)
(I_STATEMENT_FORMAT22c_FIELD iget v0 p0 Lcom/example/Complete; i I)
(I_STATEMENT_FORMAT10t goto goto_0)
(I_STATEMENT_FORMAT22c_FIELD iget v0 p0 Lcom/example/Complete; i I)
(I_STATEMENT_FORMAT22b mul-int/lit8 v0 v0 0x64)
(I_STATEMENT_FORMAT10t goto goto_0)
(I_STATEMENT_FORMAT22c_FIELD iget v0 p0 Lcom/example/Complete; i I)
(I_STATEMENT_FORMAT22b add-int/lit8 v0 v0 0x1)
(I_STATEMENT_FORMAT22c_FIELD iget v0 p0 Lcom/example/Complete; i I)
(I_STATEMENT_FORMAT22b add-int/lit8 v0 v0 0x2)
(I_STATEMENT_FORMAT10t goto goto_0)
(I_STATEMENT_FORMAT10x nop)
(I_STATEMENT_PACKED_SWITCH
(I_PACKED_SWITCH_START_KEY 0x1)
(I_PACKED_SWITCH_TARGETS 4 pswitch_0 pswitch_1 pswitch_2 pswitch_3)))
I_CATCHES
I_PARAMETERS
(I_ORDERED_DEBUG_DIRECTIVES (I_PROLOGUE 0) (I_LINE 26 0) (I_LINE 27 0) (I_LINE 39 5) (I_LINE 41 7) (I_LINE 29 8) (I_LINE 31 11) (I_LINE 33 16) (I_LINE 36 20) (I_LINE 37 24) (I_LINE 27 25))
I_ANNOTATIONS)
(I_METHOD
withWhile
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE I))
(I_ACCESS_LIST public)
(I_LOCALS 0)
(I_LABELS (I_LABEL goto_0 0))
I_PACKED_SWITCH_DECLARATIONS
I_SPARSE_SWITCH_DECLARATIONS
(I_STATEMENTS (I_STATEMENT_FORMAT30t goto/32 goto_0))
I_CATCHES
I_PARAMETERS
(I_ORDERED_DEBUG_DIRECTIVES (I_PROLOGUE 0) (I_LINE 18 0) (I_LINE 21 0))
I_ANNOTATIONS))
(I_FIELDS (I_FIELD i (I_ACCESS_LIST public) (I_FIELD_TYPE I) I_ANNOTATIONS) (I_FIELD s (I_ACCESS_LIST private) (I_FIELD_TYPE Ljava/lang/String;) I_ANNOTATIONS))
I_ANNOTATIONS)
(I_CLASS_DEF
Lcom/example/Main;
(I_ACCESS_LIST public)
(I_SUPER Ljava/lang/Object;)
(I_SOURCE "Main.java")
(I_METHODS
(I_METHOD
<init>
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE V))
(I_ACCESS_LIST public constructor)
(I_LOCALS 0)
I_LABELS
I_PACKED_SWITCH_DECLARATIONS
I_SPARSE_SWITCH_DECLARATIONS
(I_STATEMENTS
(I_STATEMENT_FORMAT35c_METHOD
invoke-direct
(I_REGISTER_LIST p0)
Ljava/lang/Object;
<init>
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE V)))
(I_STATEMENT_FORMAT10x return-void))
I_CATCHES
I_PARAMETERS
(I_ORDERED_DEBUG_DIRECTIVES (I_PROLOGUE 0) (I_LINE 3 0))
I_ANNOTATIONS)
(I_METHOD
main
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE V) [Ljava/lang/String;)
(I_ACCESS_LIST public static)
(I_LOCALS 3)
(I_LABELS (I_LABEL try_start_0 8) (I_LABEL try_end_0 11) (I_LABEL goto_0 11) (I_LABEL catch_0 30))
I_PACKED_SWITCH_DECLARATIONS
I_SPARSE_SWITCH_DECLARATIONS
(I_STATEMENTS
(I_STATEMENT_FORMAT21c new-instance v0 Lcom/example/Complete;)
(I_STATEMENT_FORMAT11n const/4 v1 0x1)
(I_STATEMENT_FORMAT21c_STRING const-string v2 "Hello world")
(I_STATEMENT_FORMAT35c_METHOD
invoke-direct
(I_REGISTER_LIST v0 v1 v2)
Lcom/example/Complete;
<init>
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE V) I Ljava/lang/String;))
(I_STATEMENT_FORMAT35c_METHOD
invoke-virtual
(I_REGISTER_LIST v0)
Lcom/example/Complete;
throwsEx
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE V)))
(I_STATEMENT_FORMAT21c_FIELD sget-object v1 Ljava/lang/System; out Ljava/io/PrintStream;)
(I_STATEMENT_FORMAT35c_METHOD
invoke-virtual
(I_REGISTER_LIST v0)
Lcom/example/Complete;
withWhile
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE I)))
(I_STATEMENT_FORMAT11x move-result v2)
(I_STATEMENT_FORMAT35c_METHOD
invoke-virtual
(I_REGISTER_LIST v1 v2)
Ljava/io/PrintStream;
println
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE V) I))
(I_STATEMENT_FORMAT21c_FIELD sget-object v1 Ljava/lang/System; out Ljava/io/PrintStream;)
(I_STATEMENT_FORMAT35c_METHOD
invoke-virtual
(I_REGISTER_LIST v0)
Lcom/example/Complete;
withSwitch
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE I)))
(I_STATEMENT_FORMAT11x move-result v0)
(I_STATEMENT_FORMAT35c_METHOD
invoke-virtual
(I_REGISTER_LIST v1 v0)
Ljava/io/PrintStream;
println
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE V) I))
(I_STATEMENT_FORMAT10x return-void)
(I_STATEMENT_FORMAT11x move-exception v1)
(I_STATEMENT_FORMAT35c_METHOD
invoke-virtual
(I_REGISTER_LIST v1)
Ljava/lang/Exception;
printStackTrace
(I_METHOD_PROTOTYPE (I_METHOD_RETURN_TYPE V)))
(I_STATEMENT_FORMAT10t goto goto_0)
(I_CATCHES (I_CATCH 11 Ljava/lang/Exception; try_start_0 try_end_0 catch_0))
(I_PARAMETERS (I_PARAMETER I_ANNOTATIONS))
(I_ORDERED_DEBUG_DIRECTIVES (I_PROLOGUE 0) (I_LINE 5 0) (I_LINE 7 8) (I_LINE 11 11) (I_LINE 12 20) (I_LINE 13 29) (I_LINE 8 30) (I_LINE 9 31))
I_ANNOTATIONS))
I_FIELDS
I_ANNOTATIONS)
@zymeng
Copy link

zymeng commented Mar 3, 2015

Hi ! Thank you for your code ! I have already found the smaliParser . But I have a question to ask you . If each source code file has one AST or not? If existing a very big AST which can include all the AST or not? I am not good at it , could you please help me ?

@zymeng
Copy link

zymeng commented Mar 3, 2015

And I also do not kown how to traverse the AST , so I can not output it . Can you tell me how to modify the code ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment