From a38bfa8400f8ba3f59d31e478bc803ad0133f19d Mon Sep 17 00:00:00 2001 From: josh Date: Thu, 30 Oct 2008 03:02:07 +0000 Subject: [PATCH] added first llvm project git-svn-id: svn://anubis/misc/llvm@60 bd8a9e45-a331-0410-811e-c64571078777 --- first/Makefile | 11 ++++++++++ first/first.cc | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 first/Makefile create mode 100644 first/first.cc diff --git a/first/Makefile b/first/Makefile new file mode 100644 index 0000000..81288ac --- /dev/null +++ b/first/Makefile @@ -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) diff --git a/first/first.cc b/first/first.cc new file mode 100644 index 0000000..e55a53e --- /dev/null +++ b/first/first.cc @@ -0,0 +1,59 @@ + +#include +#include +#include +#include +#include +#include +#include + +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(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; +}