retdec
Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
retdec::llvmir2hll::CHLLWriter Class Reference

A HLL writer for the C language (C99). More...

#include <c_hll_writer.h>

Inheritance diagram for retdec::llvmir2hll::CHLLWriter:
Inheritance graph
[legend]
Collaboration diagram for retdec::llvmir2hll::CHLLWriter:
Collaboration graph
[legend]

Public Member Functions

virtual std::string getId () const override
 Returns the ID of the writer. More...
 
Options
void setOptionEmitFunctionPrototypesForNonLibraryFuncs (bool emit=true)
 
- Public Member Functions inherited from retdec::llvmir2hll::HLLWriter
void setOptionEmitDebugComments (bool emit=true)
 Enables/disables the emission of debug comments. More...
 
void setOptionKeepAllBrackets (bool keep=true)
 Enables/disables the keeping of all brackets. More...
 
void setOptionEmitTimeVaryingInfo (bool emit=true)
 Enables/disables emission of time-varying information, like dates. More...
 
void setOptionUseCompoundOperators (bool use=true)
 Enables/disables usage of compound operators (like +=) instead of assignments. More...
 
- Public Member Functions inherited from retdec::llvmir2hll::Visitor
virtual ~Visitor ()=default
 

Static Public Member Functions

static ShPtr< HLLWritercreate (llvm::raw_ostream &out, const std::string &outputFormat="")
 Creates a new C writer. More...
 

Private Types

using StructTypeNameMap = std::map< ShPtr< StructType >, std::string >
 Mapping of a structured type into its name. More...
 

Private Member Functions

 CHLLWriter (llvm::raw_ostream &out, const std::string &outputFormat="")
 Constructs a new C writer. More...
 
virtual std::string getCommentPrefix () override
 Returns the prefix of comments in the given language. More...
 
virtual bool emitFileHeader () override
 Emits the file header. More...
 
virtual bool emitGlobalVariables () override
 Emits all global variables in the module. More...
 
virtual bool emitFunctionPrototypesHeader () override
 Emits the header of the function prototypes block. More...
 
virtual bool emitFunctionPrototypes () override
 Emits function prototypes. More...
 
virtual bool emitExternalFunction (ShPtr< Function > func) override
 Emits the given linked function, including the ending newline. More...
 
virtual bool emitTargetCode (ShPtr< Module > module) override
 Emits the code from the given module. More...
 
bool shouldEmitFunctionPrototypesHeader () const
 Returns true if we should emit the Function Prototypes header, false otherwise. More...
 
bool emitFunctionPrototypes (const FuncSet &funcs)
 Emits prototypes of the given functions. More...
 
bool emitStandardFunctionPrototypes ()
 Emits standard function prototypes. More...
 
bool emitFunctionPrototypesForNonLibraryFuncs ()
 Emits prototypes for functions which do not have any associated header file. More...
 
bool emitFunctionPrototype (ShPtr< Function > func)
 Emits a prototype of the given function, including the ending newline. More...
 
void emitFunctionDefinition (ShPtr< Function > func)
 Emits the given function definition. More...
 
void emitFunctionHeader (ShPtr< Function > func)
 Emits the header of the given function. More...
 
void emitHeaderOfFuncReturningPointerToFunc (ShPtr< Function > func)
 A specialization of emitFunctionHeader() for functions returning a pointer to a function. More...
 
void emitHeaderOfFuncReturningPointerToArray (ShPtr< Function > func)
 A specialization of emitFunctionHeader() for functions returning a pointer to an array. More...
 
void emitFunctionParameters (ShPtr< Function > func)
 Emits the parameters of the given function. More...
 
void emitVarWithType (ShPtr< Variable > var)
 Emits the given variable alongside with its type. More...
 
void emitPointerToFunc (ShPtr< PointerType > pointerToFuncType, ShPtr< Variable > var=nullptr)
 Emits a pointer to the given function, possibly with the given variable. More...
 
void emitArrayOfFuncPointers (ShPtr< ArrayType > arrayType, ShPtr< Variable > var=nullptr)
 Emits the given array of function pointers, possibly with the given variable. More...
 
void emitPointerToArray (ShPtr< PointerType > pointerToArrayType, ShPtr< Variable > var=nullptr)
 Emits a pointer to the given array, possibly with the given variable. More...
 
void emitArrayDimensions (ShPtr< ArrayType > arrayType)
 Emits the dimensions of the given array. More...
 
void emitArrayDimension (std::size_t size)
 Emits the given array dimension. More...
 
void emitInitializedConstArray (ShPtr< ConstArray > array)
 Emits the given initialized array. More...
 
void emitInitializedConstArrayInline (ShPtr< ConstArray > array)
 Emits the given array inline. More...
 
void emitInitializedConstArrayInStructuredWay (ShPtr< ConstArray > array)
 Emits the given array in a structured way (may span over multiple lines). More...
 
void emitUninitializedConstArray (ShPtr< ConstArray > array)
 Emits the given uninitialized array. More...
 
void emitTypeOfElementsInArray (ShPtr< ArrayType > arrayType)
 Emits the type of elements in the given array. More...
 
void emitCastInStandardWay (ShPtr< CastExpr > expr)
 Emits the given cast in the standard way. More...
 
