<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">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.<div><br></div><div><a href="http://llvm.org/docs/tutorial/LangImpl1.html">http://llvm.org/docs/tutorial/LangImpl1.html</a><div><br></div><div>Also, Tim is right about your undefined symbols. Something might be wrong with your build or you omitted the libs from your compile.</div><div><br></div><div>- Chris</div><div><br></div><div><br><div><div>On Aug 2, 2014, at 4:02 AM, Prakash Premkumar <<a href="mailto:prakash.prax@gmail.com">prakash.prax@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr">Thanks a lot Tim.<div><br></div><div>I modified my code add the main function :</div><div><br></div><div>







<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">#include <span class=""><stdio.h><br></span><span class="">#include </span>"llvm/IR/LLVMContext.h"<br>

<span class="">#include </span>"llvm/IR/Module.h"<br><span class="">#include </span>"llvm/IR/IRBuilder.h"<br><span class="">#include </span>"llvm/IR/Function.h"<br><br><span class="">int</span> main()<br>

{<br>  llvm::LLVMContext& context = llvm::getGlobalContext();<br>  llvm::Module* module = <span class="">new</span> llvm::Module(<span class="">"top"</span>, context);<br>  llvm::IRBuilder<> builder(context); <br>

  llvm::FunctionType *funcType =<br>  llvm::FunctionType::get(builder.getInt32Ty(), <span class="">false</span>);<br>  llvm::Function *mainFunc =llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, <span class="">"main"</span>, module);<br>

  module->dump( );<br>}</blockquote><div><br></div><div>and when i compile with the commands you specified i get the following error:</div><div><br></div>







<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Undefined symbols for architecture x86_64:<br>  "llvm::FunctionType::get(llvm::Type*, bool)", referenced from:<br>

      _main in try-nnbqRa.o<br>  "llvm::getGlobalContext()", referenced from:<br>      _main in try-nnbqRa.o<br>  "llvm::llvm_unreachable_internal(char const*, char const*, unsigned int)", referenced from:<br>

      llvm::User::operator delete(void*, unsigned int) in try-nnbqRa.o<br>  "llvm::Type::getInt32Ty(llvm::LLVMContext&)", referenced from:<br>      llvm::IRBuilderBase::getInt32Ty() in try-nnbqRa.o<br>  "llvm::User::operator new(unsigned long, unsigned int)", referenced from:<br>

      llvm::Function::Create(llvm::FunctionType*, llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*) in try-nnbqRa.o<br>  "llvm::Module::Module(llvm::StringRef, llvm::LLVMContext&)", referenced from:<br>

      _main in try-nnbqRa.o<br>  "llvm::Function::Function(llvm::FunctionType*, llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*)", referenced from:<br>      llvm::Function::Create(llvm::FunctionType*, llvm::GlobalValue::LinkageTypes, llvm::Twine const&, llvm::Module*) in try-nnbqRa.o<br>

  "llvm::Module::dump() const", referenced from:<br>      _main in try-nnbqRa.o<br>ld: symbol(s) not found for architecture x86_64 </blockquote><div><br></div><div>As you mentioned try.s is unlinked, Can you please tell me how i will link it.</div>

<div><br></div><div>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.</div><div><br></div><div>Thanks a lot </div>



































</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Aug 2, 2014 at 1:07 PM, Tim Northover <span dir="ltr"><<a href="mailto:t.p.northover@gmail.com" target="_blank">t.p.northover@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Prakash,<br>
<br>
The way you're trying to use LLVM seems like a very odd way to start.<br>
<br>
The natural beginning move would be to write a C++ program that<br>
produced a simple module and then try to execute that simple module<br>
(maybe with lli).<br>
<br>
But what you're doing is creating an (extremely complex!) module<br>
(try.s) that, when executed will use LLVM again to create a much<br>
simpler module. Rather more self-referential than I'd expect (and much<br>
more difficult to get right, as you're discovering).<br>
<br>
The way I'd expect you to be testing that program is:<br>
$ clang++ `llvm-config --cxx-flags --ldflags --libs` try.cpp -otry<br>
$ ./try 2> mymodule.ll<br>
$ lli mymodule.ll<br>
<br>
(You'll still get an error, because you need to define a @main<br>
function to be able to execute a module, but it's a much simpler<br>
error).<br>
<div class=""><br>
> gives me Program used external function '_ZN4llvm16getGlobalContextEv' which<br>
> could not be resolved!<br>
<br>
</div>I'll say a bit about this error anyway. The "try.s" module produced by<br>
your command has many dangling references to all the functions it<br>
refers to from the LLVM libraries. It's the equivalent of a plain<br>
object file ("clang -c xyz.c") and hasn't been linked at all, so you<br>
shouldn't expect to be able to execute it directly (without more<br>
work).<br>
<br>
Cheers.<br>
<span class="HOEnZb"><font color="#888888"><br>
Tim.<br>
</font></span></blockquote></div><br></div>
_______________________________________________<br>LLVM Developers mailing list<br><a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu">http://llvm.cs.uiuc.edu</a><br><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br></blockquote></div><br></div></div></body></html>