Skip to content

Commit d12d438

Browse files
committed
First attempt at recovering params/locals
1 parent 88fa7f1 commit d12d438

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

lib/Lifters/FunctionLifter.cpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ void FunctionLifter::VisitBlock(CodeBlock blk) {
15571557
bb_lifted_func.func->addFnAttr(llvm::Attribute::NoInline);
15581558

15591559
this->LiftBasicBlockIntoFunction(bb_lifted_func, blk);
1560-
std::array<llvm::Value *, remill::kNumBlockArgs + 1> args;
1560+
std::vector<llvm::Value *> args(remill::kNumBlockArgs + 1);
15611561
args[remill::kStatePointerArgNum] = state_ptr;
15621562
args[remill::kPCArgNum] =
15631563
options.program_counter_init_procedure(builder, pc_reg, blk.addr);
@@ -1566,6 +1566,19 @@ void FunctionLifter::VisitBlock(CodeBlock blk) {
15661566

15671567
args[remill::kNumBlockArgs] = remill::LoadNextProgramCounterRef(llvm_blk);
15681568

1569+
for (auto &param : curr_decl->params) {
1570+
args.push_back(LoadLiftedValue(param, type_specifier.Dictionary(),
1571+
intrinsics, llvm_blk, state_ptr,
1572+
args[remill::kMemoryPointerArgNum]));
1573+
}
1574+
for (auto &[name, local] : curr_decl->locals) {
1575+
if (local.values.size() == 1) {
1576+
args.push_back(LoadLiftedValue(
1577+
local.values[0], type_specifier.Dictionary(), intrinsics, llvm_blk,
1578+
state_ptr, args[remill::kMemoryPointerArgNum]));
1579+
}
1580+
}
1581+
15691582
auto new_mem_ptr = builder.CreateCall(bb_lifted_func.func, args);
15701583

15711584
auto mem_ptr_ref = remill::LoadMemoryPointerRef(llvm_blk);
@@ -1604,6 +1617,15 @@ FunctionLifter::CreateBasicBlockFunction(const CodeBlock &block) {
16041617
std::vector<llvm::Type *> params = std::vector(
16051618
lifted_func_type->param_begin(), lifted_func_type->param_end());
16061619
params.push_back(llvm::PointerType::get(context, 0));
1620+
size_t first_param_arg = params.size();
1621+
for (auto &param : curr_decl->params) {
1622+
params.push_back(param.type);
1623+
}
1624+
for (auto &[name, local] : curr_decl->locals) {
1625+
if (local.values.size() == 1) {
1626+
params.push_back(local.values[0].type);
1627+
}
1628+
}
16071629

16081630
llvm::FunctionType *func_type =
16091631
llvm::FunctionType::get(lifted_func_type->getReturnType(), params, false);
@@ -1614,7 +1636,7 @@ FunctionLifter::CreateBasicBlockFunction(const CodeBlock &block) {
16141636
llvm::Function::Create(func_type, llvm::GlobalValue::ExternalLinkage, 0u,
16151637
name, this->semantics_module.get());
16161638

1617-
auto memory = remill::NthArgument(func, remill::kMemoryPointerArgNum);
1639+
llvm::Value *memory = remill::NthArgument(func, remill::kMemoryPointerArgNum);
16181640
auto state = remill::NthArgument(func, remill::kStatePointerArgNum);
16191641
auto pc = remill::NthArgument(func, remill::kPCArgNum);
16201642
auto next_pc_out = remill::NthArgument(func, remill::kNumBlockArgs);
@@ -1625,6 +1647,23 @@ FunctionLifter::CreateBasicBlockFunction(const CodeBlock &block) {
16251647

16261648
options.arch->InitializeEmptyLiftedFunction(func);
16271649

1650+
auto &blk = func->getEntryBlock();
1651+
for (auto &param : curr_decl->params) {
1652+
auto arg = func->getArg(first_param_arg++);
1653+
arg->setName(param.name);
1654+
memory = StoreNativeValue(arg, param, type_provider.Dictionary(),
1655+
intrinsics, &blk, state, memory);
1656+
}
1657+
for (auto &[name, local] : curr_decl->locals) {
1658+
if (local.values.size() == 1) {
1659+
auto arg = func->getArg(first_param_arg++);
1660+
arg->setName(name);
1661+
memory =
1662+
StoreNativeValue(arg, local.values[0], type_provider.Dictionary(),
1663+
intrinsics, &blk, state, memory);
1664+
}
1665+
}
1666+
16281667
auto state_ptr = remill::NthArgument(func, remill::kStatePointerArgNum);
16291668
auto pc_arg = remill::NthArgument(func, remill::kPCArgNum);
16301669
auto mem_arg = remill::NthArgument(func, remill::kMemoryPointerArgNum);

0 commit comments

Comments
 (0)