OpenCores
URL https://opencores.org/ocsvn/thor/thor/trunk

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [software/] [CC64/] [source/] [temp.cpp] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
#include "stdafx.h"
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <string.h>
5
 
6
/* Subroutines in this file take care of temporary variable management */
7
 
8
#define SWIDTH 8
9
#define REGION_MAX 128  /* The maximum number of stack elements that can be
10
                                                                        used for temporaries. */
11
#define MARK    -1                      /* Marks cells that are in use but are not the
12
                                                                        first cell of the region */
13
typedef int CELL;                       /* In-use (Region) map is an array of these */
14
 
15
int tmpVarSpace();
16
void tmpFreeAll();
17
void tmpReset();
18
int tmpAlloc(int);
19
void tmpFree(int);
20
 
21
static CELL Region[REGION_MAX];
22
static CELL *HighWaterMark = Region;
23
 
24
/* ---------------------------------------------------------------------------
25
   Description :
26
      Allocate a portion of the temporary variable region of the required
27
        size, expanding the tmp-region size if necessary. Return the offset in
28
        bytes from the start of the rvalue region to the first cell of the
29
        temporary. 0 is returned if no space is available, and an error message is
30
        also printed in this situation. This way the code-generation can go on as
31
        if space had been found without having to worry about testing for errors.
32
        (Bad code is generated, but so what?)
33
--------------------------------------------------------------------------- */
34
 
35
int tmpAlloc(int size)
36
{
37
        CELL *start, *p;
38
        int i;
39
 
40
        if (size == 0)
41
                printf("INTERNAL, tmpAlloc: zero length region requested.\n");
42
 
43
        // Round size to smallest size of stack element
44
        //---------------------------------------------
45
        if (size % SWIDTH)
46
                size = size + SWIDTH - (size % SWIDTH);
47
        size /= SWIDTH;
48
 
49
        // Seach for an empty area big enough to contain the temp.
50
        //--------------------------------------------------------
51
        for (start = Region; start < HighWaterMark;)
52
        {
53
                for (i = size, p = start; --i >= 0 && !*p; ++p)
54
                        ;
55
                if (i >= 0)
56
                        start = p+1;
57
                else
58
                        break;
59
        }
60
 
61
        if (start < HighWaterMark)
62
                p = start;
63
        else
64
        {
65
                if ((HighWaterMark + size) > (Region + REGION_MAX))     // No room
66
                {
67
                        printf("Expression too complex, break into smaller pieces.\n");
68
                        return 0;
69
                }
70
                p = HighWaterMark;
71
                HighWaterMark += size;
72
        }
73
        for(*p = size; --size > 0; *++p = MARK)  /* 1st cell =size. Others = MARK) */
74
                ;
75
 
76
        return ((start - Region)*SWIDTH);
77
}
78
 
79
 
80
/* ---------------------------------------------------------------------------
81
   Description :
82
      Release a temporary var.; offset should have been returned from
83
   tmp_alloc().
84
--------------------------------------------------------------------------- */
85
 
86
void tmpFree(int offset)
87
{
88
        CELL *p = Region + offset/SWIDTH;
89
        int size;
90
 
91
        if (p < Region || p > HighWaterMark || !*p || *p == MARK)
92
                printf("INTERNAL, tmpFree: Bad offset (%d)\n", offset);
93
        else
94
                for (size = *p; --size >= 0; *p++ = 0)
95
                        ;
96
}
97
 
98
 
99
/* ---------------------------------------------------------------
100
        Description :
101
                Reset everything back to the virgin state, including
102
        the HighWater mark. This routine should be called just
103
        before a subroutine body is processed, when the prefix is
104
        output. See also: tmpFreeall().
105
--------------------------------------------------------------- */
106
 
107
void tmpReset()
108
{
109
        tmpFreeAll();
110
        HighWaterMark = Region;
111
}
112
 
113
 
114
/* ---------------------------------------------------------------
115
        Description :
116
                Free all temporaries currently in use (without modifying
117
        the high-water mark). This subroutine should be called
118
        after processing arithmetic statements to clean up any
119
        temporaries still kicking around (there is usually at least
120
        one).
121
--------------------------------------------------------------- */
122
 
123
void tmpFreeAll()
124
{
125
        memset(Region, 0, sizeof(Region));
126
}
127
 
128
 
129
/* ---------------------------------------------------------------
130
        Description :
131
                Return the total cumulative size of the temporary
132
        variable region in stack elements, not bytes. This number
133
        can be used as an argument to the link instruction.
134
 
135
        Returns :
136
                Size in stack elements of temporary variable area.
137
--------------------------------------------------------------- */
138
 
139
int tmpVarSpace()
140
{
141
        return (HighWaterMark - Region)*SWIDTH;
142
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.