[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 | // |
---|
[7baed7d] | 7 | // CodeGenerator.h -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[c850687] | 11 | // Last Modified By : Andrew Beach |
---|
[0dd18fd] | 12 | // Last Modified On : Fri Aug 18 15:40:00 2017 |
---|
| 13 | // Update Count : 56 |
---|
[51587aa] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[bf2438c] | 18 | #include <list> // for list |
---|
| 19 | #include <ostream> // for ostream, operator<< |
---|
| 20 | #include <string> // for string |
---|
[51b7345] | 21 | |
---|
[e6cee92] | 22 | #include "Common/Indenter.h" // for Indenter |
---|
[9857e8d] | 23 | #include "Common/PassVisitor.h" // for PassVisitor |
---|
[bf2438c] | 24 | #include "SynTree/Declaration.h" // for DeclarationWithType (ptr only), Fun... |
---|
| 25 | #include "SynTree/Visitor.h" // for Visitor |
---|
| 26 | #include "SynTree/SynTree.h" // for Visitor Nodes |
---|
[c850687] | 27 | |
---|
[51b7345] | 28 | namespace CodeGen { |
---|
[9857e8d] | 29 | struct CodeGenerator : public WithShortCircuiting, public WithVisitorRef<CodeGenerator> { |
---|
| 30 | static int tabsize; |
---|
[51587aa] | 31 | |
---|
[c850687] | 32 | CodeGenerator( std::ostream &os, bool pretty = false, bool genC = false, bool lineMarks = false ); |
---|
[6c4ff37] | 33 | CodeGenerator( std::ostream &os, std::string, int indent = 0, bool infun = false ); |
---|
| 34 | CodeGenerator( std::ostream &os, char *, int indent = 0, bool infun = false ); |
---|
[51587aa] | 35 | |
---|
[9857e8d] | 36 | //*** Turn off visit_children for all nodes |
---|
| 37 | void previsit( BaseSyntaxNode * ); |
---|
| 38 | |
---|
| 39 | //*** Error for unhandled node types |
---|
| 40 | void postvisit( BaseSyntaxNode * ); |
---|
| 41 | |
---|
[51587aa] | 42 | //*** Declaration |
---|
[9857e8d] | 43 | void postvisit( StructDecl * ); |
---|
| 44 | void postvisit( FunctionDecl * ); |
---|
| 45 | void postvisit( ObjectDecl * ); |
---|
| 46 | void postvisit( UnionDecl *aggregateDecl ); |
---|
| 47 | void postvisit( EnumDecl *aggregateDecl ); |
---|
| 48 | void postvisit( TraitDecl *aggregateDecl ); |
---|
| 49 | void postvisit( TypedefDecl *typeDecl ); |
---|
| 50 | void postvisit( TypeDecl *typeDecl ); |
---|
[51587aa] | 51 | |
---|
| 52 | //*** Initializer |
---|
[9857e8d] | 53 | void postvisit( Designation * ); |
---|
| 54 | void postvisit( SingleInit * ); |
---|
| 55 | void postvisit( ListInit * ); |
---|
| 56 | void postvisit( ConstructorInit * ); |
---|
[51587aa] | 57 | |
---|
| 58 | //*** Constant |
---|
[9857e8d] | 59 | void postvisit( Constant * ); |
---|
[51587aa] | 60 | |
---|
| 61 | //*** Expression |
---|
[9857e8d] | 62 | void postvisit( ApplicationExpr *applicationExpr ); |
---|
| 63 | void postvisit( UntypedExpr *untypedExpr ); |
---|
| 64 | void postvisit( RangeExpr * rangeExpr ); |
---|
| 65 | void postvisit( NameExpr *nameExpr ); |
---|
| 66 | void postvisit( AddressExpr *addressExpr ); |
---|
| 67 | void postvisit( LabelAddressExpr *addressExpr ); |
---|
| 68 | void postvisit( CastExpr *castExpr ); |
---|
| 69 | void postvisit( VirtualCastExpr *castExpr ); |
---|
| 70 | void postvisit( UntypedMemberExpr *memberExpr ); |
---|
| 71 | void postvisit( MemberExpr *memberExpr ); |
---|
| 72 | void postvisit( VariableExpr *variableExpr ); |
---|
| 73 | void postvisit( ConstantExpr *constantExpr ); |
---|
| 74 | void postvisit( SizeofExpr *sizeofExpr ); |
---|
| 75 | void postvisit( AlignofExpr *alignofExpr ); |
---|
| 76 | void postvisit( UntypedOffsetofExpr *offsetofExpr ); |
---|
| 77 | void postvisit( OffsetofExpr *offsetofExpr ); |
---|
| 78 | void postvisit( OffsetPackExpr *offsetPackExpr ); |
---|
| 79 | void postvisit( LogicalExpr *logicalExpr ); |
---|
| 80 | void postvisit( ConditionalExpr *conditionalExpr ); |
---|
| 81 | void postvisit( CommaExpr *commaExpr ); |
---|
| 82 | void postvisit( CompoundLiteralExpr *compLitExpr ); |
---|
| 83 | void postvisit( UniqueExpr * ); |
---|
| 84 | void postvisit( TupleAssignExpr * tupleExpr ); |
---|
| 85 | void postvisit( UntypedTupleExpr *tupleExpr ); |
---|
| 86 | void postvisit( TupleExpr *tupleExpr ); |
---|
| 87 | void postvisit( TupleIndexExpr * tupleExpr ); |
---|
| 88 | void postvisit( TypeExpr *typeExpr ); |
---|
| 89 | void postvisit( AsmExpr * ); |
---|
| 90 | void postvisit( StmtExpr * ); |
---|
[51587aa] | 91 | |
---|
| 92 | //*** Statements |
---|
[9857e8d] | 93 | void postvisit( CompoundStmt * ); |
---|
| 94 | void postvisit( ExprStmt * ); |
---|
| 95 | void postvisit( AsmStmt * ); |
---|
| 96 | void postvisit( AsmDecl * ); // special: statement in declaration context |
---|
| 97 | void postvisit( IfStmt * ); |
---|
| 98 | void postvisit( SwitchStmt * ); |
---|
| 99 | void postvisit( CaseStmt * ); |
---|
| 100 | void postvisit( BranchStmt * ); |
---|
| 101 | void postvisit( ReturnStmt * ); |
---|
| 102 | void postvisit( ThrowStmt * ); |
---|
| 103 | void postvisit( WhileStmt * ); |
---|
| 104 | void postvisit( ForStmt * ); |
---|
| 105 | void postvisit( NullStmt * ); |
---|
| 106 | void postvisit( DeclStmt * ); |
---|
| 107 | void postvisit( ImplicitCtorDtorStmt * ); |
---|
[7baed7d] | 108 | |
---|
| 109 | void genAttributes( std::list< Attribute * > & attributes ); |
---|
[51587aa] | 110 | |
---|
| 111 | template< class Iterator > void genCommaList( Iterator begin, Iterator end ); |
---|
[cda48b6] | 112 | |
---|
[888cbe4] | 113 | struct LabelPrinter { |
---|
| 114 | LabelPrinter(CodeGenerator &cg) : cg(cg), labels( 0 ) {} |
---|
| 115 | LabelPrinter & operator()( std::list< Label > & l ); |
---|
| 116 | CodeGenerator & cg; |
---|
| 117 | std::list< Label > * labels; |
---|
[cda48b6] | 118 | }; |
---|
[e04ef3a] | 119 | |
---|
[58dd019] | 120 | void asmName( DeclarationWithType *decl ); |
---|
| 121 | |
---|
[8e9cbb2] | 122 | void extension( Expression *expr ); |
---|
| 123 | void extension( Declaration *decl ); |
---|
[0dd18fd] | 124 | |
---|
| 125 | void updateLocation( BaseSyntaxNode const * to ); |
---|
[51587aa] | 126 | private: |
---|
[cda48b6] | 127 | Indenter indent; |
---|
[51587aa] | 128 | bool insideFunction; |
---|
[6c4ff37] | 129 | std::ostream &output; |
---|
[888cbe4] | 130 | LabelPrinter printLabels; |
---|
[35b1bf4] | 131 | bool pretty = false; // pretty print |
---|
[e39241b] | 132 | bool genC = false; // true if output has to be C code |
---|
[c850687] | 133 | bool lineMarks = false; |
---|
[51587aa] | 134 | |
---|
[8bafacc] | 135 | CodeLocation currentLocation; |
---|
| 136 | void updateLocation( CodeLocation const & to ); |
---|
| 137 | void nextLine(); |
---|
| 138 | |
---|
[dd020c0] | 139 | void handleStorageClass( DeclarationWithType *decl ); |
---|
[5f642e38] | 140 | void handleAggregate( AggregateDecl *aggDecl, const std::string & kind ); |
---|
[51587aa] | 141 | void handleTypedef( NamedTypeDecl *namedType ); |
---|
[486341f] | 142 | std::string mangleName( DeclarationWithType * decl ); |
---|
[66d12f7] | 143 | }; // CodeGenerator |
---|
[7baed7d] | 144 | |
---|
[51587aa] | 145 | template< class Iterator > |
---|
[6c4ff37] | 146 | void CodeGenerator::genCommaList( Iterator begin, Iterator end ) { |
---|
[44a81853] | 147 | if ( begin == end ) return; |
---|
[51587aa] | 148 | for ( ;; ) { |
---|
[9857e8d] | 149 | (*begin++)->accept( *visitor ); |
---|
[44a81853] | 150 | if ( begin == end ) break; |
---|
| 151 | output << ", "; // separator |
---|
[51587aa] | 152 | } // for |
---|
[44a81853] | 153 | } // genCommaList |
---|
[7baed7d] | 154 | |
---|
[51587aa] | 155 | inline bool doSemicolon( Declaration* decl ) { |
---|
| 156 | if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) { |
---|
| 157 | return ! func->get_statements(); |
---|
| 158 | } // if |
---|
| 159 | return true; |
---|
[44a81853] | 160 | } // doSemicolon |
---|
[9facf3b] | 161 | |
---|
| 162 | /// returns C-compatible name of declaration |
---|
| 163 | std::string genName( DeclarationWithType * decl ); |
---|
[51b7345] | 164 | } // namespace CodeGen |
---|
| 165 | |
---|
[51587aa] | 166 | // Local Variables: // |
---|
| 167 | // tab-width: 4 // |
---|
| 168 | // mode: c++ // |
---|
| 169 | // compile-command: "make install" // |
---|
| 170 | // End: // |
---|