-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcodedb.c
262 lines (225 loc) · 6.48 KB
/
codedb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
This file is part of shuJIT,
Just In Time compiler for Sun Java Virtual Machine.
Copyright (C) 1998,1999,2000 Kazuyuki Shudo
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id$
*/
#include "config.h"
#ifdef CODE_DB
#include <stdio.h>
#include <sys/types.h> // for lseek()
#include <unistd.h> // for lseek()
#include "compiler.h"
#ifdef GDBM
# include <gdbm.h>
#else
# include <ndbm.h>
#endif
#define MAX_SIG_LEN 256
// in codedb.c
void writeCompiledCode(
# ifdef GDBM
GDBM_FILE db
# else
DBM *db
# endif
, int fd, CompilerContext *cc) {
struct methodblock *mb = cc->mb;
datum key, dbdata;
off_t position;
char sig[MAX_SIG_LEN];
#ifdef CODE_DB_DEBUG
printf("writeCompiledCode() called.\n"); fflush(stdout);
#endif
position = lseek(fd, (off_t)0, SEEK_END);
#ifdef CODE_DB_DEBUG
printf(" position: %d\n", position); fflush(stdout);
#endif
// write to DB
{
CodeInfo *info = (CodeInfo *)mb->CompiledCodeInfo;
pcentry *pcentry = cc->pctable;
#ifdef EXC_BY_SIGNAL
throwentry *tentry = info->throwtable;
#endif // EXC_BY_SIGNAL
uint32_t len;
int i = 0;
#ifdef CODE_DB_DEBUG
printf(" code_size:4: 0x%x\n", info->code_size); fflush(stdout);
#endif
write(fd, &info->code_size, 4);
len = pctableLen(cc);
#ifdef CODE_DB_DEBUG
printf(" pctablelen:4: 0x%x\n", len); fflush(stdout);
#endif
write(fd, &len, 4);
#ifdef EXC_BY_SIGNAL
#ifdef CODE_DB_DEBUG
printf(" throwtablelen:4: 0x%x\n", info->throwtablelen); fflush(stdout);
#endif
write(fd, &info->throwtablelen, 4);
#endif // EXC_BY_SIGNAL
#ifdef CODE_DB_DEBUG
printf(" exc_handler_nativeoff:4: 0x%x\n", info->exc_handler_nativeoff);
printf(" finish_return_nativeoff:4: 0x%x\n",
info->finish_return_nativeoff);
fflush(stdout);
#endif
write(fd, &info->exc_handler_nativeoff, 4);
write(fd, &info->finish_return_nativeoff, 4);
for (i = 0; i < len; i++) {
write(fd, &pcentry->opcode, 2);
write(fd, &pcentry->flag, 2);
write(fd, &pcentry->operand, 4);
write(fd, &pcentry->byteoff, 4);
write(fd, &pcentry->increasing_byteoff, 4);
write(fd, &pcentry->nativeoff, 4);
pcentry++;
}
#ifdef EXC_BY_SIGNAL
len = info->throwtablelen;
for (i = 0; i < len; i++) {
write(fd, &tentry->start, 4);
write(fd, &tentry->byteoff, 2);
write(fd, &tentry->len, 1);
# ifdef PATCH_WITH_SIGTRAP
write(fd, &tentry->patched_code, 1);
write(fd, &tentry->cb, 4);
write(fd, &tentry->opcode, 2);
# endif
tentry++;
}
#endif // EXC_BY_SIGNAL
write(fd, mb->CompiledCode, info->code_size);
}
// store
snprintf(sig, MAX_SIG_LEN, "%s#%s%s",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
#ifdef CODE_DB_DEBUG
printf(" key: %s\n", sig); fflush(stdout);
#endif
key.dptr = sig;
key.dsize = strlen(sig);
dbdata.dptr = (char *)&position;
dbdata.dsize = sizeof(off_t);
#ifdef GDBM
sym_dbm_store(db, key, dbdata, GDBM_REPLACE);
//sym_dbm_sync(db);
#else
sym_dbm_store(db, key, dbdata, DBM_REPLACE);
#endif
}
int readCompiledCode(
# ifdef GDBM
GDBM_FILE db
# else
DBM *db
# endif
, int fd, CompilerContext *cc) {
struct methodblock *mb = cc->mb;
datum key, dbdata;
off_t offset;
char sig[MAX_SIG_LEN];
#ifdef CODE_DB_DEBUG
printf("readCompiledCode() called.\n"); fflush(stdout);
#endif
// fetch
snprintf(sig, MAX_SIG_LEN, "%s#%s%s",
cbName(fieldclass(&mb->fb)), mb->fb.name, mb->fb.signature);
#ifdef CODE_DB_DEBUG
printf(" key: %s\n", sig); fflush(stdout);
#endif
key.dptr = sig;
key.dsize = strlen(sig);
dbdata = sym_dbm_fetch(db, key);
if (!dbdata.dptr) { // compiled code is not found
#ifdef CODE_DB_DEBUG
printf(" code isn't found.\n"); fflush(stdout);
#endif
return 0;
}
offset = *((off_t *)dbdata.dptr);
#ifdef CODE_DB_DEBUG
printf(" offset: %d\n", offset); fflush(stdout);
#endif
if ((lseek(fd, offset, SEEK_SET)) < 0) {
perror("lseek");
JVM_Exit(1);
}
// read from DB
{
CodeInfo *info = (CodeInfo *)mb->CompiledCodeInfo;
uint32_t len;
pcentry *pcentry;
#ifdef EXC_BY_SIGNAL
throwentry *tentry;
#endif // EXC_BY_SIGNAL
int i;
read(fd, &info->code_size, 4);
#ifdef CODE_DB_DEBUG
printf(" code_size:4: 0x%x\n", info->code_size); fflush(stdout);
#endif
read(fd, &len, 4);
pctableSetLen(cc, len);
#ifdef CODE_DB_DEBUG
printf(" pctablelen:4: 0x%x\n", pctableLen(cc)); fflush(stdout);
#endif
#ifdef EXC_BY_SIGNAL
read(fd, &info->throwtablelen, 4);
#ifdef CODE_DB_DEBUG
printf(" throwtablelen:4: 0x%x\n", info->throwtablelen); fflush(stdout);
#endif
#endif // EXC_BY_SIGNAL
read(fd, &info->exc_handler_nativeoff, 4);
read(fd, &info->finish_return_nativeoff, 4);
#ifdef CODE_DB_DEBUG
printf(" exc_handler_nativeoff:4: 0x%x\n", info->exc_handler_nativeoff);
printf(" finish_return_nativeoff:4: 0x%x\n",
info->finish_return_nativeoff);
fflush(stdout);
#endif
len = pctableLen(cc);
pctableExtend(cc, len);
pcentry = cc->pctable;
for (i = 0; i < len; i++) {
read(fd, &pcentry->opcode, 2);
read(fd, &pcentry->flag, 2);
read(fd, &pcentry->operand, 4);
read(fd, &pcentry->byteoff, 4);
read(fd, &pcentry->increasing_byteoff, 4);
read(fd, &pcentry->nativeoff, 4);
pcentry++;
}
#ifdef EXC_BY_SIGNAL
throwtableExtend(info, info->throwtablelen);
tentry = info->throwtable;
len = info->throwtablelen;
for (i = 0; i < len; i++) {
read(fd, &tentry->start, 4);
read(fd, &tentry->byteoff, 2);
read(fd, &tentry->len, 1);
# ifdef PATCH_WITH_SIGTRAP
read(fd, &tentry->patched_code, 1);
read(fd, &tentry->cb, 4);
read(fd, &tentry->opcode, 2);
# endif
tentry++;
}
#endif // EXC_BY_SIGNAL
mb->CompiledCode = (void *)sysMalloc(info->code_size);
read(fd, mb->CompiledCode, info->code_size);
}
return 1;
}
#endif // CODE_DB