| 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 : Rob Schluntz | 
|---|
| 12 | // Last Modified On : Wed Aug 12 14:27:14 2015 | 
|---|
| 13 | // Update Count     : 27 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #ifndef CODEGENV_H | 
|---|
| 17 | #define CODEGENV_H | 
|---|
| 18 |  | 
|---|
| 19 | #include <list> | 
|---|
| 20 |  | 
|---|
| 21 | #include "SynTree/SynTree.h" | 
|---|
| 22 | #include "SynTree/Visitor.h" | 
|---|
| 23 | #include "SymTab/Indexer.h" | 
|---|
| 24 |  | 
|---|
| 25 | namespace CodeGen { | 
|---|
| 26 | class CodeGenerator : public Visitor { | 
|---|
| 27 | public: | 
|---|
| 28 | static int tabsize; | 
|---|
| 29 |  | 
|---|
| 30 | CodeGenerator( std::ostream &os ); | 
|---|
| 31 | CodeGenerator( std::ostream &os, std::string, int indent = 0, bool infun = false ); | 
|---|
| 32 | CodeGenerator( std::ostream &os, char *, int indent = 0, bool infun = false ); | 
|---|
| 33 |  | 
|---|
| 34 | //*** Declaration | 
|---|
| 35 | virtual void visit( StructDecl * ); | 
|---|
| 36 | virtual void visit( FunctionDecl * ); | 
|---|
| 37 | virtual void visit( ObjectDecl * ); | 
|---|
| 38 | virtual void visit( UnionDecl *aggregateDecl ); | 
|---|
| 39 | virtual void visit( EnumDecl *aggregateDecl ); | 
|---|
| 40 | virtual void visit( ContextDecl *aggregateDecl ); | 
|---|
| 41 | virtual void visit( TypedefDecl *typeDecl ); | 
|---|
| 42 | virtual void visit( TypeDecl *typeDecl ); | 
|---|
| 43 |  | 
|---|
| 44 | //*** Initializer | 
|---|
| 45 | virtual void visit( SingleInit * ); | 
|---|
| 46 | virtual void visit( ListInit * ); | 
|---|
| 47 |  | 
|---|
| 48 | //*** Constant | 
|---|
| 49 | virtual void visit( Constant * ); | 
|---|
| 50 |  | 
|---|
| 51 | //*** Expression | 
|---|
| 52 | virtual void visit( ApplicationExpr *applicationExpr ); | 
|---|
| 53 | virtual void visit( UntypedExpr *untypedExpr ); | 
|---|
| 54 | virtual void visit( NameExpr *nameExpr ); | 
|---|
| 55 | virtual void visit( AddressExpr *addressExpr ); | 
|---|
| 56 | virtual void visit( CastExpr *castExpr ); | 
|---|
| 57 | virtual void visit( UntypedMemberExpr *memberExpr ); | 
|---|
| 58 | virtual void visit( MemberExpr *memberExpr ); | 
|---|
| 59 | virtual void visit( VariableExpr *variableExpr ); | 
|---|
| 60 | virtual void visit( ConstantExpr *constantExpr ); | 
|---|
| 61 | virtual void visit( SizeofExpr *sizeofExpr ); | 
|---|
| 62 | virtual void visit( AlignofExpr *alignofExpr ); | 
|---|
| 63 | virtual void visit( OffsetofExpr *offsetofExpr ); | 
|---|
| 64 | virtual void visit( LogicalExpr *logicalExpr ); | 
|---|
| 65 | virtual void visit( ConditionalExpr *conditionalExpr ); | 
|---|
| 66 | virtual void visit( CommaExpr *commaExpr ); | 
|---|
| 67 | virtual void visit( TupleExpr *tupleExpr ); | 
|---|
| 68 | virtual void visit( TypeExpr *typeExpr ); | 
|---|
| 69 | virtual void visit( AsmExpr * ); | 
|---|
| 70 |  | 
|---|
| 71 | //*** Statements | 
|---|
| 72 | virtual void visit( CompoundStmt * ); | 
|---|
| 73 | virtual void visit( ExprStmt * ); | 
|---|
| 74 | virtual void visit( AsmStmt * ); | 
|---|
| 75 | virtual void visit( IfStmt * ); | 
|---|
| 76 | virtual void visit( SwitchStmt * ); | 
|---|
| 77 | virtual void visit( CaseStmt * ); | 
|---|
| 78 | virtual void visit( BranchStmt * ); | 
|---|
| 79 | virtual void visit( ReturnStmt * ); | 
|---|
| 80 | virtual void visit( WhileStmt * ); | 
|---|
| 81 | virtual void visit( ForStmt * ); | 
|---|
| 82 | virtual void visit( NullStmt * ); | 
|---|
| 83 | virtual void visit( DeclStmt * ); | 
|---|
| 84 |  | 
|---|
| 85 | template< class Iterator > void genCommaList( Iterator begin, Iterator end ); | 
|---|
| 86 |  | 
|---|
| 87 | struct Indenter { | 
|---|
| 88 | Indenter(CodeGenerator &cg) : cg(cg) {} | 
|---|
| 89 | CodeGenerator & cg; | 
|---|
| 90 | std::ostream& operator()(std::ostream & os); | 
|---|
| 91 | }; | 
|---|
| 92 | private: | 
|---|
| 93 |  | 
|---|
| 94 | Indenter indent; | 
|---|
| 95 | int cur_indent; | 
|---|
| 96 | bool insideFunction; | 
|---|
| 97 | std::ostream &output; | 
|---|
| 98 |  | 
|---|
| 99 | void printDesignators( std::list< Expression * > & ); | 
|---|
| 100 | static std::string printLabels ( std::list < Label > & ); | 
|---|
| 101 | void handleStorageClass( Declaration *decl ); | 
|---|
| 102 | void handleAggregate( AggregateDecl *aggDecl ); | 
|---|
| 103 | void handleTypedef( NamedTypeDecl *namedType ); | 
|---|
| 104 |  | 
|---|
| 105 | }; | 
|---|
| 106 |  | 
|---|
| 107 | template< class Iterator > | 
|---|
| 108 | void CodeGenerator::genCommaList( Iterator begin, Iterator end ) { | 
|---|
| 109 | if ( begin == end ) return; | 
|---|
| 110 |  | 
|---|
| 111 | for ( ;; ) { | 
|---|
| 112 | (*begin++)->accept( *this ); | 
|---|
| 113 | if ( begin == end ) return; | 
|---|
| 114 | output << ", "; | 
|---|
| 115 | } // for | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | inline bool doSemicolon( Declaration* decl ) { | 
|---|
| 119 | if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) { | 
|---|
| 120 | return ! func->get_statements(); | 
|---|
| 121 | } // if | 
|---|
| 122 | return true; | 
|---|
| 123 | } | 
|---|
| 124 | } // namespace CodeGen | 
|---|
| 125 |  | 
|---|
| 126 | #endif // CODEGENV_H | 
|---|
| 127 |  | 
|---|
| 128 | // Local Variables: // | 
|---|
| 129 | // tab-width: 4 // | 
|---|
| 130 | // mode: c++ // | 
|---|
| 131 | // compile-command: "make install" // | 
|---|
| 132 | // End: // | 
|---|