[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 | // |
---|
[71f4e4f] | 7 | // CodeGenerator.cc -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[8688ce1] | 11 | // Last Modified By : Peter A. Buhr |
---|
[fbcde64] | 12 | // Last Modified On : Thu Mar 30 16:38:01 2017 |
---|
| 13 | // Update Count : 482 |
---|
[51587aa] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include <algorithm> |
---|
| 17 | #include <iostream> |
---|
| 18 | #include <cassert> |
---|
| 19 | #include <list> |
---|
| 20 | |
---|
[68cd1ce] | 21 | #include "Parser/ParseNode.h" |
---|
| 22 | |
---|
[e8032b0] | 23 | #include "SynTree/Declaration.h" |
---|
[51b7345] | 24 | #include "SynTree/Expression.h" |
---|
| 25 | #include "SynTree/Initializer.h" |
---|
[68cd1ce] | 26 | #include "SynTree/Statement.h" |
---|
[e8032b0] | 27 | #include "SynTree/Type.h" |
---|
[7baed7d] | 28 | #include "SynTree/Attribute.h" |
---|
[51b7345] | 29 | |
---|
[d3b7937] | 30 | #include "Common/utility.h" |
---|
| 31 | #include "Common/UnimplementedError.h" |
---|
[51b7345] | 32 | |
---|
[a61fea9a] | 33 | #include "CodeGenerator.h" |
---|
[51b7345] | 34 | #include "OperatorTable.h" |
---|
| 35 | #include "GenType.h" |
---|
| 36 | |
---|
[10a7775] | 37 | #include "InitTweak/InitTweak.h" |
---|
| 38 | |
---|
[51b7345] | 39 | using namespace std; |
---|
| 40 | |
---|
| 41 | namespace CodeGen { |
---|
[6c4ff37] | 42 | int CodeGenerator::tabsize = 4; |
---|
[51587aa] | 43 | |
---|
[145f1fc] | 44 | // the kinds of statements that would ideally be followed by whitespace |
---|
[2b6c1e0] | 45 | bool wantSpacing( Statement * stmt) { |
---|
| 46 | return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) || |
---|
[08061589] | 47 | dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt ); |
---|
[2b6c1e0] | 48 | } |
---|
| 49 | |
---|
[8688ce1] | 50 | void CodeGenerator::extension( Expression * expr ) { |
---|
[8e9cbb2] | 51 | if ( expr->get_extension() ) { |
---|
| 52 | output << "__extension__ "; |
---|
| 53 | } // if |
---|
| 54 | } // extension |
---|
| 55 | |
---|
[8688ce1] | 56 | void CodeGenerator::extension( Declaration * decl ) { |
---|
[8e9cbb2] | 57 | if ( decl->get_extension() ) { |
---|
| 58 | output << "__extension__ "; |
---|
| 59 | } // if |
---|
| 60 | } // extension |
---|
| 61 | |
---|
[58dd019] | 62 | void CodeGenerator::asmName( DeclarationWithType * decl ) { |
---|
| 63 | if ( ConstantExpr * asmName = decl->get_asmName() ) { |
---|
| 64 | output << " asm ( " << asmName->get_constant()->get_value() << " )"; |
---|
| 65 | } // if |
---|
| 66 | } // extension |
---|
| 67 | |
---|
[888cbe4] | 68 | ostream & CodeGenerator::Indenter::operator()( ostream & output ) const { |
---|
[cda48b6] | 69 | return output << string( cg.cur_indent, ' ' ); |
---|
| 70 | } |
---|
| 71 | |
---|
[888cbe4] | 72 | ostream & operator<<( ostream & output, const CodeGenerator::Indenter &indent ) { |
---|
[cda48b6] | 73 | return indent( output ); |
---|
| 74 | } |
---|
[51587aa] | 75 | |
---|
[888cbe4] | 76 | CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) { |
---|
| 77 | labels = &l; |
---|
| 78 | return *this; |
---|
| 79 | } |
---|
| 80 | |
---|
[8688ce1] | 81 | ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) { |
---|
[888cbe4] | 82 | std::list< Label > & labs = *printLabels.labels; |
---|
| 83 | // l.unique(); // assumes a sorted list. Why not use set? Does order matter? |
---|
| 84 | for ( Label & l : labs ) { |
---|
| 85 | output << l.get_name() + ": "; |
---|
| 86 | printLabels.cg.genAttributes( l.get_attributes() ); |
---|
[8688ce1] | 87 | } // for |
---|
[888cbe4] | 88 | return output; |
---|
| 89 | } |
---|
| 90 | |
---|
[35b1bf4] | 91 | CodeGenerator::CodeGenerator( std::ostream & os, bool pretty ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ) {} |
---|
[cda48b6] | 92 | |
---|
[8688ce1] | 93 | CodeGenerator::CodeGenerator( std::ostream & os, std::string init, int indentation, bool infunp ) |
---|
[888cbe4] | 94 | : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) { |
---|
[6c4ff37] | 95 | //output << std::string( init ); |
---|
[51587aa] | 96 | } |
---|
| 97 | |
---|
[8688ce1] | 98 | CodeGenerator::CodeGenerator( std::ostream & os, char * init, int indentation, bool infunp ) |
---|
[888cbe4] | 99 | : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) { |
---|
[6c4ff37] | 100 | //output << std::string( init ); |
---|
[51587aa] | 101 | } |
---|
| 102 | |
---|
[486341f] | 103 | string CodeGenerator::mangleName( DeclarationWithType * decl ) { |
---|
[35b1bf4] | 104 | if ( pretty ) return decl->get_name(); |
---|
[51587aa] | 105 | if ( decl->get_mangleName() != "" ) { |
---|
[f326f99] | 106 | // need to incorporate scope level in order to differentiate names for destructors |
---|
| 107 | return decl->get_scopedMangleName(); |
---|
[51587aa] | 108 | } else { |
---|
| 109 | return decl->get_name(); |
---|
| 110 | } // if |
---|
| 111 | } |
---|
[94b4364] | 112 | |
---|
[44a81853] | 113 | void CodeGenerator::genAttributes( list< Attribute * > & attributes ) { |
---|
| 114 | if ( attributes.empty() ) return; |
---|
| 115 | output << "__attribute__ (("; |
---|
| 116 | for ( list< Attribute * >::iterator attr( attributes.begin() );; ) { |
---|
| 117 | output << (*attr)->get_name(); |
---|
| 118 | if ( ! (*attr)->get_parameters().empty() ) { |
---|
| 119 | output << "("; |
---|
| 120 | genCommaList( (*attr)->get_parameters().begin(), (*attr)->get_parameters().end() ); |
---|
| 121 | output << ")"; |
---|
| 122 | } // if |
---|
| 123 | if ( ++attr == attributes.end() ) break; |
---|
| 124 | output << ","; // separator |
---|
| 125 | } // for |
---|
| 126 | output << ")) "; |
---|
| 127 | } // CodeGenerator::genAttributes |
---|
[7baed7d] | 128 | |
---|
| 129 | |
---|
| 130 | //*** Declarations |
---|
[8688ce1] | 131 | void CodeGenerator::visit( FunctionDecl * functionDecl ) { |
---|
[8e9cbb2] | 132 | extension( functionDecl ); |
---|
[7baed7d] | 133 | genAttributes( functionDecl->get_attributes() ); |
---|
| 134 | |
---|
[51587aa] | 135 | handleStorageClass( functionDecl ); |
---|
[6e8bd43] | 136 | functionDecl->get_funcSpec().print( output ); |
---|
[dd020c0] | 137 | |
---|
[35b1bf4] | 138 | output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), pretty ); |
---|
[51587aa] | 139 | |
---|
[58dd019] | 140 | asmName( functionDecl ); |
---|
| 141 | |
---|
[51587aa] | 142 | // acceptAll( functionDecl->get_oldDecls(), *this ); |
---|
| 143 | if ( functionDecl->get_statements() ) { |
---|
[7f5566b] | 144 | functionDecl->get_statements()->accept( *this ); |
---|
[51587aa] | 145 | } // if |
---|
| 146 | } |
---|
| 147 | |
---|
[8688ce1] | 148 | void CodeGenerator::visit( ObjectDecl * objectDecl ) { |
---|
[d9c8a59] | 149 | if (objectDecl->get_name().empty()) { |
---|
| 150 | static UniqueName name = { "__anonymous_object" }; |
---|
| 151 | objectDecl->set_name( name.newName() ); |
---|
| 152 | } |
---|
| 153 | |
---|
[8e9cbb2] | 154 | extension( objectDecl ); |
---|
[f9cebb5] | 155 | genAttributes( objectDecl->get_attributes() ); |
---|
| 156 | |
---|
[51587aa] | 157 | handleStorageClass( objectDecl ); |
---|
[35b1bf4] | 158 | output << genType( objectDecl->get_type(), mangleName( objectDecl ), pretty ); |
---|
[71f4e4f] | 159 | |
---|
[58dd019] | 160 | asmName( objectDecl ); |
---|
| 161 | |
---|
[51587aa] | 162 | if ( objectDecl->get_init() ) { |
---|
[6c4ff37] | 163 | output << " = "; |
---|
[51587aa] | 164 | objectDecl->get_init()->accept( *this ); |
---|
| 165 | } // if |
---|
[3778cb2] | 166 | |
---|
[51587aa] | 167 | if ( objectDecl->get_bitfieldWidth() ) { |
---|
[6c4ff37] | 168 | output << ":"; |
---|
[51587aa] | 169 | objectDecl->get_bitfieldWidth()->accept( *this ); |
---|
| 170 | } // if |
---|
| 171 | } |
---|
| 172 | |
---|
[8688ce1] | 173 | void CodeGenerator::handleAggregate( AggregateDecl * aggDecl ) { |
---|
[c0aa336] | 174 | genAttributes( aggDecl->get_attributes() ); |
---|
[35b1bf4] | 175 | |
---|
[51587aa] | 176 | if ( aggDecl->get_name() != "" ) |
---|
[6c4ff37] | 177 | output << aggDecl->get_name(); |
---|
[71f4e4f] | 178 | |
---|
[2c57025] | 179 | // std::list< Declaration * > & memb = aggDecl->get_members(); |
---|
| 180 | // if ( ! memb.empty() ) { |
---|
| 181 | if ( aggDecl->has_body() ) { |
---|
| 182 | std::list< Declaration * > & memb = aggDecl->get_members(); |
---|
[94b4364] | 183 | output << " {" << endl; |
---|
[51587aa] | 184 | |
---|
[71f4e4f] | 185 | cur_indent += CodeGenerator::tabsize; |
---|
[3778cb2] | 186 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { |
---|
[71f4e4f] | 187 | output << indent; |
---|
[7f5566b] | 188 | (*i)->accept( *this ); |
---|
[6c4ff37] | 189 | output << ";" << endl; |
---|
[3778cb2] | 190 | } // for |
---|
[51587aa] | 191 | |
---|
[71f4e4f] | 192 | cur_indent -= CodeGenerator::tabsize; |
---|
[51587aa] | 193 | |
---|
[cda48b6] | 194 | output << indent << "}"; |
---|
[17cd4eb] | 195 | } // if |
---|
[51587aa] | 196 | } |
---|
[17cd4eb] | 197 | |
---|
[8688ce1] | 198 | void CodeGenerator::visit( StructDecl * structDecl ) { |
---|
[8e9cbb2] | 199 | extension( structDecl ); |
---|
[6c4ff37] | 200 | output << "struct "; |
---|
[51587aa] | 201 | handleAggregate( structDecl ); |
---|
| 202 | } |
---|
[17cd4eb] | 203 | |
---|
[8688ce1] | 204 | void CodeGenerator::visit( UnionDecl * unionDecl ) { |
---|
[8e9cbb2] | 205 | extension( unionDecl ); |
---|
[6c4ff37] | 206 | output << "union "; |
---|
[8e9cbb2] | 207 | handleAggregate( unionDecl ); |
---|
[51587aa] | 208 | } |
---|
[71f4e4f] | 209 | |
---|
[8688ce1] | 210 | void CodeGenerator::visit( EnumDecl * enumDecl ) { |
---|
[8e9cbb2] | 211 | extension( enumDecl ); |
---|
[6c4ff37] | 212 | output << "enum "; |
---|
[c0aa336] | 213 | genAttributes( enumDecl->get_attributes() ); |
---|
[51587aa] | 214 | |
---|
[8e9cbb2] | 215 | if ( enumDecl->get_name() != "" ) |
---|
| 216 | output << enumDecl->get_name(); |
---|
[71f4e4f] | 217 | |
---|
[8e9cbb2] | 218 | std::list< Declaration* > &memb = enumDecl->get_members(); |
---|
[51587aa] | 219 | |
---|
| 220 | if ( ! memb.empty() ) { |
---|
[cda48b6] | 221 | output << " {" << endl; |
---|
[51587aa] | 222 | |
---|
[71f4e4f] | 223 | cur_indent += CodeGenerator::tabsize; |
---|
[51587aa] | 224 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { |
---|
[8688ce1] | 225 | ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); |
---|
[51587aa] | 226 | assert( obj ); |
---|
[71f4e4f] | 227 | output << indent << mangleName( obj ); |
---|
[51587aa] | 228 | if ( obj->get_init() ) { |
---|
[6c4ff37] | 229 | output << " = "; |
---|
[7f5566b] | 230 | obj->get_init()->accept( *this ); |
---|
[51587aa] | 231 | } // if |
---|
[6c4ff37] | 232 | output << "," << endl; |
---|
[51587aa] | 233 | } // for |
---|
| 234 | |
---|
[71f4e4f] | 235 | cur_indent -= CodeGenerator::tabsize; |
---|
[51587aa] | 236 | |
---|
[cda48b6] | 237 | output << indent << "}"; |
---|
[51587aa] | 238 | } // if |
---|
| 239 | } |
---|
[71f4e4f] | 240 | |
---|
[8688ce1] | 241 | void CodeGenerator::visit( TraitDecl * traitDecl ) {} |
---|
[71f4e4f] | 242 | |
---|
[8688ce1] | 243 | void CodeGenerator::visit( TypedefDecl * typeDecl ) { |
---|
[8e9cbb2] | 244 | assert( false && "Typedefs are removed and substituted in earlier passes." ); |
---|
| 245 | //output << "typedef "; |
---|
[35b1bf4] | 246 | //output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty ); |
---|
[51587aa] | 247 | } |
---|
[71f4e4f] | 248 | |
---|
[8688ce1] | 249 | void CodeGenerator::visit( TypeDecl * typeDecl ) { |
---|
[51587aa] | 250 | // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, |
---|
| 251 | // still to be done |
---|
[8e9cbb2] | 252 | extension( typeDecl ); |
---|
[6c4ff37] | 253 | output << "extern unsigned long " << typeDecl->get_name(); |
---|
[51587aa] | 254 | if ( typeDecl->get_base() ) { |
---|
[35b1bf4] | 255 | output << " = sizeof( " << genType( typeDecl->get_base(), "", pretty ) << " )"; |
---|
[51587aa] | 256 | } // if |
---|
| 257 | } |
---|
| 258 | |
---|
[e45215c] | 259 | void CodeGenerator::printDesignators( std::list< Expression * > & designators ) { |
---|
| 260 | typedef std::list< Expression * > DesignatorList; |
---|
| 261 | if ( designators.size() == 0 ) return; |
---|
| 262 | for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) { |
---|
[8a4da06] | 263 | if ( dynamic_cast< NameExpr * >( *iter ) ) { |
---|
[f32c7f4] | 264 | // if expression is a name, then initializing aggregate member |
---|
| 265 | output << "."; |
---|
| 266 | (*iter)->accept( *this ); |
---|
| 267 | } else { |
---|
| 268 | // if not a simple name, it has to be a constant expression, i.e. an array designator |
---|
[e45215c] | 269 | output << "["; |
---|
| 270 | (*iter)->accept( *this ); |
---|
| 271 | output << "]"; |
---|
[3778cb2] | 272 | } // if |
---|
| 273 | } // for |
---|
[e45215c] | 274 | output << " = "; |
---|
| 275 | } |
---|
| 276 | |
---|
[8688ce1] | 277 | void CodeGenerator::visit( SingleInit * init ) { |
---|
[e45215c] | 278 | printDesignators( init->get_designators() ); |
---|
[51587aa] | 279 | init->get_value()->accept( *this ); |
---|
| 280 | } |
---|
| 281 | |
---|
[8688ce1] | 282 | void CodeGenerator::visit( ListInit * init ) { |
---|
[e45215c] | 283 | printDesignators( init->get_designators() ); |
---|
[6c4ff37] | 284 | output << "{ "; |
---|
[4d2434a] | 285 | if ( init->begin() == init->end() ) { |
---|
[8e9cbb2] | 286 | // illegal to leave initializer list empty for scalar initializers, but always legal to have 0 |
---|
[5b40f30] | 287 | output << "0"; |
---|
| 288 | } else { |
---|
[4d2434a] | 289 | genCommaList( init->begin(), init->end() ); |
---|
[8688ce1] | 290 | } // if |
---|
[6c4ff37] | 291 | output << " }"; |
---|
[51587aa] | 292 | } |
---|
| 293 | |
---|
[fc638d2] | 294 | void CodeGenerator::visit( ConstructorInit * init ){ |
---|
| 295 | assertf( false, "ConstructorInit nodes should not make it to CodeGen." ); |
---|
| 296 | } |
---|
| 297 | |
---|
[8688ce1] | 298 | void CodeGenerator::visit( Constant * constant ) { |
---|
[6c4ff37] | 299 | output << constant->get_value() ; |
---|
[51587aa] | 300 | } |
---|
| 301 | |
---|
| 302 | //*** Expressions |
---|
[8688ce1] | 303 | void CodeGenerator::visit( ApplicationExpr * applicationExpr ) { |
---|
[e04ef3a] | 304 | extension( applicationExpr ); |
---|
[8688ce1] | 305 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { |
---|
[51587aa] | 306 | OperatorInfo opInfo; |
---|
| 307 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { |
---|
| 308 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin(); |
---|
| 309 | switch ( opInfo.type ) { |
---|
| 310 | case OT_PREFIXASSIGN: |
---|
| 311 | case OT_POSTFIXASSIGN: |
---|
| 312 | case OT_INFIXASSIGN: |
---|
[356189a] | 313 | case OT_CTOR: |
---|
[c2ce2350] | 314 | case OT_DTOR: |
---|
[51587aa] | 315 | { |
---|
| 316 | assert( arg != applicationExpr->get_args().end() ); |
---|
[8688ce1] | 317 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { |
---|
[356189a] | 318 | // remove & from first assignment/ctor argument |
---|
[51587aa] | 319 | *arg = addrExpr->get_arg(); |
---|
| 320 | } else { |
---|
[356189a] | 321 | // no address-of operator, so must be a pointer - add dereference |
---|
[066d77a] | 322 | // NOTE: if the assertion starts to trigger, check that the application expr isn't being shared. |
---|
| 323 | // Since its arguments are modified here, this assertion most commonly triggers when the application |
---|
| 324 | // is visited multiple times. |
---|
[8688ce1] | 325 | UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) ); |
---|
[51587aa] | 326 | newExpr->get_args().push_back( *arg ); |
---|
[906e24d] | 327 | Type * type = InitTweak::getPointerBase( (*arg)->get_result() ); |
---|
[066d77a] | 328 | assertf( type, "First argument to a derefence must be a pointer. Ensure that expressions are not being shared." ); |
---|
[906e24d] | 329 | newExpr->set_result( type->clone() ); |
---|
[51587aa] | 330 | *arg = newExpr; |
---|
| 331 | } // if |
---|
| 332 | break; |
---|
| 333 | } |
---|
[71f4e4f] | 334 | |
---|
[51587aa] | 335 | default: |
---|
| 336 | // do nothing |
---|
| 337 | ; |
---|
[3778cb2] | 338 | } // switch |
---|
[71f4e4f] | 339 | |
---|
[51587aa] | 340 | switch ( opInfo.type ) { |
---|
| 341 | case OT_INDEX: |
---|
| 342 | assert( applicationExpr->get_args().size() == 2 ); |
---|
| 343 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 344 | output << "["; |
---|
[51587aa] | 345 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 346 | output << "]"; |
---|
[51587aa] | 347 | break; |
---|
[71f4e4f] | 348 | |
---|
[51587aa] | 349 | case OT_CALL: |
---|
[356189a] | 350 | // there are no intrinsic definitions of the function call operator |
---|
[51587aa] | 351 | assert( false ); |
---|
| 352 | break; |
---|
[71f4e4f] | 353 | |
---|
[f1e012b] | 354 | case OT_CTOR: |
---|
[c2ce2350] | 355 | case OT_DTOR: |
---|
[356189a] | 356 | if ( applicationExpr->get_args().size() == 1 ) { |
---|
[8e9cbb2] | 357 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
| 358 | // effects, so must still output this expression |
---|
[64071c2] | 359 | output << "("; |
---|
[356189a] | 360 | (*arg++)->accept( *this ); |
---|
[64071c2] | 361 | output << ") /* " << opInfo.inputName << " */"; |
---|
[356189a] | 362 | } else if ( applicationExpr->get_args().size() == 2 ) { |
---|
[c2ce2350] | 363 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
[356189a] | 364 | output << "("; |
---|
| 365 | (*arg++)->accept( *this ); |
---|
| 366 | output << opInfo.symbol; |
---|
| 367 | (*arg)->accept( *this ); |
---|
[c2ce2350] | 368 | output << ") /* " << opInfo.inputName << " */"; |
---|
[356189a] | 369 | } else { |
---|
[c2ce2350] | 370 | // no constructors with 0 or more than 2 parameters |
---|
[356189a] | 371 | assert( false ); |
---|
[8688ce1] | 372 | } // if |
---|
[356189a] | 373 | break; |
---|
[f1e012b] | 374 | |
---|
[51587aa] | 375 | case OT_PREFIX: |
---|
| 376 | case OT_PREFIXASSIGN: |
---|
| 377 | assert( applicationExpr->get_args().size() == 1 ); |
---|
[6c4ff37] | 378 | output << "("; |
---|
| 379 | output << opInfo.symbol; |
---|
[51587aa] | 380 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 381 | output << ")"; |
---|
[51587aa] | 382 | break; |
---|
[71f4e4f] | 383 | |
---|
[51587aa] | 384 | case OT_POSTFIX: |
---|
| 385 | case OT_POSTFIXASSIGN: |
---|
| 386 | assert( applicationExpr->get_args().size() == 1 ); |
---|
| 387 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 388 | output << opInfo.symbol; |
---|
[51587aa] | 389 | break; |
---|
| 390 | |
---|
[f1e012b] | 391 | |
---|
[51587aa] | 392 | case OT_INFIX: |
---|
| 393 | case OT_INFIXASSIGN: |
---|
| 394 | assert( applicationExpr->get_args().size() == 2 ); |
---|
[6c4ff37] | 395 | output << "("; |
---|
[51587aa] | 396 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 397 | output << opInfo.symbol; |
---|
[51587aa] | 398 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 399 | output << ")"; |
---|
[51587aa] | 400 | break; |
---|
[71f4e4f] | 401 | |
---|
[51587aa] | 402 | case OT_CONSTANT: |
---|
[721f17a] | 403 | case OT_LABELADDRESS: |
---|
| 404 | // there are no intrinsic definitions of 0/1 or label addresses as functions |
---|
[51587aa] | 405 | assert( false ); |
---|
[3778cb2] | 406 | } // switch |
---|
[17cd4eb] | 407 | } else { |
---|
[51587aa] | 408 | varExpr->accept( *this ); |
---|
[6c4ff37] | 409 | output << "("; |
---|
[51587aa] | 410 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 411 | output << ")"; |
---|
[17cd4eb] | 412 | } // if |
---|
[51587aa] | 413 | } else { |
---|
| 414 | applicationExpr->get_function()->accept( *this ); |
---|
[6c4ff37] | 415 | output << "("; |
---|
[51587aa] | 416 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 417 | output << ")"; |
---|
[51587aa] | 418 | } // if |
---|
| 419 | } |
---|
[71f4e4f] | 420 | |
---|
[8688ce1] | 421 | void CodeGenerator::visit( UntypedExpr * untypedExpr ) { |
---|
[e04ef3a] | 422 | extension( untypedExpr ); |
---|
[8688ce1] | 423 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { |
---|
[51587aa] | 424 | OperatorInfo opInfo; |
---|
| 425 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { |
---|
| 426 | std::list< Expression* >::iterator arg = untypedExpr->get_args().begin(); |
---|
| 427 | switch ( opInfo.type ) { |
---|
| 428 | case OT_INDEX: |
---|
| 429 | assert( untypedExpr->get_args().size() == 2 ); |
---|
| 430 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 431 | output << "["; |
---|
[51587aa] | 432 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 433 | output << "]"; |
---|
[51587aa] | 434 | break; |
---|
[71f4e4f] | 435 | |
---|
[51587aa] | 436 | case OT_CALL: |
---|
[f1e012b] | 437 | assert( false ); |
---|
| 438 | |
---|
[c2ce2350] | 439 | case OT_CTOR: |
---|
| 440 | case OT_DTOR: |
---|
| 441 | if ( untypedExpr->get_args().size() == 1 ) { |
---|
[8e9cbb2] | 442 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
| 443 | // effects, so must still output this expression |
---|
[64071c2] | 444 | output << "("; |
---|
[c2ce2350] | 445 | (*arg++)->accept( *this ); |
---|
[64071c2] | 446 | output << ") /* " << opInfo.inputName << " */"; |
---|
[c2ce2350] | 447 | } else if ( untypedExpr->get_args().size() == 2 ) { |
---|
| 448 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
| 449 | output << "("; |
---|
| 450 | (*arg++)->accept( *this ); |
---|
| 451 | output << opInfo.symbol; |
---|
| 452 | (*arg)->accept( *this ); |
---|
| 453 | output << ") /* " << opInfo.inputName << " */"; |
---|
| 454 | } else { |
---|
| 455 | // no constructors with 0 or more than 2 parameters |
---|
| 456 | assert( false ); |
---|
[3778cb2] | 457 | } // if |
---|
[51587aa] | 458 | break; |
---|
[71f4e4f] | 459 | |
---|
[51587aa] | 460 | case OT_PREFIX: |
---|
| 461 | case OT_PREFIXASSIGN: |
---|
[de62360d] | 462 | case OT_LABELADDRESS: |
---|
[51587aa] | 463 | assert( untypedExpr->get_args().size() == 1 ); |
---|
[6c4ff37] | 464 | output << "("; |
---|
| 465 | output << opInfo.symbol; |
---|
[51587aa] | 466 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 467 | output << ")"; |
---|
[51587aa] | 468 | break; |
---|
[71f4e4f] | 469 | |
---|
[51587aa] | 470 | case OT_POSTFIX: |
---|
| 471 | case OT_POSTFIXASSIGN: |
---|
| 472 | assert( untypedExpr->get_args().size() == 1 ); |
---|
| 473 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 474 | output << opInfo.symbol; |
---|
[51587aa] | 475 | break; |
---|
[71f4e4f] | 476 | |
---|
[51587aa] | 477 | case OT_INFIX: |
---|
| 478 | case OT_INFIXASSIGN: |
---|
| 479 | assert( untypedExpr->get_args().size() == 2 ); |
---|
[6c4ff37] | 480 | output << "("; |
---|
[51587aa] | 481 | (*arg++)->accept( *this ); |
---|
[6c4ff37] | 482 | output << opInfo.symbol; |
---|
[51587aa] | 483 | (*arg)->accept( *this ); |
---|
[6c4ff37] | 484 | output << ")"; |
---|
[51587aa] | 485 | break; |
---|
[71f4e4f] | 486 | |
---|
[51587aa] | 487 | case OT_CONSTANT: |
---|
| 488 | // there are no intrinsic definitions of 0 or 1 as functions |
---|
| 489 | assert( false ); |
---|
[3778cb2] | 490 | } // switch |
---|
[51587aa] | 491 | } else { |
---|
[8688ce1] | 492 | if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2 |
---|
[ac911f4] | 493 | assert( untypedExpr->get_args().size() == 2 ); |
---|
| 494 | (*untypedExpr->get_args().begin())->accept( *this ); |
---|
| 495 | output << " ... "; |
---|
| 496 | (*--untypedExpr->get_args().end())->accept( *this ); |
---|
[057b34f] | 497 | } else { // builtin routines |
---|
| 498 | nameExpr->accept( *this ); |
---|
| 499 | output << "("; |
---|
| 500 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); |
---|
| 501 | output << ")"; |
---|
[66d12f7] | 502 | } // if |
---|
[51587aa] | 503 | } // if |
---|
| 504 | } else { |
---|
| 505 | untypedExpr->get_function()->accept( *this ); |
---|
[6c4ff37] | 506 | output << "("; |
---|
[51587aa] | 507 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); |
---|
[6c4ff37] | 508 | output << ")"; |
---|
[51587aa] | 509 | } // if |
---|
| 510 | } |
---|
[71f4e4f] | 511 | |
---|
[064e3ff] | 512 | void CodeGenerator::visit( RangeExpr * rangeExpr ) { |
---|
| 513 | rangeExpr->get_low()->accept( *this ); |
---|
| 514 | output << " ... "; |
---|
| 515 | rangeExpr->get_high()->accept( *this ); |
---|
| 516 | } |
---|
| 517 | |
---|
[8688ce1] | 518 | void CodeGenerator::visit( NameExpr * nameExpr ) { |
---|
[e04ef3a] | 519 | extension( nameExpr ); |
---|
[51587aa] | 520 | OperatorInfo opInfo; |
---|
| 521 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { |
---|
| 522 | assert( opInfo.type == OT_CONSTANT ); |
---|
[6c4ff37] | 523 | output << opInfo.symbol; |
---|
[51587aa] | 524 | } else { |
---|
[6c4ff37] | 525 | output << nameExpr->get_name(); |
---|
[51587aa] | 526 | } // if |
---|
| 527 | } |
---|
[71f4e4f] | 528 | |
---|
[8688ce1] | 529 | void CodeGenerator::visit( AddressExpr * addressExpr ) { |
---|
[e04ef3a] | 530 | extension( addressExpr ); |
---|
[6c4ff37] | 531 | output << "(&"; |
---|
[51587aa] | 532 | // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address |
---|
[8688ce1] | 533 | if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) { |
---|
[6c4ff37] | 534 | output << mangleName( variableExpr->get_var() ); |
---|
[51587aa] | 535 | } else { |
---|
| 536 | addressExpr->get_arg()->accept( *this ); |
---|
| 537 | } // if |
---|
[6c4ff37] | 538 | output << ")"; |
---|
[51587aa] | 539 | } |
---|
| 540 | |
---|
[8688ce1] | 541 | void CodeGenerator::visit( CastExpr * castExpr ) { |
---|
[e04ef3a] | 542 | extension( castExpr ); |
---|
[803deb1] | 543 | output << "("; |
---|
[906e24d] | 544 | if ( castExpr->get_result()->isVoid() ) { |
---|
[803deb1] | 545 | output << "(void)" ; |
---|
[615a096] | 546 | } else if ( ! castExpr->get_result()->get_lvalue() ) { |
---|
[803deb1] | 547 | // at least one result type of cast, but not an lvalue |
---|
| 548 | output << "("; |
---|
[35b1bf4] | 549 | output << genType( castExpr->get_result(), "", pretty ); |
---|
[71f4e4f] | 550 | output << ")"; |
---|
[803deb1] | 551 | } else { |
---|
[8e9cbb2] | 552 | // otherwise, the cast is to an lvalue type, so the cast should be dropped, since the result of a cast is |
---|
[803deb1] | 553 | // never an lvalue in C |
---|
[3778cb2] | 554 | } // if |
---|
[803deb1] | 555 | castExpr->get_arg()->accept( *this ); |
---|
| 556 | output << ")"; |
---|
[51587aa] | 557 | } |
---|
[71f4e4f] | 558 | |
---|
[8688ce1] | 559 | void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) { |
---|
[51587aa] | 560 | assert( false ); |
---|
| 561 | } |
---|
[71f4e4f] | 562 | |
---|
[8688ce1] | 563 | void CodeGenerator::visit( MemberExpr * memberExpr ) { |
---|
[e04ef3a] | 564 | extension( memberExpr ); |
---|
[51587aa] | 565 | memberExpr->get_aggregate()->accept( *this ); |
---|
[6c4ff37] | 566 | output << "." << mangleName( memberExpr->get_member() ); |
---|
[51587aa] | 567 | } |
---|
[71f4e4f] | 568 | |
---|
[8688ce1] | 569 | void CodeGenerator::visit( VariableExpr * variableExpr ) { |
---|
[e04ef3a] | 570 | extension( variableExpr ); |
---|
[51587aa] | 571 | OperatorInfo opInfo; |
---|
| 572 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { |
---|
[6c4ff37] | 573 | output << opInfo.symbol; |
---|
[51587aa] | 574 | } else { |
---|
[6c4ff37] | 575 | output << mangleName( variableExpr->get_var() ); |
---|
[51587aa] | 576 | } // if |
---|
| 577 | } |
---|
[71f4e4f] | 578 | |
---|
[8688ce1] | 579 | void CodeGenerator::visit( ConstantExpr * constantExpr ) { |
---|
[51587aa] | 580 | assert( constantExpr->get_constant() ); |
---|
[e04ef3a] | 581 | extension( constantExpr ); |
---|
[51587aa] | 582 | constantExpr->get_constant()->accept( *this ); |
---|
| 583 | } |
---|
[71f4e4f] | 584 | |
---|
[8688ce1] | 585 | void CodeGenerator::visit( SizeofExpr * sizeofExpr ) { |
---|
[e04ef3a] | 586 | extension( sizeofExpr ); |
---|
[6c4ff37] | 587 | output << "sizeof("; |
---|
[51587aa] | 588 | if ( sizeofExpr->get_isType() ) { |
---|
[35b1bf4] | 589 | output << genType( sizeofExpr->get_type(), "", pretty ); |
---|
[51587aa] | 590 | } else { |
---|
| 591 | sizeofExpr->get_expr()->accept( *this ); |
---|
| 592 | } // if |
---|
[6c4ff37] | 593 | output << ")"; |
---|
[51587aa] | 594 | } |
---|
[47534159] | 595 | |
---|
[8688ce1] | 596 | void CodeGenerator::visit( AlignofExpr * alignofExpr ) { |
---|
[47534159] | 597 | // use GCC extension to avoid bumping std to C11 |
---|
[8e9cbb2] | 598 | extension( alignofExpr ); |
---|
[47534159] | 599 | output << "__alignof__("; |
---|
[25a054f] | 600 | if ( alignofExpr->get_isType() ) { |
---|
[35b1bf4] | 601 | output << genType( alignofExpr->get_type(), "", pretty ); |
---|
[47534159] | 602 | } else { |
---|
[25a054f] | 603 | alignofExpr->get_expr()->accept( *this ); |
---|
[47534159] | 604 | } // if |
---|
| 605 | output << ")"; |
---|
| 606 | } |
---|
[71f4e4f] | 607 | |
---|
[8688ce1] | 608 | void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) { |
---|
[8e9cbb2] | 609 | assert( false && "UntypedOffsetofExpr should not reach code generation." ); |
---|
[2a4b088] | 610 | } |
---|
| 611 | |
---|
[8688ce1] | 612 | void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) { |
---|
[25a054f] | 613 | // use GCC builtin |
---|
| 614 | output << "__builtin_offsetof("; |
---|
[35b1bf4] | 615 | output << genType( offsetofExpr->get_type(), "", pretty ); |
---|
[e551c69] | 616 | output << ", " << mangleName( offsetofExpr->get_member() ); |
---|
[25a054f] | 617 | output << ")"; |
---|
| 618 | } |
---|
[d63eeb0] | 619 | |
---|
[8688ce1] | 620 | void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) { |
---|
[8e9cbb2] | 621 | assert( false && "OffsetPackExpr should not reach code generation." ); |
---|
[afc1045] | 622 | } |
---|
[70a06f6] | 623 | |
---|
[8688ce1] | 624 | void CodeGenerator::visit( LogicalExpr * logicalExpr ) { |
---|
[e04ef3a] | 625 | extension( logicalExpr ); |
---|
[6c4ff37] | 626 | output << "("; |
---|
[51587aa] | 627 | logicalExpr->get_arg1()->accept( *this ); |
---|
| 628 | if ( logicalExpr->get_isAnd() ) { |
---|
[6c4ff37] | 629 | output << " && "; |
---|
[51587aa] | 630 | } else { |
---|
[6c4ff37] | 631 | output << " || "; |
---|
[51587aa] | 632 | } // if |
---|
| 633 | logicalExpr->get_arg2()->accept( *this ); |
---|
[6c4ff37] | 634 | output << ")"; |
---|
[51587aa] | 635 | } |
---|
[71f4e4f] | 636 | |
---|
[8688ce1] | 637 | void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) { |
---|
[e04ef3a] | 638 | extension( conditionalExpr ); |
---|
[6c4ff37] | 639 | output << "("; |
---|
[51587aa] | 640 | conditionalExpr->get_arg1()->accept( *this ); |
---|
[6c4ff37] | 641 | output << " ? "; |
---|
[51587aa] | 642 | conditionalExpr->get_arg2()->accept( *this ); |
---|
[6c4ff37] | 643 | output << " : "; |
---|
[51587aa] | 644 | conditionalExpr->get_arg3()->accept( *this ); |
---|
[6c4ff37] | 645 | output << ")"; |
---|
[51587aa] | 646 | } |
---|
[71f4e4f] | 647 | |
---|
[8688ce1] | 648 | void CodeGenerator::visit( CommaExpr * commaExpr ) { |
---|
[e04ef3a] | 649 | extension( commaExpr ); |
---|
[6c4ff37] | 650 | output << "("; |
---|
[51587aa] | 651 | commaExpr->get_arg1()->accept( *this ); |
---|
[6c4ff37] | 652 | output << " , "; |
---|
[51587aa] | 653 | commaExpr->get_arg2()->accept( *this ); |
---|
[6c4ff37] | 654 | output << ")"; |
---|
[51587aa] | 655 | } |
---|
[71f4e4f] | 656 | |
---|
[907eccb] | 657 | void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) { assertf( false, "UntypedTupleExpr should not make it to Code Gen" ); } |
---|
| 658 | |
---|
| 659 | void CodeGenerator::visit( TupleExpr * tupleExpr ) { assertf( false, "TupleExpr should not make it to Code Gen" ); } |
---|
[71f4e4f] | 660 | |
---|
[8688ce1] | 661 | void CodeGenerator::visit( TypeExpr * typeExpr ) {} |
---|
[2b6c1e0] | 662 | |
---|
[8688ce1] | 663 | void CodeGenerator::visit( AsmExpr * asmExpr ) { |
---|
[7f5566b] | 664 | if ( asmExpr->get_inout() ) { |
---|
| 665 | output << "[ "; |
---|
| 666 | asmExpr->get_inout()->accept( *this ); |
---|
| 667 | output << " ] "; |
---|
| 668 | } // if |
---|
| 669 | asmExpr->get_constraint()->accept( *this ); |
---|
| 670 | output << " ( "; |
---|
| 671 | asmExpr->get_operand()->accept( *this ); |
---|
| 672 | output << " )"; |
---|
| 673 | } |
---|
| 674 | |
---|
[3c13c03] | 675 | void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) { |
---|
[fbcde64] | 676 | assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) ); |
---|
| 677 | output << "(" << genType( compLitExpr->get_result(), "", pretty ) << ")"; |
---|
[3c13c03] | 678 | compLitExpr->get_initializer()->accept( *this ); |
---|
| 679 | } |
---|
| 680 | |
---|
[6eb8948] | 681 | void CodeGenerator::visit( StmtExpr * stmtExpr ) { |
---|
[3c13c03] | 682 | std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); |
---|
| 683 | output << "({" << std::endl; |
---|
| 684 | cur_indent += CodeGenerator::tabsize; |
---|
| 685 | unsigned int numStmts = stmts.size(); |
---|
| 686 | unsigned int i = 0; |
---|
| 687 | for ( Statement * stmt : stmts ) { |
---|
| 688 | output << indent << printLabels( stmt->get_labels() ); |
---|
| 689 | if ( i+1 == numStmts ) { |
---|
| 690 | // last statement in a statement expression needs to be handled specially - |
---|
| 691 | // cannot cast to void, otherwise the expression statement has no value |
---|
| 692 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) { |
---|
| 693 | exprStmt->get_expr()->accept( *this ); |
---|
| 694 | output << ";" << endl; |
---|
| 695 | ++i; |
---|
| 696 | break; |
---|
| 697 | } |
---|
| 698 | } |
---|
| 699 | stmt->accept( *this ); |
---|
| 700 | output << endl; |
---|
| 701 | if ( wantSpacing( stmt ) ) { |
---|
| 702 | output << endl; |
---|
| 703 | } // if |
---|
| 704 | ++i; |
---|
| 705 | } |
---|
| 706 | cur_indent -= CodeGenerator::tabsize; |
---|
| 707 | output << indent << "})"; |
---|
[6eb8948] | 708 | } |
---|
| 709 | |
---|
[51587aa] | 710 | //*** Statements |
---|
[8688ce1] | 711 | void CodeGenerator::visit( CompoundStmt * compoundStmt ) { |
---|
[51587aa] | 712 | std::list<Statement*> ks = compoundStmt->get_kids(); |
---|
[2b6c1e0] | 713 | output << "{" << endl; |
---|
[51587aa] | 714 | |
---|
[2b6c1e0] | 715 | cur_indent += CodeGenerator::tabsize; |
---|
[51587aa] | 716 | |
---|
[7f5566b] | 717 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { |
---|
[cda48b6] | 718 | output << indent << printLabels( (*i)->get_labels() ); |
---|
[7f5566b] | 719 | (*i)->accept( *this ); |
---|
[2b6c1e0] | 720 | |
---|
[6c4ff37] | 721 | output << endl; |
---|
[2b6c1e0] | 722 | if ( wantSpacing( *i ) ) { |
---|
| 723 | output << endl; |
---|
[3778cb2] | 724 | } // if |
---|
[8688ce1] | 725 | } // for |
---|
[71f4e4f] | 726 | cur_indent -= CodeGenerator::tabsize; |
---|
[51587aa] | 727 | |
---|
[cda48b6] | 728 | output << indent << "}"; |
---|
[51587aa] | 729 | } |
---|
| 730 | |
---|
[8688ce1] | 731 | void CodeGenerator::visit( ExprStmt * exprStmt ) { |
---|
[6c4ff37] | 732 | assert( exprStmt ); |
---|
[321a2481] | 733 | // cast the top-level expression to void to reduce gcc warnings. |
---|
| 734 | Expression * expr = new CastExpr( exprStmt->get_expr() ); |
---|
| 735 | expr->accept( *this ); |
---|
| 736 | output << ";"; |
---|
[51587aa] | 737 | } |
---|
| 738 | |
---|
[8688ce1] | 739 | void CodeGenerator::visit( AsmStmt * asmStmt ) { |
---|
[7f5566b] | 740 | output << "asm "; |
---|
| 741 | if ( asmStmt->get_voltile() ) output << "volatile "; |
---|
| 742 | if ( ! asmStmt->get_gotolabels().empty() ) output << "goto "; |
---|
| 743 | output << "( "; |
---|
| 744 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this ); |
---|
| 745 | output << " : "; |
---|
| 746 | genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() ); |
---|
| 747 | output << " : "; |
---|
| 748 | genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() ); |
---|
| 749 | output << " : "; |
---|
| 750 | genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() ); |
---|
| 751 | if ( ! asmStmt->get_gotolabels().empty() ) { |
---|
| 752 | output << " : "; |
---|
| 753 | for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) { |
---|
| 754 | output << *begin++; |
---|
| 755 | if ( begin == asmStmt->get_gotolabels().end() ) break; |
---|
| 756 | output << ", "; |
---|
| 757 | } // for |
---|
| 758 | } // if |
---|
| 759 | output << " );" ; |
---|
| 760 | } |
---|
| 761 | |
---|
[e994912] | 762 | void CodeGenerator::visit( AsmDecl * asmDecl ) { |
---|
| 763 | output << "asm "; |
---|
| 764 | AsmStmt * asmStmt = asmDecl->get_stmt(); |
---|
| 765 | output << "( "; |
---|
| 766 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this ); |
---|
| 767 | output << " )" ; |
---|
| 768 | } |
---|
| 769 | |
---|
[8688ce1] | 770 | void CodeGenerator::visit( IfStmt * ifStmt ) { |
---|
[7f5566b] | 771 | output << "if ( "; |
---|
| 772 | ifStmt->get_condition()->accept( *this ); |
---|
| 773 | output << " ) "; |
---|
[51587aa] | 774 | |
---|
[7f5566b] | 775 | ifStmt->get_thenPart()->accept( *this ); |
---|
[51587aa] | 776 | |
---|
| 777 | if ( ifStmt->get_elsePart() != 0) { |
---|
[2b6c1e0] | 778 | output << " else "; |
---|
[7f5566b] | 779 | ifStmt->get_elsePart()->accept( *this ); |
---|
[51587aa] | 780 | } // if |
---|
| 781 | } |
---|
| 782 | |
---|
[8688ce1] | 783 | void CodeGenerator::visit( SwitchStmt * switchStmt ) { |
---|
[7f5566b] | 784 | output << "switch ( " ; |
---|
| 785 | switchStmt->get_condition()->accept( *this ); |
---|
| 786 | output << " ) "; |
---|
[71f4e4f] | 787 | |
---|
[2b6c1e0] | 788 | output << "{" << std::endl; |
---|
[6c4ff37] | 789 | cur_indent += CodeGenerator::tabsize; |
---|
[8688ce1] | 790 | acceptAll( switchStmt->get_statements(), *this ); |
---|
[6c4ff37] | 791 | cur_indent -= CodeGenerator::tabsize; |
---|
[cda48b6] | 792 | output << indent << "}"; |
---|
[51587aa] | 793 | } |
---|
| 794 | |
---|
[8688ce1] | 795 | void CodeGenerator::visit( CaseStmt * caseStmt ) { |
---|
[cda48b6] | 796 | output << indent; |
---|
[eb3261f] | 797 | if ( caseStmt->isDefault()) { |
---|
[2b6c1e0] | 798 | output << "default"; |
---|
[eb3261f] | 799 | } else { |
---|
[2b6c1e0] | 800 | output << "case "; |
---|
[7f5566b] | 801 | caseStmt->get_condition()->accept( *this ); |
---|
[17cd4eb] | 802 | } // if |
---|
[6c4ff37] | 803 | output << ":\n"; |
---|
[71f4e4f] | 804 | |
---|
[51587aa] | 805 | std::list<Statement *> sts = caseStmt->get_statements(); |
---|
| 806 | |
---|
[6c4ff37] | 807 | cur_indent += CodeGenerator::tabsize; |
---|
[51587aa] | 808 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { |
---|
[cda48b6] | 809 | output << indent << printLabels( (*i)->get_labels() ) ; |
---|
[7f5566b] | 810 | (*i)->accept( *this ); |
---|
[6c4ff37] | 811 | output << endl; |
---|
[3778cb2] | 812 | } // for |
---|
[6c4ff37] | 813 | cur_indent -= CodeGenerator::tabsize; |
---|
[51587aa] | 814 | } |
---|
| 815 | |
---|
[8688ce1] | 816 | void CodeGenerator::visit( BranchStmt * branchStmt ) { |
---|
[51587aa] | 817 | switch ( branchStmt->get_type()) { |
---|
| 818 | case BranchStmt::Goto: |
---|
| 819 | if ( ! branchStmt->get_target().empty() ) |
---|
[6c4ff37] | 820 | output << "goto " << branchStmt->get_target(); |
---|
[71f4e4f] | 821 | else { |
---|
[51587aa] | 822 | if ( branchStmt->get_computedTarget() != 0 ) { |
---|
[6c4ff37] | 823 | output << "goto *"; |
---|
[51587aa] | 824 | branchStmt->get_computedTarget()->accept( *this ); |
---|
| 825 | } // if |
---|
| 826 | } // if |
---|
| 827 | break; |
---|
| 828 | case BranchStmt::Break: |
---|
[6c4ff37] | 829 | output << "break"; |
---|
[51587aa] | 830 | break; |
---|
| 831 | case BranchStmt::Continue: |
---|
[6c4ff37] | 832 | output << "continue"; |
---|
[51587aa] | 833 | break; |
---|
[3778cb2] | 834 | } // switch |
---|
[2b6c1e0] | 835 | output << ";"; |
---|
[51587aa] | 836 | } |
---|
| 837 | |
---|
[8688ce1] | 838 | void CodeGenerator::visit( ReturnStmt * returnStmt ) { |
---|
[6c4ff37] | 839 | output << "return "; |
---|
[4b2589a] | 840 | maybeAccept( returnStmt->get_expr(), *this ); |
---|
[6c4ff37] | 841 | output << ";"; |
---|
[51587aa] | 842 | } |
---|
| 843 | |
---|
[8688ce1] | 844 | void CodeGenerator::visit( WhileStmt * whileStmt ) { |
---|
[321a2481] | 845 | if ( whileStmt->get_isDoWhile() ) { |
---|
[6c4ff37] | 846 | output << "do" ; |
---|
[321a2481] | 847 | } else { |
---|
[6c4ff37] | 848 | output << "while (" ; |
---|
[7f5566b] | 849 | whileStmt->get_condition()->accept( *this ); |
---|
[6c4ff37] | 850 | output << ")"; |
---|
[51587aa] | 851 | } // if |
---|
[2b6c1e0] | 852 | output << " "; |
---|
[51587aa] | 853 | |
---|
[2b6c1e0] | 854 | output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() ); |
---|
[51587aa] | 855 | whileStmt->get_body()->accept( *this ); |
---|
| 856 | |
---|
[cda48b6] | 857 | output << indent; |
---|
[51587aa] | 858 | |
---|
| 859 | if ( whileStmt->get_isDoWhile() ) { |
---|
[6c4ff37] | 860 | output << " while (" ; |
---|
[7f5566b] | 861 | whileStmt->get_condition()->accept( *this ); |
---|
[6c4ff37] | 862 | output << ");"; |
---|
[51587aa] | 863 | } // if |
---|
| 864 | } |
---|
| 865 | |
---|
[8688ce1] | 866 | void CodeGenerator::visit( ForStmt * forStmt ) { |
---|
[8e9cbb2] | 867 | // initialization is always hoisted, so don't bother doing anything with that |
---|
[145f1fc] | 868 | output << "for (;"; |
---|
[51587aa] | 869 | |
---|
[321a2481] | 870 | if ( forStmt->get_condition() != 0 ) { |
---|
[51587aa] | 871 | forStmt->get_condition()->accept( *this ); |
---|
[3778cb2] | 872 | } // if |
---|
[6c4ff37] | 873 | output << ";"; |
---|
[51587aa] | 874 | |
---|
[321a2481] | 875 | if ( forStmt->get_increment() != 0 ) { |
---|
| 876 | // cast the top-level expression to void to reduce gcc warnings. |
---|
| 877 | Expression * expr = new CastExpr( forStmt->get_increment() ); |
---|
| 878 | expr->accept( *this ); |
---|
[3778cb2] | 879 | } // if |
---|
[2b6c1e0] | 880 | output << ") "; |
---|
[51587aa] | 881 | |
---|
| 882 | if ( forStmt->get_body() != 0 ) { |
---|
[2b6c1e0] | 883 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() ); |
---|
[51587aa] | 884 | forStmt->get_body()->accept( *this ); |
---|
| 885 | } // if |
---|
| 886 | } |
---|
| 887 | |
---|
[8688ce1] | 888 | void CodeGenerator::visit( NullStmt * nullStmt ) { |
---|
[cda48b6] | 889 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); |
---|
[6c4ff37] | 890 | output << "/* null statement */ ;"; |
---|
[51587aa] | 891 | } |
---|
| 892 | |
---|
[8688ce1] | 893 | void CodeGenerator::visit( DeclStmt * declStmt ) { |
---|
[51587aa] | 894 | declStmt->get_decl()->accept( *this ); |
---|
[71f4e4f] | 895 | |
---|
[51587aa] | 896 | if ( doSemicolon( declStmt->get_decl() ) ) { |
---|
[6c4ff37] | 897 | output << ";"; |
---|
[51587aa] | 898 | } // if |
---|
| 899 | } |
---|
| 900 | |
---|
[dd020c0] | 901 | void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) { |
---|
[fb04321] | 902 | if ( decl->get_storageClasses().any() ) { |
---|
[6e8bd43] | 903 | decl->get_storageClasses().print( output ); |
---|
[a7c90d4] | 904 | } // if |
---|
[dd020c0] | 905 | } // CodeGenerator::handleStorageClass |
---|
[9facf3b] | 906 | |
---|
| 907 | std::string genName( DeclarationWithType * decl ) { |
---|
| 908 | CodeGen::OperatorInfo opInfo; |
---|
| 909 | if ( operatorLookup( decl->get_name(), opInfo ) ) { |
---|
| 910 | return opInfo.outputName; |
---|
| 911 | } else { |
---|
| 912 | return decl->get_name(); |
---|
| 913 | } // if |
---|
| 914 | } |
---|
[51b7345] | 915 | } // namespace CodeGen |
---|
[51587aa] | 916 | |
---|
| 917 | // Local Variables: // |
---|
| 918 | // tab-width: 4 // |
---|
| 919 | // mode: c++ // |
---|
| 920 | // compile-command: "make install" // |
---|
| 921 | // End: // |
---|