Purdue University Purdue e-Pubs Computer Science Technical Reports Department of Computer Science 1970 On the Number of Registers Needed to Evaluate Arithmetic Expressions Victor Schneider Report Number: 70-047 Schneider, Victor, "On the Number of Registers Needed to Evaluate Arithmetic Expressions" (1970). Computer Science Technical Reports. Paper 389. http://docs.lib.purdue.edu/cstech/389 This document has been made available through Purdue e-Pubs, a service of the Purdue University Libraries. Please contact [email protected] for additional information. OH THE NUMBER OF REGISTERS NEEDED TO EVALUATE ARITHMETIC EXPRESSIONS V i c t or Schneider Sep t ember It, 1970 Revised Compu t er Sciences Dep t . Purdue Universi ty Lafaye t t e , Ind i ana 1*7907 Supported in par t by NSF Grant GJ-851 . ABSTRACT The quest ion of hov many t emporary storage registers are needed t o evaluate compi led ari thmet ic and mask i ng expressions is d i scussed . It is assumed t ha t any combinat ion of l ef t - t o-r i gh t , r i gh t - t o- l ef t , top-tobo t t om , and bo t t om- t o- t op techniques may be used to eva l ua t e an express i on , bu t t ha t no factoring or re-arranging of the expression may occur . On this bas i s , t he max i mum number of registers needed t o eva l ua t e nonparen t hes i zed expressions is N + l , w i t h N the number of dyad i c operator precedence l eve l s . For paren t hes i zed expressions w i t h a max i mum of K nes t ed paren t he t i ca l subexpress i ons , t he max i mum number of reg i s t ers needed is (K+l)N+l . Key Words and Phrases : ar i t hme t i c express i on , compi ler des i gn , dependency t r e e , programm i ng , s t orage m i n i m i za t i on• £ B Categories: 1*.12, If.22, 6.32 1' This paper looks at the quest ion of how many temporary storage registers are cal led for in the design of a computer for performing comb i na t i ons of ari thmet ic and masking operat ions . It assumes that machine- language code is t o he compi led from expressions in some FORTRAN or ALGOLl ike l anguage , and that the compi ler can evaluate i ts expression tree in any sequence wha t ever , "but wi thout transforming that t ree . language computer is assumed The target- t o have tvo or more operand reg i s t ers , and we w i l l represent statements In i ts machine language using t he fol lowing notat ion: Acc l *• Acc2 + Acc3 Acc2 +• name l name l Acc3 In theBe s t a t emen t s , name l refers t o a locat ion in core s t orage , a l l operat ions must be performed on operands stored in the numbered reg i s t ers , and some mechanism exists for storing data from core into these registers and for removing data from registers to core . In wha t fo l l ows , we w i l l use as an example an ALGOL-l ike language of ari thmet ic expressions having two operator precedence l eve l s . The operat ions w i l l be represented by the operators "+" and "x" having the convent ional interpretat ions and pr i or i t i es . We w i l l first discuss ari thmet ic expressions that are wr i t t en ent irely w i t hou t paren t heses , end deduce the number of accumulators that are needed t o evaluate the translat ions of Buch expressions . We can then extend our resul ts to the case of expressions having a larger number of operators and precedence l eve l s . To i l lustrate t he process of code generat ion from programming language express i ons , ve w i l l represent the expressions in terms of dependency t ree diagrams such as t hose suggested by J . Cohen (l) and 2' D . T . Ross (U) . t he express i on For examp l e , t he dependency d i agram of F i gure 1 represents a + h x c + d: /\ F i gure 1 . The Dependency Tree for a t t x c t d It should he no t ed t ha t the dependency t ree diagram for a genera l expression can b e constructed using ra t her we l l -known compi ler t echn i ques (1,5)• On an intui t ive b a s i s , ve can say t ha t t hese trees are cons t ruc t ed so that each operator Is t he root of a b i nary t ree whose subtrees are t he operands . 1 H e n c e , because h i gher precedence operat ions are more "binding* t han lover precedence opera t i ons , we can deduce the fol lowing tree diagram property: In t he dependency t ree d i agram of a paren t heses-free express i on , operators of l ower precedence dominate ( i . e . . t ake as operands) opera t i ons of t he same or h i nder precedence . T h u s , In our d i agram , the p l us signs appear above the mu l t i p l i ca t i on symbo l . The dependency t ree i n wh i ch a mu l t i p l i ca t i on symbo l dominates opera t i ons of addi t ion can on l y represen t a paren t hes i zed express i on in wh i ch paren t heses are used t o del imi t subexpressions t ha t are t rea t ed l i ke s i ng l e operands . 3' In terms of a s i ng l e-scan , left-to-right compi l ing a l gor i t hm such as t ha t given in the append i x , t he fol lowing code wou l d "be genera t ed for t he expression i n Figure 1 : Acc l b Acc2 c Acc l Acc l x Acc2 Acc2 <*• d Acc l *• Acc l + Acc2 Acc2 •*• a Acc l -*- Acc2 + Acc l , As can be seen in the examp l e above , the compi l ing a l gor i t hm proceeds in stepB t o generate code for evaluat ing the leftmost subtree in wh i ch t he opera t or roo t dominates t wo var i ab l es . In each s t ep , code is wr i t t en for t h i s l ef t mos t sub t ree , after wh i ch t h i s subtree is rep l aced in t he t ree by an accumulator name (denot ing an intermediate resu l t ) , wh i ch is t rea t ed as a var i ab l e in the fol lowing s t ep . T h u s , t he first code for our example was t he subtree (b x c ) , fo l l owed b y ((b x c) + d ) , e t c . We can deduce from t h i s examp l e the fol lowing interest ing proper t y of non-paren t hes i zed expressions: Proper t y 1 : Any non-paren t hes i zed expression in wh i ch the operators are wr i t t en in descending (or ascend i ng) order of precedence requires at mos t t wo accumulators to_ execute the t rans l a t ed code . Th i s is t rue because , in t he dependency t rees for such express i ons , each opera t i on dominates ei ther t wo variables or one var i ab l e and one operat i on . T h u s , for an express i on wr i t t en in descending (a l t erna t i ve l y , in ascend i ng) order of opera t or precedence , t he l ef t mos t subtree (rightmost sub t ree) has t he highest precedence operator as roo t dom i na t i ng t wo k var i ab l es . The remainder of t he t ree has operators dom i na t i ng operat ions as l ef t subtrees (right sub t rees) and variables as right subtrees (left sub t rees) . Hence , when the code is comp i l ed for t h i s class of expression t rees , each step of t he code generat ion process causes a subexpression t o be comb i ned v l t h a var i ab l e , leaving a subexpress i on resul t in a single operand reg i s t er . Any unary operat ions only affect t he intermediate resul t already s t ored i n the single operand reg i s t er . By i nduc t i on , this resul t can be ex t ended t o languages of expressions hav i ng any fini te number of b i nary operator , precedence l eve l s . m A - . / F i gure £ . x The Dependency Tree for a * b x c + d + e Code Produced from t he Dependency Tree : Acc l -*- a Acc2 d Acc2 •*• b Acc l Acc l + Acc2 Acc l +• Acc l * Acc2 Acc2 •*• e c Acc l •*- Acc l + Acc2 Acc2 Acc l «- Acc l x Acc2 5' As iB apparen t from Figure 1 , we can general ize Proper t y 1 as fo l l ows : Proper t y 2 : Any non-parenthesized expression in wh i ch the operators are wr i t t en in ascending fol lowed by descending order requires at mos t two accumulators to execute the t rans l a t ed code . This is so because , in the resu l t i ng dependency t r e e , the leftmost subtree in wh i ch the operator dominates t wo var i ab l es iB at the bo t t om of a l ef t -branch i ng , descending order subtree whose root is t he rightmost node of an ascending order sub t ree . T h u s , t h i s l ef t -branch i ng subtree w i l l be t rans l a t ed Into code using a t mos t two accumulators. , exac t l y as in Proper t y 1 . When this first l ef t -branch i ng subtree is t rans l a t ed , the compi ler i s left w i t h a r i gh t -branch i ng t ree whose r i gh t mos t opera t or dominates a variable on the l ef t , and the intermediate resul t deno t i ng t he subtree on the r i gh t . The compi ler t hen proceeds t o evaluate t h i s r i gh t -branch i ng sub t ree , exac t l y as in Proper t y 1 . As we see n e x t , it is no t t rue t ha t non-paren t hes i zed expressions wr i t t en in descending fo l l owed by ascending order can be eva l ua t ed using t wo reg i s t ers . Given our know l edge of how non-paren t hes i zed expressions are eva l ua t ed i t is interest ing t o determine wha t max i mum number of operand registerB are needed to execute t rans l a t ed code for more general express i ons . a l gor i t hm . Cons i der our l ef t - t o-r i gh t , single-scan code genera t i on In this a l gor i t hm , when an opera t or is the roo t of a t ree hav i ng operators as roots of each sub t ree , the left subtree w i l l be transl a t ed f i rs t , and i ts va l ue w i l l be stored in a t emporary reg i s t er wh i l e t he r i gh t subtree is eva l ua t ed . If t he right sub t ree in t urn is of t he some f o r m , t here resul ts a second intermediate va l ue s t ored i n an operand 6 reg i s t er wh i l e a r i gh t sub t ree is eva l ua t ed , and so on . The ques t i on t hen becomes , "For how many l eve l s can we force t h i s process of s t or i ng i n t ermed i a t e resu l t s t o con t i nue?" The answer is t ha t t he max i mum number of l eve l s i s t he same as t he max i mum number of b i nary opera t or precedence l eve l s in t he express i on l anguage . We can see t h i s proper t y mos t c l ear l y In t he examp l e g i ven in F i gure 3 , where a t h i rd l eve l of opera t or precedence , symbo l i zed by d i v i s i on "+" is i nc l uded for c l ar i t y : F i gure 3 . Dependency Tree for a x b + c * d * e * f In t h i s r i gh t -branch i ng t r e e , each opera t or dom i na t es e i t her t wo var i ab l es or t wo opera t i ons of h i gher precedence . Because of t h i s pyram i d of opera t i ons , t he s i ng l e-scan comp i l er causes t he i n t ermed i a t e resu l t for (a x b ) t o be s t ored in one reg i s t er , t he i n t ermed i a t e resu l t for (c + d ) is s t ored in a second reg i s t er , and t hen (e + f ) is eva l ua t ed us i ng t wo more t emporary reg i s t ers . Trans l a t ed Code Produced by a Lef t - t o-R l gh t Comp i l er for t he Express i on in F i gure 3 . F i rs t Sub t ree : Acc l «- a Acc2 •*- b Acc l * Acc l x Acc2 7' Second Sub t ree Th i rd Sub t ree : Acc2 c Acc3 d Acc2 Acc2 + Acc3 Acc3 + e f Acc3 Resu l t 1 : Acc2 F i na l Resu l t : Acc l Acc3 * Accij «- Acc2 x Acc3 Acc l + Acc2 T h u s , at mos t four operand reg i s t ers are used t o eva l ua t e t he comp i l ed t hree- l eve l , non-paren t hes i zed express i on . As obv i ous from i nspec t i on of F i gure 3 , comp i l er t ha t is no t conf i ned t o s i ng l e-scan , l ef t - t o-r i gh t t rans l a t i on cou l d eas i l y genera t e t hree reg i s t er code for our express i on . (For examp l e , a r i gh t - t o- l ef t t rans l a t or cou l d produce t hree-reg i s t er code . ) We cou l d t hen i nqu i re i n t o t he poss i b i l i t y of rewr i t i ng t he express i on in F i gure 3 so t ha t any comp i l i ng scheme wou l d ca l l for t he use of at l eas t four reg i s t ers . By exp l o i t i ng symme t ry , we can arr i ve a t t he express i on i l l us t ra t ed in F i gure U for wh i ch any comp i l i ng a l gor i t hm mus t use four t emporary s t ores : a F i gure U . A Non-Op t l m i zab l e Paren t heses-Free Express i on . a . »bxc + d+ e * f x g * h 8 Because of t he symme t ry of the dependency t ree in F i gure k , any comp i l i ng a l gor i t hm mus t work upward from t he bo t t ommos t l e v e l of opera t i ons , and mus t t herefore s t ore t emporary resu l t s of t he f i rs t sub t rees eva l ua t ed wh i l e t he rema i n i ng sub t rees are be i ng eva l ua t ed . Suppose t ha t we nex t a t t emp t t o a l t er t he express i on of F i gure U by i ncreas i ng i t s l eng t h . It soom becomes apparen t t ha t a l t er i ng t he l ef t or r i gh t sub t ree of F i gure U can a t mos t cause Acc^t t o be used more frequen t l y dur i ng execu t i on . Thus , i n t he wors t cases for paren t heses-free express i ons of t hree precedence l e v e l s , a t mos t four accumu l a t ors are needed t o execu t e t he t rans l a t ed code . We can nex t a t t emp t t o ex t end t hese resu l t s i nduc t i ve l y t o t he case of paren t heses-free express i ons w i t h more precedence l eve l s . By us i ng t he opera t i ons pyram i d d i agrammed i n F i gure U , i t is easy t o show t ha t a one- l eve l l anguage uBes t wo operand reg i s t ers 3 and a t wo- l eve l l anguage uses t hree . T h u s , t he hypo t hes i s is t rue for up t o t hree opera t or l eve l s . If w e t ake a f i c t i t i ous l anguage of express i ons in wh i ch t he opera t ors are > , + , and d i v i s i on i n order of precedence , w e can cons t ruc t an opera t i ons pyram i d , in wh i ch t he ">" symbo l dom i na t es t wo cop i es of t he t ree in F i gure U whose execu t i on i n assemb l y l anguage uses five accumu l a t ors . Moreover , by observ i ng t ha t unary opera t i ons of any precedence do no t i ncrease t he number of accumu l a t ors needed for execu t i on , we have i nduc t i ve l y demons t ra t ed t ha t Proper t y 3 : N+ l accumu l a t ors are needed t o execu t e the comp i l ed code produced for a paren t heses-free express i on l anguage hav i ng N precedence l eve l s of b i nary opera t ors . F i n a l l y , v e can cons i der t he mos t genera l case of a paren t hes i zed 9' express i on , and ask how such an expression can be wr i t ten so as to force the use of a max i mum number of reg i s t ers . l ef t - t o-r l gh t , single-scan comp i l er . Cons i der first t he case of a If we l ook at t he worB t -case dependency t ree in Figure 3 , we see t ha t each variable in the tree can be rep l aced by a paren t hes i zed express i on . However , t he max i mum use of registers only arises when the rightmost operator (in t h i s d i agram , the "T" symbo l ) dominates t wo paren t hes i zed subexpress i ons . Th i s is because each preced i ng subtree claims one operand register t o store t emporary resu l t s , and t herefore , t he max i mum storage of t emporary resul ts occurs when t he r i gh t mos t operat ion in t urn dominates a further r i gh t -branch i ng t r e e , as shown i n F i gure 5: S Figure 5 . + c * d * Dependency Tree for a * b ( e + f ) * ( a x b + c * d x e * f ) Note in F i gure 5 that t he choice of (e + f) as the left operand in the right subtree under "t" is arb i t rary . Any other subexpress i on t ha t forces the s i ng l e-scan compi ler t o produce code and genera t e a t emporary resul t in an operand register wou l d do as we l l . As we can see from the 10 branch i ng pat tern of this t ree , five registers w i l l be in use for storing temporary resul ts wh i l e t he rightmost subtree (e * f) is being evaluated . Hence , a maximum of Beven registers w i l l be In use at any one t i me during evaluat ion of this express i on . If we replace the rightmost (e + f) in Figure 5 by (e + f) + (a x b + c + d x e + f) we see immediately that the maximum number of registers in use is now t en . Cont inuing this w a y , we arrive at t he fol lowing resu l t . Property l|: (K+l)H+l operand registers are needed t o execute the code produced by a non-opt imized compi ler for general parenthesized expressions having N levels of binary opera t ors . The surprising fact is that this resul t also holds for so-cal led "opt imized" compi lers; in fac t , i t w i l l be Bhown t o ho l d for any scheme of code generat ion wha t ever . Our expedient is simply to make Figure 5 symmetrical in t he same way that Figure 3 was made symmetrical . Wi th this symme t ry , i t w i l l then be apparent t ha t , no mat ter wh i ch subtree is chOBen firBt in t he code-generat ion process , a t o t a l of (K+l)N+l operand registers w i l l be cal led Into use upon execu t i on . dependency tree This synmetrical Bhown In Figure 6 . Acknowledgments The author expresses his grat i tude t o Professor M . H . Halstead of Purdue Un i vers i t y for many helpful suggest ions . was bupported in part by N . S . F . grant OJ-851 . v Research for this paper A A A A A A A A Figure A A A A A A /\ A A It A A A A A „ hA. / \ d/\ A , A A Symmetrical Dependency Tree Based on Figures h and g: x(a v b x c T d + e r f x g f h ) T ( a T b x c T d + e ^ f 5 < g v b ) +(a r b * C T d + e i f x g 4 h ) T ( a » b > « c 4 d + e T f x g ^ h ) x(a * b x c * d + e * f x g * h ) * ( a * b x c * d + e * f x g * h ) Ah Ah A A /\ APPENDIX A Translat ion Grammar for a Two-Leve l Language of Ar i t hme t i c Expressions (Notat ion from W l r t h & Weber (6)) Syn t ac t i c Ru l es : p l : V ? 3= V V <var> <name> <factor> •+ <var> <factor> -+ (<sum> ) <term> •+ <tenn> * <factor> <term> •+ <factor> <sum> •*• <sum> + <term> P • <Bum> p 8"• <stat> •+ <var>: V <term> B <sum> Translat ion Ru l es : k •*• k + 1 ; 0[k] •«- hash(name) ; type [k] *2> *3 , 1 "reference ; empty = "type ' reference ' t hen U output [Jl] • ' Acc{p) A+ l ; p «- p+1 ; {0[k—1 ]} ' ; t ype [k-l] +- ' value ' ; 0[k- l ] •*- p ) else If t ype [k] = ' reference ' t hen (4 output [£] t- *Acc{p} t ype [k] £+1; p p+1 ; {0[k]} *; ' value ' ; 0[k] + p ) else undef i ned ; p • m i n (0[k- l ] , 0[k]) ; I + £+1; ou t pu t [£] k S i m i l ar t o emp t y ' Acctp} -i- Acc{0[k-1]> x Acc t O t k]} ' ; k- l ; 0[k] •»- p ; if t ype [k] = ' reference ' t hen (p p+1 ; I *• £+1; output [»] + ' Acc{p> {0[k]} ' ; t ype [k] •*• ' value ' ; 0[k] •*• p ) e l se undef i ned p p+1 ; i •*• Jl+2; output [i] •*• ' {0[k-l]} -c Acc{0[k]} *; 0[k- l ] + 0[k] ; t ype [k-l] t ype [k]; k «- k - BIBLIOGRAPHY Cohen , J . Langages pour l ' ^cri ture de compi lateurs . Doctoral d i sser t a t i on , Universi ty of Grenob l e , Grenob l e , France , 1967 . Naka t a , I . On compi l ing algori thms for ari thmet ic express i ons . Comm . ACM 1 0 , 8 (August 1967) , U92-k9h . Redz i e j owsk i , R . R . On ari thmet ic expressions and t rees . Comm . ACM 1 2 , 2 (February 1969) , 81-8U . Ross , D . T . des i gn . The AED approach to general ized computer-aided Proc . 22nd. Nat ional ACM Conf . , 1967 . Schne i der , V . B . A system for designing fast programm i ng language t rans l a t ors . Proc . 1969 Spring Joint Comp . Conf . W i r t h , N . and Weber , H , A general izat ion of ALGOL and i ts formal defini t ion: Parts I and II . Coma . ACM 1 (Jan. - Feb . 89-99.
© Copyright 2026 Paperzz