Execution.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  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();
  329. }
  330. return Dest;
  331. }
  332. #define IMPLEMENT_UNORDERED(TY, X,Y) \
  333. if (TY == Type::FloatTy) { \
  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::FloatTy)
  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::FloatTy)
  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();
  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();
  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();
  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. if (CallingSF.Caller.getType() != Type::VoidTy) // Save result...
  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. const Type *RetTy = Type::VoidTy;
  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::visitUnwindInst(UnwindInst &I) {
  566. // Unwind stack
  567. Instruction *Inst;
  568. do {
  569. ECStack.pop_back ();
  570. if (ECStack.empty ())
  571. llvm_report_error("Empty stack during unwind!");
  572. Inst = ECStack.back ().Caller.getInstruction ();
  573. } while (!(Inst && isa<InvokeInst> (Inst)));
  574. // Return from invoke
  575. ExecutionContext &InvokingSF = ECStack.back ();
  576. InvokingSF.Caller = CallSite ();
  577. // Go to exceptional destination BB of invoke instruction
  578. SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
  579. }
  580. void Interpreter::visitUnreachableInst(UnreachableInst &I) {
  581. llvm_report_error("ERROR: Program executed an 'unreachable' instruction!");
  582. }
  583. void Interpreter::visitBranchInst(BranchInst &I) {
  584. ExecutionContext &SF = ECStack.back();
  585. BasicBlock *Dest;
  586. Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
  587. if (!I.isUnconditional()) {
  588. Value *Cond = I.getCondition();
  589. if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
  590. Dest = I.getSuccessor(1);
  591. }
  592. SwitchToNewBasicBlock(Dest, SF);
  593. }
  594. void Interpreter::visitSwitchInst(SwitchInst &I) {
  595. ExecutionContext &SF = ECStack.back();
  596. GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
  597. const Type *ElTy = I.getOperand(0)->getType();
  598. // Check to see if any of the cases match...
  599. BasicBlock *Dest = 0;
  600. for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
  601. if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
  602. .IntVal != 0) {
  603. Dest = cast<BasicBlock>(I.getOperand(i+1));
  604. break;
  605. }
  606. if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
  607. SwitchToNewBasicBlock(Dest, SF);
  608. }
  609. // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
  610. // This function handles the actual updating of block and instruction iterators
  611. // as well as execution of all of the PHI nodes in the destination block.
  612. //
  613. // This method does this because all of the PHI nodes must be executed
  614. // atomically, reading their inputs before any of the results are updated. Not
  615. // doing this can cause problems if the PHI nodes depend on other PHI nodes for
  616. // their inputs. If the input PHI node is updated before it is read, incorrect
  617. // results can happen. Thus we use a two phase approach.
  618. //
  619. void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
  620. BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
  621. SF.CurBB = Dest; // Update CurBB to branch destination
  622. SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
  623. if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
  624. // Loop over all of the PHI nodes in the current block, reading their inputs.
  625. std::vector<GenericValue> ResultValues;
  626. for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
  627. // Search for the value corresponding to this previous bb...
  628. int i = PN->getBasicBlockIndex(PrevBB);
  629. assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
  630. Value *IncomingValue = PN->getIncomingValue(i);
  631. // Save the incoming value for this PHI node...
  632. ResultValues.push_back(getOperandValue(IncomingValue, SF));
  633. }
  634. // Now loop over all of the PHI nodes setting their values...
  635. SF.CurInst = SF.CurBB->begin();
  636. for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
  637. PHINode *PN = cast<PHINode>(SF.CurInst);
  638. SetValue(PN, ResultValues[i], SF);
  639. }
  640. }
  641. //===----------------------------------------------------------------------===//
  642. // Memory Instruction Implementations
  643. //===----------------------------------------------------------------------===//
  644. void Interpreter::visitAllocationInst(AllocationInst &I) {
  645. ExecutionContext &SF = ECStack.back();
  646. const Type *Ty = I.getType()->getElementType(); // Type to be allocated
  647. // Get the number of elements being allocated by the array...
  648. unsigned NumElements =
  649. getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
  650. unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
  651. // Avoid malloc-ing zero bytes, use max()...
  652. unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
  653. // Allocate enough memory to hold the type...
  654. void *Memory = malloc(MemToAlloc);
  655. DOUT << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x "
  656. << NumElements << " (Total: " << MemToAlloc << ") at "
  657. << uintptr_t(Memory) << '\n';
  658. GenericValue Result = PTOGV(Memory);
  659. assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
  660. SetValue(&I, Result, SF);
  661. if (I.getOpcode() == Instruction::Alloca)
  662. ECStack.back().Allocas.add(Memory);
  663. }
  664. void Interpreter::visitFreeInst(FreeInst &I) {
  665. ExecutionContext &SF = ECStack.back();
  666. assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
  667. GenericValue Value = getOperandValue(I.getOperand(0), SF);
  668. // TODO: Check to make sure memory is allocated
  669. free(GVTOP(Value)); // Free memory
  670. }
  671. // getElementOffset - The workhorse for getelementptr.
  672. //
  673. GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
  674. gep_type_iterator E,
  675. ExecutionContext &SF) {
  676. assert(isa<PointerType>(Ptr->getType()) &&
  677. "Cannot getElementOffset of a nonpointer type!");
  678. uint64_t Total = 0;
  679. for (; I != E; ++I) {
  680. if (const StructType *STy = dyn_cast<StructType>(*I)) {
  681. const StructLayout *SLO = TD.getStructLayout(STy);
  682. const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
  683. unsigned Index = unsigned(CPU->getZExtValue());
  684. Total += SLO->getElementOffset(Index);
  685. } else {
  686. const SequentialType *ST = cast<SequentialType>(*I);
  687. // Get the index number for the array... which must be long type...
  688. GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
  689. int64_t Idx;
  690. unsigned BitWidth =
  691. cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
  692. if (BitWidth == 32)
  693. Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
  694. else {
  695. assert(BitWidth == 64 && "Invalid index type for getelementptr");
  696. Idx = (int64_t)IdxGV.IntVal.getZExtValue();
  697. }
  698. Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
  699. }
  700. }
  701. GenericValue Result;
  702. Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
  703. DOUT << "GEP Index " << Total << " bytes.\n";
  704. return Result;
  705. }
  706. void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
  707. ExecutionContext &SF = ECStack.back();
  708. SetValue(&I, executeGEPOperation(I.getPointerOperand(),
  709. gep_type_begin(I), gep_type_end(I), SF), SF);
  710. }
  711. void Interpreter::visitLoadInst(LoadInst &I) {
  712. ExecutionContext &SF = ECStack.back();
  713. GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
  714. GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
  715. GenericValue Result;
  716. LoadValueFromMemory(Result, Ptr, I.getType());
  717. SetValue(&I, Result, SF);
  718. if (I.isVolatile() && PrintVolatile)
  719. cerr << "Volatile load " << I;
  720. }
  721. void Interpreter::visitStoreInst(StoreInst &I) {
  722. ExecutionContext &SF = ECStack.back();
  723. GenericValue Val = getOperandValue(I.getOperand(0), SF);
  724. GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
  725. StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
  726. I.getOperand(0)->getType());
  727. if (I.isVolatile() && PrintVolatile)
  728. cerr << "Volatile store: " << I;
  729. }
  730. //===----------------------------------------------------------------------===//
  731. // Miscellaneous Instruction Implementations
  732. //===----------------------------------------------------------------------===//
  733. void Interpreter::visitCallSite(CallSite CS) {
  734. ExecutionContext &SF = ECStack.back();
  735. // Check to see if this is an intrinsic function call...
  736. Function *F = CS.getCalledFunction();
  737. if (F && F->isDeclaration ())
  738. switch (F->getIntrinsicID()) {
  739. case Intrinsic::not_intrinsic:
  740. break;
  741. case Intrinsic::vastart: { // va_start
  742. GenericValue ArgIndex;
  743. ArgIndex.UIntPairVal.first = ECStack.size() - 1;
  744. ArgIndex.UIntPairVal.second = 0;
  745. SetValue(CS.getInstruction(), ArgIndex, SF);
  746. return;
  747. }
  748. case Intrinsic::vaend: // va_end is a noop for the interpreter
  749. return;
  750. case Intrinsic::vacopy: // va_copy: dest = src
  751. SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
  752. return;
  753. default:
  754. // If it is an unknown intrinsic function, use the intrinsic lowering
  755. // class to transform it into hopefully tasty LLVM code.
  756. //
  757. BasicBlock::iterator me(CS.getInstruction());
  758. BasicBlock *Parent = CS.getInstruction()->getParent();
  759. bool atBegin(Parent->begin() == me);
  760. if (!atBegin)
  761. --me;
  762. IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
  763. // Restore the CurInst pointer to the first instruction newly inserted, if
  764. // any.
  765. if (atBegin) {
  766. SF.CurInst = Parent->begin();
  767. } else {
  768. SF.CurInst = me;
  769. ++SF.CurInst;
  770. }
  771. return;
  772. }
  773. SF.Caller = CS;
  774. std::vector<GenericValue> ArgVals;
  775. const unsigned NumArgs = SF.Caller.arg_size();
  776. ArgVals.reserve(NumArgs);
  777. uint16_t pNum = 1;
  778. for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
  779. e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
  780. Value *V = *i;
  781. ArgVals.push_back(getOperandValue(V, SF));
  782. // Promote all integral types whose size is < sizeof(i32) into i32.
  783. // We do this by zero or sign extending the value as appropriate
  784. // according to the parameter attributes
  785. const Type *Ty = V->getType();
  786. if (Ty->isInteger() && (ArgVals.back().IntVal.getBitWidth() < 32)) {
  787. if (CS.paramHasAttr(pNum, Attribute::ZExt))
  788. ArgVals.back().IntVal = ArgVals.back().IntVal.zext(32);
  789. else if (CS.paramHasAttr(pNum, Attribute::SExt))
  790. ArgVals.back().IntVal = ArgVals.back().IntVal.sext(32);
  791. }
  792. }
  793. // To handle indirect calls, we must get the pointer value from the argument
  794. // and treat it as a function pointer.
  795. GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
  796. callFunction((Function*)GVTOP(SRC), ArgVals);
  797. }
  798. void Interpreter::visitShl(BinaryOperator &I) {
  799. ExecutionContext &SF = ECStack.back();
  800. GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
  801. GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
  802. GenericValue Dest;
  803. if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
  804. Dest.IntVal = Src1.IntVal.shl(Src2.IntVal.getZExtValue());
  805. else
  806. Dest.IntVal = Src1.IntVal;
  807. SetValue(&I, Dest, SF);
  808. }
  809. void Interpreter::visitLShr(BinaryOperator &I) {
  810. ExecutionContext &SF = ECStack.back();
  811. GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
  812. GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
  813. GenericValue Dest;
  814. if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
  815. Dest.IntVal = Src1.IntVal.lshr(Src2.IntVal.getZExtValue());
  816. else
  817. Dest.IntVal = Src1.IntVal;
  818. SetValue(&I, Dest, SF);
  819. }
  820. void Interpreter::visitAShr(BinaryOperator &I) {
  821. ExecutionContext &SF = ECStack.back();
  822. GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
  823. GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
  824. GenericValue Dest;
  825. if (Src2.IntVal.getZExtValue() < Src1.IntVal.getBitWidth())
  826. Dest.IntVal = Src1.IntVal.ashr(Src2.IntVal.getZExtValue());
  827. else
  828. Dest.IntVal = Src1.IntVal;
  829. SetValue(&I, Dest, SF);
  830. }
  831. GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy,
  832. ExecutionContext &SF) {
  833. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  834. const IntegerType *DITy = cast<IntegerType>(DstTy);
  835. unsigned DBitWidth = DITy->getBitWidth();
  836. Dest.IntVal = Src.IntVal.trunc(DBitWidth);
  837. return Dest;
  838. }
  839. GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy,
  840. ExecutionContext &SF) {
  841. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  842. const IntegerType *DITy = cast<IntegerType>(DstTy);
  843. unsigned DBitWidth = DITy->getBitWidth();
  844. Dest.IntVal = Src.IntVal.sext(DBitWidth);
  845. return Dest;
  846. }
  847. GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy,
  848. ExecutionContext &SF) {
  849. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  850. const IntegerType *DITy = cast<IntegerType>(DstTy);
  851. unsigned DBitWidth = DITy->getBitWidth();
  852. Dest.IntVal = Src.IntVal.zext(DBitWidth);
  853. return Dest;
  854. }
  855. GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy,
  856. ExecutionContext &SF) {
  857. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  858. assert(SrcVal->getType() == Type::DoubleTy && DstTy == Type::FloatTy &&
  859. "Invalid FPTrunc instruction");
  860. Dest.FloatVal = (float) Src.DoubleVal;
  861. return Dest;
  862. }
  863. GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy,
  864. ExecutionContext &SF) {
  865. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  866. assert(SrcVal->getType() == Type::FloatTy && DstTy == Type::DoubleTy &&
  867. "Invalid FPTrunc instruction");
  868. Dest.DoubleVal = (double) Src.FloatVal;
  869. return Dest;
  870. }
  871. GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy,
  872. ExecutionContext &SF) {
  873. const Type *SrcTy = SrcVal->getType();
  874. uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
  875. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  876. assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction");
  877. if (SrcTy->getTypeID() == Type::FloatTyID)
  878. Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
  879. else
  880. Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
  881. return Dest;
  882. }
  883. GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy,
  884. ExecutionContext &SF) {
  885. const Type *SrcTy = SrcVal->getType();
  886. uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
  887. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  888. assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction");
  889. if (SrcTy->getTypeID() == Type::FloatTyID)
  890. Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
  891. else
  892. Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
  893. return Dest;
  894. }
  895. GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy,
  896. ExecutionContext &SF) {
  897. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  898. assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction");
  899. if (DstTy->getTypeID() == Type::FloatTyID)
  900. Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
  901. else
  902. Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
  903. return Dest;
  904. }
  905. GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy,
  906. ExecutionContext &SF) {
  907. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  908. assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction");
  909. if (DstTy->getTypeID() == Type::FloatTyID)
  910. Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
  911. else
  912. Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
  913. return Dest;
  914. }
  915. GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy,
  916. ExecutionContext &SF) {
  917. uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
  918. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  919. assert(isa<PointerType>(SrcVal->getType()) && "Invalid PtrToInt instruction");
  920. Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
  921. return Dest;
  922. }
  923. GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy,
  924. ExecutionContext &SF) {
  925. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  926. assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction");
  927. uint32_t PtrSize = TD.getPointerSizeInBits();
  928. if (PtrSize != Src.IntVal.getBitWidth())
  929. Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
  930. Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
  931. return Dest;
  932. }
  933. GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy,
  934. ExecutionContext &SF) {
  935. const Type *SrcTy = SrcVal->getType();
  936. GenericValue Dest, Src = getOperandValue(SrcVal, SF);
  937. if (isa<PointerType>(DstTy)) {
  938. assert(isa<PointerType>(SrcTy) && "Invalid BitCast");
  939. Dest.PointerVal = Src.PointerVal;
  940. } else if (DstTy->isInteger()) {
  941. if (SrcTy == Type::FloatTy) {
  942. Dest.IntVal.zext(sizeof(Src.FloatVal) * CHAR_BIT);
  943. Dest.IntVal.floatToBits(Src.FloatVal);
  944. } else if (SrcTy == Type::DoubleTy) {
  945. Dest.IntVal.zext(sizeof(Src.DoubleVal) * CHAR_BIT);
  946. Dest.IntVal.doubleToBits(Src.DoubleVal);
  947. } else if (SrcTy->isInteger()) {
  948. Dest.IntVal = Src.IntVal;
  949. } else
  950. LLVM_UNREACHABLE("Invalid BitCast");
  951. } else if (DstTy == Type::FloatTy) {
  952. if (SrcTy->isInteger())
  953. Dest.FloatVal = Src.IntVal.bitsToFloat();
  954. else
  955. Dest.FloatVal = Src.FloatVal;
  956. } else if (DstTy == Type::DoubleTy) {
  957. if (SrcTy->isInteger())
  958. Dest.DoubleVal = Src.IntVal.bitsToDouble();
  959. else
  960. Dest.DoubleVal = Src.DoubleVal;
  961. } else
  962. LLVM_UNREACHABLE("Invalid Bitcast");
  963. return Dest;
  964. }
  965. void Interpreter::visitTruncInst(TruncInst &I) {
  966. ExecutionContext &SF = ECStack.back();
  967. SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
  968. }
  969. void Interpreter::visitSExtInst(SExtInst &I) {
  970. ExecutionContext &SF = ECStack.back();
  971. SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
  972. }
  973. void Interpreter::visitZExtInst(ZExtInst &I) {
  974. ExecutionContext &SF = ECStack.back();
  975. SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
  976. }
  977. void Interpreter::visitFPTruncInst(FPTruncInst &I) {
  978. ExecutionContext &SF = ECStack.back();
  979. SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
  980. }
  981. void Interpreter::visitFPExtInst(FPExtInst &I) {
  982. ExecutionContext &SF = ECStack.back();
  983. SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
  984. }
  985. void Interpreter::visitUIToFPInst(UIToFPInst &I) {
  986. ExecutionContext &SF = ECStack.back();
  987. SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
  988. }
  989. void Interpreter::visitSIToFPInst(SIToFPInst &I) {
  990. ExecutionContext &SF = ECStack.back();
  991. SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
  992. }
  993. void Interpreter::visitFPToUIInst(FPToUIInst &I) {
  994. ExecutionContext &SF = ECStack.back();
  995. SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
  996. }
  997. void Interpreter::visitFPToSIInst(FPToSIInst &I) {
  998. ExecutionContext &SF = ECStack.back();
  999. SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
  1000. }
  1001. void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
  1002. ExecutionContext &SF = ECStack.back();
  1003. SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
  1004. }
  1005. void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
  1006. ExecutionContext &SF = ECStack.back();
  1007. SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
  1008. }
  1009. void Interpreter::visitBitCastInst(BitCastInst &I) {
  1010. ExecutionContext &SF = ECStack.back();
  1011. SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
  1012. }
  1013. #define IMPLEMENT_VAARG(TY) \
  1014. case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
  1015. void Interpreter::visitVAArgInst(VAArgInst &I) {
  1016. ExecutionContext &SF = ECStack.back();
  1017. // Get the incoming valist parameter. LLI treats the valist as a
  1018. // (ec-stack-depth var-arg-index) pair.
  1019. GenericValue VAList = getOperandValue(I.getOperand(0), SF);
  1020. GenericValue Dest;
  1021. GenericValue Src = ECStack[VAList.UIntPairVal.first]
  1022. .VarArgs[VAList.UIntPairVal.second];
  1023. const Type *Ty = I.getType();
  1024. switch (Ty->getTypeID()) {
  1025. case Type::IntegerTyID: Dest.IntVal = Src.IntVal;
  1026. IMPLEMENT_VAARG(Pointer);
  1027. IMPLEMENT_VAARG(Float);
  1028. IMPLEMENT_VAARG(Double);
  1029. default:
  1030. cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
  1031. llvm_unreachable();
  1032. }
  1033. // Set the Value of this Instruction.
  1034. SetValue(&I, Dest, SF);
  1035. // Move the pointer to the next vararg.
  1036. ++VAList.UIntPairVal.second;
  1037. }
  1038. GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
  1039. ExecutionContext &SF) {
  1040. switch (CE->getOpcode()) {
  1041. case Instruction::Trunc:
  1042. return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
  1043. case Instruction::ZExt:
  1044. return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
  1045. case Instruction::SExt:
  1046. return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
  1047. case Instruction::FPTrunc:
  1048. return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
  1049. case Instruction::FPExt:
  1050. return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
  1051. case Instruction::UIToFP:
  1052. return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
  1053. case Instruction::SIToFP:
  1054. return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
  1055. case Instruction::FPToUI:
  1056. return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
  1057. case Instruction::FPToSI:
  1058. return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
  1059. case Instruction::PtrToInt:
  1060. return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
  1061. case Instruction::IntToPtr:
  1062. return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
  1063. case Instruction::BitCast:
  1064. return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
  1065. case Instruction::GetElementPtr:
  1066. return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
  1067. gep_type_end(CE), SF);
  1068. case Instruction::FCmp:
  1069. case Instruction::ICmp:
  1070. return executeCmpInst(CE->getPredicate(),
  1071. getOperandValue(CE->getOperand(0), SF),
  1072. getOperandValue(CE->getOperand(1), SF),
  1073. CE->getOperand(0)->getType());
  1074. case Instruction::Select:
  1075. return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
  1076. getOperandValue(CE->getOperand(1), SF),
  1077. getOperandValue(CE->getOperand(2), SF));
  1078. default :
  1079. break;
  1080. }
  1081. // The cases below here require a GenericValue parameter for the result
  1082. // so we initialize one, compute it and then return it.
  1083. GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
  1084. GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
  1085. GenericValue Dest;
  1086. const Type * Ty = CE->getOperand(0)->getType();
  1087. switch (CE->getOpcode()) {
  1088. case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
  1089. case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
  1090. case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
  1091. case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
  1092. case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
  1093. case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
  1094. case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
  1095. case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
  1096. case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
  1097. case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
  1098. case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
  1099. case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
  1100. case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
  1101. case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
  1102. case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
  1103. case Instruction::Shl:
  1104. Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
  1105. break;
  1106. case Instruction::LShr:
  1107. Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
  1108. break;
  1109. case Instruction::AShr:
  1110. Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
  1111. break;
  1112. default:
  1113. cerr << "Unhandled ConstantExpr: " << *CE << "\n";
  1114. llvm_unreachable();
  1115. return GenericValue();
  1116. }
  1117. return Dest;
  1118. }
  1119. GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
  1120. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
  1121. return getConstantExprValue(CE, SF);
  1122. } else if (Constant *CPV = dyn_cast<Constant>(V)) {
  1123. return getConstantValue(CPV);
  1124. } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
  1125. return PTOGV(getPointerToGlobal(GV));
  1126. } else {
  1127. return SF.Values[V];
  1128. }
  1129. }
  1130. //===----------------------------------------------------------------------===//
  1131. // Dispatch and Execution Code
  1132. //===----------------------------------------------------------------------===//
  1133. //===----------------------------------------------------------------------===//
  1134. // callFunction - Execute the specified function...
  1135. //
  1136. void Interpreter::callFunction(Function *F,
  1137. const std::vector<GenericValue> &ArgVals) {
  1138. assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
  1139. ECStack.back().Caller.arg_size() == ArgVals.size()) &&
  1140. "Incorrect number of arguments passed into function call!");
  1141. // Make a new stack frame... and fill it in.
  1142. ECStack.push_back(ExecutionContext());
  1143. ExecutionContext &StackFrame = ECStack.back();
  1144. StackFrame.CurFunction = F;
  1145. // Special handling for external functions.
  1146. if (F->isDeclaration()) {
  1147. GenericValue Result = callExternalFunction (F, ArgVals);
  1148. // Simulate a 'ret' instruction of the appropriate type.
  1149. popStackAndReturnValueToCaller (F->getReturnType (), Result);
  1150. return;
  1151. }
  1152. // Get pointers to first LLVM BB & Instruction in function.
  1153. StackFrame.CurBB = F->begin();
  1154. StackFrame.CurInst = StackFrame.CurBB->begin();
  1155. // Run through the function arguments and initialize their values...
  1156. assert((ArgVals.size() == F->arg_size() ||
  1157. (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
  1158. "Invalid number of values passed to function invocation!");
  1159. // Handle non-varargs arguments...
  1160. unsigned i = 0;
  1161. for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
  1162. AI != E; ++AI, ++i)
  1163. SetValue(AI, ArgVals[i], StackFrame);
  1164. // Handle varargs arguments...
  1165. StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
  1166. }
  1167. void Interpreter::run() {
  1168. while (!ECStack.empty()) {
  1169. // Interpret a single instruction & increment the "PC".
  1170. ExecutionContext &SF = ECStack.back(); // Current stack frame
  1171. Instruction &I = *SF.CurInst++; // Increment before execute
  1172. // Track the number of dynamic instructions executed.
  1173. ++NumDynamicInsts;
  1174. DOUT << "About to interpret: " << I;
  1175. visit(I); // Dispatch to one of the visit* methods...
  1176. #if 0
  1177. // This is not safe, as visiting the instruction could lower it and free I.
  1178. #ifndef NDEBUG
  1179. if (!isa<CallInst>(I) && !isa<InvokeInst>(I) &&
  1180. I.getType() != Type::VoidTy) {
  1181. DOUT << " --> ";
  1182. const GenericValue &Val = SF.Values[&I];
  1183. switch (I.getType()->getTypeID()) {
  1184. default: LLVM_UNREACHABLE("Invalid GenericValue Type");
  1185. case Type::VoidTyID: DOUT << "void"; break;
  1186. case Type::FloatTyID: DOUT << "float " << Val.FloatVal; break;
  1187. case Type::DoubleTyID: DOUT << "double " << Val.DoubleVal; break;
  1188. case Type::PointerTyID: DOUT << "void* " << intptr_t(Val.PointerVal);
  1189. break;
  1190. case Type::IntegerTyID:
  1191. DOUT << "i" << Val.IntVal.getBitWidth() << " "
  1192. << Val.IntVal.toStringUnsigned(10)
  1193. << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
  1194. break;
  1195. }
  1196. }
  1197. #endif
  1198. #endif
  1199. }
  1200. }