source: src/CodeGen/CodeGenerator.cc@ f9cebb5

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory 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 f9cebb5 was f9cebb5, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

add gcc attributes to ObjectDecl, hoist destructed static variables, add breaks to end of generated switch

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