Skip to content

Advanced IR Generation and IT Certification Exams

  • Contact Us
Close Menu

Adding support for multiple inheritance – IR Generation for High-Level Language Constructs-1

December 28, 2021December 28, 2021| Donna martinAdding support for multiple inheritance – IR Generation for High-Level Language Constructs-1| 0 Comment| 03:16

Categories :
  • Adding debug metadata
  • ITCertification Exams
  • Raising an exception
  • The LLVM pass manager

Multiple inheritance adds another challenge. If a class inherits from two or more base classes, then we need to combine the data members in such a way that they are still accessible from the methods. Like in the single inheritance case, the solution is to append all data members, including the hidden vtable pointers.

The Circle class is not only a geometric shape but also a graphic object. To model this, we let the Circle class inherit from the Shape class and the GraphicObj class. In the class layout, the fields from the Shape class come first. Then, we append all fields of the GraphicObj class, including the hidden vtable pointer. After that, we add the new data members of the Circle class, resulting in the overall structure shown in Figure 5.4:

Figure 5.4 – Layout of classes and vtables with multiple inheritance

This approach has several implications. There can now be several pointers to the object. A pointer to the Shape or Circle class points to the top of the object, while a pointer to a GraphicObj class points to inside this object, the beginning of the embedded GraphicObj object. This has to be taken into account when comparing pointers.

Calling a virtual method is also affected. If a method is defined in the GraphicObj class, then this method expects the class layout of the GraphicObj class. If this method is not overridden in the Circle class, then there a two possibilities. The easy case is if the method call is done with a pointer to a GraphicObj instance: in this case, you look up the address of the method in the vtable of the GraphicObj class and call the function. The more complicated case is if you call the method with a pointer to the Circle class. Again, you can look up the address of the method in the vtable of the Circle class. The called method expects a this pointer to be an instance of the GraphicObj class, so we have to adjust that pointer, too. We can do this because we know the offset of the GraphicObj class inside the Circle class.

If a GrapicObj method is overridden in the Circle class, then nothing special needs to be done if the method is called through a pointer to the Circle class. However, if the method is called through a pointer to a GraphicObj instance, then we need to make another adjustment because the method needs a this pointer pointing to a Circle instance. At compilation time, we cannot compute this adjustment because we do not know whether or not this GraphicObj instance is part of a multiple inheritance hierarchy. To solve this, we store the adjustment we need to make to the this pointer before calling the method together with each function pointer in the vtable, as in Figure 5.5:

Figure 5.5 – vtable with adjustments to the this pointer

A method call now becomes the following:

  1. Look up the function pointer in the vtable.
  2. Adjust the this pointer.
  3. Call the method.

Post navigation

Previous page Extending single inheritance with interfaces – IR Generation for High-Level Language Constructs
Next page Adding support for multiple inheritance – IR Generation for High-Level Language Constructs-2

Leave a Reply Cancel reply

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

Related Post

Adding debug support to tinylang – Advanced IR Generation-1

January 8, 2024January 8, 2024 Donna martinAdding debug support to tinylang – Advanced IR Generation-1

We encapsulate the generation of debug metadata in the new CGDebugInfo class. Additionally, we place [...]

Read MoreRead More

Adding TBAA metadata to tinylang – Advanced IR Generation-1

April 29, 2023April 29, 2023 Donna martinAdding TBAA metadata to tinylang – Advanced IR Generation-1

To support TBAA, we must add a new CGTBAA class. This class is responsible for [...]

Read MoreRead More

Understanding the need for additional metadata – Advanced IR Generation-1

December 27, 2022December 27, 2022 Donna martinUnderstanding the need for additional metadata – Advanced IR Generation-1

To demonstrate the problem, let’s look at the following function:void doSomething(int *p, float *q) {  *p [...]

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