1 #include <msp/core/algorithm.h>
10 bool is_scalar(const BasicTypeDeclaration &type)
12 return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
15 bool is_vector_or_matrix(const BasicTypeDeclaration &type)
17 return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
20 BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &type)
22 if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
24 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
25 return (basic_base ? get_element_type(*basic_base) : 0);
31 bool can_convert(const BasicTypeDeclaration &from, const BasicTypeDeclaration &to)
33 if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
34 return from.size<=to.size;
35 else if(from.kind!=to.kind)
37 else if(from.kind==BasicTypeDeclaration::INT && from.sign!=to.sign)
38 return from.sign && from.size<=to.size;
39 else if(is_vector_or_matrix(from) && from.size==to.size)
41 BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
42 BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
43 return (from_base && to_base && can_convert(*from_base, *to_base));
50 unsigned TypeComparer::next_tag = 1;
52 TypeComparer::TypeComparer():
59 void TypeComparer::compare(Node &node1, Node &node2)
71 T *TypeComparer::multi_visit(T &node)
73 static unsigned tag = next_tag++;
83 else if(!first || tag!=first_tag)
87 T *f = static_cast<T *>(first);
95 void TypeComparer::visit(Literal &literal)
97 if(Literal *lit1 = multi_visit(literal))
99 if(!lit1->type || !literal.type)
103 compare(*lit1->type, *literal.type);
105 r_result = (literal.value.check_type<int>() && lit1->value.value<int>()==literal.value.value<int>());
110 void TypeComparer::visit(VariableReference &var)
112 if(VariableReference *var1 = multi_visit(var))
114 if(!var1->declaration || !var.declaration)
116 else if(!var1->declaration->constant || !var.declaration->constant)
118 else if(!var1->declaration->init_expression || !var.declaration->init_expression)
121 compare(*var1->declaration->init_expression, *var.declaration->init_expression);
125 void TypeComparer::visit(BasicTypeDeclaration &basic)
127 if(BasicTypeDeclaration *basic1 = multi_visit(basic))
129 if(basic1->kind!=basic.kind || basic1->size!=basic.size || basic1->sign!=basic.sign)
131 else if(basic1->base_type && basic.base_type)
132 compare(*basic1->base_type, *basic.base_type);
134 r_result = (!basic1->base_type && !basic.base_type);
138 void TypeComparer::visit(ImageTypeDeclaration &image)
140 if(ImageTypeDeclaration *image1 = multi_visit(image))
142 if(image1->dimensions!=image.dimensions || image1->array!=image.array)
144 else if(image1->sampled!=image.sampled || image1->shadow!=image.shadow)
146 else if(image1->base_type && image.base_type)
147 compare(*image1->base_type, *image.base_type);
149 r_result = (!image1->base_type && !image.base_type);
153 void TypeComparer::visit(StructDeclaration &strct)
155 if(StructDeclaration *strct1 = multi_visit(strct))
157 if(strct1->members.body.size()!=strct.members.body.size())
162 auto i = strct1->members.body.begin();
163 auto j = strct.members.body.begin();
164 for(; (r_result && i!=strct1->members.body.end()); ++i, ++j)
170 void TypeComparer::visit(VariableDeclaration &var)
172 if(VariableDeclaration *var1 = multi_visit(var))
174 if(var1->name!=var.name || var1->array!=var.array)
176 else if(!var1->type_declaration || !var.type_declaration)
183 if(var1->array_size && var.array_size)
184 compare(*var1->array_size, *var.array_size);
186 if(r_result && var1->type_declaration!=var.type_declaration)
187 compare(*var1->type_declaration, *var.type_declaration);
188 // TODO Compare layout qualifiers for interface block members
194 LocationCounter::LocationCounter():
198 void LocationCounter::visit(BasicTypeDeclaration &basic)
200 r_count = basic.kind==BasicTypeDeclaration::MATRIX ? basic.size>>16 : 1;
203 void LocationCounter::visit(ImageTypeDeclaration &)
208 void LocationCounter::visit(StructDeclaration &strct)
211 for(const RefPtr<Statement> &s: strct.members.body)
220 void LocationCounter::visit(VariableDeclaration &var)
223 if(var.type_declaration)
224 var.type_declaration->visit(*this);
226 if(const Literal *literal = dynamic_cast<const Literal *>(var.array_size.get()))
227 if(literal->value.check_type<int>())
228 r_count *= literal->value.value<int>();
232 void MemoryRequirementsCalculator::visit(BasicTypeDeclaration &basic)
234 if(basic.kind==BasicTypeDeclaration::BOOL)
239 else if(basic.kind==BasicTypeDeclaration::INT || basic.kind==BasicTypeDeclaration::FLOAT)
241 r_size = basic.size/8;
242 r_alignment = r_size;
244 else if(basic.kind==BasicTypeDeclaration::VECTOR || basic.kind==BasicTypeDeclaration::MATRIX)
246 basic.base_type->visit(*this);
247 unsigned n_elem = basic.size&0xFFFF;
249 if(basic.kind==BasicTypeDeclaration::VECTOR)
250 r_alignment *= (n_elem==3 ? 4 : n_elem);
252 else if(basic.kind==BasicTypeDeclaration::ARRAY)
253 basic.base_type->visit(*this);
256 void MemoryRequirementsCalculator::visit(StructDeclaration &strct)
259 unsigned max_align = 1;
260 for(const RefPtr<Statement> &s: strct.members.body)
268 total += r_alignment-1;
269 total -= total%r_alignment;
271 max_align = max(max_align, r_alignment);
274 r_alignment = max_align;
277 void MemoryRequirementsCalculator::visit(VariableDeclaration &var)
281 auto i = find_member(var.layout->qualifiers, string("offset"), &Layout::Qualifier::name);
282 if(i!=var.layout->qualifiers.end())
286 if(var.type_declaration)
287 var.type_declaration->visit(*this);
289 if(const Literal *literal = dynamic_cast<const Literal *>(var.array_size.get()))
290 if(literal->value.check_type<int>())
291 r_size += r_alignment*(literal->value.value<int>()-1);
295 set<Node *> DependencyCollector::apply(FunctionDeclaration &func)
301 void DependencyCollector::visit(VariableReference &var)
303 if(var.declaration && !locals.count(var.declaration))
305 dependencies.insert(var.declaration);
306 var.declaration->visit(*this);
310 void DependencyCollector::visit(InterfaceBlockReference &iface)
312 if(iface.declaration)
314 dependencies.insert(iface.declaration);
315 iface.declaration->visit(*this);
319 void DependencyCollector::visit(FunctionCall &call)
323 dependencies.insert(call.declaration);
324 if(call.declaration->definition)
325 call.declaration->definition->visit(*this);
327 TraversingVisitor::visit(call);
330 void DependencyCollector::visit(VariableDeclaration &var)
333 if(var.type_declaration)
335 dependencies.insert(var.type_declaration);
336 var.type_declaration->visit(*this);
339 TraversingVisitor::visit(var);
342 void DependencyCollector::visit(FunctionDeclaration &func)
344 if(!visited_functions.count(&func))
346 visited_functions.insert(&func);
347 TraversingVisitor::visit(func);