| 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 Sep 17 15:24:08 2015
 | 
|---|
| 13 | // Update Count     : 231
 | 
|---|
| 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 | 
 | 
|---|
| 35 | using namespace std;
 | 
|---|
| 36 | 
 | 
|---|
| 37 | namespace CodeGen {
 | 
|---|
| 38 |         int CodeGenerator::tabsize = 4;
 | 
|---|
| 39 | 
 | 
|---|
| 40 |         // the kinds of statements that would ideally be followed by 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::printDesignators( std::list< Expression * > & designators ) {
 | 
|---|
| 189 |                 typedef std::list< Expression * > DesignatorList;
 | 
|---|
| 190 |                 if ( designators.size() == 0 ) return;
 | 
|---|
| 191 |                 for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) {
 | 
|---|
| 192 |                         if ( dynamic_cast< NameExpr * >( *iter ) ) {
 | 
|---|
| 193 |                                 // if expression is a name, then initializing aggregate member
 | 
|---|
| 194 |                                 output << ".";
 | 
|---|
| 195 |                                 (*iter)->accept( *this );
 | 
|---|
| 196 |                         } else {
 | 
|---|
| 197 |                                 // if not a simple name, it has to be a constant expression, i.e. an array designator
 | 
|---|
| 198 |                                 output << "[";
 | 
|---|
| 199 |                                 (*iter)->accept( *this );
 | 
|---|
| 200 |                                 output << "]";
 | 
|---|
| 201 |                         }
 | 
|---|
| 202 |                 }
 | 
|---|
| 203 |                 output << " = ";
 | 
|---|
| 204 |         }
 | 
|---|
| 205 | 
 | 
|---|
| 206 |         void CodeGenerator::visit( SingleInit *init ) {
 | 
|---|
| 207 |                 printDesignators( init->get_designators() );
 | 
|---|
| 208 |                 init->get_value()->accept( *this );
 | 
|---|
| 209 |         }
 | 
|---|
| 210 | 
 | 
|---|
| 211 |         void CodeGenerator::visit( ListInit *init ) {
 | 
|---|
| 212 |                 printDesignators( init->get_designators() );
 | 
|---|
| 213 |                 output << "{ ";
 | 
|---|
| 214 |                 genCommaList( init->begin_initializers(), init->end_initializers() );
 | 
|---|
| 215 |                 output << " }";
 | 
|---|
| 216 |         }
 | 
|---|
| 217 | 
 | 
|---|
| 218 |         void CodeGenerator::visit( Constant *constant ) { 
 | 
|---|
| 219 |                 output << constant->get_value() ;
 | 
|---|
| 220 |         }
 | 
|---|
| 221 | 
 | 
|---|
| 222 |         //*** Expressions
 | 
|---|
| 223 |         void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
 | 
|---|
| 224 |                 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
 | 
|---|
| 225 |                         OperatorInfo opInfo;
 | 
|---|
| 226 |                         if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
 | 
|---|
| 227 |                                 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
 | 
|---|
| 228 |                                 switch ( opInfo.type ) {
 | 
|---|
| 229 |                                   case OT_PREFIXASSIGN:
 | 
|---|
| 230 |                                   case OT_POSTFIXASSIGN:
 | 
|---|
| 231 |                                   case OT_INFIXASSIGN:
 | 
|---|
| 232 |                                         {
 | 
|---|
| 233 |                                                 assert( arg != applicationExpr->get_args().end() );
 | 
|---|
| 234 |                                                 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
 | 
|---|
| 235 |                 
 | 
|---|
| 236 |                                                         *arg = addrExpr->get_arg();
 | 
|---|
| 237 |                                                 } else {
 | 
|---|
| 238 |                                                         UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
 | 
|---|
| 239 |                                                         newExpr->get_args().push_back( *arg );
 | 
|---|
| 240 |                                                         *arg = newExpr;
 | 
|---|
| 241 |                                                 } // if
 | 
|---|
| 242 |                                                 break;
 | 
|---|
| 243 |                                         }
 | 
|---|
| 244 |               
 | 
|---|
| 245 |                                   default:
 | 
|---|
| 246 |                                         // do nothing
 | 
|---|
| 247 |                                         ;
 | 
|---|
| 248 |                                 }
 | 
