1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // CodeGenerator.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Mon May 18 07:44:20 2015 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Thr May 2 10:47:00 2019 |
---|
13 | // Update Count : 497 |
---|
14 | // |
---|
15 | #include "CodeGenerator.h" |
---|
16 | |
---|
17 | #include <cassert> // for assert, assertf |
---|
18 | #include <list> // for _List_iterator, list, list<>::it... |
---|
19 | |
---|
20 | #include "Common/UniqueName.h" // for UniqueName |
---|
21 | #include "Common/utility.h" // for CodeLocation, toString |
---|
22 | #include "GenType.h" // for genType |
---|
23 | #include "InitTweak/InitTweak.h" // for getPointerBase |
---|
24 | #include "OperatorTable.h" // for OperatorInfo, operatorLookup |
---|
25 | #include "Parser/LinkageSpec.h" // for Spec, Intrinsic |
---|
26 | #include "SynTree/Attribute.h" // for Attribute |
---|
27 | #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode |
---|
28 | #include "SynTree/Constant.h" // for Constant |
---|
29 | #include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl |
---|
30 | #include "SynTree/Expression.h" // for Expression, UntypedExpr, Applica... |
---|
31 | #include "SynTree/Initializer.h" // for Initializer, ListInit, Designation |
---|
32 | #include "SynTree/Label.h" // for Label, operator<< |
---|
33 | #include "SynTree/Statement.h" // for Statement, AsmStmt, BranchStmt |
---|
34 | #include "SynTree/Type.h" // for Type, Type::StorageClasses, Func... |
---|
35 | |
---|
36 | using namespace std; |
---|
37 | |
---|
38 | namespace CodeGen { |
---|
39 | int CodeGenerator::tabsize = 4; |
---|
40 | |
---|
41 | // the kinds of statements that would ideally be followed by whitespace |
---|
42 | bool wantSpacing( Statement * stmt) { |
---|
43 | return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) || |
---|
44 | dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt ); |
---|
45 | } |
---|
46 | |
---|
47 | void CodeGenerator::extension( Expression * expr ) { |
---|
48 | if ( expr->get_extension() ) { |
---|
49 | output << "__extension__ "; |
---|
50 | } // if |
---|
51 | } // extension |
---|
52 | |
---|
53 | void CodeGenerator::extension( Declaration * decl ) { |
---|
54 | if ( decl->get_extension() ) { |
---|
55 | output << "__extension__ "; |
---|
56 | } // if |
---|
57 | } // extension |
---|
58 | |
---|
59 | void CodeGenerator::asmName( DeclarationWithType * decl ) { |
---|
60 | if ( ConstantExpr * asmName = dynamic_cast<ConstantExpr *>(decl->get_asmName()) ) { |
---|
61 | output << " asm ( " << asmName->get_constant()->get_value() << " )"; |
---|
62 | } // if |
---|
63 | } // extension |
---|
64 | |
---|
65 | CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) { |
---|
66 | labels = &l; |
---|
67 | return *this; |
---|
68 | } |
---|
69 | |
---|
70 | ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) { |
---|
71 | std::list< Label > & labs = *printLabels.labels; |
---|
72 | // l.unique(); // assumes a sorted list. Why not use set? Does order matter? |
---|
73 | for ( Label & l : labs ) { |
---|
74 | output << l.get_name() + ": "; |
---|
75 | printLabels.cg.genAttributes( l.get_attributes() ); |
---|
76 | } // for |
---|
77 | return output; |
---|
78 | } |
---|
79 | |
---|
80 | /* Using updateLocation at the beginning of a node and endl |
---|
81 | * within a node should become the method of formating. |
---|
82 | */ |
---|
83 | void CodeGenerator::updateLocation( CodeLocation const & to ) { |
---|
84 | // skip if linemarks shouldn't appear or if codelocation is unset |
---|
85 | if ( !options.lineMarks || to.isUnset() ) return; |
---|
86 | |
---|
87 | if ( currentLocation.followedBy( to, 0 ) ) { |
---|
88 | return; |
---|
89 | } else if ( currentLocation.followedBy( to, 1 ) ) { |
---|
90 | output << "\n" << indent; |
---|
91 | currentLocation.first_line += 1; |
---|
92 | } else if ( currentLocation.followedBy( to, 2 ) ) { |
---|
93 | output << "\n\n" << indent; |
---|
94 | currentLocation.first_line += 2; |
---|
95 | } else { |
---|
96 | output << "\n# " << to.first_line << " \"" << to.filename |
---|
97 | << "\"\n" << indent; |
---|
98 | currentLocation = to; |
---|
99 | } |
---|
100 | output << std::flush; |
---|
101 | } |
---|
102 | |
---|
103 | void CodeGenerator::updateLocation( BaseSyntaxNode const * to ) { |
---|
104 | updateLocation( to->location ); |
---|
105 | } |
---|
106 | |
---|
107 | // replace endl |
---|
108 | ostream & CodeGenerator::LineEnder::operator()( ostream & os ) const { |
---|
109 | // if ( !cg.lineMarks ) { |
---|
110 | // os << "\n" << cg.indent << std::flush; |
---|
111 | // } |
---|
112 | os << "\n" << std::flush; |
---|
113 | cg.currentLocation.first_line++; |
---|
114 | // os << "/* did endl; current loc is: " << cg.currentLocation.first_line << "*/"; |
---|
115 | return os; |
---|
116 | } |
---|
117 | |
---|
118 | CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks, bool printExprTypes ) : indent( CodeGenerator::tabsize ), output( os ), printLabels( *this ), options( pretty, genC, lineMarks, printExprTypes ), endl( *this ) {} |
---|
119 | CodeGenerator::CodeGenerator( std::ostream & os, const Options &options ) : indent( CodeGenerator::tabsize ), output( os ), printLabels( *this ), options(options), endl( *this ) {} |
---|
120 | |
---|
121 | string CodeGenerator::mangleName( DeclarationWithType * decl ) { |
---|
122 | // GCC builtins should always be printed unmangled |
---|
123 | if ( options.pretty || decl->linkage.is_gcc_builtin ) return decl->name; |
---|
124 | if ( decl->mangleName != "" ) { |
---|
125 | // need to incorporate scope level in order to differentiate names for destructors |
---|
126 | return decl->get_scopedMangleName(); |
---|
127 | } else { |
---|
128 | return decl->name; |
---|
129 | } // if |
---|
130 | } |
---|
131 | |
---|
132 | void CodeGenerator::genAttributes( list< Attribute * > & attributes ) { |
---|
133 | if ( attributes.empty() ) return; |
---|
134 | output << "__attribute__ (("; |
---|
135 | for ( list< Attribute * >::iterator attr( attributes.begin() );; ) { |
---|
136 | output << (*attr)->name; |
---|
137 | if ( ! (*attr)->parameters.empty() ) { |
---|
138 | output << "("; |
---|
139 | genCommaList( (*attr)->parameters.begin(), (*attr)->parameters.end() ); |
---|
140 | output << ")"; |
---|
141 | } // if |
---|
142 | if ( ++attr == attributes.end() ) break; |
---|
143 | output << ","; // separator |
---|
144 | } // for |
---|
145 | output << ")) "; |
---|
146 | } // CodeGenerator::genAttributes |
---|
147 | |
---|
148 | // *** BaseSyntaxNode |
---|
149 | void CodeGenerator::previsit( BaseSyntaxNode * node ) { |
---|
150 | // turn off automatic recursion for all nodes, to allow each visitor to |
---|
151 | // precisely control the order in which its children are visited. |
---|
152 | visit_children = false; |
---|
153 | updateLocation( node ); |
---|
154 | } |
---|
155 | |
---|
156 | // *** BaseSyntaxNode |
---|
157 | void CodeGenerator::postvisit( BaseSyntaxNode * node ) { |
---|
158 | std::stringstream ss; |
---|
159 | node->print( ss ); |
---|
160 | assertf( false, "Unhandled node reached in CodeGenerator: %s", ss.str().c_str() ); |
---|
161 | } |
---|
162 | |
---|
163 | // *** Expression |
---|
164 | void CodeGenerator::previsit( Expression * node ) { |
---|
165 | previsit( (BaseSyntaxNode *)node ); |
---|
166 | GuardAction( [this, node](){ |
---|
167 | if ( options.printExprTypes && node->result ) { |
---|
168 | output << " /* " << genType( node->result, "", options ) << " */ "; |
---|
169 | } |
---|
170 | } ); |
---|
171 | } |
---|
172 | |
---|
173 | // *** Declarations |
---|
174 | void CodeGenerator::postvisit( FunctionDecl * functionDecl ) { |
---|
175 | // deleted decls should never be used, so don't print them |
---|
176 | if ( functionDecl->isDeleted && options.genC ) return; |
---|
177 | extension( functionDecl ); |
---|
178 | genAttributes( functionDecl->get_attributes() ); |
---|
179 | |
---|
180 | handleStorageClass( functionDecl ); |
---|
181 | functionDecl->get_funcSpec().print( output ); |
---|
182 | |
---|
183 | Options subOptions = options; |
---|
184 | subOptions.anonymousUnused = functionDecl->has_body(); |
---|
185 | output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), subOptions ); |
---|
186 | |
---|
187 | asmName( functionDecl ); |
---|
188 | |
---|
189 | if ( functionDecl->get_statements() ) { |
---|
190 | functionDecl->get_statements()->accept( *visitor ); |
---|
191 | } // if |
---|
192 | if ( functionDecl->isDeleted ) { |
---|
193 | output << " = void"; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | void CodeGenerator::postvisit( ObjectDecl * objectDecl ) { |
---|
198 | // deleted decls should never be used, so don't print them |
---|
199 | if ( objectDecl->isDeleted && options.genC ) return; |
---|
200 | if (objectDecl->get_name().empty() && options.genC ) { |
---|
201 | // only generate an anonymous name when generating C code, otherwise it clutters the output too much |
---|
202 | static UniqueName name = { "__anonymous_object" }; |
---|
203 | objectDecl->set_name( name.newName() ); |
---|
204 | // Stops unused parameter warnings. |
---|
205 | if ( options.anonymousUnused ) { |
---|
206 | objectDecl->attributes.push_back( new Attribute( "unused" ) ); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | extension( objectDecl ); |
---|
211 | genAttributes( objectDecl->get_attributes() ); |
---|
212 | |
---|
213 | handleStorageClass( objectDecl ); |
---|
214 | output << genType( objectDecl->get_type(), mangleName( objectDecl ), options.pretty, options.genC ); |
---|
215 | |
---|
216 | asmName( objectDecl ); |
---|
217 | |
---|
218 | if ( objectDecl->get_init() ) { |
---|
219 | output << " = "; |
---|
220 | objectDecl->get_init()->accept( *visitor ); |
---|
221 | } // if |
---|
222 | if ( objectDecl->isDeleted ) { |
---|
223 | output << " = void"; |
---|
224 | } |
---|
225 | |
---|
226 | if ( objectDecl->get_bitfieldWidth() ) { |
---|
227 | output << ":"; |
---|
228 | objectDecl->get_bitfieldWidth()->accept( *visitor ); |
---|
229 | } // if |
---|
230 | } |
---|
231 | |
---|
232 | void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) { |
---|
233 | if( ! aggDecl->parameters.empty() && ! options.genC ) { |
---|
234 | // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); |
---|
235 | output << "forall("; |
---|
236 | genCommaList( aggDecl->parameters.begin(), aggDecl->parameters.end() ); |
---|
237 | output << ")" << endl; |
---|
238 | output << indent; |
---|
239 | } |
---|
240 | |
---|
241 | output << kind; |
---|
242 | genAttributes( aggDecl->attributes ); |
---|
243 | output << aggDecl->name; |
---|
244 | |
---|
245 | if ( aggDecl->has_body() ) { |
---|
246 | std::list< Declaration * > & memb = aggDecl->members; |
---|
247 | output << " {" << endl; |
---|
248 | |
---|
249 | ++indent; |
---|
250 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { |
---|
251 | output << indent; |
---|
252 | (*i)->accept( *visitor ); |
---|
253 | output << ";" << endl; |
---|
254 | } // for |
---|
255 | |
---|
256 | --indent; |
---|
257 | |
---|
258 | output << indent << "}"; |
---|
259 | } // if |
---|
260 | } |
---|
261 | |
---|
262 | void CodeGenerator::postvisit( StructDecl * structDecl ) { |
---|
263 | extension( structDecl ); |
---|
264 | handleAggregate( structDecl, "struct " ); |
---|
265 | } |
---|
266 | |
---|
267 | void CodeGenerator::postvisit( UnionDecl * unionDecl ) { |
---|
268 | extension( unionDecl ); |
---|
269 | handleAggregate( unionDecl, "union " ); |
---|
270 | } |
---|
271 | |
---|
272 | void CodeGenerator::postvisit( EnumDecl * enumDecl ) { |
---|
273 | extension( enumDecl ); |
---|
274 | output << "enum "; |
---|
275 | genAttributes( enumDecl->get_attributes() ); |
---|
276 | |
---|
277 | output << enumDecl->get_name(); |
---|
278 | |
---|
279 | std::list< Declaration* > &memb = enumDecl->get_members(); |
---|
280 | |
---|
281 | if ( ! memb.empty() ) { |
---|
282 | output << " {" << endl; |
---|
283 | |
---|
284 | ++indent; |
---|
285 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { |
---|
286 | ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); |
---|
287 | assert( obj ); |
---|
288 | output << indent << mangleName( obj ); |
---|
289 | if ( obj->get_init() ) { |
---|
290 | output << " = "; |
---|
291 | obj->get_init()->accept( *visitor ); |
---|
292 | } // if |
---|
293 | output << "," << endl; |
---|
294 | } // for |
---|
295 | |
---|
296 | --indent; |
---|
297 | |
---|
298 | output << indent << "}"; |
---|
299 | } // if |
---|
300 | } |
---|
301 | |
---|
302 | void CodeGenerator::postvisit( TraitDecl * traitDecl ) { |
---|
303 | assertf( ! options.genC, "TraitDecls should not reach code generation." ); |
---|
304 | extension( traitDecl ); |
---|
305 | handleAggregate( traitDecl, "trait " ); |
---|
306 | } |
---|
307 | |
---|
308 | void CodeGenerator::postvisit( TypedefDecl * typeDecl ) { |
---|
309 | assertf( ! options.genC, "Typedefs are removed and substituted in earlier passes." ); |
---|
310 | output << "typedef "; |
---|
311 | output << genType( typeDecl->get_base(), typeDecl->get_name(), options ) << endl; |
---|
312 | } |
---|
313 | |
---|
314 | void CodeGenerator::postvisit( TypeDecl * typeDecl ) { |
---|
315 | assertf( ! options.genC, "TypeDecls should not reach code generation." ); |
---|
316 | output << typeDecl->genTypeString() << " " << typeDecl->name; |
---|
317 | if ( typeDecl->sized ) { |
---|
318 | output << " | sized(" << typeDecl->name << ")"; |
---|
319 | } |
---|
320 | if ( ! typeDecl->assertions.empty() ) { |
---|
321 | output << " | { "; |
---|
322 | for ( DeclarationWithType * assert : typeDecl->assertions ) { |
---|
323 | assert->accept( *visitor ); |
---|
324 | output << "; "; |
---|
325 | } |
---|
326 | output << " }"; |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | void CodeGenerator::postvisit( StaticAssertDecl * assertDecl ) { |
---|
331 | output << "_Static_assert("; |
---|
332 | assertDecl->condition->accept( *visitor ); |
---|
333 | output << ", "; |
---|
334 | assertDecl->message->accept( *visitor ); |
---|
335 | output << ")"; |
---|
336 | } |
---|
337 | |
---|
338 | void CodeGenerator::postvisit( Designation * designation ) { |
---|
339 | std::list< Expression * > designators = designation->get_designators(); |
---|
340 | if ( designators.size() == 0 ) return; |
---|
341 | for ( Expression * des : designators ) { |
---|
342 | if ( dynamic_cast< NameExpr * >( des ) || dynamic_cast< VariableExpr * >( des ) ) { |
---|
343 | // if expression is a NameExpr or VariableExpr, then initializing aggregate member |
---|
344 | output << "."; |
---|
345 | des->accept( *visitor ); |
---|
346 | } else { |
---|
347 | // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt |
---|
348 | output << "["; |
---|
349 | des->accept( *visitor ); |
---|
350 | output << "]"; |
---|
351 | } // if |
---|
352 | } // for |
---|
353 | output << " = "; |
---|
354 | } |
---|
355 | |
---|
356 | void CodeGenerator::postvisit( SingleInit * init ) { |
---|
357 | init->get_value()->accept( *visitor ); |
---|
358 | } |
---|
359 | |
---|
360 | void CodeGenerator::postvisit( ListInit * init ) { |
---|
361 | auto initBegin = init->begin(); |
---|
362 | auto initEnd = init->end(); |
---|
363 | auto desigBegin = init->get_designations().begin(); |
---|
364 | auto desigEnd = init->get_designations().end(); |
---|
365 | |
---|
366 | output << "{ "; |
---|
367 | for ( ; initBegin != initEnd && desigBegin != desigEnd; ) { |
---|
368 | (*desigBegin)->accept( *visitor ); |
---|
369 | (*initBegin)->accept( *visitor ); |
---|
370 | ++initBegin, ++desigBegin; |
---|
371 | if ( initBegin != initEnd ) { |
---|
372 | output << ", "; |
---|
373 | } |
---|
374 | } |
---|
375 | output << " }"; |
---|
376 | assertf( initBegin == initEnd && desigBegin == desigEnd, "Initializers and designators not the same length. %s", toString( init ).c_str() ); |
---|
377 | } |
---|
378 | |
---|
379 | void CodeGenerator::postvisit( ConstructorInit * init ){ |
---|
380 | assertf( ! options.genC, "ConstructorInit nodes should not reach code generation." ); |
---|
381 | // pseudo-output for constructor/destructor pairs |
---|
382 | output << "<ctorinit>{" << endl << ++indent << "ctor: "; |
---|
383 | maybeAccept( init->get_ctor(), *visitor ); |
---|
384 | output << ", " << endl << indent << "dtor: "; |
---|
385 | maybeAccept( init->get_dtor(), *visitor ); |
---|
386 | output << endl << --indent << "}"; |
---|
387 | } |
---|
388 | |
---|
389 | void CodeGenerator::postvisit( Constant * constant ) { |
---|
390 | output << constant->get_value(); |
---|
391 | } |
---|
392 | |
---|
393 | // *** Expressions |
---|
394 | void CodeGenerator::postvisit( ApplicationExpr * applicationExpr ) { |
---|
395 | extension( applicationExpr ); |
---|
396 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { |
---|
397 | OperatorInfo opInfo; |
---|
398 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { |
---|
399 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin(); |
---|
400 | switch ( opInfo.type ) { |
---|
401 | case OT_INDEX: |
---|
402 | assert( applicationExpr->get_args().size() == 2 ); |
---|
403 | (*arg++)->accept( *visitor ); |
---|
404 | output << "["; |
---|
405 | (*arg)->accept( *visitor ); |
---|
406 | output << "]"; |
---|
407 | break; |
---|
408 | |
---|
409 | case OT_CALL: |
---|
410 | // there are no intrinsic definitions of the function call operator |
---|
411 | assert( false ); |
---|
412 | break; |
---|
413 | |
---|
414 | case OT_CTOR: |
---|
415 | case OT_DTOR: |
---|
416 | if ( applicationExpr->get_args().size() == 1 ) { |
---|
417 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
418 | // effects, so must still output this expression |
---|
419 | output << "("; |
---|
420 | (*arg++)->accept( *visitor ); |
---|
421 | output << ") /* " << opInfo.inputName << " */"; |
---|
422 | } else if ( applicationExpr->get_args().size() == 2 ) { |
---|
423 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
424 | output << "("; |
---|
425 | (*arg++)->accept( *visitor ); |
---|
426 | output << opInfo.symbol; |
---|
427 | (*arg)->accept( *visitor ); |
---|
428 | output << ") /* " << opInfo.inputName << " */"; |
---|
429 | } else { |
---|
430 | // no constructors with 0 or more than 2 parameters |
---|
431 | assert( false ); |
---|
432 | } // if |
---|
433 | break; |
---|
434 | |
---|
435 | case OT_PREFIX: |
---|
436 | case OT_PREFIXASSIGN: |
---|
437 | assert( applicationExpr->get_args().size() == 1 ); |
---|
438 | output << "("; |
---|
439 | output << opInfo.symbol; |
---|
440 | (*arg)->accept( *visitor ); |
---|
441 | output << ")"; |
---|
442 | break; |
---|
443 | |
---|
444 | case OT_POSTFIX: |
---|
445 | case OT_POSTFIXASSIGN: |
---|
446 | assert( applicationExpr->get_args().size() == 1 ); |
---|
447 | (*arg)->accept( *visitor ); |
---|
448 | output << opInfo.symbol; |
---|
449 | break; |
---|
450 | |
---|
451 | |
---|
452 | case OT_INFIX: |
---|
453 | case OT_INFIXASSIGN: |
---|
454 | assert( applicationExpr->get_args().size() == 2 ); |
---|
455 | output << "("; |
---|
456 | (*arg++)->accept( *visitor ); |
---|
457 | output << opInfo.symbol; |
---|
458 | (*arg)->accept( *visitor ); |
---|
459 | output << ")"; |
---|
460 | break; |
---|
461 | |
---|
462 | case OT_CONSTANT: |
---|
463 | case OT_LABELADDRESS: |
---|
464 | // there are no intrinsic definitions of 0/1 or label addresses as functions |
---|
465 | assert( false ); |
---|
466 | } // switch |
---|
467 | } else { |
---|
468 | varExpr->accept( *visitor ); |
---|
469 | output << "("; |
---|
470 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
471 | output << ")"; |
---|
472 | } // if |
---|
473 | } else { |
---|
474 | applicationExpr->get_function()->accept( *visitor ); |
---|
475 | output << "("; |
---|
476 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
477 | output << ")"; |
---|
478 | } // if |
---|
479 | } |
---|
480 | |
---|
481 | void CodeGenerator::postvisit( UntypedExpr * untypedExpr ) { |
---|
482 | extension( untypedExpr ); |
---|
483 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->function ) ) { |
---|
484 | OperatorInfo opInfo; |
---|
485 | if ( operatorLookup( nameExpr->name, opInfo ) ) { |
---|
486 | std::list< Expression* >::iterator arg = untypedExpr->args.begin(); |
---|
487 | switch ( opInfo.type ) { |
---|
488 | case OT_INDEX: |
---|
489 | assert( untypedExpr->args.size() == 2 ); |
---|
490 | (*arg++)->accept( *visitor ); |
---|
491 | output << "["; |
---|
492 | (*arg)->accept( *visitor ); |
---|
493 | output << "]"; |
---|
494 | break; |
---|
495 | |
---|
496 | case OT_CALL: |
---|
497 | assert( false ); |
---|
498 | |
---|
499 | case OT_CTOR: |
---|
500 | case OT_DTOR: |
---|
501 | if ( untypedExpr->args.size() == 1 ) { |
---|
502 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
503 | // effects, so must still output this expression |
---|
504 | output << "("; |
---|
505 | (*arg++)->accept( *visitor ); |
---|
506 | output << ") /* " << opInfo.inputName << " */"; |
---|
507 | } else if ( untypedExpr->get_args().size() == 2 ) { |
---|
508 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
509 | output << "("; |
---|
510 | (*arg++)->accept( *visitor ); |
---|
511 | output << opInfo.symbol; |
---|
512 | (*arg)->accept( *visitor ); |
---|
513 | output << ") /* " << opInfo.inputName << " */"; |
---|
514 | } else { |
---|
515 | // no constructors with 0 or more than 2 parameters |
---|
516 | assertf( ! options.genC, "UntypedExpr constructor/destructor with 0 or more than 2 parameters." ); |
---|
517 | output << "("; |
---|
518 | (*arg++)->accept( *visitor ); |
---|
519 | output << opInfo.symbol << "{ "; |
---|
520 | genCommaList( arg, untypedExpr->args.end() ); |
---|
521 | output << "}) /* " << opInfo.inputName << " */"; |
---|
522 | } // if |
---|
523 | break; |
---|
524 | |
---|
525 | case OT_PREFIX: |
---|
526 | case OT_PREFIXASSIGN: |
---|
527 | case OT_LABELADDRESS: |
---|
528 | assert( untypedExpr->args.size() == 1 ); |
---|
529 | output << "("; |
---|
530 | output << opInfo.symbol; |
---|
531 | (*arg)->accept( *visitor ); |
---|
532 | output << ")"; |
---|
533 | break; |
---|
534 | |
---|
535 | case OT_POSTFIX: |
---|
536 | case OT_POSTFIXASSIGN: |
---|
537 | assert( untypedExpr->args.size() == 1 ); |
---|
538 | (*arg)->accept( *visitor ); |
---|
539 | output << opInfo.symbol; |
---|
540 | break; |
---|
541 | |
---|
542 | case OT_INFIX: |
---|
543 | case OT_INFIXASSIGN: |
---|
544 | assert( untypedExpr->args.size() == 2 ); |
---|
545 | output << "("; |
---|
546 | (*arg++)->accept( *visitor ); |
---|
547 | output << opInfo.symbol; |
---|
548 | (*arg)->accept( *visitor ); |
---|
549 | output << ")"; |
---|
550 | break; |
---|
551 | |
---|
552 | case OT_CONSTANT: |
---|
553 | // there are no intrinsic definitions of 0 or 1 as functions |
---|
554 | assert( false ); |
---|
555 | } // switch |
---|
556 | } else { |
---|
557 | // builtin routines |
---|
558 | nameExpr->accept( *visitor ); |
---|
559 | output << "("; |
---|
560 | genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() ); |
---|
561 | output << ")"; |
---|
562 | } // if |
---|
563 | } else { |
---|
564 | untypedExpr->function->accept( *visitor ); |
---|
565 | output << "("; |
---|
566 | genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() ); |
---|
567 | output << ")"; |
---|
568 | } // if |
---|
569 | } |
---|
570 | |
---|
571 | void CodeGenerator::postvisit( RangeExpr * rangeExpr ) { |
---|
572 | rangeExpr->low->accept( *visitor ); |
---|
573 | output << " ... "; |
---|
574 | rangeExpr->high->accept( *visitor ); |
---|
575 | } |
---|
576 | |
---|
577 | void CodeGenerator::postvisit( NameExpr * nameExpr ) { |
---|
578 | extension( nameExpr ); |
---|
579 | OperatorInfo opInfo; |
---|
580 | if ( operatorLookup( nameExpr->name, opInfo ) ) { |
---|
581 | if ( opInfo.type == OT_CONSTANT ) { |
---|
582 | output << opInfo.symbol; |
---|
583 | } else { |
---|
584 | output << opInfo.outputName; |
---|
585 | } |
---|
586 | } else { |
---|
587 | output << nameExpr->get_name(); |
---|
588 | } // if |
---|
589 | } |
---|
590 | |
---|
591 | void CodeGenerator::postvisit( AddressExpr * addressExpr ) { |
---|
592 | extension( addressExpr ); |
---|
593 | output << "(&"; |
---|
594 | addressExpr->arg->accept( *visitor ); |
---|
595 | output << ")"; |
---|
596 | } |
---|
597 | |
---|
598 | void CodeGenerator::postvisit( LabelAddressExpr *addressExpr ) { |
---|
599 | extension( addressExpr ); |
---|
600 | output << "(&&" << addressExpr->arg << ")"; |
---|
601 | } |
---|
602 | |
---|
603 | void CodeGenerator::postvisit( CastExpr * castExpr ) { |
---|
604 | extension( castExpr ); |
---|
605 | output << "("; |
---|
606 | if ( castExpr->get_result()->isVoid() ) { |
---|
607 | output << "(void)"; |
---|
608 | } else { |
---|
609 | // at least one result type of cast. |
---|
610 | // Note: previously, lvalue casts were skipped. Since it's now impossible for the user to write |
---|
611 | // an lvalue cast, this has been taken out. |
---|
612 | output << "("; |
---|
613 | output << genType( castExpr->get_result(), "", options ); |
---|
614 | output << ")"; |
---|
615 | } // if |
---|
616 | castExpr->arg->accept( *visitor ); |
---|
617 | output << ")"; |
---|
618 | } |
---|
619 | |
---|
620 | void CodeGenerator::postvisit( KeywordCastExpr * castExpr ) { |
---|
621 | assertf( ! options.genC, "KeywordCast should not reach code generation." ); |
---|
622 | extension( castExpr ); |
---|
623 | output << "((" << castExpr->targetString() << " &)"; |
---|
624 | castExpr->arg->accept( *visitor ); |
---|
625 | output << ")"; |
---|
626 | } |
---|
627 | |
---|
628 | void CodeGenerator::postvisit( VirtualCastExpr * castExpr ) { |
---|
629 | assertf( ! options.genC, "VirtualCastExpr should not reach code generation." ); |
---|
630 | extension( castExpr ); |
---|
631 | output << "(virtual "; |
---|
632 | castExpr->get_arg()->accept( *visitor ); |
---|
633 | output << ")"; |
---|
634 | } |
---|
635 | |
---|
636 | void CodeGenerator::postvisit( UntypedMemberExpr * memberExpr ) { |
---|
637 | assertf( ! options.genC, "UntypedMemberExpr should not reach code generation." ); |
---|
638 | extension( memberExpr ); |
---|
639 | memberExpr->get_aggregate()->accept( *visitor ); |
---|
640 | output << "."; |
---|
641 | memberExpr->get_member()->accept( *visitor ); |
---|
642 | } |
---|
643 | |
---|
644 | void CodeGenerator::postvisit( MemberExpr * memberExpr ) { |
---|
645 | extension( memberExpr ); |
---|
646 | memberExpr->get_aggregate()->accept( *visitor ); |
---|
647 | output << "." << mangleName( memberExpr->get_member() ); |
---|
648 | } |
---|
649 | |
---|
650 | void CodeGenerator::postvisit( VariableExpr * variableExpr ) { |
---|
651 | extension( variableExpr ); |
---|
652 | OperatorInfo opInfo; |
---|
653 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { |
---|
654 | output << opInfo.symbol; |
---|
655 | } else { |
---|
656 | output << mangleName( variableExpr->get_var() ); |
---|
657 | } // if |
---|
658 | } |
---|
659 | |
---|
660 | void CodeGenerator::postvisit( ConstantExpr * constantExpr ) { |
---|
661 | assert( constantExpr->get_constant() ); |
---|
662 | extension( constantExpr ); |
---|
663 | constantExpr->get_constant()->accept( *visitor ); |
---|
664 | } |
---|
665 | |
---|
666 | void CodeGenerator::postvisit( SizeofExpr * sizeofExpr ) { |
---|
667 | extension( sizeofExpr ); |
---|
668 | output << "sizeof("; |
---|
669 | if ( sizeofExpr->get_isType() ) { |
---|
670 | output << genType( sizeofExpr->get_type(), "", options ); |
---|
671 | } else { |
---|
672 | sizeofExpr->get_expr()->accept( *visitor ); |
---|
673 | } // if |
---|
674 | output << ")"; |
---|
675 | } |
---|
676 | |
---|
677 | void CodeGenerator::postvisit( AlignofExpr * alignofExpr ) { |
---|
678 | // use GCC extension to avoid bumping std to C11 |
---|
679 | extension( alignofExpr ); |
---|
680 | output << "__alignof__("; |
---|
681 | if ( alignofExpr->get_isType() ) { |
---|
682 | output << genType( alignofExpr->get_type(), "", options ); |
---|
683 | } else { |
---|
684 | alignofExpr->get_expr()->accept( *visitor ); |
---|
685 | } // if |
---|
686 | output << ")"; |
---|
687 | } |
---|
688 | |
---|
689 | void CodeGenerator::postvisit( UntypedOffsetofExpr * offsetofExpr ) { |
---|
690 | assertf( ! options.genC, "UntypedOffsetofExpr should not reach code generation." ); |
---|
691 | output << "offsetof("; |
---|
692 | output << genType( offsetofExpr->get_type(), "", options ); |
---|
693 | output << ", " << offsetofExpr->get_member(); |
---|
694 | output << ")"; |
---|
695 | } |
---|
696 | |
---|
697 | void CodeGenerator::postvisit( OffsetofExpr * offsetofExpr ) { |
---|
698 | // use GCC builtin |
---|
699 | output << "__builtin_offsetof("; |
---|
700 | output << genType( offsetofExpr->get_type(), "", options ); |
---|
701 | output << ", " << mangleName( offsetofExpr->get_member() ); |
---|
702 | output << ")"; |
---|
703 | } |
---|
704 | |
---|
705 | void CodeGenerator::postvisit( OffsetPackExpr * offsetPackExpr ) { |
---|
706 | assertf( ! options.genC, "OffsetPackExpr should not reach code generation." ); |
---|
707 | output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", options ) << ")"; |
---|
708 | } |
---|
709 | |
---|
710 | void CodeGenerator::postvisit( LogicalExpr * logicalExpr ) { |
---|
711 | extension( logicalExpr ); |
---|
712 | output << "("; |
---|
713 | logicalExpr->get_arg1()->accept( *visitor ); |
---|
714 | if ( logicalExpr->get_isAnd() ) { |
---|
715 | output << " && "; |
---|
716 | } else { |
---|
717 | output << " || "; |
---|
718 | } // if |
---|
719 | logicalExpr->get_arg2()->accept( *visitor ); |
---|
720 | output << ")"; |
---|
721 | } |
---|
722 | |
---|
723 | void CodeGenerator::postvisit( ConditionalExpr * conditionalExpr ) { |
---|
724 | extension( conditionalExpr ); |
---|
725 | output << "("; |
---|
726 | conditionalExpr->get_arg1()->accept( *visitor ); |
---|
727 | output << " ? "; |
---|
728 | conditionalExpr->get_arg2()->accept( *visitor ); |
---|
729 | output << " : "; |
---|
730 | conditionalExpr->get_arg3()->accept( *visitor ); |
---|
731 | output << ")"; |
---|
732 | } |
---|
733 | |
---|
734 | void CodeGenerator::postvisit( CommaExpr * commaExpr ) { |
---|
735 | extension( commaExpr ); |
---|
736 | output << "("; |
---|
737 | if ( options.genC ) { |
---|
738 | // arg1 of a CommaExpr is never used, so it can be safely cast to void to reduce gcc warnings. |
---|
739 | commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) ); |
---|
740 | } |
---|
741 | commaExpr->get_arg1()->accept( *visitor ); |
---|
742 | output << " , "; |
---|
743 | commaExpr->get_arg2()->accept( *visitor ); |
---|
744 | output << ")"; |
---|
745 | } |
---|
746 | |
---|
747 | void CodeGenerator::postvisit( TupleAssignExpr * tupleExpr ) { |
---|
748 | assertf( ! options.genC, "TupleAssignExpr should not reach code generation." ); |
---|
749 | tupleExpr->stmtExpr->accept( *visitor ); |
---|
750 | } |
---|
751 | |
---|
752 | void CodeGenerator::postvisit( UntypedTupleExpr * tupleExpr ) { |
---|
753 | assertf( ! options.genC, "UntypedTupleExpr should not reach code generation." ); |
---|
754 | extension( tupleExpr ); |
---|
755 | output << "["; |
---|
756 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); |
---|
757 | output << "]"; |
---|
758 | } |
---|
759 | |
---|
760 | void CodeGenerator::postvisit( TupleExpr * tupleExpr ) { |
---|
761 | assertf( ! options.genC, "TupleExpr should not reach code generation." ); |
---|
762 | extension( tupleExpr ); |
---|
763 | output << "["; |
---|
764 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); |
---|
765 | output << "]"; |
---|
766 | } |
---|
767 | |
---|
768 | void CodeGenerator::postvisit( TupleIndexExpr * tupleExpr ) { |
---|
769 | assertf( ! options.genC, "TupleIndexExpr should not reach code generation." ); |
---|
770 | extension( tupleExpr ); |
---|
771 | tupleExpr->get_tuple()->accept( *visitor ); |
---|
772 | output << "." << tupleExpr->get_index(); |
---|
773 | } |
---|
774 | |
---|
775 | void CodeGenerator::postvisit( TypeExpr * typeExpr ) { |
---|
776 | // if ( options.genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl; |
---|
777 | // assertf( ! options.genC, "TypeExpr should not reach code generation." ); |
---|
778 | if ( ! options.genC ) { |
---|
779 | output << genType( typeExpr->get_type(), "", options ); |
---|
780 | } |
---|
781 | } |
---|
782 | |
---|
783 | void CodeGenerator::postvisit( AsmExpr * asmExpr ) { |
---|
784 | if ( asmExpr->get_inout() ) { |
---|
785 | output << "[ "; |
---|
786 | asmExpr->get_inout()->accept( *visitor ); |
---|
787 | output << " ] "; |
---|
788 | } // if |
---|
789 | asmExpr->get_constraint()->accept( *visitor ); |
---|
790 | output << " ( "; |
---|
791 | asmExpr->get_operand()->accept( *visitor ); |
---|
792 | output << " )"; |
---|
793 | } |
---|
794 | |
---|
795 | void CodeGenerator::postvisit( CompoundLiteralExpr *compLitExpr ) { |
---|
796 | assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) ); |
---|
797 | output << "(" << genType( compLitExpr->get_result(), "", options ) << ")"; |
---|
798 | compLitExpr->get_initializer()->accept( *visitor ); |
---|
799 | } |
---|
800 | |
---|
801 | void CodeGenerator::postvisit( UniqueExpr * unqExpr ) { |
---|
802 | assertf( ! options.genC, "Unique expressions should not reach code generation." ); |
---|
803 | output << "unq<" << unqExpr->get_id() << ">{ "; |
---|
804 | unqExpr->get_expr()->accept( *visitor ); |
---|
805 | output << " }"; |
---|
806 | } |
---|
807 | |
---|
808 | void CodeGenerator::postvisit( StmtExpr * stmtExpr ) { |
---|
809 | std::list< Statement * > & stmts = stmtExpr->statements->kids; |
---|
810 | output << "({" << endl; |
---|
811 | ++indent; |
---|
812 | unsigned int numStmts = stmts.size(); |
---|
813 | unsigned int i = 0; |
---|
814 | for ( Statement * stmt : stmts ) { |
---|
815 | output << indent << printLabels( stmt->get_labels() ); |
---|
816 | if ( i+1 == numStmts ) { |
---|
817 | // last statement in a statement expression needs to be handled specially - |
---|
818 | // cannot cast to void, otherwise the expression statement has no value |
---|
819 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) { |
---|
820 | exprStmt->expr->accept( *visitor ); |
---|
821 | output << ";" << endl; |
---|
822 | ++i; |
---|
823 | break; |
---|
824 | } |
---|
825 | } |
---|
826 | stmt->accept( *visitor ); |
---|
827 | output << endl; |
---|
828 | if ( wantSpacing( stmt ) ) { |
---|
829 | output << endl; |
---|
830 | } // if |
---|
831 | ++i; |
---|
832 | } |
---|
833 | --indent; |
---|
834 | output << indent << "})"; |
---|
835 | } |
---|
836 | |
---|
837 | void CodeGenerator::postvisit( ConstructorExpr * expr ) { |
---|
838 | assertf( ! options.genC, "Unique expressions should not reach code generation." ); |
---|
839 | expr->callExpr->accept( *visitor ); |
---|
840 | } |
---|
841 | |
---|
842 | void CodeGenerator::postvisit( DeletedExpr * expr ) { |
---|
843 | assertf( ! options.genC, "Deleted expressions should not reach code generation." ); |
---|
844 | expr->expr->accept( *visitor ); |
---|
845 | } |
---|
846 | |
---|
847 | void CodeGenerator::postvisit( DefaultArgExpr * arg ) { |
---|
848 | assertf( ! options.genC, "Default argument expressions should not reach code generation." ); |
---|
849 | arg->expr->accept( *visitor ); |
---|
850 | } |
---|
851 | |
---|
852 | void CodeGenerator::postvisit( GenericExpr * expr ) { |
---|
853 | assertf( ! options.genC, "C11 _Generic expressions should not reach code generation." ); |
---|
854 | output << "_Generic("; |
---|
855 | expr->control->accept( *visitor ); |
---|
856 | output << ", "; |
---|
857 | unsigned int numAssocs = expr->associations.size(); |
---|
858 | unsigned int i = 0; |
---|
859 | for ( GenericExpr::Association & assoc : expr->associations ) { |
---|
860 | if (assoc.isDefault) { |
---|
861 | output << "default: "; |
---|
862 | } else { |
---|
863 | output << genType( assoc.type, "", options ) << ": "; |
---|
864 | } |
---|
865 | assoc.expr->accept( *visitor ); |
---|
866 | if ( i+1 != numAssocs ) { |
---|
867 | output << ", "; |
---|
868 | } |
---|
869 | i++; |
---|
870 | } |
---|
871 | output << ")"; |
---|
872 | } |
---|
873 | |
---|
874 | |
---|
875 | // *** Statements |
---|
876 | void CodeGenerator::postvisit( CompoundStmt * compoundStmt ) { |
---|
877 | std::list<Statement*> ks = compoundStmt->get_kids(); |
---|
878 | output << "{" << endl; |
---|
879 | |
---|
880 | ++indent; |
---|
881 | |
---|
882 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { |
---|
883 | output << indent << printLabels( (*i)->get_labels() ); |
---|
884 | (*i)->accept( *visitor ); |
---|
885 | |
---|
886 | output << endl; |
---|
887 | if ( wantSpacing( *i ) ) { |
---|
888 | output << endl; |
---|
889 | } // if |
---|
890 | } // for |
---|
891 | --indent; |
---|
892 | |
---|
893 | output << indent << "}"; |
---|
894 | } |
---|
895 | |
---|
896 | void CodeGenerator::postvisit( ExprStmt * exprStmt ) { |
---|
897 | assert( exprStmt ); |
---|
898 | if ( options.genC ) { |
---|
899 | // cast the top-level expression to void to reduce gcc warnings. |
---|
900 | exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) ); |
---|
901 | } |
---|
902 | exprStmt->get_expr()->accept( *visitor ); |
---|
903 | output << ";"; |
---|
904 | } |
---|
905 | |
---|
906 | void CodeGenerator::postvisit( AsmStmt * asmStmt ) { |
---|
907 | output << "asm "; |
---|
908 | if ( asmStmt->get_voltile() ) output << "volatile "; |
---|
909 | if ( ! asmStmt->get_gotolabels().empty() ) output << "goto "; |
---|
910 | output << "( "; |
---|
911 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor ); |
---|
912 | output << " : "; |
---|
913 | genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() ); |
---|
914 | output << " : "; |
---|
915 | genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() ); |
---|
916 | output << " : "; |
---|
917 | genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() ); |
---|
918 | if ( ! asmStmt->get_gotolabels().empty() ) { |
---|
919 | output << " : "; |
---|
920 | for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) { |
---|
921 | output << *begin++; |
---|
922 | if ( begin == asmStmt->get_gotolabels().end() ) break; |
---|
923 | output << ", "; |
---|
924 | } // for |
---|
925 | } // if |
---|
926 | output << " );"; |
---|
927 | } |
---|
928 | |
---|
929 | void CodeGenerator::postvisit( AsmDecl * asmDecl ) { |
---|
930 | output << "asm "; |
---|
931 | AsmStmt * asmStmt = asmDecl->get_stmt(); |
---|
932 | output << "( "; |
---|
933 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor ); |
---|
934 | output << " )"; |
---|
935 | } |
---|
936 | |
---|
937 | void CodeGenerator::postvisit( DirectiveStmt * dirStmt ) { |
---|
938 | output << endl << dirStmt->directive; // endl prevents spaces before directive |
---|
939 | } |
---|
940 | |
---|
941 | void CodeGenerator::postvisit( IfStmt * ifStmt ) { |
---|
942 | output << "if ( "; |
---|
943 | ifStmt->get_condition()->accept( *visitor ); |
---|
944 | output << " ) "; |
---|
945 | |
---|
946 | ifStmt->get_thenPart()->accept( *visitor ); |
---|
947 | |
---|
948 | if ( ifStmt->get_elsePart() != 0) { |
---|
949 | output << " else "; |
---|
950 | ifStmt->get_elsePart()->accept( *visitor ); |
---|
951 | } // if |
---|
952 | } |
---|
953 | |
---|
954 | void CodeGenerator::postvisit( SwitchStmt * switchStmt ) { |
---|
955 | output << "switch ( "; |
---|
956 | switchStmt->get_condition()->accept( *visitor ); |
---|
957 | output << " ) "; |
---|
958 | |
---|
959 | output << "{" << endl; |
---|
960 | ++indent; |
---|
961 | acceptAll( switchStmt->get_statements(), *visitor ); |
---|
962 | --indent; |
---|
963 | output << indent << "}"; |
---|
964 | } |
---|
965 | |
---|
966 | void CodeGenerator::postvisit( CaseStmt * caseStmt ) { |
---|
967 | updateLocation( caseStmt ); |
---|
968 | output << indent; |
---|
969 | if ( caseStmt->isDefault()) { |
---|
970 | output << "default"; |
---|
971 | } else { |
---|
972 | output << "case "; |
---|
973 | caseStmt->get_condition()->accept( *visitor ); |
---|
974 | } // if |
---|
975 | output << ":" << endl; |
---|
976 | |
---|
977 | std::list<Statement *> sts = caseStmt->get_statements(); |
---|
978 | |
---|
979 | ++indent; |
---|
980 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { |
---|
981 | output << indent << printLabels( (*i)->get_labels() ) ; |
---|
982 | (*i)->accept( *visitor ); |
---|
983 | output << endl; |
---|
984 | } // for |
---|
985 | --indent; |
---|
986 | } |
---|
987 | |
---|
988 | void CodeGenerator::postvisit( BranchStmt * branchStmt ) { |
---|
989 | switch ( branchStmt->get_type()) { |
---|
990 | case BranchStmt::Goto: |
---|
991 | if ( ! branchStmt->get_target().empty() ) |
---|
992 | output << "goto " << branchStmt->get_target(); |
---|
993 | else { |
---|
994 | if ( branchStmt->get_computedTarget() != 0 ) { |
---|
995 | output << "goto *"; |
---|
996 | branchStmt->get_computedTarget()->accept( *visitor ); |
---|
997 | } // if |
---|
998 | } // if |
---|
999 | break; |
---|
1000 | case BranchStmt::Break: |
---|
1001 | output << "break"; |
---|
1002 | break; |
---|
1003 | case BranchStmt::Continue: |
---|
1004 | output << "continue"; |
---|
1005 | break; |
---|
1006 | case BranchStmt::FallThrough: |
---|
1007 | case BranchStmt::FallThroughDefault: |
---|
1008 | assertf( ! options.genC, "fallthru should not reach code generation." ); |
---|
1009 | output << "fallthru"; |
---|
1010 | break; |
---|
1011 | } // switch |
---|
1012 | // print branch target for labelled break/continue/fallthru in debug mode |
---|
1013 | if ( ! options.genC && branchStmt->get_type() != BranchStmt::Goto ) { |
---|
1014 | if ( ! branchStmt->get_target().empty() ) { |
---|
1015 | output << " " << branchStmt->get_target(); |
---|
1016 | } else if ( branchStmt->get_type() == BranchStmt::FallThrough ) { |
---|
1017 | output << " default"; |
---|
1018 | } |
---|
1019 | } |
---|
1020 | output << ";"; |
---|
1021 | } |
---|
1022 | |
---|
1023 | void CodeGenerator::postvisit( ReturnStmt * returnStmt ) { |
---|
1024 | output << "return "; |
---|
1025 | maybeAccept( returnStmt->get_expr(), *visitor ); |
---|
1026 | output << ";"; |
---|
1027 | } |
---|
1028 | |
---|
1029 | void CodeGenerator::postvisit( ThrowStmt * throwStmt ) { |
---|
1030 | assertf( ! options.genC, "Throw statements should not reach code generation." ); |
---|
1031 | |
---|
1032 | output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ? |
---|
1033 | "throw" : "throwResume"); |
---|
1034 | if (throwStmt->get_expr()) { |
---|
1035 | output << " "; |
---|
1036 | throwStmt->get_expr()->accept( *visitor ); |
---|
1037 | } |
---|
1038 | if (throwStmt->get_target()) { |
---|
1039 | output << " _At "; |
---|
1040 | throwStmt->get_target()->accept( *visitor ); |
---|
1041 | } |
---|
1042 | output << ";"; |
---|
1043 | } |
---|
1044 | void CodeGenerator::postvisit( CatchStmt * stmt ) { |
---|
1045 | assertf( ! options.genC, "Catch statements should not reach code generation." ); |
---|
1046 | |
---|
1047 | output << ((stmt->get_kind() == CatchStmt::Terminate) ? |
---|
1048 | "catch" : "catchResume"); |
---|
1049 | output << "( "; |
---|
1050 | stmt->decl->accept( *visitor ); |
---|
1051 | output << " ) "; |
---|
1052 | |
---|
1053 | if( stmt->cond ) { |
---|
1054 | output << "if/when(?) ("; |
---|
1055 | stmt->cond->accept( *visitor ); |
---|
1056 | output << ") "; |
---|
1057 | } |
---|
1058 | stmt->body->accept( *visitor ); |
---|
1059 | } |
---|
1060 | |
---|
1061 | void CodeGenerator::postvisit( WaitForStmt * stmt ) { |
---|
1062 | assertf( ! options.genC, "Waitfor statements should not reach code generation." ); |
---|
1063 | |
---|
1064 | bool first = true; |
---|
1065 | for( auto & clause : stmt->clauses ) { |
---|
1066 | if(first) { output << "or "; first = false; } |
---|
1067 | if( clause.condition ) { |
---|
1068 | output << "when("; |
---|
1069 | stmt->timeout.condition->accept( *visitor ); |
---|
1070 | output << ") "; |
---|
1071 | } |
---|
1072 | output << "waitfor("; |
---|
1073 | clause.target.function->accept( *visitor ); |
---|
1074 | for( Expression * expr : clause.target.arguments ) { |
---|
1075 | output << ","; |
---|
1076 | expr->accept( *visitor ); |
---|
1077 | } |
---|
1078 | output << ") "; |
---|
1079 | clause.statement->accept( *visitor ); |
---|
1080 | } |
---|
1081 | |
---|
1082 | if( stmt->timeout.statement ) { |
---|
1083 | output << "or "; |
---|
1084 | if( stmt->timeout.condition ) { |
---|
1085 | output << "when("; |
---|
1086 | stmt->timeout.condition->accept( *visitor ); |
---|
1087 | output << ") "; |
---|
1088 | } |
---|
1089 | output << "timeout("; |
---|
1090 | stmt->timeout.time->accept( *visitor ); |
---|
1091 | output << ") "; |
---|
1092 | stmt->timeout.statement->accept( *visitor ); |
---|
1093 | } |
---|
1094 | |
---|
1095 | if( stmt->orelse.statement ) { |
---|
1096 | output << "or "; |
---|
1097 | if( stmt->orelse.condition ) { |
---|
1098 | output << "when("; |
---|
1099 | stmt->orelse.condition->accept( *visitor ); |
---|
1100 | output << ")"; |
---|
1101 | } |
---|
1102 | output << "else "; |
---|
1103 | stmt->orelse.statement->accept( *visitor ); |
---|
1104 | } |
---|
1105 | } |
---|
1106 | |
---|
1107 | void CodeGenerator::postvisit( WithStmt * with ) { |
---|
1108 | if ( ! options.genC ) { |
---|
1109 | output << "with ( "; |
---|
1110 | genCommaList( with->exprs.begin(), with->exprs.end() ); |
---|
1111 | output << " ) "; |
---|
1112 | } |
---|
1113 | with->stmt->accept( *visitor ); |
---|
1114 | } |
---|
1115 | |
---|
1116 | void CodeGenerator::postvisit( WhileStmt * whileStmt ) { |
---|
1117 | if ( whileStmt->get_isDoWhile() ) { |
---|
1118 | output << "do"; |
---|
1119 | } else { |
---|
1120 | output << "while ("; |
---|
1121 | whileStmt->get_condition()->accept( *visitor ); |
---|
1122 | output << ")"; |
---|
1123 | } // if |
---|
1124 | output << " "; |
---|
1125 | |
---|
1126 | output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() ); |
---|
1127 | whileStmt->get_body()->accept( *visitor ); |
---|
1128 | |
---|
1129 | output << indent; |
---|
1130 | |
---|
1131 | if ( whileStmt->get_isDoWhile() ) { |
---|
1132 | output << " while ("; |
---|
1133 | whileStmt->get_condition()->accept( *visitor ); |
---|
1134 | output << ");"; |
---|
1135 | } // if |
---|
1136 | } |
---|
1137 | |
---|
1138 | void CodeGenerator::postvisit( ForStmt * forStmt ) { |
---|
1139 | // initialization is always hoisted, so don't bother doing anything with that |
---|
1140 | output << "for (;"; |
---|
1141 | |
---|
1142 | if ( forStmt->get_condition() != 0 ) { |
---|
1143 | forStmt->get_condition()->accept( *visitor ); |
---|
1144 | } // if |
---|
1145 | output << ";"; |
---|
1146 | |
---|
1147 | if ( forStmt->get_increment() != 0 ) { |
---|
1148 | // cast the top-level expression to void to reduce gcc warnings. |
---|
1149 | Expression * expr = new CastExpr( forStmt->get_increment() ); |
---|
1150 | expr->accept( *visitor ); |
---|
1151 | } // if |
---|
1152 | output << ") "; |
---|
1153 | |
---|
1154 | if ( forStmt->get_body() != 0 ) { |
---|
1155 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() ); |
---|
1156 | forStmt->get_body()->accept( *visitor ); |
---|
1157 | } // if |
---|
1158 | } |
---|
1159 | |
---|
1160 | void CodeGenerator::postvisit( __attribute__((unused)) NullStmt * nullStmt ) { |
---|
1161 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); |
---|
1162 | output << "/* null statement */ ;"; |
---|
1163 | } |
---|
1164 | |
---|
1165 | void CodeGenerator::postvisit( DeclStmt * declStmt ) { |
---|
1166 | declStmt->get_decl()->accept( *visitor ); |
---|
1167 | |
---|
1168 | if ( doSemicolon( declStmt->get_decl() ) ) { |
---|
1169 | output << ";"; |
---|
1170 | } // if |
---|
1171 | } |
---|
1172 | |
---|
1173 | void CodeGenerator::postvisit( ImplicitCtorDtorStmt * stmt ) { |
---|
1174 | assertf( ! options.genC, "ImplicitCtorDtorStmts should not reach code generation." ); |
---|
1175 | stmt->callStmt->accept( *visitor ); |
---|
1176 | } |
---|
1177 | |
---|
1178 | void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) { |
---|
1179 | if ( decl->get_storageClasses().any() ) { |
---|
1180 | decl->get_storageClasses().print( output ); |
---|
1181 | } // if |
---|
1182 | } // CodeGenerator::handleStorageClass |
---|
1183 | |
---|
1184 | std::string genName( DeclarationWithType * decl ) { |
---|
1185 | CodeGen::OperatorInfo opInfo; |
---|
1186 | if ( operatorLookup( decl->get_name(), opInfo ) ) { |
---|
1187 | return opInfo.outputName; |
---|
1188 | } else { |
---|
1189 | return decl->get_name(); |
---|
1190 | } // if |
---|
1191 | } |
---|
1192 | } // namespace CodeGen |
---|
1193 | |
---|
1194 | |
---|
1195 | unsigned Indenter::tabsize = 2; |
---|
1196 | |
---|
1197 | std::ostream & operator<<( std::ostream & out, const BaseSyntaxNode * node ) { |
---|
1198 | if ( node ) { |
---|
1199 | node->print( out ); |
---|
1200 | } else { |
---|
1201 | out << "nullptr"; |
---|
1202 | } |
---|
1203 | return out; |
---|
1204 | } |
---|
1205 | |
---|
1206 | // Local Variables: // |
---|
1207 | // tab-width: 4 // |
---|
1208 | // mode: c++ // |
---|
1209 | // compile-command: "make install" // |
---|
1210 | // End: // |
---|