《深入理解Android虚拟机内存管理》示例程序编译阶段生成的各种语法树完整版

1.tokens

"int"                   "int"

<SPACES>                " "

<IDENTIFIER>            "global_a"

<SPACES>                " "

"="                     "="

<SPACES>                " "

<INTEGER>               "5"

";"                     ";"

<SPACES>                " "

"long"                  "long"

<SPACES>                " "

<IDENTIFIER>            "global_b"

";"                     ";"

<SPACES>                " "

"char"                  "char"

<SPACES>                " "

"*"                     "*"

<SPACES>                " "

<IDENTIFIER>            "global_c"

<SPACES>                " "

"="                     "="

<SPACES>                " "

"""                    ""HelloWorld!""

";"                     ";"

<SPACES>                " "

"int"                   "int"

"["                     "["

<INTEGER>               "5"

"]"                     "]"

<SPACES>                " "

<IDENTIFIER>            "global_array"

";"                     ";"

<SPACES>                " "

"struct"                "struct"

<SPACES>                " "

<IDENTIFIER>            "point"

<SPACES>                " "

<IDENTIFIER>            "global_struct"

";"                     ";"

<SPACES>                " "

"struct"                "struct"

<SPACES>                " "

<IDENTIFIER>            "point"

"{"                     "{"

<SPACES>                "   "

"int"                   "int"

<SPACES>                " "

<IDENTIFIER>            "x"

";"                     ";"

<SPACES>                "   "

"int"                   "int"

<SPACES>                " "

<IDENTIFIER>            "y"

";"                     ";"

<SPACES>                " "

"}"                     "}"

";"                     ";"

<SPACES>                " "

"int"                   "int"

<SPACES>                " "

<IDENTIFIER>            "func"

"("                     "("

"int"                   "int"

<SPACES>                " "

<IDENTIFIER>            "m"

","                     ","

<SPACES>                " "

"int"                   "int"

<SPACES>                " "

<IDENTIFIER>            "n"

")"                     ")"

"{"                     "{"

<SPACES>                "   "

"int"                   "int"

<SPACES>                " "

<IDENTIFIER>            "local_a"

<SPACES>                " "

"="                     "="

<SPACES>                " "

<INTEGER>               "4"

";"                     ";"

<SPACES>                "   "

<IDENTIFIER>            "local_a"

<SPACES>                " "

"="                     "="

<SPACES>                " "

<IDENTIFIER>            "m"

";"                     ";"

<SPACES>                "   "

<IDENTIFIER>            "local_a"

<SPACES>                " "

"="                     "="

<SPACES>                " "

<IDENTIFIER>            "n"

";"                     ";"

<SPACES>                "   "

"return"                "return"

<SPACES>                " "

<IDENTIFIER>            "local_a"

";"                     ";"

<SPACES>                " "

"}"                     "}"

<SPACES>                " "

"int"                   "int"

<SPACES>                " "

<IDENTIFIER>            "main"

"("                     "("

"int"                   "int"

<SPACES>                " "

<IDENTIFIER>            "argc"

","                     ","

<SPACES>                " "

"char"                  "char"

"*"                     "*"

"*"                     "*"

<SPACES>                " "

<IDENTIFIER>            "argv"

")"                     ")"

"{"                     "{"

<SPACES>                "   "

<IDENTIFIER>            "func"

"("                     "("

<INTEGER>               "2"

","                     ","

<INTEGER>               "3"

")"                     ")"

";"                     ";"

<SPACES>                "   "

"return"                "return"

<SPACES>                " "

<INTEGER>               "0"

";"                     ";"

<SPACES>                " "

"}"                     "}"

<SPACES>                " "

<EOF>                   ""

 

2.抽象语法树

<<AST>> (helloworld.cb:1)

variables:

    <<DefinedVariable>> (helloworld.cb:1)

    name: "global_a"

    isPrivate: false

    typeNode: int

    initializer:

        <<IntegerLiteralNode>> (helloworld.cb:1)

        typeNode: int

        value: 5

    <<DefinedVariable>> (helloworld.cb:2)

    name: "global_b"

    isPrivate: false

    typeNode: long

    initializer: null

    <<DefinedVariable>> (helloworld.cb:3)

    name: "global_c"

    isPrivate: false

    typeNode: char*

    initializer:

        <<StringLiteralNode>> (helloworld.cb:3)

        value: "HelloWorld!"

    <<DefinedVariable>> (helloworld.cb:4)

    name: "global_array"

    isPrivate: false

    typeNode: int[5]

    initializer: null

    <<DefinedVariable>> (helloworld.cb:5)

    name: "global_struct"

    isPrivate: false

    typeNode: struct point

    initializer: null

functions:

    <<DefinedFunction>> (helloworld.cb:10)

    name: "func"

    isPrivate: false

    params:

        parameters:

            <<Parameter>> (helloworld.cb:10)

            name: "m"

            typeNode: int

            <<Parameter>> (helloworld.cb:10)

            name: "n"

            typeNode: int

    body:

        <<BlockNode>> (helloworld.cb:10)

        variables:

            <<DefinedVariable>> (helloworld.cb:11)

            name: "local_a"

            isPrivate: false

            typeNode: int

            initializer:

                <<IntegerLiteralNode>> (helloworld.cb:11)

                typeNode: int

                value: 4

        stmts:

            <<ExprStmtNode>> (helloworld.cb:12)

            expr:

                <<AssignNode>> (helloworld.cb:12)

                lhs:

                    <<VariableNode>> (helloworld.cb:12)

                    name: "local_a"

                rhs:

                    <<VariableNode>> (helloworld.cb:12)

                    name: "m"

            <<ExprStmtNode>> (helloworld.cb:13)

            expr:

                <<AssignNode>> (helloworld.cb:13)

                lhs:

                    <<VariableNode>> (helloworld.cb:13)

                    name: "local_a"

                rhs:

                    <<VariableNode>> (helloworld.cb:13)

                    name: "n"

            <<ReturnNode>> (helloworld.cb:14)

            expr:

                <<VariableNode>> (helloworld.cb:14)

                name: "local_a"

    <<DefinedFunction>> (helloworld.cb:16)

    name: "main"

    isPrivate: false

    params:

        parameters:

            <<Parameter>> (helloworld.cb:16)

            name: "argc"

            typeNode: int

            <<Parameter>> (helloworld.cb:16)

            name: "argv"

            typeNode: char**

    body:

        <<BlockNode>> (helloworld.cb:16)

        variables:

        stmts:

            <<ExprStmtNode>> (helloworld.cb:17)

            expr:

                <<FuncallNode>> (helloworld.cb:17)

                expr:

                    <<VariableNode>> (helloworld.cb:17)

                    name: "func"

                args:

                    <<IntegerLiteralNode>> (helloworld.cb:17)

                    typeNode: int

                    value: 2

                    <<IntegerLiteralNode>> (helloworld.cb:17)

                    typeNode: int

                    value: 3

            <<ReturnNode>> (helloworld.cb:18)

            expr:

                <<IntegerLiteralNode>> (helloworld.cb:18)

                typeNode: int

                value: 0

 

3.中间代码

<<IR>> (helloworld.cb:1)

variables:

    <<DefinedVariable>> (helloworld.cb:1)

    name: global_a

    isPrivate: false

    type: int

    initializer:

        <<Int>>

        type: INT32

        value: 5

    <<DefinedVariable>> (helloworld.cb:2)

    name: global_b

    isPrivate: false

    type: long

    initializer: null

    <<DefinedVariable>> (helloworld.cb:3)

    name: global_c

    isPrivate: false

    type: char*

    initializer:

        <<Str>>

        type: INT32

        entry: net.loveruby.cflat.entity.ConstantEntry@13fd745

    <<DefinedVariable>> (helloworld.cb:4)

    name: global_array

    isPrivate: false

    type: int[5]

    initializer: null

    <<DefinedVariable>> (helloworld.cb:5)

    name: global_struct

    isPrivate: false

    type: struct point

    initializer: null

functions:

    <<DefinedFunction>> (helloworld.cb:10)

    name: func

    isPrivate: false

    type: int(int, int)

    body:

        <<Assign>> (helloworld.cb:11)

        lhs:

            <<Addr>>

            type: INT32

            entity: local_a

        rhs:

            <<Int>>

            type: INT32

            value: 4

        <<Assign>> (helloworld.cb:12)

        lhs:

            <<Addr>>

            type: INT32

            entity: local_a

        rhs:

            <<Var>>

            type: INT32

            entity: m

        <<Assign>> (helloworld.cb:13)

        lhs:

            <<Addr>>

            type: INT32

            entity: local_a

        rhs:

            <<Var>>

            type: INT32

            entity: n

        <<Return>> (helloworld.cb:14)

        expr:

            <<Var>>

            type: INT32

            entity: local_a

    <<DefinedFunction>> (helloworld.cb:16)

    name: main

    isPrivate: false

    type: int(int, char**)

    body:

        <<ExprStmt>> (helloworld.cb:17)

        expr:

            <<Call>>

            type: INT32

            expr:

                <<Addr>>

                type: INT32

                entity: func

            args:

                <<Int>>

                type: INT32

                value: 2

                <<Int>>

                type: INT32

                value: 3

        <<Return>> (helloworld.cb:18)

        expr:

            <<Int>>

            type: INT32

            value: 0

 4.汇编代码

.file "helloworld.cb"

.data

.globl global_a

.align 4

.type global_a,@object

.size global_a,4

global_a:

.long 5

.globl global_c

.align 4

.type global_c,@object

.size global_c,4

global_c:

.long .LC0

.section .rodata

.LC0:

.string "HelloWorld!"

.text

.globl func

.type func,@function

func:

pushl %ebp

movl %esp, %ebp

subl $4, %esp

movl $4, %eax

movl %eax, -4(%ebp)

movl 8(%ebp), %eax

movl %eax, -4(%ebp)

movl 12(%ebp), %eax

movl %eax, -4(%ebp)

movl -4(%ebp), %eax

jmp .L0

.L0:

movl %ebp, %esp

popl %ebp

ret

.size func,.-func

.globl main

.type main,@function

main:

pushl %ebp

movl %esp, %ebp

movl $3, %eax

pushl %eax

movl $2, %eax

pushl %eax

call func

addl $8, %esp

movl $0, %eax

jmp .L1

.L1:

movl %ebp, %esp

popl %ebp

ret

.size main,.-main

.comm global_b,4,4

.comm global_array,20,4

.comm global_struct,8,8

原文地址:https://www.cnblogs.com/joey-hua/p/6571998.html