decompiler  1.0.0
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Attributes | List of all members
ghidra::PrintLanguage Class Referenceabstract

The base class API for emitting a high-level language. More...

#include <printlanguage.hh>

Inheritance diagram for ghidra::PrintLanguage:
ghidra::PrintC ghidra::PrintJava

Classes

struct  Atom
 A single non-operator token emitted by the decompiler. More...
 
struct  NodePending
 A pending data-flow node; waiting to be placed on the reverse polish notation stack. More...
 
struct  ReversePolish
 An entry on the reverse polish notation (RPN) stack. More...
 

Public Types

enum  modifiers {
  force_hex = 1, force_dec = 2, bestfit = 4, force_scinote = 8,
  force_pointer = 0x10, print_load_value = 0x20, print_store_value = 0x40, no_branch = 0x80,
  only_branch = 0x100, comma_separate = 0x200, flat = 0x400, falsebranch = 0x800,
  nofallthru = 0x1000, negatetoken = 0x2000, hide_thisparam = 0x4000, pending_brace = 0x8000
}
 Possible context sensitive modifiers to how tokens get emitted. More...
 
enum  tagtype {
  syntax, vartoken, functoken, optoken,
  typetoken, fieldtoken, blanktoken
}
 Possible types of Atom. More...
 
enum  namespace_strategy { MINIMAL_NAMESPACES = 0, NO_NAMESPACES = 1, ALL_NAMESPACES = 2 }
 Strategies for displaying namespace tokens. More...
 

Public Member Functions

 PrintLanguage (Architecture *g, const string &nm)
 Constructor. More...
 
virtual ~PrintLanguage (void)
 Destructor.
 
const string & getName (void) const
 Get the language name.
 
CastStrategygetCastStrategy (void) const
 Get the casting strategy for the language.
 
ostream * getOutputStream (void) const
 Get the output stream being emitted to.
 
void setOutputStream (ostream *t)
 Set the output stream to emit to.
 
void setMaxLineSize (int4 mls)
 Set the maximum number of characters per line.
 
void setIndentIncrement (int4 inc)
 Set the number of characters to indent per level of code nesting.
 
void setLineCommentIndent (int4 val)
 Set the number of characters to indent comment lines. More...
 
void setCommentDelimeter (const string &start, const string &stop, bool usecommentfill)
 Establish comment delimiters for the language. More...
 
uint4 getInstructionComment (void) const
 Get the type of comments suitable within the body of a function.
 
void setInstructionComment (uint4 val)
 Set the type of comments suitable within the body of a function.
 
void setNamespaceStrategy (namespace_strategy strat)
 Set how namespace tokens are displayed.
 
uint4 getHeaderComment (void) const
 Get the type of comments suitable for a function header.
 
void setHeaderComment (uint4 val)
 Set the type of comments suitable for a function header.
 
bool emitsMarkup (void) const
 Does the low-level emitter, emit markup.
 
void setMarkup (bool val)
 Set whether the low-level emitter, emits markup. More...
 
void setFlat (bool val)
 Set whether nesting code structure should be emitted. More...
 
virtual void initializeFromArchitecture (void)=0
 Initialize architecture specific aspects of printer.
 
virtual void adjustTypeOperators (void)=0
 Set basic data-type information for p-code operators.
 
virtual void resetDefaults (void)
 Set printing options to their default value.
 
virtual void clear (void)
 Clear the RPN stack and the low-level emitter.
 
virtual void setIntegerFormat (const string &nm)
 Set the default integer format. More...
 
virtual void setCommentStyle (const string &nm)=0
 Set the way comments are displayed in decompiler output. More...
 
virtual void docTypeDefinitions (const TypeFactory *typegrp)=0
 Emit definitions of data-types. More...
 
virtual void docAllGlobals (void)=0
 Emit declarations of global variables.
 
virtual void docSingleGlobal (const Symbol *sym)=0
 Emit the declaration for a single (global) Symbol. More...
 
virtual void docFunction (const Funcdata *fd)=0
 Emit the declaration (and body) of a function. More...
 
virtual void emitBlockBasic (const BlockBasic *bb)=0
 Emit statements in a basic block.
 
virtual void emitBlockGraph (const BlockGraph *bl)=0
 Emit (an unspecified) list of blocks.
 
virtual void emitBlockCopy (const BlockCopy *bl)=0
 Emit a basic block (with any labels)
 
virtual void emitBlockGoto (const BlockGoto *bl)=0
 Emit a block ending with a goto statement.
 
virtual void emitBlockLs (const BlockList *bl)=0
 Emit a sequence of blocks.
 
virtual void emitBlockCondition (const BlockCondition *bl)=0
 Emit a conditional statement.
 
virtual void emitBlockIf (const BlockIf *bl)=0
 Emit an if/else style construct.
 
virtual void emitBlockWhileDo (const BlockWhileDo *bl)=0
 Emit a loop structure, check at top.
 
