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 "implgoal.h"
21 #include "loader.h"
22 #include "file.h"
23 #include "code.h"
24 #include "const.h"
25 #include "../system/memory.h"
26 #include "ld_message.h"
27 #include "searchtab.h"
28
29 TwoBytes LD_IMPORTTAB_numImportTabs;
30 WordPtr* LD_IMPORTTAB_ImportTabs;
31
32 WordPtr LD_IMPORTTAB_LoadImportTab(MEM_GmtEnt* ent);
33
LD_IMPORTTAB_LoadImportTabs(MEM_GmtEnt * ent)34 void LD_IMPORTTAB_LoadImportTabs(MEM_GmtEnt* ent)
35 {
36 int i;
37 TwoBytes count=LD_IMPORTTAB_numImportTabs=LD_FILE_GET2();
38 LD_mutter("Loading %d import tables\n",count);
39 LD_IMPORTTAB_ImportTabs=(WordPtr*)EM_malloc(count*sizeof(WordPtr));
40
41 EM_TRY{
42 ent->addtable=(CSpacePtr)LD_IMPORTTAB_LoadImportTab(ent);
43 }EM_CATCH{
44 LD_error("While loading add code table\n");
45 EM_RETHROW();
46 }
47 for(i=1;i<count;i++)
48 {
49 LD_debug("i=%d\n",i);
50 EM_TRY{
51 LD_IMPORTTAB_ImportTabs[i]=LD_IMPORTTAB_LoadImportTab(ent);
52 }EM_CATCH{
53 LD_error("While loading import table %d\n",i);
54 EM_RETHROW();
55 }
56 }
57
58 return;
59 }
60
LD_IMPORTTAB_LoadImportTab(MEM_GmtEnt * ent)61 WordPtr LD_IMPORTTAB_LoadImportTab(MEM_GmtEnt* ent)
62 {
63 WordPtr tab;
64 int psts;
65 int cst;
66 int i;
67 int numSegs;
68 int nctSize;
69 int lctSize;
70 Byte fcf;
71 MemPtr lcTab;
72
73 LD_debug("Loading import table\n");
74 ///\todo Check on the space requirements of the import table.
75
76 tab=LD_LOADER_ExtendModSpace(ent,MEM_IMP_FIX_SIZE);
77 numSegs = LD_FILE_GET1();//Get number of segments.
78 MEM_impPutNCSEG(tab,numSegs);
79 LD_debug(" Import table has %d segments\n",numSegs);
80 //Load Next Clause Table
81 nctSize=(int)LD_FILE_GET2();
82 LD_debug(" Next clause table has %d entries\n",nctSize);
83
84 MEM_impPutLTS(tab,nctSize);
85
86 // added by XQ
87 LD_LOADER_ExtendModSpace(ent, nctSize);
88 for(i=0;i<nctSize;i++)
89 {
90 cst=(int)LD_CONST_GetConstInd();
91 MEM_impPutLT(tab,i,cst);
92 }
93
94 //Local Constant table
95 lctSize=(int)LD_FILE_GET2();
96 LD_debug(" Local constant table has %d entries\n",lctSize);
97
98 MEM_impPutNLC(tab, lctSize);
99 lcTab=MEM_impLCT(tab,nctSize);
100 ///\todo Reorder Link table and local constant table
101 // added by XQ
102 LD_LOADER_ExtendModSpace(ent, lctSize);
103
104 //for(i=0;i<nctSize;i++) -- XQ
105 for (i = 0; i < lctSize; i++)
106 {
107 cst=(int)LD_CONST_GetConstInd();
108 MEM_impPutLCT(lcTab, i, cst);
109 }
110
111 //Load FindCodeFunc
112 fcf=LD_FILE_GET1();
113 if(fcf==SEARCHTAB_FCF_SEQNSEARCH)
114 {
115 LD_debug(" Loading sequential search table\n");
116 MEM_impPutFC(tab,(MEM_FindCodeFnPtr)&LD_SEARCHTAB_SeqnSrch);
117 LD_SEARCHTAB_LoadSeqSTab(ent,&psts);
118 MEM_impPutPSTS(tab,psts);
119 ///\todo do something with returned address.
120 }
121 else if(fcf==SEARCHTAB_FCF_HASHSEARCH)
122 {
123
124 LD_debug(" Loading hash table\n");
125 MEM_impPutFC(tab,(MEM_FindCodeFnPtr)&LD_SEARCHTAB_HashSrch);
126 LD_SEARCHTAB_LoadHashTab(ent,&psts);
127 MEM_impPutPSTS(tab,psts);
128 ///\todo do something with returned address.
129 } else {
130 LD_error("Invalid find code function %d\n",fcf);
131 EM_THROW(LD_LoadError);
132 }
133 LD_debug("Done Loading Import table\n");
134 return tab;
135 }
136
LD_IMPORTTAB_GetImportTabAddr()137 WordPtr LD_IMPORTTAB_GetImportTabAddr()
138 {
139 int i =(int) LD_FILE_GET2();
140 if(0>i || i>LD_IMPORTTAB_numImportTabs)
141 EM_THROW(LD_LoadError);
142 return LD_IMPORTTAB_ImportTabs[i];
143 }
144