blob: 0916b2b876aaa8847606cebcd9161a3db129cb4b [file] [log] [blame]
Chris Lattner05950c32001-10-13 06:47:01 +00001//===- ReadInst.cpp - Code to read an instruction from bytecode -----------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This file defines the mechanism to read an instruction from a bytecode
4// stream.
5//
6// Note that this library should be as fast as possible, reentrant, and
7// threadsafe!!
8//
9// TODO: Change from getValue(Raw.Arg1) etc, to getArg(Raw, 1)
10// Make it check type, so that casts are checked.
11//
Chris Lattner05950c32001-10-13 06:47:01 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner7061dc52001-12-03 18:02:31 +000014#include "ReaderInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000015#include "llvm/iTerminators.h"
16#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000017#include "llvm/iPHINode.h"
18#include "llvm/iOther.h"
Chris Lattner697954c2002-01-20 22:54:45 +000019#include <iostream>
20using std::vector;
21using std::cerr;
Chris Lattner00950542001-06-06 20:29:01 +000022
23bool BytecodeParser::ParseRawInst(const uchar *&Buf, const uchar *EndBuf,
24 RawInst &Result) {
25 unsigned Op, Typ;
Chris Lattner3d3f2892001-07-28 17:50:18 +000026 if (read(Buf, EndBuf, Op)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner2b9f6002001-10-23 03:21:10 +000028 // bits Instruction format: Common to all formats
29 // --------------------------
30 // 01-00: Opcode type, fixed to 1.
31 // 07-02: Opcode
32 Result.NumOperands = (Op >> 0) & 03;
33 Result.Opcode = (Op >> 2) & 63;
Chris Lattner00950542001-06-06 20:29:01 +000034
35 switch (Result.NumOperands) {
36 case 1:
Chris Lattner2b9f6002001-10-23 03:21:10 +000037 // bits Instruction format:
38 // --------------------------
39 // 19-08: Resulting type plane
40 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
41 //
42 Result.Ty = getType((Op >> 8) & 4095);
43 Result.Arg1 = (Op >> 20) & 4095;
Chris Lattner00950542001-06-06 20:29:01 +000044 if (Result.Arg1 == 4095) // Handle special encoding for 0 operands...
45 Result.NumOperands = 0;
46 break;
47 case 2:
Chris Lattner2b9f6002001-10-23 03:21:10 +000048 // bits Instruction format:
49 // --------------------------
50 // 15-08: Resulting type plane
51 // 23-16: Operand #1
52 // 31-24: Operand #2
53 //
54 Result.Ty = getType((Op >> 8) & 255);
55 Result.Arg1 = (Op >> 16) & 255;
56 Result.Arg2 = (Op >> 24) & 255;
Chris Lattner00950542001-06-06 20:29:01 +000057 break;
58 case 3:
Chris Lattner2b9f6002001-10-23 03:21:10 +000059 // bits Instruction format:
60 // --------------------------
61 // 13-08: Resulting type plane
62 // 19-14: Operand #1
63 // 25-20: Operand #2
64 // 31-26: Operand #3
65 //
66 Result.Ty = getType((Op >> 8) & 63);
67 Result.Arg1 = (Op >> 14) & 63;
68 Result.Arg2 = (Op >> 20) & 63;
69 Result.Arg3 = (Op >> 26) & 63;
Chris Lattner00950542001-06-06 20:29:01 +000070 break;
71 case 0:
72 Buf -= 4; // Hrm, try this again...
Chris Lattner3d3f2892001-07-28 17:50:18 +000073 if (read_vbr(Buf, EndBuf, Result.Opcode)) return failure(true);
Chris Lattner2b9f6002001-10-23 03:21:10 +000074 Result.Opcode >>= 2;
Chris Lattner3d3f2892001-07-28 17:50:18 +000075 if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000076 Result.Ty = getType(Typ);
Chris Lattner5ab1f872001-10-21 00:14:44 +000077 if (Result.Ty == 0) return failure(true);
Chris Lattner3d3f2892001-07-28 17:50:18 +000078 if (read_vbr(Buf, EndBuf, Result.NumOperands)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000079
80 switch (Result.NumOperands) {
81 case 0:
82 cerr << "Zero Arg instr found!\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +000083 return failure(true); // This encoding is invalid!
Chris Lattner00950542001-06-06 20:29:01 +000084 case 1:
Chris Lattner3d3f2892001-07-28 17:50:18 +000085 if (read_vbr(Buf, EndBuf, Result.Arg1)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000086 break;
87 case 2:
88 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +000089 read_vbr(Buf, EndBuf, Result.Arg2)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000090 break;
91 case 3:
92 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
93 read_vbr(Buf, EndBuf, Result.Arg2) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +000094 read_vbr(Buf, EndBuf, Result.Arg3)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000095 break;
96 default:
97 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +000098 read_vbr(Buf, EndBuf, Result.Arg2)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000099
100 // Allocate a vector to hold arguments 3, 4, 5, 6 ...
101 Result.VarArgs = new vector<unsigned>(Result.NumOperands-2);
102 for (unsigned a = 0; a < Result.NumOperands-2; a++)
Chris Lattner3d3f2892001-07-28 17:50:18 +0000103 if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000104 break;
105 }
Chris Lattner3d3f2892001-07-28 17:50:18 +0000106 if (align32(Buf, EndBuf)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000107 break;
108 }
109
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000110#if 0
111 cerr << "NO: " << Result.NumOperands << " opcode: " << Result.Opcode
Chris Lattner1d670cc2001-09-07 16:37:43 +0000112 << " Ty: " << Result.Ty->getDescription() << " arg1: " << Result.Arg1
Chris Lattner697954c2002-01-20 22:54:45 +0000113 << " arg2: " << Result.Arg2 << " arg3: " << Result.Arg3 << "\n";
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000114#endif
Chris Lattner00950542001-06-06 20:29:01 +0000115 return false;
116}
117
118
119bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
120 Instruction *&Res) {
121 RawInst Raw;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000122 if (ParseRawInst(Buf, EndBuf, Raw))
123 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000124
125 if (Raw.Opcode >= Instruction::FirstUnaryOp &&
126 Raw.Opcode < Instruction::NumUnaryOps && Raw.NumOperands == 1) {
Chris Lattner9f3d2762001-07-07 20:17:23 +0000127 Res = UnaryOperator::create((Instruction::UnaryOps)Raw.Opcode,
128 getValue(Raw.Ty,Raw.Arg1));
Chris Lattner00950542001-06-06 20:29:01 +0000129 return false;
130 } else if (Raw.Opcode >= Instruction::FirstBinaryOp &&
131 Raw.Opcode < Instruction::NumBinaryOps && Raw.NumOperands == 2) {
Chris Lattner9f3d2762001-07-07 20:17:23 +0000132 Res = BinaryOperator::create((Instruction::BinaryOps)Raw.Opcode,
133 getValue(Raw.Ty, Raw.Arg1),
134 getValue(Raw.Ty, Raw.Arg2));
Chris Lattner00950542001-06-06 20:29:01 +0000135 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000136 }
137
138 Value *V;
139 switch (Raw.Opcode) {
Chris Lattner5ab1f872001-10-21 00:14:44 +0000140 case Instruction::Cast: {
141 V = getValue(Raw.Ty, Raw.Arg1);
142 const Type *Ty = getType(Raw.Arg2);
143 if (V == 0 || Ty == 0) { cerr << "Invalid cast!\n"; return true; }
144 Res = new CastInst(V, Ty);
Chris Lattner09083092001-07-08 04:57:15 +0000145 return false;
Chris Lattner5ab1f872001-10-21 00:14:44 +0000146 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000147 case Instruction::PHINode: {
Chris Lattner00950542001-06-06 20:29:01 +0000148 PHINode *PN = new PHINode(Raw.Ty);
149 switch (Raw.NumOperands) {
Chris Lattneree976f32001-06-11 15:04:40 +0000150 case 0:
151 case 1:
152 case 3: cerr << "Invalid phi node encountered!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000153 delete PN;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000154 return failure(true);
Chris Lattneree976f32001-06-11 15:04:40 +0000155 case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
Chris Lattnerb00c5822001-10-02 03:41:24 +0000156 cast<BasicBlock>(getValue(Type::LabelTy,Raw.Arg2)));
Chris Lattneree976f32001-06-11 15:04:40 +0000157 break;
Chris Lattner00950542001-06-06 20:29:01 +0000158 default:
Chris Lattneree976f32001-06-11 15:04:40 +0000159 PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
Chris Lattnerb00c5822001-10-02 03:41:24 +0000160 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
Chris Lattneree976f32001-06-11 15:04:40 +0000161 if (Raw.VarArgs->size() & 1) {
162 cerr << "PHI Node with ODD number of arguments!\n";
163 delete PN;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000164 return failure(true);
Chris Lattneree976f32001-06-11 15:04:40 +0000165 } else {
Chris Lattner00950542001-06-06 20:29:01 +0000166 vector<unsigned> &args = *Raw.VarArgs;
Chris Lattneree976f32001-06-11 15:04:40 +0000167 for (unsigned i = 0; i < args.size(); i+=2)
168 PN->addIncoming(getValue(Raw.Ty, args[i]),
Chris Lattnerb00c5822001-10-02 03:41:24 +0000169 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
Chris Lattner00950542001-06-06 20:29:01 +0000170 }
Chris Lattneree976f32001-06-11 15:04:40 +0000171 delete Raw.VarArgs;
172 break;
Chris Lattner00950542001-06-06 20:29:01 +0000173 }
174 Res = PN;
175 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000176 }
177
178 case Instruction::Shl:
179 case Instruction::Shr:
180 Res = new ShiftInst((Instruction::OtherOps)Raw.Opcode,
181 getValue(Raw.Ty, Raw.Arg1),
182 getValue(Type::UByteTy, Raw.Arg2));
183 return false;
184 case Instruction::Ret:
Chris Lattner00950542001-06-06 20:29:01 +0000185 if (Raw.NumOperands == 0) {
186 Res = new ReturnInst(); return false;
187 } else if (Raw.NumOperands == 1) {
188 Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false;
189 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000190 break;
191
192 case Instruction::Br:
Chris Lattner00950542001-06-06 20:29:01 +0000193 if (Raw.NumOperands == 1) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000194 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)));
Chris Lattner00950542001-06-06 20:29:01 +0000195 return false;
196 } else if (Raw.NumOperands == 3) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000197 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)),
198 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)),
199 getValue(Type::BoolTy , Raw.Arg3));
Chris Lattner00950542001-06-06 20:29:01 +0000200 return false;
201 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000202 break;
203
204 case Instruction::Switch: {
Chris Lattner00950542001-06-06 20:29:01 +0000205 SwitchInst *I =
206 new SwitchInst(getValue(Raw.Ty, Raw.Arg1),
Chris Lattnerb00c5822001-10-02 03:41:24 +0000207 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
Chris Lattner00950542001-06-06 20:29:01 +0000208 Res = I;
209 if (Raw.NumOperands < 3) return false; // No destinations? Wierd.
210
211 if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) {
212 cerr << "Switch statement with odd number of arguments!\n";
213 delete I;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000214 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000215 }
216
217 vector<unsigned> &args = *Raw.VarArgs;
218 for (unsigned i = 0; i < args.size(); i += 2)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000219 I->dest_push_back(cast<Constant>(getValue(Raw.Ty, args[i])),
Chris Lattnerb00c5822001-10-02 03:41:24 +0000220 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
Chris Lattner00950542001-06-06 20:29:01 +0000221
222 delete Raw.VarArgs;
223 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000224 }
225
226 case Instruction::Call: {
Chris Lattner05950c32001-10-13 06:47:01 +0000227 Value *M = getValue(Raw.Ty, Raw.Arg1);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000228 if (M == 0) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000229
Chris Lattner05950c32001-10-13 06:47:01 +0000230 // Check to make sure we have a pointer to method type
Chris Lattnera48836b2002-06-05 17:38:28 +0000231 const PointerType *PTy = dyn_cast<PointerType>(M->getType());
Chris Lattner05950c32001-10-13 06:47:01 +0000232 if (PTy == 0) return failure(true);
Chris Lattnera48836b2002-06-05 17:38:28 +0000233 const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner05950c32001-10-13 06:47:01 +0000234 if (MTy == 0) return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000235
Chris Lattner05950c32001-10-13 06:47:01 +0000236 vector<Value *> Params;
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000237 const FunctionType::ParamTypes &PL = MTy->getParamTypes();
Chris Lattner05950c32001-10-13 06:47:01 +0000238
239 if (!MTy->isVarArg()) {
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000240 FunctionType::ParamTypes::const_iterator It = PL.begin();
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000241
242 switch (Raw.NumOperands) {
243 case 0: cerr << "Invalid call instruction encountered!\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000244 return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000245 case 1: break;
246 case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
247 case 3: Params.push_back(getValue(*It++, Raw.Arg2));
Chris Lattner3d3f2892001-07-28 17:50:18 +0000248 if (It == PL.end()) return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000249 Params.push_back(getValue(*It++, Raw.Arg3)); break;
250 default:
251 Params.push_back(getValue(*It++, Raw.Arg2));
252 {
253 vector<unsigned> &args = *Raw.VarArgs;
254 for (unsigned i = 0; i < args.size(); i++) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000255 if (It == PL.end()) return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000256 // TODO: Check getValue for null!
257 Params.push_back(getValue(*It++, args[i]));
258 }
Chris Lattner00950542001-06-06 20:29:01 +0000259 }
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000260 delete Raw.VarArgs;
261 }
Chris Lattner3d3f2892001-07-28 17:50:18 +0000262 if (It != PL.end()) return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000263 } else {
Chris Lattner05950c32001-10-13 06:47:01 +0000264 if (Raw.NumOperands > 2) {
265 vector<unsigned> &args = *Raw.VarArgs;
266 if (args.size() < 1) return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000267
Chris Lattner05950c32001-10-13 06:47:01 +0000268 if ((args.size() & 1) != 0)
269 return failure(true); // Must be pairs of type/value
270 for (unsigned i = 0; i < args.size(); i+=2) {
271 const Type *Ty = getType(args[i]);
272 if (Ty == 0)
273 return failure(true);
274
275 Value *V = getValue(Ty, args[i+1]);
276 if (V == 0) return failure(true);
277 Params.push_back(V);
278 }
279 delete Raw.VarArgs;
Chris Lattner00950542001-06-06 20:29:01 +0000280 }
Chris Lattner00950542001-06-06 20:29:01 +0000281 }
Chris Lattner00950542001-06-06 20:29:01 +0000282
283 Res = new CallInst(M, Params);
284 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000285 }
Chris Lattner05950c32001-10-13 06:47:01 +0000286 case Instruction::Invoke: {
287 Value *M = getValue(Raw.Ty, Raw.Arg1);
288 if (M == 0) return failure(true);
289
290 // Check to make sure we have a pointer to method type
Chris Lattnera48836b2002-06-05 17:38:28 +0000291 const PointerType *PTy = dyn_cast<PointerType>(M->getType());
Chris Lattner05950c32001-10-13 06:47:01 +0000292 if (PTy == 0) return failure(true);
Chris Lattnera48836b2002-06-05 17:38:28 +0000293 const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner05950c32001-10-13 06:47:01 +0000294 if (MTy == 0) return failure(true);
295
296 vector<Value *> Params;
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000297 const FunctionType::ParamTypes &PL = MTy->getParamTypes();
Chris Lattner05950c32001-10-13 06:47:01 +0000298 vector<unsigned> &args = *Raw.VarArgs;
299
300 BasicBlock *Normal, *Except;
301
302 if (!MTy->isVarArg()) {
303 if (Raw.NumOperands < 3) return failure(true);
304
305 Normal = cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2));
306 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
307
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000308 FunctionType::ParamTypes::const_iterator It = PL.begin();
Chris Lattner05950c32001-10-13 06:47:01 +0000309 for (unsigned i = 1; i < args.size(); i++) {
310 if (It == PL.end()) return failure(true);
311 // TODO: Check getValue for null!
312 Params.push_back(getValue(*It++, args[i]));
313 }
314
315 if (It != PL.end()) return failure(true);
316 } else {
317 if (args.size() < 4) return failure(true);
318
319 Normal = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
320 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[2]));
321
322 if ((args.size() & 1) != 0)
323 return failure(true); // Must be pairs of type/value
324 for (unsigned i = 4; i < args.size(); i+=2) {
325 // TODO: Check getValue for null!
326 Params.push_back(getValue(getType(args[i]), args[i+1]));
327 }
328 }
329
330 delete Raw.VarArgs;
331 Res = new InvokeInst(M, Normal, Except, Params);
332 return false;
333 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000334 case Instruction::Malloc:
Chris Lattner3d3f2892001-07-28 17:50:18 +0000335 if (Raw.NumOperands > 2) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000336 V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
337 Res = new MallocInst(Raw.Ty, V);
Chris Lattner00950542001-06-06 20:29:01 +0000338 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000339
340 case Instruction::Alloca:
Chris Lattner3d3f2892001-07-28 17:50:18 +0000341 if (Raw.NumOperands > 2) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000342 V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
343 Res = new AllocaInst(Raw.Ty, V);
Chris Lattner00950542001-06-06 20:29:01 +0000344 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000345
346 case Instruction::Free:
347 V = getValue(Raw.Ty, Raw.Arg1);
Chris Lattner9b625032002-05-06 16:15:30 +0000348 if (!isa<PointerType>(V->getType())) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000349 Res = new FreeInst(V);
350 return false;
351
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000352 case Instruction::Load:
353 case Instruction::GetElementPtr: {
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000354 vector<Value*> Idx;
355 if (!isa<PointerType>(Raw.Ty)) return failure(true);
Chris Lattnerb024ef52001-12-14 16:30:09 +0000356 const CompositeType *TopTy = dyn_cast<CompositeType>(Raw.Ty);
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000357
Chris Lattner027dcc52001-07-08 21:10:27 +0000358 switch (Raw.NumOperands) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000359 case 0: cerr << "Invalid load encountered!\n"; return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000360 case 1: break;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000361 case 2:
362 if (!TopTy) return failure(true);
363 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
364 if (!V) return failure(true);
365 break;
366 case 3: {
367 if (!TopTy) return failure(true);
368 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
369 if (!V) return failure(true);
370
Chris Lattnerb024ef52001-12-14 16:30:09 +0000371 const Type *ETy = MemAccessInst::getIndexedType(TopTy, Idx, true);
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000372 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
373 if (!ElTy) return failure(true);
374
375 Idx.push_back(V = getValue(ElTy->getIndexType(), Raw.Arg3));
376 if (!V) return failure(true);
377 break;
378 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000379 default:
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000380 if (!TopTy) return failure(true);
381 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
382 if (!V) return failure(true);
383
Chris Lattner027dcc52001-07-08 21:10:27 +0000384 vector<unsigned> &args = *Raw.VarArgs;
385 for (unsigned i = 0, E = args.size(); i != E; ++i) {
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000386 const Type *ETy = MemAccessInst::getIndexedType(Raw.Ty, Idx, true);
387 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
388 if (!ElTy) return failure(true);
389 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
390 if (!V) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000391 }
392 delete Raw.VarArgs;
393 break;
394 }
Chris Lattner77a316a2001-11-12 21:48:38 +0000395
396 if (Raw.Opcode == Instruction::Load) {
397 assert(MemAccessInst::getIndexedType(Raw.Ty, Idx) &&
Chris Lattnere6e8b282001-11-12 21:52:57 +0000398 "Bad indices for Load!");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000399 Res = new LoadInst(getValue(Raw.Ty, Raw.Arg1), Idx);
Chris Lattner77a316a2001-11-12 21:48:38 +0000400 } else if (Raw.Opcode == Instruction::GetElementPtr)
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000401 Res = new GetElementPtrInst(getValue(Raw.Ty, Raw.Arg1), Idx);
402 else
403 abort();
404 return false;
405 }
406 case Instruction::Store: {
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000407 vector<Value*> Idx;
408 if (!isa<PointerType>(Raw.Ty)) return failure(true);
Chris Lattnerb024ef52001-12-14 16:30:09 +0000409 const CompositeType *TopTy = dyn_cast<CompositeType>(Raw.Ty);
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000410
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000411 switch (Raw.NumOperands) {
412 case 0:
Chris Lattner3d3f2892001-07-28 17:50:18 +0000413 case 1: cerr << "Invalid store encountered!\n"; return failure(true);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000414 case 2: break;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000415 case 3:
416 if (!TopTy) return failure(true);
417 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg3));
418 if (!V) return failure(true);
419 break;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000420 default:
421 vector<unsigned> &args = *Raw.VarArgs;
Chris Lattnerb024ef52001-12-14 16:30:09 +0000422 const CompositeType *ElTy = TopTy;
423 unsigned i, E;
424 for (i = 0, E = args.size(); ElTy && i != E; ++i) {
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000425 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
426 if (!V) return failure(true);
Chris Lattnerb024ef52001-12-14 16:30:09 +0000427
428 const Type *ETy = MemAccessInst::getIndexedType(Raw.Ty, Idx, true);
429 ElTy = dyn_cast_or_null<CompositeType>(ETy);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000430 }
Chris Lattnerb024ef52001-12-14 16:30:09 +0000431 if (i != E)
432 return failure(true); // didn't use up all of the indices!
433
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000434 delete Raw.VarArgs;
435 break;
436 }
437
438 const Type *ElType = StoreInst::getIndexedType(Raw.Ty, Idx);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000439 if (ElType == 0) return failure(true);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000440 Res = new StoreInst(getValue(ElType, Raw.Arg1), getValue(Raw.Ty, Raw.Arg2),
441 Idx);
Chris Lattner00950542001-06-06 20:29:01 +0000442 return false;
443 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000444 } // end switch(Raw.Opcode)
Chris Lattner00950542001-06-06 20:29:01 +0000445
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000446 cerr << "Unrecognized instruction! " << Raw.Opcode
Chris Lattner697954c2002-01-20 22:54:45 +0000447 << " ADDR = 0x" << (void*)Buf << "\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000448 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000449}