parser.cpp 43 KB

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