1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=79:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Copyright (C) 2009 University of Szeged
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * ***** END LICENSE BLOCK ***** */
31 #include "assembler/wtf/Platform.h"
33 #if ENABLE_ASSEMBLER && WTF_CPU_ARM_TRADITIONAL
35 #include "ARMAssembler.h"
41 void ARMAssembler::patchConstantPoolLoad(void* loadAddr, void* constPoolAddr)
43 ARMWord *ldr = reinterpret_cast<ARMWord*>(loadAddr);
44 ARMWord diff = reinterpret_cast<ARMWord*>(constPoolAddr) - ldr;
45 ARMWord index = (*ldr & 0xfff) >> 1;
48 if (diff >= 2 || index > 0) {
49 diff = (diff + index - 2) * sizeof(ARMWord);
50 ASSERT(diff <= 0xfff);
51 *ldr = (*ldr & ~0xfff) | diff;
53 *ldr = (*ldr & ~(0xfff | ARMAssembler::DT_UP)) | sizeof(ARMWord);
58 ARMWord ARMAssembler::getOp2(ARMWord imm)
65 if ((imm & 0xff000000) == 0) {
70 imm = (imm << 24) | (imm >> 8);
74 if ((imm & 0xff000000) == 0) {
79 if ((imm & 0xf0000000) == 0) {
84 if ((imm & 0xc0000000) == 0) {
89 if ((imm & 0x00ffffff) == 0)
90 return OP2_IMM | (imm >> 24) | (rol << 8);
95 int ARMAssembler::genInt(int reg, ARMWord imm, bool positive)
97 // Step1: Search a non-immediate part
106 if ((imm & mask) == 0) {
107 imm = (imm << rol) | (imm >> (32 - rol));
108 rol = 4 + (rol >> 1);
115 imm = (imm << 8) | (imm >> 24);
119 if ((imm & mask) == 0) {
120 imm = (imm << rol) | (imm >> (32 - rol));
121 rol = (rol >> 1) - 8;
133 ASSERT((imm & 0xff) == 0);
135 if ((imm & 0xff000000) == 0) {
136 imm1 = OP2_IMM | ((imm >> 16) & 0xff) | (((rol + 4) & 0xf) << 8);
137 imm2 = OP2_IMM | ((imm >> 8) & 0xff) | (((rol + 8) & 0xf) << 8);
138 } else if (imm & 0xc0000000) {
139 imm1 = OP2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
143 if ((imm & 0xff000000) == 0) {
148 if ((imm & 0xf0000000) == 0) {
153 if ((imm & 0xc0000000) == 0) {
158 if ((imm & 0x00ffffff) == 0)
159 imm2 = OP2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
163 if ((imm & 0xf0000000) == 0) {
168 if ((imm & 0xc0000000) == 0) {
173 imm1 = OP2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
177 if ((imm & 0xf0000000) == 0) {
182 if ((imm & 0xc0000000) == 0) {
187 if ((imm & 0x00ffffff) == 0)
188 imm2 = OP2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
195 orr_r(reg, reg, imm2);
198 bic_r(reg, reg, imm2);
205 // If the result of this function isn't used, the caller should probably be
207 __attribute__((warn_unused_result))
209 ARMWord ARMAssembler::getImm(ARMWord imm, int tmpReg, bool invert)
213 // Do it by 1 instruction
215 if (tmp != INVALID_IMM)
219 if (tmp != INVALID_IMM) {
221 return tmp | OP2_INV_IMM;
226 return encodeComplexImm(imm, tmpReg);
229 void ARMAssembler::moveImm(ARMWord imm, int dest)
233 // Do it by 1 instruction
235 if (tmp != INVALID_IMM) {
241 if (tmp != INVALID_IMM) {
246 encodeComplexImm(imm, dest);
249 ARMWord ARMAssembler::encodeComplexImm(ARMWord imm, int dest)
251 #if WTF_ARM_ARCH_VERSION >= 7
252 ARMWord tmp = getImm16Op2(imm);
253 if (tmp != INVALID_IMM) {
257 movw_r(dest, getImm16Op2(imm & 0xffff));
258 movt_r(dest, getImm16Op2(imm >> 16));
261 // Do it by 2 instruction
262 if (genInt(dest, imm, true))
264 if (genInt(dest, ~imm, false))
272 // Memory load/store helpers
274 void ARMAssembler::dataTransfer32(bool isLoad, RegisterID srcDst, RegisterID base, int32_t offset)
278 dtr_u(isLoad, srcDst, base, offset);
279 else if (offset <= 0xfffff) {
280 add_r(ARMRegisters::S0, base, OP2_IMM | (offset >> 12) | (10 << 8));
281 dtr_u(isLoad, srcDst, ARMRegisters::S0, (offset & 0xfff));
283 moveImm(offset, ARMRegisters::S0);
284 dtr_ur(isLoad, srcDst, base, ARMRegisters::S0);
289 dtr_d(isLoad, srcDst, base, offset);
290 else if (offset <= 0xfffff) {
291 sub_r(ARMRegisters::S0, base, OP2_IMM | (offset >> 12) | (10 << 8));
292 dtr_d(isLoad, srcDst, ARMRegisters::S0, (offset & 0xfff));
294 moveImm(offset, ARMRegisters::S0);
295 dtr_dr(isLoad, srcDst, base, ARMRegisters::S0);
300 void ARMAssembler::dataTransfer8(bool isLoad, RegisterID srcDst, RegisterID base, int32_t offset)
304 dtrb_u(isLoad, srcDst, base, offset);
305 else if (offset <= 0xfffff) {
306 add_r(ARMRegisters::S0, base, OP2_IMM | (offset >> 12) | (10 << 8));
307 dtrb_u(isLoad, srcDst, ARMRegisters::S0, (offset & 0xfff));
309 moveImm(offset, ARMRegisters::S0);
310 dtrb_ur(isLoad, srcDst, base, ARMRegisters::S0);
315 dtrb_d(isLoad, srcDst, base, offset);
316 else if (offset <= 0xfffff) {
317 sub_r(ARMRegisters::S0, base, OP2_IMM | (offset >> 12) | (10 << 8));
318 dtrb_d(isLoad, srcDst, ARMRegisters::S0, (offset & 0xfff));
320 moveImm(offset, ARMRegisters::S0);
321 dtrb_dr(isLoad, srcDst, base, ARMRegisters::S0);
326 void ARMAssembler::baseIndexTransfer32(bool isLoad, RegisterID srcDst, RegisterID base, RegisterID index, int scale, int32_t offset)
330 ASSERT(scale >= 0 && scale <= 3);
331 op2 = lsl(index, scale);
333 if (offset >= 0 && offset <= 0xfff) {
334 add_r(ARMRegisters::S0, base, op2);
335 dtr_u(isLoad, srcDst, ARMRegisters::S0, offset);
338 if (offset <= 0 && offset >= -0xfff) {
339 add_r(ARMRegisters::S0, base, op2);
340 dtr_d(isLoad, srcDst, ARMRegisters::S0, -offset);
344 ldr_un_imm(ARMRegisters::S0, offset);
345 add_r(ARMRegisters::S0, ARMRegisters::S0, op2);
346 dtr_ur(isLoad, srcDst, base, ARMRegisters::S0);
349 void ARMAssembler::doubleTransfer(bool isLoad, FPRegisterID srcDst, RegisterID base, int32_t offset)
352 if (offset <= 0x3ff && offset >= 0) {
353 fdtr_u(isLoad, srcDst, base, offset >> 2);
356 if (offset <= 0x3ffff && offset >= 0) {
357 add_r(ARMRegisters::S0, base, OP2_IMM | (offset >> 10) | (11 << 8));
358 fdtr_u(isLoad, srcDst, ARMRegisters::S0, (offset >> 2) & 0xff);
363 if (offset <= 0x3ff && offset >= 0) {
364 fdtr_d(isLoad, srcDst, base, offset >> 2);
367 if (offset <= 0x3ffff && offset >= 0) {
368 sub_r(ARMRegisters::S0, base, OP2_IMM | (offset >> 10) | (11 << 8));
369 fdtr_d(isLoad, srcDst, ARMRegisters::S0, (offset >> 2) & 0xff);
375 // TODO: This is broken in the case that offset is unaligned. VFP can never
376 // perform unaligned accesses, even from an unaligned register base. (NEON
377 // can, but VFP isn't NEON. It is not advisable to interleave a NEON load
378 // with VFP code, so the best solution here is probably to perform an
379 // unaligned integer load, then move the result into VFP using VMOV.)
380 ASSERT((offset & 0x3) == 0);
382 ldr_un_imm(ARMRegisters::S0, offset);
383 add_r(ARMRegisters::S0, ARMRegisters::S0, base);
384 fdtr_u(isLoad, srcDst, ARMRegisters::S0, 0);
387 // Fix up the offsets and literal-pool loads in buffer. The buffer should
388 // already contain the code from m_buffer.
389 inline void ARMAssembler::fixUpOffsets(void * buffer)
391 char * data = reinterpret_cast<char *>(buffer);
392 for (Jumps::Iterator iter = m_jumps.begin(); iter != m_jumps.end(); ++iter) {
393 // The last bit is set if the constant must be placed on constant pool.
394 int pos = (*iter) & (~0x1);
395 ARMWord* ldrAddr = reinterpret_cast<ARMWord*>(data + pos);
396 ARMWord* addr = getLdrImmAddress(ldrAddr);
397 if (*addr != InvalidBranchTarget) {
398 // The following is disabled for JM because we patch some branches after
399 // calling fixUpOffset, and the branch patcher doesn't know how to handle 'B'
403 int diff = reinterpret_cast<ARMWord*>(data + *addr) - (ldrAddr + DefaultPrefetching);
405 if ((diff <= BOFFSET_MAX && diff >= BOFFSET_MIN)) {
406 *ldrAddr = B | getConditionalField(*ldrAddr) | (diff & BRANCH_MASK);
411 *addr = reinterpret_cast<ARMWord>(data + *addr);
416 void* ARMAssembler::executableCopy(ExecutablePool* allocator)
418 // 64-bit alignment is required for next constant pool and JIT code as well
419 m_buffer.flushWithoutBarrier(true);
420 if (m_buffer.uncheckedSize() & 0x7)
423 void * data = m_buffer.executableCopy(allocator);
429 // This just dumps the code into the specified buffer, fixing up absolute
430 // offsets and literal pool loads as it goes. The buffer is assumed to be large
431 // enough to hold the code, and any pre-existing literal pool is assumed to
432 // have been flushed.
433 void* ARMAssembler::executableCopy(void * buffer)
438 ASSERT(m_buffer.sizeOfConstantPool() == 0);
440 memcpy(buffer, m_buffer.data(), m_buffer.size());
441 fixUpOffsets(buffer);
447 #endif // ENABLE(ASSEMBLER) && CPU(ARM_TRADITIONAL)