idioms.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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.GetFirstIcode();
  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,
  1108. ip, USE_DEF, 1);
  1109. rhs = COND_EXPR::idLong (&localId, SRC, pIcode, LOW_FIRST,
  1110. ip, eUSE, 1);
  1111. exp = COND_EXPR::boolOp (lhs, rhs, SUB);
  1112. pIcode->setAsgn(lhs, exp);
  1113. (pIcode+1)->invalidate();
  1114. pIcode++;
  1115. ip++;
  1116. }
  1117. pIcode++;
  1118. break;
  1119. case iOR: /* Idiom 10 */
  1120. idiom10 (pIcode, pEnd);
  1121. pIcode++;
  1122. break;
  1123. case iNEG: /* Idiom 11 */
  1124. if (idiom11 (pIcode, pEnd))
  1125. {
  1126. lhs = COND_EXPR::idLong (&localId, DST, pIcode, HIGH_FIRST,
  1127. ip, USE_DEF, 1);
  1128. rhs = COND_EXPR::unary (NEGATION, lhs);
  1129. pIcode->setAsgn(lhs, rhs);
  1130. (pIcode+1)->invalidate();
  1131. (pIcode+2)->invalidate();
  1132. pIcode += 3;
  1133. ip += 2;
  1134. }
  1135. else if (idiom16 (pIcode, pEnd))
  1136. {
  1137. lhs = COND_EXPR::idReg (pIcode->ic.ll.dst.regi, pIcode->ic.ll.flg,
  1138. &localId);
  1139. rhs = lhs->clone();
  1140. rhs = COND_EXPR::unary (NEGATION, lhs);
  1141. pIcode->setAsgn(lhs, rhs);
  1142. (pIcode+1)->invalidate();
  1143. (pIcode+2)->invalidate();
  1144. pIcode += 3;
  1145. ip += 2;
  1146. }
  1147. else
  1148. pIcode++;
  1149. break;
  1150. case iNOP:
  1151. (pIcode++)->invalidate();
  1152. break;
  1153. case iENTER: /* ENTER is equivalent to init PUSH bp */
  1154. if (ip == 0)
  1155. flg |= (PROC_HLL | PROC_IS_HLL);
  1156. pIcode++;
  1157. break;
  1158. case iXOR: /* Idiom 7 */
  1159. if (idiom21 (pIcode, pEnd))
  1160. {
  1161. lhs = COND_EXPR::idLong (&localId, DST, pIcode,HIGH_FIRST, ip, eDEF, 1);
  1162. rhs = COND_EXPR::idKte ((pIcode+1)->ic.ll.immed.op , 4);
  1163. pIcode->setAsgn(lhs, rhs);
  1164. pIcode->du.use = 0; /* clear register used in iXOR */
  1165. (pIcode+1)->invalidate();
  1166. pIcode++;
  1167. ip++;
  1168. }
  1169. else if (idiom7 (pIcode))
  1170. {
  1171. lhs = COND_EXPR::id (pIcode, DST, this, ip, pIcode, NONE);
  1172. rhs = COND_EXPR::idKte (0, 2);
  1173. pIcode->setAsgn(lhs, rhs);
  1174. pIcode->du.use = 0; /* clear register used in iXOR */
  1175. pIcode->ic.ll.flg |= I;
  1176. }
  1177. pIcode++;
  1178. break;
  1179. default:
  1180. pIcode++;
  1181. }
  1182. ip++;
  1183. }
  1184. /* Check if number of parameter bytes match their calling convention */
  1185. if ((flg & PROC_HLL) && (!args.sym.empty()))
  1186. {
  1187. args.minOff += (flg & PROC_FAR ? 4 : 2);
  1188. delta = args.maxOff - args.minOff;
  1189. if (cbParam != delta)
  1190. {
  1191. cbParam = delta;
  1192. flg |= (CALL_MASK & CALL_UNKNOWN);
  1193. }
  1194. }
  1195. }
  1196. /* Sets up the TARGET flag for jump target addresses, and
  1197. * binds jump target addresses to icode offsets. */
  1198. void Function::bindIcodeOff()
  1199. {
  1200. Int i, j; /* idx into icode array */
  1201. ICODE * pIcode; /* ptr icode array */
  1202. dword *p; /* for case table */
  1203. if (! Icode.GetNumIcodes()) /* No Icode */
  1204. return;
  1205. pIcode = Icode.GetFirstIcode();
  1206. /* Flag all jump targets for BB construction and disassembly stage 2 */
  1207. for (i = 0; i < Icode.GetNumIcodes(); i++)
  1208. if ((pIcode[i].ic.ll.flg & I) && JmpInst(pIcode[i].ic.ll.opcode))
  1209. {
  1210. if (Icode.labelSrch(pIcode[i].ic.ll.immed.op, &j))
  1211. {
  1212. pIcode[j].ic.ll.flg |= TARGET;
  1213. }
  1214. }
  1215. /* Finally bind jump targets to Icode offsets. Jumps for which no label
  1216. * is found (no code at dest. of jump) are simply left unlinked and
  1217. * flagged as going nowhere. */
  1218. pIcode = Icode.GetFirstIcode();
  1219. for (i = 0; i < Icode.GetNumIcodes(); i++)
  1220. if (JmpInst(pIcode[i].ic.ll.opcode))
  1221. {
  1222. if (pIcode[i].ic.ll.flg & I)
  1223. {
  1224. if (! Icode.labelSrch(pIcode[i].ic.ll.immed.op,
  1225. (Int *)&pIcode[i].ic.ll.immed.op))
  1226. pIcode[i].ic.ll.flg |= NO_LABEL;
  1227. }
  1228. else if (pIcode[i].ic.ll.flg & SWITCH)
  1229. {
  1230. p = pIcode[i].ic.ll.caseTbl.entries;
  1231. for (j = 0; j < pIcode[i].ic.ll.caseTbl.numEntries; j++, p++)
  1232. Icode.labelSrch(*p, (Int *)p);
  1233. }
  1234. }
  1235. }
  1236. /* Performs idioms analysis, and propagates long operands, if any */
  1237. void Function::lowLevelAnalysis ()
  1238. {
  1239. /* Idiom analysis - sets up some flags and creates some HIGH_LEVEL icodes */
  1240. findIdioms();
  1241. /* Propagate HIGH_LEVEL idiom information for long operands */
  1242. propLong();
  1243. }