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