|---|
| 249 |             
 | 
|---|
| 250 |                                 switch ( opInfo.type ) {
 | 
|---|
| 251 |                                   case OT_INDEX:
 | 
|---|
| 252 |                                         assert( applicationExpr->get_args().size() == 2 );
 | 
|---|
| 253 |                                         (*arg++)->accept( *this );
 | 
|---|
| 254 |                                         output << "[";
 | 
|---|
| 255 |                                         (*arg)->accept( *this );
 | 
|---|
| 256 |                                         output << "]";
 | 
|---|
| 257 |                                         break;
 | 
|---|
| 258 |               
 | 
|---|
| 259 |                                   case OT_CALL:
 | 
|---|
| 260 |                                         // there are no intrinsic definitions of the function call operator
 | 
|---|
| 261 |                                         assert( false );
 | 
|---|
| 262 |                                         break;
 | 
|---|
| 263 |               
 | 
|---|
| 264 |                                   case OT_PREFIX:
 | 
|---|
| 265 |                                   case OT_PREFIXASSIGN:
 | 
|---|
| 266 |                                         assert( applicationExpr->get_args().size() == 1 );
 | 
|---|
| 267 |                                         output << "(";
 | 
|---|
| 268 |                                         output << opInfo.symbol;
 | 
|---|
| 269 |                                         (*arg)->accept( *this );
 | 
|---|
| 270 |                                         output << ")";
 | 
|---|
| 271 |                                         break;
 | 
|---|
| 272 |               
 | 
|---|
| 273 |                                   case OT_POSTFIX:
 | 
|---|
| 274 |                                   case OT_POSTFIXASSIGN:
 | 
|---|
| 275 |                                         assert( applicationExpr->get_args().size() == 1 );
 | 
|---|
| 276 |                                         (*arg)->accept( *this );
 | 
|---|
| 277 |                                         output << opInfo.symbol;
 | 
|---|
| 278 |                                         break;
 | 
|---|
| 279 | 
 | 
|---|
| 280 |                                   case OT_INFIX:
 | 
|---|
| 281 |                                   case OT_INFIXASSIGN:
 | 
|---|
| 282 |                                         assert( applicationExpr->get_args().size() == 2 );
 | 
|---|
| 283 |                                         output << "(";
 | 
|---|
| 284 |                                         (*arg++)->accept( *this );
 | 
|---|
| 285 |                                         output << opInfo.symbol;
 | 
|---|
| 286 |                                         (*arg)->accept( *this );
 | 
|---|
| 287 |                                         output << ")";
 | 
|---|
| 288 |                                         break;
 | 
|---|
| 289 |               
 | 
|---|
| 290 |                                   case OT_CONSTANT:
 | 
|---|
| 291 |                                   case OT_LABELADDRESS:
 | 
|---|
| 292 |                                         // there are no intrinsic definitions of 0/1 or label addresses as functions
 | 
|---|
| 293 |                                         assert( false );
 | 
|---|
| 294 |                                 }
 | 
|---|
| 295 |                         } else {
 | 
|---|
| 296 |                                 varExpr->accept( *this );
 | 
|---|
| 297 |                                 output << "(";
 | 
|---|
| 298 |                                 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
 | 
|---|
| 299 |                                 output << ")";
 | 
|---|
| 300 |                         } // if
 | 
|---|
| 301 |                 } else {
 | 
|---|
| 302 |                         applicationExpr->get_function()->accept( *this );
 | 
|---|
| 303 |                         output << "(";
 | 
|---|
| 304 |                         genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
 | 
|---|
| 305 |                         output << ")";
 | 
|---|
| 306 |                 } // if
 | 
|---|
| 307 |         }
 | 
|---|
| 308 |   
 | 
|---|
| 309 |         void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
 | 
|---|
| 310 |                 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
 | 
|---|
| 311 |                         OperatorInfo opInfo;
 | 
|---|
| 312 |                         if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
 | 
|---|
| 313 |                                 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
 | 
