source: src/CodeGen/CodeGenerator.cc@ b2fe1c9

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 b2fe1c9 was a16764a6, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Changed warning system to prepare for toggling warnings

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