object-macros.h 21.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2016 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.

// Note 1: Any file that includes this one should include object-macros-undef.h
// at the bottom.

// Note 2: This file is deliberately missing the include guards (the undeffing
// approach wouldn't work otherwise).
10 11
//
// PRESUBMIT_INTENTIONALLY_MISSING_INCLUDE_GUARD
12

13 14 15 16
// The accessors with RELAXED_, ACQUIRE_, and RELEASE_ prefixes should be used
// for fields that can be written to and read from multiple threads at the same
// time. See comments in src/base/atomicops.h for the memory ordering sematics.

17
#include "src/base/memory.h"
18

19 20
// Since this changes visibility, it should always be last in a class
// definition.
21 22 23 24 25 26 27 28
#define OBJECT_CONSTRUCTORS(Type, ...)             \
 public:                                           \
  constexpr Type() : __VA_ARGS__() {}              \
                                                   \
 protected:                                        \
  template <typename TFieldType, int kFieldOffset> \
  friend class TaggedField;                        \
                                                   \
29
  explicit inline Type(Address ptr)
30 31 32 33

#define OBJECT_CONSTRUCTORS_IMPL(Type, Super) \
  inline Type::Type(Address ptr) : Super(ptr) { SLOW_DCHECK(Is##Type()); }

34 35 36 37
#define NEVER_READ_ONLY_SPACE   \
  inline Heap* GetHeap() const; \
  inline Isolate* GetIsolate() const;

38 39
// TODO(leszeks): Add checks in the factory that we never allocate these
// objects in RO space.
40 41 42 43
#define NEVER_READ_ONLY_SPACE_IMPL(Type)                                   \
  Heap* Type::GetHeap() const { return GetHeapFromWritableObject(*this); } \
  Isolate* Type::GetIsolate() const {                                      \
    return GetIsolateFromWritableObject(*this);                            \
44 45
  }

46 47 48
#define DECL_PRIMITIVE_ACCESSORS(name, type) \
  inline type name() const;                  \
  inline void set_##name(type value);
49

50 51 52 53
#define DECL_SYNCHRONIZED_PRIMITIVE_ACCESSORS(name, type) \
  inline type synchronized_##name() const;                \
  inline void synchronized_set_##name(type value);

54
#define DECL_BOOLEAN_ACCESSORS(name) DECL_PRIMITIVE_ACCESSORS(name, bool)
55

56 57
#define DECL_INT_ACCESSORS(name) DECL_PRIMITIVE_ACCESSORS(name, int)

58 59 60
#define DECL_SYNCHRONIZED_INT_ACCESSORS(name) \
  DECL_SYNCHRONIZED_PRIMITIVE_ACCESSORS(name, int)

61
#define DECL_INT32_ACCESSORS(name) DECL_PRIMITIVE_ACCESSORS(name, int32_t)
62

63 64 65 66
#define DECL_UINT16_ACCESSORS(name) \
  inline uint16_t name() const;     \
  inline void set_##name(int value);

67 68 69 70
#define DECL_INT16_ACCESSORS(name) \
  inline int16_t name() const;     \
  inline void set_##name(int16_t value);

71 72 73 74
#define DECL_UINT8_ACCESSORS(name) \
  inline uint8_t name() const;     \
  inline void set_##name(int value);

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
// TODO(ishell): eventually isolate-less getters should not be used anymore.
// For full pointer-mode the C++ compiler should optimize away unused isolate
// parameter.
#define DECL_GETTER(name, type) \
  inline type name() const;     \
  inline type name(Isolate* isolate) const;

#define DEF_GETTER(holder, name, type)               \
  type holder::name() const {                        \
    Isolate* isolate = GetIsolateForPtrCompr(*this); \
    return holder::name(isolate);                    \
  }                                                  \
  type holder::name(Isolate* isolate) const

#define DECL_ACCESSORS(name, type)   \
  DECL_GETTER(name, type)            \
  inline void set_##name(type value, \
92 93
                         WriteBarrierMode mode = UPDATE_WRITE_BARRIER);

94
#define DECL_CAST(Type)                                 \
95 96 97
  V8_INLINE static Type cast(Object object);            \
  V8_INLINE static Type unchecked_cast(Object object) { \
    return bit_cast<Type>(object);                      \
98
  }
99

100
#define CAST_ACCESSOR(Type) \
101
  Type Type::cast(Object object) { return Type(object.ptr()); }
102

103 104
#define INT_ACCESSORS(holder, name, offset)                   \
  int holder::name() const { return ReadField<int>(offset); } \
105
  void holder::set_##name(int value) { WriteField<int>(offset, value); }
106

107 108
#define INT32_ACCESSORS(holder, name, offset)                         \
  int32_t holder::name() const { return ReadField<int32_t>(offset); } \
109
  void holder::set_##name(int32_t value) { WriteField<int32_t>(offset, value); }
110

111 112
#define RELAXED_INT32_ACCESSORS(holder, name, offset) \
  int32_t holder::name() const {                      \
113
    return RELAXED_READ_INT32_FIELD(*this, offset);   \
114 115
  }                                                   \
  void holder::set_##name(int32_t value) {            \
116
    RELAXED_WRITE_INT32_FIELD(*this, offset, value);  \
117 118
  }