|---|
| 314 |                                 switch ( opInfo.type ) {
 | 
|---|
| 315 |                                   case OT_INDEX:
 | 
|---|
| 316 |                                         assert( untypedExpr->get_args().size() == 2 );
 | 
|---|
| 317 |                                         (*arg++)->accept( *this );
 | 
|---|
| 318 |                                         output << "[";
 | 
|---|
| 319 |                                         (*arg)->accept( *this );
 | 
|---|
| 320 |                                         output << "]";
 | 
|---|
| 321 |                                         break;
 | 
|---|
| 322 |               
 | 
|---|
| 323 |                                   case OT_CALL:
 | 
|---|
| 324 |                                         assert( false );
 | 
|---|
| 325 |                                         break;
 | 
|---|
| 326 |               
 | 
|---|
| 327 |                                   case OT_PREFIX:
 | 
|---|
| 328 |                                   case OT_PREFIXASSIGN:
 | 
|---|
| 329 |                                   case OT_LABELADDRESS:
 | 
|---|
| 330 |                                         assert( untypedExpr->get_args().size() == 1 );
 | 
|---|
| 331 |                                         output << "(";
 | 
|---|
| 332 |                                         output << opInfo.symbol;
 | 
|---|
| 333 |                                         (*arg)->accept( *this );
 | 
|---|
| 334 |                                         output << ")";
 | 
|---|
| 335 |                                         break;
 | 
|---|
| 336 |               
 | 
|---|
| 337 |                                   case OT_POSTFIX:
 | 
|---|
| 338 |                                   case OT_POSTFIXASSIGN:
 | 
|---|
| 339 |                                         assert( untypedExpr->get_args().size() == 1 );
 | 
|---|
| 340 |                                         (*arg)->accept( *this );
 | 
|---|
| 341 |                                         output << opInfo.symbol;
 | 
|---|
| 342 |                                         break;
 | 
|---|
| 343 |   
 | 
|---|
| 344 |                                   case OT_INFIX:
 | 
|---|
| 345 |                                   case OT_INFIXASSIGN:
 | 
|---|
| 346 |                                         assert( untypedExpr->get_args().size() == 2 );
 | 
|---|
| 347 |                                         output << "(";
 | 
|---|
| 348 |                                         (*arg++)->accept( *this );
 | 
|---|
| 349 |                                         output << opInfo.symbol;
 | 
|---|
| 350 |                                         (*arg)->accept( *this );
 | 
|---|
| 351 |                                         output << ")";
 | 
|---|
| 352 |                                         break;
 | 
|---|
| 353 |                                         
 | 
|---|
| 354 |                                   case OT_CONSTANT:
 | 
|---|
| 355 |                                         // there are no intrinsic definitions of 0 or 1 as functions
 | 
|---|
| 356 |                                         assert( false );
 | 
|---|
| 357 |                                 }
 | 
|---|
| 358 |                         } else {
 | 
|---|
| 359 |                                 nameExpr->accept( *this );
 | 
|---|
| 360 |                                 output << "(";
 | 
|---|
| 361 |                                 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
 | 
|---|
| 362 |                                 output << ")";
 | 
|---|
| 363 |                         } // if
 | 
|---|
| 364 |                 } else {
 | 
|---|
| 365 |                         untypedExpr->get_function()->accept( *this );
 | 
|---|
| 366 |                         output << "(";
 | 
|---|
| 367 |                         genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
 | 
|---|
| 368 |                         output << ")";
 | 
|---|
| 369 |                 } // if
 | 
|---|
| 370 |         }
 | 
|---|
| 371 |   
 | 
|---|
| 372 |         void CodeGenerator::visit( NameExpr *nameExpr ) {
 | 
|---|
| 373 |                 OperatorInfo opInfo;
 | 
|---|
| 374 |                 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
 | 
|---|
| 375 |                         assert( opInfo.type == OT_CONSTANT );
 | 
|---|
| 376 |                         output << opInfo.symbol;
 | 
|---|
| 377 |                 } else {
 | 
|---|
| 378 |                         output << nameExpr->get_name();
 | 
|---|
| 379 |                 } // if
 | 
|---|
| 380 |         }
 | 
|---|
| 381 |   
 | 
|---|
| 382 |         void CodeGenerator::visit( AddressExpr *addressExpr ) {
 | 
|---|
| 383 |                 output << "(&";
 | 
|---|
| 384 |                 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
 | 
|---|
| 385 |                 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
 | 
|---|
| 386 |                         output << mangleName( variableExpr->get_var() );
 | 
|---|
| 387 |                 } else {
 | 
|---|
| 388 |                         addressExpr->get_arg()->accept( *this );
 | 
|---|
| 389 |                 } // if
 | 
