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#ifndef V8_TYPE_INFO_H_
6#define V8_TYPE_INFO_H_
7
8#include "src/allocation.h"
9#include "src/contexts.h"
10#include "src/globals.h"
11#include "src/parsing/token.h"
12#include "src/types.h"
13#include "src/zone.h"
14
15namespace v8 {
16namespace internal {
17
18// Forward declarations.
19class SmallMapList;
20class FeedbackNexus;
21
22class TypeFeedbackOracle: public ZoneObject {
23 public:
24  TypeFeedbackOracle(Isolate* isolate, Zone* zone, Handle<Code> code,
25                     Handle<TypeFeedbackVector> feedback_vector,
26                     Handle<Context> native_context);
27
28  InlineCacheState LoadInlineCacheState(FeedbackVectorSlot slot);
29  bool StoreIsUninitialized(FeedbackVectorSlot slot);
30  bool CallIsUninitialized(FeedbackVectorSlot slot);
31  bool CallIsMonomorphic(FeedbackVectorSlot slot);
32  bool CallNewIsMonomorphic(FeedbackVectorSlot slot);
33
34  // TODO(1571) We can't use ForInStatement::ForInType as the return value due
35  // to various cycles in our headers.
36  // TODO(rossberg): once all oracle access is removed from ast.cc, it should
37  // be possible.
38  byte ForInType(FeedbackVectorSlot feedback_vector_slot);
39
40  void GetStoreModeAndKeyType(FeedbackVectorSlot slot,
41                              KeyedAccessStoreMode* store_mode,
42                              IcCheckType* key_type);
43
44  void PropertyReceiverTypes(FeedbackVectorSlot slot, Handle<Name> name,
45                             SmallMapList* receiver_types);
46  void KeyedPropertyReceiverTypes(FeedbackVectorSlot slot,
47                                  SmallMapList* receiver_types, bool* is_string,
48                                  IcCheckType* key_type);
49  void AssignmentReceiverTypes(FeedbackVectorSlot slot, Handle<Name> name,
50                               SmallMapList* receiver_types);
51  void KeyedAssignmentReceiverTypes(FeedbackVectorSlot slot,
52                                    SmallMapList* receiver_types,
53                                    KeyedAccessStoreMode* store_mode,
54                                    IcCheckType* key_type);
55  void CountReceiverTypes(FeedbackVectorSlot slot,
56                          SmallMapList* receiver_types);
57
58  void CollectReceiverTypes(FeedbackVectorSlot slot, SmallMapList* types);
59  void CollectReceiverTypes(FeedbackNexus* nexus, SmallMapList* types);
60
61  static bool IsRelevantFeedback(Map* map, Context* native_context) {
62    Object* constructor = map->GetConstructor();
63    return !constructor->IsJSFunction() ||
64           JSFunction::cast(constructor)->context()->native_context() ==
65               native_context;
66  }
67
68  Handle<JSFunction> GetCallTarget(FeedbackVectorSlot slot);
69  Handle<AllocationSite> GetCallAllocationSite(FeedbackVectorSlot slot);
70  Handle<JSFunction> GetCallNewTarget(FeedbackVectorSlot slot);
71  Handle<AllocationSite> GetCallNewAllocationSite(FeedbackVectorSlot slot);
72
73  // TODO(1571) We can't use ToBooleanICStub::Types as the return value because
74  // of various cycles in our headers. Death to tons of implementations in
75  // headers!! :-P
76  uint16_t ToBooleanTypes(TypeFeedbackId id);
77
78  // Get type information for arithmetic operations and compares.
79  void BinaryType(TypeFeedbackId id,
80                  Type** left,
81                  Type** right,
82                  Type** result,
83                  Maybe<int>* fixed_right_arg,
84                  Handle<AllocationSite>* allocation_site,
85                  Token::Value operation);
86
87  void CompareType(TypeFeedbackId id,
88                   Type** left,
89                   Type** right,
90                   Type** combined);
91
92  Type* CountType(TypeFeedbackId id);
93
94  Zone* zone() const { return zone_; }
95  Isolate* isolate() const { return isolate_; }
96
97 private:
98  void CollectReceiverTypes(FeedbackVectorSlot slot, Handle<Name> name,
99                            Code::Flags flags, SmallMapList* types);
100  void CollectReceiverTypes(FeedbackNexus* nexus, Handle<Name> name,
101                            Code::Flags flags, SmallMapList* types);
102
103  // Returns true if there is at least one string map and if
104  // all maps are string maps.
105  bool HasOnlyStringMaps(SmallMapList* receiver_types);
106
107  void SetInfo(TypeFeedbackId id, Object* target);
108
109  void BuildDictionary(Handle<Code> code);
110  void GetRelocInfos(Handle<Code> code, ZoneList<RelocInfo>* infos);
111  void CreateDictionary(Handle<Code> code, ZoneList<RelocInfo>* infos);
112  void RelocateRelocInfos(ZoneList<RelocInfo>* infos,
113                          Code* old_code,
114                          Code* new_code);
115  void ProcessRelocInfos(ZoneList<RelocInfo>* infos);
116
117  // Returns an element from the backing store. Returns undefined if
118  // there is no information.
119  Handle<Object> GetInfo(TypeFeedbackId id);
120
121  // Returns an element from the type feedback vector. Returns undefined
122  // if there is no information.
123  Handle<Object> GetInfo(FeedbackVectorSlot slot);
124
125 private:
126  Handle<Context> native_context_;
127  Isolate* isolate_;
128  Zone* zone_;
129  Handle<UnseededNumberDictionary> dictionary_;
130  Handle<TypeFeedbackVector> feedback_vector_;
131
132  DISALLOW_COPY_AND_ASSIGN(TypeFeedbackOracle);
133};
134
135}  // namespace internal
136}  // namespace v8
137
138#endif  // V8_TYPE_INFO_H_
139