119 120 121 122 123
#define UINT16_ACCESSORS(holder, name, offset)                          \
  uint16_t holder::name() const { return ReadField<uint16_t>(offset); } \
  void holder::set_##name(int value) {                                  \
    DCHECK_GE(value, 0);                                                \
    DCHECK_LE(value, static_cast<uint16_t>(-1));                        \
124
    WriteField<uint16_t>(offset, value);                                \
125 126
  }

127 128 129 130 131
#define UINT8_ACCESSORS(holder, name, offset)                         \
  uint8_t holder::name() const { return ReadField<uint8_t>(offset); } \
  void holder::set_##name(int value) {                                \
    DCHECK_GE(value, 0);                                              \
    DCHECK_LE(value, static_cast<uint8_t>(-1));                       \
132
    WriteField<uint8_t>(offset, value);                               \
133 134
  }

135 136
#define ACCESSORS_CHECKED2(holder, name, type, offset, get_condition, \
                           set_condition)                             \
137
  DEF_GETTER(holder, name, type) {                                    \
138
    type value = TaggedField<type, offset>::load(isolate, *this);     \
139 140 141 142 143
    DCHECK(get_condition);                                            \
    return value;                                                     \
  }                                                                   \
  void holder::set_##name(type value, WriteBarrierMode mode) {        \
    DCHECK(set_condition);                                            \
144
    TaggedField<type, offset>::store(*this, value);                   \
145
    CONDITIONAL_WRITE_BARRIER(*this, offset, value, mode);            \
146
  }
147

148 149 150 151 152 153
#define ACCESSORS_CHECKED(holder, name, type, offset, condition) \
  ACCESSORS_CHECKED2(holder, name, type, offset, condition, condition)

#define ACCESSORS(holder, name, type, offset) \
  ACCESSORS_CHECKED(holder, name, type, offset, true)

154 155
#define SYNCHRONIZED_ACCESSORS_CHECKED2(holder, name, type, offset,       \
                                        get_condition, set_condition)     \
156
  DEF_GETTER(holder, name, type) {                                        \
157 158 159 160 161 162 163 164
    type value = TaggedField<type, offset>::Acquire_Load(isolate, *this); \
    DCHECK(get_condition);                                                \
    return value;                                                         \
  }                                                                       \
  void holder::set_##name(type value, WriteBarrierMode mode) {            \
    DCHECK(set_condition);                                                \
    TaggedField<type, offset>::Release_Store(*this, value);               \
    CONDITIONAL_WRITE_BARRIER(*this, offset, value, mode);                \
165 166 167 168 169 170 171 172 173
  }

#define SYNCHRONIZED_ACCESSORS_CHECKED(holder, name, type, offset, condition) \
  SYNCHRONIZED_ACCESSORS_CHECKED2(holder, name, type, offset, condition,      \
                                  condition)

