source: src/CodeGen/CodeGenerator.cc@ 03e5d14

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii 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 03e5d14 was 03e5d14, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

add support for constructor/destructor attribute priority and set priority for library constructors to high

  • Property mode set to 100644
File size: 21.3 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 : Rob Schluntz
12// Last Modified On : Fri May 06 15:40:35 2016
13// Update Count : 243
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
29#include "Common/utility.h"
30#include "Common/UnimplementedError.h"
31
32#include "CodeGenerator.h"
33#include "OperatorTable.h"
34#include "GenType.h"
35
36using namespace std;
37
38namespace CodeGen {
39 int CodeGenerator::tabsize = 4;
40
41 // the kinds of statements that would ideally be followed by whitespace
42 bool wantSpacing( Statement * stmt) {
43 return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
44 dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
45 }
46
47 ostream & CodeGenerator::Indenter::operator()( ostream & output ) {
48 return output << string( cg.cur_indent, ' ' );
49 }
50
51 ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) {
52 return indent( output );
53 }
54
55 CodeGenerator::CodeGenerator( std::ostream &os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
56
57 CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
58 : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
59 //output << std::string( init );
60 }
61
62 CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
63 : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
64 //output << std::string( init );
65 }
66
67 string mangleName( DeclarationWithType *decl ) {
68 if ( decl->get_mangleName() != "" ) {
69 return decl->get_mangleName();
70 } else {
71 return decl->get_name();
72 } // if
73 }
74
75 //*** Declarations
76 void CodeGenerator::visit( FunctionDecl *functionDecl ) {
77 // generalize this
78 FunctionDecl::Attribute attr = functionDecl->get_attribute();
79 switch ( attr.type ) {
80 case FunctionDecl::Attribute::Constructor:
81 output << "__attribute__ ((constructor";
82 if ( attr.priority != FunctionDecl::Attribute::Default ) {
83 output << "(" << attr.priority << ")";
84 }
85 output << ")) ";
86 break;
87 case FunctionDecl::Attribute::Destructor:
88 output << "__attribute__ ((destructor";
89 if ( attr.priority != FunctionDecl::Attribute::Default ) {
90 output << "(" << attr.priority << ")";
91 }
92 output << ")) ";
93 break;
94 default:
95 break;
96 }
97 handleStorageClass( functionDecl );
98 if ( functionDecl->get_isInline() ) {
99 output << "inline ";
100 } // if
101 if ( functionDecl->get_isNoreturn() ) {
102 output << "_Noreturn ";
103 } // if
104 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
105
106 // how to get this to the Functype?
107 std::list< Declaration * > olds = functionDecl->get_oldDecls();
108 if ( ! olds.empty() ) {
109 output << " /* function has old declaration */";
110 } // if
111
112 // acceptAll( functionDecl->get_oldDecls(), *this );
113 if ( functionDecl->get_statements() ) {
114 functionDecl->get_statements()->accept( *this );
115 } // if
116 }
117
118 void CodeGenerator::visit( ObjectDecl *objectDecl ) {
119 handleStorageClass( objectDecl );
120 output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
121
122 if ( objectDecl->get_init() ) {
123 output << " = ";
124 objectDecl->get_init()->accept( *this );
125 } // if
126 if ( objectDecl->get_bitfieldWidth() ) {
127 output << ":";
128 objectDecl->get_bitfieldWidth()->accept( *this );
129 } // if
130 }
131
132 void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
133 if ( aggDecl->get_name() != "" )
134 output << aggDecl->get_name();
135
136 std::list< Declaration * > &memb = aggDecl->get_members();
137
138 if ( ! memb.empty() ) {
139 output << " {" << endl;
140
141 cur_indent += CodeGenerator::tabsize;
142 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
143 output << indent;
144 (*i)->accept( *this );
145 output << ";" << endl;
146 }
147
148 cur_indent -= CodeGenerator::tabsize;
149
150 output << indent << "}";
151 } // if
152 }
153
154 void CodeGenerator::visit( StructDecl *structDecl ) {
155 output << "struct ";
156 handleAggregate( structDecl );
157 }
158
159 void CodeGenerator::visit( UnionDecl *aggregateDecl ) {
160 output << "union ";
161 handleAggregate( aggregateDecl );
162 }
163
164 void CodeGenerator::visit( EnumDecl *aggDecl ) {
165 output << "enum ";
166
167 if ( aggDecl->get_name() != "" )
168 output << aggDecl->get_name();
169
170 std::list< Declaration* > &memb = aggDecl->get_members();
171
172 if ( ! memb.empty() ) {
173 output << " {" << endl;
174
175 cur_indent += CodeGenerator::tabsize;
176 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
177 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
178 assert( obj );
179 output << indent << mangleName( obj );
180 if ( obj->get_init() ) {
181 output << " = ";
182 obj->get_init()->accept( *this );
183 } // if
184 output << "," << endl;
185 } // for
186
187 cur_indent -= CodeGenerator::tabsize;
188
189 output << indent << "}";
190 } // if
191 }
192
193 void CodeGenerator::visit( TraitDecl *aggregateDecl ) {}
194
195 void CodeGenerator::visit( TypedefDecl *typeDecl ) {
196 output << "typedef ";
197 output << genType( typeDecl->get_base(), typeDecl->get_name() );
198 }
199
200 void CodeGenerator::visit( TypeDecl *typeDecl ) {
201 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
202 // still to be done
203 output << "extern unsigned long " << typeDecl->get_name();
204 if ( typeDecl->get_base() ) {
205 output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
206 } // if
207 }
208
209 void CodeGenerator::printDesignators( std::list< Expression * > & designators ) {
210 typedef std::list< Expression * > DesignatorList;
211 if ( designators.size() == 0 ) return;
212 for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) {
213 if ( dynamic_cast< NameExpr * >( *iter ) ) {
214 // if expression is a name, then initializing aggregate member
215 output << ".";
216 (*iter)->accept( *this );
217 } else {
218 // if not a simple name, it has to be a constant expression, i.e. an array designator
219 output << "[";
220 (*iter)->accept( *this );
221 output << "]";
222 }
223 }
224 output << " = ";
225 }
226
227 void CodeGenerator::visit( SingleInit *init ) {
228 printDesignators( init->get_designators() );
229 init->get_value()->accept( *this );
230 }
231
232 void CodeGenerator::visit( ListInit *init ) {
233 printDesignators( init->get_designators() );
234 output << "{ ";
235 genCommaList( init->begin_initializers(), init->end_initializers() );
236 output << " }";
237 }
238
239 void CodeGenerator::visit( Constant *constant ) {
240 output << constant->get_value() ;
241 }
242
243 //*** Expressions
244 void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
245 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
246 OperatorInfo opInfo;
247 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
248 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
249 switch ( opInfo.type ) {
250 case OT_PREFIXASSIGN:
251 case OT_POSTFIXASSIGN:
252 case OT_INFIXASSIGN:
253 {
254 assert( arg != applicationExpr->get_args().end() );
255 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
256
257 *arg = addrExpr->get_arg();
258 } else {
259 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
260 newExpr->get_args().push_back( *arg );
261 *arg = newExpr;
262 } // if
263 break;
264 }
265
266 default:
267 // do nothing
268 ;
269 }
270
271 switch ( opInfo.type ) {
272 case OT_INDEX:
273 assert( applicationExpr->get_args().size() == 2 );
274 (*arg++)->accept( *this );
275 output << "[";
276 (*arg)->accept( *this );
277 output << "]";
278 break;
279
280 case OT_CALL:
281 // there are no intrinsic definitions of the function call operator
282 assert( false );
283 break;
284
285 case OT_PREFIX:
286 case OT_PREFIXASSIGN:
287 assert( applicationExpr->get_args().size() == 1 );
288 output << "(";
289 output << opInfo.symbol;
290 (*arg)->accept( *this );
291 output << ")";
292 break;
293
294 case OT_POSTFIX:
295 case OT_POSTFIXASSIGN:
296 assert( applicationExpr->get_args().size() == 1 );
297 (*arg)->accept( *this );
298 output << opInfo.symbol;
299 break;
300
301 case OT_INFIX:
302 case OT_INFIXASSIGN:
303 assert( applicationExpr->get_args().size() == 2 );
304 output << "(";
305 (*arg++)->accept( *this );
306 output << opInfo.symbol;
307 (*arg)->accept( *this );
308 output << ")";
309 break;
310
311 case OT_CONSTANT:
312 case OT_LABELADDRESS:
313 // there are no intrinsic definitions of 0/1 or label addresses as functions
314 assert( false );
315 }
316 } else {
317 varExpr->accept( *this );
318 output << "(";
319 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
320 output << ")";
321 } // if
322 } else {
323 applicationExpr->get_function()->accept( *this );
324 output << "(";
325 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
326 output << ")";
327 } // if
328 }
329
330 void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
331 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
332 OperatorInfo opInfo;
333 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
334 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
335 switch ( opInfo.type ) {
336 case OT_INDEX:
337 assert( untypedExpr->get_args().size() == 2 );
338 (*arg++)->accept( *this );
339 output << "[";
340 (*arg)->accept( *this );
341 output << "]";
342 break;
343
344 case OT_CALL:
345 assert( false );
346 break;
347
348 case OT_PREFIX:
349 case OT_PREFIXASSIGN:
350 case OT_LABELADDRESS:
351 assert( untypedExpr->get_args().size() == 1 );
352 output << "(";
353 output << opInfo.symbol;
354 (*arg)->accept( *this );
355 output << ")";
356 break;
357
358 case OT_POSTFIX:
359 case OT_POSTFIXASSIGN:
360 assert( untypedExpr->get_args().size() == 1 );
361 (*arg)->accept( *this );
362 output << opInfo.symbol;
363 break;
364
365 case OT_INFIX:
366 case OT_INFIXASSIGN:
367 assert( untypedExpr->get_args().size() == 2 );
368 output << "(";
369 (*arg++)->accept( *this );
370 output << opInfo.symbol;
371 (*arg)->accept( *this );
372 output << ")";
373 break;
374
375 case OT_CONSTANT:
376 // there are no intrinsic definitions of 0 or 1 as functions
377 assert( false );
378 }
379 } else {
380 nameExpr->accept( *this );
381 output << "(";
382 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
383 output << ")";
384 } // if
385 } else {
386 untypedExpr->get_function()->accept( *this );
387 output << "(";
388 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
389 output << ")";
390 } // if
391 }
392
393 void CodeGenerator::visit( NameExpr *nameExpr ) {
394 OperatorInfo opInfo;
395 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
396 assert( opInfo.type == OT_CONSTANT );
397 output << opInfo.symbol;
398 } else {
399 output << nameExpr->get_name();
400 } // if
401 }
402
403 void CodeGenerator::visit( AddressExpr *addressExpr ) {
404 output << "(&";
405 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
406 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
407 output << mangleName( variableExpr->get_var() );
408 } else {
409 addressExpr->get_arg()->accept( *this );
410 } // if
411 output << ")";
412 }
413
414 void CodeGenerator::visit( CastExpr *castExpr ) {
415 output << "(";
416 if ( castExpr->get_results().empty() ) {
417 output << "(void)" ;
418 } else if ( ! castExpr->get_results().front()->get_isLvalue() ) {
419 // at least one result type of cast, but not an lvalue
420 output << "(";
421 output << genType( castExpr->get_results().front(), "" );
422 output << ")";
423 } else {
424 // otherwise, the cast is to an lvalue type, so the cast
425 // should be dropped, since the result of a cast is
426 // never an lvalue in C
427 }
428 castExpr->get_arg()->accept( *this );
429 output << ")";
430 }
431
432 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
433 assert( false );
434 }
435
436 void CodeGenerator::visit( MemberExpr *memberExpr ) {
437 memberExpr->get_aggregate()->accept( *this );
438 output << "." << mangleName( memberExpr->get_member() );
439 }
440
441 void CodeGenerator::visit( VariableExpr *variableExpr ) {
442 OperatorInfo opInfo;
443 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
444 output << opInfo.symbol;
445 } else {
446 output << mangleName( variableExpr->get_var() );
447 } // if
448 }
449
450 void CodeGenerator::visit( ConstantExpr *constantExpr ) {
451 assert( constantExpr->get_constant() );
452 constantExpr->get_constant()->accept( *this );
453 }
454
455 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
456 output << "sizeof(";
457 if ( sizeofExpr->get_isType() ) {
458 output << genType( sizeofExpr->get_type(), "" );
459 } else {
460 sizeofExpr->get_expr()->accept( *this );
461 } // if
462 output << ")";
463 }
464
465 void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
466 // use GCC extension to avoid bumping std to C11
467 output << "__alignof__(";
468 if ( alignofExpr->get_isType() ) {
469 output << genType( alignofExpr->get_type(), "" );
470 } else {
471 alignofExpr->get_expr()->accept( *this );
472 } // if
473 output << ")";
474 }
475
476 void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
477 assert( false && "UntypedOffsetofExpr should not reach code generation" );
478 }
479
480 void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
481 // use GCC builtin
482 output << "__builtin_offsetof(";
483 output << genType( offsetofExpr->get_type(), "" );
484 output << ", " << mangleName( offsetofExpr->get_member() );
485 output << ")";
486 }
487
488 void CodeGenerator::visit( OffsetPackExpr *offsetPackExpr ) {
489 assert( false && "OffsetPackExpr should not reach code generation" );
490 }
491
492 void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
493 output << "(";
494 logicalExpr->get_arg1()->accept( *this );
495 if ( logicalExpr->get_isAnd() ) {
496 output << " && ";
497 } else {
498 output << " || ";
499 } // if
500 logicalExpr->get_arg2()->accept( *this );
501 output << ")";
502 }
503
504 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
505 output << "(";
506 conditionalExpr->get_arg1()->accept( *this );
507 output << " ? ";
508 conditionalExpr->get_arg2()->accept( *this );
509 output << " : ";
510 conditionalExpr->get_arg3()->accept( *this );
511 output << ")";
512 }
513
514 void CodeGenerator::visit( CommaExpr *commaExpr ) {
515 output << "(";
516 commaExpr->get_arg1()->accept( *this );
517 output << " , ";
518 commaExpr->get_arg2()->accept( *this );
519 output << ")";
520 }
521
522 void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
523
524 void CodeGenerator::visit( TypeExpr *typeExpr ) {}
525
526 void CodeGenerator::visit( AsmExpr *asmExpr ) {
527 if ( asmExpr->get_inout() ) {
528 output << "[ ";
529 asmExpr->get_inout()->accept( *this );
530 output << " ] ";
531 } // if
532 asmExpr->get_constraint()->accept( *this );
533 output << " ( ";
534 asmExpr->get_operand()->accept( *this );
535 output << " )";
536 }
537
538 //*** Statements
539 void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
540 std::list<Statement*> ks = compoundStmt->get_kids();
541 output << "{" << endl;
542
543 cur_indent += CodeGenerator::tabsize;
544
545 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
546 output << indent << printLabels( (*i)->get_labels() );
547 (*i)->accept( *this );
548
549 output << endl;
550 if ( wantSpacing( *i ) ) {
551 output << endl;
552 }
553 }
554 cur_indent -= CodeGenerator::tabsize;
555
556 output << indent << "}";
557 }
558
559 void CodeGenerator::visit( ExprStmt *exprStmt ) {
560 // I don't see why this check is necessary.
561 // If this starts to cause problems then put it back in,
562 // with an explanation
563 assert( exprStmt );
564
565 // if ( exprStmt != 0 ) {
566 exprStmt->get_expr()->accept( *this );
567 output << ";" ;
568 // } // if
569 }
570
571 void CodeGenerator::visit( AsmStmt *asmStmt ) {
572 output << "asm ";
573 if ( asmStmt->get_voltile() ) output << "volatile ";
574 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
575 output << "( ";
576 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
577 output << " : ";
578 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
579 output << " : ";
580 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
581 output << " : ";
582 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
583 if ( ! asmStmt->get_gotolabels().empty() ) {
584 output << " : ";
585 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
586 output << *begin++;
587 if ( begin == asmStmt->get_gotolabels().end() ) break;
588 output << ", ";
589 } // for
590 } // if
591 output << " );" ;
592 }
593
594 void CodeGenerator::visit( IfStmt *ifStmt ) {
595 output << "if ( ";
596 ifStmt->get_condition()->accept( *this );
597 output << " ) ";
598
599 ifStmt->get_thenPart()->accept( *this );
600
601 if ( ifStmt->get_elsePart() != 0) {
602 output << " else ";
603 ifStmt->get_elsePart()->accept( *this );
604 } // if
605 }
606
607 void CodeGenerator::visit( SwitchStmt *switchStmt ) {
608 output << "switch ( " ;
609 switchStmt->get_condition()->accept( *this );
610 output << " ) ";
611
612 output << "{" << std::endl;
613 cur_indent += CodeGenerator::tabsize;
614
615 acceptAll( switchStmt->get_branches(), *this );
616
617 cur_indent -= CodeGenerator::tabsize;
618
619 output << indent << "}";
620 }
621
622 void CodeGenerator::visit( CaseStmt *caseStmt ) {
623 output << indent;
624 if ( caseStmt->isDefault()) {
625 output << "default";
626 } else {
627 output << "case ";
628 caseStmt->get_condition()->accept( *this );
629 } // if
630 output << ":\n";
631
632 std::list<Statement *> sts = caseStmt->get_statements();
633
634 cur_indent += CodeGenerator::tabsize;
635 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
636 output << indent << printLabels( (*i)->get_labels() ) ;
637 (*i)->accept( *this );
638 output << endl;
639 }
640 cur_indent -= CodeGenerator::tabsize;
641 }
642
643 void CodeGenerator::visit( BranchStmt *branchStmt ) {
644 switch ( branchStmt->get_type()) {
645 case BranchStmt::Goto:
646 if ( ! branchStmt->get_target().empty() )
647 output << "goto " << branchStmt->get_target();
648 else {
649 if ( branchStmt->get_computedTarget() != 0 ) {
650 output << "goto *";
651 branchStmt->get_computedTarget()->accept( *this );
652 } // if
653 } // if
654 break;
655 case BranchStmt::Break:
656 output << "break";
657 break;
658 case BranchStmt::Continue:
659 output << "continue";
660 break;
661 }
662 output << ";";
663 }
664
665
666 void CodeGenerator::visit( ReturnStmt *returnStmt ) {
667 output << "return ";
668
669 // xxx -- check for null expression;
670 if ( returnStmt->get_expr() ) {
671 returnStmt->get_expr()->accept( *this );
672 } // if
673 output << ";";
674 }
675
676 void CodeGenerator::visit( WhileStmt *whileStmt ) {
677 if ( whileStmt->get_isDoWhile() )
678 output << "do" ;
679 else {
680 output << "while (" ;
681 whileStmt->get_condition()->accept( *this );
682 output << ")";
683 } // if
684 output << " ";
685
686 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
687 whileStmt->get_body()->accept( *this );
688
689 output << indent;
690
691 if ( whileStmt->get_isDoWhile() ) {
692 output << " while (" ;
693 whileStmt->get_condition()->accept( *this );
694 output << ");";
695 } // if
696 }
697
698 void CodeGenerator::visit( ForStmt *forStmt ) {
699 // initialization is always hoisted, so don't
700 // bother doing anything with that
701 output << "for (;";
702
703 if ( forStmt->get_condition() != 0 )
704 forStmt->get_condition()->accept( *this );
705 output << ";";
706
707 if ( forStmt->get_increment() != 0 )
708 forStmt->get_increment()->accept( *this );
709 output << ") ";
710
711 if ( forStmt->get_body() != 0 ) {
712 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
713 forStmt->get_body()->accept( *this );
714 } // if
715 }
716
717 void CodeGenerator::visit( NullStmt *nullStmt ) {
718 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
719 output << "/* null statement */ ;";
720 }
721
722 void CodeGenerator::visit( DeclStmt *declStmt ) {
723 declStmt->get_decl()->accept( *this );
724
725 if ( doSemicolon( declStmt->get_decl() ) ) {
726 output << ";";
727 } // if
728 }
729
730 std::string CodeGenerator::printLabels( std::list< Label > &l ) {
731 std::string str( "" );
732 l.unique(); // assumes a sorted list. Why not use set?
733
734 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
735 str += *i + ": ";
736
737 return str;
738 }
739
740 void CodeGenerator::handleStorageClass( Declaration *decl ) {
741 switch ( decl->get_storageClass() ) {
742 case DeclarationNode::Extern:
743 output << "extern ";
744 break;
745 case DeclarationNode::Static:
746 output << "static ";
747 break;
748 case DeclarationNode::Auto:
749 // silently drop storage class
750 break;
751 case DeclarationNode::Register:
752 output << "register ";
753 break;
754 case DeclarationNode::Inline:
755 output << "inline ";
756 break;
757 case DeclarationNode::Fortran:
758 output << "fortran ";
759 break;
760 case DeclarationNode::Noreturn:
761 output << "_Noreturn ";
762 break;
763 case DeclarationNode::Threadlocal:
764 output << "_Thread_local ";
765 break;
766 case DeclarationNode::NoStorageClass:
767 break;
768 } // switch
769 }
770} // namespace CodeGen
771
772// Local Variables: //
773// tab-width: 4 //
774// mode: c++ //
775// compile-command: "make install" //
776// End: //
Note: See TracBrowser for help on using the repository browser.