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