1989-09-25 14:28:10 +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".
|
|
|
|
*/
|
1994-06-27 08:03:14 +00:00
|
|
|
/* $Id$ */
|
1989-09-25 14:28:10 +00:00
|
|
|
/* SEMANTIC ANALYSIS (CHAPTER 3.3) -- BINARY OPERATORS */
|
|
|
|
|
2013-05-12 19:45:55 +00:00
|
|
|
#include "parameters.h"
|
1989-09-25 14:28:10 +00:00
|
|
|
#include <alloc.h>
|
|
|
|
#include <flt_arith.h>
|
|
|
|
#include "arith.h"
|
|
|
|
#include "type.h"
|
|
|
|
#include "struct.h"
|
|
|
|
#include "label.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "Lpars.h"
|
1990-04-25 15:09:51 +00:00
|
|
|
#include "sizes.h"
|
1989-09-25 14:28:10 +00:00
|
|
|
|
|
|
|
extern char options[];
|
|
|
|
extern char *symbol2str();
|
|
|
|
|
2016-11-10 21:04:18 +00:00
|
|
|
void pntminuspnt();
|
|
|
|
|
1989-09-25 14:28:10 +00:00
|
|
|
/* This chapter asks for the repeated application of code to handle
|
|
|
|
an operation that may be executed at compile time or at run time,
|
|
|
|
depending on the constancy of the operands.
|
|
|
|
*/
|
|
|
|
|
1990-09-06 11:32:51 +00:00
|
|
|
/*
|
|
|
|
* Although the relational operators are generally not commutative, we can
|
|
|
|
* switch the arguments if the operator is adapted (e.g. < becomes >)
|
|
|
|
*/
|
1989-09-25 14:28:10 +00:00
|
|
|
#define non_commutative_binop(expp, oper, expr) mk_binop(expp, oper, expr, 0)
|
1990-09-06 11:32:51 +00:00
|
|
|
#define commutative_binop(expp, oper, expr) mk_binop(expp, oper, expr, 1)
|
|
|
|
#define non_commutative_relop(expp, oper, expr) mk_binop(expp, oper, expr, 1)
|
1989-09-25 14:28:10 +00:00
|
|
|
|
2016-11-10 21:04:18 +00:00
|
|
|
void
|
1989-09-25 14:28:10 +00:00
|
|
|
ch3bin(expp, oper, expr)
|
|
|
|
register struct expr **expp;
|
|
|
|
struct expr *expr;
|
|
|
|
{
|
|
|
|
/* apply binary operator oper between *expp and expr.
|
|
|
|
NB: don't swap operands if op is one of the op= operators!!!
|
|
|
|
*/
|
1989-10-19 14:53:25 +00:00
|
|
|
register struct type *expp_tp;
|
1989-09-25 14:28:10 +00:00
|
|
|
|
|
|
|
any2opnd(expp, oper);
|
1989-10-19 14:53:25 +00:00
|
|
|
expp_tp = (*expp)->ex_type;
|
1989-11-22 13:58:36 +00:00
|
|
|
/* expp_tp can never be ARRAY, since any2opnd() converts the type
|
|
|
|
* to pointer (except for SIZEOF and unary &).
|
|
|
|
*/
|
1989-09-25 14:28:10 +00:00
|
|
|
any2opnd(&expr, oper);
|
|
|
|
switch (oper) {
|
|
|
|
case '[': /* 3.3.2.1 */
|
|
|
|
/* indexing follows the commutative laws */
|
1989-10-19 14:53:25 +00:00
|
|
|
switch (expp_tp->tp_fund) {
|
1989-09-25 14:28:10 +00:00
|
|
|
case POINTER:
|
|
|
|
break;
|
|
|
|
case ERRONEOUS:
|
|
|
|
return;
|
|
|
|
default: /* unindexable */
|
|
|
|
switch (expr->ex_type->tp_fund) {
|
|
|
|
case POINTER:
|
|
|
|
break;
|
|
|
|
case ERRONEOUS:
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
expr_error(*expp,
|
|
|
|
"indexing an object of type %s",
|
1989-10-19 14:53:25 +00:00
|
|
|
symbol2str(expp_tp->tp_fund));
|
1989-09-25 14:28:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ch3bin(expp, '+', expr);
|
|
|
|
ch3mon('*', expp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '(': /* 3.3.2.2 */
|
1989-12-12 12:52:03 +00:00
|
|
|
if (expp_tp->tp_fund == POINTER
|
|
|
|
&& expp_tp->tp_up->tp_fund == FUNCTION) {
|
1989-09-25 14:28:10 +00:00
|
|
|
ch3mon('*', expp);
|
1989-10-19 14:53:25 +00:00
|
|
|
expp_tp = (*expp)->ex_type;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
1989-10-19 14:53:25 +00:00
|
|
|
if (expp_tp->tp_fund != FUNCTION) {
|
1989-09-25 14:28:10 +00:00
|
|
|
expr_error(*expp, "call of non-function (%s)",
|
1989-10-19 14:53:25 +00:00
|
|
|
symbol2str(expp_tp->tp_fund));
|
1989-09-25 14:28:10 +00:00
|
|
|
/* leave the expression; it may still serve */
|
|
|
|
free_expression(expr); /* there go the parameters */
|
|
|
|
*expp = new_oper(error_type,
|
|
|
|
*expp, '(', (struct expr *)0);
|
|
|
|
}
|
|
|
|
else
|
1990-01-18 16:58:53 +00:00
|
|
|
*expp = new_oper(expp_tp->tp_up, *expp, '(', expr);
|
1989-09-25 14:28:10 +00:00
|
|
|
(*expp)->ex_flags |= EX_SIDEEFFECTS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PARCOMMA: /* 3.3.2.2 */
|
|
|
|
*expp = new_oper(expr->ex_type, *expp, PARCOMMA, expr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '%':
|
|
|
|
case MODAB:
|
|
|
|
case ANDAB:
|
|
|
|
case XORAB:
|
|
|
|
case ORAB:
|
|
|
|
opnd2integral(expp, oper);
|
|
|
|
opnd2integral(&expr, oper);
|
|
|
|
/* fallthrough */
|
|
|
|
case '/':
|
|
|
|
case DIVAB:
|
|
|
|
case TIMESAB:
|
|
|
|
arithbalance(expp, oper, &expr);
|
|
|
|
non_commutative_binop(expp, oper, expr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '&':
|
|
|
|
case '^':
|
|
|
|
case '|':
|
|
|
|
opnd2integral(expp, oper);
|
|
|
|
opnd2integral(&expr, oper);
|
|
|
|
/* fallthrough */
|
|
|
|
case '*':
|
|
|
|
arithbalance(expp, oper, &expr);
|
|
|
|
commutative_binop(expp, oper, expr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
if (expr->ex_type->tp_fund == POINTER) { /* swap operands */
|
|
|
|
struct expr *etmp = expr;
|
1989-11-22 13:58:36 +00:00
|
|
|
expp_tp = expr->ex_type; /* both in registers */
|
1989-09-25 14:28:10 +00:00
|
|
|
expr = *expp;
|
|
|
|
*expp = etmp;
|
|
|
|
}
|
|
|
|
/* fallthrough */
|
|
|
|
case PLUSAB:
|
|
|
|
case POSTINCR:
|
|
|
|
case PLUSPLUS:
|
1989-10-19 14:53:25 +00:00
|
|
|
if (expp_tp->tp_fund == POINTER) {
|
1989-09-25 14:28:10 +00:00
|
|
|
pointer_arithmetic(expp, oper, &expr);
|
|
|
|
if (expr->ex_type->tp_size != (*expp)->ex_type->tp_size)
|
|
|
|
ch3cast(&expr, CAST, (*expp)->ex_type);
|
|
|
|
pointer_binary(expp, oper, expr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
arithbalance(expp, oper, &expr);
|
|
|
|
if (oper == '+')
|
|
|
|
commutative_binop(expp, oper, expr);
|
|
|
|
else
|
|
|
|
non_commutative_binop(expp, oper, expr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
case MINAB:
|
|
|
|
case POSTDECR:
|
|
|
|
case MINMIN:
|
1989-10-19 14:53:25 +00:00
|
|
|
if (expp_tp->tp_fund == POINTER) {
|
1989-09-25 14:28:10 +00:00
|
|
|
if (expr->ex_type->tp_fund == POINTER)
|
|
|
|
pntminuspnt(expp, oper, expr);
|
|
|
|
else {
|
|
|
|
pointer_arithmetic(expp, oper, &expr);
|
|
|
|
pointer_binary(expp, oper, expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
arithbalance(expp, oper, &expr);
|
|
|
|
non_commutative_binop(expp, oper, expr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LEFT:
|
|
|
|
case RIGHT:
|
|
|
|
case LEFTAB:
|
|
|
|
case RIGHTAB:
|
|
|
|
opnd2integral(expp, oper);
|
|
|
|
opnd2integral(&expr, oper);
|
1989-09-29 16:20:38 +00:00
|
|
|
arithbalance(expp, oper, &expr); /* ch. 3.3.7 */
|
1989-09-25 14:28:10 +00:00
|
|
|
ch3cast(&expr, oper, int_type); /* cvt. rightop to int */
|
|
|
|
non_commutative_binop(expp, oper, expr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case LESSEQ:
|
|
|
|
case GREATEREQ:
|
|
|
|
case EQUAL:
|
|
|
|
case NOTEQUAL:
|
|
|
|
relbalance(expp, oper, &expr);
|
1990-09-06 11:32:51 +00:00
|
|
|
non_commutative_relop(expp, oper, expr);
|
1989-09-25 14:28:10 +00:00
|
|
|
(*expp)->ex_type = int_type;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AND:
|
|
|
|
case OR:
|
|
|
|
opnd2test(expp, oper);
|
|
|
|
opnd2test(&expr, oper);
|
|
|
|
if (is_cp_cst(*expp)) {
|
|
|
|
register struct expr *ex = *expp;
|
|
|
|
|
|
|
|
/* the following condition is a short-hand for
|
|
|
|
((oper == AND) && o1) || ((oper == OR) && !o1)
|
|
|
|
where o1 == (*expp)->VL_VALUE;
|
|
|
|
and ((oper == AND) || (oper == OR))
|
|
|
|
*/
|
1993-01-18 15:10:01 +00:00
|
|
|
if ((oper == AND) == (ex->VL_VALUE != (arith)0)) {
|
1989-09-25 14:28:10 +00:00
|
|
|
*expp = expr;
|
1993-01-18 15:10:01 +00:00
|
|
|
}
|
1989-09-25 14:28:10 +00:00
|
|
|
else {
|
|
|
|
ex->ex_flags |= expr->ex_flags;
|
|
|
|
free_expression(expr);
|
1990-03-20 16:24:21 +00:00
|
|
|
*expp = intexpr((arith)(oper != AND), INT);
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
1989-10-20 13:06:10 +00:00
|
|
|
(*expp)->ex_flags |= ex->ex_flags | EX_ILVALUE;
|
1989-09-25 14:28:10 +00:00
|
|
|
free_expression(ex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (is_cp_cst(expr)) {
|
|
|
|
/* Note!!!: the following condition is a short-hand for
|
|
|
|
((oper == AND) && o2) || ((oper == OR) && !o2)
|
|
|
|
where o2 == expr->VL_VALUE
|
|
|
|
and ((oper == AND) || (oper == OR))
|
|
|
|
*/
|
|
|
|
if ((oper == AND) == (expr->VL_VALUE != (arith)0)) {
|
1989-10-20 13:06:10 +00:00
|
|
|
(*expp)->ex_flags |= expr->ex_flags | EX_ILVALUE;
|
1989-09-25 14:28:10 +00:00
|
|
|
free_expression(expr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (oper == OR)
|
|
|
|
expr->VL_VALUE = (arith)1;
|
|
|
|
ch3bin(expp, ',', expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*expp = new_oper(int_type, *expp, oper, expr);
|
|
|
|
}
|
|
|
|
(*expp)->ex_flags |= EX_LOGICAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ':':
|
1989-12-12 12:52:03 +00:00
|
|
|
if (is_struct_or_union(expp_tp->tp_fund)
|
|
|
|
|| is_struct_or_union(expr->ex_type->tp_fund)) {
|
1991-03-26 09:45:20 +00:00
|
|
|
if (!equal_type(expp_tp, expr->ex_type, -1, 0))
|
1989-09-25 14:28:10 +00:00
|
|
|
expr_error(*expp, "illegal balance");
|
|
|
|
}
|
1989-12-12 12:52:03 +00:00
|
|
|
else
|
1989-09-25 14:28:10 +00:00
|
|
|
relbalance(expp, oper, &expr);
|
|
|
|
#ifdef LINT
|
|
|
|
if ( (is_cp_cst(*expp) && is_cp_cst(expr))
|
|
|
|
&& (*expp)->VL_VALUE == expr->VL_VALUE
|
|
|
|
) {
|
|
|
|
hwarning("operands of : are constant and equal");
|
|
|
|
}
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* LINT */
|
1989-09-25 14:28:10 +00:00
|
|
|
*expp = new_oper((*expp)->ex_type, *expp, oper, expr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
|
|
|
opnd2logical(expp, oper);
|
|
|
|
if (is_cp_cst(*expp)) {
|
|
|
|
#ifdef LINT
|
|
|
|
hwarning("condition in ?: expression is constant");
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* LINT */
|
1993-01-18 15:10:01 +00:00
|
|
|
if ((*expp)->VL_VALUE) {
|
|
|
|
free_expression(*expp);
|
|
|
|
free_expression(expr->OP_RIGHT);
|
|
|
|
*expp = expr->OP_LEFT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
free_expression(*expp);
|
|
|
|
free_expression(expr->OP_LEFT);
|
|
|
|
*expp = expr->OP_RIGHT;
|
|
|
|
}
|
|
|
|
free_expr(expr);
|
1989-10-13 11:04:42 +00:00
|
|
|
(*expp)->ex_flags |= EX_ILVALUE;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*expp = new_oper(expr->ex_type, *expp, oper, expr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ',':
|
1990-12-07 14:42:26 +00:00
|
|
|
if (is_cp_cst(*expp)) {
|
|
|
|
#ifdef LINT
|
|
|
|
hwarning("constant expression ignored");
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* LINT */
|
1993-01-18 15:10:01 +00:00
|
|
|
free_expression(*expp);
|
1989-09-25 14:28:10 +00:00
|
|
|
*expp = expr;
|
1990-12-07 14:42:26 +00:00
|
|
|
}
|
|
|
|
else {
|
1989-09-25 14:28:10 +00:00
|
|
|
*expp = new_oper(expr->ex_type, *expp, oper, expr);
|
1990-12-07 14:42:26 +00:00
|
|
|
}
|
1989-09-25 14:28:10 +00:00
|
|
|
(*expp)->ex_flags |= EX_COMMA;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 21:04:18 +00:00
|
|
|
void
|
1989-09-25 14:28:10 +00:00
|
|
|
pntminuspnt(expp, oper, expr)
|
|
|
|
register struct expr **expp, *expr;
|
|
|
|
{
|
|
|
|
/* Subtracting two pointers is so complicated it merits a
|
|
|
|
routine of its own.
|
|
|
|
*/
|
|
|
|
struct type *up_type = (*expp)->ex_type->tp_up;
|
|
|
|
|
1991-03-26 09:45:20 +00:00
|
|
|
if (!equal_type(up_type, expr->ex_type->tp_up, -1, 0)) {
|
1989-09-25 14:28:10 +00:00
|
|
|
expr_error(*expp, "subtracting incompatible pointers");
|
|
|
|
free_expression(expr);
|
|
|
|
erroneous2int(expp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* we hope the optimizer will eliminate the load-time
|
|
|
|
pointer subtraction
|
|
|
|
*/
|
|
|
|
*expp = new_oper((*expp)->ex_type, *expp, oper, expr);
|
|
|
|
ch3cast(expp, CAST, pa_type); /* ptr-ptr: result has pa_type */
|
1989-11-22 13:58:36 +00:00
|
|
|
ch3bin(expp, '/'
|
|
|
|
, intexpr(size_of_type(up_type, symbol2str(up_type->tp_fund))
|
|
|
|
, pa_type->tp_fund));
|
|
|
|
ch3cast(expp, CAST, pa_type); /* result will be an integral expr */
|
1989-10-20 13:06:10 +00:00
|
|
|
/* cast necessary ??? */
|
1990-04-25 15:09:51 +00:00
|
|
|
if (int_size != pointer_size) (*expp)->ex_flags |= EX_PTRDIFF;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
|
1990-09-06 11:32:51 +00:00
|
|
|
/*
|
|
|
|
* The function arg_switched() returns the operator that should be used
|
|
|
|
* when the arguments are switched. This is special for some relational
|
|
|
|
* operators.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
arg_switched(oper)
|
|
|
|
{
|
|
|
|
switch (oper) {
|
|
|
|
case '<': return '>';
|
|
|
|
case '>': return '<';
|
|
|
|
case LESSEQ: return GREATEREQ;
|
|
|
|
case GREATEREQ: return LESSEQ;
|
|
|
|
default: return oper;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1989-09-25 14:28:10 +00:00
|
|
|
mk_binop(expp, oper, expr, commutative)
|
|
|
|
struct expr **expp;
|
|
|
|
register struct expr *expr;
|
|
|
|
{
|
|
|
|
/* Constructs in *expp the operation indicated by the operands.
|
|
|
|
"commutative" indicates whether "oper" is a commutative
|
|
|
|
operator.
|
|
|
|
*/
|
|
|
|
register struct expr *ex = *expp;
|
|
|
|
|
|
|
|
if (is_cp_cst(expr) && is_cp_cst(ex))
|
|
|
|
cstbin(expp, oper, expr);
|
|
|
|
else if (is_fp_cst(expr) && is_fp_cst(ex))
|
|
|
|
fltcstbin(expp, oper, expr);
|
1990-09-06 11:32:51 +00:00
|
|
|
else {
|
|
|
|
*expp = (commutative
|
|
|
|
&& !(ex->ex_flags & EX_VOLATILE)
|
1990-09-06 14:49:52 +00:00
|
|
|
&& (expr->ex_depth > ex->ex_depth
|
|
|
|
|| ((expr->ex_flags & EX_SIDEEFFECTS)
|
|
|
|
&& !(ex->ex_flags & EX_SIDEEFFECTS))
|
1990-09-06 11:32:51 +00:00
|
|
|
|| is_cp_cst(ex)))
|
|
|
|
? new_oper(ex->ex_type, expr, arg_switched(oper), ex)
|
|
|
|
: new_oper(ex->ex_type, ex, oper, expr);
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pointer_arithmetic(expp1, oper, expp2)
|
|
|
|
register struct expr **expp1, **expp2;
|
|
|
|
{
|
|
|
|
int typ;
|
|
|
|
/* prepares the integral expression expp2 in order to
|
|
|
|
apply it to the pointer expression expp1
|
|
|
|
*/
|
|
|
|
if ((typ = any2arith(expp2, oper)) == FLOAT
|
|
|
|
|| typ == DOUBLE
|
|
|
|
|| typ == LNGDBL) {
|
|
|
|
expr_error(*expp2,
|
|
|
|
"illegal combination of %s and pointer",
|
|
|
|
symbol2str(typ));
|
|
|
|
erroneous2int(expp2);
|
|
|
|
}
|
|
|
|
ch3bin( expp2, '*',
|
|
|
|
intexpr(size_of_type((*expp1)->ex_type->tp_up, "object"),
|
|
|
|
pa_type->tp_fund)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
pointer_binary(expp, oper, expr)
|
|
|
|
register struct expr **expp, *expr;
|
|
|
|
{
|
|
|
|
/* constructs the pointer arithmetic expression out of
|
|
|
|
a pointer expression, a binary operator and an integral
|
|
|
|
expression.
|
|
|
|
*/
|
|
|
|
if (is_ld_cst(expr) && is_ld_cst(*expp))
|
|
|
|
cstbin(expp, oper, expr);
|
|
|
|
else
|
|
|
|
*expp = new_oper((*expp)->ex_type, *expp, oper, expr);
|
|
|
|
}
|