23.10.2013 Views

FAST Forth Native-Language Embedded Computers

FAST Forth Native-Language Embedded Computers

FAST Forth Native-Language Embedded Computers

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Table Two. Code generated for five words, and associated stack-state changes.<br />

dup (state = 0) ........................ MOV EAX, [EDI] ............. (new state = 1)<br />

dup (state = 1) ........................ MOV EBX, EAX .............. (new state = 4)<br />

dup (state = 12) ...................... SUB EDI, 4<br />

MOV [EDI], EBX<br />

MOV EDX, EBX ............ (new state = 13)<br />

drop (state = 0) ....................... ADD EDI, 4 ................( state unchanged)<br />

drop (state = 1) ....................... .......... (new state = 0)<br />

drop (state = 12) ..................... .......... (new state = 5)<br />

swap (state = 0) ..................... MOV EAX, [EDI]<br />

MOV EBX, [ED1]+4<br />

ADD EDI, 8 ..................... (new state = 6)<br />

swap (state = 1 ) ..................... MOV EBX, [EDI]<br />

(illustrated in Figure Two-a) ...... ADD EDI, 4 ..................... (new state = 6)<br />

swap (state = 12) ................... ........ (new state = 10)<br />

(illustrated in Figure Two-b)<br />

rot (state = 0) .......................... MOV EAX, [EDI]<br />

MOV EBX, [ED1]+4<br />

MOV EDX, [ED1]+8<br />

ADD EDI, 12 ................. (new state = 14)<br />

rot (state = 1) .......................... MOV EBX, [EDI]<br />

MOV EDX, [ED1]+4<br />

ADD EDI, 8 ................... (new state = 14)<br />

rot (state = 12) ........................ ........ (new state = 15)<br />

xor (state = 0) ......................... MOV EAX, [ED!]<br />

XOR EAX, [ED1]+4<br />

ADD EDI, 8 ..................... (new state = 1)<br />

xor (state = 1) ......................... XOR EAX, [EDI]<br />

(illustrated in Figure Two-c) ...... ADD EDI, 4 ................( state unchanged)<br />

xor (state = 12) ....................... XOR EAX, EBX .............. (new state = 5)<br />

(illustrated in Figure Two-d)<br />

Figure Two-a. Generated code and change-of-stack-state example for swap.<br />

1 1 EAX Top item EBX Top item<br />

\ri<br />

EAX<br />

(Pops one item<br />

from memory<br />

part of stack)<br />

[ED1+8] 5th item<br />

I J<br />

[ED1+12] 6th item<br />

reset to zero. Figure Four<br />

illustrates the virtual offset<br />

and its use.<br />

In the previous section,<br />

updates to the stack pointer<br />

(EDI) were generated in code<br />

for the sake of clarity. How-<br />

ever, in practice, equivalent<br />

modifications are made to<br />

the virtual offset at compile<br />

time, and the actual offsets<br />

used in address calculations<br />

(using the [EDIl+n address<br />

mode) are modified by the<br />

prior value of the virtual off-<br />

set. Table Three shows the<br />

same generated code as<br />

Table Two, but modified to<br />

accommodate a compile-<br />

time virtual offset; code for<br />

the exit word has been<br />

added to the table, in order<br />

to illustrate the code that is<br />

generated when the pointer<br />

register must be updated.<br />

(For all compiled <strong>Forth</strong><br />

words, I am assuming that<br />

stack state 1 is the state for all<br />

entries and exits; the virtual<br />

offset must be zero for all<br />

entries and exits.)<br />

N. Special<br />

Considerations for<br />

Stack-Top Op~tions<br />

The optimization tech-<br />

niques for the data stack<br />

require special handling<br />

when program branches<br />

and/or direct accesses to the<br />

data stack are present. There<br />

are three different aspects<br />

that must be considered<br />

when optimizing a <strong>Forth</strong> prc-<br />

gram with these techniques:<br />

1. When branch targets<br />

(and the sequential flow<br />

of execution) come to-<br />

gether at a common<br />

point, the stack states<br />

and the values of Voffs<br />

(the "virtual offset" for<br />

the data stack) must be<br />

"rectified so that a single,<br />

common stack state and<br />

Voffs value can be as-<br />

sumed by the compiler<br />

at that point, regardless<br />

of the source of each<br />

Stack state = 1 New stack state = 6 branch. This consider-<br />

ation applies both to<br />

<strong>Forth</strong> Dimensions 35 March 1994 April

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!