Execution.cpp 49 KB

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