/
emit-aarch64.C
195 lines (153 loc) · 6.01 KB
/
emit-aarch64.C
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* See the dyninst/COPYRIGHT file for copyright information.
*
* We provide the Paradyn Tools (below described as "Paradyn")
* on an AS IS basis, and do not warrant its validity or performance.
* We reserve the right to update, modify, or discontinue this
* software at any time. We shall have no obligation to supply such
* updates or modifications or any other form of support to you.
*
* By your use of Paradyn, you understand and agree that we (or any
* other person or entity with proprietary rights in Paradyn) are
* under no obligation to provide either maintenance services,
* update services, notices of latent defects, or correction of
* defects for Paradyn.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* emit-aarch64.C - ARMv8 code generators (emitters)
*/
/*
#include <assert.h>
#include <stdio.h>
#include "common/src/Types.h"
#include "dyninstAPI/src/codegen.h"
#include "dyninstAPI/src/function.h"
#include "dyninstAPI/src/inst-x86.h"
#include "dyninstAPI/src/debug.h"
#include "dyninstAPI/src/ast.h"
#include "dyninstAPI/h/BPatch.h"
#include "dyninstAPI/h/BPatch_memoryAccess_NP.h"
#include "dyninstAPI/src/dynProcess.h"
#include "dyninstAPI/src/binaryEdit.h"
#include "dyninstAPI/src/image.h"
// get_index...
#include "dyninstAPI/src/dynThread.h"
#include "ABI.h"
#include "liveness.h"
#include "RegisterConversion.h"
*/
#include "dyninstAPI/src/emit-aarch64.h"
#include "dyninstAPI/src/registerSpace.h"
codeBufIndex_t EmitterAARCH64::emitIf(
Register expr_reg, Register target, RegControl /*rc*/, codeGen &gen)
{
instruction insn;
insn.clear();
// compare to 0 and branch
// register number, its value is compared to 0.
INSN_SET(insn, 0, 4, expr_reg);
INSN_SET(insn, 5, 23, (target+4)/4);
INSN_SET(insn, 25, 30, 0x1a); // CBZ
INSN_SET(insn, 31, 31, 1);
insnCodeGen::generate(gen,insn);
// Retval: where the jump is in this sequence
codeBufIndex_t retval = gen.getIndex();
return retval;
}
void EmitterAARCH64::emitLoadConst(Register dest, Address imm, codeGen &gen)
{
insnCodeGen::loadImmIntoReg<Address>(gen, dest, imm);
}
void EmitterAARCH64::emitLoad(Register dest, Address addr, int size, codeGen &gen)
{
Register scratch = gen.rs()->getScratchRegister(gen);
insnCodeGen::loadImmIntoReg<Address>(gen, scratch, addr);
insnCodeGen::generateMemAccess32or64(gen, insnCodeGen::Load, dest, scratch, 0, true);
gen.rs()->freeRegister(scratch);
gen.markRegDefined(dest);
}
void EmitterAARCH64::emitStore(Address addr, Register src, int size, codeGen &gen)
{
Register scratch = gen.rs()->getScratchRegister(gen);
insnCodeGen::loadImmIntoReg<Address>(gen, scratch, addr);
insnCodeGen::generateMemAccess32or64(gen, insnCodeGen::Store, src, scratch, 0, true);
gen.rs()->freeRegister(scratch);
gen.markRegDefined(src);
}
void EmitterAARCH64::emitOp(
unsigned opcode, Register dest, Register src1, Register src2, codeGen &gen)
{
// dest = src1 + src2
if( opcode == plusOp )
insnCodeGen::generateAddSubShifted(gen, insnCodeGen::Add, 0, 0, src1, src2, dest, true);
// dest = src1 - src2
else if( opcode == minusOp )
insnCodeGen::generateAddSubShifted(gen, insnCodeGen::Sub, 0, 0, src1, src2, dest, true);
// dest = src1 / src2
else if( opcode == divOp )
insnCodeGen::generateDiv(gen, src1, src2, dest, true);
// dest = src1 * src2
else if( opcode == timesOp )
insnCodeGen::generateMul(gen, src1, src2, dest, true);
// dest = src1 & src2
else if( opcode == andOp )
insnCodeGen::generateBitwiseOpShifted(gen, insnCodeGen::And, 0, src1, 0, src2, dest, true);
// dest = src1 | src2
else if( opcode == orOp )
insnCodeGen::generateBitwiseOpShifted(gen, insnCodeGen::Or, 0, src1, 0, src2, dest, true);
}
void EmitterAARCH64::emitRelOp(
unsigned opcode, Register dest, Register src1, Register src2, codeGen &gen)
{
// CMP is an alias to SUBS;
// dest here has src1-src2, which it's not important because the flags are
// used for the comparison, not the subtration value.
// Besides that dest must contain 1 for true or 0 for false, and the content
// of dest is gonna be changed as follow.
insnCodeGen::generateAddSubShifted(gen, insnCodeGen::Sub, 0, 0, src2, src1, dest, true);
// make dest = 1, meaning true
insnCodeGen::loadImmIntoReg<Address>(gen, dest, 0x0);
// insert conditional jump to skip dest=0 in case the comparison resulted true
// therefore keeping dest=1
insnCodeGen::generateConditionalBranch(gen, 8, opcode);
// make dest = 0, in case it fails the branch
insnCodeGen::loadImmIntoReg<Address>(gen, dest, 0x1);
}
void EmitterAARCH64::emitGetParam(
Register dest, Register param_num,
instPoint::Type pt_type, opCode op,
bool addr_of, codeGen &gen)
{
registerSlot *regSlot = NULL;
switch (op) {
case getParamOp:
if(param_num <= 3) {
// param_num is 0..8 - it's a parameter number, not a register
regSlot = (*(gen.rs()))[registerSpace::r0 + param_num];
break;
} else {
assert(0);
}
break;
default:
assert(0);
break;
} // end of swich(op)
assert(regSlot);
Register reg = regSlot->number;
//return reg;
}