blob: 54ca8695115240f7e47f86c41efdeef42eeadd4d [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;
23 if (read(Buf, EndBuf, Op)) return true;
24
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...
48 if (read_vbr(Buf, EndBuf, Result.Opcode)) return true;
49 if (read_vbr(Buf, EndBuf, Typ)) return true;
50 Result.Ty = getType(Typ);
51 if (read_vbr(Buf, EndBuf, Result.NumOperands)) return true;
52
53 switch (Result.NumOperands) {
54 case 0:
55 cerr << "Zero Arg instr found!\n";
56 return true; // This encoding is invalid!
57 case 1:
58 if (read_vbr(Buf, EndBuf, Result.Arg1)) return true;
59 break;
60 case 2:
61 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
62 read_vbr(Buf, EndBuf, Result.Arg2)) return true;
63 break;
64 case 3:
65 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
66 read_vbr(Buf, EndBuf, Result.Arg2) ||
67 read_vbr(Buf, EndBuf, Result.Arg3)) return true;
68 break;
69 default:
70 if (read_vbr(Buf, EndBuf, Result.Arg1) ||
71 read_vbr(Buf, EndBuf, Result.Arg2)) return true;
72
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++)
76 if (read_vbr(Buf, EndBuf, (*Result.VarArgs)[a])) return true;
77 break;
78 }
79 if (align32(Buf, EndBuf)) return true;
80 break;
81 }
82
83 //cerr << "NO: " << Result.NumOperands << " opcode: " << Result.Opcode
84 // << " Ty: " << Result.Ty->getName() << " arg1: " << Result.Arg1 << endl;
85 return false;
86}
87
88
89bool BytecodeParser::ParseInstruction(const uchar *&Buf, const uchar *EndBuf,
90 Instruction *&Res) {
91 RawInst Raw;
92 if (ParseRawInst(Buf, EndBuf, Raw)) return true;;
93
94 if (Raw.Opcode >= Instruction::FirstUnaryOp &&
95 Raw.Opcode < Instruction::NumUnaryOps && Raw.NumOperands == 1) {
Chris Lattner477c2ec2001-06-08 21:30:13 +000096 Res = UnaryOperator::getUnaryOperator(Raw.Opcode,getValue(Raw.Ty,Raw.Arg1));
Chris Lattner00950542001-06-06 20:29:01 +000097 return false;
98 } else if (Raw.Opcode >= Instruction::FirstBinaryOp &&
99 Raw.Opcode < Instruction::NumBinaryOps && Raw.NumOperands == 2) {
Chris Lattner477c2ec2001-06-08 21:30:13 +0000100 Res = BinaryOperator::getBinaryOperator(Raw.Opcode,
101 getValue(Raw.Ty, Raw.Arg1),
102 getValue(Raw.Ty, Raw.Arg2));
Chris Lattner00950542001-06-06 20:29:01 +0000103 return false;
104 } else if (Raw.Opcode == Instruction::PHINode) {
105 PHINode *PN = new PHINode(Raw.Ty);
106 switch (Raw.NumOperands) {
107 case 0: cerr << "Invalid phi node encountered!\n";
108 delete PN;
109 return true;
110 case 1: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1)); break;
111 case 2: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1));
112 PN->addIncoming(getValue(Raw.Ty, Raw.Arg2)); break;
113 case 3: PN->addIncoming(getValue(Raw.Ty, Raw.Arg1));
114 PN->addIncoming(getValue(Raw.Ty, Raw.Arg2));
115 PN->addIncoming(getValue(Raw.Ty, Raw.Arg3)); break;
116 default:
117 PN->addIncoming(getValue(Raw.Ty, Raw.Arg1));
118 PN->addIncoming(getValue(Raw.Ty, Raw.Arg2));
119 {
120 vector<unsigned> &args = *Raw.VarArgs;
121 for (unsigned i = 0; i < args.size(); i++)
122 PN->addIncoming(getValue(Raw.Ty, args[i]));
123 }
124 delete Raw.VarArgs;
125 }
126 Res = PN;
127 return false;
128 } else if (Raw.Opcode == Instruction::Ret) {
129 if (Raw.NumOperands == 0) {
130 Res = new ReturnInst(); return false;
131 } else if (Raw.NumOperands == 1) {
132 Res = new ReturnInst(getValue(Raw.Ty, Raw.Arg1)); return false;
133 }
134 } else if (Raw.Opcode == Instruction::Br) {
135 if (Raw.NumOperands == 1) {
136 Res = new BranchInst((BasicBlock*)getValue(Type::LabelTy, Raw.Arg1));
137 return false;
138 } else if (Raw.NumOperands == 3) {
139 Res = new BranchInst((BasicBlock*)getValue(Type::LabelTy, Raw.Arg1),
140 (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2),
141 getValue(Type::BoolTy , Raw.Arg3));
142 return false;
143 }
144 } else if (Raw.Opcode == Instruction::Switch) {
145 SwitchInst *I =
146 new SwitchInst(getValue(Raw.Ty, Raw.Arg1),
147 (BasicBlock*)getValue(Type::LabelTy, Raw.Arg2));
148 Res = I;
149 if (Raw.NumOperands < 3) return false; // No destinations? Wierd.
150
151 if (Raw.NumOperands == 3 || Raw.VarArgs->size() & 1) {
152 cerr << "Switch statement with odd number of arguments!\n";
153 delete I;
154 return true;
155 }
156
157 vector<unsigned> &args = *Raw.VarArgs;
158 for (unsigned i = 0; i < args.size(); i += 2)
159 I->dest_push_back((ConstPoolVal*)getValue(Raw.Ty, args[i]),
160 (BasicBlock*)getValue(Type::LabelTy, args[i+1]));
161
162 delete Raw.VarArgs;
163 return false;
164 } else if (Raw.Opcode == Instruction::Call) {
165 Method *M = (Method*)getValue(Raw.Ty, Raw.Arg1);
166 if (M == 0) return true;
167
168 const MethodType::ParamTypes &PL = M->getMethodType()->getParamTypes();
169 MethodType::ParamTypes::const_iterator It = PL.begin();
170
171 vector<Value *> Params;
172 switch (Raw.NumOperands) {
173 case 0: cerr << "Invalid call instruction encountered!\n";
174 return true;
175 case 1: break;
176 case 2: Params.push_back(getValue(*It++, Raw.Arg2)); break;
177 case 3: Params.push_back(getValue(*It++, Raw.Arg2));
178 if (It == PL.end()) return true;
179 Params.push_back(getValue(*It++, Raw.Arg3)); break;
180 default:
181 Params.push_back(getValue(*It++, Raw.Arg2));
182 {
183 vector<unsigned> &args = *Raw.VarArgs;
184 for (unsigned i = 0; i < args.size(); i++) {
185 if (It == PL.end()) return true;
186 Params.push_back(getValue(*It++, args[i]));
187 }
188 }
189 delete Raw.VarArgs;
190 }
191 if (It != PL.end()) return true;
192
193 Res = new CallInst(M, Params);
194 return false;
195 } else if (Raw.Opcode == Instruction::Malloc) {
196 if (Raw.NumOperands > 2) return true;
197 Value *Sz = (Raw.NumOperands == 2) ? getValue(Type::UIntTy, Raw.Arg2) : 0;
198 Res = new MallocInst((ConstPoolType*)getValue(Type::TypeTy, Raw.Arg1), Sz);
199 return false;
200 } else if (Raw.Opcode == Instruction::Alloca) {
201 if (Raw.NumOperands > 2) return true;
202 Value *Sz = (Raw.NumOperands == 2) ? getValue(Type::UIntTy, Raw.Arg2) : 0;
203 Res = new AllocaInst((ConstPoolType*)getValue(Type::TypeTy, Raw.Arg1), Sz);
204 return false;
205 } else if (Raw.Opcode == Instruction::Free) {
206 Value *Val = getValue(Raw.Ty, Raw.Arg1);
207 if (!Val->getType()->isPointerType()) return true;
208 Res = new FreeInst(Val);
209 return false;
210 }
211
212 cerr << "Unrecognized instruction! " << Raw.Opcode << endl;
213 return true;
214}