virtual void emitBlockDoWhile (const BlockDoWhile *bl)=0
 Emit a loop structure, check at bottom.
 
virtual void emitBlockInfLoop (const BlockInfLoop *bl)=0
 Emit an infinite loop structure.
 
virtual void emitBlockSwitch (const BlockSwitch *bl)=0
 Emit a switch structure.
 
virtual void opCopy (const PcodeOp *op)=0
 Emit a COPY operator.
 
virtual void opLoad (const PcodeOp *op)=0
 Emit a LOAD operator.
 
virtual void opStore (const PcodeOp *op)=0
 Emit a STORE operator.
 
virtual void opBranch (const PcodeOp *op)=0
 Emit a BRANCH operator.
 
virtual void opCbranch (const PcodeOp *op)=0
 Emit a CBRANCH operator.
 
virtual void opBranchind (const PcodeOp *op)=0
 Emit a BRANCHIND operator.
 
virtual void opCall (const PcodeOp *op)=0
 Emit a CALL operator.
 
virtual void opCallind (const PcodeOp *op)=0
 Emit a CALLIND operator.
 
virtual void opCallother (const PcodeOp *op)=0
 Emit a CALLOTHER operator.
 
virtual void opConstructor (const PcodeOp *op, bool withNew)=0
 Emit an operator constructing an object.
 
virtual void opReturn (const PcodeOp *op)=0
 Emit a RETURN operator.
 
virtual void opIntEqual (const PcodeOp *op)=0
 Emit a INT_EQUAL operator.
 
virtual void opIntNotEqual (const PcodeOp *op)=0
 Emit a INT_NOTEQUAL operator.
 
virtual void opIntSless (const PcodeOp *op)=0
 Emit a INT_SLESS operator.
 
virtual void opIntSlessEqual (const PcodeOp *op)=0
 Emit a INT_SLESSEQUAL operator.
 
virtual void opIntLess (const PcodeOp *op)=0
 Emit a INT_LESS operator.
 
virtual void opIntLessEqual (const PcodeOp *op)=0
 Emit a INT_LESSEQUAL operator.
 
virtual void opIntZext (const PcodeOp *op, const PcodeOp *readOp)=0
 Emit a INT_ZEXT operator.
 
virtual void opIntSext (const PcodeOp *op, const PcodeOp *readOp)=0
 Emit a INT_SEXT operator.
 
virtual void opIntAdd (const PcodeOp *op)=0
 Emit a INT_ADD operator.
 
virtual void opIntSub (const PcodeOp *op)=0
 Emit a INT_SUB operator.
 
virtual void opIntCarry (const PcodeOp *op)=0
 Emit a INT_CARRY operator.
 
virtual void opIntScarry (const PcodeOp *op)=0
 Emit a INT_SCARRY operator.
 
virtual void opIntSborrow (const PcodeOp *op)=0
 Emit a INT_SBORROW operator.
 
virtual void opInt2Comp (const PcodeOp *op)=0
 Emit a INT_2COMP operator.
 
virtual void opIntNegate (const PcodeOp *op)=0
 Emit a INT_NEGATE operator.
 
virtual void opIntXor (const PcodeOp *op)=0
 Emit a INT_XOR operator.
 
virtual void opIntAnd (const PcodeOp *op)=0
 Emit a INT_AND operator.
 
virtual void opIntOr (const PcodeOp *op)=0
 Emit a INT_OR operator.
 
virtual void opIntLeft (const PcodeOp *op)=0
 Emit a INT_LEFT operator.
 
virtual void opIntRight (const PcodeOp *op)=0
 Emit a INT_RIGHT operator.
 
virtual void opIntSright (const PcodeOp *op)=0
 Emit a INT_SRIGHT operator.
 
virtual void opIntMult (const PcodeOp *op)=0
 Emit a INT_MULT operator.
 
virtual void opIntDiv (const PcodeOp *op)=0
 Emit a INT_DIV operator.
 
virtual void opIntSdiv (const PcodeOp *op)=0
 Emit a INT_SDIV operator.
 
virtual void opIntRem (const PcodeOp *op)=0
 Emit a INT_REM operator.
 
virtual void opIntSrem (const PcodeOp *op)=0
 Emit a INT_SREM operator.
 
virtual void opBoolNegate (const PcodeOp *op)=0
 Emit a BOOL_NEGATE operator.
 
virtual void opBoolXor (const PcodeOp *op)=0
 Emit a BOOL_XOR operator.
 
virtual void opBoolAnd (const PcodeOp *op)=0
 Emit a BOOL_AND operator.
 
virtual void opBoolOr (const PcodeOp *op)=0
 Emit a BOOL_OR operator.
 
virtual void opFloatEqual (const PcodeOp *op)=0
 Emit a FLOAT_EQUAL operator.
 
virtual void opFloatNotEqual (const PcodeOp *op)=0
 Emit a FLOAT_NOTEQUAL operator.
 
virtual void opFloatLess (const PcodeOp *op)=0
 Emit a FLOAT_LESS operator.
 
