list.c 4.6 KB

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