|---|
| 390 |                 output << ")";
 | 
|---|
| 391 |         }
 | 
|---|
| 392 | 
 | 
|---|
| 393 |         void CodeGenerator::visit( CastExpr *castExpr ) {
 | 
|---|
| 394 |                 // if the cast is to an lvalue type, then the cast
 | 
|---|
| 395 |                 // should be dropped, since the result of a cast is
 | 
|---|
| 396 |                 // never an lvalue in C
 | 
|---|
| 397 |                 if ( castExpr->get_results().front()->get_isLvalue() ) {
 | 
|---|
| 398 |                         castExpr->get_arg()->accept( *this );
 | 
|---|
| 399 |                 } else {
 | 
|---|
| 400 |                         output << "((";
 | 
|---|
| 401 |                         if ( castExpr->get_results().empty() ) {
 | 
|---|
| 402 |                                 output << "void" ;
 | 
|---|
| 403 |                         } else {
 | 
|---|
| 404 |                                 output << genType( castExpr->get_results().front(), "" );
 | 
|---|
| 405 |                         } // if
 | 
|---|
| 406 |                         output << ")";
 | 
|---|
| 407 |                         castExpr->get_arg()->accept( *this );
 | 
|---|
| 408 |                         output << ")";                  
 | 
|---|
| 409 |                 }
 | 
|---|
| 410 |         }
 | 
|---|
| 411 |   
 | 
|---|
| 412 |         void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
 | 
|---|
| 413 |                 assert( false );
 | 
|---|
| 414 |         }
 | 
|---|
| 415 |   
 | 
|---|
| 416 |         void CodeGenerator::visit( MemberExpr *memberExpr ) {
 | 
|---|
| 417 |                 memberExpr->get_aggregate()->accept( *this );
 | 
|---|
| 418 |                 output << "." << mangleName( memberExpr->get_member() );
 | 
|---|
| 419 |         }
 | 
|---|
| 420 |   
 | 
|---|
| 421 |         void CodeGenerator::visit( VariableExpr *variableExpr ) {
 | 
|---|
| 422 |                 OperatorInfo opInfo;
 | 
|---|
| 423 |                 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
 | 
|---|
| 424 |                         output << opInfo.symbol;
 | 
|---|
| 425 |                 } else {
 | 
|---|
| 426 |                         output << mangleName( variableExpr->get_var() );
 | 
|---|
| 427 |                 } // if
 | 
|---|
| 428 |         }
 | 
|---|
| 429 |   
 | 
|---|
| 430 |         void CodeGenerator::visit( ConstantExpr *constantExpr ) {
 | 
|---|
| 431 |                 assert( constantExpr->get_constant() );
 | 
|---|
| 432 |                 constantExpr->get_constant()->accept( *this );
 | 
|---|
| 433 |         }
 | 
|---|
| 434 |   
 | 
|---|
| 435 |         void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
 | 
|---|
| 436 |                 output << "sizeof(";
 | 
|---|
| 437 |                 if ( sizeofExpr->get_isType() ) {
 | 
|---|
| 438 |                         output << genType( sizeofExpr->get_type(), "" );
 | 
|---|
| 439 |                 } else {
 | 
|---|
| 440 |                         sizeofExpr->get_expr()->accept( *this );
 | 
|---|
| 441 |                 } // if
 | 
|---|
| 442 |                 output << ")";
 | 
|---|
| 443 |         }
 | 
|---|
| 444 |   
 | 
|---|
| 445 |         void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
 | 
|---|
| 446 |                 output << "(";
 | 
|---|
| 447 |                 logicalExpr->get_arg1()->accept( *this );
 | 
|---|
| 448 |                 if ( logicalExpr->get_isAnd() ) {
 | 
|---|
| 449 |                         output << " && ";
 | 
|---|
| 450 |                 } else {
 | 
|---|
| 451 |                         output << " || ";
 | 
|---|
| 452 |                 } // if
 | 
|---|
| 453 |                 logicalExpr->get_arg2()->accept( *this );
 | 
|---|
| 454 |                 output << ")";
 | 
