retdec
|
A base class of all HLL writers. More...
#include <hll_writer.h>
Public Member Functions | |
virtual std::string | getId () const =0 |
Returns the ID of the writer. More... | |
virtual bool | emitTargetCode (ShPtr< Module > module) |
Emits the code from the given module. More... | |
Options | |
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... | |
![]() | |
virtual | ~Visitor ()=default |
virtual void | visit (ShPtr< GlobalVarDef > varDef)=0 |
virtual void | visit (ShPtr< Function > func)=0 |
virtual void | visit (ShPtr< AssignStmt > stmt)=0 |
virtual void | visit (ShPtr< BreakStmt > stmt)=0 |
virtual void | visit (ShPtr< CallStmt > stmt)=0 |
virtual void | visit (ShPtr< ContinueStmt > stmt)=0 |
virtual void | visit (ShPtr< EmptyStmt > stmt)=0 |
virtual void | visit (ShPtr< ForLoopStmt > stmt)=0 |
virtual void | visit (ShPtr< UForLoopStmt > stmt)=0 |
virtual void | visit (ShPtr< GotoStmt > stmt)=0 |
virtual void | visit (ShPtr< IfStmt > stmt)=0 |
virtual void | visit (ShPtr< ReturnStmt > stmt)=0 |
virtual void | visit (ShPtr< SwitchStmt > stmt)=0 |
virtual void | visit (ShPtr< UnreachableStmt > stmt)=0 |
virtual void | visit (ShPtr< VarDefStmt > stmt)=0 |
virtual void | visit (ShPtr< WhileLoopStmt > stmt)=0 |
virtual void | visit (ShPtr< AddOpExpr > expr)=0 |
virtual void | visit (ShPtr< AddressOpExpr > expr)=0 |
virtual void | visit (ShPtr< AndOpExpr > expr)=0 |
virtual void | visit (ShPtr< ArrayIndexOpExpr > expr)=0 |
virtual void | visit (ShPtr< AssignOpExpr > expr)=0 |
virtual void | visit (ShPtr< BitAndOpExpr > expr)=0 |
virtual void | visit (ShPtr< BitOrOpExpr > expr)=0 |
virtual void | visit (ShPtr< BitShlOpExpr > expr)=0 |
virtual void | visit (ShPtr< BitShrOpExpr > expr)=0 |
virtual void | visit (ShPtr< BitXorOpExpr > expr)=0 |
virtual void | visit (ShPtr< CallExpr > expr)=0 |
virtual void | visit (ShPtr< CommaOpExpr > expr)=0 |
virtual void | visit (ShPtr< DerefOpExpr > expr)=0 |
virtual void | visit (ShPtr< DivOpExpr > expr)=0 |
virtual void | visit (ShPtr< EqOpExpr > expr)=0 |
virtual void | visit (ShPtr< GtEqOpExpr > expr)=0 |
virtual void | visit (ShPtr< GtOpExpr > expr)=0 |
virtual void | visit (ShPtr< LtEqOpExpr > expr)=0 |
virtual void | visit (ShPtr< LtOpExpr > expr)=0 |
virtual void | visit (ShPtr< ModOpExpr > expr)=0 |
virtual void | visit (ShPtr< MulOpExpr > expr)=0 |
virtual void | visit (ShPtr< NegOpExpr > expr)=0 |
virtual void | visit (ShPtr< NeqOpExpr > expr)=0 |
virtual void | visit (ShPtr< NotOpExpr > expr)=0 |
virtual void | visit (ShPtr< OrOpExpr > expr)=0 |
virtual void | visit (ShPtr< StructIndexOpExpr > expr)=0 |
virtual void | visit (ShPtr< SubOpExpr > expr)=0 |
virtual void | visit (ShPtr< TernaryOpExpr > expr)=0 |
virtual void | visit (ShPtr< Variable > var)=0 |
virtual void | visit (ShPtr< BitCastExpr > expr)=0 |
virtual void | visit (ShPtr< ExtCastExpr > expr)=0 |
virtual void | visit (ShPtr< FPToIntCastExpr > expr)=0 |
virtual void | visit (ShPtr< IntToFPCastExpr > expr)=0 |
virtual void | visit (ShPtr< IntToPtrCastExpr > expr)=0 |
virtual void | visit (ShPtr< PtrToIntCastExpr > expr)=0 |
virtual void | visit (ShPtr< TruncCastExpr > expr)=0 |
virtual void | visit (ShPtr< ConstArray > constant)=0 |
virtual void | visit (ShPtr< ConstBool > constant)=0 |
virtual void | visit (ShPtr< ConstFloat > constant)=0 |
virtual void | visit (ShPtr< ConstInt > constant)=0 |
virtual void | visit (ShPtr< ConstNullPointer > constant)=0 |
virtual void | visit (ShPtr< ConstString > constant)=0 |
virtual void | visit (ShPtr< ConstStruct > constant)=0 |
virtual void | visit (ShPtr< ConstSymbol > constant)=0 |
virtual void | visit (ShPtr< ArrayType > type)=0 |
virtual void | visit (ShPtr< FloatType > type)=0 |
virtual void | visit (ShPtr< IntType > type)=0 |
virtual void | visit (ShPtr< PointerType > type)=0 |
virtual void | visit (ShPtr< StringType > type)=0 |
virtual void | visit (ShPtr< StructType > type)=0 |
virtual void | visit (ShPtr< FunctionType > type)=0 |
virtual void | visit (ShPtr< VoidType > type)=0 |
virtual void | visit (ShPtr< UnknownType > type)=0 |
Protected Member Functions | |
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... | |
Commenting | |
virtual std::string | getCommentPrefix ()=0 |
Returns the prefix of comments in the given language. More... | |
Indentation | |
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... | |
Emission | |
virtual bool | emitFileHeader () |
Emits the file header. More... | |
virtual bool | emitGlobalVariablesHeader () |
Emits the header of the global variables block. More... | |
virtual bool | emitGlobalVariables () |
Emits all global variables in the module. 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 | emitFunctionPrototypesHeader () |
Emits the header of the function prototypes block. More... | |
virtual bool | emitFunctionPrototypes () |
Emits function prototypes. 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 | emitExternalFunction (ShPtr< Function > func) |
Emits the given linked function, including the ending newline. 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 §ionName) |
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... | |
Representations | |
virtual std::string | getConstNullPointerTextRepr () const |
Returns the textual representation of a null pointer. More... | |
![]() | |
Visitor ()=default | |
Protected Attributes | |
ShPtr< Module > | module |
The module to be written. More... | |
UPtr< OutputManager > | out |
Output where the resulting code will be generated. More... | |
ShPtr< BracketManager > | bracketsManager |
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< Function > | currFunc |
The currently emitted function definition (if any). More... | |
std::size_t | currFuncGotoLabelCounter |
Counter for goto labels for the current function. More... | |
Private Member Functions | |
std::string | getRawGotoLabel (ShPtr< Statement > stmt) |
Returns a "raw" goto label for the given statement. More... | |
std::string | getReadableClassName (const std::string &cl) const |
Returns the most readable name of the given class. More... | |
StringVector | getReadableClassNames (const StringVector &classes) const |
Returns the most readable names of the given classes. More... | |
Emission of Meta-Information | |
bool | emitMetaInfoDetectedCompilerOrPacker () |
Emits the detected compiler or packer (if any). More... | |
bool | emitMetaInfoDetectedLanguage () |
Emits the detected language (if any). More... | |
bool | emitMetaInfoNumberOfDetectedFuncs () |
Emits the number of detected functions. More... | |
bool | emitMetaInfoSelectedButNotFoundFuncs () |
Emits functions that were selected to be decompiled but were not found (if any). More... | |
bool | emitMetaInfoDecompilationDate () |
Emits the decompilation date. More... | |
![]() | |
NonCopyable (const NonCopyable &)=delete | |
NonCopyable & | operator= (const NonCopyable &)=delete |
NonCopyable ()=default | |
~NonCopyable ()=default | |
Private Attributes | |
std::string | currentIndent |
Spaces to indent the current block. More... | |
A base class of all HLL writers.
Every HLL writer should subclass this class and override at least the needed protected emit*() functions. If the writer needs to do some more magic, like changing the order in which the blocks are emitted, then override emitTargetCode().
Instances of this class have reference object semantics.
|
protected |
Constructs a new writer.
[in] | o | Output into which the HLL code will be emitted. |
[in] | outputFormat | Output format in which to emit the HLL. |
|
protected |
Decreases the indentation level.
|
protected |
Emits address range for the given function (if available).
If there is address range available, emit it in a comment.
true
if some code was emitted, false
otherwise.
|
protected |
Emits the given binary expression.
[in] | opRepr | Textual representation of the operator (without spaces). |
[in] | expr | Expression to be emitted. |
[in] | spaceBefore | Should there be a space before operator? |
[in] | spaceAfter | Should there be a space after operator? |
Brackets are emitted when needed. Use this function if you simply need to emit the operator without any specialties.
|
protectedvirtual |
Emits the class with the given name.
true
if some code has been emitted, false
otherwise.
|
protectedvirtual |
Emits classes.
true
if some code has been emitted, false
otherwise.By default (if it is not overridden), it calls emitClass() for each class.
|
protectedvirtual |
Emits the header of the classes block.
true
if some code has been emitted, false
otherwise. Emits class-related information for the given function (if available).
true
if some code was emitted, false
otherwise. Emits a comment of the given function (if available).
true
if some code was emitted, false
otherwise.
|
protected |
Emits demangled name of the given function (if available).
true
if some code was emitted, false
otherwise.
|
protected |
Emits a description of the detected cryptographic pattern for the given global variable.
The description is emitted in a comment and ended with an new line.
true
if some code was emitted, false
otherwise.
|
protected |
Emits a list of detected cryptographic patterns for the given function (if available).
true
if some code was emitted, false
otherwise.
|
protectedvirtual |
Emits dynamically linked functions in the module.
true
if some code has been emitted, false
otherwise.By default (if it is not overridden), it sort all dynamically linked functions in the module by their name and calls emitExternalFunction() on each of them.
|
protectedvirtual |
Emits the header of the dynamically linked functions block.
true
if some code has been emitted, false
otherwise.
|
protectedvirtual |
Emits the given expression with brackets around it (if needed).
Emits the given linked function, including the ending newline.
[in] | func | Linked function to be emitted. |
By default (if it is not overridden), it emits nothing.
Reimplemented in retdec::llvmir2hll::CHLLWriter.
|
protectedvirtual |
Emits the given external functions.
true
if some code has been emitted, false
otherwise.By default (if it is not overridden), it sort all the given functions and calls emitExternalFunction() on each of them.
|
protectedvirtual |
Emits the file header.
true
if some code has been emitted, false
otherwise.By default (if it is not overridden), it emits the default header.
Reimplemented in retdec::llvmir2hll::CHLLWriter.
Emits the given function, including the ending newline.
[in] | func | Function to be emitted. |
By default, it emits information about func (like address range, module, etc., if available), and then calls func->accept(this)
.
|
protectedvirtual |
Emits function prototypes.
true
if some code has been emitted, false
otherwise.By default (if it is not overridden), it emits nothing.
Reimplemented in retdec::llvmir2hll::CHLLWriter.
|
protectedvirtual |
Emits the header of the function prototypes block.
true
if some code has been emitted, false
otherwise.By default (if it is not overridden), it emits nothing.
Reimplemented in retdec::llvmir2hll::CHLLWriter.
|
protectedvirtual |
Emits functions in the module.
true
if some code has been emitted, false
otherwise.By default (if it is not overridden), it tries to sort the functions in the module and calls emitFunction() on each of them.
|
protectedvirtual |
Emits the header of the functions block.
true
if some code has been emitted, false
otherwise.
|
protectedvirtual |
Emits the given global variable, including the ending newline.
[in] | varDef | Definition of the variable. |
By default (if it is not overridden), it calls varDef->accept(this)
and returns true
.
|
protectedvirtual |
Emits all global variables in the module.
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 in retdec::llvmir2hll::CHLLWriter.
|
protectedvirtual |
Emits the header of the global variables block.
true
if some code has been emitted, false
otherwise.By default (if it is not overridden), it emits nothing.
|
protectedvirtual |
Emits instruction-idiom functions in the module.
true
if some code has been emitted, false
otherwise.By default (if it is not overridden), it sort all instruction-idiom functions in the module by their name and calls emitExternalFunction() on each of them.
|
protectedvirtual |
Emits the header of the instruction-idiom functions block.
true
if some code has been emitted, false
otherwise.
|
protected |
Emits line range for the given function (if available).
If there is line information available, emit the line range in a comment.
true
if some code was emitted, false
otherwise.
|
protectedvirtual |
Emits meta-information.
true
if some code has been emitted, false
otherwise.By default (if it is not overridden), it emits several meta-information concerning the generated code.
|
private |
Emits the decompilation date.
true
if some code was emitted, false
otherwise.
|
private |
Emits the detected compiler or packer (if any).
true
if some code was emitted, false
otherwise.
|
private |
Emits the detected language (if any).
true
if some code was emitted, false
otherwise.
|
protectedvirtual |
Emits the header of the meta-information block.
true
if some code has been emitted, false
otherwise.By default (if it is not overridden), it emits nothing.
|
private |
Emits the number of detected functions.
true
if some code was emitted, false
otherwise.
|
private |
Emits functions that were selected to be decompiled but were not found (if any).
|
protected |
If there is a module name from debug information assigned to the function, emit it in a comment.
true
if some code was emitted, false
otherwise.
|
protected |
Emits a section header comment.
[in] | sectionName | Name of the section. |
The emitted comment is ended with a newline.
|
inlineprotected |
Emits the given sequence seq by calling accept
on every value. Separator = ','.
[in] | seq | Sequence of values to be emitted. |
[in] | space | Space to insert after separator. |
[in] | newline | If true , newline is inserted after separator and before space. |
ContainerType | Container of Visitable values. |
|
protectedvirtual |
Emits statically linked functions in the module.
true
if some code has been emitted, false
otherwise.By default (if it is not overridden), it sort all statically linked functions in the module by their name and calls emitExternalFunction() on each of them.
|
protectedvirtual |
Emits the header of the statically linked functions block.
true
if some code has been emitted, false
otherwise.
|
protectedvirtual |
Emits syscall functions in the module.
true
if some code has been emitted, false
otherwise.By default (if it is not overridden), it sort all syscall functions in the module by their name and calls emitExternalFunction() on each of them.
|
protectedvirtual |
Emits the header of the syscall functions block.
true
if some code has been emitted, false
otherwise. Emits the code from the given module.
[in] | module | Module to be written. |
true
if some code has been emitted, false
otherwise.The functions prints the code blocks in the following order (by calling appropriate emit*() functions):
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 in retdec::llvmir2hll::CHLLWriter.
|
protected |
Emits the given unary expression.
[in] | opRepr | Textual representation of the operator (without spaces). |
[in] | expr | Expression to be emitted. |
Brackets are emitted when needed. Use this function if you simply need to emit the operator without any specialties.
|
protected |
Emits wrapper-related information for the given function (if available).
See the description of Module::getWrappedFuncName() for more details.
true
if some code was emitted, false
otherwise.
|
protectedpure virtual |
Returns the prefix of comments in the given language.
For example, in C, it should return "//"
.
Implemented in retdec::llvmir2hll::CHLLWriter.
|
protectedvirtual |
Returns the textual representation of a null pointer.
By default, it returns "NULL"
.
|
protected |
Returns the current indentation (to indent the current block).
Returns the goto label for the given statement.
|
pure virtual |
Returns the ID of the writer.
Implemented in retdec::llvmir2hll::CHLLWriter.
|
protected |
Returns indentation for a goto label.
Returns a "raw" goto label for the given statement.
"raw" means without any prefix.
|
private |
Returns the most readable name of the given class.
|
private |
Returns the most readable names of the given classes.
|
protected |
Returns the single level of indentation.
The returned value is the string that is used in increaseIndentLevel() and decreaseIndentLevel() to increase and decrease the current indentation, respectively.
|
protected |
Increases the indentation level.
void retdec::llvmir2hll::HLLWriter::setOptionEmitDebugComments | ( | bool | emit = true | ) |
Enables/disables the emission of debug comments.
[in] | emit | If true , enables the emission of debug comments. If false , disables the emission of debug comments. |
void retdec::llvmir2hll::HLLWriter::setOptionEmitTimeVaryingInfo | ( | bool | emit = true | ) |
Enables/disables emission of time-varying information, like dates.
[in] | emit | If true , time-varying information, like dates, will be emitted. |
void retdec::llvmir2hll::HLLWriter::setOptionKeepAllBrackets | ( | bool | keep = true | ) |
Enables/disables the keeping of all brackets.
[in] | keep | If true , all brackets will be kept. If false , redundant brackets will be eliminated. |
void retdec::llvmir2hll::HLLWriter::setOptionUseCompoundOperators | ( | bool | use = true | ) |
Enables/disables usage of compound operators (like +=
) instead of assignments.
[in] | use | If true , compound operators will be used. |
|
protected |
Returns true
if the given constant should be emitted in hexa, false
otherwise.
|
protected |
Checks whether the given array should be emitted in a structured way.
|
protected |
Checks whether the given structure should be emitted in a structured way.
|
protected |
Sort the given list of functions for emission.
If there is line information from debug information available, the functions are sorted by their position in the original source code. If line information is not available but there are address ranges, they are sorted by them. Otherwise, the order is left untouched.
Tries to emit the address of the given variable into a comment.
true
if some code has been emitted, false
otherwise.
|
protected |
Tries to emit a comment with the information about the given variable.
true
if some code has been emitted, false
otherwise.If the emission of debug comments is disabled, this function does nothing and returns false
;
|
protected |
Recognizes which brackets around expressions are needed.
|
private |
Spaces to indent the current block.
The currently emitted function definition (if any).
|
protected |
Counter for goto labels for the current function.
|
protected |
Should we emit constants in a structured way? This variable is used to structure large initializations so that they are properly generated over multiple lines, not on a single line.
|
protected |
Emit also debug comments?
|
protected |
Emit time-varying information, like dates?
|
protected |
Keep all (even redundant) brackets?
|
protected |
Use compound operators (like +=
) instead of assignments?
|
protected |
Output where the resulting code will be generated.