114 4 Logic Programming Languages
4.4 The Translation of Literals
Literals in PRO L are treated like procedure calls in imperative languages. Therefore,
a stack frame is allocated for their evaluation. Representations of terms are con-
structed in the heap that serve as actual parameters and references to these are stored
within this stack frame. Finally, control is transferred to the code for the procedure,
that is, the predicate. This results in the following translation scheme for literals:
code
G
q(t
1
,...,t
k
)
ρ
= mark B // allocate a stack frame
code
A
t
1
ρ
...
code
A
t
k
ρ
call q/k // call the procedure q/k
B : ...
Example 4.4.1 Consider the literal q
(a, X, g(
¯
X, Y
)). In an address environment
ρ
= {X → 1, Y → 2}, our translation scheme results in:
mark B putref 1 call q
/3
putatom a putvar 2 B : ...
putvar 1 putstruct g
/2
Before we implement the WiM instruction mark B, we must understand howa stack
frame of the W
IM is organized (Fig. 4.10). The simplest organization places first the
organizationalcellswithintheframe.Above them, the actual parametersof the literal
are allocated, possibly followed by further local variables of the selected clause. As
with the virtual machine for C, the parameters and local variables are addressed
relative to FP. As usual, we letthecurrent FP pointtothelastorganizationalcell.We
have already identified two organizational cells. One such cell is needed for storing
the current PC,thepositive continuation address, which is the location in the code
at which execution should continue if the literal has been completed successfully.
This is similar to the return address in the C-Machine. One further memory cell is
required for saving the contents of the register FP before the call. It turns out that,
for the implementation of backtracking, four further registers and, accordingly, four
more organizational cells are required.
Now we can implement the instruction mark B (Fig. 4.11). This instruction
allocates space for six organizational cells on the stack. Then, it saves the current FP
and saves B as the positive continuation address.
The instruction call q
/k makes the register FP point to the topmost organiza-
tional cell of the stack frame and calls the k-ary predicate q
/k (Fig. 4.12).