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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
/*
* Copyright (C) 2008, 2014 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef MacroAssemblerX86_h
#define MacroAssemblerX86_h
#if ENABLE(ASSEMBLER) && CPU(X86)
#include "MacroAssemblerX86Common.h"
#if USE(MASM_PROBE)
#include <wtf/StdLibExtras.h>
#endif
namespace JSC {
class MacroAssemblerX86 : public MacroAssemblerX86Common {
public:
static const Scale ScalePtr = TimesFour;
using MacroAssemblerX86Common::add32;
using MacroAssemblerX86Common::and32;
using MacroAssemblerX86Common::branchAdd32;
using MacroAssemblerX86Common::branchSub32;
using MacroAssemblerX86Common::sub32;
using MacroAssemblerX86Common::or32;
using MacroAssemblerX86Common::load32;
using MacroAssemblerX86Common::load8;
using MacroAssemblerX86Common::store32;
using MacroAssemblerX86Common::store8;
using MacroAssemblerX86Common::branch32;
using MacroAssemblerX86Common::call;
using MacroAssemblerX86Common::jump;
using MacroAssemblerX86Common::addDouble;
using MacroAssemblerX86Common::loadDouble;
using MacroAssemblerX86Common::storeDouble;
using MacroAssemblerX86Common::convertInt32ToDouble;
using MacroAssemblerX86Common::branch8;
using MacroAssemblerX86Common::branchTest8;
void add32(TrustedImm32 imm, RegisterID src, RegisterID dest)
{
m_assembler.leal_mr(imm.m_value, src, dest);
}
void add32(TrustedImm32 imm, AbsoluteAddress address)
{
m_assembler.addl_im(imm.m_value, address.m_ptr);
}
void add32(AbsoluteAddress address, RegisterID dest)
{
m_assembler.addl_mr(address.m_ptr, dest);
}
void add64(TrustedImm32 imm, AbsoluteAddress address)
{
m_assembler.addl_im(imm.m_value, address.m_ptr);
m_assembler.adcl_im(imm.m_value >> 31, reinterpret_cast<const char*>(address.m_ptr) + sizeof(int32_t));
}
void and32(TrustedImm32 imm, AbsoluteAddress address)
{
m_assembler.andl_im(imm.m_value, address.m_ptr);
}
void or32(TrustedImm32 imm, AbsoluteAddress address)
{
m_assembler.orl_im(imm.m_value, address.m_ptr);
}
void or32(RegisterID reg, AbsoluteAddress address)
{
m_assembler.orl_rm(reg, address.m_ptr);
}
void sub32(TrustedImm32 imm, AbsoluteAddress address)
{
m_assembler.subl_im(imm.m_value, address.m_ptr);
}
void load32(const void* address, RegisterID dest)
{
m_assembler.movl_mr(address, dest);
}
void load8(const void* address, RegisterID dest)
{
m_assembler.movzbl_mr(address, dest);
}
void abortWithReason(AbortReason reason)
{
move(TrustedImm32(reason), X86Registers::eax);
breakpoint();
}
void abortWithReason(AbortReason reason, intptr_t misc)
{
move(TrustedImm32(misc), X86Registers::edx);
abortWithReason(reason);
}
ConvertibleLoadLabel convertibleLoadPtr(Address address, RegisterID dest)
{
ConvertibleLoadLabel result = ConvertibleLoadLabel(this);
m_assembler.movl_mr(address.offset, address.base, dest);
return result;
}
void addDouble(AbsoluteAddress address, FPRegisterID dest)
{
m_assembler.addsd_mr(address.m_ptr, dest);
}
void storeDouble(FPRegisterID src, TrustedImmPtr address)
{
ASSERT(isSSE2Present());
ASSERT(address.m_value);
m_assembler.movsd_rm(src, address.m_value);
}
void convertInt32ToDouble(AbsoluteAddress src, FPRegisterID dest)
{
m_assembler.cvtsi2sd_mr(src.m_ptr, dest);
}
void store32(TrustedImm32 imm, void* address)
{
m_assembler.movl_i32m(imm.m_value, address);
}
void store32(RegisterID src, void* address)
{
m_assembler.movl_rm(src, address);
}
void store8(RegisterID src, void* address)
{
m_assembler.movb_rm(src, address);
}
void store8(TrustedImm32 imm, void* address)
{
ASSERT(-128 <= imm.m_value && imm.m_value < 128);
m_assembler.movb_i8m(imm.m_value, address);
}
void moveDoubleToInts(FPRegisterID src, RegisterID dest1, RegisterID dest2)
{
ASSERT(isSSE2Present());
m_assembler.pextrw_irr(3, src, dest1);
m_assembler.pextrw_irr(2, src, dest2);
lshift32(TrustedImm32(16), dest1);
or32(dest1, dest2);
movePackedToInt32(src, dest1);
}
void moveIntsToDouble(RegisterID src1, RegisterID src2, FPRegisterID dest, FPRegisterID scratch)
{
moveInt32ToPacked(src1, dest);
moveInt32ToPacked(src2, scratch);
lshiftPacked(TrustedImm32(32), scratch);
orPacked(scratch, dest);
}
Jump branchAdd32(ResultCondition cond, TrustedImm32 imm, AbsoluteAddress dest)
{
m_assembler.addl_im(imm.m_value, dest.m_ptr);
return Jump(m_assembler.jCC(x86Condition(cond)));
}
Jump branchSub32(ResultCondition cond, TrustedImm32 imm, AbsoluteAddress dest)
{
m_assembler.subl_im(imm.m_value, dest.m_ptr);
return Jump(m_assembler.jCC(x86Condition(cond)));
}
Jump branch32(RelationalCondition cond, AbsoluteAddress left, RegisterID right)
{
m_assembler.cmpl_rm(right, left.m_ptr);
return Jump(m_assembler.jCC(x86Condition(cond)));
}
Jump branch32(RelationalCondition cond, AbsoluteAddress left, TrustedImm32 right)
{
m_assembler.cmpl_im(right.m_value, left.m_ptr);
return Jump(m_assembler.jCC(x86Condition(cond)));
}
Call call()
{
return Call(m_assembler.call(), Call::Linkable);
}
// Address is a memory location containing the address to jump to
void jump(AbsoluteAddress address)
{
m_assembler.jmp_m(address.m_ptr);
}
Call tailRecursiveCall()
{
return Call::fromTailJump(jump());
}
Call makeTailRecursiveCall(Jump oldJump)
{
return Call::fromTailJump(oldJump);
}
DataLabelPtr moveWithPatch(TrustedImmPtr initialValue, RegisterID dest)
{
padBeforePatch();
m_assembler.movl_i32r(initialValue.asIntptr(), dest);
return DataLabelPtr(this);
}
Jump branch8(RelationalCondition cond, AbsoluteAddress left, TrustedImm32 right)
{
m_assembler.cmpb_im(right.m_value, left.m_ptr);
return Jump(m_assembler.jCC(x86Condition(cond)));
}
Jump branchTest8(ResultCondition cond, AbsoluteAddress address, TrustedImm32 mask = TrustedImm32(-1))
{
ASSERT(mask.m_value >= -128 && mask.m_value <= 255);
if (mask.m_value == -1)
m_assembler.cmpb_im(0, address.m_ptr);
else
m_assembler.testb_im(mask.m_value, address.m_ptr);
return Jump(m_assembler.jCC(x86Condition(cond)));
}
Jump branchPtrWithPatch(RelationalCondition cond, RegisterID left, DataLabelPtr& dataLabel, TrustedImmPtr initialRightValue = TrustedImmPtr(0))
{
padBeforePatch();
m_assembler.cmpl_ir_force32(initialRightValue.asIntptr(), left);
dataLabel = DataLabelPtr(this);
return Jump(m_assembler.jCC(x86Condition(cond)));
}
Jump branchPtrWithPatch(RelationalCondition cond, Address left, DataLabelPtr& dataLabel, TrustedImmPtr initialRightValue = TrustedImmPtr(0))
{
padBeforePatch();
m_assembler.cmpl_im_force32(initialRightValue.asIntptr(), left.offset, left.base);
dataLabel = DataLabelPtr(this);
return Jump(m_assembler.jCC(x86Condition(cond)));
}
Jump branch32WithPatch(RelationalCondition cond, Address left, DataLabel32& dataLabel, TrustedImm32 initialRightValue = TrustedImm32(0))
{
padBeforePatch();
m_assembler.cmpl_im_force32(initialRightValue.m_value, left.offset, left.base);
dataLabel = DataLabel32(this);
return Jump(m_assembler.jCC(x86Condition(cond)));
}
DataLabelPtr storePtrWithPatch(TrustedImmPtr initialValue, ImplicitAddress address)
{
padBeforePatch();
m_assembler.movl_i32m(initialValue.asIntptr(), address.offset, address.base);
return DataLabelPtr(this);
}
static bool supportsFloatingPoint() { return isSSE2Present(); }
// See comment on MacroAssemblerARMv7::supportsFloatingPointTruncate()
static bool supportsFloatingPointTruncate() { return isSSE2Present(); }
static bool supportsFloatingPointSqrt() { return isSSE2Present(); }
static bool supportsFloatingPointAbs() { return isSSE2Present(); }
static FunctionPtr readCallTarget(CodeLocationCall call)
{
intptr_t offset = reinterpret_cast<int32_t*>(call.dataLocation())[-1];
return FunctionPtr(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(call.dataLocation()) + offset));
}
static bool canJumpReplacePatchableBranchPtrWithPatch() { return true; }
static bool canJumpReplacePatchableBranch32WithPatch() { return true; }
static CodeLocationLabel startOfBranchPtrWithPatchOnRegister(CodeLocationDataLabelPtr label)
{
const int opcodeBytes = 1;
const int modRMBytes = 1;
const int immediateBytes = 4;
const int totalBytes = opcodeBytes + modRMBytes + immediateBytes;
ASSERT(totalBytes >= maxJumpReplacementSize());
return label.labelAtOffset(-totalBytes);
}
static CodeLocationLabel startOfPatchableBranchPtrWithPatchOnAddress(CodeLocationDataLabelPtr label)
{
const int opcodeBytes = 1;
const int modRMBytes = 1;
const int offsetBytes = 0;
const int immediateBytes = 4;
const int totalBytes = opcodeBytes + modRMBytes + offsetBytes + immediateBytes;
ASSERT(totalBytes >= maxJumpReplacementSize());
return label.labelAtOffset(-totalBytes);
}
static CodeLocationLabel startOfPatchableBranch32WithPatchOnAddress(CodeLocationDataLabel32 label)
{
const int opcodeBytes = 1;
const int modRMBytes = 1;
const int offsetBytes = 0;
const int immediateBytes = 4;
const int totalBytes = opcodeBytes + modRMBytes + offsetBytes + immediateBytes;
ASSERT(totalBytes >= maxJumpReplacementSize());
return label.labelAtOffset(-totalBytes);
}
static void revertJumpReplacementToBranchPtrWithPatch(CodeLocationLabel instructionStart, RegisterID reg, void* initialValue)
{
X86Assembler::revertJumpTo_cmpl_ir_force32(instructionStart.executableAddress(), reinterpret_cast<intptr_t>(initialValue), reg);
}
static void revertJumpReplacementToPatchableBranchPtrWithPatch(CodeLocationLabel instructionStart, Address address, void* initialValue)
{
ASSERT(!address.offset);
X86Assembler::revertJumpTo_cmpl_im_force32(instructionStart.executableAddress(), reinterpret_cast<intptr_t>(initialValue), 0, address.base);
}
static void revertJumpReplacementToPatchableBranch32WithPatch(CodeLocationLabel instructionStart, Address address, int32_t initialValue)
{
ASSERT(!address.offset);
X86Assembler::revertJumpTo_cmpl_im_force32(instructionStart.executableAddress(), initialValue, 0, address.base);
}
#if USE(MASM_PROBE)
// For details about probe(), see comment in MacroAssemblerX86_64.h.
void probe(ProbeFunction, void* arg1 = 0, void* arg2 = 0);
#endif // USE(MASM_PROBE)
private:
friend class LinkBuffer;
friend class RepatchBuffer;
static void linkCall(void* code, Call call, FunctionPtr function)
{
X86Assembler::linkCall(code, call.m_label, function.value());
}
static void repatchCall(CodeLocationCall call, CodeLocationLabel destination)
{
X86Assembler::relinkCall(call.dataLocation(), destination.executableAddress());
}
static void repatchCall(CodeLocationCall call, FunctionPtr destination)
{
X86Assembler::relinkCall(call.dataLocation(), destination.executableAddress());
}
#if USE(MASM_PROBE)
inline TrustedImm32 trustedImm32FromPtr(void* ptr)
{
return TrustedImm32(TrustedImmPtr(ptr));
}
inline TrustedImm32 trustedImm32FromPtr(ProbeFunction function)
{
return TrustedImm32(TrustedImmPtr(reinterpret_cast<void*>(function)));
}
inline TrustedImm32 trustedImm32FromPtr(void (*function)())
{
return TrustedImm32(TrustedImmPtr(reinterpret_cast<void*>(function)));
}
#endif
};
#if USE(MASM_PROBE)
extern "C" void ctiMasmProbeTrampoline();
// For details on "What code is emitted for the probe?" and "What values are in
// the saved registers?", see comment for MacroAssemblerX86::probe() in
// MacroAssemblerX86_64.h.
inline void MacroAssemblerX86::probe(MacroAssemblerX86::ProbeFunction function, void* arg1, void* arg2)
{
push(RegisterID::esp);
push(RegisterID::eax);
push(trustedImm32FromPtr(arg2));
push(trustedImm32FromPtr(arg1));
push(trustedImm32FromPtr(function));
move(trustedImm32FromPtr(ctiMasmProbeTrampoline), RegisterID::eax);
call(RegisterID::eax);
}
#endif // USE(MASM_PROBE)
} // namespace JSC
#endif // ENABLE(ASSEMBLER)
#endif // MacroAssemblerX86_h
|