index.rst 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. =============================================
  2. My First Language Frontend with LLVM Tutorial
  3. =============================================
  4. **Requirements:** This tutorial assumes you know C++, but no previous
  5. compiler experience is necessary.
  6. Welcome to the "My First Language Frontend with LLVM" tutorial. Here we
  7. run through the implementation of a simple language, showing
  8. how fun and easy it can be. This tutorial will get you up and running
  9. fast and show a concrete example of something that uses LLVM to generate
  10. code.
  11. This tutorial introduces the simple "Kaleidoscope" language, building it
  12. iteratively over the course of several chapters, showing how it is built
  13. over time. This lets us cover a range of language design and LLVM-specific
  14. ideas, showing and explaining the code for it all along the way,
  15. and reduces the overwhelming amount of details up front. We strongly
  16. encourage that you *work with this code* - make a copy and hack it up and
  17. experiment.
  18. **Warning**: In order to focus on teaching compiler techniques and LLVM
  19. specifically,
  20. this tutorial does *not* show best practices in software engineering
  21. principles. For example, the code uses global variables
  22. pervasively, doesn't use
  23. `visitors <http://en.wikipedia.org/wiki/Visitor_pattern>`_, etc... but
  24. instead keeps things simple and focuses on the topics at hand.
  25. This tutorial is structured into chapters covering individual topics,
  26. allowing you to skip ahead as you wish:
  27. - `Chapter #1: Kaleidoscope language and Lexer <LangImpl01.html>`_ -
  28. This shows where we are
  29. going and the basic functionality that we want to build. A lexer
  30. is also the first part of building a parser for a language, and we
  31. use a simple C++ lexer which is easy to understand.
  32. - `Chapter #2: Implementing a Parser and AST <LangImpl02.html>`_ -
  33. With the lexer in place, we can talk about parsing techniques and
  34. basic AST construction. This tutorial describes recursive descent
  35. parsing and operator precedence parsing.
  36. - `Chapter #3: Code generation to LLVM IR <LangImpl03.html>`_ - with
  37. the AST ready, we show how easy it is to generate LLVM IR, and show
  38. a simple way to incorporate LLVM into your project.
  39. - `Chapter #4: Adding JIT and Optimizer Support <LangImpl04.html>`_ -
  40. One great thing about LLVM is its support for JIT compilation, so
  41. we'll dive right into it and show you the 3 lines it takes to add JIT
  42. support. Later chapters show how to generate .o files.
  43. - `Chapter #5: Extending the Language: Control Flow <LangImpl05.html>`_ - With the basic language up and running, we show how to extend
  44. it with control flow operations ('if' statement and a 'for' loop). This
  45. gives us a chance to talk about SSA construction and control
  46. flow.
  47. - `Chapter #6: Extending the Language: User-defined Operators
  48. <LangImpl06.html>`_ - This chapter extends the language to let
  49. users define arbitrary unary and binary operators - with assignable
  50. precedence! This allows us to build a significant piece of the
  51. "language" as library routines.
  52. - `Chapter #7: Extending the Language: Mutable Variables
  53. <LangImpl07.html>`_ - This chapter talks about adding user-defined local
  54. variables along with an assignment operator. This shows how easy it is
  55. to construct SSA form in LLVM: LLVM does *not* require your front-end
  56. to construct SSA form in order to use it!
  57. - `Chapter #8: Compiling to Object Files <LangImpl08.html>`_ - This
  58. chapter explains how to take LLVM IR and compile it down to object
  59. files, like a static compiler does.
  60. - `Chapter #9: Debug Information <LangImpl09.html>`_ - A real language
  61. needs to support debuggers, so we
  62. add debug information that allows setting breakpoints in Kaleidoscope
  63. functions, print out argument variables, and call functions!
  64. - `Chapter #10: Conclusion and other tidbits <LangImpl10.html>`_ - This
  65. chapter wraps up the series by discussing ways to extend the language
  66. and includes pointers to info on "special topics" like adding garbage
  67. collection support, exceptions, debugging, support for "spaghetti
  68. stacks", etc.
  69. By the end of the tutorial, we'll have written a bit less than 1000 lines
  70. of (non-comment, non-blank) lines of code. With this small amount of
  71. code, we'll have built up a nice little compiler for a non-trivial
  72. language including a hand-written lexer, parser, AST, as well as code
  73. generation support - both static and JIT! The breadth of this is a great
  74. testament to the strengths of LLVM and shows why it is such a popular
  75. target for language designers and others who need high performance code
  76. generation.