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