virtual void opFloatLessEqual (const PcodeOp *op)=0
 Emit a FLOAT_LESSEQUAL operator.
 
virtual void opFloatNan (const PcodeOp *op)=0
 Emit a FLOAT_NAN operator.
 
virtual void opFloatAdd (const PcodeOp *op)=0
 Emit a FLOAT_ADD operator.
 
virtual void opFloatDiv (const PcodeOp *op)=0
 Emit a FLOAT_DIV operator.
 
virtual void opFloatMult (const PcodeOp *op)=0
 Emit a FLOAT_MULT operator.
 
virtual void opFloatSub (const PcodeOp *op)=0
 Emit a FLOAT_SUB operator.
 
virtual void opFloatNeg (const PcodeOp *op)=0
 Emit a FLOAT_NEG operator.
 
virtual void opFloatAbs (const PcodeOp *op)=0
 Emit a FLOAT_ABS operator.
 
virtual void opFloatSqrt (const PcodeOp *op)=0
 Emit a FLOAT_SQRT operator.
 
virtual void opFloatInt2Float (const PcodeOp *op)=0
 Emit a FLOAT_INT2FLOAT operator.
 
virtual void opFloatFloat2Float (const PcodeOp *op)=0
 Emit a FLOAT_FLOAT2FLOAT operator.
 
virtual void opFloatTrunc (const PcodeOp *op)=0
 Emit a FLOAT_TRUNC operator.
 
virtual void opFloatCeil (const PcodeOp *op)=0
 Emit a FLOAT_CEIL operator.
 
virtual void opFloatFloor (const PcodeOp *op)=0
 Emit a FLOAT_FLOOR operator.
 
virtual void opFloatRound (const PcodeOp *op)=0
 Emit a FLOAT_ROUND operator.
 
virtual void opMultiequal (const PcodeOp *op)=0
 Emit a MULTIEQUAL operator.
 
virtual void opIndirect (const PcodeOp *op)=0
 Emit a INDIRECT operator.
 
virtual void opPiece (const PcodeOp *op)=0
 Emit a PIECE operator.
 
virtual void opSubpiece (const PcodeOp *op)=0
 Emit a SUBPIECE operator.
 
virtual void opCast (const PcodeOp *op)=0
 Emit a CAST operator.
 
virtual void opPtradd (const PcodeOp *op)=0
 Emit a PTRADD operator.
 
virtual void opPtrsub (const PcodeOp *op)=0
 Emit a PTRSUB operator.
 
virtual void opSegmentOp (const PcodeOp *op)=0
 Emit a SEGMENTOP operator.
 
virtual void opCpoolRefOp (const PcodeOp *op)=0
 Emit a CPOOLREF operator.
 
virtual void opNewOp (const PcodeOp *op)=0
 Emit a NEW operator.
 
virtual void opInsertOp (const PcodeOp *op)=0
 Emit an INSERT operator.
 
virtual void opExtractOp (const PcodeOp *op)=0
 Emit an EXTRACT operator.
 
virtual void opPopcountOp (const PcodeOp *op)=0
 Emit a POPCOUNT operator.
 
virtual void opLzcountOp (const PcodeOp *op)=0
 Emit a LZCOUNT operator.
 
virtual string unnamedField (int4 off, int4 size)
 Generate an artificial field name. More...
 

Static Public Member Functions

static int4 mostNaturalBase (uintb val)
 Determine the most natural base for an integer. More...
 
static void formatBinary (ostream &s, uintb val)
 Print a number in binary form. More...
 

Static Public Attributes

static const string OPEN_PAREN = "("
 "(" token
 
static const string CLOSE_PAREN = ")"
 ")" token
 

Protected Member Functions

bool isSet (uint4 m) const
 Is the given printing modification active.
 
void pushScope (const Scope *sc)
 Push a new symbol scope.
 
void popScope (void)
 Pop to the previous symbol scope.
 
void pushMod (void)
 Push current printing modifications to the stack.
 
void popMod (void)
 Pop to the previous printing modifications.
 
void setMod (uint4 m)
 Activate the given printing modification.
 
void unsetMod (uint4 m)
 Deactivate the given printing modification.
 
void pushOp (const OpToken *tok, const PcodeOp *op)
 Push an operator token onto the RPN stack. More...
 
void pushAtom (const Atom &atom)
 Push a variable token onto the RPN stack. More...
 
void pushVn (const Varnode *vn, const PcodeOp *op, uint4 m)
 Push an expression rooted at a Varnode onto the RPN stack. More...
 
void pushVnExplicit (const Varnode *vn, const PcodeOp *op)
 Push an explicit variable onto the RPN stack. More...
 
void pushSymbolDetail (const Varnode *vn, const PcodeOp *op, bool isRead)
 Push symbol name with adornments matching given Varnode. More...
 
bool parentheses (const OpToken *op2)
 Determine if the given token should be emitted in its own parenthetic expression. More...
 
