[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [DotGNU]Register based bytecode stuff...

From: Rhys Weatherley
Subject: Re: [DotGNU]Register based bytecode stuff...
Date: Thu, 02 May 2002 15:04:20 +1000

James Michael DuPont wrote:

> Would it be feasable to translate Parrot code that is
> written to use many registere into IL Code that is
> dependent on a Stack?
> I imagine it would be difficult.
> Maybe i am missing something?

Parrot code that looks like this:

    add x, y, z   (i.e. x = y + z)

would be converted into:

    ldloc y
    ldloc z
    stloc x

where the Parrot registers x, y, and z are mapped to local

This is a bit verbose, and would be a pain on more complex
expressions.  A smarter code generator would try to analyse
the register code a little to figure out the dependencies.
e.g. consider x = y * z + w:

    mul t, y, z
    add x, t, w

where t is a temporary register.  Niave code generation
would produce:

    ldloc y
    ldloc z
    stloc t
    ldloc t
    ldloc w
    stloc x

A little bit of dynamic flow analysis will show that the value
in "t" is unused after the "mul" instruction.  This allows the
code generator to remove the "stloc t, ldloc t" pair to give the
correct stack machine code:

    ldloc y
    ldloc z
    ldloc w
    stloc x

A little more data flow analysis will also avoid the need to copy
values from Parrot local variables into temporary registers and
back again.

A little lateral thinking is required, but it isn't terriby difficult.
The "Code Generation" chapter of the Dragon Book contains
a number of efficient data flow analysis algorithms that can
be used to track which registers are "live" at any given point,
to eliminate unnecessary copies.

The interesting thing is that once the stack machine code is
converted into machine code using a decently-written JIT,
all of the inefficiencies in the stack machine instructions
will be removed and you will be back to efficient register
machine code for the target CPU.  Presto!



reply via email to

[Prev in Thread] Current Thread [Next in Thread]