blob: 0ade581c1fda8415fe63af758a348553ab673f50 [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:
127 if (!hasOldStyleVarargs) {
128 Result = new VANextInst(getValue(RI.Type, Args[0]), getType(Args[1]));
129 } else {
130 // In the old-style varargs scheme, this was the "va_arg" instruction.
131 // Emit emulation code now.
132 if (!usesOldStyleVarargs) {
133 usesOldStyleVarargs = true;
134 std::cerr << "WARNING: this bytecode file uses obsolete features. "
135 << "Disassemble and assemble to update it.\n";
136 }
137
138 Value *VAListPtr = getValue(RI.Type, Args[0]);
139 const Type *ArgTy = getType(Args[1]);
140
141 // First, load the valist...
142 Instruction *CurVAList = new LoadInst(VAListPtr, "");
143 BB->getInstList().push_back(CurVAList);
144
145 // Construct the vaarg
146 Result = new VAArgInst(CurVAList, ArgTy);
147
148 // Now we must advance the pointer and update it in memory.
149 Instruction *TheVANext = new VANextInst(CurVAList, ArgTy);
150 BB->getInstList().push_back(TheVANext);
151
152 BB->getInstList().push_back(new StoreInst(TheVANext, VAListPtr));
153 }
154
155 break;
Chris Lattner927b1852003-10-09 20:22:47 +0000156 case Instruction::Cast:
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000157 Result = new CastInst(getValue(RI.Type, Args[0]), getType(Args[1]));
158 break;
Chris Lattnere3ac22a2004-03-12 05:51:49 +0000159 case Instruction::Select:
160 Result = new SelectInst(getValue(Type::BoolTyID, Args[0]),
161 getValue(RI.Type, Args[1]),
162 getValue(RI.Type, Args[2]));
163 break;
Chris Lattner3b237fc2003-10-19 21:34:28 +0000164 case Instruction::PHI: {
Chris Lattner927b1852003-10-09 20:22:47 +0000165 if (Args.size() == 0 || (Args.size() & 1))
Chris Lattner3483f542003-10-09 18:25:19 +0000166 throw std::string("Invalid phi node encountered!\n");
Chris Lattner927b1852003-10-09 20:22:47 +0000167
168 PHINode *PN = new PHINode(InstTy);
Chris Lattner4c523922003-10-09 22:46:58 +0000169 PN->op_reserve(Args.size());
Chris Lattner927b1852003-10-09 20:22:47 +0000170 for (unsigned i = 0, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000171 PN->addIncoming(getValue(RI.Type, Args[i]), getBasicBlock(Args[i+1]));
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000172 Result = PN;
173 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000174 }
175
176 case Instruction::Shl:
177 case Instruction::Shr:
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000178 Result = new ShiftInst((Instruction::OtherOps)RI.Opcode,
179 getValue(RI.Type, Args[0]),
180 getValue(Type::UByteTyID, Args[1]));
181 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000182 case Instruction::Ret:
Chris Lattner927b1852003-10-09 20:22:47 +0000183 if (Args.size() == 0)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000184 Result = new ReturnInst();
Chris Lattner927b1852003-10-09 20:22:47 +0000185 else if (Args.size() == 1)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000186 Result = new ReturnInst(getValue(RI.Type, Args[0]));
187 else
188 throw std::string("Unrecognized instruction!");
Chris Lattner027dcc52001-07-08 21:10:27 +0000189 break;
190
191 case Instruction::Br:
Chris Lattner927b1852003-10-09 20:22:47 +0000192 if (Args.size() == 1)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000193 Result = new BranchInst(getBasicBlock(Args[0]));
Chris Lattner927b1852003-10-09 20:22:47 +0000194 else if (Args.size() == 3)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000195 Result = new BranchInst(getBasicBlock(Args[0]), getBasicBlock(Args[1]),
196 getValue(Type::BoolTyID , Args[2]));
197 else
198 throw std::string("Invalid number of operands for a 'br' instruction!");
199 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000200 case Instruction::Switch: {
Chris Lattner927b1852003-10-09 20:22:47 +0000201 if (Args.size() & 1)
Chris Lattner3483f542003-10-09 18:25:19 +0000202 throw std::string("Switch statement with odd number of arguments!");
Chris Lattner00950542001-06-06 20:29:01 +0000203
Chris Lattner35d2ca62003-10-09 22:39:30 +0000204 SwitchInst *I = new SwitchInst(getValue(RI.Type, Args[0]),
Chris Lattner927b1852003-10-09 20:22:47 +0000205 getBasicBlock(Args[1]));
206 for (unsigned i = 2, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000207 I->addCase(cast<Constant>(getValue(RI.Type, Args[i])),
Chris Lattner927b1852003-10-09 20:22:47 +0000208 getBasicBlock(Args[i+1]));
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000209 Result = I;
210 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000211 }
212
213 case Instruction::Call: {
Chris Lattner927b1852003-10-09 20:22:47 +0000214 if (Args.size() == 0)
215 throw std::string("Invalid call instruction encountered!");
216
Chris Lattner35d2ca62003-10-09 22:39:30 +0000217 Value *F = getValue(RI.Type, Args[0]);
Chris Lattner00950542001-06-06 20:29:01 +0000218
Chris Lattner3483f542003-10-09 18:25:19 +0000219 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000220 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000221 if (PTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000222 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000223 if (FTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000224
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000225 std::vector<Value *> Params;
Chris Lattner5bea4112003-09-05 18:25:29 +0000226 if (!FTy->isVarArg()) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000227 FunctionType::param_iterator It = FTy->param_begin();
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000228
Chris Lattner927b1852003-10-09 20:22:47 +0000229 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000230 if (It == FTy->param_end())
231 throw std::string("Invalid call instruction!");
Chris Lattner8eb49932003-11-19 17:21:11 +0000232 Params.push_back(getValue(getTypeSlot(*It++), Args[i]));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000233 }
Chris Lattnerd5d89962004-02-09 04:14:01 +0000234 if (It != FTy->param_end())
235 throw std::string("Invalid call instruction!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000236 } else {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000237 Args.erase(Args.begin(), Args.begin()+1+hasVarArgCallPadding);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000238
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000239 unsigned FirstVariableOperand;
240 if (!hasVarArgCallPadding) {
241 if (Args.size() < FTy->getNumParams())
242 throw std::string("Call instruction missing operands!");
243
244 // Read all of the fixed arguments
245 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Chris Lattner8eb49932003-11-19 17:21:11 +0000246 Params.push_back(getValue(getTypeSlot(FTy->getParamType(i)),Args[i]));
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000247
248 FirstVariableOperand = FTy->getNumParams();
249 } else {
250 FirstVariableOperand = 0;
251 }
252
253 if ((Args.size()-FirstVariableOperand) & 1) // Must be pairs of type/value
Chris Lattner927b1852003-10-09 20:22:47 +0000254 throw std::string("Invalid call instruction!");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000255
256 for (unsigned i = FirstVariableOperand, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000257 Params.push_back(getValue(Args[i], Args[i+1]));
Chris Lattner00950542001-06-06 20:29:01 +0000258 }
Chris Lattner00950542001-06-06 20:29:01 +0000259
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000260 Result = new CallInst(F, Params);
261 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000262 }
Chris Lattner05950c32001-10-13 06:47:01 +0000263 case Instruction::Invoke: {
Chris Lattner927b1852003-10-09 20:22:47 +0000264 if (Args.size() < 3) throw std::string("Invalid invoke instruction!");
Chris Lattner35d2ca62003-10-09 22:39:30 +0000265 Value *F = getValue(RI.Type, Args[0]);
Chris Lattner05950c32001-10-13 06:47:01 +0000266
Chris Lattner3483f542003-10-09 18:25:19 +0000267 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000268 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000269 if (PTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000270 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000271 if (FTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner05950c32001-10-13 06:47:01 +0000272
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000273 std::vector<Value *> Params;
Chris Lattner05950c32001-10-13 06:47:01 +0000274 BasicBlock *Normal, *Except;
275
Chris Lattner5bea4112003-09-05 18:25:29 +0000276 if (!FTy->isVarArg()) {
Chris Lattner927b1852003-10-09 20:22:47 +0000277 Normal = getBasicBlock(Args[1]);
278 Except = getBasicBlock(Args[2]);
Chris Lattner05950c32001-10-13 06:47:01 +0000279
Chris Lattnerd5d89962004-02-09 04:14:01 +0000280 FunctionType::param_iterator It = FTy->param_begin();
Chris Lattner927b1852003-10-09 20:22:47 +0000281 for (unsigned i = 3, e = Args.size(); i != e; ++i) {
Chris Lattnerd5d89962004-02-09 04:14:01 +0000282 if (It == FTy->param_end())
283 throw std::string("Invalid invoke instruction!");
Chris Lattner8eb49932003-11-19 17:21:11 +0000284 Params.push_back(getValue(getTypeSlot(*It++), Args[i]));
Chris Lattner05950c32001-10-13 06:47:01 +0000285 }
Chris Lattnerd5d89962004-02-09 04:14:01 +0000286 if (It != FTy->param_end())
287 throw std::string("Invalid invoke instruction!");
Chris Lattner05950c32001-10-13 06:47:01 +0000288 } else {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000289 Args.erase(Args.begin(), Args.begin()+1+hasVarArgCallPadding);
Chris Lattner927b1852003-10-09 20:22:47 +0000290
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000291 unsigned FirstVariableArgument;
292 if (!hasVarArgCallPadding) {
293 Normal = getBasicBlock(Args[0]);
294 Except = getBasicBlock(Args[1]);
295
296 FirstVariableArgument = FTy->getNumParams()+2;
297 for (unsigned i = 2; i != FirstVariableArgument; ++i)
Chris Lattner8eb49932003-11-19 17:21:11 +0000298 Params.push_back(getValue(getTypeSlot(FTy->getParamType(i-2)),
299 Args[i]));
Chris Lattner4ee8ef22003-10-08 22:52:54 +0000300
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000301 } else {
302 if (Args.size() < 4) throw std::string("Invalid invoke instruction!");
303 if (Args[0] != Type::LabelTyID || Args[2] != Type::LabelTyID)
304 throw std::string("Invalid invoke instruction!");
305 Normal = getBasicBlock(Args[1]);
306 Except = getBasicBlock(Args[3]);
Chris Lattner05950c32001-10-13 06:47:01 +0000307
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000308 FirstVariableArgument = 4;
309 }
310
311 if (Args.size()-FirstVariableArgument & 1) // Must be pairs of type/value
Chris Lattner3483f542003-10-09 18:25:19 +0000312 throw std::string("Invalid invoke instruction!");
313
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000314 for (unsigned i = FirstVariableArgument; i < Args.size(); i += 2)
Chris Lattner927b1852003-10-09 20:22:47 +0000315 Params.push_back(getValue(Args[i], Args[i+1]));
Chris Lattner05950c32001-10-13 06:47:01 +0000316 }
317
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000318 Result = new InvokeInst(F, Normal, Except, Params);
319 break;
Chris Lattner05950c32001-10-13 06:47:01 +0000320 }
Chris Lattner027dcc52001-07-08 21:10:27 +0000321 case Instruction::Malloc:
Chris Lattner927b1852003-10-09 20:22:47 +0000322 if (Args.size() > 2) throw std::string("Invalid malloc instruction!");
323 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000324 throw std::string("Invalid malloc instruction!");
325
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000326 Result = new MallocInst(cast<PointerType>(InstTy)->getElementType(),
327 Args.size() ? getValue(Type::UIntTyID,
328 Args[0]) : 0);
329 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000330
331 case Instruction::Alloca:
Chris Lattner927b1852003-10-09 20:22:47 +0000332 if (Args.size() > 2) throw std::string("Invalid alloca instruction!");
333 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000334 throw std::string("Invalid alloca instruction!");
Chris Lattner027dcc52001-07-08 21:10:27 +0000335
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000336 Result = new AllocaInst(cast<PointerType>(InstTy)->getElementType(),
337 Args.size() ? getValue(Type::UIntTyID, Args[0]) :0);
338 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000339 case Instruction::Free:
Chris Lattner927b1852003-10-09 20:22:47 +0000340 if (!isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000341 throw std::string("Invalid free instruction!");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000342 Result = new FreeInst(getValue(RI.Type, Args[0]));
343 break;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000344 case Instruction::GetElementPtr: {
Chris Lattner927b1852003-10-09 20:22:47 +0000345 if (Args.size() == 0 || !isa<PointerType>(InstTy))
Chris Lattner3483f542003-10-09 18:25:19 +0000346 throw std::string("Invalid getelementptr instruction!");
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000347
Chris Lattner927b1852003-10-09 20:22:47 +0000348 std::vector<Value*> Idx;
Chris Lattnerb2b12b42001-11-26 16:54:55 +0000349
Chris Lattner927b1852003-10-09 20:22:47 +0000350 const Type *NextTy = InstTy;
351 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
352 const CompositeType *TopTy = dyn_cast_or_null<CompositeType>(NextTy);
353 if (!TopTy) throw std::string("Invalid getelementptr instruction!");
Chris Lattner258b5372004-01-09 05:42:34 +0000354 // FIXME: when PR82 is resolved.
355 unsigned IdxTy = isa<StructType>(TopTy) ? Type::UByteTyID :Type::LongTyID;
356
357 Idx.push_back(getValue(IdxTy, Args[i]));
Chris Lattner927b1852003-10-09 20:22:47 +0000358 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 Lattner8eb49932003-11-19 17:21:11 +0000379 Result = new StoreInst(getValue(getTypeSlot(ValTy), Args[0]), Ptr,
380 RI.Opcode == 63);
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000381 break;
Chris Lattner00950542001-06-06 20:29:01 +0000382 }
Chris Lattner36143fc2003-09-08 18:54:55 +0000383 case Instruction::Unwind:
Chris Lattner927b1852003-10-09 20:22:47 +0000384 if (Args.size() != 0) throw std::string("Invalid unwind instruction!");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000385 Result = new UnwindInst();
386 break;
Chris Lattner927b1852003-10-09 20:22:47 +0000387 } // end switch(RI.Opcode)
Chris Lattner00950542001-06-06 20:29:01 +0000388
Chris Lattner8eb49932003-11-19 17:21:11 +0000389 unsigned TypeSlot;
390 if (Result->getType() == InstTy)
391 TypeSlot = RI.Type;
392 else
393 TypeSlot = getTypeSlot(Result->getType());
394
395 insertValue(Result, TypeSlot, Values);
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000396 BB->getInstList().push_back(Result);
397 BCR_TRACE(4, *Result);
Chris Lattner00950542001-06-06 20:29:01 +0000398}