void emitOp (const ReversePolish &entry)
 Send an operator token from the RPN to the emitter. More...
 
void emitAtom (const Atom &atom)
 Send an variable token from the RPN to the emitter. More...
 
bool escapeCharacterData (ostream &s, const uint1 *buf, int4 count, int4 charsize, bool bigend) const
 Emit a byte buffer to the stream as unicode characters. More...
 
void recurse (void)
 Emit from the RPN stack as much as possible. More...
 
void opBinary (const OpToken *tok, const PcodeOp *op)
 Push a binary operator onto the RPN stack. More...
 
void opUnary (const OpToken *tok, const PcodeOp *op)
 Push a unary operator onto the RPN stack. More...
 
int4 getPending (void) const
 Get the number of pending nodes yet to be put on the RPN stack.
 
void resetDefaultsInternal (void)
 Reset options to default for PrintLanguage.
 
virtual void printUnicode (ostream &s, int4 onechar) const =0
 Print a single unicode character as a character constant for the high-level language. More...
 
virtual void pushType (const Datatype *ct)=0
 Push a data-type name onto the RPN expression stack. More...
 
virtual void pushConstant (uintb val, const Datatype *ct, const Varnode *vn, const PcodeOp *op)=0
 Push a constant onto the RPN stack. More...
 
virtual bool pushEquate (uintb val, int4 sz, const EquateSymbol *sym, const Varnode *vn, const PcodeOp *op)=0
 Push a constant marked up by and EquateSymbol onto the RPN stack. More...
 
virtual void pushAnnotation (const Varnode *vn, const PcodeOp *op)=0
 Push an address which is not in the normal data-flow. More...
 
virtual void pushSymbol (const Symbol *sym, const Varnode *vn, const PcodeOp *op)=0
 Push a specific Symbol onto the RPN stack. More...
 
virtual void pushUnnamedLocation (const Address &addr, const Varnode *vn, const PcodeOp *op)=0
 Push an address as a substitute for a Symbol onto the RPN stack. More...
 
virtual void pushPartialSymbol (const Symbol *sym, int4 off, int4 sz, const Varnode *vn, const PcodeOp *op, int4 inslot)=0
 Push a variable that represents only part of a symbol onto the RPN stack. More...
 
virtual void pushMismatchSymbol (const Symbol *sym, int4 off, int4 sz, const Varnode *vn, const PcodeOp *op)=0
 Push an identifier for a variable that mismatches with its Symbol. More...
 
virtual void pushImpliedField (const Varnode *vn, const PcodeOp *op)=0
 Push the implied field of a given Varnode as an object member extraction operation. More...
 
virtual void emitLineComment (int4 indent, const Comment *comm)
 Emit a comment line. More...
 
virtual void emitVarDecl (const Symbol *sym)=0
 Emit a variable declaration. More...
 
virtual void emitVarDeclStatement (const Symbol *sym)=0
 Emit a variable declaration statement. More...
 
virtual bool emitScopeVarDecls (const Scope *symScope, int4 cat)=0
 Emit all the variable declarations for a given scope. More...
 
virtual void emitExpression (const PcodeOp *op)=0
 Emit a full expression. More...
 
virtual void emitFunctionDeclaration (const Funcdata *fd)=0
 Emit a function declaration. More...
 
virtual bool checkPrintNegation (const Varnode *vn)=0
 Check whether a given boolean Varnode can be printed in negated form. More...
 

Static Protected Member Functions

static bool unicodeNeedsEscape (int4 codepoint)
 Determine if the given codepoint needs to be escaped. More...
 

Protected Attributes

Architectureglb
 The Architecture owning the language emitter.
 
const Scopecurscope
 The current symbol scope.
 
CastStrategycastStrategy
 The strategy for emitting explicit case operations.
 
Emitemit
 The low-level token emitter.
 
uint4 mods
 Currently active printing modifications.
 
uint4 instr_comment_type
 Type of instruction comments to display.
 
uint4 head_comment_type
 Type of header comments to display.
 
namespace_strategy namespc_strategy
 How should namespace tokens be displayed.
 

Private Attributes

string name
 The name of the high-level language.
 
vector< uint4 > modstack
 Printing modification stack.
 
vector< const Scope * > scopestack
 The symbol scope stack.
 
vector< ReversePolishrevpol
 The Reverse Polish Notation (RPN) token stack.
 
vector< NodePendingnodepend
 Data-flow nodes waiting to be pushed onto the RPN stack.
 
int4 pending
 Number of data-flow nodes waiting to be pushed.
 
int4 line_commentindent
 Number of characters a comment line should be indented.
 
string commentstart
 Delimiter characters for the start of a comment.
 
string commentend
 Delimiter characters (if any) for the end of a comment.
 

Detailed Description

The base class API for emitting a high-level language.

Instances of this object are responsible for converting a function's (transformed) data-flow graph into the final stream of tokens of a high-level source code language. There a few main entry points including:

The system is responsible for printing:

