| [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
|
|---|
| [e04ef3a] | 11 | // Last Modified By : Peter A. Buhr
|
|---|
| [5d125e4] | 12 | // Last Modified On : Fri Jul 15 15:53:15 2016
|
|---|
| 13 | // Update Count : 306
|
|---|
| [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 ) ||
|
|---|
| 47 | dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| [8e9cbb2] | 50 | void CodeGenerator::extension( Expression *expr ) {
|
|---|
| 51 | if ( expr->get_extension() ) {
|
|---|
| 52 | output << "__extension__ ";
|
|---|
| 53 | } // if
|
|---|
| 54 | } // extension
|
|---|
| 55 |
|
|---|
| 56 | void CodeGenerator::extension( Declaration *decl ) {
|
|---|
| 57 | if ( decl->get_extension() ) {
|
|---|
| 58 | output << "__extension__ ";
|
|---|
| 59 | } // if
|
|---|
| 60 | } // extension
|
|---|
| 61 |
|
|---|
| [888cbe4] | 62 | ostream & CodeGenerator::Indenter::operator()( ostream & output ) const {
|
|---|
| [cda48b6] | 63 | return output << string( cg.cur_indent, ' ' );
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| [888cbe4] | 66 | ostream & operator<<( ostream & output, const CodeGenerator::Indenter &indent ) {
|
|---|
| [cda48b6] | 67 | return indent( output );
|
|---|
| 68 | }
|
|---|
| [51587aa] | 69 |
|
|---|
| [888cbe4] | 70 | CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) {
|
|---|
| 71 | labels = &l;
|
|---|
| 72 | return *this;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter &printLabels ) {
|
|---|
| 76 | std::list< Label > & labs = *printLabels.labels;
|
|---|
| 77 | // l.unique(); // assumes a sorted list. Why not use set? Does order matter?
|
|---|
| 78 | for ( Label & l : labs ) {
|
|---|
| 79 | output << l.get_name() + ": ";
|
|---|
| 80 | printLabels.cg.genAttributes( l.get_attributes() );
|
|---|
| 81 | }
|
|---|
| 82 | return output;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | CodeGenerator::CodeGenerator( std::ostream &os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ) { }
|
|---|
| [cda48b6] | 86 |
|
|---|
| 87 | CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
|
|---|
| [888cbe4] | 88 | : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) {
|
|---|
| [6c4ff37] | 89 | //output << std::string( init );
|
|---|
| [51587aa] | 90 | }
|
|---|
| 91 |
|
|---|
| [cda48b6] | 92 | CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
|
|---|
| [888cbe4] | 93 | : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) {
|
|---|
| [6c4ff37] | 94 | //output << std::string( init );
|
|---|
| [51587aa] | 95 | }
|
|---|
| 96 |
|
|---|
| 97 | string mangleName( DeclarationWithType *decl ) {
|
|---|
| 98 | if ( decl->get_mangleName() != "" ) {
|
|---|
| [f326f99] | 99 | // need to incorporate scope level in order to differentiate names for destructors
|
|---|
| 100 | return decl->get_scopedMangleName();
|
|---|
| [51587aa] | 101 | } else {
|
|---|
| 102 | return decl->get_name();
|
|---|
| 103 | } // if
|
|---|
| 104 | }
|
|---|
| [94b4364] | 105 |
|
|---|
| [7baed7d] | 106 | void CodeGenerator::genAttributes( std::list< Attribute * > & attributes ) {
|
|---|
| 107 | if ( ! attributes.empty() ) {
|
|---|
| 108 | output << "__attribute__ ((";
|
|---|
| 109 | for ( Attribute *& attr : attributes ) {
|
|---|
| 110 | if ( ! attr->empty() ) {
|
|---|
| 111 | output << attr->get_name() << "(";
|
|---|
| 112 | genCommaList( attr->get_parameters().begin(), attr->get_parameters().end() );
|
|---|
| 113 | output << ")";
|
|---|
| [03e5d14] | 114 | }
|
|---|
| [7baed7d] | 115 | output << ",";
|
|---|
| 116 | }
|
|---|
| 117 | output << ")) ";
|
|---|
| [4e24610] | 118 | }
|
|---|
| [7baed7d] | 119 | }
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 | //*** Declarations
|
|---|
| 123 | void CodeGenerator::visit( FunctionDecl *functionDecl ) {
|
|---|
| [8e9cbb2] | 124 | extension( functionDecl );
|
|---|
| [7baed7d] | 125 | genAttributes( functionDecl->get_attributes() );
|
|---|
| 126 |
|
|---|
| [51587aa] | 127 | handleStorageClass( functionDecl );
|
|---|
| [f38c8d9] | 128 | if ( functionDecl->get_isInline() ) {
|
|---|
| [6c4ff37] | 129 | output << "inline ";
|
|---|
| [f38c8d9] | 130 | } // if
|
|---|
| [de62360d] | 131 | if ( functionDecl->get_isNoreturn() ) {
|
|---|
| 132 | output << "_Noreturn ";
|
|---|
| 133 | } // if
|
|---|
| [6c4ff37] | 134 | output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
|
|---|
| [51587aa] | 135 |
|
|---|
| 136 | // how to get this to the Functype?
|
|---|
| 137 | std::list< Declaration * > olds = functionDecl->get_oldDecls();
|
|---|
| 138 | if ( ! olds.empty() ) {
|
|---|
| [6c4ff37] | 139 | output << " /* function has old declaration */";
|
|---|
| [51587aa] | 140 | } // if
|
|---|
| 141 |
|
|---|
| 142 | // acceptAll( functionDecl->get_oldDecls(), *this );
|
|---|
| 143 | if ( functionDecl->get_statements() ) {
|
|---|
| [7f5566b] | 144 | functionDecl->get_statements()->accept( *this );
|
|---|
| [51587aa] | 145 | } // if
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| [6c4ff37] | 148 | void CodeGenerator::visit( ObjectDecl *objectDecl ) {
|
|---|
| [8e9cbb2] | 149 | extension( objectDecl );
|
|---|
| [51587aa] | 150 | handleStorageClass( objectDecl );
|
|---|
| [6c4ff37] | 151 | output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
|
|---|
| [71f4e4f] | 152 |
|
|---|
| [51587aa] | 153 | if ( objectDecl->get_init() ) {
|
|---|
| [6c4ff37] | 154 | output << " = ";
|
|---|
| [51587aa] | 155 | objectDecl->get_init()->accept( *this );
|
|---|
| 156 | } // if
|
|---|
| 157 | if ( objectDecl->get_bitfieldWidth() ) {
|
|---|
| [6c4ff37] | 158 | output << ":";
|
|---|
| [51587aa] | 159 | objectDecl->get_bitfieldWidth()->accept( *this );
|
|---|
| 160 | } // if
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| [6c4ff37] | 163 | void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
|
|---|
| [51587aa] | 164 | if ( aggDecl->get_name() != "" )
|
|---|
| [6c4ff37] | 165 | output << aggDecl->get_name();
|
|---|
| [71f4e4f] | 166 |
|
|---|
| [51587aa] | 167 | std::list< Declaration * > &memb = aggDecl->get_members();
|
|---|
| 168 | if ( ! memb.empty() ) {
|
|---|
| [5d125e4] | 169 | // if ( aggDecl->has_body() ) {
|
|---|
| 170 | // std::list< Declaration * > &memb = aggDecl->get_members();
|
|---|
| [94b4364] | 171 | output << " {" << endl;
|
|---|
| [51587aa] | 172 |
|
|---|
| [71f4e4f] | 173 | cur_indent += CodeGenerator::tabsize;
|
|---|
| [51587aa] | 174 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
|
|---|
| [71f4e4f] | 175 | output << indent;
|
|---|
| [7f5566b] | 176 | (*i)->accept( *this );
|
|---|
| [6c4ff37] | 177 | output << ";" << endl;
|
|---|
| [51587aa] | 178 | }
|
|---|
| 179 |
|
|---|
| [71f4e4f] | 180 | cur_indent -= CodeGenerator::tabsize;
|
|---|
| [51587aa] | 181 |
|
|---|
| [cda48b6] | 182 | output << indent << "}";
|
|---|
| [17cd4eb] | 183 | } // if
|
|---|
| [51587aa] | 184 | }
|
|---|
| [17cd4eb] | 185 |
|
|---|
| [6c4ff37] | 186 | void CodeGenerator::visit( StructDecl *structDecl ) {
|
|---|
| [8e9cbb2] | 187 | extension( structDecl );
|
|---|
| [6c4ff37] | 188 | output << "struct ";
|
|---|
| [51587aa] | 189 | handleAggregate( structDecl );
|
|---|
| 190 | }
|
|---|
| [17cd4eb] | 191 |
|
|---|
| [8e9cbb2] | 192 | void CodeGenerator::visit( UnionDecl *unionDecl ) {
|
|---|
| 193 | extension( unionDecl );
|
|---|
| [6c4ff37] | 194 | output << "union ";
|
|---|
| [8e9cbb2] | 195 | handleAggregate( unionDecl );
|
|---|
| [51587aa] | 196 | }
|
|---|
| [71f4e4f] | 197 |
|
|---|
| [8e9cbb2] | 198 | void CodeGenerator::visit( EnumDecl *enumDecl ) {
|
|---|
| 199 | extension( enumDecl );
|
|---|
| [6c4ff37] | 200 | output << "enum ";
|
|---|
| [51587aa] | 201 |
|
|---|
| [8e9cbb2] | 202 | if ( enumDecl->get_name() != "" )
|
|---|
| 203 | output << enumDecl->get_name();
|
|---|
| [71f4e4f] | 204 |
|
|---|
| [8e9cbb2] | 205 | std::list< Declaration* > &memb = enumDecl->get_members();
|
|---|
| [51587aa] | 206 |
|
|---|
| 207 | if ( ! memb.empty() ) {
|
|---|
| [cda48b6] | 208 | output << " {" << endl;
|
|---|
| [51587aa] | 209 |
|
|---|
| [71f4e4f] | 210 | cur_indent += CodeGenerator::tabsize;
|
|---|
| [51587aa] | 211 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
|
|---|
| 212 | ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
|
|---|
| 213 | assert( obj );
|
|---|
| [71f4e4f] | 214 | output << indent << mangleName( obj );
|
|---|
| [51587aa] | 215 | if ( obj->get_init() ) {
|
|---|
| [6c4ff37] | 216 | output << " = ";
|
|---|
| [7f5566b] | 217 | obj->get_init()->accept( *this );
|
|---|
| [51587aa] | 218 | } // if
|
|---|
| [6c4ff37] | 219 | output << "," << endl;
|
|---|
| [51587aa] | 220 | } // for
|
|---|
| 221 |
|
|---|
| [71f4e4f] | 222 | cur_indent -= CodeGenerator::tabsize;
|
|---|
| [51587aa] | 223 |
|
|---|
| [cda48b6] | 224 | output << indent << "}";
|
|---|
| [51587aa] | 225 | } // if
|
|---|
| 226 | }
|
|---|
| [71f4e4f] | 227 |
|
|---|
| [8e9cbb2] | 228 | void CodeGenerator::visit( TraitDecl *traitDecl ) {}
|
|---|
| [71f4e4f] | 229 |
|
|---|
| [6c4ff37] | 230 | void CodeGenerator::visit( TypedefDecl *typeDecl ) {
|
|---|
| [8e9cbb2] | 231 | assert( false && "Typedefs are removed and substituted in earlier passes." );
|
|---|
| 232 | //output << "typedef ";
|
|---|
| 233 | //output << genType( typeDecl->get_base(), typeDecl->get_name() );
|
|---|
| [51587aa] | 234 | }
|
|---|
| [71f4e4f] | 235 |
|
|---|
| [6c4ff37] | 236 | void CodeGenerator::visit( TypeDecl *typeDecl ) {
|
|---|
| [51587aa] | 237 | // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
|
|---|
| 238 | // still to be done
|
|---|
| [8e9cbb2] | 239 | extension( typeDecl );
|
|---|
| [6c4ff37] | 240 | output << "extern unsigned long " << typeDecl->get_name();
|
|---|
| [51587aa] | 241 | if ( typeDecl->get_base() ) {
|
|---|
| [6c4ff37] | 242 | output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
|
|---|
| [51587aa] | 243 | } // if
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| [e45215c] | 246 | void CodeGenerator::printDesignators( std::list< Expression * > & designators ) {
|
|---|
| 247 | typedef std::list< Expression * > DesignatorList;
|
|---|
| 248 | if ( designators.size() == 0 ) return;
|
|---|
| 249 | for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) {
|
|---|
| [8a4da06] | 250 | if ( dynamic_cast< NameExpr * >( *iter ) ) {
|
|---|
| [f32c7f4] | 251 | // if expression is a name, then initializing aggregate member
|
|---|
| 252 | output << ".";
|
|---|
| 253 | (*iter)->accept( *this );
|
|---|
| 254 | } else {
|
|---|
| 255 | // if not a simple name, it has to be a constant expression, i.e. an array designator
|
|---|
| [e45215c] | 256 | output << "[";
|
|---|
| 257 | (*iter)->accept( *this );
|
|---|
| 258 | output << "]";
|
|---|
| 259 | }
|
|---|
| 260 | }
|
|---|
| 261 | output << " = ";
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| [6c4ff37] | 264 | void CodeGenerator::visit( SingleInit *init ) {
|
|---|
| [e45215c] | 265 | printDesignators( init->get_designators() );
|
|---|
| [51587aa] | 266 | init->get_value()->accept( *this );
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| [6c4ff37] | 269 | void CodeGenerator::visit( ListInit *init ) {
|
|---|
| [e45215c] | 270 | printDesignators( init->get_designators() );
|
|---|
| [6c4ff37] | 271 | output << "{ ";
|
|---|
| [5b40f30] | 272 | if ( init->begin_initializers() == init->end_initializers() ) {
|
|---|
| [8e9cbb2] | 273 | // illegal to leave initializer list empty for scalar initializers, but always legal to have 0
|
|---|
| [5b40f30] | 274 | output << "0";
|
|---|
| 275 | } else {
|
|---|
| 276 | genCommaList( init->begin_initializers(), init->end_initializers() );
|
|---|
| 277 | }
|
|---|
| [6c4ff37] | 278 | output << " }";
|
|---|
| [51587aa] | 279 | }
|
|---|
| 280 |
|
|---|
| [71f4e4f] | 281 | void CodeGenerator::visit( Constant *constant ) {
|
|---|
| [6c4ff37] | 282 | output << constant->get_value() ;
|
|---|
| [51587aa] | 283 | }
|
|---|
| 284 |
|
|---|
| 285 | //*** Expressions
|
|---|
| [6c4ff37] | 286 | void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
|
|---|
| [e04ef3a] | 287 | extension( applicationExpr );
|
|---|
| [51587aa] | 288 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
|
|---|
| 289 | OperatorInfo opInfo;
|
|---|
| 290 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
|
|---|
| 291 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
|
|---|
| 292 | switch ( opInfo.type ) {
|
|---|
| 293 | case OT_PREFIXASSIGN:
|
|---|
| 294 | case OT_POSTFIXASSIGN:
|
|---|
| 295 | case OT_INFIXASSIGN:
|
|---|
| [356189a] | 296 | case OT_CTOR:
|
|---|
| [c2ce2350] | 297 | case OT_DTOR:
|
|---|
| [51587aa] | 298 | {
|
|---|
| 299 | assert( arg != applicationExpr->get_args().end() );
|
|---|
| 300 | if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
|
|---|
| [356189a] | 301 | // remove & from first assignment/ctor argument
|
|---|
| [51587aa] | 302 | *arg = addrExpr->get_arg();
|
|---|
| 303 | } else {
|
|---|
| [356189a] | 304 | // no address-of operator, so must be a pointer - add dereference
|
|---|
| [51587aa] | 305 | UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
|
|---|
| 306 | newExpr->get_args().push_back( *arg );
|
|---|
| [10a7775] | 307 | assert( (*arg)->get_results().size() == 1 );
|
|---|
| 308 | Type * type = InitTweak::getPointerBase( (*arg)->get_results().front() );
|
|---|
| 309 | assert( type );
|
|---|
| 310 | newExpr->get_results().push_back( type );
|
|---|
| [51587aa] | 311 | *arg = newExpr;
|
|---|
| 312 | } // if
|
|---|
| 313 | break;
|
|---|
| 314 | }
|
|---|
| [71f4e4f] | 315 |
|
|---|
| [51587aa] | 316 | default:
|
|---|
| 317 | // do nothing
|
|---|
| 318 | ;
|
|---|
| 319 | }
|
|---|
| [71f4e4f] | 320 |
|
|---|
| [51587aa] | 321 | switch ( opInfo.type ) {
|
|---|
| 322 | case OT_INDEX:
|
|---|
| 323 | assert( applicationExpr->get_args().size() == 2 );
|
|---|
| 324 | (*arg++)->accept( *this );
|
|---|
| [6c4ff37] | 325 | output << "[";
|
|---|
| [51587aa] | 326 | (*arg)->accept( *this );
|
|---|
| [6c4ff37] | 327 | output << "]";
|
|---|
| [51587aa] | 328 | break;
|
|---|
| [71f4e4f] | 329 |
|
|---|
| [51587aa] | 330 | case OT_CALL:
|
|---|
| [356189a] | 331 | // there are no intrinsic definitions of the function call operator
|
|---|
| [51587aa] | 332 | assert( false );
|
|---|
| 333 | break;
|
|---|
| [71f4e4f] | 334 |
|
|---|
| [f1e012b] | 335 | case OT_CTOR:
|
|---|
| [c2ce2350] | 336 | case OT_DTOR:
|
|---|
| [356189a] | 337 | if ( applicationExpr->get_args().size() == 1 ) {
|
|---|
| [8e9cbb2] | 338 | // the expression fed into a single parameter constructor or destructor may contain side
|
|---|
| 339 | // effects, so must still output this expression
|
|---|
| [64071c2] | 340 | output << "(";
|
|---|
| [356189a] | 341 | (*arg++)->accept( *this );
|
|---|
| [64071c2] | 342 | output << ") /* " << opInfo.inputName << " */";
|
|---|
| [356189a] | 343 | } else if ( applicationExpr->get_args().size() == 2 ) {
|
|---|
| [c2ce2350] | 344 | // intrinsic two parameter constructors are essentially bitwise assignment
|
|---|
| [356189a] | 345 | output << "(";
|
|---|
| 346 | (*arg++)->accept( *this );
|
|---|
| 347 | output << opInfo.symbol;
|
|---|
| 348 | (*arg)->accept( *this );
|
|---|
| [c2ce2350] | 349 | output << ") /* " << opInfo.inputName << " */";
|
|---|
| [356189a] | 350 | } else {
|
|---|
| [c2ce2350] | 351 | // no constructors with 0 or more than 2 parameters
|
|---|
| [356189a] | 352 | assert( false );
|
|---|
| 353 | }
|
|---|
| 354 | break;
|
|---|
| [f1e012b] | 355 |
|
|---|
| [51587aa] | 356 | case OT_PREFIX:
|
|---|
| 357 | case OT_PREFIXASSIGN:
|
|---|
| 358 | assert( applicationExpr->get_args().size() == 1 );
|
|---|
| [6c4ff37] | 359 | output << "(";
|
|---|
| 360 | output << opInfo.symbol;
|
|---|
| [51587aa] | 361 | (*arg)->accept( *this );
|
|---|
| [6c4ff37] | 362 | output << ")";
|
|---|
| [51587aa] | 363 | break;
|
|---|
| [71f4e4f] | 364 |
|
|---|
| [51587aa] | 365 | case OT_POSTFIX:
|
|---|
| 366 | case OT_POSTFIXASSIGN:
|
|---|
| 367 | assert( applicationExpr->get_args().size() == 1 );
|
|---|
| 368 | (*arg)->accept( *this );
|
|---|
| [6c4ff37] | 369 | output << opInfo.symbol;
|
|---|
| [51587aa] | 370 | break;
|
|---|
| 371 |
|
|---|
| [f1e012b] | 372 |
|
|---|
| [51587aa] | 373 | case OT_INFIX:
|
|---|
| 374 | case OT_INFIXASSIGN:
|
|---|
| 375 | assert( applicationExpr->get_args().size() == 2 );
|
|---|
| [6c4ff37] | 376 | output << "(";
|
|---|
| [51587aa] | 377 | (*arg++)->accept( *this );
|
|---|
| [6c4ff37] | 378 | output << opInfo.symbol;
|
|---|
| [51587aa] | 379 | (*arg)->accept( *this );
|
|---|
| [6c4ff37] | 380 | output << ")";
|
|---|
| [51587aa] | 381 | break;
|
|---|
| [71f4e4f] | 382 |
|
|---|
| [51587aa] | 383 | case OT_CONSTANT:
|
|---|
| [721f17a] | 384 | case OT_LABELADDRESS:
|
|---|
| 385 | // there are no intrinsic definitions of 0/1 or label addresses as functions
|
|---|
| [51587aa] | 386 | assert( false );
|
|---|
| 387 | }
|
|---|
| [17cd4eb] | 388 | } else {
|
|---|
| [51587aa] | 389 | varExpr->accept( *this );
|
|---|
| [6c4ff37] | 390 | output << "(";
|
|---|
| [51587aa] | 391 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
|---|
| [6c4ff37] | 392 | output << ")";
|
|---|
| [17cd4eb] | 393 | } // if
|
|---|
| [51587aa] | 394 | } else {
|
|---|
| 395 | applicationExpr->get_function()->accept( *this );
|
|---|
| [6c4ff37] | 396 | output << "(";
|
|---|
| [51587aa] | 397 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
|---|
| [6c4ff37] | 398 | output << ")";
|
|---|
| [51587aa] | 399 | } // if
|
|---|
| 400 | }
|
|---|
| [71f4e4f] | 401 |
|
|---|
| [6c4ff37] | 402 | void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
|
|---|
| [e04ef3a] | 403 | extension( untypedExpr );
|
|---|
| [51587aa] | 404 | if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
|
|---|
| 405 | OperatorInfo opInfo;
|
|---|
| 406 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
|
|---|
| 407 | std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
|
|---|
| 408 | switch ( opInfo.type ) {
|
|---|
| 409 | case OT_INDEX:
|
|---|
| 410 | assert( untypedExpr->get_args().size() == 2 );
|
|---|
| 411 | (*arg++)->accept( *this );
|
|---|
| [6c4ff37] | 412 | output << "[";
|
|---|
| [51587aa] | 413 | (*arg)->accept( *this );
|
|---|
| [6c4ff37] | 414 | output << "]";
|
|---|
| [51587aa] | 415 | break;
|
|---|
| [71f4e4f] | 416 |
|
|---|
| [51587aa] | 417 | case OT_CALL:
|
|---|
| [f1e012b] | 418 | assert( false );
|
|---|
| 419 |
|
|---|
| [c2ce2350] | 420 |
|
|---|
| 421 | case OT_CTOR:
|
|---|
| 422 | case OT_DTOR:
|
|---|
| 423 | if ( untypedExpr->get_args().size() == 1 ) {
|
|---|
| [8e9cbb2] | 424 | // the expression fed into a single parameter constructor or destructor may contain side
|
|---|
| 425 | // effects, so must still output this expression
|
|---|
| [64071c2] | 426 | output << "(";
|
|---|
| [c2ce2350] | 427 | (*arg++)->accept( *this );
|
|---|
| [64071c2] | 428 | output << ") /* " << opInfo.inputName << " */";
|
|---|
| [c2ce2350] | 429 | } else if ( untypedExpr->get_args().size() == 2 ) {
|
|---|
| 430 | // intrinsic two parameter constructors are essentially bitwise assignment
|
|---|
| 431 | output << "(";
|
|---|
| 432 | (*arg++)->accept( *this );
|
|---|
| 433 | output << opInfo.symbol;
|
|---|
| 434 | (*arg)->accept( *this );
|
|---|
| 435 | output << ") /* " << opInfo.inputName << " */";
|
|---|
| 436 | } else {
|
|---|
| 437 | // no constructors with 0 or more than 2 parameters
|
|---|
| 438 | assert( false );
|
|---|
| 439 | }
|
|---|
| [51587aa] | 440 | break;
|
|---|
| [71f4e4f] | 441 |
|
|---|
| [51587aa] | 442 | case OT_PREFIX:
|
|---|
| 443 | case OT_PREFIXASSIGN:
|
|---|
| [de62360d] | 444 | case OT_LABELADDRESS:
|
|---|
| [51587aa] | 445 | assert( untypedExpr->get_args().size() == 1 );
|
|---|
| [6c4ff37] | 446 | output << "(";
|
|---|
| 447 | output << opInfo.symbol;
|
|---|
| [51587aa] | 448 | (*arg)->accept( *this );
|
|---|
| [6c4ff37] | 449 | output << ")";
|
|---|
| [51587aa] | 450 | break;
|
|---|
| [71f4e4f] | 451 |
|
|---|
| [51587aa] | 452 | case OT_POSTFIX:
|
|---|
| 453 | case OT_POSTFIXASSIGN:
|
|---|
| 454 | assert( untypedExpr->get_args().size() == 1 );
|
|---|
| 455 | (*arg)->accept( *this );
|
|---|
| [6c4ff37] | 456 | output << opInfo.symbol;
|
|---|
| [51587aa] | 457 | break;
|
|---|
| [71f4e4f] | 458 |
|
|---|
| [51587aa] | 459 | case OT_INFIX:
|
|---|
| 460 | case OT_INFIXASSIGN:
|
|---|
| 461 | assert( untypedExpr->get_args().size() == 2 );
|
|---|
| [6c4ff37] | 462 | output << "(";
|
|---|
| [51587aa] | 463 | (*arg++)->accept( *this );
|
|---|
| [6c4ff37] | 464 | output << opInfo.symbol;
|
|---|
| [51587aa] | 465 | (*arg)->accept( *this );
|
|---|
| [6c4ff37] | 466 | output << ")";
|
|---|
| [51587aa] | 467 | break;
|
|---|
| [71f4e4f] | 468 |
|
|---|
| [51587aa] | 469 | case OT_CONSTANT:
|
|---|
| 470 | // there are no intrinsic definitions of 0 or 1 as functions
|
|---|
| 471 | assert( false );
|
|---|
| 472 | }
|
|---|
| 473 | } else {
|
|---|
| 474 | nameExpr->accept( *this );
|
|---|
| [6c4ff37] | 475 | output << "(";
|
|---|
| [51587aa] | 476 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
|
|---|
| [6c4ff37] | 477 | output << ")";
|
|---|
| [51587aa] | 478 | } // if
|
|---|
| 479 | } else {
|
|---|
| 480 | untypedExpr->get_function()->accept( *this );
|
|---|
| [6c4ff37] | 481 | output << "(";
|
|---|
| [51587aa] | 482 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
|
|---|
| [6c4ff37] | 483 | output << ")";
|
|---|
| [51587aa] | 484 | } // if
|
|---|
| 485 | }
|
|---|
| [71f4e4f] | 486 |
|
|---|
| [6c4ff37] | 487 | void CodeGenerator::visit( NameExpr *nameExpr ) {
|
|---|
| [e04ef3a] | 488 | extension( nameExpr );
|
|---|
| [51587aa] | 489 | OperatorInfo opInfo;
|
|---|
| 490 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
|
|---|
| 491 | assert( opInfo.type == OT_CONSTANT );
|
|---|
| [6c4ff37] | 492 | output << opInfo.symbol;
|
|---|
| [51587aa] | 493 | } else {
|
|---|
| [6c4ff37] | 494 | output << nameExpr->get_name();
|
|---|
| [51587aa] | 495 | } // if
|
|---|
| 496 | }
|
|---|
| [71f4e4f] | 497 |
|
|---|
| [6c4ff37] | 498 | void CodeGenerator::visit( AddressExpr *addressExpr ) {
|
|---|
| [e04ef3a] | 499 | extension( addressExpr );
|
|---|
| [6c4ff37] | 500 | output << "(&";
|
|---|
| [51587aa] | 501 | // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
|
|---|
| 502 | if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
|
|---|
| [6c4ff37] | 503 | output << mangleName( variableExpr->get_var() );
|
|---|
| [51587aa] | 504 | } else {
|
|---|
| 505 | addressExpr->get_arg()->accept( *this );
|
|---|
| 506 | } // if
|
|---|
| [6c4ff37] | 507 | output << ")";
|
|---|
| [51587aa] | 508 | }
|
|---|
| 509 |
|
|---|
| [6c4ff37] | 510 | void CodeGenerator::visit( CastExpr *castExpr ) {
|
|---|
| [e04ef3a] | 511 | extension( castExpr );
|
|---|
| [803deb1] | 512 | output << "(";
|
|---|
| 513 | if ( castExpr->get_results().empty() ) {
|
|---|
| 514 | output << "(void)" ;
|
|---|
| 515 | } else if ( ! castExpr->get_results().front()->get_isLvalue() ) {
|
|---|
| 516 | // at least one result type of cast, but not an lvalue
|
|---|
| 517 | output << "(";
|
|---|
| 518 | output << genType( castExpr->get_results().front(), "" );
|
|---|
| [71f4e4f] | 519 | output << ")";
|
|---|
| [803deb1] | 520 | } else {
|
|---|
| [8e9cbb2] | 521 | // otherwise, the cast is to an lvalue type, so the cast should be dropped, since the result of a cast is
|
|---|
| [803deb1] | 522 | // never an lvalue in C
|
|---|
| [76b48f1] | 523 | }
|
|---|
| [803deb1] | 524 | castExpr->get_arg()->accept( *this );
|
|---|
| 525 | output << ")";
|
|---|
| [51587aa] | 526 | }
|
|---|
| [71f4e4f] | 527 |
|
|---|
| [6c4ff37] | 528 | void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
|
|---|
| [51587aa] | 529 | assert( false );
|
|---|
| 530 | }
|
|---|
| [71f4e4f] | 531 |
|
|---|
| [6c4ff37] | 532 | void CodeGenerator::visit( MemberExpr *memberExpr ) {
|
|---|
| [e04ef3a] | 533 | extension( memberExpr );
|
|---|
| [51587aa] | 534 | memberExpr->get_aggregate()->accept( *this );
|
|---|
| [6c4ff37] | 535 | output << "." << mangleName( memberExpr->get_member() );
|
|---|
| [51587aa] | 536 | }
|
|---|
| [71f4e4f] | 537 |
|
|---|
| [6c4ff37] | 538 | void CodeGenerator::visit( VariableExpr *variableExpr ) {
|
|---|
| [e04ef3a] | 539 | extension( variableExpr );
|
|---|
| [51587aa] | 540 | OperatorInfo opInfo;
|
|---|
| 541 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
|
|---|
| [6c4ff37] | 542 | output << opInfo.symbol;
|
|---|
| [51587aa] | 543 | } else {
|
|---|
| [6c4ff37] | 544 | output << mangleName( variableExpr->get_var() );
|
|---|
| [51587aa] | 545 | } // if
|
|---|
| 546 | }
|
|---|
| [71f4e4f] | 547 |
|
|---|
| [6c4ff37] | 548 | void CodeGenerator::visit( ConstantExpr *constantExpr ) {
|
|---|
| [51587aa] | 549 | assert( constantExpr->get_constant() );
|
|---|
| [e04ef3a] | 550 | extension( constantExpr );
|
|---|
| [51587aa] | 551 | constantExpr->get_constant()->accept( *this );
|
|---|
| 552 | }
|
|---|
| [71f4e4f] | 553 |
|
|---|
| [6c4ff37] | 554 | void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
|
|---|
| [e04ef3a] | 555 | extension( sizeofExpr );
|
|---|
| [6c4ff37] | 556 | output << "sizeof(";
|
|---|
| [51587aa] | 557 | if ( sizeofExpr->get_isType() ) {
|
|---|
| [6c4ff37] | 558 | output << genType( sizeofExpr->get_type(), "" );
|
|---|
| [51587aa] | 559 | } else {
|
|---|
| 560 | sizeofExpr->get_expr()->accept( *this );
|
|---|
| 561 | } // if
|
|---|
| [6c4ff37] | 562 | output << ")";
|
|---|
| [51587aa] | 563 | }
|
|---|
| [47534159] | 564 |
|
|---|
| [25a054f] | 565 | void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
|
|---|
| [47534159] | 566 | // use GCC extension to avoid bumping std to C11
|
|---|
| [8e9cbb2] | 567 | extension( alignofExpr );
|
|---|
| [47534159] | 568 | output << "__alignof__(";
|
|---|
| [25a054f] | 569 | if ( alignofExpr->get_isType() ) {
|
|---|
| 570 | output << genType( alignofExpr->get_type(), "" );
|
|---|
| [47534159] | 571 | } else {
|
|---|
| [25a054f] | 572 | alignofExpr->get_expr()->accept( *this );
|
|---|
| [47534159] | 573 | } // if
|
|---|
| 574 | output << ")";
|
|---|
| 575 | }
|
|---|
| [71f4e4f] | 576 |
|
|---|
| [2a4b088] | 577 | void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
|
|---|
| [8e9cbb2] | 578 | assert( false && "UntypedOffsetofExpr should not reach code generation." );
|
|---|
| [2a4b088] | 579 | }
|
|---|
| 580 |
|
|---|
| [25a054f] | 581 | void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
|
|---|
| 582 | // use GCC builtin
|
|---|
| 583 | output << "__builtin_offsetof(";
|
|---|
| 584 | output << genType( offsetofExpr->get_type(), "" );
|
|---|
| [e551c69] | 585 | output << ", " << mangleName( offsetofExpr->get_member() );
|
|---|
| [25a054f] | 586 | output << ")";
|
|---|
| 587 | }
|
|---|
| [d63eeb0] | 588 |
|
|---|
| [afc1045] | 589 | void CodeGenerator::visit( OffsetPackExpr *offsetPackExpr ) {
|
|---|
| [8e9cbb2] | 590 | assert( false && "OffsetPackExpr should not reach code generation." );
|
|---|
| [afc1045] | 591 | }
|
|---|
| [70a06f6] | 592 |
|
|---|
| [6c4ff37] | 593 | void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
|
|---|
| [e04ef3a] | 594 | extension( logicalExpr );
|
|---|
| [6c4ff37] | 595 | output << "(";
|
|---|
| [51587aa] | 596 | logicalExpr->get_arg1()->accept( *this );
|
|---|
| 597 | if ( logicalExpr->get_isAnd() ) {
|
|---|
| [6c4ff37] | 598 | output << " && ";
|
|---|
| [51587aa] | 599 | } else {
|
|---|
| [6c4ff37] | 600 | output << " || ";
|
|---|
| [51587aa] | 601 | } // if
|
|---|
| 602 | logicalExpr->get_arg2()->accept( *this );
|
|---|
| [6c4ff37] | 603 | output << ")";
|
|---|
| [51587aa] | 604 | }
|
|---|
| [71f4e4f] | 605 |
|
|---|
| [6c4ff37] | 606 | void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
|
|---|
| [e04ef3a] | 607 | extension( conditionalExpr );
|
|---|
| [6c4ff37] | 608 | output << "(";
|
|---|
| [51587aa] | 609 | conditionalExpr->get_arg1()->accept( *this );
|
|---|
| [6c4ff37] | 610 | output << " ? ";
|
|---|
| [51587aa] | 611 | conditionalExpr->get_arg2()->accept( *this );
|
|---|
| [6c4ff37] | 612 | output << " : ";
|
|---|
| [51587aa] | 613 | conditionalExpr->get_arg3()->accept( *this );
|
|---|
| [6c4ff37] | 614 | output << ")";
|
|---|
| [51587aa] | 615 | }
|
|---|
| [71f4e4f] | 616 |
|
|---|
| [6c4ff37] | 617 | void CodeGenerator::visit( CommaExpr *commaExpr ) {
|
|---|
| [e04ef3a] | 618 | extension( commaExpr );
|
|---|
| [6c4ff37] | 619 | output << "(";
|
|---|
| [51587aa] | 620 | commaExpr->get_arg1()->accept( *this );
|
|---|
| [6c4ff37] | 621 | output << " , ";
|
|---|
| [51587aa] | 622 | commaExpr->get_arg2()->accept( *this );
|
|---|
| [6c4ff37] | 623 | output << ")";
|
|---|
| [51587aa] | 624 | }
|
|---|
| [71f4e4f] | 625 |
|
|---|
| [6c4ff37] | 626 | void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
|
|---|
| [71f4e4f] | 627 |
|
|---|
| [6c4ff37] | 628 | void CodeGenerator::visit( TypeExpr *typeExpr ) {}
|
|---|
| [2b6c1e0] | 629 |
|
|---|
| [7f5566b] | 630 | void CodeGenerator::visit( AsmExpr *asmExpr ) {
|
|---|
| 631 | if ( asmExpr->get_inout() ) {
|
|---|
| 632 | output << "[ ";
|
|---|
| 633 | asmExpr->get_inout()->accept( *this );
|
|---|
| 634 | output << " ] ";
|
|---|
| 635 | } // if
|
|---|
| 636 | asmExpr->get_constraint()->accept( *this );
|
|---|
| 637 | output << " ( ";
|
|---|
| 638 | asmExpr->get_operand()->accept( *this );
|
|---|
| 639 | output << " )";
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| [51587aa] | 642 | //*** Statements
|
|---|
| [6c4ff37] | 643 | void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
|
|---|
| [51587aa] | 644 | std::list<Statement*> ks = compoundStmt->get_kids();
|
|---|
| [2b6c1e0] | 645 | output << "{" << endl;
|
|---|
| [51587aa] | 646 |
|
|---|
| [2b6c1e0] | 647 | cur_indent += CodeGenerator::tabsize;
|
|---|
| [51587aa] | 648 |
|
|---|
| [7f5566b] | 649 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
|
|---|
| [cda48b6] | 650 | output << indent << printLabels( (*i)->get_labels() );
|
|---|
| [7f5566b] | 651 | (*i)->accept( *this );
|
|---|
| [2b6c1e0] | 652 |
|
|---|
| [6c4ff37] | 653 | output << endl;
|
|---|
| [2b6c1e0] | 654 | if ( wantSpacing( *i ) ) {
|
|---|
| 655 | output << endl;
|
|---|
| 656 | }
|
|---|
| [51587aa] | 657 | }
|
|---|
| [71f4e4f] | 658 | cur_indent -= CodeGenerator::tabsize;
|
|---|
| [51587aa] | 659 |
|
|---|
| [cda48b6] | 660 | output << indent << "}";
|
|---|
| [51587aa] | 661 | }
|
|---|
| 662 |
|
|---|
| [6c4ff37] | 663 | void CodeGenerator::visit( ExprStmt *exprStmt ) {
|
|---|
| 664 | assert( exprStmt );
|
|---|
| [321a2481] | 665 | // cast the top-level expression to void to reduce gcc warnings.
|
|---|
| 666 | Expression * expr = new CastExpr( exprStmt->get_expr() );
|
|---|
| 667 | expr->accept( *this );
|
|---|
| 668 | output << ";";
|
|---|
| [51587aa] | 669 | }
|
|---|
| 670 |
|
|---|
| [7f5566b] | 671 | void CodeGenerator::visit( AsmStmt *asmStmt ) {
|
|---|
| 672 | output << "asm ";
|
|---|
| 673 | if ( asmStmt->get_voltile() ) output << "volatile ";
|
|---|
| 674 | if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
|
|---|
| 675 | output << "( ";
|
|---|
| 676 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
|
|---|
| 677 | output << " : ";
|
|---|
| 678 | genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
|
|---|
| 679 | output << " : ";
|
|---|
| 680 | genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
|
|---|
| 681 | output << " : ";
|
|---|
| 682 | genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
|
|---|
| 683 | if ( ! asmStmt->get_gotolabels().empty() ) {
|
|---|
| 684 | output << " : ";
|
|---|
| 685 | for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
|
|---|
| 686 | output << *begin++;
|
|---|
| 687 | if ( begin == asmStmt->get_gotolabels().end() ) break;
|
|---|
| 688 | output << ", ";
|
|---|
| 689 | } // for
|
|---|
| 690 | } // if
|
|---|
| 691 | output << " );" ;
|
|---|
| 692 | }
|
|---|
| 693 |
|
|---|
| [6c4ff37] | 694 | void CodeGenerator::visit( IfStmt *ifStmt ) {
|
|---|
| [7f5566b] | 695 | output << "if ( ";
|
|---|
| 696 | ifStmt->get_condition()->accept( *this );
|
|---|
| 697 | output << " ) ";
|
|---|
| [51587aa] | 698 |
|
|---|
| [7f5566b] | 699 | ifStmt->get_thenPart()->accept( *this );
|
|---|
| [51587aa] | 700 |
|
|---|
| 701 | if ( ifStmt->get_elsePart() != 0) {
|
|---|
| [2b6c1e0] | 702 | output << " else ";
|
|---|
| [7f5566b] | 703 | ifStmt->get_elsePart()->accept( *this );
|
|---|
| [51587aa] | 704 | } // if
|
|---|
| 705 | }
|
|---|
| 706 |
|
|---|
| [6c4ff37] | 707 | void CodeGenerator::visit( SwitchStmt *switchStmt ) {
|
|---|
| [7f5566b] | 708 | output << "switch ( " ;
|
|---|
| 709 | switchStmt->get_condition()->accept( *this );
|
|---|
| 710 | output << " ) ";
|
|---|
| [71f4e4f] | 711 |
|
|---|
| [2b6c1e0] | 712 | output << "{" << std::endl;
|
|---|
| [6c4ff37] | 713 | cur_indent += CodeGenerator::tabsize;
|
|---|
| [51587aa] | 714 |
|
|---|
| [6c4ff37] | 715 | acceptAll( switchStmt->get_branches(), *this );
|
|---|
| [51587aa] | 716 |
|
|---|
| [6c4ff37] | 717 | cur_indent -= CodeGenerator::tabsize;
|
|---|
| [51587aa] | 718 |
|
|---|
| [cda48b6] | 719 | output << indent << "}";
|
|---|
| [51587aa] | 720 | }
|
|---|
| 721 |
|
|---|
| [6c4ff37] | 722 | void CodeGenerator::visit( CaseStmt *caseStmt ) {
|
|---|
| [cda48b6] | 723 | output << indent;
|
|---|
| [eb3261f] | 724 | if ( caseStmt->isDefault()) {
|
|---|
| [2b6c1e0] | 725 | output << "default";
|
|---|
| [eb3261f] | 726 | } else {
|
|---|
| [2b6c1e0] | 727 | output << "case ";
|
|---|
| [7f5566b] | 728 | caseStmt->get_condition()->accept( *this );
|
|---|
| [17cd4eb] | 729 | } // if
|
|---|
| [6c4ff37] | 730 | output << ":\n";
|
|---|
| [71f4e4f] | 731 |
|
|---|
| [51587aa] | 732 | std::list<Statement *> sts = caseStmt->get_statements();
|
|---|
| 733 |
|
|---|
| [6c4ff37] | 734 | cur_indent += CodeGenerator::tabsize;
|
|---|
| [51587aa] | 735 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
|
|---|
| [cda48b6] | 736 | output << indent << printLabels( (*i)->get_labels() ) ;
|
|---|
| [7f5566b] | 737 | (*i)->accept( *this );
|
|---|
| [6c4ff37] | 738 | output << endl;
|
|---|
| [51587aa] | 739 | }
|
|---|
| [6c4ff37] | 740 | cur_indent -= CodeGenerator::tabsize;
|
|---|
| [51587aa] | 741 | }
|
|---|
| 742 |
|
|---|
| [6c4ff37] | 743 | void CodeGenerator::visit( BranchStmt *branchStmt ) {
|
|---|
| [51587aa] | 744 | switch ( branchStmt->get_type()) {
|
|---|
| 745 | case BranchStmt::Goto:
|
|---|
| 746 | if ( ! branchStmt->get_target().empty() )
|
|---|
| [6c4ff37] | 747 | output << "goto " << branchStmt->get_target();
|
|---|
| [71f4e4f] | 748 | else {
|
|---|
| [51587aa] | 749 | if ( branchStmt->get_computedTarget() != 0 ) {
|
|---|
| [6c4ff37] | 750 | output << "goto *";
|
|---|
| [51587aa] | 751 | branchStmt->get_computedTarget()->accept( *this );
|
|---|
| 752 | } // if
|
|---|
| 753 | } // if
|
|---|
| 754 | break;
|
|---|
| 755 | case BranchStmt::Break:
|
|---|
| [6c4ff37] | 756 | output << "break";
|
|---|
| [51587aa] | 757 | break;
|
|---|
| 758 | case BranchStmt::Continue:
|
|---|
| [6c4ff37] | 759 | output << "continue";
|
|---|
| [51587aa] | 760 | break;
|
|---|
| 761 | }
|
|---|
| [2b6c1e0] | 762 | output << ";";
|
|---|
| [51587aa] | 763 | }
|
|---|
| 764 |
|
|---|
| 765 |
|
|---|
| [6c4ff37] | 766 | void CodeGenerator::visit( ReturnStmt *returnStmt ) {
|
|---|
| 767 | output << "return ";
|
|---|
| [4b2589a] | 768 | maybeAccept( returnStmt->get_expr(), *this );
|
|---|
| [6c4ff37] | 769 | output << ";";
|
|---|
| [51587aa] | 770 | }
|
|---|
| 771 |
|
|---|
| [6c4ff37] | 772 | void CodeGenerator::visit( WhileStmt *whileStmt ) {
|
|---|
| [321a2481] | 773 | if ( whileStmt->get_isDoWhile() ) {
|
|---|
| [6c4ff37] | 774 | output << "do" ;
|
|---|
| [321a2481] | 775 | } else {
|
|---|
| [6c4ff37] | 776 | output << "while (" ;
|
|---|
| [7f5566b] | 777 | whileStmt->get_condition()->accept( *this );
|
|---|
| [6c4ff37] | 778 | output << ")";
|
|---|
| [51587aa] | 779 | } // if
|
|---|
| [2b6c1e0] | 780 | output << " ";
|
|---|
| [51587aa] | 781 |
|
|---|
| [2b6c1e0] | 782 | output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
|
|---|
| [51587aa] | 783 | whileStmt->get_body()->accept( *this );
|
|---|
| 784 |
|
|---|
| [cda48b6] | 785 | output << indent;
|
|---|
| [51587aa] | 786 |
|
|---|
| 787 | if ( whileStmt->get_isDoWhile() ) {
|
|---|
| [6c4ff37] | 788 | output << " while (" ;
|
|---|
| [7f5566b] | 789 | whileStmt->get_condition()->accept( *this );
|
|---|
| [6c4ff37] | 790 | output << ");";
|
|---|
| [51587aa] | 791 | } // if
|
|---|
| 792 | }
|
|---|
| 793 |
|
|---|
| [6c4ff37] | 794 | void CodeGenerator::visit( ForStmt *forStmt ) {
|
|---|
| [8e9cbb2] | 795 | // initialization is always hoisted, so don't bother doing anything with that
|
|---|
| [145f1fc] | 796 | output << "for (;";
|
|---|
| [51587aa] | 797 |
|
|---|
| [321a2481] | 798 | if ( forStmt->get_condition() != 0 ) {
|
|---|
| [51587aa] | 799 | forStmt->get_condition()->accept( *this );
|
|---|
| [321a2481] | 800 | }
|
|---|
| [6c4ff37] | 801 | output << ";";
|
|---|
| [51587aa] | 802 |
|
|---|
| [321a2481] | 803 | if ( forStmt->get_increment() != 0 ) {
|
|---|
| 804 | // cast the top-level expression to void to reduce gcc warnings.
|
|---|
| 805 | Expression * expr = new CastExpr( forStmt->get_increment() );
|
|---|
| 806 | expr->accept( *this );
|
|---|
| 807 | }
|
|---|
| [2b6c1e0] | 808 | output << ") ";
|
|---|
| [51587aa] | 809 |
|
|---|
| 810 | if ( forStmt->get_body() != 0 ) {
|
|---|
| [2b6c1e0] | 811 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
|
|---|
| [51587aa] | 812 | forStmt->get_body()->accept( *this );
|
|---|
| 813 | } // if
|
|---|
| 814 | }
|
|---|
| 815 |
|
|---|
| [6c4ff37] | 816 | void CodeGenerator::visit( NullStmt *nullStmt ) {
|
|---|
| [cda48b6] | 817 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
|
|---|
| [6c4ff37] | 818 | output << "/* null statement */ ;";
|
|---|
| [51587aa] | 819 | }
|
|---|
| 820 |
|
|---|
| [6c4ff37] | 821 | void CodeGenerator::visit( DeclStmt *declStmt ) {
|
|---|
| [51587aa] | 822 | declStmt->get_decl()->accept( *this );
|
|---|
| [71f4e4f] | 823 |
|
|---|
| [51587aa] | 824 | if ( doSemicolon( declStmt->get_decl() ) ) {
|
|---|
| [6c4ff37] | 825 | output << ";";
|
|---|
| [51587aa] | 826 | } // if
|
|---|
| 827 | }
|
|---|
| 828 |
|
|---|
| [6c4ff37] | 829 | void CodeGenerator::handleStorageClass( Declaration *decl ) {
|
|---|
| [51587aa] | 830 | switch ( decl->get_storageClass() ) {
|
|---|
| [68cd1ce] | 831 | case DeclarationNode::Extern:
|
|---|
| [6c4ff37] | 832 | output << "extern ";
|
|---|
| [51587aa] | 833 | break;
|
|---|
| [68cd1ce] | 834 | case DeclarationNode::Static:
|
|---|
| [6c4ff37] | 835 | output << "static ";
|
|---|
| [51587aa] | 836 | break;
|
|---|
| [68cd1ce] | 837 | case DeclarationNode::Auto:
|
|---|
| [51587aa] | 838 | // silently drop storage class
|
|---|
| 839 | break;
|
|---|
| [68cd1ce] | 840 | case DeclarationNode::Register:
|
|---|
| [6c4ff37] | 841 | output << "register ";
|
|---|
| [51587aa] | 842 | break;
|
|---|
| [68cd1ce] | 843 | case DeclarationNode::Inline:
|
|---|
| [de62360d] | 844 | output << "inline ";
|
|---|
| [f38c8d9] | 845 | break;
|
|---|
| [68cd1ce] | 846 | case DeclarationNode::Fortran:
|
|---|
| 847 | output << "fortran ";
|
|---|
| 848 | break;
|
|---|
| 849 | case DeclarationNode::Noreturn:
|
|---|
| 850 | output << "_Noreturn ";
|
|---|
| 851 | break;
|
|---|
| 852 | case DeclarationNode::Threadlocal:
|
|---|
| 853 | output << "_Thread_local ";
|
|---|
| 854 | break;
|
|---|
| 855 | case DeclarationNode::NoStorageClass:
|
|---|
| [f38c8d9] | 856 | break;
|
|---|
| [843054c2] | 857 | } // switch
|
|---|
| [51587aa] | 858 | }
|
|---|
| [51b73452] | 859 | } // namespace CodeGen
|
|---|
| [51587aa] | 860 |
|
|---|
| 861 | // Local Variables: //
|
|---|
| 862 | // tab-width: 4 //
|
|---|
| 863 | // mode: c++ //
|
|---|
| 864 | // compile-command: "make install" //
|
|---|
| 865 | // End: //
|
|---|