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