|---|
| 455 |         }
 | 
|---|
| 456 |   
 | 
|---|
| 457 |         void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
 | 
|---|
| 458 |                 output << "(";
 | 
|---|
| 459 |                 conditionalExpr->get_arg1()->accept( *this );
 | 
|---|
| 460 |                 output << " ? ";
 | 
|---|
| 461 |                 conditionalExpr->get_arg2()->accept( *this );
 | 
|---|
| 462 |                 output << " : ";
 | 
|---|
| 463 |                 conditionalExpr->get_arg3()->accept( *this );
 | 
|---|
| 464 |                 output << ")";
 | 
|---|
| 465 |         }
 | 
|---|
| 466 |   
 | 
|---|
| 467 |         void CodeGenerator::visit( CommaExpr *commaExpr ) {
 | 
|---|
| 468 |                 output << "(";
 | 
|---|
| 469 |                 commaExpr->get_arg1()->accept( *this );
 | 
|---|
| 470 |                 output << " , ";
 | 
|---|
| 471 |                 commaExpr->get_arg2()->accept( *this );
 | 
|---|
| 472 |                 output << ")";
 | 
|---|
| 473 |         }
 | 
|---|
| 474 |   
 | 
|---|
| 475 |         void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
 | 
|---|
| 476 |   
 | 
|---|
| 477 |         void CodeGenerator::visit( TypeExpr *typeExpr ) {}
 | 
|---|
| 478 | 
 | 
|---|
| 479 |         void CodeGenerator::visit( AsmExpr *asmExpr ) {
 | 
|---|
| 480 |                 if ( asmExpr->get_inout() ) {
 | 
|---|
| 481 |                         output << "[ ";
 | 
|---|
| 482 |                         asmExpr->get_inout()->accept( *this );
 | 
|---|
| 483 |                         output << " ] ";
 | 
|---|
| 484 |                 } // if
 | 
|---|
| 485 |                 asmExpr->get_constraint()->accept( *this );
 | 
|---|
| 486 |                 output << " ( ";
 | 
|---|
| 487 |                 asmExpr->get_operand()->accept( *this );
 | 
|---|
| 488 |                 output << " )";
 | 
|---|
| 489 |         }
 | 
|---|
| 490 | 
 | 
|---|
| 491 |         //*** Statements
 | 
|---|
| 492 |         void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
 | 
|---|
| 493 |                 std::list<Statement*> ks = compoundStmt->get_kids();
 | 
|---|
| 494 |                 output << "{" << endl;
 | 
|---|
| 495 | 
 | 
|---|
| 496 |                 cur_indent += CodeGenerator::tabsize;
 | 
|---|
| 497 | 
 | 
|---|
| 498 |                 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++ ) {
 | 
|---|
| 499 |                         output << indent << printLabels( (*i)->get_labels() );
 | 
|---|
| 500 |                         (*i)->accept( *this );
 | 
|---|
| 501 | 
 | 
|---|
| 502 |                         output << endl;
 | 
|---|
| 503 |                         if ( wantSpacing( *i ) ) {
 | 
|---|
| 504 |                                 output << endl;
 | 
|---|
| 505 |                         }
 | 
|---|
| 506 |                 }
 | 
|---|
| 507 |                 cur_indent -= CodeGenerator::tabsize; 
 | 
|---|
| 508 | 
 | 
|---|
| 509 |                 output << indent << "}";
 | 
|---|
| 510 |         }
 | 
|---|
| 511 | 
 | 
|---|
| 512 |         void CodeGenerator::visit( ExprStmt *exprStmt ) {
 | 
|---|
| 513 |                 // I don't see why this check is necessary. 
 | 
|---|
| 514 |                 // If this starts to cause problems then put it back in, 
 | 
|---|
| 515 |                 // with an explanation
 | 
|---|
| 516 |                 assert( exprStmt );
 | 
|---|
| 517 | 
 | 
|---|
| 518 |                 // if ( exprStmt != 0 ) {
 | 
|---|
| 519 |                 exprStmt->get_expr()->accept( *this );
 | 
|---|
| 520 |                 output << ";" ;
 | 
|---|
| 521 |                 // } // if
 | 
|---|
| 522 |         }
 | 
|---|
| 523 | 
 | 
