stab.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * (c) copyright 1990 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. * Author: Ceriel J.H. Jacobs
  6. */
  7. /* D E B U G G E R S Y M B O L T A B L E */
  8. /* $Id$ */
  9. #include "dbsymtab.h"
  10. #ifdef DBSYMTAB
  11. #include <alloc.h>
  12. #include <em_arith.h>
  13. #include <em_label.h>
  14. #include <em_code.h>
  15. #include <flt_arith.h>
  16. #include <stb.h>
  17. #include "LLlex.h"
  18. #include "def.h"
  19. #include "type.h"
  20. #include "idf.h"
  21. #include "scope.h"
  22. #include "main.h"
  23. extern int gdb_flag;
  24. #define INCR_SIZE 64
  25. extern int proclevel;
  26. extern char *sprint();
  27. static struct db_str {
  28. unsigned sz;
  29. char *base;
  30. char *currpos;
  31. } db_str;
  32. static
  33. create_db_str()
  34. {
  35. if (! db_str.base) {
  36. db_str.base = Malloc(INCR_SIZE);
  37. db_str.sz = INCR_SIZE;
  38. }
  39. db_str.currpos = db_str.base;
  40. }
  41. static
  42. addc_db_str(c)
  43. int c;
  44. {
  45. int df = db_str.currpos - db_str.base;
  46. if (df >= db_str.sz-1) {
  47. db_str.sz += INCR_SIZE;
  48. db_str.base = Realloc(db_str.base, db_str.sz);
  49. db_str.currpos = db_str.base + df;
  50. }
  51. *db_str.currpos++ = c;
  52. *db_str.currpos = '\0';
  53. }
  54. static
  55. adds_db_str(s)
  56. char *s;
  57. {
  58. while (*s) addc_db_str(*s++);
  59. }
  60. static
  61. stb_type(tp, assign_num)
  62. register t_type *tp;
  63. {
  64. char buf[128];
  65. static int stb_count;
  66. if (tp->tp_dbindex > 0) {
  67. adds_db_str(sprint(buf, "%d", tp->tp_dbindex));
  68. return;
  69. }
  70. if (tp->tp_dbindex < 0) {
  71. if (tp->tp_next == 0) {
  72. adds_db_str(sprint(buf, "%d", -tp->tp_dbindex));
  73. return;
  74. }
  75. tp->tp_dbindex = -tp->tp_dbindex;
  76. }
  77. if (tp->tp_dbindex == 0 && assign_num) {
  78. tp->tp_dbindex = ++stb_count;
  79. }
  80. if (tp->tp_dbindex > 0) {
  81. adds_db_str(sprint(buf, "%d=", tp->tp_dbindex));
  82. }
  83. if (tp == void_type) {
  84. adds_db_str(sprint(buf, "%d", tp->tp_dbindex));
  85. return;
  86. }
  87. switch(tp->tp_fund) {
  88. /* simple types ... */
  89. case T_INTEGER:
  90. adds_db_str(sprint(buf,
  91. "r%d;%ld;%ld",
  92. tp->tp_dbindex,
  93. (long) min_int[(int)tp->tp_size],
  94. (long) max_int[(int)tp->tp_size]));
  95. break;
  96. case T_CARDINAL:
  97. adds_db_str(sprint(buf,
  98. "r%d;0;-1",
  99. tp->tp_dbindex));
  100. break;
  101. case T_REAL:
  102. adds_db_str(sprint(buf,
  103. "r%d;%ld;0",
  104. tp->tp_dbindex,
  105. (long)tp->tp_size));
  106. break;
  107. case T_CHAR:
  108. adds_db_str(sprint(buf,
  109. "r%d;0;255",
  110. tp->tp_dbindex));
  111. break;
  112. case T_WORD:
  113. if (tp->tp_size == word_size) {
  114. adds_db_str(sprint(buf,
  115. "r%d;0;-1",
  116. tp->tp_dbindex));
  117. }
  118. else {
  119. adds_db_str(sprint(buf,
  120. "r%d;0;255",
  121. tp->tp_dbindex));
  122. }
  123. break;
  124. /* constructed types ... */
  125. case T_SUBRANGE:
  126. adds_db_str(sprint(buf,
  127. "r%d;%ld;%ld",
  128. tp->tp_next->tp_dbindex,
  129. (long) tp->sub_lb,
  130. (long) tp->sub_ub));
  131. break;
  132. case T_EQUAL:
  133. stb_type(tp->tp_next, 0);
  134. if (tp->tp_dbindex < 0) tp->tp_dbindex = -tp->tp_dbindex;
  135. break;
  136. case T_HIDDEN:
  137. if (DefinitionModule && CurrVis == Defined->mod_vis) {
  138. tp->tp_dbindex = - ++stb_count;
  139. adds_db_str(sprint(buf, "%d", -tp->tp_dbindex));
  140. }
  141. else {
  142. /* ??? what to do here??? */
  143. addc_db_str('*');
  144. stb_type(void_type, 0);
  145. /* ??? this certainly is not correct */
  146. }
  147. break;
  148. case T_POINTER:
  149. if (tp->tp_next) {
  150. addc_db_str('*');
  151. stb_type(tp->tp_next, 0);
  152. if (tp->tp_dbindex < 0) tp->tp_dbindex = -tp->tp_dbindex;
  153. }
  154. else {
  155. tp->tp_dbindex = - ++stb_count;
  156. adds_db_str(sprint(buf, "%d", -tp->tp_dbindex));
  157. }
  158. break;
  159. case T_SET:
  160. addc_db_str('S');
  161. stb_type(tp->tp_next, 0);
  162. adds_db_str(sprint(buf, ";%ld;%ld;", tp->tp_size, tp->set_low));
  163. break;
  164. case T_ARRAY:
  165. addc_db_str('a');
  166. if (IsConformantArray(tp)) {
  167. addc_db_str('r');
  168. stb_type(tp->tp_next, 0);
  169. adds_db_str(sprint(buf, ";0;A%ld", tp->arr_high));
  170. }
  171. else {
  172. stb_type(tp->tp_next, 0);
  173. }
  174. addc_db_str(';');
  175. stb_type(tp->arr_elem, 0);
  176. break;
  177. case T_ENUMERATION:
  178. addc_db_str('e');
  179. {
  180. register struct def *edef = tp->enm_enums;
  181. while (edef) {
  182. adds_db_str(sprint(buf, "%s:%ld,",
  183. edef->df_idf->id_text,
  184. edef->enm_val));
  185. edef = edef->enm_next;
  186. }
  187. }
  188. addc_db_str(';');
  189. break;
  190. case T_RECORD:
  191. adds_db_str(sprint(buf, "s%ld", tp->tp_size));
  192. {
  193. register struct def *sdef = tp->rec_scope->sc_def;
  194. while (sdef) {
  195. adds_db_str(sdef->df_idf->id_text);
  196. addc_db_str(':');
  197. stb_type(sdef->df_type, 0);
  198. adds_db_str(sprint(buf,
  199. ",%ld,%ld;",
  200. sdef->fld_off*8,
  201. sdef->df_type->tp_size*8));
  202. sdef = sdef->df_nextinscope;
  203. }
  204. }
  205. addc_db_str(';');
  206. break;
  207. case T_PROCEDURE:
  208. if (gdb_flag) {
  209. addc_db_str('f');
  210. stb_type(tp->tp_next ? tp->tp_next : void_type, 0);
  211. break;
  212. }
  213. addc_db_str('Q');
  214. stb_type(tp->tp_next ? tp->tp_next : void_type, 0);
  215. {
  216. register struct paramlist *p = tp->prc_params;
  217. int paramcount = 0;
  218. while (p) {
  219. paramcount++;
  220. p = p->par_next;
  221. }
  222. adds_db_str(sprint(buf, ",%d;", paramcount));
  223. p = tp->prc_params;
  224. while (p) {
  225. addc_db_str(IsVarParam(p)
  226. ? 'v'
  227. : IsConformantArray(TypeOfParam(p))
  228. ? 'i'
  229. : 'p');
  230. stb_type(TypeOfParam(p), 0);
  231. addc_db_str(';');
  232. p = p->par_next;
  233. }
  234. }
  235. }
  236. }
  237. stb_addtp(s, tp)
  238. char *s;
  239. t_type *tp;
  240. {
  241. create_db_str();
  242. adds_db_str(s);
  243. addc_db_str(':');
  244. addc_db_str('t');
  245. stb_type(tp, 1);
  246. addc_db_str(';');
  247. C_ms_stb_cst(db_str.base,
  248. N_LSYM,
  249. tp == void_type || tp->tp_size >= max_int[2]
  250. ? 0
  251. : (int)tp->tp_size,
  252. (arith) 0);
  253. }
  254. stb_string(df, kind)
  255. register t_def *df;
  256. {
  257. register t_type *tp = df->df_type;
  258. char buf[64];
  259. create_db_str();
  260. adds_db_str(df->df_idf->id_text);
  261. addc_db_str(':');
  262. switch(kind) {
  263. case D_MODULE:
  264. if (gdb_flag) {
  265. addc_db_str('F');
  266. stb_type(void_type, 0);
  267. }
  268. else {
  269. adds_db_str(sprint(buf, "M%d;", df->mod_vis->sc_count));
  270. }
  271. C_ms_stb_pnam(db_str.base, N_FUN, gdb_flag ? 0 : proclevel, df->mod_vis->sc_scope->sc_name);
  272. break;
  273. case D_PROCEDURE:
  274. if (gdb_flag) {
  275. addc_db_str('f');
  276. }
  277. else adds_db_str(sprint(buf, "Q%d;", df->prc_vis->sc_count));
  278. stb_type(tp->tp_next ? tp->tp_next : void_type, 0);
  279. if (gdb_flag) {
  280. t_scopelist *sc = df->prc_vis;
  281. sc = enclosing(sc);
  282. while (sc) {
  283. t_def *d = sc->sc_scope->sc_definedby;
  284. if (d && d->df_kind == D_PROCEDURE) {
  285. adds_db_str(sprint(buf, ",%s", d->df_idf->id_text));
  286. break;
  287. }
  288. sc = enclosing(sc);
  289. }
  290. }
  291. else addc_db_str(';');
  292. C_ms_stb_pnam(db_str.base, N_FUN, gdb_flag ? 0 : proclevel, df->prc_vis->sc_scope->sc_name);
  293. break;
  294. case D_END:
  295. if (gdb_flag) break;
  296. adds_db_str(sprint(buf, "E%d;", df->mod_vis->sc_count));
  297. C_ms_stb_cst(db_str.base, N_SCOPE, proclevel, (arith) 0);
  298. break;
  299. case D_PEND:
  300. if (gdb_flag) break;
  301. adds_db_str(sprint(buf, "E%d;", df->prc_vis->sc_count));
  302. C_ms_stb_cst(db_str.base, N_SCOPE, proclevel, (arith) 0);
  303. break;
  304. case D_VARIABLE:
  305. if (DefinitionModule && CurrVis != Defined->mod_vis) break;
  306. if (df->df_flags & D_VARPAR) { /* VAR parameter */
  307. addc_db_str('v');
  308. stb_type(tp, 0);
  309. addc_db_str(';');
  310. C_ms_stb_cst(db_str.base, N_PSYM, 0, df->var_off);
  311. }
  312. else if (df->df_flags & D_VALPAR) { /* value parameter */
  313. addc_db_str(IsConformantArray(tp)
  314. ? 'i'
  315. : 'p');
  316. stb_type(tp, 0);
  317. addc_db_str(';');
  318. C_ms_stb_cst(db_str.base, N_PSYM, 0, df->var_off);
  319. }
  320. else if (!proclevel ||
  321. (df->df_flags & D_ADDRGIVEN)) { /* global */
  322. int knd = N_LCSYM;
  323. if (df->df_flags & D_EXPORTED) {
  324. knd = N_GSYM;
  325. addc_db_str('G');
  326. }
  327. else {
  328. addc_db_str('S');
  329. }
  330. stb_type(tp, 0);
  331. addc_db_str(';');
  332. if (df->df_flags & D_ADDRGIVEN) {
  333. C_ms_stb_cst(db_str.base, knd, 0, df->var_off);
  334. }
  335. else {
  336. C_ms_stb_dnam(db_str.base, knd, 0, df->var_name, (arith) 0);
  337. }
  338. }
  339. else { /* local variable */
  340. stb_type(tp, 1); /* assign type num to avoid
  341. difficult to parse string */
  342. addc_db_str(';');
  343. C_ms_stb_cst(db_str.base, N_LSYM, 0, df->var_off);
  344. }
  345. break;
  346. case D_TYPE:
  347. addc_db_str('t');
  348. stb_type(tp, 1);
  349. addc_db_str(';');
  350. C_ms_stb_cst(db_str.base,
  351. N_LSYM,
  352. tp == void_type || tp->tp_size >= max_int[2]
  353. ? 0
  354. : (int)tp->tp_size,
  355. (arith) 0);
  356. break;
  357. case D_CONST:
  358. if (DefinitionModule && CurrVis != Defined->mod_vis) break;
  359. addc_db_str('c');
  360. addc_db_str('=');
  361. tp = BaseType(tp);
  362. switch(tp->tp_fund) {
  363. case T_INTEGER:
  364. case T_INTORCARD:
  365. case T_CARDINAL:
  366. case T_WORD:
  367. case T_POINTER:
  368. case T_PROCEDURE:
  369. adds_db_str(sprint(buf, "i%ld;", df->con_const.TOK_INT));
  370. break;
  371. case T_CHAR:
  372. adds_db_str(sprint(buf, "c%ld;", df->con_const.TOK_INT));
  373. break;
  374. case T_REAL:
  375. addc_db_str('r');
  376. if (! df->con_const.TOK_RSTR) {
  377. char buf2[FLT_STRLEN];
  378. flt_flt2str(&df->con_const.TOK_RVAL, buf2, FLT_STRLEN);
  379. adds_db_str(buf2);
  380. }
  381. else adds_db_str(df->con_const.TOK_RSTR);
  382. addc_db_str(';');
  383. break;
  384. case T_STRING: {
  385. register char *p = df->con_const.TOK_STR;
  386. adds_db_str("s'");
  387. while (*p) {
  388. if (*p == '\'' || *p == '\\') {
  389. addc_db_str('\\');
  390. }
  391. addc_db_str(*p++);
  392. }
  393. adds_db_str("';");
  394. }
  395. break;
  396. case T_ENUMERATION:
  397. addc_db_str('e');
  398. stb_type(tp, 0);
  399. adds_db_str(sprint(buf, ",%ld;", df->con_const.TOK_INT));
  400. break;
  401. case T_SET: {
  402. register int i;
  403. addc_db_str('S');
  404. stb_type(tp, 0);
  405. for (i = 0; i < tp->tp_size; i++) {
  406. adds_db_str(sprint(buf, ",%ld",
  407. (df->con_const.tk_data.tk_set[i/(int) word_size] >> (8*(i%(int)word_size)))&0377));
  408. }
  409. addc_db_str(';');
  410. }
  411. break;
  412. }
  413. C_ms_stb_cst(db_str.base,
  414. N_LSYM,
  415. tp->tp_size < max_int[2] ? (int)tp->tp_size : 0,
  416. (arith) 0);
  417. break;
  418. }
  419. }
  420. #endif /* DBSYMTAB */