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