As part of all this printing, the system is also responsible for

To accomplish this, the API is broken up into three sections. The first section are the main entry point 'doc' methods. The second section are 'emit' methods, which are responsible for printing a representation of a particular high-level code construct. The third section are 'push' and 'op' methods, which are responsible for walking expression trees. The order in which tokens are emitted for an expression is determined by a Reverse Polish Notation (RPN) stack, that the 'push' methods manipulate. Operators and variables are pushed onto this stack and are ultimately emitted in the correct order.

The base class provides a generic printing modifications stack and a symbol scope stack to provide a printing context mechanism for derived classes.

Member Enumeration Documentation

◆ modifiers

Possible context sensitive modifiers to how tokens get emitted.

Enumerator
force_hex 

Force printing of hex.

force_dec 

Force printing of dec.

bestfit 

Decide on most aesthetic form.

force_scinote 

Force scientific notation for floats.

force_pointer 

Force '*' notation for pointers.

print_load_value 

Hide pointer deref for load with other ops.

print_store_value 

Hide pointer deref for store with other ops.

no_branch 

Do not print branch instruction.

only_branch 

Print only the branch instruction.

comma_separate 

Statements within condition.

flat 

Do not print block structure.

falsebranch 

Print the false branch (for flat)

nofallthru 

Fall-thru no longer exists.

negatetoken 

Print the token representing the negation of current token.

hide_thisparam 

Do not print the 'this' parameter in argument lists.

pending_brace 

The current block may need to surround itself with additional braces.

◆ namespace_strategy

Strategies for displaying namespace tokens.

Enumerator
MINIMAL_NAMESPACES 

(default) Print just enough namespace info to fully resolve symbol

NO_NAMESPACES 

Never print namespace information.

ALL_NAMESPACES 

Always print all namespace information.

◆ tagtype

Possible types of Atom.

Enumerator
syntax 

Emit atom as syntax.

vartoken 

Emit atom as variable.

functoken 

Emit atom as function name.

optoken 

Emit atom as operator.

typetoken 

Emit atom as operator.

fieldtoken 

Emit atom as structure field.

blanktoken 

For anonymous types.

Constructor & Destructor Documentation

◆ PrintLanguage()

ghidra::PrintLanguage::PrintLanguage ( Architecture g,
const string &  nm 
)

Constructor.

Parameters
gis the Architecture that owns and will use this PrintLanguage
nmis the formal name of the language

References ghidra::PrintLanguageCapability::name.

Member Function Documentation

◆ checkPrintNegation()

virtual bool ghidra::PrintLanguage::checkPrintNegation ( const Varnode vn)
protectedpure virtual

Check whether a given boolean Varnode can be printed in negated form.

In many situations a boolean value can be inverted by flipping the operator token producing it to a complementary token.

Parameters
vnis the given boolean Varnode
Returns
true if the value can be easily inverted

Implemented in ghidra::PrintC.

◆ docFunction()

virtual void ghidra::PrintLanguage::docFunction ( const Funcdata fd)
pure virtual

Emit the declaration (and body) of a function.

Parameters
fdis the function to emit

Implemented in ghidra::PrintC, and ghidra::PrintJava.

◆ docSingleGlobal()

virtual void ghidra::PrintLanguage::docSingleGlobal ( const Symbol sym)
pure virtual

Emit the declaration for a single (global) Symbol.

Parameters
symis the Symbol to declare

Implemented in ghidra::PrintC.

◆ docTypeDefinitions()

virtual void ghidra::PrintLanguage::docTypeDefinitions ( const TypeFactory typegrp)
pure virtual

Emit definitions of data-types.

Parameters
typegrpis the container for the data-types that should be defined

Implemented in ghidra::PrintC.

◆ emitAtom()

void ghidra::PrintLanguage::emitAtom ( const Atom atom)
protected

◆ emitExpression()

virtual void ghidra::PrintLanguage::emitExpression ( const PcodeOp op)
protectedpure virtual

Emit a full expression.

This can be an assignment statement, if the given PcodeOp has an output Varnode, or it can be a statement with no left-hand side.

Parameters
opis the given PcodeOp performing the final operation of the expression

Implemented in ghidra::PrintC.

◆ emitFunctionDeclaration()

virtual void ghidra::PrintLanguage::emitFunctionDeclaration ( const Funcdata fd)
protectedpure virtual

Emit a function declaration.

This prints the formal defining prototype for a function.

Parameters
fdis the Funcdata object representing the function to be emitted

Implemented in ghidra::PrintC.

◆ emitLineComment()

void ghidra::PrintLanguage::emitLineComment ( int4  indent,
const Comment comm 
)
protectedvirtual

Emit a comment line.

The comment will get emitted as a single line using the high-level language's delimiters with the given indent level

Parameters
indentis the number of characters to indent
commis the Comment object containing the character data and associated markup info

References ghidra::Emit::comment_color, ghidra::Comment::getAddr(), ghidra::Address::getOffset(), ghidra::Address::getSpace(), ghidra::Comment::getText(), and ghidra::Comment::setEmitted().

