source: src/CodeGen/CodeGenerator.cc@ 96812c0

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since 96812c0 was 3ed994e, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Push deleted decls through the system

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