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

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

Merge branch 'master' into references

  • Property mode set to 100644
File size: 30.4 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 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( VirtualCastExpr * castExpr ) {
563 assertf( ! genC, "VirtualCastExpr should not reach code generation." );
564 extension( castExpr );
565 output << "(virtual ";
566 castExpr->get_arg()->accept( *this );
567 output << ")";
568 }
569
570 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
571 assertf( ! genC, "UntypedMemberExpr should not reach code generation." );
572 extension( memberExpr );
573 memberExpr->get_aggregate()->accept( *this );
574 output << ".";
575 memberExpr->get_member()->accept( *this );
576 }
577
578 void CodeGenerator::visit( MemberExpr * memberExpr ) {
579 extension( memberExpr );
580 memberExpr->get_aggregate()->accept( *this );
581 output << "." << mangleName( memberExpr->get_member() );
582 }
583
584 void CodeGenerator::visit( VariableExpr * variableExpr ) {
585 extension( variableExpr );
586 OperatorInfo opInfo;
587 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
588 output << opInfo.symbol;
589 } else {
590 output << mangleName( variableExpr->get_var() );
591 } // if
592 }
593
594 void CodeGenerator::visit( ConstantExpr * constantExpr ) {
595 assert( constantExpr->get_constant() );
596 extension( constantExpr );
597 constantExpr->get_constant()->accept( *this );
598 }
599
600 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
601 extension( sizeofExpr );
602 output << "sizeof(";
603 if ( sizeofExpr->get_isType() ) {
604 output << genType( sizeofExpr->get_type(), "", pretty, genC );
605 } else {
606 sizeofExpr->get_expr()->accept( *this );
607 } // if
608 output << ")";
609 }
610
611 void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
612 // use GCC extension to avoid bumping std to C11
613 extension( alignofExpr );
614 output << "__alignof__(";
615 if ( alignofExpr->get_isType() ) {
616 output << genType( alignofExpr->get_type(), "", pretty, genC );
617 } else {
618 alignofExpr->get_expr()->accept( *this );
619 } // if
620 output << ")";
621 }
622
623 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
624 assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." );
625 output << "offsetof(";
626 output << genType( offsetofExpr->get_type(), "", pretty, genC );
627 output << ", " << offsetofExpr->get_member();
628 output << ")";
629 }
630
631 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
632 // use GCC builtin
633 output << "__builtin_offsetof(";
634 output << genType( offsetofExpr->get_type(), "", pretty, genC );
635 output << ", " << mangleName( offsetofExpr->get_member() );
636 output << ")";
637 }
638
639 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
640 assertf( ! genC, "OffsetPackExpr should not reach code generation." );
641 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")";
642 }
643
644 void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
645 extension( logicalExpr );
646 output << "(";
647 logicalExpr->get_arg1()->accept( *this );
648 if ( logicalExpr->get_isAnd() ) {
649 output << " && ";
650 } else {
651 output << " || ";
652 } // if
653 logicalExpr->get_arg2()->accept( *this );
654 output << ")";
655 }
656
657 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
658 extension( conditionalExpr );
659 output << "(";
660 conditionalExpr->get_arg1()->accept( *this );
661 output << " ? ";
662 conditionalExpr->get_arg2()->accept( *this );
663 output << " : ";
664 conditionalExpr->get_arg3()->accept( *this );
665 output << ")";
666 }
667
668 void CodeGenerator::visit( CommaExpr * commaExpr ) {
669 extension( commaExpr );
670 output << "(";
671 if ( genC ) {
672 // arg1 of a CommaExpr is never used, so it can be safely cast to void to reduce gcc warnings.
673 commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) );
674 }
675 commaExpr->get_arg1()->accept( *this );
676 output << " , ";
677 commaExpr->get_arg2()->accept( *this );
678 output << ")";
679 }
680
681 void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) {
682 assertf( ! genC, "UntypedTupleExpr should not reach code generation." );
683 extension( tupleExpr );
684 output << "[";
685 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
686 output << "]";
687 }
688
689 void CodeGenerator::visit( TupleExpr * tupleExpr ) {
690 assertf( ! genC, "TupleExpr should not reach code generation." );
691 extension( tupleExpr );
692 output << "[";
693 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
694 output << "]";
695 }
696
697 void CodeGenerator::visit( TupleIndexExpr * tupleExpr ) {
698 assertf( ! genC, "TupleIndexExpr should not reach code generation." );
699 extension( tupleExpr );
700 tupleExpr->get_tuple()->accept( *this );
701 output << "." << tupleExpr->get_index();
702 }
703
704 void CodeGenerator::visit( TypeExpr * typeExpr ) {
705 // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl;
706 // assertf( ! genC, "TypeExpr should not reach code generation." );
707 if ( ! genC ) {
708 output<< genType( typeExpr->get_type(), "", pretty, genC );
709 }
710 }
711
712 void CodeGenerator::visit( AsmExpr * asmExpr ) {
713 if ( asmExpr->get_inout() ) {
714 output << "[ ";
715 asmExpr->get_inout()->accept( *this );
716 output << " ] ";
717 } // if
718 asmExpr->get_constraint()->accept( *this );
719 output << " ( ";
720 asmExpr->get_operand()->accept( *this );
721 output << " )";
722 }
723
724 void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) {
725 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
726 output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")";
727 compLitExpr->get_initializer()->accept( *this );
728 }
729
730 void CodeGenerator::visit( StmtExpr * stmtExpr ) {
731 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
732 output << lineDirective( stmtExpr) << "({" << std::endl;
733 ++indent;
734 unsigned int numStmts = stmts.size();
735 unsigned int i = 0;
736 for ( Statement * stmt : stmts ) {
737 output << lineDirective( stmt ) << indent;
738 output << printLabels( stmt->get_labels() );
739 if ( i+1 == numStmts ) {
740 // last statement in a statement expression needs to be handled specially -
741 // cannot cast to void, otherwise the expression statement has no value
742 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
743 exprStmt->get_expr()->accept( *this );
744 output << ";" << endl;
745 ++i;
746 break;
747 }
748 }
749 stmt->accept( *this );
750 output << endl;
751 if ( wantSpacing( stmt ) ) {
752 output << endl;
753 } // if
754 ++i;
755 }
756 --indent;
757 output << indent << "})";
758 }
759
760 // *** Statements
761 void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
762 std::list<Statement*> ks = compoundStmt->get_kids();
763 output << "{" << endl;
764
765 ++indent;
766
767 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
768 output << indent << printLabels( (*i)->get_labels() );
769 (*i)->accept( *this );
770
771 output << endl;
772 if ( wantSpacing( *i ) ) {
773 output << endl;
774 } // if
775 } // for
776 --indent;
777
778 output << indent << "}";
779 }
780
781 void CodeGenerator::visit( ExprStmt * exprStmt ) {
782 assert( exprStmt );
783 if ( genC ) {
784 // cast the top-level expression to void to reduce gcc warnings.
785 exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) );
786 }
787 exprStmt->get_expr()->accept( *this );
788 output << ";";
789 }
790
791 void CodeGenerator::visit( AsmStmt * asmStmt ) {
792 output << "asm ";
793 if ( asmStmt->get_voltile() ) output << "volatile ";
794 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
795 output << "( ";
796 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
797 output << " : ";
798 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
799 output << " : ";
800 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
801 output << " : ";
802 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
803 if ( ! asmStmt->get_gotolabels().empty() ) {
804 output << " : ";
805 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
806 output << *begin++;
807 if ( begin == asmStmt->get_gotolabels().end() ) break;
808 output << ", ";
809 } // for
810 } // if
811 output << " );" ;
812 }
813
814 void CodeGenerator::visit( AsmDecl * asmDecl ) {
815 output << "asm ";
816 AsmStmt * asmStmt = asmDecl->get_stmt();
817 output << "( ";
818 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
819 output << " )" ;
820 }
821
822 void CodeGenerator::visit( IfStmt * ifStmt ) {
823 output << lineDirective( ifStmt );
824 output << "if ( ";
825 ifStmt->get_condition()->accept( *this );
826 output << " ) ";
827
828 ifStmt->get_thenPart()->accept( *this );
829
830 if ( ifStmt->get_elsePart() != 0) {
831 output << " else ";
832 ifStmt->get_elsePart()->accept( *this );
833 } // if
834 }
835
836 void CodeGenerator::visit( SwitchStmt * switchStmt ) {
837 output << lineDirective( switchStmt );
838 output << "switch ( " ;
839 switchStmt->get_condition()->accept( *this );
840 output << " ) ";
841
842 output << "{" << std::endl;
843 ++indent;
844 acceptAll( switchStmt->get_statements(), *this );
845 --indent;
846 output << indent << "}";
847 }
848
849 void CodeGenerator::visit( CaseStmt * caseStmt ) {
850 output << lineDirective( caseStmt );
851 output << indent;
852 if ( caseStmt->isDefault()) {
853 output << "default";
854 } else {
855 output << "case ";
856 caseStmt->get_condition()->accept( *this );
857 } // if
858 output << ":\n";
859
860 std::list<Statement *> sts = caseStmt->get_statements();
861
862 ++indent;
863 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
864 output << indent << printLabels( (*i)->get_labels() ) ;
865 (*i)->accept( *this );
866 output << endl;
867 } // for
868 --indent;
869 }
870
871 void CodeGenerator::visit( BranchStmt * branchStmt ) {
872 switch ( branchStmt->get_type()) {
873 case BranchStmt::Goto:
874 if ( ! branchStmt->get_target().empty() )
875 output << "goto " << branchStmt->get_target();
876 else {
877 if ( branchStmt->get_computedTarget() != 0 ) {
878 output << "goto *";
879 branchStmt->get_computedTarget()->accept( *this );
880 } // if
881 } // if
882 break;
883 case BranchStmt::Break:
884 output << "break";
885 break;
886 case BranchStmt::Continue:
887 output << "continue";
888 break;
889 } // switch
890 output << ";";
891 }
892
893 void CodeGenerator::visit( ReturnStmt * returnStmt ) {
894 output << "return ";
895 maybeAccept( returnStmt->get_expr(), *this );
896 output << ";";
897 }
898
899 void CodeGenerator::visit( ThrowStmt * throwStmt ) {
900 assertf( ! genC, "Throw statements should not reach code generation." );
901
902 output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ?
903 "throw" : "throwResume");
904 if (throwStmt->get_expr()) {
905 output << " ";
906 throwStmt->get_expr()->accept( *this );
907 }
908 if (throwStmt->get_target()) {
909 output << " _At ";
910 throwStmt->get_target()->accept( *this );
911 }
912 output << ";";
913 }
914
915 void CodeGenerator::visit( WhileStmt * whileStmt ) {
916 if ( whileStmt->get_isDoWhile() ) {
917 output << "do" ;
918 } else {
919 output << "while (" ;
920 whileStmt->get_condition()->accept( *this );
921 output << ")";
922 } // if
923 output << " ";
924
925 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
926 whileStmt->get_body()->accept( *this );
927
928 output << indent;
929
930 if ( whileStmt->get_isDoWhile() ) {
931 output << " while (" ;
932 whileStmt->get_condition()->accept( *this );
933 output << ");";
934 } // if
935 }
936
937 void CodeGenerator::visit( ForStmt * forStmt ) {
938 // initialization is always hoisted, so don't bother doing anything with that
939 output << "for (;";
940
941 if ( forStmt->get_condition() != 0 ) {
942 forStmt->get_condition()->accept( *this );
943 } // if
944 output << ";";
945
946 if ( forStmt->get_increment() != 0 ) {
947 // cast the top-level expression to void to reduce gcc warnings.
948 Expression * expr = new CastExpr( forStmt->get_increment() );
949 expr->accept( *this );
950 } // if
951 output << ") ";
952
953 if ( forStmt->get_body() != 0 ) {
954 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
955 forStmt->get_body()->accept( *this );
956 } // if
957 }
958
959 void CodeGenerator::visit( __attribute__((unused)) NullStmt * nullStmt ) {
960 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
961 output << "/* null statement */ ;";
962 }
963
964 void CodeGenerator::visit( DeclStmt * declStmt ) {
965 declStmt->get_decl()->accept( *this );
966
967 if ( doSemicolon( declStmt->get_decl() ) ) {
968 output << ";";
969 } // if
970 }
971
972 void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) {
973 if ( decl->get_storageClasses().any() ) {
974 decl->get_storageClasses().print( output );
975 } // if
976 } // CodeGenerator::handleStorageClass
977
978 std::string genName( DeclarationWithType * decl ) {
979 CodeGen::OperatorInfo opInfo;
980 if ( operatorLookup( decl->get_name(), opInfo ) ) {
981 return opInfo.outputName;
982 } else {
983 return decl->get_name();
984 } // if
985 }
986} // namespace CodeGen
987
988// Local Variables: //
989// tab-width: 4 //
990// mode: c++ //
991// compile-command: "make install" //
992// End: //
Note: See TracBrowser for help on using the repository browser.