summaryrefslogtreecommitdiff
path: root/deps/v8/src/compiler/compilation-dependencies.cc
blob: a672d0a1f04623a2831be420947cd608896a8dce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// 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.

#include "src/compiler/compilation-dependencies.h"

#include "src/handles-inl.h"
#include "src/objects-inl.h"

namespace v8 {
namespace internal {
namespace compiler {

CompilationDependencies::CompilationDependencies(Isolate* isolate, Zone* zone)
    : zone_(zone), dependencies_(zone) {}

class CompilationDependencies::Dependency : public ZoneObject {
 public:
  virtual bool IsSane() const = 0;
  virtual bool IsValid() const = 0;
  virtual void Install(Isolate* isolate, Handle<WeakCell> code) = 0;
};

class InitialMapDependency final : public CompilationDependencies::Dependency {
 public:
  InitialMapDependency(const JSFunctionRef& function, const MapRef& initial_map)
      : function_(function), initial_map_(initial_map) {
    DCHECK(IsSane());
  }

  bool IsSane() const override {
    DisallowHeapAccess no_heap_access;
    CHECK(function_.has_initial_map());
    return function_.initial_map().equals(initial_map_);
  }

  bool IsValid() const override {
    Handle<JSFunction> function = function_.object<JSFunction>();
    return function->has_initial_map() &&
           function->initial_map() == *initial_map_.object<Map>();
  }

  void Install(Isolate* isolate, Handle<WeakCell> code) override {
    DCHECK(IsValid());
    DependentCode::InstallDependency(isolate, code, initial_map_.object<Map>(),
                                     DependentCode::kInitialMapChangedGroup);
  }

 private:
  JSFunctionRef function_;
  MapRef initial_map_;
};

class StableMapDependency final : public CompilationDependencies::Dependency {
 public:
  explicit StableMapDependency(const MapRef& map) : map_(map) {
    DCHECK(IsSane());
  }

  bool IsSane() const override {
    DisallowHeapAccess no_heap_access;
    return map_.is_stable();
  }

  bool IsValid() const override { return map_.object<Map>()->is_stable(); }

  void Install(Isolate* isolate, Handle<WeakCell> code) override {
    DCHECK(IsValid());
    DependentCode::InstallDependency(isolate, code, map_.object<Map>(),
                                     DependentCode::kPrototypeCheckGroup);
  }

 private:
  MapRef map_;
};

class TransitionDependency final : public CompilationDependencies::Dependency {
 public:
  explicit TransitionDependency(const MapRef& map) : map_(map) {
    DCHECK(IsSane());
  }

  bool IsSane() const override {
    DisallowHeapAccess no_heap_access;
    return !map_.is_deprecated();
  }

  bool IsValid() const override { return !map_.object<Map>()->is_deprecated(); }

  void Install(Isolate* isolate, Handle<WeakCell> code) override {
    DCHECK(IsValid());
    DependentCode::InstallDependency(isolate, code, map_.object<Map>(),
                                     DependentCode::kTransitionGroup);
  }

 private:
  MapRef map_;
};

class PretenureModeDependency final
    : public CompilationDependencies::Dependency {
 public:
  PretenureModeDependency(const AllocationSiteRef& site, PretenureFlag mode)
      : site_(site), mode_(mode) {
    DCHECK(IsSane());
  }

  bool IsSane() const override {
    DisallowHeapAccess no_heap_access;
    return mode_ == site_.GetPretenureMode();
  }

  bool IsValid() const override {
    return mode_ == site_.object<AllocationSite>()->GetPretenureMode();
  }

  void Install(Isolate* isolate, Handle<WeakCell> code) override {
    DCHECK(IsValid());
    DependentCode::InstallDependency(
        isolate, code, site_.object<AllocationSite>(),
        DependentCode::kAllocationSiteTenuringChangedGroup);
  }

 private:
  AllocationSiteRef site_;
  PretenureFlag mode_;
};

class FieldTypeDependency final : public CompilationDependencies::Dependency {
 public:
  FieldTypeDependency(const MapRef& owner, int descriptor,
                      const FieldTypeRef& type)
      : owner_(owner), descriptor_(descriptor), type_(type) {
    DCHECK(IsSane());
  }

  bool IsSane() const override {
    DisallowHeapAccess no_heap_access;
    CHECK(owner_.equals(owner_.FindFieldOwner(descriptor_)));
    return type_.equals(owner_.GetFieldType(descriptor_));
  }