void emitStarsBeforePointedValue (ShPtr< PointerType > ptrType)
 Emits '*'s before the pointed value (there can be more than one). More...
 
void emitFunctionParameters (ShPtr< FunctionType > funcType)
 Emits parameters of the given function. More...
 
void emitReturnType (ShPtr< FunctionType > funcType)
 Emits the return type of the given function. More...
 
void emitNameOfVarIfExists (ShPtr< Variable > var)
 Emits the name of var if it is a non-null pointer. More...
 
void emitAssignment (ShPtr< Expression > lhs, ShPtr< Expression > rhs)
 Emits the given assignment (without leading or trailing whitespace). More...
 
void emitInitVarDefWhenNeeded (ShPtr< UForLoopStmt > loop)
 Emits the definition of the initialization variable of the given loop when it is needed. More...
 
void emitConstStruct (ShPtr< ConstStruct > constant, bool emitCast=true)
 Emits the given structure literal. More...
 
void emitStructDeclaration (ShPtr< StructType > structType, bool emitInline=false)
 Emits a declaration of the given structure. More...
 
void emitBlock (ShPtr< Statement > stmt)
 Recursively emits the given block. More...
 
void emitGlobalDirectives (ShPtr< Function > func)
 
void emitDebugComment (std::string comment, bool indent=true)
 Emits the given debug comment. More...
 
void emitGotoLabelIfNeeded (ShPtr< Statement > stmt)
 Emits a label of stmt if it is needed. More...
 
std::string getConstFloatSuffixIfNeeded (ShPtr< ConstFloat > constant)
 
std::string genNameForUnnamedStruct (const StructTypeVector &usedStructTypes)
 Generates a new name for an unnamed structure. More...
 
Visitor Interface
virtual void visit (ShPtr< GlobalVarDef > varDef) override
 
virtual void visit (ShPtr< Function > func) override
 
virtual void visit (ShPtr< Variable > var) override
 
virtual void visit (ShPtr< AddressOpExpr > expr) override
 
virtual void visit (ShPtr< AssignOpExpr > expr) override
 
virtual void visit (ShPtr< ArrayIndexOpExpr > expr) override
 
virtual void visit (ShPtr< StructIndexOpExpr > expr) override
 
virtual void visit (ShPtr< DerefOpExpr > expr) override
 
virtual void visit (ShPtr< NotOpExpr > expr) override
 
virtual void visit (ShPtr< NegOpExpr > expr) override
 
virtual void visit (ShPtr< EqOpExpr > expr) override
 
virtual void visit (ShPtr< NeqOpExpr > expr) override
 
virtual void visit (ShPtr< LtEqOpExpr > expr) override
 
virtual void visit (ShPtr< GtEqOpExpr > expr) override
 
virtual void visit (ShPtr< LtOpExpr > expr) override
 
virtual void visit (ShPtr< GtOpExpr > expr) override
 
virtual void visit (ShPtr< AddOpExpr > expr) override
 
virtual void visit (ShPtr< SubOpExpr > expr) override
 
virtual void visit (ShPtr< MulOpExpr > expr) override
 
virtual void visit (ShPtr< ModOpExpr > expr) override
 
virtual void visit (ShPtr< DivOpExpr > expr) override
 
virtual void visit (ShPtr< AndOpExpr > expr) override
 
virtual void visit (ShPtr< OrOpExpr > expr) override
 
virtual void visit (ShPtr< BitAndOpExpr > expr) override
 
virtual void visit (ShPtr< BitOrOpExpr > expr) override
 
virtual void visit (ShPtr< BitXorOpExpr > expr) override
 
virtual void visit (ShPtr< BitShlOpExpr > expr) override
 
virtual void visit (ShPtr< BitShrOpExpr > expr) override
 
virtual void visit (ShPtr< TernaryOpExpr > expr) override
 
virtual void visit (ShPtr< CallExpr > expr) override
 
virtual void visit (ShPtr< CommaOpExpr > expr) override
 
virtual void visit (ShPtr< ConstBool > constant) override
 
virtual void visit (ShPtr< ConstFloat > constant) override
 
virtual void visit (ShPtr< ConstInt > constant) override
 
virtual void visit (ShPtr< ConstNullPointer > constant) override
 
virtual void visit (ShPtr< ConstString > constant) override
 
virtual void visit (ShPtr< ConstArray > constant) override
 
virtual void visit (ShPtr< ConstStruct > constant) override
 
virtual void visit (ShPtr< ConstSymbol > constant) override
 
virtual void visit (ShPtr< AssignStmt > stmt) override
 
virtual void visit (ShPtr< VarDefStmt > stmt) override
 
virtual void visit (ShPtr< CallStmt > stmt) override
 
virtual void visit (ShPtr< ReturnStmt > stmt) override
 
virtual void visit (ShPtr< EmptyStmt > stmt) override
 
virtual void visit (ShPtr< IfStmt > stmt) override
 
virtual void visit (ShPtr< SwitchStmt > stmt) override
 
virtual void visit (ShPtr< WhileLoopStmt > stmt) override
 
virtual void visit (ShPtr< ForLoopStmt > stmt) override
 
virtual void visit (ShPtr< UForLoopStmt > stmt) override
 
