V8 Project
interface.cc
Go to the documentation of this file.
1 // Copyright 2012 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/v8.h"
6 
7 #include "src/interface.h"
8 
9 namespace v8 {
10 namespace internal {
11 
13  DCHECK(IsModule());
15  if (map == NULL) return NULL;
16  ZoneAllocationPolicy allocator(zone);
17  ZoneHashMap::Entry* p = map->Lookup(name.location(), name->Hash(), false,
18  allocator);
19  if (p == NULL) return NULL;
20  DCHECK(*static_cast<String**>(p->key) == *name);
21  DCHECK(p->value != NULL);
22  return static_cast<Interface*>(p->value);
23 }
24 
25 
26 #ifdef DEBUG
27 // Current nesting depth for debug output.
28 class Nesting {
29  public:
30  Nesting() { current_ += 2; }
31  ~Nesting() { current_ -= 2; }
32  static int current() { return current_; }
33  private:
34  static int current_;
35 };
36 
37 int Nesting::current_ = 0;
38 #endif
39 
40 
41 void Interface::DoAdd(const void* name, uint32_t hash, Interface* interface,
42  Zone* zone, bool* ok) {
43  MakeModule(ok);
44  if (!*ok) return;
45 
46 #ifdef DEBUG
47  if (FLAG_print_interface_details) {
48  PrintF("%*s# Adding...\n", Nesting::current(), "");
49  PrintF("%*sthis = ", Nesting::current(), "");
50  this->Print(Nesting::current());
51  const AstRawString* symbol = static_cast<const AstRawString*>(name);
52  PrintF("%*s%.*s : ", Nesting::current(), "", symbol->length(),
53  symbol->raw_data());
54  interface->Print(Nesting::current());
55  }
56 #endif
57 
58  ZoneHashMap** map = &Chase()->exports_;
59  ZoneAllocationPolicy allocator(zone);
60 
61  if (*map == NULL) {
62  *map = new(zone->New(sizeof(ZoneHashMap)))
65  }
66 
67  ZoneHashMap::Entry* p =
68  (*map)->Lookup(const_cast<void*>(name), hash, !IsFrozen(), allocator);
69  if (p == NULL) {
70  // This didn't have name but was frozen already, that's an error.
71  *ok = false;
72  } else if (p->value == NULL) {
73  p->value = interface;
74  } else {
75 #ifdef DEBUG
76  Nesting nested;
77 #endif
78  static_cast<Interface*>(p->value)->Unify(interface, zone, ok);
79  }
80 
81 #ifdef DEBUG
82  if (FLAG_print_interface_details) {
83  PrintF("%*sthis' = ", Nesting::current(), "");
84  this->Print(Nesting::current());
85  PrintF("%*s# Added.\n", Nesting::current(), "");
86  }
87 #endif
88 }
89 
90 
91 void Interface::Unify(Interface* that, Zone* zone, bool* ok) {
92  if (this->forward_) return this->Chase()->Unify(that, zone, ok);
93  if (that->forward_) return this->Unify(that->Chase(), zone, ok);
94  DCHECK(this->forward_ == NULL);
95  DCHECK(that->forward_ == NULL);
96 
97  *ok = true;
98  if (this == that) return;
99  if (this->IsValue()) {
100  that->MakeValue(ok);
101  if (*ok && this->IsConst()) that->MakeConst(ok);
102  return;
103  }
104  if (that->IsValue()) {
105  this->MakeValue(ok);
106  if (*ok && that->IsConst()) this->MakeConst(ok);
107  return;
108  }
109 
110 #ifdef DEBUG
111  if (FLAG_print_interface_details) {
112  PrintF("%*s# Unifying...\n", Nesting::current(), "");
113  PrintF("%*sthis = ", Nesting::current(), "");
114  this->Print(Nesting::current());
115  PrintF("%*sthat = ", Nesting::current(), "");
116  that->Print(Nesting::current());
117  }
118 #endif
119 
120  // Merge the smaller interface into the larger, for performance.
121  if (this->exports_ != NULL && (that->exports_ == NULL ||
122  this->exports_->occupancy() >= that->exports_->occupancy())) {
123  this->DoUnify(that, ok, zone);
124  } else {
125  that->DoUnify(this, ok, zone);
126  }
127 
128 #ifdef DEBUG
129  if (FLAG_print_interface_details) {
130  PrintF("%*sthis' = ", Nesting::current(), "");
131  this->Print(Nesting::current());
132  PrintF("%*sthat' = ", Nesting::current(), "");
133  that->Print(Nesting::current());
134  PrintF("%*s# Unified.\n", Nesting::current(), "");
135  }
136 #endif
137 }
138 
139 
140 void Interface::DoUnify(Interface* that, bool* ok, Zone* zone) {
141  DCHECK(this->forward_ == NULL);
142  DCHECK(that->forward_ == NULL);
143  DCHECK(!this->IsValue());
144  DCHECK(!that->IsValue());
145  DCHECK(this->index_ == -1);
146  DCHECK(that->index_ == -1);
147  DCHECK(*ok);
148 
149 #ifdef DEBUG
150  Nesting nested;
151 #endif
152 
153  // Try to merge all members from that into this.
154  ZoneHashMap* map = that->exports_;
155  if (map != NULL) {
156  for (ZoneHashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
157  this->DoAdd(p->key, p->hash, static_cast<Interface*>(p->value), zone, ok);
158  if (!*ok) return;
159  }
160  }
161 
162  // If the new interface is larger than that's, then there were members in
163  // 'this' which 'that' didn't have. If 'that' was frozen that is an error.
164  int this_size = this->exports_ == NULL ? 0 : this->exports_->occupancy();
165  int that_size = map == NULL ? 0 : map->occupancy();
166  if (that->IsFrozen() && this_size > that_size) {
167  *ok = false;
168  return;
169  }
170 
171  // Merge interfaces.
172  this->flags_ |= that->flags_;
173  that->forward_ = this;
174 }
175 
176 
177 #ifdef DEBUG
178 void Interface::Print(int n) {
179  int n0 = n > 0 ? n : 0;
180 
181  if (FLAG_print_interface_details) {
182  PrintF("%p", static_cast<void*>(this));
183  for (Interface* link = this->forward_; link != NULL; link = link->forward_)
184  PrintF("->%p", static_cast<void*>(link));
185  PrintF(" ");
186  }
187 
188  if (IsUnknown()) {
189  PrintF("unknown\n");
190  } else if (IsConst()) {
191  PrintF("const\n");
192  } else if (IsValue()) {
193  PrintF("value\n");
194  } else if (IsModule()) {
195  PrintF("module %d %s{", Index(), IsFrozen() ? "" : "(unresolved) ");
197  if (map == NULL || map->occupancy() == 0) {
198  PrintF("}\n");
199  } else if (n < 0 || n0 >= 2 * FLAG_print_interface_depth) {
200  // Avoid infinite recursion on cyclic types.
201  PrintF("...}\n");
202  } else {
203  PrintF("\n");
204  for (ZoneHashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
205  String* name = *static_cast<String**>(p->key);
206  Interface* interface = static_cast<Interface*>(p->value);
207  PrintF("%*s%s : ", n0 + 2, "", name->ToAsciiArray());
208  interface->Print(n0 + 2);
209  }
210  PrintF("%*s}\n", n0, "");
211  }
212  }
213 }
214 #endif
215 
216 } } // namespace v8::internal
const unsigned char * raw_data() const
virtual int length() const OVERRIDE
void MakeValue(bool *ok)
Definition: interface.h:73
ZoneHashMap * exports_
Definition: interface.h:191
void DoAdd(const void *name, uint32_t hash, Interface *interface, Zone *zone, bool *ok)
Definition: interface.cc:41
Interface * forward_
Definition: interface.h:190
void Unify(Interface *that, Zone *zone, bool *ok)
Definition: interface.cc:91
void MakeConst(bool *ok)
Definition: interface.h:79
void MakeModule(bool *ok)
Definition: interface.h:85
Interface * Chase()
Definition: interface.h:205
Interface * Lookup(Handle< String > name, Zone *zone)
Definition: interface.cc:12
void DoUnify(Interface *that, bool *ok, Zone *zone)
Definition: interface.cc:140
uint32_t occupancy() const
Definition: hashmap.h:61
Entry * Lookup(void *key, uint32_t hash, bool insert, AllocationPolicy allocator=AllocationPolicy())
Definition: hashmap.h:114
static bool PointersMatch(void *key1, void *key2)
Definition: hashmap.h:80
void * New(int size)
Definition: zone.cc:65
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf map
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be expose gc extension under the specified name show built in functions in stack traces use random jit cookie to mask large constants minimum length for automatic enable preparsing CPU profiler sampling interval in microseconds trace out of bounds accesses to external arrays default size of stack region v8 is allowed to maximum length of function source code printed in a stack trace min size of a semi the new space consists of two semi spaces print one trace line following each garbage collection do not print trace line after scavenger collection print cumulative GC statistics in name
enable harmony numeric enable harmony object literal extensions Optimize object Array DOM strings and string trace pretenuring decisions of HAllocate instructions Enables optimizations which favor memory size over execution speed maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining trace the tracking of allocation sites deoptimize every n garbage collections perform array bounds checks elimination analyze liveness of environment slots and zap dead values flushes the cache of optimized code for closures on every GC allow uint32 values on optimize frames if they are used only in safe operations track concurrent recompilation artificial compilation delay in ms do not emit check maps for constant values that have a leaf deoptimize the optimized code if the layout of the maps changes enable context specialization in TurboFan execution budget before interrupt is triggered max percentage of megamorphic generic ICs to allow optimization enable use of SAHF instruction if enable use of VFP3 instructions if available enable use of NEON instructions if enable use of SDIV and UDIV instructions if enable use of MLS instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of d16 d31 registers on ARM this requires VFP3 force all emitted branches to be in long enable alignment of csp to bytes on platforms which prefer the register to always be NULL
#define DCHECK(condition)
Definition: logging.h:205
TemplateHashMapImpl< ZoneAllocationPolicy > ZoneHashMap
Definition: zone.h:245
void PrintF(const char *format,...)
Definition: utils.cc:80
Debugger support for the V8 JavaScript engine.
Definition: accessors.cc:20