#define SYNCHRONIZED_ACCESSORS(holder, name, type, offset) \
  SYNCHRONIZED_ACCESSORS_CHECKED(holder, name, type, offset, true)

174 175
#define WEAK_ACCESSORS_CHECKED2(holder, name, offset, get_condition,  \
                                set_condition)                        \
176
  DEF_GETTER(holder, name, MaybeObject) {                             \
177 178
    MaybeObject value =                                               \
        TaggedField<MaybeObject, offset>::load(isolate, *this);       \
179 180 181 182 183
    DCHECK(get_condition);                                            \
    return value;                                                     \
  }                                                                   \
  void holder::set_##name(MaybeObject value, WriteBarrierMode mode) { \
    DCHECK(set_condition);                                            \
184
    TaggedField<MaybeObject, offset>::store(*this, value);            \
185
    CONDITIONAL_WEAK_WRITE_BARRIER(*this, offset, value, mode);       \
186 187 188 189 190 191 192 193
  }

#define WEAK_ACCESSORS_CHECKED(holder, name, offset, condition) \
  WEAK_ACCESSORS_CHECKED2(holder, name, offset, condition, condition)

#define WEAK_ACCESSORS(holder, name, offset) \
  WEAK_ACCESSORS_CHECKED(holder, name, offset, true)

194
// Getter that returns a Smi as an int and writes an int as a Smi.
195 196 197 198 199 200 201 202 203
#define SMI_ACCESSORS_CHECKED(holder, name, offset, condition)   \
  int holder::name() const {                                     \
    DCHECK(condition);                                           \
    Smi value = TaggedField<Smi, offset>::load(*this);           \
    return value.value();                                        \
  }                                                              \
  void holder::set_##name(int value) {                           \
    DCHECK(condition);                                           \
    TaggedField<Smi, offset>::store(*this, Smi::FromInt(value)); \
204 205 206 207 208
  }

#define SMI_ACCESSORS(holder, name, offset) \
  SMI_ACCESSORS_CHECKED(holder, name, offset, true)

209 210 211 212 213 214 215
#define SYNCHRONIZED_SMI_ACCESSORS(holder, name, offset)                 \
  int holder::synchronized_##name() const {                              \
    Smi value = TaggedField<Smi, offset>::Acquire_Load(*this);           \
    return value.value();                                                \
  }                                                                      \
  void holder::synchronized_set_##name(int value) {                      \
    TaggedField<Smi, offset>::Release_Store(*this, Smi::FromInt(value)); \
216 217
  }

218 219 220 221 222 223 224
#define RELAXED_SMI_ACCESSORS(holder, name, offset)                      \
  int holder::relaxed_read_##name() const {                              \
    Smi value = TaggedField<Smi, offset>::Relaxed_Load(*this);           \
    return value.value();                                                \
  }                                                                      \
  void holder::relaxed_write_##name(int value) {                         \
    TaggedField<Smi, offset>::Relaxed_Store(*this, Smi::FromInt(value)); \
225 226
  }

227 228 229 230 231 232 233 234
#define TQ_SMI_ACCESSORS(holder, name)                                       \
  int holder::name() const {                                                 \
    return TorqueGenerated##holder<holder, Super>::name().value();           \
  }                                                                          \
  void holder::set_##name(int value) {                                       \
    TorqueGenerated##holder<holder, Super>::set_##name(Smi::FromInt(value)); \
  }

235 236 237 238 239 240 241 242 243
#define BOOL_GETTER(holder, field, name, offset) \
  bool holder::name() const { return BooleanBit::get(field(), offset); }

#define BOOL_ACCESSORS(holder, field, name, offset)                      \
  bool holder::name() const { return BooleanBit::get(field(), offset); } \
  void holder::set_##name(bool value) {                                  \
    set_##field(BooleanBit::set(field(), offset, value));                \
  }

244 245 246 247 248 249 250 251
#define BIT_FIELD_ACCESSORS(holder, field, name, BitField)      \
  typename BitField::FieldType holder::name() const {           \
    return BitField::decode(field());                           \
  }                                                             \
  void holder::set_##name(typename BitField::FieldType value) { \
    set_##field(BitField::update(field(), value));              \
  }

