tree.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /* $Id$ */
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <alloc.h>
  5. #include <out.h>
  6. #if __STDC__
  7. #include <stdarg.h>
  8. #else
  9. #include <varargs.h>
  10. #endif
  11. #include "operator.h"
  12. #include "position.h"
  13. #include "file.h"
  14. #include "idf.h"
  15. #include "tree.h"
  16. #include "scope.h"
  17. #include "symbol.h"
  18. #include "langdep.h"
  19. #include "type.h"
  20. #include "expr.h"
  21. #include "misc.h"
  22. extern FILE *db_out;
  23. t_lineno currline;
  24. t_lineno listline;
  25. extern char *strrindex();
  26. extern int interrupted;
  27. #if __STDC__
  28. /*VARARGS1*/
  29. p_tree
  30. mknode(int op, ...)
  31. {
  32. va_list ap;
  33. register p_tree p = new_tree();
  34. va_start(ap, op);
  35. {
  36. register int i, na;
  37. p->t_oper = op;
  38. #else
  39. /*VARARGS1*/
  40. p_tree
  41. mknode(va_alist)
  42. va_dcl
  43. {
  44. va_list ap;
  45. register p_tree p = new_tree();
  46. va_start(ap);
  47. {
  48. register int i, na;
  49. p->t_oper = va_arg(ap, int);
  50. #endif
  51. switch(p->t_oper) {
  52. case OP_NAME:
  53. case OP_HELP:
  54. p->t_idf = va_arg(ap, struct idf *);
  55. p->t_str = va_arg(ap, char *);
  56. break;
  57. case OP_STRING:
  58. p->t_sval = va_arg(ap, char *);
  59. break;
  60. case OP_REAL:
  61. p->t_fval = va_arg(ap, double);
  62. break;
  63. case OP_AT:
  64. p->t_lino = va_arg(ap, long);
  65. p->t_filename = va_arg(ap, char *);
  66. break;
  67. case OP_INTEGER:
  68. p->t_ival = va_arg(ap, long);
  69. break;
  70. default:
  71. na = nargs(p->t_oper);
  72. assert(na <= MAXARGS);
  73. for (i = 0; i < na; i++) {
  74. p->t_args[i] = va_arg(ap, p_tree);
  75. }
  76. break;
  77. }
  78. }
  79. va_end(ap);
  80. return p;
  81. }
  82. freenode(p)
  83. register p_tree p;
  84. {
  85. register int na, i;
  86. if (! p) return;
  87. na = nargs(p->t_oper);
  88. assert(na <= MAXARGS);
  89. for (i = 0; i < na; i++) {
  90. freenode(p->t_args[i]);
  91. }
  92. free_tree(p);
  93. }
  94. t_addr
  95. get_addr_from_node(p)
  96. p_tree p;
  97. {
  98. t_addr a = ILL_ADDR;
  99. register p_symbol sym;
  100. if (! p) return NO_ADDR;
  101. if (p->t_address != 0) return p->t_address;
  102. switch(p->t_oper) {
  103. case OP_AT:
  104. if (! p->t_filename &&
  105. (! listfile || ! (p->t_filename = listfile->sy_idf->id_text))) {
  106. error("no current file");
  107. break;
  108. }
  109. a = get_addr_from_position(&(p->t_pos));
  110. if (a == ILL_ADDR) {
  111. error("could not determine address of \"%s\":%d",
  112. p->t_filename, p->t_lino);
  113. break;
  114. }
  115. p->t_address = a;
  116. break;
  117. case OP_IN:
  118. a = get_addr_from_node(p->t_args[0]);
  119. if (p->t_args[1]) {
  120. p_scope sc;
  121. a = get_addr_from_node(p->t_args[1]);
  122. sc = base_scope(get_scope_from_addr(a));
  123. sym = identify(p->t_args[0], FUNCTION|PROC|MODULE);
  124. if (! sym->sy_name.nm_scope ||
  125. ! sym->sy_name.nm_scope->sc_bp_opp) {
  126. error("could not determine address of \"%s\"", p->t_str);
  127. a = ILL_ADDR;
  128. break;
  129. }
  130. if (sc->sc_definedby != sym) {
  131. error("inconsistent address");
  132. a = ILL_ADDR;
  133. break;
  134. }
  135. }
  136. p->t_address = a;
  137. break;
  138. case OP_NAME:
  139. case OP_SELECT:
  140. sym = identify(p, FUNCTION|PROC|MODULE);
  141. if (! sym) {
  142. break;
  143. }
  144. if (! sym->sy_name.nm_scope || ! sym->sy_name.nm_scope->sc_bp_opp) {
  145. error("could not determine address of \"%s\"", p->t_str);
  146. break;
  147. }
  148. a = sym->sy_name.nm_scope->sc_bp_opp;
  149. break;
  150. default:
  151. assert(0);
  152. }
  153. return a;
  154. }
  155. static int ommit_commas = 0;
  156. print_node(f, p, top_level)
  157. register p_tree p;
  158. register FILE *f;
  159. {
  160. if (!p) return;
  161. switch(p->t_oper) {
  162. case OP_LOG:
  163. fputs("log ", f);
  164. print_node(f, p->t_args[0], 0);
  165. break;
  166. case OP_PRCOMM:
  167. fputs("rerun ?", f);
  168. break;
  169. case OP_RUN:
  170. fputs("run ", f);
  171. ommit_commas = 1;
  172. print_node(f, p->t_args[0], 0);
  173. ommit_commas = 0;
  174. break;
  175. case OP_LIST:
  176. fputs("list ", f);
  177. if (p->t_args[0]) {
  178. print_node(f, p->t_args[0], 0);
  179. if (p->t_args[1]) {
  180. if (p->t_args[1]->t_ival >= 0) {
  181. fputs(", ", f);
  182. print_node(f, p->t_args[1], 0);
  183. }
  184. else {
  185. if (p->t_args[1]->t_ival < -100000000) {
  186. fputs("-", f);
  187. }
  188. else print_node(f, p->t_args[1], 0);
  189. }
  190. }
  191. }
  192. break;
  193. case OP_PRINT:
  194. fputs("print ", f);
  195. print_node(f, p->t_args[0], 0);
  196. break;
  197. case OP_SOURCE:
  198. fputs("source ", f);
  199. print_node(f, p->t_args[0], 0);
  200. break;
  201. case OP_ENABLE:
  202. fputs("enable ", f);
  203. print_node(f, p->t_args[0], 0);
  204. break;
  205. case OP_DISABLE:
  206. fputs("disable ", f);
  207. print_node(f, p->t_args[0], 0);
  208. break;
  209. case OP_DISPLAY:
  210. fputs("display ", f);
  211. print_node(f, p->t_args[0], 0);
  212. break;
  213. case OP_LINK:
  214. print_node(f, p->t_args[0], 0);
  215. if (! ommit_commas) fputs(", ", f);
  216. else putc(' ', f);
  217. print_node(f, p->t_args[1], 0);
  218. break;
  219. case OP_FILE:
  220. fputs("file ", f);
  221. print_node(f, p->t_args[0], 0);
  222. break;
  223. case OP_FRAME:
  224. fputs("frame ", f);
  225. print_node(f, p->t_args[0], 0);
  226. break;
  227. case OP_UP:
  228. fputs("frame +", f);
  229. print_node(f, p->t_args[0], 0);
  230. break;
  231. case OP_DOWN:
  232. fputs("frame -", f);
  233. print_node(f, p->t_args[0], 0);
  234. break;
  235. case OP_SET:
  236. fputs("set ", f);
  237. print_node(f, p->t_args[0], 0);
  238. fputs(" to ", f);
  239. print_node(f, p->t_args[1], 0);
  240. break;
  241. case OP_FIND:
  242. fputs("find ", f);
  243. print_node(f, p->t_args[0], 0);
  244. break;
  245. case OP_WHICH:
  246. fputs("which ", f);
  247. print_node(f, p->t_args[0], 0);
  248. break;
  249. case OP_DELETE:
  250. fputs("delete ", f);
  251. print_node(f, p->t_args[0], 0);
  252. break;
  253. case OP_REGS:
  254. fputs("regs ", f);
  255. print_node(f, p->t_args[0], 0);
  256. break;
  257. case OP_NEXT:
  258. fputs("next ", f);
  259. print_node(f, p->t_args[0], 0);
  260. break;
  261. case OP_STEP:
  262. fputs("step ", f);
  263. print_node(f, p->t_args[0], 0);
  264. break;
  265. case OP_STATUS:
  266. fputs("status", f);
  267. break;
  268. case OP_DUMP:
  269. fputs("dump ", f);
  270. (void) print_position(p->t_address, 1);
  271. break;
  272. case OP_RESTORE:
  273. fputs("restore ", f);
  274. print_node(f, p->t_args[0], 0);
  275. break;
  276. case OP_WHERE:
  277. fputs("where ", f);
  278. print_node(f, p->t_args[0], 0);
  279. break;
  280. case OP_HELP:
  281. fputs("help ", f);
  282. print_node(f, p->t_args[0], 0);
  283. break;
  284. case OP_CONT:
  285. fputs("cont", f);
  286. if (p->t_args[0]) {
  287. fprintf(f, " %ld", p->t_args[0]->t_ival);
  288. }
  289. if (p->t_args[1]) {
  290. fputs(" ", f);
  291. print_node(f, p->t_args[1], 0);
  292. }
  293. break;
  294. case OP_WHEN:
  295. fputs("when ", f);
  296. if (p->t_address != NO_ADDR) {
  297. (void) print_position(p->t_address, 1);
  298. }
  299. else print_node(f, p->t_args[0], 0);
  300. if (p->t_args[1]) {
  301. fputs(" if ", f);
  302. print_node(f, p->t_args[1], 0);
  303. }
  304. p = p->t_args[2];
  305. fputs(" { ", f);
  306. while (p && p->t_oper == OP_LINK) {
  307. print_node(f, p->t_args[0], 0);
  308. fputs("; ", f);
  309. p = p->t_args[1];
  310. }
  311. print_node(f, p, 0);
  312. fputs(" }", f);
  313. break;
  314. case OP_STOP:
  315. fputs("stop ", f);
  316. if (p->t_address != NO_ADDR) {
  317. (void) print_position(p->t_address, 1);
  318. }
  319. else print_node(f, p->t_args[0], 0);
  320. if (p->t_args[1]) {
  321. fputs(" if ", f);
  322. print_node(f, p->t_args[1], 0);
  323. }
  324. break;
  325. case OP_TRACE:
  326. fputs("trace ", f);
  327. if (p->t_args[2]) {
  328. fputs("on ", f);
  329. print_node(f, p->t_args[2], 0);
  330. fputs(" ", f);
  331. }
  332. if (p->t_address != NO_ADDR) {
  333. (void) print_position(p->t_address, 1);
  334. }
  335. else print_node(f, p->t_args[0], 0);
  336. if (p->t_args[1]) {
  337. fputs(" if ", f);
  338. print_node(f, p->t_args[1], 0);
  339. }
  340. break;
  341. case OP_AT:
  342. fprintf(f, "at \"%s\":%d", p->t_filename, (int) p->t_lino);
  343. break;
  344. case OP_IN:
  345. fputs("in ", f);
  346. print_node(f, p->t_args[0], 0);
  347. fputs(" ", f);
  348. print_node(f, p->t_args[1], 0);
  349. break;
  350. case OP_SELECT:
  351. print_node(f, p->t_args[0], 0);
  352. fputs("`", f);
  353. print_node(f, p->t_args[1], 0);
  354. break;
  355. case OP_OUTPUT:
  356. fprintf(f, "> %s ", p->t_str);
  357. break;
  358. case OP_INPUT:
  359. fprintf(f, "< %s ", p->t_str);
  360. break;
  361. case OP_NAME:
  362. fputs(p->t_str, f);
  363. break;
  364. case OP_INTEGER:
  365. fprintf(f, currlang->decint_fmt, p->t_ival);
  366. break;
  367. case OP_STRING:
  368. (*currlang->printstring)(f, p->t_sval, strlen(p->t_sval));
  369. break;
  370. case OP_REAL:
  371. fprintf(f, currlang->real_fmt, p->t_fval);
  372. break;
  373. case OP_FORMAT:
  374. print_node(f, p->t_args[0], 0);
  375. fputs("\\", f);
  376. print_node(f, p->t_args[1], 0);
  377. break;
  378. case OP_UNOP:
  379. case OP_BINOP:
  380. (*currlang->printop)(f, p);
  381. break;
  382. default:
  383. assert(0);
  384. }
  385. if (top_level) fputs("\n", f);
  386. }
  387. int
  388. repeatable(com)
  389. p_tree com;
  390. {
  391. switch(com->t_oper) {
  392. case OP_CONT:
  393. com->t_args[0]->t_ival = 1;
  394. freenode(com->t_args[1]);
  395. com->t_args[1] = 0;
  396. return 1;
  397. case OP_NEXT:
  398. case OP_STEP:
  399. freenode(com->t_args[0]);
  400. com->t_args[0] = 0;
  401. return 1;
  402. case OP_LIST:
  403. freenode(com->t_args[0]);
  404. com->t_args[0] = 0;
  405. freenode(com->t_args[1]);
  406. com->t_args[1] = 0;
  407. return 1;
  408. }
  409. return 0;
  410. }
  411. int
  412. in_status(com)
  413. p_tree com;
  414. {
  415. switch(com->t_oper) {
  416. case OP_PRCOMM:
  417. /* not really in status but may not be removed */
  418. case OP_STOP:
  419. case OP_WHEN:
  420. case OP_TRACE:
  421. case OP_DUMP:
  422. case OP_DISPLAY:
  423. return 1;
  424. }
  425. return 0;
  426. }
  427. eval(p)
  428. p_tree p;
  429. {
  430. if (p && operators[p->t_oper].op_fun) (*operators[p->t_oper].op_fun)(p);
  431. }
  432. newfile(id)
  433. register struct idf *id;
  434. {
  435. register p_symbol sym = Lookup(id, PervasiveScope, FILESYM);
  436. if (listfile != sym) listline = 1;
  437. listfile = sym;
  438. if (! listfile) {
  439. listline = 1;
  440. listfile = add_file(id->id_text);
  441. listfile->sy_file->f_scope = FileScope;
  442. }
  443. find_language(strrindex(id->id_text, '.'));
  444. }
  445. int in_wheninvoked;
  446. perform(p, a)
  447. register p_tree p;
  448. t_addr a;
  449. {
  450. switch(p->t_oper) {
  451. case OP_WHEN:
  452. if (p->t_args[1] && ! eval_cond(p->t_args[1])) break;
  453. p = p->t_args[2];
  454. in_wheninvoked++;
  455. while (p && p->t_oper == OP_LINK) {
  456. if (interrupted) return;
  457. if (p->t_args[0]) eval(p->t_args[0]);
  458. p = p->t_args[1];
  459. }
  460. if (interrupted) return;
  461. if (p) eval(p);
  462. in_wheninvoked--;
  463. break;
  464. case OP_TRACE:
  465. if (p->t_args[0] && p->t_args[0]->t_oper == OP_IN) {
  466. register p_scope sc = base_scope(CurrentScope);
  467. if (sc != get_scope_from_addr(p->t_args[0]->t_address)) {
  468. break;
  469. }
  470. }
  471. if (interrupted) return;
  472. if (p->t_args[1] && ! eval_cond(p->t_args[1])) break;
  473. list_position(get_position_from_addr(a));
  474. if (p->t_args[2]) do_print(p->t_args[2]);
  475. break;
  476. default:
  477. assert(0);
  478. }
  479. }
  480. list_position(pos)
  481. p_position pos;
  482. {
  483. newfile(str2idf(pos->filename, 1));
  484. currfile = listfile;
  485. currline = pos->lineno;
  486. lines(currfile->sy_file, (int)currline, (int)1);
  487. listline = 0;
  488. }