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) -- MONADIC OPERATORS */
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
#include "ch3mon.h"
|
2013-05-12 19:45:55 +00:00
|
|
|
#include "parameters.h"
|
1989-09-25 14:28:10 +00:00
|
|
|
#include <alloc.h>
|
|
|
|
#include "Lpars.h"
|
|
|
|
#include <flt_arith.h>
|
2013-05-12 19:45:55 +00:00
|
|
|
#include "idf.h"
|
1989-09-25 14:28:10 +00:00
|
|
|
#include "arith.h"
|
|
|
|
#include "type.h"
|
|
|
|
#include "label.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "def.h"
|
1989-12-13 12:53:22 +00:00
|
|
|
#include "sizes.h"
|
2019-02-18 16:42:15 +00:00
|
|
|
#include "ch3.h"
|
|
|
|
#include "error.h"
|
|
|
|
|
1989-09-25 14:28:10 +00:00
|
|
|
|
|
|
|
extern char options[];
|
|
|
|
char *symbol2str();
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
void ch3mon(int oper, register struct expr **expp)
|
1989-09-25 14:28:10 +00:00
|
|
|
{
|
|
|
|
/* The monadic prefix operator oper is applied to *expp.
|
|
|
|
*/
|
|
|
|
register struct expr *expr;
|
|
|
|
|
1989-11-27 11:37:11 +00:00
|
|
|
if (oper != PLUSPLUS && oper != MINMIN)
|
|
|
|
any2opnd(expp, oper);
|
1989-11-22 13:58:36 +00:00
|
|
|
|
1989-09-25 14:28:10 +00:00
|
|
|
switch (oper) {
|
|
|
|
case '*': /* 3.3.3.2 */
|
|
|
|
/* no FIELD type allowed */
|
1992-06-26 11:57:17 +00:00
|
|
|
expr = *expp;
|
|
|
|
if (expr->ex_type->tp_fund != POINTER) {
|
|
|
|
if (expr->ex_type != error_type) {
|
|
|
|
expr_error(expr,
|
1989-09-25 14:28:10 +00:00
|
|
|
"* applied to non-pointer (%s)",
|
1992-06-26 11:57:17 +00:00
|
|
|
symbol2str(expr->ex_type->tp_fund));
|
1990-09-14 16:39:11 +00:00
|
|
|
}
|
1989-09-25 14:28:10 +00:00
|
|
|
} else {
|
1990-04-06 15:37:16 +00:00
|
|
|
if (is_ld_cst(expr))
|
1989-09-25 14:28:10 +00:00
|
|
|
/* dereference in administration only */
|
|
|
|
expr->ex_type = expr->ex_type->tp_up;
|
|
|
|
else /* runtime code */
|
|
|
|
*expp = new_oper(expr->ex_type->tp_up, NILEXPR,
|
|
|
|
'*', expr);
|
1992-06-12 09:03:07 +00:00
|
|
|
expr = *expp;
|
|
|
|
expr->ex_lvalue = (
|
|
|
|
expr->ex_type->tp_fund != ARRAY &&
|
|
|
|
expr->ex_type->tp_fund != FUNCTION
|
1989-09-25 14:28:10 +00:00
|
|
|
);
|
1992-06-26 11:57:17 +00:00
|
|
|
if (expr->ex_lvalue && expr->ex_type->tp_size <= 0) {
|
|
|
|
expr_error(expr, "incomplete type in expression");
|
|
|
|
}
|
1992-06-12 09:03:07 +00:00
|
|
|
if (expr->ex_type->tp_typequal & TQ_CONST)
|
|
|
|
expr->ex_flags |= EX_READONLY;
|
|
|
|
if (expr->ex_type->tp_typequal & TQ_VOLATILE)
|
|
|
|
expr->ex_flags |= EX_VOLATILE;
|
|
|
|
expr->ex_flags &= ~EX_ILVALUE;
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
break;
|
1989-11-22 16:41:09 +00:00
|
|
|
case ADDRESSOF:
|
1989-09-25 14:28:10 +00:00
|
|
|
if ((*expp)->ex_type->tp_fund == ARRAY) {
|
1989-11-22 13:58:36 +00:00
|
|
|
(*expp)->ex_type = pointer_to((*expp)->ex_type, 0);
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if ((*expp)->ex_type->tp_fund == FUNCTION) {
|
1989-11-22 13:58:36 +00:00
|
|
|
(*expp)->ex_type = pointer_to((*expp)->ex_type, 0);
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#ifndef NOBITFIELD
|
|
|
|
if ((*expp)->ex_type->tp_fund == FIELD)
|
|
|
|
expr_error(*expp, "& applied to field variable");
|
|
|
|
else
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* NOBITFIELD */
|
1989-09-25 14:28:10 +00:00
|
|
|
if (!(*expp)->ex_lvalue)
|
|
|
|
expr_error(*expp, "& applied to non-lvalue");
|
|
|
|
else if ((*expp)->ex_flags & EX_ILVALUE)
|
|
|
|
expr_error(*expp, "& applied to illegal lvalue");
|
|
|
|
else {
|
|
|
|
/* assume that enums are already filtered out */
|
|
|
|
if (ISNAME(*expp)) {
|
|
|
|
register struct def *def =
|
|
|
|
(*expp)->VL_IDF->id_def;
|
|
|
|
|
|
|
|
/* &<var> indicates that <var>
|
|
|
|
cannot be used as register
|
|
|
|
anymore
|
|
|
|
*/
|
|
|
|
if (def->df_sc == REGISTER) {
|
|
|
|
expr_error(*expp,
|
|
|
|
"& on register variable not allowed");
|
1989-11-22 16:41:09 +00:00
|
|
|
break; /* break case ADDRESSOF */
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
(*expp)->ex_type = pointer_to((*expp)->ex_type,
|
|
|
|
(*expp)->ex_type->tp_typequal);
|
|
|
|
(*expp)->ex_lvalue = 0;
|
1989-09-29 16:20:38 +00:00
|
|
|
(*expp)->ex_flags &= ~(EX_READONLY | EX_VOLATILE);
|
1989-09-25 14:28:10 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
{
|
|
|
|
int fund = (*expp)->ex_type->tp_fund;
|
|
|
|
|
|
|
|
if (fund == FLOAT || fund == DOUBLE || fund == LNGDBL) {
|
|
|
|
expr_error( *expp,
|
|
|
|
"~ not allowed on %s operands",
|
|
|
|
symbol2str(fund));
|
|
|
|
erroneous2int(expp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
}
|
|
|
|
case '-':
|
|
|
|
any2arith(expp, oper);
|
|
|
|
if (is_cp_cst(*expp)) {
|
Add long long literals like 123LL to ACK C.
For now, a long long literal must have the 'LL' or 'll' suffix. A
literal without 'LL' or 'll' acts as before: it may become unsigned
long but not long long. (For targets where int and long have the same
size, some literals change from unsigned int to unsigned long.)
Type `arith` may be too narrow for long long values. Add a second
type `writh` for wide arithmetic, and change some variables from arith
to writh. This may cause bugs if I forget to use writh, or if a
conversion from writh to arith overflows. I mark some conversions
with (arith) or (writh) casts.
- BigPars, SmallPars: Remove SPECIAL_ARITHMETICS. This feature
would change arith to a different type, but can't work, because it
would conflict with definitions of arith in both <em_arith.h> and
<flt_arith.h>.
- LLlex.c: Understand 'LL' or 'll' suffix. Cut size of constant when
it overflows writh, not only when it overflows the target machine's
types. (This cut might not be necessary, because we might cut it
again later.) When picking signed long or unsigned long, check the
target's long type, not the compiler's arith type; the old check
for `val >= 0` was broken where sizeof(arith) > 4.
- LLlex.h: Change struct token's tok_ival to writh, so it can hold a
long long literal.
- arith.c: Adjust to VL_VALUE being writh. Don't convert between
float and integer at compile-time if the integer might be too wide
for <flt_arith.h>. Add writh2str(), because writh might be too
wide for long2str().
- arith.h: Remove SPECIAL_ARITHMETICS. Declare full_mask[] here,
not in several *.c files. Declare writh2str().
- ch3.c, ch3bin.c, ch3mon.c, declarator.c, statement.g: Remove
obsolete casts. Adjust to VL_VALUE being writh.
- conversion.c, stab.c: Don't declare full_mask[].
- cstoper.c: Use writh for constant operations on VL_VALUE, and for
full_mask[].
- declar., field.c, ival.g: Add casts.
- dumpidf.c: Need to #include "parameters.h" before checking DEBUG.
Use writh2str, because "%ld" might not work.
- eval.c, eval.h: Add casts. Use writh when writing a wide constant
in EM.
- expr.c: Add and remove casts. In fill_int_expr(), make expression
from long long literal. In chk_cst_expr(), allow long long as
constant expression, so the compiler may accept `case 123LL:` in a
switch statement.
- expr.str: Change struct value's vl_value and struct expr's VL_VALUE
to writh, so an expression may have a long long value at compile
time.
- statement.g: Remove obsolete casts.
- switch.c, switch.str: Use writh in case entries for switch
statements, so `switch (ll) {...}` with long long ll works.
- tokenname.c: Add ULNGLNG so LLlex.c can use it for literals.
2019-09-05 02:14:38 +00:00
|
|
|
writh o1 = (*expp)->VL_VALUE;
|
1989-09-25 14:28:10 +00:00
|
|
|
|
|
|
|
(*expp)->VL_VALUE = (oper == '-') ? -o1 :
|
|
|
|
((*expp)->ex_type->tp_unsigned ?
|
1989-10-20 11:58:37 +00:00
|
|
|
(~o1) & full_mask[(int)(*expp)->ex_type->tp_size] :
|
1989-09-25 14:28:10 +00:00
|
|
|
~o1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (is_fp_cst(*expp))
|
|
|
|
switch_sign_fp(*expp);
|
|
|
|
else
|
|
|
|
*expp = new_oper((*expp)->ex_type,
|
|
|
|
NILEXPR, oper, *expp);
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
opnd2test(expp, '!');
|
|
|
|
if (is_cp_cst(*expp)) {
|
|
|
|
(*expp)->VL_VALUE = !((*expp)->VL_VALUE);
|
|
|
|
(*expp)->ex_type = int_type; /* a cast ???(EB) */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*expp = new_oper(int_type, NILEXPR, oper, *expp);
|
|
|
|
(*expp)->ex_flags |= EX_LOGICAL;
|
|
|
|
break;
|
|
|
|
case PLUSPLUS:
|
|
|
|
case MINMIN:
|
|
|
|
ch3incr(expp, oper);
|
|
|
|
break;
|
|
|
|
case SIZEOF:
|
|
|
|
if (ISNAME(*expp) && (*expp)->VL_IDF->id_def->df_formal_array)
|
|
|
|
expr_warning(*expp, "sizeof formal array %s is sizeof pointer!",
|
|
|
|
(*expp)->VL_IDF->id_text);
|
|
|
|
expr = intexpr((*expp)->ex_class == String ?
|
|
|
|
(arith)((*expp)->SG_LEN) :
|
1989-11-22 13:58:36 +00:00
|
|
|
size_of_type((*expp)->ex_type,
|
|
|
|
symbol2str((*expp)->ex_type->tp_fund))
|
1990-04-23 13:33:07 +00:00
|
|
|
, UNSIGNED);
|
1989-09-25 14:28:10 +00:00
|
|
|
expr->ex_flags |= EX_SIZEOF;
|
|
|
|
free_expression(*expp);
|
|
|
|
*expp = expr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|