source: src/CodeGen/CodeGenerator.cc@ 2f6b7c9

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

generalize notion of a GCC attribute

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