source: src/CodeGen/CodeGenerator.cc@ 3f27b9a

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 3f27b9a was 0720e049, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

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