virtual void visit (ShPtr< BreakStmt > stmt) override
 
virtual void visit (ShPtr< ContinueStmt > stmt) override
 
virtual void visit (ShPtr< GotoStmt > stmt) override
 
virtual void visit (ShPtr< UnreachableStmt > stmt) override
 
virtual void visit (ShPtr< FloatType > type) override
 
virtual void visit (ShPtr< IntType > type) override
 
virtual void visit (ShPtr< PointerType > type) override
 
virtual void visit (ShPtr< StringType > type) override
 
virtual void visit (ShPtr< ArrayType > type) override
 
virtual void visit (ShPtr< StructType > type) override
 
virtual void visit (ShPtr< FunctionType > type) override
 
virtual void visit (ShPtr< VoidType > type) override
 
virtual void visit (ShPtr< UnknownType > type) override
 
virtual void visit (ShPtr< BitCastExpr > expr) override
 
virtual void visit (ShPtr< ExtCastExpr > expr) override
 
virtual void visit (ShPtr< TruncCastExpr > expr) override
 
virtual void visit (ShPtr< FPToIntCastExpr > expr) override
 
virtual void visit (ShPtr< IntToFPCastExpr > expr) override
 
virtual void visit (ShPtr< IntToPtrCastExpr > expr) override
 
virtual void visit (ShPtr< PtrToIntCastExpr > expr) override
 

Private Attributes

ShPtr< CompoundOpManagercompoundOpManager
 Optimizes AssignStmt to compound operator when possible. More...
 
StructTypeNameMap structNames
 Mapping of a structured type into its name. More...
 
std::size_t unnamedStructCounter
 
bool emittingGlobalVarDefs
 
bool optionEmitFunctionPrototypesForNonLibraryFuncs
 

Additional Inherited Members

- Protected Member Functions inherited from retdec::llvmir2hll::HLLWriter
 HLLWriter (llvm::raw_ostream &out, const std::string &outputFormat="")
 Constructs a new writer. More...
 
void sortFuncsForEmission (FuncVector &funcs)
 Sort the given list of functions for emission. More...
 
bool tryEmitVarInfoInComment (ShPtr< Variable > var, ShPtr< Statement > stmt=nullptr)
 Tries to emit a comment with the information about the given variable. More...
 
bool tryEmitVarAddressInComment (ShPtr< Variable > var)
 Tries to emit the address of the given variable into a comment. More...
 
bool shouldBeEmittedInHexa (ShPtr< ConstInt > constant) const
 Returns true if the given constant should be emitted in hexa, false otherwise. More...
 
bool shouldBeEmittedInStructuredWay (ShPtr< ConstArray > array) const
 Checks whether the given array should be emitted in a structured way. More...
 
bool shouldBeEmittedInStructuredWay (ShPtr< ConstStruct > structure) const
 Checks whether the given structure should be emitted in a structured way. More...
 
std::string getGotoLabel (ShPtr< Statement > stmt)
 Returns the goto label for the given statement. More...
 
void increaseIndentLevel ()
 Increases the indentation level. More...
 
void decreaseIndentLevel ()
 Decreases the indentation level. More...
 
std::string getCurrentIndent () const
 Returns the current indentation (to indent the current block). More...
 
std::string getSingleLevelIndent () const
 Returns the single level of indentation. More...
 
std::string getIndentForGotoLabel () const
 Returns indentation for a goto label. More...
 
virtual bool emitGlobalVariablesHeader ()
 Emits the header of the global variables block. More...
 
virtual bool emitGlobalVariable (ShPtr< GlobalVarDef > varDef)
 Emits the given global variable, including the ending newline. More...
 
virtual bool emitClassesHeader ()
 Emits the header of the classes block. More...
 
virtual bool emitClasses ()
 Emits classes. More...
 
virtual bool emitClass (const std::string &className)
 Emits the class with the given name. More...
 
virtual bool emitFunctionsHeader ()
 Emits the header of the functions block. More...
 
virtual bool emitFunctions ()
 Emits functions in the module. More...
 
virtual bool emitFunction (ShPtr< Function > func)
 Emits the given function, including the ending newline. More...
 
virtual bool emitStaticallyLinkedFunctionsHeader ()
 Emits the header of the statically linked functions block. More...
 
virtual bool emitStaticallyLinkedFunctions ()
 Emits statically linked functions in the module. More...
 
virtual bool emitDynamicallyLinkedFunctionsHeader ()
 Emits the header of the dynamically linked functions block. More...
 
virtual bool emitDynamicallyLinkedFunctions ()
 Emits dynamically linked functions in the module. More...
 
virtual bool emitSyscallFunctionsHeader ()
 Emits the header of the syscall functions block. More...
 
virtual bool emitSyscallFunctions ()
 Emits syscall functions in the module. More...
 
virtual bool emitInstructionIdiomFunctionsHeader ()
 Emits the header of the instruction-idiom functions block. More...
 
virtual bool emitInstructionIdiomFunctions ()
 Emits instruction-idiom functions in the module. More...
 
virtual bool emitExternalFunctions (const FuncSet &funcs)
 Emits the given external functions. More...
 
virtual bool emitMetaInfoHeader ()
 Emits the header of the meta-information block. More...
 