  bool IsValid() const override {
    DisallowHeapAllocation no_heap_allocation;
    Handle<Map> owner = owner_.object<Map>();
    Handle<FieldType> type = type_.object<FieldType>();
    return *type == owner->instance_descriptors()->GetFieldType(descriptor_);
  }

  void Install(Isolate* isolate, Handle<WeakCell> code) override {
    DCHECK(IsValid());
    DependentCode::InstallDependency(isolate, code, owner_.object<Map>(),
                                     DependentCode::kFieldOwnerGroup);
  }

 private:
  MapRef owner_;
  int descriptor_;
  FieldTypeRef type_;
};

class GlobalPropertyDependency final
    : public CompilationDependencies::Dependency {
 public:
  GlobalPropertyDependency(const PropertyCellRef& cell, PropertyCellType type,
                           bool read_only)
      : cell_(cell), type_(type), read_only_(read_only) {
    DCHECK(IsSane());
  }

  bool IsSane() const override {
    DisallowHeapAccess no_heap_access;
    return type_ == cell_.property_details().cell_type() &&
           read_only_ == cell_.property_details().IsReadOnly();
  }

  bool IsValid() const override {
    Handle<PropertyCell> cell = cell_.object<PropertyCell>();
    return type_ == cell->property_details().cell_type() &&
           read_only_ == cell->property_details().IsReadOnly();
  }

  void Install(Isolate* isolate, Handle<WeakCell> code) override {
    DCHECK(IsValid());
    DependentCode::InstallDependency(isolate, code,
                                     cell_.object<PropertyCell>(),
                                     DependentCode::kPropertyCellChangedGroup);
  }

 private:
  PropertyCellRef cell_;
  PropertyCellType type_;
  bool read_only_;
};

class ProtectorDependency final : public CompilationDependencies::Dependency {
 public:
  explicit ProtectorDependency(const PropertyCellRef& cell) : cell_(cell) {
    DCHECK(IsSane());
  }

  bool IsSane() const override {
    DisallowHeapAccess no_heap_access;
    return cell_.value().IsSmi() &&
           cell_.value().AsSmi() == Isolate::kProtectorValid;
  }

  bool IsValid() const override {
    Handle<PropertyCell> cell = cell_.object<PropertyCell>();
    return cell->value() == Smi::FromInt(Isolate::kProtectorValid);
  }

  void Install(Isolate* isolate, Handle<WeakCell> code) override {
    DCHECK(IsValid());
    DependentCode::InstallDependency(isolate, code,
                                     cell_.object<PropertyCell>(),
                                     DependentCode::kPropertyCellChangedGroup);
  }

