Created
January 26, 2016 14:54
-
-
Save jagt/cb0d2f074ceeee2f993e to your computer and use it in GitHub Desktop.
LLVM Hello World from book w/ llvm-3.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "llvm/Bitcode/ReaderWriter.h" | |
#include "llvm/IR/Function.h" | |
#include "llvm/IR/LLVMContext.h" | |
#include "llvm/IR/Module.h" | |
#include "llvm/Support/CommandLine.h" | |
#include "llvm/Support/ErrorOr.h" | |
#include "llvm/Support/MemoryBuffer.h" | |
#include "llvm/Support/raw_ostream.h" | |
using namespace llvm; | |
static cl::opt<std::string> FileName(cl::Positional, cl::desc("Bitcode file"), cl::Required); | |
int main(int argc, char** argv) { | |
cl::ParseCommandLineOptions(argc, argv, "LLVM hello world\n"); | |
LLVMContext context; | |
ErrorOr<std::unique_ptr<MemoryBuffer>> mb = | |
MemoryBuffer::getFile(FileName); | |
if (std::error_code ec = mb.getError()) { | |
errs() << ec.message(); | |
return -1; | |
} | |
ErrorOr<Module*> m = parseBitcodeFile(mb->get()->getMemBufferRef(), context); | |
if (std::error_code ec = m.getError()) { | |
errs() << "Error reading bitcode: " << ec.message() << "\n"; | |
return -1; | |
} | |
for (Module::const_iterator i = m.get()->getFunctionList().begin(), | |
e = m.get()->getFunctionList().end(); i != e; ++i) { | |
if (!i->isDeclaration()) { | |
outs() << i->getName() << " has " << i->size() | |
<< " basic blocks.\n"; | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LLVM_CONFIG?=llvm-config-3.6 | |
ifndef VERBOSE | |
QUIET:=@ | |
endif | |
SRC_DIR?=$(PWD) | |
LDFLAGS+=$(shell $(LLVM_CONFIG) --ldflags) | |
COMMON_FLAGS=-Wall -Wextra | |
CXXFLAGS+=$(COMMON_FLAGS) $(shell $(LLVM_CONFIG) --cxxflags) -fno-rtti | |
CPPFLAGS+=$(shell $(LLVM_CONFIG) --cppflags) -I$(SRC_DIR) | |
LLVMLIBS=$(shell $(LLVM_CONFIG) --libs bitreader support) | |
SYSTEMLIBS=$(shell $(LLVM_CONFIG) --system-libs) | |
HELLO=helloworld | |
HELLO_OBJECTS=hello.o | |
default: $(HELLO) | |
%.o : $(SRC_DIR)/%.cpp | |
@echo Compiling $*.cpp | |
$(QUIET)$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< | |
$(HELLO) : $(HELLO_OBJECTS) | |
@echo Linking $@ | |
$(QUIET)$(CXX) -o $@ $(CXXFLAGS) $(LDFLAGS) $^ $(LLVMLIBS) $(SYSTEMLIBS) | |
clean:: | |
$(QUIET)rm -f $(HELLO) $(HELLO_OBJECTS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment