parser.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. /****************************************************************************
  2. * dcc project procedure list builder
  3. * (C) Cristina Cifuentes, Mike van Emmerik, Jeff Ledermann
  4. ****************************************************************************/
  5. #include "dcc.h"
  6. #include <string.h>
  7. #include <stdlib.h> /* For exit() */
  8. #ifdef __DOSWIN__
  9. #include <stdio.h>
  10. #endif
  11. static void FollowCtrl (Function * pProc, CALL_GRAPH * pcallGraph, STATE * pstate);
  12. static boolT process_JMP (ICODE * pIcode, STATE * pstate,
  13. CALL_GRAPH * pcallGraph);
  14. static boolT process_CALL(ICODE * pIcode, CALL_GRAPH * pcallGraph,
  15. STATE * pstate);
  16. static void process_operands(ICODE * pIcode, Function * pProc, STATE * pstate,
  17. Int ix);
  18. static void setBits(int16 type, dword start, dword len);
  19. static SYM * updateGlobSym(dword operand, Int size, word duFlag);
  20. static void process_MOV(ICODE * pIcode, STATE * pstate);
  21. static SYM * lookupAddr (LLOpcode *pm, STATE * pstate, Int size, word duFlag);
  22. void interactDis(Function * initProc, Int ic);
  23. static dword SynthLab;
  24. /* Parses the program, builds the call graph, and returns the list of
  25. * procedures found */
  26. void parse (CALL_GRAPH * *pcallGraph)
  27. {
  28. STATE state;
  29. /* Set initial state */
  30. memset(&state, 0, sizeof(STATE));
  31. state.setState(rES, 0); /* PSP segment */
  32. state.setState(rDS, 0);
  33. state.setState(rCS, prog.initCS);
  34. state.setState(rSS, prog.initSS);
  35. state.setState(rSP, prog.initSP);
  36. state.IP = ((dword)prog.initCS << 4) + prog.initIP;
  37. SynthLab = SYNTHESIZED_MIN;
  38. /* Check for special settings of initial state, based on idioms of the
  39. startup code */
  40. state.checkStartup();
  41. /* Make a struct for the initial procedure */
  42. // default-construct a Function object !
  43. pProcList.push_back(Function::Create());
  44. if (prog.offMain != -1)
  45. {
  46. /* We know where main() is. Start the flow of control from there */
  47. pProcList.front().procEntry = prog.offMain;
  48. /* In medium and large models, the segment of main may (will?) not be
  49. the same as the initial CS segment (of the startup code) */
  50. state.setState(rCS, prog.segMain);
  51. strcpy(pProcList.front().name, "main");
  52. state.IP = prog.offMain;
  53. }
  54. else
  55. {
  56. /* Create initial procedure at program start address */
  57. strcpy(pProcList.front().name, "start");
  58. pProcList.front().procEntry = (dword)state.IP;
  59. }
  60. /* The state info is for the first procedure */
  61. pProcList.front().state = state;
  62. /* Set up call graph initial node */
  63. *pcallGraph = new CALL_GRAPH;
  64. (*pcallGraph)->proc = pProcList.begin();
  65. /* This proc needs to be called to set things up for LibCheck(), which
  66. checks a proc to see if it is a know C (etc) library */
  67. SetupLibCheck();
  68. /* Recursively build entire procedure list */
  69. pProcList.front().FollowCtrl (*pcallGraph, &state);
  70. /* This proc needs to be called to clean things up from SetupLibCheck() */
  71. CleanupLibCheck();
  72. }
  73. /* Updates the type of the symbol in the symbol table. The size is updated
  74. * if necessary (0 means no update necessary). */
  75. static void updateSymType (dword symbol, hlType symType, Int size)
  76. { Int i;
  77. for (i = 0; i < symtab.size(); i++)
  78. if (symtab[i].label == symbol)
  79. {
  80. symtab[i].type = symType;
  81. if (size != 0)
  82. symtab[i].size = size;
  83. break;
  84. }
  85. }
  86. /* Returns the size of the string pointed by sym and delimited by delim.
  87. * Size includes delimiter. */
  88. Int strSize (byte *sym, char delim)
  89. {
  90. Int i;
  91. for (i = 0; *sym++ != delim; i++) ;
  92. return (i+1);
  93. }
  94. Function *fakeproc=Function::Create(0,0,"fake");
  95. /* FollowCtrl - Given an initial procedure, state information and symbol table
  96. * builds a list of procedures reachable from the initial procedure
  97. * using a depth first search. */
  98. void Function::FollowCtrl(CALL_GRAPH * pcallGraph, STATE *pstate)
  99. {
  100. ICODE _Icode, *pIcode; /* This gets copied to pProc->Icode[] later */
  101. ICODE eIcode; /* extra icodes for iDIV, iIDIV, iXCHG */
  102. SYM * psym;
  103. dword offset;
  104. eErrorId err;
  105. boolT done = FALSE;
  106. dword lab;
  107. if (strstr(name, "chkstk") != NULL)
  108. {
  109. // Danger! Dcc will likely fall over in this code.
  110. // So we act as though we have done with this proc
  111. // pProc->flg &= ~TERMINATES; // Not sure about this
  112. done = TRUE;
  113. // And mark it as a library function, so structure() won't choke on it
  114. flg |= PROC_ISLIB;
  115. return;
  116. }
  117. if (option.VeryVerbose)
  118. {
  119. printf("Parsing proc %s at %lX\n", name, pstate->IP);
  120. }
  121. while (! done && ! (err = scan(pstate->IP, &_Icode)))
  122. {
  123. pstate->IP += (dword)_Icode.ic.ll.numBytes;
  124. setBits(BM_CODE, _Icode.ic.ll.label, (dword)_Icode.ic.ll.numBytes);
  125. process_operands(&_Icode,pstate);
  126. /* Keep track of interesting instruction flags in procedure */
  127. flg |= (_Icode.ic.ll.flg & (NOT_HLL | FLOAT_OP));
  128. /* Check if this instruction has already been parsed */
  129. if (Icode.labelSrch(_Icode.ic.ll.label, lab))
  130. { /* Synthetic jump */
  131. _Icode.type = LOW_LEVEL;
  132. _Icode.ic.ll.opcode = iJMP;
  133. _Icode.ic.ll.flg = I | SYNTHETIC | NO_OPS;
  134. _Icode.ic.ll.src.SetImmediateOp(Icode[lab].GetLlLabel());
  135. _Icode.ic.ll.label = SynthLab++;
  136. }
  137. /* Copy Icode to Proc */
  138. if ((_Icode.ic.ll.opcode == iDIV) || (_Icode.ic.ll.opcode == iIDIV))
  139. {
  140. /* MOV rTMP, reg */
  141. memset (&eIcode, 0, sizeof (ICODE));
  142. eIcode.type = LOW_LEVEL;
  143. eIcode.ic.ll.opcode = iMOV;
  144. eIcode.ic.ll.dst.regi = rTMP;
  145. if (_Icode.ic.ll.flg & B)
  146. {
  147. eIcode.ic.ll.flg |= B;
  148. eIcode.ic.ll.src.regi = rAX;
  149. eIcode.setRegDU( rAX, eUSE);
  150. }
  151. else /* implicit dx:ax */
  152. {
  153. eIcode.ic.ll.flg |= IM_SRC;
  154. eIcode.setRegDU( rAX, eUSE);
  155. eIcode.setRegDU( rDX, eUSE);
  156. }
  157. eIcode.setRegDU( rTMP, eDEF);
  158. eIcode.ic.ll.flg |= SYNTHETIC;
  159. /* eIcode.ic.ll.label = SynthLab++; */
  160. eIcode.ic.ll.label = _Icode.ic.ll.label;
  161. Icode.addIcode(&eIcode);
  162. /* iDIV, iIDIV */
  163. Icode.addIcode(&_Icode);
  164. /* iMOD */
  165. memset (&eIcode, 0, sizeof (ICODE));
  166. eIcode.type = LOW_LEVEL;
  167. eIcode.ic.ll.opcode = iMOD;
  168. eIcode.ic.ll.src = _Icode.ic.ll.src;
  169. eIcode.du = _Icode.du;
  170. eIcode.ic.ll.flg = (_Icode.ic.ll.flg | SYNTHETIC);
  171. eIcode.ic.ll.label = SynthLab++;
  172. pIcode = Icode.addIcode(&eIcode);
  173. }
  174. else if (_Icode.ic.ll.opcode == iXCHG)
  175. {
  176. /* MOV rTMP, regDst */
  177. memset (&eIcode, 0, sizeof (ICODE));
  178. eIcode.type = LOW_LEVEL;
  179. eIcode.ic.ll.opcode = iMOV;
  180. eIcode.ic.ll.dst.regi = rTMP;
  181. eIcode.ic.ll.src.regi = _Icode.ic.ll.dst.regi;
  182. eIcode.setRegDU( rTMP, eDEF);
  183. eIcode.setRegDU( eIcode.ic.ll.src.regi, eUSE);
  184. eIcode.ic.ll.flg |= SYNTHETIC;
  185. /* eIcode.ic.ll.label = SynthLab++; */
  186. eIcode.ic.ll.label = _Icode.ic.ll.label;
  187. Icode.addIcode(&eIcode);
  188. /* MOV regDst, regSrc */
  189. _Icode.ic.ll.opcode = iMOV;
  190. _Icode.ic.ll.flg |= SYNTHETIC;
  191. /* Icode.ic.ll.label = SynthLab++; */
  192. Icode.addIcode(&_Icode);
  193. _Icode.ic.ll.opcode = iXCHG; /* for next case */
  194. /* MOV regSrc, rTMP */
  195. memset (&eIcode, 0, sizeof (ICODE));
  196. eIcode.type = LOW_LEVEL;
  197. eIcode.ic.ll.opcode = iMOV;
  198. eIcode.ic.ll.dst.regi = _Icode.ic.ll.src.regi;
  199. eIcode.ic.ll.src.regi = rTMP;
  200. eIcode.setRegDU( eIcode.ic.ll.dst.regi, eDEF);
  201. eIcode.setRegDU( rTMP, eUSE);
  202. eIcode.ic.ll.flg |= SYNTHETIC;
  203. eIcode.ic.ll.label = SynthLab++;
  204. pIcode = Icode.addIcode(&eIcode);
  205. }
  206. else
  207. pIcode = Icode.addIcode(&_Icode);
  208. switch (_Icode.ic.ll.opcode) {
  209. /*** Conditional jumps ***/
  210. case iLOOP: case iLOOPE: case iLOOPNE:
  211. case iJB: case iJBE: case iJAE: case iJA:
  212. case iJL: case iJLE: case iJGE: case iJG:
  213. case iJE: case iJNE: case iJS: case iJNS:
  214. case iJO: case iJNO: case iJP: case iJNP:
  215. case iJCXZ:
  216. { STATE StCopy;
  217. int ip = Icode.size()-1; /* Index of this jump */
  218. ICODE &prev(Icode.back()); /* Previous icode */
  219. boolT fBranch = FALSE;
  220. pstate->JCond.regi = 0;
  221. /* This sets up range check for indexed JMPs hopefully
  222. * Handles JA/JAE for fall through and JB/JBE on branch
  223. */
  224. if (ip > 0 && prev.ic.ll.opcode == iCMP && (prev.ic.ll.flg & I))
  225. {
  226. pstate->JCond.immed = (int16)prev.ic.ll.src.op();
  227. if (_Icode.ic.ll.opcode == iJA || _Icode.ic.ll.opcode == iJBE)
  228. pstate->JCond.immed++;
  229. if (_Icode.ic.ll.opcode == iJAE || _Icode.ic.ll.opcode == iJA)
  230. pstate->JCond.regi = prev.ic.ll.dst.regi;
  231. fBranch = (boolT)
  232. (_Icode.ic.ll.opcode == iJB || _Icode.ic.ll.opcode == iJBE);
  233. }
  234. StCopy = *pstate;
  235. //memcpy(&StCopy, pstate, sizeof(STATE));
  236. /* Straight line code */
  237. this->FollowCtrl (pcallGraph, &StCopy); // recurrent ?
  238. if (fBranch) /* Do branching code */
  239. {
  240. pstate->JCond.regi = prev.ic.ll.dst.regi;
  241. }
  242. /* Next icode. Note: not the same as GetLastIcode() because of the call
  243. to FollowCtrl() */
  244. pIcode = Icode.GetIcode(ip);
  245. } /* Fall through to do the jump path */
  246. /*** Jumps ***/
  247. case iJMP:
  248. case iJMPF: /* Returns TRUE if we've run into a loop */
  249. done = process_JMP (pIcode, pstate, pcallGraph);
  250. break;
  251. /*** Calls ***/
  252. case iCALL:
  253. case iCALLF:
  254. done = process_CALL (pIcode, pcallGraph, pstate);
  255. break;
  256. /*** Returns ***/
  257. case iRET:
  258. case iRETF:
  259. this->flg |= (_Icode.ic.ll.opcode == iRET)? PROC_NEAR:PROC_FAR;
  260. /* Fall through */
  261. case iIRET:
  262. this->flg &= ~TERMINATES;
  263. done = TRUE;
  264. break;
  265. case iINT:
  266. if (_Icode.ic.ll.src.op() == 0x21 && pstate->f[rAH])
  267. {
  268. Int funcNum = pstate->r[rAH];
  269. Int operand;
  270. Int size;
  271. /* Save function number */
  272. Icode.back().ic.ll.dst.off = (int16)funcNum;
  273. //Icode.GetIcode(Icode.GetNumIcodes() - 1)->
  274. /* Program termination: int21h, fn 00h, 31h, 4Ch */
  275. done = (boolT)(funcNum == 0x00 || funcNum == 0x31 ||
  276. funcNum == 0x4C);
  277. /* String functions: int21h, fn 09h */
  278. if (pstate->f[rDX]) /* offset goes into DX */
  279. if (funcNum == 0x09)
  280. {
  281. operand = ((dword)(word)pstate->r[rDS]<<4) +
  282. (dword)(word)pstate->r[rDX];
  283. size = prog.fCOM ?
  284. strSize (&prog.Image[operand], '$') :
  285. strSize (&prog.Image[operand + 0x100], '$');
  286. updateSymType (operand, TYPE_STR, size);
  287. }
  288. }
  289. else if ((_Icode.ic.ll.src.op() == 0x2F) && (pstate->f[rAH]))
  290. {
  291. Icode.back().ic.ll.dst.off = pstate->r[rAH];
  292. }
  293. else /* Program termination: int20h, int27h */
  294. done = (boolT)(_Icode.ic.ll.src.op() == 0x20 ||
  295. _Icode.ic.ll.src.op() == 0x27);
  296. if (done)
  297. pIcode->ic.ll.flg |= TERMINATES;
  298. break;
  299. case iMOV:
  300. process_MOV(pIcode, pstate);
  301. break;
  302. /* case iXCHG:
  303. process_MOV (pIcode, pstate);
  304. break; **** HERE ***/
  305. case iSHL:
  306. if (pstate->JCond.regi == _Icode.ic.ll.dst.regi)
  307. if ((_Icode.ic.ll.flg & I) && _Icode.ic.ll.src.op() == 1)
  308. pstate->JCond.immed *= 2;
  309. else
  310. pstate->JCond.regi = 0;
  311. break;
  312. case iLEA:
  313. if (_Icode.ic.ll.src.regi == 0) /* direct mem offset */
  314. pstate->setState( _Icode.ic.ll.dst.regi, _Icode.ic.ll.src.off);
  315. break;
  316. case iLDS: case iLES:
  317. if ((psym = lookupAddr(&_Icode.ic.ll.src, pstate, 4, eDuVal::USE))
  318. /* && (Icode.ic.ll.flg & SEG_IMMED) */ ) {
  319. offset = LH(&prog.Image[psym->label]);
  320. pstate->setState( (_Icode.ic.ll.opcode == iLDS)? rDS: rES,
  321. LH(&prog.Image[psym->label + 2]));
  322. pstate->setState( _Icode.ic.ll.dst.regi, (int16)offset);
  323. psym->type = TYPE_PTR;
  324. }
  325. break;
  326. }
  327. }
  328. if (err) {
  329. this->flg &= ~TERMINATES;
  330. if (err == INVALID_386OP || err == INVALID_OPCODE)
  331. {
  332. fatalError(err, prog.Image[_Icode.ic.ll.label], _Icode.ic.ll.label);
  333. this->flg |= PROC_BADINST;
  334. }
  335. else if (err == IP_OUT_OF_RANGE)
  336. fatalError (err, _Icode.ic.ll.label);
  337. else
  338. reportError(err, _Icode.ic.ll.label);
  339. }
  340. }
  341. /* process_JMP - Handles JMPs, returns TRUE if we should end recursion */
  342. boolT Function::process_JMP (ICODE * pIcode, STATE *pstate, CALL_GRAPH * pcallGraph)
  343. {
  344. static byte i2r[4] = {rSI, rDI, rBP, rBX};
  345. ICODE _Icode;
  346. dword cs, offTable, endTable;
  347. dword i, k, seg, target;
  348. dword tmp;
  349. if (pIcode->ic.ll.flg & I)
  350. {
  351. if (pIcode->ic.ll.opcode == iJMPF)
  352. pstate->setState( rCS, LH(prog.Image + pIcode->ic.ll.label + 3));
  353. i = pstate->IP = pIcode->ic.ll.src.op();
  354. if ((long)i < 0)
  355. {
  356. exit(1);
  357. }
  358. /* Return TRUE if jump target is already parsed */
  359. return Icode.labelSrch(i, tmp);
  360. }
  361. /* We've got an indirect JMP - look for switch() stmt. idiom of the form
  362. * JMP word ptr word_offset[rBX | rSI | rDI] */
  363. seg = (pIcode->ic.ll.src.seg)? pIcode->ic.ll.src.seg: rDS;
  364. /* Ensure we have a word offset & valid seg */
  365. if (pIcode->ic.ll.opcode == iJMP && (pIcode->ic.ll.flg & WORD_OFF) &&
  366. pstate->f[seg] &&
  367. (pIcode->ic.ll.src.regi == INDEXBASE + 4 ||
  368. pIcode->ic.ll.src.regi == INDEXBASE + 5 || /* Idx reg. BX, SI, DI */
  369. pIcode->ic.ll.src.regi == INDEXBASE + 7))
  370. {
  371. offTable = ((dword)(word)pstate->r[seg] << 4) + pIcode->ic.ll.src.off;
  372. /* Firstly look for a leading range check of the form:-
  373. * CMP {BX | SI | DI}, immed
  374. * JA | JAE | JB | JBE
  375. * This is stored in the current state as if we had just
  376. * followed a JBE branch (i.e. [reg] lies between 0 - immed).
  377. */
  378. if (pstate->JCond.regi == i2r[pIcode->ic.ll.src.regi-(INDEXBASE+4)])
  379. endTable = offTable + pstate->JCond.immed;
  380. else
  381. endTable = (dword)prog.cbImage;
  382. /* Search for first byte flagged after start of table */
  383. for (i = offTable; i <= endTable; i++)
  384. if (BITMAP(i, BM_CODE | BM_DATA))
  385. break;
  386. endTable = i & ~1; /* Max. possible table size */
  387. /* Now do some heuristic pruning. Look for ptrs. into the table
  388. * and for addresses that don't appear to point to valid code.
  389. */
  390. cs = (dword)(word)pstate->r[rCS] << 4;
  391. for (i = offTable; i < endTable; i += 2)
  392. {
  393. target = cs + LH(&prog.Image[i]);
  394. if (target < endTable && target >= offTable)
  395. endTable = target;
  396. else if (target >= (dword)prog.cbImage)
  397. endTable = i;
  398. }
  399. for (i = offTable; i < endTable; i += 2)
  400. {
  401. target = cs + LH(&prog.Image[i]);
  402. /* Be wary of 00 00 as code - it's probably data */
  403. if (! (prog.Image[target] || prog.Image[target+1]) ||
  404. scan(target, &_Icode))
  405. endTable = i;
  406. }
  407. /* Now for each entry in the table take a copy of the current
  408. * state and recursively call FollowCtrl(). */
  409. if (offTable < endTable)
  410. {
  411. STATE StCopy;
  412. Int ip;
  413. dword *psw;
  414. setBits(BM_DATA, offTable, endTable - offTable);
  415. pIcode->ic.ll.flg |= SWITCH;
  416. pIcode->ic.ll.caseTbl.numEntries = (endTable - offTable) / 2;
  417. psw = (dword*)allocMem(pIcode->ic.ll.caseTbl.numEntries*sizeof(dword));
  418. pIcode->ic.ll.caseTbl.entries = psw;
  419. for (i = offTable, k = 0; i < endTable; i += 2)
  420. {
  421. memcpy(&StCopy, pstate, sizeof(STATE));
  422. StCopy.IP = cs + LH(&prog.Image[i]);
  423. ip = Icode.size();
  424. FollowCtrl (pcallGraph, &StCopy);
  425. Icode.GetIcode(ip)->ic.ll.caseTbl.numEntries = k++;
  426. Icode.GetIcode(ip)->ic.ll.flg |= CASE;
  427. *psw++ = Icode[ip].GetLlLabel();
  428. }
  429. return TRUE;
  430. }
  431. }
  432. /* Can't do anything with this jump */
  433. flg |= PROC_IJMP;
  434. flg &= ~TERMINATES;
  435. interactDis(this, this->Icode.size()-1);
  436. return TRUE;
  437. }
  438. /* Process procedure call.
  439. * Note: We assume that CALL's will return unless there is good evidence to
  440. * the contrary - thus we return FALSE unless all paths in the called
  441. * procedure end in DOS exits. This is reasonable since C procedures
  442. * will always include the epilogue after the call anyway and it's to
  443. * be assumed that if an assembler program contains a CALL that the
  444. * programmer expected it to come back - otherwise surely a JMP would
  445. * have been used. */
  446. boolT Function::process_CALL (ICODE * pIcode, CALL_GRAPH * pcallGraph, STATE *pstate)
  447. {
  448. Int ip = Icode.size() - 1;
  449. STATE localState; /* Local copy of the machine state */
  450. dword off;
  451. boolT indirect;
  452. /* For Indirect Calls, find the function address */
  453. indirect = FALSE;
  454. //pIcode->ic.ll.immed.proc.proc=fakeproc;
  455. if ( not pIcode->isLlFlag(I) )
  456. {
  457. /* Not immediate, i.e. indirect call */
  458. if (pIcode->ic.ll.dst.regi && (!option.Calls))
  459. {
  460. /* We have not set the brave option to attempt to follow
  461. the execution path through register indirect calls.
  462. So we just exit this function, and ignore the call.
  463. We probably should not have parsed this deep, anyway.
  464. */
  465. return FALSE;
  466. }
  467. /* Offset into program image is seg:off of read input */
  468. /* Note: this assumes that the pointer itself is at
  469. es:0 where es:0 is the start of the image. This is
  470. usually wrong! Consider also CALL [BP+0E] in which the
  471. segment for the pointer is in SS! - Mike */
  472. off = (dword)(word)pIcode->ic.ll.dst.off +
  473. ((dword)(word)pIcode->ic.ll.dst.segValue << 4);
  474. /* Address of function is given by 4 (CALLF) or 2 (CALL) bytes at
  475. * previous offset into the program image */
  476. dword tgtAddr=0;
  477. if (pIcode->ic.ll.opcode == iCALLF)
  478. tgtAddr= LH(&prog.Image[off]) + (dword)(LH(&prog.Image[off+2])) << 4;
  479. else
  480. tgtAddr= LH(&prog.Image[off]) + (dword)(word)state.r[rCS] << 4;
  481. pIcode->ic.ll.src.SetImmediateOp( tgtAddr );
  482. pIcode->ic.ll.flg |= I;
  483. indirect = TRUE;
  484. }
  485. /* Process CALL. Function address is located in pIcode->ic.ll.immed.op */
  486. if (pIcode->ic.ll.flg & I)
  487. {
  488. /* Search procedure list for one with appropriate entry point */
  489. ilFunction iter= std::find_if(pProcList.begin(),pProcList.end(),
  490. [pIcode](const Function &f) ->
  491. bool { return f.procEntry==pIcode->ic.ll.src.op(); });
  492. /* Create a new procedure node and save copy of the state */
  493. if (iter==pProcList.end())
  494. {
  495. pProcList.push_back(Function::Create());
  496. Function &x(pProcList.back());
  497. iter = (++pProcList.rbegin()).base();
  498. x.procEntry = pIcode->ic.ll.src.op();
  499. LibCheck(x);
  500. if (x.flg & PROC_ISLIB)
  501. {
  502. /* A library function. No need to do any more to it */
  503. pcallGraph->insertCallGraph (this, iter);
  504. iter = (++pProcList.rbegin()).base();
  505. Icode.GetIcode(ip)->ic.ll.src.proc.proc = &x;
  506. return false;
  507. }
  508. if (indirect)
  509. x.flg |= PROC_ICALL;
  510. if (x.name[0] == '\0') /* Don't overwrite existing name */
  511. {
  512. sprintf(x.name, "proc_%ld", ++prog.cProcs);
  513. }
  514. x.depth = x.depth + 1;
  515. x.flg |= TERMINATES;
  516. /* Save machine state in localState, load up IP and CS.*/
  517. localState = *pstate;
  518. pstate->IP = pIcode->ic.ll.src.op();
  519. if (pIcode->ic.ll.opcode == iCALLF)
  520. pstate->setState( rCS, LH(prog.Image + pIcode->ic.ll.label + 3));
  521. x.state = *pstate;
  522. /* Insert new procedure in call graph */
  523. pcallGraph->insertCallGraph (this, iter);
  524. /* Process new procedure */
  525. x.FollowCtrl (pcallGraph, pstate);
  526. /* Restore segment registers & IP from localState */
  527. pstate->IP = localState.IP;
  528. pstate->setState( rCS, localState.r[rCS]);
  529. pstate->setState( rDS, localState.r[rDS]);
  530. pstate->setState( rES, localState.r[rES]);
  531. pstate->setState( rSS, localState.r[rSS]);
  532. }
  533. else
  534. pcallGraph->insertCallGraph (this, iter);
  535. Icode[ip].ic.ll.src.proc.proc = &(*iter); // ^ target proc
  536. /* return ((p->flg & TERMINATES) != 0); */
  537. return FALSE;
  538. }
  539. return FALSE; // Cristina, please check!!
  540. }
  541. /* process_MOV - Handles state changes due to simple assignments */
  542. static void process_MOV(ICODE * pIcode, STATE * pstate)
  543. {
  544. SYM * psym, *psym2; /* Pointer to symbol in global symbol table */
  545. byte dstReg = pIcode->ic.ll.dst.regi;
  546. byte srcReg = pIcode->ic.ll.src.regi;
  547. if (dstReg > 0 && dstReg < INDEXBASE)
  548. {
  549. if (pIcode->ic.ll.flg & I)
  550. pstate->setState( dstReg, (int16)pIcode->ic.ll.src.op());
  551. else if (srcReg == 0) /* direct memory offset */
  552. {
  553. psym = lookupAddr(&pIcode->ic.ll.src, pstate, 2, eDuVal::USE);
  554. if (psym && ((psym->flg & SEG_IMMED) || psym->duVal.val))
  555. pstate->setState( dstReg, LH(&prog.Image[psym->label]));
  556. }
  557. else if (srcReg < INDEXBASE && pstate->f[srcReg]) /* reg */
  558. {
  559. pstate->setState( dstReg, pstate->r[srcReg]);
  560. /* Follow moves of the possible index register */
  561. if (pstate->JCond.regi == srcReg)
  562. pstate->JCond.regi = dstReg;
  563. }
  564. }
  565. else if (dstReg == 0) { /* direct memory offset */
  566. psym = lookupAddr (&pIcode->ic.ll.dst, pstate, 2, eDEF);
  567. if (psym && ! (psym->duVal.val)) /* no initial value yet */
  568. if (pIcode->ic.ll.flg & I) { /* immediate */
  569. prog.Image[psym->label] = (byte)pIcode->ic.ll.src.op();
  570. prog.Image[psym->label+1] = (byte)(pIcode->ic.ll.src.op()>>8);
  571. psym->duVal.val = 1;
  572. }
  573. else if (srcReg == 0) { /* direct mem offset */
  574. psym2 = lookupAddr (&pIcode->ic.ll.src, pstate, 2, eDuVal::USE);
  575. if (psym2 && ((psym->flg & SEG_IMMED) || (psym->duVal.val)))
  576. {
  577. prog.Image[psym->label] = (byte)prog.Image[psym2->label];
  578. prog.Image[psym->label+1] =
  579. (byte)(prog.Image[psym2->label+1] >> 8);
  580. psym->duVal.val=1;
  581. }
  582. }
  583. else if (srcReg < INDEXBASE && pstate->f[srcReg]) /* reg */
  584. {
  585. prog.Image[psym->label] = (byte)pstate->r[srcReg];
  586. prog.Image[psym->label+1] = (byte)(pstate->r[srcReg] >> 8);
  587. psym->duVal.val;
  588. }
  589. }
  590. }
  591. /* Type of the symbol according to the number of bytes it uses */
  592. static hlType cbType[] = {TYPE_UNKNOWN, TYPE_BYTE_UNSIGN, TYPE_WORD_SIGN,
  593. TYPE_UNKNOWN, TYPE_LONG_SIGN};
  594. /* Creates an entry in the global symbol table (symtab) if the variable
  595. * is not there yet. If it is part of the symtab, the size of the variable
  596. * is checked and updated if the old size was less than the new size (ie.
  597. * the maximum size is always saved). */
  598. static SYM * updateGlobSym (dword operand, Int size, word duFlag)
  599. {
  600. Int i;
  601. /* Check for symbol in symbol table */
  602. for (i = 0; i < symtab.size(); i++)
  603. if (symtab[i].label == operand)
  604. {
  605. if (symtab[i].size < size)
  606. symtab[i].size = size;
  607. break;
  608. }
  609. /* New symbol, not in symbol table */
  610. if (i == symtab.size())
  611. {
  612. SYM v;
  613. sprintf (v.name, "var%05lX", operand);
  614. v.label = operand;
  615. v.size = size;
  616. v.type = cbType[size];
  617. if (duFlag == eDuVal::USE) /* must already have init value */
  618. {
  619. v.duVal.use =1; // USEVAL;
  620. v.duVal.val =1;
  621. }
  622. else
  623. {
  624. v.duVal.setFlags(duFlag);
  625. }
  626. symtab.push_back(v);
  627. }
  628. return (&symtab[i]);
  629. }
  630. /* Updates the offset entry to the stack frame table (arguments),
  631. * and returns a pointer to such entry. */
  632. static void updateFrameOff (STKFRAME * ps, int16 off, Int size, word duFlag)
  633. {
  634. Int i;
  635. /* Check for symbol in stack frame table */
  636. for (i = 0; i < ps->sym.size(); i++)
  637. {
  638. if (ps->sym[i].off == off)
  639. {
  640. if (ps->sym[i].size < size)
  641. {
  642. ps->sym[i].size = size;
  643. }
  644. break;
  645. }
  646. }
  647. /* New symbol, not in table */
  648. if (i == ps->sym.size())
  649. {
  650. STKSYM new_sym;
  651. sprintf (new_sym.name, "arg%ld", i);
  652. new_sym.off = off;
  653. new_sym.size = size;
  654. new_sym.type = cbType[size];
  655. if (duFlag == eDuVal::USE) /* must already have init value */
  656. {
  657. new_sym.duVal.use=1;
  658. //new_sym.duVal.val=1;
  659. }
  660. else
  661. {
  662. new_sym.duVal.setFlags(duFlag);
  663. }
  664. ps->sym.push_back(new_sym);
  665. ps->numArgs++;
  666. }
  667. /* Save maximum argument offset */
  668. if ((dword)ps->maxOff < (off + (dword)size))
  669. ps->maxOff = off + (int16)size;
  670. }
  671. /* lookupAddr - Looks up a data reference in the symbol table and stores it
  672. * if necessary.
  673. * Returns a pointer to the symbol in the
  674. * symbol table, or Null if it's not a direct memory offset. */
  675. static SYM * lookupAddr (LLOpcode *pm, STATE *pstate, Int size, word duFlag)
  676. {
  677. Int i;
  678. SYM * psym;
  679. dword operand;
  680. if (pm->regi == 0) { /* Global var */
  681. if (pm->segValue) { /* there is a value in the seg field */
  682. operand = opAdr (pm->segValue, pm->off);
  683. psym = updateGlobSym (operand, size, duFlag);
  684. /* Check for out of bounds */
  685. if (psym->label >= (dword)prog.cbImage)
  686. return (NULL);
  687. return (psym);
  688. }
  689. else if (pstate->f[pm->seg]) { /* new value */
  690. pm->segValue = pstate->r[pm->seg];
  691. operand = opAdr(pm->segValue, pm->off);
  692. i = symtab.size();
  693. psym = updateGlobSym (operand, size, duFlag);
  694. /* Flag new memory locations that are segment values */
  695. if (symtab.size() > i)
  696. {
  697. if (size == 4)
  698. operand += 2; /* High word */
  699. for (i = 0; i < prog.cReloc; i++)
  700. if (prog.relocTable[i] == operand) {
  701. psym->flg = SEG_IMMED;
  702. break;
  703. }
  704. }
  705. /* Check for out of bounds */
  706. if (psym->label >= (dword)prog.cbImage)
  707. return (NULL);
  708. return (psym);
  709. }
  710. }
  711. return (NULL);
  712. }
  713. /* setState - Assigns a value to a reg. */
  714. void STATE::setState(word reg, int16 value)
  715. {
  716. value &= 0xFFFF;
  717. r[reg] = value;
  718. f[reg] = TRUE;
  719. switch (reg) {
  720. case rAX: case rCX: case rDX: case rBX:
  721. r[reg + rAL - rAX] = value & 0xFF;
  722. f[reg + rAL - rAX] = TRUE;
  723. r[reg + rAH - rAX] = (value >> 8) & 0xFF;
  724. f[reg + rAH - rAX] = TRUE;
  725. break;
  726. case rAL: case rCL: case rDL: case rBL:
  727. if (f[reg - rAL + rAH]) {
  728. r[reg - rAL + rAX] =(r[reg - rAL + rAH] << 8) + (value & 0xFF);
  729. f[reg - rAL + rAX] = TRUE;
  730. }
  731. break;
  732. case rAH: case rCH: case rDH: case rBH:
  733. if (f[reg - rAH + rAL])
  734. {
  735. r[reg - rAH + rAX] = r[reg - rAH + rAL] + ((value & 0xFF) << 8);
  736. f[reg - rAH + rAX] = TRUE;
  737. }
  738. break;
  739. }
  740. }
  741. /* labelSrchRepl - Searches Icode for instruction with label = target, and
  742. replaces *pIndex with an icode index */
  743. bool labelSrch(CIcodeRec &pIcode, Int numIp, dword target, Int *pIndex)
  744. {
  745. Int i;
  746. for (i = 0; i < numIp; i++)
  747. {
  748. if (pIcode[i].ic.ll.label == target)
  749. {
  750. *pIndex = i;
  751. return true;
  752. }
  753. }
  754. return false;
  755. }
  756. static void setBits(int16 type, dword start, dword len)
  757. /* setBits - Sets memory bitmap bits for BM_CODE or BM_DATA (additively) */
  758. {
  759. dword i;
  760. if (start < (dword)prog.cbImage)
  761. {
  762. if (start + len > (dword)prog.cbImage)
  763. len = (dword)(prog.cbImage - start);
  764. for (i = start + len - 1; i >= start; i--)
  765. {
  766. prog.map[i >> 2] |= type << ((i & 3) << 1);
  767. if (i == 0) break; // Fixes inf loop!
  768. }
  769. }
  770. }
  771. /* DU bit definitions for each reg value - including index registers */
  772. dword duReg[] = { 0x00,
  773. 0x11001, 0x22002, 0x44004, 0x88008, /* word regs */
  774. 0x10, 0x20, 0x40, 0x80,
  775. 0x100, 0x200, 0x400, 0x800, /* seg regs */
  776. 0x1000, 0x2000, 0x4000, 0x8000, /* byte regs */
  777. 0x10000, 0x20000, 0x40000, 0x80000,
  778. 0x100000, /* tmp reg */
  779. 0x48, 0x88, 0x60, 0xA0, /* index regs */
  780. 0x40, 0x80, 0x20, 0x08 };
  781. /* Checks which registers where used and updates the du.u flag.
  782. * Places local variables on the local symbol table.
  783. * Arguments: d : SRC or DST icode operand
  784. * pIcode: ptr to icode instruction
  785. * pProc : ptr to current procedure structure
  786. * pstate: ptr to current procedure state
  787. * size : size of the operand
  788. * ix : current index into icode array */
  789. static void use (opLoc d, ICODE * pIcode, Function * pProc, STATE * pstate, Int size, Int ix)
  790. {
  791. LLOpcode * pm = (d == SRC)? &pIcode->ic.ll.src: &pIcode->ic.ll.dst;
  792. SYM * psym;
  793. if (pm->regi == 0 || pm->regi >= INDEXBASE)
  794. {
  795. if (pm->regi == INDEXBASE + 6) /* indexed on bp */
  796. {
  797. if (pm->off >= 2)
  798. updateFrameOff (&pProc->args, pm->off, size, eDuVal::USE);
  799. else if (pm->off < 0)
  800. pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0);
  801. }
  802. else if (pm->regi == INDEXBASE + 2 || pm->regi == INDEXBASE + 3)
  803. pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off,
  804. (byte)((pm->regi == INDEXBASE + 2) ? rSI : rDI));
  805. else if ((pm->regi >= INDEXBASE + 4) && (pm->regi <= INDEXBASE + 7))
  806. {
  807. if ((pm->seg == rDS) && (pm->regi == INDEXBASE + 7)) /* bx */
  808. {
  809. if (pm->off > 0) /* global indexed variable */
  810. pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,ix, TYPE_WORD_SIGN);
  811. }
  812. pIcode->du.use |= duReg[pm->regi];
  813. }
  814. else if (psym = lookupAddr(pm, pstate, size, eDuVal::USE))
  815. {
  816. setBits (BM_DATA, psym->label, (dword)size);
  817. pIcode->ic.ll.flg |= SYM_USE;
  818. pIcode->ic.ll.caseTbl.numEntries = psym - &symtab[0];
  819. }
  820. }
  821. /* Use of register */
  822. else if ((d == DST) || ((d == SRC) && (pIcode->ic.ll.flg & I) != I))
  823. pIcode->du.use |= duReg[pm->regi];
  824. }
  825. /* Checks which registers were defined (ie. got a new value) and updates the
  826. * du.d flag.
  827. * Places local variables in the local symbol table. */
  828. static void def (opLoc d, ICODE * pIcode, Function * pProc, STATE * pstate, Int size,
  829. Int ix)
  830. {
  831. LLOpcode *pm = (d == SRC)? &pIcode->ic.ll.src: &pIcode->ic.ll.dst;
  832. SYM * psym;
  833. if (pm->regi == 0 || pm->regi >= INDEXBASE)
  834. {
  835. if (pm->regi == INDEXBASE + 6) /* indexed on bp */
  836. {
  837. if (pm->off >= 2)
  838. updateFrameOff (&pProc->args, pm->off, size, eDEF);
  839. else if (pm->off < 0)
  840. pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0);
  841. }
  842. else if (pm->regi == INDEXBASE + 2 || pm->regi == INDEXBASE + 3)
  843. {
  844. pProc->localId.newByteWordStk(TYPE_WORD_SIGN, pm->off,
  845. (byte)((pm->regi == INDEXBASE + 2) ? rSI : rDI));
  846. }
  847. else if ((pm->regi >= INDEXBASE + 4) && (pm->regi <= INDEXBASE + 7))
  848. {
  849. if ((pm->seg == rDS) && (pm->regi == INDEXBASE + 7)) /* bx */
  850. {
  851. if (pm->off > 0) /* global var */
  852. pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,ix, TYPE_WORD_SIGN);
  853. }
  854. pIcode->du.use |= duReg[pm->regi];
  855. }
  856. else if (psym = lookupAddr(pm, pstate, size, eDEF))
  857. {
  858. setBits(BM_DATA, psym->label, (dword)size);
  859. pIcode->ic.ll.flg |= SYM_DEF;
  860. pIcode->ic.ll.caseTbl.numEntries = psym - &symtab[0];
  861. }
  862. }
  863. /* Definition of register */
  864. else if ((d == DST) || ((d == SRC) && (pIcode->ic.ll.flg & I) != I))
  865. {
  866. pIcode->du.def |= duReg[pm->regi];
  867. pIcode->du1.numRegsDef++;
  868. }
  869. }
  870. /* use_def - operand is both use and def'd.
  871. * Note: the destination will always be a register, stack variable, or global
  872. * variable. */
  873. static void use_def(opLoc d, ICODE * pIcode, Function * pProc, STATE * pstate, Int cb,
  874. Int ix)
  875. {
  876. LLOpcode * pm = (d == SRC)? &pIcode->ic.ll.src: &pIcode->ic.ll.dst;
  877. use (d, pIcode, pProc, pstate, cb, ix);
  878. if (pm->regi < INDEXBASE) /* register */
  879. {
  880. pIcode->du.def |= duReg[pm->regi];
  881. pIcode->du1.numRegsDef++;
  882. }
  883. }
  884. /* Set DU vector, local variables and arguments, and DATA bits in the
  885. * bitmap */
  886. void Function::process_operands(ICODE * pIcode, STATE * pstate)
  887. {
  888. Int ix=Icode.size();
  889. Int i;
  890. Int sseg = (pIcode->ic.ll.src.seg)? pIcode->ic.ll.src.seg: rDS;
  891. Int cb = (pIcode->ic.ll.flg & B) ? 1: 2;
  892. flags32 Imm = (pIcode->ic.ll.flg & I);
  893. switch (pIcode->ic.ll.opcode) {
  894. case iAND: case iOR: case iXOR:
  895. case iSAR: case iSHL: case iSHR:
  896. case iRCL: case iRCR: case iROL: case iROR:
  897. case iADD: case iADC: case iSUB: case iSBB:
  898. if (! Imm) {
  899. use(SRC, pIcode, this, pstate, cb, ix);
  900. }
  901. case iINC: case iDEC: case iNEG: case iNOT:
  902. case iAAA: case iAAD: case iAAM: case iAAS:
  903. case iDAA: case iDAS:
  904. use_def(DST, pIcode, this, pstate, cb, ix);
  905. break;
  906. case iXCHG:
  907. /* This instruction is replaced by 3 instructions, only need
  908. * to define the src operand and use the destination operand
  909. * in the mean time. */
  910. use(SRC, pIcode, this, pstate, cb, ix);
  911. def(DST, pIcode, this, pstate, cb, ix);
  912. break;
  913. case iTEST: case iCMP:
  914. if (! Imm)
  915. use(SRC, pIcode, this, pstate, cb, ix);
  916. use(DST, pIcode, this, pstate, cb, ix);
  917. break;
  918. case iDIV: case iIDIV:
  919. use(SRC, pIcode, this, pstate, cb, ix);
  920. if (cb == 1)
  921. pIcode->du.use |= duReg[rTMP];
  922. break;
  923. case iMUL: case iIMUL:
  924. use(SRC, pIcode, this, pstate, cb, ix);
  925. if (! Imm)
  926. {
  927. use (DST, pIcode, this, pstate, cb, ix);
  928. if (cb == 1)
  929. {
  930. pIcode->du.def |= duReg[rAX];
  931. pIcode->du1.numRegsDef++;
  932. }
  933. else
  934. {
  935. pIcode->du.def |= (duReg[rAX] | duReg[rDX]);
  936. pIcode->du1.numRegsDef += 2;
  937. }
  938. }
  939. else
  940. def (DST, pIcode, this, pstate, cb, ix);
  941. break;
  942. case iSIGNEX:
  943. cb = (pIcode->ic.ll.flg & SRC_B) ? 1 : 2;
  944. if (cb == 1) /* byte */
  945. {
  946. pIcode->du.def |= duReg[rAX];
  947. pIcode->du1.numRegsDef++;
  948. pIcode->du.use |= duReg[rAL];
  949. }
  950. else /* word */
  951. {
  952. pIcode->du.def |= (duReg[rDX] | duReg[rAX]);
  953. pIcode->du1.numRegsDef += 2;
  954. pIcode->du.use |= duReg[rAX];
  955. }
  956. break;
  957. case iCALLF: /* Ignore def's on CS for now */
  958. cb = 4;
  959. case iCALL: case iPUSH: case iPOP:
  960. if (! Imm) {
  961. if (pIcode->ic.ll.opcode == iPOP)
  962. def(DST, pIcode, this, pstate, cb, ix);
  963. else
  964. use(DST, pIcode, this, pstate, cb, ix);
  965. }
  966. break;
  967. case iESC: /* operands may be larger */
  968. use(DST, pIcode, this, pstate, cb, ix);
  969. break;
  970. case iLDS: case iLES:
  971. pIcode->du.def |= duReg[(pIcode->ic.ll.opcode == iLDS) ? rDS : rES];
  972. pIcode->du1.numRegsDef++;
  973. cb = 4;
  974. case iMOV:
  975. use(SRC, pIcode, this, pstate, cb, ix);
  976. def(DST, pIcode, this, pstate, cb, ix);
  977. break;
  978. case iLEA:
  979. use(SRC, pIcode, this, pstate, 2, ix);
  980. def(DST, pIcode, this, pstate, 2, ix);
  981. break;
  982. case iBOUND:
  983. use(SRC, pIcode, this, pstate, 4, ix);
  984. use(DST, pIcode, this, pstate, cb, ix);
  985. break;
  986. case iJMPF:
  987. cb = 4;
  988. case iJMP:
  989. if (! Imm)
  990. use(SRC, pIcode, this, pstate, cb, ix);
  991. break;
  992. case iLOOP: case iLOOPE: case iLOOPNE:
  993. pIcode->du.def |= duReg[rCX];
  994. pIcode->du1.numRegsDef++;
  995. case iJCXZ:
  996. pIcode->du.use |= duReg[rCX];
  997. break;
  998. case iREPNE_CMPS: case iREPE_CMPS: case iREP_MOVS:
  999. pIcode->du.def |= duReg[rCX];
  1000. pIcode->du1.numRegsDef++;
  1001. pIcode->du.use |= duReg[rCX];
  1002. case iCMPS: case iMOVS:
  1003. pIcode->du.def |= duReg[rSI] | duReg[rDI];
  1004. pIcode->du1.numRegsDef += 2;
  1005. pIcode->du.use |= duReg[rSI] | duReg[rDI] | duReg[rES] | duReg[sseg];
  1006. break;
  1007. case iREPNE_SCAS: case iREPE_SCAS: case iREP_STOS: case iREP_INS:
  1008. pIcode->du.def |= duReg[rCX];
  1009. pIcode->du1.numRegsDef++;
  1010. pIcode->du.use |= duReg[rCX];
  1011. case iSCAS: case iSTOS: case iINS:
  1012. pIcode->du.def |= duReg[rDI];
  1013. pIcode->du1.numRegsDef++;
  1014. if (pIcode->ic.ll.opcode == iREP_INS || pIcode->ic.ll.opcode== iINS)
  1015. {
  1016. pIcode->du.use |= duReg[rDI] | duReg[rES] | duReg[rDX];
  1017. }
  1018. else
  1019. {
  1020. pIcode->du.use |= duReg[rDI] | duReg[rES] | duReg[(cb == 2)? rAX: rAL];
  1021. }
  1022. break;
  1023. case iREP_LODS:
  1024. pIcode->du.def |= duReg[rCX];
  1025. pIcode->du1.numRegsDef++;
  1026. pIcode->du.use |= duReg[rCX];
  1027. case iLODS:
  1028. pIcode->du.def |= duReg[rSI] | duReg[(cb==2)? rAX: rAL];
  1029. pIcode->du1.numRegsDef += 2;
  1030. pIcode->du.use |= duReg[rSI] | duReg[sseg];
  1031. break;
  1032. case iREP_OUTS:
  1033. pIcode->du.def |= duReg[rCX];
  1034. pIcode->du1.numRegsDef++;
  1035. pIcode->du.use |= duReg[rCX];
  1036. case iOUTS:
  1037. pIcode->du.def |= duReg[rSI];
  1038. pIcode->du1.numRegsDef++;
  1039. pIcode->du.use |= duReg[rSI] | duReg[rDX] | duReg[sseg];
  1040. break;
  1041. case iIN: case iOUT:
  1042. def(DST, pIcode, this, pstate, cb, ix);
  1043. if (! Imm)
  1044. {
  1045. pIcode->du.use |= duReg[rDX];
  1046. }
  1047. break;
  1048. }
  1049. for (i = rSP; i <= rBH; i++) /* Kill all defined registers */
  1050. if (pIcode->ic.ll.flagDU.d & (1 << i))
  1051. pstate->f[i] = FALSE;
  1052. }