Handout No. 7
CS 4110 Compiler Design
Istvan Simon
Describes the PL ideal abstract machine
Other Handouts
back
next
notes
The PL abstract machine is very similar to The
Abstract Machine described in the notes. This is not surprising,
since PL is also a block structured language, just like Pascal is. PL is
however simpler than Pascal, and this is reflected in the PL abstract machine
which is also simpler.
Architecture
The architecture of the PL machine is identical to that of
the Pascal machine. It's memory consists of an array of integer words,
addressed from 1 to some maximum. It has the same three registers
that the Pascal machine has, namely pc, arp, and top,
and these are used the same way.
Instruction Set
We describe briefly each instruction of the PL machine. Each
item starts with the integer opcode of the instruction, followed by its
assembly language representation, followed by a brief description of the
instruction. To define the opcodes in your compiler, define an enumerated
type with the names of the instructions suffixed by 4, e.g. Add4, And4,
Arrow4, etc. in alphabetical order. PL
has 27 instructions, so the opcodes will go from 0 to 26.
-
0 Add Pops two
integers , adds them, pushes back the result.
-
1 And Pops two booleans,
ands them, pushes back the result. PL uses the short-circuit evaluation
of the two booleans: if the first operand is true then the
result is the second operand, otherwise the result is false. Note
that the first operand is the second one popped from the stack.
-
2 Arrow (Address) This
is a conditional jump on false. It pops a boolean and jumps
to Address if it is false.
-
3 Assign(n) This
instruction expects n addresses and n values on the stack.
It pops the addresses and the values, and stores each value at the corresponding
address.
-
4 Bar(Address) This
is an unconditional jump to the Address.
-
5 Call(Level, Address)
Constructs the context part of the activation record of the called
procedure then jumps to the Address. Level is the relative
level of the procedure name from the point where it is called.
-
6 Constant(Value)
Pushes the Value on the stack.
-
7 Divide Pops
two integers, divides the first operand (the second popped) by the second
operand, and pushes back the result.
-
8 EndProc Returns
from a procedure. Pops the activation record of the returning procedure
off the stack, restores the value of arp to the dynamic link, and
jumps back to the return address stored in the activation record.
-
9 EndProg Halts
execution.
-
10 Equal Pops
two values off the stack and pushes back true if they are equal
and false otherwise.
-
11 Fi (LineNo) Prints
a run-time error message that the if-statement on line LineNo failed
(because all
guards were false).
-
12 Greater
Pops two values and pushes back true if the fisrt operand is
greater than the second, or false otherwise .
-
13 Index(Upper,
LineNo) Pops an address and an index value off the
stack, checks if the index value is between 1 and Upper, pushes
back the address of the indexed element if it is, or reports that an indexing
error occurred on line LineNo of the source code.
-
14 Less
Pops two values off the stack and pushes back true if
the first operand is smaller than the second, or false otherwise.
-
15 Minus
Pops a value and pushes back the same value with opposite sign.
-
16 Modulo
Pops two values, pushes back the remainder of the division of the first
operand by the second.
-
17 Multiply
Pops two values, pushes back their product.
-
18 Not
Pops a boolean value, pushes back its complement.
-
19 Or
Pops two boolean values and pushes back the short-circuit evaluation
of their or.
-
20 Proc(VarLength,Address)
Allocates space for the local variables of a called procedure and jumps
to the Address. VarLength is the number of words allocated
on the stack.
-
21 Prog(VarLength,
Address) Pushes the activation record of the main
block on the stack, initializes apr, and jumps to the Address.
-
22 Read(n)
Pops n addresses, reads n integers, and stores
them at the addresses popped.
-
23 Subtract
Pops two values off the stack, subtracts the second operand from the
first, and pushes back the result on the stack.
-
24 Value
Pops an address off the stack, and pushes back the value stored at
that address.
-
25 Variable(Level,Displacement)
Pushes the absolute address of the variable whose relative address
within its activation record is Displacement . Level
is the relative level of nesting of the variable name from the
current block. Thus, it is 0 for the current block, 1 for the block
surrounding the current block, 2 for the block surrounding that one, and
so on.
-
26 Write(n)
Pops n values off the stack and prints them.
If you have comments
or suggestions, email me at simon@csuhayward.edu