source: src/CodeGen/CodeGenerator.cc@ c5e3208

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

refactor indenter from CodeGen and CurrentObject

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