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

Subversion Repositories thor

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2012-2018  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
// CC64 - 'C' derived language compiler
9
//  - 64 bit CPU
10
//
11
// This source file is free software: you can redistribute it and/or modify 
12
// it under the terms of the GNU Lesser General Public License as published 
13
// by the Free Software Foundation, either version 3 of the License, or     
14
// (at your option) any later version.                                      
15
//                                                                          
16
// This source file is distributed in the hope that it will be useful,      
17
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
18
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
19
// GNU General Public License for more details.                             
20
//                                                                          
21
// You should have received a copy of the GNU General Public License        
22
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
23
//                                                                          
24
// ============================================================================
25
//
26
#include "stdafx.h"
27
#include <stdlib.h>
28
#include <malloc.h>
29
 
30
#define BLKSIZE         4000
31
 
32
struct blk {
33
        char name[8];                   // string overwrite area
34
    struct blk *next;
35
    char       m[1];           /* memory area */
36
};
37
 
38
MBlk *MBlk::first = 0;
39
 
40
void *allocx(int sz)
41
{
42
        return MBlk::alloc(sz);
43
}
44
 
45
void *MBlk::alloc(int sz)
46
{
47
//  dfs.printf("Enter MBlk::alloc()\n");
48
        MBlk *p = (MBlk *)new char[sz+sizeof(MBlk)+15];
49
        if (p==0)
50
           return p;
51
        ZeroMemory((void *)p,sz+sizeof(MBlk));
52
        p->next = first;
53
        first = p;
54
//  dfs.printf("Leave MBlk::alloc()\n");
55
        return &p[1];
56
}
57
 
58
void MBlk::ReleaseAll()
59
{
60
        MBlk *mbk;
61
        while (first) {
62
                mbk = first->next;
63
                delete[] first;
64
                first = mbk;
65
        }
66
}
67
 
68
static int      glbsize = 0,    /* size left in current global block */
69
                locsize = 0,    /* size left in current local block */
70
                glbindx = 0,    /* global index */
71
                locindx = 0;    /* local index */
72
 
73
static struct blk       *locblk = 0,    /* pointer to local block */
74
                        *glbblk = 0;    /* pointer to global block */
75
 
76
char    *xalloc(int siz)
77
{
78
        struct blk *bp;
79
    char       *rv;
80
 
81
        while(siz % 8)  // align word
82
                siz++;
83
    if( global_flag ) {
84
        if( glbsize >= siz ) {
85
            rv = &(glbblk->m[glbindx]);
86
            glbsize -= siz;
87
            glbindx += siz;
88
            return (rv);
89
        }
90
        else {
91
            bp = (struct blk *)calloc(1,sizeof(struct blk) + BLKSIZE);
92
                        if( bp == (struct blk *)NULL )
93
                        {
94
                                printf(" not enough memory.\n");
95
                                exit(1);
96
                        }
97
                        strcpy_s(bp->name,8,"C64    ");
98
            bp->next = glbblk;
99
            glbblk = bp;
100
            glbsize = BLKSIZE - siz;
101
            glbindx = siz;
102
            return (glbblk->m);
103
        }
104
    }
105
    else    {
106
        if( locsize >= siz ) {
107
            rv = &(locblk->m[locindx]);
108
            locsize -= siz;
109
            locindx += siz;
110
            return (rv);
111
        }
112
        else {
113
            bp = (struct blk *)calloc(1,sizeof(struct blk) + BLKSIZE);
114
                        if( bp == NULL )
115
                        {
116
                                printf(" not enough local memory.\n");
117
                                exit(1);
118
                        }
119
                        strcpy_s(bp->name,8,"C64    ");
120
            bp->next = locblk;
121
            locblk = bp;
122
            locsize = BLKSIZE - siz;
123
            locindx = siz;
124
            return (locblk->m);
125
        }
126
    }
127
}
128
 
129
void ReleaseLocalMemory()
130
{
131
        struct blk      *bp1, *bp2;
132
    int             blkcnt;
133
    blkcnt = 0;
134
    bp1 = locblk;
135
    while( bp1 != NULL ) {
136
                if (strcmp(bp1->name,"C64    "))
137
                        printf("Block corrupted.");
138
        bp2 = bp1->next;
139
        free( bp1 );
140
        ++blkcnt;
141
        bp1 = bp2;
142
    }
143
    locblk = (struct blk *)NULL;
144
    locsize = 0;
145
        currentStmt = (Statement *)NULL;
146
        if (verbose) printf(" releasing %d bytes local tables.\n",blkcnt * BLKSIZE);
147
}
148
 
