added test code to generate a test routine that simply returns...

colton
_xeroxz 3 years ago
parent e9c0f6846c
commit 2c85ccae65

@ -1,65 +1,34 @@
#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/IR/CallingConv.h"
#include <Windows.h>
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/IRPrintingPasses.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Pass.h"
#include "llvm/Support/raw_ostream.h"
#include <stdio.h>
using namespace llvm;
Module *makeLLVMModule( LLVMContext &Context );
int main( int argc, char **argv )
{
LLVMContext Context;
Module *Mod = makeLLVMModule( Context );
raw_fd_ostream r( fileno( stdout ), false );
verifyModule( *Mod, &r );
// Prints the module IR
ModulePass *m = createPrintModulePass( outs(), "Module IR printer" );
legacy::PassManager PM;
PM.add( m );
PM.run( *Mod );
// Write IR to a bitcode file
FILE *mul_add_file = fopen( "mul_add.bc", "w+" );
raw_fd_ostream bitcodeWriter( fileno( mul_add_file ), true );
WriteBitcodeToFile( *Mod, bitcodeWriter );
delete Mod;
return 0;
}
Module *makeLLVMModule( LLVMContext &Context )
{
Module *mod = new Module( "mul_add", Context );
FunctionCallee mul_add_fun =
mod->getOrInsertFunction( "mul_add", Type::getInt32Ty( Context ), Type::getInt32Ty( Context ),
Type::getInt32Ty( Context ), Type::getInt32Ty( Context ) );
Function *mul_add = cast< Function >( mul_add_fun.getCallee() );
mul_add->setCallingConv( CallingConv::C );
Function::arg_iterator args = mul_add->arg_begin();
Value *x = args++;
x->setName( "x" );
Value *y = args++;
y->setName( "y" );
Value *z = args++;
z->setName( "z" );
BasicBlock *block = BasicBlock::Create( Context, "entry", mul_add );
IRBuilder<> builder( block );
Value *tmp = builder.CreateBinOp( Instruction::Mul, x, y, "tmp" );
Value *tmp2 = builder.CreateBinOp( Instruction::Add, tmp, z, "tmp2" );
builder.CreateRet( tmp2 );
return mod;
// test for now...
LLVMContext llvm_ctx;
Module llvm_module( "VMProtect 2 Static Devirtualizer", llvm_ctx );
IRBuilder<> ir_builder( llvm_ctx );
FunctionType *FT = FunctionType::get( Type::getVoidTy( llvm_ctx ), false );
Function *F = Function::Create( FT, Function::ExternalLinkage, "test_func", llvm_module );
F->setCallingConv( CallingConv::C );
auto basic_block = BasicBlock::Create( llvm_ctx, "test", F );
ir_builder.SetInsertPoint( basic_block );
ir_builder.CreateRetVoid();
basic_block->print( outs() );
}
Loading…
Cancel
Save