virtual bool emitMetaInfo ()
 Emits meta-information. More...
 
virtual void emitExprWithBracketsIfNeeded (ShPtr< Expression > expr)
 Emits the given expression with brackets around it (if needed). More...
 
void emitUnaryOpExpr (const std::string &opRepr, ShPtr< UnaryOpExpr > expr)
 Emits the given unary expression. More...
 
void emitBinaryOpExpr (const std::string &opRepr, ShPtr< BinaryOpExpr > expr, bool spaceBefore=true, bool spaceAfter=true)
 Emits the given binary expression. More...
 
bool emitDetectedCryptoPatternForGlobalVarIfAvailable (ShPtr< Variable > var)
 Emits a description of the detected cryptographic pattern for the given global variable. More...
 
bool emitModuleNameForFuncIfAvailable (ShPtr< Function > func)
 If there is a module name from debug information assigned to the function, emit it in a comment. More...
 
bool emitAddressRangeForFuncIfAvailable (ShPtr< Function > func)
 Emits address range for the given function (if available). More...
 
bool emitLineRangeForFuncIfAvailable (ShPtr< Function > func)
 Emits line range for the given function (if available). More...
 
bool emitWrapperInfoForFuncIfAvailable (ShPtr< Function > func)
 Emits wrapper-related information for the given function (if available). More...
 
bool emitClassInfoIfAvailable (ShPtr< Function > func)
 Emits class-related information for the given function (if available). More...
 
bool emitDemangledNameIfAvailable (ShPtr< Function > func)
 Emits demangled name of the given function (if available). More...
 
bool emitCommentIfAvailable (ShPtr< Function > func)
 Emits a comment of the given function (if available). More...
 
bool emitDetectedCryptoPatternsForFuncIfAvailable (ShPtr< Function > func)
 Emits a list of detected cryptographic patterns for the given function (if available). More...
 
void emitSectionHeader (const std::string &sectionName)
 Emits a section header comment. More...
 
template<class ContainerType >
void emitSequenceWithAccept (const ContainerType &seq, const std::string &space=" ", bool newline=false)
 Emits the given sequence seq by calling accept on every value. Separator = ','. More...
 
virtual std::string getConstNullPointerTextRepr () const
 Returns the textual representation of a null pointer. More...
 
- Protected Member Functions inherited from retdec::llvmir2hll::Visitor
 Visitor ()=default
 
- Protected Attributes inherited from retdec::llvmir2hll::HLLWriter
ShPtr< Modulemodule
 The module to be written. More...
 
UPtr< OutputManagerout
 Output where the resulting code will be generated. More...
 
ShPtr< BracketManagerbracketsManager
 Recognizes which brackets around expressions are needed. More...
 
bool emitConstantsInStructuredWay
 
bool optionEmitDebugComments
 Emit also debug comments? More...
 
bool optionKeepAllBrackets
 Keep all (even redundant) brackets? More...
 
bool optionEmitTimeVaryingInfo
 Emit time-varying information, like dates? More...
 
bool optionUseCompoundOperators
 Use compound operators (like +=) instead of assignments? More...
 
ShPtr< FunctioncurrFunc
 The currently emitted function definition (if any). More...
 
std::size_t currFuncGotoLabelCounter
 Counter for goto labels for the current function. More...
 

Detailed Description

A HLL writer for the C language (C99).

Use create() to create instances. Instances of this class have reference object semantics.

Member Typedef Documentation

◆ StructTypeNameMap

using retdec::llvmir2hll::CHLLWriter::StructTypeNameMap = std::map<ShPtr<StructType>, std::string>
private

Mapping of a structured type into its name.

Constructor & Destructor Documentation

◆ CHLLWriter()

retdec::llvmir2hll::CHLLWriter::CHLLWriter ( llvm::raw_ostream &  o,
const std::string &  outputFormat = "" 
)
private

Constructs a new C writer.

See create() for the description of parameters.

Member Function Documentation

◆ create()

ShPtr< HLLWriter > retdec::llvmir2hll::CHLLWriter::create ( llvm::raw_ostream &  out,
const std::string &  outputFormat = "" 
)
static

Creates a new C writer.

Parameters
[in]outOutput stream into which the HLL code will be emitted.
[in]outputFormat

◆ emitArrayDimension()

void retdec::llvmir2hll::CHLLWriter::emitArrayDimension ( std::size_t  size)
private

Emits the given array dimension.

◆ emitArrayDimensions()

void retdec::llvmir2hll::CHLLWriter::emitArrayDimensions ( ShPtr< ArrayType arrayType)
private

Emits the dimensions of the given array.

◆ emitArrayOfFuncPointers()

void retdec::llvmir2hll::CHLLWriter::emitArrayOfFuncPointers ( ShPtr< ArrayType arrayType,
ShPtr< Variable var = nullptr 
)
private

Emits the given array of function pointers, possibly with the given variable.

Let us assume that arrayType is a three-item array of pointers to functions that have a single parameter of type int. Then, if we do not pass var, the following code is emitted:

void (*[3])(int)

However, if var is non-null and its name is foo, then the following code is emitted:

void (*foo[3])(int)
Preconditions
  • arrayType is an array of function pointers

◆ emitAssignment()

