1 #include <msp/core/algorithm.h>
2 #include <msp/core/raii.h>
11 bool is_scalar(const BasicTypeDeclaration &type)
13 return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
16 bool is_vector_or_matrix(const BasicTypeDeclaration &type)
18 return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
21 BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &type)
23 if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
25 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
26 return (basic_base ? get_element_type(*basic_base) : 0);
32 bool can_convert(const BasicTypeDeclaration &from, const BasicTypeDeclaration &to)
34 if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
35 return from.size<=to.size;
36 else if(from.kind!=to.kind)
38 else if(from.kind==BasicTypeDeclaration::INT && from.sign!=to.sign)
39 return from.sign && from.size<=to.size;
40 else if(is_vector_or_matrix(from) && from.size==to.size)
42 BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
43 BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
44 return (from_base && to_base && can_convert(*from_base, *to_base));
51 unsigned TypeComparer::next_tag = 1;
53 void TypeComparer::compare(Node &node1, Node &node2)
65 T *TypeComparer::multi_visit(T &node)
67 static unsigned tag = next_tag++;
77 else if(!first || tag!=first_tag)
81 T *f = static_cast<T *>(first);
89 void TypeComparer::visit(Literal &literal)
91 if(Literal *lit1 = multi_visit(literal))
93 if(!lit1->type || !literal.type)
97 compare(*lit1->type, *literal.type);
99 r_result = (literal.value.check_type<int>() && lit1->value.value<int>()==literal.value.value<int>());
104 void TypeComparer::visit(VariableReference &var)
106 if(VariableReference *var1 = multi_visit(var))
108 if(!var1->declaration || !var.declaration)
110 else if(!var1->declaration->constant || !var.declaration->constant)
112 else if(!var1->declaration->init_expression || !var.declaration->init_expression)
115 compare(*var1->declaration->init_expression, *var.declaration->init_expression);
119 void TypeComparer::visit(UnaryExpression &unary)
121 if(UnaryExpression *unary1 = multi_visit(unary))
123 if(unary1->oper!=unary.oper)
126 compare(*unary1->expression, *unary.expression);
130 void TypeComparer::visit(BinaryExpression &binary)
132 if(BinaryExpression *binary1 = multi_visit(binary))
134 if(binary1->oper!=binary.oper)
138 compare(*binary1->left, *binary.left);
140 compare(*binary1->right, *binary.right);
145 void TypeComparer::visit(TernaryExpression &ternary)
147 if(TernaryExpression *ternary1 = multi_visit(ternary))
149 if(ternary1->oper!=ternary.oper)
153 compare(*ternary1->condition, *ternary.condition);
155 compare(*ternary1->true_expr, *ternary.true_expr);
157 compare(*ternary1->false_expr, *ternary.false_expr);
162 void TypeComparer::visit(BasicTypeDeclaration &basic)
164 if(BasicTypeDeclaration *basic1 = multi_visit(basic))
166 if(basic1->kind!=basic.kind || basic1->size!=basic.size || basic1->sign!=basic.sign)
168 else if(basic1->base_type && basic.base_type)
169 compare(*basic1->base_type, *basic.base_type);
171 r_result = (!basic1->base_type && !basic.base_type);
175 void TypeComparer::visit(ImageTypeDeclaration &image)
177 if(ImageTypeDeclaration *image1 = multi_visit(image))
179 if(image1->dimensions!=image.dimensions || image1->array!=image.array)
181 else if(image1->sampled!=image.sampled || image1->shadow!=image.shadow)
183 else if(image1->base_type && image.base_type)
184 compare(*image1->base_type, *image.base_type);
186 r_result = (!image1->base_type && !image.base_type);
190 void TypeComparer::visit(StructDeclaration &strct)
192 if(StructDeclaration *strct1 = multi_visit(strct))
194 if(strct1->members.body.size()!=strct.members.body.size())
199 auto i = strct1->members.body.begin();
200 auto j = strct.members.body.begin();
201 for(; (r_result && i!=strct1->members.body.end()); ++i, ++j)
207 void TypeComparer::visit(VariableDeclaration &var)
209 if(VariableDeclaration *var1 = multi_visit(var))
211 if(var1->name!=var.name || var1->array!=var.array)
213 else if(!var1->type_declaration || !var.type_declaration)
220 if(var1->array_size && var.array_size)
221 compare(*var1->array_size, *var.array_size);
222 else if(!var1->array_size && !var.array_size)
225 if(r_result && var1->type_declaration!=var.type_declaration)
226 compare(*var1->type_declaration, *var.type_declaration);
227 // TODO Compare layout qualifiers for interface block members
233 void LocationCounter::visit(BasicTypeDeclaration &basic)
235 r_count = basic.kind==BasicTypeDeclaration::MATRIX ? basic.size>>16 : 1;
238 void LocationCounter::visit(ImageTypeDeclaration &)
243 void LocationCounter::visit(StructDeclaration &strct)
246 for(const RefPtr<Statement> &s: strct.members.body)
255 void LocationCounter::visit(VariableDeclaration &var)
258 if(var.type_declaration)
259 var.type_declaration->visit(*this);
261 if(const Literal *literal = dynamic_cast<const Literal *>(var.array_size.get()))
262 if(literal->value.check_type<int>())
263 r_count *= literal->value.value<int>();
267 void MemoryRequirementsCalculator::visit(BasicTypeDeclaration &basic)
269 if(basic.kind==BasicTypeDeclaration::BOOL)
274 else if(basic.kind==BasicTypeDeclaration::INT || basic.kind==BasicTypeDeclaration::FLOAT)
276 r_size = basic.size/8;
277 r_alignment = r_size;
279 else if(basic.kind==BasicTypeDeclaration::VECTOR || basic.kind==BasicTypeDeclaration::MATRIX)
281 basic.base_type->visit(*this);
282 unsigned n_elem = basic.size&0xFFFF;
284 if(basic.kind==BasicTypeDeclaration::VECTOR)
285 r_alignment *= (n_elem==3 ? 4 : n_elem);
287 else if(basic.kind==BasicTypeDeclaration::ARRAY)
288 basic.base_type->visit(*this);
290 if(basic.extended_alignment)
291 r_alignment = (r_alignment+15)&~15U;
294 void MemoryRequirementsCalculator::visit(StructDeclaration &strct)
297 unsigned max_align = 1;
298 for(const RefPtr<Statement> &s: strct.members.body)
306 total += r_alignment-1;
307 total -= total%r_alignment;
309 max_align = max(max_align, r_alignment);
312 r_alignment = max_align;
313 if(strct.extended_alignment)
314 r_alignment = (r_alignment+15)&~15U;
315 r_size += r_alignment-1;
316 r_size -= r_size%r_alignment;
319 void MemoryRequirementsCalculator::visit(VariableDeclaration &var)
321 r_offset = get_layout_value(var.layout.get(), "offset");
323 if(var.type_declaration)
324 var.type_declaration->visit(*this);
326 if(const Literal *literal = dynamic_cast<const Literal *>(var.array_size.get()))
327 if(literal->value.check_type<int>())
329 unsigned aligned_size = r_size+r_alignment-1;
330 aligned_size -= aligned_size%r_alignment;
331 r_size = aligned_size*literal->value.value<int>();
336 set<Node *> DependencyCollector::apply(FunctionDeclaration &func)
342 void DependencyCollector::visit(VariableReference &var)
344 if(var.declaration && !locals.count(var.declaration))
346 dependencies.insert(var.declaration);
347 var.declaration->visit(*this);
351 void DependencyCollector::visit(FunctionCall &call)
355 dependencies.insert(call.declaration);
356 if(call.declaration->definition)
357 call.declaration->definition->visit(*this);
359 TraversingVisitor::visit(call);
362 void DependencyCollector::visit(VariableDeclaration &var)
365 if(var.type_declaration)
367 dependencies.insert(var.type_declaration);
368 var.type_declaration->visit(*this);
371 TraversingVisitor::visit(var);
374 void DependencyCollector::visit(FunctionDeclaration &func)
376 if(!visited_functions.count(&func))
378 visited_functions.insert(&func);
379 TraversingVisitor::visit(func);
384 set<Node *> AssignmentCollector::apply(Node &node)
387 return assigned_variables;
390 void AssignmentCollector::visit(VariableReference &var)
392 if(assignment_target)
393 assigned_variables.insert(var.declaration);
396 void AssignmentCollector::visit(UnaryExpression &unary)
398 SetFlag set_assignment(assignment_target, (unary.oper->token[1]=='+' || unary.oper->token[1]=='-'));
399 TraversingVisitor::visit(unary);
402 void AssignmentCollector::visit(BinaryExpression &binary)
404 binary.left->visit(*this);
405 SetFlag clear_assignment(assignment_target, false);
406 binary.right->visit(*this);
409 void AssignmentCollector::visit(Assignment &assign)
412 SetFlag set_assignment(assignment_target);
413 assign.left->visit(*this);
415 assign.right->visit(*this);