idioms.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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 (PICODE pIcode, PICODE pEnd, PPROC 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. else
  65. return 0;
  66. }
  67. /*****************************************************************************
  68. * idiom1 - HLL procedure prologue; Returns number of instructions matched.
  69. * PUSH BP ==> ENTER immed, 0
  70. * MOV BP, SP and sets PROC_HLL flag
  71. * [SUB SP, immed]
  72. * [PUSH SI]
  73. * [PUSH DI]
  74. * - Second version: Push stack variables and then save BP
  75. * PUSH BP
  76. * PUSH SI
  77. * [PUSH DI]
  78. * MOV BP, SP
  79. * - Third version: Stack variables
  80. * [PUSH SI]
  81. * [PUSH DI]
  82. ****************************************************************************/
  83. static Int idiom1(PICODE pIcode, PICODE pEnd, PPROC pProc)
  84. { Int n;
  85. /* PUSH BP as first instruction of procedure */
  86. if ( !(pIcode->ic.ll.flg & I) && pIcode->ic.ll.src.regi == rBP)
  87. {
  88. /* MOV BP, SP as next instruction */
  89. if (++pIcode < pEnd && ! (pIcode->ic.ll.flg & (I | TARGET | CASE))
  90. && pIcode->ic.ll.opcode == iMOV && pIcode->ic.ll.dst.regi == rBP
  91. && pIcode->ic.ll.src.regi == rSP)
  92. {
  93. pProc->args.minOff = 2;
  94. pProc->flg |= PROC_IS_HLL;
  95. /* Look for SUB SP, immed */
  96. if ((++pIcode < pEnd) &&
  97. (pIcode->ic.ll.flg & (I | TARGET | CASE)) == I &&
  98. pIcode->ic.ll.opcode == iSUB && pIcode->ic.ll.dst.regi == rSP)
  99. {
  100. return (3 + checkStkVars (++pIcode, pEnd, pProc));
  101. }
  102. else
  103. return (2 + checkStkVars (pIcode, pEnd, pProc));
  104. }
  105. /* PUSH SI
  106. * [PUSH DI]
  107. * MOV BP, SP */
  108. else
  109. {
  110. n = checkStkVars (pIcode, pEnd, pProc);
  111. if (n > 0)
  112. {
  113. /* Look for MOV BP, SP */
  114. pIcode += n;
  115. if (pIcode < pEnd &&
  116. ! (pIcode->ic.ll.flg & (I | TARGET | CASE)) &&
  117. pIcode->ic.ll.opcode == iMOV &&
  118. pIcode->ic.ll.dst.regi == rBP &&
  119. pIcode->ic.ll.src.regi == rSP)
  120. {
  121. pProc->args.minOff = 2 + (n * 2);
  122. return (2 + n);
  123. }
  124. else return 0; // Cristina: check this please!
  125. }
  126. else return 0; // Cristina: check this please!
  127. }
  128. }
  129. else
  130. return (checkStkVars (pIcode, pEnd, pProc));
  131. }
  132. /*****************************************************************************
  133. * popStkVars - checks for
  134. * [POP DI]
  135. * [POP SI]
  136. * or [POP SI]
  137. * [POP DI]
  138. ****************************************************************************/
  139. static void popStkVars (PICODE pIcode, PICODE pEnd, PPROC pProc)
  140. {
  141. /* Match [POP DI] */
  142. if (pIcode->ic.ll.opcode == iPOP)
  143. if ((pProc->flg & DI_REGVAR) && (pIcode->ic.ll.dst.regi == rDI))
  144. invalidateIcode (pIcode);
  145. else if ((pProc->flg & SI_REGVAR) && (pIcode->ic.ll.dst.regi == rSI))
  146. invalidateIcode (pIcode);
  147. /* Match [POP SI] */
  148. if ((pIcode+1)->ic.ll.opcode == iPOP)
  149. if ((pProc->flg & SI_REGVAR) && ((pIcode+1)->ic.ll.dst.regi == rSI))
  150. invalidateIcode (pIcode+1);
  151. else if ((pProc->flg & DI_REGVAR) && ((pIcode+1)->ic.ll.dst.regi == rDI))
  152. invalidateIcode (pIcode+1);
  153. }
  154. /*****************************************************************************
  155. * idiom2 - HLL procedure epilogue; Returns number of instructions matched.
  156. * [POP DI]
  157. * [POP SI]
  158. * MOV SP, BP
  159. * POP BP
  160. * RET(F)
  161. *****************************************************************************/
  162. static Int idiom2(PICODE pIcode, PICODE pEnd, Int ip, PPROC pProc)
  163. { PICODE nicode;
  164. /* Match MOV SP, BP */
  165. if (ip != 0 && ((pIcode->ic.ll.flg & I) != I) &&
  166. pIcode->ic.ll.dst.regi == rSP && pIcode->ic.ll.src.regi == rBP)
  167. {
  168. /* Get next icode, skip over holes in the icode array */
  169. nicode = pIcode + 1;
  170. while (nicode->ic.ll.flg & NO_CODE)
  171. nicode++;
  172. /* Match POP BP */
  173. if (nicode < pEnd &&
  174. ! (nicode->ic.ll.flg & (I | TARGET | CASE)) &&
  175. nicode->ic.ll.opcode == iPOP &&
  176. nicode->ic.ll.dst.regi == rBP)
  177. {
  178. nicode++;
  179. /* Match RET(F) */
  180. if (nicode < pEnd &&
  181. ! (nicode->ic.ll.flg & (I | TARGET | CASE)) &&
  182. (nicode->ic.ll.opcode == iRET ||
  183. nicode->ic.ll.opcode == iRETF))
  184. {
  185. popStkVars (pIcode-2, pEnd, pProc);
  186. return 2;
  187. }
  188. }
  189. }
  190. return 0;
  191. }
  192. /*****************************************************************************
  193. * idiom3 - C calling convention.
  194. * CALL(F) proc_X
  195. * ADD SP, immed
  196. * Eg: CALL proc_X
  197. * ADD SP, 6
  198. * => pProc->cbParam = immed
  199. * Special case: when the call is at the end of the procedure,
  200. * sometimes the stack gets restored by a MOV sp, bp.
  201. * Need to flag the procedure in these cases.
  202. * Used by compilers to restore the stack when invoking a procedure using
  203. * the C calling convention.
  204. ****************************************************************************/
  205. static Int idiom3(PICODE pIcode, PICODE pEnd)
  206. {
  207. /* Match ADD SP, immed */
  208. if ((++pIcode < pEnd) && (pIcode->ic.ll.flg & I) &&
  209. (pIcode->ic.ll.opcode == iADD) && (pIcode->ic.ll.dst.regi == rSP))
  210. return (pIcode->ic.ll.immed.op);
  211. else if ((pIcode->ic.ll.opcode == iMOV) && (pIcode->ic.ll.dst.regi == rSP)
  212. && (pIcode->ic.ll.src.regi == rBP))
  213. (pIcode-1)->ic.ll.flg |= REST_STK;
  214. return 0;
  215. }
  216. /*****************************************************************************
  217. * idiom 17 - C calling convention.
  218. * CALL(F) xxxx
  219. * POP reg
  220. * [POP reg] reg in {AX, BX, CX, DX}
  221. * Eg: CALL proc_X
  222. * POP cx
  223. * POP cx (4 bytes of arguments)
  224. * => pProc->cbParam = # pops * 2
  225. * Found in Turbo C when restoring the stack for a procedure that uses the
  226. * C calling convention. Used to restore the stack of 2 or 4 bytes args.
  227. ****************************************************************************/
  228. static Int idiom17 (PICODE pIcode, PICODE pEnd)
  229. { Int i = 0; /* Count on # pops */
  230. byte regi;
  231. /* Match POP reg */
  232. if ((++pIcode < pEnd) && (pIcode->ic.ll.opcode == iPOP))
  233. {
  234. regi = pIcode->ic.ll.dst.regi;
  235. if ((regi >= rAX) && (regi <= rBX))
  236. i++;
  237. while ((++pIcode)->ic.ll.opcode == iPOP)
  238. {
  239. if (pIcode->ic.ll.dst.regi == regi)
  240. i++;
  241. else
  242. break;
  243. }
  244. return (i * 2);
  245. }
  246. return (0);
  247. }
  248. /*****************************************************************************
  249. * idiom4 - Pascal calling convention.
  250. * RET(F) immed
  251. * ==> pProc->cbParam = immed
  252. * sets CALL_PASCAL flag
  253. * - Second version: check for optional pop of stack vars
  254. * [POP DI]
  255. * [POP SI]
  256. * POP BP
  257. * RET(F) [immed]
  258. * - Third version: pop stack vars
  259. * [POP DI]
  260. * [POP SI]
  261. * RET(F) [immed]
  262. ****************************************************************************/
  263. static void idiom4 (PICODE pIcode, PICODE pEnd, PPROC pProc)
  264. {
  265. /* Check for [POP DI]
  266. * [POP SI] */
  267. popStkVars (pIcode-3, pEnd, pProc);
  268. /* Check for POP BP */
  269. if (((pIcode-1)->ic.ll.opcode == iPOP) &&
  270. (((pIcode-1)->ic.ll.flg & I) != I) &&
  271. ((pIcode-1)->ic.ll.dst.regi == rBP))
  272. invalidateIcode (pIcode-1);
  273. else
  274. popStkVars (pIcode-2, pEnd, pProc);
  275. /* Check for RET(F) immed */
  276. if (pIcode->ic.ll.flg & I)
  277. {
  278. pProc->cbParam = (int16)pIcode->ic.ll.immed.op;
  279. pProc->flg |= CALL_PASCAL;
  280. }
  281. }
  282. /*****************************************************************************
  283. * idiom5 - Long addition.
  284. * ADD reg/stackOff, reg/stackOff
  285. * ADC reg/stackOff, reg/stackOff
  286. * Eg: ADD ax, [bp-4]
  287. * ADC dx, [bp-2]
  288. * => dx:ax = dx:ax + [bp-2]:[bp-4]
  289. * Found in Borland Turbo C code.
  290. * Commonly used idiom for long addition.
  291. ****************************************************************************/
  292. static boolT idiom5 (PICODE pIcode, PICODE pEnd)
  293. {
  294. if (pIcode < pEnd)
  295. if ((pIcode+1)->ic.ll.opcode == iADC)
  296. return (TRUE);
  297. return (FALSE);
  298. }
  299. /*****************************************************************************
  300. * idiom6 - Long substraction.
  301. * SUB reg/stackOff, reg/stackOff
  302. * SBB reg/stackOff, reg/stackOff
  303. * Eg: SUB ax, [bp-4]
  304. * SBB dx, [bp-2]
  305. * => dx:ax = dx:ax - [bp-2]:[bp-4]
  306. * Found in Borland Turbo C code.
  307. * Commonly used idiom for long substraction.
  308. ****************************************************************************/
  309. static boolT idiom6 (PICODE pIcode, PICODE pEnd)
  310. {
  311. if (pIcode < pEnd)
  312. if ((pIcode+1)->ic.ll.opcode == iSBB)
  313. return (TRUE);
  314. return (FALSE);
  315. }
  316. /*****************************************************************************
  317. * idiom7 - Assign zero
  318. * XOR reg/stackOff, reg/stackOff
  319. * Eg: XOR ax, ax
  320. * => ax = 0
  321. * Found in Borland Turbo C and Microsoft C code.
  322. ****************************************************************************/
  323. static boolT idiom7 (PICODE pIcode)
  324. { PMEM 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 (PICODE picode, PICODE pend)
  360. { PMEM 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 (PICODE pIcode, PICODE 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 (PICODE picode, PICODE 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 (PICODE pIcode, PICODE 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 (PICODE pIcode, PICODE 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 (PICODE pIcode, PICODE 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 (PICODE pIcode, PICODE 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 (PICODE picode, PICODE 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 (PICODE picode, PICODE 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 (PICODE pIcode, PICODE pEnd)
  630. { condId type; /* type of argument */
  631. if ((pIcode + 2) < pEnd)
  632. {
  633. type = idType (pIcode, 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 (PICODE picode, PICODE 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 (PICODE picode, PICODE pend, PPROC 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 (PICODE picode, PICODE pend, PPROC 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 (PICODE picode, PICODE pend, PPROC 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 findIdioms (PPROC pProc)
  865. { Int ip; /* Index to current icode */
  866. PICODE pEnd, pIcode; /* Pointers to end of BB and current icodes */
  867. int16 delta;
  868. COND_EXPR *rhs, *lhs; /* Pointers to left and right hand side exps */
  869. COND_EXPR *exp; /* Pointer to temporal expression */
  870. Int idx; /* Index into local identifier table */
  871. byte regH, regL; /* High and low registers for long word reg */
  872. pIcode = pProc->Icode.GetFirstIcode();
  873. pEnd = pIcode + pProc->Icode.GetNumIcodes();
  874. ip = 0;
  875. while (pIcode < pEnd)
  876. {
  877. switch (pIcode->ic.ll.opcode) {
  878. case iDEC: case iINC:
  879. if (idiom18 (pIcode, pEnd, pProc))
  880. {
  881. lhs = idCondExp (pIcode-1, SRC, pProc, ip, pIcode, E_USE);
  882. if (pIcode->ic.ll.opcode == iDEC)
  883. lhs = unaryCondExp (POST_DEC, lhs);
  884. else
  885. lhs = unaryCondExp (POST_INC, lhs);
  886. rhs = idCondExp (pIcode+1, SRC, pProc, ip, pIcode+2, E_USE);
  887. exp = boolCondExp (lhs, rhs,
  888. condOpJCond[(pIcode+2)->ic.ll.opcode - iJB]);
  889. newJCondHlIcode (pIcode+2, exp);
  890. invalidateIcode (pIcode-1);
  891. invalidateIcode (pIcode);
  892. invalidateIcode (pIcode+1);
  893. pIcode += 3;
  894. ip += 2;
  895. }
  896. else if (idiom19 (pIcode, pEnd, pProc))
  897. {
  898. lhs = idCondExp (pIcode, DST, pProc, ip, pIcode+1, E_USE);
  899. if (pIcode->ic.ll.opcode == iDEC)
  900. lhs = unaryCondExp (PRE_DEC, lhs);
  901. else
  902. lhs = unaryCondExp (PRE_INC, lhs);
  903. rhs = idCondExpKte (0, 2);
  904. exp = boolCondExp (lhs, rhs,
  905. condOpJCond[(pIcode+1)->ic.ll.opcode - iJB]);
  906. newJCondHlIcode (pIcode+1, exp);
  907. invalidateIcode (pIcode);
  908. pIcode += 2;
  909. ip++;
  910. }
  911. else if (idiom20 (pIcode, pEnd, pProc))
  912. {
  913. lhs = idCondExp (pIcode+1, SRC, pProc, ip, pIcode, E_USE);
  914. if (pIcode->ic.ll.opcode == iDEC)
  915. lhs = unaryCondExp (PRE_DEC, lhs);
  916. else
  917. lhs = unaryCondExp (PRE_INC, lhs);
  918. rhs = idCondExp (pIcode+2, SRC, pProc, ip, pIcode+3, E_USE);
  919. exp = boolCondExp (lhs, rhs,
  920. condOpJCond[(pIcode+3)->ic.ll.opcode - iJB]);
  921. newJCondHlIcode (pIcode+3, exp);
  922. invalidateIcode (pIcode);
  923. invalidateIcode (pIcode+1);
  924. invalidateIcode (pIcode+2);
  925. pIcode += 3;
  926. ip += 2;
  927. }
  928. else
  929. pIcode++;
  930. break;
  931. case iPUSH: /* Idiom 1 */
  932. if ((! (pProc->flg & PROC_HLL)) &&
  933. (idx = idiom1 (pIcode, pEnd, pProc)))
  934. {
  935. pProc->flg |= PROC_HLL;
  936. for ( ; idx > 0; idx--)
  937. {
  938. invalidateIcode (pIcode++);
  939. ip++;
  940. }
  941. ip--;
  942. }
  943. else
  944. pIcode++;
  945. break;
  946. case iMOV: /* Idiom 2 */
  947. if (idx = idiom2(pIcode, pEnd, ip, pProc))
  948. {
  949. invalidateIcode (pIcode);
  950. invalidateIcode (pIcode+1);
  951. pIcode += 3;
  952. ip += 2;
  953. }
  954. else if (idiom14 (pIcode, pEnd, &regL, &regH)) /* Idiom 14 */
  955. {
  956. idx = newLongRegId (&pProc->localId, TYPE_LONG_SIGN,
  957. regH, regL, ip);
  958. lhs = idCondExpLongIdx (idx);
  959. setRegDU (pIcode, regH, E_DEF);
  960. rhs = idCondExp (pIcode, SRC, pProc, ip, pIcode, NONE);
  961. newAsgnHlIcode (pIcode, lhs, rhs);
  962. invalidateIcode (pIcode+1);
  963. pIcode += 2;
  964. ip++;
  965. }
  966. else if (idx = idiom13 (pIcode, pEnd)) /* Idiom 13 */
  967. {
  968. lhs = idCondExpReg (idx, 0, &pProc->localId);
  969. setRegDU (pIcode, idx, E_DEF);
  970. pIcode->du1.numRegsDef--; /* prev byte reg def */
  971. rhs = idCondExp (pIcode, SRC, pProc, ip, pIcode, NONE);
  972. newAsgnHlIcode (pIcode, lhs, rhs);
  973. invalidateIcode (pIcode+1);
  974. pIcode += 2;
  975. ip++;
  976. }
  977. else
  978. pIcode++;
  979. break;
  980. case iCALL: case iCALLF:
  981. /* Check for library functions that return a long register.
  982. * Propagate this result */
  983. if (pIcode->ic.ll.immed.proc.proc != 0)
  984. if ((pIcode->ic.ll.immed.proc.proc->flg & PROC_ISLIB) &&
  985. (pIcode->ic.ll.immed.proc.proc->flg & PROC_IS_FUNC))
  986. {
  987. if ((pIcode->ic.ll.immed.proc.proc->retVal.type==TYPE_LONG_SIGN)
  988. || (pIcode->ic.ll.immed.proc.proc->retVal.type ==
  989. TYPE_LONG_UNSIGN))
  990. newLongRegId(&pProc->localId, TYPE_LONG_SIGN, rDX, rAX, ip);
  991. }
  992. /* Check for idioms */
  993. if (idx = idiom3(pIcode, pEnd)) /* idiom 3 */
  994. {
  995. if (pIcode->ic.ll.flg & I)
  996. {
  997. (pIcode->ic.ll.immed.proc.proc)->cbParam = (int16)idx;
  998. pIcode->ic.ll.immed.proc.cb = idx;
  999. (pIcode->ic.ll.immed.proc.proc)->flg |= CALL_C;
  1000. pIcode++;
  1001. invalidateIcode (pIcode++);
  1002. ip++;
  1003. }
  1004. }
  1005. else if (idx = idiom17 (pIcode, pEnd)) /* idiom 17 */
  1006. {
  1007. if (pIcode->ic.ll.flg & I)
  1008. {
  1009. (pIcode->ic.ll.immed.proc.proc)->cbParam = (int16)idx;
  1010. pIcode->ic.ll.immed.proc.cb = idx;
  1011. (pIcode->ic.ll.immed.proc.proc)->flg |= CALL_C;
  1012. ip += idx/2 - 1;
  1013. pIcode++;
  1014. for (idx /= 2; idx > 0; idx--)
  1015. invalidateIcode (pIcode++);
  1016. }
  1017. }
  1018. else
  1019. pIcode++;
  1020. break;
  1021. case iRET: /* Idiom 4 */
  1022. case iRETF:
  1023. idiom4 (pIcode, pEnd, pProc);
  1024. pIcode++;
  1025. break;
  1026. case iADD: /* Idiom 5 */
  1027. if (idiom5 (pIcode, pEnd))
  1028. {
  1029. lhs = idCondExpLong (&pProc->localId, DST, pIcode, LOW_FIRST,
  1030. ip, USE_DEF, 1);
  1031. rhs = idCondExpLong (&pProc->localId, SRC, pIcode, LOW_FIRST,
  1032. ip, E_USE, 1);
  1033. exp = boolCondExp (lhs, rhs, ADD);
  1034. newAsgnHlIcode (pIcode, lhs, exp);
  1035. invalidateIcode (pIcode + 1);
  1036. pIcode++;
  1037. ip++;
  1038. }
  1039. pIcode++;
  1040. break;
  1041. case iSAR: /* Idiom 8 */
  1042. if (idiom8 (pIcode, pEnd))
  1043. {
  1044. idx = newLongRegId (&pProc->localId, TYPE_LONG_SIGN,
  1045. pIcode->ic.ll.dst.regi, (pIcode+1)->ic.ll.dst.regi,ip);
  1046. lhs = idCondExpLongIdx (idx);
  1047. setRegDU (pIcode, (pIcode+1)->ic.ll.dst.regi, USE_DEF);
  1048. rhs = idCondExpKte (1, 2);
  1049. exp = boolCondExp (lhs, rhs, SHR);
  1050. newAsgnHlIcode (pIcode, lhs, exp);
  1051. invalidateIcode (pIcode + 1);
  1052. pIcode++;
  1053. ip++;
  1054. }
  1055. pIcode++;
  1056. break;
  1057. case iSHL:
  1058. if (idx = idiom15 (pIcode, pEnd)) /* idiom 15 */
  1059. {
  1060. lhs = idCondExpReg (pIcode->ic.ll.dst.regi,
  1061. pIcode->ic.ll.flg & NO_SRC_B,
  1062. &pProc->localId);
  1063. rhs = idCondExpKte (idx, 2);
  1064. exp = boolCondExp (lhs, rhs, SHL);
  1065. newAsgnHlIcode (pIcode, lhs, exp);
  1066. pIcode++;
  1067. for (idx-- ; idx > 0; idx--)
  1068. {
  1069. invalidateIcode (pIcode++);
  1070. ip++;
  1071. }
  1072. }
  1073. else if (idiom12 (pIcode, pEnd)) /* idiom 12 */
  1074. {
  1075. idx = newLongRegId (&pProc->localId, TYPE_LONG_UNSIGN,
  1076. (pIcode+1)->ic.ll.dst.regi, pIcode->ic.ll.dst.regi,ip);
  1077. lhs = idCondExpLongIdx (idx);
  1078. setRegDU (pIcode, (pIcode+1)->ic.ll.dst.regi, USE_DEF);
  1079. rhs = idCondExpKte (1, 2);
  1080. exp = boolCondExp (lhs, rhs, SHL);
  1081. newAsgnHlIcode (pIcode, lhs, exp);
  1082. invalidateIcode (pIcode + 1);
  1083. pIcode += 2;
  1084. ip++;
  1085. }
  1086. else
  1087. pIcode++;
  1088. break;
  1089. case iSHR: /* Idiom 9 */
  1090. if (idiom9 (pIcode, pEnd))
  1091. {
  1092. idx = newLongRegId (&pProc->localId, TYPE_LONG_UNSIGN,
  1093. pIcode->ic.ll.dst.regi, (pIcode+1)->ic.ll.dst.regi,ip);
  1094. lhs = idCondExpLongIdx (idx);
  1095. setRegDU (pIcode, (pIcode+1)->ic.ll.dst.regi, USE_DEF);
  1096. rhs = idCondExpKte (1, 2);
  1097. exp = boolCondExp (lhs, rhs, SHR);
  1098. newAsgnHlIcode (pIcode, lhs, exp);
  1099. invalidateIcode (pIcode + 1);
  1100. pIcode++;
  1101. ip++;
  1102. }
  1103. pIcode++;
  1104. break;
  1105. case iSUB: /* Idiom 6 */
  1106. if (idiom6 (pIcode, pEnd))
  1107. {
  1108. lhs = idCondExpLong (&pProc->localId, DST, pIcode, LOW_FIRST,
  1109. ip, USE_DEF, 1);
  1110. rhs = idCondExpLong (&pProc->localId, SRC, pIcode, LOW_FIRST,
  1111. ip, E_USE, 1);
  1112. exp = boolCondExp (lhs, rhs, SUB);
  1113. newAsgnHlIcode (pIcode, lhs, exp);
  1114. invalidateIcode (pIcode + 1);
  1115. pIcode++;
  1116. ip++;
  1117. }
  1118. pIcode++;
  1119. break;
  1120. case iOR: /* Idiom 10 */
  1121. idiom10 (pIcode, pEnd);
  1122. pIcode++;
  1123. break;
  1124. case iNEG: /* Idiom 11 */
  1125. if (idiom11 (pIcode, pEnd))
  1126. {
  1127. lhs = idCondExpLong (&pProc->localId, DST, pIcode, HIGH_FIRST,
  1128. ip, USE_DEF, 1);
  1129. rhs = unaryCondExp (NEGATION, lhs);
  1130. newAsgnHlIcode (pIcode, lhs, rhs);
  1131. invalidateIcode (pIcode+1);
  1132. invalidateIcode (pIcode+2);
  1133. pIcode += 3;
  1134. ip += 2;
  1135. }
  1136. else if (idiom16 (pIcode, pEnd))
  1137. {
  1138. lhs = idCondExpReg (pIcode->ic.ll.dst.regi, pIcode->ic.ll.flg,
  1139. &pProc->localId);
  1140. rhs = copyCondExp (lhs);
  1141. rhs = unaryCondExp (NEGATION, lhs);
  1142. newAsgnHlIcode (pIcode, lhs, rhs);
  1143. invalidateIcode (pIcode+1);
  1144. invalidateIcode (pIcode+2);
  1145. pIcode += 3;
  1146. ip += 2;
  1147. }
  1148. else
  1149. pIcode++;
  1150. break;
  1151. case iNOP:
  1152. invalidateIcode (pIcode++);
  1153. break;
  1154. case iENTER: /* ENTER is equivalent to init PUSH bp */
  1155. if (ip == 0)
  1156. pProc->flg |= (PROC_HLL | PROC_IS_HLL);
  1157. pIcode++;
  1158. break;
  1159. case iXOR: /* Idiom 7 */
  1160. if (idiom21 (pIcode, pEnd))
  1161. {
  1162. lhs = idCondExpLong (&pProc->localId, DST, pIcode,
  1163. HIGH_FIRST, ip, E_DEF, 1);
  1164. rhs = idCondExpKte ((pIcode+1)->ic.ll.immed.op , 4);
  1165. newAsgnHlIcode (pIcode, lhs, rhs);
  1166. pIcode->du.use = 0; /* clear register used in iXOR */
  1167. invalidateIcode (pIcode+1);
  1168. pIcode++;
  1169. ip++;
  1170. }
  1171. else if (idiom7 (pIcode))
  1172. {
  1173. lhs = idCondExp (pIcode, DST, pProc, ip, pIcode, NONE);
  1174. rhs = idCondExpKte (0, 2);
  1175. newAsgnHlIcode (pIcode, lhs, rhs);
  1176. pIcode->du.use = 0; /* clear register used in iXOR */
  1177. pIcode->ic.ll.flg |= I;
  1178. }
  1179. pIcode++;
  1180. break;
  1181. default:
  1182. pIcode++;
  1183. }
  1184. ip++;
  1185. }
  1186. /* Check if number of parameter bytes match their calling convention */
  1187. if ((pProc->flg & PROC_HLL) && (pProc->args.csym))
  1188. {
  1189. pProc->args.minOff += (pProc->flg & PROC_FAR ? 4 : 2);
  1190. delta = pProc->args.maxOff - pProc->args.minOff;
  1191. if (pProc->cbParam != delta)
  1192. {
  1193. pProc->cbParam = delta;
  1194. pProc->flg |= (CALL_MASK & CALL_UNKNOWN);
  1195. }
  1196. }
  1197. }
  1198. void bindIcodeOff (PPROC pProc)
  1199. /* Sets up the TARGET flag for jump target addresses, and
  1200. * binds jump target addresses to icode offsets. */
  1201. { Int i, j; /* idx into icode array */
  1202. PICODE pIcode; /* ptr icode array */
  1203. dword *p; /* for case table */
  1204. if (! pProc->Icode.GetNumIcodes()) /* No Icode */
  1205. return;
  1206. pIcode = pProc->Icode.GetFirstIcode();
  1207. /* Flag all jump targets for BB construction and disassembly stage 2 */
  1208. for (i = 0; i < pProc->Icode.GetNumIcodes(); i++)
  1209. if ((pIcode[i].ic.ll.flg & I) && JmpInst(pIcode[i].ic.ll.opcode))
  1210. {
  1211. if (pProc->Icode.labelSrch(pIcode[i].ic.ll.immed.op, &j))
  1212. {
  1213. pIcode[j].ic.ll.flg |= TARGET;
  1214. }
  1215. }
  1216. /* Finally bind jump targets to Icode offsets. Jumps for which no label
  1217. * is found (no code at dest. of jump) are simply left unlinked and
  1218. * flagged as going nowhere. */
  1219. pIcode = pProc->Icode.GetFirstIcode();
  1220. for (i = 0; i < pProc->Icode.GetNumIcodes(); i++)
  1221. if (JmpInst(pIcode[i].ic.ll.opcode))
  1222. {
  1223. if (pIcode[i].ic.ll.flg & I)
  1224. {
  1225. if (! pProc->Icode.labelSrch(pIcode[i].ic.ll.immed.op,
  1226. (Int *)&pIcode[i].ic.ll.immed.op))
  1227. pIcode[i].ic.ll.flg |= NO_LABEL;
  1228. }
  1229. else if (pIcode[i].ic.ll.flg & SWITCH)
  1230. {
  1231. p = pIcode[i].ic.ll.caseTbl.entries;
  1232. for (j = 0; j < pIcode[i].ic.ll.caseTbl.numEntries; j++, p++)
  1233. pProc->Icode.labelSrch(*p, (Int *)p);
  1234. }
  1235. }
  1236. }
  1237. void lowLevelAnalysis (PPROC pProc)
  1238. /* Performs idioms analysis, and propagates long operands, if any */
  1239. {
  1240. /* Idiom analysis - sets up some flags and creates some HIGH_LEVEL
  1241. * icodes */
  1242. findIdioms (pProc);
  1243. /* Propagate HIGH_LEVEL idiom information for long operands */
  1244. propLong (pProc);
  1245. }