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