Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/cpu/aarch64/assembler_aarch64.cpp
40930 views
1
/*
2
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2014, 2020 Red Hat Inc. All rights reserved.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*/
24
25
#include "precompiled.hpp"
26
#include "asm/assembler.hpp"
27
#include "asm/assembler.inline.hpp"
28
#include "asm/macroAssembler.hpp"
29
#include "compiler/disassembler.hpp"
30
#include "immediate_aarch64.hpp"
31
#include "memory/resourceArea.hpp"
32
33
#ifndef PRODUCT
34
const uintptr_t Assembler::asm_bp = 0x00007fffee09ac88;
35
#endif
36
37
static float unpack(unsigned value);
38
39
short Assembler::SIMD_Size_in_bytes[] = {
40
// T8B, T16B, T4H, T8H, T2S, T4S, T1D, T2D, T1Q
41
8, 16, 8, 16, 8, 16, 8, 16, 16
42
};
43
44
void Assembler::emit_data64(jlong data,
45
relocInfo::relocType rtype,
46
int format) {
47
if (rtype == relocInfo::none) {
48
emit_int64(data);
49
} else {
50
emit_data64(data, Relocation::spec_simple(rtype), format);
51
}
52
}
53
54
void Assembler::emit_data64(jlong data,
55
RelocationHolder const& rspec,
56
int format) {
57
58
assert(inst_mark() != NULL, "must be inside InstructionMark");
59
// Do not use AbstractAssembler::relocate, which is not intended for
60
// embedded words. Instead, relocate to the enclosing instruction.
61
code_section()->relocate(inst_mark(), rspec, format);
62
emit_int64(data);
63
}
64
65
extern "C" {
66
void das(uint64_t start, int len) {
67
ResourceMark rm;
68
len <<= 2;
69
if (len < 0)
70
Disassembler::decode((address)start + len, (address)start);
71
else
72
Disassembler::decode((address)start, (address)start + len);
73
}
74
75
JNIEXPORT void das1(uintptr_t insn) {
76
das(insn, 1);
77
}
78
}
79
80
#define __ as->
81
82
void Address::lea(MacroAssembler *as, Register r) const {
83
Relocation* reloc = _rspec.reloc();
84
relocInfo::relocType rtype = (relocInfo::relocType) reloc->type();
85
86
switch(_mode) {
87
case base_plus_offset: {
88
if (_offset == 0 && _base == r) // it's a nop
89
break;
90
if (_offset > 0)
91
__ add(r, _base, _offset);
92
else
93
__ sub(r, _base, -_offset);
94
break;
95
}
96
case base_plus_offset_reg: {
97
__ add(r, _base, _index, _ext.op(), MAX2(_ext.shift(), 0));
98
break;
99
}
100
case literal: {
101
if (rtype == relocInfo::none)
102
__ mov(r, target());
103
else
104
__ movptr(r, (uint64_t)target());
105
break;
106
}
107
default:
108
ShouldNotReachHere();
109
}
110
}
111
112
void Assembler::adrp(Register reg1, const Address &dest, uint64_t &byte_offset) {
113
ShouldNotReachHere();
114
}
115
116
#undef __
117
118
#define starti Instruction_aarch64 do_not_use(this); set_current(&do_not_use)
119
120
void Assembler::adr(Register Rd, address adr) {
121
intptr_t offset = adr - pc();
122
int offset_lo = offset & 3;
123
offset >>= 2;
124
starti;
125
f(0, 31), f(offset_lo, 30, 29), f(0b10000, 28, 24), sf(offset, 23, 5);
126
rf(Rd, 0);
127
}
128
129
void Assembler::_adrp(Register Rd, address adr) {
130
uint64_t pc_page = (uint64_t)pc() >> 12;
131
uint64_t adr_page = (uint64_t)adr >> 12;
132
intptr_t offset = adr_page - pc_page;
133
int offset_lo = offset & 3;
134
offset >>= 2;
135
starti;
136
f(1, 31), f(offset_lo, 30, 29), f(0b10000, 28, 24), sf(offset, 23, 5);
137
rf(Rd, 0);
138
}
139
140
#undef starti
141
142
Address::Address(address target, relocInfo::relocType rtype) : _mode(literal){
143
_is_lval = false;
144
_target = target;
145
switch (rtype) {
146
case relocInfo::oop_type:
147
case relocInfo::metadata_type:
148
// Oops are a special case. Normally they would be their own section
149
// but in cases like icBuffer they are literals in the code stream that
150
// we don't have a section for. We use none so that we get a literal address
151
// which is always patchable.
152
break;
153
case relocInfo::external_word_type:
154
_rspec = external_word_Relocation::spec(target);
155
break;
156
case relocInfo::internal_word_type:
157
_rspec = internal_word_Relocation::spec(target);
158
break;
159
case relocInfo::opt_virtual_call_type:
160
_rspec = opt_virtual_call_Relocation::spec();
161
break;
162
case relocInfo::static_call_type:
163
_rspec = static_call_Relocation::spec();
164
break;
165
case relocInfo::runtime_call_type:
166
_rspec = runtime_call_Relocation::spec();
167
break;
168
case relocInfo::poll_type:
169
case relocInfo::poll_return_type:
170
_rspec = Relocation::spec_simple(rtype);
171
break;
172
case relocInfo::none:
173
_rspec = RelocationHolder::none;
174
break;
175
default:
176
ShouldNotReachHere();
177
break;
178
}
179
}
180
181
void Assembler::b(const Address &dest) {
182
code_section()->relocate(pc(), dest.rspec());
183
b(dest.target());
184
}
185
186
void Assembler::bl(const Address &dest) {
187
code_section()->relocate(pc(), dest.rspec());
188
bl(dest.target());
189
}
190
191
void Assembler::adr(Register r, const Address &dest) {
192
code_section()->relocate(pc(), dest.rspec());
193
adr(r, dest.target());
194
}
195
196
void Assembler::br(Condition cc, Label &L) {
197
if (L.is_bound()) {
198
br(cc, target(L));
199
} else {
200
L.add_patch_at(code(), locator());
201
br(cc, pc());
202
}
203
}
204
205
void Assembler::wrap_label(Label &L,
206
Assembler::uncond_branch_insn insn) {
207
if (L.is_bound()) {
208
(this->*insn)(target(L));
209
} else {
210
L.add_patch_at(code(), locator());
211
(this->*insn)(pc());
212
}
213
}
214
215
void Assembler::wrap_label(Register r, Label &L,
216
compare_and_branch_insn insn) {
217
if (L.is_bound()) {
218
(this->*insn)(r, target(L));
219
} else {
220
L.add_patch_at(code(), locator());
221
(this->*insn)(r, pc());
222
}
223
}
224
225
void Assembler::wrap_label(Register r, int bitpos, Label &L,
226
test_and_branch_insn insn) {
227
if (L.is_bound()) {
228
(this->*insn)(r, bitpos, target(L));
229
} else {
230
L.add_patch_at(code(), locator());
231
(this->*insn)(r, bitpos, pc());
232
}
233
}
234
235
void Assembler::wrap_label(Label &L, prfop op, prefetch_insn insn) {
236
if (L.is_bound()) {
237
(this->*insn)(target(L), op);
238
} else {
239
L.add_patch_at(code(), locator());
240
(this->*insn)(pc(), op);
241
}
242
}
243
244
// An "all-purpose" add/subtract immediate, per ARM documentation:
245
// A "programmer-friendly" assembler may accept a negative immediate
246
// between -(2^24 -1) and -1 inclusive, causing it to convert a
247
// requested ADD operation to a SUB, or vice versa, and then encode
248
// the absolute value of the immediate as for uimm24.
249
void Assembler::add_sub_immediate(Register Rd, Register Rn, unsigned uimm, int op,
250
int negated_op) {
251
bool sets_flags = op & 1; // this op sets flags
252
union {
253
unsigned u;
254
int imm;
255
};
256
u = uimm;
257
bool shift = false;
258
bool neg = imm < 0;
259
if (neg) {
260
imm = -imm;
261
op = negated_op;
262
}
263
assert(Rd != sp || imm % 16 == 0, "misaligned stack");
264
if (imm >= (1 << 11)
265
&& ((imm >> 12) << 12 == imm)) {
266
imm >>= 12;
267
shift = true;
268
}
269
f(op, 31, 29), f(0b10001, 28, 24), f(shift, 23, 22), f(imm, 21, 10);
270
271
// add/subtract immediate ops with the S bit set treat r31 as zr;
272
// with S unset they use sp.
273
if (sets_flags)
274
zrf(Rd, 0);
275
else
276
srf(Rd, 0);
277
278
srf(Rn, 5);
279
}
280
281
bool Assembler::operand_valid_for_add_sub_immediate(int64_t imm) {
282
bool shift = false;
283
uint64_t uimm = (uint64_t)uabs((jlong)imm);
284
if (uimm < (1 << 12))
285
return true;
286
if (uimm < (1 << 24)
287
&& ((uimm >> 12) << 12 == uimm)) {
288
return true;
289
}
290
return false;
291
}
292
293
bool Assembler::operand_valid_for_logical_immediate(bool is32, uint64_t imm) {
294
return encode_logical_immediate(is32, imm) != 0xffffffff;
295
}
296
297
static uint64_t doubleTo64Bits(jdouble d) {
298
union {
299
jdouble double_value;
300
uint64_t double_bits;
301
};
302
303
double_value = d;
304
return double_bits;
305
}
306
307
bool Assembler::operand_valid_for_float_immediate(double imm) {
308
// If imm is all zero bits we can use ZR as the source of a
309
// floating-point value.
310
if (doubleTo64Bits(imm) == 0)
311
return true;
312
313
// Otherwise try to encode imm then convert the encoded value back
314
// and make sure it's the exact same bit pattern.
315
unsigned result = encoding_for_fp_immediate(imm);
316
return doubleTo64Bits(imm) == fp_immediate_for_encoding(result, true);
317
}
318
319
int AbstractAssembler::code_fill_byte() {
320
return 0;
321
}
322
323
// n.b. this is implemented in subclass MacroAssembler
324
void Assembler::bang_stack_with_offset(int offset) { Unimplemented(); }
325
326
327
// and now the routines called by the assembler which encapsulate the
328
// above encode and decode functions
329
330
uint32_t
331
asm_util::encode_logical_immediate(bool is32, uint64_t imm)
332
{
333
if (is32) {
334
/* Allow all zeros or all ones in top 32-bits, so that
335
constant expressions like ~1 are permitted. */
336
if (imm >> 32 != 0 && imm >> 32 != 0xffffffff)
337
return 0xffffffff;
338
/* Replicate the 32 lower bits to the 32 upper bits. */
339
imm &= 0xffffffff;
340
imm |= imm << 32;
341
}
342
343
return encoding_for_logical_immediate(imm);
344
}
345
346
unsigned Assembler::pack(double value) {
347
float val = (float)value;
348
unsigned result = encoding_for_fp_immediate(val);
349
guarantee(unpack(result) == value,
350
"Invalid floating-point immediate operand");
351
return result;
352
}
353
354
// Packed operands for Floating-point Move (immediate)
355
356
static float unpack(unsigned value) {
357
union {
358
unsigned ival;
359
float val;
360
};
361
ival = fp_immediate_for_encoding(value, 0);
362
return val;
363
}
364
365
address Assembler::locate_next_instruction(address inst) {
366
return inst + Assembler::instruction_size;
367
}
368
369