list.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "l.h"
  10. void
  11. listinit(void)
  12. {
  13. fmtinstall('R', Rconv);
  14. fmtinstall('A', Aconv);
  15. fmtinstall('D', Dconv);
  16. fmtinstall('S', Sconv);
  17. fmtinstall('P', Pconv);
  18. }
  19. static Prog *bigP;
  20. int
  21. Pconv(Fmt *fp)
  22. {
  23. char str[STRINGSZ];
  24. Prog *p;
  25. p = va_arg(fp->args, Prog*);
  26. bigP = p;
  27. switch(p->as) {
  28. case ATEXT:
  29. if(p->from.scale) {
  30. sprint(str, "(%ld) %A %D,%d,%D",
  31. p->line, p->as, &p->from, p->from.scale, &p->to);
  32. break;
  33. }
  34. default:
  35. sprint(str, "(%ld) %A %D,%D",
  36. p->line, p->as, &p->from, &p->to);
  37. break;
  38. case ADATA:
  39. case AINIT:
  40. case ADYNT:
  41. sprint(str, "(%ld) %A %D/%d,%D",
  42. p->line, p->as, &p->from, p->from.scale, &p->to);
  43. break;
  44. }
  45. bigP = P;
  46. return fmtstrcpy(fp, str);
  47. }
  48. int
  49. Aconv(Fmt *fp)
  50. {
  51. int i;
  52. i = va_arg(fp->args, int);
  53. return fmtstrcpy(fp, anames[i]);
  54. }
  55. int
  56. Dconv(Fmt *fp)
  57. {
  58. char str[40], s[20];
  59. Adr *a;
  60. int i;
  61. a = va_arg(fp->args, Adr*);
  62. i = a->type;
  63. if(i >= D_INDIR) {
  64. if(a->offset)
  65. sprint(str, "%lld(%R)", a->offset, i-D_INDIR);
  66. else
  67. sprint(str, "(%R)", i-D_INDIR);
  68. goto brk;
  69. }
  70. switch(i) {
  71. default:
  72. if(a->offset)
  73. sprint(str, "$%lld,%R", a->offset, i);
  74. else
  75. sprint(str, "%R", i);
  76. break;
  77. case D_NONE:
  78. str[0] = 0;
  79. break;
  80. case D_BRANCH:
  81. if(bigP != P && bigP->pcond != P)
  82. if(a->sym != S)
  83. sprint(str, "%llux+%s", bigP->pcond->pc,
  84. a->sym->name);
  85. else
  86. sprint(str, "%llux", bigP->pcond->pc);
  87. else
  88. sprint(str, "%lld(PC)", a->offset);
  89. break;
  90. case D_EXTERN:
  91. sprint(str, "%s+%lld(SB)", a->sym->name, a->offset);
  92. break;
  93. case D_STATIC:
  94. sprint(str, "%s<%d>+%lld(SB)", a->sym->name,
  95. a->sym->version, a->offset);
  96. break;
  97. case D_AUTO:
  98. sprint(str, "%s+%lld(SP)", a->sym->name, a->offset);
  99. break;
  100. case D_PARAM:
  101. if(a->sym)
  102. sprint(str, "%s+%lld(%s)", a->sym->name, a->offset, paramspace);
  103. else
  104. sprint(str, "%lld(%s)", a->offset, paramspace);
  105. break;
  106. case D_CONST:
  107. sprint(str, "$%lld", a->offset);
  108. break;
  109. case D_FCONST:
  110. sprint(str, "$(%.8lux,%.8lux)", a->ieee.h, a->ieee.l);
  111. break;
  112. case D_SCONST:
  113. sprint(str, "$\"%S\"", a->scon);
  114. break;
  115. case D_ADDR:
  116. a->type = a->index;
  117. a->index = D_NONE;
  118. sprint(str, "$%D", a);
  119. a->index = a->type;
  120. a->type = D_ADDR;
  121. goto conv;
  122. }
  123. brk:
  124. if(a->index != D_NONE) {
  125. sprint(s, "(%R*%d)", a->index, a->scale);
  126. strcat(str, s);
  127. }
  128. conv:
  129. return fmtstrcpy(fp, str);
  130. }
  131. char* regstr[] =
  132. {
  133. "AL", /* [D_AL] */
  134. "CL",
  135. "DL",
  136. "BL",
  137. "SPB",
  138. "BPB",
  139. "SIB",
  140. "DIB",
  141. "R8B",
  142. "R9B",
  143. "R10B",
  144. "R11B",
  145. "R12B",
  146. "R13B",
  147. "R14B",
  148. "R15B",
  149. "AX", /* [D_AX] */
  150. "CX",
  151. "DX",
  152. "BX",
  153. "SP",
  154. "BP",
  155. "SI",
  156. "DI",
  157. "R8",
  158. "R9",
  159. "R10",
  160. "R11",
  161. "R12",
  162. "R13",
  163. "R14",
  164. "R15",
  165. "AH",
  166. "CH",
  167. "DH",
  168. "BH",
  169. "F0", /* [D_F0] */
  170. "F1",
  171. "F2",
  172. "F3",
  173. "F4",
  174. "F5",
  175. "F6",
  176. "F7",
  177. "M0",
  178. "M1",
  179. "M2",
  180. "M3",
  181. "M4",
  182. "M5",
  183. "M6",
  184. "M7",
  185. "X0",
  186. "X1",
  187. "X2",
  188. "X3",
  189. "X4",
  190. "X5",
  191. "X6",
  192. "X7",
  193. "X8",
  194. "X9",
  195. "X10",
  196. "X11",
  197. "X12",
  198. "X13",
  199. "X14",
  200. "X15",
  201. "CS", /* [D_CS] */
  202. "SS",
  203. "DS",
  204. "ES",
  205. "FS",
  206. "GS",
  207. "GDTR", /* [D_GDTR] */
  208. "IDTR", /* [D_IDTR] */
  209. "LDTR", /* [D_LDTR] */
  210. "MSW", /* [D_MSW] */
  211. "TASK", /* [D_TASK] */
  212. "CR0", /* [D_CR] */
  213. "CR1",
  214. "CR2",
  215. "CR3",
  216. "CR4",
  217. "CR5",
  218. "CR6",
  219. "CR7",
  220. "CR8",
  221. "CR9",
  222. "CR10",
  223. "CR11",
  224. "CR12",
  225. "CR13",
  226. "CR14",
  227. "CR15",
  228. "DR0", /* [D_DR] */
  229. "DR1",
  230. "DR2",
  231. "DR3",
  232. "DR4",
  233. "DR5",
  234. "DR6",
  235. "DR7",
  236. "TR0", /* [D_TR] */
  237. "TR1",
  238. "TR2",
  239. "TR3",
  240. "TR4",
  241. "TR5",
  242. "TR6",
  243. "TR7",
  244. "NONE", /* [D_NONE] */
  245. };
  246. int
  247. Rconv(Fmt *fp)
  248. {
  249. char str[20];
  250. int r;
  251. r = va_arg(fp->args, int);
  252. if(r >= D_AL && r <= D_NONE)
  253. sprint(str, "%s", regstr[r-D_AL]);
  254. else
  255. sprint(str, "gok(%d)", r);
  256. return fmtstrcpy(fp, str);
  257. }
  258. int
  259. Sconv(Fmt *fp)
  260. {
  261. int i, c;
  262. char str[30], *p, *a;
  263. a = va_arg(fp->args, char*);
  264. p = str;
  265. for(i=0; i<sizeof(double); i++) {
  266. c = a[i] & 0xff;
  267. if(c >= 'a' && c <= 'z' ||
  268. c >= 'A' && c <= 'Z' ||
  269. c >= '0' && c <= '9') {
  270. *p++ = c;
  271. continue;
  272. }
  273. *p++ = '\\';
  274. switch(c) {
  275. default:
  276. if(c < 040 || c >= 0177)
  277. break; /* not portable */
  278. p[-1] = c;
  279. continue;
  280. case 0:
  281. *p++ = 'z';
  282. continue;
  283. case '\\':
  284. case '"':
  285. *p++ = c;
  286. continue;
  287. case '\n':
  288. *p++ = 'n';
  289. continue;
  290. case '\t':
  291. *p++ = 't';
  292. continue;
  293. }
  294. *p++ = (c>>6) + '0';
  295. *p++ = ((c>>3) & 7) + '0';
  296. *p++ = (c & 7) + '0';
  297. }
  298. *p = 0;
  299. return fmtstrcpy(fp, str);
  300. }
  301. void
  302. diag(char *fmt, ...)
  303. {
  304. char buf[STRINGSZ], *tn;
  305. va_list arg;
  306. tn = "??none??";
  307. if(curtext != P && curtext->from.sym != S)
  308. tn = curtext->from.sym->name;
  309. va_start(arg, fmt);
  310. vseprint(buf, buf+sizeof(buf), fmt, arg);
  311. va_end(arg);
  312. print("%s: %s\n", tn, buf);
  313. nerrors++;
  314. if(nerrors > 20) {
  315. print("too many errors\n");
  316. errorexit();
  317. }
  318. }