dumpidf.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* $Header$ */
  2. /* DUMP ROUTINES */
  3. #include "debug.h"
  4. #ifdef DEBUG
  5. #include "nopp.h"
  6. #include "nobitfield.h"
  7. #include "arith.h"
  8. #include "stack.h"
  9. #include "idf.h"
  10. #include "def.h"
  11. #include "type.h"
  12. #include "struct.h"
  13. #include "field.h"
  14. #include "Lpars.h"
  15. #include "label.h"
  16. #include "expr.h"
  17. /* Some routines (symbol2str, token2str, type2str) which should have
  18. * yielded strings are written to yield a pointer to a transient piece
  19. * of memory, containing the string, since this is the only reasonable
  20. * thing to do in C. `Transient' means that the result may soon
  21. * disappear, which is generally not a problem, since normally it is
  22. * consumed immediately. Sometimes we need more than one of them, and
  23. * MAXTRANS is the maximum number we will need simultaneously.
  24. */
  25. #define MAXTRANS 6
  26. extern char options[];
  27. extern char *sprintf();
  28. extern struct idf *idf_hashtable[];
  29. extern char *symbol2str(), *type2str(), *next_transient();
  30. enum sdef_kind {selector, field}; /* parameter for dumpsdefs */
  31. static int dumplevel;
  32. static
  33. newline() {
  34. int dl = dumplevel;
  35. printf("\n");
  36. while (dl >= 2) {
  37. printf("\t");
  38. dl -= 2;
  39. }
  40. if (dl)
  41. printf(" ");
  42. }
  43. dumpidftab(msg, opt)
  44. char msg[];
  45. {
  46. /* Dumps the identifier table in readable form (but in
  47. arbitrary order).
  48. Unless opt & 1, macros are not dumped.
  49. Unless opt & 2, reserved identifiers are not dumped.
  50. Unless opt & 4, universal identifiers are not dumped.
  51. */
  52. int i;
  53. printf(">>> DUMPIDF, %s (start)", msg);
  54. dumpstack();
  55. for (i = 0; i < HASHSIZE; i++) {
  56. struct idf *notch = idf_hashtable[i];
  57. while (notch) {
  58. dumpidf(notch, opt);
  59. notch = notch->next;
  60. }
  61. }
  62. newline();
  63. printf(">>> DUMPIDF, %s (end)\n", msg);
  64. }
  65. dumpstack() {
  66. /* Dumps the identifier stack, starting at the top.
  67. */
  68. struct stack_level *stl = local_level;
  69. while (stl) {
  70. struct stack_entry *se = stl->sl_entry;
  71. newline();
  72. printf("%3d: ", stl->sl_level);
  73. while (se) {
  74. printf("%s ", se->se_idf->id_text);
  75. se = se->next;
  76. }
  77. stl = stl->sl_previous;
  78. }
  79. printf("\n");
  80. }
  81. dumpidf(idf, opt)
  82. struct idf *idf;
  83. {
  84. /* All information about the identifier idf is divulged in a
  85. hopefully readable format.
  86. */
  87. int started = 0;
  88. if (!idf)
  89. return;
  90. #ifndef NOPP
  91. if ((opt&1) && idf->id_macro) {
  92. if (!started++) {
  93. newline();
  94. printf("%s:", idf->id_text);
  95. }
  96. printf(" macro");
  97. }
  98. #endif NOPP
  99. if ((opt&2) && idf->id_reserved) {
  100. if (!started++) {
  101. newline();
  102. printf("%s:", idf->id_text);
  103. }
  104. printf(" reserved: %d;", idf->id_reserved);
  105. }
  106. if (idf->id_def && ((opt&4) || idf->id_def->df_level)) {
  107. if (!started++) {
  108. newline();
  109. printf("%s:", idf->id_text);
  110. }
  111. dumpdefs(idf->id_def, opt);
  112. }
  113. if (idf->id_sdef) {
  114. if (!started++) {
  115. newline();
  116. printf("%s:", idf->id_text);
  117. }
  118. dumpsdefs(idf->id_sdef, selector);
  119. }
  120. if (idf->id_struct) {
  121. if (!started++) {
  122. newline();
  123. printf("%s:", idf->id_text);
  124. }
  125. dumptags(idf->id_struct);
  126. }
  127. if (idf->id_enum) {
  128. if (!started++) {
  129. newline();
  130. printf("%s:", idf->id_text);
  131. }
  132. dumptags(idf->id_enum);
  133. }
  134. }
  135. dumpdefs(def, opt)
  136. register struct def *def;
  137. {
  138. dumplevel++;
  139. while (def && ((opt&4) || def->df_level)) {
  140. newline();
  141. printf("L%d: %s %s%s%s%s%s %lo;",
  142. def->df_level,
  143. symbol2str(def->df_sc),
  144. (def->df_register != REG_NONE) ? "reg " : "",
  145. def->df_initialized ? "init'd " : "",
  146. def->df_used ? "used " : "",
  147. type2str(def->df_type),
  148. def->df_sc == ENUM ? ", =" : " at",
  149. def->df_address
  150. );
  151. def = def->next;
  152. }
  153. dumplevel--;
  154. }
  155. dumptags(tag)
  156. struct tag *tag;
  157. {
  158. dumplevel++;
  159. while (tag) {
  160. register struct type *tp = tag->tg_type;
  161. register int fund = tp->tp_fund;
  162. newline();
  163. printf("L%d: %s %s",
  164. tag->tg_level,
  165. fund == STRUCT ? "struct" :
  166. fund == UNION ? "union" :
  167. fund == ENUM ? "enum" : "<UNKNOWN>",
  168. tp->tp_idf->id_text
  169. );
  170. if (is_struct_or_union(fund)) {
  171. printf(" {");
  172. dumpsdefs(tp->tp_sdef, field);
  173. newline();
  174. printf("}");
  175. }
  176. printf(";");
  177. tag = tag->next;
  178. }
  179. dumplevel--;
  180. }
  181. dumpsdefs(sdef, sdk)
  182. struct sdef *sdef;
  183. enum sdef_kind sdk;
  184. {
  185. /* Since sdef's are members of two chains, there are actually
  186. two dumpsdefs's, one following the chain of all selectors
  187. belonging to the same idf, starting at idf->id_sdef;
  188. and the other following the chain of all selectors belonging
  189. to the same struct, starting at stp->tp_sdef.
  190. */
  191. dumplevel++;
  192. while (sdef) {
  193. newline();
  194. printf("L%d: ", sdef->sd_level);
  195. #ifndef NOBITFIELD
  196. if (sdk == selector)
  197. #endif NOBITFIELD
  198. printf("selector %s at offset %lu in %s;",
  199. type2str(sdef->sd_type),
  200. sdef->sd_offset, type2str(sdef->sd_stype)
  201. );
  202. #ifndef NOBITFIELD
  203. else printf("field %s at offset %lu;",
  204. type2str(sdef->sd_type), sdef->sd_offset
  205. );
  206. #endif NOBITFIELD
  207. sdef = (sdk == selector ? sdef->next : sdef->sd_sdef);
  208. }
  209. dumplevel--;
  210. }
  211. char *
  212. type2str(tp)
  213. struct type *tp;
  214. {
  215. /* Yields a pointer to a one-line description of the type tp.
  216. */
  217. char *buf = next_transient();
  218. int ops = 1;
  219. buf[0] = '\0';
  220. if (!tp) {
  221. sprintf(buf, "<NILTYPE>");
  222. return buf;
  223. }
  224. sprintf(buf, "(@%lx, #%ld, &%d) ", tp, (long)tp->tp_size, tp->tp_align);
  225. while (ops) {
  226. switch (tp->tp_fund) {
  227. case POINTER:
  228. sprintf(buf, "%spointer to ", buf);
  229. break;
  230. case ARRAY:
  231. sprintf(buf, "%sarray [%ld] of ", buf, tp->tp_size);
  232. break;
  233. case FUNCTION:
  234. sprintf(buf, "%sfunction yielding ", buf);
  235. break;
  236. default:
  237. sprintf(buf, "%s%s%s", buf,
  238. tp->tp_unsigned ? "unsigned " : "",
  239. symbol2str(tp->tp_fund)
  240. );
  241. if (tp->tp_idf)
  242. sprintf(buf, "%s %s", buf,
  243. tp->tp_idf->id_text);
  244. #ifndef NOBITFIELD
  245. if (tp->tp_field) {
  246. struct field *fd = tp->tp_field;
  247. sprintf(buf, "%s [s=%ld,w=%ld]", buf,
  248. fd->fd_shift, fd->fd_width);
  249. }
  250. #endif NOBITFIELD
  251. ops = 0;
  252. break;
  253. }
  254. tp = tp->tp_up;
  255. }
  256. return buf;
  257. }
  258. char * /* the ultimate transient buffer supplier */
  259. next_transient() {
  260. static int bnum;
  261. static char buf[MAXTRANS][300];
  262. if (++bnum == MAXTRANS)
  263. bnum = 0;
  264. return buf[bnum];
  265. }
  266. print_expr(msg, expr)
  267. char msg[];
  268. struct expr *expr;
  269. {
  270. /* Provisional routine to print an expression preceded by a
  271. message msg.
  272. */
  273. if (options['x']) {
  274. printf("\n%s: ", msg);
  275. printf("(L=line, T=type, r/lV=r/lvalue, F=flags, D=depth)\n");
  276. p1_expr(0, expr);
  277. }
  278. }
  279. p1_expr(lvl, expr)
  280. struct expr *expr;
  281. {
  282. extern char *type2str(), *symbol2str();
  283. p1_indent(lvl);
  284. if (!expr) {
  285. printf("NILEXPR\n");
  286. return;
  287. }
  288. printf("expr: L=%u, T=%s, %cV, F=%02o, D=%d, %s: ",
  289. expr->ex_line,
  290. type2str(expr->ex_type),
  291. expr->ex_lvalue ? 'l' : 'r',
  292. expr->ex_flags,
  293. expr->ex_depth,
  294. expr->ex_class == Value ? "Value" :
  295. expr->ex_class == String ? "String" :
  296. expr->ex_class == Float ? "Float" :
  297. expr->ex_class == Oper ? "Oper" :
  298. expr->ex_class == Type ? "Type" : "UNKNOWN CLASS"
  299. );
  300. switch (expr->ex_class) {
  301. struct value *v;
  302. struct oper *o;
  303. case Value:
  304. v = &expr->ex_object.ex_value;
  305. if (v->vl_idf)
  306. printf("%s + ", v->vl_idf->id_text);
  307. printf(expr->ex_type->tp_unsigned ? "%lu\n" : "%ld\n",
  308. v->vl_value);
  309. break;
  310. case String:
  311. printf("%s\n", expr->SG_VALUE);
  312. break;
  313. case Float:
  314. printf("%s\n", expr->FL_VALUE);
  315. break;
  316. case Oper:
  317. o = &expr->ex_object.ex_oper;
  318. printf("\n");
  319. p1_expr(lvl+1, o->op_left);
  320. p1_indent(lvl); printf("%s\n", symbol2str(o->op_oper));
  321. p1_expr(lvl+1, o->op_right);
  322. break;
  323. case Type:
  324. printf("\n");
  325. break;
  326. default:
  327. printf("UNKNOWN CLASS\n");
  328. break;
  329. }
  330. }
  331. p1_indent(lvl) {
  332. while (lvl--)
  333. printf(" ");
  334. }
  335. #endif DEBUG