source: src/CodeGen/CodeGenerator.cc@ 3eab0ef6

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 resolv-new with_gc
Last change on this file since 3eab0ef6 was 0dd18fd, checked in by Andrew Beach <ajbeach@…>, 8 years ago

Removed the old LineMark system.

  • Property mode set to 100644
File size: 31.4 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 : Andrew Beach
12// Last Modified On : Fri Aug 18 15:34:00 2017
13// Update Count : 488
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/SemanticError.h" // for SemanticError
21#include "Common/UniqueName.h" // for UniqueName
22#include "Common/utility.h" // for CodeLocation, toString
23#include "GenType.h" // for genType
24#include "InitTweak/InitTweak.h" // for getPointerBase
25#include "OperatorTable.h" // for OperatorInfo, operatorLookup
26#include "Parser/LinkageSpec.h" // for Spec, Intrinsic
27#include "SynTree/Attribute.h" // for Attribute
28#include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode
29#include "SynTree/Constant.h" // for Constant
30#include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl
31#include "SynTree/Expression.h" // for Expression, UntypedExpr, Applica...
32#include "SynTree/Initializer.h" // for Initializer, ListInit, Designation
33#include "SynTree/Label.h" // for Label, operator<<
34#include "SynTree/Statement.h" // for Statement, AsmStmt, BranchStmt
35#include "SynTree/Type.h" // for Type, Type::StorageClasses, Func...
36
37using namespace std;
38
39namespace CodeGen {
40 int CodeGenerator::tabsize = 4;
41
42 // the kinds of statements that would ideally be followed by whitespace
43 bool wantSpacing( Statement * stmt) {
44 return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
45 dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
46 }
47
48 void CodeGenerator::extension( Expression * expr ) {
49 if ( expr->get_extension() ) {
50 output << "__extension__ ";
51 } // if
52 } // extension
53
54 void CodeGenerator::extension( Declaration * decl ) {
55 if ( decl->get_extension() ) {
56 output << "__extension__ ";
57 } // if
58 } // extension
59
60 void CodeGenerator::asmName( DeclarationWithType * decl ) {
61 if ( ConstantExpr * asmName = decl->get_asmName() ) {
62 output << " asm ( " << asmName->get_constant()->get_value() << " )";
63 } // if
64 } // extension
65
66 CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) {
67 labels = &l;
68 return *this;
69 }
70
71 ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) {
72 std::list< Label > & labs = *printLabels.labels;
73 // l.unique(); // assumes a sorted list. Why not use set? Does order matter?
74 for ( Label & l : labs ) {
75 output << l.get_name() + ": ";
76 printLabels.cg.genAttributes( l.get_attributes() );
77 } // for
78 return output;
79 }
80
81 /* Using updateLocation at the beginning of a node and nextLine
82 * within a node should become the method of formating.
83 */
84 void CodeGenerator::updateLocation( CodeLocation const & to ) {
85 if ( !lineMarks ) {
86 return;
87 } else if ( currentLocation.followedBy( to, 0 ) ) {
88 return;
89 } else if ( currentLocation.followedBy( to, 1 ) ) {
90 output << "\n" << indent;
91 currentLocation.linenumber += 1;
92 } else if ( currentLocation.followedBy( to, 2 ) ) {
93 output << "\n\n" << indent;
94 currentLocation.linenumber += 2;
95 } else {
96 output << "\n# " << to.linenumber << " \"" << 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 void CodeGenerator::nextLine() {
108 if ( !lineMarks ) {
109 output << "\n" << indent << std::flush;
110 }
111 }
112
113 CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
114
115 string CodeGenerator::mangleName( DeclarationWithType * decl ) {
116 if ( pretty ) return decl->get_name();
117 if ( decl->get_mangleName() != "" ) {
118 // need to incorporate scope level in order to differentiate names for destructors
119 return decl->get_scopedMangleName();
120 } else {
121 return decl->get_name();
122 } // if
123 }
124
125 void CodeGenerator::genAttributes( list< Attribute * > & attributes ) {
126 if ( attributes.empty() ) return;
127 output << "__attribute__ ((";
128 for ( list< Attribute * >::iterator attr( attributes.begin() );; ) {
129 output << (*attr)->get_name();
130 if ( ! (*attr)->get_parameters().empty() ) {
131 output << "(";
132 genCommaList( (*attr)->get_parameters().begin(), (*attr)->get_parameters().end() );
133 output << ")";
134 } // if
135 if ( ++attr == attributes.end() ) break;
136 output << ","; // separator
137 } // for
138 output << ")) ";
139 } // CodeGenerator::genAttributes
140
141
142 // *** Declarations
143 void CodeGenerator::visit( FunctionDecl * functionDecl ) {
144 extension( functionDecl );
145 genAttributes( functionDecl->get_attributes() );
146
147 handleStorageClass( functionDecl );
148 functionDecl->get_funcSpec().print( output );
149
150 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), pretty, genC );
151
152 asmName( functionDecl );
153
154 // acceptAll( functionDecl->get_oldDecls(), *this );
155 if ( functionDecl->get_statements() ) {
156 functionDecl->get_statements()->accept( *this );
157 } // if
158 }
159
160 void CodeGenerator::visit( ObjectDecl * objectDecl ) {
161 if (objectDecl->get_name().empty() && genC ) {
162 // only generate an anonymous name when generating C code, otherwise it clutters the output too much
163 static UniqueName name = { "__anonymous_object" };
164 objectDecl->set_name( name.newName() );
165 }
166
167 extension( objectDecl );
168 genAttributes( objectDecl->get_attributes() );
169
170 handleStorageClass( objectDecl );
171 output << genType( objectDecl->get_type(), mangleName( objectDecl ), pretty, genC );
172
173 asmName( objectDecl );
174
175 if ( objectDecl->get_init() ) {
176 output << " = ";
177 objectDecl->get_init()->accept( *this );
178 } // if
179
180 if ( objectDecl->get_bitfieldWidth() ) {
181 output << ":";
182 objectDecl->get_bitfieldWidth()->accept( *this );
183 } // if
184 }
185
186 void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) {
187 genAttributes( aggDecl->get_attributes() );
188
189 if( ! aggDecl->get_parameters().empty() && ! genC ) {
190 // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
191 output << "forall(";
192 genCommaList( aggDecl->get_parameters().begin(), aggDecl->get_parameters().end() );
193 output << ")" << endl;
194 }
195
196 output << kind;
197 if ( aggDecl->get_name() != "" )
198 output << aggDecl->get_name();
199
200 if ( aggDecl->has_body() ) {
201 std::list< Declaration * > & memb = aggDecl->get_members();
202 output << " {" << endl;
203
204 ++indent;
205 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
206 updateLocation( *i );
207 (*i)->accept( *this );
208 output << ";" << endl;
209 } // for
210
211 --indent;
212
213 output << indent << "}";
214 } // if
215 }
216
217 void CodeGenerator::visit( StructDecl * structDecl ) {
218 extension( structDecl );
219 handleAggregate( structDecl, "struct " );
220 }
221
222 void CodeGenerator::visit( UnionDecl * unionDecl ) {
223 extension( unionDecl );
224 handleAggregate( unionDecl, "union " );
225 }
226
227 void CodeGenerator::visit( EnumDecl * enumDecl ) {
228 extension( enumDecl );
229 updateLocation( enumDecl );
230 output << "enum ";
231 genAttributes( enumDecl->get_attributes() );
232
233 if ( enumDecl->get_name() != "" )
234 output << enumDecl->get_name();
235
236 std::list< Declaration* > &memb = enumDecl->get_members();
237
238 if ( ! memb.empty() ) {
239 output << " {" << endl;
240
241 ++indent;
242 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
243 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
244 assert( obj );
245 updateLocation( obj );
246 output << mangleName( obj );
247 if ( obj->get_init() ) {
248 output << " = ";
249 obj->get_init()->accept( *this );
250 } // if
251 output << "," << endl;
252 } // for
253
254 --indent;
255
256 output << indent << "}";
257 } // if
258 }
259
260 void CodeGenerator::visit( __attribute__((unused)) TraitDecl * traitDecl ) {}
261
262 void CodeGenerator::visit( TypedefDecl * typeDecl ) {
263 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
264 updateLocation( typeDecl );
265 output << "typedef ";
266 output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
267 }
268
269 void CodeGenerator::visit( TypeDecl * typeDecl ) {
270 if ( genC ) {
271 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
272 // still to be done
273 extension( typeDecl );
274 output << "extern unsigned long " << typeDecl->get_name();
275 if ( typeDecl->get_base() ) {
276 output << " = sizeof( " << genType( typeDecl->get_base(), "", pretty, genC ) << " )";
277 } // if
278 } else {
279 output << typeDecl->genTypeString() << " " << typeDecl->get_name();
280 if ( typeDecl->get_kind() != TypeDecl::Any && typeDecl->get_sized() ) {
281 output << " | sized(" << typeDecl->get_name() << ")";
282 }
283 if ( ! typeDecl->get_assertions().empty() ) {
284 output << " | { ";
285 genCommaList( typeDecl->get_assertions().begin(), typeDecl->get_assertions().end() );
286 output << " }";
287 }
288 }
289 }
290
291 void CodeGenerator::visit( Designation * designation ) {
292 std::list< Expression * > designators = designation->get_designators();
293 if ( designators.size() == 0 ) return;
294 for ( Expression * des : designators ) {
295 if ( dynamic_cast< NameExpr * >( des ) || dynamic_cast< VariableExpr * >( des ) ) {
296 // if expression is a NameExpr or VariableExpr, then initializing aggregate member
297 output << ".";
298 des->accept( *this );
299 } else {
300 // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt
301 output << "[";
302 des->accept( *this );
303 output << "]";
304 } // if
305 } // for
306 output << " = ";
307 }
308
309 void CodeGenerator::visit( SingleInit * init ) {
310 init->get_value()->accept( *this );
311 }
312
313 void CodeGenerator::visit( ListInit * init ) {
314 auto initBegin = init->begin();
315 auto initEnd = init->end();
316 auto desigBegin = init->get_designations().begin();
317 auto desigEnd = init->get_designations().end();
318
319 output << "{ ";
320 for ( ; initBegin != initEnd && desigBegin != desigEnd; ) {
321 (*desigBegin)->accept( *this );
322 (*initBegin)->accept( *this );
323 ++initBegin, ++desigBegin;
324 if ( initBegin != initEnd ) {
325 output << ", ";
326 }
327 }
328 output << " }";
329 assertf( initBegin == initEnd && desigBegin == desigEnd, "Initializers and designators not the same length. %s", toString( init ).c_str() );
330 }
331
332 void CodeGenerator::visit( __attribute__((unused)) ConstructorInit * init ){
333 assertf( ! genC, "ConstructorInit nodes should not reach code generation." );
334 // xxx - generate something reasonable for constructor/destructor pairs
335 output << "<ctorinit>";
336 }
337
338 void CodeGenerator::visit( Constant * constant ) {
339 output << constant->get_value() ;
340 }
341
342 // *** Expressions
343 void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
344 extension( applicationExpr );
345 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
346 OperatorInfo opInfo;
347 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
348 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
349 switch ( opInfo.type ) {
350 case OT_PREFIXASSIGN:
351 case OT_POSTFIXASSIGN:
352 case OT_INFIXASSIGN:
353 case OT_CTOR:
354 case OT_DTOR:
355 {
356 assert( arg != applicationExpr->get_args().end() );
357 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
358 // remove & from first assignment/ctor argument
359 *arg = addrExpr->get_arg();
360 } else {
361 // no address-of operator, so must be a pointer - add dereference
362 // NOTE: if the assertion starts to trigger, check that the application expr isn't being shared.
363 // Since its arguments are modified here, this assertion most commonly triggers when the application
364 // is visited multiple times.
365 UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );
366 newExpr->get_args().push_back( *arg );
367 Type * type = InitTweak::getPointerBase( (*arg)->get_result() );
368 assertf( type, "First argument to a derefence must be a pointer. Ensure that expressions are not being shared." );
369 newExpr->set_result( type->clone() );
370 *arg = newExpr;
371 } // if
372 break;
373 }
374
375 default:
376 // do nothing
377 ;
378 } // switch
379
380 switch ( opInfo.type ) {
381 case OT_INDEX:
382 assert( applicationExpr->get_args().size() == 2 );
383 (*arg++)->accept( *this );
384 output << "[";
385 (*arg)->accept( *this );
386 output << "]";
387 break;
388
389 case OT_CALL:
390 // there are no intrinsic definitions of the function call operator
391 assert( false );
392 break;
393
394 case OT_CTOR:
395 case OT_DTOR:
396 if ( applicationExpr->get_args().size() == 1 ) {
397 // the expression fed into a single parameter constructor or destructor may contain side
398 // effects, so must still output this expression
399 output << "(";
400 (*arg++)->accept( *this );
401 output << ") /* " << opInfo.inputName << " */";
402 } else if ( applicationExpr->get_args().size() == 2 ) {
403 // intrinsic two parameter constructors are essentially bitwise assignment
404 output << "(";
405 (*arg++)->accept( *this );
406 output << opInfo.symbol;
407 (*arg)->accept( *this );
408 output << ") /* " << opInfo.inputName << " */";
409 } else {
410 // no constructors with 0 or more than 2 parameters
411 assert( false );
412 } // if
413 break;
414
415 case OT_PREFIX:
416 case OT_PREFIXASSIGN:
417 assert( applicationExpr->get_args().size() == 1 );
418 output << "(";
419 output << opInfo.symbol;
420 (*arg)->accept( *this );
421 output << ")";
422 break;
423
424 case OT_POSTFIX:
425 case OT_POSTFIXASSIGN:
426 assert( applicationExpr->get_args().size() == 1 );
427 (*arg)->accept( *this );
428 output << opInfo.symbol;
429 break;
430
431
432 case OT_INFIX:
433 case OT_INFIXASSIGN:
434 assert( applicationExpr->get_args().size() == 2 );
435 output << "(";
436 (*arg++)->accept( *this );
437 output << opInfo.symbol;
438 (*arg)->accept( *this );
439 output << ")";
440 break;
441
442 case OT_CONSTANT:
443 case OT_LABELADDRESS:
444 // there are no intrinsic definitions of 0/1 or label addresses as functions
445 assert( false );
446 } // switch
447 } else {
448 varExpr->accept( *this );
449 output << "(";
450 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
451 output << ")";
452 } // if
453 } else {
454 applicationExpr->get_function()->accept( *this );
455 output << "(";
456 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
457 output << ")";
458 } // if
459 }
460
461 void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
462 extension( untypedExpr );
463 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
464 OperatorInfo opInfo;
465 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
466 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
467 switch ( opInfo.type ) {
468 case OT_INDEX:
469 assert( untypedExpr->get_args().size() == 2 );
470 (*arg++)->accept( *this );
471 output << "[";
472 (*arg)->accept( *this );
473 output << "]";
474 break;
475
476 case OT_CALL:
477 assert( false );
478
479 case OT_CTOR:
480 case OT_DTOR:
481 if ( untypedExpr->get_args().size() == 1 ) {
482 // the expression fed into a single parameter constructor or destructor may contain side
483 // effects, so must still output this expression
484 output << "(";
485 (*arg++)->accept( *this );
486 output << ") /* " << opInfo.inputName << " */";
487 } else if ( untypedExpr->get_args().size() == 2 ) {
488 // intrinsic two parameter constructors are essentially bitwise assignment
489 output << "(";
490 (*arg++)->accept( *this );
491 output << opInfo.symbol;
492 (*arg)->accept( *this );
493 output << ") /* " << opInfo.inputName << " */";
494 } else {
495 // no constructors with 0 or more than 2 parameters
496 assert( false );
497 } // if
498 break;
499
500 case OT_PREFIX:
501 case OT_PREFIXASSIGN:
502 case OT_LABELADDRESS:
503 assert( untypedExpr->get_args().size() == 1 );
504 output << "(";
505 output << opInfo.symbol;
506 (*arg)->accept( *this );
507 output << ")";
508 break;
509
510 case OT_POSTFIX:
511 case OT_POSTFIXASSIGN:
512 assert( untypedExpr->get_args().size() == 1 );
513 (*arg)->accept( *this );
514 output << opInfo.symbol;
515 break;
516
517 case OT_INFIX:
518 case OT_INFIXASSIGN:
519 assert( untypedExpr->get_args().size() == 2 );
520 output << "(";
521 (*arg++)->accept( *this );
522 output << opInfo.symbol;
523 (*arg)->accept( *this );
524 output << ")";
525 break;
526
527 case OT_CONSTANT:
528 // there are no intrinsic definitions of 0 or 1 as functions
529 assert( false );
530 } // switch
531 } else {
532 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
533 assert( untypedExpr->get_args().size() == 2 );
534 (*untypedExpr->get_args().begin())->accept( *this );
535 output << " ... ";
536 (*--untypedExpr->get_args().end())->accept( *this );
537 } else { // builtin routines
538 nameExpr->accept( *this );
539 output << "(";
540 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
541 output << ")";
542 } // if
543 } // if
544 } else {
545 untypedExpr->get_function()->accept( *this );
546 output << "(";
547 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
548 output << ")";
549 } // if
550 }
551
552 void CodeGenerator::visit( RangeExpr * rangeExpr ) {
553 rangeExpr->get_low()->accept( *this );
554 output << " ... ";
555 rangeExpr->get_high()->accept( *this );
556 }
557
558 void CodeGenerator::visit( NameExpr * nameExpr ) {
559 extension( nameExpr );
560 OperatorInfo opInfo;
561 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
562 assert( opInfo.type == OT_CONSTANT );
563 output << opInfo.symbol;
564 } else {
565 output << nameExpr->get_name();
566 } // if
567 }
568
569 void CodeGenerator::visit( AddressExpr * addressExpr ) {
570 extension( addressExpr );
571 output << "(&";
572 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
573 if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
574 output << mangleName( variableExpr->get_var() );
575 } else {
576 addressExpr->get_arg()->accept( *this );
577 } // if
578 output << ")";
579 }
580
581 void CodeGenerator::visit( CastExpr * castExpr ) {
582 extension( castExpr );
583 output << "(";
584 if ( castExpr->get_result()->isVoid() ) {
585 output << "(void)" ;
586 } else if ( ! castExpr->get_result()->get_lvalue() ) {
587 // at least one result type of cast, but not an lvalue
588 output << "(";
589 output << genType( castExpr->get_result(), "", pretty, genC );
590 output << ")";
591 } else {
592 // otherwise, the cast is to an lvalue type, so the cast should be dropped, since the result of a cast is
593 // never an lvalue in C
594 } // if
595 castExpr->get_arg()->accept( *this );
596 output << ")";
597 }
598
599 void CodeGenerator::visit( VirtualCastExpr * castExpr ) {
600 assertf( ! genC, "VirtualCastExpr should not reach code generation." );
601 extension( castExpr );
602 output << "(virtual ";
603 castExpr->get_arg()->accept( *this );
604 output << ")";
605 }
606
607 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
608 assertf( ! genC, "UntypedMemberExpr should not reach code generation." );
609 extension( memberExpr );
610 memberExpr->get_aggregate()->accept( *this );
611 output << ".";
612 memberExpr->get_member()->accept( *this );
613 }
614
615 void CodeGenerator::visit( MemberExpr * memberExpr ) {
616 extension( memberExpr );
617 memberExpr->get_aggregate()->accept( *this );
618 output << "." << mangleName( memberExpr->get_member() );
619 }
620
621 void CodeGenerator::visit( VariableExpr * variableExpr ) {
622 extension( variableExpr );
623 OperatorInfo opInfo;
624 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
625 output << opInfo.symbol;
626 } else {
627 output << mangleName( variableExpr->get_var() );
628 } // if
629 }
630
631 void CodeGenerator::visit( ConstantExpr * constantExpr ) {
632 assert( constantExpr->get_constant() );
633 extension( constantExpr );
634 constantExpr->get_constant()->accept( *this );
635 }
636
637 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
638 extension( sizeofExpr );
639 output << "sizeof(";
640 if ( sizeofExpr->get_isType() ) {
641 output << genType( sizeofExpr->get_type(), "", pretty, genC );
642 } else {
643 sizeofExpr->get_expr()->accept( *this );
644 } // if
645 output << ")";
646 }
647
648 void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
649 // use GCC extension to avoid bumping std to C11
650 extension( alignofExpr );
651 output << "__alignof__(";
652 if ( alignofExpr->get_isType() ) {
653 output << genType( alignofExpr->get_type(), "", pretty, genC );
654 } else {
655 alignofExpr->get_expr()->accept( *this );
656 } // if
657 output << ")";
658 }
659
660 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
661 assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." );
662 output << "offsetof(";
663 output << genType( offsetofExpr->get_type(), "", pretty, genC );
664 output << ", " << offsetofExpr->get_member();
665 output << ")";
666 }
667
668 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
669 // use GCC builtin
670 output << "__builtin_offsetof(";
671 output << genType( offsetofExpr->get_type(), "", pretty, genC );
672 output << ", " << mangleName( offsetofExpr->get_member() );
673 output << ")";
674 }
675
676 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
677 assertf( ! genC, "OffsetPackExpr should not reach code generation." );
678 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")";
679 }
680
681 void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
682 extension( logicalExpr );
683 output << "(";
684 logicalExpr->get_arg1()->accept( *this );
685 if ( logicalExpr->get_isAnd() ) {
686 output << " && ";
687 } else {
688 output << " || ";
689 } // if
690 logicalExpr->get_arg2()->accept( *this );
691 output << ")";
692 }
693
694 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
695 extension( conditionalExpr );
696 output << "(";
697 conditionalExpr->get_arg1()->accept( *this );
698 output << " ? ";
699 conditionalExpr->get_arg2()->accept( *this );
700 output << " : ";
701 conditionalExpr->get_arg3()->accept( *this );
702 output << ")";
703 }
704
705 void CodeGenerator::visit( CommaExpr * commaExpr ) {
706 extension( commaExpr );
707 output << "(";
708 commaExpr->get_arg1()->accept( *this );
709 output << " , ";
710 commaExpr->get_arg2()->accept( *this );
711 output << ")";
712 }
713
714 void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) {
715 assertf( ! genC, "UntypedTupleExpr should not reach code generation." );
716 extension( tupleExpr );
717 output << "[";
718 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
719 output << "]";
720 }
721
722 void CodeGenerator::visit( TupleExpr * tupleExpr ) {
723 assertf( ! genC, "TupleExpr should not reach code generation." );
724 extension( tupleExpr );
725 output << "[";
726 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
727 output << "]";
728 }
729
730 void CodeGenerator::visit( TupleIndexExpr * tupleExpr ) {
731 assertf( ! genC, "TupleIndexExpr should not reach code generation." );
732 extension( tupleExpr );
733 tupleExpr->get_tuple()->accept( *this );
734 output << "." << tupleExpr->get_index();
735 }
736
737 void CodeGenerator::visit( TypeExpr * typeExpr ) {
738 // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl;
739 // assertf( ! genC, "TypeExpr should not reach code generation." );
740 if ( ! genC ) {
741 output<< genType( typeExpr->get_type(), "", pretty, genC );
742 }
743 }
744
745 void CodeGenerator::visit( AsmExpr * asmExpr ) {
746 if ( asmExpr->get_inout() ) {
747 output << "[ ";
748 asmExpr->get_inout()->accept( *this );
749 output << " ] ";
750 } // if
751 asmExpr->get_constraint()->accept( *this );
752 output << " ( ";
753 asmExpr->get_operand()->accept( *this );
754 output << " )";
755 }
756
757 void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) {
758 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
759 output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")";
760 compLitExpr->get_initializer()->accept( *this );
761 }
762
763 void CodeGenerator::visit( StmtExpr * stmtExpr ) {
764 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
765 updateLocation( stmtExpr );
766 output << "({" << std::endl;
767 ++indent;
768 unsigned int numStmts = stmts.size();
769 unsigned int i = 0;
770 for ( Statement * stmt : stmts ) {
771 updateLocation( stmt );
772 output << printLabels( stmt->get_labels() );
773 if ( i+1 == numStmts ) {
774 // last statement in a statement expression needs to be handled specially -
775 // cannot cast to void, otherwise the expression statement has no value
776 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
777 exprStmt->get_expr()->accept( *this );
778 output << ";" << endl;
779 ++i;
780 break;
781 }
782 }
783 stmt->accept( *this );
784 output << endl;
785 if ( wantSpacing( stmt ) ) {
786 output << endl;
787 } // if
788 ++i;
789 }
790 --indent;
791 output << indent << "})";
792 }
793
794 // *** Statements
795 void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
796 std::list<Statement*> ks = compoundStmt->get_kids();
797 output << "{" << endl;
798
799 ++indent;
800
801 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
802 output << indent << printLabels( (*i)->get_labels() );
803 (*i)->accept( *this );
804
805 output << endl;
806 if ( wantSpacing( *i ) ) {
807 output << endl;
808 } // if
809 } // for
810 --indent;
811
812 output << indent << "}";
813 }
814
815 void CodeGenerator::visit( ExprStmt * exprStmt ) {
816 assert( exprStmt );
817 Expression * expr = exprStmt->get_expr();
818 if ( genC ) {
819 // cast the top-level expression to void to reduce gcc warnings.
820 expr = new CastExpr( expr );
821 }
822 expr->accept( *this );
823 output << ";";
824 }
825
826 void CodeGenerator::visit( AsmStmt * asmStmt ) {
827 output << "asm ";
828 if ( asmStmt->get_voltile() ) output << "volatile ";
829 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
830 output << "( ";
831 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
832 output << " : ";
833 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
834 output << " : ";
835 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
836 output << " : ";
837 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
838 if ( ! asmStmt->get_gotolabels().empty() ) {
839 output << " : ";
840 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
841 output << *begin++;
842 if ( begin == asmStmt->get_gotolabels().end() ) break;
843 output << ", ";
844 } // for
845 } // if
846 output << " );" ;
847 }
848
849 void CodeGenerator::visit( AsmDecl * asmDecl ) {
850 output << "asm ";
851 AsmStmt * asmStmt = asmDecl->get_stmt();
852 output << "( ";
853 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
854 output << " )" ;
855 }
856
857 void CodeGenerator::visit( IfStmt * ifStmt ) {
858 updateLocation( ifStmt );
859 output << "if ( ";
860 ifStmt->get_condition()->accept( *this );
861 output << " ) ";
862
863 ifStmt->get_thenPart()->accept( *this );
864
865 if ( ifStmt->get_elsePart() != 0) {
866 output << " else ";
867 ifStmt->get_elsePart()->accept( *this );
868 } // if
869 }
870
871 void CodeGenerator::visit( SwitchStmt * switchStmt ) {
872 updateLocation( switchStmt );
873 output << "switch ( " ;
874 switchStmt->get_condition()->accept( *this );
875 output << " ) ";
876
877 output << "{" << std::endl;
878 ++indent;
879 acceptAll( switchStmt->get_statements(), *this );
880 --indent;
881 output << indent << "}";
882 }
883
884 void CodeGenerator::visit( CaseStmt * caseStmt ) {
885 updateLocation( caseStmt );
886 if ( caseStmt->isDefault()) {
887 output << "default";
888 } else {
889 output << "case ";
890 caseStmt->get_condition()->accept( *this );
891 } // if
892 output << ":\n";
893
894 std::list<Statement *> sts = caseStmt->get_statements();
895
896 ++indent;
897 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
898 output << indent << printLabels( (*i)->get_labels() ) ;
899 (*i)->accept( *this );
900 output << endl;
901 } // for
902 --indent;
903 }
904
905 void CodeGenerator::visit( BranchStmt * branchStmt ) {
906 switch ( branchStmt->get_type()) {
907 case BranchStmt::Goto:
908 if ( ! branchStmt->get_target().empty() )
909 output << "goto " << branchStmt->get_target();
910 else {
911 if ( branchStmt->get_computedTarget() != 0 ) {
912 output << "goto *";
913 branchStmt->get_computedTarget()->accept( *this );
914 } // if
915 } // if
916 break;
917 case BranchStmt::Break:
918 output << "break";
919 break;
920 case BranchStmt::Continue:
921 output << "continue";
922 break;
923 } // switch
924 output << ";";
925 }
926
927 void CodeGenerator::visit( ReturnStmt * returnStmt ) {
928 output << "return ";
929 maybeAccept( returnStmt->get_expr(), *this );
930 output << ";";
931 }
932
933 void CodeGenerator::visit( ThrowStmt * throwStmt ) {
934 assertf( ! genC, "Throw statements should not reach code generation." );
935
936 output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ?
937 "throw" : "throwResume");
938 if (throwStmt->get_expr()) {
939 output << " ";
940 throwStmt->get_expr()->accept( *this );
941 }
942 if (throwStmt->get_target()) {
943 output << " _At ";
944 throwStmt->get_target()->accept( *this );
945 }
946 output << ";";
947 }
948
949 void CodeGenerator::visit( WhileStmt * whileStmt ) {
950 if ( whileStmt->get_isDoWhile() ) {
951 output << "do" ;
952 } else {
953 output << "while (" ;
954 whileStmt->get_condition()->accept( *this );
955 output << ")";
956 } // if
957 output << " ";
958
959 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
960 whileStmt->get_body()->accept( *this );
961
962 output << indent;
963
964 if ( whileStmt->get_isDoWhile() ) {
965 output << " while (" ;
966 whileStmt->get_condition()->accept( *this );
967 output << ");";
968 } // if
969 }
970
971 void CodeGenerator::visit( ForStmt * forStmt ) {
972 // initialization is always hoisted, so don't bother doing anything with that
973 output << "for (;";
974
975 if ( forStmt->get_condition() != 0 ) {
976 forStmt->get_condition()->accept( *this );
977 } // if
978 output << ";";
979
980 if ( forStmt->get_increment() != 0 ) {
981 // cast the top-level expression to void to reduce gcc warnings.
982 Expression * expr = new CastExpr( forStmt->get_increment() );
983 expr->accept( *this );
984 } // if
985 output << ") ";
986
987 if ( forStmt->get_body() != 0 ) {
988 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
989 forStmt->get_body()->accept( *this );
990 } // if
991 }
992
993 void CodeGenerator::visit( __attribute__((unused)) NullStmt * nullStmt ) {
994 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
995 output << "/* null statement */ ;";
996 }
997
998 void CodeGenerator::visit( DeclStmt * declStmt ) {
999 declStmt->get_decl()->accept( *this );
1000
1001 if ( doSemicolon( declStmt->get_decl() ) ) {
1002 output << ";";
1003 } // if
1004 }
1005
1006 void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) {
1007 if ( decl->get_storageClasses().any() ) {
1008 decl->get_storageClasses().print( output );
1009 } // if
1010 } // CodeGenerator::handleStorageClass
1011
1012 std::string genName( DeclarationWithType * decl ) {
1013 CodeGen::OperatorInfo opInfo;
1014 if ( operatorLookup( decl->get_name(), opInfo ) ) {
1015 return opInfo.outputName;
1016 } else {
1017 return decl->get_name();
1018 } // if
1019 }
1020} // namespace CodeGen
1021
1022// Local Variables: //
1023// tab-width: 4 //
1024// mode: c++ //
1025// compile-command: "make install" //
1026// End: //
Note: See TracBrowser for help on using the repository browser.