|---|
| 524 |         void CodeGenerator::visit( AsmStmt *asmStmt ) {
 | 
|---|
| 525 |                 output << "asm ";
 | 
|---|
| 526 |                 if ( asmStmt->get_voltile() ) output << "volatile ";
 | 
|---|
| 527 |                 if ( ! asmStmt->get_gotolabels().empty()  ) output << "goto ";
 | 
|---|
| 528 |                 output << "( ";
 | 
|---|
| 529 |                 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
 | 
|---|
| 530 |                 output << " : ";
 | 
|---|
| 531 |                 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
 | 
|---|
| 532 |                 output << " : ";
 | 
|---|
| 533 |                 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
 | 
|---|
| 534 |                 output << " : ";
 | 
|---|
| 535 |                 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
 | 
|---|
| 536 |                 if ( ! asmStmt->get_gotolabels().empty() ) {
 | 
|---|
| 537 |                         output << " : ";
 | 
|---|
| 538 |                         for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
 | 
|---|
| 539 |                                 output << *begin++;
 | 
|---|
| 540 |                                 if ( begin == asmStmt->get_gotolabels().end() ) break;
 | 
|---|
| 541 |                                 output << ", ";
 | 
|---|
| 542 |                         } // for
 | 
|---|
| 543 |                 } // if
 | 
|---|
| 544 |                 output << " );" ;
 | 
|---|
| 545 |         }
 | 
|---|
| 546 | 
 | 
|---|
| 547 |         void CodeGenerator::visit( IfStmt *ifStmt ) {
 | 
|---|
| 548 |                 output << "if ( ";
 | 
|---|
| 549 |                 ifStmt->get_condition()->accept( *this );
 | 
|---|
| 550 |                 output << " ) ";
 | 
|---|
| 551 | 
 | 
|---|
| 552 |                 ifStmt->get_thenPart()->accept( *this );
 | 
|---|
| 553 | 
 | 
|---|
| 554 |                 if ( ifStmt->get_elsePart() != 0) {
 | 
|---|
| 555 |                         output << " else ";
 | 
|---|
| 556 |                         ifStmt->get_elsePart()->accept( *this );
 | 
|---|
| 557 |                 } // if
 | 
|---|
| 558 |         }
 | 
|---|
| 559 | 
 | 
|---|
| 560 |         void CodeGenerator::visit( SwitchStmt *switchStmt ) {
 | 
|---|
| 561 |                 output << "switch ( " ;
 | 
|---|
| 562 |                 switchStmt->get_condition()->accept( *this );
 | 
|---|
| 563 |                 output << " ) ";
 | 
|---|
| 564 |                 
 | 
|---|
| 565 |                 output << "{" << std::endl;
 | 
|---|
| 566 |                 cur_indent += CodeGenerator::tabsize;
 | 
|---|
| 567 | 
 | 
|---|
| 568 |                 acceptAll( switchStmt->get_branches(), *this );
 | 
|---|
| 569 | 
 | 
|---|
| 570 |                 cur_indent -= CodeGenerator::tabsize;
 | 
|---|
| 571 | 
 | 
|---|
| 572 |                 output << indent << "}";
 | 
|---|
| 573 |         }
 | 
|---|
| 574 | 
 | 
|---|
| 575 |         void CodeGenerator::visit( CaseStmt *caseStmt ) {
 | 
|---|
| 576 |                 output << indent;
 | 
|---|
| 577 |                 if ( caseStmt->isDefault()) {
 | 
|---|
| 578 |                         output << "default";
 | 
|---|
| 579 |                 } else {
 | 
|---|
| 580 |                         output << "case ";
 | 
|---|
| 581 |                         caseStmt->get_condition()->accept( *this );
 | 
|---|
| 582 |                 } // if
 | 
|---|
| 583 |                 output << ":\n";
 | 
|---|
| 584 |                 
 | 
|---|
| 585 |                 std::list<Statement *> sts = caseStmt->get_statements();
 | 
|---|
| 586 | 
 | 
|---|
| 587 |                 cur_indent += CodeGenerator::tabsize;
 | 
|---|
| 588 |                 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
 | 
|---|
| 589 |                         output << indent << printLabels( (*i)->get_labels() )  ;
 | 
|---|
| 590 |                         (*i)->accept( *this );
 | 
|---|
| 591 |                         output << endl;
 | 
|---|
| 592 |                 }
 | 