252 253 254 255 256
#define INSTANCE_TYPE_CHECKER(type, forinstancetype)    \
  V8_INLINE bool Is##type(InstanceType instance_type) { \
    return instance_type == forinstancetype;            \
  }

257
#define TYPE_CHECKER(type, ...)                                         \
258
  DEF_GETTER(HeapObject, Is##type, bool) {                              \
259
    return InstanceTypeChecker::Is##type(map(isolate).instance_type()); \
260 261
  }

262 263
#define RELAXED_INT16_ACCESSORS(holder, name, offset) \
  int16_t holder::name() const {                      \
264
    return RELAXED_READ_INT16_FIELD(*this, offset);   \
265 266
  }                                                   \
  void holder::set_##name(int16_t value) {            \
267
    RELAXED_WRITE_INT16_FIELD(*this, offset, value);  \
268 269
  }

270
#define FIELD_ADDR(p, offset) ((p).ptr() + offset - kHeapObjectTag)
271

272
#define ACQUIRE_READ_FIELD(p, offset) \
273
  TaggedField<Object>::Acquire_Load(p, offset)
274

275
#define RELAXED_READ_FIELD(p, offset) \
276
  TaggedField<Object>::Relaxed_Load(p, offset)
277

278
#define RELAXED_READ_WEAK_FIELD(p, offset) \
279
  TaggedField<MaybeObject>::Relaxed_Load(p, offset)
280

281
#define WRITE_FIELD(p, offset, value) \
282
  TaggedField<Object>::store(p, offset, value)
283

284
#define RELEASE_WRITE_FIELD(p, offset, value) \
285
  TaggedField<Object>::Release_Store(p, offset, value)
286 287

#define RELAXED_WRITE_FIELD(p, offset, value) \
288
  TaggedField<Object>::Relaxed_Store(p, offset, value)
289

290
#define RELAXED_WRITE_WEAK_FIELD(p, offset, value) \
291
  TaggedField<MaybeObject>::Relaxed_Store(p, offset, value)
292

293 294 295
#ifdef V8_DISABLE_WRITE_BARRIERS
#define WRITE_BARRIER(object, offset, value)
#else
296 297 298 299 300
#define WRITE_BARRIER(object, offset, value)                       \
  do {                                                             \
    DCHECK_NOT_NULL(GetHeapFromWritableObject(object));            \
    MarkingBarrier(object, (object).RawField(offset), value);      \
    GenerationalBarrier(object, (object).RawField(offset), value); \
301
  } while (false)
302
#endif
303

304 305 306
#ifdef V8_DISABLE_WRITE_BARRIERS
#define WEAK_WRITE_BARRIER(object, offset, value)
#else
307 308 309 310 311
#define WEAK_WRITE_BARRIER(object, offset, value)                           \
  do {                                                                      \
    DCHECK_NOT_NULL(GetHeapFromWritableObject(object));                     \
    MarkingBarrier(object, (object).RawMaybeWeakField(offset), value);      \
    GenerationalBarrier(object, (object).RawMaybeWeakField(offset), value); \
312
  } while (false)
313
#endif
314

315 316 317
#ifdef V8_DISABLE_WRITE_BARRIERS
#define EPHEMERON_KEY_WRITE_BARRIER(object, offset, value)
#else
318 319 320 321 322 323
#define EPHEMERON_KEY_WRITE_BARRIER(object, offset, value)                    \
  do {                                                                        \
    DCHECK_NOT_NULL(GetHeapFromWritableObject(object));                       \
    EphemeronHashTable table = EphemeronHashTable::cast(object);              \
    MarkingBarrier(object, (object).RawField(offset), value);                 \
    GenerationalEphemeronKeyBarrier(table, (object).RawField(offset), value); \
324
  } while (false)
325
#endif
326

