Erick Tryzelaar | 37c076b | 2008-03-30 09:57:12 +0000 | [diff] [blame^] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 3 | |
| 4 | <html> |
| 5 | <head> |
| 6 | <title>Kaleidoscope: Implementing code generation to LLVM IR</title> |
| 7 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 8 | <meta name="author" content="Chris Lattner"> |
| 9 | <meta name="author" content="Erick Tryzelaar"> |
| 10 | <link rel="stylesheet" href="../llvm.css" type="text/css"> |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <div class="doc_title">Kaleidoscope: Code generation to LLVM IR</div> |
| 16 | |
| 17 | <ul> |
| 18 | <li><a href="index.html">Up to Tutorial Index</a></li> |
| 19 | <li>Chapter 3 |
| 20 | <ol> |
| 21 | <li><a href="#intro">Chapter 3 Introduction</a></li> |
| 22 | <li><a href="#basics">Code Generation Setup</a></li> |
| 23 | <li><a href="#exprs">Expression Code Generation</a></li> |
| 24 | <li><a href="#funcs">Function Code Generation</a></li> |
| 25 | <li><a href="#driver">Driver Changes and Closing Thoughts</a></li> |
| 26 | <li><a href="#code">Full Code Listing</a></li> |
| 27 | </ol> |
| 28 | </li> |
| 29 | <li><a href="LangImpl4.html">Chapter 4</a>: Adding JIT and Optimizer |
| 30 | Support</li> |
| 31 | </ul> |
| 32 | |
| 33 | <div class="doc_author"> |
| 34 | <p> |
| 35 | Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a> |
| 36 | and <a href="mailto:idadesub@users.sourceforge.net">Erick Tryzelaar</a> |
| 37 | </p> |
| 38 | </div> |
| 39 | |
| 40 | <!-- *********************************************************************** --> |
| 41 | <div class="doc_section"><a name="intro">Chapter 3 Introduction</a></div> |
| 42 | <!-- *********************************************************************** --> |
| 43 | |
| 44 | <div class="doc_text"> |
| 45 | |
| 46 | <p>Welcome to Chapter 3 of the "<a href="index.html">Implementing a language |
| 47 | with LLVM</a>" tutorial. This chapter shows you how to transform the <a |
| 48 | href="OCamlLangImpl2.html">Abstract Syntax Tree</a>, built in Chapter 2, into |
| 49 | LLVM IR. This will teach you a little bit about how LLVM does things, as well |
| 50 | as demonstrate how easy it is to use. It's much more work to build a lexer and |
| 51 | parser than it is to generate LLVM IR code. :) |
| 52 | </p> |
| 53 | |
| 54 | <p><b>Please note</b>: the code in this chapter and later require LLVM 2.3 or |
| 55 | LLVM SVN to work. LLVM 2.2 and before will not work with it.</p> |
| 56 | |
| 57 | </div> |
| 58 | |
| 59 | <!-- *********************************************************************** --> |
| 60 | <div class="doc_section"><a name="basics">Code Generation Setup</a></div> |
| 61 | <!-- *********************************************************************** --> |
| 62 | |
| 63 | <div class="doc_text"> |
| 64 | |
| 65 | <p> |
| 66 | In order to generate LLVM IR, we want some simple setup to get started. First |
| 67 | we define virtual code generation (codegen) methods in each AST class:</p> |
| 68 | |
| 69 | <div class="doc_code"> |
| 70 | <pre> |
| 71 | let rec codegen_expr = function |
| 72 | | Ast.Number n -> ... |
| 73 | | Ast.Variable name -> ... |
| 74 | </pre> |
| 75 | </div> |
| 76 | |
| 77 | <p>The <tt>Codegen.codegen_expr</tt> function says to emit IR for that AST node |
| 78 | along with all the things it depends on, and they all return an LLVM Value |
| 79 | object. "Value" is the class used to represent a "<a |
| 80 | href="http://en.wikipedia.org/wiki/Static_single_assignment_form">Static Single |
| 81 | Assignment (SSA)</a> register" or "SSA value" in LLVM. The most distinct aspect |
| 82 | of SSA values is that their value is computed as the related instruction |
| 83 | executes, and it does not get a new value until (and if) the instruction |
| 84 | re-executes. In other words, there is no way to "change" an SSA value. For |
| 85 | more information, please read up on <a |
| 86 | href="http://en.wikipedia.org/wiki/Static_single_assignment_form">Static Single |
| 87 | Assignment</a> - the concepts are really quite natural once you grok them.</p> |
| 88 | |
| 89 | <p>The |
| 90 | second thing we want is an "Error" exception like we used for the parser, which |
| 91 | will be used to report errors found during code generation (for example, use of |
| 92 | an undeclared parameter):</p> |
| 93 | |
| 94 | <div class="doc_code"> |
| 95 | <pre> |
| 96 | exception Error of string |
| 97 | |
| 98 | let the_module = create_module "my cool jit" |
| 99 | let builder = builder () |
| 100 | let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10 |
| 101 | </pre> |
| 102 | </div> |
| 103 | |
| 104 | <p>The static variables will be used during code generation. |
| 105 | <tt>Codgen.the_module</tt> is the LLVM construct that contains all of the |
| 106 | functions and global variables in a chunk of code. In many ways, it is the |
| 107 | top-level structure that the LLVM IR uses to contain code.</p> |
| 108 | |
| 109 | <p>The <tt>Codegen.builder</tt> object is a helper object that makes it easy to |
| 110 | generate LLVM instructions. Instances of the <a |
| 111 | href="http://llvm.org/doxygen/LLVMBuilder_8h-source.html"><tt>LLVMBuilder</tt></a> |
| 112 | class keep track of the current place to insert instructions and has methods to |
| 113 | create new instructions.</p> |
| 114 | |
| 115 | <p>The <tt>Codegen.named_values</tt> map keeps track of which values are defined |
| 116 | in the current scope and what their LLVM representation is. (In other words, it |
| 117 | is a symbol table for the code). In this form of Kaleidoscope, the only things |
| 118 | that can be referenced are function parameters. As such, function parameters |
| 119 | will be in this map when generating code for their function body.</p> |
| 120 | |
| 121 | <p> |
| 122 | With these basics in place, we can start talking about how to generate code for |
| 123 | each expression. Note that this assumes that the <tt>Codgen.builder</tt> has |
| 124 | been set up to generate code <em>into</em> something. For now, we'll assume |
| 125 | that this has already been done, and we'll just use it to emit code.</p> |
| 126 | |
| 127 | </div> |
| 128 | |
| 129 | <!-- *********************************************************************** --> |
| 130 | <div class="doc_section"><a name="exprs">Expression Code Generation</a></div> |
| 131 | <!-- *********************************************************************** --> |
| 132 | |
| 133 | <div class="doc_text"> |
| 134 | |
| 135 | <p>Generating LLVM code for expression nodes is very straightforward: less |
| 136 | than 30 lines of commented code for all four of our expression nodes. First |
| 137 | we'll do numeric literals:</p> |
| 138 | |
| 139 | <div class="doc_code"> |
| 140 | <pre> |
| 141 | | Ast.Number n -> const_float double_type n |
| 142 | </pre> |
| 143 | </div> |
| 144 | |
| 145 | <p>In the LLVM IR, numeric constants are represented with the |
| 146 | <tt>ConstantFP</tt> class, which holds the numeric value in an <tt>APFloat</tt> |
| 147 | internally (<tt>APFloat</tt> has the capability of holding floating point |
| 148 | constants of <em>A</em>rbitrary <em>P</em>recision). This code basically just |
| 149 | creates and returns a <tt>ConstantFP</tt>. Note that in the LLVM IR |
| 150 | that constants are all uniqued together and shared. For this reason, the API |
| 151 | uses "the foo::get(..)" idiom instead of "new foo(..)" or "foo::create(..)".</p> |
| 152 | |
| 153 | <div class="doc_code"> |
| 154 | <pre> |
| 155 | | Ast.Variable name -> |
| 156 | (try Hashtbl.find named_values name with |
| 157 | | Not_found -> raise (Error "unknown variable name")) |
| 158 | </pre> |
| 159 | </div> |
| 160 | |
| 161 | <p>References to variables are also quite simple using LLVM. In the simple |
| 162 | version of Kaleidoscope, we assume that the variable has already been emited |
| 163 | somewhere and its value is available. In practice, the only values that can be |
| 164 | in the <tt>Codegen.named_values</tt> map are function arguments. This code |
| 165 | simply checks to see that the specified name is in the map (if not, an unknown |
| 166 | variable is being referenced) and returns the value for it. In future chapters, |
| 167 | we'll add support for <a href="LangImpl5.html#for">loop induction variables</a> |
| 168 | in the symbol table, and for <a href="LangImpl7.html#localvars">local |
| 169 | variables</a>.</p> |
| 170 | |
| 171 | <div class="doc_code"> |
| 172 | <pre> |
| 173 | | Ast.Binary (op, lhs, rhs) -> |
| 174 | let lhs_val = codegen_expr lhs in |
| 175 | let rhs_val = codegen_expr rhs in |
| 176 | begin |
| 177 | match op with |
| 178 | | '+' -> build_add lhs_val rhs_val "addtmp" builder |
| 179 | | '-' -> build_sub lhs_val rhs_val "subtmp" builder |
| 180 | | '*' -> build_mul lhs_val rhs_val "multmp" builder |
| 181 | | '<' -> |
| 182 | (* Convert bool 0/1 to double 0.0 or 1.0 *) |
| 183 | let i = build_fcmp Fcmp.Ult lhs_val rhs_val "cmptmp" builder in |
| 184 | build_uitofp i double_type "booltmp" builder |
| 185 | | _ -> raise (Error "invalid binary operator") |
| 186 | end |
| 187 | </pre> |
| 188 | </div> |
| 189 | |
| 190 | <p>Binary operators start to get more interesting. The basic idea here is that |
| 191 | we recursively emit code for the left-hand side of the expression, then the |
| 192 | right-hand side, then we compute the result of the binary expression. In this |
| 193 | code, we do a simple switch on the opcode to create the right LLVM instruction. |
| 194 | </p> |
| 195 | |
| 196 | <p>In the example above, the LLVM builder class is starting to show its value. |
| 197 | LLVMBuilder knows where to insert the newly created instruction, all you have to |
| 198 | do is specify what instruction to create (e.g. with <tt>Llvm.create_add</tt>), |
| 199 | which operands to use (<tt>lhs</tt> and <tt>rhs</tt> here) and optionally |
| 200 | provide a name for the generated instruction.</p> |
| 201 | |
| 202 | <p>One nice thing about LLVM is that the name is just a hint. For instance, if |
| 203 | the code above emits multiple "addtmp" variables, LLVM will automatically |
| 204 | provide each one with an increasing, unique numeric suffix. Local value names |
| 205 | for instructions are purely optional, but it makes it much easier to read the |
| 206 | IR dumps.</p> |
| 207 | |
| 208 | <p><a href="../LangRef.html#instref">LLVM instructions</a> are constrained by |
| 209 | strict rules: for example, the Left and Right operators of |
| 210 | an <a href="../LangRef.html#i_add">add instruction</a> must have the same |
| 211 | type, and the result type of the add must match the operand types. Because |
| 212 | all values in Kaleidoscope are doubles, this makes for very simple code for add, |
| 213 | sub and mul.</p> |
| 214 | |
| 215 | <p>On the other hand, LLVM specifies that the <a |
| 216 | href="../LangRef.html#i_fcmp">fcmp instruction</a> always returns an 'i1' value |
| 217 | (a one bit integer). The problem with this is that 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 |
| 218 | a <a href="../LangRef.html#i_uitofp">uitofp instruction</a>. This instruction |
| 219 | converts its input integer into a floating point value by treating the input |
| 220 | as an unsigned value. In contrast, if we used the <a |
| 221 | href="../LangRef.html#i_sitofp">sitofp instruction</a>, the Kaleidoscope '<' |
| 222 | operator would return 0.0 and -1.0, depending on the input value.</p> |
| 223 | |
| 224 | <div class="doc_code"> |
| 225 | <pre> |
| 226 | | Ast.Call (callee, args) -> |
| 227 | (* Look up the name in the module table. *) |
| 228 | let callee = |
| 229 | match lookup_function callee the_module with |
| 230 | | Some callee -> callee |
| 231 | | None -> raise (Error "unknown function referenced") |
| 232 | in |
| 233 | let params = params callee in |
| 234 | |
| 235 | (* If argument mismatch error. *) |
| 236 | if Array.length params == Array.length args then () else |
| 237 | raise (Error "incorrect # arguments passed"); |
| 238 | let args = Array.map codegen_expr args in |
| 239 | build_call callee args "calltmp" builder |
| 240 | </pre> |
| 241 | </div> |
| 242 | |
| 243 | <p>Code generation for function calls is quite straightforward with LLVM. The |
| 244 | code above initially does a function name lookup in the LLVM Module's symbol |
| 245 | table. Recall that the LLVM Module is the container that holds all of the |
| 246 | functions we are JIT'ing. By giving each function the same name as what the |
| 247 | user specifies, we can use the LLVM symbol table to resolve function names for |
| 248 | us.</p> |
| 249 | |
| 250 | <p>Once we have the function to call, we recursively codegen each argument that |
| 251 | is to be passed in, and create an LLVM <a href="../LangRef.html#i_call">call |
| 252 | instruction</a>. Note that LLVM uses the native C calling conventions by |
| 253 | default, allowing these calls to also call into standard library functions like |
| 254 | "sin" and "cos", with no additional effort.</p> |
| 255 | |
| 256 | <p>This wraps up our handling of the four basic expressions that we have so far |
| 257 | in Kaleidoscope. Feel free to go in and add some more. For example, by |
| 258 | browsing the <a href="../LangRef.html">LLVM language reference</a> you'll find |
| 259 | several other interesting instructions that are really easy to plug into our |
| 260 | basic framework.</p> |
| 261 | |
| 262 | </div> |
| 263 | |
| 264 | <!-- *********************************************************************** --> |
| 265 | <div class="doc_section"><a name="funcs">Function Code Generation</a></div> |
| 266 | <!-- *********************************************************************** --> |
| 267 | |
| 268 | <div class="doc_text"> |
| 269 | |
| 270 | <p>Code generation for prototypes and functions must handle a number of |
| 271 | details, which make their code less beautiful than expression code |
| 272 | generation, but allows us to illustrate some important points. First, lets |
| 273 | talk about code generation for prototypes: they are used both for function |
| 274 | bodies and external function declarations. The code starts with:</p> |
| 275 | |
| 276 | <div class="doc_code"> |
| 277 | <pre> |
| 278 | let codegen_proto = function |
| 279 | | Ast.Prototype (name, args) -> |
| 280 | (* Make the function type: double(double,double) etc. *) |
| 281 | let doubles = Array.make (Array.length args) double_type in |
| 282 | let ft = function_type double_type doubles in |
| 283 | let f = |
| 284 | match lookup_function name the_module with |
| 285 | </pre> |
| 286 | </div> |
| 287 | |
| 288 | <p>This code packs a lot of power into a few lines. Note first that this |
| 289 | function returns a "Function*" instead of a "Value*" (although at the moment |
| 290 | they both are modeled by <tt>llvalue</tt> in ocaml). Because a "prototype" |
| 291 | really talks about the external interface for a function (not the value computed |
| 292 | by an expression), it makes sense for it to return the LLVM Function it |
| 293 | corresponds to when codegen'd.</p> |
| 294 | |
| 295 | <p>The call to <tt>Llvm.function_type</tt> creates the <tt>Llvm.llvalue</tt> |
| 296 | that should be used for a given Prototype. Since all function arguments in |
| 297 | Kaleidoscope are of type double, the first line creates a vector of "N" LLVM |
| 298 | double types. It then uses the <tt>Llvm.function_type</tt> method to create a |
| 299 | function type that takes "N" doubles as arguments, returns one double as a |
| 300 | result, and that is not vararg (that uses the function |
| 301 | <tt>Llvm.var_arg_function_type</tt>). Note that Types in LLVM are uniqued just |
| 302 | like <tt>Constant</tt>s are, so you don't "new" a type, you "get" it.</p> |
| 303 | |
| 304 | <p>The final line above checks if the function has already been defined in |
| 305 | <tt>Codegen.the_module</tt>. If not, we will create it.</p> |
| 306 | |
| 307 | <div class="doc_code"> |
| 308 | <pre> |
| 309 | | None -> declare_function name ft the_module |
| 310 | </pre> |
| 311 | </div> |
| 312 | |
| 313 | <p>This indicates the type and name to use, as well as which module to insert |
| 314 | into. By default we assume a function has |
| 315 | <tt>Llvm.Linkage.ExternalLinkage</tt>. "<a href="LangRef.html#linkage">external |
| 316 | linkage</a>" means that the function may be defined outside the current module |
| 317 | and/or that it is callable by functions outside the module. The "<tt>name</tt>" |
| 318 | passed in is the name the user specified: this name is registered in |
| 319 | "<tt>Codegen.the_module</tt>"s symbol table, which is used by the function call |
| 320 | code above.</p> |
| 321 | |
| 322 | <p>In Kaleidoscope, I choose to allow redefinitions of functions in two cases: |
| 323 | first, we want to allow 'extern'ing a function more than once, as long as the |
| 324 | prototypes for the externs match (since all arguments have the same type, we |
| 325 | just have to check that the number of arguments match). Second, we want to |
| 326 | allow 'extern'ing a function and then definining a body for it. This is useful |
| 327 | when defining mutually recursive functions.</p> |
| 328 | |
| 329 | <div class="doc_code"> |
| 330 | <pre> |
| 331 | (* If 'f' conflicted, there was already something named 'name'. If it |
| 332 | * has a body, don't allow redefinition or reextern. *) |
| 333 | | Some f -> |
| 334 | (* If 'f' already has a body, reject this. *) |
| 335 | if Array.length (basic_blocks f) == 0 then () else |
| 336 | raise (Error "redefinition of function"); |
| 337 | |
| 338 | (* If 'f' took a different number of arguments, reject. *) |
| 339 | if Array.length (params f) == Array.length args then () else |
| 340 | raise (Error "redefinition of function with different # args"); |
| 341 | f |
| 342 | in |
| 343 | </pre> |
| 344 | </div> |
| 345 | |
| 346 | <p>In order to verify the logic above, we first check to see if the pre-existing |
| 347 | function is "empty". In this case, empty means that it has no basic blocks in |
| 348 | it, which means it has no body. If it has no body, it is a forward |
| 349 | declaration. Since we don't allow anything after a full definition of the |
| 350 | function, the code rejects this case. If the previous reference to a function |
| 351 | was an 'extern', we simply verify that the number of arguments for that |
| 352 | definition and this one match up. If not, we emit an error.</p> |
| 353 | |
| 354 | <div class="doc_code"> |
| 355 | <pre> |
| 356 | (* Set names for all arguments. *) |
| 357 | Array.iteri (fun i a -> |
| 358 | let n = args.(i) in |
| 359 | set_value_name n a; |
| 360 | Hashtbl.add named_values n a; |
| 361 | ) (params f); |
| 362 | f |
| 363 | </pre> |
| 364 | </div> |
| 365 | |
| 366 | <p>The last bit of code for prototypes loops over all of the arguments in the |
| 367 | function, setting the name of the LLVM Argument objects to match, and registering |
| 368 | the arguments in the <tt>Codegen.named_values</tt> map for future use by the |
| 369 | <tt>Ast.Variable</tt> variant. Once this is set up, it returns the Function |
| 370 | object to the caller. Note that we don't check for conflicting |
| 371 | argument names here (e.g. "extern foo(a b a)"). Doing so would be very |
| 372 | straight-forward with the mechanics we have already used above.</p> |
| 373 | |
| 374 | <div class="doc_code"> |
| 375 | <pre> |
| 376 | let codegen_func = function |
| 377 | | Ast.Function (proto, body) -> |
| 378 | Hashtbl.clear named_values; |
| 379 | let the_function = codegen_proto proto in |
| 380 | </pre> |
| 381 | </div> |
| 382 | |
| 383 | <p>Code generation for function definitions starts out simply enough: we just |
| 384 | codegen the prototype (Proto) and verify that it is ok. We then clear out the |
| 385 | <tt>Codegen.named_values</tt> map to make sure that there isn't anything in it |
| 386 | from the last function we compiled. Code generation of the prototype ensures |
| 387 | that there is an LLVM Function object that is ready to go for us.</p> |
| 388 | |
| 389 | <div class="doc_code"> |
| 390 | <pre> |
| 391 | (* Create a new basic block to start insertion into. *) |
| 392 | let bb = append_block "entry" the_function in |
| 393 | position_at_end bb builder; |
| 394 | |
| 395 | try |
| 396 | let ret_val = codegen_expr body in |
| 397 | </pre> |
| 398 | </div> |
| 399 | |
| 400 | <p>Now we get to the point where the <tt>Codegen.builder</tt> is set up. The |
| 401 | first line creates a new |
| 402 | <a href="http://en.wikipedia.org/wiki/Basic_block">basic block</a> (named |
| 403 | "entry"), which is inserted into <tt>the_function</tt>. The second line then |
| 404 | tells the builder that new instructions should be inserted into the end of the |
| 405 | new basic block. Basic blocks in LLVM are an important part of functions that |
| 406 | define the <a |
| 407 | href="http://en.wikipedia.org/wiki/Control_flow_graph">Control Flow Graph</a>. |
| 408 | Since we don't have any control flow, our functions will only contain one |
| 409 | block at this point. We'll fix this in <a href="OCamlLangImpl5.html">Chapter |
| 410 | 5</a> :).</p> |
| 411 | |
| 412 | <div class="doc_code"> |
| 413 | <pre> |
| 414 | let ret_val = codegen_expr body in |
| 415 | |
| 416 | (* Finish off the function. *) |
| 417 | let _ = build_ret ret_val builder in |
| 418 | |
| 419 | (* Validate the generated code, checking for consistency. *) |
| 420 | Llvm_analysis.assert_valid_function the_function; |
| 421 | |
| 422 | the_function |
| 423 | </pre> |
| 424 | </div> |
| 425 | |
| 426 | <p>Once the insertion point is set up, we call the <tt>Codegen.codegen_func</tt> |
| 427 | method for the root expression of the function. If no error happens, this emits |
| 428 | code to compute the expression into the entry block and returns the value that |
| 429 | was computed. Assuming no error, we then create an LLVM <a |
| 430 | href="../LangRef.html#i_ret">ret instruction</a>, which completes the function. |
| 431 | Once the function is built, we call |
| 432 | <tt>Llvm_analysis.assert_valid_function</tt>, which is provided by LLVM. This |
| 433 | function does a variety of consistency checks on the generated code, to |
| 434 | determine if our compiler is doing everything right. Using this is important: |
| 435 | it can catch a lot of bugs. Once the function is finished and validated, we |
| 436 | return it.</p> |
| 437 | |
| 438 | <div class="doc_code"> |
| 439 | <pre> |
| 440 | with e -> |
| 441 | delete_function the_function; |
| 442 | raise e |
| 443 | </pre> |
| 444 | </div> |
| 445 | |
| 446 | <p>The only piece left here is handling of the error case. For simplicity, we |
| 447 | handle this by merely deleting the function we produced with the |
| 448 | <tt>Llvm.delete_function</tt> method. This allows the user to redefine a |
| 449 | function that they incorrectly typed in before: if we didn't delete it, it |
| 450 | would live in the symbol table, with a body, preventing future redefinition.</p> |
| 451 | |
| 452 | <p>This code does have a bug, though. Since the <tt>Codegen.codegen_proto</tt> |
| 453 | can return a previously defined forward declaration, our code can actually delete |
| 454 | a forward declaration. There are a number of ways to fix this bug, see what you |
| 455 | can come up with! Here is a testcase:</p> |
| 456 | |
| 457 | <div class="doc_code"> |
| 458 | <pre> |
| 459 | extern foo(a b); # ok, defines foo. |
| 460 | def foo(a b) c; # error, 'c' is invalid. |
| 461 | def bar() foo(1, 2); # error, unknown function "foo" |
| 462 | </pre> |
| 463 | </div> |
| 464 | |
| 465 | </div> |
| 466 | |
| 467 | <!-- *********************************************************************** --> |
| 468 | <div class="doc_section"><a name="driver">Driver Changes and |
| 469 | Closing Thoughts</a></div> |
| 470 | <!-- *********************************************************************** --> |
| 471 | |
| 472 | <div class="doc_text"> |
| 473 | |
| 474 | <p> |
| 475 | For now, code generation to LLVM doesn't really get us much, except that we can |
| 476 | look at the pretty IR calls. The sample code inserts calls to Codegen into the |
| 477 | "<tt>Toplevel.main_loop</tt>", and then dumps out the LLVM IR. This gives a |
| 478 | nice way to look at the LLVM IR for simple functions. For example: |
| 479 | </p> |
| 480 | |
| 481 | <div class="doc_code"> |
| 482 | <pre> |
| 483 | ready> <b>4+5</b>; |
| 484 | Read top-level expression: |
| 485 | define double @""() { |
| 486 | entry: |
| 487 | %addtmp = add double 4.000000e+00, 5.000000e+00 |
| 488 | ret double %addtmp |
| 489 | } |
| 490 | </pre> |
| 491 | </div> |
| 492 | |
| 493 | <p>Note how the parser turns the top-level expression into anonymous functions |
| 494 | for us. This will be handy when we add <a href="LangImpl4.html#jit">JIT |
| 495 | support</a> in the next chapter. Also note that the code is very literally |
| 496 | transcribed, no optimizations are being performed. We will |
| 497 | <a href="OCamlLangImpl4.html#trivialconstfold">add optimizations</a> explicitly |
| 498 | in the next chapter.</p> |
| 499 | |
| 500 | <div class="doc_code"> |
| 501 | <pre> |
| 502 | ready> <b>def foo(a b) a*a + 2*a*b + b*b;</b> |
| 503 | Read function definition: |
| 504 | define double @foo(double %a, double %b) { |
| 505 | entry: |
| 506 | %multmp = mul double %a, %a |
| 507 | %multmp1 = mul double 2.000000e+00, %a |
| 508 | %multmp2 = mul double %multmp1, %b |
| 509 | %addtmp = add double %multmp, %multmp2 |
| 510 | %multmp3 = mul double %b, %b |
| 511 | %addtmp4 = add double %addtmp, %multmp3 |
| 512 | ret double %addtmp4 |
| 513 | } |
| 514 | </pre> |
| 515 | </div> |
| 516 | |
| 517 | <p>This shows some simple arithmetic. Notice the striking similarity to the |
| 518 | LLVM builder calls that we use to create the instructions.</p> |
| 519 | |
| 520 | <div class="doc_code"> |
| 521 | <pre> |
| 522 | ready> <b>def bar(a) foo(a, 4.0) + bar(31337);</b> |
| 523 | Read function definition: |
| 524 | define double @bar(double %a) { |
| 525 | entry: |
| 526 | %calltmp = call double @foo( double %a, double 4.000000e+00 ) |
| 527 | %calltmp1 = call double @bar( double 3.133700e+04 ) |
| 528 | %addtmp = add double %calltmp, %calltmp1 |
| 529 | ret double %addtmp |
| 530 | } |
| 531 | </pre> |
| 532 | </div> |
| 533 | |
| 534 | <p>This shows some function calls. Note that this function will take a long |
| 535 | time to execute if you call it. In the future we'll add conditional control |
| 536 | flow to actually make recursion useful :).</p> |
| 537 | |
| 538 | <div class="doc_code"> |
| 539 | <pre> |
| 540 | ready> <b>extern cos(x);</b> |
| 541 | Read extern: |
| 542 | declare double @cos(double) |
| 543 | |
| 544 | ready> <b>cos(1.234);</b> |
| 545 | Read top-level expression: |
| 546 | define double @""() { |
| 547 | entry: |
| 548 | %calltmp = call double @cos( double 1.234000e+00 ) |
| 549 | ret double %calltmp |
| 550 | } |
| 551 | </pre> |
| 552 | </div> |
| 553 | |
| 554 | <p>This shows an extern for the libm "cos" function, and a call to it.</p> |
| 555 | |
| 556 | |
| 557 | <div class="doc_code"> |
| 558 | <pre> |
| 559 | ready> <b>^D</b> |
| 560 | ; ModuleID = 'my cool jit' |
| 561 | |
| 562 | define double @""() { |
| 563 | entry: |
| 564 | %addtmp = add double 4.000000e+00, 5.000000e+00 |
| 565 | ret double %addtmp |
| 566 | } |
| 567 | |
| 568 | define double @foo(double %a, double %b) { |
| 569 | entry: |
| 570 | %multmp = mul double %a, %a |
| 571 | %multmp1 = mul double 2.000000e+00, %a |
| 572 | %multmp2 = mul double %multmp1, %b |
| 573 | %addtmp = add double %multmp, %multmp2 |
| 574 | %multmp3 = mul double %b, %b |
| 575 | %addtmp4 = add double %addtmp, %multmp3 |
| 576 | ret double %addtmp4 |
| 577 | } |
| 578 | |
| 579 | define double @bar(double %a) { |
| 580 | entry: |
| 581 | %calltmp = call double @foo( double %a, double 4.000000e+00 ) |
| 582 | %calltmp1 = call double @bar( double 3.133700e+04 ) |
| 583 | %addtmp = add double %calltmp, %calltmp1 |
| 584 | ret double %addtmp |
| 585 | } |
| 586 | |
| 587 | declare double @cos(double) |
| 588 | |
| 589 | define double @""() { |
| 590 | entry: |
| 591 | %calltmp = call double @cos( double 1.234000e+00 ) |
| 592 | ret double %calltmp |
| 593 | } |
| 594 | </pre> |
| 595 | </div> |
| 596 | |
| 597 | <p>When you quit the current demo, it dumps out the IR for the entire module |
| 598 | generated. Here you can see the big picture with all the functions referencing |
| 599 | each other.</p> |
| 600 | |
| 601 | <p>This wraps up the third chapter of the Kaleidoscope tutorial. Up next, we'll |
| 602 | describe how to <a href="LangImpl4.html">add JIT codegen and optimizer |
| 603 | support</a> to this so we can actually start running code!</p> |
| 604 | |
| 605 | </div> |
| 606 | |
| 607 | |
| 608 | <!-- *********************************************************************** --> |
| 609 | <div class="doc_section"><a name="code">Full Code Listing</a></div> |
| 610 | <!-- *********************************************************************** --> |
| 611 | |
| 612 | <div class="doc_text"> |
| 613 | |
| 614 | <p> |
| 615 | Here is the complete code listing for our running example, enhanced with the |
| 616 | LLVM code generator. Because this uses the LLVM libraries, we need to link |
| 617 | them in. To do this, we use the <a |
| 618 | href="http://llvm.org/cmds/llvm-config.html">llvm-config</a> tool to inform |
| 619 | our makefile/command line about which options to use:</p> |
| 620 | |
| 621 | <div class="doc_code"> |
| 622 | <pre> |
| 623 | # Compile |
| 624 | ocamlbuild toy.byte |
| 625 | # Run |
| 626 | ./toy.byte |
| 627 | </pre> |
| 628 | </div> |
| 629 | |
| 630 | <p>Here is the code:</p> |
| 631 | |
| 632 | <dl> |
| 633 | <dt>_tags:</dt> |
| 634 | <dd class="doc_code"> |
| 635 | <pre> |
| 636 | <{lexer,parser}.ml>: use_camlp4, pp(camlp4of) |
| 637 | <*.{byte,native}>: g++, use_llvm, use_llvm_analysis |
| 638 | </pre> |
| 639 | </dd> |
| 640 | |
| 641 | <dt>myocamlbuild.ml:</dt> |
| 642 | <dd class="doc_code"> |
| 643 | <pre> |
| 644 | open Ocamlbuild_plugin;; |
| 645 | |
| 646 | ocaml_lib ~extern:true "llvm";; |
| 647 | ocaml_lib ~extern:true "llvm_analysis";; |
| 648 | |
| 649 | flag ["link"; "ocaml"; "g++"] (S[A"-cc"; A"g++"]);; |
| 650 | </pre> |
| 651 | </dd> |
| 652 | |
| 653 | <dt>token.ml:</dt> |
| 654 | <dd class="doc_code"> |
| 655 | <pre> |
| 656 | (*===----------------------------------------------------------------------=== |
| 657 | * Lexer Tokens |
| 658 | *===----------------------------------------------------------------------===*) |
| 659 | |
| 660 | (* The lexer returns these 'Kwd' if it is an unknown character, otherwise one of |
| 661 | * these others for known things. *) |
| 662 | type token = |
| 663 | (* commands *) |
| 664 | | Def | Extern |
| 665 | |
| 666 | (* primary *) |
| 667 | | Ident of string | Number of float |
| 668 | |
| 669 | (* unknown *) |
| 670 | | Kwd of char |
| 671 | </pre> |
| 672 | </dd> |
| 673 | |
| 674 | <dt>lexer.ml:</dt> |
| 675 | <dd class="doc_code"> |
| 676 | <pre> |
| 677 | (*===----------------------------------------------------------------------=== |
| 678 | * Lexer |
| 679 | *===----------------------------------------------------------------------===*) |
| 680 | |
| 681 | let rec lex = parser |
| 682 | (* Skip any whitespace. *) |
| 683 | | [< ' (' ' | '\n' | '\r' | '\t'); stream >] -> lex stream |
| 684 | |
| 685 | (* identifier: [a-zA-Z][a-zA-Z0-9] *) |
| 686 | | [< ' ('A' .. 'Z' | 'a' .. 'z' as c); stream >] -> |
| 687 | let buffer = Buffer.create 1 in |
| 688 | Buffer.add_char buffer c; |
| 689 | lex_ident buffer stream |
| 690 | |
| 691 | (* number: [0-9.]+ *) |
| 692 | | [< ' ('0' .. '9' as c); stream >] -> |
| 693 | let buffer = Buffer.create 1 in |
| 694 | Buffer.add_char buffer c; |
| 695 | lex_number buffer stream |
| 696 | |
| 697 | (* Comment until end of line. *) |
| 698 | | [< ' ('#'); stream >] -> |
| 699 | lex_comment stream |
| 700 | |
| 701 | (* Otherwise, just return the character as its ascii value. *) |
| 702 | | [< 'c; stream >] -> |
| 703 | [< 'Token.Kwd c; lex stream >] |
| 704 | |
| 705 | (* end of stream. *) |
| 706 | | [< >] -> [< >] |
| 707 | |
| 708 | and lex_number buffer = parser |
| 709 | | [< ' ('0' .. '9' | '.' as c); stream >] -> |
| 710 | Buffer.add_char buffer c; |
| 711 | lex_number buffer stream |
| 712 | | [< stream=lex >] -> |
| 713 | [< 'Token.Number (float_of_string (Buffer.contents buffer)); stream >] |
| 714 | |
| 715 | and lex_ident buffer = parser |
| 716 | | [< ' ('A' .. 'Z' | 'a' .. 'z' | '0' .. '9' as c); stream >] -> |
| 717 | Buffer.add_char buffer c; |
| 718 | lex_ident buffer stream |
| 719 | | [< stream=lex >] -> |
| 720 | match Buffer.contents buffer with |
| 721 | | "def" -> [< 'Token.Def; stream >] |
| 722 | | "extern" -> [< 'Token.Extern; stream >] |
| 723 | | id -> [< 'Token.Ident id; stream >] |
| 724 | |
| 725 | and lex_comment = parser |
| 726 | | [< ' ('\n'); stream=lex >] -> stream |
| 727 | | [< 'c; e=lex_comment >] -> e |
| 728 | | [< >] -> [< >] |
| 729 | </pre> |
| 730 | </dd> |
| 731 | |
| 732 | <dt>ast.ml:</dt> |
| 733 | <dd class="doc_code"> |
| 734 | <pre> |
| 735 | (*===----------------------------------------------------------------------=== |
| 736 | * Abstract Syntax Tree (aka Parse Tree) |
| 737 | *===----------------------------------------------------------------------===*) |
| 738 | |
| 739 | (* expr - Base type for all expression nodes. *) |
| 740 | type expr = |
| 741 | (* variant for numeric literals like "1.0". *) |
| 742 | | Number of float |
| 743 | |
| 744 | (* variant for referencing a variable, like "a". *) |
| 745 | | Variable of string |
| 746 | |
| 747 | (* variant for a binary operator. *) |
| 748 | | Binary of char * expr * expr |
| 749 | |
| 750 | (* variant for function calls. *) |
| 751 | | Call of string * expr array |
| 752 | |
| 753 | (* proto - This type represents the "prototype" for a function, which captures |
| 754 | * its name, and its argument names (thus implicitly the number of arguments the |
| 755 | * function takes). *) |
| 756 | type proto = Prototype of string * string array |
| 757 | |
| 758 | (* func - This type represents a function definition itself. *) |
| 759 | type func = Function of proto * expr |
| 760 | </pre> |
| 761 | </dd> |
| 762 | |
| 763 | <dt>parser.ml:</dt> |
| 764 | <dd class="doc_code"> |
| 765 | <pre> |
| 766 | (*===---------------------------------------------------------------------=== |
| 767 | * Parser |
| 768 | *===---------------------------------------------------------------------===*) |
| 769 | |
| 770 | (* binop_precedence - This holds the precedence for each binary operator that is |
| 771 | * defined *) |
| 772 | let binop_precedence:(char, int) Hashtbl.t = Hashtbl.create 10 |
| 773 | |
| 774 | (* precedence - Get the precedence of the pending binary operator token. *) |
| 775 | let precedence c = try Hashtbl.find binop_precedence c with Not_found -> -1 |
| 776 | |
| 777 | (* primary |
| 778 | * ::= identifier |
| 779 | * ::= numberexpr |
| 780 | * ::= parenexpr *) |
| 781 | let rec parse_primary = parser |
| 782 | (* numberexpr ::= number *) |
| 783 | | [< 'Token.Number n >] -> Ast.Number n |
| 784 | |
| 785 | (* parenexpr ::= '(' expression ')' *) |
| 786 | | [< 'Token.Kwd '('; e=parse_expr; 'Token.Kwd ')' ?? "expected ')'" >] -> e |
| 787 | |
| 788 | (* identifierexpr |
| 789 | * ::= identifier |
| 790 | * ::= identifier '(' argumentexpr ')' *) |
| 791 | | [< 'Token.Ident id; stream >] -> |
| 792 | let rec parse_args accumulator = parser |
| 793 | | [< e=parse_expr; stream >] -> |
| 794 | begin parser |
| 795 | | [< 'Token.Kwd ','; e=parse_args (e :: accumulator) >] -> e |
| 796 | | [< >] -> e :: accumulator |
| 797 | end stream |
| 798 | | [< >] -> accumulator |
| 799 | in |
| 800 | let rec parse_ident id = parser |
| 801 | (* Call. *) |
| 802 | | [< 'Token.Kwd '('; |
| 803 | args=parse_args []; |
| 804 | 'Token.Kwd ')' ?? "expected ')'">] -> |
| 805 | Ast.Call (id, Array.of_list (List.rev args)) |
| 806 | |
| 807 | (* Simple variable ref. *) |
| 808 | | [< >] -> Ast.Variable id |
| 809 | in |
| 810 | parse_ident id stream |
| 811 | |
| 812 | | [< >] -> raise (Stream.Error "unknown token when expecting an expression.") |
| 813 | |
| 814 | (* binoprhs |
| 815 | * ::= ('+' primary)* *) |
| 816 | and parse_bin_rhs expr_prec lhs stream = |
| 817 | match Stream.peek stream with |
| 818 | (* If this is a binop, find its precedence. *) |
| 819 | | Some (Token.Kwd c) when Hashtbl.mem binop_precedence c -> |
| 820 | let token_prec = precedence c in |
| 821 | |
| 822 | (* If this is a binop that binds at least as tightly as the current binop, |
| 823 | * consume it, otherwise we are done. *) |
| 824 | if token_prec < expr_prec then lhs else begin |
| 825 | (* Eat the binop. *) |
| 826 | Stream.junk stream; |
| 827 | |
| 828 | (* Parse the primary expression after the binary operator. *) |
| 829 | let rhs = parse_primary stream in |
| 830 | |
| 831 | (* Okay, we know this is a binop. *) |
| 832 | let rhs = |
| 833 | match Stream.peek stream with |
| 834 | | Some (Token.Kwd c2) -> |
| 835 | (* If BinOp binds less tightly with rhs than the operator after |
| 836 | * rhs, let the pending operator take rhs as its lhs. *) |
| 837 | let next_prec = precedence c2 in |
| 838 | if token_prec < next_prec |
| 839 | then parse_bin_rhs (token_prec + 1) rhs stream |
| 840 | else rhs |
| 841 | | _ -> rhs |
| 842 | in |
| 843 | |
| 844 | (* Merge lhs/rhs. *) |
| 845 | let lhs = Ast.Binary (c, lhs, rhs) in |
| 846 | parse_bin_rhs expr_prec lhs stream |
| 847 | end |
| 848 | | _ -> lhs |
| 849 | |
| 850 | (* expression |
| 851 | * ::= primary binoprhs *) |
| 852 | and parse_expr = parser |
| 853 | | [< lhs=parse_primary; stream >] -> parse_bin_rhs 0 lhs stream |
| 854 | |
| 855 | (* prototype |
| 856 | * ::= id '(' id* ')' *) |
| 857 | let parse_prototype = |
| 858 | let rec parse_args accumulator = parser |
| 859 | | [< 'Token.Ident id; e=parse_args (id::accumulator) >] -> e |
| 860 | | [< >] -> accumulator |
| 861 | in |
| 862 | |
| 863 | parser |
| 864 | | [< 'Token.Ident id; |
| 865 | 'Token.Kwd '(' ?? "expected '(' in prototype"; |
| 866 | args=parse_args []; |
| 867 | 'Token.Kwd ')' ?? "expected ')' in prototype" >] -> |
| 868 | (* success. *) |
| 869 | Ast.Prototype (id, Array.of_list (List.rev args)) |
| 870 | |
| 871 | | [< >] -> |
| 872 | raise (Stream.Error "expected function name in prototype") |
| 873 | |
| 874 | (* definition ::= 'def' prototype expression *) |
| 875 | let parse_definition = parser |
| 876 | | [< 'Token.Def; p=parse_prototype; e=parse_expr >] -> |
| 877 | Ast.Function (p, e) |
| 878 | |
| 879 | (* toplevelexpr ::= expression *) |
| 880 | let parse_toplevel = parser |
| 881 | | [< e=parse_expr >] -> |
| 882 | (* Make an anonymous proto. *) |
| 883 | Ast.Function (Ast.Prototype ("", [||]), e) |
| 884 | |
| 885 | (* external ::= 'extern' prototype *) |
| 886 | let parse_extern = parser |
| 887 | | [< 'Token.Extern; e=parse_prototype >] -> e |
| 888 | </pre> |
| 889 | </dd> |
| 890 | |
| 891 | <dt>codegen.ml:</dt> |
| 892 | <dd class="doc_code"> |
| 893 | <pre> |
| 894 | (*===----------------------------------------------------------------------=== |
| 895 | * Code Generation |
| 896 | *===----------------------------------------------------------------------===*) |
| 897 | |
| 898 | open Llvm |
| 899 | |
| 900 | exception Error of string |
| 901 | |
| 902 | let the_module = create_module "my cool jit" |
| 903 | let builder = builder () |
| 904 | let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10 |
| 905 | |
| 906 | let rec codegen_expr = function |
| 907 | | Ast.Number n -> const_float double_type n |
| 908 | | Ast.Variable name -> |
| 909 | (try Hashtbl.find named_values name with |
| 910 | | Not_found -> raise (Error "unknown variable name")) |
| 911 | | Ast.Binary (op, lhs, rhs) -> |
| 912 | let lhs_val = codegen_expr lhs in |
| 913 | let rhs_val = codegen_expr rhs in |
| 914 | begin |
| 915 | match op with |
| 916 | | '+' -> build_add lhs_val rhs_val "addtmp" builder |
| 917 | | '-' -> build_sub lhs_val rhs_val "subtmp" builder |
| 918 | | '*' -> build_mul lhs_val rhs_val "multmp" builder |
| 919 | | '<' -> |
| 920 | (* Convert bool 0/1 to double 0.0 or 1.0 *) |
| 921 | let i = build_fcmp Fcmp.Ult lhs_val rhs_val "cmptmp" builder in |
| 922 | build_uitofp i double_type "booltmp" builder |
| 923 | | _ -> raise (Error "invalid binary operator") |
| 924 | end |
| 925 | | Ast.Call (callee, args) -> |
| 926 | (* Look up the name in the module table. *) |
| 927 | let callee = |
| 928 | match lookup_function callee the_module with |
| 929 | | Some callee -> callee |
| 930 | | None -> raise (Error "unknown function referenced") |
| 931 | in |
| 932 | let params = params callee in |
| 933 | |
| 934 | (* If argument mismatch error. *) |
| 935 | if Array.length params == Array.length args then () else |
| 936 | raise (Error "incorrect # arguments passed"); |
| 937 | let args = Array.map codegen_expr args in |
| 938 | build_call callee args "calltmp" builder |
| 939 | |
| 940 | let codegen_proto = function |
| 941 | | Ast.Prototype (name, args) -> |
| 942 | (* Make the function type: double(double,double) etc. *) |
| 943 | let doubles = Array.make (Array.length args) double_type in |
| 944 | let ft = function_type double_type doubles in |
| 945 | let f = |
| 946 | match lookup_function name the_module with |
| 947 | | None -> declare_function name ft the_module |
| 948 | |
| 949 | (* If 'f' conflicted, there was already something named 'name'. If it |
| 950 | * has a body, don't allow redefinition or reextern. *) |
| 951 | | Some f -> |
| 952 | (* If 'f' already has a body, reject this. *) |
| 953 | if block_begin f <> At_end f then |
| 954 | raise (Error "redefinition of function"); |
| 955 | |
| 956 | (* If 'f' took a different number of arguments, reject. *) |
| 957 | if element_type (type_of f) <> ft then |
| 958 | raise (Error "redefinition of function with different # args"); |
| 959 | f |
| 960 | in |
| 961 | |
| 962 | (* Set names for all arguments. *) |
| 963 | Array.iteri (fun i a -> |
| 964 | let n = args.(i) in |
| 965 | set_value_name n a; |
| 966 | Hashtbl.add named_values n a; |
| 967 | ) (params f); |
| 968 | f |
| 969 | |
| 970 | let codegen_func = function |
| 971 | | Ast.Function (proto, body) -> |
| 972 | Hashtbl.clear named_values; |
| 973 | let the_function = codegen_proto proto in |
| 974 | |
| 975 | (* Create a new basic block to start insertion into. *) |
| 976 | let bb = append_block "entry" the_function in |
| 977 | position_at_end bb builder; |
| 978 | |
| 979 | try |
| 980 | let ret_val = codegen_expr body in |
| 981 | |
| 982 | (* Finish off the function. *) |
| 983 | let _ = build_ret ret_val builder in |
| 984 | |
| 985 | (* Validate the generated code, checking for consistency. *) |
| 986 | Llvm_analysis.assert_valid_function the_function; |
| 987 | |
| 988 | the_function |
| 989 | with e -> |
| 990 | delete_function the_function; |
| 991 | raise e |
| 992 | </pre> |
| 993 | </dd> |
| 994 | |
| 995 | <dt>toplevel.ml:</dt> |
| 996 | <dd class="doc_code"> |
| 997 | <pre> |
| 998 | (*===----------------------------------------------------------------------=== |
| 999 | * Top-Level parsing and JIT Driver |
| 1000 | *===----------------------------------------------------------------------===*) |
| 1001 | |
| 1002 | open Llvm |
| 1003 | |
| 1004 | (* top ::= definition | external | expression | ';' *) |
| 1005 | let rec main_loop stream = |
| 1006 | match Stream.peek stream with |
| 1007 | | None -> () |
| 1008 | |
| 1009 | (* ignore top-level semicolons. *) |
| 1010 | | Some (Token.Kwd ';') -> |
| 1011 | Stream.junk stream; |
| 1012 | main_loop stream |
| 1013 | |
| 1014 | | Some token -> |
| 1015 | begin |
| 1016 | try match token with |
| 1017 | | Token.Def -> |
| 1018 | let e = Parser.parse_definition stream in |
| 1019 | print_endline "parsed a function definition."; |
| 1020 | dump_value (Codegen.codegen_func e); |
| 1021 | | Token.Extern -> |
| 1022 | let e = Parser.parse_extern stream in |
| 1023 | print_endline "parsed an extern."; |
| 1024 | dump_value (Codegen.codegen_proto e); |
| 1025 | | _ -> |
| 1026 | (* Evaluate a top-level expression into an anonymous function. *) |
| 1027 | let e = Parser.parse_toplevel stream in |
| 1028 | print_endline "parsed a top-level expr"; |
| 1029 | dump_value (Codegen.codegen_func e); |
| 1030 | with Stream.Error s | Codegen.Error s -> |
| 1031 | (* Skip token for error recovery. *) |
| 1032 | Stream.junk stream; |
| 1033 | print_endline s; |
| 1034 | end; |
| 1035 | print_string "ready> "; flush stdout; |
| 1036 | main_loop stream |
| 1037 | </pre> |
| 1038 | </dd> |
| 1039 | |
| 1040 | <dt>toy.ml:</dt> |
| 1041 | <dd class="doc_code"> |
| 1042 | <pre> |
| 1043 | (*===----------------------------------------------------------------------=== |
| 1044 | * Main driver code. |
| 1045 | *===----------------------------------------------------------------------===*) |
| 1046 | |
| 1047 | open Llvm |
| 1048 | |
| 1049 | let main () = |
| 1050 | (* Install standard binary operators. |
| 1051 | * 1 is the lowest precedence. *) |
| 1052 | Hashtbl.add Parser.binop_precedence '<' 10; |
| 1053 | Hashtbl.add Parser.binop_precedence '+' 20; |
| 1054 | Hashtbl.add Parser.binop_precedence '-' 20; |
| 1055 | Hashtbl.add Parser.binop_precedence '*' 40; (* highest. *) |
| 1056 | |
| 1057 | (* Prime the first token. *) |
| 1058 | print_string "ready> "; flush stdout; |
| 1059 | let stream = Lexer.lex (Stream.of_channel stdin) in |
| 1060 | |
| 1061 | (* Run the main "interpreter loop" now. *) |
| 1062 | Toplevel.main_loop stream; |
| 1063 | |
| 1064 | (* Print out all the generated code. *) |
| 1065 | dump_module Codegen.the_module |
| 1066 | ;; |
| 1067 | |
| 1068 | main () |
| 1069 | </pre> |
| 1070 | </dd> |
| 1071 | </dl> |
| 1072 | |
| 1073 | <a href="OCamlLangImpl4.html">Next: Adding JIT and Optimizer Support</a> |
| 1074 | </div> |
| 1075 | |
| 1076 | <!-- *********************************************************************** --> |
| 1077 | <hr> |
| 1078 | <address> |
| 1079 | <a href="http://jigsaw.w3.org/css-validator/check/referer"><img |
| 1080 | src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a> |
| 1081 | <a href="http://validator.w3.org/check/referer"><img |
| 1082 | src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a> |
| 1083 | |
| 1084 | <a href="mailto:sabre@nondot.org">Chris Lattner</a><br> |
| 1085 | <a href="mailto:idadesub@users.sourceforge.net">Erick Tryzelaar</a><br> |
| 1086 | <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br> |
| 1087 | Last modified: $Date: 2007-10-17 11:05:13 -0700 (Wed, 17 Oct 2007) $ |
| 1088 | </address> |
| 1089 | </body> |
| 1090 | </html> |