blob: fe5a95a5769ec7210b7fc3b8e684dcc069cc8b35 [file] [log] [blame]
David Blaikie4a696b02015-02-07 23:23:43 +00001======================================
2Kaleidoscope: Adding Debug Information
3======================================
Sean Silvad7fb3962012-12-05 00:26:32 +00004
5.. contents::
6 :local:
7
Wilfred Hughes945f43e2016-07-02 17:01:59 +00008Chapter 9 Introduction
Eric Christopher05917fa2014-12-08 18:00:47 +00009======================
Sean Silvad7fb3962012-12-05 00:26:32 +000010
Wilfred Hughes945f43e2016-07-02 17:01:59 +000011Welcome to Chapter 9 of the "`Implementing a language with
12LLVM <index.html>`_" tutorial. In chapters 1 through 8, we've built a
Eric Christopher05917fa2014-12-08 18:00:47 +000013decent little programming language with functions and variables.
14What happens if something goes wrong though, how do you debug your
15program?
Sean Silvad7fb3962012-12-05 00:26:32 +000016
Eric Christopher05917fa2014-12-08 18:00:47 +000017Source level debugging uses formatted data that helps a debugger
18translate from binary and the state of the machine back to the
19source that the programmer wrote. In LLVM we generally use a format
20called `DWARF <http://dwarfstd.org>`_. DWARF is a compact encoding
Mehdi Aminibb6805d2017-02-11 21:26:52 +000021that represents types, source locations, and variable locations.
Sean Silvad7fb3962012-12-05 00:26:32 +000022
Eric Christopher05917fa2014-12-08 18:00:47 +000023The short summary of this chapter is that we'll go through the
24various things you have to add to a programming language to
25support debug info, and how you translate that into DWARF.
Sean Silvad7fb3962012-12-05 00:26:32 +000026
Eric Christopher05917fa2014-12-08 18:00:47 +000027Caveat: For now we can't debug via the JIT, so we'll need to compile
28our program down to something small and standalone. As part of this
29we'll make a few modifications to the running of the language and
30how programs are compiled. This means that we'll have a source file
31with a simple program written in Kaleidoscope rather than the
32interactive JIT. It does involve a limitation that we can only
33have one "top level" command at a time to reduce the number of
34changes necessary.
Sean Silvad7fb3962012-12-05 00:26:32 +000035
Eric Christopher05917fa2014-12-08 18:00:47 +000036Here's the sample program we'll be compiling:
Sean Silvad7fb3962012-12-05 00:26:32 +000037
Eric Christopher05917fa2014-12-08 18:00:47 +000038.. code-block:: python
Sean Silvad7fb3962012-12-05 00:26:32 +000039
Eric Christopher05917fa2014-12-08 18:00:47 +000040 def fib(x)
41 if x < 3 then
42 1
43 else
44 fib(x-1)+fib(x-2);
Sean Silvad7fb3962012-12-05 00:26:32 +000045
Eric Christopher05917fa2014-12-08 18:00:47 +000046 fib(10)
Sean Silvad7fb3962012-12-05 00:26:32 +000047
Sean Silvad7fb3962012-12-05 00:26:32 +000048
Eric Christopher05917fa2014-12-08 18:00:47 +000049Why is this a hard problem?
50===========================
Sean Silvad7fb3962012-12-05 00:26:32 +000051
Eric Christopher05917fa2014-12-08 18:00:47 +000052Debug information is a hard problem for a few different reasons - mostly
53centered around optimized code. First, optimization makes keeping source
54locations more difficult. In LLVM IR we keep the original source location
55for each IR level instruction on the instruction. Optimization passes
56should keep the source locations for newly created instructions, but merged
57instructions only get to keep a single location - this can cause jumping
58around when stepping through optimized programs. Secondly, optimization
59can move variables in ways that are either optimized out, shared in memory
60with other variables, or difficult to track. For the purposes of this
61tutorial we're going to avoid optimization (as you'll see with one of the
62next sets of patches).
Sean Silvad7fb3962012-12-05 00:26:32 +000063
Eric Christopher05917fa2014-12-08 18:00:47 +000064Ahead-of-Time Compilation Mode
65==============================
Sean Silvad7fb3962012-12-05 00:26:32 +000066
Eric Christopher05917fa2014-12-08 18:00:47 +000067To highlight only the aspects of adding debug information to a source
68language without needing to worry about the complexities of JIT debugging
69we're going to make a few changes to Kaleidoscope to support compiling
70the IR emitted by the front end into a simple standalone program that
71you can execute, debug, and see results.
Sean Silvad7fb3962012-12-05 00:26:32 +000072
Eric Christopher05917fa2014-12-08 18:00:47 +000073First we make our anonymous function that contains our top level
74statement be our "main":
Sean Silvad7fb3962012-12-05 00:26:32 +000075
Eric Christopher05917fa2014-12-08 18:00:47 +000076.. code-block:: udiff
Sean Silvad7fb3962012-12-05 00:26:32 +000077
Lang Hames09bf4c12015-08-18 18:11:06 +000078 - auto Proto = llvm::make_unique<PrototypeAST>("", std::vector<std::string>());
79 + auto Proto = llvm::make_unique<PrototypeAST>("main", std::vector<std::string>());
Sean Silvad7fb3962012-12-05 00:26:32 +000080
Eric Christopher05917fa2014-12-08 18:00:47 +000081just with the simple change of giving it a name.
Sean Silvad7fb3962012-12-05 00:26:32 +000082
Eric Christopher05917fa2014-12-08 18:00:47 +000083Then we're going to remove the command line code wherever it exists:
Sean Silvad7fb3962012-12-05 00:26:32 +000084
Eric Christopher05917fa2014-12-08 18:00:47 +000085.. code-block:: udiff
Sean Silvad7fb3962012-12-05 00:26:32 +000086
Eric Christopher903f3db2014-12-08 18:48:08 +000087 @@ -1129,7 +1129,6 @@ static void HandleTopLevelExpression() {
88 /// top ::= definition | external | expression | ';'
89 static void MainLoop() {
90 while (1) {
91 - fprintf(stderr, "ready> ");
92 switch (CurTok) {
93 case tok_eof:
94 return;
95 @@ -1184,7 +1183,6 @@ int main() {
96 BinopPrecedence['*'] = 40; // highest.
Mehdi Aminibb6805d2017-02-11 21:26:52 +000097
Eric Christopher903f3db2014-12-08 18:48:08 +000098 // Prime the first token.
99 - fprintf(stderr, "ready> ");
100 getNextToken();
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000101
Eric Christopher05917fa2014-12-08 18:00:47 +0000102Lastly we're going to disable all of the optimization passes and the JIT so
103that the only thing that happens after we're done parsing and generating
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000104code is that the LLVM IR goes to standard error:
Sean Silvad7fb3962012-12-05 00:26:32 +0000105
Eric Christopher05917fa2014-12-08 18:00:47 +0000106.. code-block:: udiff
Sean Silvad7fb3962012-12-05 00:26:32 +0000107
Eric Christopher903f3db2014-12-08 18:48:08 +0000108 @@ -1108,17 +1108,8 @@ static void HandleExtern() {
109 static void HandleTopLevelExpression() {
110 // Evaluate a top-level expression into an anonymous function.
Lang Hames09bf4c12015-08-18 18:11:06 +0000111 if (auto FnAST = ParseTopLevelExpr()) {
Lang Hames2d789c32015-08-26 03:07:41 +0000112 - if (auto *FnIR = FnAST->codegen()) {
Eric Christopher903f3db2014-12-08 18:48:08 +0000113 - // We're just doing this to make sure it executes.
114 - TheExecutionEngine->finalizeObject();
115 - // JIT the function, returning a function pointer.
Lang Hames09bf4c12015-08-18 18:11:06 +0000116 - void *FPtr = TheExecutionEngine->getPointerToFunction(FnIR);
Eric Christopher903f3db2014-12-08 18:48:08 +0000117 -
118 - // Cast it to the right type (takes no arguments, returns a double) so we
119 - // can call it as a native function.
120 - double (*FP)() = (double (*)())(intptr_t)FPtr;
121 - // Ignore the return value for this.
122 - (void)FP;
Lang Hames2d789c32015-08-26 03:07:41 +0000123 + if (!F->codegen()) {
Eric Christopher903f3db2014-12-08 18:48:08 +0000124 + fprintf(stderr, "Error generating code for top level expr");
125 }
126 } else {
127 // Skip token for error recovery.
128 @@ -1439,11 +1459,11 @@ int main() {
129 // target lays out data structures.
130 TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
131 OurFPM.add(new DataLayoutPass());
132 +#if 0
133 OurFPM.add(createBasicAliasAnalysisPass());
134 // Promote allocas to registers.
135 OurFPM.add(createPromoteMemoryToRegisterPass());
136 @@ -1218,7 +1210,7 @@ int main() {
137 OurFPM.add(createGVNPass());
138 // Simplify the control flow graph (deleting unreachable blocks, etc).
139 OurFPM.add(createCFGSimplificationPass());
140 -
141 + #endif
142 OurFPM.doInitialization();
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000143
Eric Christopher903f3db2014-12-08 18:48:08 +0000144 // Set the global so the code gen can use this.
Sean Silvad7fb3962012-12-05 00:26:32 +0000145
Eric Christopher05917fa2014-12-08 18:00:47 +0000146This relatively small set of changes get us to the point that we can compile
147our piece of Kaleidoscope language down to an executable program via this
148command line:
Sean Silvad7fb3962012-12-05 00:26:32 +0000149
Eric Christopher05917fa2014-12-08 18:00:47 +0000150.. code-block:: bash
Sean Silvad7fb3962012-12-05 00:26:32 +0000151
Wilfred Hughes945f43e2016-07-02 17:01:59 +0000152 Kaleidoscope-Ch9 < fib.ks | & clang -x ir -
Sean Silvad7fb3962012-12-05 00:26:32 +0000153
Eric Christopher05917fa2014-12-08 18:00:47 +0000154which gives an a.out/a.exe in the current working directory.
Sean Silvad7fb3962012-12-05 00:26:32 +0000155
Eric Christopher05917fa2014-12-08 18:00:47 +0000156Compile Unit
157============
Sean Silvad7fb3962012-12-05 00:26:32 +0000158
Eric Christopher05917fa2014-12-08 18:00:47 +0000159The top level container for a section of code in DWARF is a compile unit.
160This contains the type and function data for an individual translation unit
161(read: one file of source code). So the first thing we need to do is
162construct one for our fib.ks file.
Sean Silvad7fb3962012-12-05 00:26:32 +0000163
Eric Christopher05917fa2014-12-08 18:00:47 +0000164DWARF Emission Setup
165====================
Sean Silvad7fb3962012-12-05 00:26:32 +0000166
Eric Christopher05917fa2014-12-08 18:00:47 +0000167Similar to the ``IRBuilder`` class we have a
Alex Denisov596e9792015-12-15 20:50:29 +0000168`DIBuilder <http://llvm.org/doxygen/classllvm_1_1DIBuilder.html>`_ class
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000169that helps in constructing debug metadata for an LLVM IR file. It
170corresponds 1:1 similarly to ``IRBuilder`` and LLVM IR, but with nicer names.
Eric Christopher05917fa2014-12-08 18:00:47 +0000171Using it does require that you be more familiar with DWARF terminology than
172you needed to be with ``IRBuilder`` and ``Instruction`` names, but if you
173read through the general documentation on the
Alex Denisov596e9792015-12-15 20:50:29 +0000174`Metadata Format <http://llvm.org/docs/SourceLevelDebugging.html>`_ it
Eric Christopher05917fa2014-12-08 18:00:47 +0000175should be a little more clear. We'll be using this class to construct all
176of our IR level descriptions. Construction for it takes a module so we
177need to construct it shortly after we construct our module. We've left it
178as a global static variable to make it a bit easier to use.
Sean Silvad7fb3962012-12-05 00:26:32 +0000179
Eric Christopher05917fa2014-12-08 18:00:47 +0000180Next we're going to create a small container to cache some of our frequent
181data. The first will be our compile unit, but we'll also write a bit of
182code for our one type since we won't have to worry about multiple typed
183expressions:
Sean Silvad7fb3962012-12-05 00:26:32 +0000184
Eric Christopher05917fa2014-12-08 18:00:47 +0000185.. code-block:: c++
Sean Silvad7fb3962012-12-05 00:26:32 +0000186
Eric Christopher05917fa2014-12-08 18:00:47 +0000187 static DIBuilder *DBuilder;
Sean Silvad7fb3962012-12-05 00:26:32 +0000188
Eric Christopher05917fa2014-12-08 18:00:47 +0000189 struct DebugInfo {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000190 DICompileUnit *TheCU;
191 DIType *DblTy;
Sean Silvad7fb3962012-12-05 00:26:32 +0000192
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000193 DIType *getDoubleTy();
Eric Christopher05917fa2014-12-08 18:00:47 +0000194 } KSDbgInfo;
195
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000196 DIType *DebugInfo::getDoubleTy() {
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000197 if (DblTy)
Eric Christopher05917fa2014-12-08 18:00:47 +0000198 return DblTy;
199
200 DblTy = DBuilder->createBasicType("double", 64, 64, dwarf::DW_ATE_float);
201 return DblTy;
202 }
203
204And then later on in ``main`` when we're constructing our module:
205
206.. code-block:: c++
207
208 DBuilder = new DIBuilder(*TheModule);
209
210 KSDbgInfo.TheCU = DBuilder->createCompileUnit(
211 dwarf::DW_LANG_C, "fib.ks", ".", "Kaleidoscope Compiler", 0, "", 0);
212
213There are a couple of things to note here. First, while we're producing a
214compile unit for a language called Kaleidoscope we used the language
215constant for C. This is because a debugger wouldn't necessarily understand
216the calling conventions or default ABI for a language it doesn't recognize
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000217and we follow the C ABI in our LLVM code generation so it's the closest
Eric Christopher05917fa2014-12-08 18:00:47 +0000218thing to accurate. This ensures we can actually call functions from the
219debugger and have them execute. Secondly, you'll see the "fib.ks" in the
220call to ``createCompileUnit``. This is a default hard coded value since
221we're using shell redirection to put our source into the Kaleidoscope
222compiler. In a usual front end you'd have an input file name and it would
223go there.
224
225One last thing as part of emitting debug information via DIBuilder is that
226we need to "finalize" the debug information. The reasons are part of the
227underlying API for DIBuilder, but make sure you do this near the end of
228main:
229
230.. code-block:: c++
231
232 DBuilder->finalize();
233
234before you dump out the module.
235
236Functions
237=========
238
239Now that we have our ``Compile Unit`` and our source locations, we can add
Lang Hames2d789c32015-08-26 03:07:41 +0000240function definitions to the debug info. So in ``PrototypeAST::codegen()`` we
Eric Christopher05917fa2014-12-08 18:00:47 +0000241add a few lines of code to describe a context for our subprogram, in this
242case the "File", and the actual definition of the function itself.
243
244So the context:
245
246.. code-block:: c++
247
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000248 DIFile *Unit = DBuilder->createFile(KSDbgInfo.TheCU.getFilename(),
Duncan P. N. Exon Smith0a35f652015-04-18 00:01:35 +0000249 KSDbgInfo.TheCU.getDirectory());
Eric Christopher05917fa2014-12-08 18:00:47 +0000250
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000251giving us an DIFile and asking the ``Compile Unit`` we created above for the
Eric Christopher05917fa2014-12-08 18:00:47 +0000252directory and filename where we are currently. Then, for now, we use some
253source locations of 0 (since our AST doesn't currently have source location
254information) and construct our function definition:
255
256.. code-block:: c++
257
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000258 DIScope *FContext = Unit;
Eric Christopher05917fa2014-12-08 18:00:47 +0000259 unsigned LineNo = 0;
260 unsigned ScopeLine = 0;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000261 DISubprogram *SP = DBuilder->createFunction(
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000262 FContext, P.getName(), StringRef(), Unit, LineNo,
263 CreateFunctionType(TheFunction->arg_size(), Unit),
264 false /* internal linkage */, true /* definition */, ScopeLine,
265 DINode::FlagPrototyped, false);
266 TheFunction->setSubprogram(SP);
Eric Christopher05917fa2014-12-08 18:00:47 +0000267
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000268and we now have an DISubprogram that contains a reference to all of our
Duncan P. N. Exon Smith0a35f652015-04-18 00:01:35 +0000269metadata for the function.
Eric Christopher05917fa2014-12-08 18:00:47 +0000270
271Source Locations
272================
273
274The most important thing for debug information is accurate source location -
275this makes it possible to map your source code back. We have a problem though,
276Kaleidoscope really doesn't have any source location information in the lexer
277or parser so we'll need to add it.
278
279.. code-block:: c++
280
281 struct SourceLocation {
282 int Line;
283 int Col;
284 };
285 static SourceLocation CurLoc;
286 static SourceLocation LexLoc = {1, 0};
287
288 static int advance() {
289 int LastChar = getchar();
290
291 if (LastChar == '\n' || LastChar == '\r') {
292 LexLoc.Line++;
293 LexLoc.Col = 0;
294 } else
295 LexLoc.Col++;
296 return LastChar;
297 }
298
299In this set of code we've added some functionality on how to keep track of the
300line and column of the "source file". As we lex every token we set our current
301current "lexical location" to the assorted line and column for the beginning
302of the token. We do this by overriding all of the previous calls to
303``getchar()`` with our new ``advance()`` that keeps track of the information
304and then we have added to all of our AST classes a source location:
305
306.. code-block:: c++
307
308 class ExprAST {
309 SourceLocation Loc;
310
311 public:
Lang Hames59b0da82015-08-19 18:15:58 +0000312 ExprAST(SourceLocation Loc = CurLoc) : Loc(Loc) {}
313 virtual ~ExprAST() {}
Lang Hames2d789c32015-08-26 03:07:41 +0000314 virtual Value* codegen() = 0;
Eric Christopher05917fa2014-12-08 18:00:47 +0000315 int getLine() const { return Loc.Line; }
316 int getCol() const { return Loc.Col; }
Lang Hames59b0da82015-08-19 18:15:58 +0000317 virtual raw_ostream &dump(raw_ostream &out, int ind) {
Eric Christopher05917fa2014-12-08 18:00:47 +0000318 return out << ':' << getLine() << ':' << getCol() << '\n';
319 }
320
321that we pass down through when we create a new expression:
322
323.. code-block:: c++
324
Lang Hames09bf4c12015-08-18 18:11:06 +0000325 LHS = llvm::make_unique<BinaryExprAST>(BinLoc, BinOp, std::move(LHS),
326 std::move(RHS));
Eric Christopher05917fa2014-12-08 18:00:47 +0000327
328giving us locations for each of our expressions and variables.
329
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000330To make sure that every instruction gets proper source location information,
331we have to tell ``Builder`` whenever we're at a new source location.
332We use a small helper function for this:
Eric Christopher05917fa2014-12-08 18:00:47 +0000333
334.. code-block:: c++
335
336 void DebugInfo::emitLocation(ExprAST *AST) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000337 DIScope *Scope;
Eric Christopher05917fa2014-12-08 18:00:47 +0000338 if (LexicalBlocks.empty())
Duncan P. N. Exon Smith0a35f652015-04-18 00:01:35 +0000339 Scope = TheCU;
Eric Christopher05917fa2014-12-08 18:00:47 +0000340 else
341 Scope = LexicalBlocks.back();
342 Builder.SetCurrentDebugLocation(
Duncan P. N. Exon Smith0a35f652015-04-18 00:01:35 +0000343 DebugLoc::get(AST->getLine(), AST->getCol(), Scope));
Eric Christopher05917fa2014-12-08 18:00:47 +0000344 }
345
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000346This both tells the main ``IRBuilder`` where we are, but also what scope
347we're in. The scope can either be on compile-unit level or be the nearest
348enclosing lexical block like the current function.
349To represent this we create a stack of scopes:
Eric Christopher05917fa2014-12-08 18:00:47 +0000350
351.. code-block:: c++
352
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000353 std::vector<DIScope *> LexicalBlocks;
Eric Christopher05917fa2014-12-08 18:00:47 +0000354
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000355and push the scope (function) to the top of the stack when we start
356generating the code for each function:
Eric Christopher05917fa2014-12-08 18:00:47 +0000357
358.. code-block:: c++
359
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000360 KSDbgInfo.LexicalBlocks.push_back(SP);
Eric Christopher05917fa2014-12-08 18:00:47 +0000361
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000362Also, we may not forget to pop the scope back off of the scope stack at the
Eric Christopher0dd4dd32014-12-09 00:28:24 +0000363end of the code generation for the function:
364
365.. code-block:: c++
366
367 // Pop off the lexical block for the function since we added it
368 // unconditionally.
369 KSDbgInfo.LexicalBlocks.pop_back();
370
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000371Then we make sure to emit the location every time we start to generate code
372for a new AST object:
373
374.. code-block:: c++
375
376 KSDbgInfo.emitLocation(this);
377
Eric Christopher0dd4dd32014-12-09 00:28:24 +0000378Variables
379=========
380
381Now that we have functions, we need to be able to print out the variables
382we have in scope. Let's get our function arguments set up so we can get
383decent backtraces and see how our functions are being called. It isn't
384a lot of code, and we generally handle it when we're creating the
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000385argument allocas in ``FunctionAST::codegen``.
Eric Christopher0dd4dd32014-12-09 00:28:24 +0000386
387.. code-block:: c++
388
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000389 // Record the function arguments in the NamedValues map.
390 NamedValues.clear();
391 unsigned ArgIdx = 0;
392 for (auto &Arg : TheFunction->args()) {
393 // Create an alloca for this variable.
394 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, Arg.getName());
Eric Christopher0dd4dd32014-12-09 00:28:24 +0000395
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000396 // Create a debug descriptor for the variable.
397 DILocalVariable *D = DBuilder->createParameterVariable(
398 SP, Arg.getName(), ++ArgIdx, Unit, LineNo, KSDbgInfo.getDoubleTy(),
399 true);
Eric Christopher0dd4dd32014-12-09 00:28:24 +0000400
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000401 DBuilder->insertDeclare(Alloca, D, DBuilder->createExpression(),
402 DebugLoc::get(LineNo, 0, SP),
403 Builder.GetInsertBlock());
404
405 // Store the initial value into the alloca.
406 Builder.CreateStore(&Arg, Alloca);
407
408 // Add arguments to variable symbol table.
409 NamedValues[Arg.getName()] = Alloca;
410 }
411
412
413Here we're first creating the variable, giving it the scope (``SP``),
Eric Christopher0dd4dd32014-12-09 00:28:24 +0000414the name, source location, type, and since it's an argument, the argument
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000415index. Next, we create an ``lvm.dbg.declare`` call to indicate at the IR
Eric Christopher0dd4dd32014-12-09 00:28:24 +0000416level that we've got a variable in an alloca (and it gives a starting
Lang Hames59b0da82015-08-19 18:15:58 +0000417location for the variable), and setting a source location for the
Eric Christopher0dd4dd32014-12-09 00:28:24 +0000418beginning of the scope on the declare.
419
Eric Christopher05917fa2014-12-08 18:00:47 +0000420One interesting thing to note at this point is that various debuggers have
421assumptions based on how code and debug information was generated for them
422in the past. In this case we need to do a little bit of a hack to avoid
423generating line information for the function prologue so that the debugger
424knows to skip over those instructions when setting a breakpoint. So in
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000425``FunctionAST::CodeGen`` we add some more lines:
Eric Christopher05917fa2014-12-08 18:00:47 +0000426
427.. code-block:: c++
428
429 // Unset the location for the prologue emission (leading instructions with no
430 // location in a function are considered part of the prologue and the debugger
431 // will run past them when breaking on a function)
432 KSDbgInfo.emitLocation(nullptr);
433
434and then emit a new location when we actually start generating code for the
435body of the function:
436
437.. code-block:: c++
438
Mehdi Aminibb6805d2017-02-11 21:26:52 +0000439 KSDbgInfo.emitLocation(Body.get());
Eric Christopher05917fa2014-12-08 18:00:47 +0000440
Eric Christopher0dd4dd32014-12-09 00:28:24 +0000441With this we have enough debug information to set breakpoints in functions,
442print out argument variables, and call functions. Not too bad for just a
443few simple lines of code!
Eric Christopher05917fa2014-12-08 18:00:47 +0000444
445Full Code Listing
446=================
447
448Here is the complete code listing for our running example, enhanced with
449debug information. To build this example, use:
450
451.. code-block:: bash
452
453 # Compile
Eric Christophera8c6a0a2015-01-08 19:07:01 +0000454 clang++ -g toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core mcjit native` -O3 -o toy
Eric Christopher05917fa2014-12-08 18:00:47 +0000455 # Run
456 ./toy
457
458Here is the code:
459
Wilfred Hughes945f43e2016-07-02 17:01:59 +0000460.. literalinclude:: ../../examples/Kaleidoscope/Chapter9/toy.cpp
Eric Christopher05917fa2014-12-08 18:00:47 +0000461 :language: c++
462
Wilfred Hughes945f43e2016-07-02 17:01:59 +0000463`Next: Conclusion and other useful LLVM tidbits <LangImpl10.html>`_
Sean Silvad7fb3962012-12-05 00:26:32 +0000464