source: src/CodeGen/CodeGenerator.cc@ dbfb35d

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 dbfb35d was 29cf9c8, checked in by Andrew Beach <ajbeach@…>, 8 years ago

Working, but inaccurate, line directives are being added.

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