| 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 | // | 
|---|
| 7 | // CodeGenerator.h -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| 11 | // Last Modified By : Andrew Beach | 
|---|
| 12 | // Last Modified On : Fri Aug 18 15:40:00 2017 | 
|---|
| 13 | // Update Count     : 56 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | #include <list>                   // for list | 
|---|
| 19 | #include <ostream>                // for ostream, operator<< | 
|---|
| 20 | #include <string>                 // for string | 
|---|
| 21 |  | 
|---|
| 22 | #include "Common/Indenter.h"      // for Indenter | 
|---|
| 23 | #include "Common/PassVisitor.h"   // for PassVisitor | 
|---|
| 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 | 
|---|
| 27 |  | 
|---|
| 28 | namespace CodeGen { | 
|---|
| 29 | struct CodeGenerator : public WithShortCircuiting, public WithGuards, public WithVisitorRef<CodeGenerator> { | 
|---|
| 30 | static int tabsize; | 
|---|
| 31 |  | 
|---|
| 32 | CodeGenerator( std::ostream &os, bool pretty = false, bool genC = false, bool lineMarks = false, bool printExprTypes = false ); | 
|---|
| 33 |  | 
|---|
| 34 | //*** Turn off visit_children for all nodes | 
|---|
| 35 | void previsit( BaseSyntaxNode * ); | 
|---|
| 36 |  | 
|---|
| 37 | //*** Error for unhandled node types | 
|---|
| 38 | void postvisit( BaseSyntaxNode * ); | 
|---|
| 39 |  | 
|---|
| 40 | //*** print type for all expressions | 
|---|
| 41 | void previsit( Expression * node ); | 
|---|
| 42 |  | 
|---|
| 43 | //*** Declaration | 
|---|
| 44 | void postvisit( StructDecl * ); | 
|---|
| 45 | void postvisit( FunctionDecl * ); | 
|---|
| 46 | void postvisit( ObjectDecl * ); | 
|---|
| 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 ); | 
|---|
| 53 |  | 
|---|
| 54 | //*** Initializer | 
|---|
| 55 | void postvisit( Designation * ); | 
|---|
| 56 | void postvisit( SingleInit * ); | 
|---|
| 57 | void postvisit( ListInit * ); | 
|---|
| 58 | void postvisit( ConstructorInit * ); | 
|---|
| 59 |  | 
|---|
| 60 | //*** Constant | 
|---|
| 61 | void postvisit( Constant * ); | 
|---|
| 62 |  | 
|---|
| 63 | //*** Expression | 
|---|
| 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 ); | 
|---|
| 71 | void postvisit( KeywordCastExpr * castExpr ); | 
|---|
| 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 * ); | 
|---|
| 94 | void postvisit( ConstructorExpr * ); | 
|---|
| 95 | void postvisit( DeletedExpr * ); | 
|---|
| 96 | void postvisit( DefaultArgExpr * ); | 
|---|
| 97 | void postvisit( GenericExpr * ); | 
|---|
| 98 |  | 
|---|
| 99 | //*** Statements | 
|---|
| 100 | void postvisit( CompoundStmt * ); | 
|---|
| 101 | void postvisit( ExprStmt * ); | 
|---|
| 102 | void postvisit( AsmStmt * ); | 
|---|
| 103 | void postvisit( DirectiveStmt * ); | 
|---|
| 104 | void postvisit( AsmDecl * );                            // special: statement in declaration context | 
|---|
| 105 | void postvisit( IfStmt * ); | 
|---|
| 106 | void postvisit( SwitchStmt * ); | 
|---|
| 107 | void postvisit( CaseStmt * ); | 
|---|
| 108 | void postvisit( BranchStmt * ); | 
|---|
| 109 | void postvisit( ReturnStmt * ); | 
|---|
| 110 | void postvisit( ThrowStmt * ); | 
|---|
| 111 | void postvisit( CatchStmt * ); | 
|---|
| 112 | void postvisit( WaitForStmt * ); | 
|---|
| 113 | void postvisit( WithStmt * ); | 
|---|
| 114 | void postvisit( WhileStmt * ); | 
|---|
| 115 | void postvisit( ForStmt * ); | 
|---|
| 116 | void postvisit( NullStmt * ); | 
|---|
| 117 | void postvisit( DeclStmt * ); | 
|---|
| 118 | void postvisit( ImplicitCtorDtorStmt * ); | 
|---|
| 119 |  | 
|---|
| 120 | void genAttributes( std::list< Attribute * > & attributes ); | 
|---|
| 121 |  | 
|---|
| 122 | template< class Iterator > void genCommaList( Iterator begin, Iterator end ); | 
|---|
| 123 |  | 
|---|
| 124 | struct LabelPrinter { | 
|---|
| 125 | LabelPrinter(CodeGenerator &cg) : cg(cg), labels( 0 ) {} | 
|---|
| 126 | LabelPrinter & operator()( std::list< Label > & l ); | 
|---|
| 127 | CodeGenerator & cg; | 
|---|
| 128 | std::list< Label > * labels; | 
|---|
| 129 | }; | 
|---|
| 130 |  | 
|---|
| 131 | void asmName( DeclarationWithType *decl ); | 
|---|
| 132 |  | 
|---|
| 133 | void extension( Expression *expr ); | 
|---|
| 134 | void extension( Declaration *decl ); | 
|---|
| 135 |  | 
|---|
| 136 | void updateLocation( BaseSyntaxNode const * to ); | 
|---|
| 137 | struct LineEnder { | 
|---|
| 138 | CodeGenerator & cg; | 
|---|
| 139 | LineEnder( CodeGenerator & cg ) : cg( cg ) {} | 
|---|
| 140 | std::ostream & operator()(std::ostream &) const; | 
|---|
| 141 | }; | 
|---|
| 142 | private: | 
|---|
| 143 | Indenter indent; | 
|---|
| 144 | std::ostream & output; | 
|---|
| 145 | LabelPrinter printLabels; | 
|---|
| 146 | bool pretty = false;  // pretty print | 
|---|
| 147 | bool genC = false;    // true if output has to be C code | 
|---|
| 148 | bool lineMarks = false; | 
|---|
| 149 | bool printExprTypes = false; | 
|---|
| 150 | public: | 
|---|
| 151 | LineEnder endl; | 
|---|
| 152 | private: | 
|---|
| 153 |  | 
|---|
| 154 | CodeLocation currentLocation; | 
|---|
| 155 | void updateLocation( CodeLocation const & to ); | 
|---|
| 156 |  | 
|---|
| 157 | void handleStorageClass( DeclarationWithType *decl ); | 
|---|
| 158 | void handleAggregate( AggregateDecl *aggDecl, const std::string & kind ); | 
|---|
| 159 | void handleTypedef( NamedTypeDecl *namedType ); | 
|---|
| 160 | std::string mangleName( DeclarationWithType * decl ); | 
|---|
| 161 | }; // CodeGenerator | 
|---|
| 162 |  | 
|---|
| 163 | template< class Iterator > | 
|---|
| 164 | void CodeGenerator::genCommaList( Iterator begin, Iterator end ) { | 
|---|
| 165 | if ( begin == end ) return; | 
|---|
| 166 | for ( ;; ) { | 
|---|
| 167 | (*begin++)->accept( *visitor ); | 
|---|
| 168 | if ( begin == end ) break; | 
|---|
| 169 | output << ", ";                                                         // separator | 
|---|
| 170 | } // for | 
|---|
| 171 | } // genCommaList | 
|---|
| 172 |  | 
|---|
| 173 | inline bool doSemicolon( Declaration* decl ) { | 
|---|
| 174 | if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) { | 
|---|
| 175 | return ! func->get_statements(); | 
|---|
| 176 | } // if | 
|---|
| 177 | return true; | 
|---|
| 178 | } // doSemicolon | 
|---|
| 179 |  | 
|---|
| 180 | /// returns C-compatible name of declaration | 
|---|
| 181 | std::string genName( DeclarationWithType * decl ); | 
|---|
| 182 |  | 
|---|
| 183 | inline std::ostream & operator<<( std::ostream & os, const CodeGenerator::LineEnder & endl ) { | 
|---|
| 184 | return endl( os ); | 
|---|
| 185 | } | 
|---|
| 186 | } // namespace CodeGen | 
|---|
| 187 |  | 
|---|
| 188 | // Local Variables: // | 
|---|
| 189 | // tab-width: 4 // | 
|---|
| 190 | // mode: c++ // | 
|---|
| 191 | // compile-command: "make install" // | 
|---|
| 192 | // End: // | 
|---|