parser.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. #include "project.h"
  12. using namespace std;
  13. extern Project g_proj;
  14. //static void FollowCtrl (Function * pProc, CALL_GRAPH * pcallGraph, STATE * pstate);
  15. static boolT process_JMP (ICODE * pIcode, STATE * pstate, CALL_GRAPH * pcallGraph);
  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(g_proj.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 %lX\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. if ((ll->testFlags(I)) && ll->src().getImm2() == 1)
  304. pstate->JCond.immed *= 2;
  305. else
  306. pstate->JCond.regi = 0;
  307. break;
  308. case iLEA:
  309. if (ll->src().getReg2()== rUNDEF) /* direct mem offset */
  310. pstate->setState( ll->dst.getReg2(), ll->src().off);
  311. break;
  312. case iLDS: case iLES:
  313. if ((psym = lookupAddr(&ll->src(), pstate, 4, eDuVal::USE))
  314. /* && (Icode.ll()->flg & SEG_IMMED) */ )
  315. {
  316. offset = LH(&prog.Image[psym->label]);
  317. pstate->setState( (ll->getOpcode() == iLDS)? rDS: rES,
  318. LH(&prog.Image[psym->label + 2]));
  319. pstate->setState( ll->dst.regi, (int16_t)offset);
  320. psym->type = TYPE_PTR;
  321. }
  322. break;
  323. }
  324. }
  325. if (err) {
  326. this->flg &= ~TERMINATES;
  327. if (err == INVALID_386OP || err == INVALID_OPCODE)
  328. {
  329. fatalError(err, prog.Image[_Icode.ll()->label], _Icode.ll()->label);
  330. this->flg |= PROC_BADINST;
  331. }
  332. else if (err == IP_OUT_OF_RANGE)
  333. fatalError (err, _Icode.ll()->label);
  334. else
  335. reportError(err, _Icode.ll()->label);
  336. }
  337. }
  338. /* Firstly look for a leading range check of the form:-
  339. * CMP {BX | SI | DI}, immed
  340. * JA | JAE | JB | JBE
  341. * This is stored in the current state as if we had just
  342. * followed a JBE branch (i.e. [reg] lies between 0 - immed).
  343. */
  344. void Function::extractJumpTableRange(ICODE& pIcode, STATE *pstate, JumpTable &table)
  345. {
  346. static uint8_t i2r[4] = {rSI, rDI, rBP, rBX};
  347. if (pstate->JCond.regi == i2r[pIcode.ll()->src().getReg2()-INDEX_SI])
  348. table.finish = table.start + pstate->JCond.immed;
  349. else
  350. table.finish = table.start + 2;
  351. }
  352. /* process_JMP - Handles JMPs, returns true if we should end recursion */
  353. bool Function::followAllTableEntries(JumpTable &table, uint32_t cs, ICODE& pIcode, CALL_GRAPH* pcallGraph, STATE *pstate)
  354. {
  355. PROG &prog(Project::get()->prog);
  356. STATE StCopy;
  357. setBits(BM_DATA, table.start, table.size()*table.entrySize());
  358. pIcode.ll()->setFlags(SWITCH);
  359. pIcode.ll()->caseTbl2.resize( table.size() );
  360. assert(pIcode.ll()->caseTbl2.size()<512);
  361. uint32_t k=0;
  362. for (int i = table.start; i < table.finish; i += 2)
  363. {
  364. StCopy = *pstate;
  365. StCopy.IP = cs + LH(&prog.Image[i]);
  366. iICODE last_current_insn = (++Icode.rbegin()).base();
  367. FollowCtrl (pcallGraph, &StCopy);
  368. ++last_current_insn; // incremented here because FollowCtrl might have adde more instructions after the Jmp
  369. last_current_insn->ll()->caseEntry = k++;
  370. last_current_insn->ll()->setFlags(CASE);
  371. pIcode.ll()->caseTbl2.push_back( last_current_insn->ll()->GetLlLabel() );
  372. }
  373. }
  374. bool Function::process_JMP (ICODE & pIcode, STATE *pstate, CALL_GRAPH * pcallGraph)
  375. {
  376. PROG &prog(Project::get()->prog);
  377. static uint8_t i2r[4] = {rSI, rDI, rBP, rBX};
  378. ICODE _Icode;
  379. uint32_t cs, offTable, endTable;
  380. uint32_t i, k, seg, target;
  381. uint32_t tmp;
  382. if (pIcode.ll()->testFlags(I))
  383. {
  384. if (pIcode.ll()->getOpcode() == iJMPF)
  385. pstate->setState( rCS, LH(prog.Image + pIcode.ll()->label + 3));
  386. uint32_t i = pstate->IP = pIcode.ll()->src().getImm2();
  387. if ((long)i < 0)
  388. {
  389. exit(1);
  390. }
  391. /* Return true if jump target is already parsed */
  392. return Icode.alreadyDecoded(i);
  393. }
  394. /* We've got an indirect JMP - look for switch() stmt. idiom of the form
  395. * JMP uint16_t ptr word_offset[rBX | rSI | rDI] */
  396. seg = (pIcode.ll()->src().seg)? pIcode.ll()->src().seg: rDS;
  397. /* Ensure we have a uint16_t offset & valid seg */
  398. if (pIcode.ll()->match(iJMP) and (pIcode.ll()->testFlags(WORD_OFF)) &&
  399. pstate->f[seg] &&
  400. (pIcode.ll()->src().regi == INDEX_SI ||
  401. pIcode.ll()->src().regi == INDEX_DI || /* Idx reg. BX, SI, DI */
  402. pIcode.ll()->src().regi == INDEX_BX))
  403. {
  404. offTable = ((uint32_t)(uint16_t)pstate->r[seg] << 4) + pIcode.ll()->src().off;
  405. /* Firstly look for a leading range check of the form:-
  406. * CMP {BX | SI | DI}, immed
  407. * JA | JAE | JB | JBE
  408. * This is stored in the current state as if we had just
  409. * followed a JBE branch (i.e. [reg] lies between 0 - immed).
  410. */
  411. if (pstate->JCond.regi == i2r[pIcode.ll()->src().regi-(INDEX_BX_SI+4)])
  412. endTable = offTable + pstate->JCond.immed;
  413. else
  414. endTable = (uint32_t)prog.cbImage;
  415. /* Search for first uint8_t flagged after start of table */
  416. for (i = offTable; i <= endTable; i++)
  417. if (BITMAP(i, BM_CODE | BM_DATA))
  418. break;
  419. endTable = i & ~1; /* Max. possible table size */
  420. /* Now do some heuristic pruning. Look for ptrs. into the table
  421. * and for addresses that don't appear to point to valid code.
  422. */
  423. cs = (uint32_t)(uint16_t)pstate->r[rCS] << 4;
  424. for (i = offTable; i < endTable; i += 2)
  425. {
  426. target = cs + LH(&prog.Image[i]);
  427. if (target < endTable && target >= offTable)
  428. endTable = target;
  429. else if (target >= (uint32_t)prog.cbImage)
  430. endTable = i;
  431. }
  432. for (i = offTable; i < endTable; i += 2)
  433. {
  434. target = cs + LH(&prog.Image[i]);
  435. /* Be wary of 00 00 as code - it's probably data */
  436. if (! (prog.Image[target] || prog.Image[target+1]) ||
  437. scan(target, _Icode))
  438. endTable = i;
  439. }
  440. /* Now for each entry in the table take a copy of the current
  441. * state and recursively call FollowCtrl(). */
  442. if (offTable < endTable)
  443. {
  444. assert(((endTable - offTable) / 2)<512);
  445. STATE StCopy;
  446. int ip;
  447. uint32_t *psw;
  448. setBits(BM_DATA, offTable, endTable - offTable);
  449. pIcode.ll()->setFlags(SWITCH);
  450. //pIcode.ll()->caseTbl2.numEntries = (endTable - offTable) / 2;
  451. for (i = offTable, k = 0; i < endTable; i += 2)
  452. {
  453. StCopy = *pstate;
  454. StCopy.IP = cs + LH(&prog.Image[i]);
  455. iICODE last_current_insn = (++Icode.rbegin()).base();
  456. ip = Icode.size();
  457. FollowCtrl (pcallGraph, &StCopy);
  458. ++last_current_insn;
  459. last_current_insn->ll()->caseEntry = k++;
  460. last_current_insn->ll()->setFlags(CASE);
  461. pIcode.ll()->caseTbl2.push_back( last_current_insn->ll()->GetLlLabel() );
  462. }
  463. return true;
  464. }
  465. }
  466. /* Can't do anything with this jump */
  467. flg |= PROC_IJMP;
  468. flg &= ~TERMINATES;
  469. interactDis(this, this->Icode.size()-1);
  470. return true;
  471. }
  472. /* Process procedure call.
  473. * Note: We assume that CALL's will return unless there is good evidence to
  474. * the contrary - thus we return false unless all paths in the called
  475. * procedure end in DOS exits. This is reasonable since C procedures
  476. * will always include the epilogue after the call anyway and it's to
  477. * be assumed that if an assembler program contains a CALL that the
  478. * programmer expected it to come back - otherwise surely a JMP would
  479. * have been used. */
  480. boolT Function::process_CALL (ICODE & pIcode, CALL_GRAPH * pcallGraph, STATE *pstate)
  481. {
  482. PROG &prog(Project::get()->prog);
  483. ICODE &last_insn(Icode.back());
  484. STATE localState; /* Local copy of the machine state */
  485. uint32_t off;
  486. boolT indirect;
  487. /* For Indirect Calls, find the function address */
  488. indirect = false;
  489. //pIcode.ll()->immed.proc.proc=fakeproc;
  490. if ( not pIcode.ll()->testFlags(I) )
  491. {
  492. /* Not immediate, i.e. indirect call */
  493. if (pIcode.ll()->dst.regi && (!option.Calls))
  494. {
  495. /* We have not set the brave option to attempt to follow
  496. the execution path through register indirect calls.
  497. So we just exit this function, and ignore the call.
  498. We probably should not have parsed this deep, anyway.
  499. */
  500. return false;
  501. }
  502. /* Offset into program image is seg:off of read input */
  503. /* Note: this assumes that the pointer itself is at
  504. es:0 where es:0 is the start of the image. This is
  505. usually wrong! Consider also CALL [BP+0E] in which the
  506. segment for the pointer is in SS! - Mike */
  507. if(pIcode.ll()->dst.isReg())
  508. {
  509. if( not pstate->isKnown(pIcode.ll()->dst.regi)
  510. or
  511. not pstate->isKnown(pIcode.ll()->dst.seg)
  512. )
  513. {
  514. fprintf(stderr,"Indirect call with unkown register values\n");
  515. return false;
  516. }
  517. off = pstate->r[pIcode.ll()->dst.seg];
  518. off <<=4;
  519. off += pstate->r[pIcode.ll()->dst.regi];
  520. }
  521. else
  522. {
  523. off = (uint32_t)(uint16_t)pIcode.ll()->dst.off +
  524. ((uint32_t)(uint16_t)pIcode.ll()->dst.segValue << 4);
  525. }
  526. /* Address of function is given by 4 (CALLF) or 2 (CALL) bytes at
  527. * previous offset into the program image */
  528. uint32_t tgtAddr=0;
  529. if (pIcode.ll()->getOpcode() == iCALLF)
  530. tgtAddr= LH(&prog.Image[off]) + (uint32_t)(LH(&prog.Image[off+2])) << 4;
  531. else
  532. tgtAddr= LH(&prog.Image[off]) + (uint32_t)(uint16_t)state.r[rCS] << 4;
  533. pIcode.ll()->replaceSrc(LLOperand::CreateImm2( tgtAddr ) );
  534. pIcode.ll()->setFlags(I);
  535. indirect = true;
  536. }
  537. /* Process CALL. Function address is located in pIcode.ll()->immed.op */
  538. if (pIcode.ll()->testFlags(I))
  539. {
  540. /* Search procedure list for one with appropriate entry point */
  541. ilFunction iter = g_proj.findByEntry(pIcode.ll()->src().getImm2());
  542. /* Create a new procedure node and save copy of the state */
  543. if ( not g_proj.valid(iter) )
  544. {
  545. iter = g_proj.createFunction();
  546. Function &x(*iter);
  547. x.procEntry = pIcode.ll()->src().getImm2();
  548. LibCheck(x);
  549. if (x.flg & PROC_ISLIB)
  550. {
  551. /* A library function. No need to do any more to it */
  552. pcallGraph->insertCallGraph (this, iter);
  553. //iter = (++pProcList.rbegin()).base();
  554. last_insn.ll()->src().proc.proc = &x;
  555. return false;
  556. }
  557. if (indirect)
  558. x.flg |= PROC_ICALL;
  559. if (x.name.empty()) /* Don't overwrite existing name */
  560. {
  561. ostringstream os;
  562. os<<"proc_"<< ++prog.cProcs;
  563. x.name = os.str();
  564. }
  565. x.depth = x.depth + 1;
  566. x.flg |= TERMINATES;
  567. /* Save machine state in localState, load up IP and CS.*/
  568. localState = *pstate;
  569. pstate->IP = pIcode.ll()->src().getImm2();
  570. if (pIcode.ll()->getOpcode() == iCALLF)
  571. pstate->setState( rCS, LH(prog.Image + pIcode.ll()->label + 3));
  572. x.state = *pstate;
  573. /* Insert new procedure in call graph */
  574. pcallGraph->insertCallGraph (this, iter);
  575. /* Process new procedure */
  576. x.FollowCtrl (pcallGraph, pstate);
  577. /* Restore segment registers & IP from localState */
  578. pstate->IP = localState.IP;
  579. pstate->setState( rCS, localState.r[rCS]);
  580. pstate->setState( rDS, localState.r[rDS]);
  581. pstate->setState( rES, localState.r[rES]);
  582. pstate->setState( rSS, localState.r[rSS]);
  583. }
  584. else
  585. g_proj.callGraph->insertCallGraph (this, iter);
  586. last_insn.ll()->src().proc.proc = &(*iter); // ^ target proc
  587. /* return ((p->flg & TERMINATES) != 0); */
  588. }
  589. return false; // Cristina, please check!!
  590. }
  591. /* process_MOV - Handles state changes due to simple assignments */
  592. static void process_MOV(LLInst & ll, STATE * pstate)
  593. {
  594. PROG &prog(Project::get()->prog);
  595. SYM * psym, *psym2; /* Pointer to symbol in global symbol table */
  596. uint8_t dstReg = ll.dst.regi;
  597. uint8_t srcReg = ll.src().regi;
  598. if (dstReg > 0 && dstReg < INDEX_BX_SI)
  599. {
  600. if (ll.testFlags(I))
  601. pstate->setState( dstReg, (int16_t)ll.src().getImm2());
  602. else if (srcReg == 0) /* direct memory offset */
  603. {
  604. psym = lookupAddr(&ll.src(), pstate, 2, eDuVal::USE);
  605. if (psym && ((psym->flg & SEG_IMMED) || psym->duVal.val))
  606. pstate->setState( dstReg, LH(&prog.Image[psym->label]));
  607. }
  608. else if (srcReg < INDEX_BX_SI && pstate->f[srcReg]) /* reg */
  609. {
  610. pstate->setState( dstReg, pstate->r[srcReg]);
  611. /* Follow moves of the possible index register */
  612. if (pstate->JCond.regi == srcReg)
  613. pstate->JCond.regi = dstReg;
  614. }
  615. }
  616. else if (dstReg == 0) { /* direct memory offset */
  617. int size=2;
  618. if((ll.src().regi>=rAL)&&(ll.src().regi<=rBH))
  619. size=1;
  620. psym = lookupAddr (&ll.dst, pstate, size, eDEF);
  621. if (psym && ! (psym->duVal.val)) /* no initial value yet */
  622. if (ll.testFlags(I)) /* immediate */
  623. {
  624. prog.Image[psym->label] = (uint8_t)ll.src().getImm2();
  625. if(psym->size>1)
  626. prog.Image[psym->label+1] = (uint8_t)(ll.src().getImm2()>>8);
  627. psym->duVal.val = 1;
  628. }
  629. else if (srcReg == 0) /* direct mem offset */
  630. {
  631. psym2 = lookupAddr (&ll.src(), pstate, 2, eDuVal::USE);
  632. if (psym2 && ((psym->flg & SEG_IMMED) || (psym->duVal.val)))
  633. {
  634. prog.Image[psym->label] = (uint8_t)prog.Image[psym2->label];
  635. if(psym->size>1)
  636. prog.Image[psym->label+1] = prog.Image[psym2->label+1];//(uint8_t)(prog.Image[psym2->label+1] >> 8);
  637. psym->duVal.setFlags(eDuVal::DEF);
  638. psym2->duVal.setFlags(eDuVal::USE);
  639. }
  640. }
  641. else if (srcReg < INDEX_BX_SI && pstate->f[srcReg]) /* reg */
  642. {
  643. prog.Image[psym->label] = (uint8_t)pstate->r[srcReg];
  644. if(psym->size>1)
  645. prog.Image[psym->label+1] = (uint8_t)(pstate->r[srcReg] >> 8);
  646. psym->duVal.setFlags(eDuVal::DEF);
  647. }
  648. }
  649. }
  650. /* Updates the offset entry to the stack frame table (arguments),
  651. * and returns a pointer to such entry. */
  652. void STKFRAME::updateFrameOff ( int16_t off, int _size, uint16_t duFlag)
  653. {
  654. int i;
  655. /* Check for symbol in stack frame table */
  656. auto iter=findByLabel(off);
  657. if(iter!=end())
  658. {
  659. if (iter->size < _size)
  660. {
  661. iter->size = _size;
  662. }
  663. }
  664. else
  665. {
  666. char nm[16];
  667. STKSYM new_sym;
  668. sprintf (nm, "arg%ld", size());
  669. new_sym.name = nm;
  670. new_sym.label= off;
  671. new_sym.size = _size;
  672. new_sym.type = TypeContainer::defaultTypeForSize(_size);
  673. if (duFlag == eDuVal::USE) /* must already have init value */
  674. {
  675. new_sym.duVal.use=1;
  676. //new_sym.duVal.val=1;
  677. }
  678. else
  679. {
  680. new_sym.duVal.setFlags(duFlag);
  681. }
  682. push_back(new_sym);
  683. this->numArgs++;
  684. }
  685. /* Save maximum argument offset */
  686. if ((uint32_t)this->maxOff < (off + (uint32_t)_size))
  687. this->maxOff = off + (int16_t)_size;
  688. }
  689. /* lookupAddr - Looks up a data reference in the symbol table and stores it
  690. * if necessary.
  691. * Returns a pointer to the symbol in the
  692. * symbol table, or Null if it's not a direct memory offset. */
  693. static SYM * lookupAddr (LLOperand *pm, STATE *pstate, int size, uint16_t duFlag)
  694. {
  695. PROG &prog(Project::get()->prog);
  696. int i;
  697. SYM * psym=nullptr;
  698. uint32_t operand;
  699. bool created_new=false;
  700. if (pm->regi != rUNDEF)
  701. return nullptr; // register or indexed
  702. /* Global var */
  703. if (pm->segValue) /* there is a value in the seg field */
  704. {
  705. operand = opAdr (pm->segValue, pm->off);
  706. psym = g_proj.symtab.updateGlobSym (operand, size, duFlag,created_new);
  707. }
  708. else if (pstate->f[pm->seg]) /* new value */
  709. {
  710. pm->segValue = pstate->r[pm->seg];
  711. operand = opAdr(pm->segValue, pm->off);
  712. psym = g_proj.symtab.updateGlobSym (operand, size, duFlag,created_new);
  713. /* Flag new memory locations that are segment values */
  714. if (created_new)
  715. {
  716. if (size == 4)
  717. operand += 2; /* High uint16_t */
  718. for (i = 0; i < prog.cReloc; i++)
  719. if (prog.relocTable[i] == operand) {
  720. psym->flg = SEG_IMMED;
  721. break;
  722. }
  723. }
  724. }
  725. /* Check for out of bounds */
  726. if (psym && (psym->label>=0) and (psym->label < (uint32_t)prog.cbImage))
  727. return psym;
  728. return nullptr;
  729. }
  730. /* setState - Assigns a value to a reg. */
  731. void STATE::setState(uint16_t reg, int16_t value)
  732. {
  733. value &= 0xFFFF;
  734. r[reg] = value;
  735. f[reg] = true;
  736. switch (reg) {
  737. case rAX: case rCX: case rDX: case rBX:
  738. r[reg + rAL - rAX] = value & 0xFF;
  739. f[reg + rAL - rAX] = true;
  740. r[reg + rAH - rAX] = (value >> 8) & 0xFF;
  741. f[reg + rAH - rAX] = true;
  742. break;
  743. case rAL: case rCL: case rDL: case rBL:
  744. if (f[reg - rAL + rAH]) {
  745. r[reg - rAL + rAX] =(r[reg - rAL + rAH] << 8) + (value & 0xFF);
  746. f[reg - rAL + rAX] = true;
  747. }
  748. break;
  749. case rAH: case rCH: case rDH: case rBH:
  750. if (f[reg - rAH + rAL])
  751. {
  752. r[reg - rAH + rAX] = r[reg - rAH + rAL] + ((value & 0xFF) << 8);
  753. f[reg - rAH + rAX] = true;
  754. }
  755. break;
  756. }
  757. }
  758. /* labelSrchRepl - Searches Icode for instruction with label = target, and
  759. replaces *pIndex with an icode index */
  760. /* setBits - Sets memory bitmap bits for BM_CODE or BM_DATA (additively) */
  761. static void setBits(int16_t type, uint32_t start, uint32_t len)
  762. {
  763. PROG &prog(Project::get()->prog);
  764. uint32_t i;
  765. if (start < (uint32_t)prog.cbImage)
  766. {
  767. if (start + len > (uint32_t)prog.cbImage)
  768. len = (uint32_t)(prog.cbImage - start);
  769. for (i = start + len - 1; i >= start; i--)
  770. {
  771. prog.map[i >> 2] |= type << ((i & 3) << 1);
  772. if (i == 0) break; // Fixes inf loop!
  773. }
  774. }
  775. }
  776. /* DU bit definitions for each reg value - including index registers */
  777. std::bitset<32> duReg[] = { 0x00,
  778. //AH AL . . AX, BH
  779. 0x11001, 0x22002, 0x44004, 0x88008, /* uint16_t regs */
  780. 0x10, 0x20, 0x40, 0x80,
  781. 0x100, 0x200, 0x400, 0x800, /* seg regs */
  782. 0x1000, 0x2000, 0x4000, 0x8000, /* uint8_t regs */
  783. 0x10000, 0x20000, 0x40000, 0x80000,
  784. 0x100000, /* tmp reg */
  785. 0x48, 0x88, 0x60, 0xA0, /* index regs */
  786. 0x40, 0x80, 0x20, 0x08 };
  787. /* Checks which registers were used and updates the du.u flag.
  788. * Places local variables on the local symbol table.
  789. * Arguments: d : SRC or DST icode operand
  790. * pIcode: ptr to icode instruction
  791. * pProc : ptr to current procedure structure
  792. * pstate: ptr to current procedure state
  793. * size : size of the operand
  794. * ix : current index into icode array */
  795. static void use (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int size, int ix)
  796. {
  797. const LLOperand * pm = pIcode.ll()->get(d) ;
  798. SYM * psym;
  799. if ( Machine_X86::isMemOff(pm->regi) )
  800. {
  801. if (pm->regi == INDEX_BP) /* indexed on bp */
  802. {
  803. if (pm->off >= 2)
  804. pProc->args.updateFrameOff ( pm->off, size, eDuVal::USE);
  805. else if (pm->off < 0)
  806. pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0);
  807. }
  808. else if (pm->regi == INDEX_BP_SI || pm->regi == INDEX_BP_DI)
  809. pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off,
  810. (uint8_t)((pm->regi == INDEX_BP_SI) ? rSI : rDI));
  811. else if ((pm->regi >= INDEX_SI) && (pm->regi <= INDEX_BX))
  812. {
  813. if ((pm->seg == rDS) && (pm->regi == INDEX_BX)) /* bx */
  814. {
  815. if (pm->off > 0) /* global indexed variable */
  816. pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,ix, TYPE_WORD_SIGN);
  817. }
  818. pIcode.du.use |= duReg[pm->regi];
  819. }
  820. else if (psym = lookupAddr(const_cast<LLOperand *>(pm), pstate, size, eDuVal::USE))
  821. {
  822. setBits (BM_DATA, psym->label, (uint32_t)size);
  823. pIcode.ll()->setFlags(SYM_USE);
  824. pIcode.ll()->caseEntry = distance(&g_proj.symtab[0],psym); //WARNING: was setting case count
  825. }
  826. }
  827. /* Use of register */
  828. else if ((d == DST) || ((d == SRC) && (not pIcode.ll()->testFlags(I))))
  829. pIcode.du.use |= duReg[pm->regi];
  830. }
  831. /* Checks which registers were defined (ie. got a new value) and updates the
  832. * du.d flag.
  833. * Places local variables in the local symbol table. */
  834. static void def (opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int size,
  835. int ix)
  836. {
  837. LLOperand *pm = pIcode.ll()->get(d);
  838. SYM * psym;
  839. if (pm->regi == 0 || pm->regi >= INDEX_BX_SI)
  840. {
  841. if (pm->regi == INDEX_BP) /* indexed on bp */
  842. {
  843. if (pm->off >= 2)
  844. pProc->args.updateFrameOff ( pm->off, size, eDEF);
  845. else if (pm->off < 0)
  846. pProc->localId.newByteWordStk (TYPE_WORD_SIGN, pm->off, 0);
  847. }
  848. else if (pm->regi == INDEX_BP_SI || pm->regi == INDEX_BP_DI)
  849. {
  850. pProc->localId.newByteWordStk(TYPE_WORD_SIGN, pm->off,
  851. (uint8_t)((pm->regi == INDEX_BP_SI) ? rSI : rDI));
  852. }
  853. else if ((pm->regi >= INDEX_SI) && (pm->regi <= INDEX_BX))
  854. {
  855. if ((pm->seg == rDS) && (pm->regi == INDEX_BX)) /* bx */
  856. {
  857. if (pm->off > 0) /* global var */
  858. pProc->localId.newIntIdx(pm->segValue, pm->off, rBX,ix, TYPE_WORD_SIGN);
  859. }
  860. pIcode.du.use |= duReg[pm->regi];
  861. }
  862. else if (psym = lookupAddr(pm, pstate, size, eDEF))
  863. {
  864. setBits(BM_DATA, psym->label, (uint32_t)size);
  865. pIcode.ll()->setFlags(SYM_DEF);
  866. pIcode.ll()->caseEntry = distance(&g_proj.symtab[0],psym); // WARNING: was setting Case count
  867. }
  868. }
  869. /* Definition of register */
  870. else if ((d == DST) || ((d == SRC) && (not pIcode.ll()->testFlags(I))))
  871. {
  872. pIcode.du.def |= duReg[pm->regi];
  873. pIcode.du1.numRegsDef++;
  874. }
  875. }
  876. /* use_def - operand is both use and def'd.
  877. * Note: the destination will always be a register, stack variable, or global
  878. * variable. */
  879. static void use_def(opLoc d, ICODE & pIcode, Function * pProc, STATE * pstate, int cb,
  880. int ix)
  881. {
  882. const LLOperand * pm = pIcode.ll()->get(d);
  883. use (d, pIcode, pProc, pstate, cb, ix);
  884. if (pm->regi < INDEX_BX_SI) /* register */
  885. {
  886. pIcode.du.def |= duReg[pm->regi];
  887. pIcode.du1.numRegsDef++;
  888. }
  889. }
  890. /* Set DU vector, local variables and arguments, and DATA bits in the
  891. * bitmap */
  892. extern LLOperand convertOperand(const x86_op_t &from);
  893. void Function::process_operands(ICODE & pIcode, STATE * pstate)
  894. {
  895. int ix=Icode.size();
  896. LLInst &ll_ins(*pIcode.ll());
  897. int sseg = (ll_ins.src().seg)? ll_ins.src().seg: rDS;
  898. int cb = pIcode.ll()->testFlags(B) ? 1: 2;
  899. //x86_op_t *im= pIcode.insn.x86_get_imm();
  900. bool Imm = (pIcode.ll()->testFlags(I));
  901. switch (pIcode.ll()->getOpcode()) {
  902. case iAND: case iOR: case iXOR:
  903. case iSAR: case iSHL: case iSHR:
  904. case iRCL: case iRCR: case iROL: case iROR:
  905. case iADD: case iADC: case iSUB: case iSBB:
  906. if (! Imm) {
  907. use(SRC, pIcode, this, pstate, cb, ix);
  908. }
  909. case iINC: case iDEC: case iNEG: case iNOT:
  910. case iAAA: case iAAD: case iAAM: case iAAS:
  911. case iDAA: case iDAS:
  912. use_def(DST, pIcode, this, pstate, cb, ix);
  913. break;
  914. case iXCHG:
  915. /* This instruction is replaced by 3 instructions, only need
  916. * to define the src operand and use the destination operand
  917. * in the mean time. */
  918. use(SRC, pIcode, this, pstate, cb, ix);
  919. def(DST, pIcode, this, pstate, cb, ix);
  920. break;
  921. case iTEST: case iCMP:
  922. if (! Imm)
  923. use(SRC, pIcode, this, pstate, cb, ix);
  924. use(DST, pIcode, this, pstate, cb, ix);
  925. break;
  926. case iDIV: case iIDIV:
  927. use(SRC, pIcode, this, pstate, cb, ix);
  928. if (cb == 1)
  929. pIcode.du.use |= duReg[rTMP];
  930. break;
  931. case iMUL: case iIMUL:
  932. use(SRC, pIcode, this, pstate, cb, ix);
  933. if (! Imm)
  934. {
  935. use (DST, pIcode, this, pstate, cb, ix);
  936. if (cb == 1)
  937. {
  938. pIcode.du.def |= duReg[rAX];
  939. pIcode.du1.numRegsDef++;
  940. }
  941. else
  942. {
  943. pIcode.du.def |= (duReg[rAX] | duReg[rDX]);
  944. pIcode.du1.numRegsDef += 2;
  945. }
  946. }
  947. else
  948. def (DST, pIcode, this, pstate, cb, ix);
  949. break;
  950. case iSIGNEX:
  951. cb = pIcode.ll()->testFlags(SRC_B) ? 1 : 2;
  952. if (cb == 1) /* uint8_t */
  953. {
  954. pIcode.du.def |= duReg[rAX];
  955. pIcode.du1.numRegsDef++;
  956. pIcode.du.use |= duReg[rAL];
  957. }
  958. else /* uint16_t */
  959. {
  960. pIcode.du.def |= (duReg[rDX] | duReg[rAX]);
  961. pIcode.du1.numRegsDef += 2;
  962. pIcode.du.use |= duReg[rAX];
  963. }
  964. break;
  965. case iCALLF: /* Ignore def's on CS for now */
  966. cb = 4;
  967. case iCALL: case iPUSH: case iPOP:
  968. if (! Imm) {
  969. if (pIcode.ll()->getOpcode() == iPOP)
  970. def(DST, pIcode, this, pstate, cb, ix);
  971. else
  972. use(DST, pIcode, this, pstate, cb, ix);
  973. }
  974. break;
  975. case iESC: /* operands may be larger */
  976. use(DST, pIcode, this, pstate, cb, ix);
  977. break;
  978. case iLDS: case iLES:
  979. pIcode.du.def |= duReg[(pIcode.ll()->getOpcode() == iLDS) ? rDS : rES];
  980. pIcode.du1.numRegsDef++;
  981. cb = 4;
  982. case iMOV:
  983. use(SRC, pIcode, this, pstate, cb, ix);
  984. def(DST, pIcode, this, pstate, cb, ix);
  985. break;
  986. case iLEA:
  987. use(SRC, pIcode, this, pstate, 2, ix);
  988. def(DST, pIcode, this, pstate, 2, ix);
  989. break;
  990. case iBOUND:
  991. use(SRC, pIcode, this, pstate, 4, ix);
  992. use(DST, pIcode, this, pstate, cb, ix);
  993. break;
  994. case iJMPF:
  995. cb = 4;
  996. case iJMP:
  997. if (! Imm)
  998. use(SRC, pIcode, this, pstate, cb, ix);
  999. break;
  1000. case iLOOP: case iLOOPE: case iLOOPNE:
  1001. pIcode.du.def |= duReg[rCX];
  1002. pIcode.du1.numRegsDef++;
  1003. case iJCXZ:
  1004. pIcode.du.use |= duReg[rCX];
  1005. break;
  1006. case iREPNE_CMPS: case iREPE_CMPS: case iREP_MOVS:
  1007. pIcode.du.addDefinedAndUsed(rCX);
  1008. pIcode.du1.numRegsDef++;
  1009. case iCMPS: case iMOVS:
  1010. pIcode.du.addDefinedAndUsed(rSI);
  1011. pIcode.du.addDefinedAndUsed(rDI);
  1012. pIcode.du1.numRegsDef += 2;
  1013. pIcode.du.use |= duReg[rES] | duReg[sseg];
  1014. break;
  1015. case iREPNE_SCAS: case iREPE_SCAS: case iREP_STOS: case iREP_INS:
  1016. pIcode.du.addDefinedAndUsed(rCX);
  1017. pIcode.du1.numRegsDef++;
  1018. case iSCAS: case iSTOS: case iINS:
  1019. pIcode.du.def |= duReg[rDI];
  1020. pIcode.du1.numRegsDef++;
  1021. if (pIcode.ll()->getOpcode() == iREP_INS || pIcode.ll()->getOpcode()== iINS)
  1022. {
  1023. pIcode.du.use |= duReg[rDI] | duReg[rES] | duReg[rDX];
  1024. }
  1025. else
  1026. {
  1027. pIcode.du.use |= duReg[rDI] | duReg[rES] | duReg[(cb == 2)? rAX: rAL];
  1028. }
  1029. break;
  1030. case iREP_LODS:
  1031. pIcode.du.addDefinedAndUsed(rCX);
  1032. pIcode.du1.numRegsDef++;
  1033. case iLODS:
  1034. pIcode.du.addDefinedAndUsed(rSI);
  1035. pIcode.du.def |= duReg[(cb==2)? rAX: rAL];
  1036. pIcode.du1.numRegsDef += 2;
  1037. pIcode.du.use |= duReg[sseg];
  1038. break;
  1039. case iREP_OUTS:
  1040. pIcode.du.addDefinedAndUsed(rCX);
  1041. pIcode.du1.numRegsDef++;
  1042. case iOUTS:
  1043. pIcode.du.addDefinedAndUsed(rSI);
  1044. pIcode.du1.numRegsDef++;
  1045. pIcode.du.use |= duReg[rDX] | duReg[sseg];
  1046. break;
  1047. case iIN: case iOUT:
  1048. def(DST, pIcode, this, pstate, cb, ix);
  1049. if (! Imm)
  1050. {
  1051. pIcode.du.use |= duReg[rDX];
  1052. }
  1053. break;
  1054. }
  1055. for (int i = rSP; i <= rBH; i++) /* Kill all defined registers */
  1056. if (pIcode.ll()->flagDU.d & (1 << i))
  1057. pstate->f[i] = false;
  1058. }