source: src/CodeGen/CodeGenerator.cc@ a4b3525

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 a4b3525 was 62423350, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Big push on designations and initialization: works with generic types, tuples, arrays, tests pass.
Refactor guard_value_impl.
Add list of declarations to TupleType.

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