blob: d66b12cf0d95a0bdfebb457712b390bc3cf5b6c6 [file] [log] [blame]
Chris Lattner05950c32001-10-13 06:47:01 +00001//===- ReadInst.cpp - Code to read an instruction from bytecode -----------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file defines the mechanism to read an instruction from a bytecode
11// stream.
12//
13// Note that this library should be as fast as possible, reentrant, and
14// threadsafe!!
15//
Chris Lattner05950c32001-10-13 06:47:01 +000016//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000017
Chris Lattner7061dc52001-12-03 18:02:31 +000018#include "ReaderInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/iTerminators.h"
20#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
22#include "llvm/iOther.h"
Chris Lattnercb7e2e22003-10-18 05:54:18 +000023#include "llvm/Module.h"
Chris Lattner8eb49932003-11-19 17:21:11 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner6fcf5032003-10-09 20:45:42 +000026namespace {
27 struct RawInst { // The raw fields out of the bytecode stream...
28 unsigned NumOperands;
29 unsigned Opcode;
30 unsigned Type;
31
32 RawInst(const unsigned char *&Buf, const unsigned char *EndBuf,
33 std::vector<unsigned> &Args);
34 };
35}
Chris Lattner927b1852003-10-09 20:22:47 +000036
Chris Lattner927b1852003-10-09 20:22:47 +000037RawInst::RawInst(const unsigned char *&Buf, const unsigned char *EndBuf,
38 std::vector<unsigned> &Args) {
Chris Lattner7969dc22004-01-15 06:13:09 +000039 unsigned Op = read(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000040
Chris Lattner2b9f6002001-10-23 03:21:10 +000041 // bits Instruction format: Common to all formats
42 // --------------------------
43 // 01-00: Opcode type, fixed to 1.
44 // 07-02: Opcode
Chris Lattner927b1852003-10-09 20:22:47 +000045 Opcode = (Op >> 2) & 63;
46 Args.resize((Op >> 0) & 03);
Chris Lattner00950542001-06-06 20:29:01 +000047
Chris Lattner927b1852003-10-09 20:22:47 +000048 switch (Args.size()) {
Chris Lattner00950542001-06-06 20:29:01 +000049 case 1:
Chris Lattner2b9f6002001-10-23 03:21:10 +000050 // bits Instruction format:
51 // --------------------------
52 // 19-08: Resulting type plane
53 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
54 //
Chris Lattner927b1852003-10-09 20:22:47 +000055 Type = (Op >> 8) & 4095;
56 Args[0] = (Op >> 20) & 4095;
57 if (Args[0] == 4095) // Handle special encoding for 0 operands...
58 Args.resize(0);
Chris Lattner00950542001-06-06 20:29:01 +000059 break;
60 case 2:
Chris Lattner2b9f6002001-10-23 03:21:10 +000061 // bits Instruction format:
62 // --------------------------
63 // 15-08: Resulting type plane
64 // 23-16: Operand #1
65 // 31-24: Operand #2
66 //
Chris Lattner927b1852003-10-09 20:22:47 +000067 Type = (Op >> 8) & 255;
68 Args[0] = (Op >> 16) & 255;
69 Args[1] = (Op >> 24) & 255;
Chris Lattner00950542001-06-06 20:29:01 +000070 break;
71 case 3:
Chris Lattner2b9f6002001-10-23 03:21:10 +000072 // bits Instruction format:
73 // --------------------------
74 // 13-08: Resulting type plane
75 // 19-14: Operand #1
76 // 25-20: Operand #2
77 // 31-26: Operand #3
78 //
Chris Lattner927b1852003-10-09 20:22:47 +000079 Type = (Op >> 8) & 63;
80 Args[0] = (Op >> 14) & 63;
81 Args[1] = (Op >> 20) & 63;
82 Args[2] = (Op >> 26) & 63;
Chris Lattner00950542001-06-06 20:29:01 +000083 break;
84 case 0:
85 Buf -= 4; // Hrm, try this again...
Chris Lattner7969dc22004-01-15 06:13:09 +000086 Opcode = read_vbr_uint(Buf, EndBuf);
Chris Lattner927b1852003-10-09 20:22:47 +000087 Opcode >>= 2;
Chris Lattner7969dc22004-01-15 06:13:09 +000088 Type = read_vbr_uint(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000089
Chris Lattner7969dc22004-01-15 06:13:09 +000090 unsigned NumOperands = read_vbr_uint(Buf, EndBuf);
Chris Lattner927b1852003-10-09 20:22:47 +000091 Args.resize(NumOperands);
92
93 if (NumOperands == 0)
Misha Brukmand554ebf2003-09-23 16:17:50 +000094 throw std::string("Zero-argument instruction found; this is invalid.");
Chris Lattner00950542001-06-06 20:29:01 +000095
Chris Lattner927b1852003-10-09 20:22:47 +000096 for (unsigned i = 0; i != NumOperands; ++i)
Chris Lattner7969dc22004-01-15 06:13:09 +000097 Args[i] = read_vbr_uint(Buf, EndBuf);
98 align32(Buf, EndBuf);
Chris Lattner00950542001-06-06 20:29:01 +000099 break;
100 }
Chris Lattner00950542001-06-06 20:29:01 +0000101}
102
103
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000104void BytecodeParser::ParseInstruction(const unsigned char *&Buf,
105 const unsigned char *EndBuf,
106 std::vector<unsigned> &Args,
107 BasicBlock *BB) {
Chris Lattner6fcf5032003-10-09 20:45:42 +0000108 Args.clear();
Chris Lattner927b1852003-10-09 20:22:47 +0000109 RawInst RI(Buf, EndBuf, Args);
110 const Type *InstTy = getType(RI.Type);
Chris Lattner00950542001-06-06 20:29:01 +0000111
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000112 Instruction *Result = 0;
Chris Lattner927b1852003-10-09 20:22:47 +0000113 if (RI.Opcode >= Instruction::BinaryOpsBegin &&
114 RI.Opcode < Instruction::BinaryOpsEnd && Args.size() == 2)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000115 Result = BinaryOperator::create((Instruction::BinaryOps)RI.Opcode,
116 getValue(RI.Type, Args[0]),
117 getValue(RI.Type, Args[1]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000118
Chris Lattner927b1852003-10-09 20:22:47 +0000119 switch (RI.Opcode) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000120 default:
121 if (Result == 0) throw std::string("Illegal instruction read!");
122 break;
123 case Instruction::VAArg:
124 Result = new VAArgInst(getValue(RI.Type, Args[0]), getType(Args[1]));
125 break;
126 case Instruction::VANext:
Chris Lattner9dd87702004-04-03 23:43:42 +0000127 Result = new VANextInst(getValue(RI.Type, Args[0]), getType(Args[1]));
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000128 break;
Chris Lattner927b1852003-10-09 20:22:47 +0000129 case Instruction::Cast:
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000130 Result = new CastInst(getValue(RI.Type, Args[0]), getType(Args[1]));
131 break;
Chris Lattnere3ac22a2004-03-12 05:51:49 +0000132 case Instruction::Select:
133 Result = new SelectInst(getValue(Type::BoolTyID, Args[0]),
134 getValue(RI.Type, Args[1]),
135 getValue(RI.Type, Args[2]));
136 break;
Chris Lattner3b237fc2003-10-19 21:34:28 +0000137 case Instruction::PHI: {
Chris Lattner927b1852003-10-09 20:22:47 +0000138 if (Args.size() == 0 || (Args.size() & 1))
Chris Lattner3483f542003-10-09 18:25:19 +0000139 throw std::string("Invalid phi node encountered!\n");
Chris Lattner927b1852003-10-09 20:22:47 +0000140
141 PHINode *PN = new PHINode(InstTy);
Chris Lattner4c523922003-10-09 22:46:58 +0000142 PN->op_reserve(Args.size());
Chris Lattner927b1852003-10-09 20:22:47 +0000143 for (unsigned i = 0, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000144 PN->addIncoming(getValue(RI.Type, Args[i]), getBasicBlock(Args[i+1]));
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000145 Result = PN;
146 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000147 }
148
149 case Instruction::Shl:
150 case Instruction::Shr:
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000151 Result = new ShiftInst((Instruction::OtherOps)RI.Opcode,
152 getValue(RI.Type, Args[0]),
153 getValue(Type::UByteTyID, Args[1]));
154 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000155 case Instruction::Ret:
Chris Lattner927b1852003-10-09 20:22:47 +0000156 if (Args.size() == 0)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000157 Result = new ReturnInst();
Chris Lattner927b1852003-10-09 20:22:47 +0000158 else if (Args.size() == 1)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000159 Result = new ReturnInst(getValue(RI.Type, Args[0]));
160 else
161 throw std::string("Unrecognized instruction!");
Chris Lattner027dcc52001-07-08 21:10:27 +0000162 break;
163
164 case Instruction::Br:
Chris Lattner927b1852003-10-09 20:22:47 +0000165 if (Args.size() == 1)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000166 Result = new BranchInst(getBasicBlock(Args[0]));
Chris Lattner927b1852003-10-09 20:22:47 +0000167 else if (Args.size() == 3)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000168 Result = new BranchInst(getBasicBlock(Args[0]), getBasicBlock(Args[1]),
169 getValue(Type::BoolTyID , Args[2]));
170 else
171 throw std::string("Invalid number of operands for a 'br' instruction!");
172 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000173 case Instruction::Switch: {
Chris Lattner927b1852003-10-09 20:22:47 +0000174 if (Args.size() & 1)
Chris Lattner3483f542003-10-09 18:25:19 +0000175 throw std::string("Switch statement with odd number of arguments!");
Chris Lattner00950542001-06-06 20:29:01 +0000176
Chris Lattner35d2ca62003-10-09 22:39:30 +0000177 SwitchInst *I = new SwitchInst(getValue(RI.Type, Args[0]),
Chris Lattner927b1852003-10-09 20:22:47 +0000178 getBasicBlock(Args[1]));
179 for (unsigned i = 2, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000180 I->addCase(cast<Constant>(getValue(RI.Type, Args[i])),
Chris Lattner927b1852003-10-09 20:22:47 +0000181 getBasicBlock(Args[i+1]));
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000182 Result = I;
183 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000184 }
185
186 case Instruction::Call: {
Chris Lattner927b1852003-10-09 20:22:47 +0000187 if (Args.size() == 0)
188 throw std::string("Invalid call instruction encountered!");
189
Chris Lattner35d2ca62003-10-09 22:39:30 +0000190 Value *F = getValue(RI.Type, Args[0]);
Chris Lattner00950542001-06-06 20:29:01 +0000191
Chris Lattner3483f542003-10-09 18:25:19 +0000192 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000193 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000194 if (PTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000195 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000196 if (FTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000197
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000198 std::vector<Value *> Params;
Chris Lattner5bea4112003-09-05 18:25:29 +0000199 if (!FTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000200 FunctionType::param_iterator It = FTy->param_begin();
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000201
Chris Lattner927b1852003-10-09 20:22:47 +0000202 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000203 if (It == FTy->param_end())
204 throw std::string("Invalid call instruction!");
Chris Lattner8eb49932003-11-19 17:21:11 +0000205 Params.push_back(getValue(getTypeSlot(*It++), Args[i]));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000206 }
Chris Lattnerd5d89962004-02-09 04:14:01 +0000207 if (It != FTy->param_end())
208 throw std::string("Invalid call instruction!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000209 } else {
Chris Lattner9dd87702004-04-03 23:43:42 +0000210 Args.erase(Args.begin(), Args.begin()+1);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000211
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000212 unsigned FirstVariableOperand;
Chris Lattner9dd87702004-04-03 23:43:42 +0000213 if (Args.size() < FTy->getNumParams())
214 throw std::string("Call instruction missing operands!");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000215
Chris Lattner9dd87702004-04-03 23:43:42 +0000216 // Read all of the fixed arguments
217 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
218 Params.push_back(getValue(getTypeSlot(FTy->getParamType(i)),Args[i]));
219
220 FirstVariableOperand = FTy->getNumParams();
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000221
222 if ((Args.size()-FirstVariableOperand) & 1) // Must be pairs of type/value
Chris Lattner927b1852003-10-09 20:22:47 +0000223 throw std::string("Invalid call instruction!");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000224
225 for (unsigned i = FirstVariableOperand, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000226 Params.push_back(getValue(Args[i], Args[i+1]));
Chris Lattner00950542001-06-06 20:29:01 +0000227 }
Chris Lattner00950542001-06-06 20:29:01 +0000228
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000229 Result = new CallInst(F, Params);
230 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000231 }
Chris Lattner05950c32001-10-13 06:47:01 +0000232 case Instruction::Invoke: {
Chris Lattner927b1852003-10-09 20:22:47 +0000233 if (Args.size() < 3) throw std::string("Invalid invoke instruction!");
Chris Lattner35d2ca62003-10-09 22:39:30 +0000234 Value *F = getValue(RI.Type, Args[0]);
Chris Lattner05950c32001-10-13 06:47:01 +0000235
Chris Lattner3483f542003-10-09 18:25:19 +0000236 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000237 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000238 if (PTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000239 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000240 if (FTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner05950c32001-10-13 06:47:01 +0000241
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000242 std::vector<Value *> Params;
Chris Lattner05950c32001-10-13 06:47:01 +0000243 BasicBlock *Normal, *Except;
244
Chris Lattner5bea4112003-09-05 18:25:29 +0000245 if (!FTy->isVarArg()) {
Chris Lattner927b1852003-10-09 20:22:47 +0000246 Normal = getBasicBlock(Args[1]);
247 Except = getBasicBlock(Args[2]);
Chris Lattner05950c32001-10-13 06:47:01 +0000248
Chris Lattnerd5d89962004-02-09 04:14:01 +0000249 FunctionType::param_iterator It = FTy->param_begin();
Chris Lattner927b1852003-10-09 20:22:47 +0000250 for (unsigned i = 3, e = Args.size(); i != e; ++i) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000251 if (It == FTy->param_end())
252 throw std::string("Invalid invoke instruction!");
Chris Lattner8eb49932003-11-19 17:21:11 +0000253 Params.push_back(getValue(getTypeSlot(*It++), Args[i]));
Chris Lattner05950c32001-10-13 06:47:01 +0000254 }
Chris Lattnerd5d89962004-02-09 04:14:01 +0000255 if (It != FTy->param_end())
256 throw std::string("Invalid invoke instruction!");
Chris Lattner05950c32001-10-13 06:47:01 +0000257 } else {
Chris Lattner9dd87702004-04-03 23:43:42 +0000258 Args.erase(Args.begin(), Args.begin()+1);
Chris Lattner927b1852003-10-09 20:22:47 +0000259
Chris Lattner9dd87702004-04-03 23:43:42 +0000260 Normal = getBasicBlock(Args[0]);
261 Except = getBasicBlock(Args[1]);
262
263 unsigned FirstVariableArgument = FTy->getNumParams()+2;
264 for (unsigned i = 2; i != FirstVariableArgument; ++i)
265 Params.push_back(getValue(getTypeSlot(FTy->getParamType(i-2)),
266 Args[i]));
267
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000268 if (Args.size()-FirstVariableArgument & 1) // Must be pairs of type/value
Chris Lattner3483f542003-10-09 18:25:19 +0000269 throw std::string("Invalid invoke instruction!");
270
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000271 for (unsigned i = FirstVariableArgument; i < Args.size(); i += 2)
Chris Lattner927b1852003-10-09 20:22:47 +0000272 Params.push_back(getValue(Args[i], Args[i+1]));
Chris Lattner05950c32001-10-13 06:47:01 +0000273 }
274
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000275 Result = new InvokeInst(F, Normal, Except, Params);
276 break;
Chris Lattner05950c32001-10-13 06:47:01 +0000277 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000278 case Instruction::Malloc:
Chris Lattner927b1852003-10-09 20:22:47 +0000279 if (Args.size() > 2) throw std::string("Invalid malloc instruction!");
280 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000281 throw std::string("Invalid malloc instruction!");
282
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000283 Result = new MallocInst(cast<PointerType>(InstTy)->getElementType(),
284 Args.size() ? getValue(Type::UIntTyID,
285 Args[0]) : 0);
286 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000287
288 case Instruction::Alloca:
Chris Lattner927b1852003-10-09 20:22:47 +0000289 if (Args.size() > 2) throw std::string("Invalid alloca instruction!");
290 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000291 throw std::string("Invalid alloca instruction!");
Chris Lattner027dcc52001-07-08 21:10:27 +0000292
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000293 Result = new AllocaInst(cast<PointerType>(InstTy)->getElementType(),
294 Args.size() ? getValue(Type::UIntTyID, Args[0]) :0);
295 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000296 case Instruction::Free:
Chris Lattner927b1852003-10-09 20:22:47 +0000297 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000298 throw std::string("Invalid free instruction!");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000299 Result = new FreeInst(getValue(RI.Type, Args[0]));
300 break;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000301 case Instruction::GetElementPtr: {
Chris Lattner927b1852003-10-09 20:22:47 +0000302 if (Args.size() == 0 || !isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000303 throw std::string("Invalid getelementptr instruction!");
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000304
Chris Lattner927b1852003-10-09 20:22:47 +0000305 std::vector<Value*> Idx;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000306
Chris Lattner927b1852003-10-09 20:22:47 +0000307 const Type *NextTy = InstTy;
308 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
309 const CompositeType *TopTy = dyn_cast_or_null<CompositeType>(NextTy);
310 if (!TopTy) throw std::string("Invalid getelementptr instruction!");
Chris Lattner5fa428f2004-04-05 01:27:26 +0000311
312 unsigned ValIdx = Args[i];
313 unsigned IdxTy;
314 if (!hasRestrictedGEPTypes) {
315 // Struct indices are always uints, sequential type indices can be any
316 // of the 32 or 64-bit integer types. The actual choice of type is
317 // encoded in the low two bits of the slot number.
318 if (isa<StructType>(TopTy))
319 IdxTy = Type::UIntTyID;
320 else {
321 switch (ValIdx & 3) {
322 case 0: IdxTy = Type::UIntTyID; break;
323 case 1: IdxTy = Type::IntTyID; break;
324 case 2: IdxTy = Type::ULongTyID; break;
325 case 3: IdxTy = Type::LongTyID; break;
326 }
327 ValIdx >>= 2;
328 }
329 } else {
330 IdxTy = isa<StructType>(TopTy) ? Type::UByteTyID : Type::LongTyID;
331 }
332
333 Idx.push_back(getValue(IdxTy, ValIdx));
334
335 // Convert ubyte struct indices into uint struct indices.
336 if (isa<StructType>(TopTy) && hasRestrictedGEPTypes)
337 if (ConstantUInt *C = dyn_cast<ConstantUInt>(Idx.back()))
338 Idx[Idx.size()-1] = ConstantExpr::getCast(C, Type::UIntTy);
339
Chris Lattner927b1852003-10-09 20:22:47 +0000340 NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000341 }
Chris Lattner77a316a2001-11-12 21:48:38 +0000342
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000343 Result = new GetElementPtrInst(getValue(RI.Type, Args[0]), Idx);
344 break;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000345 }
Chris Lattner09bd0252003-09-08 18:04:16 +0000346
Chris Lattnerdba2b222003-09-08 18:20:14 +0000347 case 62: // volatile load
Chris Lattner09bd0252003-09-08 18:04:16 +0000348 case Instruction::Load:
Chris Lattner927b1852003-10-09 20:22:47 +0000349 if (Args.size() != 1 || !isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000350 throw std::string("Invalid load instruction!");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000351 Result = new LoadInst(getValue(RI.Type, Args[0]), "", RI.Opcode == 62);
352 break;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000353
Chris Lattnerdba2b222003-09-08 18:20:14 +0000354 case 63: // volatile store
Chris Lattner09bd0252003-09-08 18:04:16 +0000355 case Instruction::Store: {
Chris Lattner927b1852003-10-09 20:22:47 +0000356 if (!isa<PointerType>(InstTy) || Args.size() != 2)
Chris Lattner3483f542003-10-09 18:25:19 +0000357 throw std::string("Invalid store instruction!");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000358
Chris Lattner35d2ca62003-10-09 22:39:30 +0000359 Value *Ptr = getValue(RI.Type, Args[1]);
Chris Lattner352eef72002-08-21 22:55:27 +0000360 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
Chris Lattner8eb49932003-11-19 17:21:11 +0000361 Result = new StoreInst(getValue(getTypeSlot(ValTy), Args[0]), Ptr,
362 RI.Opcode == 63);
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000363 break;
Chris Lattner00950542001-06-06 20:29:01 +0000364 }
Chris Lattner36143fc2003-09-08 18:54:55 +0000365 case Instruction::Unwind:
Chris Lattner927b1852003-10-09 20:22:47 +0000366 if (Args.size() != 0) throw std::string("Invalid unwind instruction!");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000367 Result = new UnwindInst();
368 break;
Chris Lattner927b1852003-10-09 20:22:47 +0000369 } // end switch(RI.Opcode)
Chris Lattner00950542001-06-06 20:29:01 +0000370
Chris Lattner8eb49932003-11-19 17:21:11 +0000371 unsigned TypeSlot;
372 if (Result->getType() == InstTy)
373 TypeSlot = RI.Type;
374 else
375 TypeSlot = getTypeSlot(Result->getType());
376
377 insertValue(Result, TypeSlot, Values);
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000378 BB->getInstList().push_back(Result);
379 BCR_TRACE(4, *Result);
Chris Lattner00950542001-06-06 20:29:01 +0000380}