source: src/CodeGen/CodeGenerator.cc@ 37efaf56

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox 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 37efaf56 was 58dd019, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

add asm_name clause to declarations

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