source: src/CodeGen/CodeGenerator.cc@ fc56cdbf

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

Add codegen for TupleAssignExpr, UniqueExpr and remove lvalue cast special case

  • 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 : Tus Jul 25 15:29:00 2017
13// Update Count : 486
14//
15#include "CodeGenerator.h"
16
17#include <cassert> // for assert, assertf
18#include <list> // for _List_iterator, list, list<>::it...
19
20#include "Common/SemanticError.h" // for SemanticError
21#include "Common/UniqueName.h" // for UniqueName
22#include "Common/utility.h" // for CodeLocation, toString
23#include "GenType.h" // for genType
24#include "InitTweak/InitTweak.h" // for getPointerBase
25#include "OperatorTable.h" // for OperatorInfo, operatorLookup
26#include "Parser/LinkageSpec.h" // for Spec, Intrinsic
27#include "SynTree/Attribute.h" // for Attribute
28#include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode
29#include "SynTree/Constant.h" // for Constant
30#include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl
31#include "SynTree/Expression.h" // for Expression, UntypedExpr, Applica...
32#include "SynTree/Initializer.h" // for Initializer, ListInit, Designation
33#include "SynTree/Label.h" // for Label, operator<<
34#include "SynTree/Statement.h" // for Statement, AsmStmt, BranchStmt
35#include "SynTree/Type.h" // for Type, Type::StorageClasses, Func...
36
37using namespace std;
38
39namespace CodeGen {
40 int CodeGenerator::tabsize = 4;
41
42 // the kinds of statements that would ideally be followed by whitespace
43 bool wantSpacing( Statement * stmt) {
44 return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
45 dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
46 }
47
48 void CodeGenerator::extension( Expression * expr ) {
49 if ( expr->get_extension() ) {
50 output << "__extension__ ";
51 } // if
52 } // extension
53
54 void CodeGenerator::extension( Declaration * decl ) {
55 if ( decl->get_extension() ) {
56 output << "__extension__ ";
57 } // if
58 } // extension
59
60 void CodeGenerator::asmName( DeclarationWithType * decl ) {
61 if ( ConstantExpr * asmName = decl->get_asmName() ) {
62 output << " asm ( " << asmName->get_constant()->get_value() << " )";
63 } // if
64 } // extension
65
66 CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) {
67 labels = &l;
68 return *this;
69 }
70
71 ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) {
72 std::list< Label > & labs = *printLabels.labels;
73 // l.unique(); // assumes a sorted list. Why not use set? Does order matter?
74 for ( Label & l : labs ) {
75 output << l.get_name() + ": ";
76 printLabels.cg.genAttributes( l.get_attributes() );
77 } // for
78 return output;
79 }
80
81 CodeGenerator::LineMarker::LineMarker(
82 CodeLocation const & loc, bool toPrint) :
83 loc(loc), toPrint(toPrint)
84 {}
85
86 CodeGenerator::LineMarker CodeGenerator::lineDirective(
87 BaseSyntaxNode const * node) {
88 return LineMarker(node->location, lineMarks);
89 }
90
91 std::ostream & operator<<(std::ostream & out,
92 CodeGenerator::LineMarker const & marker) {
93 if (marker.toPrint && marker.loc.isSet()) {
94 return out << "\n# " << marker.loc.linenumber << " \""
95 << marker.loc.filename << "\"\n";
96 } else if (marker.toPrint) {
97 return out << "\n/* Missing CodeLocation */\n";
98 } else {
99 return out;
100 }
101 }
102
103 CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
104
105 string CodeGenerator::mangleName( DeclarationWithType * decl ) {
106 if ( pretty ) return decl->get_name();
107 if ( decl->get_mangleName() != "" ) {
108 // need to incorporate scope level in order to differentiate names for destructors
109 return decl->get_scopedMangleName();
110 } else {
111 return decl->get_name();
112 } // if
113 }
114
115 void CodeGenerator::genAttributes( list< Attribute * > & attributes ) {
116 if ( attributes.empty() ) return;
117 output << "__attribute__ ((";
118 for ( list< Attribute * >::iterator attr( attributes.begin() );; ) {
119 output << (*attr)->get_name();
120 if ( ! (*attr)->get_parameters().empty() ) {
121 output << "(";
122 genCommaList( (*attr)->get_parameters().begin(), (*attr)->get_parameters().end() );
123 output << ")";
124 } // if
125 if ( ++attr == attributes.end() ) break;
126 output << ","; // separator
127 } // for
128 output << ")) ";
129 } // CodeGenerator::genAttributes
130
131
132 // *** Declarations
133 void CodeGenerator::visit( FunctionDecl * functionDecl ) {
134 extension( functionDecl );
135 genAttributes( functionDecl->get_attributes() );
136
137 handleStorageClass( functionDecl );
138 functionDecl->get_funcSpec().print( output );
139
140 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), pretty, genC );
141
142 asmName( functionDecl );
143
144 // acceptAll( functionDecl->get_oldDecls(), *this );
145 if ( functionDecl->get_statements() ) {
146 functionDecl->get_statements()->accept( *this );
147 } // if
148 }
149
150 void CodeGenerator::visit( ObjectDecl * objectDecl ) {
151 if (objectDecl->get_name().empty() && genC ) {
152 // only generate an anonymous name when generating C code, otherwise it clutters the output too much
153 static UniqueName name = { "__anonymous_object" };
154 objectDecl->set_name( name.newName() );
155 }
156
157 extension( objectDecl );
158 genAttributes( objectDecl->get_attributes() );
159
160 handleStorageClass( objectDecl );
161 output << genType( objectDecl->get_type(), mangleName( objectDecl ), pretty, genC );
162
163 asmName( objectDecl );
164
165 if ( objectDecl->get_init() ) {
166 output << " = ";
167 objectDecl->get_init()->accept( *this );
168 } // if
169
170 if ( objectDecl->get_bitfieldWidth() ) {
171 output << ":";
172 objectDecl->get_bitfieldWidth()->accept( *this );
173 } // if
174 }
175
176 void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) {
177 genAttributes( aggDecl->get_attributes() );
178
179 if( ! aggDecl->get_parameters().empty() && ! genC ) {
180 // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
181 output << "forall(";
182 genCommaList( aggDecl->get_parameters().begin(), aggDecl->get_parameters().end() );
183 output << ")" << endl;
184 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 {
550 // at least one result type of cast.
551 // Note: previously, lvalue casts were skipped. Since it's now impossible for the user to write
552 // an lvalue cast, this has been taken out.
553 output << "(";
554 output << genType( castExpr->get_result(), "", pretty, genC );
555 output << ")";
556 } // if
557 castExpr->get_arg()->accept( *this );
558 output << ")";
559 }
560
561 void CodeGenerator::visit( VirtualCastExpr * castExpr ) {
562 assertf( ! genC, "VirtualCastExpr should not reach code generation." );
563 extension( castExpr );
564 output << "(virtual ";
565 castExpr->get_arg()->accept( *this );
566 output << ")";
567 }
568
569 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
570 assertf( ! genC, "UntypedMemberExpr should not reach code generation." );
571 extension( memberExpr );
572 memberExpr->get_aggregate()->accept( *this );
573 output << ".";
574 memberExpr->get_member()->accept( *this );
575 }
576
577 void CodeGenerator::visit( MemberExpr * memberExpr ) {
578 extension( memberExpr );
579 memberExpr->get_aggregate()->accept( *this );
580 output << "." << mangleName( memberExpr->get_member() );
581 }
582
583 void CodeGenerator::visit( VariableExpr * variableExpr ) {
584 extension( variableExpr );
585 OperatorInfo opInfo;
586 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
587 output << opInfo.symbol;
588 } else {
589 output << mangleName( variableExpr->get_var() );
590 } // if
591 }
592
593 void CodeGenerator::visit( ConstantExpr * constantExpr ) {
594 assert( constantExpr->get_constant() );
595 extension( constantExpr );
596 constantExpr->get_constant()->accept( *this );
597 }
598
599 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
600 extension( sizeofExpr );
601 output << "sizeof(";
602 if ( sizeofExpr->get_isType() ) {
603 output << genType( sizeofExpr->get_type(), "", pretty, genC );
604 } else {
605 sizeofExpr->get_expr()->accept( *this );
606 } // if
607 output << ")";
608 }
609
610 void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
611 // use GCC extension to avoid bumping std to C11
612 extension( alignofExpr );
613 output << "__alignof__(";
614 if ( alignofExpr->get_isType() ) {
615 output << genType( alignofExpr->get_type(), "", pretty, genC );
616 } else {
617 alignofExpr->get_expr()->accept( *this );
618 } // if
619 output << ")";
620 }
621
622 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
623 assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." );
624 output << "offsetof(";
625 output << genType( offsetofExpr->get_type(), "", pretty, genC );
626 output << ", " << offsetofExpr->get_member();
627 output << ")";
628 }
629
630 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
631 // use GCC builtin
632 output << "__builtin_offsetof(";
633 output << genType( offsetofExpr->get_type(), "", pretty, genC );
634 output << ", " << mangleName( offsetofExpr->get_member() );
635 output << ")";
636 }
637
638 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
639 assertf( ! genC, "OffsetPackExpr should not reach code generation." );
640 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")";
641 }
642
643 void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
644 extension( logicalExpr );
645 output << "(";
646 logicalExpr->get_arg1()->accept( *this );
647 if ( logicalExpr->get_isAnd() ) {
648 output << " && ";
649 } else {
650 output << " || ";
651 } // if
652 logicalExpr->get_arg2()->accept( *this );
653 output << ")";
654 }
655
656 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
657 extension( conditionalExpr );
658 output << "(";
659 conditionalExpr->get_arg1()->accept( *this );
660 output << " ? ";
661 conditionalExpr->get_arg2()->accept( *this );
662 output << " : ";
663 conditionalExpr->get_arg3()->accept( *this );
664 output << ")";
665 }
666
667 void CodeGenerator::visit( CommaExpr * commaExpr ) {
668 extension( commaExpr );
669 output << "(";
670 if ( genC ) {
671 // arg1 of a CommaExpr is never used, so it can be safely cast to void to reduce gcc warnings.
672 commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) );
673 }
674 commaExpr->get_arg1()->accept( *this );
675 output << " , ";
676 commaExpr->get_arg2()->accept( *this );
677 output << ")";
678 }
679
680 void CodeGenerator::visit( TupleAssignExpr * tupleExpr ) {
681 assertf( ! genC, "TupleAssignExpr should not reach code generation." );
682 tupleExpr->stmtExpr->accept( *this );
683 }
684
685 void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) {
686 assertf( ! genC, "UntypedTupleExpr should not reach code generation." );
687 extension( tupleExpr );
688 output << "[";
689 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
690 output << "]";
691 }
692
693 void CodeGenerator::visit( TupleExpr * tupleExpr ) {
694 assertf( ! genC, "TupleExpr should not reach code generation." );
695 extension( tupleExpr );
696 output << "[";
697 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
698 output << "]";
699 }
700
701 void CodeGenerator::visit( TupleIndexExpr * tupleExpr ) {
702 assertf( ! genC, "TupleIndexExpr should not reach code generation." );
703 extension( tupleExpr );
704 tupleExpr->get_tuple()->accept( *this );
705 output << "." << tupleExpr->get_index();
706 }
707
708 void CodeGenerator::visit( TypeExpr * typeExpr ) {
709 // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl;
710 // assertf( ! genC, "TypeExpr should not reach code generation." );
711 if ( ! genC ) {
712 output<< genType( typeExpr->get_type(), "", pretty, genC );
713 }
714 }
715
716 void CodeGenerator::visit( AsmExpr * asmExpr ) {
717 if ( asmExpr->get_inout() ) {
718 output << "[ ";
719 asmExpr->get_inout()->accept( *this );
720 output << " ] ";
721 } // if
722 asmExpr->get_constraint()->accept( *this );
723 output << " ( ";
724 asmExpr->get_operand()->accept( *this );
725 output << " )";
726 }
727
728 void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) {
729 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
730 output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")";
731 compLitExpr->get_initializer()->accept( *this );
732 }
733
734 void CodeGenerator::visit( UniqueExpr * unqExpr ) {
735 assertf( ! genC, "Unique expressions should not reach code generation." );
736 output << "unq<" << unqExpr->get_id() << ">{ ";
737 unqExpr->get_expr()->accept( *this );
738 output << " }";
739 }
740
741 void CodeGenerator::visit( StmtExpr * stmtExpr ) {
742 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
743 output << lineDirective( stmtExpr) << "({" << std::endl;
744 ++indent;
745 unsigned int numStmts = stmts.size();
746 unsigned int i = 0;
747 for ( Statement * stmt : stmts ) {
748 output << lineDirective( stmt ) << indent;
749 output << printLabels( stmt->get_labels() );
750 if ( i+1 == numStmts ) {
751 // last statement in a statement expression needs to be handled specially -
752 // cannot cast to void, otherwise the expression statement has no value
753 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
754 exprStmt->get_expr()->accept( *this );
755 output << ";" << endl;
756 ++i;
757 break;
758 }
759 }
760 stmt->accept( *this );
761 output << endl;
762 if ( wantSpacing( stmt ) ) {
763 output << endl;
764 } // if
765 ++i;
766 }
767 --indent;
768 output << indent << "})";
769 }
770
771 // *** Statements
772 void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
773 std::list<Statement*> ks = compoundStmt->get_kids();
774 output << "{" << endl;
775
776 ++indent;
777
778 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
779 output << indent << printLabels( (*i)->get_labels() );
780 (*i)->accept( *this );
781
782 output << endl;
783 if ( wantSpacing( *i ) ) {
784 output << endl;
785 } // if
786 } // for
787 --indent;
788
789 output << indent << "}";
790 }
791
792 void CodeGenerator::visit( ExprStmt * exprStmt ) {
793 assert( exprStmt );
794 if ( genC ) {
795 // cast the top-level expression to void to reduce gcc warnings.
796 exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) );
797 }
798 exprStmt->get_expr()->accept( *this );
799 output << ";";
800 }
801
802 void CodeGenerator::visit( AsmStmt * asmStmt ) {
803 output << "asm ";
804 if ( asmStmt->get_voltile() ) output << "volatile ";
805 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
806 output << "( ";
807 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
808 output << " : ";
809 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
810 output << " : ";
811 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
812 output << " : ";
813 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
814 if ( ! asmStmt->get_gotolabels().empty() ) {
815 output << " : ";
816 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
817 output << *begin++;
818 if ( begin == asmStmt->get_gotolabels().end() ) break;
819 output << ", ";
820 } // for
821 } // if
822 output << " );" ;
823 }
824
825 void CodeGenerator::visit( AsmDecl * asmDecl ) {
826 output << "asm ";
827 AsmStmt * asmStmt = asmDecl->get_stmt();
828 output << "( ";
829 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
830 output << " )" ;
831 }
832
833 void CodeGenerator::visit( IfStmt * ifStmt ) {
834 output << lineDirective( ifStmt );
835 output << "if ( ";
836 ifStmt->get_condition()->accept( *this );
837 output << " ) ";
838
839 ifStmt->get_thenPart()->accept( *this );
840
841 if ( ifStmt->get_elsePart() != 0) {
842 output << " else ";
843 ifStmt->get_elsePart()->accept( *this );
844 } // if
845 }
846
847 void CodeGenerator::visit( SwitchStmt * switchStmt ) {
848 output << lineDirective( switchStmt );
849 output << "switch ( " ;
850 switchStmt->get_condition()->accept( *this );
851 output << " ) ";
852
853 output << "{" << std::endl;
854 ++indent;
855 acceptAll( switchStmt->get_statements(), *this );
856 --indent;
857 output << indent << "}";
858 }
859
860 void CodeGenerator::visit( CaseStmt * caseStmt ) {
861 output << lineDirective( caseStmt );
862 output << indent;
863 if ( caseStmt->isDefault()) {
864 output << "default";
865 } else {
866 output << "case ";
867 caseStmt->get_condition()->accept( *this );
868 } // if
869 output << ":\n";
870
871 std::list<Statement *> sts = caseStmt->get_statements();
872
873 ++indent;
874 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
875 output << indent << printLabels( (*i)->get_labels() ) ;
876 (*i)->accept( *this );
877 output << endl;
878 } // for
879 --indent;
880 }
881
882 void CodeGenerator::visit( BranchStmt * branchStmt ) {
883 switch ( branchStmt->get_type()) {
884 case BranchStmt::Goto:
885 if ( ! branchStmt->get_target().empty() )
886 output << "goto " << branchStmt->get_target();
887 else {
888 if ( branchStmt->get_computedTarget() != 0 ) {
889 output << "goto *";
890 branchStmt->get_computedTarget()->accept( *this );
891 } // if
892 } // if
893 break;
894 case BranchStmt::Break:
895 output << "break";
896 break;
897 case BranchStmt::Continue:
898 output << "continue";
899 break;
900 } // switch
901 output << ";";
902 }
903
904 void CodeGenerator::visit( ReturnStmt * returnStmt ) {
905 output << "return ";
906 maybeAccept( returnStmt->get_expr(), *this );
907 output << ";";
908 }
909
910 void CodeGenerator::visit( ThrowStmt * throwStmt ) {
911 assertf( ! genC, "Throw statements should not reach code generation." );
912
913 output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ?
914 "throw" : "throwResume");
915 if (throwStmt->get_expr()) {
916 output << " ";
917 throwStmt->get_expr()->accept( *this );
918 }
919 if (throwStmt->get_target()) {
920 output << " _At ";
921 throwStmt->get_target()->accept( *this );
922 }
923 output << ";";
924 }
925
926 void CodeGenerator::visit( WhileStmt * whileStmt ) {
927 if ( whileStmt->get_isDoWhile() ) {
928 output << "do" ;
929 } else {
930 output << "while (" ;
931 whileStmt->get_condition()->accept( *this );
932 output << ")";
933 } // if
934 output << " ";
935
936 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
937 whileStmt->get_body()->accept( *this );
938
939 output << indent;
940
941 if ( whileStmt->get_isDoWhile() ) {
942 output << " while (" ;
943 whileStmt->get_condition()->accept( *this );
944 output << ");";
945 } // if
946 }
947
948 void CodeGenerator::visit( ForStmt * forStmt ) {
949 // initialization is always hoisted, so don't bother doing anything with that
950 output << "for (;";
951
952 if ( forStmt->get_condition() != 0 ) {
953 forStmt->get_condition()->accept( *this );
954 } // if
955 output << ";";
956
957 if ( forStmt->get_increment() != 0 ) {
958 // cast the top-level expression to void to reduce gcc warnings.
959 Expression * expr = new CastExpr( forStmt->get_increment() );
960 expr->accept( *this );
961 } // if
962 output << ") ";
963
964 if ( forStmt->get_body() != 0 ) {
965 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
966 forStmt->get_body()->accept( *this );
967 } // if
968 }
969
970 void CodeGenerator::visit( __attribute__((unused)) NullStmt * nullStmt ) {
971 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
972 output << "/* null statement */ ;";
973 }
974
975 void CodeGenerator::visit( DeclStmt * declStmt ) {
976 declStmt->get_decl()->accept( *this );
977
978 if ( doSemicolon( declStmt->get_decl() ) ) {
979 output << ";";
980 } // if
981 }
982
983 void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) {
984 if ( decl->get_storageClasses().any() ) {
985 decl->get_storageClasses().print( output );
986 } // if
987 } // CodeGenerator::handleStorageClass
988
989 std::string genName( DeclarationWithType * decl ) {
990 CodeGen::OperatorInfo opInfo;
991 if ( operatorLookup( decl->get_name(), opInfo ) ) {
992 return opInfo.outputName;
993 } else {
994 return decl->get_name();
995 } // if
996 }
997} // namespace CodeGen
998
999// Local Variables: //
1000// tab-width: 4 //
1001// mode: c++ //
1002// compile-command: "make install" //
1003// End: //
Note: See TracBrowser for help on using the repository browser.