Search code examples
pythonlanguage-designlanguage-theorylanguage-implementation

what exactly is a "register machine"?


From http://code.google.com/p/unladen-swallow/wiki/ProjectPlan I quote:

"Using a JIT will also allow us to move Python from a stack-based machine to a register machine, which has been shown to improve performance in other similar languages (Ierusalimschy et al, 2005; Shi et al, 2005)."

In college I built a simple compiler for a language with recursive procedures - which maintained stack frames for each procedure called - so that they can be called recursively and so that parameters and return values would work....

2 things:

1) Am I right in thinking that what I implemented would be considered a "stack-based machine" given the terminology used in the quotation above?

2) If my assumption in point (1) was right, how does a "register machine" work? i.e. how is it different from a stack-based machine?

Thanks!


Solution

  • A register machine is a hardware or software unit that when working with data takes it from memory, puts it in a location where it can work with it quickly, and then returns the result.

    For example a regular CPU is a register machine. Since the ALU (the unit that works with numbers in a CPU) can only work with numbers in a register.

    A stack based machine adds the data onto a stack and then either pops or pushes stuff onto it.

    For example, adding two numbers would be

    Push 2 // Push 2 onto the stack
    Push 3 // Push 3 onto the stack
    Add // Add the top two things on the stack.
    

    When in a register machine it would be something like this.

    Load x, r0 // Load x onto register 0
    Load y, r1 // Load y onto register 1
    Add r0, r1, r2 // Add 1 and 2 and store the result in register 2