llvm.org GIT mirror llvm / 603fc8c
docs: Update Kaleidoscope for recent DI changes This has been bit-rotting, so fix it up. I'll have to edit this again once the MD* classes have been renamed to DI* -- I'll try to remember to do that with the commit that renames them. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235244 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 4 years ago
1 changed file(s) with 30 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
186186 static DIBuilder *DBuilder;
187187
188188 struct DebugInfo {
189 DICompileUnit TheCU;
190 DIType DblTy;
191
192 DIType getDoubleTy();
189 MDCompileUnit *TheCU;
190 MDType *DblTy;
191
192 MDType *getDoubleTy();
193193 } KSDbgInfo;
194194
195 DIType DebugInfo::getDoubleTy() {
195 MDType *DebugInfo::getDoubleTy() {
196196 if (DblTy.isValid())
197197 return DblTy;
198198
244244
245245 .. code-block:: c++
246246
247 DIFile Unit = DBuilder->createFile(KSDbgInfo.TheCU.getFilename(),
248 KSDbgInfo.TheCU.getDirectory());
249
250 giving us a DIFile and asking the ``Compile Unit`` we created above for the
247 MDFile *Unit = DBuilder->createFile(KSDbgInfo.TheCU.getFilename(),
248 KSDbgInfo.TheCU.getDirectory());
249
250 giving us an MDFile and asking the ``Compile Unit`` we created above for the
251251 directory and filename where we are currently. Then, for now, we use some
252252 source locations of 0 (since our AST doesn't currently have source location
253253 information) and construct our function definition:
254254
255255 .. code-block:: c++
256256
257 DIDescriptor FContext(Unit);
257 MDScope *FContext = Unit;
258258 unsigned LineNo = 0;
259259 unsigned ScopeLine = 0;
260 DISubprogram SP = DBuilder->createFunction(
260 MDSubprogram *SP = DBuilder->createFunction(
261261 FContext, Name, StringRef(), Unit, LineNo,
262262 CreateFunctionType(Args.size(), Unit), false /* internal linkage */,
263 true /* definition */, ScopeLine, DIDescriptor::FlagPrototyped, false, F);
264
265 and we now have a DISubprogram that contains a reference to all of our metadata
266 for the function.
263 true /* definition */, ScopeLine, DebugNode::FlagPrototyped, false, F);
264
265 and we now have an MDSubprogram that contains a reference to all of our
266 metadata for the function.
267267
268268 Source Locations
269269 ================
329329 .. code-block:: c++
330330
331331 void DebugInfo::emitLocation(ExprAST *AST) {
332 DIScope *Scope;
332 MDScope *Scope;
333333 if (LexicalBlocks.empty())
334 Scope = &TheCU;
334 Scope = TheCU;
335335 else
336336 Scope = LexicalBlocks.back();
337337 Builder.SetCurrentDebugLocation(
338 DebugLoc::get(AST->getLine(), AST->getCol(), DIScope(*Scope)));
338 DebugLoc::get(AST->getLine(), AST->getCol(), Scope));
339339 }
340340
341341 that both tells the main ``IRBuilder`` where we are, but also what scope
346346
347347 .. code-block:: c++
348348
349 std::vector LexicalBlocks;
350 std::map FnScopeMap;
351
352 and keep a map of each function to the scope that it represents (a DISubprogram
353 is also a DIScope).
349 std::vector LexicalBlocks;
350 std::map FnScopeMap;
351
352 and keep a map of each function to the scope that it represents (an
353 MDSubprogram is also an MDScope).
354354
355355 Then we make sure to:
356356
391391
392392 .. code-block:: c++
393393
394 DIScope *Scope = KSDbgInfo.LexicalBlocks.back();
395 DIFile Unit = DBuilder->createFile(KSDbgInfo.TheCU.getFilename(),
396 KSDbgInfo.TheCU.getDirectory());
397 DIVariable D = DBuilder->createLocalVariable(dwarf::DW_TAG_arg_variable,
398 *Scope, Args[Idx], Unit, Line,
399 KSDbgInfo.getDoubleTy(), Idx);
394 MDScope *Scope = KSDbgInfo.LexicalBlocks.back();
395 MDFile *Unit = DBuilder->createFile(KSDbgInfo.TheCU.getFilename(),
396 KSDbgInfo.TheCU.getDirectory());
397 MDLocalVariable D = DBuilder->createLocalVariable(
398 dwarf::DW_TAG_arg_variable, Scope, Args[Idx], Unit, Line,
399 KSDbgInfo.getDoubleTy(), Idx);
400400
401401 Instruction *Call = DBuilder->insertDeclare(
402402 Alloca, D, DBuilder->createExpression(), Builder.GetInsertBlock());
403 Call->setDebugLoc(DebugLoc::get(Line, 0, *Scope));
403 Call->setDebugLoc(DebugLoc::get(Line, 0, Scope));
404404
405405 Here we're doing a few things. First, we're grabbing our current scope
406406 for the variable so we can say what range of code our variable is valid