Execution.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. //===-- Execution.cpp - Implement code to simulate the program ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains the actual instruction interpreter.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #define DEBUG_TYPE "interpreter"
  14. #include "Interpreter.h"
  15. #include "llvm/Constants.h"
  16. #include "llvm/DerivedTypes.h"
  17. #include "llvm/Instructions.h"
  18. #include "llvm/CodeGen/IntrinsicLowering.h"
  19. #include "llvm/Support/GetElementPtrTypeIterator.h"
  20. #include "llvm/ADT/APInt.h"
  21. #include "llvm/ADT/Statistic.h"
  22. #include "llvm/Support/CommandLine.h"
  23. #include "llvm/Support/Debug.h"
  24. #include "llvm/Support/ErrorHandling.h"
  25. #include "llvm/Support/MathExtras.h"
  26. #include <algorithm>
  27. #include <cmath>
  28. using namespace llvm;
  29. STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
  30. static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
  31. cl::desc("make the interpreter print every volatile load and store"));
  32. //===----------------------------------------------------------------------===//
  33. // Various Helper Functions
  34. //===----------------------------------------------------------------------===//
  35. static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
  36. SF.Values[V] = Val;
  37. }
  38. //===----------------------------------------------------------------------===//
  39. // Binary Instruction Implementations
  40. //===----------------------------------------------------------------------===//
  41. #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
  42. case Type::TY##TyID: \
  43. Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
  44. break
  45. static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
  46. GenericValue Src2, Type *Ty) {
  47. switch (Ty->getTypeID()) {
  48. IMPLEMENT_BINARY_OPERATOR(+, Float);
  49. IMPLEMENT_BINARY_OPERATOR(+, Double);
  50. default:
  51. dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n";
  52. llvm_unreachable(0);
  53. }
  54. }
  55. static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
  56. GenericValue Src2, Type *Ty) {
  57. switch (Ty->getTypeID()) {
  58. IMPLEMENT_BINARY_OPERATOR(-, Float);
  59. IMPLEMENT_BINARY_OPERATOR(-, Double);
  60. default:
  61. dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n";
  62. llvm_unreachable(0);
  63. }
  64. }
  65. static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
  66. GenericValue Src2, Type *Ty) {
  67. switch (Ty->getTypeID()) {
  68. IMPLEMENT_BINARY_OPERATOR(*, Float);
  69. IMPLEMENT_BINARY_OPERATOR(*, Double);
  70. default:
  71. dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n";
  72. llvm_unreachable(0);
  73. }
  74. }
  75. static void executeFDivInst(GenericValue &Dest, GenericValue Src1,
  76. GenericValue Src2, Type *Ty) {
  77. switch (Ty->getTypeID()) {
  78. IMPLEMENT_BINARY_OPERATOR(/, Float);
  79. IMPLEMENT_BINARY_OPERATOR(/, Double);
  80. default:
  81. dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n";
  82. llvm_unreachable(0);
  83. }
  84. }
  85. static void executeFRemInst(GenericValue &Dest, GenericValue Src1,
  86. GenericValue Src2, Type *Ty) {
  87. switch (Ty->getTypeID()) {
  88. case Type::FloatTyID:
  89. Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
  90. break;
  91. case Type::DoubleTyID:
  92. Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
  93. break;
  94. default:
  95. dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
  96. llvm_unreachable(0);
  97. }
  98. }
  99. #define IMPLEMENT_INTEGER_ICMP(OP, TY) \
  100. case Type::IntegerTyID: \
  101. Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
  102. break;
  103. // Handle pointers specially because they must be compared with only as much
  104. // width as the host has. We _do not_ want to be comparing 64 bit values when
  105. // running on a 32-bit target, otherwise the upper 32 bits might mess up
  106. // comparisons if they contain garbage.
  107. #define IMPLEMENT_POINTER_ICMP(OP) \
  108. case Type::PointerTyID: \
  109. Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
  110. (void*)(intptr_t)Src2.PointerVal); \
  111. break;
  112. static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
  113. Type *Ty) {
  114. GenericValue Dest;
  115. switch (Ty->getTypeID()) {
  116. IMPLEMENT_INTEGER_ICMP(eq,Ty);
  117. IMPLEMENT_POINTER_ICMP(==);
  118. default:
  119. dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
  120. llvm_unreachable(0);
  121. }
  122. return Dest;
  123. }
  124. static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
  125. Type *Ty) {
  126. GenericValue Dest;
  127. switch (Ty->getTypeID()) {
  128. IMPLEMENT_INTEGER_ICMP(ne,Ty);
  129. IMPLEMENT_POINTER_ICMP(!=);
  130. default:
  131. dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
  132. llvm_unreachable(0);
  133. }
  134. return Dest;
  135. }
  136. static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
  137. Type *Ty) {
  138. GenericValue Dest;
  139. switch (Ty->getTypeID()) {
  140. IMPLEMENT_INTEGER_ICMP(ult,Ty);
  141. IMPLEMENT_POINTER_ICMP(<);
  142. default:
  143. dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
  144. llvm_unreachable(0);
  145. }
  146. return Dest;
  147. }
  148. static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
  149. Type *Ty) {
  150. GenericValue Dest;
  151. switch (Ty->getTypeID()) {
  152. IMPLEMENT_INTEGER_ICMP(slt,Ty);
  153. IMPLEMENT_POINTER_ICMP(<);
  154. default:
  155. dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
  156. llvm_unreachable(0);
  157. }
  158. return Dest;
  159. }
  160. static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
  161. Type *Ty) {
  162. GenericValue Dest;
  163. switch (Ty->getTypeID()) {
  164. IMPLEMENT_INTEGER_ICMP(ugt,Ty);
  165. IMPLEMENT_POINTER_ICMP(>);
  166. default:
  167. dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
  168. llvm_unreachable(0);
  169. }
  170. return Dest;
  171. }
  172. static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
  173. Type *Ty) {
  174. GenericValue Dest;
  175. switch (Ty->getTypeID()) {
  176. IMPLEMENT_INTEGER_ICMP(sgt,Ty);
  177. IMPLEMENT_POINTER_ICMP(>);
  178. default:
  179. dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
  180. llvm_unreachable(0);
  181. }
  182. return Dest;
  183. }
  184. static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
  185. Type *Ty) {
  186. GenericValue Dest;
  187. switch (Ty->getTypeID()) {
  188. IMPLEMENT_INTEGER_ICMP(ule,Ty);
  189. IMPLEMENT_POINTER_ICMP(<=);
  190. default:
  191. dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
  192. llvm_unreachable(0);
  193. }
  194. return Dest;
  195. }
  196. static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
  197. Type *Ty) {
  198. GenericValue Dest;
  199. switch (Ty->getTypeID()) {
  200. IMPLEMENT_INTEGER_ICMP(sle,Ty);
  201. IMPLEMENT_POINTER_ICMP(<=);
  202. default:
  203. dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
  204. llvm_unreachable(0);
  205. }
  206. return Dest;
  207. }
  208. static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
  209. Type *Ty) {
  210. GenericValue Dest;
  211. switch (Ty->getTypeID()) {
  212. IMPLEMENT_INTEGER_ICMP(uge,Ty);
  213. IMPLEMENT_POINTER_ICMP(>=);
  214. default:
  215. dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
  216. llvm_unreachable(0);
  217. }
  218. return Dest;
  219. }
  220. static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
  221. Type *Ty) {
  222. GenericValue Dest;
  223. switch (Ty->getTypeID()) {
  224. IMPLEMENT_INTEGER_ICMP(sge,Ty);
  225. IMPLEMENT_POINTER_ICMP(>=);
  226. default:
  227. dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
  228. llvm_unreachable(0);
  229. }
  230. return Dest;
  231. }
  232. void Interpreter::visitICmpInst(ICmpInst &I) {
  233. ExecutionContext &SF = ECStack.back();
  234. Type *Ty = I.getOperand(0)->getType();
  235. GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
  236. GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
  237. GenericValue R; // Result
  238. switch (I.getPredicate()) {
  239. case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break;
  240. case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break;
  241. case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
  242. case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
  243. case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
  244. case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
  245. case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
  246. case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
  247. case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
  248. case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
  249. default:
  250. dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I;
  251. llvm_unreachable(0);
  252. }
  253. SetValue(&I, R, SF);
  254. }
  255. #define IMPLEMENT_FCMP(OP, TY) \
  256. case Type::TY##TyID: \
  257. Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
  258. break
  259. static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
  260. Type *Ty) {
  261. GenericValue Dest;
  262. switch (Ty->getTypeID()) {
  263. IMPLEMENT_FCMP(==, Float);
  264. IMPLEMENT_FCMP(==, Double);
  265. default:
  266. dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
  267. llvm_unreachable(0);
  268. }
  269. return Dest;
  270. }
  271. static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
  272. Type *Ty) {
  273. GenericValue Dest;
  274. switch (Ty->getTypeID()) {
  275. IMPLEMENT_FCMP(!=, Float);
  276. IMPLEMENT_FCMP(!=, Double);
  277. default:
  278. dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
  279. llvm_unreachable(0);
  280. }
  281. return Dest;
  282. }
  283. static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
  284. Type *Ty) {
  285. GenericValue Dest;
  286. switch (Ty->getTypeID()) {
  287. IMPLEMENT_FCMP(<=, Float);
  288. IMPLEMENT_FCMP(<=, Double);
  289. default:
  290. dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
  291. llvm_unreachable(0);
  292. }
  293. return Dest;
  294. }
  295. static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
  296. Type *Ty) {
  297. GenericValue Dest;
  298. switch (Ty->getTypeID()) {
  299. IMPLEMENT_FCMP(>=, Float);
  300. IMPLEMENT_FCMP(>=, Double);
  301. default:
  302. dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
  303. llvm_unreachable(0);
  304. }
  305. return Dest;
  306. }
  307. static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
  308. Type *Ty) {
  309. GenericValue Dest;
  310. switch (Ty->getTypeID()) {
  311. IMPLEMENT_FCMP(<, Float);
  312. IMPLEMENT_FCMP(<, Double);
  313. default:
  314. dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
  315. llvm_unreachable(0);
  316. }
  317. return Dest;
  318. }
  319. static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
  320. Type *Ty) {
  321. GenericValue Dest;
  322. switch (Ty->getTypeID()) {
  323. IMPLEMENT_FCMP(>, Float);
  324. IMPLEMENT_FCMP(>, Double);
  325. default:
  326. dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
  327. llvm_unreachable(0);
  328. }
  329. return Dest;
  330. }
  331. #define IMPLEMENT_UNORDERED(TY, X,Y) \
  332. if (TY->isFloatTy()) { \
  333. if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \
  334. Dest.IntVal = APInt(1,true); \
  335. return Dest; \
  336. } \
  337. } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
  338. Dest.IntVal = APInt(1,true); \
  339. return Dest; \
  340. }
  341. static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
  342. Type *Ty) {
  343. GenericValue Dest;
  344. IMPLEMENT_UNORDERED(Ty, Src1, Src2)
  345. return executeFCMP_OEQ(Src1, Src2, Ty);
  346. }
  347. static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
  348. Type *Ty) {
  349. GenericValue Dest;
  350. IMPLEMENT_UNORDERED(Ty, Src1, Src2)
  351. return executeFCMP_ONE(Src1, Src2, Ty);
  352. }
  353. static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
  354. Type *Ty) {
  355. GenericValue Dest;
  356. IMPLEMENT_UNORDERED(Ty, Src1, Src2)
  357. return executeFCMP_OLE(Src1, Src2, Ty);
  358. }
  359. static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
  360. Type *Ty) {
  361. GenericValue Dest;
  362. IMPLEMENT_UNORDERED(Ty, Src1, Src2)
  363. return executeFCMP_OGE(Src1, Src2, Ty);
  364. }
  365. static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
  366. Type *Ty) {
  367. GenericValue Dest;
  368. IMPLEMENT_UNORDERED(Ty, Src1, Src2)
  369. return executeFCMP_OLT(Src1, Src2, Ty);
  370. }
  371. static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
  372. Type *Ty) {
  373. GenericValue Dest;
  374. IMPLEMENT_UNORDERED(Ty, Src1, Src2)
  375. return executeFCMP_OGT(Src1, Src2, Ty);
  376. }
  377. static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
  378. Type *Ty) {
  379. GenericValue Dest;
  380. if (Ty->isFloatTy())
  381. Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal &&
  382. Src2.FloatVal == Src2.FloatVal));
  383. else
  384. Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal &&
  385. Src2.DoubleVal == Src2.DoubleVal));
  386. return Dest;
  387. }
  388. static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
  389. Type *Ty) {
  390. GenericValue Dest;
  391. if (Ty->isFloatTy())
  392. Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal ||
  393. Src2.FloatVal != Src2.FloatVal));
  394. else
  395. Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal ||
  396. Src2.DoubleVal != Src2.DoubleVal));
  397. return Dest;
  398. }
  399. void Interpreter::visitFCmpInst(FCmpInst &I) {
  400. ExecutionContext &SF = ECStack.back();
  401. Type *Ty = I.getOperand(0)->getType();
  402. GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
  403. GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
  404. GenericValue R; // Result
  405. switch (I.getPredicate()) {
  406. case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
  407. case FCmpInst::FCMP_TRUE: R.IntVal = APInt(1,true); break;
  408. case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break;
  409. case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break;
  410. case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break;
  411. case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break;
  412. case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break;
  413. case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break;
  414. case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break;
  415. case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break;
  416. case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break;
  417. case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break;
  418. case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break;
  419. case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break;
  420. case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break;
  421. case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break;
  422. default:
  423. dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I;
  424. llvm_unreachable(0);
  425. }
  426. SetValue(&I, R, SF);
  427. }
  428. static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1,
  429. GenericValue Src2, Type *Ty) {
  430. GenericValue Result;
  431. switch (predicate) {
  432. case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty);
  433. case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty);
  434. case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty);
  435. case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty);
  436. case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty);
  437. case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty);
  438. case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty);
  439. case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty);
  440. case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty);
  441. case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty);
  442. case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty);
  443. case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty);
  444. case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty);
  445. case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty);
  446. case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty);
  447. case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty);
  448. case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty);
  449. case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty);
  450. case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty);
  451. case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty);
  452. case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty);
  453. case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty);
  454. case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty);
  455. case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty);
  456. case FCmpInst::FCMP_FALSE: {
  457. GenericValue Result;
  458. Result.IntVal = APInt(1, false);
  459. return Result;
  460. }
  461. case FCmpInst::FCMP_TRUE: {
  462. GenericValue Result;
  463. Result.IntVal = APInt(1, true);
  464. return Result;
  465. }
  466. default:
  467. dbgs() << "Unhandled Cmp predicate\n";
  468. llvm_unreachable(0);
  469. }
  470. }
  471. void Interpreter::visitBinaryOperator(BinaryOperator &I) {
  472. ExecutionContext &SF = ECStack.back();
  473. Type *Ty = I.getOperand(0)->getType();
  474. GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
  475. GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
  476. GenericValue R; // Result
  477. switch (I.getOpcode()) {
  478. case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break;
  479. case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break;
  480. case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break;
  481. case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break;
  482. case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break;
  483. case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break;
  484. case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break;
  485. case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break;
  486. case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
  487. case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
  488. case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
  489. case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
  490. case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break;
  491. case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break;
  492. case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
  493. default:
  494. dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
  495. llvm_unreachable(0);
  496. }
  497. SetValue(&I, R, SF);
  498. }
  499. static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
  500. GenericValue Src3) {
  501. return Src1.IntVal == 0 ? Src3 : Src2;
  502. }
  503. void Interpreter::visitSelectInst(SelectInst &I) {
  504. ExecutionContext &SF = ECStack.back();
  505. GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
  506. GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
  507. GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
  508. GenericValue R = executeSelectInst(Src1, Src2, Src3);
  509. SetValue(&I, R, SF);
  510. }
  511. //===----------------------------------------------------------------------===//
  512. // Terminator Instruction Implementations
  513. //===----------------------------------------------------------------------===//
  514. void Interpreter::exitCalled(GenericValue GV) {
  515. // runAtExitHandlers() assumes there are no stack frames, but
  516. // if exit() was called, then it had a stack frame. Blow away
  517. // the stack before interpreting atexit handlers.
  518. ECStack.clear();
  519. runAtExitHandlers();
  520. exit(GV.IntVal.zextOrTrunc(32).getZExtValue());
  521. }
  522. /// Pop the last stack frame off of ECStack and then copy the result
  523. /// back into the result variable if we are not returning void. The
  524. /// result variable may be the ExitValue, or the Value of the calling
  525. /// CallInst if there was a previous stack frame. This method may
  526. /// invalidate any ECStack iterators you have. This method also takes
  527. /// care of switching to the normal destination BB, if we are returning
  528. /// from an invoke.
  529. ///
  530. void Interpreter::popStackAndReturnValueToCaller(Type *RetTy,
  531. GenericValue Result) {
  532. // Pop the current stack frame.
  533. ECStack.pop_back();
  534. if (ECStack.empty()) { // Finished main. Put result into exit code...
  535. if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type?
  536. ExitValue = Result; // Capture the exit value of the program
  537. } else {
  538. memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
  539. }
  540. } else {
  541. // If we have a previous stack frame, and we have a previous call,
  542. // fill in the return value...
  543. ExecutionContext &CallingSF = ECStack.back();
  544. if (Instruction *I = CallingSF.Caller.getInstruction()) {
  545. // Save result...
  546. if (!CallingSF.Caller.getType()->isVoidTy())
  547. SetValue(I, Result, CallingSF);
  548. if (InvokeInst *II = dyn_cast<InvokeInst> (I))
  549. SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
  550. CallingSF.Caller = CallSite(); // We returned from the call...
  551. }
  552. }
  553. }
  554. void Interpreter::visitReturnInst(ReturnInst &I) {
  555. ExecutionContext &SF = ECStack.back();
  556. Type *RetTy = Type::getVoidTy(I.getContext());
  557. GenericValue Result;
  558. // Save away the return value... (if we are not 'ret void')
  559. if (I.getNumOperands()) {
  560. RetTy = I.getReturnValue()->getType();
  561. Result = getOperandValue(I.getReturnValue(), SF);
  562. }
  563. popStackAndReturnValueToCaller(RetTy, Result);
  564. }
  565. void Interpreter::visitUnreachableInst(UnreachableInst &I) {
  566. report_fatal_error("Program executed an 'unreachable' instruction!");
  567. }
  568. void Interpreter::visitBranchInst(BranchInst &I) {
  569. ExecutionContext &SF = ECStack.back();
  570. BasicBlock *Dest;
  571. Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
  572. if (!I.isUnconditional()) {
  573. Value *Cond = I.getCondition();
  574. if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
  575. Dest = I.getSuccessor(1);
  576. }
  577. SwitchToNewBasicBlock(Dest, SF);
  578. }
  579. void Interpreter::visitSwitchInst(SwitchInst &I) {
  580. ExecutionContext &SF = ECStack.back();
  581. Value* Cond = I.getCondition();
  582. Type *ElTy = Cond->getType();
  583. GenericValue CondVal = getOperandValue(Cond, SF);
  584. // Check to see if any of the cases match...
  585. BasicBlock *Dest = 0;
  586. unsigned NumCases = I.getNumCases();
  587. // Skip the first item since that's the default case.
  588. for (unsigned i = 0; i < NumCases; ++i) {
  589. GenericValue CaseVal = getOperandValue(I.getCaseValue(i), SF);
  590. if (executeICMP_EQ(CondVal, CaseVal, ElTy).IntVal != 0) {
  591. Dest = cast<BasicBlock>(I.getCaseSuccessor(i));
  592. break;
  593. }
  594. }
  595. if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
  596. SwitchToNewBasicBlock(Dest, SF);
  597. }
  598. void Interpreter::visitIndirectBrInst(IndirectBrInst &I) {
  599. ExecutionContext &SF = ECStack.back();
  600. void *Dest = GVTOP(getOperandValue(I.getAddress(), SF));
  601. SwitchToNewBasicBlock((BasicBlock*)Dest, SF);
  602. }
  603. // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
  604. // This function handles the actual updating of block and instruction iterators
  605. // as well as execution of all of the PHI nodes in the destination block.
  606. //
  607. // This method does this because all of the PHI nodes must be executed
  608. // atomically, reading their inputs before any of the results are updated. Not
  609. // doing this can cause problems if the PHI nodes depend on other PHI nodes for
  610. // their inputs. If the input PHI node is updated before it is read, incorrect
  611. // results can happen. Thus we use a two phase approach.
  612. //
  613. void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
  614. BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
  615. SF.CurBB = Dest; // Update CurBB to branch destination
  616. SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
  617. if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
  618. // Loop over all of the PHI nodes in the current block, reading their inputs.
  619. std::vector<GenericValue> ResultValues;
  620. for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
  621. // Search for the value corresponding to this previous bb...
  622. int i = PN->getBasicBlockIndex(PrevBB);
  623. assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
  624. Value *IncomingValue = PN->getIncomingValue(i);
  625. // Save the incoming value for this PHI node...
  626. ResultValues.push_back(getOperandValue(IncomingValue, SF));
  627. }
  628. // Now loop over all of the PHI nodes setting their values...
  629. SF.CurInst = SF.CurBB->begin();
  630. for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
  631. PHINode *PN = cast<PHINode>(SF.CurInst);
  632. SetValue(PN, ResultValues[i], SF);
  633. }
  634. }
  635. //===----------------------------------------------------------------------===//
  636. // Memory Instruction Implementations
  637. //===----------------------------------------------------------------------===//
  638. void Interpreter::visitAllocaInst(AllocaInst &I) {
  639. ExecutionContext &SF = ECStack.back();
  640. Type *Ty = I.getType()->getElementType(); // Type to be allocated
  641. // Get the number of elements being allocated by the array...
  642. unsigned NumElements =
  643. getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
  644. unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
  645. // Avoid malloc-ing zero bytes, use max()...
  646. unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
  647. // Allocate enough memory to hold the type...
  648. void *Memory = malloc(MemToAlloc);
  649. DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
  650. << NumElements << " (Total: " << MemToAlloc << ") at "
  651. << uintptr_t(Memory) << '\n');
  652. GenericValue Result = PTOGV(Memory);
  653. assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
  654. SetValue(&I, Result, SF);
  655. if (I.getOpcode() == Instruction::Alloca)
  656. ECStack.back().Allocas.add(Memory);
  657. }
  658. // getElementOffset - The workhorse for getelementptr.
  659. //
  660. GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
  661. gep_type_iterator E,
  662. ExecutionContext &SF) {
  663. assert(Ptr->getType()->isPointerTy() &&
  664. "Cannot getElementOffset of a nonpointer type!");
  665. uint64_t Total = 0;
  666. for (; I != E; ++I) {
  667. if (StructType *STy = dyn_cast<StructType>(*I)) {
  668. const StructLayout *SLO = TD.getStructLayout(STy);
  669. const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
  670. unsigned Index = unsigned(CPU->getZExtValue());
  671. Total += SLO->getElementOffset(Index);
  672. } else {
  673. SequentialType *ST = cast<SequentialType>(*I);
  674. // Get the index number for the array... which must be long type...
  675. GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
  676. int64_t Idx;
  677. unsigned BitWidth =
  678. cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
  679. if (BitWidth == 32)
  680. Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
  681. else {
  682. assert(BitWidth == 64 && "Invalid index type for getelementptr");
  683. Idx = (int64_t)IdxGV.IntVal.getZExtValue();
  684. }
  685. Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
  686. }
  687. }
  688. GenericValue Result;
  689. Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
  690. DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");
  691. return Result;
  692. }
  693. void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
  694. ExecutionContext &SF = ECStack.back();
  695. SetValue(&I, executeGEPOperation(I.getPointerOperand(),
  696. gep_type_begin(I), gep_type_end(I), SF), SF);
  697. }
  698. void Interpreter::visitLoadInst(LoadInst &I) {
  699. ExecutionContext &SF = ECStack.back();
  700. GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
  701. GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
  702. GenericValue Result;
  703. LoadValueFromMemory(Result, Ptr, I.getType());
  704. SetValue(&I, Result, SF);
  705. if (I.isVolatile() && PrintVolatile)
  706. dbgs() << "Volatile load " << I;
  707. }
  708. void Interpreter::visitStoreInst(StoreInst &I) {
  709. ExecutionContext &SF = ECStack.back();
  710. GenericValue Val = getOperandValue(I.getOperand(0), SF);
  711. GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
  712. StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
  713. I.getOperand(0)->getType());
  714. if (I.isVolatile() && PrintVolatile)
  715. dbgs() << "Volatile store: " << I;
  716. }
  717. //===----------------------------------------------------------------------===//
  718. // Miscellaneous Instruction Implementations
  719. //===----------------------------------------------------------------------===//
  720. void Interpreter::visitCallSite(CallSite CS) {
  721. ExecutionContext &SF = ECStack.back();
  722. // Check to see if this is an intrinsic function call...
  723. Function *F = CS.getCalledFunction();
  724. if (F && F->isDeclaration())
  725. switch (F->getIntrinsicID()) {
  726. case Intrinsic::not_intrinsic:
  727. break;
  728. case Intrinsic::vastart: { // va_start
  729. GenericValue ArgIndex;
  730. ArgIndex.UIntPairVal.first = ECStack.size() - 1;
  731. ArgIndex.UIntPairVal.second = 0;
  732. SetValue(CS.getInstruction(), ArgIndex, SF);
  733. return;
  734. }
  735. case Intrinsic::vaend: // va_end is a noop for the interpreter
  736. return;
  737. case Intrinsic::vacopy: // va_copy: dest = src
  738. SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
  739. return;
  740. default:
  741. // If it is an unknown intrinsic function, use the intrinsic lowering
  742. // class to transform it into hopefully tasty LLVM code.
  743. //
  744. BasicBlock::iterator me(CS.getInstruction());
  745. BasicBlock *Parent = CS.getInstruction()->getParent();
  746. bool atBegin(Parent->begin() == me);
  747. if (!atBegin)
  748. --me;
  749. IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
  750. // Restore the CurInst pointer to the first instruction newly inserted, if
  751. // any.
  752. if (atBegin) {
  753. SF.CurInst = Parent->begin();
  754. } else {
  755. SF.CurInst = me;
  756. ++SF.CurInst;
  757. }
  758. return;
  759. }
  760. SF.Caller = CS;
  761. std::vector<GenericValue> ArgVals;
  762. const unsigned NumArgs = SF.Caller.arg_size();
  763. ArgVals.reserve(NumArgs);
  764. uint16_t pNum = 1;
  765. for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
  766. e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
  767. Value *V = *i;
  768. ArgVals.push_back(getOperandValue(V, SF));
  769. }
  770. // To handle indirect calls, we must get the pointer value from the argument
  771. // and treat it as a function pointer.
  772. GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
  773. callFunction((Function*)GVTOP(SRC), ArgVals);
  774. }
  775. void Interpreter::visitShl(BinaryOperator &I) {
  776. ExecutionContext &SF = ECStack.back();
  777. GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
  778. GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
  779. GenericValue Dest;
  780. if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
  781. Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
  782. else
  783. Dest.IntVal = Src1.IntVal;
  784. SetValue(&I, Dest, SF);
  785. }
  786. void Interpreter::visitLShr(BinaryOperator &I) {
  787. ExecutionContext &SF = ECStack.back();
  788. GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
  789. GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
  790. GenericValue Dest;
  791. if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
  792. Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
  793. else
  794. Dest.IntVal = Src1.IntVal;
  795. SetValue(&I, Dest, SF);
  796. }
  797. void Interpreter::visitAShr(BinaryOperator &I) {
  798. ExecutionContext &SF = ECStack.back();
  799. GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
  800. GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
  801. GenericValue Dest;
  802. if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
  803. Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
  804. else
  805. Dest.IntVal = Src1.IntVal;
  806. SetValue(&I, Dest, SF);
  807. }
  808. GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy,
  809. ExecutionContext &SF) {
  810. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  811. IntegerType *DITy = cast<IntegerType>(DstTy);
  812. unsigned DBitWidth = DITy->getBitWidth();
  813. Dest.IntVal = Src.IntVal.trunc(DBitWidth);
  814. return Dest;
  815. }
  816. GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy,
  817. ExecutionContext &SF) {
  818. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  819. IntegerType *DITy = cast<IntegerType>(DstTy);
  820. unsigned DBitWidth = DITy->getBitWidth();
  821. Dest.IntVal = Src.IntVal.sext(DBitWidth);
  822. return Dest;
  823. }
  824. GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy,
  825. ExecutionContext &SF) {
  826. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  827. IntegerType *DITy = cast<IntegerType>(DstTy);
  828. unsigned DBitWidth = DITy->getBitWidth();
  829. Dest.IntVal = Src.IntVal.zext(DBitWidth);
  830. return Dest;
  831. }
  832. GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy,
  833. ExecutionContext &SF) {
  834. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  835. assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
  836. "Invalid FPTrunc instruction");
  837. Dest.FloatVal = (float) Src.DoubleVal;
  838. return Dest;
  839. }
  840. GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy,
  841. ExecutionContext &SF) {
  842. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  843. assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
  844. "Invalid FPTrunc instruction");
  845. Dest.DoubleVal = (double) Src.FloatVal;
  846. return Dest;
  847. }
  848. GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy,
  849. ExecutionContext &SF) {
  850. Type *SrcTy = SrcVal->getType();
  851. uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
  852. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  853. assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");
  854. if (SrcTy->getTypeID() == Type::FloatTyID)
  855. Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
  856. else
  857. Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
  858. return Dest;
  859. }
  860. GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy,
  861. ExecutionContext &SF) {
  862. Type *SrcTy = SrcVal->getType();
  863. uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
  864. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  865. assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");
  866. if (SrcTy->getTypeID() == Type::FloatTyID)
  867. Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
  868. else
  869. Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
  870. return Dest;
  871. }
  872. GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy,
  873. ExecutionContext &SF) {
  874. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  875. assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");
  876. if (DstTy->getTypeID() == Type::FloatTyID)
  877. Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
  878. else
  879. Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
  880. return Dest;
  881. }
  882. GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy,
  883. ExecutionContext &SF) {
  884. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  885. assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");
  886. if (DstTy->getTypeID() == Type::FloatTyID)
  887. Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
  888. else
  889. Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
  890. return Dest;
  891. }
  892. GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy,
  893. ExecutionContext &SF) {
  894. uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
  895. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  896. assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");
  897. Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
  898. return Dest;
  899. }
  900. GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy,
  901. ExecutionContext &SF) {
  902. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  903. assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");
  904. uint32_t PtrSize = TD.getPointerSizeInBits();
  905. if (PtrSize != Src.IntVal.getBitWidth())
  906. Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
  907. Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
  908. return Dest;
  909. }
  910. GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy,
  911. ExecutionContext &SF) {
  912. Type *SrcTy = SrcVal->getType();
  913. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  914. if (DstTy->isPointerTy()) {
  915. assert(SrcTy->isPointerTy() && "Invalid BitCast");
  916. Dest.PointerVal = Src.PointerVal;
  917. } else if (DstTy->isIntegerTy()) {
  918. if (SrcTy->isFloatTy()) {
  919. Dest.IntVal = APInt::floatToBits(Src.FloatVal);
  920. } else if (SrcTy->isDoubleTy()) {
  921. Dest.IntVal = APInt::doubleToBits(Src.DoubleVal);
  922. } else if (SrcTy->isIntegerTy()) {
  923. Dest.IntVal = Src.IntVal;
  924. } else
  925. llvm_unreachable("Invalid BitCast");
  926. } else if (DstTy->isFloatTy()) {
  927. if (SrcTy->isIntegerTy())
  928. Dest.FloatVal = Src.IntVal.bitsToFloat();
  929. else
  930. Dest.FloatVal = Src.FloatVal;
  931. } else if (DstTy->isDoubleTy()) {
  932. if (SrcTy->isIntegerTy())
  933. Dest.DoubleVal = Src.IntVal.bitsToDouble();
  934. else
  935. Dest.DoubleVal = Src.DoubleVal;
  936. } else
  937. llvm_unreachable("Invalid Bitcast");
  938. return Dest;
  939. }
  940. void Interpreter::visitTruncInst(TruncInst &I) {
  941. ExecutionContext &SF = ECStack.back();
  942. SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
  943. }
  944. void Interpreter::visitSExtInst(SExtInst &I) {
  945. ExecutionContext &SF = ECStack.back();
  946. SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
  947. }
  948. void Interpreter::visitZExtInst(ZExtInst &I) {
  949. ExecutionContext &SF = ECStack.back();
  950. SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
  951. }
  952. void Interpreter::visitFPTruncInst(FPTruncInst &I) {
  953. ExecutionContext &SF = ECStack.back();
  954. SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
  955. }
  956. void Interpreter::visitFPExtInst(FPExtInst &I) {
  957. ExecutionContext &SF = ECStack.back();
  958. SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
  959. }
  960. void Interpreter::visitUIToFPInst(UIToFPInst &I) {
  961. ExecutionContext &SF = ECStack.back();
  962. SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
  963. }
  964. void Interpreter::visitSIToFPInst(SIToFPInst &I) {
  965. ExecutionContext &SF = ECStack.back();
  966. SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
  967. }
  968. void Interpreter::visitFPToUIInst(FPToUIInst &I) {
  969. ExecutionContext &SF = ECStack.back();
  970. SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
  971. }
  972. void Interpreter::visitFPToSIInst(FPToSIInst &I) {
  973. ExecutionContext &SF = ECStack.back();
  974. SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
  975. }
  976. void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
  977. ExecutionContext &SF = ECStack.back();
  978. SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
  979. }
  980. void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
  981. ExecutionContext &SF = ECStack.back();
  982. SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
  983. }
  984. void Interpreter::visitBitCastInst(BitCastInst &I) {
  985. ExecutionContext &SF = ECStack.back();
  986. SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
  987. }
  988. #define IMPLEMENT_VAARG(TY) \
  989. case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
  990. void Interpreter::visitVAArgInst(VAArgInst &I) {
  991. ExecutionContext &SF = ECStack.back();
  992. // Get the incoming valist parameter. LLI treats the valist as a
  993. // (ec-stack-depth var-arg-index) pair.
  994. GenericValue VAList = getOperandValue(I.getOperand(0), SF);
  995. GenericValue Dest;
  996. GenericValue Src = ECStack[VAList.UIntPairVal.first]
  997. .VarArgs[VAList.UIntPairVal.second];
  998. Type *Ty = I.getType();
  999. switch (Ty->getTypeID()) {
  1000. case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
  1001. IMPLEMENT_VAARG(Pointer);
  1002. IMPLEMENT_VAARG(Float);
  1003. IMPLEMENT_VAARG(Double);
  1004. default:
  1005. dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
  1006. llvm_unreachable(0);
  1007. }
  1008. // Set the Value of this Instruction.
  1009. SetValue(&I, Dest, SF);
  1010. // Move the pointer to the next vararg.
  1011. ++VAList.UIntPairVal.second;
  1012. }
  1013. GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
  1014. ExecutionContext &SF) {
  1015. switch (CE->getOpcode()) {
  1016. case Instruction::Trunc:
  1017. return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
  1018. case Instruction::ZExt:
  1019. return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
  1020. case Instruction::SExt:
  1021. return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
  1022. case Instruction::FPTrunc:
  1023. return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
  1024. case Instruction::FPExt:
  1025. return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
  1026. case Instruction::UIToFP:
  1027. return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
  1028. case Instruction::SIToFP:
  1029. return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
  1030. case Instruction::FPToUI:
  1031. return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
  1032. case Instruction::FPToSI:
  1033. return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
  1034. case Instruction::PtrToInt:
  1035. return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
  1036. case Instruction::IntToPtr:
  1037. return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
  1038. case Instruction::BitCast:
  1039. return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
  1040. case Instruction::GetElementPtr:
  1041. return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
  1042. gep_type_end(CE), SF);
  1043. case Instruction::FCmp:
  1044. case Instruction::ICmp:
  1045. return executeCmpInst(CE->getPredicate(),
  1046. getOperandValue(CE->getOperand(0), SF),
  1047. getOperandValue(CE->getOperand(1), SF),
  1048. CE->getOperand(0)->getType());
  1049. case Instruction::Select:
  1050. return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
  1051. getOperandValue(CE->getOperand(1), SF),
  1052. getOperandValue(CE->getOperand(2), SF));
  1053. default :
  1054. break;
  1055. }
  1056. // The cases below here require a GenericValue parameter for the result
  1057. // so we initialize one, compute it and then return it.
  1058. GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
  1059. GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
  1060. GenericValue Dest;
  1061. Type * Ty = CE->getOperand(0)->getType();
  1062. switch (CE->getOpcode()) {
  1063. case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
  1064. case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
  1065. case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
  1066. case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
  1067. case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
  1068. case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
  1069. case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
  1070. case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
  1071. case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
  1072. case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
  1073. case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
  1074. case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
  1075. case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
  1076. case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
  1077. case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
  1078. case Instruction::Shl:
  1079. Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
  1080. break;
  1081. case Instruction::LShr:
  1082. Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
  1083. break;
  1084. case Instruction::AShr:
  1085. Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
  1086. break;
  1087. default:
  1088. dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";
  1089. llvm_unreachable(0);
  1090. }
  1091. return Dest;
  1092. }
  1093. GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
  1094. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
  1095. return getConstantExprValue(CE, SF);
  1096. } else if (Constant *CPV = dyn_cast<Constant>(V)) {
  1097. return getConstantValue(CPV);
  1098. } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
  1099. return PTOGV(getPointerToGlobal(GV));
  1100. } else {
  1101. return SF.Values[V];
  1102. }
  1103. }
  1104. //===----------------------------------------------------------------------===//
  1105. // Dispatch and Execution Code
  1106. //===----------------------------------------------------------------------===//
  1107. //===----------------------------------------------------------------------===//
  1108. // callFunction - Execute the specified function...
  1109. //
  1110. void Interpreter::callFunction(Function *F,
  1111. const std::vector<GenericValue> &ArgVals) {
  1112. assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
  1113. ECStack.back().Caller.arg_size() == ArgVals.size()) &&
  1114. "Incorrect number of arguments passed into function call!");
  1115. // Make a new stack frame... and fill it in.
  1116. ECStack.push_back(ExecutionContext());
  1117. ExecutionContext &StackFrame = ECStack.back();
  1118. StackFrame.CurFunction = F;
  1119. // Special handling for external functions.
  1120. if (F->isDeclaration()) {
  1121. GenericValue Result = callExternalFunction (F, ArgVals);
  1122. // Simulate a 'ret' instruction of the appropriate type.
  1123. popStackAndReturnValueToCaller (F->getReturnType (), Result);
  1124. return;
  1125. }
  1126. // Get pointers to first LLVM BB & Instruction in function.
  1127. StackFrame.CurBB = F->begin();
  1128. StackFrame.CurInst = StackFrame.CurBB->begin();
  1129. // Run through the function arguments and initialize their values...
  1130. assert((ArgVals.size() == F->arg_size() ||
  1131. (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
  1132. "Invalid number of values passed to function invocation!");
  1133. // Handle non-varargs arguments...
  1134. unsigned i = 0;
  1135. for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
  1136. AI != E; ++AI, ++i)
  1137. SetValue(AI, ArgVals[i], StackFrame);
  1138. // Handle varargs arguments...
  1139. StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
  1140. }
  1141. void Interpreter::run() {
  1142. while (!ECStack.empty()) {
  1143. // Interpret a single instruction & increment the "PC".
  1144. ExecutionContext &SF = ECStack.back(); // Current stack frame
  1145. Instruction &I = *SF.CurInst++; // Increment before execute
  1146. // Track the number of dynamic instructions executed.
  1147. ++NumDynamicInsts;
  1148. DEBUG(dbgs() << "About to interpret: " << I);
  1149. visit(I); // Dispatch to one of the visit* methods...
  1150. #if 0
  1151. // This is not safe, as visiting the instruction could lower it and free I.
  1152. DEBUG(
  1153. if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
  1154. I.getType() != Type::VoidTy) {
  1155. dbgs() << " --> ";
  1156. const GenericValue &Val = SF.Values[&I];
  1157. switch (I.getType()->getTypeID()) {
  1158. default: llvm_unreachable("Invalid GenericValue Type");
  1159. case Type::VoidTyID: dbgs() << "void"; break;
  1160. case Type::FloatTyID: dbgs() << "float " << Val.FloatVal; break;
  1161. case Type::DoubleTyID: dbgs() << "double " << Val.DoubleVal; break;
  1162. case Type::PointerTyID: dbgs() << "void* " << intptr_t(Val.PointerVal);
  1163. break;
  1164. case Type::IntegerTyID:
  1165. dbgs() << "i" << Val.IntVal.getBitWidth() << " "
  1166. << Val.IntVal.toStringUnsigned(10)
  1167. << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
  1168. break;
  1169. }
  1170. });
  1171. #endif
  1172. }
  1173. }