blob: ed5a54d392ef273bbbec584acd7b1c67e04940be [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===- ReadInst.cpp - Code to read an instruction from bytecode -------------===
2//
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//
12//===------------------------------------------------------------------------===
13
14#include "llvm/iOther.h"
15#include "llvm/iTerminators.h"
16#include "llvm/iMemory.h"
17#include "llvm/DerivedTypes.h"
18#include "ReaderInternals.h"
19
20bool BytecodeParser::ParseRawInst(const uchar *&Buf, const uchar *EndBuf,
21 RawInst &Result) {
22 unsigned Op, Typ;
Chris Lattner3d3f2892001-07-28 17:50:18 +000023 if (read(Buf, EndBuf, Op)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000024
25 Result.NumOperands = Op >> 30;
26 Result.Opcode = (Op >> 24) & 63;
27
28 switch (Result.NumOperands) {
29 case 1:
30 Result.Ty = getType((Op >> 12) & 4095);
31 Result.Arg1 = Op & 4095;
32 if (Result.Arg1 == 4095) // Handle special encoding for 0 operands...
33 Result.NumOperands = 0;
34 break;
35 case 2:
36 Result.Ty = getType((Op >> 16) & 255);
37 Result.Arg1 = (Op >> 8 ) & 255;
38 Result.Arg2 = (Op >> 0 ) & 255;
39 break;
40 case 3:
41 Result.Ty = getType((Op >> 18) & 63);
42 Result.Arg1 = (Op >> 12) & 63;
43 Result.Arg2 = (Op >> 6 ) & 63;
44 Result.Arg3 = (Op >> 0 ) & 63;
45 break;
46 case 0:
47 Buf -= 4; // Hrm, try this again...
Chris Lattner3d3f2892001-07-28 17:50:18 +000048 if (read_vbr(Buf, EndBuf, Result.Opcode)) return failure(true);
49 if (read_vbr(Buf, EndBuf, Typ)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000050 Result.Ty = getType(Typ);
Chris Lattner3d3f2892001-07-28 17:50:18 +000051 if (read_vbr(Buf, EndBuf, Result.NumOperands)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000052
53 switch (Result.NumOperands) {
54 case 0:
55 cerr << "Zero Arg instr found!\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +000056 return failure(true); // This encoding is invalid!
Chris Lattner00950542001-06-06 20:29:01 +000057 case 1:
Chris Lattner3d3f2892001-07-28 17:50:18 +000058 if (read_vbr(Buf, EndBuf, Result.Arg1)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000059 break;
60 case 2:
61 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +000062 read_vbr(Buf, EndBuf, Result.Arg2)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000063 break;
64 case 3:
65 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
66 read_vbr(Buf, EndBuf, Result.Arg2) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +000067 read_vbr(Buf, EndBuf, Result.Arg3)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000068 break;
69 default:
70 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
Chris Lattner3d3f2892001-07-28 17:50:18 +000071 read_vbr(Buf, EndBuf, Result.Arg2)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000072
73 // Allocate a vector to hold arguments 3, 4, 5, 6 ...
74 Result.VarArgs = new vector<unsigned>(Result.NumOperands-2);
75 for (unsigned a = 0; a < Result.NumOperands-2; a++)
Chris Lattner3d3f2892001-07-28 17:50:18 +000076 if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000077 break;
78 }
Chris Lattner3d3f2892001-07-28 17:50:18 +000079 if (align32(Buf, EndBuf)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000080 break;
81 }
82
Chris Lattnerc8b25d42001-07-07 08:36:50 +000083#if 0
84 cerr << "NO: " << Result.NumOperands << " opcode: " << Result.Opcode
Chris Lattner1d670cc2001-09-07 16:37:43 +000085 << " Ty: " << Result.Ty->getDescription() << " arg1: " << Result.Arg1
Chris Lattnerc8b25d42001-07-07 08:36:50 +000086 << " arg2: " << Result.Arg2 << " arg3: " << Result.Arg3 << endl;
87#endif
Chris Lattner00950542001-06-06 20:29:01 +000088 return false;
89}
90
91
92bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
93 Instruction *&Res) {
94 RawInst Raw;
Chris Lattner3d3f2892001-07-28 17:50:18 +000095 if (ParseRawInst(Buf, EndBuf, Raw)) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +000096
97 if (Raw.Opcode >= Instruction::FirstUnaryOp &&
98 Raw.Opcode < Instruction::NumUnaryOps && Raw.NumOperands == 1) {
Chris Lattner9f3d2762001-07-07 20:17:23 +000099 Res = UnaryOperator::create((Instruction::UnaryOps)Raw.Opcode,
100 getValue(Raw.Ty,Raw.Arg1));
Chris Lattner00950542001-06-06 20:29:01 +0000101 return false;
102 } else if (Raw.Opcode >= Instruction::FirstBinaryOp &&
103 Raw.Opcode < Instruction::NumBinaryOps && Raw.NumOperands == 2) {
Chris Lattner9f3d2762001-07-07 20:17:23 +0000104 Res = BinaryOperator::create((Instruction::BinaryOps)Raw.Opcode,
105 getValue(Raw.Ty, Raw.Arg1),
106 getValue(Raw.Ty, Raw.Arg2));
Chris Lattner00950542001-06-06 20:29:01 +0000107 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000108 }
109
110 Value *V;
111 switch (Raw.Opcode) {
112 case Instruction::Cast:
Chris Lattner71496b32001-07-08 19:03:27 +0000113 Res = new CastInst(getValue(Raw.Ty, Raw.Arg1), getType(Raw.Arg2));
Chris Lattner09083092001-07-08 04:57:15 +0000114 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000115
116 case Instruction::PHINode: {
Chris Lattner00950542001-06-06 20:29:01 +0000117 PHINode *PN = new PHINode(Raw.Ty);
118 switch (Raw.NumOperands) {
Chris Lattneree976f32001-06-11 15:04:40 +0000119 case 0:
120 case 1:
121 case 3: cerr << "Invalid phi node encountered!\n";
Chris Lattner00950542001-06-06 20:29:01 +0000122 delete PN;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000123 return failure(true);
Chris Lattneree976f32001-06-11 15:04:40 +0000124 case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
125 (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2));
126 break;
Chris Lattner00950542001-06-06 20:29:01 +0000127 default:
Chris Lattneree976f32001-06-11 15:04:40 +0000128 PN->addIncoming(getValue(Raw.Ty, Raw.Arg1),
129 (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2));
130 if (Raw.VarArgs->size() & 1) {
131 cerr << "PHI Node with ODD number of arguments!\n";
132 delete PN;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000133 return failure(true);
Chris Lattneree976f32001-06-11 15:04:40 +0000134 } else {
Chris Lattner00950542001-06-06 20:29:01 +0000135 vector<unsigned> &args = *Raw.VarArgs;
Chris Lattneree976f32001-06-11 15:04:40 +0000136 for (unsigned i = 0; i < args.size(); i+=2)
137 PN->addIncoming(getValue(Raw.Ty, args[i]),
138 (BasicBlock*)getValue(Type::LabelTy, args[i+1]));
Chris Lattner00950542001-06-06 20:29:01 +0000139 }
Chris Lattneree976f32001-06-11 15:04:40 +0000140 delete Raw.VarArgs;
141 break;
Chris Lattner00950542001-06-06 20:29:01 +0000142 }
143 Res = PN;
144 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000145 }
146
147 case Instruction::Shl:
148 case Instruction::Shr:
149 Res = new ShiftInst((Instruction::OtherOps)Raw.Opcode,
150 getValue(Raw.Ty, Raw.Arg1),
151 getValue(Type::UByteTy, Raw.Arg2));
152 return false;
153 case Instruction::Ret:
Chris Lattner00950542001-06-06 20:29:01 +0000154 if (Raw.NumOperands == 0) {
155 Res = new ReturnInst(); return false;
156 } else if (Raw.NumOperands == 1) {
157 Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false;
158 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000159 break;
160
161 case Instruction::Br:
Chris Lattner00950542001-06-06 20:29:01 +0000162 if (Raw.NumOperands == 1) {
163 Res = new BranchInst((BasicBlock*)getValue(Type::LabelTy, Raw.Arg1));
164 return false;
165 } else if (Raw.NumOperands == 3) {
166 Res = new BranchInst((BasicBlock*)getValue(Type::LabelTy, Raw.Arg1),
167 (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2),
168 getValue(Type::BoolTy , Raw.Arg3));
169 return false;
170 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000171 break;
172
173 case Instruction::Switch: {
Chris Lattner00950542001-06-06 20:29:01 +0000174 SwitchInst *I =
175 new SwitchInst(getValue(Raw.Ty, Raw.Arg1),
176 (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2));
177 Res = I;
178 if (Raw.NumOperands < 3) return false; // No destinations? Wierd.
179
180 if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) {
181 cerr << "Switch statement with odd number of arguments!\n";
182 delete I;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000183 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000184 }
185
186 vector<unsigned> &args = *Raw.VarArgs;
187 for (unsigned i = 0; i < args.size(); i += 2)
188 I->dest_push_back((ConstPoolVal*)getValue(Raw.Ty, args[i]),
189 (BasicBlock*)getValue(Type::LabelTy, args[i+1]));
190
191 delete Raw.VarArgs;
192 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000193 }
194
195 case Instruction::Call: {
Chris Lattner00950542001-06-06 20:29:01 +0000196 Method *M = (Method*)getValue(Raw.Ty, Raw.Arg1);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000197 if (M == 0) return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000198
Chris Lattner00950542001-06-06 20:29:01 +0000199 vector<Value *> Params;
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000200 const MethodType::ParamTypes &PL = M->getMethodType()->getParamTypes();
201
202 if (!M->getType()->isMethodType()->isVarArg()) {
203 MethodType::ParamTypes::const_iterator It = PL.begin();
204
205 switch (Raw.NumOperands) {
206 case 0: cerr << "Invalid call instruction encountered!\n";
Chris Lattner3d3f2892001-07-28 17:50:18 +0000207 return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000208 case 1: break;
209 case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
210 case 3: Params.push_back(getValue(*It++, Raw.Arg2));
Chris Lattner3d3f2892001-07-28 17:50:18 +0000211 if (It == PL.end()) return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000212 Params.push_back(getValue(*It++, Raw.Arg3)); break;
213 default:
214 Params.push_back(getValue(*It++, Raw.Arg2));
215 {
216 vector<unsigned> &args = *Raw.VarArgs;
217 for (unsigned i = 0; i < args.size(); i++) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000218 if (It == PL.end()) return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000219 // TODO: Check getValue for null!
220 Params.push_back(getValue(*It++, args[i]));
221 }
Chris Lattner00950542001-06-06 20:29:01 +0000222 }
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000223 delete Raw.VarArgs;
224 }
Chris Lattner3d3f2892001-07-28 17:50:18 +0000225 if (It != PL.end()) return failure(true);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000226 } else {
227 // The first parameter does not have a type specifier... because there
228 // must be at least one concrete argument to a vararg type...
229 Params.push_back(getValue(PL.front(), Raw.Arg2));
230
231 vector<unsigned> &args = *Raw.VarArgs;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000232 if ((args.size() & 1) != 0)
233 return failure(true); // Must be pairs of type/value
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000234 for (unsigned i = 0; i < args.size(); i+=2) {
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000235 // TODO: Check getValue for null!
Chris Lattner3d3f2892001-07-28 17:50:18 +0000236 Params.push_back(getValue(getType(args[i]), args[i+1]));
Chris Lattner00950542001-06-06 20:29:01 +0000237 }
238 delete Raw.VarArgs;
239 }
Chris Lattner00950542001-06-06 20:29:01 +0000240
241 Res = new CallInst(M, Params);
242 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000243 }
244 case Instruction::Malloc:
Chris Lattner3d3f2892001-07-28 17:50:18 +0000245 if (Raw.NumOperands > 2) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000246 V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
247 Res = new MallocInst(Raw.Ty, V);
Chris Lattner00950542001-06-06 20:29:01 +0000248 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000249
250 case Instruction::Alloca:
Chris Lattner3d3f2892001-07-28 17:50:18 +0000251 if (Raw.NumOperands > 2) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000252 V = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
253 Res = new AllocaInst(Raw.Ty, V);
Chris Lattner00950542001-06-06 20:29:01 +0000254 return false;
Chris Lattner027dcc52001-07-08 21:10:27 +0000255
256 case Instruction::Free:
257 V = getValue(Raw.Ty, Raw.Arg1);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000258 if (!V->getType()->isPointerType()) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000259 Res = new FreeInst(V);
260 return false;
261
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000262 case Instruction::Load:
263 case Instruction::GetElementPtr: {
Chris Lattner027dcc52001-07-08 21:10:27 +0000264 vector<ConstPoolVal*> Idx;
265 switch (Raw.NumOperands) {
Chris Lattner3d3f2892001-07-28 17:50:18 +0000266 case 0: cerr << "Invalid load encountered!\n"; return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000267 case 1: break;
268 case 2: V = getValue(Type::UByteTy, Raw.Arg2);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000269 if (!V->isConstant()) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000270 Idx.push_back(V->castConstant());
271 break;
272 case 3: V = getValue(Type::UByteTy, Raw.Arg2);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000273 if (!V->isConstant()) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000274 Idx.push_back(V->castConstant());
275 V = getValue(Type::UByteTy, Raw.Arg3);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000276 if (!V->isConstant()) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000277 Idx.push_back(V->castConstant());
278 break;
279 default:
280 V = getValue(Type::UByteTy, Raw.Arg2);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000281 if (!V->isConstant()) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000282 Idx.push_back(V->castConstant());
283 vector<unsigned> &args = *Raw.VarArgs;
284 for (unsigned i = 0, E = args.size(); i != E; ++i) {
285 V = getValue(Type::UByteTy, args[i]);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000286 if (!V->isConstant()) return failure(true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000287 Idx.push_back(V->castConstant());
288 }
289 delete Raw.VarArgs;
290 break;
291 }
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000292 if (Raw.Opcode == Instruction::Load)
293 Res = new LoadInst(getValue(Raw.Ty, Raw.Arg1), Idx);
294 else if (Raw.Opcode == Instruction::GetElementPtr)
295 Res = new GetElementPtrInst(getValue(Raw.Ty, Raw.Arg1), Idx);
296 else
297 abort();
298 return false;
299 }
300 case Instruction::Store: {
301 vector<ConstPoolVal*> Idx;
302 switch (Raw.NumOperands) {
303 case 0:
Chris Lattner3d3f2892001-07-28 17:50:18 +0000304 case 1: cerr << "Invalid store encountered!\n"; return failure(true);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000305 case 2: break;
306 case 3: V = getValue(Type::UByteTy, Raw.Arg3);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000307 if (!V->isConstant()) return failure(true);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000308 Idx.push_back(V->castConstant());
309 break;
310 default:
311 vector<unsigned> &args = *Raw.VarArgs;
312 for (unsigned i = 0, E = args.size(); i != E; ++i) {
313 V = getValue(Type::UByteTy, args[i]);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000314 if (!V->isConstant()) return failure(true);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000315 Idx.push_back(V->castConstant());
316 }
317 delete Raw.VarArgs;
318 break;
319 }
320
321 const Type *ElType = StoreInst::getIndexedType(Raw.Ty, Idx);
Chris Lattner3d3f2892001-07-28 17:50:18 +0000322 if (ElType == 0) return failure(true);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000323 Res = new StoreInst(getValue(ElType, Raw.Arg1), getValue(Raw.Ty, Raw.Arg2),
324 Idx);
Chris Lattner00950542001-06-06 20:29:01 +0000325 return false;
326 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000327 } // end switch(Raw.Opcode)
Chris Lattner00950542001-06-06 20:29:01 +0000328
Chris Lattnerc8b25d42001-07-07 08:36:50 +0000329 cerr << "Unrecognized instruction! " << Raw.Opcode
330 << " ADDR = 0x" << (void*)Buf << endl;
Chris Lattner3d3f2892001-07-28 17:50:18 +0000331 return failure(true);
Chris Lattner00950542001-06-06 20:29:01 +0000332}