|---|
| 593 |                 cur_indent -= CodeGenerator::tabsize;
 | 
|---|
| 594 |         }
 | 
|---|
| 595 | 
 | 
|---|
| 596 |         void CodeGenerator::visit( BranchStmt *branchStmt ) {
 | 
|---|
| 597 |                 switch ( branchStmt->get_type()) {
 | 
|---|
| 598 |                   case BranchStmt::Goto:
 | 
|---|
| 599 |                         if ( ! branchStmt->get_target().empty() )
 | 
|---|
| 600 |                                 output << "goto " << branchStmt->get_target();
 | 
|---|
| 601 |                         else { 
 | 
|---|
| 602 |                                 if ( branchStmt->get_computedTarget() != 0 ) {
 | 
|---|
| 603 |                                         output << "goto *";
 | 
|---|
| 604 |                                         branchStmt->get_computedTarget()->accept( *this );
 | 
|---|
| 605 |                                 } // if
 | 
|---|
| 606 |                         } // if
 | 
|---|
| 607 |                         break;
 | 
|---|
| 608 |                   case BranchStmt::Break:
 | 
|---|
| 609 |                         output << "break";
 | 
|---|
| 610 |                         break;
 | 
|---|
| 611 |                   case BranchStmt::Continue:
 | 
|---|
| 612 |                         output << "continue";
 | 
|---|
| 613 |                         break;
 | 
|---|
| 614 |                 }
 | 
|---|
| 615 |                 output << ";";
 | 
|---|
| 616 |         }
 | 
|---|
| 617 | 
 | 
|---|
| 618 | 
 | 
|---|
| 619 |         void CodeGenerator::visit( ReturnStmt *returnStmt ) {
 | 
|---|
| 620 |                 output << "return ";
 | 
|---|
| 621 | 
 | 
|---|
| 622 |                 // xxx -- check for null expression;
 | 
|---|
| 623 |                 if ( returnStmt->get_expr() ) {
 | 
|---|
| 624 |                         returnStmt->get_expr()->accept( *this );
 | 
|---|
| 625 |                 } // if
 | 
|---|
| 626 |                 output << ";";
 | 
|---|
| 627 |         }
 | 
|---|
| 628 | 
 | 
|---|
| 629 |         void CodeGenerator::visit( WhileStmt *whileStmt ) {
 | 
|---|
| 630 |                 if ( whileStmt->get_isDoWhile() )
 | 
|---|
| 631 |                         output << "do" ;
 | 
|---|
| 632 |                 else {
 | 
|---|
| 633 |                         output << "while (" ;
 | 
|---|
| 634 |                         whileStmt->get_condition()->accept( *this );
 | 
|---|
| 635 |                         output << ")";
 | 
|---|
| 636 |                 } // if
 | 
|---|
| 637 |                 output << " ";
 | 
|---|
| 638 | 
 | 
|---|
| 639 |                 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
 | 
|---|
| 640 |                 whileStmt->get_body()->accept( *this );
 | 
|---|
| 641 | 
 | 
|---|
| 642 |                 output << indent;
 | 
|---|
| 643 | 
 | 
|---|
| 644 |                 if ( whileStmt->get_isDoWhile() ) {
 | 
|---|
| 645 |                         output << " while (" ;
 | 
|---|
| 646 |                         whileStmt->get_condition()->accept( *this );
 | 
|---|
| 647 |                         output << ");";
 | 
|---|
| 648 |                 } // if
 | 
|---|
| 649 |         }
 | 
|---|
| 650 | 
 | 
