blob: ec8944e7a87f5413b8c44c47b163a3cfe59b52a0 [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 Lattner00950542001-06-06 20:29:01 +000024
Brian Gaeked0fde302003-11-11 22:41:34 +000025namespace llvm {
26
Chris Lattner6fcf5032003-10-09 20:45:42 +000027namespace {
28 struct RawInst { // The raw fields out of the bytecode stream...
29 unsigned NumOperands;
30 unsigned Opcode;
31 unsigned Type;
32
33 RawInst(const unsigned char *&Buf, const unsigned char *EndBuf,
34 std::vector<unsigned> &Args);
35 };
36}
Chris Lattner927b1852003-10-09 20:22:47 +000037
Chris Lattner927b1852003-10-09 20:22:47 +000038RawInst::RawInst(const unsigned char *&Buf, const unsigned char *EndBuf,
39 std::vector<unsigned> &Args) {
Chris Lattner00950542001-06-06 20:29:01 +000040 unsigned Op, Typ;
Misha Brukmand554ebf2003-09-23 16:17:50 +000041 if (read(Buf, EndBuf, Op))
42 throw std::string("Error reading from buffer.");
Chris Lattner00950542001-06-06 20:29:01 +000043
Chris Lattner2b9f6002001-10-23 03:21:10 +000044 // bits Instruction format: Common to all formats
45 // --------------------------
46 // 01-00: Opcode type, fixed to 1.
47 // 07-02: Opcode
Chris Lattner927b1852003-10-09 20:22:47 +000048 Opcode = (Op >> 2) & 63;
49 Args.resize((Op >> 0) & 03);
Chris Lattner00950542001-06-06 20:29:01 +000050
Chris Lattner927b1852003-10-09 20:22:47 +000051 switch (Args.size()) {
Chris Lattner00950542001-06-06 20:29:01 +000052 case 1:
Chris Lattner2b9f6002001-10-23 03:21:10 +000053 // bits Instruction format:
54 // --------------------------
55 // 19-08: Resulting type plane
56 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
57 //
Chris Lattner927b1852003-10-09 20:22:47 +000058 Type = (Op >> 8) & 4095;
59 Args[0] = (Op >> 20) & 4095;
60 if (Args[0] == 4095) // Handle special encoding for 0 operands...
61 Args.resize(0);
Chris Lattner00950542001-06-06 20:29:01 +000062 break;
63 case 2:
Chris Lattner2b9f6002001-10-23 03:21:10 +000064 // bits Instruction format:
65 // --------------------------
66 // 15-08: Resulting type plane
67 // 23-16: Operand #1
68 // 31-24: Operand #2
69 //
Chris Lattner927b1852003-10-09 20:22:47 +000070 Type = (Op >> 8) & 255;
71 Args[0] = (Op >> 16) & 255;
72 Args[1] = (Op >> 24) & 255;
Chris Lattner00950542001-06-06 20:29:01 +000073 break;
74 case 3:
Chris Lattner2b9f6002001-10-23 03:21:10 +000075 // bits Instruction format:
76 // --------------------------
77 // 13-08: Resulting type plane
78 // 19-14: Operand #1
79 // 25-20: Operand #2
80 // 31-26: Operand #3
81 //
Chris Lattner927b1852003-10-09 20:22:47 +000082 Type = (Op >> 8) & 63;
83 Args[0] = (Op >> 14) & 63;
84 Args[1] = (Op >> 20) & 63;
85 Args[2] = (Op >> 26) & 63;
Chris Lattner00950542001-06-06 20:29:01 +000086 break;
87 case 0:
88 Buf -= 4; // Hrm, try this again...
Chris Lattner927b1852003-10-09 20:22:47 +000089 if (read_vbr(Buf, EndBuf, Opcode))
Misha Brukmand554ebf2003-09-23 16:17:50 +000090 throw std::string("Error reading from buffer.");
Chris Lattner927b1852003-10-09 20:22:47 +000091 Opcode >>= 2;
92 if (read_vbr(Buf, EndBuf, Type))
Misha Brukmand554ebf2003-09-23 16:17:50 +000093 throw std::string("Error reading from buffer.");
Chris Lattner00950542001-06-06 20:29:01 +000094
Chris Lattner927b1852003-10-09 20:22:47 +000095 unsigned NumOperands;
96 if (read_vbr(Buf, EndBuf, NumOperands))
97 throw std::string("Error reading from buffer.");
98 Args.resize(NumOperands);
99
100 if (NumOperands == 0)
Misha Brukmand554ebf2003-09-23 16:17:50 +0000101 throw std::string("Zero-argument instruction found; this is invalid.");
Chris Lattner00950542001-06-06 20:29:01 +0000102
Chris Lattner927b1852003-10-09 20:22:47 +0000103 for (unsigned i = 0; i != NumOperands; ++i)
104 if (read_vbr(Buf, EndBuf, Args[i]))
105 throw std::string("Error reading from buffer");
106 if (align32(Buf, EndBuf))
Misha Brukmand554ebf2003-09-23 16:17:50 +0000107 throw std::string("Unaligned bytecode buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000108 break;
109 }
Chris Lattner00950542001-06-06 20:29:01 +0000110}
111
112
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000113void BytecodeParser::ParseInstruction(const unsigned char *&Buf,
114 const unsigned char *EndBuf,
115 std::vector<unsigned> &Args,
116 BasicBlock *BB) {
Chris Lattner6fcf5032003-10-09 20:45:42 +0000117 Args.clear();
Chris Lattner927b1852003-10-09 20:22:47 +0000118 RawInst RI(Buf, EndBuf, Args);
119 const Type *InstTy = getType(RI.Type);
Chris Lattner00950542001-06-06 20:29:01 +0000120
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000121 Instruction *Result = 0;
Chris Lattner927b1852003-10-09 20:22:47 +0000122 if (RI.Opcode >= Instruction::BinaryOpsBegin &&
123 RI.Opcode < Instruction::BinaryOpsEnd && Args.size() == 2)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000124 Result = BinaryOperator::create((Instruction::BinaryOps)RI.Opcode,
125 getValue(RI.Type, Args[0]),
126 getValue(RI.Type, Args[1]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000127
Chris Lattner927b1852003-10-09 20:22:47 +0000128 switch (RI.Opcode) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000129 default:
130 if (Result == 0) throw std::string("Illegal instruction read!");
131 break;
132 case Instruction::VAArg:
133 Result = new VAArgInst(getValue(RI.Type, Args[0]), getType(Args[1]));
134 break;
135 case Instruction::VANext:
136 if (!hasOldStyleVarargs) {
137 Result = new VANextInst(getValue(RI.Type, Args[0]), getType(Args[1]));
138 } else {
139 // In the old-style varargs scheme, this was the "va_arg" instruction.
140 // Emit emulation code now.
141 if (!usesOldStyleVarargs) {
142 usesOldStyleVarargs = true;
143 std::cerr << "WARNING: this bytecode file uses obsolete features. "
144 << "Disassemble and assemble to update it.\n";
145 }
146
147 Value *VAListPtr = getValue(RI.Type, Args[0]);
148 const Type *ArgTy = getType(Args[1]);
149
150 // First, load the valist...
151 Instruction *CurVAList = new LoadInst(VAListPtr, "");
152 BB->getInstList().push_back(CurVAList);
153
154 // Construct the vaarg
155 Result = new VAArgInst(CurVAList, ArgTy);
156
157 // Now we must advance the pointer and update it in memory.
158 Instruction *TheVANext = new VANextInst(CurVAList, ArgTy);
159 BB->getInstList().push_back(TheVANext);
160
161 BB->getInstList().push_back(new StoreInst(TheVANext, VAListPtr));
162 }
163
164 break;
Chris Lattner927b1852003-10-09 20:22:47 +0000165 case Instruction::Cast:
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000166 Result = new CastInst(getValue(RI.Type, Args[0]), getType(Args[1]));
167 break;
Chris Lattner3b237fc2003-10-19 21:34:28 +0000168 case Instruction::PHI: {
Chris Lattner927b1852003-10-09 20:22:47 +0000169 if (Args.size() == 0 || (Args.size() & 1))
Chris Lattner3483f542003-10-09 18:25:19 +0000170 throw std::string("Invalid phi node encountered!\n");
Chris Lattner927b1852003-10-09 20:22:47 +0000171
172 PHINode *PN = new PHINode(InstTy);
Chris Lattner4c523922003-10-09 22:46:58 +0000173 PN->op_reserve(Args.size());
Chris Lattner927b1852003-10-09 20:22:47 +0000174 for (unsigned i = 0, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000175 PN->addIncoming(getValue(RI.Type, Args[i]), getBasicBlock(Args[i+1]));
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000176 Result = PN;
177 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000178 }
179
180 case Instruction::Shl:
181 case Instruction::Shr:
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000182 Result = new ShiftInst((Instruction::OtherOps)RI.Opcode,
183 getValue(RI.Type, Args[0]),
184 getValue(Type::UByteTyID, Args[1]));
185 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000186 case Instruction::Ret:
Chris Lattner927b1852003-10-09 20:22:47 +0000187 if (Args.size() == 0)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000188 Result = new ReturnInst();
Chris Lattner927b1852003-10-09 20:22:47 +0000189 else if (Args.size() == 1)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000190 Result = new ReturnInst(getValue(RI.Type, Args[0]));
191 else
192 throw std::string("Unrecognized instruction!");
Chris Lattner027dcc52001-07-08 21:10:27 +0000193 break;
194
195 case Instruction::Br:
Chris Lattner927b1852003-10-09 20:22:47 +0000196 if (Args.size() == 1)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000197 Result = new BranchInst(getBasicBlock(Args[0]));
Chris Lattner927b1852003-10-09 20:22:47 +0000198 else if (Args.size() == 3)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000199 Result = new BranchInst(getBasicBlock(Args[0]), getBasicBlock(Args[1]),
200 getValue(Type::BoolTyID , Args[2]));
201 else
202 throw std::string("Invalid number of operands for a 'br' instruction!");
203 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000204 case Instruction::Switch: {
Chris Lattner927b1852003-10-09 20:22:47 +0000205 if (Args.size() & 1)
Chris Lattner3483f542003-10-09 18:25:19 +0000206 throw std::string("Switch statement with odd number of arguments!");
Chris Lattner00950542001-06-06 20:29:01 +0000207
Chris Lattner35d2ca62003-10-09 22:39:30 +0000208 SwitchInst *I = new SwitchInst(getValue(RI.Type, Args[0]),
Chris Lattner927b1852003-10-09 20:22:47 +0000209 getBasicBlock(Args[1]));
210 for (unsigned i = 2, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000211 I->addCase(cast<Constant>(getValue(RI.Type, Args[i])),
Chris Lattner927b1852003-10-09 20:22:47 +0000212 getBasicBlock(Args[i+1]));
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000213 Result = I;
214 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000215 }
216
217 case Instruction::Call: {
Chris Lattner927b1852003-10-09 20:22:47 +0000218 if (Args.size() == 0)
219 throw std::string("Invalid call instruction encountered!");
220
Chris Lattner35d2ca62003-10-09 22:39:30 +0000221 Value *F = getValue(RI.Type, Args[0]);
Chris Lattner00950542001-06-06 20:29:01 +0000222
Chris Lattner3483f542003-10-09 18:25:19 +0000223 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000224 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000225 if (PTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000226 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000227 if (FTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000228
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000229 std::vector<Value *> Params;
Chris Lattner5bea4112003-09-05 18:25:29 +0000230 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
Chris Lattner05950c32001-10-13 06:47:01 +0000231
Chris Lattner5bea4112003-09-05 18:25:29 +0000232 if (!FTy->isVarArg()) {
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000233 FunctionType::ParamTypes::const_iterator It = PL.begin();
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000234
Chris Lattner927b1852003-10-09 20:22:47 +0000235 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
236 if (It == PL.end()) throw std::string("Invalid call instruction!");
237 Params.push_back(getValue(*It++, Args[i]));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000238 }
Chris Lattner3483f542003-10-09 18:25:19 +0000239 if (It != PL.end()) throw std::string("Invalid call instruction!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000240 } else {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000241 Args.erase(Args.begin(), Args.begin()+1+hasVarArgCallPadding);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000242
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000243 unsigned FirstVariableOperand;
244 if (!hasVarArgCallPadding) {
245 if (Args.size() < FTy->getNumParams())
246 throw std::string("Call instruction missing operands!");
247
248 // Read all of the fixed arguments
249 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
250 Params.push_back(getValue(FTy->getParamType(i), Args[i]));
251
252 FirstVariableOperand = FTy->getNumParams();
253 } else {
254 FirstVariableOperand = 0;
255 }
256
257 if ((Args.size()-FirstVariableOperand) & 1) // Must be pairs of type/value
Chris Lattner927b1852003-10-09 20:22:47 +0000258 throw std::string("Invalid call instruction!");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000259
260 for (unsigned i = FirstVariableOperand, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000261 Params.push_back(getValue(Args[i], Args[i+1]));
Chris Lattner00950542001-06-06 20:29:01 +0000262 }
Chris Lattner00950542001-06-06 20:29:01 +0000263
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000264 Result = new CallInst(F, Params);
265 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000266 }
Chris Lattner05950c32001-10-13 06:47:01 +0000267 case Instruction::Invoke: {
Chris Lattner927b1852003-10-09 20:22:47 +0000268 if (Args.size() < 3) throw std::string("Invalid invoke instruction!");
Chris Lattner35d2ca62003-10-09 22:39:30 +0000269 Value *F = getValue(RI.Type, Args[0]);
Chris Lattner05950c32001-10-13 06:47:01 +0000270
Chris Lattner3483f542003-10-09 18:25:19 +0000271 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000272 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000273 if (PTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000274 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000275 if (FTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner05950c32001-10-13 06:47:01 +0000276
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000277 std::vector<Value *> Params;
Chris Lattner05950c32001-10-13 06:47:01 +0000278 BasicBlock *Normal, *Except;
279
Chris Lattner927b1852003-10-09 20:22:47 +0000280 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
281
Chris Lattner5bea4112003-09-05 18:25:29 +0000282 if (!FTy->isVarArg()) {
Chris Lattner927b1852003-10-09 20:22:47 +0000283 Normal = getBasicBlock(Args[1]);
284 Except = getBasicBlock(Args[2]);
Chris Lattner05950c32001-10-13 06:47:01 +0000285
Chris Lattner927b1852003-10-09 20:22:47 +0000286 FunctionType::ParamTypes::const_iterator It = PL.begin();
287 for (unsigned i = 3, e = Args.size(); i != e; ++i) {
288 if (It == PL.end()) throw std::string("Invalid invoke instruction!");
289 Params.push_back(getValue(*It++, Args[i]));
Chris Lattner05950c32001-10-13 06:47:01 +0000290 }
Chris Lattner927b1852003-10-09 20:22:47 +0000291 if (It != PL.end()) throw std::string("Invalid invoke instruction!");
Chris Lattner05950c32001-10-13 06:47:01 +0000292 } else {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000293 Args.erase(Args.begin(), Args.begin()+1+hasVarArgCallPadding);
Chris Lattner927b1852003-10-09 20:22:47 +0000294
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000295 unsigned FirstVariableArgument;
296 if (!hasVarArgCallPadding) {
297 Normal = getBasicBlock(Args[0]);
298 Except = getBasicBlock(Args[1]);
299
300 FirstVariableArgument = FTy->getNumParams()+2;
301 for (unsigned i = 2; i != FirstVariableArgument; ++i)
302 Params.push_back(getValue(FTy->getParamType(i-2), Args[i]));
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000303
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000304 } else {
305 if (Args.size() < 4) throw std::string("Invalid invoke instruction!");
306 if (Args[0] != Type::LabelTyID || Args[2] != Type::LabelTyID)
307 throw std::string("Invalid invoke instruction!");
308 Normal = getBasicBlock(Args[1]);
309 Except = getBasicBlock(Args[3]);
Chris Lattner05950c32001-10-13 06:47:01 +0000310
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000311 FirstVariableArgument = 4;
312 }
313
314 if (Args.size()-FirstVariableArgument & 1) // Must be pairs of type/value
Chris Lattner3483f542003-10-09 18:25:19 +0000315 throw std::string("Invalid invoke instruction!");
316
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000317 for (unsigned i = FirstVariableArgument; i < Args.size(); i += 2)
Chris Lattner927b1852003-10-09 20:22:47 +0000318 Params.push_back(getValue(Args[i], Args[i+1]));
Chris Lattner05950c32001-10-13 06:47:01 +0000319 }
320
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000321 Result = new InvokeInst(F, Normal, Except, Params);
322 break;
Chris Lattner05950c32001-10-13 06:47:01 +0000323 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000324 case Instruction::Malloc:
Chris Lattner927b1852003-10-09 20:22:47 +0000325 if (Args.size() > 2) throw std::string("Invalid malloc instruction!");
326 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000327 throw std::string("Invalid malloc instruction!");
328
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000329 Result = new MallocInst(cast<PointerType>(InstTy)->getElementType(),
330 Args.size() ? getValue(Type::UIntTyID,
331 Args[0]) : 0);
332 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000333
334 case Instruction::Alloca:
Chris Lattner927b1852003-10-09 20:22:47 +0000335 if (Args.size() > 2) throw std::string("Invalid alloca instruction!");
336 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000337 throw std::string("Invalid alloca instruction!");
Chris Lattner027dcc52001-07-08 21:10:27 +0000338
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000339 Result = new AllocaInst(cast<PointerType>(InstTy)->getElementType(),
340 Args.size() ? getValue(Type::UIntTyID, Args[0]) :0);
341 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000342 case Instruction::Free:
Chris Lattner927b1852003-10-09 20:22:47 +0000343 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000344 throw std::string("Invalid free instruction!");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000345 Result = new FreeInst(getValue(RI.Type, Args[0]));
346 break;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000347 case Instruction::GetElementPtr: {
Chris Lattner927b1852003-10-09 20:22:47 +0000348 if (Args.size() == 0 || !isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000349 throw std::string("Invalid getelementptr instruction!");
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000350
Chris Lattner927b1852003-10-09 20:22:47 +0000351 std::vector<Value*> Idx;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000352
Chris Lattner927b1852003-10-09 20:22:47 +0000353 const Type *NextTy = InstTy;
354 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
355 const CompositeType *TopTy = dyn_cast_or_null<CompositeType>(NextTy);
356 if (!TopTy) throw std::string("Invalid getelementptr instruction!");
357 Idx.push_back(getValue(TopTy->getIndexType(), Args[i]));
358 NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true);
Chris Lattner027dcc52001-07-08 21:10:27 +0000359 }
Chris Lattner77a316a2001-11-12 21:48:38 +0000360
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000361 Result = new GetElementPtrInst(getValue(RI.Type, Args[0]), Idx);
362 break;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000363 }
Chris Lattner09bd0252003-09-08 18:04:16 +0000364
Chris Lattnerdba2b222003-09-08 18:20:14 +0000365 case 62: // volatile load
Chris Lattner09bd0252003-09-08 18:04:16 +0000366 case Instruction::Load:
Chris Lattner927b1852003-10-09 20:22:47 +0000367 if (Args.size() != 1 || !isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000368 throw std::string("Invalid load instruction!");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000369 Result = new LoadInst(getValue(RI.Type, Args[0]), "", RI.Opcode == 62);
370 break;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000371
Chris Lattnerdba2b222003-09-08 18:20:14 +0000372 case 63: // volatile store
Chris Lattner09bd0252003-09-08 18:04:16 +0000373 case Instruction::Store: {
Chris Lattner927b1852003-10-09 20:22:47 +0000374 if (!isa<PointerType>(InstTy) || Args.size() != 2)
Chris Lattner3483f542003-10-09 18:25:19 +0000375 throw std::string("Invalid store instruction!");
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000376
Chris Lattner35d2ca62003-10-09 22:39:30 +0000377 Value *Ptr = getValue(RI.Type, Args[1]);
Chris Lattner352eef72002-08-21 22:55:27 +0000378 const Type *ValTy = cast<PointerType>(Ptr->getType())->getElementType();
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000379 Result = new StoreInst(getValue(ValTy, Args[0]), Ptr, RI.Opcode == 63);
380 break;
Chris Lattner00950542001-06-06 20:29:01 +0000381 }
Chris Lattner36143fc2003-09-08 18:54:55 +0000382 case Instruction::Unwind:
Chris Lattner927b1852003-10-09 20:22:47 +0000383 if (Args.size() != 0) throw std::string("Invalid unwind instruction!");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000384 Result = new UnwindInst();
385 break;
Chris Lattner927b1852003-10-09 20:22:47 +0000386 } // end switch(RI.Opcode)
Chris Lattner00950542001-06-06 20:29:01 +0000387
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000388 insertValue(Result, Values);
389 BB->getInstList().push_back(Result);
390 BCR_TRACE(4, *Result);
Chris Lattner00950542001-06-06 20:29:01 +0000391}
Brian Gaeked0fde302003-11-11 22:41:34 +0000392
393} // End llvm namespace