codegen-x64.cc 8.71 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#include "src/x64/codegen-x64.h"

7
#if V8_TARGET_ARCH_X64
8

9 10
#include "src/codegen.h"
#include "src/macro-assembler.h"
11

12 13
namespace v8 {
namespace internal {
14

15 16 17
// -------------------------------------------------------------------------
// Platform-specific RuntimeCallHelper functions.

18
void StubRuntimeCallHelper::BeforeCall(MacroAssembler* masm) const {
19
  masm->EnterFrame(StackFrame::INTERNAL);
20
  DCHECK(!masm->has_frame());
21
  masm->set_has_frame(true);
22 23 24
}


25
void StubRuntimeCallHelper::AfterCall(MacroAssembler* masm) const {
26
  masm->LeaveFrame(StackFrame::INTERNAL);
27
  DCHECK(masm->has_frame());
28
  masm->set_has_frame(false);
29 30 31
}


32 33
#define __ masm.

34

35
UnaryMathFunctionWithIsolate CreateSqrtFunction(Isolate* isolate) {
36 37
  size_t actual_size;
  // Allocate buffer in executable space.
38 39
  byte* buffer =
      static_cast<byte*>(base::OS::Allocate(1 * KB, &actual_size, true));
40
  if (buffer == nullptr) return nullptr;
41

42
  MacroAssembler masm(isolate, buffer, static_cast<int>(actual_size),
43
                      CodeObjectRequired::kNo);
44 45
  // xmm0: raw double input.
  // Move double input into registers.
46
  __ Sqrtsd(xmm0, xmm0);
47 48 49 50
  __ Ret();

  CodeDesc desc;
  masm.GetCode(&desc);
51
  DCHECK(!RelocInfo::RequiresRelocation(desc));
52

53
  Assembler::FlushICache(isolate, buffer, actual_size);
54
  base::OS::ProtectCode(buffer, actual_size);
55
  return FUNCTION_CAST<UnaryMathFunctionWithIsolate>(buffer);
56 57
}

58 59 60 61 62 63 64
#undef __

// -------------------------------------------------------------------------
// Code generators

#define __ ACCESS_MASM(masm)

65 66 67 68 69 70
void StringCharLoadGenerator::Generate(MacroAssembler* masm,
                                       Register string,
                                       Register index,
                                       Register result,
                                       Label* call_runtime) {
  // Fetch the instance type of the receiver into result register.
71
  __ movp(result, FieldOperand(string, HeapObject::kMapOffset));
72 73 74 75 76
  __ movzxbl(result, FieldOperand(result, Map::kInstanceTypeOffset));

  // We need special handling for indirect strings.
  Label check_sequential;
  __ testb(result, Immediate(kIsIndirectStringMask));
77
  __ j(zero, &check_sequential, Label::kNear);
78 79 80 81 82 83 84 85 86

  // Dispatch on the indirect string shape: slice or cons.
  Label cons_string;
  __ testb(result, Immediate(kSlicedNotConsMask));
  __ j(zero, &cons_string, Label::kNear);

  // Handle slices.
  Label indirect_string_loaded;
  __ SmiToInteger32(result, FieldOperand(string, SlicedString::kOffsetOffset));
87
  __ addp(index, result);
88
  __ movp(string, FieldOperand(string, SlicedString::kParentOffset));
89 90 91 92 93 94 95 96 97
  __ jmp(&indirect_string_loaded, Label::kNear);

  // Handle cons strings.
  // Check whether the right hand side is the empty string (i.e. if
  // this is really a flat string in a cons string). If that is not
  // the case we would rather go to the runtime system now to flatten
  // the string.
  __ bind(&cons_string);
  __ CompareRoot(FieldOperand(string, ConsString::kSecondOffset),
98
                 Heap::kempty_stringRootIndex);
99
  __ j(not_equal, call_runtime);
100
  __ movp(string, FieldOperand(string, ConsString::kFirstOffset));
101 102

  __ bind(&indirect_string_loaded);
103
  __ movp(result, FieldOperand(string, HeapObject::kMapOffset));
104 105 106 107 108
  __ movzxbl(result, FieldOperand(result, Map::kInstanceTypeOffset));

  // Distinguish sequential and external strings. Only these two string
  // representations can reach here (slices and flat cons strings have been
  // reduced to the underlying sequential or external string).
109
  Label seq_string;
110 111 112
  __ bind(&check_sequential);
  STATIC_ASSERT(kSeqStringTag == 0);
  __ testb(result, Immediate(kStringRepresentationMask));
113 114 115
  __ j(zero, &seq_string, Label::kNear);

  // Handle external strings.
116
  Label one_byte_external, done;
117 118 119 120
  if (FLAG_debug_code) {
    // Assert that we do not have a cons or slice (indirect strings) here.
    // Sequential strings have already been ruled out.
    __ testb(result, Immediate(kIsIndirectStringMask));
121
    __ Assert(zero, kExternalStringExpectedButNotFound);
122 123
  }
  // Rule out short external strings.
124
  STATIC_ASSERT(kShortExternalStringTag != 0);
125 126 127 128 129
  __ testb(result, Immediate(kShortExternalStringTag));
  __ j(not_zero, call_runtime);
  // Check encoding.
  STATIC_ASSERT(kTwoByteStringTag == 0);
  __ testb(result, Immediate(kStringEncodingMask));
130
  __ movp(result, FieldOperand(string, ExternalString::kResourceDataOffset));
131
  __ j(not_equal, &one_byte_external, Label::kNear);
132 133 134
  // Two-byte string.
  __ movzxwl(result, Operand(result, index, times_2, 0));
  __ jmp(&done, Label::kNear);
135 136
  __ bind(&one_byte_external);
  // One-byte string.
137 138
  __ movzxbl(result, Operand(result, index, times_1, 0));
  __ jmp(&done, Label::kNear);
139

140 141
  // Dispatch on the encoding: one-byte or two-byte.
  Label one_byte;
142
  __ bind(&seq_string);
143
  STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
144 145
  STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
  __ testb(result, Immediate(kStringEncodingMask));
146
  __ j(not_zero, &one_byte, Label::kNear);
147 148 149 150 151 152 153 154 155 156

  // Two-byte string.
  // Load the two-byte character code into the result register.
  STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
  __ movzxwl(result, FieldOperand(string,
                                  index,
                                  times_2,
                                  SeqTwoByteString::kHeaderSize));
  __ jmp(&done, Label::kNear);

157
  // One-byte string.
158
  // Load the byte into the result register.
159
  __ bind(&one_byte);
160 161 162
  __ movzxbl(result, FieldOperand(string,
                                  index,
                                  times_1,
163
                                  SeqOneByteString::kHeaderSize));
164 165 166
  __ bind(&done);
}

lrn@chromium.org's avatar
lrn@chromium.org committed
167
#undef __
168

169

170 171
CodeAgingHelper::CodeAgingHelper(Isolate* isolate) {
  USE(isolate);
172
  DCHECK(young_sequence_.length() == kNoCodeAgeSequenceLength);
173 174 175
  // The sequence of instructions that is patched out for aging code is the
  // following boilerplate stack-building prologue that is found both in
  // FUNCTION and OPTIMIZED_FUNCTION code:
176 177
  CodePatcher patcher(isolate, young_sequence_.start(),
                      young_sequence_.length());
178 179 180 181
  patcher.masm()->pushq(rbp);
  patcher.masm()->movp(rbp, rsp);
  patcher.masm()->Push(rsi);
  patcher.masm()->Push(rdi);
182 183 184
}


185 186 187 188 189 190 191 192 193
#ifdef DEBUG
bool CodeAgingHelper::IsOld(byte* candidate) const {
  return *candidate == kCallOpcode;
}
#endif


bool Code::IsYoungSequence(Isolate* isolate, byte* sequence) {
  bool result = isolate->code_aging_helper()->IsYoung(sequence);
194
  DCHECK(result || isolate->code_aging_helper()->IsOld(sequence));
195 196 197
  return result;
}

198 199 200 201 202 203 204 205
Code::Age Code::GetCodeAge(Isolate* isolate, byte* sequence) {
  if (IsYoungSequence(isolate, sequence)) return kNoAgeCodeAge;

  sequence++;  // Skip the kCallOpcode byte
  Address target_address = sequence + *reinterpret_cast<int*>(sequence) +
                           Assembler::kCallTargetAddressOffset;
  Code* stub = GetCodeFromTargetAddress(target_address);
  return GetAgeOfCodeAgeStub(stub);
206 207
}

208 209
void Code::PatchPlatformCodeAge(Isolate* isolate, byte* sequence,
                                Code::Age age) {
210
  uint32_t young_length = isolate->code_aging_helper()->young_sequence_length();
211
  if (age == kNoAgeCodeAge) {
212
    isolate->code_aging_helper()->CopyYoungSequenceTo(sequence);
213
    Assembler::FlushICache(isolate, sequence, young_length);
214
  } else {
215
    Code* stub = GetCodeAgeStub(isolate, age);
216
    CodePatcher patcher(isolate, sequence, young_length);
217
    patcher.masm()->call(stub->instruction_start());
218 219
    patcher.masm()->Nop(
        kNoCodeAgeSequenceLength - Assembler::kShortCallInstructionLength);
220 221 222 223
  }
}


224
Operand StackArgumentsAccessor::GetArgumentOperand(int index) {
225
  DCHECK(index >= 0);
226 227 228 229 230 231 232
  int receiver = (receiver_mode_ == ARGUMENTS_CONTAIN_RECEIVER) ? 1 : 0;
  int displacement_to_last_argument = base_reg_.is(rsp) ?
      kPCOnStackSize : kFPOnStackSize + kPCOnStackSize;
  displacement_to_last_argument += extra_displacement_to_last_argument_;
  if (argument_count_reg_.is(no_reg)) {
    // argument[0] is at base_reg_ + displacement_to_last_argument +
    // (argument_count_immediate_ + receiver - 1) * kPointerSize.
233
    DCHECK(argument_count_immediate_ + receiver > 0);
234 235 236 237 238 239 240 241 242 243 244
    return Operand(base_reg_, displacement_to_last_argument +
        (argument_count_immediate_ + receiver - 1 - index) * kPointerSize);
  } else {
    // argument[0] is at base_reg_ + displacement_to_last_argument +
    // argument_count_reg_ * times_pointer_size + (receiver - 1) * kPointerSize.
    return Operand(base_reg_, argument_count_reg_, times_pointer_size,
        displacement_to_last_argument + (receiver - 1 - index) * kPointerSize);
  }
}


245 246
}  // namespace internal
}  // namespace v8
247 248

#endif  // V8_TARGET_ARCH_X64