parser.cpp 43 KB

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