|
|
|
@ -54,34 +54,28 @@ namespace vm
|
|
|
|
|
void vmp_rtn_t::push( std::uint8_t num_bytes, llvm::Value *val )
|
|
|
|
|
{
|
|
|
|
|
// sub rsp, num_bytes
|
|
|
|
|
auto rsp_addr = ir_builder->CreateLoad( stack );
|
|
|
|
|
auto rsp_i64 = ir_builder->CreatePtrToInt( rsp_addr, ir_builder->getInt64Ty() );
|
|
|
|
|
auto sub_rsp_val = ir_builder->CreateSub( rsp_i64, ir_builder->getInt64( num_bytes ) );
|
|
|
|
|
ir_builder->CreateStore(
|
|
|
|
|
ir_builder->CreateIntToPtr( sub_rsp_val, llvm::PointerType::get( ir_builder->getInt8Ty(), 0ull ) ), stack );
|
|
|
|
|
auto rsp_addr = ir_builder->CreateLoad( stack, "rsp_addr" );
|
|
|
|
|
auto sub_rsp_val = ir_builder->CreateGEP( ir_builder->getInt8Ty(), rsp_addr,
|
|
|
|
|
ir_builder->getInt8( 0 - num_bytes ), "sub_rsp_val" );
|
|
|
|
|
ir_builder->CreateStore( sub_rsp_val, stack );
|
|
|
|
|
|
|
|
|
|
// mov [rsp], val
|
|
|
|
|
rsp_addr = ir_builder->CreateLoad( stack );
|
|
|
|
|
auto rsp_cast_ptr = ir_builder->CreatePointerCast(
|
|
|
|
|
rsp_addr, llvm::PointerType::get( llvm::IntegerType::get( *llvm_ctx, num_bytes * 8 ), false ) );
|
|
|
|
|
ir_builder->CreateStore( val, rsp_cast_ptr );
|
|
|
|
|
auto resized_new_rsp_addr = ir_builder->CreateBitCast(
|
|
|
|
|
sub_rsp_val, llvm::PointerType::get( llvm::IntegerType::get( *llvm_ctx, num_bytes * 8 ), 0ull ) );
|
|
|
|
|
ir_builder->CreateStore( val, resized_new_rsp_addr );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
llvm::Value *vmp_rtn_t::pop( std::uint8_t num_bytes )
|
|
|
|
|
{
|
|
|
|
|
// mov rax, [rsp]
|
|
|
|
|
auto rsp_addr = ir_builder->CreateLoad( stack );
|
|
|
|
|
auto rsp_cast_ptr = ir_builder->CreatePointerCast(
|
|
|
|
|
rsp_addr, llvm::PointerType::get( llvm::IntegerType::get( *llvm_ctx, num_bytes * 8 ), false ) );
|
|
|
|
|
auto pop_val = ir_builder->CreateLoad( rsp_cast_ptr );
|
|
|
|
|
|
|
|
|
|
// add rsp, num_bytes
|
|
|
|
|
auto rsp_i64 = ir_builder->CreatePtrToInt( rsp_addr, ir_builder->getInt64Ty() );
|
|
|
|
|
auto sub_rsp_val = ir_builder->CreateAdd( rsp_i64, ir_builder->getInt64( num_bytes ) );
|
|
|
|
|
auto sub_rsp_val_ptr =
|
|
|
|
|
ir_builder->CreateIntToPtr( sub_rsp_val, llvm::PointerType::get( ir_builder->getInt8Ty(), 0ull ) );
|
|
|
|
|
|
|
|
|
|
ir_builder->CreateStore( sub_rsp_val_ptr, stack );
|
|
|
|
|
auto rsp_addr = ir_builder->CreateLoad( stack, "rsp_addr" );
|
|
|
|
|
auto new_rsp_addr = ir_builder->CreateGEP( ir_builder->getInt8Ty(), rsp_addr, ir_builder->getInt8( num_bytes ),
|
|
|
|
|
"new_rsp_addr" );
|
|
|
|
|
auto resized_new_rsp_addr = ir_builder->CreateBitCast(
|
|
|
|
|
rsp_addr, llvm::PointerType::get( llvm::IntegerType::get( *llvm_ctx, num_bytes * 8 ), 0ull ) );
|
|
|
|
|
auto pop_val = ir_builder->CreateLoad( resized_new_rsp_addr );
|
|
|
|
|
ir_builder->CreateStore( new_rsp_addr, stack );
|
|
|
|
|
ir_builder->CreateStore( llvm::UndefValue::get( ir_builder->getInt8Ty() ), rsp_addr );
|
|
|
|
|
return pop_val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|