idioms.cpp 50 KB

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