void retdec::llvmir2hll::CHLLWriter::emitAssignment ( ShPtr< Expression lhs,
ShPtr< Expression rhs 
)
private

Emits the given assignment (without leading or trailing whitespace).

◆ emitBlock()

void retdec::llvmir2hll::CHLLWriter::emitBlock ( ShPtr< Statement stmt)
private

Recursively emits the given block.

Parameters
[in]stmtBlock to be emitted.

Before emitting the block, the indentation level is increased. After the block is emitted, the indentation level is decreased.

If a statement has some associated metadata, they're emitted in a comment before the statement.

◆ emitCastInStandardWay()

void retdec::llvmir2hll::CHLLWriter::emitCastInStandardWay ( ShPtr< CastExpr expr)
private

Emits the given cast in the standard way.

◆ emitConstStruct()

void retdec::llvmir2hll::CHLLWriter::emitConstStruct ( ShPtr< ConstStruct constant,
bool  emitCast = true 
)
private

Emits the given structure literal.

Parameters
[in]constantStructure literal to be emitted.
[in]emitCastEmit a cast before the literal?

If emitCast is true, this function emits a cast of the form (structure X) before the literal, where X is the name of the structure. This is needed if the literal is used outside of the right-hand side of VarDefStmt.

When the emittingGlobalVarDefs data member is set to true, this function does not emit any casts, even if emitCast is true. The reason is that on the global level, casts of structures are considered non-constant. For example, if we emit

struct struct4 t = {.e0 = (struct struct3){.e0 = 0}};

we get the following warning:

error: initializer element is not constant

Instead, we have to emit just

struct struct4 t = {.e0 = {.e0 = 0}};

i.e. drop the cast.

When the emitConstantsInStructuredWay data member is set to true, the constant may be emitted in a structured way, i.e. spanning over multiple lines. Whether it is actually emitted in this way depends on the result of shouldBeEmittedInStructuredWay().

◆ emitDebugComment()

void retdec::llvmir2hll::CHLLWriter::emitDebugComment ( std::string  comment,
bool  indent = true 
)
private

Emits the given debug comment.

Parameters
[in]commentDebug comment to be emitted.
[in]indentIf true, it indents the comment with getCurrentIndent().

If optionEmitDebugComments is false, this function emits nothing. A new line is emitted after the comment.

◆ emitExternalFunction()

bool retdec::llvmir2hll::CHLLWriter::emitExternalFunction ( ShPtr< Function func)
overrideprivatevirtual

Emits the given linked function, including the ending newline.

Parameters
[in]funcLinked function to be emitted.
Preconditions
  • func is non-null

By default (if it is not overridden), it emits nothing.

Reimplemented from retdec::llvmir2hll::HLLWriter.

◆ emitFileHeader()

bool retdec::llvmir2hll::CHLLWriter::emitFileHeader ( )
overrideprivatevirtual

Emits the file header.

Returns
true if some code has been emitted, false otherwise.

By default (if it is not overridden), it emits the default header.

Reimplemented from retdec::llvmir2hll::HLLWriter.

◆ emitFunctionDefinition()

void retdec::llvmir2hll::CHLLWriter::emitFunctionDefinition ( ShPtr< Function func)
private

Emits the given function definition.

Preconditions
  • func is a function efinition

◆ emitFunctionHeader()

void retdec::llvmir2hll::CHLLWriter::emitFunctionHeader ( ShPtr< Function func)
private

Emits the header of the given function.

Consider the following two functions:

void func1(int a, int b);
float func2(float a) { return a + 1; }

For func1(), it emits

void func1(int a, int b)

and for func2(), it emits

float func2(float a)

A newline is NOT emitted.

◆ emitFunctionParameters() [1/2]

void retdec::llvmir2hll::CHLLWriter::emitFunctionParameters ( ShPtr< Function func)
private

Emits the parameters of the given function.

For example, if the function is

void func(int a, char *p, ...);

this function emits

int a, char *p, ...

◆ emitFunctionParameters() [2/2]

void retdec::llvmir2hll::CHLLWriter::emitFunctionParameters ( ShPtr< FunctionType funcType)
private

Emits parameters of the given function.

◆ emitFunctionPrototype()

bool retdec::llvmir2hll::CHLLWriter::emitFunctionPrototype ( ShPtr< Function func)
private

Emits a prototype of the given function, including the ending newline.

Parameters
[in]funcFunction whose prototype is to be emitted.
Returns
true if some code has been emitted, false otherwise.
Preconditions
  • func is non-null

◆ emitFunctionPrototypes() [1/2]

bool retdec::llvmir2hll::CHLLWriter::emitFunctionPrototypes ( )
overrideprivatevirtual

Emits function prototypes.

Returns
true if some code has been emitted, false otherwise.

By default (if it is not overridden), it emits nothing.

Reimplemented from retdec::llvmir2hll::HLLWriter.

◆ emitFunctionPrototypes() [2/2]

bool retdec::llvmir2hll::CHLLWriter::emitFunctionPrototypes ( const FuncSet funcs)
private

Emits prototypes of the given functions.

Returns
true if some code has been emitted, false otherwise.

◆ emitFunctionPrototypesForNonLibraryFuncs()

bool retdec::llvmir2hll::CHLLWriter::emitFunctionPrototypesForNonLibraryFuncs ( )
private

