source: src/CodeGen/CodeGenerator.cc@ 0b2961f

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 0b2961f was eb3261f, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

removed carriage returns from code generation and some duplicate label printing, more MLE documentation

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