ack/util/grind/tree.c

498 lines
9.3 KiB
C
Raw Normal View History

1990-08-31 18:22:53 +00:00
/* $Header$ */
#include <stdio.h>
#include <varargs.h>
#include <assert.h>
#include <alloc.h>
#include <out.h>
#include "operator.h"
#include "position.h"
#include "file.h"
#include "idf.h"
#include "tree.h"
#include "scope.h"
#include "symbol.h"
#include "langdep.h"
1990-09-12 16:13:59 +00:00
#include "type.h"
1990-09-19 14:31:12 +00:00
#include "expr.h"
1990-08-31 18:22:53 +00:00
extern FILE *db_out;
1990-10-17 17:00:03 +00:00
t_lineno currline;
t_lineno listline;
1990-08-31 18:22:53 +00:00
extern char *strrindex();
extern int interrupted;
1990-08-31 18:22:53 +00:00
/*VARARGS1*/
p_tree
mknode(va_alist)
va_dcl
{
va_list ap;
register p_tree p = new_tree();
va_start(ap);
{
register int i, na;
p->t_oper = va_arg(ap, int);
switch(p->t_oper) {
case OP_NAME:
case OP_HELP:
1990-08-31 18:22:53 +00:00
p->t_idf = va_arg(ap, struct idf *);
p->t_str = va_arg(ap, char *);
break;
1990-09-07 14:56:24 +00:00
case OP_STRING:
p->t_sval = va_arg(ap, char *);
break;
case OP_REAL:
p->t_fval = va_arg(ap, double);
break;
1990-08-31 18:22:53 +00:00
case OP_AT:
p->t_lino = va_arg(ap, long);
p->t_filename = va_arg(ap, char *);
break;
1990-09-20 17:51:14 +00:00
case OP_INTEGER:
1990-08-31 18:22:53 +00:00
p->t_ival = va_arg(ap, long);
break;
default:
na = nargs(p->t_oper);
assert(na <= MAXARGS);
for (i = 0; i < na; i++) {
p->t_args[i] = va_arg(ap, p_tree);
}
break;
}
}
va_end(ap);
return p;
}
freenode(p)
register p_tree p;
{
register int na, i;
if (! p) return;
1990-09-07 14:56:24 +00:00
na = nargs(p->t_oper);
assert(na <= MAXARGS);
for (i = 0; i < na; i++) {
freenode(p->t_args[i]);
1990-08-31 18:22:53 +00:00
}
free_tree(p);
}
t_addr
1990-09-25 17:40:47 +00:00
get_addr_from_node(p)
1990-09-21 16:58:20 +00:00
p_tree p;
{
t_addr a = ILL_ADDR;
register p_symbol sym;
if (! p) return NO_ADDR;
if (p->t_address != 0) return p->t_address;
switch(p->t_oper) {
case OP_AT:
if (! p->t_filename &&
(! listfile || ! (p->t_filename = listfile->sy_idf->id_text))) {
error("no current file");
break;
}
a = get_addr_from_position(&(p->t_pos));
if (a == ILL_ADDR) {
error("could not determine address of \"%s\":%d",
p->t_filename, p->t_lino);
break;
}
p->t_address = a;
break;
case OP_IN:
1990-09-25 17:40:47 +00:00
a = get_addr_from_node(p->t_args[0]);
1990-10-17 17:00:03 +00:00
if (p->t_args[1]) {
p_scope sc;
a = get_addr_from_node(p->t_args[1]);
sc = base_scope(get_scope_from_addr(a));
sym = identify(p->t_args[0], FUNCTION|PROC|MODULE);
if (! sym->sy_name.nm_scope ||
! sym->sy_name.nm_scope->sc_bp_opp) {
error("could not determine address of \"%s\"", p->t_str);
a = ILL_ADDR;
break;
}
if (sc->sc_definedby != sym) {
error("inconsistent address");
a = ILL_ADDR;
break;
}
}
1990-09-21 16:58:20 +00:00
p->t_address = a;
break;
case OP_NAME:
case OP_SELECT:
sym = identify(p, FUNCTION|PROC|MODULE);
if (! sym) {
break;
}
if (! sym->sy_name.nm_scope || ! sym->sy_name.nm_scope->sc_bp_opp) {
error("could not determine address of \"%s\"", p->t_str);
break;
}
a = sym->sy_name.nm_scope->sc_bp_opp;
break;
default:
assert(0);
}
return a;
}
1990-11-06 12:23:41 +00:00
static int ommit_commas = 0;
print_node(f, p, top_level)
1990-08-31 18:22:53 +00:00
register p_tree p;
1990-11-06 12:23:41 +00:00
register FILE *f;
1990-08-31 18:22:53 +00:00
{
if (!p) return;
switch(p->t_oper) {
1990-11-06 12:23:41 +00:00
case OP_LOG:
fputs("log ", f);
print_node(f, p->t_args[0], 0);
break;
case OP_PRCOMM:
fputs("rerun ?", f);
break;
case OP_RUN:
fputs("run ", f);
ommit_commas = 1;
print_node(f, p->t_args[0], 0);
ommit_commas = 0;
break;
1990-08-31 18:22:53 +00:00
case OP_LIST:
1990-11-06 12:23:41 +00:00
fputs("list ", f);
1990-08-31 18:22:53 +00:00
if (p->t_args[0]) {
1990-11-06 12:23:41 +00:00
print_node(f, p->t_args[0], 0);
1990-08-31 18:22:53 +00:00
if (p->t_args[1]) {
if (p->t_args[1]->t_ival >= 0) {
1990-11-06 12:23:41 +00:00
fputs(", ", f);
print_node(f, p->t_args[1], 0);
}
else {
if (p->t_args[1]->t_ival < -100000000) {
1990-11-06 12:23:41 +00:00
fputs("-", f);
}
1990-11-06 12:23:41 +00:00
else print_node(f, p->t_args[1], 0);
}
1990-08-31 18:22:53 +00:00
}
}
break;
case OP_PRINT:
1990-11-06 12:23:41 +00:00
fputs("print ", f);
print_node(f, p->t_args[0], 0);
break;
case OP_SOURCE:
fputs("source ", f);
print_node(f, p->t_args[0], 0);
1990-08-31 18:22:53 +00:00
break;
case OP_ENABLE:
1990-11-06 12:23:41 +00:00
fputs("enable ", f);
print_node(f, p->t_args[0], 0);
break;
case OP_DISABLE:
1990-11-06 12:23:41 +00:00
fputs("disable ", f);
print_node(f, p->t_args[0], 0);
break;
1990-09-20 17:51:14 +00:00
case OP_DISPLAY:
1990-11-06 12:23:41 +00:00
fputs("display ", f);
print_node(f, p->t_args[0], 0);
1990-09-20 17:51:14 +00:00
break;
case OP_LINK:
1990-11-06 12:23:41 +00:00
print_node(f, p->t_args[0], 0);
if (! ommit_commas) fputs(", ", f);
else putc(' ', f);
print_node(f, p->t_args[1], 0);
break;
1990-08-31 18:22:53 +00:00
case OP_FILE:
1990-11-06 12:23:41 +00:00
fputs("file ", f);
print_node(f, p->t_args[0], 0);
break;
case OP_FRAME:
fputs("frame ", f);
print_node(f, p->t_args[0], 0);
break;
case OP_UP:
fputs("frame +", f);
print_node(f, p->t_args[0], 0);
break;
case OP_DOWN:
fputs("frame -", f);
print_node(f, p->t_args[0], 0);
1990-08-31 18:22:53 +00:00
break;
1990-09-19 14:31:12 +00:00
case OP_SET:
1990-11-06 12:23:41 +00:00
fputs("set ", f);
print_node(f, p->t_args[0], 0);
fputs(" to ", f);
print_node(f, p->t_args[1], 0);
1990-09-19 14:31:12 +00:00
break;
1990-09-20 17:51:14 +00:00
case OP_FIND:
1990-11-06 12:23:41 +00:00
fputs("find ", f);
print_node(f, p->t_args[0], 0);
1990-09-20 17:51:14 +00:00
break;
case OP_WHICH:
1990-11-06 12:23:41 +00:00
fputs("which ", f);
print_node(f, p->t_args[0], 0);
1990-09-20 17:51:14 +00:00
break;
1990-08-31 18:22:53 +00:00
case OP_DELETE:
1990-11-06 12:23:41 +00:00
fputs("delete ", f);
print_node(f, p->t_args[0], 0);
1990-08-31 18:22:53 +00:00
break;
case OP_REGS:
1990-11-06 12:23:41 +00:00
fputs("regs ", f);
print_node(f, p->t_args[0], 0);
1990-08-31 18:22:53 +00:00
break;
case OP_NEXT:
1990-11-06 12:23:41 +00:00
fputs("next ", f);
print_node(f, p->t_args[0], 0);
1990-08-31 18:22:53 +00:00
break;
case OP_STEP:
1990-11-06 12:23:41 +00:00
fputs("step ", f);
print_node(f, p->t_args[0], 0);
1990-08-31 18:22:53 +00:00
break;
case OP_STATUS:
1990-11-06 12:23:41 +00:00
fputs("status", f);
1990-08-31 18:22:53 +00:00
break;
case OP_DUMP:
1990-11-06 12:23:41 +00:00
fputs("dump ", f);
1990-11-12 13:46:31 +00:00
(void) print_position(p->t_address, 1);
1990-08-31 18:22:53 +00:00
break;
case OP_RESTORE:
1990-11-06 12:23:41 +00:00
fputs("restore ", f);
print_node(f, p->t_args[0], 0);
1990-08-31 18:22:53 +00:00
break;
case OP_WHERE:
1990-11-06 12:23:41 +00:00
fputs("where ", f);
print_node(f, p->t_args[0], 0);
1990-08-31 18:22:53 +00:00
break;
case OP_HELP:
1990-11-06 12:23:41 +00:00
fputs("help ", f);
print_node(f, p->t_args[0], 0);
break;
1990-08-31 18:22:53 +00:00
case OP_CONT:
1990-11-06 12:23:41 +00:00
fputs("cont", f);
1990-08-31 18:22:53 +00:00
if (p->t_args[0]) {
1990-11-06 12:23:41 +00:00
fprintf(f, " %ld", p->t_args[0]->t_ival);
1990-08-31 18:22:53 +00:00
}
if (p->t_args[1]) {
1990-11-06 12:23:41 +00:00
fputs(" ", f);
print_node(f, p->t_args[1], 0);
1990-08-31 18:22:53 +00:00
}
break;
case OP_WHEN:
1990-11-06 12:23:41 +00:00
fputs("when ", f);
1990-08-31 18:22:53 +00:00
if (p->t_address != NO_ADDR) {
1990-11-12 13:46:31 +00:00
(void) print_position(p->t_address, 1);
1990-08-31 18:22:53 +00:00
}
1990-11-06 12:23:41 +00:00
else print_node(f, p->t_args[0], 0);
1990-08-31 18:22:53 +00:00
if (p->t_args[1]) {
1990-11-06 12:23:41 +00:00
fputs(" if ", f);
print_node(f, p->t_args[1], 0);
1990-08-31 18:22:53 +00:00
}
p = p->t_args[2];
1990-11-06 12:23:41 +00:00
fputs(" { ", f);
1990-10-17 17:00:03 +00:00
while (p && p->t_oper == OP_LINK) {
1990-11-06 12:23:41 +00:00
print_node(f, p->t_args[0], 0);
fputs("; ", f);
1990-08-31 18:22:53 +00:00
p = p->t_args[1];
}
1990-11-06 12:23:41 +00:00
print_node(f, p, 0);
fputs(" }", f);
1990-08-31 18:22:53 +00:00
break;
case OP_STOP:
1990-11-06 12:23:41 +00:00
fputs("stop ", f);
1990-08-31 18:22:53 +00:00
if (p->t_address != NO_ADDR) {
1990-11-12 13:46:31 +00:00
(void) print_position(p->t_address, 1);
1990-08-31 18:22:53 +00:00
}
1990-11-06 12:23:41 +00:00
else print_node(f, p->t_args[0], 0);
1990-08-31 18:22:53 +00:00
if (p->t_args[1]) {
1990-11-06 12:23:41 +00:00
fputs(" if ", f);
print_node(f, p->t_args[1], 0);
1990-08-31 18:22:53 +00:00
}
break;
case OP_TRACE:
1990-11-06 12:23:41 +00:00
fputs("trace ", f);
1990-08-31 18:22:53 +00:00
if (p->t_args[2]) {
1990-11-06 12:23:41 +00:00
fputs("on ", f);
print_node(f, p->t_args[2], 0);
fputs(" ", f);
1990-08-31 18:22:53 +00:00
}
if (p->t_address != NO_ADDR) {
1990-11-12 13:46:31 +00:00
(void) print_position(p->t_address, 1);
1990-08-31 18:22:53 +00:00
}
1990-11-06 12:23:41 +00:00
else print_node(f, p->t_args[0], 0);
1990-08-31 18:22:53 +00:00
if (p->t_args[1]) {
1990-11-06 12:23:41 +00:00
fputs(" if ", f);
print_node(f, p->t_args[1], 0);
1990-08-31 18:22:53 +00:00
}
break;
case OP_AT:
1990-11-12 13:46:31 +00:00
fprintf(f, "at \"%s\":%d", p->t_filename, (int) p->t_lino);
1990-08-31 18:22:53 +00:00
break;
case OP_IN:
1990-11-06 12:23:41 +00:00
fputs("in ", f);
print_node(f, p->t_args[0], 0);
fputs(" ", f);
print_node(f, p->t_args[1], 0);
1990-08-31 18:22:53 +00:00
break;
case OP_SELECT:
1990-11-06 12:23:41 +00:00
print_node(f, p->t_args[0], 0);
fputs("`", f);
print_node(f, p->t_args[1], 0);
break;
case OP_OUTPUT:
fprintf(f, "> %s ", p->t_str);
break;
case OP_INPUT:
fprintf(f, "< %s ", p->t_str);
1990-08-31 18:22:53 +00:00
break;
case OP_NAME:
1990-11-06 12:23:41 +00:00
fputs(p->t_str, f);
1990-08-31 18:22:53 +00:00
break;
case OP_INTEGER:
1990-11-06 12:23:41 +00:00
fprintf(f, currlang->decint_fmt, p->t_ival);
1990-08-31 18:22:53 +00:00
break;
1990-09-07 14:56:24 +00:00
case OP_STRING:
1990-11-06 12:23:41 +00:00
(*currlang->printstring)(f, p->t_sval, strlen(p->t_sval));
1990-09-07 14:56:24 +00:00
break;
case OP_REAL:
1990-11-06 12:23:41 +00:00
fprintf(f, currlang->real_fmt, p->t_fval);
1990-09-12 16:13:59 +00:00
break;
case OP_FORMAT:
1990-11-06 12:23:41 +00:00
print_node(f, p->t_args[0], 0);
fputs("\\", f);
print_node(f, p->t_args[1], 0);
break;
1990-09-12 16:13:59 +00:00
case OP_UNOP:
case OP_BINOP:
1990-11-06 12:23:41 +00:00
(*currlang->printop)(f, p);
1990-09-07 14:56:24 +00:00
break;
1990-10-17 17:00:03 +00:00
default:
assert(0);
1990-08-31 18:22:53 +00:00
}
1990-11-06 12:23:41 +00:00
if (top_level) fputs("\n", f);
1990-08-31 18:22:53 +00:00
}
int
repeatable(com)
p_tree com;
{
switch(com->t_oper) {
case OP_CONT:
com->t_args[0]->t_ival = 1;
freenode(com->t_args[1]);
com->t_args[1] = 0;
return 1;
1990-08-31 18:22:53 +00:00
case OP_NEXT:
case OP_STEP:
1990-10-17 17:00:03 +00:00
freenode(com->t_args[0]);
com->t_args[0] = 0;
return 1;
1990-08-31 18:22:53 +00:00
case OP_LIST:
freenode(com->t_args[0]);
com->t_args[0] = 0;
freenode(com->t_args[1]);
com->t_args[1] = 0;
1990-08-31 18:22:53 +00:00
return 1;
}
return 0;
}
int
in_status(com)
p_tree com;
{
switch(com->t_oper) {
1990-11-06 12:23:41 +00:00
case OP_PRCOMM:
/* not really in status but may not be removed */
1990-08-31 18:22:53 +00:00
case OP_STOP:
case OP_WHEN:
case OP_TRACE:
case OP_DUMP:
1990-09-20 17:51:14 +00:00
case OP_DISPLAY:
1990-08-31 18:22:53 +00:00
return 1;
}
return 0;
}
eval(p)
p_tree p;
{
if (p && operators[p->t_oper].op_fun) (*operators[p->t_oper].op_fun)(p);
1990-08-31 18:22:53 +00:00
}
newfile(id)
register struct idf *id;
{
register p_symbol sym = Lookup(id, PervasiveScope, FILESYM);
1990-09-21 16:58:20 +00:00
if (listfile != sym) listline = 1;
listfile = sym;
if (! listfile) {
listline = 1;
listfile = add_file(id->id_text);
listfile->sy_file->f_scope = FileScope;
1990-08-31 18:22:53 +00:00
}
find_language(strrindex(id->id_text, '.'));
}
1990-11-06 12:23:41 +00:00
int in_wheninvoked;
1990-08-31 18:22:53 +00:00
perform(p, a)
register p_tree p;
t_addr a;
{
switch(p->t_oper) {
case OP_WHEN:
if (p->t_args[1] && ! eval_cond(p->t_args[1])) break;
1990-08-31 18:22:53 +00:00
p = p->t_args[2];
1990-11-06 12:23:41 +00:00
in_wheninvoked++;
1990-10-17 17:00:03 +00:00
while (p && p->t_oper == OP_LINK) {
if (interrupted) return;
1990-10-17 17:00:03 +00:00
if (p->t_args[0]) eval(p->t_args[0]);
1990-08-31 18:22:53 +00:00
p = p->t_args[1];
}
if (interrupted) return;
1990-10-17 17:00:03 +00:00
if (p) eval(p);
1990-11-06 12:23:41 +00:00
in_wheninvoked--;
1990-08-31 18:22:53 +00:00
break;
case OP_TRACE:
if (p->t_args[0] && p->t_args[0]->t_oper == OP_IN) {
register p_scope sc = base_scope(CurrentScope);
if (sc != get_scope_from_addr(p->t_args[0]->t_address)) {
break;
}
}
if (interrupted) return;
if (p->t_args[1] && ! eval_cond(p->t_args[1])) break;
1990-09-21 16:58:20 +00:00
list_position(get_position_from_addr(a));
if (p->t_args[2]) do_print(p->t_args[2]);
1990-08-31 18:22:53 +00:00
break;
default:
assert(0);
}
}
1990-09-21 16:58:20 +00:00
list_position(pos)
p_position pos;
{
newfile(str2idf(pos->filename, 1));
currfile = listfile;
currline = pos->lineno;
lines(currfile->sy_file, (int)currline, (int)1);
listline = 0;
1990-09-21 16:58:20 +00:00
}