[LLVMdev] LLVM Basic Program Compilation

Chris Cadwallader ccadwallader at arxan.com
Sat Aug 2 09:12:26 PDT 2014


Have you seen the Kaleidoscope tutorial on llvm? It is a parser to llvm IR of a toy language that is then JIT'd.  Sounds like it might be similar to your goals.

http://llvm.org/docs/tutorial/LangImpl1.html

Also, Tim is right about your undefined symbols. Something might be wrong with your build or you omitted the libs from your compile.

- Chris


On Aug 2, 2014, at 4:02 AM, Prakash Premkumar <prakash.prax at gmail.com<mailto:prakash.prax at gmail.com>> wrote:

Thanks a lot Tim.

I modified my code add the main function :

#include <stdio.h>
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Function.h"

int main()
{
  llvm::LLVMContext& context = llvm::getGlobalContext();
  llvm::Module* module = new llvm::Module("top", context);
  llvm::IRBuilder<> builder(context);
  llvm::FunctionType *funcType =
  llvm::FunctionType::get(builder.getInt32Ty(), false);
  llvm::Function *mainFunc =llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, "main", module);
  module->dump( );
}

and when i compile with the commands you specified i get the following error:

Undefined symbols for architecture x86_64:
  "llvm::FunctionType::get(llvm::Type*, bool)", referenced from:
      _main in try-nnbqRa.o
  "llvm::getGlobalContext()", referenced from:
      _main in try-nnbqRa.o
  "llvm::llvm_unreachable_internal(char const*, char const*, unsigned int)", referenced from:
      llvm::User::operator delete(void*, unsigned int) in try-nnbqRa.o
  "llvm::Type::getInt32Ty(llvm::LLVMContext&)", referenced from:
      llvm::IRBuilderBase::getInt32Ty() in try-nnbqRa.o
  "llvm::User::operator new(unsigned long, unsigned int)", referenced from:
      llvm::Function::Create(llvm::FunctionType*, llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*) in try-nnbqRa.o
  "llvm::Module::Module(llvm::StringRef, llvm::LLVMContext&)", referenced from:
      _main in try-nnbqRa.o
  "llvm::Function::Function(llvm::FunctionType*, llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*)", referenced from:
      llvm::Function::Create(llvm::FunctionType*, llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*) in try-nnbqRa.o
  "llvm::Module::dump() const", referenced from:
      _main in try-nnbqRa.o
ld: symbol(s) not found for architecture x86_64

As you mentioned try.s is unlinked, Can you please tell me how i will link it.

Basically I am trying to write a simple interpreter for a language which has a while loop and translates the code to llvm IR. I am writing the interpreter in C.

Thanks a lot


On Sat, Aug 2, 2014 at 1:07 PM, Tim Northover <t.p.northover at gmail.com<mailto:t.p.northover at gmail.com>> wrote:
Hi Prakash,

The way you're trying to use LLVM seems like a very odd way to start.

The natural beginning move would be to write a C++ program that
produced a simple module and then try to execute that simple module
(maybe with lli).

But what you're doing is creating an (extremely complex!) module
(try.s) that, when executed will use LLVM again to create a much
simpler module. Rather more self-referential than I'd expect (and much
more difficult to get right, as you're discovering).

The way I'd expect you to be testing that program is:
$ clang++ `llvm-config --cxx-flags --ldflags --libs` try.cpp -otry
$ ./try 2> mymodule.ll
$ lli mymodule.ll

(You'll still get an error, because you need to define a @main
function to be able to execute a module, but it's a much simpler
error).

> gives me Program used external function '_ZN4llvm16getGlobalContextEv' which
> could not be resolved!

I'll say a bit about this error anyway. The "try.s" module produced by
your command has many dangling references to all the functions it
refers to from the LLVM libraries. It's the equivalent of a plain
object file ("clang -c xyz.c") and hasn't been linked at all, so you
shouldn't expect to be able to execute it directly (without more
work).

Cheers.

Tim.

_______________________________________________
LLVM Developers mailing list
LLVMdev at cs.uiuc.edu<mailto:LLVMdev at cs.uiuc.edu>         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140802/373009ef/attachment.html>


More information about the llvm-dev mailing list