xref: /csrg-svn/old/dbx/check.c (revision 42683)
121600Sdist /*
238105Sbostic  * Copyright (c) 1983 The Regents of the University of California.
338105Sbostic  * All rights reserved.
438105Sbostic  *
5*42683Sbostic  * %sccs.include.redist.c%
621600Sdist  */
79660Slinton 
821600Sdist #ifndef lint
9*42683Sbostic static char sccsid[] = "@(#)check.c	5.4 (Berkeley) 06/01/90";
1038105Sbostic #endif /* not lint */
119660Slinton 
129660Slinton /*
139660Slinton  * Check a tree for semantic correctness.
149660Slinton  */
159660Slinton 
169660Slinton #include "defs.h"
179660Slinton #include "tree.h"
189660Slinton #include "operators.h"
199660Slinton #include "events.h"
209660Slinton #include "symbols.h"
219660Slinton #include "scanner.h"
229660Slinton #include "source.h"
239660Slinton #include "object.h"
249660Slinton #include "mappings.h"
259660Slinton #include "process.h"
2618214Slinton #include <signal.h>
279660Slinton 
289660Slinton #ifndef public
299660Slinton #endif
309660Slinton 
319660Slinton /*
329660Slinton  * Check that the nodes in a tree have the correct arguments
339660Slinton  * in order to be evaluated.  Basically the error checking here
349660Slinton  * frees the evaluation routines from worrying about anything
359660Slinton  * except dynamic errors, e.g. subscript out of range.
369660Slinton  */
379660Slinton 
check(p)389660Slinton public check(p)
399660Slinton register Node p;
409660Slinton {
4118214Slinton     Node p1, p2;
429660Slinton     Address addr;
439660Slinton     Symbol f;
449660Slinton 
459660Slinton     checkref(p);
469660Slinton     switch (p->op) {
4718214Slinton 	case O_ASSIGN:
4818214Slinton 	    p1 = p->value.arg[0];
4918214Slinton 	    p2 = p->value.arg[1];
5025811Sdonn 	    if (varIsSet("$unsafeassign")) {
5125811Sdonn 		if (size(p1->nodetype) != size(p2->nodetype)) {
5225811Sdonn 		    error("incompatible sizes");
5325811Sdonn 		}
5425811Sdonn 	    } else if (not compatible(p1->nodetype, p2->nodetype)) {
5518214Slinton 		error("incompatible types");
5618214Slinton 	    }
5718214Slinton 	    break;
5818214Slinton 
5918214Slinton 	case O_CATCH:
6018214Slinton 	case O_IGNORE:
6118214Slinton 	    if (p->value.lcon < 0 or p->value.lcon > NSIG) {
6218214Slinton 		error("invalid signal number");
6318214Slinton 	    }
6418214Slinton 	    break;
6518214Slinton 
6618214Slinton 	case O_CONT:
6718214Slinton 	    if (p->value.lcon != DEFSIG and (
6818214Slinton 		p->value.lcon < 0 or p->value.lcon > NSIG)
6918214Slinton 	    ) {
7018214Slinton 		error("invalid signal number");
7118214Slinton 	    }
7218214Slinton 	    break;
7318214Slinton 
7418214Slinton 	case O_DUMP:
7518214Slinton 	    if (p->value.arg[0] != nil) {
7618214Slinton 		if (p->value.arg[0]->op == O_SYM) {
7718214Slinton 		    f = p->value.arg[0]->value.sym;
7818214Slinton 		    if (not isblock(f)) {
7918214Slinton 			error("\"%s\" is not a block", symname(f));
8018214Slinton 		    }
8118214Slinton 		} else {
8218214Slinton 		    beginerrmsg();
8318214Slinton 		    fprintf(stderr, "expected a symbol, found \"");
8418214Slinton 		    prtree(stderr, p->value.arg[0]);
8518214Slinton 		    fprintf(stderr, "\"");
8618214Slinton 		    enderrmsg();
8718214Slinton 		}
8818214Slinton 	    }
8918214Slinton 	    break;
9018214Slinton 
919660Slinton 	case O_LIST:
929660Slinton 	    if (p->value.arg[0]->op == O_SYM) {
939660Slinton 		f = p->value.arg[0]->value.sym;
949660Slinton 		if (not isblock(f) or ismodule(f)) {
959660Slinton 		    error("\"%s\" is not a procedure or function", symname(f));
969660Slinton 		}
979660Slinton 		addr = firstline(f);
989660Slinton 		if (addr == NOADDR) {
999660Slinton 		    error("\"%s\" is empty", symname(f));
1009660Slinton 		}
1019660Slinton 	    }
1029660Slinton 	    break;
1039660Slinton 
1049660Slinton 	case O_TRACE:
1059660Slinton 	case O_TRACEI:
1069660Slinton 	    chktrace(p);
1079660Slinton 	    break;
1089660Slinton 
1099660Slinton 	case O_STOP:
1109660Slinton 	case O_STOPI:
1119660Slinton 	    chkstop(p);
1129660Slinton 	    break;
1139660Slinton 
11418214Slinton 	case O_CALLPROC:
11516607Ssam 	case O_CALL:
11616607Ssam 	    if (not isroutine(p->value.arg[0]->nodetype)) {
11716607Ssam 		beginerrmsg();
11816607Ssam 		fprintf(stderr, "\"");
11916607Ssam 		prtree(stderr, p->value.arg[0]);
12016607Ssam 		fprintf(stderr, "\" not call-able");
12116607Ssam 		enderrmsg();
12216607Ssam 	    }
12316607Ssam 	    break;
12416607Ssam 
12518214Slinton 	case O_WHEREIS:
12618214Slinton 	    if (p->value.arg[0]->op == O_SYM and
12718214Slinton 	      p->value.arg[0]->value.sym == nil) {
12818214Slinton 		error("symbol not defined");
12918214Slinton 	    }
13018214Slinton 	    break;
13118214Slinton 
1329660Slinton 	default:
1339660Slinton 	    break;
1349660Slinton     }
1359660Slinton }
1369660Slinton 
1379660Slinton /*
1389660Slinton  * Check arguments to a trace command.
1399660Slinton  */
1409660Slinton 
chktrace(p)1419660Slinton private chktrace(p)
1429660Slinton Node p;
1439660Slinton {
1449660Slinton     Node exp, place, cond;
1459660Slinton 
1469660Slinton     exp = p->value.arg[0];
1479660Slinton     place = p->value.arg[1];
1489660Slinton     cond = p->value.arg[2];
1499660Slinton     if (exp == nil) {
1509660Slinton 	chkblock(place);
1519660Slinton     } else if (exp->op == O_LCON or exp->op == O_QLINE) {
1529660Slinton 	if (place != nil) {
1539660Slinton 	    error("unexpected \"at\" or \"in\"");
1549660Slinton 	}
1559660Slinton 	if (p->op == O_TRACE) {
1569660Slinton 	    chkline(exp);
1579660Slinton 	} else {
1589660Slinton 	    chkaddr(exp);
1599660Slinton 	}
1609660Slinton     } else if (place != nil and (place->op == O_QLINE or place->op == O_LCON)) {
1619660Slinton 	if (p->op == O_TRACE) {
1629660Slinton 	    chkline(place);
1639660Slinton 	} else {
1649660Slinton 	    chkaddr(place);
1659660Slinton 	}
1669660Slinton     } else {
1679861Slinton 	if (exp->op != O_RVAL and exp->op != O_SYM and exp->op != O_CALL) {
1689660Slinton 	    error("can't trace expressions");
1699660Slinton 	}
1709660Slinton 	chkblock(place);
1719660Slinton     }
1729660Slinton }
1739660Slinton 
1749660Slinton /*
1759660Slinton  * Check arguments to a stop command.
1769660Slinton  */
1779660Slinton 
chkstop(p)1789660Slinton private chkstop(p)
1799660Slinton Node p;
1809660Slinton {
1819660Slinton     Node exp, place, cond;
1829660Slinton 
1839660Slinton     exp = p->value.arg[0];
1849660Slinton     place = p->value.arg[1];
1859660Slinton     cond = p->value.arg[2];
1869660Slinton     if (exp != nil) {
18714444Slinton 	if (exp->op != O_RVAL and exp->op != O_SYM and exp->op != O_LCON) {
1889660Slinton 	    beginerrmsg();
1899660Slinton 	    fprintf(stderr, "expected variable, found ");
1909660Slinton 	    prtree(stderr, exp);
1919660Slinton 	    enderrmsg();
1929660Slinton 	}
1939660Slinton 	chkblock(place);
19416607Ssam     } else if (place != nil) {
19516607Ssam 	if (place->op == O_SYM) {
19616607Ssam 	    chkblock(place);
1979660Slinton 	} else {
19816607Ssam 	    if (p->op == O_STOP) {
19916607Ssam 		chkline(place);
20016607Ssam 	    } else {
20116607Ssam 		chkaddr(place);
20216607Ssam 	    }
2039660Slinton 	}
2049660Slinton     }
2059660Slinton }
2069660Slinton 
2079660Slinton /*
2089660Slinton  * Check to see that the given node specifies some subprogram.
2099660Slinton  * Nil is ok since that means the entire program.
2109660Slinton  */
2119660Slinton 
chkblock(b)2129660Slinton private chkblock(b)
2139660Slinton Node b;
2149660Slinton {
21516607Ssam     Symbol p, outer;
21616607Ssam 
2179660Slinton     if (b != nil) {
2189660Slinton 	if (b->op != O_SYM) {
2199660Slinton 	    beginerrmsg();
2209660Slinton 	    fprintf(stderr, "expected subprogram, found ");
2219660Slinton 	    prtree(stderr, b);
2229660Slinton 	    enderrmsg();
22316607Ssam 	} else if (ismodule(b->value.sym)) {
22416607Ssam 	    outer = b->value.sym;
22516607Ssam 	    while (outer != nil) {
22616607Ssam 		find(p, outer->name) where p->block == outer endfind(p);
22716607Ssam 		if (p == nil) {
22816607Ssam 		    outer = nil;
22916607Ssam 		    error("\"%s\" is not a subprogram", symname(b->value.sym));
23016607Ssam 		} else if (ismodule(p)) {
23116607Ssam 		    outer = p;
23216607Ssam 		} else {
23316607Ssam 		    outer = nil;
23416607Ssam 		    b->value.sym = p;
23516607Ssam 		}
23616607Ssam 	    }
23718214Slinton 	} else if (
23818214Slinton 	    b->value.sym->class == VAR and
23918214Slinton 	    b->value.sym->name == b->value.sym->block->name and
24018214Slinton 	    b->value.sym->block->class == FUNC
24118214Slinton 	) {
24218214Slinton 	    b->value.sym = b->value.sym->block;
24316607Ssam 	} else if (not isblock(b->value.sym)) {
2449660Slinton 	    error("\"%s\" is not a subprogram", symname(b->value.sym));
2459660Slinton 	}
2469660Slinton     }
2479660Slinton }
2489660Slinton 
2499660Slinton /*
2509660Slinton  * Check to make sure a node corresponds to a source line.
2519660Slinton  */
2529660Slinton 
chkline(p)2539660Slinton private chkline(p)
2549660Slinton Node p;
2559660Slinton {
2569660Slinton     if (p == nil) {
2579660Slinton 	error("missing line");
2589660Slinton     } else if (p->op != O_QLINE and p->op != O_LCON) {
2599660Slinton 	error("expected source line number, found \"%t\"", p);
2609660Slinton     }
2619660Slinton }
2629660Slinton 
2639660Slinton /*
2649660Slinton  * Check to make sure a node corresponds to an address.
2659660Slinton  */
2669660Slinton 
chkaddr(p)2679660Slinton private chkaddr(p)
2689660Slinton Node p;
2699660Slinton {
2709660Slinton     if (p == nil) {
2719660Slinton 	error("missing address");
2729660Slinton     } else if (p->op != O_LCON and p->op != O_QLINE) {
2739660Slinton 	beginerrmsg();
2749660Slinton 	fprintf(stderr, "expected address, found \"");
2759660Slinton 	prtree(stderr, p);
2769660Slinton 	fprintf(stderr, "\"");
2779660Slinton 	enderrmsg();
2789660Slinton     }
2799660Slinton }
280