source: src/CodeGen/CodeGenerator.cc@ 8191203

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 8191203 was dd020c0, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

first attempt to create function specifiers

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