Emits prototypes for functions which do not have any associated header file.

Returns
true if some code has been emitted, false otherwise.

◆ emitFunctionPrototypesHeader()

bool retdec::llvmir2hll::CHLLWriter::emitFunctionPrototypesHeader ( )
overrideprivatevirtual

Emits the header of the function prototypes block.

Returns
true if some code has been emitted, false otherwise.

By default (if it is not overridden), it emits nothing.

Reimplemented from retdec::llvmir2hll::HLLWriter.

◆ emitGlobalDirectives()

void retdec::llvmir2hll::CHLLWriter::emitGlobalDirectives ( ShPtr< Function func)
private

◆ emitGlobalVariables()

bool retdec::llvmir2hll::CHLLWriter::emitGlobalVariables ( )
overrideprivatevirtual

Emits all global variables in the module.

Returns
true if some code has been emitted, false otherwise.

By default (if it is not overridden), it it calls emitGlobalVariable() on each global variable in the module.

Reimplemented from retdec::llvmir2hll::HLLWriter.

◆ emitGotoLabelIfNeeded()

void retdec::llvmir2hll::CHLLWriter::emitGotoLabelIfNeeded ( ShPtr< Statement stmt)
private

Emits a label of stmt if it is needed.

A label is needed if stmt is the target of a goto statement.

◆ emitHeaderOfFuncReturningPointerToArray()

void retdec::llvmir2hll::CHLLWriter::emitHeaderOfFuncReturningPointerToArray ( ShPtr< Function func)
private

A specialization of emitFunctionHeader() for functions returning a pointer to an array.

See also
emitFunctionHeader()

◆ emitHeaderOfFuncReturningPointerToFunc()

void retdec::llvmir2hll::CHLLWriter::emitHeaderOfFuncReturningPointerToFunc ( ShPtr< Function func)
private

A specialization of emitFunctionHeader() for functions returning a pointer to a function.

See also
emitFunctionHeader()

◆ emitInitializedConstArray()

void retdec::llvmir2hll::CHLLWriter::emitInitializedConstArray ( ShPtr< ConstArray array)
private

Emits the given initialized array.

Preconditions
  • array is non-null and initialized

When the emitConstantsInStructuredWay data member is set to true, the constant may be emitted in a structured way, i.e. spanning over multiple lines. Whether it is actually emitted in this way depends on the result of shouldBeEmittedInStructuredWay().

◆ emitInitializedConstArrayInline()

void retdec::llvmir2hll::CHLLWriter::emitInitializedConstArrayInline ( ShPtr< ConstArray array)
private

Emits the given array inline.

◆ emitInitializedConstArrayInStructuredWay()

void retdec::llvmir2hll::CHLLWriter::emitInitializedConstArrayInStructuredWay ( ShPtr< ConstArray array)
private

Emits the given array in a structured way (may span over multiple lines).

◆ emitInitVarDefWhenNeeded()

void retdec::llvmir2hll::CHLLWriter::emitInitVarDefWhenNeeded ( ShPtr< UForLoopStmt loop)
private

Emits the definition of the initialization variable of the given loop when it is needed.

◆ emitNameOfVarIfExists()

void retdec::llvmir2hll::CHLLWriter::emitNameOfVarIfExists ( ShPtr< Variable var)
private

Emits the name of var if it is a non-null pointer.

◆ emitPointerToArray()

void retdec::llvmir2hll::CHLLWriter::emitPointerToArray ( ShPtr< PointerType pointerToArrayType,
ShPtr< Variable var = nullptr 
)
private

Emits a pointer to the given array, possibly with the given variable.

Let us assume that arrayType is a pointer to an array of 10 ints. Then, if we do not pass var, the following code is emitted:

int (*)[10]

However, if var is non-null and its name is foo, then the following code is emitted:

int (*foo)[10]
Preconditions
  • pointerToArrayType is a pointer to an array

◆ emitPointerToFunc()

void retdec::llvmir2hll::CHLLWriter::emitPointerToFunc ( ShPtr< PointerType pointerToFuncType,
ShPtr< Variable var = nullptr 
)
private

Emits a pointer to the given function, possibly with the given variable.

Let us assume that funcType is a pointer to a void function, which has a single parameter of type int. Then, if we do not pass var, the following code is emitted:

void (*)(int)

However, if var is non-null and its name is foo, then the following code is emitted:

void (*foo)(int)
Preconditions
  • pointerToFuncType is a pointer to a function

◆ emitReturnType()

void retdec::llvmir2hll::CHLLWriter::emitReturnType ( ShPtr< FunctionType funcType)
private

Emits the return type of the given function.

◆ emitStandardFunctionPrototypes()

bool retdec::llvmir2hll::CHLLWriter::emitStandardFunctionPrototypes ( )
private

Emits standard function prototypes.

Returns
true if some code was emitted, false otherwise.

◆ emitStarsBeforePointedValue()

void retdec::llvmir2hll::CHLLWriter::emitStarsBeforePointedValue ( ShPtr< PointerType ptrType)
private

Emits '*'s before the pointed value (there can be more than one).

◆ emitStructDeclaration()

