disassem.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /****************************************************************************
  2. * dcc project disassembler
  3. * (C) Cristina Cifuentes, Mike van Emmerik, Jeff Ledermann
  4. ****************************************************************************/
  5. #include <stdint.h>
  6. #include <vector>
  7. #include <map>
  8. #include <sstream>
  9. #include <iomanip>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <malloc.h> /* For free() */
  13. #include "dcc.h"
  14. #include "symtab.h"
  15. #include "disassem.h"
  16. // Note: for the time being, there is no interactive disassembler
  17. // for unix
  18. #ifndef __UNIX__
  19. #include <conio.h> // getch() etc
  20. #endif
  21. using namespace std;
  22. #define POS_LAB 15 /* Position of label */
  23. #define POS_OPC 20 /* Position of opcode */
  24. #define POS_OPR 25 /* Position of operand */
  25. #define WID_PTR 10 /* Width of the "xword ptr" lingo */
  26. #define POS_OPR2 POS_OPR+WID_PTR /* Position of operand after "xword ptr" */
  27. #define POS_CMT 54 /* Position of comment */
  28. #define DELTA_ICODE 16 /* Number of icodes to realloc by each time */
  29. static const char *szOps[] =
  30. {
  31. "CBW", "AAA", "AAD", "AAM", "AAS", "ADC", "ADD", "AND",
  32. "BOUND","CALL", "CALL", "CLC", "CLD", "CLI", "CMC", "CMP",
  33. "CMPS", "REPNE CMPS","REPE CMPS","DAA", "DAS", "DEC", "DIV", "ENTER",
  34. "ESC", "HLT", "IDIV", "IMUL", "IN", "INC", "INS", "REP INS",
  35. "INT", "IRET", "JB", "JBE", "JAE", "JA", "JE", "JNE",
  36. "JL", "JGE", "JLE", "JG", "JS", "JNS", "JO", "JNO",
  37. "JP", "JNP", "JCXZ", "JMP", "JMP", "LAHF", "LDS", "LEA",
  38. "LEAVE","LES", "LOCK", "LODS", "REP LODS", "LOOP", "LOOPE","LOOPNE",
  39. "MOV", "MOVS", "REP MOVS", "MUL", "NEG", "NOT", "OR", "OUT",
  40. "OUTS", "REP OUTS", "POP", "POPA", "POPF", "PUSH", "PUSHA","PUSHF",
  41. "RCL", "RCR", "ROL", "ROR", "RET", "RETF", "SAHF", "SAR",
  42. "SHL", "SHR", "SBB", "SCAS", "REPNE SCAS","REPE SCAS", "CWD", "STC",
  43. "STD", "STI", "STOS", "REP STOS", "SUB", "TEST", "WAIT", "XCHG",
  44. "XLAT", "XOR", "INTO", "NOP", "REPNE", "REPE", "MOD"
  45. };
  46. /* The following opcodes are for mod != 3 */
  47. static const char *szFlops1[] =
  48. {
  49. /* 0 1 2 3 4 5 6 7 */
  50. "FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 00 */
  51. "FLD", "???", "FST", "???", "FLDENV","FLDCW", "FSTENV","FSTSW", /* 08 */
  52. "FIADD", "FIMUL", "FICOM","FICOMP","FISUB", "FISUBR","FIDIV", "FIDIVR", /* 10 */
  53. "FILD", "???", "FIST", "FISTP", "???", "???", "???", "FSTP", /* 18 */
  54. "FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 20 */
  55. "FLD", "FLD", "FST", "FSTP", "FRESTOR","???", "FSAVE", "FSTSW", /* 28 */
  56. "FIADD", "FIMUL", "FICOM","FICOMP","FISUB", "FISUBR","FIDIV", "FIDIVR", /* 30 */
  57. "FILD", "???", "FIST", "FISTP", "FBLD", "???", "FBSTP", "FISTP" /* 38 */
  58. };
  59. /* The following opcodes are for mod == 3 */
  60. static const char *szFlops2[] =
  61. {
  62. /* 0 1 2 3 4 5 6 7 */
  63. "FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 00 */
  64. "FLD", "FXCH", "FNOP", "???", "", "", "", "", /* 08 */
  65. "FIADD", "FIMUL", "FICOM","FICOMP","FISUB", "", "FIDIV", "FIDIVR", /* 10 */
  66. "FILD", "???", "FIST", "FISTP", "???", "???", "???", "FSTP", /* 18 */
  67. "FADD", "FMUL", "FCOM", "FCOMP", "FSUB", "FSUBR", "FDIV", "FDIVR", /* 20 */
  68. "FFREE", "FSTP", "FST", "???", "FUCOM", "FUCOMP","???", "???", /* 28 */
  69. "FADDP", "FMULP", "FICOM","", "FSUBRP","FISUBR","FDIVRP","FDIVP", /* 30 */
  70. "FILD", "???", "FIST", "FISTP", "", "???", "FBSTP", "FISTP" /* 38 */
  71. };
  72. static const char *szFlops0C[] =
  73. {
  74. "FCHS", "FABS", "???", "???", "FTST", "FXAM", "???", "???"
  75. };
  76. static const char *szFlops0D[] =
  77. {
  78. "FLD1", "FLDL2T","FLDL2E","FLDP1", "FLDLG2","FLDLN2","FLDZ", "???"
  79. };
  80. static const char *szFlops0E[] =
  81. {
  82. "F2XM1", "FYL2X", "FPTAN", "FPATAN","FXTRACT","FPREM1","FDECSTP","FINCSTP"
  83. };
  84. static const char *szFlops0F[] =
  85. {
  86. "FPREM", "FYLXP1","FSQRT", "FSINCOS","FRNDINT","FSCALE","FSIN","FCOS"
  87. };
  88. static const char *szFlops15[] =
  89. {
  90. "???", "FUCOMPP", "???", "???", "???", "???", "???", "???"
  91. };
  92. static const char *szFlops1C[] =
  93. {
  94. "???", "???", "FCLEX", "FINIT", "FTST", "FXAM", "???", "???"
  95. };
  96. static const char *szFlops33[] =
  97. {
  98. "???", "FCOMPP", "???", "???", "???", "???", "???", "???"
  99. };
  100. static const char *szFlops3C[] =
  101. {
  102. "FSTSWAX","???", "???", "???", "???", "???", "???", "???"
  103. };
  104. static const char *szIndex[8] = {"bx+si", "bx+di", "bp+si", "bp+di", "si", "di","bp","bx" };
  105. static const char *szBreg[8] = { "al", "cl", "dl", "bl", "ah", "ch", "dh", "bh" };
  106. static const char *szWreg[12] = { "ax", "cx", "dx", "bx", "sp", "bp", "si", "di",
  107. "es", "cs", "ss", "ds" };
  108. static const char *szPtr[2] = { "word ptr ", "byte ptr " };
  109. void dis1LineOp(int i, boolT fWin, char attr, uint16_t *len, Function * pProc);
  110. static void formatRM(ostringstream &p, uint32_t flg, const LLOperand &pm);
  111. static ostringstream &strDst(ostringstream &os, uint32_t flg, LLOperand &pm);
  112. static char *strHex(uint32_t d);
  113. static int checkScanned(uint32_t pcCur);
  114. static void setProc(Function * proc);
  115. static void dispData(uint16_t dataSeg);
  116. boolT callArg(uint16_t off, char *temp); /* Check for procedure name */
  117. static FILE *fp;
  118. static CIcodeRec pc;
  119. static int cb, j, numIcode, allocIcode;
  120. static map<int,int> pl;
  121. static uint32_t nextInst;
  122. static boolT fImpure;
  123. static int g_lab, prevPass;
  124. static Function * pProc; /* Points to current proc struct */
  125. struct POSSTACK_ENTRY
  126. {
  127. int ic; /* An icode offset */
  128. Function * pProc; /* A pointer to a PROCEDURE structure */
  129. } ;
  130. vector<POSSTACK_ENTRY> posStack; /* position stack */
  131. uint8_t iPS; /* Index into the stack */
  132. //static char cbuf[256]; /* Has to be 256 for wgetstr() to work */
  133. // These are "curses equivalent" functions. (Used to use curses for all this,
  134. // but it was too much of a distribution hassle
  135. #define printfd(x) printf(x)
  136. #define dis_newline() printf("\n")
  137. #define dis_show() // Nothing to do unless using Curses
  138. void LLInst::findJumpTargets(CIcodeRec &_pc)
  139. {
  140. if (testFlags(I) && ! testFlags(JMP_ICODE) && isJmpInst())
  141. {
  142. /* Replace the immediate operand with an icode index */
  143. iICODE labTgt=_pc.labelSrch(src.op());
  144. if (labTgt!=_pc.end())
  145. {
  146. src.SetImmediateOp(labTgt->loc_ip);
  147. /* This icode is the target of a jump */
  148. labTgt->ll()->setFlags(TARGET);
  149. setFlags(JMP_ICODE); /* So its not done twice */
  150. }
  151. else
  152. {
  153. /* This jump cannot be linked to a label */
  154. setFlags(NO_LABEL);
  155. }
  156. }
  157. }
  158. /*****************************************************************************
  159. * disassem - Prints a disassembled listing of a procedure.
  160. * pass == 1 generates output on file .a1
  161. * pass == 2 generates output on file .a2
  162. * pass == 3 generates output on file .b
  163. ****************************************************************************/
  164. void disassem(int pass, Function * ppProc)
  165. {
  166. pProc = ppProc; /* Save the passes pProc */
  167. if (pass != prevPass)
  168. {
  169. prevPass = pass;
  170. g_lab = 0; /* Restart label numbers */
  171. }
  172. createSymTables();
  173. allocIcode = numIcode = pProc->Icode.size();
  174. cb = allocIcode * sizeof(ICODE);
  175. if (numIcode == 0)
  176. {
  177. return; /* No Icode */
  178. }
  179. /* Open the output file (.a1 or .a2 only) */
  180. if (pass != 3)
  181. {
  182. auto p = (pass == 1)? asm1_name: asm2_name;
  183. fp = fopen(p, "a+");
  184. if (!fp)
  185. {
  186. fatalError(CANNOT_OPEN, p);
  187. }
  188. }
  189. pc=pProc->Icode;
  190. /* Create temporary code array */
  191. // Mike: needs objectising!
  192. //pc = (ICODE *)memcpy(allocMem(cb), pProc->Icode.GetFirstIcode(), (size_t)cb);
  193. if (pass == 1)
  194. {
  195. /* Bind jump offsets to labels */
  196. //for (i = 0; i < numIcode; i++)
  197. #ifdef _lint
  198. for (auto i=pc.begin(); i!=pc.end(); ++i)
  199. {
  200. ICODE &icode(*i);
  201. #else
  202. for( ICODE &icode : pc)
  203. {
  204. #endif
  205. LLInst *ll=icode.ll();
  206. ll->findJumpTargets(pc);
  207. }
  208. }
  209. /* Create label array to keep track of location => label name */
  210. pl.clear();
  211. /* Write procedure header */
  212. if (pass != 3)
  213. fprintf(fp, "\t\t%s PROC %s\n", pProc->name.c_str(), (pProc->flg & PROC_FAR)? "FAR": "NEAR");
  214. /* Loop over array printing each record */
  215. nextInst = 0;
  216. #ifdef _lint
  217. for (auto i=pc.begin(); i!=pc.end(); ++i)
  218. {
  219. ICODE &icode(*i);
  220. #else
  221. for( ICODE &icode : pc)
  222. {
  223. #endif
  224. icode.ll()->dis1Line(icode.loc_ip,pass);
  225. }
  226. /* Write procedure epilogue */
  227. if (pass != 3)
  228. {
  229. fprintf(fp, "\n\t\t%s ENDP\n\n", pProc->name.c_str());
  230. fclose(fp);
  231. }
  232. pc.clear();
  233. destroySymTables();
  234. }
  235. /****************************************************************************
  236. * dis1Line() - disassemble one line to stream fp * *
  237. * i is index into Icode for this proc *
  238. * It is assumed that icode i is already scanned *
  239. ****************************************************************************/
  240. void LLInst::dis1Line(int loc_ip, int pass)
  241. {
  242. ostringstream oper_stream;
  243. ostringstream hex_bytes;
  244. ostringstream result_stream;
  245. ostringstream opcode_with_mods;
  246. ostringstream operands_s;
  247. oper_stream << uppercase;
  248. hex_bytes << uppercase;
  249. /* Disassembly stage 1 --
  250. * Do not try to display NO_CODE entries or synthetic instructions,
  251. * other than JMPs, that have been introduced for def/use analysis. */
  252. if ((option.asm1) &&
  253. ( testFlags(NO_CODE) ||
  254. (testFlags(SYNTHETIC) && (opcode != iJMP))))
  255. {
  256. return;
  257. }
  258. else if (testFlags(NO_CODE))
  259. {
  260. return;
  261. }
  262. if (testFlags(TARGET | CASE))
  263. {
  264. if (pass == 3)
  265. cCode.appendCode("\n"); /* Print to c code buffer */
  266. else
  267. fprintf(fp, "\n"); /* No, print to the stream */
  268. }
  269. /* Find next instruction label and print hex bytes */
  270. if (testFlags(SYNTHETIC))
  271. nextInst = label;
  272. else
  273. {
  274. cb = (uint32_t) numBytes;
  275. nextInst = label + cb;
  276. /* Output hexa code in program image */
  277. if (pass != 3)
  278. {
  279. for (j = 0; j < cb; j++)
  280. {
  281. hex_bytes << hex << setw(2) << setfill('0') << uint16_t(prog.Image[label + j]);
  282. }
  283. hex_bytes << ' ';
  284. }
  285. }
  286. oper_stream << setw(POS_LAB) << left<< hex_bytes.str();
  287. /* Check if there is a symbol here */
  288. selectTable(Label);
  289. oper_stream << setw(5)<<left; // align for the labels
  290. {
  291. ostringstream lab_contents;
  292. if (readVal(lab_contents, label, 0))
  293. {
  294. lab_contents << ':'; /* Also removes the null */
  295. }
  296. else if (testFlags(TARGET)) /* Symbols override Lnn labels */
  297. {
  298. /* Print label */
  299. if (pl.count(loc_ip)==0)
  300. {
  301. pl[loc_ip] = ++g_lab;
  302. }
  303. lab_contents<< "L"<<pl[loc_ip]<<':';
  304. }
  305. oper_stream<< lab_contents.str();
  306. }
  307. if (opcode == iSIGNEX && testFlags(B))
  308. {
  309. opcode = iCBW;
  310. }
  311. opcode_with_mods<<szOps[opcode];
  312. switch (opcode)
  313. {
  314. case iADD: case iADC: case iSUB: case iSBB: case iAND: case iOR:
  315. case iXOR: case iTEST: case iCMP: case iMOV: case iLEA: case iXCHG:
  316. strDst(operands_s,getFlag(), dst);
  317. strSrc(operands_s);
  318. break;
  319. case iESC:
  320. flops(operands_s);
  321. break;
  322. case iSAR: case iSHL: case iSHR: case iRCL: case iRCR: case iROL:
  323. case iROR:
  324. strDst(operands_s,getFlag() | I, dst);
  325. if(testFlags(I))
  326. strSrc(operands_s);
  327. else
  328. operands_s<<", cl";
  329. break;
  330. case iINC: case iDEC: case iNEG: case iNOT: case iPOP:
  331. strDst(operands_s,getFlag() | I, dst);
  332. break;
  333. case iPUSH:
  334. if (testFlags(I))
  335. {
  336. operands_s<<strHex(src.op());
  337. // strcpy(p + WID_PTR, strHex(pIcode->ll()->immed.op));
  338. }
  339. else
  340. {
  341. strDst(operands_s,getFlag() | I, dst);
  342. }
  343. break;
  344. case iDIV: case iIDIV: case iMUL: case iIMUL: case iMOD:
  345. if (testFlags(I))
  346. {
  347. strDst(operands_s,getFlag(), dst) <<", ";
  348. formatRM(operands_s, getFlag(), src);
  349. strSrc(operands_s);
  350. }
  351. else
  352. strDst(operands_s,getFlag() | I, src);
  353. break;
  354. case iLDS: case iLES: case iBOUND:
  355. strDst(operands_s,getFlag(), dst)<<", dword ptr";
  356. strSrc(operands_s,true);
  357. break;
  358. case iJB: case iJBE: case iJAE: case iJA:
  359. case iJL: case iJLE: case iJGE: case iJG:
  360. case iJE: case iJNE: case iJS: case iJNS:
  361. case iJO: case iJNO: case iJP: case iJNP:
  362. case iJCXZ:case iLOOP: case iLOOPE:case iLOOPNE:
  363. case iJMP: case iJMPF:
  364. /* Check if there is a symbol here */
  365. {
  366. ICODE *lab=pc.GetIcode(src.op());
  367. selectTable(Label);
  368. if ((src.op() < (uint32_t)numIcode) && /* Ensure in range */
  369. readVal(operands_s, lab->ll()->label, 0))
  370. {
  371. break; /* Symbolic label. Done */
  372. }
  373. }
  374. if (testFlags(NO_LABEL))
  375. {
  376. //strcpy(p + WID_PTR, strHex(pIcode->ll()->immed.op));
  377. operands_s<<strHex(src.op());
  378. }
  379. else if (testFlags(I) )
  380. {
  381. j = src.op();
  382. if (pl.count(j)==0) /* Forward jump */
  383. {
  384. pl[j] = ++g_lab;
  385. }
  386. if (opcode == iJMPF)
  387. {
  388. operands_s<<" far ptr ";
  389. }
  390. operands_s<<"L"<<pl[j];
  391. }
  392. else if (opcode == iJMPF)
  393. {
  394. operands_s<<"dword ptr";
  395. strSrc(operands_s,true);
  396. }
  397. else
  398. {
  399. strDst(operands_s,I, src);
  400. }
  401. break;
  402. case iCALL: case iCALLF:
  403. if (testFlags(I))
  404. {
  405. if((opcode == iCALL))
  406. operands_s<< "near";
  407. else
  408. operands_s<< " far";
  409. operands_s<<" ptr "<<(src.proc.proc)->name;
  410. }
  411. else if (opcode == iCALLF)
  412. {
  413. operands_s<<"dword ptr ";
  414. strSrc(operands_s,true);
  415. }
  416. else
  417. strDst(operands_s,I, src);
  418. break;
  419. case iENTER:
  420. operands_s<<strHex(dst.off)<<", ";
  421. operands_s<<strHex(src.op());
  422. break;
  423. case iRET: case iRETF: case iINT:
  424. if (testFlags(I))
  425. {
  426. operands_s<<strHex(src.op());
  427. }
  428. break;
  429. case iCMPS: case iREPNE_CMPS: case iREPE_CMPS:
  430. case iSCAS: case iREPNE_SCAS: case iREPE_SCAS:
  431. case iSTOS: case iREP_STOS:
  432. case iLODS: case iREP_LODS:
  433. case iMOVS: case iREP_MOVS:
  434. case iINS: case iREP_INS:
  435. case iOUTS: case iREP_OUTS:
  436. if (src.segOver)
  437. {
  438. bool is_dx_src=(opcode == iOUTS || opcode == iREP_OUTS);
  439. if(is_dx_src)
  440. operands_s<<"dx, "<<szPtr[getFlag() & B];
  441. else
  442. operands_s<<szPtr[getFlag() & B];
  443. if (opcode == iLODS ||
  444. opcode == iREP_LODS ||
  445. opcode == iOUTS ||
  446. opcode == iREP_OUTS)
  447. {
  448. operands_s<<szWreg[src.segOver-rAX];
  449. }
  450. else
  451. {
  452. operands_s<<"es:[di], "<<szWreg[src.segOver - rAX];
  453. }
  454. operands_s<<":[si]";
  455. }
  456. else
  457. {
  458. (getFlag() & B)? opcode_with_mods<< "B": opcode_with_mods<< "W";
  459. }
  460. break;
  461. case iXLAT:
  462. if (src.segOver)
  463. {
  464. operands_s<<" "<<szPtr[1];
  465. operands_s<<szWreg[src.segOver-rAX]<<":[bx]";
  466. }
  467. break;
  468. case iIN:
  469. (getFlag() & B)? operands_s<<"al, " : operands_s<< "ax, ";
  470. (testFlags(I))? operands_s << strHex(src.op()) : operands_s<< "dx";
  471. break;
  472. case iOUT:
  473. {
  474. std::string d1=((testFlags(I))? strHex(src.op()): "dx");
  475. std::string d2=((getFlag() & B) ? ", al": ", ax");
  476. operands_s<<d1 << d2;
  477. }
  478. break;
  479. default:
  480. break;
  481. }
  482. oper_stream << setw(15) << left <<opcode_with_mods.str();
  483. oper_stream << operands_s.str();
  484. /* Comments */
  485. if (testFlags(SYNTHETIC))
  486. {
  487. fImpure = FALSE;
  488. }
  489. else
  490. {
  491. for (j = label, fImpure = 0; j > 0 && j < (int)nextInst; j++)
  492. {
  493. fImpure |= BITMAP(j, BM_DATA);
  494. }
  495. }
  496. result_stream << setw(54) << left << oper_stream.str();
  497. /* Check for user supplied comment */
  498. selectTable(Comment);
  499. ostringstream cbuf;
  500. if (readVal(cbuf, label, 0))
  501. {
  502. result_stream <<"; "<<cbuf.str();
  503. }
  504. else if (fImpure || (testFlags(SWITCH | CASE | SEG_IMMED | IMPURE | SYNTHETIC | TERMINATES)))
  505. {
  506. if (testFlags(CASE))
  507. {
  508. result_stream << ";Case l"<< caseTbl.numEntries;
  509. }
  510. if (testFlags(SWITCH))
  511. {
  512. result_stream << ";Switch ";
  513. }
  514. if (fImpure)
  515. {
  516. result_stream << ";Accessed as data ";
  517. }
  518. if (testFlags(IMPURE))
  519. {
  520. result_stream << ";Impure operand ";
  521. }
  522. if (testFlags(SEG_IMMED))
  523. {
  524. result_stream << ";Segment constant";
  525. }
  526. if (testFlags(TERMINATES))
  527. {
  528. result_stream << ";Exit to DOS";
  529. }
  530. }
  531. /* Comment on iINT icodes */
  532. if (opcode == iINT)
  533. writeIntComment(result_stream);
  534. /* Display output line */
  535. if(pass==3)
  536. {
  537. /* output to .b code buffer */
  538. if (testFlags(SYNTHETIC))
  539. result_stream<<";Synthetic inst";
  540. if (pass == 3) /* output to .b code buffer */
  541. cCode.appendCode("%s\n", result_stream.str().c_str());
  542. }
  543. else
  544. {
  545. if (not testFlags(SYNTHETIC) )
  546. {
  547. /* output to .a1 or .a2 file */
  548. fprintf (fp, "%03ld %06lX %s\n", loc_ip, label, result_stream.str().c_str());
  549. }
  550. else /* SYNTHETIC instruction */
  551. {
  552. result_stream<<";Synthetic inst";
  553. fprintf (fp, "%03ld %s\n", loc_ip, result_stream.str().c_str());
  554. }
  555. }
  556. }
  557. /****************************************************************************
  558. * formatRM
  559. ***************************************************************************/
  560. static void formatRM(std::ostringstream &p, uint32_t flg, const LLOperand &pm)
  561. {
  562. char seg[4];
  563. if (pm.segOver)
  564. {
  565. strcat(strcpy(seg, szWreg[pm.segOver - rAX]), ":");
  566. }
  567. else *seg = '\0';
  568. if (pm.regi == 0)
  569. {
  570. p<<seg<<"["<<strHex((uint32_t)pm.off)<<"]";
  571. }
  572. else if (pm.regi == (INDEX_BX_SI - 1))
  573. {
  574. p<<"tmp";
  575. }
  576. else if (pm.regi < INDEX_BX_SI)
  577. {
  578. if(flg & B)
  579. p << szBreg[pm.regi - rAL];
  580. else
  581. p << szWreg[pm.regi - rAX];
  582. }
  583. else if (pm.off)
  584. {
  585. if (pm.off < 0)
  586. {
  587. p <<seg<<"["<<szIndex[pm.regi - INDEX_BX_SI]<<"-"<<strHex((uint32_t)(- pm.off))<<"]";
  588. }
  589. else
  590. {
  591. p <<seg<<"["<<szIndex[pm.regi - INDEX_BX_SI]<<"+"<<strHex((uint32_t)(pm.off))<<"]";
  592. }
  593. }
  594. else
  595. p <<seg<<"["<<szIndex[pm.regi - INDEX_BX_SI]<<"]";
  596. }
  597. /*****************************************************************************
  598. * strDst
  599. ****************************************************************************/
  600. static ostringstream & strDst(ostringstream &os,uint32_t flg, LLOperand &pm)
  601. {
  602. /* Immediates to memory require size descriptor */
  603. //os << setw(WID_PTR);
  604. if ((flg & I) && (pm.regi == 0 || pm.regi >= INDEX_BX_SI))
  605. os << szPtr[flg & B];
  606. formatRM(os, flg, pm);
  607. return os;
  608. }
  609. /****************************************************************************
  610. * strSrc *
  611. ****************************************************************************/
  612. ostringstream &LLInst::strSrc(ostringstream &os,bool skip_comma)
  613. {
  614. if(false==skip_comma)
  615. os<<", ";
  616. if (testFlags(I))
  617. os<<strHex(src.op());
  618. else if (testFlags(IM_SRC)) /* level 2 */
  619. os<<"dx:ax";
  620. else
  621. formatRM(os, getFlag(), src);
  622. return os;
  623. }
  624. /****************************************************************************
  625. * strHex *
  626. ****************************************************************************/
  627. static char *strHex(uint32_t d)
  628. {
  629. static char buf[10];
  630. d &= 0xFFFF;
  631. sprintf(buf, "0%lX%s", d, (d > 9)? "h": "");
  632. return (buf + (buf[1] <= '9'));
  633. }
  634. /****************************************************************************
  635. * interactDis - interactive disassembler *
  636. ****************************************************************************/
  637. void interactDis(Function * initProc, int initIC)
  638. {
  639. printf("Sorry - interactive disasassembler option not available for Unix\n");
  640. return;
  641. }
  642. /* Handle the floating point opcodes (icode iESC) */
  643. void LLInst::flops(std::ostringstream &out)
  644. {
  645. char bf[30];
  646. uint8_t op = (uint8_t)src.op();
  647. /* Note that op is set to the escape number, e.g.
  648. esc 0x38 is FILD */
  649. if ((dst.regi == 0) || (dst.regi >= INDEX_BX_SI))
  650. {
  651. /* The mod/rm mod bits are not set to 11 (i.e. register). This is the normal floating point opcode */
  652. out<<szFlops1[op]<<' ';
  653. out <<setw(10);
  654. if ((op == 0x29) || (op == 0x1F))
  655. {
  656. strcpy(bf, "tbyte ptr ");
  657. }
  658. else switch (op & 0x30)
  659. {
  660. case 0x00:
  661. case 0x10:
  662. strcpy(bf, "dword ptr ");
  663. break;
  664. case 0x20:
  665. strcpy(bf, "qword ptr ");
  666. break;
  667. case 0x30:
  668. switch (op)
  669. {
  670. case 0x3C: /* FBLD */
  671. case 0x3E: /* FBSTP */
  672. strcpy(bf, "tbyte ptr ");
  673. break;
  674. case 0x3D: /* FILD 64 bit */
  675. case 0x3F: /* FISTP 64 bit */
  676. strcpy(bf, "qword ptr ");
  677. break;
  678. default:
  679. strcpy(bf, "uint16_t ptr ");
  680. break;
  681. }
  682. }
  683. formatRM(out, getFlag(), dst);
  684. }
  685. else
  686. {
  687. /* The mod/rm mod bits are set to 11 (i.e. register).
  688. Could be specials (0x0C-0x0F, etc), or the st(i) versions of
  689. normal opcodes. Because the opcodes are slightly different for
  690. this case (e.g. op=04 means FSUB if reg != 3, but FSUBR for
  691. reg == 3), a separate table is used (szFlops2). */
  692. int destRegIdx=dst.regi - rAX;
  693. switch (op)
  694. {
  695. case 0x0C:
  696. out << szFlops0C[destRegIdx];
  697. break;
  698. case 0x0D:
  699. out << szFlops0D[destRegIdx];
  700. break;
  701. case 0x0E:
  702. out << szFlops0E[destRegIdx];
  703. break;
  704. case 0x0F:
  705. out << szFlops0F[destRegIdx];
  706. break;
  707. case 0x15:
  708. out << szFlops15[destRegIdx];
  709. break;
  710. case 0x1C:
  711. out << szFlops1C[destRegIdx];
  712. break;
  713. case 0x33:
  714. out << szFlops33[destRegIdx];
  715. break;
  716. case 0x3C:
  717. out << szFlops3C[destRegIdx];
  718. break;
  719. default:
  720. out << szFlops2[op];
  721. if ((op >= 0x20) && (op <= 0x27))
  722. {
  723. /* This is the ST(i), ST form. */
  724. out << "ST("<<destRegIdx - rAX<<"),ST";
  725. }
  726. else
  727. {
  728. /* ST, ST(i) */
  729. out << "ST,ST("<<destRegIdx;
  730. }
  731. break;
  732. }
  733. }
  734. }