idioms.cpp 50 KB

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