[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 { |
---|
[5f08961d] | 29 | struct CodeGenerator : public WithShortCircuiting, public WithGuards, public WithVisitorRef<CodeGenerator> { |
---|
[9857e8d] | 30 | static int tabsize; |
---|
[51587aa] | 31 | |
---|
[5f08961d] | 32 | CodeGenerator( std::ostream &os, bool pretty = false, bool genC = false, bool lineMarks = false, bool printExprTypes = false ); |
---|
[51587aa] | 33 | |
---|
[9857e8d] | 34 | //*** Turn off visit_children for all nodes |
---|
| 35 | void previsit( BaseSyntaxNode * ); |
---|
| 36 | |
---|
| 37 | //*** Error for unhandled node types |
---|
| 38 | void postvisit( BaseSyntaxNode * ); |
---|
| 39 | |
---|
[5f08961d] | 40 | //*** print type for all expressions |
---|
| 41 | void previsit( Expression * node ); |
---|
| 42 | |
---|
[51587aa] | 43 | //*** Declaration |
---|
[9857e8d] | 44 | void postvisit( StructDecl * ); |
---|
| 45 | void postvisit( FunctionDecl * ); |
---|
| 46 | void postvisit( ObjectDecl * ); |
---|
[92fea32] | 47 | void postvisit( UnionDecl * aggregateDecl ); |
---|
| 48 | void postvisit( EnumDecl * aggregateDecl ); |
---|
| 49 | void postvisit( TraitDecl * aggregateDecl ); |
---|
| 50 | void postvisit( TypedefDecl * typeDecl ); |
---|
| 51 | void postvisit( TypeDecl * typeDecl ); |
---|
| 52 | void postvisit( StaticAssertDecl * assertDecl ); |
---|
[51587aa] | 53 | |
---|
| 54 | //*** Initializer |
---|
[9857e8d] | 55 | void postvisit( Designation * ); |
---|
| 56 | void postvisit( SingleInit * ); |
---|
| 57 | void postvisit( ListInit * ); |
---|
| 58 | void postvisit( ConstructorInit * ); |
---|
[51587aa] | 59 | |
---|
| 60 | //*** Constant |
---|
[9857e8d] | 61 | void postvisit( Constant * ); |
---|
[51587aa] | 62 | |
---|
| 63 | //*** Expression |
---|
[9857e8d] | 64 | void postvisit( ApplicationExpr *applicationExpr ); |
---|
| 65 | void postvisit( UntypedExpr *untypedExpr ); |
---|
| 66 | void postvisit( RangeExpr * rangeExpr ); |
---|
| 67 | void postvisit( NameExpr *nameExpr ); |
---|
| 68 | void postvisit( AddressExpr *addressExpr ); |
---|
| 69 | void postvisit( LabelAddressExpr *addressExpr ); |
---|
| 70 | void postvisit( CastExpr *castExpr ); |
---|
[da9d79b] | 71 | void postvisit( KeywordCastExpr * castExpr ); |
---|
[9857e8d] | 72 | void postvisit( VirtualCastExpr *castExpr ); |
---|
| 73 | void postvisit( UntypedMemberExpr *memberExpr ); |
---|
| 74 | void postvisit( MemberExpr *memberExpr ); |
---|
| 75 | void postvisit( VariableExpr *variableExpr ); |
---|
| 76 | void postvisit( ConstantExpr *constantExpr ); |
---|
| 77 | void postvisit( SizeofExpr *sizeofExpr ); |
---|
| 78 | void postvisit( AlignofExpr *alignofExpr ); |
---|
| 79 | void postvisit( UntypedOffsetofExpr *offsetofExpr ); |
---|
| 80 | void postvisit( OffsetofExpr *offsetofExpr ); |
---|
| 81 | void postvisit( OffsetPackExpr *offsetPackExpr ); |
---|
| 82 | void postvisit( LogicalExpr *logicalExpr ); |
---|
| 83 | void postvisit( ConditionalExpr *conditionalExpr ); |
---|
| 84 | void postvisit( CommaExpr *commaExpr ); |
---|
| 85 | void postvisit( CompoundLiteralExpr *compLitExpr ); |
---|
| 86 | void postvisit( UniqueExpr * ); |
---|
| 87 | void postvisit( TupleAssignExpr * tupleExpr ); |
---|
| 88 | void postvisit( UntypedTupleExpr *tupleExpr ); |
---|
| 89 | void postvisit( TupleExpr *tupleExpr ); |
---|
| 90 | void postvisit( TupleIndexExpr * tupleExpr ); |
---|
| 91 | void postvisit( TypeExpr *typeExpr ); |
---|
| 92 | void postvisit( AsmExpr * ); |
---|
| 93 | void postvisit( StmtExpr * ); |
---|
[4e8949f] | 94 | void postvisit( ConstructorExpr * ); |
---|
[44b4114] | 95 | void postvisit( DeletedExpr * ); |
---|
[d807ca28] | 96 | void postvisit( GenericExpr * ); |
---|
[51587aa] | 97 | |
---|
| 98 | //*** Statements |
---|
[9857e8d] | 99 | void postvisit( CompoundStmt * ); |
---|
| 100 | void postvisit( ExprStmt * ); |
---|
| 101 | void postvisit( AsmStmt * ); |
---|
[cc32d83] | 102 | void postvisit( DirectiveStmt * ); |
---|
[9857e8d] | 103 | void postvisit( AsmDecl * ); // special: statement in declaration context |
---|
| 104 | void postvisit( IfStmt * ); |
---|
| 105 | void postvisit( SwitchStmt * ); |
---|
| 106 | void postvisit( CaseStmt * ); |
---|
| 107 | void postvisit( BranchStmt * ); |
---|
| 108 | void postvisit( ReturnStmt * ); |
---|
| 109 | void postvisit( ThrowStmt * ); |
---|
[e4ea10b7] | 110 | void postvisit( CatchStmt * ); |
---|
| 111 | void postvisit( WaitForStmt * ); |
---|
[55d6e8de] | 112 | void postvisit( WithStmt * ); |
---|
[9857e8d] | 113 | void postvisit( WhileStmt * ); |
---|
| 114 | void postvisit( ForStmt * ); |
---|
| 115 | void postvisit( NullStmt * ); |
---|
| 116 | void postvisit( DeclStmt * ); |
---|
| 117 | void postvisit( ImplicitCtorDtorStmt * ); |
---|
[7baed7d] | 118 | |
---|
| 119 | void genAttributes( std::list< Attribute * > & attributes ); |
---|
[51587aa] | 120 | |
---|
| 121 | template< class Iterator > void genCommaList( Iterator begin, Iterator end ); |
---|
[cda48b6] | 122 | |
---|
[888cbe4] | 123 | struct LabelPrinter { |
---|
| 124 | LabelPrinter(CodeGenerator &cg) : cg(cg), labels( 0 ) {} |
---|
| 125 | LabelPrinter & operator()( std::list< Label > & l ); |
---|
| 126 | CodeGenerator & cg; |
---|
| 127 | std::list< Label > * labels; |
---|
[cda48b6] | 128 | }; |
---|
[e04ef3a] | 129 | |
---|
[58dd019] | 130 | void asmName( DeclarationWithType *decl ); |
---|
| 131 | |
---|
[8e9cbb2] | 132 | void extension( Expression *expr ); |
---|
| 133 | void extension( Declaration *decl ); |
---|
[0dd18fd] | 134 | |
---|
| 135 | void updateLocation( BaseSyntaxNode const * to ); |
---|
[d22e90f] | 136 | struct LineEnder { |
---|
| 137 | CodeGenerator & cg; |
---|
| 138 | LineEnder( CodeGenerator & cg ) : cg( cg ) {} |
---|
| 139 | std::ostream & operator()(std::ostream &) const; |
---|
| 140 | }; |
---|
[51587aa] | 141 | private: |
---|
[cda48b6] | 142 | Indenter indent; |
---|
[d22e90f] | 143 | std::ostream & output; |
---|
[888cbe4] | 144 | LabelPrinter printLabels; |
---|
[35b1bf4] | 145 | bool pretty = false; // pretty print |
---|
[e39241b] | 146 | bool genC = false; // true if output has to be C code |
---|
[c850687] | 147 | bool lineMarks = false; |
---|
[5f08961d] | 148 | bool printExprTypes = false; |
---|
[d22e90f] | 149 | public: |
---|
| 150 | LineEnder endl; |
---|
| 151 | private: |
---|
[51587aa] | 152 | |
---|
[8bafacc] | 153 | CodeLocation currentLocation; |
---|
| 154 | void updateLocation( CodeLocation const & to ); |
---|
| 155 | |
---|
[dd020c0] | 156 | void handleStorageClass( DeclarationWithType *decl ); |
---|
[5f642e38] | 157 | void handleAggregate( AggregateDecl *aggDecl, const std::string & kind ); |
---|
[51587aa] | 158 | void handleTypedef( NamedTypeDecl *namedType ); |
---|
[486341f] | 159 | std::string mangleName( DeclarationWithType * decl ); |
---|
[66d12f7] | 160 | }; // CodeGenerator |
---|
[7baed7d] | 161 | |
---|
[51587aa] | 162 | template< class Iterator > |
---|
[6c4ff37] | 163 | void CodeGenerator::genCommaList( Iterator begin, Iterator end ) { |
---|
[44a81853] | 164 | if ( begin == end ) return; |
---|
[51587aa] | 165 | for ( ;; ) { |
---|
[9857e8d] | 166 | (*begin++)->accept( *visitor ); |
---|
[44a81853] | 167 | if ( begin == end ) break; |
---|
| 168 | output << ", "; // separator |
---|
[51587aa] | 169 | } // for |
---|
[44a81853] | 170 | } // genCommaList |
---|
[7baed7d] | 171 | |
---|
[51587aa] | 172 | inline bool doSemicolon( Declaration* decl ) { |
---|
| 173 | if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) { |
---|
| 174 | return ! func->get_statements(); |
---|
| 175 | } // if |
---|
| 176 | return true; |
---|
[44a81853] | 177 | } // doSemicolon |
---|
[9facf3b] | 178 | |
---|
| 179 | /// returns C-compatible name of declaration |
---|
| 180 | std::string genName( DeclarationWithType * decl ); |
---|
[d22e90f] | 181 | |
---|
| 182 | inline std::ostream & operator<<( std::ostream & os, const CodeGenerator::LineEnder & endl ) { |
---|
| 183 | return endl( os ); |
---|
| 184 | } |
---|
[51b7345] | 185 | } // namespace CodeGen |
---|
| 186 | |
---|
[51587aa] | 187 | // Local Variables: // |
---|
| 188 | // tab-width: 4 // |
---|
| 189 | // mode: c++ // |
---|
| 190 | // compile-command: "make install" // |
---|
| 191 | // End: // |
---|