blob: 54796ed4dd5f057d9bce583cf8ce57e95119eb8f [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
15namespace v8 {
16namespace internal {
17
18class TypeCache;
19
20class AsmTyper : public AstVisitor {
21 public:
22 explicit AsmTyper(Isolate* isolate, Zone* zone, Script* script,
23 FunctionLiteral* root);
24 bool Validate();
Ben Murdoch097c5b22016-05-18 11:27:45 +010025 void set_allow_simd(bool simd) { allow_simd_ = simd; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000026 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_;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100116 bool visiting_exports_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000117
118 TypeCache const& cache_;
119
120 static const int kErrorMessageLimit = 100;
121 char error_message_[kErrorMessageLimit];
122
123 static const int kMaxUncombinedAdditiveSteps = 1 << 20;
124 static const int kMaxUncombinedMultiplicativeSteps = 1;
125
126 void InitializeStdlib();
127 void InitializeStdlibSIMD();
128
129 void VisitDeclarations(ZoneList<Declaration*>* d) override;
130 void VisitStatements(ZoneList<Statement*>* s) override;
131
132 void VisitExpressionAnnotation(Expression* e, Variable* var, bool is_return);
133 void VisitFunctionAnnotation(FunctionLiteral* f);
134 void VisitAsmModule(FunctionLiteral* f);
135
136 void VisitHeapAccess(Property* expr, bool assigning, Type* assignment_type);
137
138 Expression* GetReceiverOfPropertyAccess(Expression* expr, const char* name);
139 bool IsMathObject(Expression* expr);
140 bool IsSIMDObject(Expression* expr);
141 bool IsSIMDTypeObject(Expression* expr, const char* name);
142 bool IsStdlibObject(Expression* expr);
143
144 void VisitSIMDProperty(Property* expr);
145
146 int ElementShiftSize(Type* type);
147 Type* StorageType(Type* type);
148
149 void SetType(Variable* variable, Type* type);
150 Type* GetType(Variable* variable);
151 VariableInfo* GetVariableInfo(Variable* variable, bool setting);
152 void SetVariableInfo(Variable* variable, const VariableInfo* info);
153
154 VariableInfo* LibType(ObjectTypeMap* map, Handle<String> name);
155 void VisitLibraryAccess(ObjectTypeMap* map, Property* expr);
156
157 void SetResult(Expression* expr, Type* type);
158 void IntersectResult(Expression* expr, Type* type);
159
160 void VisitWithExpectation(Expression* expr, Type* expected_type,
161 const char* msg);
162
163 void VisitLiteral(Literal* expr, bool is_return);
164
Ben Murdoch097c5b22016-05-18 11:27:45 +0100165 void VisitVariableProxy(VariableProxy* expr, bool assignment);
166
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000167 void VisitIntegerBitwiseOperator(BinaryOperation* expr, Type* left_expected,
168 Type* right_expected, Type* result_type,
169 bool conversion);
170
171 Zone* zone() const { return zone_; }
172
173#define DECLARE_VISIT(type) void Visit##type(type* node) override;
174 AST_NODE_LIST(DECLARE_VISIT)
175#undef DECLARE_VISIT
176
177 DISALLOW_COPY_AND_ASSIGN(AsmTyper);
178};
179} // namespace internal
180} // namespace v8
181
182#endif // V8_TYPING_ASM_H_