source: src/CodeGen/CodeGenerator.cc@ f31cb3e

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

code generation for external asm statement (declaration)

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