source: src/CodeGen/CodeGenerator.cc@ 7eabc25

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

add integer suffix to mangled names so that shadowed variables are accessable (needed for destructors to work correctly)

  • Property mode set to 100644
File size: 21.7 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 : Mon Apr 11 15:30:52 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 );
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( LogicalExpr *logicalExpr ) {
499 output << "(";
500 logicalExpr->get_arg1()->accept( *this );
501 if ( logicalExpr->get_isAnd() ) {
502 output << " && ";
503 } else {
504 output << " || ";
505 } // if
506 logicalExpr->get_arg2()->accept( *this );
507 output << ")";
508 }
509
510 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
511 output << "(";
512 conditionalExpr->get_arg1()->accept( *this );
513 output << " ? ";
514 conditionalExpr->get_arg2()->accept( *this );
515 output << " : ";
516 conditionalExpr->get_arg3()->accept( *this );
517 output << ")";
518 }
519
520 void CodeGenerator::visit( CommaExpr *commaExpr ) {
521 output << "(";
522 commaExpr->get_arg1()->accept( *this );
523 output << " , ";
524 commaExpr->get_arg2()->accept( *this );
525 output << ")";
526 }
527
528 void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
529
530 void CodeGenerator::visit( TypeExpr *typeExpr ) {}
531
532 void CodeGenerator::visit( AsmExpr *asmExpr ) {
533 if ( asmExpr->get_inout() ) {
534 output << "[ ";
535 asmExpr->get_inout()->accept( *this );
536 output << " ] ";
537 } // if
538 asmExpr->get_constraint()->accept( *this );
539 output << " ( ";
540 asmExpr->get_operand()->accept( *this );
541 output << " )";
542 }
543
544 //*** Statements
545 void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
546 std::list<Statement*> ks = compoundStmt->get_kids();
547 output << "{" << endl;
548
549 cur_indent += CodeGenerator::tabsize;
550
551 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
552 output << indent << printLabels( (*i)->get_labels() );
553 (*i)->accept( *this );
554
555 output << endl;
556 if ( wantSpacing( *i ) ) {
557 output << endl;
558 }
559 }
560 cur_indent -= CodeGenerator::tabsize;
561
562 output << indent << "}";
563 }
564
565 void CodeGenerator::visit( ExprStmt *exprStmt ) {
566 // I don't see why this check is necessary.
567 // If this starts to cause problems then put it back in,
568 // with an explanation
569 assert( exprStmt );
570
571 // if ( exprStmt != 0 ) {
572 exprStmt->get_expr()->accept( *this );
573 output << ";" ;
574 // } // if
575 }
576
577 void CodeGenerator::visit( AsmStmt *asmStmt ) {
578 output << "asm ";
579 if ( asmStmt->get_voltile() ) output << "volatile ";
580 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
581 output << "( ";
582 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
583 output << " : ";
584 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
585 output << " : ";
586 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
587 output << " : ";
588 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
589 if ( ! asmStmt->get_gotolabels().empty() ) {
590 output << " : ";
591 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
592 output << *begin++;
593 if ( begin == asmStmt->get_gotolabels().end() ) break;
594 output << ", ";
595 } // for
596 } // if
597 output << " );" ;
598 }
599
600 void CodeGenerator::visit( IfStmt *ifStmt ) {
601 output << "if ( ";
602 ifStmt->get_condition()->accept( *this );
603 output << " ) ";
604
605 ifStmt->get_thenPart()->accept( *this );
606
607 if ( ifStmt->get_elsePart() != 0) {
608 output << " else ";
609 ifStmt->get_elsePart()->accept( *this );
610 } // if
611 }
612
613 void CodeGenerator::visit( SwitchStmt *switchStmt ) {
614 output << "switch ( " ;
615 switchStmt->get_condition()->accept( *this );
616 output << " ) ";
617
618 output << "{" << std::endl;
619 cur_indent += CodeGenerator::tabsize;
620
621 acceptAll( switchStmt->get_branches(), *this );
622
623 cur_indent -= CodeGenerator::tabsize;
624
625 output << indent << "}";
626 }
627
628 void CodeGenerator::visit( CaseStmt *caseStmt ) {
629 output << indent;
630 if ( caseStmt->isDefault()) {
631 output << "default";
632 } else {
633 output << "case ";
634 caseStmt->get_condition()->accept( *this );
635 } // if
636 output << ":\n";
637
638 std::list<Statement *> sts = caseStmt->get_statements();
639
640 cur_indent += CodeGenerator::tabsize;
641 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
642 output << indent << printLabels( (*i)->get_labels() ) ;
643 (*i)->accept( *this );
644 output << endl;
645 }
646 cur_indent -= CodeGenerator::tabsize;
647 }
648
649 void CodeGenerator::visit( BranchStmt *branchStmt ) {
650 switch ( branchStmt->get_type()) {
651 case BranchStmt::Goto:
652 if ( ! branchStmt->get_target().empty() )
653 output << "goto " << branchStmt->get_target();
654 else {
655 if ( branchStmt->get_computedTarget() != 0 ) {
656 output << "goto *";
657 branchStmt->get_computedTarget()->accept( *this );
658 } // if
659 } // if
660 break;
661 case BranchStmt::Break:
662 output << "break";
663 break;
664 case BranchStmt::Continue:
665 output << "continue";
666 break;
667 }
668 output << ";";
669 }
670
671
672 void CodeGenerator::visit( ReturnStmt *returnStmt ) {
673 output << "return ";
674
675 // xxx -- check for null expression;
676 if ( returnStmt->get_expr() ) {
677 returnStmt->get_expr()->accept( *this );
678 } // if
679 output << ";";
680 }
681
682 void CodeGenerator::visit( WhileStmt *whileStmt ) {
683 if ( whileStmt->get_isDoWhile() )
684 output << "do" ;
685 else {
686 output << "while (" ;
687 whileStmt->get_condition()->accept( *this );
688 output << ")";
689 } // if
690 output << " ";
691
692 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
693 whileStmt->get_body()->accept( *this );
694
695 output << indent;
696
697 if ( whileStmt->get_isDoWhile() ) {
698 output << " while (" ;
699 whileStmt->get_condition()->accept( *this );
700 output << ");";
701 } // if
702 }
703
704 void CodeGenerator::visit( ForStmt *forStmt ) {
705 // initialization is always hoisted, so don't
706 // bother doing anything with that
707 output << "for (;";
708
709 if ( forStmt->get_condition() != 0 )
710 forStmt->get_condition()->accept( *this );
711 output << ";";
712
713 if ( forStmt->get_increment() != 0 )
714 forStmt->get_increment()->accept( *this );
715 output << ") ";
716
717 if ( forStmt->get_body() != 0 ) {
718 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
719 forStmt->get_body()->accept( *this );
720 } // if
721 }
722
723 void CodeGenerator::visit( NullStmt *nullStmt ) {
724 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
725 output << "/* null statement */ ;";
726 }
727
728 void CodeGenerator::visit( DeclStmt *declStmt ) {
729 declStmt->get_decl()->accept( *this );
730
731 if ( doSemicolon( declStmt->get_decl() ) ) {
732 output << ";";
733 } // if
734 }
735
736 std::string CodeGenerator::printLabels( std::list< Label > &l ) {
737 std::string str( "" );
738 l.unique(); // assumes a sorted list. Why not use set?
739
740 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
741 str += *i + ": ";
742
743 return str;
744 }
745
746 void CodeGenerator::handleStorageClass( Declaration *decl ) {
747 switch ( decl->get_storageClass() ) {
748 case DeclarationNode::Extern:
749 output << "extern ";
750 break;
751 case DeclarationNode::Static:
752 output << "static ";
753 break;
754 case DeclarationNode::Auto:
755 // silently drop storage class
756 break;
757 case DeclarationNode::Register:
758 output << "register ";
759 break;
760 case DeclarationNode::Inline:
761 output << "inline ";
762 break;
763 case DeclarationNode::Fortran:
764 output << "fortran ";
765 break;
766 case DeclarationNode::Noreturn:
767 output << "_Noreturn ";
768 break;
769 case DeclarationNode::Threadlocal:
770 output << "_Thread_local ";
771 break;
772 case DeclarationNode::NoStorageClass:
773 break;
774 } // switch
775 }
776} // namespace CodeGen
777
778// Local Variables: //
779// tab-width: 4 //
780// mode: c++ //
781// compile-command: "make install" //
782// End: //
Note: See TracBrowser for help on using the repository browser.