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 #ifndef V8_FAST_ACCESSOR_ASSEMBLER_H_ 6 #define V8_FAST_ACCESSOR_ASSEMBLER_H_ 7 8 #include <stdint.h> 9 #include <vector> 10 11 #include "include/v8-experimental.h" 12 #include "src/base/macros.h" 13 #include "src/base/smart-pointers.h" 14 #include "src/handles.h" 15 16 // For CodeStubAssembler::Label. (We cannot forward-declare inner classes.) 17 #include "src/code-stub-assembler.h" 18 19 namespace v8 { 20 namespace internal { 21 22 class Code; 23 class Isolate; 24 class Zone; 25 26 namespace compiler { 27 class Node; 28 } 29 30 // This interface "exports" an aggregated subset of RawMachineAssembler, for 31 // use by the API to implement Fast Dom Accessors. 32 // 33 // This interface is made for this single purpose only and does not attempt 34 // to implement a general purpose solution. If you need one, please look at 35 // RawMachineAssembler instead. 36 // 37 // The life cycle of a FastAccessorAssembler has two phases: 38 // - After creating the instance, you can call an arbitrary sequence of 39 // builder functions to build the desired function. 40 // - When done, you can Build() the accessor and query for the build results. 41 // 42 // You cannot call any result getters before Build() was called & successful; 43 // and you cannot call any builder functions after Build() was called. 44 class FastAccessorAssembler { 45 public: 46 typedef v8::experimental::FastAccessorBuilder::ValueId ValueId; 47 typedef v8::experimental::FastAccessorBuilder::LabelId LabelId; 48 typedef v8::FunctionCallback FunctionCallback; 49 50 explicit FastAccessorAssembler(Isolate* isolate); 51 ~FastAccessorAssembler(); 52 53 // Builder / assembler functions: 54 ValueId IntegerConstant(int int_constant); 55 ValueId GetReceiver(); 56 ValueId LoadInternalField(ValueId value_id, int field_no); 57 ValueId LoadValue(ValueId value_id, int offset); 58 ValueId LoadObject(ValueId value_id, int offset); 59 60 // Builder / assembler functions for control flow. 61 void ReturnValue(ValueId value_id); 62 void CheckFlagSetOrReturnNull(ValueId value_id, int mask); 63 void CheckNotZeroOrReturnNull(ValueId value_id); 64 LabelId MakeLabel(); 65 void SetLabel(LabelId label_id); 66 void CheckNotZeroOrJump(ValueId value_id, LabelId label_id); 67 68 // C++ callback. 69 ValueId Call(FunctionCallback callback, ValueId arg); 70 71 // Assemble the code. 72 MaybeHandle<Code> Build(); 73 74 private: 75 ValueId FromRaw(compiler::Node* node); 76 LabelId FromRaw(CodeStubAssembler::Label* label); 77 compiler::Node* FromId(ValueId value) const; 78 CodeStubAssembler::Label* FromId(LabelId value) const; 79 80 void Clear(); 81 Zone* zone() { return &zone_; } 82 Isolate* isolate() const { return isolate_; } 83 84 Zone zone_; 85 Isolate* isolate_; 86 base::SmartPointer<CodeStubAssembler> assembler_; 87 88 // To prevent exposing the RMA internals to the outside world, we'll map 89 // Node + Label pointers integers wrapped in ValueId and LabelId instances. 90 // These vectors maintain this mapping. 91 std::vector<compiler::Node*> nodes_; 92 std::vector<CodeStubAssembler::Label*> labels_; 93 94 // Remember the current state for easy error checking. (We prefer to be 95 // strict as this class will be exposed at the API.) 96 enum { kBuilding, kBuilt, kError } state_; 97 98 DISALLOW_COPY_AND_ASSIGN(FastAccessorAssembler); 99 }; 100 101 } // namespace internal 102 } // namespace v8 103 104 #endif // V8_FAST_ACCESSOR_ASSEMBLER_H_ 105