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