327 328 329
#ifdef V8_DISABLE_WRITE_BARRIERS
#define CONDITIONAL_WRITE_BARRIER(object, offset, value, mode)
#else
330 331 332 333 334 335 336 337 338 339
#define CONDITIONAL_WRITE_BARRIER(object, offset, value, mode)       \
  do {                                                               \
    DCHECK_NOT_NULL(GetHeapFromWritableObject(object));              \
    DCHECK_NE(mode, UPDATE_EPHEMERON_KEY_WRITE_BARRIER);             \
    if (mode != SKIP_WRITE_BARRIER) {                                \
      if (mode == UPDATE_WRITE_BARRIER) {                            \
        MarkingBarrier(object, (object).RawField(offset), value);    \
      }                                                              \
      GenerationalBarrier(object, (object).RawField(offset), value); \
    }                                                                \
340
  } while (false)
341
#endif
342

343 344 345
#ifdef V8_DISABLE_WRITE_BARRIERS
#define CONDITIONAL_WEAK_WRITE_BARRIER(object, offset, value, mode)
#else
346 347 348 349 350 351 352 353 354 355
#define CONDITIONAL_WEAK_WRITE_BARRIER(object, offset, value, mode)           \
  do {                                                                        \
    DCHECK_NOT_NULL(GetHeapFromWritableObject(object));                       \
    DCHECK_NE(mode, UPDATE_EPHEMERON_KEY_WRITE_BARRIER);                      \
    if (mode != SKIP_WRITE_BARRIER) {                                         \
      if (mode == UPDATE_WRITE_BARRIER) {                                     \
        MarkingBarrier(object, (object).RawMaybeWeakField(offset), value);    \
      }                                                                       \
      GenerationalBarrier(object, (object).RawMaybeWeakField(offset), value); \
    }                                                                         \
356
  } while (false)
357
#endif
358

359 360 361
#ifdef V8_DISABLE_WRITE_BARRIERS
#define CONDITIONAL_EPHEMERON_KEY_WRITE_BARRIER(object, offset, value, mode)
#else
362 363 364 365 366 367 368
#define CONDITIONAL_EPHEMERON_KEY_WRITE_BARRIER(object, offset, value, mode) \
  do {                                                                       \
    DCHECK_NOT_NULL(GetHeapFromWritableObject(object));                      \
    DCHECK_NE(mode, UPDATE_EPHEMERON_KEY_WRITE_BARRIER);                     \
    EphemeronHashTable table = EphemeronHashTable::cast(object);             \
    if (mode != SKIP_WRITE_BARRIER) {                                        \
      if (mode == UPDATE_WRITE_BARRIER) {                                    \
369
        MarkingBarrier(object, (object).RawField(offset), value);            \
370
      }                                                                      \
371
      GenerationalEphemeronKeyBarrier(table, (object).RawField(offset),      \
372 373 374
                                      value);                                \
    }                                                                        \
  } while (false)
375
#endif
376

377 378 379 380
#define ACQUIRE_READ_INT32_FIELD(p, offset) \
  static_cast<int32_t>(base::Acquire_Load(  \
      reinterpret_cast<const base::Atomic32*>(FIELD_ADDR(p, offset))))

381 382 383 384 385
#define RELAXED_WRITE_INT8_FIELD(p, offset, value)                             \
  base::Relaxed_Store(reinterpret_cast<base::Atomic8*>(FIELD_ADDR(p, offset)), \
                      static_cast<base::Atomic8>(value));
#define RELAXED_READ_INT8_FIELD(p, offset) \
  static_cast<int8_t>(base::Relaxed_Load(  \
386
      reinterpret_cast<const base::Atomic8*>(FIELD_ADDR(p, offset))))
387

388 389 390 391 392 393 394 395 396
#define RELAXED_READ_INT16_FIELD(p, offset) \
  static_cast<int16_t>(base::Relaxed_Load(  \
      reinterpret_cast<const base::Atomic16*>(FIELD_ADDR(p, offset))))

#define RELAXED_WRITE_INT16_FIELD(p, offset, value)             \
  base::Relaxed_Store(                                          \
      reinterpret_cast<base::Atomic16*>(FIELD_ADDR(p, offset)), \
      static_cast<base::Atomic16>(value));

