Oops. I wanted the compile flags for C++, not the C preprocessor flags.
[oota-llvm.git] / docs / tutorial / JITTutorial1.html
index 5a2dcd0cbe1dd5efd15554a22f0dc5962b65f08f..4c5a1203c956a7c7bd02f35f08c708088f75f82b 100644 (file)
@@ -25,7 +25,7 @@
 
 <div class="doc_text">
 
-<p>For starters, lets consider a relatively straightforward function that takes three integer parameters and returns an arithmetic combination of them.  This is nice and simple, especially since it involves no control flow:</p>
+<p>For starters, let's consider a relatively straightforward function that takes three integer parameters and returns an arithmetic combination of them.  This is nice and simple, especially since it involves no control flow:</p>
 
 <div class="doc_code">
 <pre>
@@ -50,7 +50,19 @@ entry:
 
 <p>If you're unsure what the above code says, skim through the <a href="../LangRef.html">LLVM Language Reference Manual</a> and convince yourself that the above LLVM IR is actually equivalent to the original function.  Once you’re satisfied with that, let’s move on to actually generating it programmatically!</p>
 
-<p>... STUFF ABOUT HEADERS ... </p>
+<p>Of course, before we can start, we need to <code>#include</code> the appropriate LLVM header files:</p>
+
+<div class="doc_code">
+<pre>
+#include &lt;llvm/Module.h&gt;
+#include &lt;llvm/Function.h&gt;
+#include &lt;llvm/PassManager.h&gt;
+#include &lt;llvm/CallingConv.h&gt;
+#include &lt;llvm/Analysis/Verifier.h&gt;
+#include &lt;llvm/Assembly/PrintModulePass.h&gt;
+#include &lt;llvm/Support/LLVMBuilder.h&gt;
+</pre>
+</div>
 
 <p>Now, let’s get started on our real program.  Here’s what our basic <code>main()</code> will look like:</p>
 