|---|
| 651 |         void CodeGenerator::visit( ForStmt *forStmt ) {
 | 
|---|
| 652 |                 // initialization is always hoisted, so don't 
 | 
|---|
| 653 |                 // bother doing anything with that 
 | 
|---|
| 654 |                 output << "for (;";
 | 
|---|
| 655 | 
 | 
|---|
| 656 |                 if ( forStmt->get_condition() != 0 )
 | 
|---|
| 657 |                         forStmt->get_condition()->accept( *this );
 | 
|---|
| 658 |                 output << ";";
 | 
|---|
| 659 | 
 | 
|---|
| 660 |                 if ( forStmt->get_increment() != 0 )
 | 
|---|
| 661 |                         forStmt->get_increment()->accept( *this );
 | 
|---|
| 662 |                 output << ") ";
 | 
|---|
| 663 | 
 | 
|---|
| 664 |                 if ( forStmt->get_body() != 0 ) {
 | 
|---|
| 665 |                         output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
 | 
|---|
| 666 |                         forStmt->get_body()->accept( *this );
 | 
|---|
| 667 |                 } // if
 | 
|---|
| 668 |         }
 | 
|---|
| 669 | 
 | 
|---|
| 670 |         void CodeGenerator::visit( NullStmt *nullStmt ) {
 | 
|---|
| 671 |                 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
 | 
|---|
| 672 |                 output << "/* null statement */ ;";
 | 
|---|
| 673 |         }
 | 
|---|
| 674 | 
 | 
|---|
| 675 |         void CodeGenerator::visit( DeclStmt *declStmt ) {
 | 
|---|
| 676 |                 declStmt->get_decl()->accept( *this );
 | 
|---|
| 677 |         
 | 
|---|
| 678 |                 if ( doSemicolon( declStmt->get_decl() ) ) {
 | 
|---|
| 679 |                         output << ";";
 | 
|---|
| 680 |                 } // if
 | 
|---|
| 681 |         }
 | 
|---|
| 682 | 
 | 
|---|
| 683 |         std::string CodeGenerator::printLabels( std::list< Label > &l ) {
 | 
|---|
| 684 |                 std::string str( "" );
 | 
|---|
| 685 |                 l.unique(); // assumes a sorted list. Why not use set?
 | 
|---|
| 686 | 
 | 
|---|
| 687 |                 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
 | 
|---|
| 688 |                         str += *i + ": ";
 | 
|---|
| 689 | 
 | 
|---|
| 690 |                 return str;
 | 
|---|
| 691 |         }
 | 
|---|
| 692 | 
 | 
|---|
| 693 |         void CodeGenerator::handleStorageClass( Declaration *decl ) {
 | 
|---|
| 694 |                 switch ( decl->get_storageClass() ) {
 | 
|---|
| 695 |                   case DeclarationNode::Extern:
 | 
|---|
| 696 |                         output << "extern ";
 | 
|---|
| 697 |                         break;
 | 
|---|
| 698 |                   case DeclarationNode::Static:
 | 
|---|
| 699 |                         output << "static ";
 | 
|---|
| 700 |                         break;
 | 
|---|
| 701 |                   case DeclarationNode::Auto:
 | 
|---|
| 702 |                         // silently drop storage class
 | 
|---|
| 703 |                         break;
 | 
|---|
| 704 |                   case DeclarationNode::Register:
 | 
|---|
| 705 |                         output << "register ";
 | 
|---|
| 706 |                         break;
 | 
|---|
| 707 |                   case DeclarationNode::Inline:
 | 
|---|
| 708 |                         output << "inline ";
 | 
|---|
| 709 |                         break;
 | 
|---|
| 710 |                   case DeclarationNode::Fortran:
 | 
|---|
| 711 |                         output << "fortran ";
 | 
|---|
| 712 |                         break;
 | 
|---|
| 713 |                   case DeclarationNode::Noreturn:
 | 
|---|
| 714 |                         output << "_Noreturn ";
 | 
|---|
| 715 |                         break;
 | 
|---|
| 716 |                   case DeclarationNode::Threadlocal:
 | 
|---|
| 717 |                         output << "_Thread_local ";
 | 
|---|
| 718 |                         break;
 | 
|---|
| 719 |                   case DeclarationNode::NoStorageClass:
 | 
|---|
| 720 |                         break;
 | 
|---|
| 721 |                 } // switch
 | 
|---|
| 722 |         }
 | 
|---|
| 723 | } // namespace CodeGen
 | 
|---|
| 724 | 
 | 
|---|
| 725 | // Local Variables: //
 | 
|---|
| 726 | // tab-width: 4 //
 | 
|---|
| 727 | // mode: c++ //
 | 
|---|
| 728 | // compile-command: "make install" //
 | 
|---|
| 729 | // End: //
 | 
|---|