Referenced by ghidra::PrintC::emitCommentFuncHeader(), and ghidra::PrintC::emitCommentGroup().

◆ emitOp()

void ghidra::PrintLanguage::emitOp ( const ReversePolish entry)
protected

◆ emitScopeVarDecls()

virtual bool ghidra::PrintLanguage::emitScopeVarDecls ( const Scope symScope,
int4  cat 
)
protectedpure virtual

Emit all the variable declarations for a given scope.

A subset of all variables can be declared by specifying a category, 0 for parameters, -1 for everything.

Parameters
symScopeis the given Scope
catis the category of variable to declare

Implemented in ghidra::PrintC.

◆ emitVarDecl()

virtual void ghidra::PrintLanguage::emitVarDecl ( const Symbol sym)
protectedpure virtual

Emit a variable declaration.

This can be part of a full a statement, or just the declaration of a function parameter

Parameters
symis the Symbol to be declared

Implemented in ghidra::PrintC.

◆ emitVarDeclStatement()

virtual void ghidra::PrintLanguage::emitVarDeclStatement ( const Symbol sym)
protectedpure virtual

Emit a variable declaration statement.

Parameters
symis the Symbol to be declared

Implemented in ghidra::PrintC.

◆ escapeCharacterData()

bool ghidra::PrintLanguage::escapeCharacterData ( ostream &  s,
const uint1 *  buf,
int4  count,
int4  charsize,
bool  bigend 
) const
protected

Emit a byte buffer to the stream as unicode characters.

Characters are emitted until we reach a terminator character or count bytes is consumed.

Parameters
sis the output stream
bufis the byte buffer
countis the maximum number of bytes to consume
charsizeis 1 for UTF8, 2 for UTF16, or 4 for UTF32
bigendis true for a big endian encoding of UTF elements
Returns
true if we reach a terminator character

References ghidra::StringManager::getCodepoint().

Referenced by ghidra::PrintJava::opCpoolRefOp(), ghidra::PrintC::opCpoolRefOp(), and ghidra::PrintC::printCharacterConstant().

◆ formatBinary()

void ghidra::PrintLanguage::formatBinary ( ostream &  s,
uintb  val 
)
static

Print a number in binary form.

Print a string a '0' and '1' characters representing the given value

Parameters
sis the output stream
valis the given value

Referenced by ghidra::PrintC::push_integer().

◆ mostNaturalBase()

int4 ghidra::PrintLanguage::mostNaturalBase ( uintb  val)
static

Determine the most natural base for an integer.

Count '0' and '9' digits base 10. Count '0' and 'f' digits base 16. The highest count is the preferred base.

Parameters
valis the given integer
Returns
10 for decimal or 16 for hexidecimal

Referenced by ghidra::PrintC::push_integer().

◆ opBinary()

void ghidra::PrintLanguage::opBinary ( const OpToken tok,
const PcodeOp op 
)
protected

Push a binary operator onto the RPN stack.

Push an operator onto the stack that has a normal binary format. Both of its input expressions are also pushed.

Parameters
tokis the operator token to push
opis the associated PcodeOp

References ghidra::PcodeOp::getIn(), and ghidra::OpToken::negate.

◆ opUnary()

void ghidra::PrintLanguage::opUnary ( const OpToken tok,
const PcodeOp op 
)
protected

Push a unary operator onto the RPN stack.

Push an operator onto the stack that has a normal unary format. Its input expression is also pushed.

Parameters
tokis the operator token to push
opis the associated PcodeOp

References ghidra::PcodeOp::getIn().

◆ parentheses()

bool ghidra::PrintLanguage::parentheses ( const OpToken op2)
protected

Determine if the given token should be emitted in its own parenthetic expression.

The token at the top of the stack is being emitted. Check if its input expression, ending with the given operator token, needs to be surrounded by parentheses to convey the proper meaning.

Parameters
op2is the input token to this operator
Returns
true if op2 (as input to this) should be parenthesized

References ghidra::OpToken::binary, ghidra::OpToken::hiddenfunction, ghidra::OpToken::postsurround, ghidra::OpToken::precedence, ghidra::OpToken::presurround, ghidra::OpToken::space, ghidra::OpToken::type, and ghidra::OpToken::unary_prefix.

◆ printUnicode()

virtual void ghidra::PrintLanguage::printUnicode ( ostream &  s,
int4  onechar 
) const
protectedpure virtual

Print a single unicode character as a character constant for the high-level language.

For most languages, this prints the character surrounded by single quotes.

Parameters
sis the output stream
onecharis the unicode code point of the character to print

Implemented in ghidra::PrintC, and ghidra::PrintJava.

◆ pushAnnotation()

virtual void ghidra::PrintLanguage::pushAnnotation ( const Varnode vn,
const PcodeOp op 
)
protectedpure virtual

Push an address which is not in the normal data-flow.

