[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 |
---|
[e612146c] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Sun Sep 3 20:42:52 2017 |
---|
| 13 | // Update Count : 490 |
---|
[51587aa] | 14 | // |
---|
[3268a58] | 15 | #include "CodeGenerator.h" |
---|
[51587aa] | 16 | |
---|
[bf2438c] | 17 | #include <cassert> // for assert, assertf |
---|
| 18 | #include <list> // for _List_iterator, list, list<>::it... |
---|
[51b7345] | 19 | |
---|
[bf2438c] | 20 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 21 | #include "Common/utility.h" // for CodeLocation, toString |
---|
| 22 | #include "GenType.h" // for genType |
---|
| 23 | #include "InitTweak/InitTweak.h" // for getPointerBase |
---|
| 24 | #include "OperatorTable.h" // for OperatorInfo, operatorLookup |
---|
| 25 | #include "Parser/LinkageSpec.h" // for Spec, Intrinsic |
---|
| 26 | #include "SynTree/Attribute.h" // for Attribute |
---|
| 27 | #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode |
---|
| 28 | #include "SynTree/Constant.h" // for Constant |
---|
| 29 | #include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl |
---|
| 30 | #include "SynTree/Expression.h" // for Expression, UntypedExpr, Applica... |
---|
| 31 | #include "SynTree/Initializer.h" // for Initializer, ListInit, Designation |
---|
| 32 | #include "SynTree/Label.h" // for Label, operator<< |
---|
| 33 | #include "SynTree/Statement.h" // for Statement, AsmStmt, BranchStmt |
---|
| 34 | #include "SynTree/Type.h" // for Type, Type::StorageClasses, Func... |
---|
[10a7775] | 35 | |
---|
[51b7345] | 36 | using namespace std; |
---|
| 37 | |
---|
| 38 | namespace CodeGen { |
---|
[6c4ff37] | 39 | int CodeGenerator::tabsize = 4; |
---|
[51587aa] | 40 | |
---|
[145f1fc] | 41 | // the kinds of statements that would ideally be followed by whitespace |
---|
[2b6c1e0] | 42 | bool wantSpacing( Statement * stmt) { |
---|
| 43 | return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) || |
---|
[08061589] | 44 | dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt ); |
---|
[2b6c1e0] | 45 | } |
---|
| 46 | |
---|
[8688ce1] | 47 | void CodeGenerator::extension( Expression * expr ) { |
---|
[8e9cbb2] | 48 | if ( expr->get_extension() ) { |
---|
| 49 | output << "__extension__ "; |
---|
| 50 | } // if |
---|
| 51 | } // extension |
---|
| 52 | |
---|
[8688ce1] | 53 | void CodeGenerator::extension( Declaration * decl ) { |
---|
[8e9cbb2] | 54 | if ( decl->get_extension() ) { |
---|
| 55 | output << "__extension__ "; |
---|
| 56 | } // if |
---|
| 57 | } // extension |
---|
| 58 | |
---|
[58dd019] | 59 | void CodeGenerator::asmName( DeclarationWithType * decl ) { |
---|
[e612146c] | 60 | if ( ConstantExpr * asmName = dynamic_cast<ConstantExpr *>(decl->get_asmName()) ) { |
---|
[58dd019] | 61 | output << " asm ( " << asmName->get_constant()->get_value() << " )"; |
---|
| 62 | } // if |
---|
| 63 | } // extension |
---|
| 64 | |
---|
[888cbe4] | 65 | CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) { |
---|
| 66 | labels = &l; |
---|
| 67 | return *this; |
---|
| 68 | } |
---|
| 69 | |
---|
[8688ce1] | 70 | ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) { |
---|
[888cbe4] | 71 | std::list< Label > & labs = *printLabels.labels; |
---|
| 72 | // l.unique(); // assumes a sorted list. Why not use set? Does order matter? |
---|
| 73 | for ( Label & l : labs ) { |
---|
| 74 | output << l.get_name() + ": "; |
---|
| 75 | printLabels.cg.genAttributes( l.get_attributes() ); |
---|
[8688ce1] | 76 | } // for |
---|
[888cbe4] | 77 | return output; |
---|
| 78 | } |
---|
| 79 | |
---|
[d22e90f] | 80 | /* Using updateLocation at the beginning of a node and endl |
---|
[8bafacc] | 81 | * within a node should become the method of formating. |
---|
| 82 | */ |
---|
| 83 | void CodeGenerator::updateLocation( CodeLocation const & to ) { |
---|
[d22e90f] | 84 | // skip if linemarks shouldn't appear or if codelocation is unset |
---|
| 85 | if ( !lineMarks || to.isUnset() ) return; |
---|
| 86 | |
---|
| 87 | if ( currentLocation.followedBy( to, 0 ) ) { |
---|
[8bafacc] | 88 | return; |
---|
| 89 | } else if ( currentLocation.followedBy( to, 1 ) ) { |
---|
| 90 | output << "\n" << indent; |
---|
[d48e529] | 91 | currentLocation.first_line += 1; |
---|
[8bafacc] | 92 | } else if ( currentLocation.followedBy( to, 2 ) ) { |
---|
| 93 | output << "\n\n" << indent; |
---|
[d48e529] | 94 | currentLocation.first_line += 2; |
---|
[8bafacc] | 95 | } else { |
---|
[d48e529] | 96 | output << "\n# " << to.first_line << " \"" << to.filename |
---|
[8bafacc] | 97 | << "\"\n" << indent; |
---|
| 98 | currentLocation = to; |
---|
| 99 | } |
---|
| 100 | output << std::flush; |
---|
| 101 | } |
---|
[c850687] | 102 | |
---|
[8bafacc] | 103 | void CodeGenerator::updateLocation( BaseSyntaxNode const * to ) { |
---|
| 104 | updateLocation( to->location ); |
---|
[c850687] | 105 | } |
---|
| 106 | |
---|
[d22e90f] | 107 | // replace endl |
---|
| 108 | ostream & CodeGenerator::LineEnder::operator()( ostream & os ) const { |
---|
| 109 | // if ( !cg.lineMarks ) { |
---|
| 110 | // os << "\n" << cg.indent << std::flush; |
---|
| 111 | // } |
---|
| 112 | os << "\n" << std::flush; |
---|
| 113 | cg.currentLocation.first_line++; |
---|
| 114 | // os << "/* did endl; current loc is: " << cg.currentLocation.first_line << "*/"; |
---|
| 115 | return os; |
---|
[c850687] | 116 | } |
---|
| 117 | |
---|
[d22e90f] | 118 | CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ), endl( *this ) {} |
---|
[cda48b6] | 119 | |
---|
[486341f] | 120 | string CodeGenerator::mangleName( DeclarationWithType * decl ) { |
---|
[35b1bf4] | 121 | if ( pretty ) return decl->get_name(); |
---|
[51587aa] | 122 | if ( decl->get_mangleName() != "" ) { |
---|
[f326f99] | 123 | // need to incorporate scope level in order to differentiate names for destructors |
---|
| 124 | return decl->get_scopedMangleName(); |
---|
[51587aa] | 125 | } else { |
---|
| 126 | return decl->get_name(); |
---|
| 127 | } // if |
---|
| 128 | } |
---|
[94b4364] | 129 | |
---|
[44a81853] | 130 | void CodeGenerator::genAttributes( list< Attribute * > & attributes ) { |
---|
| 131 | if ( attributes.empty() ) return; |
---|
| 132 | output << "__attribute__ (("; |
---|
| 133 | for ( list< Attribute * >::iterator attr( attributes.begin() );; ) { |
---|
| 134 | output << (*attr)->get_name(); |
---|
| 135 | if ( ! (*attr)->get_parameters().empty() ) { |
---|
| 136 | output << "("; |
---|
| 137 | genCommaList( (*attr)->get_parameters().begin(), (*attr)->get_parameters().end() ); |
---|
| 138 | output << ")"; |
---|
| 139 | } // if |
---|
| 140 | if ( ++attr == attributes.end() ) break; |
---|
| 141 | output << ","; // separator |
---|
| 142 | } // for |
---|
| 143 | output << ")) "; |
---|
| 144 | } // CodeGenerator::genAttributes |
---|
[7baed7d] | 145 | |
---|
[9857e8d] | 146 | // *** BaseSyntaxNode |
---|
[d22e90f] | 147 | void CodeGenerator::previsit( BaseSyntaxNode * node ) { |
---|
[9857e8d] | 148 | // turn off automatic recursion for all nodes, to allow each visitor to |
---|
| 149 | // precisely control the order in which its children are visited. |
---|
| 150 | visit_children = false; |
---|
[d22e90f] | 151 | updateLocation( node ); |
---|
[9857e8d] | 152 | } |
---|
| 153 | |
---|
| 154 | // *** BaseSyntaxNode |
---|
| 155 | void CodeGenerator::postvisit( BaseSyntaxNode * node ) { |
---|
| 156 | std::stringstream ss; |
---|
| 157 | node->print( ss ); |
---|
| 158 | assertf( false, "Unhandled node reached in CodeGenerator: %s", ss.str().c_str() ); |
---|
| 159 | } |
---|
[7baed7d] | 160 | |
---|
[4810867] | 161 | // *** Declarations |
---|
[9857e8d] | 162 | void CodeGenerator::postvisit( FunctionDecl * functionDecl ) { |
---|
[8e9cbb2] | 163 | extension( functionDecl ); |
---|
[7baed7d] | 164 | genAttributes( functionDecl->get_attributes() ); |
---|
| 165 | |
---|
[51587aa] | 166 | handleStorageClass( functionDecl ); |
---|
[6e8bd43] | 167 | functionDecl->get_funcSpec().print( output ); |
---|
[dd020c0] | 168 | |
---|
[e39241b] | 169 | output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), pretty, genC ); |
---|
[51587aa] | 170 | |
---|
[58dd019] | 171 | asmName( functionDecl ); |
---|
| 172 | |
---|
[51587aa] | 173 | if ( functionDecl->get_statements() ) { |
---|
[9857e8d] | 174 | functionDecl->get_statements()->accept( *visitor ); |
---|
[51587aa] | 175 | } // if |
---|
| 176 | } |
---|
| 177 | |
---|
[9857e8d] | 178 | void CodeGenerator::postvisit( ObjectDecl * objectDecl ) { |
---|
[e39241b] | 179 | if (objectDecl->get_name().empty() && genC ) { |
---|
| 180 | // only generate an anonymous name when generating C code, otherwise it clutters the output too much |
---|
[d9c8a59] | 181 | static UniqueName name = { "__anonymous_object" }; |
---|
| 182 | objectDecl->set_name( name.newName() ); |
---|
| 183 | } |
---|
| 184 | |
---|
[8e9cbb2] | 185 | extension( objectDecl ); |
---|
[f9cebb5] | 186 | genAttributes( objectDecl->get_attributes() ); |
---|
| 187 | |
---|
[51587aa] | 188 | handleStorageClass( objectDecl ); |
---|
[e39241b] | 189 | output << genType( objectDecl->get_type(), mangleName( objectDecl ), pretty, genC ); |
---|
[71f4e4f] | 190 | |
---|
[58dd019] | 191 | asmName( objectDecl ); |
---|
| 192 | |
---|
[51587aa] | 193 | if ( objectDecl->get_init() ) { |
---|
[6c4ff37] | 194 | output << " = "; |
---|
[9857e8d] | 195 | objectDecl->get_init()->accept( *visitor ); |
---|
[51587aa] | 196 | } // if |
---|
[3778cb2] | 197 | |
---|
[51587aa] | 198 | if ( objectDecl->get_bitfieldWidth() ) { |
---|
[6c4ff37] | 199 | output << ":"; |
---|
[9857e8d] | 200 | objectDecl->get_bitfieldWidth()->accept( *visitor ); |
---|
[51587aa] | 201 | } // if |
---|
| 202 | } |
---|
| 203 | |
---|
[5f642e38] | 204 | void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) { |
---|
[c0aa336] | 205 | genAttributes( aggDecl->get_attributes() ); |
---|
[35b1bf4] | 206 | |
---|
[e39241b] | 207 | if( ! aggDecl->get_parameters().empty() && ! genC ) { |
---|
| 208 | // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); |
---|
| 209 | output << "forall("; |
---|
| 210 | genCommaList( aggDecl->get_parameters().begin(), aggDecl->get_parameters().end() ); |
---|
| 211 | output << ")" << endl; |
---|
[e6cee92] | 212 | output << indent; |
---|
[e39241b] | 213 | } |
---|
| 214 | |
---|
[a984e65] | 215 | output << kind << aggDecl->get_name(); |
---|
[71f4e4f] | 216 | |
---|
[2c57025] | 217 | if ( aggDecl->has_body() ) { |
---|
| 218 | std::list< Declaration * > & memb = aggDecl->get_members(); |
---|
[94b4364] | 219 | output << " {" << endl; |
---|
[51587aa] | 220 | |
---|
[f7cb0bc] | 221 | ++indent; |
---|
[3778cb2] | 222 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { |
---|
[87e08e24] | 223 | output << indent; |
---|
[9857e8d] | 224 | (*i)->accept( *visitor ); |
---|
[6c4ff37] | 225 | output << ";" << endl; |
---|
[3778cb2] | 226 | } // for |
---|
[51587aa] | 227 | |
---|
[f7cb0bc] | 228 | --indent; |
---|
[51587aa] | 229 | |
---|
[cda48b6] | 230 | output << indent << "}"; |
---|
[17cd4eb] | 231 | } // if |
---|
[51587aa] | 232 | } |
---|
[17cd4eb] | 233 | |
---|
[9857e8d] | 234 | void CodeGenerator::postvisit( StructDecl * structDecl ) { |
---|
[8e9cbb2] | 235 | extension( structDecl ); |
---|
[5f642e38] | 236 | handleAggregate( structDecl, "struct " ); |
---|
[51587aa] | 237 | } |
---|
[17cd4eb] | 238 | |
---|
[9857e8d] | 239 | void CodeGenerator::postvisit( UnionDecl * unionDecl ) { |
---|
[8e9cbb2] | 240 | extension( unionDecl ); |
---|
[5f642e38] | 241 | handleAggregate( unionDecl, "union " ); |
---|
[51587aa] | 242 | } |
---|
[71f4e4f] | 243 | |
---|
[9857e8d] | 244 | void CodeGenerator::postvisit( EnumDecl * enumDecl ) { |
---|
[8e9cbb2] | 245 | extension( enumDecl ); |
---|
[6c4ff37] | 246 | output << "enum "; |
---|
[c0aa336] | 247 | genAttributes( enumDecl->get_attributes() ); |
---|
[51587aa] | 248 | |
---|
[a984e65] | 249 | output << enumDecl->get_name(); |
---|
[71f4e4f] | 250 | |
---|
[8e9cbb2] | 251 | std::list< Declaration* > &memb = enumDecl->get_members(); |
---|
[51587aa] | 252 | |
---|
| 253 | if ( ! memb.empty() ) { |
---|
[cda48b6] | 254 | output << " {" << endl; |
---|
[51587aa] | 255 | |
---|
[f7cb0bc] | 256 | ++indent; |
---|
[51587aa] | 257 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { |
---|
[8688ce1] | 258 | ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); |
---|
[51587aa] | 259 | assert( obj ); |
---|
[87e08e24] | 260 | output << indent << mangleName( obj ); |
---|
[51587aa] | 261 | if ( obj->get_init() ) { |
---|
[6c4ff37] | 262 | output << " = "; |
---|
[9857e8d] | 263 | obj->get_init()->accept( *visitor ); |
---|
[51587aa] | 264 | } // if |
---|
[6c4ff37] | 265 | output << "," << endl; |
---|
[51587aa] | 266 | } // for |
---|
| 267 | |
---|
[f7cb0bc] | 268 | --indent; |
---|
[51587aa] | 269 | |
---|
[cda48b6] | 270 | output << indent << "}"; |
---|
[51587aa] | 271 | } // if |
---|
| 272 | } |
---|
[71f4e4f] | 273 | |
---|
[9857e8d] | 274 | void CodeGenerator::postvisit( TraitDecl * traitDecl ) { |
---|
[a0c7dc36] | 275 | assertf( ! genC, "TraitDecls should not reach code generation." ); |
---|
[a984e65] | 276 | extension( traitDecl ); |
---|
| 277 | handleAggregate( traitDecl, "trait " ); |
---|
| 278 | } |
---|
[71f4e4f] | 279 | |
---|
[9857e8d] | 280 | void CodeGenerator::postvisit( TypedefDecl * typeDecl ) { |
---|
[e39241b] | 281 | assertf( ! genC, "Typedefs are removed and substituted in earlier passes." ); |
---|
| 282 | output << "typedef "; |
---|
| 283 | output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl; |
---|
[51587aa] | 284 | } |
---|
[71f4e4f] | 285 | |
---|
[9857e8d] | 286 | void CodeGenerator::postvisit( TypeDecl * typeDecl ) { |
---|
[a0c7dc36] | 287 | assertf( ! genC, "TypeDecls should not reach code generation." ); |
---|
[ab4bff5] | 288 | output << typeDecl->genTypeString() << " " << typeDecl->name; |
---|
[f0ecf9b] | 289 | if ( typeDecl->sized ) { |
---|
[ab4bff5] | 290 | output << " | sized(" << typeDecl->name << ")"; |
---|
[a0c7dc36] | 291 | } |
---|
[ab4bff5] | 292 | if ( ! typeDecl->assertions.empty() ) { |
---|
[a0c7dc36] | 293 | output << " | { "; |
---|
[ab4bff5] | 294 | for ( DeclarationWithType * assert : typeDecl->assertions ) { |
---|
| 295 | assert->accept( *visitor ); |
---|
| 296 | output << "; "; |
---|
| 297 | } |
---|
[a0c7dc36] | 298 | output << " }"; |
---|
[e39241b] | 299 | } |
---|
[51587aa] | 300 | } |
---|
| 301 | |
---|
[9857e8d] | 302 | void CodeGenerator::postvisit( Designation * designation ) { |
---|
[e4d829b] | 303 | std::list< Expression * > designators = designation->get_designators(); |
---|
[e45215c] | 304 | if ( designators.size() == 0 ) return; |
---|
[e4d829b] | 305 | for ( Expression * des : designators ) { |
---|
[62423350] | 306 | if ( dynamic_cast< NameExpr * >( des ) || dynamic_cast< VariableExpr * >( des ) ) { |
---|
| 307 | // if expression is a NameExpr or VariableExpr, then initializing aggregate member |
---|
| 308 | output << "."; |
---|
[9857e8d] | 309 | des->accept( *visitor ); |
---|
[e4d829b] | 310 | } else { |
---|
[62423350] | 311 | // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt |
---|
| 312 | output << "["; |
---|
[9857e8d] | 313 | des->accept( *visitor ); |
---|
[62423350] | 314 | output << "]"; |
---|
[3778cb2] | 315 | } // if |
---|
| 316 | } // for |
---|
[e45215c] | 317 | output << " = "; |
---|
| 318 | } |
---|
| 319 | |
---|
[9857e8d] | 320 | void CodeGenerator::postvisit( SingleInit * init ) { |
---|
| 321 | init->get_value()->accept( *visitor ); |
---|
[51587aa] | 322 | } |
---|
| 323 | |
---|
[9857e8d] | 324 | void CodeGenerator::postvisit( ListInit * init ) { |
---|
[e4d829b] | 325 | auto initBegin = init->begin(); |
---|
| 326 | auto initEnd = init->end(); |
---|
| 327 | auto desigBegin = init->get_designations().begin(); |
---|
| 328 | auto desigEnd = init->get_designations().end(); |
---|
| 329 | |
---|
[6c4ff37] | 330 | output << "{ "; |
---|
[e4d829b] | 331 | for ( ; initBegin != initEnd && desigBegin != desigEnd; ) { |
---|
[9857e8d] | 332 | (*desigBegin)->accept( *visitor ); |
---|
| 333 | (*initBegin)->accept( *visitor ); |
---|
[e4d829b] | 334 | ++initBegin, ++desigBegin; |
---|
| 335 | if ( initBegin != initEnd ) { |
---|
| 336 | output << ", "; |
---|
| 337 | } |
---|
| 338 | } |
---|
[6c4ff37] | 339 | output << " }"; |
---|
[e4d829b] | 340 | assertf( initBegin == initEnd && desigBegin == desigEnd, "Initializers and designators not the same length. %s", toString( init ).c_str() ); |
---|
[51587aa] | 341 | } |
---|
| 342 | |
---|
[d22e90f] | 343 | void CodeGenerator::postvisit( ConstructorInit * init ){ |
---|
[e39241b] | 344 | assertf( ! genC, "ConstructorInit nodes should not reach code generation." ); |
---|
[e6cee92] | 345 | // pseudo-output for constructor/destructor pairs |
---|
[d22e90f] | 346 | output << "<ctorinit>{" << endl << ++indent << "ctor: "; |
---|
[9857e8d] | 347 | maybeAccept( init->get_ctor(), *visitor ); |
---|
[d22e90f] | 348 | output << ", " << endl << indent << "dtor: "; |
---|
[9857e8d] | 349 | maybeAccept( init->get_dtor(), *visitor ); |
---|
[d22e90f] | 350 | output << endl << --indent << "}"; |
---|
[fc638d2] | 351 | } |
---|
| 352 | |
---|
[9857e8d] | 353 | void CodeGenerator::postvisit( Constant * constant ) { |
---|
[6c4ff37] | 354 | output << constant->get_value() ; |
---|
[51587aa] | 355 | } |
---|
| 356 | |
---|
[4810867] | 357 | // *** Expressions |
---|
[9857e8d] | 358 | void CodeGenerator::postvisit( ApplicationExpr * applicationExpr ) { |
---|
[e04ef3a] | 359 | extension( applicationExpr ); |
---|
[8688ce1] | 360 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { |
---|
[51587aa] | 361 | OperatorInfo opInfo; |
---|
| 362 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { |
---|
| 363 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin(); |
---|
| 364 | switch ( opInfo.type ) { |
---|
| 365 | case OT_INDEX: |
---|
| 366 | assert( applicationExpr->get_args().size() == 2 ); |
---|
[9857e8d] | 367 | (*arg++)->accept( *visitor ); |
---|
[6c4ff37] | 368 | output << "["; |
---|
[9857e8d] | 369 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 370 | output << "]"; |
---|
[51587aa] | 371 | break; |
---|
[71f4e4f] | 372 | |
---|
[51587aa] | 373 | case OT_CALL: |
---|
[356189a] | 374 | // there are no intrinsic definitions of the function call operator |
---|
[51587aa] | 375 | assert( false ); |
---|
| 376 | break; |
---|
[71f4e4f] | 377 | |
---|
[f1e012b] | 378 | case OT_CTOR: |
---|
[c2ce2350] | 379 | case OT_DTOR: |
---|
[356189a] | 380 | if ( applicationExpr->get_args().size() == 1 ) { |
---|
[8e9cbb2] | 381 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
| 382 | // effects, so must still output this expression |
---|
[64071c2] | 383 | output << "("; |
---|
[9857e8d] | 384 | (*arg++)->accept( *visitor ); |
---|
[64071c2] | 385 | output << ") /* " << opInfo.inputName << " */"; |
---|
[356189a] | 386 | } else if ( applicationExpr->get_args().size() == 2 ) { |
---|
[c2ce2350] | 387 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
[356189a] | 388 | output << "("; |
---|
[9857e8d] | 389 | (*arg++)->accept( *visitor ); |
---|
[356189a] | 390 | output << opInfo.symbol; |
---|
[9857e8d] | 391 | (*arg)->accept( *visitor ); |
---|
[c2ce2350] | 392 | output << ") /* " << opInfo.inputName << " */"; |
---|
[356189a] | 393 | } else { |
---|
[c2ce2350] | 394 | // no constructors with 0 or more than 2 parameters |
---|
[356189a] | 395 | assert( false ); |
---|
[8688ce1] | 396 | } // if |
---|
[356189a] | 397 | break; |
---|
[f1e012b] | 398 | |
---|
[51587aa] | 399 | case OT_PREFIX: |
---|
| 400 | case OT_PREFIXASSIGN: |
---|
| 401 | assert( applicationExpr->get_args().size() == 1 ); |
---|
[6c4ff37] | 402 | output << "("; |
---|
| 403 | output << opInfo.symbol; |
---|
[9857e8d] | 404 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 405 | output << ")"; |
---|
[51587aa] | 406 | break; |
---|
[71f4e4f] | 407 | |
---|
[51587aa] | 408 | case OT_POSTFIX: |
---|
| 409 | case OT_POSTFIXASSIGN: |
---|
| 410 | assert( applicationExpr->get_args().size() == 1 ); |
---|
[9857e8d] | 411 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 412 | output << opInfo.symbol; |
---|
[51587aa] | 413 | break; |
---|
| 414 | |
---|
[f1e012b] | 415 | |
---|
[51587aa] | 416 | case OT_INFIX: |
---|
| 417 | case OT_INFIXASSIGN: |
---|
| 418 | assert( applicationExpr->get_args().size() == 2 ); |
---|
[6c4ff37] | 419 | output << "("; |
---|
[9857e8d] | 420 | (*arg++)->accept( *visitor ); |
---|
[6c4ff37] | 421 | output << opInfo.symbol; |
---|
[9857e8d] | 422 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 423 | output << ")"; |
---|
[51587aa] | 424 | break; |
---|
[71f4e4f] | 425 | |
---|
[51587aa] | 426 | case OT_CONSTANT: |
---|
[721f17a] | 427 | case OT_LABELADDRESS: |
---|
| 428 | // there are no intrinsic definitions of 0/1 or label addresses as functions |
---|
[51587aa] | 429 | assert( false ); |
---|
[3778cb2] | 430 | } // switch |
---|
[17cd4eb] | 431 | } else { |
---|
[9857e8d] | 432 | varExpr->accept( *visitor ); |
---|
[6c4ff37] | 433 | output << "("; |
---|
[51587aa] | 434 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 435 | output << ")"; |
---|
[17cd4eb] | 436 | } // if |
---|
[51587aa] | 437 | } else { |
---|
[9857e8d] | 438 | applicationExpr->get_function()->accept( *visitor ); |
---|
[6c4ff37] | 439 | output << "("; |
---|
[51587aa] | 440 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 441 | output << ")"; |
---|
[51587aa] | 442 | } // if |
---|
| 443 | } |
---|
[71f4e4f] | 444 | |
---|
[9857e8d] | 445 | void CodeGenerator::postvisit( UntypedExpr * untypedExpr ) { |
---|
[e04ef3a] | 446 | extension( untypedExpr ); |
---|
[22bc276] | 447 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->function ) ) { |
---|
[51587aa] | 448 | OperatorInfo opInfo; |
---|
[22bc276] | 449 | if ( operatorLookup( nameExpr->name, opInfo ) ) { |
---|
| 450 | std::list< Expression* >::iterator arg = untypedExpr->args.begin(); |
---|
[51587aa] | 451 | switch ( opInfo.type ) { |
---|
| 452 | case OT_INDEX: |
---|
[22bc276] | 453 | assert( untypedExpr->args.size() == 2 ); |
---|
[9857e8d] | 454 | (*arg++)->accept( *visitor ); |
---|
[6c4ff37] | 455 | output << "["; |
---|
[9857e8d] | 456 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 457 | output << "]"; |
---|
[51587aa] | 458 | break; |
---|
[71f4e4f] | 459 | |
---|
[51587aa] | 460 | case OT_CALL: |
---|
[f1e012b] | 461 | assert( false ); |
---|
| 462 | |
---|
[c2ce2350] | 463 | case OT_CTOR: |
---|
| 464 | case OT_DTOR: |
---|
[22bc276] | 465 | if ( untypedExpr->args.size() == 1 ) { |
---|
[8e9cbb2] | 466 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
| 467 | // effects, so must still output this expression |
---|
[64071c2] | 468 | output << "("; |
---|
[9857e8d] | 469 | (*arg++)->accept( *visitor ); |
---|
[64071c2] | 470 | output << ") /* " << opInfo.inputName << " */"; |
---|
[c2ce2350] | 471 | } else if ( untypedExpr->get_args().size() == 2 ) { |
---|
| 472 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
| 473 | output << "("; |
---|
[9857e8d] | 474 | (*arg++)->accept( *visitor ); |
---|
[c2ce2350] | 475 | output << opInfo.symbol; |
---|
[9857e8d] | 476 | (*arg)->accept( *visitor ); |
---|
[c2ce2350] | 477 | output << ") /* " << opInfo.inputName << " */"; |
---|
| 478 | } else { |
---|
| 479 | // no constructors with 0 or more than 2 parameters |
---|
[4e8949f] | 480 | assertf( ! genC, "UntypedExpr constructor/destructor with 0 or more than 2 parameters." ); |
---|
| 481 | output << "("; |
---|
| 482 | (*arg++)->accept( *visitor ); |
---|
| 483 | output << opInfo.symbol << "{ "; |
---|
[22bc276] | 484 | genCommaList( arg, untypedExpr->args.end() ); |
---|
[4e8949f] | 485 | output << "}) /* " << opInfo.inputName << " */"; |
---|
[3778cb2] | 486 | } // if |
---|
[51587aa] | 487 | break; |
---|
[71f4e4f] | 488 | |
---|
[51587aa] | 489 | case OT_PREFIX: |
---|
| 490 | case OT_PREFIXASSIGN: |
---|
[de62360d] | 491 | case OT_LABELADDRESS: |
---|
[22bc276] | 492 | assert( untypedExpr->args.size() == 1 ); |
---|
[6c4ff37] | 493 | output << "("; |
---|
| 494 | output << opInfo.symbol; |
---|
[9857e8d] | 495 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 496 | output << ")"; |
---|
[51587aa] | 497 | break; |
---|
[71f4e4f] | 498 | |
---|
[51587aa] | 499 | case OT_POSTFIX: |
---|
| 500 | case OT_POSTFIXASSIGN: |
---|
[22bc276] | 501 | assert( untypedExpr->args.size() == 1 ); |
---|
[9857e8d] | 502 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 503 | output << opInfo.symbol; |
---|
[51587aa] | 504 | break; |
---|
[71f4e4f] | 505 | |
---|
[51587aa] | 506 | case OT_INFIX: |
---|
| 507 | case OT_INFIXASSIGN: |
---|
[22bc276] | 508 | assert( untypedExpr->args.size() == 2 ); |
---|
[6c4ff37] | 509 | output << "("; |
---|
[9857e8d] | 510 | (*arg++)->accept( *visitor ); |
---|
[6c4ff37] | 511 | output << opInfo.symbol; |
---|
[9857e8d] | 512 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 513 | output << ")"; |
---|
[51587aa] | 514 | break; |
---|
[71f4e4f] | 515 | |
---|
[51587aa] | 516 | case OT_CONSTANT: |
---|
| 517 | // there are no intrinsic definitions of 0 or 1 as functions |
---|
| 518 | assert( false ); |
---|
[3778cb2] | 519 | } // switch |
---|
[51587aa] | 520 | } else { |
---|
[22bc276] | 521 | // builtin routines |
---|
| 522 | nameExpr->accept( *visitor ); |
---|
| 523 | output << "("; |
---|
| 524 | genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() ); |
---|
| 525 | output << ")"; |
---|
[51587aa] | 526 | } // if |
---|
| 527 | } else { |
---|
[22bc276] | 528 | untypedExpr->function->accept( *visitor ); |
---|
[6c4ff37] | 529 | output << "("; |
---|
[22bc276] | 530 | genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() ); |
---|
[6c4ff37] | 531 | output << ")"; |
---|
[51587aa] | 532 | } // if |
---|
| 533 | } |
---|
[71f4e4f] | 534 | |
---|
[9857e8d] | 535 | void CodeGenerator::postvisit( RangeExpr * rangeExpr ) { |
---|
[22bc276] | 536 | rangeExpr->low->accept( *visitor ); |
---|
[064e3ff] | 537 | output << " ... "; |
---|
[22bc276] | 538 | rangeExpr->high->accept( *visitor ); |
---|
[064e3ff] | 539 | } |
---|
| 540 | |
---|
[9857e8d] | 541 | void CodeGenerator::postvisit( NameExpr * nameExpr ) { |
---|
[e04ef3a] | 542 | extension( nameExpr ); |
---|
[51587aa] | 543 | OperatorInfo opInfo; |
---|
[5ea7a22] | 544 | if ( operatorLookup( nameExpr->name, opInfo ) ) { |
---|
| 545 | if ( opInfo.type == OT_CONSTANT ) { |
---|
| 546 | output << opInfo.symbol; |
---|
| 547 | } else { |
---|
| 548 | output << opInfo.outputName; |
---|
| 549 | } |
---|
[51587aa] | 550 | } else { |
---|
[6c4ff37] | 551 | output << nameExpr->get_name(); |
---|
[51587aa] | 552 | } // if |
---|
| 553 | } |
---|
[71f4e4f] | 554 | |
---|
[9857e8d] | 555 | void CodeGenerator::postvisit( AddressExpr * addressExpr ) { |
---|
[e04ef3a] | 556 | extension( addressExpr ); |
---|
[6c4ff37] | 557 | output << "(&"; |
---|
[9857e8d] | 558 | addressExpr->arg->accept( *visitor ); |
---|
[6c4ff37] | 559 | output << ")"; |
---|
[51587aa] | 560 | } |
---|
| 561 | |
---|
[9857e8d] | 562 | void CodeGenerator::postvisit( LabelAddressExpr *addressExpr ) { |
---|
[5809461] | 563 | extension( addressExpr ); |
---|
| 564 | output << "(&&" << addressExpr->arg << ")"; |
---|
| 565 | } |
---|
| 566 | |
---|
[9857e8d] | 567 | void CodeGenerator::postvisit( CastExpr * castExpr ) { |
---|
[e04ef3a] | 568 | extension( castExpr ); |
---|
[803deb1] | 569 | output << "("; |
---|
[906e24d] | 570 | if ( castExpr->get_result()->isVoid() ) { |
---|
[803deb1] | 571 | output << "(void)" ; |
---|
[d104b02] | 572 | } else { |
---|
| 573 | // at least one result type of cast. |
---|
| 574 | // Note: previously, lvalue casts were skipped. Since it's now impossible for the user to write |
---|
| 575 | // an lvalue cast, this has been taken out. |
---|
[803deb1] | 576 | output << "("; |
---|
[e39241b] | 577 | output << genType( castExpr->get_result(), "", pretty, genC ); |
---|
[71f4e4f] | 578 | output << ")"; |
---|
[3778cb2] | 579 | } // if |
---|
[9857e8d] | 580 | castExpr->get_arg()->accept( *visitor ); |
---|
[803deb1] | 581 | output << ")"; |
---|
[51587aa] | 582 | } |
---|
[71f4e4f] | 583 | |
---|
[9857e8d] | 584 | void CodeGenerator::postvisit( VirtualCastExpr * castExpr ) { |
---|
[a5f0529] | 585 | assertf( ! genC, "VirtualCastExpr should not reach code generation." ); |
---|
| 586 | extension( castExpr ); |
---|
| 587 | output << "(virtual "; |
---|
[9857e8d] | 588 | castExpr->get_arg()->accept( *visitor ); |
---|
[a5f0529] | 589 | output << ")"; |
---|
| 590 | } |
---|
| 591 | |
---|
[9857e8d] | 592 | void CodeGenerator::postvisit( UntypedMemberExpr * memberExpr ) { |
---|
[e39241b] | 593 | assertf( ! genC, "UntypedMemberExpr should not reach code generation." ); |
---|
| 594 | extension( memberExpr ); |
---|
[9857e8d] | 595 | memberExpr->get_aggregate()->accept( *visitor ); |
---|
[5f642e38] | 596 | output << "."; |
---|
[9857e8d] | 597 | memberExpr->get_member()->accept( *visitor ); |
---|
[51587aa] | 598 | } |
---|
[71f4e4f] | 599 | |
---|
[9857e8d] | 600 | void CodeGenerator::postvisit( MemberExpr * memberExpr ) { |
---|
[e04ef3a] | 601 | extension( memberExpr ); |
---|
[9857e8d] | 602 | memberExpr->get_aggregate()->accept( *visitor ); |
---|
[6c4ff37] | 603 | output << "." << mangleName( memberExpr->get_member() ); |
---|
[51587aa] | 604 | } |
---|
[71f4e4f] | 605 | |
---|
[9857e8d] | 606 | void CodeGenerator::postvisit( VariableExpr * variableExpr ) { |
---|
[e04ef3a] | 607 | extension( variableExpr ); |
---|
[51587aa] | 608 | OperatorInfo opInfo; |
---|
| 609 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { |
---|
[6c4ff37] | 610 | output << opInfo.symbol; |
---|
[51587aa] | 611 | } else { |
---|
[6c4ff37] | 612 | output << mangleName( variableExpr->get_var() ); |
---|
[51587aa] | 613 | } // if |
---|
| 614 | } |
---|
[71f4e4f] | 615 | |
---|
[9857e8d] | 616 | void CodeGenerator::postvisit( ConstantExpr * constantExpr ) { |
---|
[51587aa] | 617 | assert( constantExpr->get_constant() ); |
---|
[e04ef3a] | 618 | extension( constantExpr ); |
---|
[9857e8d] | 619 | constantExpr->get_constant()->accept( *visitor ); |
---|
[51587aa] | 620 | } |
---|
[71f4e4f] | 621 | |
---|
[9857e8d] | 622 | void CodeGenerator::postvisit( SizeofExpr * sizeofExpr ) { |
---|
[e04ef3a] | 623 | extension( sizeofExpr ); |
---|
[6c4ff37] | 624 | output << "sizeof("; |
---|
[51587aa] | 625 | if ( sizeofExpr->get_isType() ) { |
---|
[e39241b] | 626 | output << genType( sizeofExpr->get_type(), "", pretty, genC ); |
---|
[51587aa] | 627 | } else { |
---|
[9857e8d] | 628 | sizeofExpr->get_expr()->accept( *visitor ); |
---|
[51587aa] | 629 | } // if |
---|
[6c4ff37] | 630 | output << ")"; |
---|
[51587aa] | 631 | } |
---|
[47534159] | 632 | |
---|
[9857e8d] | 633 | void CodeGenerator::postvisit( AlignofExpr * alignofExpr ) { |
---|
[47534159] | 634 | // use GCC extension to avoid bumping std to C11 |
---|
[8e9cbb2] | 635 | extension( alignofExpr ); |
---|
[47534159] | 636 | output << "__alignof__("; |
---|
[25a054f] | 637 | if ( alignofExpr->get_isType() ) { |
---|
[e39241b] | 638 | output << genType( alignofExpr->get_type(), "", pretty, genC ); |
---|
[47534159] | 639 | } else { |
---|
[9857e8d] | 640 | alignofExpr->get_expr()->accept( *visitor ); |
---|
[47534159] | 641 | } // if |
---|
| 642 | output << ")"; |
---|
| 643 | } |
---|
[71f4e4f] | 644 | |
---|
[9857e8d] | 645 | void CodeGenerator::postvisit( UntypedOffsetofExpr * offsetofExpr ) { |
---|
[e39241b] | 646 | assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." ); |
---|
| 647 | output << "offsetof("; |
---|
| 648 | output << genType( offsetofExpr->get_type(), "", pretty, genC ); |
---|
| 649 | output << ", " << offsetofExpr->get_member(); |
---|
| 650 | output << ")"; |
---|
[2a4b088] | 651 | } |
---|
| 652 | |
---|
[9857e8d] | 653 | void CodeGenerator::postvisit( OffsetofExpr * offsetofExpr ) { |
---|
[25a054f] | 654 | // use GCC builtin |
---|
| 655 | output << "__builtin_offsetof("; |
---|
[e39241b] | 656 | output << genType( offsetofExpr->get_type(), "", pretty, genC ); |
---|
[e551c69] | 657 | output << ", " << mangleName( offsetofExpr->get_member() ); |
---|
[25a054f] | 658 | output << ")"; |
---|
| 659 | } |
---|
[d63eeb0] | 660 | |
---|
[9857e8d] | 661 | void CodeGenerator::postvisit( OffsetPackExpr * offsetPackExpr ) { |
---|
[e39241b] | 662 | assertf( ! genC, "OffsetPackExpr should not reach code generation." ); |
---|
| 663 | output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")"; |
---|
[afc1045] | 664 | } |
---|
[70a06f6] | 665 | |
---|
[9857e8d] | 666 | void CodeGenerator::postvisit( LogicalExpr * logicalExpr ) { |
---|
[e04ef3a] | 667 | extension( logicalExpr ); |
---|
[6c4ff37] | 668 | output << "("; |
---|
[9857e8d] | 669 | logicalExpr->get_arg1()->accept( *visitor ); |
---|
[51587aa] | 670 | if ( logicalExpr->get_isAnd() ) { |
---|
[6c4ff37] | 671 | output << " && "; |
---|
[51587aa] | 672 | } else { |
---|
[6c4ff37] | 673 | output << " || "; |
---|
[51587aa] | 674 | } // if |
---|
[9857e8d] | 675 | logicalExpr->get_arg2()->accept( *visitor ); |
---|
[6c4ff37] | 676 | output << ")"; |
---|
[51587aa] | 677 | } |
---|
[71f4e4f] | 678 | |
---|
[9857e8d] | 679 | void CodeGenerator::postvisit( ConditionalExpr * conditionalExpr ) { |
---|
[e04ef3a] | 680 | extension( conditionalExpr ); |
---|
[6c4ff37] | 681 | output << "("; |
---|
[9857e8d] | 682 | conditionalExpr->get_arg1()->accept( *visitor ); |
---|
[6c4ff37] | 683 | output << " ? "; |
---|
[9857e8d] | 684 | conditionalExpr->get_arg2()->accept( *visitor ); |
---|
[6c4ff37] | 685 | output << " : "; |
---|
[9857e8d] | 686 | conditionalExpr->get_arg3()->accept( *visitor ); |
---|
[6c4ff37] | 687 | output << ")"; |
---|
[51587aa] | 688 | } |
---|
[71f4e4f] | 689 | |
---|
[9857e8d] | 690 | void CodeGenerator::postvisit( CommaExpr * commaExpr ) { |
---|
[e04ef3a] | 691 | extension( commaExpr ); |
---|
[6c4ff37] | 692 | output << "("; |
---|
[8a6cf7e] | 693 | if ( genC ) { |
---|
| 694 | // arg1 of a CommaExpr is never used, so it can be safely cast to void to reduce gcc warnings. |
---|
| 695 | commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) ); |
---|
| 696 | } |
---|
[9857e8d] | 697 | commaExpr->get_arg1()->accept( *visitor ); |
---|
[6c4ff37] | 698 | output << " , "; |
---|
[9857e8d] | 699 | commaExpr->get_arg2()->accept( *visitor ); |
---|
[6c4ff37] | 700 | output << ")"; |
---|
[51587aa] | 701 | } |
---|
[71f4e4f] | 702 | |
---|
[9857e8d] | 703 | void CodeGenerator::postvisit( TupleAssignExpr * tupleExpr ) { |
---|
[d104b02] | 704 | assertf( ! genC, "TupleAssignExpr should not reach code generation." ); |
---|
[9857e8d] | 705 | tupleExpr->stmtExpr->accept( *visitor ); |
---|
[d104b02] | 706 | } |
---|
| 707 | |
---|
[9857e8d] | 708 | void CodeGenerator::postvisit( UntypedTupleExpr * tupleExpr ) { |
---|
[e39241b] | 709 | assertf( ! genC, "UntypedTupleExpr should not reach code generation." ); |
---|
[f975c65] | 710 | extension( tupleExpr ); |
---|
[e39241b] | 711 | output << "["; |
---|
| 712 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); |
---|
| 713 | output << "]"; |
---|
| 714 | } |
---|
[907eccb] | 715 | |
---|
[9857e8d] | 716 | void CodeGenerator::postvisit( TupleExpr * tupleExpr ) { |
---|
[e39241b] | 717 | assertf( ! genC, "TupleExpr should not reach code generation." ); |
---|
[f975c65] | 718 | extension( tupleExpr ); |
---|
[e39241b] | 719 | output << "["; |
---|
| 720 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); |
---|
| 721 | output << "]"; |
---|
| 722 | } |
---|
[71f4e4f] | 723 | |
---|
[9857e8d] | 724 | void CodeGenerator::postvisit( TupleIndexExpr * tupleExpr ) { |
---|
[f975c65] | 725 | assertf( ! genC, "TupleIndexExpr should not reach code generation." ); |
---|
| 726 | extension( tupleExpr ); |
---|
[9857e8d] | 727 | tupleExpr->get_tuple()->accept( *visitor ); |
---|
[f975c65] | 728 | output << "." << tupleExpr->get_index(); |
---|
| 729 | } |
---|
| 730 | |
---|
[9857e8d] | 731 | void CodeGenerator::postvisit( TypeExpr * typeExpr ) { |
---|
[e4d829b] | 732 | // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl; |
---|
| 733 | // assertf( ! genC, "TypeExpr should not reach code generation." ); |
---|
| 734 | if ( ! genC ) { |
---|
| 735 | output<< genType( typeExpr->get_type(), "", pretty, genC ); |
---|
| 736 | } |
---|
[e39241b] | 737 | } |
---|
[2b6c1e0] | 738 | |
---|
[9857e8d] | 739 | void CodeGenerator::postvisit( AsmExpr * asmExpr ) { |
---|
[7f5566b] | 740 | if ( asmExpr->get_inout() ) { |
---|
| 741 | output << "[ "; |
---|
[9857e8d] | 742 | asmExpr->get_inout()->accept( *visitor ); |
---|
[7f5566b] | 743 | output << " ] "; |
---|
| 744 | } // if |
---|
[9857e8d] | 745 | asmExpr->get_constraint()->accept( *visitor ); |
---|
[7f5566b] | 746 | output << " ( "; |
---|
[9857e8d] | 747 | asmExpr->get_operand()->accept( *visitor ); |
---|
[7f5566b] | 748 | output << " )"; |
---|
| 749 | } |
---|
| 750 | |
---|
[9857e8d] | 751 | void CodeGenerator::postvisit( CompoundLiteralExpr *compLitExpr ) { |
---|
[fbcde64] | 752 | assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) ); |
---|
[e39241b] | 753 | output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")"; |
---|
[9857e8d] | 754 | compLitExpr->get_initializer()->accept( *visitor ); |
---|
[3c13c03] | 755 | } |
---|
| 756 | |
---|
[9857e8d] | 757 | void CodeGenerator::postvisit( UniqueExpr * unqExpr ) { |
---|
[d104b02] | 758 | assertf( ! genC, "Unique expressions should not reach code generation." ); |
---|
| 759 | output << "unq<" << unqExpr->get_id() << ">{ "; |
---|
[9857e8d] | 760 | unqExpr->get_expr()->accept( *visitor ); |
---|
[d104b02] | 761 | output << " }"; |
---|
| 762 | } |
---|
| 763 | |
---|
[9857e8d] | 764 | void CodeGenerator::postvisit( StmtExpr * stmtExpr ) { |
---|
[44b4114] | 765 | std::list< Statement * > & stmts = stmtExpr->statements->kids; |
---|
[d22e90f] | 766 | output << "({" << endl; |
---|
[f7cb0bc] | 767 | ++indent; |
---|
[3c13c03] | 768 | unsigned int numStmts = stmts.size(); |
---|
| 769 | unsigned int i = 0; |
---|
| 770 | for ( Statement * stmt : stmts ) { |
---|
[058f549] | 771 | output << indent << printLabels( stmt->get_labels() ); |
---|
[3c13c03] | 772 | if ( i+1 == numStmts ) { |
---|
| 773 | // last statement in a statement expression needs to be handled specially - |
---|
| 774 | // cannot cast to void, otherwise the expression statement has no value |
---|
| 775 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) { |
---|
[44b4114] | 776 | exprStmt->expr->accept( *visitor ); |
---|
[3c13c03] | 777 | output << ";" << endl; |
---|
| 778 | ++i; |
---|
| 779 | break; |
---|
| 780 | } |
---|
| 781 | } |
---|
[9857e8d] | 782 | stmt->accept( *visitor ); |
---|
[3c13c03] | 783 | output << endl; |
---|
| 784 | if ( wantSpacing( stmt ) ) { |
---|
| 785 | output << endl; |
---|
| 786 | } // if |
---|
| 787 | ++i; |
---|
| 788 | } |
---|
[f7cb0bc] | 789 | --indent; |
---|
[3c13c03] | 790 | output << indent << "})"; |
---|
[6eb8948] | 791 | } |
---|
| 792 | |
---|
[4e8949f] | 793 | void CodeGenerator::postvisit( ConstructorExpr * expr ) { |
---|
| 794 | assertf( ! genC, "Unique expressions should not reach code generation." ); |
---|
| 795 | expr->callExpr->accept( *visitor ); |
---|
| 796 | } |
---|
| 797 | |
---|
[44b4114] | 798 | void CodeGenerator::postvisit( DeletedExpr * expr ) { |
---|
| 799 | assertf( ! genC, "Deleted expressions should not reach code generation." ); |
---|
| 800 | expr->expr->accept( *visitor ); |
---|
| 801 | } |
---|
| 802 | |
---|
[4810867] | 803 | // *** Statements |
---|
[9857e8d] | 804 | void CodeGenerator::postvisit( CompoundStmt * compoundStmt ) { |
---|
[51587aa] | 805 | std::list<Statement*> ks = compoundStmt->get_kids(); |
---|
[2b6c1e0] | 806 | output << "{" << endl; |
---|
[51587aa] | 807 | |
---|
[f7cb0bc] | 808 | ++indent; |
---|
[51587aa] | 809 | |
---|
[7f5566b] | 810 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { |
---|
[cda48b6] | 811 | output << indent << printLabels( (*i)->get_labels() ); |
---|
[9857e8d] | 812 | (*i)->accept( *visitor ); |
---|
[2b6c1e0] | 813 | |
---|
[6c4ff37] | 814 | output << endl; |
---|
[2b6c1e0] | 815 | if ( wantSpacing( *i ) ) { |
---|
| 816 | output << endl; |
---|
[3778cb2] | 817 | } // if |
---|
[8688ce1] | 818 | } // for |
---|
[f7cb0bc] | 819 | --indent; |
---|
[51587aa] | 820 | |
---|
[cda48b6] | 821 | output << indent << "}"; |
---|
[51587aa] | 822 | } |
---|
| 823 | |
---|
[9857e8d] | 824 | void CodeGenerator::postvisit( ExprStmt * exprStmt ) { |
---|
[6c4ff37] | 825 | assert( exprStmt ); |
---|
[262f085f] | 826 | if ( genC ) { |
---|
| 827 | // cast the top-level expression to void to reduce gcc warnings. |
---|
[8a6cf7e] | 828 | exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) ); |
---|
[262f085f] | 829 | } |
---|
[9857e8d] | 830 | exprStmt->get_expr()->accept( *visitor ); |
---|
[321a2481] | 831 | output << ";"; |
---|
[51587aa] | 832 | } |
---|
| 833 | |
---|
[9857e8d] | 834 | void CodeGenerator::postvisit( AsmStmt * asmStmt ) { |
---|
[7f5566b] | 835 | output << "asm "; |
---|
| 836 | if ( asmStmt->get_voltile() ) output << "volatile "; |
---|
| 837 | if ( ! asmStmt->get_gotolabels().empty() ) output << "goto "; |
---|
| 838 | output << "( "; |
---|
[9857e8d] | 839 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor ); |
---|
[7f5566b] | 840 | output << " : "; |
---|
| 841 | genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() ); |
---|
| 842 | output << " : "; |
---|
| 843 | genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() ); |
---|
| 844 | output << " : "; |
---|
| 845 | genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() ); |
---|
| 846 | if ( ! asmStmt->get_gotolabels().empty() ) { |
---|
| 847 | output << " : "; |
---|
| 848 | for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) { |
---|
| 849 | output << *begin++; |
---|
| 850 | if ( begin == asmStmt->get_gotolabels().end() ) break; |
---|
| 851 | output << ", "; |
---|
| 852 | } // for |
---|
| 853 | } // if |
---|
| 854 | output << " );" ; |
---|
| 855 | } |
---|
| 856 | |
---|
[9857e8d] | 857 | void CodeGenerator::postvisit( AsmDecl * asmDecl ) { |
---|
[e994912] | 858 | output << "asm "; |
---|
| 859 | AsmStmt * asmStmt = asmDecl->get_stmt(); |
---|
| 860 | output << "( "; |
---|
[9857e8d] | 861 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor ); |
---|
[e994912] | 862 | output << " )" ; |
---|
| 863 | } |
---|
| 864 | |
---|
[9857e8d] | 865 | void CodeGenerator::postvisit( IfStmt * ifStmt ) { |
---|
[7f5566b] | 866 | output << "if ( "; |
---|
[9857e8d] | 867 | ifStmt->get_condition()->accept( *visitor ); |
---|
[7f5566b] | 868 | output << " ) "; |
---|
[51587aa] | 869 | |
---|
[9857e8d] | 870 | ifStmt->get_thenPart()->accept( *visitor ); |
---|
[51587aa] | 871 | |
---|
| 872 | if ( ifStmt->get_elsePart() != 0) { |
---|
[2b6c1e0] | 873 | output << " else "; |
---|
[9857e8d] | 874 | ifStmt->get_elsePart()->accept( *visitor ); |
---|
[51587aa] | 875 | } // if |
---|
| 876 | } |
---|
| 877 | |
---|
[9857e8d] | 878 | void CodeGenerator::postvisit( SwitchStmt * switchStmt ) { |
---|
[7f5566b] | 879 | output << "switch ( " ; |
---|
[9857e8d] | 880 | switchStmt->get_condition()->accept( *visitor ); |
---|
[7f5566b] | 881 | output << " ) "; |
---|
[71f4e4f] | 882 | |
---|
[d22e90f] | 883 | output << "{" << endl; |
---|
[f7cb0bc] | 884 | ++indent; |
---|
[9857e8d] | 885 | acceptAll( switchStmt->get_statements(), *visitor ); |
---|
[f7cb0bc] | 886 | --indent; |
---|
[cda48b6] | 887 | output << indent << "}"; |
---|
[51587aa] | 888 | } |
---|
| 889 | |
---|
[9857e8d] | 890 | void CodeGenerator::postvisit( CaseStmt * caseStmt ) { |
---|
[8bafacc] | 891 | updateLocation( caseStmt ); |
---|
[1dcd9554] | 892 | output << indent; |
---|
[eb3261f] | 893 | if ( caseStmt->isDefault()) { |
---|
[2b6c1e0] | 894 | output << "default"; |
---|
[eb3261f] | 895 | } else { |
---|
[2b6c1e0] | 896 | output << "case "; |
---|
[9857e8d] | 897 | caseStmt->get_condition()->accept( *visitor ); |
---|
[17cd4eb] | 898 | } // if |
---|
[d22e90f] | 899 | output << ":" << endl; |
---|
[71f4e4f] | 900 | |
---|
[51587aa] | 901 | std::list<Statement *> sts = caseStmt->get_statements(); |
---|
| 902 | |
---|
[f7cb0bc] | 903 | ++indent; |
---|
[51587aa] | 904 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { |
---|
[cda48b6] | 905 | output << indent << printLabels( (*i)->get_labels() ) ; |
---|
[9857e8d] | 906 | (*i)->accept( *visitor ); |
---|
[6c4ff37] | 907 | output << endl; |
---|
[3778cb2] | 908 | } // for |
---|
[f7cb0bc] | 909 | --indent; |
---|
[51587aa] | 910 | } |
---|
| 911 | |
---|
[9857e8d] | 912 | void CodeGenerator::postvisit( BranchStmt * branchStmt ) { |
---|
[51587aa] | 913 | switch ( branchStmt->get_type()) { |
---|
| 914 | case BranchStmt::Goto: |
---|
| 915 | if ( ! branchStmt->get_target().empty() ) |
---|
[6c4ff37] | 916 | output << "goto " << branchStmt->get_target(); |
---|
[71f4e4f] | 917 | else { |
---|
[51587aa] | 918 | if ( branchStmt->get_computedTarget() != 0 ) { |
---|
[6c4ff37] | 919 | output << "goto *"; |
---|
[9857e8d] | 920 | branchStmt->get_computedTarget()->accept( *visitor ); |
---|
[51587aa] | 921 | } // if |
---|
| 922 | } // if |
---|
| 923 | break; |
---|
| 924 | case BranchStmt::Break: |
---|
[6c4ff37] | 925 | output << "break"; |
---|
[51587aa] | 926 | break; |
---|
| 927 | case BranchStmt::Continue: |
---|
[6c4ff37] | 928 | output << "continue"; |
---|
[51587aa] | 929 | break; |
---|
[7c2a7b6] | 930 | case BranchStmt::FallThrough: |
---|
| 931 | case BranchStmt::FallThroughDefault: |
---|
| 932 | assertf( ! genC, "fallthru should not reach code generation." ); |
---|
| 933 | output << "fallthru"; |
---|
| 934 | break; |
---|
[3778cb2] | 935 | } // switch |
---|
[7c2a7b6] | 936 | // print branch target for labelled break/continue/fallthru in debug mode |
---|
| 937 | if ( ! genC && branchStmt->get_type() != BranchStmt::Goto ) { |
---|
| 938 | if ( ! branchStmt->get_target().empty() ) { |
---|
| 939 | output << " " << branchStmt->get_target(); |
---|
| 940 | } else if ( branchStmt->get_type() == BranchStmt::FallThrough ) { |
---|
| 941 | output << " default"; |
---|
| 942 | } |
---|
| 943 | } |
---|
[2b6c1e0] | 944 | output << ";"; |
---|
[51587aa] | 945 | } |
---|
| 946 | |
---|
[9857e8d] | 947 | void CodeGenerator::postvisit( ReturnStmt * returnStmt ) { |
---|
[6c4ff37] | 948 | output << "return "; |
---|
[9857e8d] | 949 | maybeAccept( returnStmt->get_expr(), *visitor ); |
---|
[6c4ff37] | 950 | output << ";"; |
---|
[51587aa] | 951 | } |
---|
| 952 | |
---|
[9857e8d] | 953 | void CodeGenerator::postvisit( ThrowStmt * throwStmt ) { |
---|
[daf1af8] | 954 | assertf( ! genC, "Throw statements should not reach code generation." ); |
---|
| 955 | |
---|
| 956 | output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ? |
---|
| 957 | "throw" : "throwResume"); |
---|
| 958 | if (throwStmt->get_expr()) { |
---|
| 959 | output << " "; |
---|
[9857e8d] | 960 | throwStmt->get_expr()->accept( *visitor ); |
---|
[daf1af8] | 961 | } |
---|
| 962 | if (throwStmt->get_target()) { |
---|
| 963 | output << " _At "; |
---|
[9857e8d] | 964 | throwStmt->get_target()->accept( *visitor ); |
---|
[daf1af8] | 965 | } |
---|
| 966 | output << ";"; |
---|
| 967 | } |
---|
[e4ea10b7] | 968 | void CodeGenerator::postvisit( CatchStmt * stmt ) { |
---|
| 969 | assertf( ! genC, "Catch statements should not reach code generation." ); |
---|
| 970 | |
---|
| 971 | output << ((stmt->get_kind() == CatchStmt::Terminate) ? |
---|
| 972 | "catch" : "catchResume"); |
---|
| 973 | output << "( "; |
---|
| 974 | stmt->decl->accept( *visitor ); |
---|
| 975 | output << " ) "; |
---|
| 976 | |
---|
| 977 | if( stmt->cond ) { |
---|
| 978 | output << "if/when(?) ("; |
---|
| 979 | stmt->cond->accept( *visitor ); |
---|
| 980 | output << ") "; |
---|
| 981 | } |
---|
| 982 | stmt->body->accept( *visitor ); |
---|
| 983 | } |
---|
| 984 | |
---|
| 985 | void CodeGenerator::postvisit( WaitForStmt * stmt ) { |
---|
| 986 | assertf( ! genC, "Waitfor statements should not reach code generation." ); |
---|
| 987 | |
---|
| 988 | bool first = true; |
---|
| 989 | for( auto & clause : stmt->clauses ) { |
---|
| 990 | if(first) { output << "or "; first = false; } |
---|
| 991 | if( clause.condition ) { |
---|
| 992 | output << "when("; |
---|
| 993 | stmt->timeout.condition->accept( *visitor ); |
---|
| 994 | output << ") "; |
---|
| 995 | } |
---|
| 996 | output << "waitfor("; |
---|
| 997 | clause.target.function->accept( *visitor ); |
---|
| 998 | for( Expression * expr : clause.target.arguments ) { |
---|
| 999 | output << ","; |
---|
| 1000 | expr->accept( *visitor ); |
---|
| 1001 | } |
---|
| 1002 | output << ") "; |
---|
| 1003 | clause.statement->accept( *visitor ); |
---|
| 1004 | } |
---|
| 1005 | |
---|
| 1006 | if( stmt->timeout.statement ) { |
---|
| 1007 | output << "or "; |
---|
| 1008 | if( stmt->timeout.condition ) { |
---|
| 1009 | output << "when("; |
---|
| 1010 | stmt->timeout.condition->accept( *visitor ); |
---|
| 1011 | output << ") "; |
---|
| 1012 | } |
---|
| 1013 | output << "timeout("; |
---|
| 1014 | stmt->timeout.time->accept( *visitor ); |
---|
| 1015 | output << ") "; |
---|
| 1016 | stmt->timeout.statement->accept( *visitor ); |
---|
| 1017 | } |
---|
| 1018 | |
---|
| 1019 | if( stmt->orelse.statement ) { |
---|
| 1020 | output << "or "; |
---|
| 1021 | if( stmt->orelse.condition ) { |
---|
| 1022 | output << "when("; |
---|
| 1023 | stmt->orelse.condition->accept( *visitor ); |
---|
| 1024 | output << ")"; |
---|
| 1025 | } |
---|
| 1026 | output << "else "; |
---|
| 1027 | stmt->orelse.statement->accept( *visitor ); |
---|
| 1028 | } |
---|
| 1029 | } |
---|
| 1030 | |
---|
[55d6e8de] | 1031 | void CodeGenerator::postvisit( WithStmt * with ) { |
---|
| 1032 | if ( ! genC ) { |
---|
| 1033 | output << "with ( "; |
---|
| 1034 | genCommaList( with->exprs.begin(), with->exprs.end() ); |
---|
| 1035 | output << " ) "; |
---|
| 1036 | } |
---|
| 1037 | with->stmt->accept( *visitor ); |
---|
| 1038 | } |
---|
[daf1af8] | 1039 | |
---|
[9857e8d] | 1040 | void CodeGenerator::postvisit( WhileStmt * whileStmt ) { |
---|
[321a2481] | 1041 | if ( whileStmt->get_isDoWhile() ) { |
---|
[6c4ff37] | 1042 | output << "do" ; |
---|
[321a2481] | 1043 | } else { |
---|
[6c4ff37] | 1044 | output << "while (" ; |
---|
[9857e8d] | 1045 | whileStmt->get_condition()->accept( *visitor ); |
---|
[6c4ff37] | 1046 | output << ")"; |
---|
[51587aa] | 1047 | } // if |
---|
[2b6c1e0] | 1048 | output << " "; |
---|
[51587aa] | 1049 | |
---|
[2b6c1e0] | 1050 | output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() ); |
---|
[9857e8d] | 1051 | whileStmt->get_body()->accept( *visitor ); |
---|
[51587aa] | 1052 | |
---|
[cda48b6] | 1053 | output << indent; |
---|
[51587aa] | 1054 | |
---|
| 1055 | if ( whileStmt->get_isDoWhile() ) { |
---|
[6c4ff37] | 1056 | output << " while (" ; |
---|
[9857e8d] | 1057 | whileStmt->get_condition()->accept( *visitor ); |
---|
[6c4ff37] | 1058 | output << ");"; |
---|
[51587aa] | 1059 | } // if |
---|
| 1060 | } |
---|
| 1061 | |
---|
[9857e8d] | 1062 | void CodeGenerator::postvisit( ForStmt * forStmt ) { |
---|
[8e9cbb2] | 1063 | // initialization is always hoisted, so don't bother doing anything with that |
---|
[145f1fc] | 1064 | output << "for (;"; |
---|
[51587aa] | 1065 | |
---|
[321a2481] | 1066 | if ( forStmt->get_condition() != 0 ) { |
---|
[9857e8d] | 1067 | forStmt->get_condition()->accept( *visitor ); |
---|
[3778cb2] | 1068 | } // if |
---|
[6c4ff37] | 1069 | output << ";"; |
---|
[51587aa] | 1070 | |
---|
[321a2481] | 1071 | if ( forStmt->get_increment() != 0 ) { |
---|
| 1072 | // cast the top-level expression to void to reduce gcc warnings. |
---|
| 1073 | Expression * expr = new CastExpr( forStmt->get_increment() ); |
---|
[9857e8d] | 1074 | expr->accept( *visitor ); |
---|
[3778cb2] | 1075 | } // if |
---|
[2b6c1e0] | 1076 | output << ") "; |
---|
[51587aa] | 1077 | |
---|
| 1078 | if ( forStmt->get_body() != 0 ) { |
---|
[2b6c1e0] | 1079 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() ); |
---|
[9857e8d] | 1080 | forStmt->get_body()->accept( *visitor ); |
---|
[51587aa] | 1081 | } // if |
---|
| 1082 | } |
---|
| 1083 | |
---|
[9857e8d] | 1084 | void CodeGenerator::postvisit( __attribute__((unused)) NullStmt * nullStmt ) { |
---|
[cda48b6] | 1085 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); |
---|
[6c4ff37] | 1086 | output << "/* null statement */ ;"; |
---|
[51587aa] | 1087 | } |
---|
| 1088 | |
---|
[9857e8d] | 1089 | void CodeGenerator::postvisit( DeclStmt * declStmt ) { |
---|
| 1090 | declStmt->get_decl()->accept( *visitor ); |
---|
[71f4e4f] | 1091 | |
---|
[51587aa] | 1092 | if ( doSemicolon( declStmt->get_decl() ) ) { |
---|
[6c4ff37] | 1093 | output << ";"; |
---|
[51587aa] | 1094 | } // if |
---|
| 1095 | } |
---|
| 1096 | |
---|
[9857e8d] | 1097 | void CodeGenerator::postvisit( ImplicitCtorDtorStmt * stmt ) { |
---|
| 1098 | assertf( ! genC, "ImplicitCtorDtorStmts should not reach code generation." ); |
---|
| 1099 | stmt->callStmt->accept( *visitor ); |
---|
| 1100 | } |
---|
| 1101 | |
---|
[dd020c0] | 1102 | void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) { |
---|
[fb04321] | 1103 | if ( decl->get_storageClasses().any() ) { |
---|
[6e8bd43] | 1104 | decl->get_storageClasses().print( output ); |
---|
[a7c90d4] | 1105 | } // if |
---|
[dd020c0] | 1106 | } // CodeGenerator::handleStorageClass |
---|
[9facf3b] | 1107 | |
---|
| 1108 | std::string genName( DeclarationWithType * decl ) { |
---|
| 1109 | CodeGen::OperatorInfo opInfo; |
---|
| 1110 | if ( operatorLookup( decl->get_name(), opInfo ) ) { |
---|
| 1111 | return opInfo.outputName; |
---|
| 1112 | } else { |
---|
| 1113 | return decl->get_name(); |
---|
| 1114 | } // if |
---|
| 1115 | } |
---|
[51b7345] | 1116 | } // namespace CodeGen |
---|
[51587aa] | 1117 | |
---|
[50377a4] | 1118 | |
---|
| 1119 | unsigned Indenter::tabsize = 2; |
---|
| 1120 | |
---|
[e149f77] | 1121 | std::ostream & operator<<( std::ostream & out, const BaseSyntaxNode * node ) { |
---|
| 1122 | if ( node ) { |
---|
| 1123 | node->print( out ); |
---|
| 1124 | } else { |
---|
| 1125 | out << "nullptr"; |
---|
| 1126 | } |
---|
| 1127 | return out; |
---|
| 1128 | } |
---|
| 1129 | |
---|
[51587aa] | 1130 | // Local Variables: // |
---|
| 1131 | // tab-width: 4 // |
---|
| 1132 | // mode: c++ // |
---|
| 1133 | // compile-command: "make install" // |
---|
| 1134 | // End: // |
---|