From 33ba405a21bc7918c2eb23cf4a050a365870bd49 Mon Sep 17 00:00:00 2001 From: _xeroxz Date: Fri, 13 Aug 2021 00:14:53 -0700 Subject: [PATCH] compiling IR to OBJ file now! need to make .vmp2 file format v3... --- src/main.cpp | 40 +++++++++++----------------------------- src/vmp_rtn.cpp | 36 +++++++++++++++--------------------- 2 files changed, 26 insertions(+), 50 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index faf54a4..dbf3909 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -85,40 +85,22 @@ int main( int argc, const char *argv[] ) llvm::LLVMInitializeX86AsmParser(); llvm::LLVMInitializeX86AsmPrinter(); + TargetOptions opt; + llvm::SmallVector< char, 128 > buff; + llvm::raw_svector_ostream dest( buff ); + legacy::PassManager pass; + auto TargetTriple = sys::getDefaultTargetTriple(); llvm_module.setTargetTriple( TargetTriple ); std::string Error; auto Target = TargetRegistry::lookupTarget( TargetTriple, Error ); - auto CPU = "generic"; - auto Features = ""; - - TargetOptions opt; - auto RM = Optional< Reloc::Model >(); - auto TheTargetMachine = Target->createTargetMachine( TargetTriple, CPU, Features, opt, RM ); - - llvm_module.setDataLayout( TheTargetMachine->createDataLayout() ); - - auto Filename = "output.o"; - std::error_code EC; - raw_fd_ostream dest( Filename, EC, sys::fs::OF_None ); - - if ( EC ) - { - errs() << "Could not open file: " << EC.message(); - return 1; - } - - legacy::PassManager pass; - auto FileType = CGFT_ObjectFile; - - if ( TheTargetMachine->addPassesToEmitFile( pass, dest, nullptr, FileType ) ) - { - errs() << "TheTargetMachine can't emit a file of this type"; - return 1; - } - + auto reloc_model = Optional< Reloc::Model >(); + auto target_machine = Target->createTargetMachine( TargetTriple, "generic", "", opt, reloc_model ); + llvm_module.setDataLayout( target_machine->createDataLayout() ); + target_machine->addPassesToEmitFile( pass, dest, nullptr, CGFT_ObjectFile ); pass.run( llvm_module ); - dest.flush(); + + std::printf( "> obj size = %d\n", buff.size() ); } \ No newline at end of file diff --git a/src/vmp_rtn.cpp b/src/vmp_rtn.cpp index 874218d..92d6a50 100644 --- a/src/vmp_rtn.cpp +++ b/src/vmp_rtn.cpp @@ -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; }