Execution.cpp 50 KB

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