source: src/CodeGen/CodeGenerator.cc@ 7e003011

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 7e003011 was 7e003011, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Some more unused argument removal, also removed some functions

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