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