From 4b221b4da70783ba00fe82926f7c58c2d502763e Mon Sep 17 00:00:00 2001 From: _xeroxz Date: Fri, 6 Aug 2021 15:43:18 -0700 Subject: [PATCH] figured out how to call CreateGEP lol... --- include/vm_lifters.hpp | 5 ++++- include/vmp_rtn.hpp | 5 +++-- src/lifters/lconst.cpp | 3 ++- src/lifters/sreg.cpp | 22 ++++++++++++++++++++++ src/vmp_rtn.cpp | 14 +++++++++++--- 5 files changed, 42 insertions(+), 7 deletions(-) diff --git a/include/vm_lifters.hpp b/include/vm_lifters.hpp index 1063bba..0e8827b 100644 --- a/include/vm_lifters.hpp +++ b/include/vm_lifters.hpp @@ -23,7 +23,10 @@ namespace vmp2::lifters const vm::instrs::virt_instr_t &vinstr, llvm::IRBuilder<> *ir_builder ) >; extern lifter_callback_t lconstq; - inline std::map< vm::handler::mnemonic_t, lifter_callback_t * > lifters = { { vm::handler::LCONSTQ, &lconstq } }; + extern lifter_callback_t sregq; + + inline std::map< vm::handler::mnemonic_t, lifter_callback_t * > lifters = { { vm::handler::LCONSTQ, &lconstq }, + { vm::handler::SREGQ, &sregq } }; inline bool lift( vm::vmp_rtn_t *rtn, const vm::instrs::code_block_t &vm_code_block, const vm::instrs::virt_instr_t &vinstr, llvm::IRBuilder<> *ir_builder ) diff --git a/include/vmp_rtn.hpp b/include/vmp_rtn.hpp index fa88dc4..05993b6 100644 --- a/include/vmp_rtn.hpp +++ b/include/vmp_rtn.hpp @@ -26,11 +26,12 @@ namespace vm llvm::Function *lift( void ); llvm::LLVMContext *llvm_ctx; llvm::Module *llvm_module; - private: std::uintptr_t rtn_begin; llvm::Function *llvm_fptr; std::shared_ptr< llvm::IRBuilder<> > ir_builder; - llvm::AllocaInst *virutal_registers, *virtual_stack; + llvm::AllocaInst *virtual_stack, *stack_ptr; + + std::vector< llvm::AllocaInst * > virtual_registers; std::vector< llvm::BasicBlock * > llvm_code_blocks; std::vector< vm::instrs::code_block_t > vmp2_code_blocks; }; diff --git a/src/lifters/lconst.cpp b/src/lifters/lconst.cpp index 55e6b4c..638960b 100644 --- a/src/lifters/lconst.cpp +++ b/src/lifters/lconst.cpp @@ -3,7 +3,8 @@ namespace vmp2::lifters { lifter_callback_t lconstq = [ & ]( vm::vmp_rtn_t *rtn, const vm::instrs::code_block_t &vm_code_block, - const vm::instrs::virt_instr_t &vinstr, llvm::IRBuilder<> *ir_builder ) { + const vm::instrs::virt_instr_t &vinstr, llvm::IRBuilder<> *ir_builder ) { auto push_val = llvm::ConstantInt::get( llvm::IntegerType::getInt64Ty( *rtn->llvm_ctx ), vinstr.operand.imm.u ); + }; } \ No newline at end of file diff --git a/src/lifters/sreg.cpp b/src/lifters/sreg.cpp index e69de29..3b899b9 100644 --- a/src/lifters/sreg.cpp +++ b/src/lifters/sreg.cpp @@ -0,0 +1,22 @@ +#include + +namespace vmp2::lifters +{ + // %t1 = %stack[%sp] + // add %sp, 8 + // %vregX = %1 + lifter_callback_t sregq = [ & ]( vm::vmp_rtn_t *rtn, const vm::instrs::code_block_t &vm_code_block, + const vm::instrs::virt_instr_t &vinstr, llvm::IRBuilder<> *ir_builder ) { + auto old_sp = + ir_builder->CreateLoad( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), rtn->stack_ptr, false, "old_sp" ); + + auto new_sp = ir_builder->CreateSub( + old_sp, llvm::ConstantInt::get( llvm::IntegerType::get( *rtn->llvm_ctx, 64 ), 8 ), "new_sp" ); + + ir_builder->CreateStore( new_sp, rtn->stack_ptr ); + + auto top_stack = + ir_builder->CreateGEP( llvm::ArrayType::get( llvm::IntegerType::get( *rtn->llvm_ctx, 8 ), 1024 ), + rtn->virtual_stack, new_sp, "top_stack" ); + }; +} // namespace vmp2::lifters \ No newline at end of file diff --git a/src/vmp_rtn.cpp b/src/vmp_rtn.cpp index ee8f895..3760363 100644 --- a/src/vmp_rtn.cpp +++ b/src/vmp_rtn.cpp @@ -30,13 +30,21 @@ namespace vm ir_builder = std::make_shared< llvm::IRBuilder<> >( *llvm_ctx ); ir_builder->SetInsertPoint( llvm_code_blocks[ 0 ] ); - // allocate virtual register space... - virutal_registers = ir_builder->CreateAlloca( - llvm::ArrayType::get( llvm::IntegerType::get( *llvm_ctx, 64 ), 24 ), nullptr, "vregs" ); + for ( auto idx = 0u; idx < 24; ++idx ) + // allocate virtual register space... + virtual_registers.push_back( + ir_builder->CreateAlloca( llvm::IntegerType::get( *llvm_ctx, 64 ), nullptr, + ( std::string( "vreg" ) + std::to_string( idx ) ).c_str() ) ); // allocate stack space... virtual_stack = ir_builder->CreateAlloca( llvm::ArrayType::get( llvm::IntegerType::get( *llvm_ctx, 8 ), 1024 ), nullptr, "stack" ); + + // allocate stack pointer... + stack_ptr = ir_builder->CreateAlloca( llvm::IntegerType::get( *llvm_ctx, 64 ), nullptr, "sp" ); + + ir_builder->CreateStore( llvm::ConstantInt::get( llvm::IntegerType::getInt64Ty( *llvm_ctx ), 1024 ), + stack_ptr ); } llvm::Function *vmp_rtn_t::lift( void )