void retdec::llvmir2hll::CHLLWriter::emitStructDeclaration ( ShPtr< StructType structType,
bool  emitInline = false 
)
private

Emits a declaration of the given structure.

Parameters
[in]structTypeType of the structure whose declaration is to be emitted.
[in]emitInlineIf true, the type is emitted without any newlines.

Example: When emitInline is false, it emits

struct Name {
type1 name1;
type2 name2;
...
}

without a trailing newline. When emitInline is true, it emits

struct Name { type1 name1; type2 name2; ... }

Again, there is no trailing newline.

◆ emitTargetCode()

bool retdec::llvmir2hll::CHLLWriter::emitTargetCode ( ShPtr< Module module)
overrideprivatevirtual

Emits the code from the given module.

Parameters
[in]moduleModule to be written.
Returns
true if some code has been emitted, false otherwise.

The functions prints the code blocks in the following order (by calling appropriate emit*() functions):

  • file header
  • function prototypes header, function prototypes
  • global header, global variables
  • functions header, functions
  • external functions header, external functions (there are many types of external functions; a separate section is emitted for each type)
  • meta-information header, meta-information

Each block is separated by a blank line. A subclass of this class can just override the appropriate emit*() functions. To change the order of blocks, override this function.

Reimplemented from retdec::llvmir2hll::HLLWriter.

◆ emitTypeOfElementsInArray()

void retdec::llvmir2hll::CHLLWriter::emitTypeOfElementsInArray ( ShPtr< ArrayType arrayType)
private

Emits the type of elements in the given array.

◆ emitUninitializedConstArray()

void retdec::llvmir2hll::CHLLWriter::emitUninitializedConstArray ( ShPtr< ConstArray array)
private

Emits the given uninitialized array.

Preconditions
  • array is non-null and uninitialized

◆ emitVarWithType()

void retdec::llvmir2hll::CHLLWriter::emitVarWithType ( ShPtr< Variable var)
private

Emits the given variable alongside with its type.

For example, if var is named foo and is of type int, the following code is emitted:

int foo
Preconditions
  • var is non-null

◆ genNameForUnnamedStruct()

std::string retdec::llvmir2hll::CHLLWriter::genNameForUnnamedStruct ( const StructTypeVector usedStructTypes)
private

Generates a new name for an unnamed structure.

Parameters
[in]usedStructTypesAll used structured types.

The usedStructTypes parameter is needed because all the created names have to differ from all the existing names of structures.

◆ getCommentPrefix()

std::string retdec::llvmir2hll::CHLLWriter::getCommentPrefix ( )
overrideprivatevirtual

Returns the prefix of comments in the given language.

For example, in C, it should return "//".

Implements retdec::llvmir2hll::HLLWriter.

◆ getConstFloatSuffixIfNeeded()

std::string retdec::llvmir2hll::CHLLWriter::getConstFloatSuffixIfNeeded ( ShPtr< ConstFloat constant)
private
Returns
Get a suffix for the given floating-point constant (if needed).

◆ getId()

std::string retdec::llvmir2hll::CHLLWriter::getId ( ) const
overridevirtual

Returns the ID of the writer.

Implements retdec::llvmir2hll::HLLWriter.

◆ setOptionEmitFunctionPrototypesForNonLibraryFuncs()

void retdec::llvmir2hll::CHLLWriter::setOptionEmitFunctionPrototypesForNonLibraryFuncs ( bool  emit = true)

◆ shouldEmitFunctionPrototypesHeader()

bool retdec::llvmir2hll::CHLLWriter::shouldEmitFunctionPrototypesHeader ( ) const
private

Returns true if we should emit the Function Prototypes header, false otherwise.

◆ visit() [1/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< AddOpExpr expr)
overrideprivatevirtual

◆ visit() [2/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< AddressOpExpr expr)
overrideprivatevirtual

◆ visit() [3/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< AndOpExpr expr)
overrideprivatevirtual

◆ visit() [4/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ArrayIndexOpExpr expr)
overrideprivatevirtual

◆ visit() [5/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ArrayType type)
overrideprivatevirtual

◆ visit() [6/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< AssignOpExpr expr)
overrideprivatevirtual

◆ visit() [7/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< AssignStmt stmt)
overrideprivatevirtual

◆ visit() [8/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< BitAndOpExpr expr)
overrideprivatevirtual

◆ visit() [9/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< BitCastExpr expr)
overrideprivatevirtual

◆ visit() [10/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< BitOrOpExpr expr)
overrideprivatevirtual

◆ visit() [11/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< BitShlOpExpr expr)
overrideprivatevirtual

◆ visit() [12/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< BitShrOpExpr expr)
overrideprivatevirtual

◆ visit() [13/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< BitXorOpExpr expr)
overrideprivatevirtual

◆ visit() [14/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< BreakStmt stmt)
overrideprivatevirtual

◆ visit() [15/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< CallExpr expr)
overrideprivatevirtual

◆ visit() [16/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< CallStmt stmt)
overrideprivatevirtual

◆ visit() [17/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< CommaOpExpr expr)
overrideprivatevirtual

◆ visit() [18/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ConstArray constant)
overrideprivatevirtual

◆ visit() [19/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ConstBool constant)
overrideprivatevirtual

◆ visit() [20/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ConstFloat constant)
overrideprivatevirtual

