blob: 123a94c9dc5752af086b28bfbe8a1c92ee312ae1 [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 +000019using std::vector;
20using std::cerr;
Chris Lattner00950542001-06-06 20:29:01 +000021
22bool BytecodeParser::ParseRawInst(const uchar *&Buf, const uchar *EndBuf,
23 RawInst &Result) {
24 unsigned Op, Typ;
Chris Lattner74734132002-08-17 22:01:27 +000025 if (read(Buf, EndBuf, Op)) return true;
Chris Lattner00950542001-06-06 20:29:01 +000026
Chris Lattner2b9f6002001-10-23 03:21:10 +000027 // bits Instruction format: Common to all formats
28 // --------------------------
29 // 01-00: Opcode type, fixed to 1.
30 // 07-02: Opcode
31 Result.NumOperands = (Op >> 0) & 03;
32 Result.Opcode = (Op >> 2) & 63;
Chris Lattner00950542001-06-06 20:29:01 +000033
34 switch (Result.NumOperands) {
35 case 1:
Chris Lattner2b9f6002001-10-23 03:21:10 +000036 // bits Instruction format:
37 // --------------------------
38 // 19-08: Resulting type plane
39 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
40 //
41 Result.Ty = getType((Op >> 8) & 4095);
42 Result.Arg1 = (Op >> 20) & 4095;
Chris Lattner00950542001-06-06 20:29:01 +000043 if (Result.Arg1 == 4095) // Handle special encoding for 0 operands...
44 Result.NumOperands = 0;
45 break;
46 case 2:
Chris Lattner2b9f6002001-10-23 03:21:10 +000047 // bits Instruction format:
48 // --------------------------
49 // 15-08: Resulting type plane
50 // 23-16: Operand #1
51 // 31-24: Operand #2
52 //
53 Result.Ty = getType((Op >> 8) & 255);
54 Result.Arg1 = (Op >> 16) & 255;
55 Result.Arg2 = (Op >> 24) & 255;
Chris Lattner00950542001-06-06 20:29:01 +000056 break;
57 case 3:
Chris Lattner2b9f6002001-10-23 03:21:10 +000058 // bits Instruction format:
59 // --------------------------
60 // 13-08: Resulting type plane
61 // 19-14: Operand #1
62 // 25-20: Operand #2
63 // 31-26: Operand #3
64 //
65 Result.Ty = getType((Op >> 8) & 63);
66 Result.Arg1 = (Op >> 14) & 63;
67 Result.Arg2 = (Op >> 20) & 63;
68 Result.Arg3 = (Op >> 26) & 63;
Chris Lattner00950542001-06-06 20:29:01 +000069 break;
70 case 0:
71 Buf -= 4; // Hrm, try this again...
Chris Lattner74734132002-08-17 22:01:27 +000072 if (read_vbr(Buf, EndBuf, Result.Opcode)) return true;
Chris Lattner2b9f6002001-10-23 03:21:10 +000073 Result.Opcode >>= 2;
Chris Lattner74734132002-08-17 22:01:27 +000074 if (read_vbr(Buf, EndBuf, Typ)) return true;
Chris Lattner00950542001-06-06 20:29:01 +000075 Result.Ty = getType(Typ);
Chris Lattner74734132002-08-17 22:01:27 +000076 if (Result.Ty == 0) return true;
77 if (read_vbr(Buf, EndBuf, Result.NumOperands)) return true;
Chris Lattner00950542001-06-06 20:29:01 +000078
79 switch (Result.NumOperands) {
80 case 0:
81 cerr << "Zero Arg instr found!\n";
Chris Lattner74734132002-08-17 22:01:27 +000082 return true; // This encoding is invalid!
Chris Lattner00950542001-06-06 20:29:01 +000083 case 1:
Chris Lattner74734132002-08-17 22:01:27 +000084 if (read_vbr(Buf, EndBuf, Result.Arg1)) return true;
Chris Lattner00950542001-06-06 20:29:01 +000085 break;
86 case 2:
87 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
Chris Lattner74734132002-08-17 22:01:27 +000088 read_vbr(Buf, EndBuf, Result.Arg2)) return true;
Chris Lattner00950542001-06-06 20:29:01 +000089 break;
90 case 3:
91 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
92 read_vbr(Buf, EndBuf, Result.Arg2) ||
Chris Lattner74734132002-08-17 22:01:27 +000093 read_vbr(Buf, EndBuf, Result.Arg3)) return true;
Chris Lattner00950542001-06-06 20:29:01 +000094 break;
95 default:
96 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
Chris Lattner74734132002-08-17 22:01:27 +000097 read_vbr(Buf, EndBuf, Result.Arg2)) return true;
Chris Lattner00950542001-06-06 20:29:01 +000098
99 // Allocate a vector to hold arguments 3, 4, 5, 6 ...
100 Result.VarArgs = new vector<unsigned>(Result.NumOperands-2);
101 for (unsigned a = 0; a < Result.NumOperands-2; a++)
Chris Lattner74734132002-08-17 22:01:27 +0000102 if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000103 break;
104 }
Chris Lattner74734132002-08-17 22:01:27 +0000105 if (align32(Buf, EndBuf)) return true;
Chris Lattner00950542001-06-06 20:29:01 +0000106 break;
107 }
108
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000109#if 0
110 cerr << "NO: " << Result.NumOperands << " opcode: " << Result.Opcode
Chris Lattner1d670cc2001-09-07 16:37:43 +0000111 << " Ty: " << Result.Ty->getDescription() << " arg1: " << Result.Arg1
Chris Lattner697954c2002-01-20 22:54:45 +0000112 << " arg2: " << Result.Arg2 << " arg3: " << Result.Arg3 << "\n";
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000113#endif
Chris Lattner00950542001-06-06 20:29:01 +0000114 return false;
115}
116
117
118bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
Chris Lattner352eef72002-08-21 22:55:27 +0000119 Instruction *&Res,
120 BasicBlock *BB /*HACK*/) {
Chris Lattner00950542001-06-06 20:29:01 +0000121 RawInst Raw;
Chris Lattner2b9f6002001-10-23 03:21:10 +0000122 if (ParseRawInst(Buf, EndBuf, Raw))
Chris Lattner74734132002-08-17 22:01:27 +0000123 return 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 Lattner74734132002-08-17 22:01:27 +0000149 return 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 Lattner74734132002-08-17 22:01:27 +0000159 return 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 Lattner74734132002-08-17 22:01:27 +0000209 return 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 Lattner74734132002-08-17 22:01:27 +0000223 if (M == 0) return 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 Lattner74734132002-08-17 22:01:27 +0000227 if (PTy == 0) return true;
Chris Lattnera48836b2002-06-05 17:38:28 +0000228 const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner74734132002-08-17 22:01:27 +0000229 if (MTy == 0) return 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 Lattner74734132002-08-17 22:01:27 +0000239 return 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 Lattner74734132002-08-17 22:01:27 +0000243 if (It == PL.end()) return 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 Lattner74734132002-08-17 22:01:27 +0000250 if (It == PL.end()) return 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 Lattner74734132002-08-17 22:01:27 +0000257 if (It != PL.end()) return 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;
Chris Lattner74734132002-08-17 22:01:27 +0000261 if (args.size() < 1) return true;
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000262
Chris Lattner05950c32001-10-13 06:47:01 +0000263 if ((args.size() & 1) != 0)
Chris Lattner74734132002-08-17 22:01:27 +0000264 return true; // Must be pairs of type/value
Chris Lattner05950c32001-10-13 06:47:01 +0000265 for (unsigned i = 0; i < args.size(); i+=2) {
266 const Type *Ty = getType(args[i]);
267 if (Ty == 0)
Chris Lattner74734132002-08-17 22:01:27 +0000268 return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000269
270 Value *V = getValue(Ty, args[i+1]);
Chris Lattner74734132002-08-17 22:01:27 +0000271 if (V == 0) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000272 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);
Chris Lattner74734132002-08-17 22:01:27 +0000283 if (M == 0) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000284
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 Lattner74734132002-08-17 22:01:27 +0000287 if (PTy == 0) return true;
Chris Lattnera48836b2002-06-05 17:38:28 +0000288 const FunctionType *MTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner74734132002-08-17 22:01:27 +0000289 if (MTy == 0) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000290
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()) {
Chris Lattner74734132002-08-17 22:01:27 +0000298 if (Raw.NumOperands < 3) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000299
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++) {
Chris Lattner74734132002-08-17 22:01:27 +0000305 if (It == PL.end()) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000306 // TODO: Check getValue for null!
307 Params.push_back(getValue(*It++, args[i]));
308 }
309
Chris Lattner74734132002-08-17 22:01:27 +0000310 if (It != PL.end()) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000311 } else {
Chris Lattner74734132002-08-17 22:01:27 +0000312 if (args.size() < 4) return true;
Chris Lattner05950c32001-10-13 06:47:01 +0000313
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)
Chris Lattner74734132002-08-17 22:01:27 +0000318 return true; // Must be pairs of type/value
Chris Lattner05950c32001-10-13 06:47:01 +0000319 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 Lattner74734132002-08-17 22:01:27 +0000330 if (Raw.NumOperands > 2) return 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 Lattner74734132002-08-17 22:01:27 +0000336 if (Raw.NumOperands > 2) return 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 Lattner74734132002-08-17 22:01:27 +0000343 if (!isa<PointerType>(V->getType())) return 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;
Chris Lattner74734132002-08-17 22:01:27 +0000350 if (!isa<PointerType>(Raw.Ty)) return 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 Lattner74734132002-08-17 22:01:27 +0000354 case 0: cerr << "Invalid load encountered!\n"; return true;
Chris Lattner027dcc52001-07-08 21:10:27 +0000355 case 1: break;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000356 case 2:
Chris Lattner74734132002-08-17 22:01:27 +0000357 if (!TopTy) return true;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000358 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
Chris Lattner74734132002-08-17 22:01:27 +0000359 if (!V) return true;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000360 break;
361 case 3: {
Chris Lattner74734132002-08-17 22:01:27 +0000362 if (!TopTy) return true;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000363 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
Chris Lattner74734132002-08-17 22:01:27 +0000364 if (!V) return true;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000365
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);
Chris Lattner74734132002-08-17 22:01:27 +0000368 if (!ElTy) return true;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000369
370 Idx.push_back(V = getValue(ElTy->getIndexType(), Raw.Arg3));
Chris Lattner74734132002-08-17 22:01:27 +0000371 if (!V) return true;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000372 break;
373 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000374 default:
Chris Lattner74734132002-08-17 22:01:27 +0000375 if (!TopTy) return true;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000376 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg2));
Chris Lattner74734132002-08-17 22:01:27 +0000377 if (!V) return true;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000378
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);
Chris Lattner74734132002-08-17 22:01:27 +0000383 if (!ElTy) return true;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000384 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
Chris Lattner74734132002-08-17 22:01:27 +0000385 if (!V) return 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) {
Chris Lattner352eef72002-08-21 22:55:27 +0000392 Value *Src = getValue(Raw.Ty, Raw.Arg1);
393 if (!Idx.empty()) {
394 cerr << "WARNING: Bytecode contains load instruction with indices. "
395 << "Replacing with getelementptr/load pair\n";
396 assert(MemAccessInst::getIndexedType(Raw.Ty, Idx) &&
397 "Bad indices for Load!");
398 Src = new GetElementPtrInst(Src, Idx);
399 // FIXME: Remove this compatibility code and the BB parameter to this
400 // method.
401 BB->getInstList().push_back(cast<Instruction>(Src));
402 }
403 Res = new LoadInst(Src);
Chris Lattner77a316a2001-11-12 21:48:38 +0000404 } else if (Raw.Opcode == Instruction::GetElementPtr)
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000405 Res = new GetElementPtrInst(getValue(Raw.Ty, Raw.Arg1), Idx);
406 else
407 abort();
408 return false;
409 }
410 case Instruction::Store: {
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000411 vector<Value*> Idx;
Chris Lattner74734132002-08-17 22:01:27 +0000412 if (!isa<PointerType>(Raw.Ty)) return true;
Chris Lattnerb024ef52001-12-14 16:30:09 +0000413 const CompositeType *TopTy = dyn_cast<CompositeType>(Raw.Ty);
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000414
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000415 switch (Raw.NumOperands) {
416 case 0:
Chris Lattner74734132002-08-17 22:01:27 +0000417 case 1: cerr << "Invalid store encountered!\n"; return true;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000418 case 2: break;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000419 case 3:
Chris Lattner74734132002-08-17 22:01:27 +0000420 if (!TopTy) return true;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000421 Idx.push_back(V = getValue(TopTy->getIndexType(), Raw.Arg3));
Chris Lattner74734132002-08-17 22:01:27 +0000422 if (!V) return true;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000423 break;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000424 default:
425 vector<unsigned> &args = *Raw.VarArgs;
Chris Lattnerb024ef52001-12-14 16:30:09 +0000426 const CompositeType *ElTy = TopTy;
427 unsigned i, E;
428 for (i = 0, E = args.size(); ElTy && i != E; ++i) {
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000429 Idx.push_back(V = getValue(ElTy->getIndexType(), args[i]));
Chris Lattner74734132002-08-17 22:01:27 +0000430 if (!V) return true;
Chris Lattnerb024ef52001-12-14 16:30:09 +0000431
432 const Type *ETy = MemAccessInst::getIndexedType(Raw.Ty, Idx, true);
433 ElTy = dyn_cast_or_null<CompositeType>(ETy);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000434 }
Chris Lattnerb024ef52001-12-14 16:30:09 +0000435 if (i != E)
Chris Lattner74734132002-08-17 22:01:27 +0000436 return true; // didn't use up all of the indices!
Chris Lattnerb024ef52001-12-14 16:30:09 +0000437
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000438 delete Raw.VarArgs;
439 break;
440 }
441
Chris Lattner352eef72002-08-21 22:55:27 +0000442 Value *Ptr = getValue(Raw.Ty, Raw.Arg2);
443 if (!Idx.empty()) {
444 cerr << "WARNING: Bytecode contains load instruction with indices. "
445 << "Replacing with getelementptr/load pair\n";
446
Chris Lattner24ea74e2002-08-22 22:49:05 +0000447 const Type *ElType = GetElementPtrInst::getIndexedType(Raw.Ty, Idx);
Chris Lattner352eef72002-08-21 22:55:27 +0000448 if (ElType == 0) return true;
449
450 Ptr = new GetElementPtrInst(Ptr, Idx);
451 // FIXME: Remove this compatibility code and the BB parameter to this
452 // method.
453 BB->getInstList().push_back(cast<Instruction>(Ptr));
454 }
455
456 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
457 Res = new StoreInst(getValue(ValTy, Raw.Arg1), Ptr);
Chris Lattner00950542001-06-06 20:29:01 +0000458 return false;
459 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000460 } // end switch(Raw.Opcode)
Chris Lattner00950542001-06-06 20:29:01 +0000461
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000462 cerr << "Unrecognized instruction! " << Raw.Opcode
Chris Lattner697954c2002-01-20 22:54:45 +0000463 << " ADDR = 0x" << (void*)Buf << "\n";
Chris Lattner74734132002-08-17 22:01:27 +0000464 return true;
Chris Lattner00950542001-06-06 20:29:01 +0000465}