[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 | //
|
---|
[6c4ff37] | 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
|
---|
| 12 | // Last Modified On : Wed Jul 15 14:47:42 2015
|
---|
| 13 | // Update Count : 177
|
---|
[51587aa] | 14 | //
|
---|
| 15 |
|
---|
[51b73452] | 16 | #include <algorithm>
|
---|
| 17 | #include <iostream>
|
---|
| 18 | #include <cassert>
|
---|
| 19 | #include <list>
|
---|
| 20 |
|
---|
[68cd1ce] | 21 | #include "Parser/ParseNode.h"
|
---|
| 22 |
|
---|
[51b73452] | 23 | #include "SynTree/Type.h"
|
---|
| 24 | #include "SynTree/Expression.h"
|
---|
| 25 | #include "SynTree/Initializer.h"
|
---|
[68cd1ce] | 26 | #include "SynTree/Statement.h"
|
---|
[51b73452] | 27 |
|
---|
| 28 | #include "utility.h"
|
---|
| 29 | #include "UnimplementedError.h"
|
---|
| 30 |
|
---|
[a61fea9a] | 31 | #include "CodeGenerator.h"
|
---|
[51b73452] | 32 | #include "OperatorTable.h"
|
---|
| 33 | #include "GenType.h"
|
---|
| 34 |
|
---|
| 35 | using namespace std;
|
---|
| 36 |
|
---|
| 37 | namespace CodeGen {
|
---|
[6c4ff37] | 38 | int CodeGenerator::tabsize = 4;
|
---|
[51587aa] | 39 |
|
---|
[145f1fc] | 40 | // the kinds of statements that would ideally be followed by whitespace
|
---|
[2b6c1e0] | 41 | bool wantSpacing( Statement * stmt) {
|
---|
| 42 | return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
|
---|
| 43 | dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[cda48b6] | 46 | ostream & CodeGenerator::Indenter::operator()( ostream & output ) {
|
---|
| 47 | return output << string( cg.cur_indent, ' ' );
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) {
|
---|
| 51 | return indent( output );
|
---|
| 52 | }
|
---|
[51587aa] | 53 |
|
---|
[cda48b6] | 54 | CodeGenerator::CodeGenerator( std::ostream &os ) : indent(*this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
|
---|
| 55 |
|
---|
| 56 | CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
|
---|
| 57 | : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
|
---|
[6c4ff37] | 58 | //output << std::string( init );
|
---|
[51587aa] | 59 | }
|
---|
| 60 |
|
---|
[cda48b6] | 61 | CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
|
---|
| 62 | : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
|
---|
[6c4ff37] | 63 | //output << std::string( init );
|
---|
[51587aa] | 64 | }
|
---|
| 65 |
|
---|
| 66 | string mangleName( DeclarationWithType *decl ) {
|
---|
| 67 | if ( decl->get_mangleName() != "" ) {
|
---|
| 68 | return decl->get_mangleName();
|
---|
| 69 | } else {
|
---|
| 70 | return decl->get_name();
|
---|
| 71 | } // if
|
---|
| 72 | }
|
---|
[94b4364] | 73 |
|
---|
[51587aa] | 74 | //*** Declarations
|
---|
[6c4ff37] | 75 | void CodeGenerator::visit( FunctionDecl *functionDecl ) {
|
---|
[51587aa] | 76 | handleStorageClass( functionDecl );
|
---|
[f38c8d9] | 77 | if ( functionDecl->get_isInline() ) {
|
---|
[6c4ff37] | 78 | output << "inline ";
|
---|
[f38c8d9] | 79 | } // if
|
---|
[de62360d] | 80 | if ( functionDecl->get_isNoreturn() ) {
|
---|
| 81 | output << "_Noreturn ";
|
---|
| 82 | } // if
|
---|
[6c4ff37] | 83 | output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
|
---|
[51587aa] | 84 |
|
---|
| 85 | // how to get this to the Functype?
|
---|
| 86 | std::list< Declaration * > olds = functionDecl->get_oldDecls();
|
---|
| 87 | if ( ! olds.empty() ) {
|
---|
[6c4ff37] | 88 | output << " /* function has old declaration */";
|
---|
[51587aa] | 89 | } // if
|
---|
| 90 |
|
---|
| 91 | // acceptAll( functionDecl->get_oldDecls(), *this );
|
---|
| 92 | if ( functionDecl->get_statements() ) {
|
---|
| 93 | functionDecl->get_statements()->accept(*this );
|
---|
| 94 | } // if
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[6c4ff37] | 97 | void CodeGenerator::visit( ObjectDecl *objectDecl ) {
|
---|
[51587aa] | 98 | handleStorageClass( objectDecl );
|
---|
[6c4ff37] | 99 | output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
|
---|
[51587aa] | 100 |
|
---|
| 101 | if ( objectDecl->get_init() ) {
|
---|
[6c4ff37] | 102 | output << " = ";
|
---|
[51587aa] | 103 | objectDecl->get_init()->accept( *this );
|
---|
| 104 | } // if
|
---|
| 105 | if ( objectDecl->get_bitfieldWidth() ) {
|
---|
[6c4ff37] | 106 | output << ":";
|
---|
[51587aa] | 107 | objectDecl->get_bitfieldWidth()->accept( *this );
|
---|
| 108 | } // if
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[6c4ff37] | 111 | void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
|
---|
[51587aa] | 112 | if ( aggDecl->get_name() != "" )
|
---|
[6c4ff37] | 113 | output << aggDecl->get_name();
|
---|
[51587aa] | 114 |
|
---|
| 115 | std::list< Declaration * > &memb = aggDecl->get_members();
|
---|
| 116 |
|
---|
| 117 | if ( ! memb.empty() ) {
|
---|
[94b4364] | 118 | output << " {" << endl;
|
---|
[51587aa] | 119 |
|
---|
[6c4ff37] | 120 | cur_indent += CodeGenerator::tabsize;
|
---|
[51587aa] | 121 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
|
---|
[cda48b6] | 122 | output << indent;
|
---|
[51587aa] | 123 | (*i)->accept(*this );
|
---|
[6c4ff37] | 124 | output << ";" << endl;
|
---|
[51587aa] | 125 | }
|
---|
| 126 |
|
---|
[6c4ff37] | 127 | cur_indent -= CodeGenerator::tabsize;
|
---|
[51587aa] | 128 |
|
---|
[cda48b6] | 129 | output << indent << "}";
|
---|
[17cd4eb] | 130 | } // if
|
---|
[51587aa] | 131 | }
|
---|
[17cd4eb] | 132 |
|
---|
[6c4ff37] | 133 | void CodeGenerator::visit( StructDecl *structDecl ) {
|
---|
| 134 | output << "struct ";
|
---|
[51587aa] | 135 | handleAggregate( structDecl );
|
---|
| 136 | }
|
---|
[17cd4eb] | 137 |
|
---|
[6c4ff37] | 138 | void CodeGenerator::visit( UnionDecl *aggregateDecl ) {
|
---|
| 139 | output << "union ";
|
---|
[51587aa] | 140 | handleAggregate( aggregateDecl );
|
---|
| 141 | }
|
---|
[51b73452] | 142 |
|
---|
[6c4ff37] | 143 | void CodeGenerator::visit( EnumDecl *aggDecl ) {
|
---|
| 144 | output << "enum ";
|
---|
[51587aa] | 145 |
|
---|
| 146 | if ( aggDecl->get_name() != "" )
|
---|
[6c4ff37] | 147 | output << aggDecl->get_name();
|
---|
[51587aa] | 148 |
|
---|
| 149 | std::list< Declaration* > &memb = aggDecl->get_members();
|
---|
| 150 |
|
---|
| 151 | if ( ! memb.empty() ) {
|
---|
[cda48b6] | 152 | output << " {" << endl;
|
---|
[51587aa] | 153 |
|
---|
[6c4ff37] | 154 | cur_indent += CodeGenerator::tabsize;
|
---|
[51587aa] | 155 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
|
---|
| 156 | ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
|
---|
| 157 | assert( obj );
|
---|
[cda48b6] | 158 | output << indent << mangleName( obj );
|
---|
[51587aa] | 159 | if ( obj->get_init() ) {
|
---|
[6c4ff37] | 160 | output << " = ";
|
---|
[51587aa] | 161 | obj->get_init()->accept(*this );
|
---|
| 162 | } // if
|
---|
[6c4ff37] | 163 | output << "," << endl;
|
---|
[51587aa] | 164 | } // for
|
---|
| 165 |
|
---|
[6c4ff37] | 166 | cur_indent -= CodeGenerator::tabsize;
|
---|
[51587aa] | 167 |
|
---|
[cda48b6] | 168 | output << indent << "}";
|
---|
[51587aa] | 169 | } // if
|
---|
| 170 | }
|
---|
[51b73452] | 171 |
|
---|
[6c4ff37] | 172 | void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
|
---|
[51587aa] | 173 |
|
---|
[6c4ff37] | 174 | void CodeGenerator::visit( TypedefDecl *typeDecl ) {
|
---|
| 175 | output << "typedef ";
|
---|
| 176 | output << genType( typeDecl->get_base(), typeDecl->get_name() );
|
---|
[51587aa] | 177 | }
|
---|
[51b73452] | 178 |
|
---|
[6c4ff37] | 179 | void CodeGenerator::visit( TypeDecl *typeDecl ) {
|
---|
[51587aa] | 180 | // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
|
---|
| 181 | // still to be done
|
---|
[6c4ff37] | 182 | output << "extern unsigned long " << typeDecl->get_name();
|
---|
[51587aa] | 183 | if ( typeDecl->get_base() ) {
|
---|
[6c4ff37] | 184 | output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
|
---|
[51587aa] | 185 | } // if
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[6c4ff37] | 188 | void CodeGenerator::visit( SingleInit *init ) {
|
---|
[51587aa] | 189 | init->get_value()->accept( *this );
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[6c4ff37] | 192 | void CodeGenerator::visit( ListInit *init ) {
|
---|
| 193 | output << "{ ";
|
---|
[51587aa] | 194 | genCommaList( init->begin_initializers(), init->end_initializers() );
|
---|
[6c4ff37] | 195 | output << " }";
|
---|
[51587aa] | 196 | }
|
---|
| 197 |
|
---|
[6c4ff37] | 198 | void CodeGenerator::visit( Constant *constant ) {
|
---|
| 199 | output << constant->get_value() ;
|
---|
[51587aa] | 200 | }
|
---|
| 201 |
|
---|
| 202 | //*** Expressions
|
---|
[6c4ff37] | 203 | void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
|
---|
[51587aa] | 204 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
|
---|
| 205 | OperatorInfo opInfo;
|
---|
| 206 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
|
---|
| 207 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
|
---|
| 208 | switch ( opInfo.type ) {
|
---|
| 209 | case OT_PREFIXASSIGN:
|
---|
| 210 | case OT_POSTFIXASSIGN:
|
---|
| 211 | case OT_INFIXASSIGN:
|
---|
| 212 | {
|
---|
| 213 | assert( arg != applicationExpr->get_args().end() );
|
---|
| 214 | if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
|
---|
| 215 |
|
---|
| 216 | *arg = addrExpr->get_arg();
|
---|
| 217 | } else {
|
---|
| 218 | UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
| 219 | newExpr->get_args().push_back( *arg );
|
---|
| 220 | *arg = newExpr;
|
---|
| 221 | } // if
|
---|
| 222 | break;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | default:
|
---|
| 226 | // do nothing
|
---|
| 227 | ;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | switch ( opInfo.type ) {
|
---|
| 231 | case OT_INDEX:
|
---|
| 232 | assert( applicationExpr->get_args().size() == 2 );
|
---|
| 233 | (*arg++)->accept( *this );
|
---|
[6c4ff37] | 234 | output << "[";
|
---|
[51587aa] | 235 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 236 | output << "]";
|
---|
[51587aa] | 237 | break;
|
---|
| 238 |
|
---|
| 239 | case OT_CALL:
|
---|
[2794fff] | 240 | case OT_CTOR:
|
---|
| 241 | case OT_DTOR:
|
---|
| 242 | // there are no intrinsic definitions of the function call operator or constructors or destructors
|
---|
[51587aa] | 243 | assert( false );
|
---|
| 244 | break;
|
---|
| 245 |
|
---|
| 246 | case OT_PREFIX:
|
---|
| 247 | case OT_PREFIXASSIGN:
|
---|
| 248 | assert( applicationExpr->get_args().size() == 1 );
|
---|
[6c4ff37] | 249 | output << "(";
|
---|
| 250 | output << opInfo.symbol;
|
---|
[51587aa] | 251 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 252 | output << ")";
|
---|
[51587aa] | 253 | break;
|
---|
| 254 |
|
---|
| 255 | case OT_POSTFIX:
|
---|
| 256 | case OT_POSTFIXASSIGN:
|
---|
| 257 | assert( applicationExpr->get_args().size() == 1 );
|
---|
| 258 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 259 | output << opInfo.symbol;
|
---|
[51587aa] | 260 | break;
|
---|
| 261 |
|
---|
| 262 | case OT_INFIX:
|
---|
| 263 | case OT_INFIXASSIGN:
|
---|
| 264 | assert( applicationExpr->get_args().size() == 2 );
|
---|
[6c4ff37] | 265 | output << "(";
|
---|
[51587aa] | 266 | (*arg++)->accept( *this );
|
---|
[6c4ff37] | 267 | output << opInfo.symbol;
|
---|
[51587aa] | 268 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 269 | output << ")";
|
---|
[51587aa] | 270 | break;
|
---|
| 271 |
|
---|
| 272 | case OT_CONSTANT:
|
---|
[721f17a] | 273 | case OT_LABELADDRESS:
|
---|
| 274 | // there are no intrinsic definitions of 0/1 or label addresses as functions
|
---|
[51587aa] | 275 | assert( false );
|
---|
| 276 | }
|
---|
[17cd4eb] | 277 | } else {
|
---|
[51587aa] | 278 | varExpr->accept( *this );
|
---|
[6c4ff37] | 279 | output << "(";
|
---|
[51587aa] | 280 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
---|
[6c4ff37] | 281 | output << ")";
|
---|
[17cd4eb] | 282 | } // if
|
---|
[51587aa] | 283 | } else {
|
---|
| 284 | applicationExpr->get_function()->accept( *this );
|
---|
[6c4ff37] | 285 | output << "(";
|
---|
[51587aa] | 286 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
|
---|
[6c4ff37] | 287 | output << ")";
|
---|
[51587aa] | 288 | } // if
|
---|
| 289 | }
|
---|
[51b73452] | 290 |
|
---|
[6c4ff37] | 291 | void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
|
---|
[51587aa] | 292 | if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
|
---|
| 293 | OperatorInfo opInfo;
|
---|
| 294 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
|
---|
| 295 | std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
|
---|
| 296 | switch ( opInfo.type ) {
|
---|
| 297 | case OT_INDEX:
|
---|
| 298 | assert( untypedExpr->get_args().size() == 2 );
|
---|
| 299 | (*arg++)->accept( *this );
|
---|
[6c4ff37] | 300 | output << "[";
|
---|
[51587aa] | 301 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 302 | output << "]";
|
---|
[51587aa] | 303 | break;
|
---|
| 304 |
|
---|
| 305 | case OT_CALL:
|
---|
| 306 | assert( false );
|
---|
| 307 | break;
|
---|
| 308 |
|
---|
| 309 | case OT_PREFIX:
|
---|
| 310 | case OT_PREFIXASSIGN:
|
---|
[de62360d] | 311 | case OT_LABELADDRESS:
|
---|
[51587aa] | 312 | assert( untypedExpr->get_args().size() == 1 );
|
---|
[6c4ff37] | 313 | output << "(";
|
---|
| 314 | output << opInfo.symbol;
|
---|
[51587aa] | 315 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 316 | output << ")";
|
---|
[51587aa] | 317 | break;
|
---|
| 318 |
|
---|
| 319 | case OT_POSTFIX:
|
---|
| 320 | case OT_POSTFIXASSIGN:
|
---|
| 321 | assert( untypedExpr->get_args().size() == 1 );
|
---|
| 322 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 323 | output << opInfo.symbol;
|
---|
[51587aa] | 324 | break;
|
---|
[51b73452] | 325 |
|
---|
[51587aa] | 326 | case OT_INFIX:
|
---|
| 327 | case OT_INFIXASSIGN:
|
---|
| 328 | assert( untypedExpr->get_args().size() == 2 );
|
---|
[6c4ff37] | 329 | output << "(";
|
---|
[51587aa] | 330 | (*arg++)->accept( *this );
|
---|
[6c4ff37] | 331 | output << opInfo.symbol;
|
---|
[51587aa] | 332 | (*arg)->accept( *this );
|
---|
[6c4ff37] | 333 | output << ")";
|
---|
[51587aa] | 334 | break;
|
---|
[de62360d] | 335 |
|
---|
[51587aa] | 336 | case OT_CONSTANT:
|
---|
| 337 | // there are no intrinsic definitions of 0 or 1 as functions
|
---|
| 338 | assert( false );
|
---|
| 339 | }
|
---|
| 340 | } else {
|
---|
| 341 | nameExpr->accept( *this );
|
---|
[6c4ff37] | 342 | output << "(";
|
---|
[51587aa] | 343 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
|
---|
[6c4ff37] | 344 | output << ")";
|
---|
[51587aa] | 345 | } // if
|
---|
| 346 | } else {
|
---|
| 347 | untypedExpr->get_function()->accept( *this );
|
---|
[6c4ff37] | 348 | output << "(";
|
---|
[51587aa] | 349 | genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
|
---|
[6c4ff37] | 350 | output << ")";
|
---|
[51587aa] | 351 | } // if
|
---|
| 352 | }
|
---|
[51b73452] | 353 |
|
---|
[6c4ff37] | 354 | void CodeGenerator::visit( NameExpr *nameExpr ) {
|
---|
[51587aa] | 355 | OperatorInfo opInfo;
|
---|
| 356 | if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
|
---|
| 357 | assert( opInfo.type == OT_CONSTANT );
|
---|
[6c4ff37] | 358 | output << opInfo.symbol;
|
---|
[51587aa] | 359 | } else {
|
---|
[6c4ff37] | 360 | output << nameExpr->get_name();
|
---|
[51587aa] | 361 | } // if
|
---|
| 362 | }
|
---|
[51b73452] | 363 |
|
---|
[6c4ff37] | 364 | void CodeGenerator::visit( AddressExpr *addressExpr ) {
|
---|
| 365 | output << "(&";
|
---|
[51587aa] | 366 | // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
|
---|
| 367 | if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
|
---|
[6c4ff37] | 368 | output << mangleName( variableExpr->get_var() );
|
---|
[51587aa] | 369 | } else {
|
---|
| 370 | addressExpr->get_arg()->accept( *this );
|
---|
| 371 | } // if
|
---|
[6c4ff37] | 372 | output << ")";
|
---|
[51587aa] | 373 | }
|
---|
| 374 |
|
---|
[6c4ff37] | 375 | void CodeGenerator::visit( CastExpr *castExpr ) {
|
---|
| 376 | output << "((";
|
---|
[51587aa] | 377 | if ( castExpr->get_results().empty() ) {
|
---|
[6c4ff37] | 378 | output << "void" ;
|
---|
[51587aa] | 379 | } else {
|
---|
[6c4ff37] | 380 | output << genType( castExpr->get_results().front(), "" );
|
---|
[51587aa] | 381 | } // if
|
---|
[6c4ff37] | 382 | output << ")";
|
---|
[51587aa] | 383 | castExpr->get_arg()->accept( *this );
|
---|
[6c4ff37] | 384 | output << ")";
|
---|
[51587aa] | 385 | }
|
---|
[51b73452] | 386 |
|
---|
[6c4ff37] | 387 | void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
|
---|
[51587aa] | 388 | assert( false );
|
---|
| 389 | }
|
---|
[51b73452] | 390 |
|
---|
[6c4ff37] | 391 | void CodeGenerator::visit( MemberExpr *memberExpr ) {
|
---|
[51587aa] | 392 | memberExpr->get_aggregate()->accept( *this );
|
---|
[6c4ff37] | 393 | output << "." << mangleName( memberExpr->get_member() );
|
---|
[51587aa] | 394 | }
|
---|
[51b73452] | 395 |
|
---|
[6c4ff37] | 396 | void CodeGenerator::visit( VariableExpr *variableExpr ) {
|
---|
[51587aa] | 397 | OperatorInfo opInfo;
|
---|
| 398 | if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
|
---|
[6c4ff37] | 399 | output << opInfo.symbol;
|
---|
[51587aa] | 400 | } else {
|
---|
[6c4ff37] | 401 | output << mangleName( variableExpr->get_var() );
|
---|
[51587aa] | 402 | } // if
|
---|
| 403 | }
|
---|
[51b73452] | 404 |
|
---|
[6c4ff37] | 405 | void CodeGenerator::visit( ConstantExpr *constantExpr ) {
|
---|
[51587aa] | 406 | assert( constantExpr->get_constant() );
|
---|
| 407 | constantExpr->get_constant()->accept( *this );
|
---|
| 408 | }
|
---|
[51b73452] | 409 |
|
---|
[6c4ff37] | 410 | void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
|
---|
| 411 | output << "sizeof(";
|
---|
[51587aa] | 412 | if ( sizeofExpr->get_isType() ) {
|
---|
[6c4ff37] | 413 | output << genType( sizeofExpr->get_type(), "" );
|
---|
[51587aa] | 414 | } else {
|
---|
| 415 | sizeofExpr->get_expr()->accept( *this );
|
---|
| 416 | } // if
|
---|
[6c4ff37] | 417 | output << ")";
|
---|
[51587aa] | 418 | }
|
---|
[51b73452] | 419 |
|
---|
[6c4ff37] | 420 | void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
|
---|
| 421 | output << "(";
|
---|
[51587aa] | 422 | logicalExpr->get_arg1()->accept( *this );
|
---|
| 423 | if ( logicalExpr->get_isAnd() ) {
|
---|
[6c4ff37] | 424 | output << " && ";
|
---|
[51587aa] | 425 | } else {
|
---|
[6c4ff37] | 426 | output << " || ";
|
---|
[51587aa] | 427 | } // if
|
---|
| 428 | logicalExpr->get_arg2()->accept( *this );
|
---|
[6c4ff37] | 429 | output << ")";
|
---|
[51587aa] | 430 | }
|
---|
[51b73452] | 431 |
|
---|
[6c4ff37] | 432 | void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
|
---|
| 433 | output << "(";
|
---|
[51587aa] | 434 | conditionalExpr->get_arg1()->accept( *this );
|
---|
[6c4ff37] | 435 | output << " ? ";
|
---|
[51587aa] | 436 | conditionalExpr->get_arg2()->accept( *this );
|
---|
[6c4ff37] | 437 | output << " : ";
|
---|
[51587aa] | 438 | conditionalExpr->get_arg3()->accept( *this );
|
---|
[6c4ff37] | 439 | output << ")";
|
---|
[51587aa] | 440 | }
|
---|
[51b73452] | 441 |
|
---|
[6c4ff37] | 442 | void CodeGenerator::visit( CommaExpr *commaExpr ) {
|
---|
| 443 | output << "(";
|
---|
[51587aa] | 444 | commaExpr->get_arg1()->accept( *this );
|
---|
[6c4ff37] | 445 | output << " , ";
|
---|
[51587aa] | 446 | commaExpr->get_arg2()->accept( *this );
|
---|
[6c4ff37] | 447 | output << ")";
|
---|
[51587aa] | 448 | }
|
---|
[51b73452] | 449 |
|
---|
[6c4ff37] | 450 | void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
|
---|
[51b73452] | 451 |
|
---|
[6c4ff37] | 452 | void CodeGenerator::visit( TypeExpr *typeExpr ) {}
|
---|
[2b6c1e0] | 453 |
|
---|
[51587aa] | 454 | //*** Statements
|
---|
[6c4ff37] | 455 | void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
|
---|
[51587aa] | 456 | std::list<Statement*> ks = compoundStmt->get_kids();
|
---|
[2b6c1e0] | 457 | output << "{" << endl;
|
---|
[51587aa] | 458 |
|
---|
[2b6c1e0] | 459 | cur_indent += CodeGenerator::tabsize;
|
---|
[51587aa] | 460 |
|
---|
| 461 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++) {
|
---|
[cda48b6] | 462 | output << indent << printLabels( (*i)->get_labels() );
|
---|
[51587aa] | 463 | (*i)->accept(*this );
|
---|
[2b6c1e0] | 464 |
|
---|
[6c4ff37] | 465 | output << endl;
|
---|
[2b6c1e0] | 466 | if ( wantSpacing( *i ) ) {
|
---|
| 467 | output << endl;
|
---|
| 468 | }
|
---|
[51587aa] | 469 | }
|
---|
[6c4ff37] | 470 | cur_indent -= CodeGenerator::tabsize;
|
---|
[51587aa] | 471 |
|
---|
[cda48b6] | 472 | output << indent << "}";
|
---|
[51587aa] | 473 | }
|
---|
| 474 |
|
---|
[6c4ff37] | 475 | void CodeGenerator::visit( ExprStmt *exprStmt ) {
|
---|
| 476 | // I don't see why this check is necessary.
|
---|
| 477 | // If this starts to cause problems then put it back in,
|
---|
| 478 | // with an explanation
|
---|
| 479 | assert( exprStmt );
|
---|
| 480 |
|
---|
| 481 | // if ( exprStmt != 0 ) {
|
---|
| 482 | exprStmt->get_expr()->accept( *this );
|
---|
| 483 | output << ";" ;
|
---|
| 484 | // } // if
|
---|
[51587aa] | 485 | }
|
---|
| 486 |
|
---|
[6c4ff37] | 487 | void CodeGenerator::visit( IfStmt *ifStmt ) {
|
---|
| 488 | output << "if (";
|
---|
[51587aa] | 489 | ifStmt->get_condition()->accept(*this );
|
---|
[2b6c1e0] | 490 | output << ") ";
|
---|
[51587aa] | 491 |
|
---|
| 492 | ifStmt->get_thenPart()->accept(*this );
|
---|
| 493 |
|
---|
| 494 | if ( ifStmt->get_elsePart() != 0) {
|
---|
[2b6c1e0] | 495 | output << " else ";
|
---|
[51587aa] | 496 | ifStmt->get_elsePart()->accept(*this );
|
---|
| 497 | } // if
|
---|
| 498 | }
|
---|
| 499 |
|
---|
[6c4ff37] | 500 | void CodeGenerator::visit( SwitchStmt *switchStmt ) {
|
---|
| 501 | output << "switch (" ;
|
---|
[51587aa] | 502 | switchStmt->get_condition()->accept(*this );
|
---|
[2b6c1e0] | 503 | output << ") ";
|
---|
[6c4ff37] | 504 |
|
---|
[2b6c1e0] | 505 | output << "{" << std::endl;
|
---|
[6c4ff37] | 506 | cur_indent += CodeGenerator::tabsize;
|
---|
[51587aa] | 507 |
|
---|
[6c4ff37] | 508 | acceptAll( switchStmt->get_branches(), *this );
|
---|
[51587aa] | 509 |
|
---|
[6c4ff37] | 510 | cur_indent -= CodeGenerator::tabsize;
|
---|
[51587aa] | 511 |
|
---|
[cda48b6] | 512 | output << indent << "}";
|
---|
[51587aa] | 513 | }
|
---|
| 514 |
|
---|
[6c4ff37] | 515 | void CodeGenerator::visit( CaseStmt *caseStmt ) {
|
---|
[cda48b6] | 516 | output << indent;
|
---|
[eb3261f] | 517 | if ( caseStmt->isDefault()) {
|
---|
[2b6c1e0] | 518 | output << "default";
|
---|
[eb3261f] | 519 | } else {
|
---|
[2b6c1e0] | 520 | output << "case ";
|
---|
[51587aa] | 521 | caseStmt->get_condition()->accept(*this );
|
---|
[17cd4eb] | 522 | } // if
|
---|
[6c4ff37] | 523 | output << ":\n";
|
---|
| 524 |
|
---|
[51587aa] | 525 | std::list<Statement *> sts = caseStmt->get_statements();
|
---|
| 526 |
|
---|
[6c4ff37] | 527 | cur_indent += CodeGenerator::tabsize;
|
---|
[51587aa] | 528 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
|
---|
[cda48b6] | 529 | output << indent << printLabels( (*i)->get_labels() ) ;
|
---|
[51587aa] | 530 | (*i)->accept(*this );
|
---|
[6c4ff37] | 531 | output << endl;
|
---|
[51587aa] | 532 | }
|
---|
[6c4ff37] | 533 | cur_indent -= CodeGenerator::tabsize;
|
---|
[51587aa] | 534 | }
|
---|
| 535 |
|
---|
[6c4ff37] | 536 | void CodeGenerator::visit( BranchStmt *branchStmt ) {
|
---|
[51587aa] | 537 | switch ( branchStmt->get_type()) {
|
---|
| 538 | case BranchStmt::Goto:
|
---|
| 539 | if ( ! branchStmt->get_target().empty() )
|
---|
[6c4ff37] | 540 | output << "goto " << branchStmt->get_target();
|
---|
[51587aa] | 541 | else {
|
---|
| 542 | if ( branchStmt->get_computedTarget() != 0 ) {
|
---|
[6c4ff37] | 543 | output << "goto *";
|
---|
[51587aa] | 544 | branchStmt->get_computedTarget()->accept( *this );
|
---|
| 545 | } // if
|
---|
| 546 | } // if
|
---|
| 547 | break;
|
---|
| 548 | case BranchStmt::Break:
|
---|
[6c4ff37] | 549 | output << "break";
|
---|
[51587aa] | 550 | break;
|
---|
| 551 | case BranchStmt::Continue:
|
---|
[6c4ff37] | 552 | output << "continue";
|
---|
[51587aa] | 553 | break;
|
---|
| 554 | }
|
---|
[2b6c1e0] | 555 | output << ";";
|
---|
[51587aa] | 556 | }
|
---|
| 557 |
|
---|
| 558 |
|
---|
[6c4ff37] | 559 | void CodeGenerator::visit( ReturnStmt *returnStmt ) {
|
---|
| 560 | output << "return ";
|
---|
[51587aa] | 561 |
|
---|
| 562 | // xxx -- check for null expression;
|
---|
| 563 | if ( returnStmt->get_expr() ) {
|
---|
| 564 | returnStmt->get_expr()->accept( *this );
|
---|
| 565 | } // if
|
---|
[6c4ff37] | 566 | output << ";";
|
---|
[51587aa] | 567 | }
|
---|
| 568 |
|
---|
[6c4ff37] | 569 | void CodeGenerator::visit( WhileStmt *whileStmt ) {
|
---|
[51587aa] | 570 | if ( whileStmt->get_isDoWhile() )
|
---|
[6c4ff37] | 571 | output << "do" ;
|
---|
[51587aa] | 572 | else {
|
---|
[6c4ff37] | 573 | output << "while (" ;
|
---|
[51587aa] | 574 | whileStmt->get_condition()->accept(*this );
|
---|
[6c4ff37] | 575 | output << ")";
|
---|
[51587aa] | 576 | } // if
|
---|
[2b6c1e0] | 577 | output << " ";
|
---|
[51587aa] | 578 |
|
---|
[2b6c1e0] | 579 | output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
|
---|
[51587aa] | 580 | whileStmt->get_body()->accept( *this );
|
---|
| 581 |
|
---|
[cda48b6] | 582 | output << indent;
|
---|
[51587aa] | 583 |
|
---|
| 584 | if ( whileStmt->get_isDoWhile() ) {
|
---|
[6c4ff37] | 585 | output << " while (" ;
|
---|
[51587aa] | 586 | whileStmt->get_condition()->accept(*this );
|
---|
[6c4ff37] | 587 | output << ");";
|
---|
[51587aa] | 588 | } // if
|
---|
| 589 | }
|
---|
| 590 |
|
---|
[6c4ff37] | 591 | void CodeGenerator::visit( ForStmt *forStmt ) {
|
---|
[145f1fc] | 592 | // initialization is always hoisted, so don't
|
---|
| 593 | // bother doing anything with that
|
---|
| 594 | output << "for (;";
|
---|
[51587aa] | 595 |
|
---|
| 596 | if ( forStmt->get_condition() != 0 )
|
---|
| 597 | forStmt->get_condition()->accept( *this );
|
---|
[6c4ff37] | 598 | output << ";";
|
---|
[51587aa] | 599 |
|
---|
| 600 | if ( forStmt->get_increment() != 0 )
|
---|
| 601 | forStmt->get_increment()->accept( *this );
|
---|
[2b6c1e0] | 602 | output << ") ";
|
---|
[51587aa] | 603 |
|
---|
| 604 | if ( forStmt->get_body() != 0 ) {
|
---|
[2b6c1e0] | 605 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
|
---|
[51587aa] | 606 | forStmt->get_body()->accept( *this );
|
---|
| 607 | } // if
|
---|
| 608 | }
|
---|
| 609 |
|
---|
[6c4ff37] | 610 | void CodeGenerator::visit( NullStmt *nullStmt ) {
|
---|
[cda48b6] | 611 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
|
---|
[6c4ff37] | 612 | output << "/* null statement */ ;";
|
---|
[51587aa] | 613 | }
|
---|
| 614 |
|
---|
[6c4ff37] | 615 | void CodeGenerator::visit( DeclStmt *declStmt ) {
|
---|
[51587aa] | 616 | declStmt->get_decl()->accept( *this );
|
---|
| 617 |
|
---|
| 618 | if ( doSemicolon( declStmt->get_decl() ) ) {
|
---|
[6c4ff37] | 619 | output << ";";
|
---|
[51587aa] | 620 | } // if
|
---|
| 621 | }
|
---|
| 622 |
|
---|
[6c4ff37] | 623 | std::string CodeGenerator::printLabels( std::list< Label > &l ) {
|
---|
[51587aa] | 624 | std::string str( "" );
|
---|
[6c4ff37] | 625 | l.unique(); // assumes a sorted list. Why not use set?
|
---|
[51587aa] | 626 |
|
---|
| 627 | for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
|
---|
| 628 | str += *i + ": ";
|
---|
| 629 |
|
---|
| 630 | return str;
|
---|
| 631 | }
|
---|
| 632 |
|
---|
[6c4ff37] | 633 | void CodeGenerator::handleStorageClass( Declaration *decl ) {
|
---|
[51587aa] | 634 | switch ( decl->get_storageClass() ) {
|
---|
[68cd1ce] | 635 | case DeclarationNode::Extern:
|
---|
[6c4ff37] | 636 | output << "extern ";
|
---|
[51587aa] | 637 | break;
|
---|
[68cd1ce] | 638 | case DeclarationNode::Static:
|
---|
[6c4ff37] | 639 | output << "static ";
|
---|
[51587aa] | 640 | break;
|
---|
[68cd1ce] | 641 | case DeclarationNode::Auto:
|
---|
[51587aa] | 642 | // silently drop storage class
|
---|
| 643 | break;
|
---|
[68cd1ce] | 644 | case DeclarationNode::Register:
|
---|
[6c4ff37] | 645 | output << "register ";
|
---|
[51587aa] | 646 | break;
|
---|
[68cd1ce] | 647 | case DeclarationNode::Inline:
|
---|
[de62360d] | 648 | output << "inline ";
|
---|
[f38c8d9] | 649 | break;
|
---|
[68cd1ce] | 650 | case DeclarationNode::Fortran:
|
---|
| 651 | output << "fortran ";
|
---|
| 652 | break;
|
---|
| 653 | case DeclarationNode::Noreturn:
|
---|
| 654 | output << "_Noreturn ";
|
---|
| 655 | break;
|
---|
| 656 | case DeclarationNode::Threadlocal:
|
---|
| 657 | output << "_Thread_local ";
|
---|
| 658 | break;
|
---|
| 659 | case DeclarationNode::NoStorageClass:
|
---|
[f38c8d9] | 660 | break;
|
---|
[843054c2] | 661 | } // switch
|
---|
[51587aa] | 662 | }
|
---|
[51b73452] | 663 | } // namespace CodeGen
|
---|
[51587aa] | 664 |
|
---|
| 665 | // Local Variables: //
|
---|
| 666 | // tab-width: 4 //
|
---|
| 667 | // mode: c++ //
|
---|
| 668 | // compile-command: "make install" //
|
---|
| 669 | // End: //
|
---|