1 #ifndef ROSE_BinaryAnalysis_InstructionSemantics_LlvmSemantics_H
2 #define ROSE_BinaryAnalysis_InstructionSemantics_LlvmSemantics_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
6 #include <Rose/BinaryAnalysis/BasicTypes.h>
7 #include <Rose/BinaryAnalysis/InstructionSemantics/SymbolicSemantics.h>
8 #include <Rose/BinaryAnalysis/InstructionSemantics/DispatcherX86.h>
9 #include <Rose/CommandLine.h>
12 namespace BinaryAnalysis {
13 namespace InstructionSemantics {
16 namespace LlvmSemantics {
18 typedef std::vector<RegisterDescriptor> RegisterDescriptors;
25 typedef SymbolicExpression::Nodes TreeNodes;
33 typedef SymbolicSemantics::MemoryStatePtr MemoryStatePtr;
56 RegisterStatePtr prev_regstate;
57 RegisterDescriptors important_registers;
60 std::string indent_string;
131 int indent(
int nlevels=1) { indent_level += nlevels;
return nlevels; }
134 std::string
prefix()
const;
139 RiscOperatorsPtr ops_ptr;
141 explicit Indent(
const RiscOperatorsPtr &ops_ptr,
int nlevels=1): ops_ptr(ops_ptr), nlevels(nlevels) {
157 virtual void reset();
185 virtual void add_rewrite(
const ExpressionPtr &from,
const LeafPtr &to);
249 virtual std::string
llvm_term(
const ExpressionPtr&);
296 virtual LeafPtr
emit_assignment(std::ostream&,
const ExpressionPtr &rhs);
304 virtual ExpressionPtr
emit_zero_extend(std::ostream&,
const ExpressionPtr &value,
size_t nbits);
305 virtual ExpressionPtr
emit_sign_extend(std::ostream&,
const ExpressionPtr &value,
size_t nbits);
306 virtual ExpressionPtr
emit_truncate(std::ostream&,
const ExpressionPtr &value,
size_t nbits);
307 virtual ExpressionPtr
emit_unsigned_resize(std::ostream&,
const ExpressionPtr &value,
size_t nbits);
308 virtual ExpressionPtr
emit_binary(std::ostream&,
const std::string &llvm_op,
const ExpressionPtr&,
const ExpressionPtr&);
309 virtual ExpressionPtr
emit_signed_binary(std::ostream&,
const std::string &llvm_op,
const ExpressionPtr&,
const ExpressionPtr&);
310 virtual ExpressionPtr
emit_unsigned_binary(std::ostream&,
const std::string &llvm_op,
const ExpressionPtr&,
const ExpressionPtr&);
311 virtual ExpressionPtr
emit_logical_right_shift(std::ostream&,
const ExpressionPtr &value,
const ExpressionPtr &amount);
314 virtual ExpressionPtr
emit_left_shift(std::ostream&,
const ExpressionPtr &value,
const ExpressionPtr &amount);
315 virtual ExpressionPtr
emit_left_shift_ones(std::ostream&,
const ExpressionPtr &value,
const ExpressionPtr &amount);
316 virtual ExpressionPtr
emit_lssb(std::ostream&,
const ExpressionPtr&);
317 virtual ExpressionPtr
emit_mssb(std::ostream&,
const ExpressionPtr&);
318 virtual ExpressionPtr
emit_extract(std::ostream&,
const ExpressionPtr &value,
const ExpressionPtr &from,
size_t result_nbits);
319 virtual ExpressionPtr
emit_invert(std::ostream&,
const ExpressionPtr &value);
320 virtual ExpressionPtr
emit_left_associative(std::ostream&,
const std::string &llvm_op,
const TreeNodes &operands);
321 virtual ExpressionPtr
emit_concat(std::ostream&, TreeNodes operands);
322 virtual ExpressionPtr
emit_signed_divide(std::ostream&,
const ExpressionPtr &numerator,
const ExpressionPtr &denominator);
323 virtual ExpressionPtr
emit_unsigned_divide(std::ostream&,
const ExpressionPtr &numerator,
const ExpressionPtr &denominator);
324 virtual ExpressionPtr
emit_signed_modulo(std::ostream&,
const ExpressionPtr &numerator,
const ExpressionPtr &denominator);
325 virtual ExpressionPtr
emit_unsigned_modulo(std::ostream&,
const ExpressionPtr &numerator,
const ExpressionPtr &denominator);
328 virtual ExpressionPtr
emit_rotate_left(std::ostream&,
const ExpressionPtr &value,
const ExpressionPtr &amount);
329 virtual ExpressionPtr
emit_rotate_right(std::ostream&,
const ExpressionPtr &value,
const ExpressionPtr &amount);
330 virtual ExpressionPtr
emit_compare(std::ostream&,
const std::string &llvm_op,
const ExpressionPtr&,
const ExpressionPtr&);
331 virtual ExpressionPtr
emit_ite(std::ostream&,
const ExpressionPtr &cond,
const ExpressionPtr&,
const ExpressionPtr&);
332 virtual ExpressionPtr
emit_memory_read(std::ostream&,
const ExpressionPtr &address,
size_t nbits);
333 virtual ExpressionPtr
emit_global_read(std::ostream&,
const std::string &varname,
size_t nbits);
334 virtual void emit_memory_write(std::ostream&,
const ExpressionPtr &address,
const ExpressionPtr &value);
348 RiscOperatorsPtr operators;
std::string prefix() const
Return indentation string.
static TranscoderPtr instanceX86()
Factory method to create a new transcoder for 32-bit X86 instructions.
virtual std::string llvm_integer_type(size_t nbits)
Obtain the LLVM type name for an integer.
virtual ExpressionPtr emit_unsigned_divide(std::ostream &, const ExpressionPtr &numerator, const ExpressionPtr &denominator)
Emit an operation as LLVM instructions.
static TranscoderPtr instance(const BaseSemantics::DispatcherPtr &)
Factory method to create a new transcoder for an arbitrary machine architecture.
static RiscOperatorsPtr instanceFromState(const BaseSemantics::StatePtr &, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiates a new RiscOperators object with specified state.
size_t transcodeBasicBlock(SgAsmBlock *, std::ostream &)
Transcode a basic block of machine instructions to LLVM instructions.
void emitFunctionFragements(bool b)
Property to determine whether function fragments should be emitted.
virtual void emit_register_definitions(std::ostream &, const RegisterDescriptors &)
Output LLVM global register definitions for the specified registers.
boost::shared_ptr< RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to a RISC operators object.
virtual void writeMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &data, const BaseSemantics::SValuePtr &cond) override
Writes a value to memory.
static RiscOperatorsPtr instanceFromProtoval(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiates a new RiscOperators object with specified prototypical values.
Base class for machine instructions.
TranscoderPtr Ptr
Reference counting pointer.
virtual RegisterDescriptors get_modified_registers()
Return the list of important registers that have been modified since the last call to make_current()...
static RiscOperatorsPtr promote(const BaseSemantics::RiscOperatorsPtr &)
Run-time promotion of a base RiscOperators pointer to Llvm operators.
virtual ExpressionPtr emit_binary(std::ostream &, const std::string &llvm_op, const ExpressionPtr &, const ExpressionPtr &)
Emit an operation as LLVM instructions.
virtual ExpressionPtr emit_left_shift(std::ostream &, const ExpressionPtr &value, const ExpressionPtr &amount)
Emit an operation as LLVM instructions.
virtual void emit_register_declarations(std::ostream &, const RegisterDescriptors &)
Output LLVM global register declarations for the specified registers.
virtual ExpressionPtr emit_global_read(std::ostream &, const std::string &varname, size_t nbits)
Emit an operation as LLVM instructions.
Represents a synthesized function.
virtual void emit_next_eip(std::ostream &, SgAsmInstruction *latest_insn)
Output an LLVM branch instruction.
virtual ExpressionPtr emit_logical_right_shift_ones(std::ostream &, const ExpressionPtr &value, const ExpressionPtr &amount)
Emit an operation as LLVM instructions.
virtual std::string get_variable(const LeafPtr &)
Returns the LLVM name for a variable, including the sigil.
virtual std::string addr_label(rose_addr_t)
Obtain a label for a virtual address.
Main namespace for the ROSE library.
virtual ExpressionPtr emit_concat(std::ostream &, TreeNodes operands)
Emit an operation as LLVM instructions.
virtual std::string llvm_term(const ExpressionPtr &)
Convert a ROSE variable or integer to an LLVM term.
bool quietErrors() const
Property to control what happens when a translation exception occurs.
A RegisterState for any architecture.
void transcodeInstruction(SgAsmInstruction *, std::ostream &)
Translate a single machine instruction to LLVM instructions.
virtual ExpressionPtr emit_arithmetic_right_shift(std::ostream &, const ExpressionPtr &value, const ExpressionPtr &amount)
Emit an operation as LLVM instructions.
Reference-counting intrusive smart pointer.
boost::shared_ptr< State > StatePtr
Shared-ownership pointer to a semantic state.
virtual ExpressionPtr emit_signed_multiply(std::ostream &, const TreeNodes &operands)
Emit an operation as LLVM instructions.
virtual BaseSemantics::RiscOperatorsPtr create(const BaseSemantics::SValuePtr &protoval, const SmtSolverPtr &solver=SmtSolverPtr()) const override
Virtual allocating constructor.
std::string emitFilePrologue()
Emit LLVM file prologue.
virtual RegisterDescriptors get_stored_registers()
Return the list of important registers that are stored.
boost::shared_ptr< Dispatcher > DispatcherPtr
Shared-ownership pointer to a semantics instruction dispatcher.
virtual ExpressionPtr emit_left_shift_ones(std::ostream &, const ExpressionPtr &value, const ExpressionPtr &amount)
Emit an operation as LLVM instructions.
virtual void emit_memory_writes(std::ostream &)
Output changed memory state.
void quietErrors(bool b)
Property to control what happens when a translation exception occurs.
virtual ExpressionPtr emit_left_associative(std::ostream &, const std::string &llvm_op, const TreeNodes &operands)
Emit an operation as LLVM instructions.
boost::shared_ptr< class RegisterStateGeneric > RegisterStateGenericPtr
Shared-ownership pointer to generic register states.
int indent(int nlevels=1)
Increase indentation by nlevels levels.
Translates machine instructions to LLVM.
virtual void make_current()
Mark the current state as having been emitted.
virtual const RegisterDescriptors & get_important_registers()
Return the list of registers that needs to be emitted to LLVM.
void llvmVersion(int v)
Property: LLVM version.
virtual ExpressionPtr emit_unsigned_modulo(std::ostream &, const ExpressionPtr &numerator, const ExpressionPtr &denominator)
Emit an operation as LLVM instructions.
virtual ExpressionPtr emit_unsigned_multiply(std::ostream &, const TreeNodes &operands)
Emit an operation as LLVM instructions.
virtual ExpressionPtr emit_rotate_left(std::ostream &, const ExpressionPtr &value, const ExpressionPtr &amount)
Emit an operation as LLVM instructions.
Describes (part of) a physical CPU register.
RiscOperatorsPtr Ptr
Shared-ownership pointer.
virtual LeafPtr emit_expression(std::ostream &, const SValuePtr &)
Emit LLVM statements for an expression.
This class represents the base class for all IR nodes within Sage III.
virtual std::string add_variable(const LeafPtr &)
Register an LLVM variable.
Type of values manipulated by the SymbolicSemantics domain.
boost::shared_ptr< class RiscOperators > RiscOperatorsPtr
Shared-ownership pointer to LLVM RISC operations.
virtual ExpressionPtr emit_invert(std::ostream &, const ExpressionPtr &value)
Emit an operation as LLVM instructions.
void emitFunctionDeclarations(SgNode *ast, std::ostream &)
Emit function declarations.
virtual ExpressionPtr emit_mssb(std::ostream &, const ExpressionPtr &)
Emit an operation as LLVM instructions.
virtual ExpressionPtr emit_lssb(std::ostream &, const ExpressionPtr &)
Emit an operation as LLVM instructions.
virtual void emit_prerequisites(std::ostream &, const RegisterDescriptors &, const RegisterDictionaryPtr &)
Output LLVM global variable reads that are needed to define the specified registers and pending memor...
bool emitFunctionFragements() const
Property to determine whether function fragments should be emitted.
virtual const TreeNodes & get_memory_writes()
Return the list of memory writes that have occured since the last call to make_current().
Defines RISC operators for the SymbolicSemantics domain.
virtual ExpressionPtr emit_zero_extend(std::ostream &, const ExpressionPtr &value, size_t nbits)
Emit an operation as LLVM instructions.
virtual ExpressionPtr emit_logical_right_shift(std::ostream &, const ExpressionPtr &value, const ExpressionPtr &amount)
Emit an operation as LLVM instructions.
int llvmVersion() const
Property: LLVM version.
virtual RegisterDescriptor get_insn_pointer_register()
Return the descriptor for the instruction pointer register.
virtual ExpressionPtr emit_ite(std::ostream &, const ExpressionPtr &cond, const ExpressionPtr &, const ExpressionPtr &)
Emit an operation as LLVM instructions.
virtual ExpressionPtr emit_extract(std::ostream &, const ExpressionPtr &value, const ExpressionPtr &from, size_t result_nbits)
Emit an operation as LLVM instructions.
virtual ExpressionPtr emit_rotate_right(std::ostream &, const ExpressionPtr &value, const ExpressionPtr &amount)
Emit an operation as LLVM instructions.
virtual BaseSemantics::SValuePtr readMemory(RegisterDescriptor segreg, const BaseSemantics::SValuePtr &addr, const BaseSemantics::SValuePtr &dflt, const BaseSemantics::SValuePtr &cond) override
Reads a value from memory.
virtual LeafPtr emit_assignment(std::ostream &, const ExpressionPtr &rhs)
Emit an assignment and add a rewrite rule.
size_t transcodeFunction(SgAsmFunction *, std::ostream &)
Transcode an entire function to LLVM instructions.
virtual ExpressionPtr emit_truncate(std::ostream &, const ExpressionPtr &value, size_t nbits)
Emit an operation as LLVM instructions.
virtual SValuePtr get_instruction_pointer()
Return the value of the instruction pointer.
static RiscOperatorsPtr instanceFromRegisters(const RegisterDictionaryPtr &, const SmtSolverPtr &solver=SmtSolverPtr())
Instantiates a new RiscOperators object and configures it to use semantic values and states that are ...
void transcodeInterpretation(SgAsmInterpretation *, std::ostream &)
Transcode an entire binary interpretation.
virtual std::string next_label()
Obtain the name for an LLVM label, excluding the "%" sigil.
virtual SmtSolverPtr solver() const
Property: Satisfiability module theory (SMT) solver.
virtual ExpressionPtr emit_signed_binary(std::ostream &, const std::string &llvm_op, const ExpressionPtr &, const ExpressionPtr &)
Emit an operation as LLVM instructions.
virtual ExpressionPtr emit_memory_read(std::ostream &, const ExpressionPtr &address, size_t nbits)
Emit an operation as LLVM instructions.
std::shared_ptr< SmtSolver > SmtSolverPtr
Reference counting pointer.
Leaf node of an expression tree for instruction semantics.
Interior node of an expression tree for instruction semantics.
virtual SValuePtr protoval() const
Property: Prototypical semantic value.
virtual void add_rewrite(const ExpressionPtr &from, const LeafPtr &to)
Register a rewrite.
virtual ExpressionPtr emit_unsigned_resize(std::ostream &, const ExpressionPtr &value, size_t nbits)
Emit an operation as LLVM instructions.
virtual void reset()
Reset to initial state.
Base class for semantics machine states.
virtual ExpressionPtr emit_signed_divide(std::ostream &, const ExpressionPtr &numerator, const ExpressionPtr &denominator)
Emit an operation as LLVM instructions.
virtual std::string function_label(SgAsmFunction *)
Obtain a label for a function.
Cause indentation until this object is destroyed.
Represents an interpretation of a binary container.
virtual void emit_changed_state(std::ostream &)
Output LLVM to bring the LLVM state up to date with respect to the ROSE state.
virtual ExpressionPtr emit_sign_extend(std::ostream &, const ExpressionPtr &value, size_t nbits)
Emit an operation as LLVM instructions.
virtual std::string llvm_lvalue(const LeafPtr &)
Convert a ROSE variable to an LLVM lvalue.
virtual ExpressionPtr emit_unsigned_binary(std::ostream &, const std::string &llvm_op, const ExpressionPtr &, const ExpressionPtr &)
Emit an operation as LLVM instructions.
virtual ExpressionPtr emit_signed_modulo(std::ostream &, const ExpressionPtr &numerator, const ExpressionPtr &denominator)
Emit an operation as LLVM instructions.
boost::shared_ptr< class Transcoder > TranscoderPtr
Shared-ownership pointer to an LLVM transcoder.
virtual LeafPtr next_temporary(size_t nbits)
Create a temporary variable.
virtual ExpressionPtr emit_compare(std::ostream &, const std::string &llvm_op, const ExpressionPtr &, const ExpressionPtr &)
Emit an operation as LLVM instructions.
virtual void emit_memory_write(std::ostream &, const ExpressionPtr &address, const ExpressionPtr &value)
Emit an operation as LLVM instructions.
int llvmVersion() const
Property: LLVM version number.