source: src/CodeGen/CodeGenerator.cc@ dc2e7e0

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

output intrinsic constructor expressions as void expression or C assignment, don't generate field constructor which takes unnamed bitfield as a parameter

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