1 // Copyright 2013 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_TYPING_H_
6 #define V8_TYPING_H_
7 
8 #include "src/v8.h"
9 
10 #include "src/allocation.h"
11 #include "src/ast.h"
12 #include "src/compiler.h"
13 #include "src/effects.h"
14 #include "src/scopes.h"
15 #include "src/type-info.h"
16 #include "src/types.h"
17 #include "src/zone.h"
18 
19 namespace v8 {
20 namespace internal {
21 
22 
23 class AstTyper: public AstVisitor {
24  public:
25   static void Run(CompilationInfo* info);
26 
new(size_t size,Zone * zone)27   void* operator new(size_t size, Zone* zone) {
28     return zone->New(static_cast<int>(size));
29   }
delete(void * pointer,Zone * zone)30   void operator delete(void* pointer, Zone* zone) { }
delete(void * pointer)31   void operator delete(void* pointer) { }
32 
33   DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
34 
35  private:
36   explicit AstTyper(CompilationInfo* info);
37 
38   Effect ObservedOnStack(Object* value);
39   void ObserveTypesAtOsrEntry(IterationStatement* stmt);
40 
41   static const int kNoVar = INT_MIN;
42   typedef v8::internal::Effects<int, kNoVar> Effects;
43   typedef v8::internal::NestedEffects<int, kNoVar> Store;
44 
45   CompilationInfo* info_;
46   TypeFeedbackOracle oracle_;
47   Store store_;
48 
oracle()49   TypeFeedbackOracle* oracle() { return &oracle_; }
50 
NarrowType(Expression * e,Bounds b)51   void NarrowType(Expression* e, Bounds b) {
52     e->set_bounds(Bounds::Both(e->bounds(), b, zone()));
53   }
NarrowLowerType(Expression * e,Type * t)54   void NarrowLowerType(Expression* e, Type* t) {
55     e->set_bounds(Bounds::NarrowLower(e->bounds(), t, zone()));
56   }
57 
EnterEffects()58   Effects EnterEffects() {
59     store_ = store_.Push();
60     return store_.Top();
61   }
ExitEffects()62   void ExitEffects() { store_ = store_.Pop(); }
63 
parameter_index(int index)64   int parameter_index(int index) { return -index - 2; }
stack_local_index(int index)65   int stack_local_index(int index) { return index; }
66 
variable_index(Variable * var)67   int variable_index(Variable* var) {
68     // Stack locals have the range [0 .. l]
69     // Parameters have the range [-1 .. p]
70     // We map this to [-p-2 .. -1, 0 .. l]
71     return var->IsStackLocal() ? stack_local_index(var->index()) :
72            var->IsParameter() ? parameter_index(var->index()) : kNoVar;
73   }
74 
75   void VisitDeclarations(ZoneList<Declaration*>* declarations);
76   void VisitStatements(ZoneList<Statement*>* statements);
77 
78 #define DECLARE_VISIT(type) virtual void Visit##type(type* node);
79   AST_NODE_LIST(DECLARE_VISIT)
80 #undef DECLARE_VISIT
81 
82   DISALLOW_COPY_AND_ASSIGN(AstTyper);
83 };
84 
85 } }  // namespace v8::internal
86 
87 #endif  // V8_TYPING_H_
88