blob: 3ca00f35cc66f1418b8fb9af8f6d037b7c87ea58 [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 Lattner00950542001-06-06 20:29:01 +000039 unsigned Op, Typ;
Misha Brukmand554ebf2003-09-23 16:17:50 +000040 if (read(Buf, EndBuf, Op))
41 throw std::string("Error reading from buffer.");
Chris Lattner00950542001-06-06 20:29:01 +000042
Chris Lattner2b9f6002001-10-23 03:21:10 +000043 // bits Instruction format: Common to all formats
44 // --------------------------
45 // 01-00: Opcode type, fixed to 1.
46 // 07-02: Opcode
Chris Lattner927b1852003-10-09 20:22:47 +000047 Opcode = (Op >> 2) & 63;
48 Args.resize((Op >> 0) & 03);
Chris Lattner00950542001-06-06 20:29:01 +000049
Chris Lattner927b1852003-10-09 20:22:47 +000050 switch (Args.size()) {
Chris Lattner00950542001-06-06 20:29:01 +000051 case 1:
Chris Lattner2b9f6002001-10-23 03:21:10 +000052 // bits Instruction format:
53 // --------------------------
54 // 19-08: Resulting type plane
55 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
56 //
Chris Lattner927b1852003-10-09 20:22:47 +000057 Type = (Op >> 8) & 4095;
58 Args[0] = (Op >> 20) & 4095;
59 if (Args[0] == 4095) // Handle special encoding for 0 operands...
60 Args.resize(0);
Chris Lattner00950542001-06-06 20:29:01 +000061 break;
62 case 2:
Chris Lattner2b9f6002001-10-23 03:21:10 +000063 // bits Instruction format:
64 // --------------------------
65 // 15-08: Resulting type plane
66 // 23-16: Operand #1
67 // 31-24: Operand #2
68 //
Chris Lattner927b1852003-10-09 20:22:47 +000069 Type = (Op >> 8) & 255;
70 Args[0] = (Op >> 16) & 255;
71 Args[1] = (Op >> 24) & 255;
Chris Lattner00950542001-06-06 20:29:01 +000072 break;
73 case 3:
Chris Lattner2b9f6002001-10-23 03:21:10 +000074 // bits Instruction format:
75 // --------------------------
76 // 13-08: Resulting type plane
77 // 19-14: Operand #1
78 // 25-20: Operand #2
79 // 31-26: Operand #3
80 //
Chris Lattner927b1852003-10-09 20:22:47 +000081 Type = (Op >> 8) & 63;
82 Args[0] = (Op >> 14) & 63;
83 Args[1] = (Op >> 20) & 63;
84 Args[2] = (Op >> 26) & 63;
Chris Lattner00950542001-06-06 20:29:01 +000085 break;
86 case 0:
87 Buf -= 4; // Hrm, try this again...
Chris Lattner927b1852003-10-09 20:22:47 +000088 if (read_vbr(Buf, EndBuf, Opcode))
Misha Brukmand554ebf2003-09-23 16:17:50 +000089 throw std::string("Error reading from buffer.");
Chris Lattner927b1852003-10-09 20:22:47 +000090 Opcode >>= 2;
91 if (read_vbr(Buf, EndBuf, Type))
Misha Brukmand554ebf2003-09-23 16:17:50 +000092 throw std::string("Error reading from buffer.");
Chris Lattner00950542001-06-06 20:29:01 +000093
Chris Lattner927b1852003-10-09 20:22:47 +000094 unsigned NumOperands;
95 if (read_vbr(Buf, EndBuf, NumOperands))
96 throw std::string("Error reading from buffer.");
97 Args.resize(NumOperands);
98
99 if (NumOperands == 0)
Misha Brukmand554ebf2003-09-23 16:17:50 +0000100 throw std::string("Zero-argument instruction found; this is invalid.");
Chris Lattner00950542001-06-06 20:29:01 +0000101
Chris Lattner927b1852003-10-09 20:22:47 +0000102 for (unsigned i = 0; i != NumOperands; ++i)
103 if (read_vbr(Buf, EndBuf, Args[i]))
104 throw std::string("Error reading from buffer");
105 if (align32(Buf, EndBuf))
Misha Brukmand554ebf2003-09-23 16:17:50 +0000106 throw std::string("Unaligned bytecode buffer.");
Chris Lattner00950542001-06-06 20:29:01 +0000107 break;
108 }
Chris Lattner00950542001-06-06 20:29:01 +0000109}
110
111
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000112void BytecodeParser::ParseInstruction(const unsigned char *&Buf,
113 const unsigned char *EndBuf,
114 std::vector<unsigned> &Args,
115 BasicBlock *BB) {
Chris Lattner6fcf5032003-10-09 20:45:42 +0000116 Args.clear();
Chris Lattner927b1852003-10-09 20:22:47 +0000117 RawInst RI(Buf, EndBuf, Args);
118 const Type *InstTy = getType(RI.Type);
Chris Lattner00950542001-06-06 20:29:01 +0000119
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000120 Instruction *Result = 0;
Chris Lattner927b1852003-10-09 20:22:47 +0000121 if (RI.Opcode >= Instruction::BinaryOpsBegin &&
122 RI.Opcode < Instruction::BinaryOpsEnd && Args.size() == 2)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000123 Result = BinaryOperator::create((Instruction::BinaryOps)RI.Opcode,
124 getValue(RI.Type, Args[0]),
125 getValue(RI.Type, Args[1]));
Chris Lattner027dcc52001-07-08 21:10:27 +0000126
Chris Lattner927b1852003-10-09 20:22:47 +0000127 switch (RI.Opcode) {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000128 default:
129 if (Result == 0) throw std::string("Illegal instruction read!");
130 break;
131 case Instruction::VAArg:
132 Result = new VAArgInst(getValue(RI.Type, Args[0]), getType(Args[1]));
133 break;
134 case Instruction::VANext:
135 if (!hasOldStyleVarargs) {
136 Result = new VANextInst(getValue(RI.Type, Args[0]), getType(Args[1]));
137 } else {
138 // In the old-style varargs scheme, this was the "va_arg" instruction.
139 // Emit emulation code now.
140 if (!usesOldStyleVarargs) {
141 usesOldStyleVarargs = true;
142 std::cerr << "WARNING: this bytecode file uses obsolete features. "
143 << "Disassemble and assemble to update it.\n";
144 }
145
146 Value *VAListPtr = getValue(RI.Type, Args[0]);
147 const Type *ArgTy = getType(Args[1]);
148
149 // First, load the valist...
150 Instruction *CurVAList = new LoadInst(VAListPtr, "");
151 BB->getInstList().push_back(CurVAList);
152
153 // Construct the vaarg
154 Result = new VAArgInst(CurVAList, ArgTy);
155
156 // Now we must advance the pointer and update it in memory.
157 Instruction *TheVANext = new VANextInst(CurVAList, ArgTy);
158 BB->getInstList().push_back(TheVANext);
159
160 BB->getInstList().push_back(new StoreInst(TheVANext, VAListPtr));
161 }
162
163 break;
Chris Lattner927b1852003-10-09 20:22:47 +0000164 case Instruction::Cast:
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000165 Result = new CastInst(getValue(RI.Type, Args[0]), getType(Args[1]));
166 break;
Chris Lattner3b237fc2003-10-19 21:34:28 +0000167 case Instruction::PHI: {
Chris Lattner927b1852003-10-09 20:22:47 +0000168 if (Args.size() == 0 || (Args.size() & 1))
Chris Lattner3483f542003-10-09 18:25:19 +0000169 throw std::string("Invalid phi node encountered!\n");
Chris Lattner927b1852003-10-09 20:22:47 +0000170
171 PHINode *PN = new PHINode(InstTy);
Chris Lattner4c523922003-10-09 22:46:58 +0000172 PN->op_reserve(Args.size());
Chris Lattner927b1852003-10-09 20:22:47 +0000173 for (unsigned i = 0, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000174 PN->addIncoming(getValue(RI.Type, Args[i]), getBasicBlock(Args[i+1]));
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000175 Result = PN;
176 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000177 }
178
179 case Instruction::Shl:
180 case Instruction::Shr:
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000181 Result = new ShiftInst((Instruction::OtherOps)RI.Opcode,
182 getValue(RI.Type, Args[0]),
183 getValue(Type::UByteTyID, Args[1]));
184 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000185 case Instruction::Ret:
Chris Lattner927b1852003-10-09 20:22:47 +0000186 if (Args.size() == 0)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000187 Result = new ReturnInst();
Chris Lattner927b1852003-10-09 20:22:47 +0000188 else if (Args.size() == 1)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000189 Result = new ReturnInst(getValue(RI.Type, Args[0]));
190 else
191 throw std::string("Unrecognized instruction!");
Chris Lattner027dcc52001-07-08 21:10:27 +0000192 break;
193
194 case Instruction::Br:
Chris Lattner927b1852003-10-09 20:22:47 +0000195 if (Args.size() == 1)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000196 Result = new BranchInst(getBasicBlock(Args[0]));
Chris Lattner927b1852003-10-09 20:22:47 +0000197 else if (Args.size() == 3)
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000198 Result = new BranchInst(getBasicBlock(Args[0]), getBasicBlock(Args[1]),
199 getValue(Type::BoolTyID , Args[2]));
200 else
201 throw std::string("Invalid number of operands for a 'br' instruction!");
202 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000203 case Instruction::Switch: {
Chris Lattner927b1852003-10-09 20:22:47 +0000204 if (Args.size() & 1)
Chris Lattner3483f542003-10-09 18:25:19 +0000205 throw std::string("Switch statement with odd number of arguments!");
Chris Lattner00950542001-06-06 20:29:01 +0000206
Chris Lattner35d2ca62003-10-09 22:39:30 +0000207 SwitchInst *I = new SwitchInst(getValue(RI.Type, Args[0]),
Chris Lattner927b1852003-10-09 20:22:47 +0000208 getBasicBlock(Args[1]));
209 for (unsigned i = 2, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000210 I->addCase(cast<Constant>(getValue(RI.Type, Args[i])),
Chris Lattner927b1852003-10-09 20:22:47 +0000211 getBasicBlock(Args[i+1]));
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000212 Result = I;
213 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000214 }
215
216 case Instruction::Call: {
Chris Lattner927b1852003-10-09 20:22:47 +0000217 if (Args.size() == 0)
218 throw std::string("Invalid call instruction encountered!");
219
Chris Lattner35d2ca62003-10-09 22:39:30 +0000220 Value *F = getValue(RI.Type, Args[0]);
Chris Lattner00950542001-06-06 20:29:01 +0000221
Chris Lattner3483f542003-10-09 18:25:19 +0000222 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000223 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000224 if (PTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000225 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000226 if (FTy == 0) throw std::string("Call to non function pointer value!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000227
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000228 std::vector<Value *> Params;
Chris Lattner5bea4112003-09-05 18:25:29 +0000229 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
Chris Lattner05950c32001-10-13 06:47:01 +0000230
Chris Lattner5bea4112003-09-05 18:25:29 +0000231 if (!FTy->isVarArg()) {
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000232 FunctionType::ParamTypes::const_iterator It = PL.begin();
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000233
Chris Lattner927b1852003-10-09 20:22:47 +0000234 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
235 if (It == PL.end()) throw std::string("Invalid call instruction!");
Chris Lattner8eb49932003-11-19 17:21:11 +0000236 Params.push_back(getValue(getTypeSlot(*It++), Args[i]));
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000237 }
Chris Lattner3483f542003-10-09 18:25:19 +0000238 if (It != PL.end()) throw std::string("Invalid call instruction!");
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000239 } else {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000240 Args.erase(Args.begin(), Args.begin()+1+hasVarArgCallPadding);
Chris Lattnere5a57ee2001-07-25 22:47:55 +0000241
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000242 unsigned FirstVariableOperand;
243 if (!hasVarArgCallPadding) {
244 if (Args.size() < FTy->getNumParams())
245 throw std::string("Call instruction missing operands!");
246
247 // Read all of the fixed arguments
248 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Chris Lattner8eb49932003-11-19 17:21:11 +0000249 Params.push_back(getValue(getTypeSlot(FTy->getParamType(i)),Args[i]));
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000250
251 FirstVariableOperand = FTy->getNumParams();
252 } else {
253 FirstVariableOperand = 0;
254 }
255
256 if ((Args.size()-FirstVariableOperand) & 1) // Must be pairs of type/value
Chris Lattner927b1852003-10-09 20:22:47 +0000257 throw std::string("Invalid call instruction!");
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000258
259 for (unsigned i = FirstVariableOperand, e = Args.size(); i != e; i += 2)
Chris Lattner35d2ca62003-10-09 22:39:30 +0000260 Params.push_back(getValue(Args[i], Args[i+1]));
Chris Lattner00950542001-06-06 20:29:01 +0000261 }
Chris Lattner00950542001-06-06 20:29:01 +0000262
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000263 Result = new CallInst(F, Params);
264 break;
Chris Lattner027dcc52001-07-08 21:10:27 +0000265 }
Chris Lattner05950c32001-10-13 06:47:01 +0000266 case Instruction::Invoke: {
Chris Lattner927b1852003-10-09 20:22:47 +0000267 if (Args.size() < 3) throw std::string("Invalid invoke instruction!");
Chris Lattner35d2ca62003-10-09 22:39:30 +0000268 Value *F = getValue(RI.Type, Args[0]);
Chris Lattner05950c32001-10-13 06:47:01 +0000269
Chris Lattner3483f542003-10-09 18:25:19 +0000270 // Check to make sure we have a pointer to function type
Chris Lattner5bea4112003-09-05 18:25:29 +0000271 const PointerType *PTy = dyn_cast<PointerType>(F->getType());
Chris Lattner3483f542003-10-09 18:25:19 +0000272 if (PTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner5bea4112003-09-05 18:25:29 +0000273 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
Chris Lattner3483f542003-10-09 18:25:19 +0000274 if (FTy == 0) throw std::string("Invoke to non function pointer value!");
Chris Lattner05950c32001-10-13 06:47:01 +0000275
Chris Lattner0d75d8d72003-03-06 16:32:25 +0000276 std::vector<Value *> Params;
Chris Lattner05950c32001-10-13 06:47:01 +0000277 BasicBlock *Normal, *Except;
278
Chris Lattner927b1852003-10-09 20:22:47 +0000279 const FunctionType::ParamTypes &PL = FTy->getParamTypes();
280
Chris Lattner5bea4112003-09-05 18:25:29 +0000281 if (!FTy->isVarArg()) {
Chris Lattner927b1852003-10-09 20:22:47 +0000282 Normal = getBasicBlock(Args[1]);
283 Except = getBasicBlock(Args[2]);
Chris Lattner05950c32001-10-13 06:47:01 +0000284
Chris Lattner927b1852003-10-09 20:22:47 +0000285 FunctionType::ParamTypes::const_iterator It = PL.begin();
286 for (unsigned i = 3, e = Args.size(); i != e; ++i) {
287 if (It == PL.end()) throw std::string("Invalid invoke instruction!");
Chris Lattner8eb49932003-11-19 17:21:11 +0000288 Params.push_back(getValue(getTypeSlot(*It++), Args[i]));
Chris Lattner05950c32001-10-13 06:47:01 +0000289 }
Chris Lattner927b1852003-10-09 20:22:47 +0000290 if (It != PL.end()) throw std::string("Invalid invoke instruction!");
Chris Lattner05950c32001-10-13 06:47:01 +0000291 } else {
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000292 Args.erase(Args.begin(), Args.begin()+1+hasVarArgCallPadding);
Chris Lattner927b1852003-10-09 20:22:47 +0000293
Chris Lattnercb7e2e22003-10-18 05:54:18 +0000294 unsigned FirstVariableArgument;
295 if (!hasVarArgCallPadding) {
296 Normal = getBasicBlock(Args[0]);
297 Except = getBasicBlock(Args[1]);
298
299 FirstVariableArgument = FTy->getNumParams()+2;
300 for (unsigned i = 2; i != FirstVariableArgument; ++i)
Chris Lattner8eb49932003-11-19 17:21:11 +0000301 Params.push_back(getValue(getTypeSlot(FTy->getParamType(i-2)),
302 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!");
Chris Lattner8eb49932003-11-19 17:21:11 +0000357 Idx.push_back(getValue(TopTy->getIndexType()->getPrimitiveID(), 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}