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

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 6c4ff37 was 6c4ff37, checked in by Rob Schluntz <rschlunt@…>, 11 years ago

rename CodeGenerator2 -> CodeGenerator, CodeGenerator refactoring and output reformatting, fix bug where while loop body label does not print

  • Property mode set to 100644
File size: 17.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 : Rob Schluntz
12// Last Modified On : Tue Jun 02 11:45:06 2015
13// Update Count : 65
14//
15
16#include <algorithm>
17#include <iostream>
18#include <cassert>
19#include <list>
20
21#include "SynTree/Type.h"
22#include "SynTree/Declaration.h"
23#include "SynTree/Statement.h"
24#include "SynTree/Expression.h"
25#include "SynTree/Initializer.h"
26
27#include "utility.h"
28#include "UnimplementedError.h"
29
30#include "CodeGenerator.h"
31#include "OperatorTable.h"
32#include "GenType.h"
33
34using namespace std;
35
36namespace CodeGen {
37 int CodeGenerator::tabsize = 4;
38
39 CodeGenerator::CodeGenerator( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), output( os ) { }
40
41 CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indent, bool infunp )
42 : cur_indent( indent ), insideFunction( infunp ), output( os ) {
43 //output << std::string( init );
44 }
45
46 CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent, bool infunp )
47 : cur_indent( indent ), insideFunction( infunp ), output( os ) {
48 //output << std::string( init );
49 }
50
51 string mangleName( DeclarationWithType *decl ) {
52 if ( decl->get_mangleName() != "" ) {
53 return decl->get_mangleName();
54 } else {
55 return decl->get_name();
56 } // if
57 }
58
59 //*** Declarations
60 void CodeGenerator::visit( FunctionDecl *functionDecl ) {
61 handleStorageClass( functionDecl );
62 if ( functionDecl->get_isInline() ) {
63 output << "inline ";
64 } // if
65 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
66
67 // how to get this to the Functype?
68 std::list< Declaration * > olds = functionDecl->get_oldDecls();
69 if ( ! olds.empty() ) {
70 output << " /* function has old declaration */";
71 } // if
72
73 // acceptAll( functionDecl->get_oldDecls(), *this );
74 if ( functionDecl->get_statements() ) {
75 functionDecl->get_statements()->accept(*this );
76 } // if
77 }
78
79 void CodeGenerator::visit( ObjectDecl *objectDecl ) {
80 handleStorageClass( objectDecl );
81 output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
82
83 if ( objectDecl->get_init() ) {
84 output << " = ";
85 objectDecl->get_init()->accept( *this );
86 } // if
87 if ( objectDecl->get_bitfieldWidth() ) {
88 output << ":";
89 objectDecl->get_bitfieldWidth()->accept( *this );
90 } // if
91 }
92
93 void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
94 if ( aggDecl->get_name() != "" )
95 output << aggDecl->get_name();
96
97 std::list< Declaration * > &memb = aggDecl->get_members();
98
99 if ( ! memb.empty() ) {
100 output << endl << string( cur_indent, ' ' ) << "{" << endl;
101
102 cur_indent += CodeGenerator::tabsize;
103 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
104 output << string( cur_indent, ' ' );
105 (*i)->accept(*this );
106 output << ";" << endl;
107 }
108
109 cur_indent -= CodeGenerator::tabsize;
110
111 output << string( cur_indent, ' ' ) << "}";
112 } // if
113 }
114
115 void CodeGenerator::visit( StructDecl *structDecl ) {
116 output << "struct ";
117 handleAggregate( structDecl );
118 }
119
120 void CodeGenerator::visit( UnionDecl *aggregateDecl ) {
121 output << "union ";
122 handleAggregate( aggregateDecl );
123 }
124
125 void CodeGenerator::visit( EnumDecl *aggDecl ) {
126 output << "enum ";
127
128 if ( aggDecl->get_name() != "" )
129 output << aggDecl->get_name();
130
131 std::list< Declaration* > &memb = aggDecl->get_members();
132
133 if ( ! memb.empty() ) {
134 output << endl << "{" << endl;
135
136 cur_indent += CodeGenerator::tabsize;
137 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
138 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
139 assert( obj );
140 output << string( cur_indent, ' ' ) << mangleName( obj );
141 if ( obj->get_init() ) {
142 output << " = ";
143 obj->get_init()->accept(*this );
144 } // if
145 output << "," << endl;
146 } // for
147
148 cur_indent -= CodeGenerator::tabsize;
149
150 output << "}" << endl;
151 } // if
152 }
153
154 void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
155
156 void CodeGenerator::visit( TypedefDecl *typeDecl ) {
157 output << "typedef ";
158 output << genType( typeDecl->get_base(), typeDecl->get_name() );
159 }
160
161 void CodeGenerator::visit( TypeDecl *typeDecl ) {
162 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
163 // still to be done
164 output << "extern unsigned long " << typeDecl->get_name();
165 if ( typeDecl->get_base() ) {
166 output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
167 } // if
168 }
169
170 void CodeGenerator::visit( SingleInit *init ) {
171 init->get_value()->accept( *this );
172 }
173
174 void CodeGenerator::visit( ListInit *init ) {
175 output << "{ ";
176 genCommaList( init->begin_initializers(), init->end_initializers() );
177 output << " }";
178 }
179
180 void CodeGenerator::visit( Constant *constant ) {
181 output << constant->get_value() ;
182 }
183
184 //*** Expressions
185 void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
186 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
187 OperatorInfo opInfo;
188 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
189 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
190 switch ( opInfo.type ) {
191 case OT_PREFIXASSIGN:
192 case OT_POSTFIXASSIGN:
193 case OT_INFIXASSIGN:
194 {
195 assert( arg != applicationExpr->get_args().end() );
196 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
197
198 *arg = addrExpr->get_arg();
199 } else {
200 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
201 newExpr->get_args().push_back( *arg );
202 *arg = newExpr;
203 } // if
204 break;
205 }
206
207 default:
208 // do nothing
209 ;
210 }
211
212 switch ( opInfo.type ) {
213 case OT_INDEX:
214 assert( applicationExpr->get_args().size() == 2 );
215 (*arg++)->accept( *this );
216 output << "[";
217 (*arg)->accept( *this );
218 output << "]";
219 break;
220
221 case OT_CALL:
222 // there are no intrinsic definitions of the function call operator
223 assert( false );
224 break;
225
226 case OT_PREFIX:
227 case OT_PREFIXASSIGN:
228 assert( applicationExpr->get_args().size() == 1 );
229 output << "(";
230 output << opInfo.symbol;
231 (*arg)->accept( *this );
232 output << ")";
233 break;
234
235 case OT_POSTFIX:
236 case OT_POSTFIXASSIGN:
237 assert( applicationExpr->get_args().size() == 1 );
238 (*arg)->accept( *this );
239 output << opInfo.symbol;
240 break;
241
242 case OT_INFIX:
243 case OT_INFIXASSIGN:
244 assert( applicationExpr->get_args().size() == 2 );
245 output << "(";
246 (*arg++)->accept( *this );
247 output << opInfo.symbol;
248 (*arg)->accept( *this );
249 output << ")";
250 break;
251
252 case OT_CONSTANT:
253 // there are no intrinsic definitions of 0 or 1 as functions
254 assert( false );
255 }
256 } else {
257 varExpr->accept( *this );
258 output << "(";
259 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
260 output << ")";
261 } // if
262 } else {
263 applicationExpr->get_function()->accept( *this );
264 output << "(";
265 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
266 output << ")";
267 } // if
268 }
269
270 void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
271 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
272 OperatorInfo opInfo;
273 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
274 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
275 switch ( opInfo.type ) {
276 case OT_INDEX:
277 assert( untypedExpr->get_args().size() == 2 );
278 (*arg++)->accept( *this );
279 output << "[";
280 (*arg)->accept( *this );
281 output << "]";
282 break;
283
284 case OT_CALL:
285 assert( false );
286 break;
287
288 case OT_PREFIX:
289 case OT_PREFIXASSIGN:
290 assert( untypedExpr->get_args().size() == 1 );
291 output << "(";
292 output << opInfo.symbol;
293 (*arg)->accept( *this );
294 output << ")";
295 break;
296
297 case OT_POSTFIX:
298 case OT_POSTFIXASSIGN:
299 assert( untypedExpr->get_args().size() == 1 );
300 (*arg)->accept( *this );
301 output << opInfo.symbol;
302 break;
303
304 case OT_INFIX:
305 case OT_INFIXASSIGN:
306 assert( untypedExpr->get_args().size() == 2 );
307 output << "(";
308 (*arg++)->accept( *this );
309 output << opInfo.symbol;
310 (*arg)->accept( *this );
311 output << ")";
312 break;
313
314 case OT_CONSTANT:
315 // there are no intrinsic definitions of 0 or 1 as functions
316 assert( false );
317 }
318 } else {
319 nameExpr->accept( *this );
320 output << "(";
321 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
322 output << ")";
323 } // if
324 } else {
325 untypedExpr->get_function()->accept( *this );
326 output << "(";
327 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
328 output << ")";
329 } // if
330 }
331
332 void CodeGenerator::visit( NameExpr *nameExpr ) {
333 OperatorInfo opInfo;
334 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
335 assert( opInfo.type == OT_CONSTANT );
336 output << opInfo.symbol;
337 } else {
338 output << nameExpr->get_name();
339 } // if
340 }
341
342 void CodeGenerator::visit( AddressExpr *addressExpr ) {
343 output << "(&";
344 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
345 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
346 output << mangleName( variableExpr->get_var() );
347 } else {
348 addressExpr->get_arg()->accept( *this );
349 } // if
350 output << ")";
351 }
352
353 void CodeGenerator::visit( CastExpr *castExpr ) {
354 output << "((";
355 if ( castExpr->get_results().empty() ) {
356 output << "void" ;
357 } else {
358 output << genType( castExpr->get_results().front(), "" );
359 } // if
360 output << ")";
361 castExpr->get_arg()->accept( *this );
362 output << ")";
363 }
364
365 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
366 assert( false );
367 }
368
369 void CodeGenerator::visit( MemberExpr *memberExpr ) {
370 memberExpr->get_aggregate()->accept( *this );
371 output << "." << mangleName( memberExpr->get_member() );
372 }
373
374 void CodeGenerator::visit( VariableExpr *variableExpr ) {
375 OperatorInfo opInfo;
376 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
377 output << opInfo.symbol;
378 } else {
379 output << mangleName( variableExpr->get_var() );
380 } // if
381 }
382
383 void CodeGenerator::visit( ConstantExpr *constantExpr ) {
384 assert( constantExpr->get_constant() );
385 constantExpr->get_constant()->accept( *this );
386 }
387
388 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
389 output << "sizeof(";
390 if ( sizeofExpr->get_isType() ) {
391 output << genType( sizeofExpr->get_type(), "" );
392 } else {
393 sizeofExpr->get_expr()->accept( *this );
394 } // if
395 output << ")";
396 }
397
398 void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
399 output << "(";
400 logicalExpr->get_arg1()->accept( *this );
401 if ( logicalExpr->get_isAnd() ) {
402 output << " && ";
403 } else {
404 output << " || ";
405 } // if
406 logicalExpr->get_arg2()->accept( *this );
407 output << ")";
408 }
409
410 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
411 output << "(";
412 conditionalExpr->get_arg1()->accept( *this );
413 output << " ? ";
414 conditionalExpr->get_arg2()->accept( *this );
415 output << " : ";
416 conditionalExpr->get_arg3()->accept( *this );
417 output << ")";
418 }
419
420 void CodeGenerator::visit( CommaExpr *commaExpr ) {
421 output << "(";
422 commaExpr->get_arg1()->accept( *this );
423 output << " , ";
424 commaExpr->get_arg2()->accept( *this );
425 output << ")";
426 }
427
428 void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
429
430 void CodeGenerator::visit( TypeExpr *typeExpr ) {}
431
432
433 //*** Statements
434 void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
435 std::list<Statement*> ks = compoundStmt->get_kids();
436
437 output << endl << string( cur_indent, ' ' ) << "{" << endl;
438
439 cur_indent += CodeGenerator::tabsize;
440
441 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++) {
442 output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() ) ;
443 (*i)->accept(*this );
444 output << endl;
445 }
446 cur_indent -= CodeGenerator::tabsize;
447
448 output << string( cur_indent, ' ' ) << "}" << endl;
449 }
450
451 void CodeGenerator::visit( ExprStmt *exprStmt ) {
452 // I don't see why this check is necessary.
453 // If this starts to cause problems then put it back in,
454 // with an explanation
455 assert( exprStmt );
456
457 // if ( exprStmt != 0 ) {
458 exprStmt->get_expr()->accept( *this );
459 output << ";" ;
460 // } // if
461 }
462
463 void CodeGenerator::visit( IfStmt *ifStmt ) {
464 output << "if (";
465 ifStmt->get_condition()->accept(*this );
466 output << ")\n";
467
468 cur_indent += CodeGenerator::tabsize;
469 output << string( cur_indent, ' ' );
470 ifStmt->get_thenPart()->accept(*this );
471 cur_indent -= CodeGenerator::tabsize;
472 output << endl;
473
474 if ( ifStmt->get_elsePart() != 0) {
475 output << string( cur_indent, ' ' ) << " else " << endl ;
476
477 cur_indent += CodeGenerator::tabsize;
478 ifStmt->get_elsePart()->accept(*this );
479 cur_indent -= CodeGenerator::tabsize;
480 } // if
481 }
482
483 void CodeGenerator::visit( SwitchStmt *switchStmt ) {
484 //output << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator::printLabels( switchStmt->get_labels() )
485 output << "switch (" ;
486 switchStmt->get_condition()->accept(*this );
487 output << ")\n";
488
489 output << string( cur_indent, ' ' ) << "{" << std::endl;
490 cur_indent += CodeGenerator::tabsize;
491
492 acceptAll( switchStmt->get_branches(), *this );
493
494 cur_indent -= CodeGenerator::tabsize;
495
496 output << /* "\r" << */ string( cur_indent, ' ' ) << "}" << endl;
497 }
498
499 void CodeGenerator::visit( CaseStmt *caseStmt ) {
500 output << string( cur_indent, ' ' );
501 if ( caseStmt->isDefault())
502 output << "default" ;
503 else {
504 output << "case " ;
505 caseStmt->get_condition()->accept(*this );
506 } // if
507 output << ":\n";
508
509 std::list<Statement *> sts = caseStmt->get_statements();
510
511 cur_indent += CodeGenerator::tabsize;
512 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
513 output << /* "\r" << */ string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() ) ;
514 (*i)->accept(*this );
515 output << endl;
516 }
517 cur_indent -= CodeGenerator::tabsize;
518 }
519
520 void CodeGenerator::visit( BranchStmt *branchStmt ) {
521 output << "\r" << string( cur_indent, ' ' );
522 output << CodeGenerator::printLabels( branchStmt->get_labels());
523
524 switch ( branchStmt->get_type()) {
525 case BranchStmt::Goto:
526 if ( ! branchStmt->get_target().empty() )
527 output << "goto " << branchStmt->get_target();
528 else {
529 if ( branchStmt->get_computedTarget() != 0 ) {
530 output << "goto *";
531 branchStmt->get_computedTarget()->accept( *this );
532 } // if
533 } // if
534 break;
535 case BranchStmt::Break:
536 output << "break";
537 break;
538 case BranchStmt::Continue:
539 output << "continue";
540 break;
541 }
542 output << ";" << endl;
543 }
544
545
546 void CodeGenerator::visit( ReturnStmt *returnStmt ) {
547 output << "return ";
548
549 // xxx -- check for null expression;
550 if ( returnStmt->get_expr() ) {
551 returnStmt->get_expr()->accept( *this );
552 } // if
553 output << ";";
554 }
555
556 void CodeGenerator::visit( WhileStmt *whileStmt ) {
557 if ( whileStmt->get_isDoWhile() )
558 output << "do" ;
559 else {
560 output << "while (" ;
561 whileStmt->get_condition()->accept(*this );
562 output << ")";
563 } // if
564 output << "\n";
565
566 output << string( cur_indent, ' ' ) << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
567 whileStmt->get_body()->accept( *this );
568
569 output << /* "\r" << */ string( cur_indent, ' ' );
570
571 if ( whileStmt->get_isDoWhile() ) {
572 output << " while (" ;
573 whileStmt->get_condition()->accept(*this );
574 output << ");";
575 } // if
576
577 output << "\n";
578 }
579
580 void CodeGenerator::visit( ForStmt *forStmt ) {
581 output << "for (";
582
583 if ( forStmt->get_initialization() != 0 )
584 forStmt->get_initialization()->accept( *this );
585 else
586 output << ";";
587
588 if ( forStmt->get_condition() != 0 )
589 forStmt->get_condition()->accept( *this );
590 output << ";";
591
592 if ( forStmt->get_increment() != 0 )
593 forStmt->get_increment()->accept( *this );
594 output << ")" << endl;
595
596 if ( forStmt->get_body() != 0 ) {
597 output << string( cur_indent, ' ' ) << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
598 forStmt->get_body()->accept( *this );
599 } // if
600 }
601
602 void CodeGenerator::visit( NullStmt *nullStmt ) {
603 //output << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator::printLabels( nullStmt->get_labels() );
604 output << "/* null statement */ ;";
605 }
606
607 void CodeGenerator::visit( DeclStmt *declStmt ) {
608 declStmt->get_decl()->accept( *this );
609
610 if ( doSemicolon( declStmt->get_decl() ) ) {
611 output << ";";
612 } // if
613 }
614
615 std::string CodeGenerator::printLabels( std::list< Label > &l ) {
616 std::string str( "" );
617 l.unique(); // assumes a sorted list. Why not use set?
618
619 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
620 str += *i + ": ";
621
622 return str;
623 }
624
625 void CodeGenerator::handleStorageClass( Declaration *decl ) {
626 switch ( decl->get_storageClass() ) {
627 case Declaration::NoStorageClass:
628 break;
629 case Declaration::Extern:
630 output << "extern ";
631 break;
632 case Declaration::Static:
633 output << "static ";
634 break;
635 case Declaration::Auto:
636 // silently drop storage class
637 break;
638 case Declaration::Register:
639 output << "register ";
640 break;
641 case Declaration::Inline:
642 // handled as special via isInline flag (FIX)
643 break;
644 case Declaration::Fortran:
645 // not handled
646 break;
647 } // switch
648 }
649} // namespace CodeGen
650
651// Local Variables: //
652// tab-width: 4 //
653// mode: c++ //
654// compile-command: "make install" //
655// End: //
Note: See TracBrowser for help on using the repository browser.