◆ visit() [21/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ConstInt constant)
overrideprivatevirtual

◆ visit() [22/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ConstNullPointer constant)
overrideprivatevirtual

◆ visit() [23/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ConstString constant)
overrideprivatevirtual

◆ visit() [24/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ConstStruct constant)
overrideprivatevirtual

◆ visit() [25/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ConstSymbol constant)
overrideprivatevirtual

◆ visit() [26/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ContinueStmt stmt)
overrideprivatevirtual

◆ visit() [27/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< DerefOpExpr expr)
overrideprivatevirtual

◆ visit() [28/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< DivOpExpr expr)
overrideprivatevirtual

◆ visit() [29/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< EmptyStmt stmt)
overrideprivatevirtual

◆ visit() [30/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< EqOpExpr expr)
overrideprivatevirtual

◆ visit() [31/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ExtCastExpr expr)
overrideprivatevirtual

◆ visit() [32/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< FloatType type)
overrideprivatevirtual

◆ visit() [33/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ForLoopStmt stmt)
overrideprivatevirtual

◆ visit() [34/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< FPToIntCastExpr expr)
overrideprivatevirtual

◆ visit() [35/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< Function func)
overrideprivatevirtual

◆ visit() [36/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< FunctionType type)
overrideprivatevirtual

◆ visit() [37/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< GlobalVarDef varDef)
overrideprivatevirtual

◆ visit() [38/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< GotoStmt stmt)
overrideprivatevirtual

◆ visit() [39/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< GtEqOpExpr expr)
overrideprivatevirtual

◆ visit() [40/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< GtOpExpr expr)
overrideprivatevirtual

◆ visit() [41/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< IfStmt stmt)
overrideprivatevirtual

◆ visit() [42/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< IntToFPCastExpr expr)
overrideprivatevirtual

◆ visit() [43/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< IntToPtrCastExpr expr)
overrideprivatevirtual

◆ visit() [44/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< IntType type)
overrideprivatevirtual

◆ visit() [45/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< LtEqOpExpr expr)
overrideprivatevirtual

◆ visit() [46/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< LtOpExpr expr)
overrideprivatevirtual

◆ visit() [47/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ModOpExpr expr)
overrideprivatevirtual

◆ visit() [48/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< MulOpExpr expr)
overrideprivatevirtual

◆ visit() [49/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< NegOpExpr expr)
overrideprivatevirtual

◆ visit() [50/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< NeqOpExpr expr)
overrideprivatevirtual

◆ visit() [51/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< NotOpExpr expr)
overrideprivatevirtual

◆ visit() [52/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< OrOpExpr expr)
overrideprivatevirtual

◆ visit() [53/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< PointerType type)
overrideprivatevirtual

◆ visit() [54/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< PtrToIntCastExpr expr)
overrideprivatevirtual

◆ visit() [55/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< ReturnStmt stmt)
overrideprivatevirtual

◆ visit() [56/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< StringType type)
overrideprivatevirtual

◆ visit() [57/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< StructIndexOpExpr expr)
overrideprivatevirtual

◆ visit() [58/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< StructType type)
overrideprivatevirtual

◆ visit() [59/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< SubOpExpr expr)
overrideprivatevirtual

◆ visit() [60/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< SwitchStmt stmt)
overrideprivatevirtual

◆ visit() [61/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< TernaryOpExpr expr)
overrideprivatevirtual

◆ visit() [62/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< TruncCastExpr expr)
overrideprivatevirtual

◆ visit() [63/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< UForLoopStmt stmt)
overrideprivatevirtual

◆ visit() [64/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< UnknownType type)
overrideprivatevirtual

◆ visit() [65/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< UnreachableStmt stmt)
overrideprivatevirtual

◆ visit() [66/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< VarDefStmt stmt)
overrideprivatevirtual

◆ visit() [67/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< Variable var)
overrideprivatevirtual

◆ visit() [68/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< VoidType type)
overrideprivatevirtual

◆ visit() [69/69]

void retdec::llvmir2hll::CHLLWriter::visit ( ShPtr< WhileLoopStmt stmt)
overrideprivatevirtual

Member Data Documentation

◆ compoundOpManager

ShPtr<CompoundOpManager> retdec::llvmir2hll::CHLLWriter::compoundOpManager
private

Optimizes AssignStmt to compound operator when possible.

◆ emittingGlobalVarDefs

bool retdec::llvmir2hll::CHLLWriter::emittingGlobalVarDefs
private

Are we emitting global variables? This variable is needed because we cannot emit casts of structures inside the initialization of global variables. See emitConstStruct() for more details.

◆ optionEmitFunctionPrototypesForNonLibraryFuncs

bool retdec::llvmir2hll::CHLLWriter::optionEmitFunctionPrototypesForNonLibraryFuncs
private

Emit prototypes for functions which do not have any associated header file?

◆ structNames

StructTypeNameMap retdec::llvmir2hll::CHLLWriter::structNames
private

Mapping of a structured type into its name.

◆ unnamedStructCounter

std::size_t retdec::llvmir2hll::CHLLWriter::unnamedStructCounter
private

A counter for unnamed structures. It is needed for assigning names to unnamed structures.


The documentation for this class was generated from the following files: