Ophis/book/x1008.html
2014-05-25 01:46:17 -07:00

258 lines
4.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>VTables and Object-Oriented Assembler</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="Programming with Ophis"
HREF="book1.html"><LINK
REL="UP"
TITLE="Functionals"
HREF="c953.html"><LINK
REL="PREVIOUS"
TITLE="Dispatch-on-type and Data-Directed Assembler"
HREF="x992.html"><LINK
REL="NEXT"
TITLE="A final reminder"
HREF="x1029.html"></HEAD
><BODY
CLASS="SECTION"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Programming with Ophis</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x992.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Functionals</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x1029.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECTION"
><H1
CLASS="SECTION"
><A
NAME="AEN1008"
>VTables and Object-Oriented Assembler</A
></H1
><P
> The usual technique for getting something that looks
object-oriented in non-object-oriented languages is to fill a
structure with function pointers, and have those functions take
the structure itself as an argument. This works just fine in
assembler, of course (and doesn't really require anything more
than your traditional jump-indirects), but it's also possible to
use a lot of the standard optimizations that languages such as C++
provide.
</P
><P
> The most important of these is the <I
CLASS="EMPHASIS"
>vtable</I
>.
Each object type has its own vtable, and it's a list of function
pointers for all the methods that type provides. This is a space
savings over the traditional structs-with-function-pointers
approach because when you have many objects of the same class, you
only have to represent the vtable once. So that all objects may
be treated identically, the vtable location is traditionally fixed
as being the first entry in the corresponding structure.
</P
><P
> Virtual method invocation takes an object pointer (traditionally
called <TT
CLASS="LITERAL"
>self</TT
> or <TT
CLASS="LITERAL"
>this</TT
>) and a
method index and invokes the approprate method on that object.
Gee, where have we seen that before?
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>sprite'vtable:
jsr do'jump'table
.word sprite'init, sprite'update, sprite'render</PRE
></TD
></TR
></TABLE
><P
> We mentioned before that vtables are generally the first entries
in objects. We can play another nasty trick here, paying an
additional byte per object to have the vtable be not merely a
pointer to its vtable routine, but an actual jump instruction to
it. (That is, if an object is at location X, then location X is
the byte value <TT
CLASS="LITERAL"
>$4C</TT
>,
representing <TT
CLASS="LITERAL"
>JMP</TT
>, location X+1 is the low byte
of the vtable, and location X+2 is the high byte of the vtable.)
Given that, our <TT
CLASS="LITERAL"
>invokevirtual</TT
> function becomes
very simple indeed:
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>invokevirtual:
sta this
stx this+1
jmp (this)</PRE
></TD
></TR
></TABLE
><P
> Which, combined with all our previous work here, takes
the <TT
CLASS="LITERAL"
>this</TT
> pointer in <TT
CLASS="LITERAL"
>.AX</TT
> and
a method identifier in <TT
CLASS="LITERAL"
>.Y</TT
> and invokes that
method on that object. Arguments besides <TT
CLASS="LITERAL"
>this</TT
>
need to be set up before the call
to <TT
CLASS="LITERAL"
>invokevirtual</TT
>, probably in some global
argument array somewhere as discussed back in <A
HREF="c680.html"
>the Chapter called <I
>Structured Programming</I
></A
>.
</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x992.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="book1.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x1029.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Dispatch-on-type and Data-Directed Assembler</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c953.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>A final reminder</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>