From d3f0cddb62ccab81115f587bc2d3195fd307b2ed Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 23 Oct 2007 04:51:30 +0000 Subject: [PATCH] Finish up expr codegen. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43238 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/tutorial/LangImpl3.html | 68 +++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/docs/tutorial/LangImpl3.html b/docs/tutorial/LangImpl3.html index b236f0bef0f..020cf925d9c 100644 --- a/docs/tutorial/LangImpl3.html +++ b/docs/tutorial/LangImpl3.html @@ -132,12 +132,13 @@ Value *NumberExprAST::Codegen() { -

In the LLVM IR, numeric constants are represented with the ConstantFP class, -which holds the numeric value in an APFloat internally (APFloat has the -capability of holding floating point constants of arbitrary precision). This -code basically just creates and returns a ConstantFP. Note that in the LLVM IR +

In the LLVM IR, numeric constants are represented with the +ConstantFP class, which holds the numeric value in an APFloat +internally (APFloat has the capability of holding floating point +constants of Arbitrary Precision). This code basically just +creates and returns a ConstantFP. Note that in the LLVM IR that constants are all uniqued together and shared. For this reason, the API -uses "the foo::get(...)" idiom instead of a "create" method or "new foo".

+uses "the foo::get(..)" idiom instead of "new foo(..)" or "foo::create(..).

@@ -149,9 +150,10 @@ Value *VariableExprAST::Codegen() {
 
-

References to variables is also quite simple here. In our system, we assume -that the variable has already been emited somewhere and its value is available. -In practice, the only values in the NamedValues map will be arguments. This +

References to variables is also quite simple here. In the simple version +of Kaleidoscope, we assume that the variable has already been emited somewhere +and its value is available. In practice, the only values that can be in the +NamedValues map are function arguments. This code simply checks to see that the specified name is in the map (if not, an unknown variable is being referenced) and returns the value for it.

@@ -176,7 +178,38 @@ Value *BinaryExprAST::Codegen() { +

Binary operators start to get more interesting. The basic idea here is that +we recursively emit code for the left-hand side of the expression, then the +right-hand side, then we compute the result of the binary expression. In this +code, we do a simple switch on the opcode to create the right LLVM instruction. +

+

In this example, the LLVM builder class is starting to show its value. +Because it knows where to insert the newly created instruction, you just have to +specificy what instruction to create (e.g. with CreateAdd), which +operands to use (L and R here) and optionally provide a name +for the generated instruction. One nice thing about LLVM is that the name is +just a hint: if there are multiple additions in a single function, the first +will be named "addtmp" and the second will be "autorenamed" by adding a suffix, +giving it a name like "addtmp42". Local value names for instructions are purely +optional, but it makes it much easier to read the IR dumps.

+ +

LLVM instructions are constrained to +have very strict type properties: for example, the Left and Right operators of +an add instruction have to have the same +type, and that the result of the add matches the operands. Because all values +in Kaleidoscope are doubles, this makes for very simple code for add, sub and +mul.

+ +

On the other hand, LLVM specifies that the fcmp instruction always returns an 'i1' value +(a one bit integer). However, Kaleidoscope wants the value to be a 0.0 or 1.0 +value. In order to get these semantics, we combine the fcmp instruction with +a uitofp instruction. This instruction +converts its input integer into a floating point value by treating the input +as an unsigned value. In contrast, if we used the sitofp instruction, the Kaleidoscope '<' +operator would return 0.0 and -1.0, depending on the input value.

@@ -201,7 +234,24 @@ Value *CallExprAST::Codegen() {
 
-

more todo

+

Code generation for function calls is quite straight-forward with LLVM. The +code above first looks the name of the function up in the LLVM Module's symbol +table. Recall that the LLVM Module is the container that holds all of the +functions we are JIT'ing. By giving each function the same name as what the +user specifies, we can use the LLVM symbol table to resolve function names for +us.

+ +

Once we have the function to call, we recursively codegen each argument that +is to be passed in, and create an LLVM call +instruction. Note that LLVM uses the native C calling conventions by +default, allowing these calls to call into standard library functions like +"sin" and "cos" with no additional effort.

+ +

This wraps up our handling of the four basic expressions that we have so far +in Kaleidoscope. Feel free to go in and add some more. For example, by +browsing the LLVM language reference you'll find +several other interesting instructions that are really easy to plug into our +basic framework.

-- 2.34.1