source: src/CodeGen/CodeGenerator.cc@ f006f01

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

make TupleAssignment generate temporaries, add StmtExpr for GCC statement expressions, expand tuple assignment expressions, collapse SolvedTupleExpr, MassAssignExpr, and MultipleAssignExpr into TupleAssignExpr

  • Property mode set to 100644
File size: 25.5 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( Constant * constant ) {
286 output << constant->get_value() ;
287 }
288
289 //*** Expressions
290 void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
291 extension( applicationExpr );
292 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
293 OperatorInfo opInfo;
294 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
295 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
296 switch ( opInfo.type ) {
297 case OT_PREFIXASSIGN:
298 case OT_POSTFIXASSIGN:
299 case OT_INFIXASSIGN:
300 case OT_CTOR:
301 case OT_DTOR:
302 {
303 assert( arg != applicationExpr->get_args().end() );
304 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
305 // remove & from first assignment/ctor argument
306 *arg = addrExpr->get_arg();
307 } else {
308 // no address-of operator, so must be a pointer - add dereference
309 UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );
310 newExpr->get_args().push_back( *arg );
311 assert( (*arg)->get_results().size() == 1 );
312 Type * type = InitTweak::getPointerBase( (*arg)->get_results().front() );
313 assert( type );
314 newExpr->get_results().push_back( type->clone() );
315 *arg = newExpr;
316 } // if
317 break;
318 }
319
320 default:
321 // do nothing
322 ;
323 } // switch
324
325 switch ( opInfo.type ) {
326 case OT_INDEX:
327 assert( applicationExpr->get_args().size() == 2 );
328 (*arg++)->accept( *this );
329 output << "[";
330 (*arg)->accept( *this );
331 output << "]";
332 break;
333
334 case OT_CALL:
335 // there are no intrinsic definitions of the function call operator
336 assert( false );
337 break;
338
339 case OT_CTOR:
340 case OT_DTOR:
341 if ( applicationExpr->get_args().size() == 1 ) {
342 // the expression fed into a single parameter constructor or destructor may contain side
343 // effects, so must still output this expression
344 output << "(";
345 (*arg++)->accept( *this );
346 output << ") /* " << opInfo.inputName << " */";
347 } else if ( applicationExpr->get_args().size() == 2 ) {
348 // intrinsic two parameter constructors are essentially bitwise assignment
349 output << "(";
350 (*arg++)->accept( *this );
351 output << opInfo.symbol;
352 (*arg)->accept( *this );
353 output << ") /* " << opInfo.inputName << " */";
354 } else {
355 // no constructors with 0 or more than 2 parameters
356 assert( false );
357 } // if
358 break;
359
360 case OT_PREFIX:
361 case OT_PREFIXASSIGN:
362 assert( applicationExpr->get_args().size() == 1 );
363 output << "(";
364 output << opInfo.symbol;
365 (*arg)->accept( *this );
366 output << ")";
367 break;
368
369 case OT_POSTFIX:
370 case OT_POSTFIXASSIGN:
371 assert( applicationExpr->get_args().size() == 1 );
372 (*arg)->accept( *this );
373 output << opInfo.symbol;
374 break;
375
376
377 case OT_INFIX:
378 case OT_INFIXASSIGN:
379 assert( applicationExpr->get_args().size() == 2 );
380 output << "(";
381 (*arg++)->accept( *this );
382 output << opInfo.symbol;
383 (*arg)->accept( *this );
384 output << ")";
385 break;
386
387 case OT_CONSTANT:
388 case OT_LABELADDRESS:
389 // there are no intrinsic definitions of 0/1 or label addresses as functions
390 assert( false );
391 } // switch
392 } else {
393 varExpr->accept( *this );
394 output << "(";
395 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
396 output << ")";
397 } // if
398 } else {
399 applicationExpr->get_function()->accept( *this );
400 output << "(";
401 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
402 output << ")";
403 } // if
404 }
405
406 void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
407 extension( untypedExpr );
408 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
409 OperatorInfo opInfo;
410 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
411 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
412 switch ( opInfo.type ) {
413 case OT_INDEX:
414 assert( untypedExpr->get_args().size() == 2 );
415 (*arg++)->accept( *this );
416 output << "[";
417 (*arg)->accept( *this );
418 output << "]";
419 break;
420
421 case OT_CALL:
422 assert( false );
423
424 case OT_CTOR:
425 case OT_DTOR:
426 if ( untypedExpr->get_args().size() == 1 ) {
427 // the expression fed into a single parameter constructor or destructor may contain side
428 // effects, so must still output this expression
429 output << "(";
430 (*arg++)->accept( *this );
431 output << ") /* " << opInfo.inputName << " */";
432 } else if ( untypedExpr->get_args().size() == 2 ) {
433 // intrinsic two parameter constructors are essentially bitwise assignment
434 output << "(";
435 (*arg++)->accept( *this );
436 output << opInfo.symbol;
437 (*arg)->accept( *this );
438 output << ") /* " << opInfo.inputName << " */";
439 } else {
440 // no constructors with 0 or more than 2 parameters
441 assert( false );
442 } // if
443 break;
444
445 case OT_PREFIX:
446 case OT_PREFIXASSIGN:
447 case OT_LABELADDRESS:
448 assert( untypedExpr->get_args().size() == 1 );
449 output << "(";
450 output << opInfo.symbol;
451 (*arg)->accept( *this );
452 output << ")";
453 break;
454
455 case OT_POSTFIX:
456 case OT_POSTFIXASSIGN:
457 assert( untypedExpr->get_args().size() == 1 );
458 (*arg)->accept( *this );
459 output << opInfo.symbol;
460 break;
461
462 case OT_INFIX:
463 case OT_INFIXASSIGN:
464 assert( untypedExpr->get_args().size() == 2 );
465 output << "(";
466 (*arg++)->accept( *this );
467 output << opInfo.symbol;
468 (*arg)->accept( *this );
469 output << ")";
470 break;
471
472 case OT_CONSTANT:
473 // there are no intrinsic definitions of 0 or 1 as functions
474 assert( false );
475 } // switch
476 } else {
477 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
478 assert( untypedExpr->get_args().size() == 2 );
479 (*untypedExpr->get_args().begin())->accept( *this );
480 output << " ... ";
481 (*--untypedExpr->get_args().end())->accept( *this );
482 } else { // builtin routines
483 nameExpr->accept( *this );
484 output << "(";
485 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
486 output << ")";
487 } // if
488 } // if
489 } else {
490 untypedExpr->get_function()->accept( *this );
491 output << "(";
492 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
493 output << ")";
494 } // if
495 }
496
497 void CodeGenerator::visit( RangeExpr * rangeExpr ) {
498 rangeExpr->get_low()->accept( *this );
499 output << " ... ";
500 rangeExpr->get_high()->accept( *this );
501 }
502
503 void CodeGenerator::visit( NameExpr * nameExpr ) {
504 extension( nameExpr );
505 OperatorInfo opInfo;
506 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
507 assert( opInfo.type == OT_CONSTANT );
508 output << opInfo.symbol;
509 } else {
510 output << nameExpr->get_name();
511 } // if
512 }
513
514 void CodeGenerator::visit( AddressExpr * addressExpr ) {
515 extension( addressExpr );
516 output << "(&";
517 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
518 if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
519 output << mangleName( variableExpr->get_var() );
520 } else {
521 addressExpr->get_arg()->accept( *this );
522 } // if
523 output << ")";
524 }
525
526 void CodeGenerator::visit( CastExpr * castExpr ) {
527 extension( castExpr );
528 output << "(";
529 if ( castExpr->get_results().empty() ) {
530 output << "(void)" ;
531 } else if ( ! castExpr->get_results().front()->get_isLvalue() ) {
532 // at least one result type of cast, but not an lvalue
533 output << "(";
534 output << genType( castExpr->get_results().front(), "" );
535 output << ")";
536 } else {
537 // otherwise, the cast is to an lvalue type, so the cast should be dropped, since the result of a cast is
538 // never an lvalue in C
539 } // if
540 castExpr->get_arg()->accept( *this );
541 output << ")";
542 }
543
544 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
545 assert( false );
546 }
547
548 void CodeGenerator::visit( MemberExpr * memberExpr ) {
549 extension( memberExpr );
550 memberExpr->get_aggregate()->accept( *this );
551 output << "." << mangleName( memberExpr->get_member() );
552 }
553
554 void CodeGenerator::visit( VariableExpr * variableExpr ) {
555 extension( variableExpr );
556 OperatorInfo opInfo;
557 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
558 output << opInfo.symbol;
559 } else {
560 output << mangleName( variableExpr->get_var() );
561 } // if
562 }
563
564 void CodeGenerator::visit( ConstantExpr * constantExpr ) {
565 assert( constantExpr->get_constant() );
566 extension( constantExpr );
567 constantExpr->get_constant()->accept( *this );
568 }
569
570 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
571 extension( sizeofExpr );
572 output << "sizeof(";
573 if ( sizeofExpr->get_isType() ) {
574 output << genType( sizeofExpr->get_type(), "" );
575 } else {
576 sizeofExpr->get_expr()->accept( *this );
577 } // if
578 output << ")";
579 }
580
581 void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
582 // use GCC extension to avoid bumping std to C11
583 extension( alignofExpr );
584 output << "__alignof__(";
585 if ( alignofExpr->get_isType() ) {
586 output << genType( alignofExpr->get_type(), "" );
587 } else {
588 alignofExpr->get_expr()->accept( *this );
589 } // if
590 output << ")";
591 }
592
593 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
594 assert( false && "UntypedOffsetofExpr should not reach code generation." );
595 }
596
597 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
598 // use GCC builtin
599 output << "__builtin_offsetof(";
600 output << genType( offsetofExpr->get_type(), "" );
601 output << ", " << mangleName( offsetofExpr->get_member() );
602 output << ")";
603 }
604
605 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
606 assert( false && "OffsetPackExpr should not reach code generation." );
607 }
608
609 void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
610 extension( logicalExpr );
611 output << "(";
612 logicalExpr->get_arg1()->accept( *this );
613 if ( logicalExpr->get_isAnd() ) {
614 output << " && ";
615 } else {
616 output << " || ";
617 } // if
618 logicalExpr->get_arg2()->accept( *this );
619 output << ")";
620 }
621
622 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
623 extension( conditionalExpr );
624 output << "(";
625 conditionalExpr->get_arg1()->accept( *this );
626 output << " ? ";
627 conditionalExpr->get_arg2()->accept( *this );
628 output << " : ";
629 conditionalExpr->get_arg3()->accept( *this );
630 output << ")";
631 }
632
633 void CodeGenerator::visit( CommaExpr * commaExpr ) {
634 extension( commaExpr );
635 output << "(";
636 commaExpr->get_arg1()->accept( *this );
637 output << " , ";
638 commaExpr->get_arg2()->accept( *this );
639 output << ")";
640 }
641
642 void CodeGenerator::visit( TupleExpr * tupleExpr ) {}
643
644 void CodeGenerator::visit( TypeExpr * typeExpr ) {}
645
646 void CodeGenerator::visit( AsmExpr * asmExpr ) {
647 if ( asmExpr->get_inout() ) {
648 output << "[ ";
649 asmExpr->get_inout()->accept( *this );
650 output << " ] ";
651 } // if
652 asmExpr->get_constraint()->accept( *this );
653 output << " ( ";
654 asmExpr->get_operand()->accept( *this );
655 output << " )";
656 }
657
658 void CodeGenerator::visit( StmtExpr * stmtExpr ) {
659 output << "(";
660 stmtExpr->get_statements()->accept( *this );
661 output << ")";
662 }
663
664 //*** Statements
665 void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
666 std::list<Statement*> ks = compoundStmt->get_kids();
667 output << "{" << endl;
668
669 cur_indent += CodeGenerator::tabsize;
670
671 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
672 output << indent << printLabels( (*i)->get_labels() );
673 (*i)->accept( *this );
674
675 output << endl;
676 if ( wantSpacing( *i ) ) {
677 output << endl;
678 } // if
679 } // for
680 cur_indent -= CodeGenerator::tabsize;
681
682 output << indent << "}";
683 }
684
685 void CodeGenerator::visit( ExprStmt * exprStmt ) {
686 assert( exprStmt );
687 // cast the top-level expression to void to reduce gcc warnings.
688 Expression * expr = new CastExpr( exprStmt->get_expr() );
689 expr->accept( *this );
690 output << ";";
691 }
692
693 void CodeGenerator::visit( AsmStmt * asmStmt ) {
694 output << "asm ";
695 if ( asmStmt->get_voltile() ) output << "volatile ";
696 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
697 output << "( ";
698 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
699 output << " : ";
700 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
701 output << " : ";
702 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
703 output << " : ";
704 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
705 if ( ! asmStmt->get_gotolabels().empty() ) {
706 output << " : ";
707 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
708 output << *begin++;
709 if ( begin == asmStmt->get_gotolabels().end() ) break;
710 output << ", ";
711 } // for
712 } // if
713 output << " );" ;
714 }
715
716 void CodeGenerator::visit( IfStmt * ifStmt ) {
717 output << "if ( ";
718 ifStmt->get_condition()->accept( *this );
719 output << " ) ";
720
721 ifStmt->get_thenPart()->accept( *this );
722
723 if ( ifStmt->get_elsePart() != 0) {
724 output << " else ";
725 ifStmt->get_elsePart()->accept( *this );
726 } // if
727 }
728
729 void CodeGenerator::visit( SwitchStmt * switchStmt ) {
730 output << "switch ( " ;
731 switchStmt->get_condition()->accept( *this );
732 output << " ) ";
733
734 output << "{" << std::endl;
735 cur_indent += CodeGenerator::tabsize;
736 acceptAll( switchStmt->get_statements(), *this );
737 cur_indent -= CodeGenerator::tabsize;
738 output << indent << "}";
739 }
740
741 void CodeGenerator::visit( CaseStmt * caseStmt ) {
742 output << indent;
743 if ( caseStmt->isDefault()) {
744 output << "default";
745 } else {
746 output << "case ";
747 caseStmt->get_condition()->accept( *this );
748 } // if
749 output << ":\n";
750
751 std::list<Statement *> sts = caseStmt->get_statements();
752
753 cur_indent += CodeGenerator::tabsize;
754 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
755 output << indent << printLabels( (*i)->get_labels() ) ;
756 (*i)->accept( *this );
757 output << endl;
758 } // for
759 cur_indent -= CodeGenerator::tabsize;
760 }
761
762 void CodeGenerator::visit( BranchStmt * branchStmt ) {
763 switch ( branchStmt->get_type()) {
764 case BranchStmt::Goto:
765 if ( ! branchStmt->get_target().empty() )
766 output << "goto " << branchStmt->get_target();
767 else {
768 if ( branchStmt->get_computedTarget() != 0 ) {
769 output << "goto *";
770 branchStmt->get_computedTarget()->accept( *this );
771 } // if
772 } // if
773 break;
774 case BranchStmt::Break:
775 output << "break";
776 break;
777 case BranchStmt::Continue:
778 output << "continue";
779 break;
780 } // switch
781 output << ";";
782 }
783
784
785 void CodeGenerator::visit( ReturnStmt * returnStmt ) {
786 output << "return ";
787 maybeAccept( returnStmt->get_expr(), *this );
788 output << ";";
789 }
790
791 void CodeGenerator::visit( WhileStmt * whileStmt ) {
792 if ( whileStmt->get_isDoWhile() ) {
793 output << "do" ;
794 } else {
795 output << "while (" ;
796 whileStmt->get_condition()->accept( *this );
797 output << ")";
798 } // if
799 output << " ";
800
801 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
802 whileStmt->get_body()->accept( *this );
803
804 output << indent;
805
806 if ( whileStmt->get_isDoWhile() ) {
807 output << " while (" ;
808 whileStmt->get_condition()->accept( *this );
809 output << ");";
810 } // if
811 }
812
813 void CodeGenerator::visit( ForStmt * forStmt ) {
814 // initialization is always hoisted, so don't bother doing anything with that
815 output << "for (;";
816
817 if ( forStmt->get_condition() != 0 ) {
818 forStmt->get_condition()->accept( *this );
819 } // if
820 output << ";";
821
822 if ( forStmt->get_increment() != 0 ) {
823 // cast the top-level expression to void to reduce gcc warnings.
824 Expression * expr = new CastExpr( forStmt->get_increment() );
825 expr->accept( *this );
826 } // if
827 output << ") ";
828
829 if ( forStmt->get_body() != 0 ) {
830 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
831 forStmt->get_body()->accept( *this );
832 } // if
833 }
834
835 void CodeGenerator::visit( NullStmt * nullStmt ) {
836 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
837 output << "/* null statement */ ;";
838 }
839
840 void CodeGenerator::visit( DeclStmt * declStmt ) {
841 declStmt->get_decl()->accept( *this );
842
843 if ( doSemicolon( declStmt->get_decl() ) ) {
844 output << ";";
845 } // if
846 }
847
848 void CodeGenerator::handleStorageClass( Declaration * decl ) {
849 switch ( decl->get_storageClass() ) {
850 case DeclarationNode::Extern:
851 output << "extern ";
852 break;
853 case DeclarationNode::Static:
854 output << "static ";
855 break;
856 case DeclarationNode::Auto:
857 // silently drop storage class
858 break;
859 case DeclarationNode::Register:
860 output << "register ";
861 break;
862 case DeclarationNode::Inline:
863 output << "inline ";
864 break;
865 case DeclarationNode::Fortran:
866 output << "fortran ";
867 break;
868 case DeclarationNode::Noreturn:
869 output << "_Noreturn ";
870 break;
871 case DeclarationNode::Threadlocal:
872 output << "_Thread_local ";
873 break;
874 case DeclarationNode::NoStorageClass:
875 break;
876 } // switch
877 }
878} // namespace CodeGen
879
880// Local Variables: //
881// tab-width: 4 //
882// mode: c++ //
883// compile-command: "make install" //
884// End: //
Note: See TracBrowser for help on using the repository browser.