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