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