149
void ReleaseGlobalMemory()
150
{
151
        struct blk      *bp1, *bp2;
152
  int             blkcnt;
153
 
154
  dfs.printf("Enter ReleaseGlobalMemory\n");
155
  bp1 = glbblk;
156
  blkcnt = 0;
157
  while( bp1 != (struct blk *)NULL ) {
158
                if (strcmp(bp1->name,"C64    "))
159
                  dfs.printf("Block corrupted.");
160
    bp2 = bp1->next;
161
    free(bp1);
162
    ++blkcnt;
163
    bp1 = bp2;
164
  }
165
  glbblk = (struct blk *)NULL;
166
  glbsize = 0;
167
//    gsyms.head = NULL;         /* clear global symbol table */
168
//      gsyms.tail = NULL;
169
        memset(gsyms,0,sizeof(gsyms));
170
        if (verbose) printf(" releasing %d bytes global tables.\n",blkcnt * BLKSIZE);
171
    strtab = (struct slit *)NULL;             /* clear literal table */
172
 dfs.printf("Leave ReleaseGlobalMemory\n");
173
}
174
 
175
SYM *allocSYM() {
176
        SYM *sym = (SYM *)&compiler.symbolTable[compiler.symnum];
177
        ZeroMemory(sym,sizeof(SYM));
178
        sym->id = compiler.symnum;
179
        sym->name = new std::string("");
180
        sym->name2 = new std::string("");
181
        sym->name3 = new std::string("");
182
        sym->shortname = new std::string("");
183
        sym->lsyms.SetOwner(compiler.symnum);
184
        compiler.symnum++;
185
        if (compiler.symnum > 32760) {
186
          dfs.printf("Too many symbols.\n");
187
    throw new C64PException(ERR_TOOMANY_SYMBOLS,1);
188
  }
189
        return sym;
190
};
191
 
192
Function *allocFunction(int symnum)
193
{
194
        int count;
195
 
196
        for (count = 0; count < 3000; count++) {
197
                Function *sym = &compiler.functionTable[compiler.funcnum];
198
                if (!sym->valid) {
199
                        ZeroMemory(sym, sizeof(Function));
200
                        sym->valid = TRUE;
201
                        sym->params.SetOwner(symnum);
202
                        sym->proto.SetOwner(symnum);
203
                        sym->UsesTemps = true;
204
                        sym->UsesStackParms = true;
205
                        compiler.funcnum++;
206
                        if (compiler.funcnum > 2999)
207
                                compiler.funcnum = 0;
208
                        return (sym);
209
                }
210
                compiler.funcnum++;
211
                if (compiler.funcnum > 2999)
212
                        compiler.funcnum = 0;
213
        }
214
        dfs.printf("Too many functions.\n");
215
        throw new C64PException(ERR_TOOMANY_SYMBOLS, 1);
216
};
217
 
218
void FreeFunction(Function *fn)
219
{
220
        fn->valid = FALSE;
221
}
222
 
223
TYP *allocTYP()
224
{
225
//  printf("allocTYP()\r\n");
226
        TYP *tp = (TYP *)&compiler.typeTable[compiler.typenum];
227
        ZeroMemory(tp,sizeof(TYP));
228
        tp->sname = new std::string("");
229
  tp->bit_width = -1;
230
//      printf("Leave allocTYP():%p\r\n",tp);
231
  compiler.typenum++;
232
        if (compiler.typenum > 32760) {
233
          dfs.printf("Too many types\n");
234
    throw new C64PException(ERR_TOOMANY_SYMBOLS,1);
235
  }
236
        return tp;
237
};
238
 
239
Statement *allocSnode() { return (Statement *)xalloc(sizeof(Statement)); };
240
ENODE *allocEnode() {
241
  ENODE *p;
242
  p = (ENODE *)allocx(sizeof(ENODE));
243
  ZeroMemory(p, sizeof(ENODE));
244
  p->sp = new std::string();
245
  return (p);
246
};
247
Operand *allocOperand() { return (Operand *)xalloc(sizeof(Operand)); };
248
CSE *allocCSE() { return (CSE *)xalloc(sizeof(CSE)); };
249
 

powered by: WebSVN 2.1.0

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