[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 << "{ ";
|
---|
[4d2434a] | 326 | if ( init->begin() == init->end() ) {
|
---|
[8e9cbb2] | 327 | // illegal to leave initializer list empty for scalar initializers, but always legal to have 0
|
---|
[5b40f30] | 328 | output << "0";
|
---|
| 329 | } else {
|
---|
[4d2434a] | 330 | genCommaList( init->begin(), init->end() );
|
---|
[8688ce1] | 331 | } // if
|
---|
[6c4ff37] | 332 | output << " }";
|
---|
[51587aa] | 333 | }
|
---|
| 334 |
|
---|
[fc638d2] | 335 | void CodeGenerator::visit( ConstructorInit * init ){
|
---|
[e39241b] | 336 | assertf( ! genC, "ConstructorInit nodes should not reach code generation." );
|
---|
| 337 | // xxx - generate something reasonable for constructor/destructor pairs
|
---|
| 338 | output << "<ctorinit>";
|
---|
[fc638d2] | 339 | }
|
---|
| 340 |
|
---|
[8688ce1] | 341 | void CodeGenerator::visit( Constant * constant ) {
|
---|
[6c4ff37] | 342 | output << constant->get_value() ;
|
---|
[51587aa] | 343 | }
|
---|
| 344 |
|
---|
[4810867] | 345 | // *** Expressions
|
---|
[8688ce1] | 346 | void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
|
---|
[e04ef3a] | 347 | extension( applicationExpr );
|
---|
[8688ce1] | 348 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
|
---|
[51587aa] | 349 | OperatorInfo opInfo;
|
---|
| 350 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
|
---|
| 351 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
|
---|
| 352 | switch ( opInfo.type ) {
|
---|
| 353 | case OT_PREFIXASSIGN:
|
---|
| 354 | case OT_POSTFIXASSIGN:
|
---|
| 355 | case OT_INFIXASSIGN:
|
---|
[356189a] | 356 | case OT_CTOR:
|
---|
[c2ce2350] | 357 | case OT_DTOR:
|
---|
[51587aa] | 358 | {
|
---|
| 359 | assert( arg != applicationExpr->get_args().end() );
|
---|
[8688ce1] | 360 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
|
---|
[356189a] | 361 | // remove & from first assignment/ctor argument
|
---|
[51587aa] | 362 | *arg = addrExpr->get_arg();
|
---|
| 363 | } else {
|
---|
[356189a] | 364 | // no address-of operator, so must be a pointer - add dereference
|
---|
[066d77a] | 365 | // NOTE: if the assertion starts to trigger, check that the application expr isn't being shared.
|
---|
| 366 | // Since its arguments are modified here, this assertion most commonly triggers when the application
|
---|
| 367 | // is visited multiple times.
|
---|
[8688ce1] | 368 | UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
[51587aa] | 369 | newExpr->get_args().push_back( *arg );
|
---|
[906e24d] | 370 | Type * type = InitTweak::getPointerBase( (*arg)->get_result() );
|
---|
[066d77a] | 371 | assertf( type, "First argument to a derefence must be a pointer. Ensure that expressions are not being shared." );
|
---|
[906e24d] | 372 | newExpr->set_result( type->clone() );
|
---|
[51587aa] | 373 | *arg = newExpr;
|
---|
| 374 | } // if
|
---|
| 375 | break;
|
---|
| 376 | }
|
---|
[71f4e4f] | 377 |
|
---|
[51587aa] | 378 | default:
|
---|
| 379 | // do nothing
|
---|
| 380 | ;
|
---|
[3778cb2] | 381 | } // switch
|
---|
[71f4e4f] | 382 |
|
---|
[51587aa] | 383 | switch ( opInfo.type ) {
|
---|
| 384 | case OT_INDEX:
|
---|
| 385 | assert( applicationExpr->get_args().size() == 2 );
|
---|
| 386 | (*arg++)->accept( *this );
|
---|
[6c4ff37] | 387 | output << "[";
|
---|
[51587aa] | 388 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 389 | output << "]";
|
---|
[51587aa] | 390 | break;
|
---|
[71f4e4f] | 391 |
|
---|
[51587aa] | 392 | case OT_CALL:
|
---|
[356189a] | 393 | // there are no intrinsic definitions of the function call operator
|
---|
[51587aa] | 394 | assert( false );
|
---|
| 395 | break;
|
---|
[71f4e4f] | 396 |
|
---|
[f1e012b] | 397 | case OT_CTOR:
|
---|
[c2ce2350] | 398 | case OT_DTOR:
|
---|
[356189a] | 399 | if ( applicationExpr->get_args().size() == 1 ) {
|
---|
[8e9cbb2] | 400 | // the expression fed into a single parameter constructor or destructor may contain side
|
---|
| 401 | // effects, so must still output this expression
|
---|
[64071c2] | 402 | output << "(";
|
---|
[356189a] | 403 | (*arg++)->accept( *this );
|
---|
[64071c2] | 404 | output << ") /* " << opInfo.inputName << " */";
|
---|
[356189a] | 405 | } else if ( applicationExpr->get_args().size() == 2 ) {
|
---|
[c2ce2350] | 406 | // intrinsic two parameter constructors are essentially bitwise assignment
|
---|
[356189a] | 407 | output << "(";
|
---|
| 408 | (*arg++)->accept( *this );
|
---|
| 409 | output << opInfo.symbol;
|
---|
| 410 | (*arg)->accept( *this );
|
---|
[c2ce2350] | 411 | output << ") /* " << opInfo.inputName << " */";
|
---|
[356189a] | 412 | } else {
|
---|
[c2ce2350] | 413 | // no constructors with 0 or more than 2 parameters
|
---|
[356189a] | 414 | assert( false );
|
---|
[8688ce1] | 415 | } // if
|
---|
[356189a] | 416 | break;
|
---|
[f1e012b] | 417 |
|
---|
[51587aa] | 418 | case OT_PREFIX:
|
---|
| 419 | case OT_PREFIXASSIGN:
|
---|
| 420 | assert( applicationExpr->get_args().size() == 1 );
|
---|
[6c4ff37] | 421 | output << "(";
|
---|
| 422 | output << opInfo.symbol;
|
---|
[51587aa] | 423 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 424 | output << ")";
|
---|
[51587aa] | 425 | break;
|
---|
[71f4e4f] | 426 |
|
---|
[51587aa] | 427 | case OT_POSTFIX:
|
---|
| 428 | case OT_POSTFIXASSIGN:
|
---|
| 429 | assert( applicationExpr->get_args().size() == 1 );
|
---|
| 430 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 431 | output << opInfo.symbol;
|
---|
[51587aa] | 432 | break;
|
---|
| 433 |
|
---|
[f1e012b] | 434 |
|
---|
[51587aa] | 435 | case OT_INFIX:
|
---|
| 436 | case OT_INFIXASSIGN:
|
---|
| 437 | assert( applicationExpr->get_args().size() == 2 );
|
---|
[6c4ff37] | 438 | output << "(";
|
---|
[51587aa] | 439 | (*arg++)->accept( *this );
|
---|
[6c4ff37] | 440 | output << opInfo.symbol;
|
---|
[51587aa] | 441 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 442 | output << ")";
|
---|
[51587aa] | 443 | break;
|
---|
[71f4e4f] | 444 |
|
---|
[51587aa] | 445 | case OT_CONSTANT:
|
---|
[721f17a] | 446 | case OT_LABELADDRESS:
|
---|
| 447 | // there are no intrinsic definitions of 0/1 or label addresses as functions
|
---|
[51587aa] | 448 | assert( false );
|
---|
[3778cb2] | 449 | } // switch
|
---|
[17cd4eb] | 450 | } else {
|
---|
[51587aa] | 451 | varExpr->accept( *this );
|
---|
[6c4ff37] | 452 | output << "(";
|
---|
[51587aa] | 453 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
---|
[6c4ff37] | 454 | output << ")";
|
---|
[17cd4eb] | 455 | } // if
|
---|
[51587aa] | 456 | } else {
|
---|
| 457 | applicationExpr->get_function()->accept( *this );
|
---|
[6c4ff37] | 458 | output << "(";
|
---|
[51587aa] | 459 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
---|
[6c4ff37] | 460 | output << ")";
|
---|
[51587aa] | 461 | } // if
|
---|
| 462 | }
|
---|
[71f4e4f] | 463 |
|
---|
[8688ce1] | 464 | void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
|
---|
[e04ef3a] | 465 | extension( untypedExpr );
|
---|
[8688ce1] | 466 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
|
---|
[51587aa] | 467 | OperatorInfo opInfo;
|
---|
| 468 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
|
---|
| 469 | std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
|
---|
| 470 | switch ( opInfo.type ) {
|
---|
| 471 | case OT_INDEX:
|
---|
| 472 | assert( untypedExpr->get_args().size() == 2 );
|
---|
| 473 | (*arg++)->accept( *this );
|
---|
[6c4ff37] | 474 | output << "[";
|
---|
[51587aa] | 475 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 476 | output << "]";
|
---|
[51587aa] | 477 | break;
|
---|
[71f4e4f] | 478 |
|
---|
[51587aa] | 479 | case OT_CALL:
|
---|
[f1e012b] | 480 | assert( false );
|
---|
| 481 |
|
---|
[c2ce2350] | 482 | case OT_CTOR:
|
---|
| 483 | case OT_DTOR:
|
---|
| 484 | if ( untypedExpr->get_args().size() == 1 ) {
|
---|
[8e9cbb2] | 485 | // the expression fed into a single parameter constructor or destructor may contain side
|
---|
| 486 | // effects, so must still output this expression
|
---|
[64071c2] | 487 | output << "(";
|
---|
[c2ce2350] | 488 | (*arg++)->accept( *this );
|
---|
[64071c2] | 489 | output << ") /* " << opInfo.inputName << " */";
|
---|
[c2ce2350] | 490 | } else if ( untypedExpr->get_args().size() == 2 ) {
|
---|
| 491 | // intrinsic two parameter constructors are essentially bitwise assignment
|
---|
| 492 | output << "(";
|
---|
| 493 | (*arg++)->accept( *this );
|
---|
| 494 | output << opInfo.symbol;
|
---|
| 495 | (*arg)->accept( *this );
|
---|
| 496 | output << ") /* " << opInfo.inputName << " */";
|
---|
| 497 | } else {
|
---|
| 498 | // no constructors with 0 or more than 2 parameters
|
---|
| 499 | assert( false );
|
---|
[3778cb2] | 500 | } // if
|
---|
[51587aa] | 501 | break;
|
---|
[71f4e4f] | 502 |
|
---|
[51587aa] | 503 | case OT_PREFIX:
|
---|
| 504 | case OT_PREFIXASSIGN:
|
---|
[de62360d] | 505 | case OT_LABELADDRESS:
|
---|
[51587aa] | 506 | assert( untypedExpr->get_args().size() == 1 );
|
---|
[6c4ff37] | 507 | output << "(";
|
---|
| 508 | output << opInfo.symbol;
|
---|
[51587aa] | 509 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 510 | output << ")";
|
---|
[51587aa] | 511 | break;
|
---|
[71f4e4f] | 512 |
|
---|
[51587aa] | 513 | case OT_POSTFIX:
|
---|
| 514 | case OT_POSTFIXASSIGN:
|
---|
| 515 | assert( untypedExpr->get_args().size() == 1 );
|
---|
| 516 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 517 | output << opInfo.symbol;
|
---|
[51587aa] | 518 | break;
|
---|
[71f4e4f] | 519 |
|
---|
[51587aa] | 520 | case OT_INFIX:
|
---|
| 521 | case OT_INFIXASSIGN:
|
---|
| 522 | assert( untypedExpr->get_args().size() == 2 );
|
---|
[6c4ff37] | 523 | output << "(";
|
---|
[51587aa] | 524 | (*arg++)->accept( *this );
|
---|
[6c4ff37] | 525 | output << opInfo.symbol;
|
---|
[51587aa] | 526 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 527 | output << ")";
|
---|
[51587aa] | 528 | break;
|
---|
[71f4e4f] | 529 |
|
---|
[51587aa] | 530 | case OT_CONSTANT:
|
---|
| 531 | // there are no intrinsic definitions of 0 or 1 as functions
|
---|
| 532 | assert( false );
|
---|
[3778cb2] | 533 | } // switch
|
---|
[51587aa] | 534 | } else {
|
---|
[8688ce1] | 535 | if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
|
---|
[ac911f4] | 536 | assert( untypedExpr->get_args().size() == 2 );
|
---|
| 537 | (*untypedExpr->get_args().begin())->accept( *this );
|
---|
| 538 | output << " ... ";
|
---|
| 539 | (*--untypedExpr->get_args().end())->accept( *this );
|
---|
[057b34f] | 540 | } else { // builtin routines
|
---|
| 541 | nameExpr->accept( *this );
|
---|
| 542 | output << "(";
|
---|
| 543 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
|
---|
| 544 | output << ")";
|
---|
[66d12f7] | 545 | } // if
|
---|
[51587aa] | 546 | } // if
|
---|
| 547 | } else {
|
---|
| 548 | untypedExpr->get_function()->accept( *this );
|
---|
[6c4ff37] | 549 | output << "(";
|
---|
[51587aa] | 550 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
|
---|
[6c4ff37] | 551 | output << ")";
|
---|
[51587aa] | 552 | } // if
|
---|
| 553 | }
|
---|
[71f4e4f] | 554 |
|
---|
[064e3ff] | 555 | void CodeGenerator::visit( RangeExpr * rangeExpr ) {
|
---|
| 556 | rangeExpr->get_low()->accept( *this );
|
---|
| 557 | output << " ... ";
|
---|
| 558 | rangeExpr->get_high()->accept( *this );
|
---|
| 559 | }
|
---|
| 560 |
|
---|
[8688ce1] | 561 | void CodeGenerator::visit( NameExpr * nameExpr ) {
|
---|
[e04ef3a] | 562 | extension( nameExpr );
|
---|
[51587aa] | 563 | OperatorInfo opInfo;
|
---|
| 564 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
|
---|
| 565 | assert( opInfo.type == OT_CONSTANT );
|
---|
[6c4ff37] | 566 | output << opInfo.symbol;
|
---|
[51587aa] | 567 | } else {
|
---|
[6c4ff37] | 568 | output << nameExpr->get_name();
|
---|
[51587aa] | 569 | } // if
|
---|
| 570 | }
|
---|
[71f4e4f] | 571 |
|
---|
[8688ce1] | 572 | void CodeGenerator::visit( AddressExpr * addressExpr ) {
|
---|
[e04ef3a] | 573 | extension( addressExpr );
|
---|
[6c4ff37] | 574 | output << "(&";
|
---|
[51587aa] | 575 | // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
|
---|
[8688ce1] | 576 | if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
|
---|
[6c4ff37] | 577 | output << mangleName( variableExpr->get_var() );
|
---|
[51587aa] | 578 | } else {
|
---|
| 579 | addressExpr->get_arg()->accept( *this );
|
---|
| 580 | } // if
|
---|
[6c4ff37] | 581 | output << ")";
|
---|
[51587aa] | 582 | }
|
---|
| 583 |
|
---|
[8688ce1] | 584 | void CodeGenerator::visit( CastExpr * castExpr ) {
|
---|
[e04ef3a] | 585 | extension( castExpr );
|
---|
[803deb1] | 586 | output << "(";
|
---|
[906e24d] | 587 | if ( castExpr->get_result()->isVoid() ) {
|
---|
[803deb1] | 588 | output << "(void)" ;
|
---|
[615a096] | 589 | } else if ( ! castExpr->get_result()->get_lvalue() ) {
|
---|
[803deb1] | 590 | // at least one result type of cast, but not an lvalue
|
---|
| 591 | output << "(";
|
---|
[e39241b] | 592 | output << genType( castExpr->get_result(), "", pretty, genC );
|
---|
[71f4e4f] | 593 | output << ")";
|
---|
[803deb1] | 594 | } else {
|
---|
[8e9cbb2] | 595 | // otherwise, the cast is to an lvalue type, so the cast should be dropped, since the result of a cast is
|
---|
[803deb1] | 596 | // never an lvalue in C
|
---|
[3778cb2] | 597 | } // if
|
---|
[803deb1] | 598 | castExpr->get_arg()->accept( *this );
|
---|
| 599 | output << ")";
|
---|
[51587aa] | 600 | }
|
---|
[71f4e4f] | 601 |
|
---|
[8688ce1] | 602 | void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
|
---|
[e39241b] | 603 | assertf( ! genC, "UntypedMemberExpr should not reach code generation." );
|
---|
| 604 | extension( memberExpr );
|
---|
| 605 | memberExpr->get_aggregate()->accept( *this );
|
---|
[5f642e38] | 606 | output << ".";
|
---|
| 607 | memberExpr->get_member()->accept( *this );
|
---|
[51587aa] | 608 | }
|
---|
[71f4e4f] | 609 |
|
---|
[8688ce1] | 610 | void CodeGenerator::visit( MemberExpr * memberExpr ) {
|
---|
[e04ef3a] | 611 | extension( memberExpr );
|
---|
[51587aa] | 612 | memberExpr->get_aggregate()->accept( *this );
|
---|
[6c4ff37] | 613 | output << "." << mangleName( memberExpr->get_member() );
|
---|
[51587aa] | 614 | }
|
---|
[71f4e4f] | 615 |
|
---|
[8688ce1] | 616 | void CodeGenerator::visit( VariableExpr * variableExpr ) {
|
---|
[e04ef3a] | 617 | extension( variableExpr );
|
---|
[51587aa] | 618 | OperatorInfo opInfo;
|
---|
| 619 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
|
---|
[6c4ff37] | 620 | output << opInfo.symbol;
|
---|
[51587aa] | 621 | } else {
|
---|
[6c4ff37] | 622 | output << mangleName( variableExpr->get_var() );
|
---|
[51587aa] | 623 | } // if
|
---|
| 624 | }
|
---|
[71f4e4f] | 625 |
|
---|
[8688ce1] | 626 | void CodeGenerator::visit( ConstantExpr * constantExpr ) {
|
---|
[51587aa] | 627 | assert( constantExpr->get_constant() );
|
---|
[e04ef3a] | 628 | extension( constantExpr );
|
---|
[51587aa] | 629 | constantExpr->get_constant()->accept( *this );
|
---|
| 630 | }
|
---|
[71f4e4f] | 631 |
|
---|
[8688ce1] | 632 | void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
|
---|
[e04ef3a] | 633 | extension( sizeofExpr );
|
---|
[6c4ff37] | 634 | output << "sizeof(";
|
---|
[51587aa] | 635 | if ( sizeofExpr->get_isType() ) {
|
---|
[e39241b] | 636 | output << genType( sizeofExpr->get_type(), "", pretty, genC );
|
---|
[51587aa] | 637 | } else {
|
---|
| 638 | sizeofExpr->get_expr()->accept( *this );
|
---|
| 639 | } // if
|
---|
[6c4ff37] | 640 | output << ")";
|
---|
[51587aa] | 641 | }
|
---|
[47534159] | 642 |
|
---|
[8688ce1] | 643 | void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
|
---|
[47534159] | 644 | // use GCC extension to avoid bumping std to C11
|
---|
[8e9cbb2] | 645 | extension( alignofExpr );
|
---|
[47534159] | 646 | output << "__alignof__(";
|
---|
[25a054f] | 647 | if ( alignofExpr->get_isType() ) {
|
---|
[e39241b] | 648 | output << genType( alignofExpr->get_type(), "", pretty, genC );
|
---|
[47534159] | 649 | } else {
|
---|
[25a054f] | 650 | alignofExpr->get_expr()->accept( *this );
|
---|
[47534159] | 651 | } // if
|
---|
| 652 | output << ")";
|
---|
| 653 | }
|
---|
[71f4e4f] | 654 |
|
---|
[8688ce1] | 655 | void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
|
---|
[e39241b] | 656 | assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." );
|
---|
| 657 | output << "offsetof(";
|
---|
| 658 | output << genType( offsetofExpr->get_type(), "", pretty, genC );
|
---|
| 659 | output << ", " << offsetofExpr->get_member();
|
---|
| 660 | output << ")";
|
---|
[2a4b088] | 661 | }
|
---|
| 662 |
|
---|
[8688ce1] | 663 | void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
|
---|
[25a054f] | 664 | // use GCC builtin
|
---|
| 665 | output << "__builtin_offsetof(";
|
---|
[e39241b] | 666 | output << genType( offsetofExpr->get_type(), "", pretty, genC );
|
---|
[e551c69] | 667 | output << ", " << mangleName( offsetofExpr->get_member() );
|
---|
[25a054f] | 668 | output << ")";
|
---|
| 669 | }
|
---|
[d63eeb0] | 670 |
|
---|
[8688ce1] | 671 | void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
|
---|
[e39241b] | 672 | assertf( ! genC, "OffsetPackExpr should not reach code generation." );
|
---|
| 673 | output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")";
|
---|
[afc1045] | 674 | }
|
---|
[70a06f6] | 675 |
|
---|
[8688ce1] | 676 | void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
|
---|
[e04ef3a] | 677 | extension( logicalExpr );
|
---|
[6c4ff37] | 678 | output << "(";
|
---|
[51587aa] | 679 | logicalExpr->get_arg1()->accept( *this );
|
---|
| 680 | if ( logicalExpr->get_isAnd() ) {
|
---|
[6c4ff37] | 681 | output << " && ";
|
---|
[51587aa] | 682 | } else {
|
---|
[6c4ff37] | 683 | output << " || ";
|
---|
[51587aa] | 684 | } // if
|
---|
| 685 | logicalExpr->get_arg2()->accept( *this );
|
---|
[6c4ff37] | 686 | output << ")";
|
---|
[51587aa] | 687 | }
|
---|
[71f4e4f] | 688 |
|
---|
[8688ce1] | 689 | void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
|
---|
[e04ef3a] | 690 | extension( conditionalExpr );
|
---|
[6c4ff37] | 691 | output << "(";
|
---|
[51587aa] | 692 | conditionalExpr->get_arg1()->accept( *this );
|
---|
[6c4ff37] | 693 | output << " ? ";
|
---|
[51587aa] | 694 | conditionalExpr->get_arg2()->accept( *this );
|
---|
[6c4ff37] | 695 | output << " : ";
|
---|
[51587aa] | 696 | conditionalExpr->get_arg3()->accept( *this );
|
---|
[6c4ff37] | 697 | output << ")";
|
---|
[51587aa] | 698 | }
|
---|
[71f4e4f] | 699 |
|
---|
[8688ce1] | 700 | void CodeGenerator::visit( CommaExpr * commaExpr ) {
|
---|
[e04ef3a] | 701 | extension( commaExpr );
|
---|
[6c4ff37] | 702 | output << "(";
|
---|
[51587aa] | 703 | commaExpr->get_arg1()->accept( *this );
|
---|
[6c4ff37] | 704 | output << " , ";
|
---|
[51587aa] | 705 | commaExpr->get_arg2()->accept( *this );
|
---|
[6c4ff37] | 706 | output << ")";
|
---|
[51587aa] | 707 | }
|
---|
[71f4e4f] | 708 |
|
---|
[e39241b] | 709 | void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) {
|
---|
| 710 | assertf( ! genC, "UntypedTupleExpr should not reach code generation." );
|
---|
[f975c65] | 711 | extension( tupleExpr );
|
---|
[e39241b] | 712 | output << "[";
|
---|
| 713 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
|
---|
| 714 | output << "]";
|
---|
| 715 | }
|
---|
[907eccb] | 716 |
|
---|
[e39241b] | 717 | void CodeGenerator::visit( TupleExpr * tupleExpr ) {
|
---|
| 718 | assertf( ! genC, "TupleExpr should not reach code generation." );
|
---|
[f975c65] | 719 | extension( tupleExpr );
|
---|
[e39241b] | 720 | output << "[";
|
---|
| 721 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
|
---|
| 722 | output << "]";
|
---|
| 723 | }
|
---|
[71f4e4f] | 724 |
|
---|
[f975c65] | 725 | void CodeGenerator::visit( TupleIndexExpr * tupleExpr ) {
|
---|
| 726 | assertf( ! genC, "TupleIndexExpr should not reach code generation." );
|
---|
| 727 | extension( tupleExpr );
|
---|
| 728 | tupleExpr->get_tuple()->accept( *this );
|
---|
| 729 | output << "." << tupleExpr->get_index();
|
---|
| 730 | }
|
---|
| 731 |
|
---|
[e39241b] | 732 | void CodeGenerator::visit( TypeExpr * typeExpr ) {
|
---|
| 733 | assertf( ! genC, "TypeExpr should not reach code generation." );
|
---|
| 734 | output<< genType( typeExpr->get_type(), "", pretty, genC );
|
---|
| 735 | }
|
---|
[2b6c1e0] | 736 |
|
---|
[8688ce1] | 737 | void CodeGenerator::visit( AsmExpr * asmExpr ) {
|
---|
[7f5566b] | 738 | if ( asmExpr->get_inout() ) {
|
---|
| 739 | output << "[ ";
|
---|
| 740 | asmExpr->get_inout()->accept( *this );
|
---|
| 741 | output << " ] ";
|
---|
| 742 | } // if
|
---|
| 743 | asmExpr->get_constraint()->accept( *this );
|
---|
| 744 | output << " ( ";
|
---|
| 745 | asmExpr->get_operand()->accept( *this );
|
---|
| 746 | output << " )";
|
---|
| 747 | }
|
---|
| 748 |
|
---|
[3c13c03] | 749 | void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) {
|
---|
[fbcde64] | 750 | assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
|
---|
[e39241b] | 751 | output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")";
|
---|
[3c13c03] | 752 | compLitExpr->get_initializer()->accept( *this );
|
---|
| 753 | }
|
---|
| 754 |
|
---|
[6eb8948] | 755 | void CodeGenerator::visit( StmtExpr * stmtExpr ) {
|
---|
[3c13c03] | 756 | std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
|
---|
[4810867] | 757 | output << lineDirective( stmtExpr) << "({" << std::endl;
|
---|
[3c13c03] | 758 | cur_indent += CodeGenerator::tabsize;
|
---|
| 759 | unsigned int numStmts = stmts.size();
|
---|
| 760 | unsigned int i = 0;
|
---|
| 761 | for ( Statement * stmt : stmts ) {
|
---|
[4810867] | 762 | output << lineDirective( stmt ) << indent;
|
---|
| 763 | output << printLabels( stmt->get_labels() );
|
---|
[3c13c03] | 764 | if ( i+1 == numStmts ) {
|
---|
| 765 | // last statement in a statement expression needs to be handled specially -
|
---|
| 766 | // cannot cast to void, otherwise the expression statement has no value
|
---|
| 767 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
|
---|
| 768 | exprStmt->get_expr()->accept( *this );
|
---|
| 769 | output << ";" << endl;
|
---|
| 770 | ++i;
|
---|
| 771 | break;
|
---|
| 772 | }
|
---|
| 773 | }
|
---|
| 774 | stmt->accept( *this );
|
---|
| 775 | output << endl;
|
---|
| 776 | if ( wantSpacing( stmt ) ) {
|
---|
| 777 | output << endl;
|
---|
| 778 | } // if
|
---|
| 779 | ++i;
|
---|
| 780 | }
|
---|
| 781 | cur_indent -= CodeGenerator::tabsize;
|
---|
| 782 | output << indent << "})";
|
---|
[6eb8948] | 783 | }
|
---|
| 784 |
|
---|
[4810867] | 785 | // *** Statements
|
---|
[8688ce1] | 786 | void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
|
---|
[51587aa] | 787 | std::list<Statement*> ks = compoundStmt->get_kids();
|
---|
[2b6c1e0] | 788 | output << "{" << endl;
|
---|
[51587aa] | 789 |
|
---|
[2b6c1e0] | 790 | cur_indent += CodeGenerator::tabsize;
|
---|
[51587aa] | 791 |
|
---|
[7f5566b] | 792 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
|
---|
[cda48b6] | 793 | output << indent << printLabels( (*i)->get_labels() );
|
---|
[7f5566b] | 794 | (*i)->accept( *this );
|
---|
[2b6c1e0] | 795 |
|
---|
[6c4ff37] | 796 | output << endl;
|
---|
[2b6c1e0] | 797 | if ( wantSpacing( *i ) ) {
|
---|
| 798 | output << endl;
|
---|
[3778cb2] | 799 | } // if
|
---|
[8688ce1] | 800 | } // for
|
---|
[71f4e4f] | 801 | cur_indent -= CodeGenerator::tabsize;
|
---|
[51587aa] | 802 |
|
---|
[cda48b6] | 803 | output << indent << "}";
|
---|
[51587aa] | 804 | }
|
---|
| 805 |
|
---|
[8688ce1] | 806 | void CodeGenerator::visit( ExprStmt * exprStmt ) {
|
---|
[6c4ff37] | 807 | assert( exprStmt );
|
---|
[262f085f] | 808 | Expression * expr = exprStmt->get_expr();
|
---|
| 809 | if ( genC ) {
|
---|
| 810 | // cast the top-level expression to void to reduce gcc warnings.
|
---|
| 811 | expr = new CastExpr( expr );
|
---|
| 812 | }
|
---|
[321a2481] | 813 | expr->accept( *this );
|
---|
| 814 | output << ";";
|
---|
[51587aa] | 815 | }
|
---|
| 816 |
|
---|
[8688ce1] | 817 | void CodeGenerator::visit( AsmStmt * asmStmt ) {
|
---|
[7f5566b] | 818 | output << "asm ";
|
---|
| 819 | if ( asmStmt->get_voltile() ) output << "volatile ";
|
---|
| 820 | if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
|
---|
| 821 | output << "( ";
|
---|
| 822 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
|
---|
| 823 | output << " : ";
|
---|
| 824 | genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
|
---|
| 825 | output << " : ";
|
---|
| 826 | genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
|
---|
| 827 | output << " : ";
|
---|
| 828 | genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
|
---|
| 829 | if ( ! asmStmt->get_gotolabels().empty() ) {
|
---|
| 830 | output << " : ";
|
---|
| 831 | for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
|
---|
| 832 | output << *begin++;
|
---|
| 833 | if ( begin == asmStmt->get_gotolabels().end() ) break;
|
---|
| 834 | output << ", ";
|
---|
| 835 | } // for
|
---|
| 836 | } // if
|
---|
| 837 | output << " );" ;
|
---|
| 838 | }
|
---|
| 839 |
|
---|
[e994912] | 840 | void CodeGenerator::visit( AsmDecl * asmDecl ) {
|
---|
| 841 | output << "asm ";
|
---|
| 842 | AsmStmt * asmStmt = asmDecl->get_stmt();
|
---|
| 843 | output << "( ";
|
---|
| 844 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
|
---|
| 845 | output << " )" ;
|
---|
| 846 | }
|
---|
| 847 |
|
---|
[8688ce1] | 848 | void CodeGenerator::visit( IfStmt * ifStmt ) {
|
---|
[4810867] | 849 | output << lineDirective( ifStmt );
|
---|
[7f5566b] | 850 | output << "if ( ";
|
---|
| 851 | ifStmt->get_condition()->accept( *this );
|
---|
| 852 | output << " ) ";
|
---|
[51587aa] | 853 |
|
---|
[7f5566b] | 854 | ifStmt->get_thenPart()->accept( *this );
|
---|
[51587aa] | 855 |
|
---|
| 856 | if ( ifStmt->get_elsePart() != 0) {
|
---|
[2b6c1e0] | 857 | output << " else ";
|
---|
[7f5566b] | 858 | ifStmt->get_elsePart()->accept( *this );
|
---|
[51587aa] | 859 | } // if
|
---|
| 860 | }
|
---|
| 861 |
|
---|
[8688ce1] | 862 | void CodeGenerator::visit( SwitchStmt * switchStmt ) {
|
---|
[4810867] | 863 | output << lineDirective( switchStmt );
|
---|
[7f5566b] | 864 | output << "switch ( " ;
|
---|
| 865 | switchStmt->get_condition()->accept( *this );
|
---|
| 866 | output << " ) ";
|
---|
[71f4e4f] | 867 |
|
---|
[2b6c1e0] | 868 | output << "{" << std::endl;
|
---|
[6c4ff37] | 869 | cur_indent += CodeGenerator::tabsize;
|
---|
[8688ce1] | 870 | acceptAll( switchStmt->get_statements(), *this );
|
---|
[6c4ff37] | 871 | cur_indent -= CodeGenerator::tabsize;
|
---|
[cda48b6] | 872 | output << indent << "}";
|
---|
[51587aa] | 873 | }
|
---|
| 874 |
|
---|
[8688ce1] | 875 | void CodeGenerator::visit( CaseStmt * caseStmt ) {
|
---|
[4810867] | 876 | output << lineDirective( caseStmt );
|
---|
[cda48b6] | 877 | output << indent;
|
---|
[eb3261f] | 878 | if ( caseStmt->isDefault()) {
|
---|
[2b6c1e0] | 879 | output << "default";
|
---|
[eb3261f] | 880 | } else {
|
---|
[2b6c1e0] | 881 | output << "case ";
|
---|
[7f5566b] | 882 | caseStmt->get_condition()->accept( *this );
|
---|
[17cd4eb] | 883 | } // if
|
---|
[6c4ff37] | 884 | output << ":\n";
|
---|
[71f4e4f] | 885 |
|
---|
[51587aa] | 886 | std::list<Statement *> sts = caseStmt->get_statements();
|
---|
| 887 |
|
---|
[6c4ff37] | 888 | cur_indent += CodeGenerator::tabsize;
|
---|
[51587aa] | 889 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
|
---|
[cda48b6] | 890 | output << indent << printLabels( (*i)->get_labels() ) ;
|
---|
[7f5566b] | 891 | (*i)->accept( *this );
|
---|
[6c4ff37] | 892 | output << endl;
|
---|
[3778cb2] | 893 | } // for
|
---|
[6c4ff37] | 894 | cur_indent -= CodeGenerator::tabsize;
|
---|
[51587aa] | 895 | }
|
---|
| 896 |
|
---|
[8688ce1] | 897 | void CodeGenerator::visit( BranchStmt * branchStmt ) {
|
---|
[51587aa] | 898 | switch ( branchStmt->get_type()) {
|
---|
| 899 | case BranchStmt::Goto:
|
---|
| 900 | if ( ! branchStmt->get_target().empty() )
|
---|
[6c4ff37] | 901 | output << "goto " << branchStmt->get_target();
|
---|
[71f4e4f] | 902 | else {
|
---|
[51587aa] | 903 | if ( branchStmt->get_computedTarget() != 0 ) {
|
---|
[6c4ff37] | 904 | output << "goto *";
|
---|
[51587aa] | 905 | branchStmt->get_computedTarget()->accept( *this );
|
---|
| 906 | } // if
|
---|
| 907 | } // if
|
---|
| 908 | break;
|
---|
| 909 | case BranchStmt::Break:
|
---|
[6c4ff37] | 910 | output << "break";
|
---|
[51587aa] | 911 | break;
|
---|
| 912 | case BranchStmt::Continue:
|
---|
[6c4ff37] | 913 | output << "continue";
|
---|
[51587aa] | 914 | break;
|
---|
[3778cb2] | 915 | } // switch
|
---|
[2b6c1e0] | 916 | output << ";";
|
---|
[51587aa] | 917 | }
|
---|
| 918 |
|
---|
[8688ce1] | 919 | void CodeGenerator::visit( ReturnStmt * returnStmt ) {
|
---|
[6c4ff37] | 920 | output << "return ";
|
---|
[4b2589a] | 921 | maybeAccept( returnStmt->get_expr(), *this );
|
---|
[6c4ff37] | 922 | output << ";";
|
---|
[51587aa] | 923 | }
|
---|
| 924 |
|
---|
[8688ce1] | 925 | void CodeGenerator::visit( WhileStmt * whileStmt ) {
|
---|
[321a2481] | 926 | if ( whileStmt->get_isDoWhile() ) {
|
---|
[6c4ff37] | 927 | output << "do" ;
|
---|
[321a2481] | 928 | } else {
|
---|
[6c4ff37] | 929 | output << "while (" ;
|
---|
[7f5566b] | 930 | whileStmt->get_condition()->accept( *this );
|
---|
[6c4ff37] | 931 | output << ")";
|
---|
[51587aa] | 932 | } // if
|
---|
[2b6c1e0] | 933 | output << " ";
|
---|
[51587aa] | 934 |
|
---|
[2b6c1e0] | 935 | output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
|
---|
[51587aa] | 936 | whileStmt->get_body()->accept( *this );
|
---|
| 937 |
|
---|
[cda48b6] | 938 | output << indent;
|
---|
[51587aa] | 939 |
|
---|
| 940 | if ( whileStmt->get_isDoWhile() ) {
|
---|
[6c4ff37] | 941 | output << " while (" ;
|
---|
[7f5566b] | 942 | whileStmt->get_condition()->accept( *this );
|
---|
[6c4ff37] | 943 | output << ");";
|
---|
[51587aa] | 944 | } // if
|
---|
| 945 | }
|
---|
| 946 |
|
---|
[8688ce1] | 947 | void CodeGenerator::visit( ForStmt * forStmt ) {
|
---|
[8e9cbb2] | 948 | // initialization is always hoisted, so don't bother doing anything with that
|
---|
[145f1fc] | 949 | output << "for (;";
|
---|
[51587aa] | 950 |
|
---|
[321a2481] | 951 | if ( forStmt->get_condition() != 0 ) {
|
---|
[51587aa] | 952 | forStmt->get_condition()->accept( *this );
|
---|
[3778cb2] | 953 | } // if
|
---|
[6c4ff37] | 954 | output << ";";
|
---|
[51587aa] | 955 |
|
---|
[321a2481] | 956 | if ( forStmt->get_increment() != 0 ) {
|
---|
| 957 | // cast the top-level expression to void to reduce gcc warnings.
|
---|
| 958 | Expression * expr = new CastExpr( forStmt->get_increment() );
|
---|
| 959 | expr->accept( *this );
|
---|
[3778cb2] | 960 | } // if
|
---|
[2b6c1e0] | 961 | output << ") ";
|
---|
[51587aa] | 962 |
|
---|
| 963 | if ( forStmt->get_body() != 0 ) {
|
---|
[2b6c1e0] | 964 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
|
---|
[51587aa] | 965 | forStmt->get_body()->accept( *this );
|
---|
| 966 | } // if
|
---|
| 967 | }
|
---|
| 968 |
|
---|
[8688ce1] | 969 | void CodeGenerator::visit( NullStmt * nullStmt ) {
|
---|
[cda48b6] | 970 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
|
---|
[6c4ff37] | 971 | output << "/* null statement */ ;";
|
---|
[51587aa] | 972 | }
|
---|
| 973 |
|
---|
[8688ce1] | 974 | void CodeGenerator::visit( DeclStmt * declStmt ) {
|
---|
[51587aa] | 975 | declStmt->get_decl()->accept( *this );
|
---|
[71f4e4f] | 976 |
|
---|
[51587aa] | 977 | if ( doSemicolon( declStmt->get_decl() ) ) {
|
---|
[6c4ff37] | 978 | output << ";";
|
---|
[51587aa] | 979 | } // if
|
---|
| 980 | }
|
---|
| 981 |
|
---|
[dd020c0] | 982 | void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) {
|
---|
[fb04321] | 983 | if ( decl->get_storageClasses().any() ) {
|
---|
[6e8bd43] | 984 | decl->get_storageClasses().print( output );
|
---|
[a7c90d4] | 985 | } // if
|
---|
[dd020c0] | 986 | } // CodeGenerator::handleStorageClass
|
---|
[9facf3b] | 987 |
|
---|
| 988 | std::string genName( DeclarationWithType * decl ) {
|
---|
| 989 | CodeGen::OperatorInfo opInfo;
|
---|
| 990 | if ( operatorLookup( decl->get_name(), opInfo ) ) {
|
---|
| 991 | return opInfo.outputName;
|
---|
| 992 | } else {
|
---|
| 993 | return decl->get_name();
|
---|
| 994 | } // if
|
---|
| 995 | }
|
---|
[51b73452] | 996 | } // namespace CodeGen
|
---|
[51587aa] | 997 |
|
---|
| 998 | // Local Variables: //
|
---|
| 999 | // tab-width: 4 //
|
---|
| 1000 | // mode: c++ //
|
---|
| 1001 | // compile-command: "make install" //
|
---|
| 1002 | // End: //
|
---|