added first llvm project
git-svn-id: svn://anubis/misc/llvm@60 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
parent
05887de9c5
commit
a38bfa8400
11
first/Makefile
Normal file
11
first/Makefile
Normal file
@ -0,0 +1,11 @@
|
||||
|
||||
TARGET := first
|
||||
OBJS := $(TARGET).o
|
||||
|
||||
CXXFLAGS := `llvm-config --cxxflags`
|
||||
LDFLAGS := `llvm-config --ldflags --libs core`
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CXX) -o $@ $< $(LDFLAGS)
|
59
first/first.cc
Normal file
59
first/first.cc
Normal file
@ -0,0 +1,59 @@
|
||||
|
||||
#include <llvm/Module.h>
|
||||
#include <llvm/Function.h>
|
||||
#include <llvm/PassManager.h>
|
||||
#include <llvm/CallingConv.h>
|
||||
#include <llvm/Analysis/Verifier.h>
|
||||
#include <llvm/Assembly/PrintModulePass.h>
|
||||
#include <llvm/Support/IRBuilder.h>
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
Module * makeLLVMModule();
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
Module * Mod = makeLLVMModule();
|
||||
|
||||
verifyModule(*Mod, PrintMessageAction);
|
||||
|
||||
PassManager PM;
|
||||
PM.add(new PrintModulePass(&llvm::cout));
|
||||
PM.run(*Mod);
|
||||
|
||||
delete Mod;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Module * makeLLVMModule()
|
||||
{
|
||||
Module * mod = new Module("test");
|
||||
|
||||
Constant * c = mod->getOrInsertFunction("mul_add",
|
||||
/* ret type */ IntegerType::get(32),
|
||||
/* args */ IntegerType::get(32),
|
||||
IntegerType::get(32),
|
||||
IntegerType::get(32),
|
||||
NULL);
|
||||
Function * mul_add = cast<Function>(c);
|
||||
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("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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user