home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / pascal / src / tmps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-16  |  7.1 KB  |  272 lines

  1. /*-
  2.  * Copyright (c) 1980 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)tmps.c    5.3 (Berkeley) 4/16/91";
  36. #endif /* not lint */
  37.  
  38. #include "whoami.h"
  39. #include "0.h"
  40. #include "objfmt.h"
  41. #ifdef PC
  42. #   include "pc.h"
  43. #endif PC
  44. #include "align.h"
  45. #include "tmps.h"
  46.  
  47. /*
  48.  * This routine defines the register allocation strategy
  49.  * All temporaries are allocated here, and this routines decides
  50.  * where they are to be put.
  51.  */
  52. #ifdef PC
  53.     /*
  54.      *    register temporaries
  55.      *    - are allocated from highreg towards lowreg.
  56.      *    - are of size regsize.
  57.      *    - register numbers from the various register types are mapped to 
  58.      *      integer register numbers using the offsets.  (cf. pcc/mac2defs)
  59.      *
  60.      *    stack temporaries
  61.      *    - are allocated on a downward growing stack.
  62.      */
  63.  
  64. #ifdef vax
  65.     /*
  66.      *    first pass register declaration constants
  67.      */
  68. struct    regtype {
  69.     long    lowreg;
  70.     long    highreg;
  71.     long    regsize;
  72. } regtypes[NUMREGTYPES] = {
  73.     { 6, 11, 4 },        /* r6..r11 */
  74. };
  75. #endif vax
  76.  
  77. #ifdef tahoe
  78.     /*
  79.      *    first pass register declaration constants
  80.      */
  81. struct    regtype {
  82.     long    lowreg;
  83.     long    highreg;
  84.     long    regsize;
  85. } regtypes[NUMREGTYPES] = {
  86.     { 6, 12, 4 },        /* r6..r12 */
  87. };
  88. #endif tahoe
  89.  
  90. #ifdef mc68000
  91.     /*
  92.      *    first pass register declaration constants
  93.      */
  94. struct    regtype {
  95.     long    lowreg;
  96.     long    highreg;
  97.     long    regsize;
  98. } regtypes[NUMREGTYPES] = {
  99.     { 2, 7, 4 },        /* d2..d7 */
  100.     { 2, 5, 4 },        /* a2..a5 */
  101. };
  102. #endif mc68000
  103. #endif PC
  104.  
  105. tmpinit(cbn)
  106.     int    cbn;
  107. {
  108.     struct om    *sizesp = &sizes[cbn];
  109. #    ifdef PC
  110.     int    i;
  111. #    endif PC
  112.  
  113.     sizesp->om_max = -DPOFF1;
  114.     sizesp->curtmps.om_off = -DPOFF1;
  115. #    ifdef PC
  116.         for (i = 0; i < NUMREGTYPES; i++) {
  117.             sizesp->low_water[i] = regtypes[i].highreg + 1;
  118.             sizesp->curtmps.next_avail[i] = regtypes[i].highreg;
  119.         }
  120. #    endif PC
  121. }
  122.  
  123. /*
  124.  * allocate runtime temporary variables
  125.  */
  126. /*ARGSUSED*/
  127. struct nl *
  128. tmpalloc(size, type, mode)
  129.     long size;
  130.     struct nl *type;
  131.     int mode;
  132. {
  133.     register struct om    *op = &sizes[ cbn ];
  134.     register int        offset;
  135.     register struct nl    *nlp;
  136.     long            alignment;
  137.  
  138. #    ifdef PC
  139. #        if defined(vax) || defined(tahoe)
  140.         if (  mode == REGOK
  141.            && size == regtypes[REG_GENERAL].regsize
  142.            && op->curtmps.next_avail[REG_GENERAL]
  143.                 >= regtypes[REG_GENERAL].lowreg) {
  144.             offset = op->curtmps.next_avail[REG_GENERAL]--;
  145.             if (offset < op->low_water[REG_GENERAL]) {
  146.                 op->low_water[REG_GENERAL] = offset;
  147.             }
  148.             nlp = defnl( (char *) 0 , VAR , type , offset );
  149.             nlp -> extra_flags = NLOCAL | NREGVAR;
  150.             putlbracket(ftnno, op);
  151.             return nlp;
  152.         }
  153. #        endif vax || tahoe
  154. #        ifdef mc68000
  155.         if (  mode == REGOK
  156.            && type != nl + TPTR
  157.            && size == regtypes[REG_DATA].regsize
  158.            && op->curtmps.next_avail[REG_DATA]
  159.                 >= regtypes[REG_DATA].lowreg) {
  160.             offset = op->curtmps.next_avail[REG_DATA]--;
  161.             if (offset < op->low_water[REG_DATA]) {
  162.                 op->low_water[REG_DATA] = offset;
  163.             }
  164.             nlp = defnl(0, VAR, type, offset + DATA_REG_OFFSET );
  165.             nlp -> extra_flags = NLOCAL | NREGVAR;
  166.             putlbracket(ftnno, op);
  167.             return nlp;
  168.         }
  169.         if (  mode == REGOK
  170.            && type == nl + TPTR
  171.            && size == regtypes[REG_ADDR].regsize
  172.            && op->curtmps.next_avail[REG_ADDR]
  173.                 >= regtypes[REG_ADDR].lowreg) {
  174.             offset = op->curtmps.next_avail[REG_ADDR]--;
  175.             if (offset < op->low_water[REG_ADDR]) {
  176.                 op->low_water[REG_ADDR] = offset;
  177.             }
  178.             nlp = defnl(0, VAR, type, offset + ADDR_REG_OFFSET );
  179.             nlp -> extra_flags = NLOCAL | NREGVAR;
  180.             putlbracket(ftnno, op);
  181.             return nlp;
  182.         }
  183. #        endif mc68000
  184. #    endif PC
  185.     if (type == NIL) {
  186.         alignment = A_STACK;
  187.     } else if (type == nl+TPTR) {
  188.         alignment = A_POINT;
  189.     } else {
  190.         alignment = align(type);
  191.     }
  192.         op->curtmps.om_off =
  193.         roundup((int)(op->curtmps.om_off - size), alignment);
  194.     offset = op->curtmps.om_off;
  195.     if ( offset < op->om_max ) {
  196.             op->om_max = offset;
  197.     }
  198.     nlp = defnl( (char *) 0 , VAR , type , offset );
  199. #    ifdef PC
  200.         nlp -> extra_flags = NLOCAL;
  201.         putlbracket(ftnno, op);
  202. #    endif PC
  203.     return nlp;
  204. }
  205.  
  206. /*
  207.  * deallocate runtime temporary variables
  208.  */
  209. /*ARGSUSED*/
  210. tmpfree(restore)
  211.     register struct tmps    *restore;
  212. {
  213. #   ifdef PC
  214.     register struct om        *op = &sizes[ cbn ];
  215.     bool            change = FALSE;
  216.  
  217. #    if defined(vax) || defined(tahoe)
  218.         if (restore->next_avail[REG_GENERAL]
  219.         > op->curtmps.next_avail[REG_GENERAL]) {
  220.             op->curtmps.next_avail[REG_GENERAL]
  221.             = restore->next_avail[REG_GENERAL];
  222.             change = TRUE;
  223.         }
  224. #    endif vax || tahoe
  225. #    ifdef mc68000
  226.         if (restore->next_avail[REG_DATA]
  227.         > op->curtmps.next_avail[REG_DATA]) {
  228.             op->curtmps.next_avail[REG_DATA]
  229.             = restore->next_avail[REG_DATA];
  230.             change = TRUE;
  231.         }
  232.         if (restore->next_avail[REG_ADDR]
  233.         > op->curtmps.next_avail[REG_ADDR]) {
  234.             op->curtmps.next_avail[REG_ADDR]
  235.             = restore->next_avail[REG_ADDR];
  236.             change = TRUE;
  237.         }
  238. #    endif mc68000
  239.     if (restore->om_off > op->curtmps.om_off) {
  240.         op->curtmps.om_off = restore->om_off;
  241.         change = TRUE;
  242.     }
  243.     if (change) {
  244.         putlbracket(ftnno, op);
  245.     }
  246. #endif PC
  247. }
  248.  
  249. #ifdef PC
  250. #if defined(vax) || defined(tahoe)
  251. /*
  252.  * create a save mask for registers which have been used
  253.  * in this level
  254.  */
  255. savmask()
  256. {
  257.     int mask;
  258.     int i;
  259.  
  260.     mask = RSAVEMASK;
  261.     if (opt('t'))
  262.             mask |= RUNCHECK;
  263.     for (i = 0; i <= regtypes[REG_GENERAL].highreg; i++) {
  264.         if (i >= sizes[cbn].low_water[REG_GENERAL]) {
  265.         mask |= 1 << i;
  266.         }
  267.     }
  268.     return mask;
  269. }
  270. #endif vax || tahoe
  271. #endif PC
  272.