list.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include "l.h"
  2. void
  3. listinit(void)
  4. {
  5. fmtinstall('R', Rconv);
  6. fmtinstall('A', Aconv);
  7. fmtinstall('D', Dconv);
  8. fmtinstall('S', Sconv);
  9. fmtinstall('P', Pconv);
  10. }
  11. static Prog *bigP;
  12. int
  13. Pconv(Fmt *fp)
  14. {
  15. char str[STRINGSZ];
  16. Prog *p;
  17. p = va_arg(fp->args, Prog*);
  18. bigP = p;
  19. switch(p->as) {
  20. case ATEXT:
  21. if(p->from.scale) {
  22. sprint(str, "(%ld) %A %D,%d,%D",
  23. p->line, p->as, &p->from, p->from.scale, &p->to);
  24. break;
  25. }
  26. default:
  27. sprint(str, "(%ld) %A %D,%D",
  28. p->line, p->as, &p->from, &p->to);
  29. break;
  30. case ADATA:
  31. case AINIT:
  32. case ADYNT:
  33. sprint(str, "(%ld) %A %D/%d,%D",
  34. p->line, p->as, &p->from, p->from.scale, &p->to);
  35. break;
  36. }
  37. bigP = P;
  38. return fmtstrcpy(fp, str);
  39. }
  40. int
  41. Aconv(Fmt *fp)
  42. {
  43. int i;
  44. i = va_arg(fp->args, int);
  45. return fmtstrcpy(fp, anames[i]);
  46. }
  47. int
  48. Dconv(Fmt *fp)
  49. {
  50. char str[40], s[20];
  51. Adr *a;
  52. int i;
  53. a = va_arg(fp->args, Adr*);
  54. i = a->type;
  55. if(i >= D_INDIR) {
  56. if(a->offset)
  57. sprint(str, "%ld(%R)", a->offset, i-D_INDIR);
  58. else
  59. sprint(str, "(%R)", i-D_INDIR);
  60. goto brk;
  61. }
  62. switch(i) {
  63. default:
  64. sprint(str, "%R", i);
  65. break;
  66. case D_NONE:
  67. str[0] = 0;
  68. break;
  69. case D_BRANCH:
  70. if(bigP != P && bigP->pcond != P)
  71. if(a->sym != S)
  72. sprint(str, "%lux+%s", bigP->pcond->pc,
  73. a->sym->name);
  74. else
  75. sprint(str, "%lux", bigP->pcond->pc);
  76. else
  77. sprint(str, "%ld(PC)", a->offset);
  78. break;
  79. case D_EXTERN:
  80. sprint(str, "%s+%ld(SB)", a->sym->name, a->offset);
  81. break;
  82. case D_STATIC:
  83. sprint(str, "%s<%d>+%ld(SB)", a->sym->name,
  84. a->sym->version, a->offset);
  85. break;
  86. case D_AUTO:
  87. sprint(str, "%s+%ld(SP)", a->sym->name, a->offset);
  88. break;
  89. case D_PARAM:
  90. if(a->sym)
  91. sprint(str, "%s+%ld(FP)", a->sym->name, a->offset);
  92. else
  93. sprint(str, "%ld(FP)", a->offset);
  94. break;
  95. case D_CONST:
  96. sprint(str, "$%ld", a->offset);
  97. break;
  98. case D_FCONST:
  99. sprint(str, "$(%.8lux,%.8lux)", a->ieee.h, a->ieee.l);
  100. break;
  101. case D_SCONST:
  102. sprint(str, "$\"%S\"", a->scon);
  103. break;
  104. case D_ADDR:
  105. a->type = a->index;
  106. a->index = D_NONE;
  107. sprint(str, "$%D", a);
  108. a->index = a->type;
  109. a->type = D_ADDR;
  110. goto conv;
  111. }
  112. brk:
  113. if(a->index != D_NONE) {
  114. sprint(s, "(%R*%d)", a->index, a->scale);
  115. strcat(str, s);
  116. }
  117. conv:
  118. return fmtstrcpy(fp, str);
  119. }
  120. char* regstr[] =
  121. {
  122. "AL", /* [D_AL] */
  123. "CL",
  124. "DL",
  125. "BL",
  126. "AH",
  127. "CH",
  128. "DH",
  129. "BH",
  130. "AX", /* [D_AX] */
  131. "CX",
  132. "DX",
  133. "BX",
  134. "SP",
  135. "BP",
  136. "SI",
  137. "DI",
  138. "F0", /* [D_F0] */
  139. "F1",
  140. "F2",
  141. "F3",
  142. "F4",
  143. "F5",
  144. "F6",
  145. "F7",
  146. "CS", /* [D_CS] */
  147. "SS",
  148. "DS",
  149. "ES",
  150. "FS",
  151. "GS",
  152. "GDTR", /* [D_GDTR] */
  153. "IDTR", /* [D_IDTR] */
  154. "LDTR", /* [D_LDTR] */
  155. "MSW", /* [D_MSW] */
  156. "TASK", /* [D_TASK] */
  157. "CR0", /* [D_CR] */
  158. "CR1",
  159. "CR2",
  160. "CR3",
  161. "CR4",
  162. "CR5",
  163. "CR6",
  164. "CR7",
  165. "DR0", /* [D_DR] */
  166. "DR1",
  167. "DR2",
  168. "DR3",
  169. "DR4",
  170. "DR5",
  171. "DR6",
  172. "DR7",
  173. "TR0", /* [D_TR] */
  174. "TR1",
  175. "TR2",
  176. "TR3",
  177. "TR4",
  178. "TR5",
  179. "TR6",
  180. "TR7",
  181. "NONE", /* [D_NONE] */
  182. };
  183. int
  184. Rconv(Fmt *fp)
  185. {
  186. char str[20];
  187. int r;
  188. r = va_arg(fp->args, int);
  189. if(r >= D_AL && r <= D_NONE)
  190. sprint(str, "%s", regstr[r-D_AL]);
  191. else
  192. sprint(str, "gok(%d)", r);
  193. return fmtstrcpy(fp, str);
  194. }
  195. int
  196. Sconv(Fmt *fp)
  197. {
  198. int i, c;
  199. char str[30], *p, *a;
  200. a = va_arg(fp->args, char*);
  201. p = str;
  202. for(i=0; i<sizeof(double); i++) {
  203. c = a[i] & 0xff;
  204. if(c >= 'a' && c <= 'z' ||
  205. c >= 'A' && c <= 'Z' ||
  206. c >= '0' && c <= '9') {
  207. *p++ = c;
  208. continue;
  209. }
  210. *p++ = '\\';
  211. switch(c) {
  212. default:
  213. if(c < 040 || c >= 0177)
  214. break; /* not portable */
  215. p[-1] = c;
  216. continue;
  217. case 0:
  218. *p++ = 'z';
  219. continue;
  220. case '\\':
  221. case '"':
  222. *p++ = c;
  223. continue;
  224. case '\n':
  225. *p++ = 'n';
  226. continue;
  227. case '\t':
  228. *p++ = 't';
  229. continue;
  230. }
  231. *p++ = (c>>6) + '0';
  232. *p++ = ((c>>3) & 7) + '0';
  233. *p++ = (c & 7) + '0';
  234. }
  235. *p = 0;
  236. return fmtstrcpy(fp, str);
  237. }
  238. void
  239. diag(char *fmt, ...)
  240. {
  241. char buf[STRINGSZ], *tn;
  242. va_list arg;
  243. tn = "??none??";
  244. if(curtext != P && curtext->from.sym != S)
  245. tn = curtext->from.sym->name;
  246. va_start(arg, fmt);
  247. vseprint(buf, buf+sizeof(buf), fmt, arg);
  248. va_end(arg);
  249. print("%s: %s\n", tn, buf);
  250. nerrors++;
  251. if(nerrors > 20 && !debug['A']) {
  252. print("too many errors\n");
  253. errorexit();
  254. }
  255. }