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 /****************************************************************************/
21 /*                                                                          */
22 /*   Files memory.h{c}. These files define the system memory structures and */
23 /*   their access functions, including the system memory, run-time symbol   */
24 /*   tables, implication and import tables and the system module table.     */
25 /*                                                                          */
26 /****************************************************************************/
27 
28 #ifndef MEMORY_H
29 #define MEMORY_H
30 
31 #include <limits.h>
32 #include <math.h>
33 #include "../simulator/mctypes.h"        //to be changed
34 #include "../simulator/dataformats.h"    //to be changed
35 //#include "../config.h"
36 
37 /******************************************************************************/
38 /*                FIND CODE FUNCTION                                          */
39 /******************************************************************************/
40 //arguments: constInd, search table size, search table addr
41 typedef CSpacePtr (*MEM_FindCodeFnPtr)(int, int, MemPtr);
42 
43 /******************************************************************************/
44 /*                SYSTEM MEMORY MANAGEMENT                                    */
45 /******************************************************************************/
46 extern  WordPtr MEM_memBeg;       //starting addr of the system memory
47 extern  WordPtr MEM_memEnd;       //end addr of the system memory
48 extern  WordPtr MEM_memTop;       //the first usable word in the system memory
49 extern  WordPtr MEM_memBot;       //the last usable word in the system memory
50 
51 /* Asking for the system memory of a given size (in word),                    */
52 /* and initialize relevant global variables.                                  */
53 void    MEM_memInit(unsigned int size);
54 /* Asking the simulator (system) memory for space of a given size (in word)  */
55 WordPtr MEM_memExtend(unsigned int size);
56 
57 /******************************************************************************/
58 /*                MODULE SPACE COMPONENTS                                     */
59 /*----------------------------------------------------------------------------*/
60 /* I.  Run time symbol tables: kind table; type skeleton table; constant table*/
61 /* II. Implication table                                                      */
62 /* III.Import table                                                           */
63 /******************************************************************************/
64 
65 /*****************************************************************************/
66 /*                          KIND SYMBOL TABLE                                */
67 /*****************************************************************************/
68 /*  kind symbol table entry                                                  */
69 typedef struct
70 {
71     DF_StrDataPtr name;
72     TwoBytes      arity; //agree with DF_StrTypeArity (simulator/dataformats.c)
73 } MEM_KstEnt;
74 
75 typedef MEM_KstEnt *MEM_KstPtr;
76 
77 /*  max possible index of kind table                                        */
78 /*  (agree with DF_KstTabInd in simulator/dataformats.c)                    */
79 #define MEM_KST_MAX_IND        USHRT_MAX
80 
81 /*  size of each entry of this table (in word)                              */
82 //Note this arithematic should in reality go into "config.h"
83 #define MEM_KST_ENTRY_SIZE     (int)ceil((double)sizeof(MEM_KstEnt)/WORD_SIZE)
84 
85 /*****************************************************************************/
86 /*                         TYPE SKELETON TABLE                               */
87 /*****************************************************************************/
88 /* type skeleton table entry                                                 */
89 typedef DF_TypePtr MEM_TstEnt;
90 
91 typedef MEM_TstEnt *MEM_TstPtr;
92 
93 /*  max possible index of type skeleton table                                */
94 #define MEM_TST_MAX_IND        USHRT_MAX
95 
96 /*  size of each entry of this table (in word)                               */
97 //Note this arithematic should in reality go into "config.h"
98 #define MEM_TST_ENTRY_SIZE    (int)ceil((double)sizeof(MEM_TstEnt)/WORD_SIZE)
99 
100 /*****************************************************************************/
101 /*                        CONSTANT SYMBOL TABLE                              */
102 /*****************************************************************************/
103 /*  constant symbol table entry                                              */
104 typedef struct
105 {
106     DF_StrDataPtr name;
107     TwoBytes      typeEnvSize;
108     TwoBytes      tskTabIndex;     //index to the type skeleton table
109     TwoBytes      neededness;      //neededness info
110     TwoBytes      univCount;
111     int           precedence;
112     int           fixity;
113 } MEM_CstEnt;
114 
115 typedef MEM_CstEnt *MEM_CstPtr;
116 
117 /*  max possible index of constant symbol table                             */
118 /*  (agree with DF_CstTabInd in simulator/dataformats.c)                    */
119 #define MEM_CST_MAX_IND     USHRT_MAX
120                                           //add one entry at the current top
121 /*  size of each entry of this table (in word)                               */
122 //Note this arithematic should in reality go into "config.h"
123 #define MEM_CST_ENTRY_SIZE    (int)(sizeof(MEM_CstEnt)/WORD_SIZE)
124 
125 /*****************************************************************************/
126 /*               ACCESSING THE IMPLICATION GOAL TABLE                        */
127 /*****************************************************************************/
128 #define MEM_IMPL_FIX_SIZE    3
129 /* functions for filling in the fields of an impl table                      */
130 /* Q: the data stored in each field in byte code: are they word or in their  */
131 /*    specific types?                                                        */
132 void MEM_implPutLTS(WordPtr tab, int lts);               //# pred (def extended)
133 void MEM_implPutFC(WordPtr tab, MEM_FindCodeFnPtr fcPtr);//ptr to find code func
134 void MEM_implPutPSTS(WordPtr tab, int tabSize);          //# entries link tab
135 void MEM_implPutLT(WordPtr tab, int ind, int cst);       //link tab; ind from 0
136 
137 /* functions for retrieving the addresses of associated tables               */
138 MemPtr MEM_implLT(MemPtr tab);           //start add of seq. of pred (link tab)
139 MemPtr MEM_implPST(MemPtr tab, int lts); //start add of pred search tab
140 
141 /* functions for retrieving the fields of a impl table                       */
142 int MEM_implLTS(MemPtr tab);                 //pred field (def extended)
143 MEM_FindCodeFnPtr MEM_implFC(MemPtr tab);    //ptr to find code func
144 int MEM_implPSTS(MemPtr tab);                //num entries in link tab
145 int MEM_implIthLT(MemPtr ltab, int index);   /* value in ith entry of link tab
146                                                  ltab is the addr of link tab;
147                                                  index should start from 0    */
148 
149 /*****************************************************************************
150  *                  ACCESSING THE IMPORTED MODULE TABLE                      *
151  *****************************************************************************/
152 #define MEM_IMP_FIX_SIZE    5
153 /* functions for filling in the fields of an import table                   */
154 /* Q: the data stored in each field in byte code: are they word or in their  */
155 /*    specific types?                                                        */
156 void MEM_impPutNCSEG(WordPtr tab, int nseg);           //# code segments
157 void MEM_impPutNLC(WordPtr tab, int nlc);              //# local constants
158 void MEM_impPutLTS(WordPtr tab, int lts);              //# pred (def extended)
159 void MEM_impPutFC(WordPtr tab, MEM_FindCodeFnPtr fcp); //ptr to find code func
160 void MEM_impPutPSTS(WordPtr tab, int tabSize);         //# entries in link tab
161 void MEM_impPutLT(WordPtr tab, int ind, int cst);      //link tab; ind from 0
162 void MEM_impPutLCT(WordPtr lcTab, int ind, int cst);   /*loc c tab(may null)
163                                                          lcTab addr of local
164                                                          ctab; ind from 0 */
165 
166 /* functions for retrieving the addresses of associated tables               */
167 MemPtr MEM_impLT(MemPtr tab);           //start addr of seq. of pred (link tab)
168 MemPtr MEM_impLCT(MemPtr tab, int lts); //start addr of local const table
169 MemPtr MEM_impPST(MemPtr tab, int lts, int nlc); //start addr of pred search tab
170 
171 /* functions for retrieving the fields of a impl table                       */
172 int MEM_impNCSEG(MemPtr tab);             //# code segments
173 int MEM_impNLC(MemPtr tab);               //# local constants
174 int MEM_impLTS(MemPtr tab);               //# of preds (def extended)
175 MEM_FindCodeFnPtr MEM_impFC(MemPtr tab);  //ptr to find code func
176 int MEM_impPSTS(MemPtr tab);              //# entries in link tab
177 int MEM_impIthLT(MemPtr lt, int ind);    /* ith entry in the link table: lt addr
178                                             of link tab; ind start from 0 */
179 int MEM_impIthLCT(MemPtr lct, int ind);  /* ith entry in the local const table:
180                                             lct local c tab; ind start from 0 */
181 
182 
183 /*****************************************************************************/
184 /*    ACCESSING THE BOUND VARIABLE INDEXING TABLE (BRACHING TABLE)           */
185 /*****************************************************************************/
186 int       MEM_branchTabIndexVal(MemPtr tab, int index); //the nth index value
187 CSpacePtr MEM_branchTabCodePtr(MemPtr tab, int index);  //transfer addr
188 
189 /*****************************************************************************/
190 /*                          SYSTEM MODULE TABLE                              */
191 /*****************************************************************************/
192 typedef struct
193 {
194     char        *modname;        //(top-level) module name
195     CSpacePtr   addtable;        //addr to the add code table of the top module
196     MEM_KstPtr  kstBase;         //starting addr of kind table
197     MEM_TstPtr  tstBase;         //starting addr of type skel table
198     MEM_CstPtr  cstBase;         //starting addr of constant table
199     WordPtr     modSpaceBeg;     //starting addr of module space
200     WordPtr     modSpaceEnd;     //ending addr of module space
201 	WordPtr     codeSpaceBeg;    //starting addr of module space
202 	WordPtr     codeSpaceEnd;    //ending addr of module space
203 } MEM_GmtEnt;
204 
205 #define MEM_MAX_MODULES    255   //max number of modules (temp)
206 
207 typedef MEM_GmtEnt MEM_Gmt[MEM_MAX_MODULES];
208 
209 extern  MEM_Gmt    MEM_modTable; //global module table
210 
211 MEM_GmtEnt *MEM_findInModTable(char* name);
212 MEM_GmtEnt *MEM_findFreeModTableEntry();
213 void MEM_removeModTableEntry(char* name);
214 
215 
216 extern MEM_GmtEnt  MEM_topModule; //top module
217 void MEM_topModuleInit();
218 
219 extern MEM_GmtEnt *MEM_currentModule; //current module being used
220 
221 
222 #endif  //MEMORY_H
223