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