compiling IR to OBJ file now! need to make .vmp2 file format v3...

merge-requests/6/head
_xeroxz 3 years ago
parent 4b0e53fbe1
commit 33ba405a21

@ -85,40 +85,22 @@ int main( int argc, const char *argv[] )
llvm::LLVMInitializeX86AsmParser(); llvm::LLVMInitializeX86AsmParser();
llvm::LLVMInitializeX86AsmPrinter(); llvm::LLVMInitializeX86AsmPrinter();
TargetOptions opt;
llvm::SmallVector< char, 128 > buff;
llvm::raw_svector_ostream dest( buff );
legacy::PassManager pass;
auto TargetTriple = sys::getDefaultTargetTriple(); auto TargetTriple = sys::getDefaultTargetTriple();
llvm_module.setTargetTriple( TargetTriple ); llvm_module.setTargetTriple( TargetTriple );
std::string Error; std::string Error;
auto Target = TargetRegistry::lookupTarget( TargetTriple, Error ); auto Target = TargetRegistry::lookupTarget( TargetTriple, Error );
auto CPU = "generic"; auto reloc_model = Optional< Reloc::Model >();
auto Features = ""; auto target_machine = Target->createTargetMachine( TargetTriple, "generic", "", opt, reloc_model );
llvm_module.setDataLayout( target_machine->createDataLayout() );
TargetOptions opt; target_machine->addPassesToEmitFile( pass, dest, nullptr, CGFT_ObjectFile );
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;
}
pass.run( llvm_module ); pass.run( llvm_module );
dest.flush();
std::printf( "> obj size = %d\n", buff.size() );
} }

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

Loading…
Cancel
Save