1 //////////////////////////////////////////////////////////////////////////////
2 //Copyright 2008
3 // Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
4 //////////////////////////////////////////////////////////////////////////////
5 // This file is part of Teyjus. //
6 // //
7 // Teyjus is free software: you can redistribute it and/or modify //
8 // it under the terms of the GNU General Public License as published by //
9 // the Free Software Foundation, either version 3 of the License, or //
10 // (at your option) any later version. //
11 // //
12 // Teyjus is distributed in the hope that it will be useful, //
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
15 // GNU General Public License for more details. //
16 // //
17 // You should have received a copy of the GNU General Public License //
18 // along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
19 //////////////////////////////////////////////////////////////////////////////
20 #ifndef MEMORY_C
21 #define MEMORY_C
22
23 #include <string.h>
24 #include <stdio.h>
25 #include "error.h"
26 #include "memory.h"
27 #include "error.h"
28 #include "../tables/pervasives.h"
29 #include "../tables/pervinit.h"
30 #include "../simulator/mctypes.h"
31
32 /******************************************************************************/
33 /* SYSTEM MEMORY MANAGEMENT */
34 /******************************************************************************/
35 WordPtr MEM_memBeg; //starting addr of the system memory
36 WordPtr MEM_memEnd; //end addr of the system memory
37 WordPtr MEM_memTop; //the first usable word in the system memory
38 WordPtr MEM_memBot; //the last usable word in the system memory
39
40 /* Asking for the system memory of a given size (in word), */
41 /* and initialize relevant global variables. */
MEM_memInit(unsigned int size)42 void MEM_memInit(unsigned int size)
43 {
44 MEM_memBeg = MEM_memTop = (WordPtr)EM_malloc(size * sizeof(Word));
45 MEM_memEnd = MEM_memBot = MEM_memBeg + (size - 1);
46 }
47
48 /* Asking the simulator (system) memory for space of a given size (in word) */
MEM_memExtend(unsigned int size)49 WordPtr MEM_memExtend(unsigned int size)
50 {
51 WordPtr rtPtr = MEM_memTop;
52
53 if ((MEM_memTop + size) > MEM_memBot) EM_error(EM_OUT_OF_MEMORY);
54 else MEM_memTop += size;
55 return rtPtr;
56 }
57
58 /*****************************************************************************/
59 /* ACCESSING THE IMPLICATION GOAL TABLE */
60 /*****************************************************************************/
61 /* functions for filling in the fields of a impl table */
62 //#pred field (def extended)
MEM_implPutLTS(WordPtr tab,int lts)63 void MEM_implPutLTS(WordPtr tab, int lts) { *((int *)tab) = lts; }
64 //ptr to find code func
MEM_implPutFC(WordPtr tab,MEM_FindCodeFnPtr fcPtr)65 void MEM_implPutFC(WordPtr tab, MEM_FindCodeFnPtr fcPtr)
66 {
67 *((MEM_FindCodeFnPtr *)(tab + 1)) = fcPtr;
68 }
69 //num entries in the link tab
MEM_implPutPSTS(WordPtr tab,int tabSize)70 void MEM_implPutPSTS(WordPtr tab, int tabSize){ *((int *)(tab + 2)) = tabSize;}
71 //fill in the ith entry of the link tab
72 //Note: index should start from 0
MEM_implPutLT(WordPtr tab,int ind,int cst)73 void MEM_implPutLT(WordPtr tab, int ind, int cst)
74 {
75 *((int *)(tab + 3 + ind)) = cst;
76 }
77
78 /* functions for retrieving the addresses of associated tables */
79 //start add of seq. of pred (link tab)
MEM_implLT(MemPtr tab)80 MemPtr MEM_implLT(MemPtr tab) { return (tab + 3); }
81 //start add of search tab
MEM_implPST(MemPtr tab,int lts)82 MemPtr MEM_implPST(MemPtr tab, int lts) { return (tab + 3 + lts); }
83
84 /* functions for retrieving the fields of a impl table */
85 //pred field (def extended)
MEM_implLTS(MemPtr tab)86 int MEM_implLTS(MemPtr tab) { return *((int *)tab); }
87 //ptr to find code func
MEM_implFC(MemPtr tab)88 MEM_FindCodeFnPtr MEM_implFC(MemPtr tab){return *((MEM_FindCodeFnPtr*)(tab+1));}
89 //num entries in the link tab
MEM_implPSTS(MemPtr tab)90 int MEM_implPSTS(MemPtr tab) { return *((int *)(tab + 2)); }
91 //ith entry in the link table
92 //Note: a) given address should be the start addr of the link table
93 // b) index starts from 0
MEM_implIthLT(MemPtr lt,int index)94 int MEM_implIthLT(MemPtr lt, int index) { return *((int *)(lt + index)); }
95
96 /*****************************************************************************
97 * ACCESSING THE IMPORTED MODULE TABLE *
98 *****************************************************************************/
99 /* functions for filling in the fields of an import table */
100 /* Q: the data stored in each field in byte code: are they word or in their */
101 /* specific types? */
102 //# code segments
MEM_impPutNCSEG(WordPtr tab,int nseg)103 void MEM_impPutNCSEG(WordPtr tab, int nseg) {*((int *)tab) = nseg; }
104 //# local constants
MEM_impPutNLC(WordPtr tab,int nlc)105 void MEM_impPutNLC(WordPtr tab, int nlc) {*((int *)(tab + 1)) = nlc; }
106 //# pred (def extended)
MEM_impPutLTS(WordPtr tab,int lts)107 void MEM_impPutLTS(WordPtr tab, int lts) {*((int *)(tab + 2)) = lts; }
108 //ptr to find code func
MEM_impPutFC(WordPtr tab,MEM_FindCodeFnPtr fcp)109 void MEM_impPutFC(WordPtr tab, MEM_FindCodeFnPtr fcp)
110 {
111 *((MEM_FindCodeFnPtr *)(tab + 3)) = fcp;
112 }
113 //# entries in link tab
MEM_impPutPSTS(WordPtr tab,int tabSize)114 void MEM_impPutPSTS(WordPtr tab, int tabSize){*((int *)(tab + 4)) = tabSize;}
115 //link tab
116 //Note: ind should start from 0
MEM_impPutLT(WordPtr tab,int ind,int cst)117 void MEM_impPutLT(WordPtr tab, int ind, int cst)
118 {
119 *((int *)(tab+5+ind)) = cst;
120 }
121 //loc c tab(may null)
122 //Note 1) the input tab addr should be the starting addr of the local const tab
123 // 2) ind should start from 0
MEM_impPutLCT(WordPtr lcTab,int ind,int cst)124 void MEM_impPutLCT(WordPtr lcTab, int ind, int cst)
125 {
126 *((int *)(lcTab+ind)) = cst;
127 }
128
129 /* functions for retrieving the addresses of associated tables */
130 //start addr of seq. of pred names (link tab)
MEM_impLT(MemPtr tab)131 MemPtr MEM_impLT(MemPtr tab) { return (tab + 5); }
132 //start addr of local const tab (possible null)
MEM_impLCT(MemPtr tab,int lts)133 MemPtr MEM_impLCT(MemPtr tab, int lts) { return (tab + 5 + lts); }
134 //start addr of search tab
MEM_impPST(MemPtr tab,int lts,int nlc)135 MemPtr MEM_impPST(MemPtr tab, int lts, int nlc) { return (tab + 5 + lts + nlc);}
136
137 /* functions for retrieving the fields of a impl table */
138 //# code segments
MEM_impNCSEG(MemPtr tab)139 int MEM_impNCSEG(MemPtr tab) { return *((int *)tab); }
140 //# local constants
MEM_impNLC(MemPtr tab)141 int MEM_impNLC(MemPtr tab) { return *((int *)(tab+1)); }
142 //# of preds (def extended)
MEM_impLTS(MemPtr tab)143 int MEM_impLTS(MemPtr tab) { return *((int *)(tab+2)); }
144 //ptr to find code func
MEM_impFC(MemPtr tab)145 MEM_FindCodeFnPtr MEM_impFC(MemPtr tab){return *((MEM_FindCodeFnPtr *)(tab+3));}
146 //# entries in pred name tab
MEM_impPSTS(MemPtr tab)147 int MEM_impPSTS(MemPtr tab) { return *((int *)(tab+4)); }
148 //ith entry in the link table
149 //Note 1) the input tab addr should be the starting addr of the link tab
150 // 2) ind should start from 0
MEM_impIthLT(MemPtr lt,int ind)151 int MEM_impIthLT(MemPtr lt, int ind) { return *((int *)(lt + ind)); }
152 //ith entry in the local const table
153 //Note 1) the input tab addr should be the starting addr of the local const tab
154 // 2) ind should start from 0
MEM_impIthLCT(MemPtr lct,int ind)155 int MEM_impIthLCT(MemPtr lct, int ind) { return *((int *)(lct + ind)); }
156
157 /*****************************************************************************/
158 /* ACCESSING THE BOUND VARIABLE INDEXING TABLE (BRACHING TABLE) */
159 /*****************************************************************************/
MEM_branchTabIndexVal(MemPtr tab,int index)160 int MEM_branchTabIndexVal(MemPtr tab, int index) //the nth index value
161 {
162 return *((int *)(tab + 8*index));
163
164 }
165
MEM_branchTabCodePtr(MemPtr tab,int index)166 CSpacePtr MEM_branchTabCodePtr(MemPtr tab, int index) //transfer addr
167 {
168 return *((CSpacePtr *)(tab + 8*index + 4));
169 }
170
171 /*****************************************************************************/
172 /* GLOBAL MODULE TABLE */
173 /*****************************************************************************/
174 MEM_Gmt MEM_modTable; //global module table
175
MEM_findInModTable(char * name)176 MEM_GmtEnt *MEM_findInModTable(char* name)
177 {
178 int i;
179 char *myname;
180 for (i = 0; i <= MEM_MAX_MODULES; i++) {
181 if (myname = MEM_modTable[i].modname)
182 if (strcmp(myname, name) == 0) return (MEM_modTable + i);
183 }
184 // module must have been found: failure must have been caught by the OCaml
185 // find module function
186 return NULL;
187 }
188
MEM_findFreeModTableEntry()189 MEM_GmtEnt *MEM_findFreeModTableEntry()
190 {
191 int i;
192 for (i = 0; i<= MEM_MAX_MODULES; i++) {
193 if (MEM_modTable[i].modname != NULL) return (MEM_modTable + i);
194 }
195 // impossible to fail: failure must have been caught by the OCaml
196 // find module function
197 return NULL;
198 }
199
MEM_removeModTableEntry(char * name)200 void MEM_removeModTableEntry(char* name)
201 {
202 MEM_GmtEnt *mod;
203 mod = MEM_findInModTable(name);
204 free(mod -> modname);
205 mod -> modname = NULL;
206 }
207
208
209 MEM_GmtEnt MEM_topModule; //top module
MEM_topModuleInit()210 void MEM_topModuleInit()
211 {
212 WordPtr memTop = MEM_memTop;
213 MEM_KstPtr kst=(MEM_KstPtr)MEM_memExtend(MEM_KST_ENTRY_SIZE*PERV_KIND_NUM);
214 MEM_TstPtr tst=(MEM_TstPtr)MEM_memExtend(MEM_TST_ENTRY_SIZE*PERV_TY_SKEL_NUM);
215 MEM_CstPtr cst=(MEM_CstPtr)MEM_memExtend(MEM_CST_ENTRY_SIZE*PERV_CONST_NUM);
216
217 PERVINIT_copyKindDataTab(kst);
218 PERVINIT_copyTySkelTab(tst);
219 PERVINIT_copyConstDataTab(cst);
220
221 MEM_topModule.modname = "";
222 MEM_topModule.addtable = NULL;
223 MEM_topModule.kstBase = kst;
224 MEM_topModule.tstBase = tst;
225 MEM_topModule.cstBase = cst;
226 MEM_topModule.modSpaceBeg = memTop;
227 MEM_topModule.modSpaceEnd = MEM_memTop;
228 MEM_topModule.codeSpaceBeg = NULL;
229 MEM_topModule.codeSpaceEnd = NULL;
230 }
231
232 MEM_GmtEnt *MEM_currentModule; //current module being used
233
234
235 #endif //MEMORY_C
236