source: src/CodeGen/CodeGenerator.cc@ e0ff3e6

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 e0ff3e6 was 94e0864d, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

Merge branch 'master' into resolver

Conflicts:

src/CodeGen/CodeGenerator.cc
src/Parser/ExpressionNode.cc
src/ResolvExpr/Resolver.cc

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