1988-09-20 16:44:27 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
|
|
|
/* $Header$ */
|
|
|
|
/* Lint main routines */
|
|
|
|
|
|
|
|
#include "lint.h"
|
|
|
|
|
|
|
|
#ifdef LINT
|
|
|
|
|
|
|
|
#include <alloc.h> /* for st_free */
|
1989-02-02 13:57:07 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "interface.h"
|
1988-09-26 11:37:38 +00:00
|
|
|
#include "assert.h"
|
1988-09-20 16:44:27 +00:00
|
|
|
#include "arith.h" /* definition arith */
|
|
|
|
#include "label.h" /* definition label */
|
|
|
|
#include "expr.h"
|
|
|
|
#include "idf.h"
|
|
|
|
#include "def.h"
|
|
|
|
#include "code.h" /* RVAL etc */
|
|
|
|
#include "LLlex.h"
|
|
|
|
#include "Lpars.h"
|
|
|
|
#include "stack.h"
|
|
|
|
#include "type.h"
|
|
|
|
#include "level.h"
|
|
|
|
#include "nofloat.h"
|
|
|
|
#include "l_lint.h"
|
|
|
|
#include "l_state.h"
|
|
|
|
#include "l_outdef.h"
|
|
|
|
|
|
|
|
extern char options[128];
|
|
|
|
extern char *symbol2str();
|
|
|
|
|
1989-02-02 13:57:07 +00:00
|
|
|
PRIVATE struct expr_state *expr2state();
|
|
|
|
PRIVATE struct expr_state *value2state();
|
|
|
|
PRIVATE struct expr_state *oper2state();
|
|
|
|
PRIVATE expr_ignored();
|
|
|
|
PRIVATE add_expr_state();
|
|
|
|
PRIVATE referred_esp();
|
|
|
|
PRIVATE free_expr_states();
|
1988-09-20 16:44:27 +00:00
|
|
|
|
|
|
|
lint_init()
|
|
|
|
{
|
1988-11-03 15:18:46 +00:00
|
|
|
lint_init_comment();
|
|
|
|
lint_init_stack();
|
1988-09-20 16:44:27 +00:00
|
|
|
}
|
|
|
|
|
1989-02-02 13:57:07 +00:00
|
|
|
lint_expr(expr, used)
|
1988-09-20 16:44:27 +00:00
|
|
|
struct expr *expr;
|
1989-02-02 13:57:07 +00:00
|
|
|
int used; /* USED or IGNORED */
|
1988-09-20 16:44:27 +00:00
|
|
|
{
|
|
|
|
register struct expr_state *esp;
|
|
|
|
|
1989-02-02 13:57:07 +00:00
|
|
|
esp = expr2state(expr, RVAL, used);
|
|
|
|
referred_esp(esp);
|
1988-09-20 16:44:27 +00:00
|
|
|
free_expr_states(esp);
|
|
|
|
}
|
|
|
|
|
1989-02-02 13:57:07 +00:00
|
|
|
PRIVATE struct expr_state *
|
|
|
|
expr2state(expr, val, used)
|
1988-09-20 16:44:27 +00:00
|
|
|
register struct expr *expr;
|
1989-02-02 13:57:07 +00:00
|
|
|
int val; /* RVAL or LVAL */
|
|
|
|
int used; /* USED or IGNORED */
|
1988-09-20 16:44:27 +00:00
|
|
|
{
|
|
|
|
/* Main function to process an expression tree.
|
|
|
|
* It returns a structure containing information about which variables
|
|
|
|
* are set and which are used in the expression.
|
|
|
|
* In addition it sets 'used' and 'set' fields of the corresponding
|
|
|
|
* variables in the current state.
|
|
|
|
* If the value of an operation without side-effects is not used,
|
|
|
|
* a warning is given.
|
|
|
|
*/
|
|
|
|
if (used == IGNORED) {
|
|
|
|
expr_ignored(expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (expr->ex_class) {
|
|
|
|
case Value:
|
1989-02-02 13:57:07 +00:00
|
|
|
return value2state(expr, val);
|
1988-09-20 16:44:27 +00:00
|
|
|
|
|
|
|
case Oper:
|
1989-02-02 13:57:07 +00:00
|
|
|
return oper2state(expr, val, used);
|
1988-09-25 17:29:37 +00:00
|
|
|
|
1989-02-02 13:57:07 +00:00
|
|
|
default: /* String, Float, Type */
|
1988-09-25 17:29:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1989-02-02 13:57:07 +00:00
|
|
|
PRIVATE struct expr_state *
|
|
|
|
value2state(expr, val)
|
|
|
|
struct expr *expr;
|
|
|
|
int val; /* RVAL or LVAL */
|
1988-09-25 17:29:37 +00:00
|
|
|
{
|
|
|
|
switch (expr->VL_CLASS) {
|
|
|
|
case Const:
|
|
|
|
case Label:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case Name:
|
1988-09-20 16:44:27 +00:00
|
|
|
{
|
1988-09-25 17:29:37 +00:00
|
|
|
register struct idf *idf = expr->VL_IDF;
|
1989-02-02 13:57:07 +00:00
|
|
|
struct expr_state *esp = 0;
|
1988-09-25 17:29:37 +00:00
|
|
|
|
|
|
|
if (!idf || !idf->id_def)
|
|
|
|
return 0;
|
|
|
|
|
1989-02-02 13:57:07 +00:00
|
|
|
if (val == RVAL && expr->ex_lvalue == 1) {
|
|
|
|
/* value of identifier used */
|
1988-09-25 17:29:37 +00:00
|
|
|
change_state(idf, USED);
|
1989-02-02 13:57:07 +00:00
|
|
|
add_expr_state(expr->EX_VALUE, USED, &esp);
|
|
|
|
}
|
|
|
|
if (val == RVAL && expr->ex_lvalue == 0) {
|
|
|
|
/* address of identifier used */
|
|
|
|
add_expr_state(expr->EX_VALUE, REFERRED, &esp);
|
1988-09-25 17:29:37 +00:00
|
|
|
}
|
1989-02-02 13:57:07 +00:00
|
|
|
return esp;
|
1988-09-25 17:29:37 +00:00
|
|
|
}
|
1988-09-20 16:44:27 +00:00
|
|
|
|
1988-09-25 17:29:37 +00:00
|
|
|
default:
|
1988-09-26 11:37:38 +00:00
|
|
|
NOTREACHED();
|
1988-09-25 17:29:37 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
1988-09-20 16:44:27 +00:00
|
|
|
|
1989-02-02 13:57:07 +00:00
|
|
|
/* Let's get this straight.
|
|
|
|
An assignment is performed by elaborating the LHS and the RHS
|
|
|
|
collaterally, to use the A68 terminology, and then serially do the
|
|
|
|
actual assignment. This means:
|
|
|
|
1. evaluate the LHS as an LVAL,
|
|
|
|
2. evaluate the RHS as an RVAL,
|
|
|
|
3. merge them checking for interference,
|
|
|
|
4. set the result of the LHS to SET, if it is a named variable
|
|
|
|
*/
|
|
|
|
|
|
|
|
PRIVATE struct expr_state *
|
|
|
|
oper2state(expr, val, used)
|
1988-09-25 17:29:37 +00:00
|
|
|
struct expr *expr;
|
1989-02-02 13:57:07 +00:00
|
|
|
int val; /* RVAL or LVAL */
|
|
|
|
int used; /* USED or IGNORED */
|
1988-09-25 17:29:37 +00:00
|
|
|
{
|
|
|
|
register int oper = expr->OP_OPER;
|
|
|
|
register struct expr *left = expr->OP_LEFT;
|
|
|
|
register struct expr *right = expr->OP_RIGHT;
|
1989-02-02 13:57:07 +00:00
|
|
|
struct expr_state *esp_l = 0;
|
|
|
|
struct expr_state *esp_r = 0;
|
1988-09-25 17:29:37 +00:00
|
|
|
|
|
|
|
switch (oper) {
|
1989-02-02 13:57:07 +00:00
|
|
|
|
|
|
|
/* assignments */
|
1988-09-25 17:29:37 +00:00
|
|
|
case '=':
|
|
|
|
case PLUSAB:
|
|
|
|
case MINAB:
|
|
|
|
case TIMESAB:
|
|
|
|
case DIVAB:
|
|
|
|
case MODAB:
|
|
|
|
case LEFTAB:
|
|
|
|
case RIGHTAB:
|
|
|
|
case ANDAB:
|
|
|
|
case XORAB:
|
|
|
|
case ORAB:
|
1989-02-02 13:57:07 +00:00
|
|
|
/* evaluate the LHS, only once; see RM 7.14 */
|
|
|
|
esp_l = expr2state(left, (oper == '=' ? LVAL : RVAL), USED);
|
|
|
|
|
|
|
|
/* evaluate the RHS as an RVAL and merge */
|
|
|
|
esp_r = expr2state(right, RVAL, USED);
|
|
|
|
check_and_merge(expr, &esp_l, esp_r);
|
|
|
|
|
|
|
|
/* set resulting variable, if any */
|
|
|
|
if (ISNAME(left)) {
|
|
|
|
change_state(left->VL_IDF, SET);
|
|
|
|
add_expr_state(left->EX_VALUE, SET, &esp_l);
|
1988-09-25 17:29:37 +00:00
|
|
|
}
|
1989-02-02 13:57:07 +00:00
|
|
|
|
|
|
|
return esp_l;
|
1988-09-25 17:29:37 +00:00
|
|
|
|
|
|
|
case POSTINCR:
|
|
|
|
case POSTDECR:
|
|
|
|
case PLUSPLUS:
|
|
|
|
case MINMIN:
|
1989-02-02 13:57:07 +00:00
|
|
|
esp_l = expr2state(left, RVAL, USED);
|
|
|
|
|
|
|
|
/* set resulting variable, if any */
|
|
|
|
if (ISNAME(left)) {
|
|
|
|
change_state(left->VL_IDF, SET);
|
|
|
|
add_expr_state(left->EX_VALUE, SET, &esp_l);
|
1988-09-25 17:29:37 +00:00
|
|
|
}
|
|
|
|
|
1989-02-02 13:57:07 +00:00
|
|
|
return esp_l;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
esp_l = expr2state(left, RVAL, USED);
|
|
|
|
esp_r = expr2state(right->OP_LEFT, RVAL, USED);
|
|
|
|
check_and_merge(expr, &esp_l, esp_r);
|
|
|
|
esp_r = expr2state(right->OP_RIGHT, RVAL, USED);
|
|
|
|
check_and_merge(expr, &esp_l, esp_r);
|
|
|
|
return esp_l;
|
1988-09-25 17:29:37 +00:00
|
|
|
|
|
|
|
case '(':
|
|
|
|
if (right != 0) {
|
|
|
|
/* function call with parameters */
|
|
|
|
register struct expr *ex = right;
|
|
|
|
|
|
|
|
while ( ex->ex_class == Oper
|
|
|
|
&& ex->OP_OPER == PARCOMMA
|
1988-09-20 16:44:27 +00:00
|
|
|
) {
|
1989-02-02 13:57:07 +00:00
|
|
|
esp_r = expr2state(ex->OP_RIGHT, RVAL, USED);
|
|
|
|
check_and_merge(expr, &esp_l, esp_r);
|
1988-09-25 17:29:37 +00:00
|
|
|
ex = ex->OP_LEFT;
|
1988-09-20 16:44:27 +00:00
|
|
|
}
|
1989-02-02 13:57:07 +00:00
|
|
|
esp_r = expr2state(ex, RVAL, USED);
|
|
|
|
check_and_merge(expr, &esp_l, esp_r);
|
1988-09-25 17:29:37 +00:00
|
|
|
}
|
1989-02-02 13:57:07 +00:00
|
|
|
|
|
|
|
if (ISNAME(left)) {
|
1988-09-25 17:29:37 +00:00
|
|
|
fill_outcall(expr,
|
|
|
|
expr->ex_type->tp_fund == VOID ?
|
|
|
|
VOIDED : used
|
1988-09-20 16:44:27 +00:00
|
|
|
);
|
1988-09-25 17:29:37 +00:00
|
|
|
outcall();
|
|
|
|
left->VL_IDF->id_def->df_used = 1;
|
1988-09-20 16:44:27 +00:00
|
|
|
}
|
1988-09-25 17:29:37 +00:00
|
|
|
else {
|
1989-02-02 13:57:07 +00:00
|
|
|
esp_r = expr2state(left, RVAL, USED);
|
|
|
|
check_and_merge(expr, &esp_l, esp_r);
|
1988-09-25 17:29:37 +00:00
|
|
|
}
|
1989-02-02 13:57:07 +00:00
|
|
|
referred_esp(esp_l);
|
|
|
|
return esp_l;
|
1988-09-25 17:29:37 +00:00
|
|
|
|
|
|
|
case '.':
|
1989-02-02 13:57:07 +00:00
|
|
|
return expr2state(left, val, USED);
|
1988-09-25 17:29:37 +00:00
|
|
|
|
|
|
|
case ARROW:
|
1989-02-02 13:57:07 +00:00
|
|
|
return expr2state(left, RVAL, USED);
|
1988-09-25 17:29:37 +00:00
|
|
|
|
|
|
|
case INT2INT:
|
|
|
|
case INT2FLOAT:
|
|
|
|
case FLOAT2INT:
|
|
|
|
case FLOAT2FLOAT:
|
1989-02-02 13:57:07 +00:00
|
|
|
return expr2state(right, RVAL, USED);
|
|
|
|
|
|
|
|
/* monadic operators */
|
|
|
|
case '-':
|
|
|
|
case '*':
|
|
|
|
if (left)
|
|
|
|
goto dyadic;
|
|
|
|
case '~':
|
|
|
|
case '!':
|
|
|
|
return expr2state(right, RVAL, USED);
|
1988-09-25 17:29:37 +00:00
|
|
|
|
1989-02-02 13:57:07 +00:00
|
|
|
/* relational operators */
|
1988-09-25 17:29:37 +00:00
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case LESSEQ:
|
|
|
|
case GREATEREQ:
|
|
|
|
case EQUAL:
|
|
|
|
case NOTEQUAL:
|
|
|
|
lint_relop(left, right, oper);
|
|
|
|
lint_relop(right, left,
|
|
|
|
oper == '<' ? '>' :
|
|
|
|
oper == '>' ? '<' :
|
|
|
|
oper == LESSEQ ? GREATEREQ :
|
|
|
|
oper == GREATEREQ ? LESSEQ :
|
|
|
|
oper
|
|
|
|
);
|
1989-02-02 13:57:07 +00:00
|
|
|
goto dyadic;
|
|
|
|
|
|
|
|
/* dyadic operators */
|
|
|
|
dyadic:
|
1988-09-25 17:29:37 +00:00
|
|
|
case '+':
|
|
|
|
case '/':
|
|
|
|
case '%':
|
|
|
|
case ',':
|
|
|
|
case LEFT:
|
|
|
|
case RIGHT:
|
|
|
|
case '&':
|
|
|
|
case '|':
|
|
|
|
case '^':
|
|
|
|
case OR:
|
|
|
|
case AND:
|
1989-02-02 13:57:07 +00:00
|
|
|
esp_l = expr2state(left, RVAL,
|
1988-09-25 17:29:37 +00:00
|
|
|
oper == ',' ? IGNORED : USED);
|
1989-02-02 13:57:07 +00:00
|
|
|
esp_r = expr2state(right, RVAL,
|
1988-09-25 17:29:37 +00:00
|
|
|
oper == ',' ? used : USED);
|
1989-02-02 13:57:07 +00:00
|
|
|
check_and_merge(expr, &esp_l, esp_r);
|
|
|
|
|
|
|
|
return esp_l;
|
1988-09-20 16:44:27 +00:00
|
|
|
|
|
|
|
default:
|
1988-09-25 17:29:37 +00:00
|
|
|
return 0; /* for initcomma */
|
1988-09-20 16:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1989-02-02 13:57:07 +00:00
|
|
|
PRIVATE
|
1988-09-20 16:44:27 +00:00
|
|
|
expr_ignored(expr)
|
|
|
|
struct expr *expr;
|
|
|
|
{
|
|
|
|
switch (expr->ex_class) {
|
|
|
|
case Oper:
|
|
|
|
switch (expr->OP_OPER) {
|
|
|
|
case '=':
|
|
|
|
case TIMESAB:
|
|
|
|
case DIVAB:
|
|
|
|
case MODAB:
|
|
|
|
case LEFTAB:
|
|
|
|
case RIGHTAB:
|
|
|
|
case ANDAB:
|
|
|
|
case XORAB:
|
|
|
|
case ORAB:
|
|
|
|
case AND: /* doubtful but useful */
|
|
|
|
case OR: /* doubtful but useful */
|
|
|
|
case '(':
|
|
|
|
case '?':
|
|
|
|
case ',':
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PLUSAB:
|
|
|
|
case MINAB:
|
|
|
|
case POSTINCR:
|
|
|
|
case POSTDECR:
|
|
|
|
case PLUSPLUS:
|
|
|
|
case MINMIN:
|
1989-02-02 13:57:07 +00:00
|
|
|
/* may hide the operator '*' */
|
1988-09-20 16:44:27 +00:00
|
|
|
if ( /* operation on a pointer */
|
|
|
|
expr->OP_TYPE->tp_fund == POINTER
|
|
|
|
&& /* the result is dereferenced, e.g. *p++; */
|
|
|
|
expr->ex_type == expr->OP_TYPE->tp_up
|
|
|
|
) {
|
|
|
|
hwarning("result of * ignored");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
hwarning("result of %s ignored",
|
|
|
|
symbol2str(expr->OP_OPER));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Value:
|
1988-11-03 15:18:46 +00:00
|
|
|
hwarning("value as statement");
|
1988-09-20 16:44:27 +00:00
|
|
|
break;
|
|
|
|
|
1988-09-25 17:29:37 +00:00
|
|
|
default: /* String Float */
|
1988-09-20 16:44:27 +00:00
|
|
|
hwarning("constant as statement");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1989-02-02 13:57:07 +00:00
|
|
|
PRIVATE
|
|
|
|
add_expr_state(value, to_state, espp)
|
|
|
|
struct value value;
|
|
|
|
struct expr_state **espp;
|
|
|
|
{
|
|
|
|
register struct expr_state *esp = *espp;
|
|
|
|
|
|
|
|
ASSERT(value.vl_class == Name);
|
|
|
|
|
|
|
|
/* try to find the esp */
|
|
|
|
while ( esp
|
|
|
|
&& !( esp->es_idf == value.vl_data.vl_idf
|
|
|
|
&& esp->es_offset == value.vl_value
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
esp = esp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if not found, add it */
|
|
|
|
if (!esp) {
|
|
|
|
esp = new_expr_state();
|
|
|
|
esp->es_idf = value.vl_data.vl_idf;
|
|
|
|
esp->es_offset = value.vl_value;
|
|
|
|
esp->next = *espp;
|
|
|
|
*espp = esp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set state */
|
|
|
|
switch (to_state) {
|
|
|
|
case USED:
|
|
|
|
esp->es_used = 1;
|
|
|
|
break;
|
|
|
|
case REFERRED:
|
|
|
|
esp->es_referred = 1;
|
|
|
|
break;
|
|
|
|
case SET:
|
|
|
|
esp->es_set = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NOTREACHED();
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIVATE
|
|
|
|
referred_esp(esp)
|
|
|
|
struct expr_state *esp;
|
|
|
|
{
|
|
|
|
/* raises all REFERRED items to SET and USED status */
|
|
|
|
while (esp) {
|
|
|
|
if (esp->es_referred) {
|
|
|
|
esp->es_set = 1;
|
|
|
|
change_state(esp->es_idf, SET);
|
|
|
|
esp->es_used = 1;
|
|
|
|
change_state(esp->es_idf, USED);
|
|
|
|
esp->es_referred = 0;
|
|
|
|
}
|
|
|
|
esp = esp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIVATE
|
|
|
|
free_expr_states(esp)
|
|
|
|
register struct expr_state *esp;
|
|
|
|
{
|
|
|
|
while (esp) {
|
|
|
|
register struct expr_state *esp2 = esp;
|
|
|
|
|
|
|
|
esp = esp->next;
|
|
|
|
free_expr_state(esp2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
print_esp(msg, esp)
|
|
|
|
char *msg;
|
|
|
|
struct expr_state *esp;
|
|
|
|
{
|
|
|
|
print("%s: <", msg);
|
|
|
|
while (esp) {
|
|
|
|
print(" %s[%d]%c%c%c ",
|
|
|
|
esp->es_idf->id_text, esp->es_offset,
|
|
|
|
(esp->es_used ? 'U' : ' '),
|
|
|
|
(esp->es_referred ? 'R' : ' '),
|
|
|
|
(esp->es_set ? 'S' : ' ')
|
|
|
|
);
|
|
|
|
esp = esp->next;
|
|
|
|
}
|
|
|
|
print(">\n");
|
|
|
|
}
|
|
|
|
#endif DEBUG
|
|
|
|
|
1988-09-20 16:44:27 +00:00
|
|
|
#endif LINT
|