source: src/CodeGen/CodeGenerator.cc@ e33f321

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

Merge branch 'master' into tuples

Conflicts:

src/ResolvExpr/CommonType.cc
src/tests/.expect/32/extension.txt
src/tests/.expect/32/gccExtensions.txt
src/tests/.expect/64/declarationSpecifier.txt
src/tests/.expect/64/extension.txt
src/tests/.expect/64/gccExtensions.txt
src/tests/.expect/castError.txt
src/tests/Makefile.am

  • Property mode set to 100644
File size: 27.4 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( ConstructorInit * init ){
296 assertf( false, "ConstructorInit nodes should not make it to CodeGen." );
297 }
298
299 void CodeGenerator::visit( Constant * constant ) {
300 output << constant->get_value() ;
301 }
302
303 //*** Expressions
304 void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
305 extension( applicationExpr );
306 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
307 OperatorInfo opInfo;
308 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
309 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
310 switch ( opInfo.type ) {
311 case OT_PREFIXASSIGN:
312 case OT_POSTFIXASSIGN:
313 case OT_INFIXASSIGN:
314 case OT_CTOR:
315 case OT_DTOR:
316 {
317 assert( arg != applicationExpr->get_args().end() );
318 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
319 // remove & from first assignment/ctor argument
320 *arg = addrExpr->get_arg();
321 } else {
322 // no address-of operator, so must be a pointer - add dereference
323 // NOTE: if the assertion starts to trigger, check that the application expr isn't being shared.
324 // Since its arguments are modified here, this assertion most commonly triggers when the application
325 // is visited multiple times.
326 UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );
327 newExpr->get_args().push_back( *arg );
328 Type * type = InitTweak::getPointerBase( (*arg)->get_result() );
329 assertf( type, "First argument to a derefence must be a pointer. Ensure that expressions are not being shared." );
330 newExpr->set_result( type->clone() );
331 *arg = newExpr;
332 } // if
333 break;
334 }
335
336 default:
337 // do nothing
338 ;
339 } // switch
340
341 switch ( opInfo.type ) {
342 case OT_INDEX:
343 assert( applicationExpr->get_args().size() == 2 );
344 (*arg++)->accept( *this );
345 output << "[";
346 (*arg)->accept( *this );
347 output << "]";
348 break;
349
350 case OT_CALL:
351 // there are no intrinsic definitions of the function call operator
352 assert( false );
353 break;
354
355 case OT_CTOR:
356 case OT_DTOR:
357 if ( applicationExpr->get_args().size() == 1 ) {
358 // the expression fed into a single parameter constructor or destructor may contain side
359 // effects, so must still output this expression
360 output << "(";
361 (*arg++)->accept( *this );
362 output << ") /* " << opInfo.inputName << " */";
363 } else if ( applicationExpr->get_args().size() == 2 ) {
364 // intrinsic two parameter constructors are essentially bitwise assignment
365 output << "(";
366 (*arg++)->accept( *this );
367 output << opInfo.symbol;
368 (*arg)->accept( *this );
369 output << ") /* " << opInfo.inputName << " */";
370 } else {
371 // no constructors with 0 or more than 2 parameters
372 assert( false );
373 } // if
374 break;
375
376 case OT_PREFIX:
377 case OT_PREFIXASSIGN:
378 assert( applicationExpr->get_args().size() == 1 );
379 output << "(";
380 output << opInfo.symbol;
381 (*arg)->accept( *this );
382 output << ")";
383 break;
384
385 case OT_POSTFIX:
386 case OT_POSTFIXASSIGN:
387 assert( applicationExpr->get_args().size() == 1 );
388 (*arg)->accept( *this );
389 output << opInfo.symbol;
390 break;
391
392
393 case OT_INFIX:
394 case OT_INFIXASSIGN:
395 assert( applicationExpr->get_args().size() == 2 );
396 output << "(";
397 (*arg++)->accept( *this );
398 output << opInfo.symbol;
399 (*arg)->accept( *this );
400 output << ")";
401 break;
402
403 case OT_CONSTANT:
404 case OT_LABELADDRESS:
405 // there are no intrinsic definitions of 0/1 or label addresses as functions
406 assert( false );
407 } // switch
408 } else {
409 varExpr->accept( *this );
410 output << "(";
411 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
412 output << ")";
413 } // if
414 } else {
415 applicationExpr->get_function()->accept( *this );
416 output << "(";
417 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
418 output << ")";
419 } // if
420 }
421
422 void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
423 extension( untypedExpr );
424 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
425 OperatorInfo opInfo;
426 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
427 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
428 switch ( opInfo.type ) {
429 case OT_INDEX:
430 assert( untypedExpr->get_args().size() == 2 );
431 (*arg++)->accept( *this );
432 output << "[";
433 (*arg)->accept( *this );
434 output << "]";
435 break;
436
437 case OT_CALL:
438 assert( false );
439
440 case OT_CTOR:
441 case OT_DTOR:
442 if ( untypedExpr->get_args().size() == 1 ) {
443 // the expression fed into a single parameter constructor or destructor may contain side
444 // effects, so must still output this expression
445 output << "(";
446 (*arg++)->accept( *this );
447 output << ") /* " << opInfo.inputName << " */";
448 } else if ( untypedExpr->get_args().size() == 2 ) {
449 // intrinsic two parameter constructors are essentially bitwise assignment
450 output << "(";
451 (*arg++)->accept( *this );
452 output << opInfo.symbol;
453 (*arg)->accept( *this );
454 output << ") /* " << opInfo.inputName << " */";
455 } else {
456 // no constructors with 0 or more than 2 parameters
457 assert( false );
458 } // if
459 break;
460
461 case OT_PREFIX:
462 case OT_PREFIXASSIGN:
463 case OT_LABELADDRESS:
464 assert( untypedExpr->get_args().size() == 1 );
465 output << "(";
466 output << opInfo.symbol;
467 (*arg)->accept( *this );
468 output << ")";
469 break;
470
471 case OT_POSTFIX:
472 case OT_POSTFIXASSIGN:
473 assert( untypedExpr->get_args().size() == 1 );
474 (*arg)->accept( *this );
475 output << opInfo.symbol;
476 break;
477
478 case OT_INFIX:
479 case OT_INFIXASSIGN:
480 assert( untypedExpr->get_args().size() == 2 );
481 output << "(";
482 (*arg++)->accept( *this );
483 output << opInfo.symbol;
484 (*arg)->accept( *this );
485 output << ")";
486 break;
487
488 case OT_CONSTANT:
489 // there are no intrinsic definitions of 0 or 1 as functions
490 assert( false );
491 } // switch
492 } else {
493 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
494 assert( untypedExpr->get_args().size() == 2 );
495 (*untypedExpr->get_args().begin())->accept( *this );
496 output << " ... ";
497 (*--untypedExpr->get_args().end())->accept( *this );
498 } else { // builtin routines
499 nameExpr->accept( *this );
500 output << "(";
501 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
502 output << ")";
503 } // if
504 } // if
505 } else {
506 untypedExpr->get_function()->accept( *this );
507 output << "(";
508 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
509 output << ")";
510 } // if
511 }
512
513 void CodeGenerator::visit( RangeExpr * rangeExpr ) {
514 rangeExpr->get_low()->accept( *this );
515 output << " ... ";
516 rangeExpr->get_high()->accept( *this );
517 }
518
519 void CodeGenerator::visit( NameExpr * nameExpr ) {
520 extension( nameExpr );
521 OperatorInfo opInfo;
522 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
523 assert( opInfo.type == OT_CONSTANT );
524 output << opInfo.symbol;
525 } else {
526 output << nameExpr->get_name();
527 } // if
528 }
529
530 void CodeGenerator::visit( AddressExpr * addressExpr ) {
531 extension( addressExpr );
532 output << "(&";
533 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
534 if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
535 output << mangleName( variableExpr->get_var() );
536 } else {
537 addressExpr->get_arg()->accept( *this );
538 } // if
539 output << ")";
540 }
541
542 void CodeGenerator::visit( CastExpr * castExpr ) {
543 extension( castExpr );
544 output << "(";
545 if ( castExpr->get_result()->isVoid() ) {
546 output << "(void)" ;
547 } else if ( ! castExpr->get_result()->get_isLvalue() ) {
548 // at least one result type of cast, but not an lvalue
549 output << "(";
550 output << genType( castExpr->get_result(), "" );
551 output << ")";
552 } else {
553 // otherwise, the cast is to an lvalue type, so the cast should be dropped, since the result of a cast is
554 // never an lvalue in C
555 } // if
556 castExpr->get_arg()->accept( *this );
557 output << ")";
558 }
559
560 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
561 assert( false );
562 }
563
564 void CodeGenerator::visit( MemberExpr * memberExpr ) {
565 extension( memberExpr );
566 memberExpr->get_aggregate()->accept( *this );
567 output << "." << mangleName( memberExpr->get_member() );
568 }
569
570 void CodeGenerator::visit( VariableExpr * variableExpr ) {
571 extension( variableExpr );
572 OperatorInfo opInfo;
573 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
574 output << opInfo.symbol;
575 } else {
576 output << mangleName( variableExpr->get_var() );
577 } // if
578 }
579
580 void CodeGenerator::visit( ConstantExpr * constantExpr ) {
581 assert( constantExpr->get_constant() );
582 extension( constantExpr );
583 constantExpr->get_constant()->accept( *this );
584 }
585
586 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
587 extension( sizeofExpr );
588 output << "sizeof(";
589 if ( sizeofExpr->get_isType() ) {
590 output << genType( sizeofExpr->get_type(), "" );
591 } else {
592 sizeofExpr->get_expr()->accept( *this );
593 } // if
594 output << ")";
595 }
596
597 void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
598 // use GCC extension to avoid bumping std to C11
599 extension( alignofExpr );
600 output << "__alignof__(";
601 if ( alignofExpr->get_isType() ) {
602 output << genType( alignofExpr->get_type(), "" );
603 } else {
604 alignofExpr->get_expr()->accept( *this );
605 } // if
606 output << ")";
607 }
608
609 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
610 assert( false && "UntypedOffsetofExpr should not reach code generation." );
611 }
612
613 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
614 // use GCC builtin
615 output << "__builtin_offsetof(";
616 output << genType( offsetofExpr->get_type(), "" );
617 output << ", " << mangleName( offsetofExpr->get_member() );
618 output << ")";
619 }
620
621 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
622 assert( false && "OffsetPackExpr should not reach code generation." );
623 }
624
625 void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
626 extension( logicalExpr );
627 output << "(";
628 logicalExpr->get_arg1()->accept( *this );
629 if ( logicalExpr->get_isAnd() ) {
630 output << " && ";
631 } else {
632 output << " || ";
633 } // if
634 logicalExpr->get_arg2()->accept( *this );
635 output << ")";
636 }
637
638 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
639 extension( conditionalExpr );
640 output << "(";
641 conditionalExpr->get_arg1()->accept( *this );
642 output << " ? ";
643 conditionalExpr->get_arg2()->accept( *this );
644 output << " : ";
645 conditionalExpr->get_arg3()->accept( *this );
646 output << ")";
647 }
648
649 void CodeGenerator::visit( CommaExpr * commaExpr ) {
650 extension( commaExpr );
651 output << "(";
652 commaExpr->get_arg1()->accept( *this );
653 output << " , ";
654 commaExpr->get_arg2()->accept( *this );
655 output << ")";
656 }
657
658 void CodeGenerator::visit( TupleExpr * tupleExpr ) { assert( false ); }
659
660 void CodeGenerator::visit( TypeExpr * typeExpr ) {}
661
662 void CodeGenerator::visit( AsmExpr * asmExpr ) {
663 if ( asmExpr->get_inout() ) {
664 output << "[ ";
665 asmExpr->get_inout()->accept( *this );
666 output << " ] ";
667 } // if
668 asmExpr->get_constraint()->accept( *this );
669 output << " ( ";
670 asmExpr->get_operand()->accept( *this );
671 output << " )";
672 }
673
674 void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) {
675 assert( compLitExpr->get_type() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
676 output << "(" << genType( compLitExpr->get_type(), "" ) << ")";
677 compLitExpr->get_initializer()->accept( *this );
678 }
679
680 void CodeGenerator::visit( StmtExpr * stmtExpr ) {
681 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
682 output << "({" << std::endl;
683 cur_indent += CodeGenerator::tabsize;
684 unsigned int numStmts = stmts.size();
685 unsigned int i = 0;
686 for ( Statement * stmt : stmts ) {
687 output << indent << printLabels( stmt->get_labels() );
688 if ( i+1 == numStmts ) {
689 // last statement in a statement expression needs to be handled specially -
690 // cannot cast to void, otherwise the expression statement has no value
691 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
692 exprStmt->get_expr()->accept( *this );
693 output << ";" << endl;
694 ++i;
695 break;
696 }
697 }
698 stmt->accept( *this );
699 output << endl;
700 if ( wantSpacing( stmt ) ) {
701 output << endl;
702 } // if
703 ++i;
704 }
705 cur_indent -= CodeGenerator::tabsize;
706 output << indent << "})";
707 }
708
709 //*** Statements
710 void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
711 std::list<Statement*> ks = compoundStmt->get_kids();
712 output << "{" << endl;
713
714 cur_indent += CodeGenerator::tabsize;
715
716 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
717 output << indent << printLabels( (*i)->get_labels() );
718 (*i)->accept( *this );
719
720 output << endl;
721 if ( wantSpacing( *i ) ) {
722 output << endl;
723 } // if
724 } // for
725 cur_indent -= CodeGenerator::tabsize;
726
727 output << indent << "}";
728 }
729
730 void CodeGenerator::visit( ExprStmt * exprStmt ) {
731 assert( exprStmt );
732 // cast the top-level expression to void to reduce gcc warnings.
733 Expression * expr = new CastExpr( exprStmt->get_expr() );
734 expr->accept( *this );
735 output << ";";
736 }
737
738 void CodeGenerator::visit( AsmStmt * asmStmt ) {
739 output << "asm ";
740 if ( asmStmt->get_voltile() ) output << "volatile ";
741 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
742 output << "( ";
743 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
744 output << " : ";
745 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
746 output << " : ";
747 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
748 output << " : ";
749 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
750 if ( ! asmStmt->get_gotolabels().empty() ) {
751 output << " : ";
752 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
753 output << *begin++;
754 if ( begin == asmStmt->get_gotolabels().end() ) break;
755 output << ", ";
756 } // for
757 } // if
758 output << " );" ;
759 }
760
761 void CodeGenerator::visit( IfStmt * ifStmt ) {
762 output << "if ( ";
763 ifStmt->get_condition()->accept( *this );
764 output << " ) ";
765
766 ifStmt->get_thenPart()->accept( *this );
767
768 if ( ifStmt->get_elsePart() != 0) {
769 output << " else ";
770 ifStmt->get_elsePart()->accept( *this );
771 } // if
772 }
773
774 void CodeGenerator::visit( SwitchStmt * switchStmt ) {
775 output << "switch ( " ;
776 switchStmt->get_condition()->accept( *this );
777 output << " ) ";
778
779 output << "{" << std::endl;
780 cur_indent += CodeGenerator::tabsize;
781 acceptAll( switchStmt->get_statements(), *this );
782 cur_indent -= CodeGenerator::tabsize;
783 output << indent << "}";
784 }
785
786 void CodeGenerator::visit( CaseStmt * caseStmt ) {
787 output << indent;
788 if ( caseStmt->isDefault()) {
789 output << "default";
790 } else {
791 output << "case ";
792 caseStmt->get_condition()->accept( *this );
793 } // if
794 output << ":\n";
795
796 std::list<Statement *> sts = caseStmt->get_statements();
797
798 cur_indent += CodeGenerator::tabsize;
799 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
800 output << indent << printLabels( (*i)->get_labels() ) ;
801 (*i)->accept( *this );
802 output << endl;
803 } // for
804 cur_indent -= CodeGenerator::tabsize;
805 }
806
807 void CodeGenerator::visit( BranchStmt * branchStmt ) {
808 switch ( branchStmt->get_type()) {
809 case BranchStmt::Goto:
810 if ( ! branchStmt->get_target().empty() )
811 output << "goto " << branchStmt->get_target();
812 else {
813 if ( branchStmt->get_computedTarget() != 0 ) {
814 output << "goto *";
815 branchStmt->get_computedTarget()->accept( *this );
816 } // if
817 } // if
818 break;
819 case BranchStmt::Break:
820 output << "break";
821 break;
822 case BranchStmt::Continue:
823 output << "continue";
824 break;
825 } // switch
826 output << ";";
827 }
828
829
830 void CodeGenerator::visit( ReturnStmt * returnStmt ) {
831 output << "return ";
832 maybeAccept( returnStmt->get_expr(), *this );
833 output << ";";
834 }
835
836 void CodeGenerator::visit( WhileStmt * whileStmt ) {
837 if ( whileStmt->get_isDoWhile() ) {
838 output << "do" ;
839 } else {
840 output << "while (" ;
841 whileStmt->get_condition()->accept( *this );
842 output << ")";
843 } // if
844 output << " ";
845
846 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
847 whileStmt->get_body()->accept( *this );
848
849 output << indent;
850
851 if ( whileStmt->get_isDoWhile() ) {
852 output << " while (" ;
853 whileStmt->get_condition()->accept( *this );
854 output << ");";
855 } // if
856 }
857
858 void CodeGenerator::visit( ForStmt * forStmt ) {
859 // initialization is always hoisted, so don't bother doing anything with that
860 output << "for (;";
861
862 if ( forStmt->get_condition() != 0 ) {
863 forStmt->get_condition()->accept( *this );
864 } // if
865 output << ";";
866
867 if ( forStmt->get_increment() != 0 ) {
868 // cast the top-level expression to void to reduce gcc warnings.
869 Expression * expr = new CastExpr( forStmt->get_increment() );
870 expr->accept( *this );
871 } // if
872 output << ") ";
873
874 if ( forStmt->get_body() != 0 ) {
875 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
876 forStmt->get_body()->accept( *this );
877 } // if
878 }
879
880 void CodeGenerator::visit( NullStmt * nullStmt ) {
881 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
882 output << "/* null statement */ ;";
883 }
884
885 void CodeGenerator::visit( DeclStmt * declStmt ) {
886 declStmt->get_decl()->accept( *this );
887
888 if ( doSemicolon( declStmt->get_decl() ) ) {
889 output << ";";
890 } // if
891 }
892
893 void CodeGenerator::handleStorageClass( Declaration * decl ) {
894 switch ( decl->get_storageClass() ) {
895 case DeclarationNode::Extern:
896 output << "extern ";
897 break;
898 case DeclarationNode::Static:
899 output << "static ";
900 break;
901 case DeclarationNode::Auto:
902 // silently drop storage class
903 break;
904 case DeclarationNode::Register:
905 output << "register ";
906 break;
907 case DeclarationNode::Inline:
908 output << "inline ";
909 break;
910 case DeclarationNode::Fortran:
911 output << "fortran ";
912 break;
913 case DeclarationNode::Noreturn:
914 output << "_Noreturn ";
915 break;
916 case DeclarationNode::Threadlocal:
917 output << "_Thread_local ";
918 break;
919 case DeclarationNode::NoStorageClass:
920 break;
921 } // switch
922 }
923
924 std::string genName( DeclarationWithType * decl ) {
925 CodeGen::OperatorInfo opInfo;
926 if ( operatorLookup( decl->get_name(), opInfo ) ) {
927 return opInfo.outputName;
928 } else {
929 return decl->get_name();
930 } // if
931 }
932} // namespace CodeGen
933
934// Local Variables: //
935// tab-width: 4 //
936// mode: c++ //
937// compile-command: "make install" //
938// End: //
Note: See TracBrowser for help on using the repository browser.