A calculation like “the factorial of a number” may be used several times in a large program. Subroutines allow this kind of functionality to be abstracted into a unit. It’s a benefit for code reuse and maintainability. Even though PASM is just an assembly language for a virtual processor, it has a number of features to support high-level subroutine calls. PIR offers a smoother interface to those features.
PIR provides several different sets of syntax for subroutine calls. This is a language designed to implement other languages, and every language does subroutine calls a little differently. What’s needed is a set of building blocks and tools, not a single prepackaged solution.
As we mentioned in Chapter 9, Parrot defines a set of calling conventions for externally visible subroutines. In these calls, the caller is responsible for preserving its own registers, and arguments and return values are passed in a predefined set of Parrot registers. The calling conventions use the Continuation Passing Style to pass control to subroutines and back again.
The fact that the
Parrot-calling conventions are
clearly defined also makes it possible to provide some higher-level
syntax for it. Manually setting up all the registers for each
subroutine call isn’t just tedious,
it’s also prone to bugs introduced by typos.
PIR’s simplest subroutine call syntax looks much
like a high-level language. This example calls the subroutine
_fact with two arguments and ...