Parrot intermediate representation
Encyclopedia
The Parrot intermediate representation (PIR), previously called Intermediate code (IMC), is one of the two assembly language
Assembly language
An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture...

s for the Parrot virtual machine
Parrot virtual machine
Parrot is a register-based process virtual machine designed to run dynamic languages efficiently. It uses just-in-time compilation for speed to reduce the interpretation overhead. It is currently possible to compile Parrot assembly language and PIR to Parrot bytecode and execute it...

. The other is Parrot assembly language
Parrot assembly language
The Parrot assembly language is the basic assembly language used by the Parrot virtual machine.PASM is the lowest level assembly language in the Parrot stack...

 or PASM. Compared to PASM, PIR exists at a slightly higher abstraction layer
Abstraction layer
An abstraction layer is a way of hiding the implementation details of a particular set of functionality...

, and provides temporary registers and named registers, simplifying code generation.

While Parrot is still evolving, it is currently being used in many different capacities, and has undergone several releases.

Overview

PIR provides a set of abstractions that allow the programmer to ignore certain redundancies in the Parrot bytecode
Bytecode
Bytecode, also known as p-code , is a term which has been used to denote various forms of instruction sets designed for efficient execution by a software interpreter as well as being suitable for further compilation into machine code...

 and quickly write code that adheres to the complexities of Parrot, such as the calling conventions.

Abstractions

PIR provides both type abstraction and polymorphism
Type polymorphism
In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. The concept of parametric polymorphism applies to both data types and functions...

 to some degree. For example, the "+" operator can be used with int, num or both:

.local int a
.local num b
a = 1
b = 1.1
.local num c
c = a + b

Calling conventions

The calling conventions in Parrot are complex, but all of that complexity can be hidden by using PIR directives:

.sub foo
.param int a
.param int b
.local int tmp
tmp = a + b
.return (tmp)
.end

Each of the directives prefixed with a "." expands to the required Parrot bytecode, but does not directly represent any fundamental Parrot operation.

Example

The hello world program
Hello world program
A "Hello world" program is a computer program that outputs "Hello world" on a display device. Because it is typically one of the simplest programs possible in most programming languages, it is by tradition often used to illustrate to beginners the most basic syntax of a programming language, or to...

 in PIR is

.sub hello :main
print "Hello world!\n"
.end


If the program is saved as hello.pir, it can be compiled
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...

 and executed with this command: parrot hello.pir

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK