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