source: src/CodeGen/CodeGenerator.cc@ dfee306

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 dfee306 was 721f17a, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

fix OT_LABELADDRESS warning, parse genric types

  • 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 : Peter A. Buhr
12// Last Modified On : Fri Jun 26 16:52:58 2015
13// Update Count : 144
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 case OT_LABELADDRESS:
272 // there are no intrinsic definitions of 0/1 or label addresses as functions
273 assert( false );
274 }
275 } else {
276 varExpr->accept( *this );
277 output << "(";
278 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
279 output << ")";
280 } // if
281 } else {
282 applicationExpr->get_function()->accept( *this );
283 output << "(";
284 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
285 output << ")";
286 } // if
287 }
288
289 void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
290 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
291 OperatorInfo opInfo;
292 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
293 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
294 switch ( opInfo.type ) {
295 case OT_INDEX:
296 assert( untypedExpr->get_args().size() == 2 );
297 (*arg++)->accept( *this );
298 output << "[";
299 (*arg)->accept( *this );
300 output << "]";
301 break;
302
303 case OT_CALL:
304 assert( false );
305 break;
306
307 case OT_PREFIX:
308 case OT_PREFIXASSIGN:
309 case OT_LABELADDRESS:
310 assert( untypedExpr->get_args().size() == 1 );
311 output << "(";
312 output << opInfo.symbol;
313 (*arg)->accept( *this );
314 output << ")";
315 break;
316
317 case OT_POSTFIX:
318 case OT_POSTFIXASSIGN:
319 assert( untypedExpr->get_args().size() == 1 );
320 (*arg)->accept( *this );
321 output << opInfo.symbol;
322 break;
323
324 case OT_INFIX:
325 case OT_INFIXASSIGN:
326 assert( untypedExpr->get_args().size() == 2 );
327 output << "(";
328 (*arg++)->accept( *this );
329 output << opInfo.symbol;
330 (*arg)->accept( *this );
331 output << ")";
332 break;
333
334 case OT_CONSTANT:
335 // there are no intrinsic definitions of 0 or 1 as functions
336 assert( false );
337 }
338 } else {
339 nameExpr->accept( *this );
340 output << "(";
341 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
342 output << ")";
343 } // if
344 } else {
345 untypedExpr->get_function()->accept( *this );
346 output << "(";
347 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
348 output << ")";
349 } // if
350 }
351
352 void CodeGenerator::visit( NameExpr *nameExpr ) {
353 OperatorInfo opInfo;
354 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
355 assert( opInfo.type == OT_CONSTANT );
356 output << opInfo.symbol;
357 } else {
358 output << nameExpr->get_name();
359 } // if
360 }
361
362 void CodeGenerator::visit( AddressExpr *addressExpr ) {
363 output << "(&";
364 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
365 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
366 output << mangleName( variableExpr->get_var() );
367 } else {
368 addressExpr->get_arg()->accept( *this );
369 } // if
370 output << ")";
371 }
372
373 void CodeGenerator::visit( CastExpr *castExpr ) {
374 output << "((";
375 if ( castExpr->get_results().empty() ) {
376 output << "void" ;
377 } else {
378 output << genType( castExpr->get_results().front(), "" );
379 } // if
380 output << ")";
381 castExpr->get_arg()->accept( *this );
382 output << ")";
383 }
384
385 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
386 assert( false );
387 }
388
389 void CodeGenerator::visit( MemberExpr *memberExpr ) {
390 memberExpr->get_aggregate()->accept( *this );
391 output << "." << mangleName( memberExpr->get_member() );
392 }
393
394 void CodeGenerator::visit( VariableExpr *variableExpr ) {
395 OperatorInfo opInfo;
396 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
397 output << opInfo.symbol;
398 } else {
399 output << mangleName( variableExpr->get_var() );
400 } // if
401 }
402
403 void CodeGenerator::visit( ConstantExpr *constantExpr ) {
404 assert( constantExpr->get_constant() );
405 constantExpr->get_constant()->accept( *this );
406 }
407
408 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
409 output << "sizeof(";
410 if ( sizeofExpr->get_isType() ) {
411 output << genType( sizeofExpr->get_type(), "" );
412 } else {
413 sizeofExpr->get_expr()->accept( *this );
414 } // if
415 output << ")";
416 }
417
418 void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
419 output << "(";
420 logicalExpr->get_arg1()->accept( *this );
421 if ( logicalExpr->get_isAnd() ) {
422 output << " && ";
423 } else {
424 output << " || ";
425 } // if
426 logicalExpr->get_arg2()->accept( *this );
427 output << ")";
428 }
429
430 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
431 output << "(";
432 conditionalExpr->get_arg1()->accept( *this );
433 output << " ? ";
434 conditionalExpr->get_arg2()->accept( *this );
435 output << " : ";
436 conditionalExpr->get_arg3()->accept( *this );
437 output << ")";
438 }
439
440 void CodeGenerator::visit( CommaExpr *commaExpr ) {
441 output << "(";
442 commaExpr->get_arg1()->accept( *this );
443 output << " , ";
444 commaExpr->get_arg2()->accept( *this );
445 output << ")";
446 }
447
448 void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
449
450 void CodeGenerator::visit( TypeExpr *typeExpr ) {}
451
452 //*** Statements
453 void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
454 std::list<Statement*> ks = compoundStmt->get_kids();
455 output << "{" << endl;
456
457 cur_indent += CodeGenerator::tabsize;
458
459 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++) {
460 output << indent << printLabels( (*i)->get_labels() );
461 (*i)->accept(*this );
462
463 output << endl;
464 if ( wantSpacing( *i ) ) {
465 output << endl;
466 }
467 }
468 cur_indent -= CodeGenerator::tabsize;
469
470 output << indent << "}";
471 }
472
473 void CodeGenerator::visit( ExprStmt *exprStmt ) {
474 // I don't see why this check is necessary.
475 // If this starts to cause problems then put it back in,
476 // with an explanation
477 assert( exprStmt );
478
479 // if ( exprStmt != 0 ) {
480 exprStmt->get_expr()->accept( *this );
481 output << ";" ;
482 // } // if
483 }
484
485 void CodeGenerator::visit( IfStmt *ifStmt ) {
486 output << "if (";
487 ifStmt->get_condition()->accept(*this );
488 output << ") ";
489
490 ifStmt->get_thenPart()->accept(*this );
491
492 if ( ifStmt->get_elsePart() != 0) {
493 output << " else ";
494 ifStmt->get_elsePart()->accept(*this );
495 } // if
496 }
497
498 void CodeGenerator::visit( SwitchStmt *switchStmt ) {
499 output << "switch (" ;
500 switchStmt->get_condition()->accept(*this );
501 output << ") ";
502
503 output << "{" << std::endl;
504 cur_indent += CodeGenerator::tabsize;
505
506 acceptAll( switchStmt->get_branches(), *this );
507
508 cur_indent -= CodeGenerator::tabsize;
509
510 output << indent << "}";
511 }
512
513 void CodeGenerator::visit( CaseStmt *caseStmt ) {
514 output << indent;
515 if ( caseStmt->isDefault()) {
516 output << "default";
517 } else {
518 output << "case ";
519 caseStmt->get_condition()->accept(*this );
520 } // if
521 output << ":\n";
522
523 std::list<Statement *> sts = caseStmt->get_statements();
524
525 cur_indent += CodeGenerator::tabsize;
526 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
527 output << indent << printLabels( (*i)->get_labels() ) ;
528 (*i)->accept(*this );
529 output << endl;
530 }
531 cur_indent -= CodeGenerator::tabsize;
532 }
533
534 void CodeGenerator::visit( BranchStmt *branchStmt ) {
535 switch ( branchStmt->get_type()) {
536 case BranchStmt::Goto:
537 if ( ! branchStmt->get_target().empty() )
538 output << "goto " << branchStmt->get_target();
539 else {
540 if ( branchStmt->get_computedTarget() != 0 ) {
541 output << "goto *";
542 branchStmt->get_computedTarget()->accept( *this );
543 } // if
544 } // if
545 break;
546 case BranchStmt::Break:
547 output << "break";
548 break;
549 case BranchStmt::Continue:
550 output << "continue";
551 break;
552 }
553 output << ";";
554 }
555
556
557 void CodeGenerator::visit( ReturnStmt *returnStmt ) {
558 output << "return ";
559
560 // xxx -- check for null expression;
561 if ( returnStmt->get_expr() ) {
562 returnStmt->get_expr()->accept( *this );
563 } // if
564 output << ";";
565 }
566
567 void CodeGenerator::visit( WhileStmt *whileStmt ) {
568 if ( whileStmt->get_isDoWhile() )
569 output << "do" ;
570 else {
571 output << "while (" ;
572 whileStmt->get_condition()->accept(*this );
573 output << ")";
574 } // if
575 output << " ";
576
577 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
578 whileStmt->get_body()->accept( *this );
579
580 output << indent;
581
582 if ( whileStmt->get_isDoWhile() ) {
583 output << " while (" ;
584 whileStmt->get_condition()->accept(*this );
585 output << ");";
586 } // if
587 }
588
589 void CodeGenerator::visit( ForStmt *forStmt ) {
590 output << "for (";
591
592 if ( forStmt->get_initialization() != 0 )
593 forStmt->get_initialization()->accept( *this );
594 else
595 output << ";";
596
597 if ( forStmt->get_condition() != 0 )
598 forStmt->get_condition()->accept( *this );
599 output << ";";
600
601 if ( forStmt->get_increment() != 0 )
602 forStmt->get_increment()->accept( *this );
603 output << ") ";
604
605 if ( forStmt->get_body() != 0 ) {
606 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
607 forStmt->get_body()->accept( *this );
608 } // if
609 }
610
611 void CodeGenerator::visit( NullStmt *nullStmt ) {
612 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
613 output << "/* null statement */ ;";
614 }
615
616 void CodeGenerator::visit( DeclStmt *declStmt ) {
617 declStmt->get_decl()->accept( *this );
618
619 if ( doSemicolon( declStmt->get_decl() ) ) {
620 output << ";";
621 } // if
622 }
623
624 std::string CodeGenerator::printLabels( std::list< Label > &l ) {
625 std::string str( "" );
626 l.unique(); // assumes a sorted list. Why not use set?
627
628 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
629 str += *i + ": ";
630
631 return str;
632 }
633
634 void CodeGenerator::handleStorageClass( Declaration *decl ) {
635 switch ( decl->get_storageClass() ) {
636 case DeclarationNode::Extern:
637 output << "extern ";
638 break;
639 case DeclarationNode::Static:
640 output << "static ";
641 break;
642 case DeclarationNode::Auto:
643 // silently drop storage class
644 break;
645 case DeclarationNode::Register:
646 output << "register ";
647 break;
648 case DeclarationNode::Inline:
649 output << "inline ";
650 break;
651 case DeclarationNode::Fortran:
652 output << "fortran ";
653 break;
654 case DeclarationNode::Noreturn:
655 output << "_Noreturn ";
656 break;
657 case DeclarationNode::Threadlocal:
658 output << "_Thread_local ";
659 break;
660 case DeclarationNode::NoStorageClass:
661 break;
662 } // switch
663 }
664} // namespace CodeGen
665
666// Local Variables: //
667// tab-width: 4 //
668// mode: c++ //
669// compile-command: "make install" //
670// End: //
Note: See TracBrowser for help on using the repository browser.