The given Varnode is treated as an address, which may or may not have a symbol name.

Parameters
vnis the annotation Varnode
opis the PcodeOp which takes the annotation as input

Implemented in ghidra::PrintC.

◆ pushAtom()

void ghidra::PrintLanguage::pushAtom ( const Atom atom)
protected

◆ pushConstant()

virtual void ghidra::PrintLanguage::pushConstant ( uintb  val,
const Datatype ct,
const Varnode vn,
const PcodeOp op 
)
protectedpure virtual

Push a constant onto the RPN stack.

The value is ultimately emitted based on its data-type and other associated mark-up

Parameters
valis the value of the constant
ctis the data-type of the constant
vnis the Varnode holding the constant (optional)
opis the PcodeOp using the constant (optional)

Implemented in ghidra::PrintC.

◆ pushEquate()

virtual bool ghidra::PrintLanguage::pushEquate ( uintb  val,
int4  sz,
const EquateSymbol sym,
const Varnode vn,
const PcodeOp op 
)
protectedpure virtual

Push a constant marked up by and EquateSymbol onto the RPN stack.

The equate may substitute a name or force a conversion for the constant

Parameters
valis the value of the constant
szis the number of bytes to use for the encoding
symis the EquateSymbol that marks up the constant
vnis the Varnode holding the constant (optional)
opis the PcodeOp using the constant (optional)

Implemented in ghidra::PrintC.

◆ pushImpliedField()

virtual void ghidra::PrintLanguage::pushImpliedField ( const Varnode vn,
const PcodeOp op 
)
protectedpure virtual

Push the implied field of a given Varnode as an object member extraction operation.

If a Varnode is implied and has a union data-type, the particular read of the varnode may correspond to a particular field that needs to get printed as a token, even though the Varnode itself is printed directly. This method pushes the field name token.

Parameters
vnis the given Varnode
opis the particular PcodeOp reading the Varnode

Implemented in ghidra::PrintC.

◆ pushMismatchSymbol()

virtual void ghidra::PrintLanguage::pushMismatchSymbol ( const Symbol sym,
int4  off,
int4  sz,
const Varnode vn,
const PcodeOp op 
)
protectedpure virtual

Push an identifier for a variable that mismatches with its Symbol.

This happens when a Varnode overlaps, but is not contained by a Symbol. This most commonly happens when the size of a Symbol is unknown

Parameters
symis the overlapped symbol
offis the byte offset of the variable relative to the symbol
szis the size of the variable in bytes
vnis the Varnode representing the variable
opis a PcodeOp associated with the Varnode

Implemented in ghidra::PrintC.

◆ pushOp()

void ghidra::PrintLanguage::pushOp ( const OpToken tok,
const PcodeOp op 
)
protected

◆ pushPartialSymbol()

virtual void ghidra::PrintLanguage::pushPartialSymbol ( const Symbol sym,
int4  off,
int4  sz,
const Varnode vn,
const PcodeOp op,
int4  inslot 
)
protectedpure virtual

Push a variable that represents only part of a symbol onto the RPN stack.

Generally member syntax specifying a field within a structure gets emitted.

Parameters
symis the root Symbol
offis the byte offset, within the Symbol, of the partial variable
szis the number of bytes in the partial variable
vnis the Varnode holding the partial value
opis a PcodeOp associate with the Varnode
inslotis the input slot of vn with op, or -1 if op writes vn

Implemented in ghidra::PrintC.

◆ pushSymbol()

virtual void ghidra::PrintLanguage::pushSymbol ( const Symbol sym,
const Varnode vn,
const PcodeOp op 
)
protectedpure virtual

Push a specific Symbol onto the RPN stack.

Parameters
symis the given Symbol
vnis the Varnode holding the Symbol value
opis a PcodeOp associated with the Varnode

Implemented in ghidra::PrintC.

◆ pushSymbolDetail()

void ghidra::PrintLanguage::pushSymbolDetail ( const Varnode vn,
const PcodeOp op,
bool  isRead 
)
protected

Push symbol name with adornments matching given Varnode.

We know that the given Varnode matches part of a single Symbol. Push a set of tokens that represents the Varnode, which may require extracting subfields or casting to get the correct value.

Parameters
vnis the given Varnode
opis the PcodeOp involved in the expression with the Varnode
isReadis true if the PcodeOp reads the Varnode

References ghidra::Varnode::getAddr(), ghidra::Varnode::getHigh(), ghidra::HighVariable::getNameRepresentative(), ghidra::Varnode::getSize(), ghidra::Datatype::getSize(), ghidra::PcodeOp::getSlot(), ghidra::HighVariable::getSymbol(), ghidra::HighVariable::getSymbolOffset(), ghidra::Symbol::getType(), and ghidra::Datatype::needsResolution().

Referenced by ghidra::PrintC::emitExpression().

◆ pushType()

virtual void ghidra::PrintLanguage::pushType ( const Datatype ct)
protectedpure virtual

