| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "src/compiler/fast-accessor-assembler.h" | |
| 6 | |
| 7 #include "src/base/logging.h" | |
| 8 #include "src/code-stubs.h" // For CallApiFunctionStub. | |
| 9 #include "src/compiler/graph.h" | |
| 10 #include "src/compiler/linkage.h" | |
| 11 #include "src/compiler/pipeline.h" | |
| 12 #include "src/compiler/raw-machine-assembler.h" | |
| 13 #include "src/compiler/schedule.h" | |
| 14 #include "src/compiler/verifier.h" | |
| 15 #include "src/handles-inl.h" | |
| 16 #include "src/objects.h" // For FAA::GetInternalField impl. | |
| 17 | |
| 18 namespace v8 { | |
| 19 namespace internal { | |
| 20 namespace compiler { | |
| 21 | |
| 22 FastAccessorAssembler::FastAccessorAssembler(Isolate* isolate) | |
| 23 : zone_(), | |
| 24 assembler_(new RawMachineAssembler( | |
| 25 isolate, new (zone()) Graph(zone()), | |
| 26 Linkage::GetJSCallDescriptor(&zone_, false, 1, | |
| 27 CallDescriptor::kNoFlags))), | |
| 28 state_(kBuilding) {} | |
| 29 | |
| 30 | |
| 31 FastAccessorAssembler::~FastAccessorAssembler() {} | |
| 32 | |
| 33 | |
| 34 FastAccessorAssembler::ValueId FastAccessorAssembler::IntegerConstant( | |
| 35 int const_value) { | |
| 36 CHECK_EQ(kBuilding, state_); | |
| 37 return FromRaw(assembler_->NumberConstant(const_value)); | |
| 38 } | |
| 39 | |
| 40 | |
| 41 FastAccessorAssembler::ValueId FastAccessorAssembler::GetReceiver() { | |
| 42 CHECK_EQ(kBuilding, state_); | |
| 43 | |
| 44 // For JS call descriptor, the receiver is parameter 0. If we use other | |
| 45 // call descriptors, this may or may not hold. So let's check. | |
| 46 CHECK(assembler_->call_descriptor()->IsJSFunctionCall()); | |
| 47 return FromRaw(assembler_->Parameter(0)); | |
| 48 } | |
| 49 | |
| 50 | |
| 51 FastAccessorAssembler::ValueId FastAccessorAssembler::LoadInternalField( | |
| 52 ValueId value, int field_no) { | |
| 53 CHECK_EQ(kBuilding, state_); | |
| 54 // Determine the 'value' object's instance type. | |
| 55 Node* object_map = | |
| 56 assembler_->Load(MachineType::Pointer(), FromId(value), | |
| 57 assembler_->IntPtrConstant( | |
| 58 Internals::kHeapObjectMapOffset - kHeapObjectTag)); | |
| 59 Node* instance_type = assembler_->WordAnd( | |
| 60 assembler_->Load( | |
| 61 MachineType::Uint16(), object_map, | |
| 62 assembler_->IntPtrConstant( | |
| 63 Internals::kMapInstanceTypeAndBitFieldOffset - kHeapObjectTag)), | |
| 64 assembler_->IntPtrConstant(0xff)); | |
| 65 | |
| 66 // Check whether we have a proper JSObject. | |
| 67 RawMachineLabel is_jsobject, is_not_jsobject, merge; | |
| 68 assembler_->Branch( | |
| 69 assembler_->WordEqual( | |
| 70 instance_type, assembler_->IntPtrConstant(Internals::kJSObjectType)), | |
| 71 &is_jsobject, &is_not_jsobject); | |
| 72 | |
| 73 // JSObject? Then load the internal field field_no. | |
| 74 assembler_->Bind(&is_jsobject); | |
| 75 Node* internal_field = assembler_->Load( | |
| 76 MachineType::Pointer(), FromId(value), | |
| 77 assembler_->IntPtrConstant(JSObject::kHeaderSize - kHeapObjectTag + | |
| 78 kPointerSize * field_no)); | |
| 79 assembler_->Goto(&merge); | |
| 80 | |
| 81 // No JSObject? Return undefined. | |
| 82 // TODO(vogelheim): Check whether this is the appropriate action, or whether | |
| 83 // the method should take a label instead. | |
| 84 assembler_->Bind(&is_not_jsobject); | |
| 85 Node* fail_value = assembler_->UndefinedConstant(); | |
| 86 assembler_->Goto(&merge); | |
| 87 | |
| 88 // Return. | |
| 89 assembler_->Bind(&merge); | |
| 90 Node* phi = assembler_->Phi(MachineRepresentation::kTagged, internal_field, | |
| 91 fail_value); | |
| 92 return FromRaw(phi); | |
| 93 } | |
| 94 | |
| 95 | |
| 96 FastAccessorAssembler::ValueId FastAccessorAssembler::LoadValue(ValueId value, | |
| 97 int offset) { | |
| 98 CHECK_EQ(kBuilding, state_); | |
| 99 return FromRaw(assembler_->Load(MachineType::IntPtr(), FromId(value), | |
| 100 assembler_->IntPtrConstant(offset))); | |
| 101 } | |
| 102 | |
| 103 | |
| 104 FastAccessorAssembler::ValueId FastAccessorAssembler::LoadObject(ValueId value, | |
| 105 int offset) { | |
| 106 CHECK_EQ(kBuilding, state_); | |
| 107 return FromRaw( | |
| 108 assembler_->Load(MachineType::AnyTagged(), | |
| 109 assembler_->Load(MachineType::Pointer(), FromId(value), | |
| 110 assembler_->IntPtrConstant(offset)))); | |
| 111 } | |
| 112 | |
| 113 | |
| 114 void FastAccessorAssembler::ReturnValue(ValueId value) { | |
| 115 CHECK_EQ(kBuilding, state_); | |
| 116 assembler_->Return(FromId(value)); | |
| 117 } | |
| 118 | |
| 119 | |
| 120 void FastAccessorAssembler::CheckFlagSetOrReturnNull(ValueId value, int mask) { | |
| 121 CHECK_EQ(kBuilding, state_); | |
| 122 RawMachineLabel pass, fail; | |
| 123 assembler_->Branch( | |
| 124 assembler_->Word32Equal( | |
| 125 assembler_->Word32And(FromId(value), assembler_->Int32Constant(mask)), | |
| 126 assembler_->Int32Constant(0)), | |
| 127 &pass, &fail); | |
| 128 assembler_->Bind(&fail); | |
| 129 assembler_->Return(assembler_->NullConstant()); | |
| 130 assembler_->Bind(&pass); | |
| 131 } | |
| 132 | |
| 133 | |
| 134 void FastAccessorAssembler::CheckNotZeroOrReturnNull(ValueId value) { | |
| 135 CHECK_EQ(kBuilding, state_); | |
| 136 RawMachineLabel is_null, not_null; | |
| 137 assembler_->Branch( | |
| 138 assembler_->IntPtrEqual(FromId(value), assembler_->IntPtrConstant(0)), | |
| 139 &is_null, ¬_null); | |
| 140 assembler_->Bind(&is_null); | |
| 141 assembler_->Return(assembler_->NullConstant()); | |
| 142 assembler_->Bind(¬_null); | |
| 143 } | |
| 144 | |
| 145 | |
| 146 FastAccessorAssembler::LabelId FastAccessorAssembler::MakeLabel() { | |
| 147 CHECK_EQ(kBuilding, state_); | |
| 148 RawMachineLabel* label = | |
| 149 new (zone()->New(sizeof(RawMachineLabel))) RawMachineLabel; | |
| 150 return FromRaw(label); | |
| 151 } | |
| 152 | |
| 153 | |
| 154 void FastAccessorAssembler::SetLabel(LabelId label_id) { | |
| 155 CHECK_EQ(kBuilding, state_); | |
| 156 assembler_->Bind(FromId(label_id)); | |
| 157 } | |
| 158 | |
| 159 | |
| 160 void FastAccessorAssembler::CheckNotZeroOrJump(ValueId value_id, | |
| 161 LabelId label_id) { | |
| 162 CHECK_EQ(kBuilding, state_); | |
| 163 RawMachineLabel pass; | |
| 164 assembler_->Branch( | |
| 165 assembler_->IntPtrEqual(FromId(value_id), assembler_->IntPtrConstant(0)), | |
| 166 &pass, FromId(label_id)); | |
| 167 assembler_->Bind(&pass); | |
| 168 } | |
| 169 | |
| 170 FastAccessorAssembler::ValueId FastAccessorAssembler::Call( | |
| 171 FunctionCallback callback_function, ValueId arg) { | |
| 172 CHECK_EQ(kBuilding, state_); | |
| 173 | |
| 174 // Create API function stub. | |
| 175 CallApiFunctionStub stub(assembler_->isolate(), true); | |
| 176 | |
| 177 // Wrap the FunctionCallback in an ExternalReference. | |
| 178 ApiFunction callback_api_function(FUNCTION_ADDR(callback_function)); | |
| 179 ExternalReference callback(&callback_api_function, | |
| 180 ExternalReference::DIRECT_API_CALL, | |
| 181 assembler_->isolate()); | |
| 182 | |
| 183 // The stub has 5 parameters, and kJSParam (here: 1) parameters to pass | |
| 184 // through to the callback. | |
| 185 // See: ApiFunctionDescriptor::BuildCallInterfaceDescriptorFunctionType | |
| 186 static const int kStackParam = 1; | |
| 187 Node* args[] = { | |
| 188 // Stub/register parameters: | |
| 189 assembler_->Parameter(0), /* receiver (use accessor's) */ | |
| 190 assembler_->UndefinedConstant(), /* call_data (undefined) */ | |
| 191 assembler_->NullConstant(), /* holder (null) */ | |
| 192 assembler_->ExternalConstant(callback), /* API callback function */ | |
| 193 assembler_->IntPtrConstant(kStackParam), /* # JS arguments */ | |
| 194 | |
| 195 // kStackParam stack parameter(s): | |
| 196 FromId(arg), | |
| 197 | |
| 198 // Context parameter. (See Linkage::GetStubCallDescriptor.) | |
| 199 assembler_->UndefinedConstant()}; | |
| 200 CHECK_EQ(5 + kStackParam + 1, arraysize(args)); | |
| 201 | |
| 202 Node* call = assembler_->CallN( | |
| 203 Linkage::GetStubCallDescriptor( | |
| 204 assembler_->isolate(), zone(), stub.GetCallInterfaceDescriptor(), | |
| 205 kStackParam + stub.GetStackParameterCount(), | |
| 206 CallDescriptor::kNoFlags), | |
| 207 assembler_->HeapConstant(stub.GetCode()), args); | |
| 208 return FromRaw(call); | |
| 209 } | |
| 210 | |
| 211 MaybeHandle<Code> FastAccessorAssembler::Build() { | |
| 212 CHECK_EQ(kBuilding, state_); | |
| 213 | |
| 214 // Cleanup: We no longer need this. | |
| 215 nodes_.clear(); | |
| 216 labels_.clear(); | |
| 217 | |
| 218 // Export the schedule and call the compiler. | |
| 219 Schedule* schedule = assembler_->Export(); | |
| 220 Code::Flags flags = Code::ComputeFlags(Code::STUB); | |
| 221 MaybeHandle<Code> code = Pipeline::GenerateCodeForCodeStub( | |
| 222 assembler_->isolate(), assembler_->call_descriptor(), assembler_->graph(), | |
| 223 schedule, flags, "FastAccessorAssembler"); | |
| 224 | |
| 225 // Update state & return. | |
| 226 state_ = !code.is_null() ? kBuilt : kError; | |
| 227 return code; | |
| 228 } | |
| 229 | |
| 230 | |
| 231 FastAccessorAssembler::ValueId FastAccessorAssembler::FromRaw(Node* node) { | |
| 232 nodes_.push_back(node); | |
| 233 ValueId value = {nodes_.size() - 1}; | |
| 234 return value; | |
| 235 } | |
| 236 | |
| 237 | |
| 238 FastAccessorAssembler::LabelId FastAccessorAssembler::FromRaw( | |
| 239 RawMachineLabel* label) { | |
| 240 labels_.push_back(label); | |
| 241 LabelId label_id = {labels_.size() - 1}; | |
| 242 return label_id; | |
| 243 } | |
| 244 | |
| 245 | |
| 246 Node* FastAccessorAssembler::FromId(ValueId value) const { | |
| 247 CHECK_LT(value.value_id, nodes_.size()); | |
| 248 CHECK_NOT_NULL(nodes_.at(value.value_id)); | |
| 249 return nodes_.at(value.value_id); | |
| 250 } | |
| 251 | |
| 252 | |
| 253 RawMachineLabel* FastAccessorAssembler::FromId(LabelId label) const { | |
| 254 CHECK_LT(label.label_id, labels_.size()); | |
| 255 CHECK_NOT_NULL(labels_.at(label.label_id)); | |
| 256 return labels_.at(label.label_id); | |
| 257 } | |
| 258 | |
| 259 | |
| 260 } // namespace compiler | |
| 261 } // namespace internal | |
| 262 } // namespace v8 | |
| OLD | NEW |