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