Skip to content

Advanced IR Generation and IT Certification Exams

  • Contact Us
Close Menu

Adding debug metadata – Advanced IR Generation-3

August 18, 2023August 18, 2023| Donna martinAdding debug metadata – Advanced IR Generation-3| 0 Comment| 03:50

Categories :
  • Adding debug metadata
  • Adding line numbers
  • Catching an exception
  • ITCertification Exams
  • The LLVM pass manager

To create the debug metadata for the function, we have to create a type for the signature first, and then the metadata for the function itself. This is similar to the creation of IR for a function. The signature of the function is an array with all the types of parameters in source order and the return type of the function as the first element at index 0. Usually, this array is constructed dynamically. In our case, we can also construct the metadata statically. This is useful for internal functions, such as for module initializing. Typically, the parameters of these functions are always known, and the compiler writer can hard-code them:


llvm::Metadata *DbgSigTy = {DbgIntTy};
llvm::DITypeRefArray DbgParamsTy =
                      DBuilder.getOrCreateTypeArray(DbgSigTy);
llvm::DISubroutineType *DbgFuncTy =
                   DBuilder.createSubroutineType(DbgParamsTy);

Our function has the INTEGER return type and no further parameters, so the DbgSigTy array only contains the pointer to the metadata for this type. This static array is turned into a type array, which is then used to create the type for the function.

The function itself requires more data:


unsigned LineNo = 5;
unsigned ScopeLine = 5;
llvm::DISubprogram *DbgFunc = DBuilder.createFunction(
      DbgCU, “Func”, “_t4File4Func”, DbgFile, LineNo,
      DbgFuncTy, ScopeLine, llvm::DISubprogram::FlagPrivate,
      llvm::DISubprogram::SPFlagLocalToUnit);

A function belongs to a compilation unit, which in our case is stored in the DbgCU variable. We need to specify the name of the function in the source file, which is Func, and the mangled name is stored in the object file. This information helps the debugger locate the machine code of the function. The mangled name, based on the rules of tinylang, is _t4File4Func. We also have to specify the file that contains the function.

This may sound surprising at first, but think of the include mechanism in C and C++: a function can be stored in a different file, which is then included with include in the main compilation unit. Here, this is not the case and we use the same file as the one the compilation unit uses. Next, the line number of the function and the function type are passed. The line number of the function may not be the line number where the lexical scope of the function begins. In this case, you can specify a different ScopeLine. A function also has protection, which we specify here with the FlagPrivate value to indicate a private function. Other possible values for function protection are FlagPublic and FlagProtected, for public and protected functions, respectively.

Besides the protection level, other flags can be specified here. For example, FlagVirtual indicates a virtual function and FlagNoReturn indicates that the function does not return to the caller. You can find the complete list of possible values in the LLVM include file – that is, llvm/include/llvm/IR/DebugInfoFlags.def.

Lastly, flags specific to a function can be specified. The most commonly used flag is the SPFlagLocalToUnit value, which indicates that the function is local to this compilation unit. The MainSubprogram value is also used often, indicating that this function is the main function of the application. The LLVM include file mentioned previously also lists all possible values related to flags specific to functions.

So far, we have only created the metadata referring to static data. Variables are dynamic, so we’ll explore how to attach the static metadata to the IR code for accessing variables in the next section.

Post navigation

Previous page Adding debug metadata – Advanced IR Generation-2
Next page Tracking variables and their values – Advanced IR Generation-1

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Post

Tracking variables and their values – Advanced IR Generation-1

September 2, 2023September 2, 2023 Donna martinTracking variables and their values – Advanced IR Generation-1

To be useful, the type metadata described in the previous section needs to be associated [...]

Read MoreRead More

Adding line numbers – Advanced IR Generation

December 29, 2023December 29, 2023 Donna martinAdding line numbers – Advanced IR Generation

A debugger allows a programmer to step through an application line by line. For this, [...]

Read MoreRead More

Adding debug metadata – Advanced IR Generation-1

June 20, 2023June 20, 2023 Donna martinAdding debug metadata – Advanced IR Generation-1

To allow source-level debugging, we have to add debug information. Support for debug information in [...]

Read MoreRead More

Search

Dropdown Categories

Archives

  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • December 2021
  • November 2021

Meta

  • Log in

Tag Cloud

Back to Top

Cookie Policy | Terms | Privacy | About Us | © Sherrieland