fast-accessor-assembler.h 3.94 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_FAST_ACCESSOR_ASSEMBLER_H_
#define V8_FAST_ACCESSOR_ASSEMBLER_H_
7 8

#include <stdint.h>
9
#include <memory>
10 11 12 13 14 15
#include <vector>

#include "include/v8-experimental.h"
#include "src/base/macros.h"
#include "src/handles.h"

16
// For CodeStubAssembler::Label. (We cannot forward-declare inner classes.)
17
#include "src/code-stub-assembler.h"
18 19 20 21 22 23 24 25 26 27

namespace v8 {
namespace internal {

class Code;
class Isolate;
class Zone;

namespace compiler {
class Node;
28
}
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

// This interface "exports" an aggregated subset of RawMachineAssembler, for
// use by the API to implement Fast Dom Accessors.
//
// This interface is made for this single purpose only and does not attempt
// to implement a general purpose solution. If you need one, please look at
// RawMachineAssembler instead.
//
// The life cycle of a FastAccessorAssembler has two phases:
// - After creating the instance, you can call an arbitrary sequence of
//   builder functions to build the desired function.
// - When done, you can Build() the accessor and query for the build results.
//
// You cannot call any result getters before Build() was called & successful;
// and you cannot call any builder functions after Build() was called.
class FastAccessorAssembler {
 public:
  typedef v8::experimental::FastAccessorBuilder::ValueId ValueId;
  typedef v8::experimental::FastAccessorBuilder::LabelId LabelId;
48
  typedef v8::FunctionCallback FunctionCallback;
49 50 51 52 53 54 55 56

  explicit FastAccessorAssembler(Isolate* isolate);
  ~FastAccessorAssembler();

  // Builder / assembler functions:
  ValueId IntegerConstant(int int_constant);
  ValueId GetReceiver();
  ValueId LoadInternalField(ValueId value_id, int field_no);
57 58 59 60 61 62 63 64 65

  // Loads internal field and assumes the object is indeed a valid API object
  // with the proper internal fields present.
  // The intended use is to call this on an object whose structure has already
  // been checked previously, e.g. the accessor's receiver, which is map-checked
  // before the fast accessor is called on it. Using this on an arbitrary object
  // will result in unsafe memory accesses.
  ValueId LoadInternalFieldUnchecked(ValueId value_id, int field_no);

66 67 68
  ValueId LoadValue(ValueId value_id, int offset);
  ValueId LoadObject(ValueId value_id, int offset);

69 70 71
  // Converts a machine integer to a SMI.
  ValueId ToSmi(ValueId value_id);

72 73 74 75 76 77
  // Builder / assembler functions for control flow.
  void ReturnValue(ValueId value_id);
  void CheckFlagSetOrReturnNull(ValueId value_id, int mask);
  void CheckNotZeroOrReturnNull(ValueId value_id);
  LabelId MakeLabel();
  void SetLabel(LabelId label_id);
78
  void Goto(LabelId label_id);
79 80
  void CheckNotZeroOrJump(ValueId value_id, LabelId label_id);

81 82 83
  // C++ callback.
  ValueId Call(FunctionCallback callback, ValueId arg);

84 85 86 87
  // Assemble the code.
  MaybeHandle<Code> Build();

 private:
88
  ValueId FromRaw(compiler::Node* node);
89
  LabelId FromRaw(CodeStubAssembler::Label* label);
90
  compiler::Node* FromId(ValueId value) const;
91
  CodeStubAssembler::Label* FromId(LabelId value) const;
92

93 94
  void CheckIsJSObjectOrJump(ValueId value, LabelId label_id);

95
  void Clear();
96
  Zone* zone() { return &zone_; }
97
  Isolate* isolate() const { return isolate_; }
98 99

  Zone zone_;
100
  Isolate* isolate_;
101
  std::unique_ptr<CodeStubAssembler> assembler_;
102 103 104 105

  // To prevent exposing the RMA internals to the outside world, we'll map
  // Node + Label pointers integers wrapped in ValueId and LabelId instances.
  // These vectors maintain this mapping.
106
  std::vector<compiler::Node*> nodes_;
107
  std::vector<CodeStubAssembler::Label*> labels_;
108 109 110 111 112 113 114 115 116 117 118

  // Remember the current state for easy error checking. (We prefer to be
  // strict as this class will be exposed at the API.)
  enum { kBuilding, kBuilt, kError } state_;

  DISALLOW_COPY_AND_ASSIGN(FastAccessorAssembler);
};

}  // namespace internal
}  // namespace v8

119
#endif  // V8_FAST_ACCESSOR_ASSEMBLER_H_