summaryrefslogtreecommitdiffstats
path: root/libpixelflinger/codeflinger/x86/X86Assembler.h
blob: 03502d5756904f3b89683fa1c9ed107865d79514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* libs/pixelflinger/codeflinger/x86/X86Assembler.h
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#ifndef ANDROID_X86ASSEMBLER_H
#define ANDROID_X86ASSEMBLER_H

#include <stdint.h>
#include <sys/types.h>

#include <utils/Vector.h>
#include <utils/KeyedVector.h>

#include "codeflinger/tinyutils/smartpointer.h"
#include "codeflinger/CodeCache.h"
#include "enc_wrapper.h"

namespace android {

// ----------------------------------------------------------------------------

class X86Assembler
{
public:

    enum {
        EAX = PhysicalReg_EAX, EBX = PhysicalReg_EBX, ECX = PhysicalReg_ECX,
        EDX = PhysicalReg_EDX, EDI = PhysicalReg_EDI, ESI = PhysicalReg_ESI,
        ESP = PhysicalReg_ESP, EBP = PhysicalReg_EBP
    };

    X86Assembler(const sp<Assembly>& assembly);
    ~X86Assembler();

    char*   base() const;
    char*   pc() const;


    void        disassemble(const char* name);

    // ------------------------------------------------------------------------
    // X86AssemblerInterface...
    // ------------------------------------------------------------------------

    void    reset();

    int     generate(const char* name);

    void    comment(const char* string);

    void    label(const char* theLabel);

    void    JCC(Mnemonic cc, const char* label);

    void    JMP(const char* label);

    void    prepare_esp(int old_offset);

    void    update_esp(int new_offset);

    void    shrink_esp(int shrink_offset);

    void    callee_work();

    void    return_work();

    char*   pcForLabel(const char* label);

    void    PUSH(int reg);

    void    POP(int reg);

    void    ADD_REG_TO_REG(int src, int dst);
    void    ADD_IMM_TO_REG(int imm, int dst);
    void    ADD_IMM_TO_MEM(int imm, int disp, int dst);
    void    ADD_MEM_TO_REG(int base_reg, int disp, int dst);
    void    ADD_REG_TO_MEM(int src, int base_reg, int disp);
    void    SUB_REG_TO_REG(int src, int dst);
    void    SUB_IMM_TO_REG(int imm, int dst);
    void    SUB_IMM_TO_MEM(int imm, int disp, int dst);
    void    SUB_REG_TO_MEM(int src, int base_reg, int disp);

    void    TEST_REG_TO_REG(int src, int dst, OpndSize size=OpndSize_32);
    void    CMP_REG_TO_REG(int src, int dst, OpndSize size=OpndSize_32);
    void    CMP_MEM_TO_REG(int base_reg, int disp, int dst, OpndSize size=OpndSize_32);
    void    CMP_REG_TO_MEM(int reg, int disp, int base_reg, OpndSize size=OpndSize_32);
    void    CMP_IMM_TO_REG(int imm, int dst);

    void    AND_REG_TO_REG(int src, int dst);
    void    AND_IMM_TO_REG(int imm, int dst);
    void    OR_REG_TO_REG(int src, int dst);
    void    XOR(int src, int dst);
    void    OR_IMM_TO_REG(int imm, int dst);
    void    NOT(int dst);
    void    NEG(int dst);
    void    SHL(int imm, int dst);
    void    SHL(int imm, int disp, int dst);
    void    SHR(int imm, int dst);
    void    SHR(int imm, int disp, int dst);
    void    SAR(int imm, int dst);
    void    ROR(const int imm, int dst);
    void    ROR(int imm, int disp, int dst);
    void    IMUL(int reg);
    void    IMUL(int src, int dst);
    void    MUL(int reg);

    void    MOVSX_MEM_TO_REG(OpndSize size, int base_reg, int disp, int dst);
    void    MOVSX_REG_TO_REG(OpndSize size, int src, int dst);
    void    MOVZX_MEM_TO_REG(OpndSize size, int base_reg, int disp, int dst);
    void    MOVZX_REG_TO_REG(OpndSize size, int src, int dst);
    void    MOV_IMM_TO_REG(int32_t imm, int dst);
    void    MOV_REG_TO_REG(int src, int dst, OpndSize size=OpndSize_32);
    void    MOV_MEM_TO_REG(int disp, int base_reg, int reg, OpndSize size=OpndSize_32);
    void    MOV_REG_TO_MEM(int reg, int disp, int base_reg, OpndSize size=OpndSize_32);
    void    MOV_MEM_SCALE_TO_REG(int base_reg, int index_reg, int scale, int reg, OpndSize size=OpndSize_32);
    void    CMOV_REG_TO_REG(Mnemonic cc, int src, int dst, OpndSize size=OpndSize_32);
    void    CMOV_MEM_TO_REG(Mnemonic cc, int disp, int base_reg, int dst, OpndSize size=OpndSize_32);


    sp<Assembly>    mAssembly;
    char*           mBase;
    char*           mStream;
    //branch target offset is relative to the next instruction
    char*           mStreamNext;
    //updating esp after iterating the loop
    char*           mStreamUpdate;

    int64_t         mDuration;
#if defined(WITH_LIB_HARDWARE)
    bool            mQemuTracing;
#endif

    struct branch_target_t {
        inline branch_target_t() : label(0), pc(0), next_pc(0) { }
        inline branch_target_t(const char* l, char* p, char* next_p)
            : label(l), pc(p), next_pc(next_p) { }
        const char* label;
        char*   pc;
        char*   next_pc;
    };

    Vector<branch_target_t>             mBranchTargets;
    KeyedVector< const char*, char* >   mLabels;
    KeyedVector< char*, const char* >   mLabelsInverseMapping;
    KeyedVector< char*, const char* >   mComments;
};

}; // namespace android

#endif //ANDROID_X86ASSEMBLER_H