source: src/CodeGen/CodeGenerator.cc@ a64644c

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 a64644c was fc638d2, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

fixed StmtExpr code in PolyMutator, added missing copy constructor, misc documentation

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