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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
//===- ReadInst.cpp - Code to read an instruction from bytecode -----------===//
//
// This file defines the mechanism to read an instruction from a bytecode
// stream.
//
// Note that this library should be as fast as possible, reentrant, and
// threadsafe!!
//
// TODO: Change from getValue(Raw.Arg1) etc, to getArg(Raw, 1)
// Make it check type, so that casts are checked.
//
//===----------------------------------------------------------------------===//
#include "ReaderInternals.h"
#include "llvm/iTerminators.h"
#include "llvm/iMemory.h"
#include "llvm/iPHINode.h"
#include "llvm/iOther.h"
bool BytecodeParser::ParseRawInst(const uchar *&Buf, const uchar *EndBuf,
RawInst &Result) {
unsigned Op, Typ;
if (read(Buf, EndBuf, Op)) return failure(true);
// bits Instruction format: Common to all formats
// --------------------------
// 01-00: Opcode type, fixed to 1.
// 07-02: Opcode
Result.NumOperands = (Op >> 0) & 03;
Result.Opcode = (Op >> 2) & 63;
switch (Result.NumOperands) {
case 1:
// bits Instruction format:
// --------------------------
// 19-08: Resulting type plane
// 31-20: Operand #1 (if set to (2^12-1), then zero operands)
//
Result.Ty = getType((Op >> 8) & 4095);
Result.Arg1 = (Op >> 20) & 4095;
if (Result.Arg1 == 4095) // Handle special encoding for 0 operands...
Result.NumOperands = 0;
break;
case 2:
// bits Instruction format:
// --------------------------
// 15-08: Resulting type plane
// 23-16: Operand #1
// 31-24: Operand #2
//
Result.Ty = getType((Op >> 8) & 255);
Result.Arg1 = (Op >> 16) & 255;
Result.Arg2 = (Op >> 24) & 255;
break;
case 3:
// bits Instruction format:
// --------------------------
// 13-08: Resulting type plane
// 19-14: Operand #1
// 25-20: Operand #2
// 31-26: Operand #3
//
Result.Ty = getType((Op >> 8) & 63);
Result.Arg1 = (Op >> 14) & 63;
Result.Arg2 = (Op >> 20) & 63;
Result.Arg3 = (Op >> 26) & 63;
break;
case 0:
Buf -= 4; // Hrm, try this again...
if (read_vbr(Buf, EndBuf, Result.Opcode)) return failure(true);
Result.Opcode >>= 2;
if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
Result.Ty = getType(Typ);
if (Result.Ty == 0) return failure(true);
if (read_vbr(Buf, EndBuf, Result.NumOperands)) return failure(true);
switch (Result.NumOperands) {
case 0:
cerr << "Zero Arg instr found!\n";
return failure(true); // This encoding is invalid!
case 1:
if (read_vbr(Buf, EndBuf, Result.Arg1)) return failure(true);
break;
case 2:
if (read_vbr(Buf, EndBuf, Result.Arg1) ||
read_vbr(Buf, EndBuf, Result.Arg2)) return failure(true);
break;
case 3:
if (read_vbr(Buf, EndBuf, Result.Arg1) ||
read_vbr(Buf, EndBuf, Result.Arg2) ||
read_vbr(Buf, EndBuf, Result.Arg3)) return failure(true);
break;
default:
if (read_vbr(Buf, EndBuf, Result.Arg1) ||
read_vbr(Buf, EndBuf, Result.Arg2)) return failure(true);
// Allocate a vector to hold arguments 3, 4, 5, 6 ...
Result.VarArgs = new vector<unsigned>(Result.NumOperands-2);
for (unsigned a = 0; a < Result.NumOperands-2; a++)
if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return failure(true);
break;
}
if (align32(Buf, EndBuf)) return failure(true);
break;
}
#if 0
cerr << "NO: " << Result.NumOperands << " opcode: " << Result.Opcode
<< " Ty: " << Result.Ty->getDescription() << " arg1: " << Result.Arg1
<< " arg2: " << Result.Arg2 << " arg3: " << Result.Arg3 << endl;
#endif
return false;
}
bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
Instruction *&Res) {
RawInst Raw;
if (ParseRawInst(Buf, EndBuf, Raw))
return failure(true);
if (Raw.Opcode >= Instruction::FirstUnaryOp &&
Raw.Opcode < Instruction::NumUnaryOps && Raw.NumOperands == 1) {
Res = UnaryOperator::create((Instruction::UnaryOps)Raw.Opcode,
getValue(Raw.Ty,Raw.Arg1));
return false;
} else if (Raw.Opcode >= Instruction::FirstBinaryOp &&
Raw.Opcode < Instruction::NumBinaryOps && Raw.NumOperands == 2) {
Res = BinaryOperator::create((Instruction::BinaryOps)Raw.Opcode,
getValue(Raw.Ty, Raw.Arg1),
getValue(Raw.Ty, Raw.Arg2));
return false;
}
Value *V;
switch (Raw.Opcode) {
case Instruction::Cast: {
V = getValue(Raw.Ty, Raw.Arg1);
const Type *Ty = getType(Raw.Arg2);
if (V == 0 || Ty == 0) { cerr << "Invalid cast!\n"; return true; }
Res = new CastInst(V, Ty);
return false;
}
case Instruction::PHINode: {
PHINode *PN = new PHINode(Raw.Ty);
switch (Raw.NumOperands) {
case 0:
case 1:
case 3: cerr << "Invalid phi node encountered!\n";
delete PN;
return failure(true);
case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
cast<BasicBlock>(getValue(Type::LabelTy,Raw.Arg2)));
break;
default:
PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
if (Raw.VarArgs->size() & 1) {
cerr << "PHI Node with ODD number of arguments!\n";
delete PN;
return failure(true);
} else {
vector<unsigned> &args = *Raw.VarArgs;
for (unsigned i = 0; i < args.size(); i+=2)
PN->addIncoming(getValue(Raw.Ty, args[i]),
cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
}
delete Raw.VarArgs;
break;
}
Res = PN;
return false;
}
case Instruction::Shl:
case Instruction::Shr:
Res = new ShiftInst((Instruction::OtherOps)Raw.Opcode,
getValue(Raw.Ty, Raw.Arg1),
getValue(Type::UByteTy, Raw.Arg2));
return false;
case Instruction::Ret:
if (Raw.NumOperands == 0) {
Res = new ReturnInst(); return false;
} else if (Raw.NumOperands == 1) {
Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false;
}
break;
case Instruction::Br:
if (Raw.NumOperands == 1) {
Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)));
return false;
} else if (Raw.NumOperands == 3) {
Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)),
cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)),
getValue(Type::BoolTy , Raw.Arg3));
return false;
}
break;
case Instruction::Switch: {
SwitchInst *I =
new SwitchInst(getValue(Raw.Ty, Raw.Arg1),
cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
Res = I;
if (Raw.NumOperands < 3) return false; // No destinations? Wierd.
if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) {
cerr << "Switch statement with odd number of arguments!\n";
delete I;
return failure(true);
}
vector<unsigned> &args = *Raw.VarArgs;
for (unsigned i = 0; i < args.size(); i += 2)
I->dest_push_back(cast<Constant>(getValue(Raw.Ty, args[i])),
cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
delete Raw.VarArgs;
return false;
}
case Instruction::Call: {
Value *M = getValue(Raw.Ty, Raw.Arg1);
if (M == 0) return failure(true);
// Check to make sure we have a pointer to method type
PointerType *PTy = dyn_cast<PointerType>(M->getType());
if (PTy == 0) return failure(true);
MethodType *MTy = dyn_cast<MethodType>(PTy->getElementType());
if (MTy == 0) return failure(true);
vector<Value *> Params;
const MethodType::ParamTypes &PL = MTy->getParamTypes();
if (!MTy->isVarArg()) {
MethodType::ParamTypes::const_iterator It = PL.begin();
switch (Raw.NumOperands) {
case 0: cerr << "Invalid call instruction encountered!\n";
return failure(true);
case 1: break;
case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
case 3: Params.push_back(getValue(*It++, Raw.Arg2));
if (It == PL.end()) return failure(true);
Params.push_back(getValue(*It++, Raw.Arg3)); break;
default:
Params.push_back(getValue(*It++, Raw.Arg2));
{
vector<unsigned> &args = *Raw.VarArgs;
for (unsigned i = 0; i < args.size(); i++) {
if (It == PL.end()) return failure(true);
// TODO: Check getValue for null!
Params.push_back(getValue(*It++, args[i]));
}
}
delete Raw.VarArgs;
}
if (It != PL.end()) return failure(true);
} else {
if (Raw.NumOperands > 2) {
vector<unsigned> &args = *Raw.VarArgs;
if (args.size() < 1) return failure(true);
if ((args.size() & 1) != 0)
return failure(true); // Must be pairs of type/value
for (unsigned i = 0; i < args.size(); i+=2) {
const Type *Ty = getType(args[i]);
if (Ty == 0)
return failure(true);
Value *V = getValue(Ty, args[i+1]);
if (V == 0) return failure(true);
Params.push_back(V);
}
delete Raw.VarArgs;
}
}
Res = new CallInst(M, Params);
return false;
}
case Instruction::Invoke: {
Value *M = getValue(Raw.Ty, Raw.Arg1);
if (M == 0) return failure(true);
// Check to make sure we have a pointer to method type
PointerType *PTy = dyn_cast<PointerType>(M->getType());
if (PTy == 0) return failure(true);
MethodType *MTy = dyn_cast<MethodType>(PTy->getElementType());
if (MTy == 0) return failure(true);
vector<Value *> Params;
const MethodType::ParamTypes &PL = MTy->getParamTypes();
vector<unsigned> &args = *Raw.VarArgs;
BasicBlock *Normal, *Except;
if (!MTy->isVarArg()) {
if (Raw.NumOperands < 3) return failure(true);
Normal = cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2));
Except = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
MethodType::ParamTypes::const_iterator It = PL.begin();
for (unsigned i = 1; i < args.size(); i++) {
if (It == PL.end()) return failure(true);
// TODO: Check getValue for null!
Params.push_back(getValue(*It++, args[i]));
}
if (It != PL.end()) return failure(true);
} else {
if (args.size() < 4) return failure(true);
Normal = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
Except = cast<BasicBlock>(getValue(Type::LabelTy, args[2]));
if ((args.size() & 1) != 0)
return failure(true); // Must be pairs of type/value
for (unsigned i = 4; i < args.size(); i+=2) {
// TODO: Check getValue for null!
Params.push_back(getValue(getType(args[i]), args[i+1]));
}
}
delete Raw.VarArgs;
Res = new InvokeInst(M, Normal, Except, Params);
return false;
}
case Instruction::Malloc:
if (Raw.NumOperands > 2) return failure(true);
V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
Res = new MallocInst(Raw.Ty, V);
return false;
case Instruction::Alloca:
if (Raw.NumOperands > 2) return failure(true);
V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
Res = new AllocaInst(Raw.Ty, V);
return false;
case Instruction::Free:
V = getValue(Raw.Ty, Raw.Arg1);
if (!V->getType()->isPointerType()) return failure(true);
Res = new FreeInst(V);
return false;
case Instruction::Load:
case Instruction::GetElementPtr: {
vector<Value*> Idx;
if (!isa<PointerType>(Raw.Ty)) return failure(true);
const CompositeType *TopTy =
dyn_cast<CompositeType>(cast<PointerType>(Raw.Ty)->getElementType());
switch (Raw.NumOperands) {
case 0: cerr << "Invalid load encountered!\n"; return failure(true);
case 1: break;
case 2:
if (!TopTy) return failure(true);
Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
if (!V) return failure(true);
break;
case 3: {
if (!TopTy) return failure(true);
Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
if (!V) return failure(true);
const Type *ETy = MemAccessInst::getIndexedType(Raw.Ty, Idx, true);
const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
if (!ElTy) return failure(true);
Idx.push_back(V = getValue(ElTy->getIndexType(), Raw.Arg3));
if (!V) return failure(true);
break;
}
default:
if (!TopTy) return failure(true);
Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
if (!V) return failure(true);
vector<unsigned> &args = *Raw.VarArgs;
for (unsigned i = 0, E = args.size(); i != E; ++i) {
const Type *ETy = MemAccessInst::getIndexedType(Raw.Ty, Idx, true);
const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
if (!ElTy) return failure(true);
Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
if (!V) return failure(true);
}
delete Raw.VarArgs;
break;
}
if (Raw.Opcode == Instruction::Load) {
assert(MemAccessInst::getIndexedType(Raw.Ty, Idx) &&
"Bad indices for Load!");
Res = new LoadInst(getValue(Raw.Ty, Raw.Arg1), Idx);
} else if (Raw.Opcode == Instruction::GetElementPtr)
Res = new GetElementPtrInst(getValue(Raw.Ty, Raw.Arg1), Idx);
else
abort();
return false;
}
case Instruction::Store: {
vector<Value*> Idx;
if (!isa<PointerType>(Raw.Ty)) return failure(true);
const CompositeType *TopTy =
dyn_cast<CompositeType>(cast<PointerType>(Raw.Ty)->getElementType());
switch (Raw.NumOperands) {
case 0:
case 1: cerr << "Invalid store encountered!\n"; return failure(true);
case 2: break;
case 3:
if (!TopTy) return failure(true);
Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg3));
if (!V) return failure(true);
break;
default:
vector<unsigned> &args = *Raw.VarArgs;
for (unsigned i = 0, E = args.size(); i != E; ++i) {
const Type *ETy = MemAccessInst::getIndexedType(Raw.Ty, Idx, true);
const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
if (!ElTy) return failure(true);
Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
if (!V) return failure(true);
}
delete Raw.VarArgs;
break;
}
const Type *ElType = StoreInst::getIndexedType(Raw.Ty, Idx);
if (ElType == 0) return failure(true);
Res = new StoreInst(getValue(ElType, Raw.Arg1), getValue(Raw.Ty, Raw.Arg2),
Idx);
return false;
}
} // end switch(Raw.Opcode)
cerr << "Unrecognized instruction! " << Raw.Opcode
<< " ADDR = 0x" << (void*)Buf << endl;
return failure(true);
}
|