source: src/CodeGen/CodeGenerator.cc@ 70a06f6

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 stuck-waitfor-destruct with_gc
Last change on this file since 70a06f6 was 70a06f6, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

Merge branch 'master' into ctor

Conflicts:

src/CodeGen/CodeGenerator.cc
src/GenPoly/Box.cc
src/Parser/DeclarationNode.cc
src/Parser/ParseNode.h
src/Parser/parser.cc
src/Parser/parser.yy
src/SymTab/AddVisit.h
src/SymTab/Validate.cc
src/SynTree/Expression.cc
src/SynTree/Expression.h
src/SynTree/Mutator.cc
src/SynTree/Mutator.h
src/SynTree/SynTree.h
src/SynTree/Visitor.cc
src/SynTree/Visitor.h
src/libcfa/iostream.c

  • Property mode set to 100644
File size: 21.9 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 15:40:00 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 {
241 assert( arg != applicationExpr->get_args().end() );
242 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
243
244 *arg = addrExpr->get_arg();
245 } else {
246 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
247 newExpr->get_args().push_back( *arg );
248 *arg = newExpr;
249 } // if
250 break;
251 }
252
253 default:
254 // do nothing
255 ;
256 }
257
258 switch ( opInfo.type ) {
259 case OT_INDEX:
260 assert( applicationExpr->get_args().size() == 2 );
261 (*arg++)->accept( *this );
262 output << "[";
263 (*arg)->accept( *this );
264 output << "]";
265 break;
266
267 case OT_CALL:
268 // there are no intrinsic definitions of the function call operator or constructors or destructors
269 assert( false );
270 break;
271
272 case OT_CTOR:
273 // it's just an optimization to disallow this, so for now let it through
274 // since it makes autogenerating constructors a lot easier
275 varExpr->accept( *this );
276 output << "(";
277 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
278 output << ")";
279
280 // intrinsic constructors should never be called directly - they should be transformed back into Initializer nodes
281 // assert(false);
282 break;
283
284 case OT_DTOR:
285 // intrinsic destructors do nothing - don't generate any code
286 output << " /* " << dynamic_cast<VariableExpr*>(applicationExpr->get_function())->get_var()->get_name() << " */";
287 break;
288
289 case OT_PREFIX:
290 case OT_PREFIXASSIGN:
291 assert( applicationExpr->get_args().size() == 1 );
292 output << "(";
293 output << opInfo.symbol;
294 (*arg)->accept( *this );
295 output << ")";
296 break;
297
298 case OT_POSTFIX:
299 case OT_POSTFIXASSIGN:
300 assert( applicationExpr->get_args().size() == 1 );
301 (*arg)->accept( *this );
302 output << opInfo.symbol;
303 break;
304
305
306 case OT_INFIX:
307 case OT_INFIXASSIGN:
308 assert( applicationExpr->get_args().size() == 2 );
309 output << "(";
310 (*arg++)->accept( *this );
311 output << opInfo.symbol;
312 (*arg)->accept( *this );
313 output << ")";
314 break;
315
316 case OT_CONSTANT:
317 case OT_LABELADDRESS:
318 // there are no intrinsic definitions of 0/1 or label addresses as functions
319 assert( false );
320 }
321 } else {
322 varExpr->accept( *this );
323 output << "(";
324 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
325 output << ")";
326 } // if
327 } else {
328 applicationExpr->get_function()->accept( *this );
329 output << "(";
330 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
331 output << ")";
332 } // if
333 }
334
335 void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
336 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
337 OperatorInfo opInfo;
338 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
339 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
340 switch ( opInfo.type ) {
341 case OT_INDEX:
342 assert( untypedExpr->get_args().size() == 2 );
343 (*arg++)->accept( *this );
344 output << "[";
345 (*arg)->accept( *this );
346 output << "]";
347 break;
348
349 case OT_CALL:
350 assert( false );
351
352 case OT_CTOR:
353 case OT_DTOR:
354 // intrinsic constructors should never be called
355 // intrinsic destructors do nothing
356 break;
357
358 case OT_PREFIX:
359 case OT_PREFIXASSIGN:
360 case OT_LABELADDRESS:
361 assert( untypedExpr->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( untypedExpr->get_args().size() == 1 );
371 (*arg)->accept( *this );
372 output << opInfo.symbol;
373 break;
374
375 case OT_INFIX:
376 case OT_INFIXASSIGN:
377 assert( untypedExpr->get_args().size() == 2 );
378 output << "(";
379 (*arg++)->accept( *this );
380 output << opInfo.symbol;
381 (*arg)->accept( *this );
382 output << ")";
383 break;
384
385 case OT_CONSTANT:
386 // there are no intrinsic definitions of 0 or 1 as functions
387 assert( false );
388 }
389 } else {
390 nameExpr->accept( *this );
391 output << "(";
392 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
393 output << ")";
394 } // if
395 } else {
396 untypedExpr->get_function()->accept( *this );
397 output << "(";
398 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
399 output << ")";
400 } // if
401 }
402
403 void CodeGenerator::visit( NameExpr *nameExpr ) {
404 OperatorInfo opInfo;
405 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
406 assert( opInfo.type == OT_CONSTANT );
407 output << opInfo.symbol;
408 } else {
409 output << nameExpr->get_name();
410 } // if
411 }
412
413 void CodeGenerator::visit( AddressExpr *addressExpr ) {
414 output << "(&";
415 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
416 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
417 output << mangleName( variableExpr->get_var() );
418 } else {
419 addressExpr->get_arg()->accept( *this );
420 } // if
421 output << ")";
422 }
423
424 void CodeGenerator::visit( CastExpr *castExpr ) {
425 output << "(";
426 if ( castExpr->get_results().empty() ) {
427 output << "(void)" ;
428 } else if ( ! castExpr->get_results().front()->get_isLvalue() ) {
429 // at least one result type of cast, but not an lvalue
430 output << "(";
431 output << genType( castExpr->get_results().front(), "" );
432 output << ")";
433 } else {
434 // otherwise, the cast is to an lvalue type, so the cast
435 // should be dropped, since the result of a cast is
436 // never an lvalue in C
437 }
438 castExpr->get_arg()->accept( *this );
439 output << ")";
440 }
441
442 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
443 assert( false );
444 }
445
446 void CodeGenerator::visit( MemberExpr *memberExpr ) {
447 memberExpr->get_aggregate()->accept( *this );
448 output << "." << mangleName( memberExpr->get_member() );
449 }
450
451 void CodeGenerator::visit( VariableExpr *variableExpr ) {
452 OperatorInfo opInfo;
453 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
454 output << opInfo.symbol;
455 } else {
456 output << mangleName( variableExpr->get_var() );
457 } // if
458 }
459
460 void CodeGenerator::visit( ConstantExpr *constantExpr ) {
461 assert( constantExpr->get_constant() );
462 constantExpr->get_constant()->accept( *this );
463 }
464
465 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
466 output << "sizeof(";
467 if ( sizeofExpr->get_isType() ) {
468 output << genType( sizeofExpr->get_type(), "" );
469 } else {
470 sizeofExpr->get_expr()->accept( *this );
471 } // if
472 output << ")";
473 }
474
475 void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
476 // use GCC extension to avoid bumping std to C11
477 output << "__alignof__(";
478 if ( alignofExpr->get_isType() ) {
479 output << genType( alignofExpr->get_type(), "" );
480 } else {
481 alignofExpr->get_expr()->accept( *this );
482 } // if
483 output << ")";
484 }
485
486 void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
487 assert( false && "UntypedOffsetofExpr should not reach code generation" );
488 }
489
490 void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
491 // use GCC builtin
492 output << "__builtin_offsetof(";
493 output << genType( offsetofExpr->get_type(), "" );
494 output << ", " << mangleName( offsetofExpr->get_member() );
495 output << ")";
496 }
497
498 void CodeGenerator::visit( OffsetPackExpr *offsetPackExpr ) {
499 assert( false && "OffsetPackExpr should not reach code generation" );
500 }
501
502 void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
503 output << "(";
504 logicalExpr->get_arg1()->accept( *this );
505 if ( logicalExpr->get_isAnd() ) {
506 output << " && ";
507 } else {
508 output << " || ";
509 } // if
510 logicalExpr->get_arg2()->accept( *this );
511 output << ")";
512 }
513
514 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
515 output << "(";
516 conditionalExpr->get_arg1()->accept( *this );
517 output << " ? ";
518 conditionalExpr->get_arg2()->accept( *this );
519 output << " : ";
520 conditionalExpr->get_arg3()->accept( *this );
521 output << ")";
522 }
523
524 void CodeGenerator::visit( CommaExpr *commaExpr ) {
525 output << "(";
526 commaExpr->get_arg1()->accept( *this );
527 output << " , ";
528 commaExpr->get_arg2()->accept( *this );
529 output << ")";
530 }
531
532 void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
533
534 void CodeGenerator::visit( TypeExpr *typeExpr ) {}
535
536 void CodeGenerator::visit( AsmExpr *asmExpr ) {
537 if ( asmExpr->get_inout() ) {
538 output << "[ ";
539 asmExpr->get_inout()->accept( *this );
540 output << " ] ";
541 } // if
542 asmExpr->get_constraint()->accept( *this );
543 output << " ( ";
544 asmExpr->get_operand()->accept( *this );
545 output << " )";
546 }
547
548 //*** Statements
549 void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
550 std::list<Statement*> ks = compoundStmt->get_kids();
551 output << "{" << endl;
552
553 cur_indent += CodeGenerator::tabsize;
554
555 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
556 output << indent << printLabels( (*i)->get_labels() );
557 (*i)->accept( *this );
558
559 output << endl;
560 if ( wantSpacing( *i ) ) {
561 output << endl;
562 }
563 }
564 cur_indent -= CodeGenerator::tabsize;
565
566 output << indent << "}";
567 }
568
569 void CodeGenerator::visit( ExprStmt *exprStmt ) {
570 // I don't see why this check is necessary.
571 // If this starts to cause problems then put it back in,
572 // with an explanation
573 assert( exprStmt );
574
575 // if ( exprStmt != 0 ) {
576 exprStmt->get_expr()->accept( *this );
577 output << ";" ;
578 // } // if
579 }
580
581 void CodeGenerator::visit( AsmStmt *asmStmt ) {
582 output << "asm ";
583 if ( asmStmt->get_voltile() ) output << "volatile ";
584 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
585 output << "( ";
586 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
587 output << " : ";
588 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
589 output << " : ";
590 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
591 output << " : ";
592 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
593 if ( ! asmStmt->get_gotolabels().empty() ) {
594 output << " : ";
595 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
596 output << *begin++;
597 if ( begin == asmStmt->get_gotolabels().end() ) break;
598 output << ", ";
599 } // for
600 } // if
601 output << " );" ;
602 }
603
604 void CodeGenerator::visit( IfStmt *ifStmt ) {
605 output << "if ( ";
606 ifStmt->get_condition()->accept( *this );
607 output << " ) ";
608
609 ifStmt->get_thenPart()->accept( *this );
610
611 if ( ifStmt->get_elsePart() != 0) {
612 output << " else ";
613 ifStmt->get_elsePart()->accept( *this );
614 } // if
615 }
616
617 void CodeGenerator::visit( SwitchStmt *switchStmt ) {
618 output << "switch ( " ;
619 switchStmt->get_condition()->accept( *this );
620 output << " ) ";
621
622 output << "{" << std::endl;
623 cur_indent += CodeGenerator::tabsize;
624
625 acceptAll( switchStmt->get_branches(), *this );
626
627 cur_indent -= CodeGenerator::tabsize;
628
629 output << indent << "}";
630 }
631
632 void CodeGenerator::visit( CaseStmt *caseStmt ) {
633 output << indent;
634 if ( caseStmt->isDefault()) {
635 output << "default";
636 } else {
637 output << "case ";
638 caseStmt->get_condition()->accept( *this );
639 } // if
640 output << ":\n";
641
642 std::list<Statement *> sts = caseStmt->get_statements();
643
644 cur_indent += CodeGenerator::tabsize;
645 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
646 output << indent << printLabels( (*i)->get_labels() ) ;
647 (*i)->accept( *this );
648 output << endl;
649 }
650 cur_indent -= CodeGenerator::tabsize;
651 }
652
653 void CodeGenerator::visit( BranchStmt *branchStmt ) {
654 switch ( branchStmt->get_type()) {
655 case BranchStmt::Goto:
656 if ( ! branchStmt->get_target().empty() )
657 output << "goto " << branchStmt->get_target();
658 else {
659 if ( branchStmt->get_computedTarget() != 0 ) {
660 output << "goto *";
661 branchStmt->get_computedTarget()->accept( *this );
662 } // if
663 } // if
664 break;
665 case BranchStmt::Break:
666 output << "break";
667 break;
668 case BranchStmt::Continue:
669 output << "continue";
670 break;
671 }
672 output << ";";
673 }
674
675
676 void CodeGenerator::visit( ReturnStmt *returnStmt ) {
677 output << "return ";
678
679 // xxx -- check for null expression;
680 if ( returnStmt->get_expr() ) {
681 returnStmt->get_expr()->accept( *this );
682 } // if
683 output << ";";
684 }
685
686 void CodeGenerator::visit( WhileStmt *whileStmt ) {
687 if ( whileStmt->get_isDoWhile() )
688 output << "do" ;
689 else {
690 output << "while (" ;
691 whileStmt->get_condition()->accept( *this );
692 output << ")";
693 } // if
694 output << " ";
695
696 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
697 whileStmt->get_body()->accept( *this );
698
699 output << indent;
700
701 if ( whileStmt->get_isDoWhile() ) {
702 output << " while (" ;
703 whileStmt->get_condition()->accept( *this );
704 output << ");";
705 } // if
706 }
707
708 void CodeGenerator::visit( ForStmt *forStmt ) {
709 // initialization is always hoisted, so don't
710 // bother doing anything with that
711 output << "for (;";
712
713 if ( forStmt->get_condition() != 0 )
714 forStmt->get_condition()->accept( *this );
715 output << ";";
716
717 if ( forStmt->get_increment() != 0 )
718 forStmt->get_increment()->accept( *this );
719 output << ") ";
720
721 if ( forStmt->get_body() != 0 ) {
722 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
723 forStmt->get_body()->accept( *this );
724 } // if
725 }
726
727 void CodeGenerator::visit( NullStmt *nullStmt ) {
728 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
729 output << "/* null statement */ ;";
730 }
731
732 void CodeGenerator::visit( DeclStmt *declStmt ) {
733 declStmt->get_decl()->accept( *this );
734
735 if ( doSemicolon( declStmt->get_decl() ) ) {
736 output << ";";
737 } // if
738 }
739
740 std::string CodeGenerator::printLabels( std::list< Label > &l ) {
741 std::string str( "" );
742 l.unique(); // assumes a sorted list. Why not use set?
743
744 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
745 str += *i + ": ";
746
747 return str;
748 }
749
750 void CodeGenerator::handleStorageClass( Declaration *decl ) {
751 switch ( decl->get_storageClass() ) {
752 case DeclarationNode::Extern:
753 output << "extern ";
754 break;
755 case DeclarationNode::Static:
756 output << "static ";
757 break;
758 case DeclarationNode::Auto:
759 // silently drop storage class
760 break;
761 case DeclarationNode::Register:
762 output << "register ";
763 break;
764 case DeclarationNode::Inline:
765 output << "inline ";
766 break;
767 case DeclarationNode::Fortran:
768 output << "fortran ";
769 break;
770 case DeclarationNode::Noreturn:
771 output << "_Noreturn ";
772 break;
773 case DeclarationNode::Threadlocal:
774 output << "_Thread_local ";
775 break;
776 case DeclarationNode::NoStorageClass:
777 break;
778 } // switch
779 }
780} // namespace CodeGen
781
782// Local Variables: //
783// tab-width: 4 //
784// mode: c++ //
785// compile-command: "make install" //
786// End: //
Note: See TracBrowser for help on using the repository browser.