xref: /aosp_15_r20/external/llvm/docs/tutorial/LangImpl09.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker======================================
2*9880d681SAndroid Build Coastguard WorkerKaleidoscope: Adding Debug Information
3*9880d681SAndroid Build Coastguard Worker======================================
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Worker.. contents::
6*9880d681SAndroid Build Coastguard Worker   :local:
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard WorkerChapter 9 Introduction
9*9880d681SAndroid Build Coastguard Worker======================
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard WorkerWelcome to Chapter 9 of the "`Implementing a language with
12*9880d681SAndroid Build Coastguard WorkerLLVM <index.html>`_" tutorial. In chapters 1 through 8, we've built a
13*9880d681SAndroid Build Coastguard Workerdecent little programming language with functions and variables.
14*9880d681SAndroid Build Coastguard WorkerWhat happens if something goes wrong though, how do you debug your
15*9880d681SAndroid Build Coastguard Workerprogram?
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard WorkerSource level debugging uses formatted data that helps a debugger
18*9880d681SAndroid Build Coastguard Workertranslate from binary and the state of the machine back to the
19*9880d681SAndroid Build Coastguard Workersource that the programmer wrote. In LLVM we generally use a format
20*9880d681SAndroid Build Coastguard Workercalled `DWARF <http://dwarfstd.org>`_. DWARF is a compact encoding
21*9880d681SAndroid Build Coastguard Workerthat represents types, source locations, and variable locations.
22*9880d681SAndroid Build Coastguard Worker
23*9880d681SAndroid Build Coastguard WorkerThe short summary of this chapter is that we'll go through the
24*9880d681SAndroid Build Coastguard Workervarious things you have to add to a programming language to
25*9880d681SAndroid Build Coastguard Workersupport debug info, and how you translate that into DWARF.
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard WorkerCaveat: For now we can't debug via the JIT, so we'll need to compile
28*9880d681SAndroid Build Coastguard Workerour program down to something small and standalone. As part of this
29*9880d681SAndroid Build Coastguard Workerwe'll make a few modifications to the running of the language and
30*9880d681SAndroid Build Coastguard Workerhow programs are compiled. This means that we'll have a source file
31*9880d681SAndroid Build Coastguard Workerwith a simple program written in Kaleidoscope rather than the
32*9880d681SAndroid Build Coastguard Workerinteractive JIT. It does involve a limitation that we can only
33*9880d681SAndroid Build Coastguard Workerhave one "top level" command at a time to reduce the number of
34*9880d681SAndroid Build Coastguard Workerchanges necessary.
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard WorkerHere's the sample program we'll be compiling:
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker.. code-block:: python
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker   def fib(x)
41*9880d681SAndroid Build Coastguard Worker     if x < 3 then
42*9880d681SAndroid Build Coastguard Worker       1
43*9880d681SAndroid Build Coastguard Worker     else
44*9880d681SAndroid Build Coastguard Worker       fib(x-1)+fib(x-2);
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Worker   fib(10)
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard WorkerWhy is this a hard problem?
50*9880d681SAndroid Build Coastguard Worker===========================
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard WorkerDebug information is a hard problem for a few different reasons - mostly
53*9880d681SAndroid Build Coastguard Workercentered around optimized code. First, optimization makes keeping source
54*9880d681SAndroid Build Coastguard Workerlocations more difficult. In LLVM IR we keep the original source location
55*9880d681SAndroid Build Coastguard Workerfor each IR level instruction on the instruction. Optimization passes
56*9880d681SAndroid Build Coastguard Workershould keep the source locations for newly created instructions, but merged
57*9880d681SAndroid Build Coastguard Workerinstructions only get to keep a single location - this can cause jumping
58*9880d681SAndroid Build Coastguard Workeraround when stepping through optimized programs. Secondly, optimization
59*9880d681SAndroid Build Coastguard Workercan move variables in ways that are either optimized out, shared in memory
60*9880d681SAndroid Build Coastguard Workerwith other variables, or difficult to track. For the purposes of this
61*9880d681SAndroid Build Coastguard Workertutorial we're going to avoid optimization (as you'll see with one of the
62*9880d681SAndroid Build Coastguard Workernext sets of patches).
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard WorkerAhead-of-Time Compilation Mode
65*9880d681SAndroid Build Coastguard Worker==============================
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard WorkerTo highlight only the aspects of adding debug information to a source
68*9880d681SAndroid Build Coastguard Workerlanguage without needing to worry about the complexities of JIT debugging
69*9880d681SAndroid Build Coastguard Workerwe're going to make a few changes to Kaleidoscope to support compiling
70*9880d681SAndroid Build Coastguard Workerthe IR emitted by the front end into a simple standalone program that
71*9880d681SAndroid Build Coastguard Workeryou can execute, debug, and see results.
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard WorkerFirst we make our anonymous function that contains our top level
74*9880d681SAndroid Build Coastguard Workerstatement be our "main":
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker.. code-block:: udiff
77*9880d681SAndroid Build Coastguard Worker
78*9880d681SAndroid Build Coastguard Worker  -    auto Proto = llvm::make_unique<PrototypeAST>("", std::vector<std::string>());
79*9880d681SAndroid Build Coastguard Worker  +    auto Proto = llvm::make_unique<PrototypeAST>("main", std::vector<std::string>());
80*9880d681SAndroid Build Coastguard Worker
81*9880d681SAndroid Build Coastguard Workerjust with the simple change of giving it a name.
82*9880d681SAndroid Build Coastguard Worker
83*9880d681SAndroid Build Coastguard WorkerThen we're going to remove the command line code wherever it exists:
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker.. code-block:: udiff
86*9880d681SAndroid Build Coastguard Worker
87*9880d681SAndroid Build Coastguard Worker  @@ -1129,7 +1129,6 @@ static void HandleTopLevelExpression() {
88*9880d681SAndroid Build Coastguard Worker   /// top ::= definition | external | expression | ';'
89*9880d681SAndroid Build Coastguard Worker   static void MainLoop() {
90*9880d681SAndroid Build Coastguard Worker     while (1) {
91*9880d681SAndroid Build Coastguard Worker  -    fprintf(stderr, "ready> ");
92*9880d681SAndroid Build Coastguard Worker       switch (CurTok) {
93*9880d681SAndroid Build Coastguard Worker       case tok_eof:
94*9880d681SAndroid Build Coastguard Worker         return;
95*9880d681SAndroid Build Coastguard Worker  @@ -1184,7 +1183,6 @@ int main() {
96*9880d681SAndroid Build Coastguard Worker     BinopPrecedence['*'] = 40; // highest.
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Worker     // Prime the first token.
99*9880d681SAndroid Build Coastguard Worker  -  fprintf(stderr, "ready> ");
100*9880d681SAndroid Build Coastguard Worker     getNextToken();
101*9880d681SAndroid Build Coastguard Worker
102*9880d681SAndroid Build Coastguard WorkerLastly we're going to disable all of the optimization passes and the JIT so
103*9880d681SAndroid Build Coastguard Workerthat the only thing that happens after we're done parsing and generating
104*9880d681SAndroid Build Coastguard Workercode is that the llvm IR goes to standard error:
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker.. code-block:: udiff
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker  @@ -1108,17 +1108,8 @@ static void HandleExtern() {
109*9880d681SAndroid Build Coastguard Worker   static void HandleTopLevelExpression() {
110*9880d681SAndroid Build Coastguard Worker     // Evaluate a top-level expression into an anonymous function.
111*9880d681SAndroid Build Coastguard Worker     if (auto FnAST = ParseTopLevelExpr()) {
112*9880d681SAndroid Build Coastguard Worker  -    if (auto *FnIR = FnAST->codegen()) {
113*9880d681SAndroid Build Coastguard Worker  -      // We're just doing this to make sure it executes.
114*9880d681SAndroid Build Coastguard Worker  -      TheExecutionEngine->finalizeObject();
115*9880d681SAndroid Build Coastguard Worker  -      // JIT the function, returning a function pointer.
116*9880d681SAndroid Build Coastguard Worker  -      void *FPtr = TheExecutionEngine->getPointerToFunction(FnIR);
117*9880d681SAndroid Build Coastguard Worker  -
118*9880d681SAndroid Build Coastguard Worker  -      // Cast it to the right type (takes no arguments, returns a double) so we
119*9880d681SAndroid Build Coastguard Worker  -      // can call it as a native function.
120*9880d681SAndroid Build Coastguard Worker  -      double (*FP)() = (double (*)())(intptr_t)FPtr;
121*9880d681SAndroid Build Coastguard Worker  -      // Ignore the return value for this.
122*9880d681SAndroid Build Coastguard Worker  -      (void)FP;
123*9880d681SAndroid Build Coastguard Worker  +    if (!F->codegen()) {
124*9880d681SAndroid Build Coastguard Worker  +      fprintf(stderr, "Error generating code for top level expr");
125*9880d681SAndroid Build Coastguard Worker       }
126*9880d681SAndroid Build Coastguard Worker     } else {
127*9880d681SAndroid Build Coastguard Worker       // Skip token for error recovery.
128*9880d681SAndroid Build Coastguard Worker  @@ -1439,11 +1459,11 @@ int main() {
129*9880d681SAndroid Build Coastguard Worker     // target lays out data structures.
130*9880d681SAndroid Build Coastguard Worker     TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
131*9880d681SAndroid Build Coastguard Worker     OurFPM.add(new DataLayoutPass());
132*9880d681SAndroid Build Coastguard Worker  +#if 0
133*9880d681SAndroid Build Coastguard Worker     OurFPM.add(createBasicAliasAnalysisPass());
134*9880d681SAndroid Build Coastguard Worker     // Promote allocas to registers.
135*9880d681SAndroid Build Coastguard Worker     OurFPM.add(createPromoteMemoryToRegisterPass());
136*9880d681SAndroid Build Coastguard Worker  @@ -1218,7 +1210,7 @@ int main() {
137*9880d681SAndroid Build Coastguard Worker     OurFPM.add(createGVNPass());
138*9880d681SAndroid Build Coastguard Worker     // Simplify the control flow graph (deleting unreachable blocks, etc).
139*9880d681SAndroid Build Coastguard Worker     OurFPM.add(createCFGSimplificationPass());
140*9880d681SAndroid Build Coastguard Worker  -
141*9880d681SAndroid Build Coastguard Worker  +  #endif
142*9880d681SAndroid Build Coastguard Worker     OurFPM.doInitialization();
143*9880d681SAndroid Build Coastguard Worker
144*9880d681SAndroid Build Coastguard Worker     // Set the global so the code gen can use this.
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard WorkerThis relatively small set of changes get us to the point that we can compile
147*9880d681SAndroid Build Coastguard Workerour piece of Kaleidoscope language down to an executable program via this
148*9880d681SAndroid Build Coastguard Workercommand line:
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard Worker.. code-block:: bash
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker  Kaleidoscope-Ch9 < fib.ks | & clang -x ir -
153*9880d681SAndroid Build Coastguard Worker
154*9880d681SAndroid Build Coastguard Workerwhich gives an a.out/a.exe in the current working directory.
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard WorkerCompile Unit
157*9880d681SAndroid Build Coastguard Worker============
158*9880d681SAndroid Build Coastguard Worker
159*9880d681SAndroid Build Coastguard WorkerThe top level container for a section of code in DWARF is a compile unit.
160*9880d681SAndroid Build Coastguard WorkerThis contains the type and function data for an individual translation unit
161*9880d681SAndroid Build Coastguard Worker(read: one file of source code). So the first thing we need to do is
162*9880d681SAndroid Build Coastguard Workerconstruct one for our fib.ks file.
163*9880d681SAndroid Build Coastguard Worker
164*9880d681SAndroid Build Coastguard WorkerDWARF Emission Setup
165*9880d681SAndroid Build Coastguard Worker====================
166*9880d681SAndroid Build Coastguard Worker
167*9880d681SAndroid Build Coastguard WorkerSimilar to the ``IRBuilder`` class we have a
168*9880d681SAndroid Build Coastguard Worker`DIBuilder <http://llvm.org/doxygen/classllvm_1_1DIBuilder.html>`_ class
169*9880d681SAndroid Build Coastguard Workerthat helps in constructing debug metadata for an llvm IR file. It
170*9880d681SAndroid Build Coastguard Workercorresponds 1:1 similarly to ``IRBuilder`` and llvm IR, but with nicer names.
171*9880d681SAndroid Build Coastguard WorkerUsing it does require that you be more familiar with DWARF terminology than
172*9880d681SAndroid Build Coastguard Workeryou needed to be with ``IRBuilder`` and ``Instruction`` names, but if you
173*9880d681SAndroid Build Coastguard Workerread through the general documentation on the
174*9880d681SAndroid Build Coastguard Worker`Metadata Format <http://llvm.org/docs/SourceLevelDebugging.html>`_ it
175*9880d681SAndroid Build Coastguard Workershould be a little more clear. We'll be using this class to construct all
176*9880d681SAndroid Build Coastguard Workerof our IR level descriptions. Construction for it takes a module so we
177*9880d681SAndroid Build Coastguard Workerneed to construct it shortly after we construct our module. We've left it
178*9880d681SAndroid Build Coastguard Workeras a global static variable to make it a bit easier to use.
179*9880d681SAndroid Build Coastguard Worker
180*9880d681SAndroid Build Coastguard WorkerNext we're going to create a small container to cache some of our frequent
181*9880d681SAndroid Build Coastguard Workerdata. The first will be our compile unit, but we'll also write a bit of
182*9880d681SAndroid Build Coastguard Workercode for our one type since we won't have to worry about multiple typed
183*9880d681SAndroid Build Coastguard Workerexpressions:
184*9880d681SAndroid Build Coastguard Worker
185*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
186*9880d681SAndroid Build Coastguard Worker
187*9880d681SAndroid Build Coastguard Worker  static DIBuilder *DBuilder;
188*9880d681SAndroid Build Coastguard Worker
189*9880d681SAndroid Build Coastguard Worker  struct DebugInfo {
190*9880d681SAndroid Build Coastguard Worker    DICompileUnit *TheCU;
191*9880d681SAndroid Build Coastguard Worker    DIType *DblTy;
192*9880d681SAndroid Build Coastguard Worker
193*9880d681SAndroid Build Coastguard Worker    DIType *getDoubleTy();
194*9880d681SAndroid Build Coastguard Worker  } KSDbgInfo;
195*9880d681SAndroid Build Coastguard Worker
196*9880d681SAndroid Build Coastguard Worker  DIType *DebugInfo::getDoubleTy() {
197*9880d681SAndroid Build Coastguard Worker    if (DblTy.isValid())
198*9880d681SAndroid Build Coastguard Worker      return DblTy;
199*9880d681SAndroid Build Coastguard Worker
200*9880d681SAndroid Build Coastguard Worker    DblTy = DBuilder->createBasicType("double", 64, 64, dwarf::DW_ATE_float);
201*9880d681SAndroid Build Coastguard Worker    return DblTy;
202*9880d681SAndroid Build Coastguard Worker  }
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard WorkerAnd then later on in ``main`` when we're constructing our module:
205*9880d681SAndroid Build Coastguard Worker
206*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
207*9880d681SAndroid Build Coastguard Worker
208*9880d681SAndroid Build Coastguard Worker  DBuilder = new DIBuilder(*TheModule);
209*9880d681SAndroid Build Coastguard Worker
210*9880d681SAndroid Build Coastguard Worker  KSDbgInfo.TheCU = DBuilder->createCompileUnit(
211*9880d681SAndroid Build Coastguard Worker      dwarf::DW_LANG_C, "fib.ks", ".", "Kaleidoscope Compiler", 0, "", 0);
212*9880d681SAndroid Build Coastguard Worker
213*9880d681SAndroid Build Coastguard WorkerThere are a couple of things to note here. First, while we're producing a
214*9880d681SAndroid Build Coastguard Workercompile unit for a language called Kaleidoscope we used the language
215*9880d681SAndroid Build Coastguard Workerconstant for C. This is because a debugger wouldn't necessarily understand
216*9880d681SAndroid Build Coastguard Workerthe calling conventions or default ABI for a language it doesn't recognize
217*9880d681SAndroid Build Coastguard Workerand we follow the C ABI in our llvm code generation so it's the closest
218*9880d681SAndroid Build Coastguard Workerthing to accurate. This ensures we can actually call functions from the
219*9880d681SAndroid Build Coastguard Workerdebugger and have them execute. Secondly, you'll see the "fib.ks" in the
220*9880d681SAndroid Build Coastguard Workercall to ``createCompileUnit``. This is a default hard coded value since
221*9880d681SAndroid Build Coastguard Workerwe're using shell redirection to put our source into the Kaleidoscope
222*9880d681SAndroid Build Coastguard Workercompiler. In a usual front end you'd have an input file name and it would
223*9880d681SAndroid Build Coastguard Workergo there.
224*9880d681SAndroid Build Coastguard Worker
225*9880d681SAndroid Build Coastguard WorkerOne last thing as part of emitting debug information via DIBuilder is that
226*9880d681SAndroid Build Coastguard Workerwe need to "finalize" the debug information. The reasons are part of the
227*9880d681SAndroid Build Coastguard Workerunderlying API for DIBuilder, but make sure you do this near the end of
228*9880d681SAndroid Build Coastguard Workermain:
229*9880d681SAndroid Build Coastguard Worker
230*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard Worker  DBuilder->finalize();
233*9880d681SAndroid Build Coastguard Worker
234*9880d681SAndroid Build Coastguard Workerbefore you dump out the module.
235*9880d681SAndroid Build Coastguard Worker
236*9880d681SAndroid Build Coastguard WorkerFunctions
237*9880d681SAndroid Build Coastguard Worker=========
238*9880d681SAndroid Build Coastguard Worker
239*9880d681SAndroid Build Coastguard WorkerNow that we have our ``Compile Unit`` and our source locations, we can add
240*9880d681SAndroid Build Coastguard Workerfunction definitions to the debug info. So in ``PrototypeAST::codegen()`` we
241*9880d681SAndroid Build Coastguard Workeradd a few lines of code to describe a context for our subprogram, in this
242*9880d681SAndroid Build Coastguard Workercase the "File", and the actual definition of the function itself.
243*9880d681SAndroid Build Coastguard Worker
244*9880d681SAndroid Build Coastguard WorkerSo the context:
245*9880d681SAndroid Build Coastguard Worker
246*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
247*9880d681SAndroid Build Coastguard Worker
248*9880d681SAndroid Build Coastguard Worker  DIFile *Unit = DBuilder->createFile(KSDbgInfo.TheCU.getFilename(),
249*9880d681SAndroid Build Coastguard Worker                                      KSDbgInfo.TheCU.getDirectory());
250*9880d681SAndroid Build Coastguard Worker
251*9880d681SAndroid Build Coastguard Workergiving us an DIFile and asking the ``Compile Unit`` we created above for the
252*9880d681SAndroid Build Coastguard Workerdirectory and filename where we are currently. Then, for now, we use some
253*9880d681SAndroid Build Coastguard Workersource locations of 0 (since our AST doesn't currently have source location
254*9880d681SAndroid Build Coastguard Workerinformation) and construct our function definition:
255*9880d681SAndroid Build Coastguard Worker
256*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
257*9880d681SAndroid Build Coastguard Worker
258*9880d681SAndroid Build Coastguard Worker  DIScope *FContext = Unit;
259*9880d681SAndroid Build Coastguard Worker  unsigned LineNo = 0;
260*9880d681SAndroid Build Coastguard Worker  unsigned ScopeLine = 0;
261*9880d681SAndroid Build Coastguard Worker  DISubprogram *SP = DBuilder->createFunction(
262*9880d681SAndroid Build Coastguard Worker      FContext, Name, StringRef(), Unit, LineNo,
263*9880d681SAndroid Build Coastguard Worker      CreateFunctionType(Args.size(), Unit), false /* internal linkage */,
264*9880d681SAndroid Build Coastguard Worker      true /* definition */, ScopeLine, DINode::FlagPrototyped, false);
265*9880d681SAndroid Build Coastguard Worker  F->setSubprogram(SP);
266*9880d681SAndroid Build Coastguard Worker
267*9880d681SAndroid Build Coastguard Workerand we now have an DISubprogram that contains a reference to all of our
268*9880d681SAndroid Build Coastguard Workermetadata for the function.
269*9880d681SAndroid Build Coastguard Worker
270*9880d681SAndroid Build Coastguard WorkerSource Locations
271*9880d681SAndroid Build Coastguard Worker================
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard WorkerThe most important thing for debug information is accurate source location -
274*9880d681SAndroid Build Coastguard Workerthis makes it possible to map your source code back. We have a problem though,
275*9880d681SAndroid Build Coastguard WorkerKaleidoscope really doesn't have any source location information in the lexer
276*9880d681SAndroid Build Coastguard Workeror parser so we'll need to add it.
277*9880d681SAndroid Build Coastguard Worker
278*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
279*9880d681SAndroid Build Coastguard Worker
280*9880d681SAndroid Build Coastguard Worker   struct SourceLocation {
281*9880d681SAndroid Build Coastguard Worker     int Line;
282*9880d681SAndroid Build Coastguard Worker     int Col;
283*9880d681SAndroid Build Coastguard Worker   };
284*9880d681SAndroid Build Coastguard Worker   static SourceLocation CurLoc;
285*9880d681SAndroid Build Coastguard Worker   static SourceLocation LexLoc = {1, 0};
286*9880d681SAndroid Build Coastguard Worker
287*9880d681SAndroid Build Coastguard Worker   static int advance() {
288*9880d681SAndroid Build Coastguard Worker     int LastChar = getchar();
289*9880d681SAndroid Build Coastguard Worker
290*9880d681SAndroid Build Coastguard Worker     if (LastChar == '\n' || LastChar == '\r') {
291*9880d681SAndroid Build Coastguard Worker       LexLoc.Line++;
292*9880d681SAndroid Build Coastguard Worker       LexLoc.Col = 0;
293*9880d681SAndroid Build Coastguard Worker     } else
294*9880d681SAndroid Build Coastguard Worker       LexLoc.Col++;
295*9880d681SAndroid Build Coastguard Worker     return LastChar;
296*9880d681SAndroid Build Coastguard Worker   }
297*9880d681SAndroid Build Coastguard Worker
298*9880d681SAndroid Build Coastguard WorkerIn this set of code we've added some functionality on how to keep track of the
299*9880d681SAndroid Build Coastguard Workerline and column of the "source file". As we lex every token we set our current
300*9880d681SAndroid Build Coastguard Workercurrent "lexical location" to the assorted line and column for the beginning
301*9880d681SAndroid Build Coastguard Workerof the token. We do this by overriding all of the previous calls to
302*9880d681SAndroid Build Coastguard Worker``getchar()`` with our new ``advance()`` that keeps track of the information
303*9880d681SAndroid Build Coastguard Workerand then we have added to all of our AST classes a source location:
304*9880d681SAndroid Build Coastguard Worker
305*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
306*9880d681SAndroid Build Coastguard Worker
307*9880d681SAndroid Build Coastguard Worker   class ExprAST {
308*9880d681SAndroid Build Coastguard Worker     SourceLocation Loc;
309*9880d681SAndroid Build Coastguard Worker
310*9880d681SAndroid Build Coastguard Worker     public:
311*9880d681SAndroid Build Coastguard Worker       ExprAST(SourceLocation Loc = CurLoc) : Loc(Loc) {}
312*9880d681SAndroid Build Coastguard Worker       virtual ~ExprAST() {}
313*9880d681SAndroid Build Coastguard Worker       virtual Value* codegen() = 0;
314*9880d681SAndroid Build Coastguard Worker       int getLine() const { return Loc.Line; }
315*9880d681SAndroid Build Coastguard Worker       int getCol() const { return Loc.Col; }
316*9880d681SAndroid Build Coastguard Worker       virtual raw_ostream &dump(raw_ostream &out, int ind) {
317*9880d681SAndroid Build Coastguard Worker         return out << ':' << getLine() << ':' << getCol() << '\n';
318*9880d681SAndroid Build Coastguard Worker       }
319*9880d681SAndroid Build Coastguard Worker
320*9880d681SAndroid Build Coastguard Workerthat we pass down through when we create a new expression:
321*9880d681SAndroid Build Coastguard Worker
322*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
323*9880d681SAndroid Build Coastguard Worker
324*9880d681SAndroid Build Coastguard Worker   LHS = llvm::make_unique<BinaryExprAST>(BinLoc, BinOp, std::move(LHS),
325*9880d681SAndroid Build Coastguard Worker                                          std::move(RHS));
326*9880d681SAndroid Build Coastguard Worker
327*9880d681SAndroid Build Coastguard Workergiving us locations for each of our expressions and variables.
328*9880d681SAndroid Build Coastguard Worker
329*9880d681SAndroid Build Coastguard WorkerFrom this we can make sure to tell ``DIBuilder`` when we're at a new source
330*9880d681SAndroid Build Coastguard Workerlocation so it can use that when we generate the rest of our code and make
331*9880d681SAndroid Build Coastguard Workersure that each instruction has source location information. We do this
332*9880d681SAndroid Build Coastguard Workerby constructing another small function:
333*9880d681SAndroid Build Coastguard Worker
334*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
335*9880d681SAndroid Build Coastguard Worker
336*9880d681SAndroid Build Coastguard Worker  void DebugInfo::emitLocation(ExprAST *AST) {
337*9880d681SAndroid Build Coastguard Worker    DIScope *Scope;
338*9880d681SAndroid Build Coastguard Worker    if (LexicalBlocks.empty())
339*9880d681SAndroid Build Coastguard Worker      Scope = TheCU;
340*9880d681SAndroid Build Coastguard Worker    else
341*9880d681SAndroid Build Coastguard Worker      Scope = LexicalBlocks.back();
342*9880d681SAndroid Build Coastguard Worker    Builder.SetCurrentDebugLocation(
343*9880d681SAndroid Build Coastguard Worker        DebugLoc::get(AST->getLine(), AST->getCol(), Scope));
344*9880d681SAndroid Build Coastguard Worker  }
345*9880d681SAndroid Build Coastguard Worker
346*9880d681SAndroid Build Coastguard Workerthat both tells the main ``IRBuilder`` where we are, but also what scope
347*9880d681SAndroid Build Coastguard Workerwe're in. Since we've just created a function above we can either be in
348*9880d681SAndroid Build Coastguard Workerthe main file scope (like when we created our function), or now we can be
349*9880d681SAndroid Build Coastguard Workerin the function scope we just created. To represent this we create a stack
350*9880d681SAndroid Build Coastguard Workerof scopes:
351*9880d681SAndroid Build Coastguard Worker
352*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
353*9880d681SAndroid Build Coastguard Worker
354*9880d681SAndroid Build Coastguard Worker   std::vector<DIScope *> LexicalBlocks;
355*9880d681SAndroid Build Coastguard Worker   std::map<const PrototypeAST *, DIScope *> FnScopeMap;
356*9880d681SAndroid Build Coastguard Worker
357*9880d681SAndroid Build Coastguard Workerand keep a map of each function to the scope that it represents (an
358*9880d681SAndroid Build Coastguard WorkerDISubprogram is also an DIScope).
359*9880d681SAndroid Build Coastguard Worker
360*9880d681SAndroid Build Coastguard WorkerThen we make sure to:
361*9880d681SAndroid Build Coastguard Worker
362*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
363*9880d681SAndroid Build Coastguard Worker
364*9880d681SAndroid Build Coastguard Worker   KSDbgInfo.emitLocation(this);
365*9880d681SAndroid Build Coastguard Worker
366*9880d681SAndroid Build Coastguard Workeremit the location every time we start to generate code for a new AST, and
367*9880d681SAndroid Build Coastguard Workeralso:
368*9880d681SAndroid Build Coastguard Worker
369*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
370*9880d681SAndroid Build Coastguard Worker
371*9880d681SAndroid Build Coastguard Worker  KSDbgInfo.FnScopeMap[this] = SP;
372*9880d681SAndroid Build Coastguard Worker
373*9880d681SAndroid Build Coastguard Workerstore the scope (function) when we create it and use it:
374*9880d681SAndroid Build Coastguard Worker
375*9880d681SAndroid Build Coastguard Worker  KSDbgInfo.LexicalBlocks.push_back(&KSDbgInfo.FnScopeMap[Proto]);
376*9880d681SAndroid Build Coastguard Worker
377*9880d681SAndroid Build Coastguard Workerwhen we start generating the code for each function.
378*9880d681SAndroid Build Coastguard Worker
379*9880d681SAndroid Build Coastguard Workeralso, don't forget to pop the scope back off of your scope stack at the
380*9880d681SAndroid Build Coastguard Workerend of the code generation for the function:
381*9880d681SAndroid Build Coastguard Worker
382*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
383*9880d681SAndroid Build Coastguard Worker
384*9880d681SAndroid Build Coastguard Worker  // Pop off the lexical block for the function since we added it
385*9880d681SAndroid Build Coastguard Worker  // unconditionally.
386*9880d681SAndroid Build Coastguard Worker  KSDbgInfo.LexicalBlocks.pop_back();
387*9880d681SAndroid Build Coastguard Worker
388*9880d681SAndroid Build Coastguard WorkerVariables
389*9880d681SAndroid Build Coastguard Worker=========
390*9880d681SAndroid Build Coastguard Worker
391*9880d681SAndroid Build Coastguard WorkerNow that we have functions, we need to be able to print out the variables
392*9880d681SAndroid Build Coastguard Workerwe have in scope. Let's get our function arguments set up so we can get
393*9880d681SAndroid Build Coastguard Workerdecent backtraces and see how our functions are being called. It isn't
394*9880d681SAndroid Build Coastguard Workera lot of code, and we generally handle it when we're creating the
395*9880d681SAndroid Build Coastguard Workerargument allocas in ``PrototypeAST::CreateArgumentAllocas``.
396*9880d681SAndroid Build Coastguard Worker
397*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
398*9880d681SAndroid Build Coastguard Worker
399*9880d681SAndroid Build Coastguard Worker  DIScope *Scope = KSDbgInfo.LexicalBlocks.back();
400*9880d681SAndroid Build Coastguard Worker  DIFile *Unit = DBuilder->createFile(KSDbgInfo.TheCU.getFilename(),
401*9880d681SAndroid Build Coastguard Worker                                      KSDbgInfo.TheCU.getDirectory());
402*9880d681SAndroid Build Coastguard Worker  DILocalVariable D = DBuilder->createParameterVariable(
403*9880d681SAndroid Build Coastguard Worker      Scope, Args[Idx], Idx + 1, Unit, Line, KSDbgInfo.getDoubleTy(), true);
404*9880d681SAndroid Build Coastguard Worker
405*9880d681SAndroid Build Coastguard Worker  DBuilder->insertDeclare(Alloca, D, DBuilder->createExpression(),
406*9880d681SAndroid Build Coastguard Worker                          DebugLoc::get(Line, 0, Scope),
407*9880d681SAndroid Build Coastguard Worker                          Builder.GetInsertBlock());
408*9880d681SAndroid Build Coastguard Worker
409*9880d681SAndroid Build Coastguard WorkerHere we're doing a few things. First, we're grabbing our current scope
410*9880d681SAndroid Build Coastguard Workerfor the variable so we can say what range of code our variable is valid
411*9880d681SAndroid Build Coastguard Workerthrough. Second, we're creating the variable, giving it the scope,
412*9880d681SAndroid Build Coastguard Workerthe name, source location, type, and since it's an argument, the argument
413*9880d681SAndroid Build Coastguard Workerindex. Third, we create an ``lvm.dbg.declare`` call to indicate at the IR
414*9880d681SAndroid Build Coastguard Workerlevel that we've got a variable in an alloca (and it gives a starting
415*9880d681SAndroid Build Coastguard Workerlocation for the variable), and setting a source location for the
416*9880d681SAndroid Build Coastguard Workerbeginning of the scope on the declare.
417*9880d681SAndroid Build Coastguard Worker
418*9880d681SAndroid Build Coastguard WorkerOne interesting thing to note at this point is that various debuggers have
419*9880d681SAndroid Build Coastguard Workerassumptions based on how code and debug information was generated for them
420*9880d681SAndroid Build Coastguard Workerin the past. In this case we need to do a little bit of a hack to avoid
421*9880d681SAndroid Build Coastguard Workergenerating line information for the function prologue so that the debugger
422*9880d681SAndroid Build Coastguard Workerknows to skip over those instructions when setting a breakpoint. So in
423*9880d681SAndroid Build Coastguard Worker``FunctionAST::CodeGen`` we add a couple of lines:
424*9880d681SAndroid Build Coastguard Worker
425*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
426*9880d681SAndroid Build Coastguard Worker
427*9880d681SAndroid Build Coastguard Worker  // Unset the location for the prologue emission (leading instructions with no
428*9880d681SAndroid Build Coastguard Worker  // location in a function are considered part of the prologue and the debugger
429*9880d681SAndroid Build Coastguard Worker  // will run past them when breaking on a function)
430*9880d681SAndroid Build Coastguard Worker  KSDbgInfo.emitLocation(nullptr);
431*9880d681SAndroid Build Coastguard Worker
432*9880d681SAndroid Build Coastguard Workerand then emit a new location when we actually start generating code for the
433*9880d681SAndroid Build Coastguard Workerbody of the function:
434*9880d681SAndroid Build Coastguard Worker
435*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
436*9880d681SAndroid Build Coastguard Worker
437*9880d681SAndroid Build Coastguard Worker  KSDbgInfo.emitLocation(Body);
438*9880d681SAndroid Build Coastguard Worker
439*9880d681SAndroid Build Coastguard WorkerWith this we have enough debug information to set breakpoints in functions,
440*9880d681SAndroid Build Coastguard Workerprint out argument variables, and call functions. Not too bad for just a
441*9880d681SAndroid Build Coastguard Workerfew simple lines of code!
442*9880d681SAndroid Build Coastguard Worker
443*9880d681SAndroid Build Coastguard WorkerFull Code Listing
444*9880d681SAndroid Build Coastguard Worker=================
445*9880d681SAndroid Build Coastguard Worker
446*9880d681SAndroid Build Coastguard WorkerHere is the complete code listing for our running example, enhanced with
447*9880d681SAndroid Build Coastguard Workerdebug information. To build this example, use:
448*9880d681SAndroid Build Coastguard Worker
449*9880d681SAndroid Build Coastguard Worker.. code-block:: bash
450*9880d681SAndroid Build Coastguard Worker
451*9880d681SAndroid Build Coastguard Worker    # Compile
452*9880d681SAndroid Build Coastguard Worker    clang++ -g toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core mcjit native` -O3 -o toy
453*9880d681SAndroid Build Coastguard Worker    # Run
454*9880d681SAndroid Build Coastguard Worker    ./toy
455*9880d681SAndroid Build Coastguard Worker
456*9880d681SAndroid Build Coastguard WorkerHere is the code:
457*9880d681SAndroid Build Coastguard Worker
458*9880d681SAndroid Build Coastguard Worker.. literalinclude:: ../../examples/Kaleidoscope/Chapter9/toy.cpp
459*9880d681SAndroid Build Coastguard Worker   :language: c++
460*9880d681SAndroid Build Coastguard Worker
461*9880d681SAndroid Build Coastguard Worker`Next: Conclusion and other useful LLVM tidbits <LangImpl10.html>`_
462*9880d681SAndroid Build Coastguard Worker
463