This paper describes the design and implementation of OPTIMA, a Portable optimizer for the PASCAL-P4 compiler. The PASCAL-P4 compiler generates code, called PCODE, for a hypothetical stack computer. The stack computer is easy to simulate or to emulate on a wide range of computer architectures. However, the code produced for it by PASCAL-P4 is unoptimized and inefficient when emulated on conventional computers. OPTIMA improves the code which is produced by the compiler. It has three levels of optimization: PCODE transformations, TUPLE transformations and Code generation. Programs processed by the optimizer are substantially smaller and faster than before optimization. OPTIMA was implemented for the Weizmann Institute of Science GOLEM-B computer.The average program spends a significant portion of its running time calculating the addresses of subscripted array references. Consider a k-dimensional array, where the ith index range is L i . . Hi, for each dimension, i = 1 , 2 , ..., k . The PASCAL-P4 compiler allocates the array such that in stepping through sequential elements, the rightmost index varies fastest, the second rightmost varies next fastest, and the leftmost index varies slowest, like the dials of an odometer. The element A [I1 ,12, . . . , Ik] is offset [I1 -Ll]"D2"D3". . ."Dk + [12 -~2 ] *~3 * . . ."Dk + . .. + [ I k -Lk] from the first word of the array, where Di = Hi-Li+ 1 . Note that Ii can be any expression of appropriate scalar type. R R N , r 1, o f f set, l a b e l RCN,rl,integec c o n s t a n t , l a b e l R M N , r l , l e v e l , o f f s e t , l a b e l FVN , r l , o f f set, l a b e l R I N , r 1, r 2 , o f f s e t , l a b e l SSN,label A J J-1 A[ I I A C J 1 J I f i r 1 1 A J J-1 W AI J I A[ 53 I 1 141 J1 J -1 J PCODE transformation 33 replaces: LDCI integer constant AD1 by INCI integer constant DECI integer constant or TUPLE transformation 99 cumulates indexing increment or decrement in the corresponding offset (see rapid calculations of subscripted addresses). QCODE transformation 98 replaces test followed by a false-jump by a test-andjump command.Note the use of registers to hold values of all kinds, including expressions, addresses and stored values. There are not enough registers to maintain all the needed values.