idioms.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. /*****************************************************************************
  2. * dcc project machine idiom recognition
  3. * (C) Cristina Cifuentes
  4. ****************************************************************************/
  5. #include "dcc.h"
  6. #include <string.h>
  7. #ifdef __DOSWIN__
  8. #include <stdio.h>
  9. #endif
  10. /*****************************************************************************
  11. * JmpInst - Returns TRUE if opcode is a conditional or unconditional jump
  12. ****************************************************************************/
  13. bool JmpInst(llIcode opcode)
  14. {
  15. switch (opcode) {
  16. case iJMP: case iJMPF: case iJCXZ:
  17. case iLOOP: case iLOOPE:case iLOOPNE:
  18. case iJB: case iJBE: case iJAE: case iJA:
  19. case iJL: case iJLE: case iJGE: case iJG:
  20. case iJE: case iJNE: case iJS: case iJNS:
  21. case iJO: case iJNO: case iJP: case iJNP:
  22. return true;
  23. }
  24. return false;
  25. }
  26. /*****************************************************************************
  27. /* checkStkVars - Checks for PUSH SI
  28. * [PUSH DI]
  29. * or PUSH DI
  30. * [PUSH SI]
  31. * In which case, the stack variable flags are set
  32. ****************************************************************************/
  33. static Int checkStkVars (iICODE pIcode, iICODE pEnd, Function * pProc)
  34. {
  35. /* Look for PUSH SI */
  36. if ((pIcode < pEnd) && (pIcode->ic.ll.opcode == iPUSH) &&
  37. (pIcode->ic.ll.dst.regi == rSI))
  38. {
  39. pProc->flg |= SI_REGVAR;
  40. /* Look for PUSH DI */
  41. if (++pIcode < pEnd && (pIcode->ic.ll.opcode == iPUSH) &&
  42. (pIcode->ic.ll.dst.regi == rDI))
  43. {
  44. pProc->flg |= DI_REGVAR;
  45. return 2;
  46. }
  47. else
  48. return 1;
  49. }
  50. else if ((pIcode < pEnd) && (pIcode->ic.ll.opcode == iPUSH) &&
  51. (pIcode->ic.ll.dst.regi == rDI))
  52. {
  53. pProc->flg |= DI_REGVAR;
  54. /* Look for PUSH SI */
  55. if ((++pIcode < pEnd) && (pIcode->ic.ll.opcode == iPUSH) &&
  56. (pIcode->ic.ll.dst.regi == rSI))
  57. {
  58. pProc->flg |= SI_REGVAR;
  59. return 2;
  60. }
  61. else
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. typedef std::list<ICODE>::iterator cITER;
  67. static Int checkStkVars (cITER pIcode, cITER pEnd, Function * pProc)
  68. {
  69. /* Look for PUSH SI */
  70. if ((pIcode != pEnd) && (pIcode->ic.ll.opcode == iPUSH) &&
  71. (pIcode->ic.ll.dst.regi == rSI))
  72. {
  73. pProc->flg |= SI_REGVAR;
  74. /* Look for PUSH DI */
  75. ++pIcode;
  76. if (pIcode != pEnd && (pIcode->ic.ll.opcode == iPUSH) && (pIcode->ic.ll.dst.regi == rDI))
  77. {
  78. pProc->flg |= DI_REGVAR;
  79. return 2;
  80. }
  81. else
  82. return 1;
  83. }
  84. else if ((pIcode != pEnd) && (pIcode->ic.ll.opcode == iPUSH) &&
  85. (pIcode->ic.ll.dst.regi == rDI))
  86. {
  87. pProc->flg |= DI_REGVAR;
  88. /* Look for PUSH SI */
  89. ++pIcode;
  90. if ((pIcode != pEnd) && (pIcode->ic.ll.opcode == iPUSH) && (pIcode->ic.ll.dst.regi == rSI))
  91. {
  92. pProc->flg |= SI_REGVAR;
  93. return 2;
  94. }
  95. else
  96. return 1;
  97. }
  98. return 0;
  99. }
  100. /*****************************************************************************
  101. * idiom1 - HLL procedure prologue; Returns number of instructions matched.
  102. * PUSH BP ==> ENTER immed, 0
  103. * MOV BP, SP and sets PROC_HLL flag
  104. * [SUB SP, immed]
  105. * [PUSH SI]
  106. * [PUSH DI]
  107. * - Second version: Push stack variables and then save BP
  108. * PUSH BP
  109. * PUSH SI
  110. * [PUSH DI]
  111. * MOV BP, SP
  112. * - Third version: Stack variables
  113. * [PUSH SI]
  114. * [PUSH DI]
  115. ****************************************************************************/
  116. static Int idiom1(iICODE pIcode, iICODE pEnd, Function * pProc)
  117. {
  118. std::vector<iICODE> entries;
  119. Int n;
  120. /* PUSH BP as first instruction of procedure */
  121. if ( !(pIcode->ic.ll.flg & I) && pIcode->ic.ll.src.regi == rBP)
  122. {
  123. /* MOV BP, SP as next instruction */
  124. pIcode++;
  125. if (pIcode < pEnd && ! (pIcode->ic.ll.flg & (I | TARGET | CASE))
  126. && pIcode->ic.ll.opcode == iMOV && pIcode->ic.ll.dst.regi == rBP
  127. && pIcode->ic.ll.src.regi == rSP)
  128. {
  129. pProc->args.minOff = 2;
  130. pProc->flg |= PROC_IS_HLL;
  131. /* Look for SUB SP, immed */
  132. if ((++pIcode < pEnd) &&
  133. (pIcode->ic.ll.flg & (I | TARGET | CASE)) == I &&
  134. pIcode->ic.ll.opcode == iSUB && pIcode->ic.ll.dst.regi == rSP)
  135. {
  136. return (3 + checkStkVars (++pIcode, pEnd, pProc));
  137. }
  138. else
  139. return (2 + checkStkVars (pIcode, pEnd, pProc));
  140. }
  141. /* PUSH SI
  142. * [PUSH DI]
  143. * MOV BP, SP */
  144. else
  145. {
  146. n = checkStkVars (pIcode, pEnd, pProc);
  147. if (n > 0)
  148. {
  149. /* Look for MOV BP, SP */
  150. pIcode += n;
  151. if (pIcode < pEnd &&
  152. ! (pIcode->ic.ll.flg & (I | TARGET | CASE)) &&
  153. pIcode->ic.ll.opcode == iMOV &&
  154. pIcode->ic.ll.dst.regi == rBP &&
  155. pIcode->ic.ll.src.regi == rSP)
  156. {
  157. pProc->args.minOff = 2 + (n * 2);
  158. return (2 + n);
  159. }
  160. else return 0; // Cristina: check this please!
  161. }
  162. else return 0; // Cristina: check this please!
  163. }
  164. }
  165. else
  166. return (checkStkVars (pIcode, pEnd, pProc));
  167. }
  168. static Int idiom1x(cITER pIcode, cITER pEnd, Function * pProc)
  169. {
  170. Int n;
  171. /* PUSH BP as first instruction of procedure */
  172. if ( !(pIcode->ic.ll.flg & I) && pIcode->ic.ll.src.regi == rBP)
  173. {
  174. /* MOV BP, SP as next instruction */
  175. pIcode++;
  176. if (pIcode != pEnd && ! (pIcode->ic.ll.flg & (I | TARGET | CASE))
  177. && pIcode->ic.ll.opcode == iMOV && pIcode->ic.ll.dst.regi == rBP
  178. && pIcode->ic.ll.src.regi == rSP)
  179. {
  180. pProc->args.minOff = 2;
  181. pProc->flg |= PROC_IS_HLL;
  182. /* Look for SUB SP, immed */
  183. ++pIcode;
  184. if ((pIcode != pEnd) &&
  185. (pIcode->ic.ll.flg & (I | TARGET | CASE)) == I &&
  186. pIcode->ic.ll.opcode == iSUB && pIcode->ic.ll.dst.regi == rSP)
  187. {
  188. return (3 + checkStkVars (++pIcode, pEnd, pProc));
  189. }
  190. else
  191. return (2 + checkStkVars (pIcode, pEnd, pProc));
  192. }
  193. /* PUSH SI
  194. * [PUSH DI]
  195. * MOV BP, SP */
  196. else
  197. {
  198. n = checkStkVars (pIcode, pEnd, pProc);
  199. if (n > 0)
  200. {
  201. /* Look for MOV BP, SP */
  202. std::advance(pIcode,n);
  203. if (pIcode != pEnd &&
  204. ! (pIcode->ic.ll.flg & (I | TARGET | CASE)) &&
  205. pIcode->ic.ll.opcode == iMOV &&
  206. pIcode->ic.ll.dst.regi == rBP &&
  207. pIcode->ic.ll.src.regi == rSP)
  208. {
  209. pProc->args.minOff = 2 + (n * 2);
  210. return (2 + n);
  211. }
  212. else return 0; // Cristina: check this please!
  213. }
  214. else return 0; // Cristina: check this please!
  215. }
  216. }
  217. else
  218. return (checkStkVars (pIcode, pEnd, pProc));
  219. }
  220. /*****************************************************************************
  221. * popStkVars - checks for
  222. * [POP DI]
  223. * [POP SI]
  224. * or [POP SI]
  225. * [POP DI]
  226. ****************************************************************************/
  227. static void popStkVars (iICODE pIcode, iICODE pEnd, Function * pProc)
  228. {
  229. /* Match [POP DI] */
  230. if (pIcode->ic.ll.opcode == iPOP)
  231. if ((pProc->flg & DI_REGVAR) && (pIcode->ic.ll.dst.regi == rDI))
  232. pIcode->invalidate();
  233. else if ((pProc->flg & SI_REGVAR) && (pIcode->ic.ll.dst.regi == rSI))
  234. pIcode->invalidate();
  235. /* Match [POP SI] */
  236. if ((pIcode+1)->ic.ll.opcode == iPOP)
  237. if ((pProc->flg & SI_REGVAR) && ((pIcode+1)->ic.ll.dst.regi == rSI))
  238. (pIcode+1)->invalidate();
  239. else if ((pProc->flg & DI_REGVAR) && ((pIcode+1)->ic.ll.dst.regi == rDI))
  240. (pIcode+1)->invalidate();
  241. }
  242. /*****************************************************************************
  243. * idiom2 - HLL procedure epilogue; Returns number of instructions matched.
  244. * [POP DI]
  245. * [POP SI]
  246. * MOV SP, BP
  247. * POP BP
  248. * RET(F)
  249. *****************************************************************************/
  250. static Int idiom2(iICODE pIcode, iICODE pEnd, Int ip, Function * pProc)
  251. {
  252. iICODE nicode;
  253. /* Match MOV SP, BP */
  254. if (ip != 0 && ((pIcode->ic.ll.flg & I) != I) &&
  255. pIcode->ic.ll.dst.regi == rSP && pIcode->ic.ll.src.regi == rBP)
  256. {
  257. /* Get next icode, skip over holes in the icode array */
  258. nicode = pIcode + 1;
  259. while (nicode->ic.ll.flg & NO_CODE)
  260. nicode++;
  261. /* Match POP BP */
  262. if (nicode < pEnd &&
  263. ! (nicode->ic.ll.flg & (I | TARGET | CASE)) &&
  264. nicode->ic.ll.opcode == iPOP &&
  265. nicode->ic.ll.dst.regi == rBP)
  266. {
  267. nicode++;
  268. /* Match RET(F) */
  269. if (nicode < pEnd &&
  270. ! (nicode->ic.ll.flg & (I | TARGET | CASE)) &&
  271. (nicode->ic.ll.opcode == iRET ||
  272. nicode->ic.ll.opcode == iRETF))
  273. {
  274. popStkVars (pIcode-2, pEnd, pProc);
  275. return 2;
  276. }
  277. }
  278. }
  279. return 0;
  280. }
  281. /*****************************************************************************
  282. * idiom3 - C calling convention.
  283. * CALL(F) proc_X
  284. * ADD SP, immed
  285. * Eg: CALL proc_X
  286. * ADD SP, 6
  287. * => pProc->cbParam = immed
  288. * Special case: when the call is at the end of the procedure,
  289. * sometimes the stack gets restored by a MOV sp, bp.
  290. * Need to flag the procedure in these cases.
  291. * Used by compilers to restore the stack when invoking a procedure using
  292. * the C calling convention.
  293. ****************************************************************************/
  294. static Int idiom3(iICODE pIcode, iICODE pEnd)
  295. {
  296. /* Match ADD SP, immed */
  297. ++pIcode;
  298. if ((pIcode < pEnd) && (pIcode->ic.ll.flg & I) &&
  299. (pIcode->ic.ll.opcode == iADD) && (pIcode->ic.ll.dst.regi == rSP))
  300. return (pIcode->ic.ll.src.op());
  301. else if ((pIcode->ic.ll.opcode == iMOV) && (pIcode->ic.ll.dst.regi == rSP)
  302. && (pIcode->ic.ll.src.regi == rBP))
  303. (pIcode-1)->ic.ll.flg |= REST_STK;
  304. return 0;
  305. }
  306. /*****************************************************************************
  307. * idiom 17 - C calling convention.
  308. * CALL(F) xxxx
  309. * POP reg
  310. * [POP reg] reg in {AX, BX, CX, DX}
  311. * Eg: CALL proc_X
  312. * POP cx
  313. * POP cx (4 bytes of arguments)
  314. * => pProc->cbParam = # pops * 2
  315. * Found in Turbo C when restoring the stack for a procedure that uses the
  316. * C calling convention. Used to restore the stack of 2 or 4 bytes args.
  317. ****************************************************************************/
  318. static Int idiom17 (iICODE pIcode, iICODE pEnd)
  319. {
  320. Int i = 0; /* Count on # pops */
  321. byte regi;
  322. /* Match POP reg */
  323. ++pIcode;
  324. if ((pIcode < pEnd) && (pIcode->ic.ll.opcode == iPOP))
  325. {
  326. regi = pIcode->ic.ll.dst.regi;
  327. if ((regi >= rAX) && (regi <= rBX))
  328. i++;
  329. ++pIcode;
  330. while (pIcode->ic.ll.opcode == iPOP)
  331. {
  332. if (pIcode->ic.ll.dst.regi == regi)
  333. {
  334. i++;
  335. ++pIcode;
  336. }
  337. else
  338. break;
  339. }
  340. return (i * 2);
  341. }
  342. return (0);
  343. }
  344. /*****************************************************************************
  345. * idiom4 - Pascal calling convention.
  346. * RET(F) immed
  347. * ==> pProc->cbParam = immed
  348. * sets CALL_PASCAL flag
  349. * - Second version: check for optional pop of stack vars
  350. * [POP DI]
  351. * [POP SI]
  352. * POP BP
  353. * RET(F) [immed]
  354. * - Third version: pop stack vars
  355. * [POP DI]
  356. * [POP SI]
  357. * RET(F) [immed]
  358. ****************************************************************************/
  359. static void idiom4 (iICODE pIcode, iICODE pEnd, Function * pProc)
  360. {
  361. /* Check for [POP DI]
  362. * [POP SI] */
  363. popStkVars (pIcode-3, pEnd, pProc);
  364. /* Check for POP BP */
  365. if (((pIcode-1)->ic.ll.opcode == iPOP) &&
  366. (((pIcode-1)->ic.ll.flg & I) != I) &&
  367. ((pIcode-1)->ic.ll.dst.regi == rBP))
  368. (pIcode-1)->invalidate();
  369. else
  370. popStkVars (pIcode-2, pEnd, pProc);
  371. /* Check for RET(F) immed */
  372. if (pIcode->ic.ll.flg & I)
  373. {
  374. pProc->cbParam = (int16)pIcode->ic.ll.src.op();
  375. pProc->flg |= CALL_PASCAL;
  376. }
  377. }
  378. /*****************************************************************************
  379. * idiom5 - Long addition.
  380. * ADD reg/stackOff, reg/stackOff
  381. * ADC reg/stackOff, reg/stackOff
  382. * Eg: ADD ax, [bp-4]
  383. * ADC dx, [bp-2]
  384. * => dx:ax = dx:ax + [bp-2]:[bp-4]
  385. * Found in Borland Turbo C code.
  386. * Commonly used idiom for long addition.
  387. ****************************************************************************/
  388. static boolT idiom5 (iICODE pIcode, iICODE pEnd)
  389. {
  390. if (pIcode < pEnd)
  391. if ((pIcode+1)->ic.ll.opcode == iADC)
  392. return true;
  393. return false;
  394. }
  395. /*****************************************************************************
  396. * idiom6 - Long substraction.
  397. * SUB reg/stackOff, reg/stackOff
  398. * SBB reg/stackOff, reg/stackOff
  399. * Eg: SUB ax, [bp-4]
  400. * SBB dx, [bp-2]
  401. * => dx:ax = dx:ax - [bp-2]:[bp-4]
  402. * Found in Borland Turbo C code.
  403. * Commonly used idiom for long substraction.
  404. ****************************************************************************/
  405. static boolT idiom6 (iICODE pIcode, iICODE pEnd)
  406. {
  407. if (pIcode < pEnd)
  408. if ((pIcode+1)->ic.ll.opcode == iSBB)
  409. return true;
  410. return false;
  411. }
  412. /*****************************************************************************
  413. * idiom7 - Assign zero
  414. * XOR reg/stackOff, reg/stackOff
  415. * Eg: XOR ax, ax
  416. * => ax = 0
  417. * Found in Borland Turbo C and Microsoft C code.
  418. ****************************************************************************/
  419. static boolT idiom7 (iICODE pIcode)
  420. {
  421. LLOpcode *dst, *src;
  422. dst = &pIcode->ic.ll.dst;
  423. src = &pIcode->ic.ll.src;
  424. if (dst->regi == 0) /* global variable */
  425. {
  426. if ((dst->segValue == src->segValue) && (dst->off == src->off))
  427. return true;
  428. }
  429. else if (dst->regi < INDEXBASE) /* register */
  430. {
  431. if (dst->regi == src->regi)
  432. return true;
  433. }
  434. else if ((dst->off) && (dst->seg == rSS) && (dst->regi == INDEXBASE + 6))
  435. /* offset from BP */
  436. {
  437. if ((dst->off == src->off) && (dst->seg == src->seg) &&
  438. (dst->regi == src->regi))
  439. return true;
  440. }
  441. return false;
  442. }
  443. /*****************************************************************************
  444. * idiom21 - Assign long kte with high part zero
  445. * XOR regH, regH
  446. * MOV regL, kte
  447. * => regH:regL = kte
  448. * Eg: XOR dx, dx
  449. * MOV ax, 3
  450. * => dx:ax = 3
  451. * Note: only the following valid combinations are available:
  452. * dx:ax
  453. * cx:bx
  454. * Found in Borland Turbo C code.
  455. ****************************************************************************/
  456. static boolT idiom21 (iICODE picode, iICODE pend)
  457. {
  458. LLOpcode *dst, *src;
  459. dst = &picode->ic.ll.dst;
  460. src = &picode->ic.ll.src;
  461. if (((picode+1) < pend) && ((picode+1)->ic.ll.flg & I))
  462. {
  463. if ((dst->regi == src->regi) && (dst->regi > 0) &&
  464. (dst->regi < INDEXBASE))
  465. {
  466. if ((dst->regi == rDX) && ((picode+1)->ic.ll.dst.regi == rAX))
  467. return true;
  468. if ((dst->regi == rCX) && ((picode+1)->ic.ll.dst.regi == rBX))
  469. return true;
  470. }
  471. }
  472. return false;
  473. }
  474. /*****************************************************************************
  475. * idiom8 - Shift right by 1 (signed long ops)
  476. * SAR reg, 1
  477. * RCR reg, 1
  478. * Eg: SAR dx, 1
  479. * RCR ax, 1
  480. * => dx:ax = dx:ax >> 1 (dx:ax are signed long)
  481. * Found in Microsoft C code for long signed variable shift right.
  482. ****************************************************************************/
  483. static boolT idiom8 (iICODE pIcode, iICODE pEnd)
  484. {
  485. iICODE next=pIcode+1;
  486. if((pIcode>=pEnd) and (next>=pEnd))
  487. return false;
  488. if (pIcode->ic.ll.anyFlagSet(I) && (pIcode->ic.ll.src.op() == 1))
  489. if ((next->ic.ll.opcode == iRCR) &&
  490. next->ic.ll.anyFlagSet(I) &&
  491. (next->ic.ll.src.op() == 1))
  492. return true;
  493. return false;
  494. }
  495. /*****************************************************************************
  496. * idiom 15 - Shift left by n
  497. * SHL reg, 1
  498. * SHL reg, 1
  499. * [...]
  500. * [SHL reg, 1]
  501. * Eg: SHL ax, 1
  502. * SHL ax, 1
  503. * => ax = ax << 2
  504. * Found in Borland Turbo C code to index an array (array multiplication)
  505. ****************************************************************************/
  506. static Int idiom15 (iICODE picode, iICODE pend)
  507. {
  508. Int n = 1;
  509. byte regi;
  510. if (picode < pend)
  511. {
  512. /* Match SHL reg, 1 */
  513. if ((picode->ic.ll.flg & I) && (picode->ic.ll.src.op() == 1))
  514. {
  515. regi = picode->ic.ll.dst.regi;
  516. for(auto iter=picode+1;iter < pend;++iter)
  517. {
  518. if ((iter->ic.ll.opcode == iSHL) &&
  519. (iter->ic.ll.flg & I) &&
  520. (iter->ic.ll.src.op() == 1) &&
  521. (iter->ic.ll.dst.regi == regi))
  522. n++;
  523. else
  524. break;
  525. }
  526. }
  527. }
  528. if (n > 1)
  529. return (n);
  530. else
  531. return (0);
  532. }
  533. /*****************************************************************************
  534. * idiom12 - Shift left long by 1
  535. * SHL reg, 1
  536. * RCL reg, 1
  537. * Eg: SHL ax, 1
  538. * RCL dx, 1
  539. * => dx:ax = dx:ax << 1
  540. * Found in Borland Turbo C code for long variable shift left.
  541. ****************************************************************************/
  542. static boolT idiom12 (iICODE pIcode, iICODE pEnd)
  543. {
  544. iICODE next=pIcode+1;
  545. if (pIcode < pEnd)
  546. {
  547. if (pIcode->ic.ll.anyFlagSet(I) && (pIcode->ic.ll.src.op() == 1))
  548. if ((next->ic.ll.opcode == iRCL) &&
  549. (next->ic.ll.anyFlagSet(I)) &&
  550. (next->ic.ll.src.op() == 1))
  551. return true;
  552. }
  553. return false;
  554. }
  555. /*****************************************************************************
  556. * idiom9 - Shift right by 1 (unsigned long ops)
  557. * SHR reg, 1
  558. * RCR reg, 1
  559. * Eg: SHR dx, 1
  560. * RCR ax, 1
  561. * => dx:ax = dx:ax >> 1 (dx:ax are unsigned long)
  562. * Found in Microsoft C code for long unsigned variable shift right.
  563. ****************************************************************************/
  564. static boolT idiom9 (iICODE pIcode, iICODE pEnd)
  565. {
  566. iICODE next=pIcode+1;
  567. if (pIcode < pEnd)
  568. {
  569. if (((pIcode->ic.ll.flg & I) == I) && (pIcode->ic.ll.src.op() == 1))
  570. if ((next->ic.ll.opcode == iRCR) &&
  571. ((next->ic.ll.flg & I) == I) &&
  572. (next->ic.ll.src.op() == 1))
  573. return true;
  574. }
  575. return false;
  576. }
  577. /*****************************************************************************
  578. * idiom10 - Jump if not equal to 0
  579. * OR reg, reg
  580. * JNE labX
  581. * Eg: OR ax, ax
  582. * JNE labX
  583. * => HLI_JCOND (ax != 0) labX
  584. * Note: we also check that these instructions are not followed by
  585. * CMP reg, kte
  586. * JE lab
  587. * because this is most likely a long conditional equality test.
  588. * Found in Borland Turbo C.
  589. ****************************************************************************/
  590. static boolT idiom10old (iICODE pIcode, iICODE pEnd)
  591. {
  592. if (pIcode < pEnd)
  593. {
  594. /* Check OR reg, reg */
  595. if (((pIcode->ic.ll.flg & I) != I) &&
  596. (pIcode->ic.ll.src. regi > 0) &&
  597. (pIcode->ic.ll.src.regi < INDEXBASE) &&
  598. (pIcode->ic.ll.src.regi == pIcode->ic.ll.dst.regi))
  599. if ((pIcode+3) < pEnd)
  600. {
  601. if (((pIcode+1)->ic.ll.opcode == iJNE) &&
  602. ((pIcode+2)->ic.ll.opcode != iCMP) &&
  603. ((pIcode+3)->ic.ll.opcode != iJE))
  604. return true;
  605. }
  606. else /* at the end of the procedure */
  607. if (((pIcode+1) < pEnd) && ((pIcode+1)->ic.ll.opcode == iJNE))
  608. return true;
  609. }
  610. return false;
  611. }
  612. /*****************************************************************************
  613. * idiom10 - Jump if not equal to 0
  614. * OR reg, reg
  615. * JNE labX
  616. * Eg: OR ax, ax
  617. * JNE labX
  618. * => CMP reg 0
  619. * JNE labX
  620. * This instruction is NOT converted into the equivalent high-level
  621. * instruction "HLI_JCOND (reg != 0) labX" because we do not know yet if
  622. * it forms part of a long register conditional test. It is therefore
  623. * modified to simplify the analysis.
  624. * Found in Borland Turbo C.
  625. ****************************************************************************/
  626. static void idiom10 (iICODE pIcode, iICODE pEnd)
  627. {
  628. iICODE next=pIcode+1;
  629. if((pIcode>=pEnd) or (next>=pEnd))
  630. return;
  631. /* Check OR reg, reg */
  632. if (((pIcode->ic.ll.flg & I) != I) &&
  633. (pIcode->ic.ll.src.regi > 0) &&
  634. (pIcode->ic.ll.src.regi < INDEXBASE) &&
  635. (pIcode->ic.ll.src.regi == pIcode->ic.ll.dst.regi))
  636. if (next->ic.ll.opcode == iJNE)
  637. {
  638. pIcode->ic.ll.opcode = iCMP;
  639. pIcode->ic.ll.flg |= I;
  640. pIcode->ic.ll.src.SetImmediateOp(0); // todo check if proc should be zeroed too
  641. pIcode->du.def = 0;
  642. pIcode->du1.numRegsDef = 0;
  643. }
  644. }
  645. /*****************************************************************************
  646. * idiom 13 - Word assign
  647. * MOV regL, mem
  648. * MOV regH, 0
  649. * Eg: MOV al, [bp-2]
  650. * MOV ah, 0
  651. * => MOV ax, [bp-2]
  652. * Found in Borland Turbo C, used for multiplication and division of
  653. * byte operands (ie. they need to be extended to words).
  654. ****************************************************************************/
  655. static byte idiom13 (iICODE pIcode, iICODE pEnd)
  656. {
  657. byte regi;
  658. iICODE next=pIcode+1;
  659. if((pIcode>=pEnd) or (next>=pEnd))
  660. return 0;
  661. /* Check for regL */
  662. regi = pIcode->ic.ll.dst.regi;
  663. if (((pIcode->ic.ll.flg & I) != I) && (regi >= rAL) && (regi <= rBH))
  664. {
  665. /* Check for MOV regH, 0 */
  666. if ((next->ic.ll.opcode == iMOV) &&
  667. (next->ic.ll.flg & I) &&
  668. (next->ic.ll.src.op() == 0))
  669. {
  670. if (next->ic.ll.dst.regi == (regi + 4))
  671. return (regi - rAL + rAX);
  672. }
  673. }
  674. return (0);
  675. }
  676. /*****************************************************************************
  677. * idiom 14 - Long word assign
  678. * MOV regL, mem/reg
  679. * XOR regH, regH
  680. * Eg: MOV ax, di
  681. * XOR dx, dx
  682. * => MOV dx:ax, di
  683. * Note: only the following combinations are allowed:
  684. * dx:ax
  685. * cx:bx
  686. * this is to remove the possibility of making errors in situations
  687. * like this:
  688. * MOV dx, offH
  689. * MOV ax, offL
  690. * XOR cx, cx
  691. * Found in Borland Turbo C, used for division of unsigned integer
  692. * operands.
  693. ****************************************************************************/
  694. static boolT idiom14 (iICODE picode, iICODE pend, byte *regL, byte *regH)
  695. {
  696. if (picode < pend)
  697. {
  698. /* Check for regL */
  699. *regL = picode->ic.ll.dst.regi;
  700. if (((picode->ic.ll.flg & I) != I) && ((*regL == rAX) || (*regL ==rBX)))
  701. {
  702. /* Check for XOR regH, regH */
  703. if (((picode+1)->ic.ll.opcode == iXOR) &&
  704. (((picode+1)->ic.ll.flg & I) != I))
  705. {
  706. *regH = (picode+1)->ic.ll.dst.regi;
  707. if (*regH == (picode+1)->ic.ll.src.regi)
  708. {
  709. if ((*regL == rAX) && (*regH == rDX))
  710. return true;
  711. if ((*regL == rBX) && (*regH == rCX))
  712. return true;
  713. }
  714. }
  715. }
  716. }
  717. return false;
  718. }
  719. /*****************************************************************************
  720. * idiom11 - Negate long integer
  721. * NEG regH
  722. * NEG regL
  723. * SBB regH, 0
  724. * Eg: NEG dx
  725. * NEG ax
  726. * SBB dx, 0
  727. * => dx:ax = - dx:ax
  728. * Found in Borland Turbo C.
  729. ****************************************************************************/
  730. static boolT idiom11 (iICODE pIcode, iICODE pEnd)
  731. { condId type; /* type of argument */
  732. if ((pIcode + 2) < pEnd)
  733. {
  734. type = pIcode->idType(DST);
  735. if ((type != CONSTANT) && (type != OTHER))
  736. {
  737. /* Check NEG reg/mem
  738. * SBB reg/mem, 0*/
  739. if (((pIcode+1)->ic.ll.opcode == iNEG) &&
  740. ((pIcode+2)->ic.ll.opcode == iSBB))
  741. switch (type) {
  742. case GLOB_VAR: if (((pIcode+2)->ic.ll.dst.segValue ==
  743. pIcode->ic.ll.dst.segValue) &&
  744. ((pIcode+2)->ic.ll.dst.off ==
  745. pIcode->ic.ll.dst.off))
  746. return true;
  747. break;
  748. case REGISTER: if ((pIcode+2)->ic.ll.dst.regi ==
  749. pIcode->ic.ll.dst.regi)
  750. return true;
  751. break;
  752. case PARAM:
  753. case LOCAL_VAR: if ((pIcode+2)->ic.ll.dst.off ==
  754. pIcode->ic.ll.dst.off)
  755. return true;
  756. break;
  757. }
  758. }
  759. }
  760. return false;
  761. }
  762. /*****************************************************************************
  763. * idiom 16: Bitwise negation
  764. * NEG reg
  765. * SBB reg, reg
  766. * INC reg
  767. * => ASGN reg, !reg
  768. * Eg: NEG ax
  769. * SBB ax, ax
  770. * INC ax
  771. * => ax = !ax
  772. * Found in Borland Turbo C when negating bitwise.
  773. ****************************************************************************/
  774. static boolT idiom16 (iICODE picode, iICODE pend)
  775. { byte regi;
  776. if ((picode+2) < pend)
  777. {
  778. regi = picode->ic.ll.dst.regi;
  779. if ((regi >= rAX) && (regi < INDEXBASE))
  780. {
  781. if (((picode+1)->ic.ll.opcode == iSBB) &&
  782. ((picode+2)->ic.ll.opcode == iINC))
  783. if (((picode+1)->ic.ll.dst.regi ==
  784. ((picode+1)->ic.ll.src.regi)) &&
  785. ((picode+1)->ic.ll.dst.regi == regi) &&
  786. ((picode+2)->ic.ll.dst.regi == regi))
  787. return true;
  788. }
  789. }
  790. return false;
  791. }
  792. /*****************************************************************************
  793. * idiom 18: Post-increment or post-decrement in a conditional jump
  794. * Used
  795. * 0 MOV reg, var (including register variables)
  796. * 1 INC var or DEC var <------------------------- input point
  797. * 2 CMP var, Y
  798. * 3 JX label
  799. * => HLI_JCOND (var++ X Y)
  800. * Eg: MOV ax, si
  801. * INC si
  802. * CMP ax, 8
  803. * JL labX
  804. * => HLI_JCOND (si++ < 8)
  805. * Found in Borland Turbo C. Intrinsic to C languages.
  806. ****************************************************************************/
  807. static boolT idiom18 (iICODE picode, iICODE pend, Function * pproc)
  808. {
  809. assert(picode+2<=pend);
  810. std::vector<iICODE> used;
  811. for(int i=0; i<4; i++)
  812. {
  813. used.push_back(picode-1+i);
  814. }
  815. int type = -1; /* type of variable: 1 = reg-var, 2 = local */
  816. byte regi; /* register of the MOV */
  817. /* Get variable */
  818. if (used[1]->ic.ll.dst.regi == 0) /* global variable */
  819. {
  820. /* not supported yet */
  821. type = 0;
  822. }
  823. else if (used[1]->ic.ll.dst.regi < INDEXBASE) /* register */
  824. {
  825. if ((used[1]->ic.ll.dst.regi == rSI) && (pproc->flg & SI_REGVAR))
  826. type = 1;
  827. else if ((used[1]->ic.ll.dst.regi == rDI) && (pproc->flg & DI_REGVAR))
  828. type = 1;
  829. }
  830. else if (used[1]->ic.ll.dst.off) /* local variable */
  831. type = 2;
  832. else /* indexed */
  833. {
  834. type=3;
  835. /* not supported yet */
  836. printf("Unsupported idiom18 type: indexed");
  837. }
  838. switch(type)
  839. {
  840. case 0: // global
  841. printf("Unsupported idiom18 type: global variable");
  842. break;
  843. case 1: /* register variable */
  844. /* Check previous instruction for a MOV */
  845. if ((used[0]->ic.ll.opcode == iMOV) && (used[0]->ic.ll.src.regi == used[1]->ic.ll.dst.regi))
  846. {
  847. regi = used[0]->ic.ll.dst.regi;
  848. if ((regi > 0) && (regi < INDEXBASE))
  849. {
  850. if ( (used[2]->ic.ll.opcode == iCMP) && (used[2]->ic.ll.dst.regi == regi) &&
  851. used[3]->ic.ll.conditionalJump() )
  852. return true;
  853. }
  854. }
  855. break;
  856. case 2: /* local */
  857. if ((used[0]->ic.ll.opcode == iMOV) && (used[0]->ic.ll.src.off == used[1]->ic.ll.dst.off))
  858. {
  859. regi = used[0]->ic.ll.dst.regi;
  860. if ((regi > 0) && (regi < INDEXBASE))
  861. {
  862. if ( (used[2]->ic.ll.opcode == iCMP) && (used[2]->ic.ll.dst.regi == regi) &&
  863. used[3]->ic.ll.conditionalJump() )
  864. return true;
  865. }
  866. }
  867. break;
  868. case 3: // indexed
  869. printf("Unsupported idiom18 type: indexed");
  870. break;
  871. }
  872. return false;
  873. }
  874. /*****************************************************************************
  875. * idiom 19: pre-increment or pre-decrement in conditional jump, comparing
  876. * against 0.
  877. * INC var or DEC var (including register vars)
  878. * JX lab JX lab
  879. * => HLI_JCOND (++var X 0) or HLI_JCOND (--var X 0)
  880. * Eg: INC [bp+4]
  881. * JG lab2
  882. * => HLI_JCOND (++[bp+4] > 0)
  883. * Found in Borland Turbo C. Intrinsic to C language.
  884. ****************************************************************************/
  885. static boolT idiom19 (iICODE picode, iICODE pend, Function * pproc)
  886. {
  887. if (picode->ic.ll.dst.regi == 0) /* global variable */
  888. /* not supported yet */ ;
  889. else if (picode->ic.ll.dst.regi < INDEXBASE) /* register */
  890. {
  891. if (((picode->ic.ll.dst.regi == rSI) && (pproc->flg & SI_REGVAR)) ||
  892. ((picode->ic.ll.dst.regi == rDI) && (pproc->flg & DI_REGVAR)))
  893. if ((picode < pend) && ((picode+1)->ic.ll.opcode >= iJB) &&
  894. ((picode+1)->ic.ll.opcode < iJCXZ))
  895. return true;
  896. }
  897. else if (picode->ic.ll.dst.off) /* stack variable */
  898. {
  899. if ((picode < pend) && ((picode+1)->ic.ll.opcode >= iJB) &&
  900. ((picode+1)->ic.ll.opcode < iJCXZ))
  901. return true;
  902. }
  903. else /* indexed */
  904. /* not supported yet */ ;
  905. return false;
  906. }
  907. /*****************************************************************************
  908. * idiom20: Pre increment/decrement in conditional expression (compares
  909. * against a register, variable or constant different than 0).
  910. * INC var or DEC var (including register vars)
  911. * MOV reg, var MOV reg, var
  912. * CMP reg, Y CMP reg, Y
  913. * JX lab JX lab
  914. * => HLI_JCOND (++var X Y) or HLI_JCOND (--var X Y)
  915. * Eg: INC si (si is a register variable)
  916. * MOV ax, si
  917. * CMP ax, 2
  918. * JL lab4
  919. * => HLI_JCOND (++si < 2)
  920. * Found in Turbo C. Intrinsic to C language.
  921. ****************************************************************************/
  922. static boolT idiom20 (iICODE picode, iICODE pend, Function * pproc)
  923. { boolT type = 0; /* type of variable: 1 = reg-var, 2 = local */
  924. byte regi; /* register of the MOV */
  925. /* Get variable */
  926. if (picode->ic.ll.dst.regi == 0) /* global variable */
  927. /* not supported yet */ ;
  928. else if (picode->ic.ll.dst.regi < INDEXBASE) /* register */
  929. {
  930. if ((picode->ic.ll.dst.regi == rSI) && (pproc->flg & SI_REGVAR))
  931. type = 1;
  932. else if ((picode->ic.ll.dst.regi == rDI) && (pproc->flg & DI_REGVAR))
  933. type = 1;
  934. }
  935. else if (picode->ic.ll.dst.off) /* local variable */
  936. type = 2;
  937. else /* indexed */
  938. /* not supported yet */ ;
  939. /* Check previous instruction for a MOV */
  940. if (type == 1) /* register variable */
  941. {
  942. if ((picode < pend) && ((picode+1)->ic.ll.opcode == iMOV) &&
  943. ((picode+1)->ic.ll.src.regi == picode->ic.ll.dst.regi))
  944. {
  945. regi = (picode+1)->ic.ll.dst.regi;
  946. if ((regi > 0) && (regi < INDEXBASE))
  947. {
  948. if (((picode+1) < pend) && ((picode+2) < pend) &&
  949. ((picode+2)->ic.ll.opcode == iCMP) &&
  950. ((picode+2)->ic.ll.dst.regi == regi) &&
  951. (((picode+3)->ic.ll.opcode >= iJB) &&
  952. ((picode+3)->ic.ll.opcode < iJCXZ)))
  953. return true;
  954. }
  955. }
  956. }
  957. else if (type == 2) /* local */
  958. {
  959. if ((picode < pend) && ((picode+1)->ic.ll.opcode == iMOV) &&
  960. ((picode+1)->ic.ll.src.off == picode->ic.ll.dst.off))
  961. {
  962. regi = (picode+1)->ic.ll.dst.regi;
  963. if ((regi > 0) && (regi < INDEXBASE))
  964. {
  965. if (((picode+1) < pend) && ((picode+2) < pend) &&
  966. ((picode+2)->ic.ll.opcode == iCMP) &&
  967. ((picode+2)->ic.ll.dst.regi == regi) &&
  968. (((picode+3)->ic.ll.opcode >= iJB) &&
  969. ((picode+3)->ic.ll.opcode < iJCXZ)))
  970. return true;
  971. }
  972. }
  973. }
  974. return false;
  975. }
  976. /*****************************************************************************
  977. * findIdioms - translates LOW_LEVEL icode idioms into HIGH_LEVEL icodes.
  978. ****************************************************************************/
  979. void Function::findIdioms()
  980. {
  981. Int ip; /* Index to current icode */
  982. iICODE pEnd, pIcode; /* Pointers to end of BB and current icodes */
  983. int16 delta;
  984. COND_EXPR *rhs, *lhs; /* Pointers to left and right hand side exps */
  985. COND_EXPR *exp; /* Pointer to temporal expression */
  986. Int idx; /* Index into local identifier table */
  987. byte regH, regL; /* High and low registers for long word reg */
  988. pIcode = Icode.begin();
  989. pEnd = Icode.end();
  990. ip = 0;
  991. while (pIcode < pEnd)
  992. {
  993. switch (pIcode->ic.ll.opcode)
  994. {
  995. case iDEC: case iINC:
  996. if (idiom18 (pIcode, pEnd, this))
  997. {
  998. lhs = COND_EXPR::id (*(pIcode-1), SRC, this, ip, *pIcode, eUSE);
  999. if (pIcode->ic.ll.opcode == iDEC)
  1000. lhs = COND_EXPR::unary (POST_DEC, lhs);
  1001. else
  1002. lhs = COND_EXPR::unary (POST_INC, lhs);
  1003. rhs = COND_EXPR::id (*(pIcode+1), SRC, this, ip, *(pIcode+2), eUSE);
  1004. exp = COND_EXPR::boolOp (lhs, rhs,
  1005. condOpJCond[(pIcode+2)->ic.ll.opcode - iJB]);
  1006. (pIcode+2)->setJCond(exp);
  1007. (pIcode-1)->invalidate();
  1008. pIcode->invalidate();
  1009. (pIcode+1)->invalidate();
  1010. pIcode += 3;
  1011. ip += 2;
  1012. }
  1013. else if (idiom19 (pIcode, pEnd, this))
  1014. {
  1015. lhs = COND_EXPR::id (*pIcode, DST, this, ip, *(pIcode+1), eUSE);
  1016. if (pIcode->ic.ll.opcode == iDEC)
  1017. lhs = COND_EXPR::unary (PRE_DEC, lhs);
  1018. else
  1019. lhs = COND_EXPR::unary (PRE_INC, lhs);
  1020. rhs = COND_EXPR::idKte (0, 2);
  1021. exp = COND_EXPR::boolOp (lhs, rhs,
  1022. condOpJCond[(pIcode+1)->ic.ll.opcode - iJB]);
  1023. (pIcode+1)->setJCond(exp);
  1024. pIcode->invalidate();
  1025. pIcode += 2;
  1026. ip++;
  1027. }
  1028. else if (idiom20 (pIcode, pEnd, this))
  1029. {
  1030. lhs = COND_EXPR::id (*(pIcode+1), SRC, this, ip, *pIcode, eUSE);
  1031. if (pIcode->ic.ll.opcode == iDEC)
  1032. lhs = COND_EXPR::unary (PRE_DEC, lhs);
  1033. else
  1034. lhs = COND_EXPR::unary (PRE_INC, lhs);
  1035. rhs = COND_EXPR::id (*(pIcode+2), SRC, this, ip, *(pIcode+3), eUSE);
  1036. exp = COND_EXPR::boolOp (lhs, rhs,
  1037. condOpJCond[(pIcode+3)->ic.ll.opcode - iJB]);
  1038. (pIcode+3)->setJCond(exp);
  1039. pIcode->invalidate();
  1040. (pIcode+1)->invalidate();
  1041. (pIcode+2)->invalidate();
  1042. pIcode += 3;
  1043. ip += 2;
  1044. }
  1045. else
  1046. pIcode++;
  1047. break;
  1048. case iPUSH: /* Idiom 1 */
  1049. if ((! (flg & PROC_HLL)) && (idx = idiom1 (pIcode, pEnd, this)))
  1050. {
  1051. flg |= PROC_HLL;
  1052. for ( ; idx > 0; idx--)
  1053. {
  1054. (pIcode++)->invalidate();
  1055. ip++;
  1056. }
  1057. ip--;
  1058. }
  1059. else
  1060. pIcode++;
  1061. break;
  1062. case iMOV: /* Idiom 2 */
  1063. if (idx = idiom2(pIcode, pEnd, ip, this))
  1064. {
  1065. pIcode->invalidate();
  1066. (pIcode+1)->invalidate();
  1067. pIcode += 3;
  1068. ip += 2;
  1069. }
  1070. else if (idiom14 (pIcode, pEnd, &regL, &regH)) /* Idiom 14 */
  1071. {
  1072. idx = localId.newLongReg (TYPE_LONG_SIGN, regH, regL, ip);
  1073. lhs = COND_EXPR::idLongIdx (idx);
  1074. pIcode->setRegDU( regH, eDEF);
  1075. rhs = COND_EXPR::id (*pIcode, SRC, this, ip, *pIcode, NONE);
  1076. pIcode->setAsgn(lhs, rhs);
  1077. (pIcode+1)->invalidate();
  1078. pIcode += 2;
  1079. ip++;
  1080. }
  1081. else if (idx = idiom13 (pIcode, pEnd)) /* Idiom 13 */
  1082. {
  1083. lhs = COND_EXPR::idReg (idx, 0, &localId);
  1084. pIcode->setRegDU( idx, eDEF);
  1085. pIcode->du1.numRegsDef--; /* prev byte reg def */
  1086. rhs = COND_EXPR::id (*pIcode, SRC, this, ip, *pIcode, NONE);
  1087. pIcode->setAsgn(lhs, rhs);
  1088. (pIcode+1)->invalidate();
  1089. pIcode += 2;
  1090. ip++;
  1091. }
  1092. else
  1093. pIcode++;
  1094. break;
  1095. case iCALL: case iCALLF:
  1096. /* Check for library functions that return a long register.
  1097. * Propagate this result */
  1098. if (pIcode->ic.ll.src.proc.proc != 0)
  1099. if ((pIcode->ic.ll.src.proc.proc->flg & PROC_ISLIB) &&
  1100. (pIcode->ic.ll.src.proc.proc->flg & PROC_IS_FUNC))
  1101. {
  1102. if ((pIcode->ic.ll.src.proc.proc->retVal.type==TYPE_LONG_SIGN)
  1103. || (pIcode->ic.ll.src.proc.proc->retVal.type == TYPE_LONG_UNSIGN))
  1104. localId.newLongReg(TYPE_LONG_SIGN, rDX, rAX, ip);
  1105. }
  1106. /* Check for idioms */
  1107. if (idx = idiom3(pIcode, pEnd)) /* idiom 3 */
  1108. {
  1109. if (pIcode->ic.ll.flg & I)
  1110. {
  1111. (pIcode->ic.ll.src.proc.proc)->cbParam = (int16)idx;
  1112. pIcode->ic.ll.src.proc.cb = idx;
  1113. (pIcode->ic.ll.src.proc.proc)->flg |= CALL_C;
  1114. pIcode++;
  1115. (pIcode++)->invalidate();
  1116. ip++;
  1117. }
  1118. else
  1119. {
  1120. printf("Indirect call at idiom3\n");
  1121. pIcode++;
  1122. }
  1123. }
  1124. else if (idx = idiom17 (pIcode, pEnd)) /* idiom 17 */
  1125. {
  1126. if (pIcode->isLlFlag(I))
  1127. {
  1128. (pIcode->ic.ll.src.proc.proc)->cbParam = (int16)idx;
  1129. pIcode->ic.ll.src.proc.cb = idx;
  1130. (pIcode->ic.ll.src.proc.proc)->flg |= CALL_C;
  1131. ip += idx/2 - 1;
  1132. pIcode++;
  1133. for (idx /= 2; idx > 0; idx--)
  1134. (pIcode++)->invalidate();
  1135. }
  1136. // TODO : it's a calculated call
  1137. else
  1138. {
  1139. printf("Indirect call at idiom17\n");
  1140. pIcode++;
  1141. }
  1142. }
  1143. else
  1144. pIcode++;
  1145. break;
  1146. case iRET: /* Idiom 4 */
  1147. case iRETF:
  1148. idiom4 (pIcode, pEnd, this);
  1149. pIcode++;
  1150. break;
  1151. case iADD: /* Idiom 5 */
  1152. if (idiom5 (pIcode, pEnd))
  1153. {
  1154. lhs = COND_EXPR::idLong (&localId, DST, pIcode, LOW_FIRST,
  1155. ip, USE_DEF, 1);
  1156. rhs = COND_EXPR::idLong (&localId, SRC, pIcode, LOW_FIRST,
  1157. ip, eUSE, 1);
  1158. exp = COND_EXPR::boolOp (lhs, rhs, ADD);
  1159. pIcode->setAsgn(lhs, exp);
  1160. (pIcode+1)->invalidate();
  1161. pIcode++;
  1162. ip++;
  1163. }
  1164. pIcode++;
  1165. break;
  1166. case iSAR: /* Idiom 8 */
  1167. if (idiom8 (pIcode, pEnd))
  1168. {
  1169. idx = localId.newLongReg(TYPE_LONG_SIGN,
  1170. pIcode->ic.ll.dst.regi, (pIcode+1)->ic.ll.dst.regi,ip);
  1171. lhs = COND_EXPR::idLongIdx (idx);
  1172. pIcode->setRegDU( (pIcode+1)->ic.ll.dst.regi, USE_DEF);
  1173. rhs = COND_EXPR::idKte (1, 2);
  1174. exp = COND_EXPR::boolOp (lhs, rhs, SHR);
  1175. pIcode->setAsgn(lhs, exp);
  1176. (pIcode+1)->invalidate();
  1177. pIcode++;
  1178. ip++;
  1179. }
  1180. pIcode++;
  1181. break;
  1182. case iSHL:
  1183. if (idx = idiom15 (pIcode, pEnd)) /* idiom 15 */
  1184. {
  1185. lhs = COND_EXPR::idReg (pIcode->ic.ll.dst.regi,
  1186. pIcode->ic.ll.flg & NO_SRC_B,
  1187. &localId);
  1188. rhs = COND_EXPR::idKte (idx, 2);
  1189. exp = COND_EXPR::boolOp (lhs, rhs, SHL);
  1190. pIcode->setAsgn(lhs, exp);
  1191. pIcode++;
  1192. for (idx-- ; idx > 0; idx--)
  1193. {
  1194. (pIcode++)->invalidate();
  1195. ip++;
  1196. }
  1197. }
  1198. else if (idiom12 (pIcode, pEnd)) /* idiom 12 */
  1199. {
  1200. idx = localId.newLongReg (TYPE_LONG_UNSIGN,
  1201. (pIcode+1)->ic.ll.dst.regi, pIcode->ic.ll.dst.regi,ip);
  1202. lhs = COND_EXPR::idLongIdx (idx);
  1203. pIcode->setRegDU( (pIcode+1)->ic.ll.dst.regi, USE_DEF);
  1204. rhs = COND_EXPR::idKte (1, 2);
  1205. exp = COND_EXPR::boolOp (lhs, rhs, SHL);
  1206. pIcode->setAsgn(lhs, exp);
  1207. (pIcode+1)->invalidate();
  1208. pIcode += 2;
  1209. ip++;
  1210. }
  1211. else
  1212. pIcode++;
  1213. break;
  1214. case iSHR: /* Idiom 9 */
  1215. if (idiom9 (pIcode, pEnd))
  1216. {
  1217. idx = localId.newLongReg (TYPE_LONG_UNSIGN,
  1218. pIcode->ic.ll.dst.regi, (pIcode+1)->ic.ll.dst.regi,ip);
  1219. lhs = COND_EXPR::idLongIdx (idx);
  1220. pIcode->setRegDU( (pIcode+1)->ic.ll.dst.regi, USE_DEF);
  1221. rhs = COND_EXPR::idKte (1, 2);
  1222. exp = COND_EXPR::boolOp (lhs, rhs, SHR);
  1223. pIcode->setAsgn(lhs, exp);
  1224. (pIcode+1)->invalidate();
  1225. pIcode++;
  1226. ip++;
  1227. }
  1228. pIcode++;
  1229. break;
  1230. case iSUB: /* Idiom 6 */
  1231. if (idiom6 (pIcode, pEnd))
  1232. {
  1233. lhs = COND_EXPR::idLong (&localId, DST, pIcode, LOW_FIRST, ip, USE_DEF, 1);
  1234. rhs = COND_EXPR::idLong (&localId, SRC, pIcode, LOW_FIRST, ip, eUSE, 1);
  1235. exp = COND_EXPR::boolOp (lhs, rhs, SUB);
  1236. pIcode->setAsgn(lhs, exp);
  1237. (pIcode+1)->invalidate();
  1238. pIcode++;
  1239. ip++;
  1240. }
  1241. pIcode++;
  1242. break;
  1243. case iOR: /* Idiom 10 */
  1244. idiom10 (pIcode, pEnd);
  1245. pIcode++;
  1246. break;
  1247. case iNEG: /* Idiom 11 */
  1248. if (idiom11 (pIcode, pEnd))
  1249. {
  1250. lhs = COND_EXPR::idLong (&localId, DST, pIcode, HIGH_FIRST,
  1251. ip, USE_DEF, 1);
  1252. rhs = COND_EXPR::unary (NEGATION, lhs);
  1253. pIcode->setAsgn(lhs, rhs);
  1254. (pIcode+1)->invalidate();
  1255. (pIcode+2)->invalidate();
  1256. pIcode += 3;
  1257. ip += 2;
  1258. }
  1259. else if (idiom16 (pIcode, pEnd))
  1260. {
  1261. lhs = COND_EXPR::idReg (pIcode->ic.ll.dst.regi, pIcode->ic.ll.flg,
  1262. &localId);
  1263. rhs = lhs->clone();
  1264. rhs = COND_EXPR::unary (NEGATION, lhs);
  1265. pIcode->setAsgn(lhs, rhs);
  1266. (pIcode+1)->invalidate();
  1267. (pIcode+2)->invalidate();
  1268. pIcode += 3;
  1269. ip += 2;
  1270. }
  1271. else
  1272. pIcode++;
  1273. break;
  1274. case iNOP:
  1275. (pIcode++)->invalidate();
  1276. break;
  1277. case iENTER: /* ENTER is equivalent to init PUSH bp */
  1278. if (ip == 0)
  1279. flg |= (PROC_HLL | PROC_IS_HLL);
  1280. pIcode++;
  1281. break;
  1282. case iXOR: /* Idiom 7 */
  1283. if (idiom21 (pIcode, pEnd))
  1284. {
  1285. lhs = COND_EXPR::idLong (&localId, DST, pIcode,HIGH_FIRST, ip, eDEF, 1);
  1286. rhs = COND_EXPR::idKte ((pIcode+1)->ic.ll.src.op() , 4);
  1287. pIcode->setAsgn(lhs, rhs);
  1288. pIcode->du.use = 0; /* clear register used in iXOR */
  1289. (pIcode+1)->invalidate();
  1290. pIcode++;
  1291. ip++;
  1292. }
  1293. else if (idiom7 (pIcode))
  1294. {
  1295. lhs = COND_EXPR::id (*pIcode, DST, this, ip, *pIcode, NONE);
  1296. rhs = COND_EXPR::idKte (0, 2);
  1297. pIcode->setAsgn(lhs, rhs);
  1298. pIcode->du.use = 0; /* clear register used in iXOR */
  1299. pIcode->ic.ll.flg |= I;
  1300. }
  1301. pIcode++;
  1302. break;
  1303. default:
  1304. pIcode++;
  1305. }
  1306. ip++;
  1307. }
  1308. /* Check if number of parameter bytes match their calling convention */
  1309. if ((flg & PROC_HLL) && (!args.sym.empty()))
  1310. {
  1311. args.minOff += (flg & PROC_FAR ? 4 : 2);
  1312. delta = args.maxOff - args.minOff;
  1313. if (cbParam != delta)
  1314. {
  1315. cbParam = delta;
  1316. flg |= (CALL_MASK & CALL_UNKNOWN);
  1317. }
  1318. }
  1319. }
  1320. /* Sets up the TARGET flag for jump target addresses, and
  1321. * binds jump target addresses to icode offsets. */
  1322. void Function::bindIcodeOff()
  1323. {
  1324. Int i; /* idx into icode array */
  1325. iICODE pIcode; /* ptr icode array */
  1326. dword *p,j; /* for case table */
  1327. if (Icode.empty()) /* No Icode */
  1328. return;
  1329. pIcode = Icode.begin();
  1330. /* Flag all jump targets for BB construction and disassembly stage 2 */
  1331. for (i = 0; i < Icode.size(); i++)
  1332. if ((pIcode[i].ic.ll.flg & I) && JmpInst(pIcode[i].ic.ll.opcode))
  1333. {
  1334. if (Icode.labelSrch(pIcode[i].ic.ll.src.op(), j))
  1335. {
  1336. pIcode[j].ic.ll.flg |= TARGET;
  1337. }
  1338. }
  1339. /* Finally bind jump targets to Icode offsets. Jumps for which no label
  1340. * is found (no code at dest. of jump) are simply left unlinked and
  1341. * flagged as going nowhere. */
  1342. for (pIcode = Icode.begin(); pIcode!= Icode.end(); pIcode++)
  1343. if (JmpInst(pIcode->ic.ll.opcode))
  1344. {
  1345. if (pIcode->ic.ll.flg & I)
  1346. {
  1347. dword found;
  1348. if (! Icode.labelSrch(pIcode->ic.ll.src.op(), found))
  1349. {
  1350. pIcode->ic.ll.flg |= NO_LABEL;
  1351. }
  1352. else
  1353. pIcode->ic.ll.src.SetImmediateOp(found);
  1354. }
  1355. else if (pIcode->ic.ll.flg & SWITCH)
  1356. {
  1357. p = pIcode->ic.ll.caseTbl.entries;
  1358. for (j = 0; j < pIcode->ic.ll.caseTbl.numEntries; j++, p++)
  1359. Icode.labelSrch(*p, *p);
  1360. }
  1361. }
  1362. }
  1363. /* Performs idioms analysis, and propagates long operands, if any */
  1364. void Function::lowLevelAnalysis ()
  1365. {
  1366. /* Idiom analysis - sets up some flags and creates some HIGH_LEVEL icodes */
  1367. findIdioms();
  1368. /* Propagate HIGH_LEVEL idiom information for long operands */
  1369. propLong();
  1370. }