blob: 0de8f39c579dffd2b97d1f156081b3ad768bb963 [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
Chris Lattnerad333482002-08-14 18:24:09 +0000125 if (Raw.Opcode >= Instruction::FirstBinaryOp &&
126 Raw.Opcode < Instruction::NumBinaryOps && Raw.NumOperands == 2) {
Chris Lattner9f3d2762001-07-07 20:17:23 +0000127 Res = BinaryOperator::create((Instruction::BinaryOps)Raw.Opcode,
128 getValue(Raw.Ty, Raw.Arg1),
129 getValue(Raw.Ty, Raw.Arg2));
Chris Lattner00950542001-06-06 20:29:01 +0000130 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000131 }
132
133 Value *V;
134 switch (Raw.Opcode) {
Chris Lattner5ab1f872001-10-21 00:14:44 +0000135 case Instruction::Cast: {
136 V = getValue(Raw.Ty, Raw.Arg1);
137 const Type *Ty = getType(Raw.Arg2);
138 if (V == 0 || Ty == 0) { cerr << "Invalid cast!\n"; return true; }
139 Res = new CastInst(V, Ty);
Chris Lattner09083092001-07-08 04:57:15 +0000140 return false;
Chris Lattner5ab1f872001-10-21 00:14:44 +0000141 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000142 case Instruction::PHINode: {
Chris Lattner00950542001-06-06 20:29:01 +0000143 PHINode *PN = new PHINode(Raw.Ty);
144 switch (Raw.NumOperands) {
Chris Lattneree976f32001-06-11 15:04:40 +0000145 case 0:
146 case 1:
147 case 3: cerr << "Invalid phi node encountered!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000148 delete PN;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000149 return failure(true);
Chris Lattneree976f32001-06-11 15:04:40 +0000150 case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
Chris Lattnerb00c5822001-10-02 03:41:24 +0000151 cast<BasicBlock>(getValue(Type::LabelTy,Raw.Arg2)));
Chris Lattneree976f32001-06-11 15:04:40 +0000152 break;
Chris Lattner00950542001-06-06 20:29:01 +0000153 default:
Chris Lattneree976f32001-06-11 15:04:40 +0000154 PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
Chris Lattnerb00c5822001-10-02 03:41:24 +0000155 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
Chris Lattneree976f32001-06-11 15:04:40 +0000156 if (Raw.VarArgs->size() & 1) {
157 cerr << "PHI Node with ODD number of arguments!\n";
158 delete PN;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000159 return failure(true);
Chris Lattneree976f32001-06-11 15:04:40 +0000160 } else {
Chris Lattner00950542001-06-06 20:29:01 +0000161 vector<unsigned> &args = *Raw.VarArgs;
Chris Lattneree976f32001-06-11 15:04:40 +0000162 for (unsigned i = 0; i < args.size(); i+=2)
163 PN->addIncoming(getValue(Raw.Ty, args[i]),
Chris Lattnerb00c5822001-10-02 03:41:24 +0000164 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
Chris Lattner00950542001-06-06 20:29:01 +0000165 }
Chris Lattneree976f32001-06-11 15:04:40 +0000166 delete Raw.VarArgs;
167 break;
Chris Lattner00950542001-06-06 20:29:01 +0000168 }
169 Res = PN;
170 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000171 }
172
173 case Instruction::Shl:
174 case Instruction::Shr:
175 Res = new ShiftInst((Instruction::OtherOps)Raw.Opcode,
176 getValue(Raw.Ty, Raw.Arg1),
177 getValue(Type::UByteTy, Raw.Arg2));
178 return false;
179 case Instruction::Ret:
Chris Lattner00950542001-06-06 20:29:01 +0000180 if (Raw.NumOperands == 0) {
181 Res = new ReturnInst(); return false;
182 } else if (Raw.NumOperands == 1) {
183 Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false;
184 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000185 break;
186
187 case Instruction::Br:
Chris Lattner00950542001-06-06 20:29:01 +0000188 if (Raw.NumOperands == 1) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000189 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)));
Chris Lattner00950542001-06-06 20:29:01 +0000190 return false;
191 } else if (Raw.NumOperands == 3) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000192 Res = new BranchInst(cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg1)),
193 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)),
194 getValue(Type::BoolTy , Raw.Arg3));
Chris Lattner00950542001-06-06 20:29:01 +0000195 return false;
196 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000197 break;
198
199 case Instruction::Switch: {
Chris Lattner00950542001-06-06 20:29:01 +0000200 SwitchInst *I =
201 new SwitchInst(getValue(Raw.Ty, Raw.Arg1),
Chris Lattnerb00c5822001-10-02 03:41:24 +0000202 cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2)));
Chris Lattner00950542001-06-06 20:29:01 +0000203 Res = I;
204 if (Raw.NumOperands < 3) return false; // No destinations? Wierd.
205
206 if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) {
207 cerr << "Switch statement with odd number of arguments!\n";
208 delete I;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000209 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000210 }
211
212 vector<unsigned> &args = *Raw.VarArgs;
213 for (unsigned i = 0; i < args.size(); i += 2)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000214 I->dest_push_back(cast<Constant>(getValue(Raw.Ty, args[i])),
Chris Lattnerb00c5822001-10-02 03:41:24 +0000215 cast<BasicBlock>(getValue(Type::LabelTy, args[i+1])));
Chris Lattner00950542001-06-06 20:29:01 +0000216
217 delete Raw.VarArgs;
218 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000219 }
220
221 case Instruction::Call: {
Chris Lattner05950c32001-10-13 06:47:01 +0000222 Value *M = getValue(Raw.Ty, Raw.Arg1);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000223 if (M == 0) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000224
Chris Lattner05950c32001-10-13 06:47:01 +0000225 // Check to make sure we have a pointer to method type
Chris Lattnera48836b2002-06-05 17:38:28 +0000226 const PointerType *PTy = dyn_cast<PointerType>(M->getType());
Chris Lattner05950c32001-10-13 06:47:01 +0000227 if (PTy == 0) return failure(true);
Chris Lattnera48836b2002-06-05 17:38:28 +0000228 const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner05950c32001-10-13 06:47:01 +0000229 if (MTy == 0) return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000230
Chris Lattner05950c32001-10-13 06:47:01 +0000231 vector<Value *> Params;
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000232 const FunctionType::ParamTypes &PL = MTy->getParamTypes();
Chris Lattner05950c32001-10-13 06:47:01 +0000233
234 if (!MTy->isVarArg()) {
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000235 FunctionType::ParamTypes::const_iterator It = PL.begin();
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000236
237 switch (Raw.NumOperands) {
238 case 0: cerr << "Invalid call instruction encountered!\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000239 return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000240 case 1: break;
241 case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
242 case 3: Params.push_back(getValue(*It++, Raw.Arg2));
Chris Lattner3d3f2892001-07-28 17:50:18 +0000243 if (It == PL.end()) return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000244 Params.push_back(getValue(*It++, Raw.Arg3)); break;
245 default:
246 Params.push_back(getValue(*It++, Raw.Arg2));
247 {
248 vector<unsigned> &args = *Raw.VarArgs;
249 for (unsigned i = 0; i < args.size(); i++) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000250 if (It == PL.end()) return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000251 // TODO: Check getValue for null!
252 Params.push_back(getValue(*It++, args[i]));
253 }
Chris Lattner00950542001-06-06 20:29:01 +0000254 }
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000255 delete Raw.VarArgs;
256 }
Chris Lattner3d3f2892001-07-28 17:50:18 +0000257 if (It != PL.end()) return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000258 } else {
Chris Lattner05950c32001-10-13 06:47:01 +0000259 if (Raw.NumOperands > 2) {
260 vector<unsigned> &args = *Raw.VarArgs;
261 if (args.size() < 1) return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000262
Chris Lattner05950c32001-10-13 06:47:01 +0000263 if ((args.size() & 1) != 0)
264 return failure(true); // Must be pairs of type/value
265 for (unsigned i = 0; i < args.size(); i+=2) {
266 const Type *Ty = getType(args[i]);
267 if (Ty == 0)
268 return failure(true);
269
270 Value *V = getValue(Ty, args[i+1]);
271 if (V == 0) return failure(true);
272 Params.push_back(V);
273 }
274 delete Raw.VarArgs;
Chris Lattner00950542001-06-06 20:29:01 +0000275 }
Chris Lattner00950542001-06-06 20:29:01 +0000276 }
Chris Lattner00950542001-06-06 20:29:01 +0000277
278 Res = new CallInst(M, Params);
279 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000280 }
Chris Lattner05950c32001-10-13 06:47:01 +0000281 case Instruction::Invoke: {
282 Value *M = getValue(Raw.Ty, Raw.Arg1);
283 if (M == 0) return failure(true);
284
285 // Check to make sure we have a pointer to method type
Chris Lattnera48836b2002-06-05 17:38:28 +0000286 const PointerType *PTy = dyn_cast<PointerType>(M->getType());
Chris Lattner05950c32001-10-13 06:47:01 +0000287 if (PTy == 0) return failure(true);
Chris Lattnera48836b2002-06-05 17:38:28 +0000288 const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner05950c32001-10-13 06:47:01 +0000289 if (MTy == 0) return failure(true);
290
291 vector<Value *> Params;
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000292 const FunctionType::ParamTypes &PL = MTy->getParamTypes();
Chris Lattner05950c32001-10-13 06:47:01 +0000293 vector<unsigned> &args = *Raw.VarArgs;
294
295 BasicBlock *Normal, *Except;
296
297 if (!MTy->isVarArg()) {
298 if (Raw.NumOperands < 3) return failure(true);
299
300 Normal = cast<BasicBlock>(getValue(Type::LabelTy, Raw.Arg2));
301 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
302
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000303 FunctionType::ParamTypes::const_iterator It = PL.begin();
Chris Lattner05950c32001-10-13 06:47:01 +0000304 for (unsigned i = 1; i < args.size(); i++) {
305 if (It == PL.end()) return failure(true);
306 // TODO: Check getValue for null!
307 Params.push_back(getValue(*It++, args[i]));
308 }
309
310 if (It != PL.end()) return failure(true);
311 } else {
312 if (args.size() < 4) return failure(true);
313
314 Normal = cast<BasicBlock>(getValue(Type::LabelTy, args[0]));
315 Except = cast<BasicBlock>(getValue(Type::LabelTy, args[2]));
316
317 if ((args.size() & 1) != 0)
318 return failure(true); // Must be pairs of type/value
319 for (unsigned i = 4; i < args.size(); i+=2) {
320 // TODO: Check getValue for null!
321 Params.push_back(getValue(getType(args[i]), args[i+1]));
322 }
323 }
324
325 delete Raw.VarArgs;
326 Res = new InvokeInst(M, Normal, Except, Params);
327 return false;
328 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000329 case Instruction::Malloc:
Chris Lattner3d3f2892001-07-28 17:50:18 +0000330 if (Raw.NumOperands > 2) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000331 V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
332 Res = new MallocInst(Raw.Ty, V);
Chris Lattner00950542001-06-06 20:29:01 +0000333 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000334
335 case Instruction::Alloca:
Chris Lattner3d3f2892001-07-28 17:50:18 +0000336 if (Raw.NumOperands > 2) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000337 V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
338 Res = new AllocaInst(Raw.Ty, V);
Chris Lattner00950542001-06-06 20:29:01 +0000339 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000340
341 case Instruction::Free:
342 V = getValue(Raw.Ty, Raw.Arg1);
Chris Lattner9b625032002-05-06 16:15:30 +0000343 if (!isa<PointerType>(V->getType())) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000344 Res = new FreeInst(V);
345 return false;
346
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000347 case Instruction::Load:
348 case Instruction::GetElementPtr: {
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000349 vector<Value*> Idx;
350 if (!isa<PointerType>(Raw.Ty)) return failure(true);
Chris Lattnerb024ef52001-12-14 16:30:09 +0000351 const CompositeType *TopTy = dyn_cast<CompositeType>(Raw.Ty);
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000352
Chris Lattner027dcc52001-07-08 21:10:27 +0000353 switch (Raw.NumOperands) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000354 case 0: cerr << "Invalid load encountered!\n"; return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000355 case 1: break;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000356 case 2:
357 if (!TopTy) return failure(true);
358 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
359 if (!V) return failure(true);
360 break;
361 case 3: {
362 if (!TopTy) return failure(true);
363 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
364 if (!V) return failure(true);
365
Chris Lattnerb024ef52001-12-14 16:30:09 +0000366 const Type *ETy = MemAccessInst::getIndexedType(TopTy, Idx, true);
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000367 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
368 if (!ElTy) return failure(true);
369
370 Idx.push_back(V = getValue(ElTy->getIndexType(), Raw.Arg3));
371 if (!V) return failure(true);
372 break;
373 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000374 default:
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000375 if (!TopTy) return failure(true);
376 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
377 if (!V) return failure(true);
378
Chris Lattner027dcc52001-07-08 21:10:27 +0000379 vector<unsigned> &args = *Raw.VarArgs;
380 for (unsigned i = 0, E = args.size(); i != E; ++i) {
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000381 const Type *ETy = MemAccessInst::getIndexedType(Raw.Ty, Idx, true);
382 const CompositeType *ElTy = dyn_cast_or_null<CompositeType>(ETy);
383 if (!ElTy) return failure(true);
384 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
385 if (!V) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000386 }
387 delete Raw.VarArgs;
388 break;
389 }
Chris Lattner77a316a2001-11-12 21:48:38 +0000390
391 if (Raw.Opcode == Instruction::Load) {
392 assert(MemAccessInst::getIndexedType(Raw.Ty, Idx) &&
Chris Lattnere6e8b282001-11-12 21:52:57 +0000393 "Bad indices for Load!");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000394 Res = new LoadInst(getValue(Raw.Ty, Raw.Arg1), Idx);
Chris Lattner77a316a2001-11-12 21:48:38 +0000395 } else if (Raw.Opcode == Instruction::GetElementPtr)
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000396 Res = new GetElementPtrInst(getValue(Raw.Ty, Raw.Arg1), Idx);
397 else
398 abort();
399 return false;
400 }
401 case Instruction::Store: {
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000402 vector<Value*> Idx;
403 if (!isa<PointerType>(Raw.Ty)) return failure(true);
Chris Lattnerb024ef52001-12-14 16:30:09 +0000404 const CompositeType *TopTy = dyn_cast<CompositeType>(Raw.Ty);
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000405
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000406 switch (Raw.NumOperands) {
407 case 0:
Chris Lattner3d3f2892001-07-28 17:50:18 +0000408 case 1: cerr << "Invalid store encountered!\n"; return failure(true);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000409 case 2: break;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000410 case 3:
411 if (!TopTy) return failure(true);
412 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg3));
413 if (!V) return failure(true);
414 break;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000415 default:
416 vector<unsigned> &args = *Raw.VarArgs;
Chris Lattnerb024ef52001-12-14 16:30:09 +0000417 const CompositeType *ElTy = TopTy;
418 unsigned i, E;
419 for (i = 0, E = args.size(); ElTy && i != E; ++i) {
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000420 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
421 if (!V) return failure(true);
Chris Lattnerb024ef52001-12-14 16:30:09 +0000422
423 const Type *ETy = MemAccessInst::getIndexedType(Raw.Ty, Idx, true);
424 ElTy = dyn_cast_or_null<CompositeType>(ETy);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000425 }
Chris Lattnerb024ef52001-12-14 16:30:09 +0000426 if (i != E)
427 return failure(true); // didn't use up all of the indices!
428
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000429 delete Raw.VarArgs;
430 break;
431 }
432
433 const Type *ElType = StoreInst::getIndexedType(Raw.Ty, Idx);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000434 if (ElType == 0) return failure(true);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000435 Res = new StoreInst(getValue(ElType, Raw.Arg1), getValue(Raw.Ty, Raw.Arg2),
436 Idx);
Chris Lattner00950542001-06-06 20:29:01 +0000437 return false;
438 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000439 } // end switch(Raw.Opcode)
Chris Lattner00950542001-06-06 20:29:01 +0000440
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000441 cerr << "Unrecognized instruction! " << Raw.Opcode
Chris Lattner697954c2002-01-20 22:54:45 +0000442 << " ADDR = 0x" << (void*)Buf << "\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000443 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000444}