source: src/CodeGen/CodeGenerator.cc@ 0961bf4

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 0961bf4 was 6e300d9, checked in by Andrew Beach <ajbeach@…>, 8 years ago

Updated last update marker.

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