[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 |
---|
| 12 | // Last Modified On : Wed Jul 15 14:47:42 2015 |
---|
| 13 | // Update Count : 177 |
---|
[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 | |
---|
[cda48b6] | 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 ) { |
---|
[6c4ff37] | 58 | //output << std::string( init ); |
---|
[51587aa] | 59 | } |
---|
| 60 | |
---|
[cda48b6] | 61 | CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp ) |
---|
| 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() ) { |
---|
| 93 | functionDecl->get_statements()->accept(*this ); |
---|
| 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; |
---|
[51587aa] | 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 << " = "; |
---|
[51587aa] | 161 | obj->get_init()->accept(*this ); |
---|
| 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 | |
---|
[6c4ff37] | 188 | void CodeGenerator::visit( SingleInit *init ) { |
---|
[51587aa] | 189 | init->get_value()->accept( *this ); |
---|
| 190 | } |
---|
| 191 | |
---|
[6c4ff37] | 192 | void CodeGenerator::visit( ListInit *init ) { |
---|
| 193 | output << "{ "; |
---|
[51587aa] | 194 | genCommaList( init->begin_initializers(), init->end_initializers() ); |
---|
[6c4ff37] | 195 | output << " }"; |
---|
[51587aa] | 196 | } |
---|
| 197 | |
---|
[6c4ff37] | 198 | void CodeGenerator::visit( Constant *constant ) { |
---|
| 199 | output << constant->get_value() ; |
---|
[51587aa] | 200 | } |
---|
| 201 | |
---|
| 202 | //*** Expressions |
---|
[6c4ff37] | 203 | void CodeGenerator::visit( ApplicationExpr *applicationExpr ) { |
---|
[51587aa] | 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 ); |
---|
[6c4ff37] | 234 | output << "["; |
---|
[51587aa] | 235 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 236 | output << "]"; |
---|
[51587aa] | 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 ); |
---|
[6c4ff37] | 247 | output << "("; |
---|
| 248 | output << opInfo.symbol; |
---|
[51587aa] | 249 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 250 | output << ")"; |
---|
[51587aa] | 251 | break; |
---|
| 252 | |
---|
| 253 | case OT_POSTFIX: |
---|
| 254 | case OT_POSTFIXASSIGN: |
---|
| 255 | assert( applicationExpr->get_args().size() == 1 ); |
---|
| 256 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 257 | output << opInfo.symbol; |
---|
[51587aa] | 258 | break; |
---|
| 259 | |
---|
| 260 | case OT_INFIX: |
---|
| 261 | case OT_INFIXASSIGN: |
---|
| 262 | assert( applicationExpr->get_args().size() == 2 ); |
---|
[6c4ff37] | 263 | output << "("; |
---|
[51587aa] | 264 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 265 | output << opInfo.symbol; |
---|
[51587aa] | 266 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 267 | output << ")"; |
---|
[51587aa] | 268 | break; |
---|
| 269 | |
---|
| 270 | case OT_CONSTANT: |
---|
[721f17a] | 271 | case OT_LABELADDRESS: |
---|
| 272 | // there are no intrinsic definitions of 0/1 or label addresses as functions |
---|
[51587aa] | 273 | assert( false ); |
---|
| 274 | } |
---|
[17cd4eb] | 275 | } else { |
---|
[51587aa] | 276 | varExpr->accept( *this ); |
---|
[6c4ff37] | 277 | output << "("; |
---|
[51587aa] | 278 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 279 | output << ")"; |
---|
[17cd4eb] | 280 | } // if |
---|
[51587aa] | 281 | } else { |
---|
| 282 | applicationExpr->get_function()->accept( *this ); |
---|
[6c4ff37] | 283 | output << "("; |
---|
[51587aa] | 284 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 285 | output << ")"; |
---|
[51587aa] | 286 | } // if |
---|
| 287 | } |
---|
[51b7345] | 288 | |
---|
[6c4ff37] | 289 | void CodeGenerator::visit( UntypedExpr *untypedExpr ) { |
---|
[51587aa] | 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 ); |
---|
[6c4ff37] | 298 | output << "["; |
---|
[51587aa] | 299 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 300 | output << "]"; |
---|
[51587aa] | 301 | break; |
---|
| 302 | |
---|
| 303 | case OT_CALL: |
---|
| 304 | assert( false ); |
---|
| 305 | break; |
---|
| 306 | |
---|
| 307 | case OT_PREFIX: |
---|
| 308 | case OT_PREFIXASSIGN: |
---|
[de62360d] | 309 | case OT_LABELADDRESS: |
---|
[51587aa] | 310 | assert( untypedExpr->get_args().size() == 1 ); |
---|
[6c4ff37] | 311 | output << "("; |
---|
| 312 | output << opInfo.symbol; |
---|
[51587aa] | 313 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 314 | output << ")"; |
---|
[51587aa] | 315 | break; |
---|
| 316 | |
---|
| 317 | case OT_POSTFIX: |
---|
| 318 | case OT_POSTFIXASSIGN: |
---|
| 319 | assert( untypedExpr->get_args().size() == 1 ); |
---|
| 320 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 321 | output << opInfo.symbol; |
---|
[51587aa] | 322 | break; |
---|
[51b7345] | 323 | |
---|
[51587aa] | 324 | case OT_INFIX: |
---|
| 325 | case OT_INFIXASSIGN: |
---|
| 326 | assert( untypedExpr->get_args().size() == 2 ); |
---|
[6c4ff37] | 327 | output << "("; |
---|
[51587aa] | 328 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 329 | output << opInfo.symbol; |
---|
[51587aa] | 330 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 331 | output << ")"; |
---|
[51587aa] | 332 | break; |
---|
[de62360d] | 333 | |
---|
[51587aa] | 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 ); |
---|
[6c4ff37] | 340 | output << "("; |
---|
[51587aa] | 341 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); |
---|
[6c4ff37] | 342 | output << ")"; |
---|
[51587aa] | 343 | } // if |
---|
| 344 | } else { |
---|
| 345 | untypedExpr->get_function()->accept( *this ); |
---|
[6c4ff37] | 346 | output << "("; |
---|
[51587aa] | 347 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); |
---|
[6c4ff37] | 348 | output << ")"; |
---|
[51587aa] | 349 | } // if |
---|
| 350 | } |
---|
[51b7345] | 351 | |
---|
[6c4ff37] | 352 | void CodeGenerator::visit( NameExpr *nameExpr ) { |
---|
[51587aa] | 353 | OperatorInfo opInfo; |
---|
| 354 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { |
---|
| 355 | assert( opInfo.type == OT_CONSTANT ); |
---|
[6c4ff37] | 356 | output << opInfo.symbol; |
---|
[51587aa] | 357 | } else { |
---|
[6c4ff37] | 358 | output << nameExpr->get_name(); |
---|
[51587aa] | 359 | } // if |
---|
| 360 | } |
---|
[51b7345] | 361 | |
---|
[6c4ff37] | 362 | void CodeGenerator::visit( AddressExpr *addressExpr ) { |
---|
| 363 | output << "(&"; |
---|
[51587aa] | 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() ) ) { |
---|
[6c4ff37] | 366 | output << mangleName( variableExpr->get_var() ); |
---|
[51587aa] | 367 | } else { |
---|
| 368 | addressExpr->get_arg()->accept( *this ); |
---|
| 369 | } // if |
---|
[6c4ff37] | 370 | output << ")"; |
---|
[51587aa] | 371 | } |
---|
| 372 | |
---|
[6c4ff37] | 373 | void CodeGenerator::visit( CastExpr *castExpr ) { |
---|
| 374 | output << "(("; |
---|
[51587aa] | 375 | if ( castExpr->get_results().empty() ) { |
---|
[6c4ff37] | 376 | output << "void" ; |
---|
[51587aa] | 377 | } else { |
---|
[6c4ff37] | 378 | output << genType( castExpr->get_results().front(), "" ); |
---|
[51587aa] | 379 | } // if |
---|
[6c4ff37] | 380 | output << ")"; |
---|
[51587aa] | 381 | castExpr->get_arg()->accept( *this ); |
---|
[6c4ff37] | 382 | output << ")"; |
---|
[51587aa] | 383 | } |
---|
[51b7345] | 384 | |
---|
[6c4ff37] | 385 | void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) { |
---|
[51587aa] | 386 | assert( false ); |
---|
| 387 | } |
---|
[51b7345] | 388 | |
---|
[6c4ff37] | 389 | void CodeGenerator::visit( MemberExpr *memberExpr ) { |
---|
[51587aa] | 390 | memberExpr->get_aggregate()->accept( *this ); |
---|
[6c4ff37] | 391 | output << "." << mangleName( memberExpr->get_member() ); |
---|
[51587aa] | 392 | } |
---|
[51b7345] | 393 | |
---|
[6c4ff37] | 394 | void CodeGenerator::visit( VariableExpr *variableExpr ) { |
---|
[51587aa] | 395 | OperatorInfo opInfo; |
---|
| 396 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { |
---|
[6c4ff37] | 397 | output << opInfo.symbol; |
---|
[51587aa] | 398 | } else { |
---|
[6c4ff37] | 399 | output << mangleName( variableExpr->get_var() ); |
---|
[51587aa] | 400 | } // if |
---|
| 401 | } |
---|
[51b7345] | 402 | |
---|
[6c4ff37] | 403 | void CodeGenerator::visit( ConstantExpr *constantExpr ) { |
---|
[51587aa] | 404 | assert( constantExpr->get_constant() ); |
---|
| 405 | constantExpr->get_constant()->accept( *this ); |
---|
| 406 | } |
---|
[51b7345] | 407 | |
---|
[6c4ff37] | 408 | void CodeGenerator::visit( SizeofExpr *sizeofExpr ) { |
---|
| 409 | output << "sizeof("; |
---|
[51587aa] | 410 | if ( sizeofExpr->get_isType() ) { |
---|
[6c4ff37] | 411 | output << genType( sizeofExpr->get_type(), "" ); |
---|
[51587aa] | 412 | } else { |
---|
| 413 | sizeofExpr->get_expr()->accept( *this ); |
---|
| 414 | } // if |
---|
[6c4ff37] | 415 | output << ")"; |
---|
[51587aa] | 416 | } |
---|
[51b7345] | 417 | |
---|
[6c4ff37] | 418 | void CodeGenerator::visit( LogicalExpr *logicalExpr ) { |
---|
| 419 | output << "("; |
---|
[51587aa] | 420 | logicalExpr->get_arg1()->accept( *this ); |
---|
| 421 | if ( logicalExpr->get_isAnd() ) { |
---|
[6c4ff37] | 422 | output << " && "; |
---|
[51587aa] | 423 | } else { |
---|
[6c4ff37] | 424 | output << " || "; |
---|
[51587aa] | 425 | } // if |
---|
| 426 | logicalExpr->get_arg2()->accept( *this ); |
---|
[6c4ff37] | 427 | output << ")"; |
---|
[51587aa] | 428 | } |
---|
[51b7345] | 429 | |
---|
[6c4ff37] | 430 | void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) { |
---|
| 431 | output << "("; |
---|
[51587aa] | 432 | conditionalExpr->get_arg1()->accept( *this ); |
---|
[6c4ff37] | 433 | output << " ? "; |
---|
[51587aa] | 434 | conditionalExpr->get_arg2()->accept( *this ); |
---|
[6c4ff37] | 435 | output << " : "; |
---|
[51587aa] | 436 | conditionalExpr->get_arg3()->accept( *this ); |
---|
[6c4ff37] | 437 | output << ")"; |
---|
[51587aa] | 438 | } |
---|
[51b7345] | 439 | |
---|
[6c4ff37] | 440 | void CodeGenerator::visit( CommaExpr *commaExpr ) { |
---|
| 441 | output << "("; |
---|
[51587aa] | 442 | commaExpr->get_arg1()->accept( *this ); |
---|
[6c4ff37] | 443 | output << " , "; |
---|
[51587aa] | 444 | commaExpr->get_arg2()->accept( *this ); |
---|
[6c4ff37] | 445 | output << ")"; |
---|
[51587aa] | 446 | } |
---|
[51b7345] | 447 | |
---|
[6c4ff37] | 448 | void CodeGenerator::visit( TupleExpr *tupleExpr ) {} |
---|
[51b7345] | 449 | |
---|
[6c4ff37] | 450 | void CodeGenerator::visit( TypeExpr *typeExpr ) {} |
---|
[2b6c1e0] | 451 | |
---|
[51587aa] | 452 | //*** Statements |
---|
[6c4ff37] | 453 | void CodeGenerator::visit( CompoundStmt *compoundStmt ) { |
---|
[51587aa] | 454 | std::list<Statement*> ks = compoundStmt->get_kids(); |
---|
[2b6c1e0] | 455 | output << "{" << endl; |
---|
[51587aa] | 456 | |
---|
[2b6c1e0] | 457 | cur_indent += CodeGenerator::tabsize; |
---|
[51587aa] | 458 | |
---|
| 459 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++) { |
---|
[cda48b6] | 460 | output << indent << printLabels( (*i)->get_labels() ); |
---|
[51587aa] | 461 | (*i)->accept(*this ); |
---|
[2b6c1e0] | 462 | |
---|
[6c4ff37] | 463 | output << endl; |
---|
[2b6c1e0] | 464 | if ( wantSpacing( *i ) ) { |
---|
| 465 | output << endl; |
---|
| 466 | } |
---|
[51587aa] | 467 | } |
---|
[6c4ff37] | 468 | cur_indent -= CodeGenerator::tabsize; |
---|
[51587aa] | 469 | |
---|
[cda48b6] | 470 | output << indent << "}"; |
---|
[51587aa] | 471 | } |
---|
| 472 | |
---|
[6c4ff37] | 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 |
---|
[51587aa] | 483 | } |
---|
| 484 | |
---|
[6c4ff37] | 485 | void CodeGenerator::visit( IfStmt *ifStmt ) { |
---|
| 486 | output << "if ("; |
---|
[51587aa] | 487 | ifStmt->get_condition()->accept(*this ); |
---|
[2b6c1e0] | 488 | output << ") "; |
---|
[51587aa] | 489 | |
---|
| 490 | ifStmt->get_thenPart()->accept(*this ); |
---|
| 491 | |
---|
| 492 | if ( ifStmt->get_elsePart() != 0) { |
---|
[2b6c1e0] | 493 | output << " else "; |
---|
[51587aa] | 494 | ifStmt->get_elsePart()->accept(*this ); |
---|
| 495 | } // if |
---|
| 496 | } |
---|
| 497 | |
---|
[6c4ff37] | 498 | void CodeGenerator::visit( SwitchStmt *switchStmt ) { |
---|
| 499 | output << "switch (" ; |
---|
[51587aa] | 500 | switchStmt->get_condition()->accept(*this ); |
---|
[2b6c1e0] | 501 | output << ") "; |
---|
[6c4ff37] | 502 | |
---|
[2b6c1e0] | 503 | output << "{" << std::endl; |
---|
[6c4ff37] | 504 | cur_indent += CodeGenerator::tabsize; |
---|
[51587aa] | 505 | |
---|
[6c4ff37] | 506 | acceptAll( switchStmt->get_branches(), *this ); |
---|
[51587aa] | 507 | |
---|
[6c4ff37] | 508 | cur_indent -= CodeGenerator::tabsize; |
---|
[51587aa] | 509 | |
---|
[cda48b6] | 510 | output << indent << "}"; |
---|
[51587aa] | 511 | } |
---|
| 512 | |
---|
[6c4ff37] | 513 | void CodeGenerator::visit( CaseStmt *caseStmt ) { |
---|
[cda48b6] | 514 | output << indent; |
---|
[eb3261f] | 515 | if ( caseStmt->isDefault()) { |
---|
[2b6c1e0] | 516 | output << "default"; |
---|
[eb3261f] | 517 | } else { |
---|
[2b6c1e0] | 518 | output << "case "; |
---|
[51587aa] | 519 | caseStmt->get_condition()->accept(*this ); |
---|
[17cd4eb] | 520 | } // if |
---|
[6c4ff37] | 521 | output << ":\n"; |
---|
| 522 | |
---|
[51587aa] | 523 | std::list<Statement *> sts = caseStmt->get_statements(); |
---|
| 524 | |
---|
[6c4ff37] | 525 | cur_indent += CodeGenerator::tabsize; |
---|
[51587aa] | 526 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { |
---|
[cda48b6] | 527 | output << indent << printLabels( (*i)->get_labels() ) ; |
---|
[51587aa] | 528 | (*i)->accept(*this ); |
---|
[6c4ff37] | 529 | output << endl; |
---|
[51587aa] | 530 | } |
---|
[6c4ff37] | 531 | cur_indent -= CodeGenerator::tabsize; |
---|
[51587aa] | 532 | } |
---|
| 533 | |
---|
[6c4ff37] | 534 | void CodeGenerator::visit( BranchStmt *branchStmt ) { |
---|
[51587aa] | 535 | switch ( branchStmt->get_type()) { |
---|
| 536 | case BranchStmt::Goto: |
---|
| 537 | if ( ! branchStmt->get_target().empty() ) |
---|
[6c4ff37] | 538 | output << "goto " << branchStmt->get_target(); |
---|
[51587aa] | 539 | else { |
---|
| 540 | if ( branchStmt->get_computedTarget() != 0 ) { |
---|
[6c4ff37] | 541 | output << "goto *"; |
---|
[51587aa] | 542 | branchStmt->get_computedTarget()->accept( *this ); |
---|
| 543 | } // if |
---|
| 544 | } // if |
---|
| 545 | break; |
---|
| 546 | case BranchStmt::Break: |
---|
[6c4ff37] | 547 | output << "break"; |
---|
[51587aa] | 548 | break; |
---|
| 549 | case BranchStmt::Continue: |
---|
[6c4ff37] | 550 | output << "continue"; |
---|
[51587aa] | 551 | break; |
---|
| 552 | } |
---|
[2b6c1e0] | 553 | output << ";"; |
---|
[51587aa] | 554 | } |
---|
| 555 | |
---|
| 556 | |
---|
[6c4ff37] | 557 | void CodeGenerator::visit( ReturnStmt *returnStmt ) { |
---|
| 558 | output << "return "; |
---|
[51587aa] | 559 | |
---|
| 560 | // xxx -- check for null expression; |
---|
| 561 | if ( returnStmt->get_expr() ) { |
---|
| 562 | returnStmt->get_expr()->accept( *this ); |
---|
| 563 | } // if |
---|
[6c4ff37] | 564 | output << ";"; |
---|
[51587aa] | 565 | } |
---|
| 566 | |
---|
[6c4ff37] | 567 | void CodeGenerator::visit( WhileStmt *whileStmt ) { |
---|
[51587aa] | 568 | if ( whileStmt->get_isDoWhile() ) |
---|
[6c4ff37] | 569 | output << "do" ; |
---|
[51587aa] | 570 | else { |
---|
[6c4ff37] | 571 | output << "while (" ; |
---|
[51587aa] | 572 | whileStmt->get_condition()->accept(*this ); |
---|
[6c4ff37] | 573 | output << ")"; |
---|
[51587aa] | 574 | } // if |
---|
[2b6c1e0] | 575 | output << " "; |
---|
[51587aa] | 576 | |
---|
[2b6c1e0] | 577 | output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() ); |
---|
[51587aa] | 578 | whileStmt->get_body()->accept( *this ); |
---|
| 579 | |
---|
[cda48b6] | 580 | output << indent; |
---|
[51587aa] | 581 | |
---|
| 582 | if ( whileStmt->get_isDoWhile() ) { |
---|
[6c4ff37] | 583 | output << " while (" ; |
---|
[51587aa] | 584 | whileStmt->get_condition()->accept(*this ); |
---|
[6c4ff37] | 585 | output << ");"; |
---|
[51587aa] | 586 | } // if |
---|
| 587 | } |
---|
| 588 | |
---|
[6c4ff37] | 589 | void CodeGenerator::visit( ForStmt *forStmt ) { |
---|
[145f1fc] | 590 | // initialization is always hoisted, so don't |
---|
| 591 | // bother doing anything with that |
---|
| 592 | output << "for (;"; |
---|
[51587aa] | 593 | |
---|
| 594 | if ( forStmt->get_condition() != 0 ) |
---|
| 595 | forStmt->get_condition()->accept( *this ); |
---|
[6c4ff37] | 596 | output << ";"; |
---|
[51587aa] | 597 | |
---|
| 598 | if ( forStmt->get_increment() != 0 ) |
---|
| 599 | forStmt->get_increment()->accept( *this ); |
---|
[2b6c1e0] | 600 | output << ") "; |
---|
[51587aa] | 601 | |
---|
| 602 | if ( forStmt->get_body() != 0 ) { |
---|
[2b6c1e0] | 603 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() ); |
---|
[51587aa] | 604 | forStmt->get_body()->accept( *this ); |
---|
| 605 | } // if |
---|
| 606 | } |
---|
| 607 | |
---|
[6c4ff37] | 608 | void CodeGenerator::visit( NullStmt *nullStmt ) { |
---|
[cda48b6] | 609 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); |
---|
[6c4ff37] | 610 | output << "/* null statement */ ;"; |
---|
[51587aa] | 611 | } |
---|
| 612 | |
---|
[6c4ff37] | 613 | void CodeGenerator::visit( DeclStmt *declStmt ) { |
---|
[51587aa] | 614 | declStmt->get_decl()->accept( *this ); |
---|
| 615 | |
---|
| 616 | if ( doSemicolon( declStmt->get_decl() ) ) { |
---|
[6c4ff37] | 617 | output << ";"; |
---|
[51587aa] | 618 | } // if |
---|
| 619 | } |
---|
| 620 | |
---|
[6c4ff37] | 621 | std::string CodeGenerator::printLabels( std::list< Label > &l ) { |
---|
[51587aa] | 622 | std::string str( "" ); |
---|
[6c4ff37] | 623 | l.unique(); // assumes a sorted list. Why not use set? |
---|
[51587aa] | 624 | |
---|
| 625 | for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ ) |
---|
| 626 | str += *i + ": "; |
---|
| 627 | |
---|
| 628 | return str; |
---|
| 629 | } |
---|
| 630 | |
---|
[6c4ff37] | 631 | void CodeGenerator::handleStorageClass( Declaration *decl ) { |
---|
[51587aa] | 632 | switch ( decl->get_storageClass() ) { |
---|
[68cd1ce] | 633 | case DeclarationNode::Extern: |
---|
[6c4ff37] | 634 | output << "extern "; |
---|
[51587aa] | 635 | break; |
---|
[68cd1ce] | 636 | case DeclarationNode::Static: |
---|
[6c4ff37] | 637 | output << "static "; |
---|
[51587aa] | 638 | break; |
---|
[68cd1ce] | 639 | case DeclarationNode::Auto: |
---|
[51587aa] | 640 | // silently drop storage class |
---|
| 641 | break; |
---|
[68cd1ce] | 642 | case DeclarationNode::Register: |
---|
[6c4ff37] | 643 | output << "register "; |
---|
[51587aa] | 644 | break; |
---|
[68cd1ce] | 645 | case DeclarationNode::Inline: |
---|
[de62360d] | 646 | output << "inline "; |
---|
[f38c8d9] | 647 | break; |
---|
[68cd1ce] | 648 | case DeclarationNode::Fortran: |
---|
| 649 | output << "fortran "; |
---|
| 650 | break; |
---|
| 651 | case DeclarationNode::Noreturn: |
---|
| 652 | output << "_Noreturn "; |
---|
| 653 | break; |
---|
| 654 | case DeclarationNode::Threadlocal: |
---|
| 655 | output << "_Thread_local "; |
---|
| 656 | break; |
---|
| 657 | case DeclarationNode::NoStorageClass: |
---|
[f38c8d9] | 658 | break; |
---|
[843054c2] | 659 | } // switch |
---|
[51587aa] | 660 | } |
---|
[51b7345] | 661 | } // namespace CodeGen |
---|
[51587aa] | 662 | |
---|
| 663 | // Local Variables: // |
---|
| 664 | // tab-width: 4 // |
---|
| 665 | // mode: c++ // |
---|
| 666 | // compile-command: "make install" // |
---|
| 667 | // End: // |
---|