idioms.cpp 47 KB

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