 private:
  PropertyCellRef cell_;
};

class ElementsKindDependency final
    : public CompilationDependencies::Dependency {
 public:
  ElementsKindDependency(const AllocationSiteRef& site, ElementsKind kind)
      : site_(site), kind_(kind) {
    DCHECK(IsSane());
  }

  bool IsSane() const override {
    DisallowHeapAccess no_heap_access;
    DCHECK(AllocationSite::ShouldTrack(kind_));
    ElementsKind kind = site_.PointsToLiteral()
                            ? site_.boilerplate().GetElementsKind()
                            : site_.GetElementsKind();
    return kind_ == kind;
  }

  bool IsValid() const override {
    Handle<AllocationSite> site = site_.object<AllocationSite>();
    ElementsKind kind = site->PointsToLiteral()
                            ? site->boilerplate()->GetElementsKind()
                            : site->GetElementsKind();
    return kind_ == kind;
  }

  void Install(Isolate* isolate, Handle<WeakCell> code) override {
    DCHECK(IsValid());
    DependentCode::InstallDependency(
        isolate, code, site_.object<AllocationSite>(),
        DependentCode::kAllocationSiteTransitionChangedGroup);
  }

 private:
  AllocationSiteRef site_;
  ElementsKind kind_;
};

MapRef CompilationDependencies::DependOnInitialMap(
    const JSFunctionRef& function) {
  MapRef map = function.initial_map();
  dependencies_.push_front(new (zone_) InitialMapDependency(function, map));
  return map;
}

void CompilationDependencies::DependOnStableMap(const MapRef& map) {
  if (map.CanTransition()) {
    dependencies_.push_front(new (zone_) StableMapDependency(map));
  } else {
    DCHECK(map.is_stable());
  }
}

void CompilationDependencies::DependOnTransition(const MapRef& target_map) {
  if (target_map.CanBeDeprecated()) {
    dependencies_.push_front(new (zone_) TransitionDependency(target_map));
  } else {
    DCHECK(!target_map.is_deprecated());
  }
}

PretenureFlag CompilationDependencies::DependOnPretenureMode(
    const AllocationSiteRef& site) {
  PretenureFlag mode = site.GetPretenureMode();
  dependencies_.push_front(new (zone_) PretenureModeDependency(site, mode));
  return mode;
}

void CompilationDependencies::DependOnFieldType(const MapRef& map,
                                                int descriptor) {
  MapRef owner = map.FindFieldOwner(descriptor);
  FieldTypeRef type = owner.GetFieldType(descriptor);
  DCHECK(type.equals(map.GetFieldType(descriptor)));
  dependencies_.push_front(new (zone_)
                               FieldTypeDependency(owner, descriptor, type));
}

void CompilationDependencies::DependOnGlobalProperty(
    const PropertyCellRef& cell) {
  PropertyCellType type = cell.property_details().cell_type();
  bool read_only = cell.property_details().IsReadOnly();
  dependencies_.push_front(new (zone_)
                               GlobalPropertyDependency(cell, type, read_only));
}

void CompilationDependencies::DependOnProtector(const PropertyCellRef& cell) {
  dependencies_.push_front(new (zone_) ProtectorDependency(cell));
}

void CompilationDependencies::DependOnElementsKind(
    const AllocationSiteRef& site) {
  // Do nothing if the object doesn't have any useful element transitions left.
  ElementsKind kind = site.PointsToLiteral()
                          ? site.boilerplate().GetElementsKind()
                          : site.GetElementsKind();
  if (AllocationSite::ShouldTrack(kind)) {
    dependencies_.push_front(new (zone_) ElementsKindDependency(site, kind));
  }
}

bool CompilationDependencies::AreValid() const {
  for (auto dep : dependencies_) {
    if (!dep->IsValid()) return false;
  }
  return true;
}

bool CompilationDependencies::Commit(Handle<Code> code) {
  Isolate* isolate = code->GetIsolate();

  // Check validity of all dependencies first, such that we can abort before
  // installing anything.
  if (!AreValid()) {
    dependencies_.clear();
    return false;
  }

  Handle<WeakCell> cell = Code::WeakCellFor(code);
  for (auto dep : dependencies_) {
    dep->Install(isolate, cell);
  }
  dependencies_.clear();
  return true;
}

namespace {
void DependOnStablePrototypeChain(const JSHeapBroker* broker,
                                  CompilationDependencies* deps,
                                  Handle<Map> map,
                                  MaybeHandle<JSReceiver> last_prototype) {
  for (PrototypeIterator i(broker->isolate(), map); !i.IsAtEnd(); i.Advance()) {
    Handle<JSReceiver> const current =
        PrototypeIterator::GetCurrent<JSReceiver>(i);
    deps->DependOnStableMap(
        MapRef(broker, handle(current->map(), broker->isolate())));
    Handle<JSReceiver> last;
    if (last_prototype.ToHandle(&last) && last.is_identical_to(current)) {
      break;
    }
  }
}
}  // namespace

void CompilationDependencies::DependOnStablePrototypeChains(
    const JSHeapBroker* broker, Handle<Context> native_context,
    std::vector<Handle<Map>> const& receiver_maps, Handle<JSObject> holder) {
  Isolate* isolate = holder->GetIsolate();
  // Determine actual holder and perform prototype chain checks.
  for (auto map : receiver_maps) {
    // Perform the implicit ToObject for primitives here.
    // Implemented according to ES6 section 7.3.2 GetV (V, P).
    Handle<JSFunction> constructor;
    if (Map::GetConstructorFunction(map, native_context)
            .ToHandle(&constructor)) {
      map = handle(constructor->initial_map(), isolate);
    }
    DependOnStablePrototypeChain(broker, this, map, holder);
  }
}

void CompilationDependencies::DependOnElementsKinds(
    const AllocationSiteRef& site) {
  AllocationSiteRef current = site;
  while (true) {
    DependOnElementsKind(current);
    if (!current.nested_site().IsAllocationSite()) break;
    current = current.nested_site().AsAllocationSite();
  }
  CHECK_EQ(current.nested_site().AsSmi(), 0);
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8