RE: [Question] Code Generation of OOP languages

"Mikael Lyngvig" <mikael@pobox.com>
3 Dec 1999 01:09:06 -0500

          From comp.compilers

Related articles
RE: [Question] Code Generation of OOP languages mikael@pobox.com (Mikael Lyngvig) (1999-12-03)
Re: [Question] Code Generation of OOP languages ghoul@ibm.net (Mark van Gulik) (1999-12-20)
| List of all articles for this month |

From: "Mikael Lyngvig" <mikael@pobox.com>
Newsgroups: comp.compilers
Date: 3 Dec 1999 01:09:06 -0500
Organization: Compilers Central
Keywords: OOP, code

> I want to know about the code generation of OOP languages.
>
> How do OOP languages ,such as C++ & JAVA, translate
> source code - with OO concept - into
> binary or intermediate(like assembly) code - without OO concept ?
>
> Any useful references(book or website) about this topic?


No references, just a brief description. It really is pretty simple
(although it becomes complex when all the features of typical OOP
languages have to be taken into consideration). The key issue, IMHO,
is to think of it in plain C operations. Translating from C to
assembly is usually fairly straightforward.


First, think of a class as a C structure or Pascal record with
associated functions.


So:


class cFoo
{
public:
      cFoo(void);
      void Assign(int Value);


private:
      int Number;
};


cFoo::cFoo(void) :
      Number(0)
{
}


void cFoo::Assign(int Value)
{
      Number = Value;
}


Becomes:


typedef struct
{
      int Number;
} cFoo;


void cFooConstructor(cFoo *this)
{
      this->Number = 0;
}


void cFooAssign(cFoo *this, int Value)
{
      this->Number = Value;
}


Virtual methods are really just function pointers typically packaged
in a space-saving way. The function pointers are set up by the
constructor:


class cFoo
{
public:
      void cFoo(void);
      virtual void Assign(int Value);


private:
      int Number;
};


Becomes something like:


typedef void (*cFooAssignPtr)(int Value); /* function pointer */
typedef struct
{
      cFooAssignPtr AssignPtr;
} cFooVtable;
typedef struct
{
      cFooVtable *__vtable;
      int Number;
} cFoo;


static const cFooVtable = // a single global vtable shared by all instances
{
      cFooAssignBody
};




void cFooConstructor(cFoo *this)
{
      this->__vtable = &cFooVtable;
      this->Number = 0;
}




void cFooAssign(cFoo *this, int Value)
{
      this->__vtable->AssignPtr(this, Value);
}




void cFooAssignBody(cFoo *this, int Value)
{
      this->Number = Value;
}


(Non-virtual) inheritance is just a matter of defining a new
structure, which (as its first member) includes an instance of the
base class and inserting the appropriate type-casts when needed:


// cFoo: same interface and implementation as before


class cBar :
      public cFoo
{
public:
      cBar(void);
      virtual void Assign(int Value); // inherited virtual
      virtual void Clear(void); // introduced virtual


private:
      int Count; // introduced data member
};




cBar::cBar(void) :
      Count(0)
{
}


void cBar::Assign(int Value)
{
      cFoo::Assign(Value);
      Count += 1;
}


void cBar::Clear(void)
{
      Count = 0;
}


Becomes:


// cFoo: same code as in the previous example


// cBar declarations
typedef void (*cBarClearPtr)(void);
typedef struct
{
      cFooAssignPtr AssignPtr;
      cBarClearPtr ClearPtr;
} cBarVtable;


void cBarAssignBody(cBar *this, int Number);
void cBarClearBody(cBar *this);
static const cFooVtable =
{
      cBarAssignBody,
      cBarClearBody
};


typedef struct
{
      cBarVtable *__vtable;
      cFoo __base;
      int Count;
} cBar;


void cBarConstructor(cBar *this)
{
      this->__vtable = &cBarVtable;
      cFooConstructor(&this->__base);
      this->Count = 0;
}


void cBarAssignBody(cBar *this, int Value)
{
      cFooAssignBody(&this->__base, Value);
      this->Count += 1;
}


void cBarClearBody(cBar *this)
{
      this->__vtable->ClearPtr(this);
}


The code above hasn't been tested (compiled and executed), but it
should give you a good idea of how to get from C++ to C. Getting from
C to assembly is just a question of traditional compiler technology
and is described in lots of books.




-- Mikael


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.