source: src/CodeGen/CodeGenerator.cc@ c28afead

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 c28afead was 5f08961d, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Add debug codegen for types in postfix comments for expressions

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