source: src/CodeGen/CodeGenerator.cc@ 6d267ca

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

Fix TupleAssignment code for references

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