Skip to content

Commit

Permalink
remove instructionInfoTable
Browse files Browse the repository at this point in the history
  • Loading branch information
ladisgin committed Aug 23, 2023
1 parent cdaf174 commit 4332026
Show file tree
Hide file tree
Showing 16 changed files with 539 additions and 506 deletions.
13 changes: 10 additions & 3 deletions include/klee/Core/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <unordered_map>
#include <vector>

#include "llvm/ADT/StringRef.h"
#include <nonstd/optional.hpp>

using nonstd::optional;
Expand Down Expand Up @@ -61,6 +62,12 @@ class InterpreterHandler {
const char *suffix, bool isError = false) = 0;
};

// TODO remove
using FInstructions = std::unordered_map<
std::string,
std::unordered_map<
unsigned, std::unordered_map<unsigned, std::unordered_set<unsigned>>>>;

class Interpreter {
public:
enum class GuidanceKind {
Expand Down Expand Up @@ -140,9 +147,9 @@ class Interpreter {
setModule(std::vector<std::unique_ptr<llvm::Module>> &userModules,
std::vector<std::unique_ptr<llvm::Module>> &libsModules,
const ModuleOptions &opts,
const std::unordered_set<std::string> &mainModuleFunctions,
const std::unordered_set<std::string> &mainModuleGlobals,
std::unique_ptr<InstructionInfoTable> origInfos) = 0;
std::set<llvm::StringRef> &mainModuleFunctions,
std::set<llvm::StringRef> &mainModuleGlobals,
FInstructions &&origInstructions) = 0;

// supply a tree stream writer which the interpreter will use
// to record the concrete path (as a stream of '0' and '1' bytes).
Expand Down
160 changes: 75 additions & 85 deletions include/klee/Module/InstructionInfoTable.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//===-- InstructionInfoTable.h ----------------------------------*- C++ -*-===//
//
// The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
////===-- InstructionInfoTable.h ----------------------------------*- C++ -*-===//
////
//// The KLEE Symbolic Virtual Machine
////
//// This file is distributed under the University of Illinois Open Source
//// License. See LICENSE.TXT for details.
////
////===----------------------------------------------------------------------===//

#ifndef KLEE_INSTRUCTIONINFOTABLE_H
#define KLEE_INSTRUCTIONINFOTABLE_H
Expand All @@ -31,91 +31,81 @@ class Module;

namespace klee {

// TODO move to methods of kInstruction
/// @brief InstructionInfo stores debug information for a KInstruction.
struct InstructionInfo {
/// @brief The instruction id.
unsigned id;
/// @brief Line number in source file.
// unsigned line;
/// @brief Column number in source file.
// unsigned column;
/// @brief Line number in generated assembly.ll.
llvm::Optional<uint64_t> assemblyLine;
/// @brief Source file name.
// const std::string &file;

public:
InstructionInfo(unsigned id, llvm::Optional<uint64_t> assemblyLine)
: id{id}, assemblyLine{assemblyLine} {}
};

/// @brief FunctionInfo stores debug information for a KFunction.
struct FunctionInfo { // TODO clear this too
/// @brief The function id.
unsigned id;
/// @brief Line number in source file.
unsigned line;
/// @brief Line number in generated assembly.ll.
llvm::Optional<uint64_t> assemblyLine;
/// @brief Source file name.
const std::string &file;

public:
FunctionInfo(unsigned id, const std::string &file, unsigned line,
llvm::Optional<uint64_t> assemblyLine)
: id{id}, line{line}, assemblyLine{assemblyLine}, file{file} {}

FunctionInfo(const FunctionInfo &) = delete;
FunctionInfo &operator=(FunctionInfo const &) = delete;

FunctionInfo(FunctionInfo &&) = default;
};

class InstructionInfoTable {
public:
using Instructions = std::unordered_map<
std::string,
std::unordered_map<
unsigned int,
std::unordered_map<unsigned int, std::unordered_set<unsigned int>>>>;
using LocationToFunctionsMap =
std::unordered_map<std::string,
std::unordered_set<const llvm::Function *>>;

private:
std::unordered_map<const llvm::Instruction *,
std::unique_ptr<InstructionInfo>>
infos; // TODO change to llvm::Instruction* -> KInstruction*
std::unordered_map<const llvm::Function *, std::unique_ptr<FunctionInfo>>
functionInfos;
std::vector<std::unique_ptr<std::string>> internedStrings; // TODO remove
LocationToFunctionsMap fileNameToFunctions; // TODO remove
std::unordered_set<std::string> filesNames; // TODO remove
Instructions insts; // TODO remove when move prepare target to main

public:
explicit InstructionInfoTable(
const llvm::Module &m, std::unique_ptr<llvm::raw_fd_ostream> assemblyFS,
bool withInstructions = false);

unsigned getMaxID() const;
const InstructionInfo &getInfo(const llvm::Instruction &) const;
const FunctionInfo &getFunctionInfo(const llvm::Function &) const;
const LocationToFunctionsMap &getFileNameToFunctions() const;
const std::unordered_set<std::string> &getFilesNames() const;
Instructions getInstructions();
};
//// TODO move to methods of kInstruction
///// @brief InstructionInfo stores debug information for a KInstruction.
//struct InstructionInfo {
// /// @brief The instruction id.
//// unsigned id; // TODO move to kInstruction
// /// @brief Line number in source file.
// // unsigned line;
// /// @brief Column number in source file.
// // unsigned column;
// /// @brief Line number in generated assembly.ll.
//// llvm::Optional<uint64_t> assemblyLine;
// /// @brief Source file name.
//// const std::string &file;
//
//public:
// InstructionInfo(unsigned id) {}
//};
//
///// @brief FunctionInfo stores debug information for a KFunction.
//struct FunctionInfo { // TODO clear this too
// /// @brief The function id.
//// unsigned id; // TODO move to kFunction
// /// @brief Line number in source file.
//// unsigned line;
// /// @brief Line number in generated assembly.ll.
//// llvm::Optional<uint64_t> assemblyLine;
// /// @brief Source file name.
//// const std::string &file;
//
//public:
// FunctionInfo(unsigned id) {}
//
// FunctionInfo(const FunctionInfo &) = delete;
// FunctionInfo &operator=(FunctionInfo const &) = delete;
//
// FunctionInfo(FunctionInfo &&) = default;
//};
//
//class InstructionInfoTable {
//public:
// using LocationToFunctionsMap =
// std::unordered_map<std::string,
// std::unordered_set<const llvm::Function *>>;
//
//private:
// std::unordered_map<const llvm::Instruction *,
// std::unique_ptr<InstructionInfo>> infos;
// std::unordered_map<const llvm::Function *, std::unique_ptr<FunctionInfo>>
// functionInfos;
// LocationToFunctionsMap fileNameToFunctions; // TODO remove
//// Instructions insts; // TODO remove when move prepare target to main
//
//public:
// explicit InstructionInfoTable(
// const llvm::Module &m);
//
//// unsigned getMaxID() const;
// const InstructionInfo &getInfo(const llvm::Instruction &) const;
// const FunctionInfo &getFunctionInfo(const llvm::Function &) const;
// const LocationToFunctionsMap &getFileNameToFunctions() const;
//// Instructions getInstructions();
//};

struct LocationInfo {
std::string file;
size_t line;
size_t column;
};


// TODO need unify with kFunction
LocationInfo getLocationInfo(const llvm::Function *func);

// TODO need unify with kInstruction
LocationInfo getLocationInfo(const llvm::Instruction &Inst,
const FunctionInfo *f);
LocationInfo getLocationInfo(const llvm::Instruction *inst);

} // namespace klee

Expand Down
39 changes: 32 additions & 7 deletions include/klee/Module/KInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
#include "klee/Support/CompilerWarning.h"
DISABLE_WARNING_PUSH
DISABLE_WARNING_DEPRECATED_DECLARATIONS
#include "KModule.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/raw_ostream.h"
DISABLE_WARNING_POP

#include <optional>
#include <vector>

namespace llvm {
Expand All @@ -28,15 +30,15 @@ class Instruction;

namespace klee {
class Executor;
struct InstructionInfo;
//struct InstructionInfo;
class KModule;
struct KBlock;

/// KInstruction - Intermediate instruction representation used
/// during execution.
struct KInstruction {
llvm::Instruction *inst;
const InstructionInfo *info; // TODO remove it
// const InstructionInfo *info; // TODO remove it

/// Value numbers for each operand. -1 is an invalid value,
/// otherwise negative numbers are indices (negated and offset by
Expand All @@ -45,18 +47,27 @@ struct KInstruction {
int *operands;
KBlock *parent;

private:
// Instruction index in the basic block
size_t index; // TODO maybe change to pointer or size_t
const unsigned globalIndex;

/// Destination register index.
// unsigned dest;
public:
/// Unique index for KFunction and KInstruction inside KModule
/// from 0 to [KFunction + KInstruction]
[[nodiscard]] unsigned getGlobalIndex() const;
/// Instruction index in the basic block
[[nodiscard]] unsigned getIndex() const; // TODO change to uintptr_t
[[nodiscard]] unsigned getIndex() const;
/// Destination register index.
[[nodiscard]] unsigned getDest() const; // TODO change to uintptr_t
[[nodiscard]] unsigned getDest() const;

KInstruction() = default; // TODO remove default constructor
KInstruction(const std::unordered_map<llvm::Instruction *, unsigned>
&_instructionToRegisterMap,
llvm::Instruction *_inst, KModule *_km, KBlock *_kb,
unsigned &_globalIndexInc);

KInstruction() = delete; // TODO remove default constructor
explicit KInstruction(const KInstruction &ki) = delete;
virtual ~KInstruction();

Expand All @@ -66,6 +77,14 @@ struct KInstruction {

[[nodiscard]] std::string getSourceLocationString() const;
[[nodiscard]] std::string toString() const;

[[nodiscard]] inline KBlock *getKBlock() const { return parent; }
[[nodiscard]] inline KFunction *getKFunction() const {
return getKBlock()->parent;
}
[[nodiscard]] inline KModule *getKModule() const {
return getKFunction()->parent;
}
};

struct KGEPInstruction : KInstruction {
Expand All @@ -80,7 +99,13 @@ struct KGEPInstruction : KInstruction {
uint64_t offset;

public:
KGEPInstruction() = default;
KGEPInstruction(const std::unordered_map<llvm::Instruction *, unsigned>
&_instructionToRegisterMap,
llvm::Instruction *_inst, KModule *_km, KBlock *_kb,
unsigned &_globalIndexInc)
: KInstruction(_instructionToRegisterMap, _inst, _km, _kb,
_globalIndexInc) {}
KGEPInstruction() = delete;
explicit KGEPInstruction(const KGEPInstruction &ki) = delete;
};
} // namespace klee
Expand Down
Loading

0 comments on commit 4332026

Please sign in to comment.