Method and system for constructing hybrid virtual function...

Data processing: software development – installation – and managem – Software program development tool – Translation of code

Reexamination Certificate

Rate now

  [ 0.00 ] – not rated yet Voters 0   Comments 0

Details

Reexamination Certificate

active

06182282

ABSTRACT:

FIELD OF THE INVENTION
This invention relates to compilers used in computer programming languages. In particular, this invention relates to a method and system for constructing virtual functions within a hierarchy of classes where the classes can be compiled using different virtual function table structures.
BACKGROUND OF THE INVENTION
Object-oriented programming languages, such as C++ and Java, share several common properties. Generally, they support “encapsulation”, or the binding of functions and data structures. Encapsulation allows the creation of classes which combine data members and member functions. For example, a user can define a class Circle:
class Circle {
int x;
int y;
int radius;
void DrawCircle ( );
};
The data members of the class Circle are x, y, and radius. The member function of the class is DrawCircle ( ).
“Inheritance” is the second property shared by object-oriented programming languages. Inheritance allows a user to define a derived class which inherits the data and function members of one or more previously defined classes. For example, a class ShadedCircle can be defined to inherit from previously defined class Circle:
class ShadedCircle : Circle {
int Colour;
void ShadeCircle ( );
};
Class ShadedCircle inherits the data and function members from Circle and introduces new data member Colour, and new function member ShadeCircle. A hierarchy of classes can be developed, each deriving data and function members from previously defined classes. If a class derives data and function members from more than one class in a hierarchy, it is an example of multiple inheritance.
The final property shared by true object-oriented programming languages is “polymorphism”, or late binding. Polymorphism allows a function or operator to be shared up and down a class hierarchy, with each class in the hierarchy implementing the function or operator in a manner appropriate to itself. In C++, polymorphism is accomplished by declaring a function “virtual”. A virtual function can be overridden by a function of the same name and type in a derived class. For example, the function DrawCircle is declared to be virtual in classes Circle and ShadedCircle:
class Circle {
int x;
int y;
int radius;
virtual void DrawCircle( );
};
class ShadedCircle : Circle {
int Colour;
virtual void DrawCircle( );
};
If objects a and b are defined to be objects of type class Circle and type class ShadedCircle, respectively:
Circle a;
ShadedCircle b; then the statement:
a.DrawCircle ( ) refers to the function DrawCircle as defined in class Circle; and
b.DrawCircle ( ) refers to the function DrawCircle as defined in class ShadedCircle.
Encapsulation, inheritance and polymorphism allow the development of modular, maintainable and portable code. Ideally, classes and functions developed for one application can be re-used in subsequent applications.
A virtual function table (“VFT”) is a compiler-created data structure used to support virtual function dispatch in object-oriented application programs. The VFT provides a technique to implement the two aspects of calling a virtual function at run time: which function to call, and how to adjust the value of a “this” pointer, as described below. Under an implementation commonly employed in early C++ compilers, each class having access to a virtual function member has an associated VFT which lists the addresses of each virtual function, in order of declaration, and an adjustment to the this pointer. Each object in a class contains a pointer to the VFT. The pointer to the VFT is initialized with the address of the associated virtual function table when the object is instantiated. To invoke a virtual function, the compiler generates code which accesses the virtual function member through the VFT.
Generally, a derived class inherits a copy of the VFT(s) associated with its base classes. A derived class that introduces a virtual function member has its own VFT which can include the entries from inherited VFT's from its base classes and entries for any newly introduced virtual functions.
For example, if derived class ShadedCircle introduces a new virtual function StretchCircle( ):
class ShadedCircle : Circle {
int Color;
virtual void DrawCircle( );
virtual void StretchCircle( );
};
the associated VFT for class Circle would be:
&Circle :: DrawCircle( )
0
where &Circle :: DrawCircle( ) is the address of virtual function DrawCircle( ) for class Circle and the adjustment to the this pointer is zero. Class ShadedCircle would inherit the VFT for DrawCircle( ) from class Circle, and would construct a VFT for virtual function StretchCircle( ), resulting in two VFT's, the first containing the address to virtual function DrawCircle( ) and a zero adjustment to the this pointer, the second containing the address of virtual function StretchCircle( ) and an adjustment to the this pointer, again zero in this example:
&ShadedCircle :: DrawCircle( )
0
&ShadedCircle :: StretchCircle( )
0
In C++, the this pointer points to the object for which a function is called. Generally, the this pointer is passed to the function as a hidden argument. The this pointer in the case of a call can assume two values. When called, the this pointer is set to point within the object to the base class where it was introduced. The this pointer is the address of the occurrence of the introducing class in the object associated with the call. When a function is overridden, the overriding function must adjust the this pointer by the offset between the address of the object and the address of the occurrence of the introducing class within the object. The adjustment, commonly known as a “this adjustment”, permits the overriding function to access the entire object of the class in which it was declared.
As discussed above, for reasons of backward compatibility, for example CFRONT™ compatibility, many compilers employ VFT's which contain a virtual function address and a this pointer adjustment for each virtual function inherited or introduced by a class. Calling a virtual function through a VFT constructed in such a manner has two significant drawbacks. First, adjustment of the this pointer occurs for every call, even when the required adjustment is zero. Secondly, even when the adjustment is zero, the space for an adjustment is reserved in the table, making the tables potentially quite large. Examination of typical C++ programs shows that the majority of adjustments are, in fact, zero, such as in cases of single inheritance and for classes on the left leg of a multiple inheritance hierarchy, as discussed below in greater detail. As a result, the code for calling a virtual function is larger and slower than may otherwise be necessary.
A more efficient VFT structure and method was subsequently, developed, employing an adjustor routine, commonly known as a “thunk”. The term “thunk” is a common term in the art to denote the activities that take place in moving from one environment to another in a mixed environment program in the case described here, each VFT entry contains one pointer, if there is a non-zero adjustment required, then the pointer is the address of a thunk that adjusts the this pointer and then jumps to the function to be called. The advantages of the adjustor thunk implementation for calling virtual functions are that the cost of the this adjustment, in terms of both time and memory requirements, is only paid when a non-zero adjustment is required, and each entry for a virtual function in its associated VFT's contains only one member, either the address of the function or the address of a thunk for a function having a non-zero this pointer adjustment. This results in shorter, faster code and VFT's which are half the size as in the first method described above.
However, there are still problems associated with the prior art VFT structures and virtual function call methods. For example, the VFT's for virtual functions compiled by compilers using the first method are not compatibl

LandOfFree

Say what you really think

Search LandOfFree.com for the USA inventors and patents. Rate them and share your experience with other people.

Rating

Method and system for constructing hybrid virtual function... does not yet have a rating. At this time, there are no reviews or comments for this patent.

If you have personal experience with Method and system for constructing hybrid virtual function..., we encourage you to share that experience with our LandOfFree.com community. Your opinion is very important and Method and system for constructing hybrid virtual function... will most certainly appreciate the feedback.

Rate now

     

Profile ID: LFUS-PAI-O-2517956

  Search
All data on this website is collected from public sources. Our data reflects the most accurate information available at the time of publication.