397 398 399 400 401 402 403 404 405
#define RELAXED_READ_UINT32_FIELD(p, offset) \
  static_cast<uint32_t>(base::Relaxed_Load(  \
      reinterpret_cast<const base::Atomic32*>(FIELD_ADDR(p, offset))))

#define RELAXED_WRITE_UINT32_FIELD(p, offset, value)            \
  base::Relaxed_Store(                                          \
      reinterpret_cast<base::Atomic32*>(FIELD_ADDR(p, offset)), \
      static_cast<base::Atomic32>(value));

406 407 408 409
#define RELAXED_READ_INT32_FIELD(p, offset) \
  static_cast<int32_t>(base::Relaxed_Load(  \
      reinterpret_cast<const base::Atomic32*>(FIELD_ADDR(p, offset))))

410 411
#define RELEASE_WRITE_INT32_FIELD(p, offset, value)             \
  base::Release_Store(                                          \
412 413 414 415 416
      reinterpret_cast<base::Atomic32*>(FIELD_ADDR(p, offset)), \
      static_cast<base::Atomic32>(value))

#define RELAXED_WRITE_INT32_FIELD(p, offset, value)             \
  base::Relaxed_Store(                                          \
417 418 419
      reinterpret_cast<base::Atomic32*>(FIELD_ADDR(p, offset)), \
      static_cast<base::Atomic32>(value));

420 421
#define RELAXED_READ_BYTE_FIELD(p, offset) \
  static_cast<byte>(base::Relaxed_Load(    \
422
      reinterpret_cast<const base::Atomic8*>(FIELD_ADDR(p, offset))))
423

424 425 426
#define RELAXED_WRITE_BYTE_FIELD(p, offset, value)                             \
  base::Relaxed_Store(reinterpret_cast<base::Atomic8*>(FIELD_ADDR(p, offset)), \
                      static_cast<base::Atomic8>(value));
427

428 429 430 431 432 433
#ifdef OBJECT_PRINT
#define DECL_PRINTER(Name) void Name##Print(std::ostream& os);  // NOLINT
#else
#define DECL_PRINTER(Name)
#endif

434
#ifdef VERIFY_HEAP
435
#define DECL_VERIFIER(Name) void Name##Verify(Isolate* isolate);
436 437
#define EXPORT_DECL_VERIFIER(Name) \
  V8_EXPORT_PRIVATE void Name##Verify(Isolate* isolate);
438
#else
439
#define DECL_VERIFIER(Name)
440
#define EXPORT_DECL_VERIFIER(Name)
441
#endif
442

443 444 445 446 447
#define DEFINE_DEOPT_ELEMENT_ACCESSORS(name, type) \
  type DeoptimizationData::name() const {          \
    return type::cast(get(k##name##Index));        \
  }                                                \
  void DeoptimizationData::Set##name(type value) { set(k##name##Index, value); }
448 449

#define DEFINE_DEOPT_ENTRY_ACCESSORS(name, type)                \
450
  type DeoptimizationData::name(int i) const {                  \
451 452
    return type::cast(get(IndexForEntry(i) + k##name##Offset)); \
  }                                                             \
453
  void DeoptimizationData::Set##name(int i, type value) {       \
454 455
    set(IndexForEntry(i) + k##name##Offset, value);             \
  }
456

457 458 459 460 461 462 463 464 465
#define TQ_OBJECT_CONSTRUCTORS(Type)               \
 public:                                           \
  constexpr Type() = default;                      \
                                                   \
 protected:                                        \
  template <typename TFieldType, int kFieldOffset> \
  friend class TaggedField;                        \
                                                   \
  inline explicit Type(Address ptr);               \
466 467 468 469 470
  friend class TorqueGenerated##Type<Type, Super>;

#define TQ_OBJECT_CONSTRUCTORS_IMPL(Type) \
  inline Type::Type(Address ptr)          \
      : TorqueGenerated##Type<Type, Type::Super>(ptr) {}