Home | History | Annotate | Download | only in src
      1 // Copyright 2015 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_ASM_H_
      6 #define V8_TYPING_ASM_H_
      7 
      8 #include "src/allocation.h"
      9 #include "src/ast/ast.h"
     10 #include "src/effects.h"
     11 #include "src/type-info.h"
     12 #include "src/types.h"
     13 #include "src/zone.h"
     14 
     15 namespace v8 {
     16 namespace internal {
     17 
     18 class TypeCache;
     19 
     20 class AsmTyper : public AstVisitor {
     21  public:
     22   explicit AsmTyper(Isolate* isolate, Zone* zone, Script* script,
     23                     FunctionLiteral* root);
     24   bool Validate();
     25   void set_allow_simd(bool simd);
     26   const char* error_message() { return error_message_; }
     27 
     28   enum StandardMember {
     29     kNone = 0,
     30     kStdlib,
     31     kInfinity,
     32     kNaN,
     33     kMathAcos,
     34     kMathAsin,
     35     kMathAtan,
     36     kMathCos,
     37     kMathSin,
     38     kMathTan,
     39     kMathExp,
     40     kMathLog,
     41     kMathCeil,
     42     kMathFloor,
     43     kMathSqrt,
     44     kMathAbs,
     45     kMathMin,
     46     kMathMax,
     47     kMathAtan2,
     48     kMathPow,
     49     kMathImul,
     50     kMathFround,
     51     kMathE,
     52     kMathLN10,
     53     kMathLN2,
     54     kMathLOG2E,
     55     kMathLOG10E,
     56     kMathPI,
     57     kMathSQRT1_2,
     58     kMathSQRT2,
     59   };
     60 
     61   StandardMember VariableAsStandardMember(Variable* variable);
     62 
     63   DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
     64 
     65  private:
     66   Zone* zone_;
     67   Isolate* isolate_;
     68   Script* script_;
     69   FunctionLiteral* root_;
     70   bool valid_;
     71   bool allow_simd_;
     72 
     73   struct VariableInfo : public ZoneObject {
     74     Type* type;
     75     bool is_check_function;
     76     bool is_constructor_function;
     77     StandardMember standard_member;
     78 
     79     VariableInfo()
     80         : type(NULL),
     81           is_check_function(false),
     82           is_constructor_function(false),
     83           standard_member(kNone) {}
     84     explicit VariableInfo(Type* t)
     85         : type(t),
     86           is_check_function(false),
     87           is_constructor_function(false),
     88           standard_member(kNone) {}
     89   };
     90 
     91   // Information for bi-directional typing with a cap on nesting depth.
     92   Type* expected_type_;
     93   Type* computed_type_;
     94   VariableInfo* property_info_;
     95   int intish_;  // How many ops we've gone without a x|0.
     96 
     97   Type* return_type_;  // Return type of last function.
     98   size_t array_size_;  // Array size of last ArrayLiteral.
     99 
    100   typedef ZoneMap<std::string, VariableInfo*> ObjectTypeMap;
    101   ObjectTypeMap stdlib_types_;
    102   ObjectTypeMap stdlib_heap_types_;
    103   ObjectTypeMap stdlib_math_types_;
    104 #define V(NAME, Name, name, lane_count, lane_type) \
    105   ObjectTypeMap stdlib_simd_##name##_types_;       \
    106   VariableInfo* stdlib_simd_##name##_constructor_type_;
    107   SIMD128_TYPES(V)
    108 #undef V
    109 
    110   // Map from Variable* to global/local variable Type*.
    111   ZoneHashMap global_variable_type_;
    112   ZoneHashMap local_variable_type_;
    113 
    114   bool in_function_;  // In module function?
    115   bool building_function_tables_;
    116 
    117   TypeCache const& cache_;
    118 
    119   static const int kErrorMessageLimit = 100;
    120   char error_message_[kErrorMessageLimit];
    121 
    122   static const int kMaxUncombinedAdditiveSteps = 1 << 20;
    123   static const int kMaxUncombinedMultiplicativeSteps = 1;
    124 
    125   void InitializeStdlib();
    126   void InitializeStdlibSIMD();
    127 
    128   void VisitDeclarations(ZoneList<Declaration*>* d) override;
    129   void VisitStatements(ZoneList<Statement*>* s) override;
    130 
    131   void VisitExpressionAnnotation(Expression* e, Variable* var, bool is_return);
    132   void VisitFunctionAnnotation(FunctionLiteral* f);
    133   void VisitAsmModule(FunctionLiteral* f);
    134 
    135   void VisitHeapAccess(Property* expr, bool assigning, Type* assignment_type);
    136 
    137   Expression* GetReceiverOfPropertyAccess(Expression* expr, const char* name);
    138   bool IsMathObject(Expression* expr);
    139   bool IsSIMDObject(Expression* expr);
    140   bool IsSIMDTypeObject(Expression* expr, const char* name);
    141   bool IsStdlibObject(Expression* expr);
    142 
    143   void VisitSIMDProperty(Property* expr);
    144 
    145   int ElementShiftSize(Type* type);
    146   Type* StorageType(Type* type);
    147 
    148   void SetType(Variable* variable, Type* type);
    149   Type* GetType(Variable* variable);
    150   VariableInfo* GetVariableInfo(Variable* variable, bool setting);
    151   void SetVariableInfo(Variable* variable, const VariableInfo* info);
    152 
    153   VariableInfo* LibType(ObjectTypeMap* map, Handle<String> name);
    154   void VisitLibraryAccess(ObjectTypeMap* map, Property* expr);
    155 
    156   void SetResult(Expression* expr, Type* type);
    157   void IntersectResult(Expression* expr, Type* type);
    158 
    159   void VisitWithExpectation(Expression* expr, Type* expected_type,
    160                             const char* msg);
    161 
    162   void VisitLiteral(Literal* expr, bool is_return);
    163 
    164   void VisitIntegerBitwiseOperator(BinaryOperation* expr, Type* left_expected,
    165                                    Type* right_expected, Type* result_type,
    166                                    bool conversion);
    167 
    168   Zone* zone() const { return zone_; }
    169 
    170 #define DECLARE_VISIT(type) void Visit##type(type* node) override;
    171   AST_NODE_LIST(DECLARE_VISIT)
    172 #undef DECLARE_VISIT
    173 
    174   DISALLOW_COPY_AND_ASSIGN(AsmTyper);
    175 };
    176 }  // namespace internal
    177 }  // namespace v8
    178 
    179 #endif  // V8_TYPING_ASM_H_
    180