Push a data-type name onto the RPN expression stack.

The data-type is generally emitted as if for a cast.

Parameters
ctis the data-type to push

Implemented in ghidra::PrintC.

◆ pushUnnamedLocation()

virtual void ghidra::PrintLanguage::pushUnnamedLocation ( const Address addr,
const Varnode vn,
const PcodeOp op 
)
protectedpure virtual

Push an address as a substitute for a Symbol onto the RPN stack.

If there is no Symbol or other name source for an explicit variable, this method is used to print something to represent the variable based on its storage address.

Parameters
addris the storage address
vnis the Varnode representing the variable (if present)
opis a PcodeOp associated with the variable

Implemented in ghidra::PrintC.

◆ pushVn()

void ghidra::PrintLanguage::pushVn ( const Varnode vn,
const PcodeOp op,
uint4  m 
)
protected

◆ pushVnExplicit()

void ghidra::PrintLanguage::pushVnExplicit ( const Varnode vn,
const PcodeOp op 
)
protected

Push an explicit variable onto the RPN stack.

This method pushes a given Varnode as a leaf of the current expression. It decides how the Varnode should get emitted, as a symbol, constant, etc., and then pushes the resulting leaf Atom onto the stack.

Parameters
vnis the given explicit Varnode
opis the PcodeOp incorporating the Varnode into the current expression

References ghidra::Varnode::getHighTypeReadFacing(), ghidra::Varnode::getOffset(), ghidra::Varnode::isAnnotation(), and ghidra::Varnode::isConstant().

Referenced by ghidra::PrintC::emitInplaceOp().

◆ recurse()

void ghidra::PrintLanguage::recurse ( void  )
protected

◆ setCommentDelimeter()

void ghidra::PrintLanguage::setCommentDelimeter ( const string &  start,
const string &  stop,
bool  usecommentfill 
)

Establish comment delimiters for the language.

By default, comments are indicated in the high-level language by preceding them with a specific sequence of delimiter characters, and optionally by ending the comment with another set of delimiter characters.

Parameters
startis the initial sequence of characters delimiting a comment
stopif not empty is the sequence delimiting the end of the comment
usecommentfillis true if the delimiter needs to be emitted after every line break

◆ setCommentStyle()

virtual void ghidra::PrintLanguage::setCommentStyle ( const string &  nm)
pure virtual

Set the way comments are displayed in decompiler output.

This method can either be provided a formal name or a sample of the initial delimiter, then it will choose from among the schemes it knows

Parameters
nmis the configuration description

Implemented in ghidra::PrintC.

Referenced by ghidra::OptionCommentStyle::apply().

◆ setFlat()

void ghidra::PrintLanguage::setFlat ( bool  val)

Set whether nesting code structure should be emitted.

Emitting formal code structuring can be turned off, causing all control-flow to be represented as goto statements and labels.

Parameters
valis true if no code structuring should be emitted

◆ setIntegerFormat()

void ghidra::PrintLanguage::setIntegerFormat ( const string &  nm)
virtual

Set the default integer format.

This determines how integers are displayed by default. Possible values are "hex" and "dec" to force a given format, or "best" can be used to let the decompiler select what it thinks best for each individual integer.

Parameters
nmis "hex", "dec", or "best"

Referenced by ghidra::OptionIntegerFormat::apply().

◆ setLineCommentIndent()

void ghidra::PrintLanguage::setLineCommentIndent ( int4  val)

Set the number of characters to indent comment lines.

Parameters
valis the number of characters

Referenced by ghidra::OptionCommentIndent::apply().

◆ setMarkup()

void ghidra::PrintLanguage::setMarkup ( bool  val)

Set whether the low-level emitter, emits markup.

Tell the emitter whether to emit just the raw tokens or if additional mark-up should be provided.

Parameters
valis true for additional mark-up

Referenced by ghidra::ArchitectureGhidra::ArchitectureGhidra().

◆ unicodeNeedsEscape()

bool ghidra::PrintLanguage::unicodeNeedsEscape ( int4  codepoint)
staticprotected

Determine if the given codepoint needs to be escaped.

Separate unicode characters that can be clearly emitted in a source code string (letters, numbers, punctuation, symbols) from characters that are better represented in source code with an escape sequence (control characters, unusual spaces, separators, private use characters etc.

Parameters
codepointis the given unicode codepoint to categorize.
Returns
true if the codepoint needs to be escaped

Referenced by ghidra::PrintJava::printUnicode(), and ghidra::PrintC::printUnicode().

◆ unnamedField()

string ghidra::PrintLanguage::unnamedField ( int4  off,
int4  size 
)
virtual

Generate an artificial field name.

This is used if a value is extracted from a structured data-type, but the natural name is not available. An artificial name is generated given just the offset into the data-type and the size in bytes.

Parameters
offis the byte offset into the data-type
sizeis the number of bytes in the extracted value
Returns
a string describing the artificial field

Referenced by ghidra::PrintC::pushPartialSymbol().


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