retdec
c_hll_writer.h
Go to the documentation of this file.
1 
7 #ifndef RETDEC_LLVMIR2HLL_HLL_HLL_WRITERS_C_HLL_WRITER_H
8 #define RETDEC_LLVMIR2HLL_HLL_HLL_WRITERS_C_HLL_WRITER_H
9 
10 #include <cstddef>
11 #include <map>
12 #include <string>
13 
16 
17 namespace retdec {
18 namespace llvmir2hll {
19 
20 class BracketManager;
21 class CastExpr;
22 class CompoundOpManager;
23 class OutputManager;
24 class StructType;
25 
32 class CHLLWriter: public HLLWriter {
33 public:
34  static ShPtr<HLLWriter> create(
35  llvm::raw_ostream &out,
36  const std::string& outputFormat = "");
37 
38  virtual std::string getId() const override;
39 
44 
45 private:
47  using StructTypeNameMap = std::map<ShPtr<StructType>, std::string>;
48 
49 private:
50  CHLLWriter(
51  llvm::raw_ostream &out,
52  const std::string& outputFormat = "");
53 
54  virtual std::string getCommentPrefix() override;
55  virtual bool emitFileHeader() override;
56  virtual bool emitGlobalVariables() override;
57  virtual bool emitFunctionPrototypesHeader() override;
58  virtual bool emitFunctionPrototypes() override;
59  virtual bool emitExternalFunction(ShPtr<Function> func) override;
60  virtual bool emitTargetCode(ShPtr<Module> module) override;
61 
64  virtual void visit(ShPtr<GlobalVarDef> varDef) override;
65  virtual void visit(ShPtr<Function> func) override;
66  virtual void visit(ShPtr<Variable> var) override;
67  virtual void visit(ShPtr<AddressOpExpr> expr) override;
68  virtual void visit(ShPtr<AssignOpExpr> expr) override;
69  virtual void visit(ShPtr<ArrayIndexOpExpr> expr) override;
70  virtual void visit(ShPtr<StructIndexOpExpr> expr) override;
71  virtual void visit(ShPtr<DerefOpExpr> expr) override;
72  virtual void visit(ShPtr<NotOpExpr> expr) override;
73  virtual void visit(ShPtr<NegOpExpr> expr) override;
74  virtual void visit(ShPtr<EqOpExpr> expr) override;
75  virtual void visit(ShPtr<NeqOpExpr> expr) override;
76  virtual void visit(ShPtr<LtEqOpExpr> expr) override;
77  virtual void visit(ShPtr<GtEqOpExpr> expr) override;
78  virtual void visit(ShPtr<LtOpExpr> expr) override;
79  virtual void visit(ShPtr<GtOpExpr> expr) override;
80  virtual void visit(ShPtr<AddOpExpr> expr) override;
81  virtual void visit(ShPtr<SubOpExpr> expr) override;
82  virtual void visit(ShPtr<MulOpExpr> expr) override;
83  virtual void visit(ShPtr<ModOpExpr> expr) override;
84  virtual void visit(ShPtr<DivOpExpr> expr) override;
85  virtual void visit(ShPtr<AndOpExpr> expr) override;
86  virtual void visit(ShPtr<OrOpExpr> expr) override;
87  virtual void visit(ShPtr<BitAndOpExpr> expr) override;
88  virtual void visit(ShPtr<BitOrOpExpr> expr) override;
89  virtual void visit(ShPtr<BitXorOpExpr> expr) override;
90  virtual void visit(ShPtr<BitShlOpExpr> expr) override;
91  virtual void visit(ShPtr<BitShrOpExpr> expr) override;
92  virtual void visit(ShPtr<TernaryOpExpr> expr) override;
93  virtual void visit(ShPtr<CallExpr> expr) override;
94  virtual void visit(ShPtr<CommaOpExpr> expr) override;
95  virtual void visit(ShPtr<ConstBool> constant) override;
96  virtual void visit(ShPtr<ConstFloat> constant) override;
97  virtual void visit(ShPtr<ConstInt> constant) override;
98  virtual void visit(ShPtr<ConstNullPointer> constant) override;
99  virtual void visit(ShPtr<ConstString> constant) override;
100  virtual void visit(ShPtr<ConstArray> constant) override;
101  virtual void visit(ShPtr<ConstStruct> constant) override;
102  virtual void visit(ShPtr<ConstSymbol> constant) override;
103  virtual void visit(ShPtr<AssignStmt> stmt) override;
104  virtual void visit(ShPtr<VarDefStmt> stmt) override;
105  virtual void visit(ShPtr<CallStmt> stmt) override;
106  virtual void visit(ShPtr<ReturnStmt> stmt) override;
107  virtual void visit(ShPtr<EmptyStmt> stmt) override;
108  virtual void visit(ShPtr<IfStmt> stmt) override;
109  virtual void visit(ShPtr<SwitchStmt> stmt) override;
110  virtual void visit(ShPtr<WhileLoopStmt> stmt) override;
111  virtual void visit(ShPtr<ForLoopStmt> stmt) override;
112  virtual void visit(ShPtr<UForLoopStmt> stmt) override;
113  virtual void visit(ShPtr<BreakStmt> stmt) override;
114  virtual void visit(ShPtr<ContinueStmt> stmt) override;
115  virtual void visit(ShPtr<GotoStmt> stmt) override;
116  virtual void visit(ShPtr<UnreachableStmt> stmt) override;
117  // Types
118  virtual void visit(ShPtr<FloatType> type) override;
119  virtual void visit(ShPtr<IntType> type) override;
120  virtual void visit(ShPtr<PointerType> type) override;
121  virtual void visit(ShPtr<StringType> type) override;
122  virtual void visit(ShPtr<ArrayType> type) override;
123  virtual void visit(ShPtr<StructType> type) override;
124  virtual void visit(ShPtr<FunctionType> type) override;
125  virtual void visit(ShPtr<VoidType> type) override;
126  virtual void visit(ShPtr<UnknownType> type) override;
127  // Casts
128  virtual void visit(ShPtr<BitCastExpr> expr) override;
129  virtual void visit(ShPtr<ExtCastExpr> expr) override;
130  virtual void visit(ShPtr<TruncCastExpr> expr) override;
131  virtual void visit(ShPtr<FPToIntCastExpr> expr) override;
132  virtual void visit(ShPtr<IntToFPCastExpr> expr) override;
133  virtual void visit(ShPtr<IntToPtrCastExpr> expr) override;
134  virtual void visit(ShPtr<PtrToIntCastExpr> expr) override;
136 
138  bool emitFunctionPrototypes(const FuncSet &funcs);
148  void emitPointerToFunc(ShPtr<PointerType> pointerToFuncType,
149  ShPtr<Variable> var = nullptr);
151  ShPtr<Variable> var = nullptr);
152  void emitPointerToArray(ShPtr<PointerType> pointerToArrayType,
153  ShPtr<Variable> var = nullptr);
154  void emitArrayDimensions(ShPtr<ArrayType> arrayType);
155  void emitArrayDimension(std::size_t size);
164  void emitReturnType(ShPtr<FunctionType> funcType);
168  void emitConstStruct(ShPtr<ConstStruct> constant, bool emitCast = true);
170  bool emitInline = false);
171  void emitBlock(ShPtr<Statement> stmt);
173  void emitDebugComment(std::string comment, bool indent = true);
175 
176  std::string getConstFloatSuffixIfNeeded(ShPtr<ConstFloat> constant);
177  std::string genNameForUnnamedStruct(const StructTypeVector &usedStructTypes);
178 
179 private:
182 
185 
188  std::size_t unnamedStructCounter;
189 
195 
199 };
200 
201 } // namespace llvmir2hll
202 } // namespace retdec
203 
204 #endif
A HLL writer for the C language (C99).
Definition: c_hll_writer.h:32
static ShPtr< HLLWriter > create(llvm::raw_ostream &out, const std::string &outputFormat="")
Creates a new C writer.
Definition: c_hll_writer.cpp:256
StructTypeNameMap structNames
Mapping of a structured type into its name.
Definition: c_hll_writer.h:184
virtual void visit(ShPtr< GlobalVarDef > varDef) override
Definition: c_hll_writer.cpp:504
void emitFunctionParameters(ShPtr< Function > func)
Emits the parameters of the given function.
Definition: c_hll_writer.cpp:1500
void emitVarWithType(ShPtr< Variable > var)
Emits the given variable alongside with its type.
Definition: c_hll_writer.cpp:1539
void emitInitializedConstArray(ShPtr< ConstArray > array)
Emits the given initialized array.
Definition: c_hll_writer.cpp:1712
void emitGotoLabelIfNeeded(ShPtr< Statement > stmt)
Emits a label of stmt if it is needed.
Definition: c_hll_writer.cpp:2030
virtual bool emitTargetCode(ShPtr< Module > module) override
Emits the code from the given module.
Definition: c_hll_writer.cpp:549
void emitBlock(ShPtr< Statement > stmt)
Recursively emits the given block.
Definition: c_hll_writer.cpp:1999
bool shouldEmitFunctionPrototypesHeader() const
Returns true if we should emit the Function Prototypes header, false otherwise.
Definition: c_hll_writer.cpp:1233
void emitStructDeclaration(ShPtr< StructType > structType, bool emitInline=false)
Emits a declaration of the given structure.
Definition: c_hll_writer.cpp:1944
void setOptionEmitFunctionPrototypesForNonLibraryFuncs(bool emit=true)
void emitAssignment(ShPtr< Expression > lhs, ShPtr< Expression > rhs)
Emits the given assignment (without leading or trailing whitespace).
Definition: c_hll_writer.cpp:855
void emitInitializedConstArrayInline(ShPtr< ConstArray > array)
Emits the given array inline.
Definition: c_hll_writer.cpp:1723
void emitConstStruct(ShPtr< ConstStruct > constant, bool emitCast=true)
Emits the given structure literal.
Definition: c_hll_writer.cpp:1862
void emitUninitializedConstArray(ShPtr< ConstArray > array)
Emits the given uninitialized array.
Definition: c_hll_writer.cpp:1763
bool emitStandardFunctionPrototypes()
Emits standard function prototypes.
Definition: c_hll_writer.cpp:1260
void emitTypeOfElementsInArray(ShPtr< ArrayType > arrayType)
Emits the type of elements in the given array.
Definition: c_hll_writer.cpp:1776
virtual bool emitFileHeader() override
Emits the file header.
Definition: c_hll_writer.cpp:272
void emitHeaderOfFuncReturningPointerToArray(ShPtr< Function > func)
A specialization of emitFunctionHeader() for functions returning a pointer to an array.
Definition: c_hll_writer.cpp:1462
void emitInitializedConstArrayInStructuredWay(ShPtr< ConstArray > array)
Emits the given array in a structured way (may span over multiple lines).
Definition: c_hll_writer.cpp:1737
virtual bool emitFunctionPrototypes() override
Emits function prototypes.
Definition: c_hll_writer.cpp:470
std::string getConstFloatSuffixIfNeeded(ShPtr< ConstFloat > constant)
Definition: c_hll_writer.cpp:2059
virtual bool emitFunctionPrototypesHeader() override
Emits the header of the function prototypes block.
Definition: c_hll_writer.cpp:462
virtual bool emitGlobalVariables() override
Emits all global variables in the module.
Definition: c_hll_writer.cpp:453
void emitFunctionHeader(ShPtr< Function > func)
Emits the header of the given function.
Definition: c_hll_writer.cpp:1371
void emitArrayDimension(std::size_t size)
Emits the given array dimension.
Definition: c_hll_writer.cpp:1695
bool emitFunctionPrototype(ShPtr< Function > func)
Emits a prototype of the given function, including the ending newline.
Definition: c_hll_writer.cpp:1317
bool emitFunctionPrototypesForNonLibraryFuncs()
Emits prototypes for functions which do not have any associated header file.
Definition: c_hll_writer.cpp:1287
std::string genNameForUnnamedStruct(const StructTypeVector &usedStructTypes)
Generates a new name for an unnamed structure.
Definition: c_hll_writer.cpp:2101
void emitCastInStandardWay(ShPtr< CastExpr > expr)
Emits the given cast in the standard way.
Definition: c_hll_writer.cpp:1783
bool emittingGlobalVarDefs
Definition: c_hll_writer.h:194
void emitStarsBeforePointedValue(ShPtr< PointerType > ptrType)
Emits '*'s before the pointed value (there can be more than one).
Definition: c_hll_writer.cpp:1793
void emitPointerToArray(ShPtr< PointerType > pointerToArrayType, ShPtr< Variable > var=nullptr)
Emits a pointer to the given array, possibly with the given variable.
Definition: c_hll_writer.cpp:1668
void emitDebugComment(std::string comment, bool indent=true)
Emits the given debug comment.
Definition: c_hll_writer.cpp:2081
void emitHeaderOfFuncReturningPointerToFunc(ShPtr< Function > func)
A specialization of emitFunctionHeader() for functions returning a pointer to a function.
Definition: c_hll_writer.cpp:1428
std::size_t unnamedStructCounter
Definition: c_hll_writer.h:188
virtual std::string getCommentPrefix() override
Returns the prefix of comments in the given language.
Definition: c_hll_writer.cpp:266
virtual std::string getId() const override
Returns the ID of the writer.
Definition: c_hll_writer.cpp:262
void emitReturnType(ShPtr< FunctionType > funcType)
Emits the return type of the given function.
Definition: c_hll_writer.cpp:1816
void emitFunctionDefinition(ShPtr< Function > func)
Emits the given function definition.
Definition: c_hll_writer.cpp:1341
void emitArrayOfFuncPointers(ShPtr< ArrayType > arrayType, ShPtr< Variable > var=nullptr)
Emits the given array of function pointers, possibly with the given variable.
Definition: c_hll_writer.cpp:1630
void emitPointerToFunc(ShPtr< PointerType > pointerToFuncType, ShPtr< Variable > var=nullptr)
Emits a pointer to the given function, possibly with the given variable.
Definition: c_hll_writer.cpp:1594
void emitGlobalDirectives(ShPtr< Function > func)
void emitInitVarDefWhenNeeded(ShPtr< UForLoopStmt > loop)
Emits the definition of the initialization variable of the given loop when it is needed.
Definition: c_hll_writer.cpp:876
virtual bool emitExternalFunction(ShPtr< Function > func) override
Emits the given linked function, including the ending newline.
Definition: c_hll_writer.cpp:490
void emitArrayDimensions(ShPtr< ArrayType > arrayType)
Emits the dimensions of the given array.
Definition: c_hll_writer.cpp:1686
bool optionEmitFunctionPrototypesForNonLibraryFuncs
Definition: c_hll_writer.h:198
std::map< ShPtr< StructType >, std::string > StructTypeNameMap
Mapping of a structured type into its name.
Definition: c_hll_writer.h:47
void emitNameOfVarIfExists(ShPtr< Variable > var)
Emits the name of var if it is a non-null pointer.
Definition: c_hll_writer.cpp:1823
ShPtr< CompoundOpManager > compoundOpManager
Optimizes AssignStmt to compound operator when possible.
Definition: c_hll_writer.h:181
CHLLWriter(llvm::raw_ostream &out, const std::string &outputFormat="")
Constructs a new C writer.
Definition: c_hll_writer.cpp:240
A base class of all HLL writers.
Definition: hll_writer.h:41
ShPtr< Module > module
The module to be written.
Definition: hll_writer.h:177
UPtr< OutputManager > out
Output where the resulting code will be generated.
Definition: hll_writer.h:180
A base class of all HLL writers.
A library providing API for working with back-end IR.
std::shared_ptr< T > ShPtr
An alias for a shared pointer.
Definition: smart_ptr.h:18
std::vector< ShPtr< StructType > > StructTypeVector
Vector of structured types.
Definition: types.h:111
std::set< ShPtr< Function > > FuncSet
Set of functions.
Definition: types.h:78
Definition: archive_wrapper.h:19
Declarations, aliases, macros, etc. for the use of smart pointers.