| 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 : Andrew Beach
|
|---|
| 12 | // Last Modified On : Fri Aug 18 15:34:00 2017
|
|---|
| 13 | // Update Count : 488
|
|---|
| 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 = 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;
|
|---|
| 198 | if ( aggDecl->get_name() != "" )
|
|---|
| 199 | output << aggDecl->get_name();
|
|---|
| 200 |
|
|---|
| 201 | if ( aggDecl->has_body() ) {
|
|---|
| 202 | std::list< Declaration * > & memb = aggDecl->get_members();
|
|---|
| 203 | output << " {" << endl;
|
|---|
| 204 |
|
|---|
| 205 | ++indent;
|
|---|
| 206 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
|
|---|
| 207 | updateLocation( *i );
|
|---|
| 208 | output << indent;
|
|---|
| 209 | (*i)->accept( *this );
|
|---|
| 210 | output << ";" << endl;
|
|---|
| 211 | } // for
|
|---|
| 212 |
|
|---|
| 213 | --indent;
|
|---|
| 214 |
|
|---|
| 215 | output << indent << "}";
|
|---|
| 216 | } // if
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | void CodeGenerator::visit( StructDecl * structDecl ) {
|
|---|
| 220 | extension( structDecl );
|
|---|
| 221 | handleAggregate( structDecl, "struct " );
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | void CodeGenerator::visit( UnionDecl * unionDecl ) {
|
|---|
| 225 | extension( unionDecl );
|
|---|
| 226 | handleAggregate( unionDecl, "union " );
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | void CodeGenerator::visit( EnumDecl * enumDecl ) {
|
|---|
| 230 | extension( enumDecl );
|
|---|
| 231 | updateLocation( enumDecl );
|
|---|
| 232 | output << "enum ";
|
|---|
| 233 | genAttributes( enumDecl->get_attributes() );
|
|---|
| 234 |
|
|---|
| 235 | if ( enumDecl->get_name() != "" )
|
|---|
| 236 | output << enumDecl->get_name();
|
|---|
| 237 |
|
|---|
| 238 | std::list< Declaration* > &memb = enumDecl->get_members();
|
|---|
| 239 |
|
|---|
| 240 | if ( ! memb.empty() ) {
|
|---|
| 241 | output << " {" << endl;
|
|---|
| 242 |
|
|---|
| 243 | ++indent;
|
|---|
| 244 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
|
|---|
| 245 | ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
|
|---|
| 246 | assert( obj );
|
|---|
| 247 | updateLocation( obj );
|
|---|
| 248 | output << indent << mangleName( obj );
|
|---|
| 249 | if ( obj->get_init() ) {
|
|---|
| 250 | output << " = ";
|
|---|
| 251 | obj->get_init()->accept( *this );
|
|---|
| 252 | } // if
|
|---|
| 253 | output << "," << endl;
|
|---|
| 254 | } // for
|
|---|
| 255 |
|
|---|
| 256 | --indent;
|
|---|
| 257 |
|
|---|
| 258 | output << indent << "}";
|
|---|
| 259 | } // if
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | void CodeGenerator::visit( __attribute__((unused)) TraitDecl * traitDecl ) {}
|
|---|
| 263 |
|
|---|
| 264 | void CodeGenerator::visit( TypedefDecl * typeDecl ) {
|
|---|
| 265 | assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
|
|---|
| 266 | updateLocation( typeDecl );
|
|---|
| 267 | output << "typedef ";
|
|---|
| 268 | output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | void CodeGenerator::visit( TypeDecl * typeDecl ) {
|
|---|
| 272 | if ( genC ) {
|
|---|
| 273 | // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
|
|---|
| 274 | // still to be done
|
|---|
| 275 | extension( typeDecl );
|
|---|
| 276 | output << "extern unsigned long " << typeDecl->get_name();
|
|---|
| 277 | if ( typeDecl->get_base() ) {
|
|---|
| 278 | output << " = sizeof( " << genType( typeDecl->get_base(), "", pretty, genC ) << " )";
|
|---|
| 279 | } // if
|
|---|
| 280 | } else {
|
|---|
| 281 | output << typeDecl->genTypeString() << " " << typeDecl->get_name();
|
|---|
| 282 | if ( typeDecl->get_kind() != TypeDecl::Any && typeDecl->get_sized() ) {
|
|---|
| 283 | output << " | sized(" << typeDecl->get_name() << ")";
|
|---|
| 284 | }
|
|---|
| 285 | if ( ! typeDecl->get_assertions().empty() ) {
|
|---|
| 286 | output << " | { ";
|
|---|
| 287 | genCommaList( typeDecl->get_assertions().begin(), typeDecl->get_assertions().end() );
|
|---|
| 288 | output << " }";
|
|---|
| 289 | }
|
|---|
| 290 | }
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | void CodeGenerator::visit( Designation * designation ) {
|
|---|
| 294 | std::list< Expression * > designators = designation->get_designators();
|
|---|
| 295 | if ( designators.size() == 0 ) return;
|
|---|
| 296 | for ( Expression * des : designators ) {
|
|---|
| 297 | if ( dynamic_cast< NameExpr * >( des ) || dynamic_cast< VariableExpr * >( des ) ) {
|
|---|
| 298 | // if expression is a NameExpr or VariableExpr, then initializing aggregate member
|
|---|
| 299 | output << ".";
|
|---|
| 300 | des->accept( *this );
|
|---|
| 301 | } else {
|
|---|
| 302 | // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt
|
|---|
| 303 | output << "[";
|
|---|
| 304 | des->accept( *this );
|
|---|
| 305 | output << "]";
|
|---|
| 306 | } // if
|
|---|
| 307 | } // for
|
|---|
| 308 | output << " = ";
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | void CodeGenerator::visit( SingleInit * init ) {
|
|---|
| 312 | init->get_value()->accept( *this );
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | void CodeGenerator::visit( ListInit * init ) {
|
|---|
| 316 | auto initBegin = init->begin();
|
|---|
| 317 | auto initEnd = init->end();
|
|---|
| 318 | auto desigBegin = init->get_designations().begin();
|
|---|
| 319 | auto desigEnd = init->get_designations().end();
|
|---|
| 320 |
|
|---|
| 321 | output << "{ ";
|
|---|
| 322 | for ( ; initBegin != initEnd && desigBegin != desigEnd; ) {
|
|---|
| 323 | (*desigBegin)->accept( *this );
|
|---|
| 324 | (*initBegin)->accept( *this );
|
|---|
| 325 | ++initBegin, ++desigBegin;
|
|---|
| 326 | if ( initBegin != initEnd ) {
|
|---|
| 327 | output << ", ";
|
|---|
| 328 | }
|
|---|
| 329 | }
|
|---|
| 330 | output << " }";
|
|---|
| 331 | assertf( initBegin == initEnd && desigBegin == desigEnd, "Initializers and designators not the same length. %s", toString( init ).c_str() );
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | void CodeGenerator::visit( __attribute__((unused)) ConstructorInit * init ){
|
|---|
| 335 | assertf( ! genC, "ConstructorInit nodes should not reach code generation." );
|
|---|
| 336 | // pseudo-output for constructor/destructor pairs
|
|---|
| 337 | output << "<ctorinit>{" << std::endl << ++indent << "ctor: ";
|
|---|
| 338 | maybeAccept( init->get_ctor(), *this );
|
|---|
| 339 | output << ", " << std::endl << indent << "dtor: ";
|
|---|
| 340 | maybeAccept( init->get_dtor(), *this );
|
|---|
| 341 | output << std::endl << --indent << "}";
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | void CodeGenerator::visit( Constant * constant ) {
|
|---|
| 345 | output << constant->get_value() ;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | // *** Expressions
|
|---|
| 349 | void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
|
|---|
| 350 | extension( applicationExpr );
|
|---|
| 351 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
|
|---|
| 352 | OperatorInfo opInfo;
|
|---|
| 353 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
|
|---|
| 354 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
|
|---|
| 355 | switch ( opInfo.type ) {
|
|---|
| 356 | case OT_INDEX:
|
|---|
| 357 | assert( applicationExpr->get_args().size() == 2 );
|
|---|
| 358 | (*arg++)->accept( *this );
|
|---|
| 359 | output << "[";
|
|---|
| 360 | (*arg)->accept( *this );
|
|---|
| 361 | output << "]";
|
|---|
| 362 | break;
|
|---|
| 363 |
|
|---|
| 364 | case OT_CALL:
|
|---|
| 365 | // there are no intrinsic definitions of the function call operator
|
|---|
| 366 | assert( false );
|
|---|
| 367 | break;
|
|---|
| 368 |
|
|---|
| 369 | case OT_CTOR:
|
|---|
| 370 | case OT_DTOR:
|
|---|
| 371 | if ( applicationExpr->get_args().size() == 1 ) {
|
|---|
| 372 | // the expression fed into a single parameter constructor or destructor may contain side
|
|---|
| 373 | // effects, so must still output this expression
|
|---|
| 374 | output << "(";
|
|---|
| 375 | (*arg++)->accept( *this );
|
|---|
| 376 | output << ") /* " << opInfo.inputName << " */";
|
|---|
| 377 | } else if ( applicationExpr->get_args().size() == 2 ) {
|
|---|
| 378 | // intrinsic two parameter constructors are essentially bitwise assignment
|
|---|
| 379 | output << "(";
|
|---|
| 380 | (*arg++)->accept( *this );
|
|---|
| 381 | output << opInfo.symbol;
|
|---|
| 382 | (*arg)->accept( *this );
|
|---|
| 383 | output << ") /* " << opInfo.inputName << " */";
|
|---|
| 384 | } else {
|
|---|
| 385 | // no constructors with 0 or more than 2 parameters
|
|---|
| 386 | assert( false );
|
|---|
| 387 | } // if
|
|---|
| 388 | break;
|
|---|
| 389 |
|
|---|
| 390 | case OT_PREFIX:
|
|---|
| 391 | case OT_PREFIXASSIGN:
|
|---|
| 392 | assert( applicationExpr->get_args().size() == 1 );
|
|---|
| 393 | output << "(";
|
|---|
| 394 | output << opInfo.symbol;
|
|---|
| 395 | (*arg)->accept( *this );
|
|---|
| 396 | output << ")";
|
|---|
| 397 | break;
|
|---|
| 398 |
|
|---|
| 399 | case OT_POSTFIX:
|
|---|
| 400 | case OT_POSTFIXASSIGN:
|
|---|
| 401 | assert( applicationExpr->get_args().size() == 1 );
|
|---|
| 402 | (*arg)->accept( *this );
|
|---|
| 403 | output << opInfo.symbol;
|
|---|
| 404 | break;
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 | case OT_INFIX:
|
|---|
| 408 | case OT_INFIXASSIGN:
|
|---|
| 409 | assert( applicationExpr->get_args().size() == 2 );
|
|---|
| 410 | output << "(";
|
|---|
| 411 | (*arg++)->accept( *this );
|
|---|
| 412 | output << opInfo.symbol;
|
|---|
| 413 | (*arg)->accept( *this );
|
|---|
| 414 | output << ")";
|
|---|
| 415 | break;
|
|---|
| 416 |
|
|---|
| 417 | case OT_CONSTANT:
|
|---|
| 418 | case OT_LABELADDRESS:
|
|---|
| 419 | // there are no intrinsic definitions of 0/1 or label addresses as functions
|
|---|
| 420 | assert( false );
|
|---|
| 421 | } // switch
|
|---|
| 422 | } else {
|
|---|
| 423 | varExpr->accept( *this );
|
|---|
| 424 | output << "(";
|
|---|
| 425 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
|---|
| 426 | output << ")";
|
|---|
| 427 | } // if
|
|---|
| 428 | } else {
|
|---|
| 429 | applicationExpr->get_function()->accept( *this );
|
|---|
| 430 | output << "(";
|
|---|
| 431 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
|---|
| 432 | output << ")";
|
|---|
| 433 | } // if
|
|---|
| 434 | }
|
|---|
| 435 |
|
|---|
| 436 | void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
|
|---|
| 437 | extension( untypedExpr );
|
|---|
| 438 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
|
|---|
| 439 | OperatorInfo opInfo;
|
|---|
| 440 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
|
|---|
| 441 | std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
|
|---|
| 442 | switch ( opInfo.type ) {
|
|---|
| 443 | case OT_INDEX:
|
|---|
| 444 | assert( untypedExpr->get_args().size() == 2 );
|
|---|
| 445 | (*arg++)->accept( *this );
|
|---|
| 446 | output << "[";
|
|---|
| 447 | (*arg)->accept( *this );
|
|---|
| 448 | output << "]";
|
|---|
| 449 | break;
|
|---|
| 450 |
|
|---|
| 451 | case OT_CALL:
|
|---|
| 452 | assert( false );
|
|---|
| 453 |
|
|---|
| 454 | case OT_CTOR:
|
|---|
| 455 | case OT_DTOR:
|
|---|
| 456 | if ( untypedExpr->get_args().size() == 1 ) {
|
|---|
| 457 | // the expression fed into a single parameter constructor or destructor may contain side
|
|---|
| 458 | // effects, so must still output this expression
|
|---|
| 459 | output << "(";
|
|---|
| 460 | (*arg++)->accept( *this );
|
|---|
| 461 | output << ") /* " << opInfo.inputName << " */";
|
|---|
| 462 | } else if ( untypedExpr->get_args().size() == 2 ) {
|
|---|
| 463 | // intrinsic two parameter constructors are essentially bitwise assignment
|
|---|
| 464 | output << "(";
|
|---|
| 465 | (*arg++)->accept( *this );
|
|---|
| 466 | output << opInfo.symbol;
|
|---|
| 467 | (*arg)->accept( *this );
|
|---|
| 468 | output << ") /* " << opInfo.inputName << " */";
|
|---|
| 469 | } else {
|
|---|
| 470 | // no constructors with 0 or more than 2 parameters
|
|---|
| 471 | assert( false );
|
|---|
| 472 | } // if
|
|---|
| 473 | break;
|
|---|
| 474 |
|
|---|
| 475 | case OT_PREFIX:
|
|---|
| 476 | case OT_PREFIXASSIGN:
|
|---|
| 477 | case OT_LABELADDRESS:
|
|---|
| 478 | assert( untypedExpr->get_args().size() == 1 );
|
|---|
| 479 | output << "(";
|
|---|
| 480 | output << opInfo.symbol;
|
|---|
| 481 | (*arg)->accept( *this );
|
|---|
| 482 | output << ")";
|
|---|
| 483 | break;
|
|---|
| 484 |
|
|---|
| 485 | case OT_POSTFIX:
|
|---|
| 486 | case OT_POSTFIXASSIGN:
|
|---|
| 487 | assert( untypedExpr->get_args().size() == 1 );
|
|---|
| 488 | (*arg)->accept( *this );
|
|---|
| 489 | output << opInfo.symbol;
|
|---|
| 490 | break;
|
|---|
| 491 |
|
|---|
| 492 | case OT_INFIX:
|
|---|
| 493 | case OT_INFIXASSIGN:
|
|---|
| 494 | assert( untypedExpr->get_args().size() == 2 );
|
|---|
| 495 | output << "(";
|
|---|
| 496 | (*arg++)->accept( *this );
|
|---|
| 497 | output << opInfo.symbol;
|
|---|
| 498 | (*arg)->accept( *this );
|
|---|
| 499 | output << ")";
|
|---|
| 500 | break;
|
|---|
| 501 |
|
|---|
| 502 | case OT_CONSTANT:
|
|---|
| 503 | // there are no intrinsic definitions of 0 or 1 as functions
|
|---|
| 504 | assert( false );
|
|---|
| 505 | } // switch
|
|---|
| 506 | } else {
|
|---|
| 507 | if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
|
|---|
| 508 | assert( untypedExpr->get_args().size() == 2 );
|
|---|
| 509 | (*untypedExpr->get_args().begin())->accept( *this );
|
|---|
| 510 | output << " ... ";
|
|---|
| 511 | (*--untypedExpr->get_args().end())->accept( *this );
|
|---|
| 512 | } else { // builtin routines
|
|---|
| 513 | nameExpr->accept( *this );
|
|---|
| 514 | output << "(";
|
|---|
| 515 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
|
|---|
| 516 | output << ")";
|
|---|
| 517 | } // if
|
|---|
| 518 | } // if
|
|---|
| 519 | } else {
|
|---|
| 520 | untypedExpr->get_function()->accept( *this );
|
|---|
| 521 | output << "(";
|
|---|
| 522 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
|
|---|
| 523 | output << ")";
|
|---|
| 524 | } // if
|
|---|
| 525 | }
|
|---|
| 526 |
|
|---|
| 527 | void CodeGenerator::visit( RangeExpr * rangeExpr ) {
|
|---|
| 528 | rangeExpr->get_low()->accept( *this );
|
|---|
| 529 | output << " ... ";
|
|---|
| 530 | rangeExpr->get_high()->accept( *this );
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 | void CodeGenerator::visit( NameExpr * nameExpr ) {
|
|---|
| 534 | extension( nameExpr );
|
|---|
| 535 | OperatorInfo opInfo;
|
|---|
| 536 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
|
|---|
| 537 | assert( opInfo.type == OT_CONSTANT );
|
|---|
| 538 | output << opInfo.symbol;
|
|---|
| 539 | } else {
|
|---|
| 540 | output << nameExpr->get_name();
|
|---|
| 541 | } // if
|
|---|
| 542 | }
|
|---|
| 543 |
|
|---|
| 544 | void CodeGenerator::visit( AddressExpr * addressExpr ) {
|
|---|
| 545 | extension( addressExpr );
|
|---|
| 546 | output << "(&";
|
|---|
| 547 | // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
|
|---|
| 548 | if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
|
|---|
| 549 | output << mangleName( variableExpr->get_var() );
|
|---|
| 550 | } else {
|
|---|
| 551 | addressExpr->get_arg()->accept( *this );
|
|---|
| 552 | } // if
|
|---|
| 553 | output << ")";
|
|---|
| 554 | }
|
|---|
| 555 |
|
|---|
| 556 | void CodeGenerator::visit( CastExpr * castExpr ) {
|
|---|
| 557 | extension( castExpr );
|
|---|
| 558 | output << "(";
|
|---|
| 559 | if ( castExpr->get_result()->isVoid() ) {
|
|---|
| 560 | output << "(void)" ;
|
|---|
| 561 | } else {
|
|---|
| 562 | // at least one result type of cast.
|
|---|
| 563 | // Note: previously, lvalue casts were skipped. Since it's now impossible for the user to write
|
|---|
| 564 | // an lvalue cast, this has been taken out.
|
|---|
| 565 | output << "(";
|
|---|
| 566 | output << genType( castExpr->get_result(), "", pretty, genC );
|
|---|
| 567 | output << ")";
|
|---|
| 568 | } // if
|
|---|
| 569 | castExpr->get_arg()->accept( *this );
|
|---|
| 570 | output << ")";
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | void CodeGenerator::visit( VirtualCastExpr * castExpr ) {
|
|---|
| 574 | assertf( ! genC, "VirtualCastExpr should not reach code generation." );
|
|---|
| 575 | extension( castExpr );
|
|---|
| 576 | output << "(virtual ";
|
|---|
| 577 | castExpr->get_arg()->accept( *this );
|
|---|
| 578 | output << ")";
|
|---|
| 579 | }
|
|---|
| 580 |
|
|---|
| 581 | void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
|
|---|
| 582 | assertf( ! genC, "UntypedMemberExpr should not reach code generation." );
|
|---|
| 583 | extension( memberExpr );
|
|---|
| 584 | memberExpr->get_aggregate()->accept( *this );
|
|---|
| 585 | output << ".";
|
|---|
| 586 | memberExpr->get_member()->accept( *this );
|
|---|
| 587 | }
|
|---|
| 588 |
|
|---|
| 589 | void CodeGenerator::visit( MemberExpr * memberExpr ) {
|
|---|
| 590 | extension( memberExpr );
|
|---|
| 591 | memberExpr->get_aggregate()->accept( *this );
|
|---|
| 592 | output << "." << mangleName( memberExpr->get_member() );
|
|---|
| 593 | }
|
|---|
| 594 |
|
|---|
| 595 | void CodeGenerator::visit( VariableExpr * variableExpr ) {
|
|---|
| 596 | extension( variableExpr );
|
|---|
| 597 | OperatorInfo opInfo;
|
|---|
| 598 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
|
|---|
| 599 | output << opInfo.symbol;
|
|---|
| 600 | } else {
|
|---|
| 601 | output << mangleName( variableExpr->get_var() );
|
|---|
| 602 | } // if
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | void CodeGenerator::visit( ConstantExpr * constantExpr ) {
|
|---|
| 606 | assert( constantExpr->get_constant() );
|
|---|
| 607 | extension( constantExpr );
|
|---|
| 608 | constantExpr->get_constant()->accept( *this );
|
|---|
| 609 | }
|
|---|
| 610 |
|
|---|
| 611 | void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
|
|---|
| 612 | extension( sizeofExpr );
|
|---|
| 613 | output << "sizeof(";
|
|---|
| 614 | if ( sizeofExpr->get_isType() ) {
|
|---|
| 615 | output << genType( sizeofExpr->get_type(), "", pretty, genC );
|
|---|
| 616 | } else {
|
|---|
| 617 | sizeofExpr->get_expr()->accept( *this );
|
|---|
| 618 | } // if
|
|---|
| 619 | output << ")";
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
|
|---|
| 623 | // use GCC extension to avoid bumping std to C11
|
|---|
| 624 | extension( alignofExpr );
|
|---|
| 625 | output << "__alignof__(";
|
|---|
| 626 | if ( alignofExpr->get_isType() ) {
|
|---|
| 627 | output << genType( alignofExpr->get_type(), "", pretty, genC );
|
|---|
| 628 | } else {
|
|---|
| 629 | alignofExpr->get_expr()->accept( *this );
|
|---|
| 630 | } // if
|
|---|
| 631 | output << ")";
|
|---|
| 632 | }
|
|---|
| 633 |
|
|---|
| 634 | void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
|
|---|
| 635 | assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." );
|
|---|
| 636 | output << "offsetof(";
|
|---|
| 637 | output << genType( offsetofExpr->get_type(), "", pretty, genC );
|
|---|
| 638 | output << ", " << offsetofExpr->get_member();
|
|---|
| 639 | output << ")";
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 | void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
|
|---|
| 643 | // use GCC builtin
|
|---|
| 644 | output << "__builtin_offsetof(";
|
|---|
| 645 | output << genType( offsetofExpr->get_type(), "", pretty, genC );
|
|---|
| 646 | output << ", " << mangleName( offsetofExpr->get_member() );
|
|---|
| 647 | output << ")";
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
|
|---|
| 651 | assertf( ! genC, "OffsetPackExpr should not reach code generation." );
|
|---|
| 652 | output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")";
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 | void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
|
|---|
| 656 | extension( logicalExpr );
|
|---|
| 657 | output << "(";
|
|---|
| 658 | logicalExpr->get_arg1()->accept( *this );
|
|---|
| 659 | if ( logicalExpr->get_isAnd() ) {
|
|---|
| 660 | output << " && ";
|
|---|
| 661 | } else {
|
|---|
| 662 | output << " || ";
|
|---|
| 663 | } // if
|
|---|
| 664 | logicalExpr->get_arg2()->accept( *this );
|
|---|
| 665 | output << ")";
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
|
|---|
| 669 | extension( conditionalExpr );
|
|---|
| 670 | output << "(";
|
|---|
| 671 | conditionalExpr->get_arg1()->accept( *this );
|
|---|
| 672 | output << " ? ";
|
|---|
| 673 | conditionalExpr->get_arg2()->accept( *this );
|
|---|
| 674 | output << " : ";
|
|---|
| 675 | conditionalExpr->get_arg3()->accept( *this );
|
|---|
| 676 | output << ")";
|
|---|
| 677 | }
|
|---|
| 678 |
|
|---|
| 679 | void CodeGenerator::visit( CommaExpr * commaExpr ) {
|
|---|
| 680 | extension( commaExpr );
|
|---|
| 681 | output << "(";
|
|---|
| 682 | if ( genC ) {
|
|---|
| 683 | // arg1 of a CommaExpr is never used, so it can be safely cast to void to reduce gcc warnings.
|
|---|
| 684 | commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) );
|
|---|
| 685 | }
|
|---|
| 686 | commaExpr->get_arg1()->accept( *this );
|
|---|
| 687 | output << " , ";
|
|---|
| 688 | commaExpr->get_arg2()->accept( *this );
|
|---|
| 689 | output << ")";
|
|---|
| 690 | }
|
|---|
| 691 |
|
|---|
| 692 | void CodeGenerator::visit( TupleAssignExpr * tupleExpr ) {
|
|---|
| 693 | assertf( ! genC, "TupleAssignExpr should not reach code generation." );
|
|---|
| 694 | tupleExpr->stmtExpr->accept( *this );
|
|---|
| 695 | }
|
|---|
| 696 |
|
|---|
| 697 | void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) {
|
|---|
| 698 | assertf( ! genC, "UntypedTupleExpr 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( TupleExpr * tupleExpr ) {
|
|---|
| 706 | assertf( ! genC, "TupleExpr should not reach code generation." );
|
|---|
| 707 | extension( tupleExpr );
|
|---|
| 708 | output << "[";
|
|---|
| 709 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
|
|---|
| 710 | output << "]";
|
|---|
| 711 | }
|
|---|
| 712 |
|
|---|
| 713 | void CodeGenerator::visit( TupleIndexExpr * tupleExpr ) {
|
|---|
| 714 | assertf( ! genC, "TupleIndexExpr should not reach code generation." );
|
|---|
| 715 | extension( tupleExpr );
|
|---|
| 716 | tupleExpr->get_tuple()->accept( *this );
|
|---|
| 717 | output << "." << tupleExpr->get_index();
|
|---|
| 718 | }
|
|---|
| 719 |
|
|---|
| 720 | void CodeGenerator::visit( TypeExpr * typeExpr ) {
|
|---|
| 721 | // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl;
|
|---|
| 722 | // assertf( ! genC, "TypeExpr should not reach code generation." );
|
|---|
| 723 | if ( ! genC ) {
|
|---|
| 724 | output<< genType( typeExpr->get_type(), "", pretty, genC );
|
|---|
| 725 | }
|
|---|
| 726 | }
|
|---|
| 727 |
|
|---|
| 728 | void CodeGenerator::visit( AsmExpr * asmExpr ) {
|
|---|
| 729 | if ( asmExpr->get_inout() ) {
|
|---|
| 730 | output << "[ ";
|
|---|
| 731 | asmExpr->get_inout()->accept( *this );
|
|---|
| 732 | output << " ] ";
|
|---|
| 733 | } // if
|
|---|
| 734 | asmExpr->get_constraint()->accept( *this );
|
|---|
| 735 | output << " ( ";
|
|---|
| 736 | asmExpr->get_operand()->accept( *this );
|
|---|
| 737 | output << " )";
|
|---|
| 738 | }
|
|---|
| 739 |
|
|---|
| 740 | void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) {
|
|---|
| 741 | assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
|
|---|
| 742 | output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")";
|
|---|
| 743 | compLitExpr->get_initializer()->accept( *this );
|
|---|
| 744 | }
|
|---|
| 745 |
|
|---|
| 746 | void CodeGenerator::visit( UniqueExpr * unqExpr ) {
|
|---|
| 747 | assertf( ! genC, "Unique expressions should not reach code generation." );
|
|---|
| 748 | output << "unq<" << unqExpr->get_id() << ">{ ";
|
|---|
| 749 | unqExpr->get_expr()->accept( *this );
|
|---|
| 750 | output << " }";
|
|---|
| 751 | }
|
|---|
| 752 |
|
|---|
| 753 | void CodeGenerator::visit( StmtExpr * stmtExpr ) {
|
|---|
| 754 | std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
|
|---|
| 755 | updateLocation( stmtExpr );
|
|---|
| 756 | output << "({" << std::endl;
|
|---|
| 757 | ++indent;
|
|---|
| 758 | unsigned int numStmts = stmts.size();
|
|---|
| 759 | unsigned int i = 0;
|
|---|
| 760 | for ( Statement * stmt : stmts ) {
|
|---|
| 761 | updateLocation( stmt );
|
|---|
| 762 | output << printLabels( stmt->get_labels() );
|
|---|
| 763 | if ( i+1 == numStmts ) {
|
|---|
| 764 | // last statement in a statement expression needs to be handled specially -
|
|---|
| 765 | // cannot cast to void, otherwise the expression statement has no value
|
|---|
| 766 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
|
|---|
| 767 | exprStmt->get_expr()->accept( *this );
|
|---|
| 768 | output << ";" << endl;
|
|---|
| 769 | ++i;
|
|---|
| 770 | break;
|
|---|
| 771 | }
|
|---|
| 772 | }
|
|---|
| 773 | stmt->accept( *this );
|
|---|
| 774 | output << endl;
|
|---|
| 775 | if ( wantSpacing( stmt ) ) {
|
|---|
| 776 | output << endl;
|
|---|
| 777 | } // if
|
|---|
| 778 | ++i;
|
|---|
| 779 | }
|
|---|
| 780 | --indent;
|
|---|
| 781 | output << indent << "})";
|
|---|
| 782 | }
|
|---|
| 783 |
|
|---|
| 784 | // *** Statements
|
|---|
| 785 | void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
|
|---|
| 786 | std::list<Statement*> ks = compoundStmt->get_kids();
|
|---|
| 787 | output << "{" << endl;
|
|---|
| 788 |
|
|---|
| 789 | ++indent;
|
|---|
| 790 |
|
|---|
| 791 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
|
|---|
| 792 | output << indent << printLabels( (*i)->get_labels() );
|
|---|
| 793 | (*i)->accept( *this );
|
|---|
| 794 |
|
|---|
| 795 | output << endl;
|
|---|
| 796 | if ( wantSpacing( *i ) ) {
|
|---|
| 797 | output << endl;
|
|---|
| 798 | } // if
|
|---|
| 799 | } // for
|
|---|
| 800 | --indent;
|
|---|
| 801 |
|
|---|
| 802 | output << indent << "}";
|
|---|
| 803 | }
|
|---|
| 804 |
|
|---|
| 805 | void CodeGenerator::visit( ExprStmt * exprStmt ) {
|
|---|
| 806 | assert( exprStmt );
|
|---|
| 807 | if ( genC ) {
|
|---|
| 808 | // cast the top-level expression to void to reduce gcc warnings.
|
|---|
| 809 | exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) );
|
|---|
| 810 | }
|
|---|
| 811 | exprStmt->get_expr()->accept( *this );
|
|---|
| 812 | output << ";";
|
|---|
| 813 | }
|
|---|
| 814 |
|
|---|
| 815 | void CodeGenerator::visit( AsmStmt * asmStmt ) {
|
|---|
| 816 | output << "asm ";
|
|---|
| 817 | if ( asmStmt->get_voltile() ) output << "volatile ";
|
|---|
| 818 | if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
|
|---|
| 819 | output << "( ";
|
|---|
| 820 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
|
|---|
| 821 | output << " : ";
|
|---|
| 822 | genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
|
|---|
| 823 | output << " : ";
|
|---|
| 824 | genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
|
|---|
| 825 | output << " : ";
|
|---|
| 826 | genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
|
|---|
| 827 | if ( ! asmStmt->get_gotolabels().empty() ) {
|
|---|
| 828 | output << " : ";
|
|---|
| 829 | for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
|
|---|
| 830 | output << *begin++;
|
|---|
| 831 | if ( begin == asmStmt->get_gotolabels().end() ) break;
|
|---|
| 832 | output << ", ";
|
|---|
| 833 | } // for
|
|---|
| 834 | } // if
|
|---|
| 835 | output << " );" ;
|
|---|
| 836 | }
|
|---|
| 837 |
|
|---|
| 838 | void CodeGenerator::visit( AsmDecl * asmDecl ) {
|
|---|
| 839 | output << "asm ";
|
|---|
| 840 | AsmStmt * asmStmt = asmDecl->get_stmt();
|
|---|
| 841 | output << "( ";
|
|---|
| 842 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
|
|---|
| 843 | output << " )" ;
|
|---|
| 844 | }
|
|---|
| 845 |
|
|---|
| 846 | void CodeGenerator::visit( IfStmt * ifStmt ) {
|
|---|
| 847 | updateLocation( ifStmt );
|
|---|
| 848 | output << "if ( ";
|
|---|
| 849 | ifStmt->get_condition()->accept( *this );
|
|---|
| 850 | output << " ) ";
|
|---|
| 851 |
|
|---|
| 852 | ifStmt->get_thenPart()->accept( *this );
|
|---|
| 853 |
|
|---|
| 854 | if ( ifStmt->get_elsePart() != 0) {
|
|---|
| 855 | output << " else ";
|
|---|
| 856 | ifStmt->get_elsePart()->accept( *this );
|
|---|
| 857 | } // if
|
|---|
| 858 | }
|
|---|
| 859 |
|
|---|
| 860 | void CodeGenerator::visit( SwitchStmt * switchStmt ) {
|
|---|
| 861 | updateLocation( switchStmt );
|
|---|
| 862 | output << "switch ( " ;
|
|---|
| 863 | switchStmt->get_condition()->accept( *this );
|
|---|
| 864 | output << " ) ";
|
|---|
| 865 |
|
|---|
| 866 | output << "{" << std::endl;
|
|---|
| 867 | ++indent;
|
|---|
| 868 | acceptAll( switchStmt->get_statements(), *this );
|
|---|
| 869 | --indent;
|
|---|
| 870 | output << indent << "}";
|
|---|
| 871 | }
|
|---|
| 872 |
|
|---|
| 873 | void CodeGenerator::visit( CaseStmt * caseStmt ) {
|
|---|
| 874 | updateLocation( caseStmt );
|
|---|
| 875 | if ( caseStmt->isDefault()) {
|
|---|
| 876 | output << "default";
|
|---|
| 877 | } else {
|
|---|
| 878 | output << "case ";
|
|---|
| 879 | caseStmt->get_condition()->accept( *this );
|
|---|
| 880 | } // if
|
|---|
| 881 | output << ":\n";
|
|---|
| 882 |
|
|---|
| 883 | std::list<Statement *> sts = caseStmt->get_statements();
|
|---|
| 884 |
|
|---|
| 885 | ++indent;
|
|---|
| 886 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
|
|---|
| 887 | output << indent << printLabels( (*i)->get_labels() ) ;
|
|---|
| 888 | (*i)->accept( *this );
|
|---|
| 889 | output << endl;
|
|---|
| 890 | } // for
|
|---|
| 891 | --indent;
|
|---|
| 892 | }
|
|---|
| 893 |
|
|---|
| 894 | void CodeGenerator::visit( BranchStmt * branchStmt ) {
|
|---|
| 895 | switch ( branchStmt->get_type()) {
|
|---|
| 896 | case BranchStmt::Goto:
|
|---|
| 897 | if ( ! branchStmt->get_target().empty() )
|
|---|
| 898 | output << "goto " << branchStmt->get_target();
|
|---|
| 899 | else {
|
|---|
| 900 | if ( branchStmt->get_computedTarget() != 0 ) {
|
|---|
| 901 | output << "goto *";
|
|---|
| 902 | branchStmt->get_computedTarget()->accept( *this );
|
|---|
| 903 | } // if
|
|---|
| 904 | } // if
|
|---|
| 905 | break;
|
|---|
| 906 | case BranchStmt::Break:
|
|---|
| 907 | output << "break";
|
|---|
| 908 | break;
|
|---|
| 909 | case BranchStmt::Continue:
|
|---|
| 910 | output << "continue";
|
|---|
| 911 | break;
|
|---|
| 912 | } // switch
|
|---|
| 913 | output << ";";
|
|---|
| 914 | }
|
|---|
| 915 |
|
|---|
| 916 | void CodeGenerator::visit( ReturnStmt * returnStmt ) {
|
|---|
| 917 | output << "return ";
|
|---|
| 918 | maybeAccept( returnStmt->get_expr(), *this );
|
|---|
| 919 | output << ";";
|
|---|
| 920 | }
|
|---|
| 921 |
|
|---|
| 922 | void CodeGenerator::visit( ThrowStmt * throwStmt ) {
|
|---|
| 923 | assertf( ! genC, "Throw statements should not reach code generation." );
|
|---|
| 924 |
|
|---|
| 925 | output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ?
|
|---|
| 926 | "throw" : "throwResume");
|
|---|
| 927 | if (throwStmt->get_expr()) {
|
|---|
| 928 | output << " ";
|
|---|
| 929 | throwStmt->get_expr()->accept( *this );
|
|---|
| 930 | }
|
|---|
| 931 | if (throwStmt->get_target()) {
|
|---|
| 932 | output << " _At ";
|
|---|
| 933 | throwStmt->get_target()->accept( *this );
|
|---|
| 934 | }
|
|---|
| 935 | output << ";";
|
|---|
| 936 | }
|
|---|
| 937 |
|
|---|
| 938 | void CodeGenerator::visit( WhileStmt * whileStmt ) {
|
|---|
| 939 | if ( whileStmt->get_isDoWhile() ) {
|
|---|
| 940 | output << "do" ;
|
|---|
| 941 | } else {
|
|---|
| 942 | output << "while (" ;
|
|---|
| 943 | whileStmt->get_condition()->accept( *this );
|
|---|
| 944 | output << ")";
|
|---|
| 945 | } // if
|
|---|
| 946 | output << " ";
|
|---|
| 947 |
|
|---|
| 948 | output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
|
|---|
| 949 | whileStmt->get_body()->accept( *this );
|
|---|
| 950 |
|
|---|
| 951 | output << indent;
|
|---|
| 952 |
|
|---|
| 953 | if ( whileStmt->get_isDoWhile() ) {
|
|---|
| 954 | output << " while (" ;
|
|---|
| 955 | whileStmt->get_condition()->accept( *this );
|
|---|
| 956 | output << ");";
|
|---|
| 957 | } // if
|
|---|
| 958 | }
|
|---|
| 959 |
|
|---|
| 960 | void CodeGenerator::visit( ForStmt * forStmt ) {
|
|---|
| 961 | // initialization is always hoisted, so don't bother doing anything with that
|
|---|
| 962 | output << "for (;";
|
|---|
| 963 |
|
|---|
| 964 | if ( forStmt->get_condition() != 0 ) {
|
|---|
| 965 | forStmt->get_condition()->accept( *this );
|
|---|
| 966 | } // if
|
|---|
| 967 | output << ";";
|
|---|
| 968 |
|
|---|
| 969 | if ( forStmt->get_increment() != 0 ) {
|
|---|
| 970 | // cast the top-level expression to void to reduce gcc warnings.
|
|---|
| 971 | Expression * expr = new CastExpr( forStmt->get_increment() );
|
|---|
| 972 | expr->accept( *this );
|
|---|
| 973 | } // if
|
|---|
| 974 | output << ") ";
|
|---|
| 975 |
|
|---|
| 976 | if ( forStmt->get_body() != 0 ) {
|
|---|
| 977 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
|
|---|
| 978 | forStmt->get_body()->accept( *this );
|
|---|
| 979 | } // if
|
|---|
| 980 | }
|
|---|
| 981 |
|
|---|
| 982 | void CodeGenerator::visit( __attribute__((unused)) NullStmt * nullStmt ) {
|
|---|
| 983 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
|
|---|
| 984 | output << "/* null statement */ ;";
|
|---|
| 985 | }
|
|---|
| 986 |
|
|---|
| 987 | void CodeGenerator::visit( DeclStmt * declStmt ) {
|
|---|
| 988 | declStmt->get_decl()->accept( *this );
|
|---|
| 989 |
|
|---|
| 990 | if ( doSemicolon( declStmt->get_decl() ) ) {
|
|---|
| 991 | output << ";";
|
|---|
| 992 | } // if
|
|---|
| 993 | }
|
|---|
| 994 |
|
|---|
| 995 | void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) {
|
|---|
| 996 | if ( decl->get_storageClasses().any() ) {
|
|---|
| 997 | decl->get_storageClasses().print( output );
|
|---|
| 998 | } // if
|
|---|
| 999 | } // CodeGenerator::handleStorageClass
|
|---|
| 1000 |
|
|---|
| 1001 | std::string genName( DeclarationWithType * decl ) {
|
|---|
| 1002 | CodeGen::OperatorInfo opInfo;
|
|---|
| 1003 | if ( operatorLookup( decl->get_name(), opInfo ) ) {
|
|---|
| 1004 | return opInfo.outputName;
|
|---|
| 1005 | } else {
|
|---|
| 1006 | return decl->get_name();
|
|---|
| 1007 | } // if
|
|---|
| 1008 | }
|
|---|
| 1009 | } // namespace CodeGen
|
|---|
| 1010 |
|
|---|
| 1011 | // Local Variables: //
|
|---|
| 1012 | // tab-width: 4 //
|
|---|
| 1013 | // mode: c++ //
|
|---|
| 1014 | // compile-command: "make install" //
|
|---|
| 1015 | // End: //
|
|---|