@@ -74,13 +86,13 @@ int main(int argc, char**argv) {
 </pre>
 </div>
 
-<p>The first segment is pretty simple: it creates an LLVM “module.”  In LLVM, a module represents a single unit of code that is to be processed together.  A module contains things like global variables and function declarations and implementations.  Here, we’ve declared a <code>makeLLVMModule()</code> function to do the real work of creating the module.  Don’t worry, we’ll be looking at that one next!</p>
+<p>The first segment is pretty simple: it creates an LLVM “module.”  In LLVM, a module represents a single unit of code that is to be processed together.  A module contains things like global variables, function declarations, and implementations.  Here we’ve declared a <code>makeLLVMModule()</code> function to do the real work of creating the module.  Don’t worry, we’ll be looking at that one next!</p>
 
 <p>The second segment runs the LLVM module verifier on our newly created module.  While this probably isn’t really necessary for a simple module like this one, it’s always a good idea, especially if you’re generating LLVM IR based on some input.  The verifier will print an error message if your LLVM module is malformed in any way.</p>
 
 <p>Finally, we instantiate an LLVM <code>PassManager</code> and run the <code>PrintModulePass</code> on our module.  LLVM uses an explicit pass infrastructure to manage optimizations and various other things.  A <code>PassManager</code>, as should be obvious from its name, manages passes: it is responsible for scheduling them, invoking them, and insuring the proper disposal after we’re done with them.  For this example, we’re just using a trivial pass that prints out our module in textual form.</p>
 
-<p>Now onto the interesting part: creating a populating a module.  Here’s the first chunk of our <code>makeLLVMModule()</code>:</p>
+<p>Now onto the interesting part: creating and populating a module.  Here’s the first chunk of our <code>makeLLVMModule()</code>:</p>
 
 <div class="doc_code">
 <pre>
@@ -94,7 +106,7 @@ Module* makeLLVMModule() {
 
 <div class="doc_code">
 <pre>
-  Constant* c = mod->getOrInsertFunction("mul_add",
+  Constant* c = mod-&gt;getOrInsertFunction("mul_add",
   /*ret type*/                           IntegerType::get(32),
   /*args*/                               IntegerType::get(32),
                                          IntegerType::get(32),
@@ -102,31 +114,31 @@ Module* makeLLVMModule() {
   /*varargs terminated with null*/       NULL);
   
   Function* mul_add = cast&lt;Function&gt;(c);
-  mul_add->setCallingConv(CallingConv::C);
+  mul_add-&gt;setCallingConv(CallingConv::C);
 </pre>
 </div>
 
-<p>We construct our <code>Function</code> by calling <code>getOrInsertFunction()</code> on our module, passing in the name, return type, and argument types of the function.  In the case of our <code>mul_add</code> function, that means one 32-bit integer for the return value, and three 32-bit integers for the arguments.</p>
+<p>We construct our <code>Function</code> by calling <code>getOrInsertFunction()</code> on our module, passing in the name, return type, and argument types of the function.  In the case of our <code>mul_add</code> function, that means one 32-bit integer for the return value and three 32-bit integers for the arguments.</p>
 
-<p>You'll notice that <code>getOrInsertFunction</code> doesn't actually return a <code>Function*</code>.  This is because, if the function already existed, but with a different prototype, <code>getOrInsertFunction</code> will return a cast of the existing function to the desired prototype.  Since we know that there's not already a <code>mul_add</code> function, we can safely just cast <code>c</code> to a <code>Function*</code>.
+<p>You'll notice that <code>getOrInsertFunction()</code> doesn't actually return a <code>Function*</code>.  This is because <code>getOrInsertFunction()</code> will return a cast of the existing function if the function already existed with a different prototype.  Since we know that there's not already a <code>mul_add</code> function, we can safely just cast <code>c</code> to a <code>Function*</code>.
   
 <p>In addition, we set the calling convention for our new function to be the C calling convention.  This isn’t strictly necessary, but it insures that our new function will interoperate properly with C code, which is a good thing.</p>
 
 <div class="doc_code">
 <pre>
-  Function::arg_iterator args = mul_add->arg_begin();
+  Function::arg_iterator args = mul_add-&gt;arg_begin();
   Value* x = args++;
-  x->setName("x");
+  x-&gt;setName("x");
   Value* y = args++;
-  y->setName("y");
+  y-&gt;setName("y");
   Value* z = args++;
-  z->setName("z");
+  z-&gt;setName("z");
 </pre>
 </div>
 
-<p>While we’re setting up our function, let’s also give names to the parameters.  This also isn’t strictly necessary (LLVM will generate names for them if you don’t specify them), but it’ll make looking at our output somewhat more pleasant.  To name the parameters, we iterator over the arguments of our function, and call <code>setName()</code> on them.  We’ll also keep the pointer to <code>x</code>, <code>y</code>, and <code>z</code> around, since we’ll need them when we get around to creating instructions.</p>
+<p>While we’re setting up our function, let’s also give names to the parameters.  This also isn’t strictly necessary (LLVM will generate names for them if you don’t specify them), but it’ll make looking at our output somewhat more pleasant.  To name the parameters, we iterate over the arguments of our function and call <code>setName()</code> on them.  We’ll also keep the pointer to <code>x</code>, <code>y</code>, and <code>z</code> around, since we’ll need them when we get around to creating instructions.</p>
 
-<p>Great!  We have a function now.  But what good is a function if it has no body?  Before we start working on a body for our new function, we need to recall some details of the LLVM IR.  The IR, being an abstract assembly language, represents control flow using jumps (we call them branches), both conditional and unconditional.  The straight-line sequences of code between branches are called basic blocks, or just blocks.  To create a body for our function, we fill it with blocks!</p>
+<p>Great!  We have a function now.  But what good is a function if it has no body?  Before we start working on a body for our new function, we need to recall some details of the LLVM IR.  The IR, being an abstract assembly language, represents control flow using jumps (we call them branches), both conditional and unconditional.  The straight-line sequences of code between branches are called basic blocks, or just blocks.  To create a body for our function, we fill it with blocks:</p>
 
 <div class="doc_code">
 <pre>
@@ -153,10 +165,18 @@ Module* makeLLVMModule() {
 
 <p>The final step in creating our function is to create the instructions that make it up.  Our <code>mul_add</code> function is composed of just three instructions: a multiply, an add, and a return.  <code>LLVMBuilder</code> gives us a simple interface for constructing these instructions and appending them to the “entry” block.  Each of the calls to <code>LLVMBuilder</code> returns a <code>Value*</code> that represents the value yielded by the instruction.  You’ll also notice that, above, <code>x</code>, <code>y</code>, and <code>z</code> are also <code>Value*</code>’s, so it’s clear that instructions operate on <code>Value*</code>’s.</p>
 
-<p>And that’s it!  Now you can compile and run your code, and get a wonder textual print out of the LLVM IR we saw at the beginning.</p>
+<p>And that’s it!  Now you can compile and run your code, and get a wonderful textual print out of the LLVM IR we saw at the beginning.  To compile, use the following command line as a guide:</p>
+
+<div class="doc_code">
+<pre>
+# c++ -g tut1.cpp `llvm-config --cxxflags --ldflags --libs core` -o tut1
+# ./tut1
+</pre>
+</div>
 
-<p> ... SECTION ABOUT USING llvm-config TO GET THE NECESSARY COMPILER FLAGS TO COMPILE YOUR CODE ... </p>
+<p>The <code>llvm-config</code> utility is used to obtain the necessary GCC-compatible compiler flags for linking with LLVM.  For this example, we only need the 'core' library.  We'll use others once we start adding optimizers and the JIT engine.</p>
 
+<a href="JITTutorial2.html">Next: A More Complicated Function</a>
 </div>
 
 <!-- *********************************************************************** -->
@@ -173,4 +193,4 @@ Module* makeLLVMModule() {
 </address>
 
 </body>
-</html>
\ No newline at end of file
+</html>