source: src/CodeGen/CodeGenerator.cc@ e365cb5

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

can use intrinsic constructors on const objects

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