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 #include "file.h"
21 #include "loader.h"
22 #include "const.h"
23 #include "code.h"
24 #include "searchtab.h"
25 #include "../system/memory.h"
26 #include "ld_message.h"
27 
28 struct HashTabEnt;
29 
30 struct HashTabEnt
31 {
32   int constInd;
33   CSpacePtr codeAddr;
34   struct HashTabEnt* next;
35 };
36 
37 
Hash(int ci,int size)38 int Hash(int ci,int size)
39 {
40   return ci%size;
41 }
42 
LD_SEARCHTAB_LoadHashTab(MEM_GmtEnt * ent,int * size)43 WordPtr LD_SEARCHTAB_LoadHashTab(MEM_GmtEnt* ent, int* size)
44 {
45   int i;
46   int cst;
47   struct HashTabEnt *tabEnt;
48   struct HashTabEnt *tab;
49   int numEntries=*size=LD_FILE_GET2();
50   LD_debug("Hash table has %d entries\n",numEntries);
51   /*
52   struct HashTabEnt *tab=(struct HashTabEnt*)LD_LOADER_ExtendModSpace(ent,sizeof(struct HashTabEnt)*numEntries); --XQ
53   */
54   tab=(struct HashTabEnt*)LD_LOADER_ExtendModSpaceInByte(ent,sizeof(struct HashTabEnt)*numEntries);
55   for(i=0;i<numEntries;i++)
56   {
57     tab[i].constInd=-1;
58     tab[i].next=NULL;
59   }
60 
61   tabEnt=NULL;
62   for(i=0;i<numEntries;i++)
63   {
64     cst=(int)LD_CONST_GetConstInd();
65     tabEnt=&(tab[Hash(cst,numEntries)]);
66     if(tabEnt->constInd==-1)
67     {
68       tabEnt->constInd=cst;
69       tabEnt->codeAddr=LD_CODE_GetCodeInd();
70     } else {
71       while(tabEnt->next!=NULL)
72         tabEnt=tabEnt->next;
73       /*
74       tabEnt=tabEnt->next=(struct HashTabEnt*)LD_LOADER_ExtendModSpace(ent,sizeof(struct HashTabEnt));
75       -- XQ */
76       tabEnt=tabEnt->next=
77           (struct HashTabEnt*)LD_LOADER_ExtendModSpaceInByte(ent,sizeof(struct HashTabEnt));
78       tabEnt->constInd=cst;
79       tabEnt->codeAddr=LD_CODE_GetCodeInd();
80       tabEnt->next=NULL;
81     }
82   }
83   return (WordPtr)tab;
84 }
85 
LD_SEARCHTAB_HashSrch(int constInd,int STabSize,MemPtr STabAddr)86 CSpacePtr LD_SEARCHTAB_HashSrch(int constInd, int STabSize, MemPtr STabAddr)
87 {
88   struct HashTabEnt *tabEnt=&(((struct HashTabEnt*)(STabAddr))[Hash(constInd,STabSize)]);
89   struct HashTabEnt *tmp;
90   tmp = tabEnt;
91 
92   //  do
93   while(tabEnt != NULL)
94   {
95       if(tabEnt->constInd==constInd){
96 	//fprintf(stderr, "found: %u\n", tabEnt -> codeAddr);
97           return tabEnt->codeAddr;
98       }
99       tabEnt=tabEnt->next;
100   }
101   //while(tabEnt->next!=NULL);
102   //constInd not found
103   return NULL;
104 }
105 
106 typedef struct
107 {
108   int constInd;
109   CSpacePtr codeAddr;
110 } SeqSTabEnt;
111 
LD_SEARCHTAB_LoadSeqSTab(MEM_GmtEnt * ent,int * size)112 WordPtr LD_SEARCHTAB_LoadSeqSTab(MEM_GmtEnt* ent, int* size)
113 {
114   int numEntries=*size=LD_FILE_GET2();
115   int i;
116 
117   //SeqSTabEnt* tab=(SeqSTabEnt*)LD_LOADER_ExtendModSpace(ent,sizeof(SeqSTabEnt)*numEntries); -- XQ
118   SeqSTabEnt* tab=(SeqSTabEnt*)LD_LOADER_ExtendModSpaceInByte(ent,sizeof(SeqSTabEnt)*numEntries);
119   for(i=0;i<numEntries;i++)
120   {
121     tab[i].constInd=(int)LD_CONST_GetConstInd();
122     tab[i].codeAddr=LD_CODE_GetCodeInd();
123   }
124 
125   return (WordPtr)tab;
126 }
127 
LD_SEARCHTAB_SeqnSrch(int constInd,int STabSize,MemPtr STabAddr)128 CSpacePtr LD_SEARCHTAB_SeqnSrch(int constInd, int STabSize, MemPtr STabAddr)
129 {
130   int i;
131   SeqSTabEnt* tab=(SeqSTabEnt*)STabAddr;
132   for(i=0;i<STabSize;i++)
133   {
134     if(tab[i].constInd==constInd)
135       return tab[i].codeAddr;
136   }
137   //constInd not found
138   return NULL;
139 }
140