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