Skip to content

Instantly share code, notes, and snippets.

@stmtk1
Last active June 18, 2020 15:03
Show Gist options
  • Save stmtk1/b89e11595ed4eca6b6753cecfd2db343 to your computer and use it in GitHub Desktop.
Save stmtk1/b89e11595ed4eca6b6753cecfd2db343 to your computer and use it in GitHub Desktop.
#!/bin/zsh
g++ `llvm-config --cxxflags --ldflags --libs --system-libs` main.cpp -o test
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/SourceMgr.h"
static llvm::LLVMContext TheContext;
static llvm::IRBuilder<> Builder(TheContext);
static std::unique_ptr<llvm::Module> TheModule;
int main() {
TheModule = std::make_unique<llvm::Module>("top", TheContext);
llvm::Function* mainFunc = llvm::Function::Create(
llvm::FunctionType::get(llvm::Type::getInt32Ty(TheContext), false),
llvm::Function::ExternalLinkage, "main", TheModule.get());
Builder.SetInsertPoint(llvm::BasicBlock::Create(TheContext, "entry", mainFunc));
llvm::Value* a = Builder.CreateAlloca(Builder.getInt32Ty(), nullptr, "a");
llvm::Value* b = Builder.CreateAlloca(Builder.getInt32Ty(), nullptr, "b");
Builder.CreateStore(Builder.getInt32(32), a);
Builder.CreateStore(Builder.CreateAdd(Builder.CreateLoad(a), Builder.getInt32(8)), b);
Builder.CreateRet(Builder.CreateAdd(Builder.CreateLoad(a), Builder.CreateLoad(b)));
TheModule->print(llvm::errs(), nullptr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment