1989-02-07 11:04:05 +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-02-07 11:04:05 +00:00
|
|
|
/* EXPRESSION TREE HANDLING */
|
|
|
|
|
2017-11-10 03:22:13 +00:00
|
|
|
#include <assert.h>
|
2013-05-12 19:45:55 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "parameters.h"
|
1989-02-07 11:04:05 +00:00
|
|
|
#include <alloc.h>
|
1989-09-19 16:13:23 +00:00
|
|
|
#include <flt_arith.h>
|
2019-02-18 16:42:15 +00:00
|
|
|
#include "expr.h"
|
2013-05-12 19:45:55 +00:00
|
|
|
#include "idf.h"
|
1989-02-07 11:04:05 +00:00
|
|
|
#include "arith.h"
|
|
|
|
#include "def.h"
|
|
|
|
#include "type.h"
|
|
|
|
#include "label.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "LLlex.h"
|
|
|
|
#include "Lpars.h"
|
|
|
|
#include "decspecs.h"
|
|
|
|
#include "declar.h"
|
|
|
|
#include "sizes.h"
|
|
|
|
#include "level.h"
|
2019-02-18 16:42:15 +00:00
|
|
|
#include "cstoper.h"
|
|
|
|
#include "error.h"
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
|
|
extern char *symbol2str();
|
|
|
|
extern char options[];
|
1989-09-19 16:13:23 +00:00
|
|
|
extern int InSizeof;
|
1989-02-07 11:04:05 +00:00
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
int rank_of(int oper)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* The rank of the operator oper is returned.
|
2019-02-18 16:42:15 +00:00
|
|
|
*/
|
|
|
|
switch (oper)
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
default:
|
2019-02-18 16:42:15 +00:00
|
|
|
return 0; /* INT2INT etc. */
|
1989-02-07 11:04:05 +00:00
|
|
|
case '[':
|
|
|
|
case '(':
|
|
|
|
case '.':
|
|
|
|
case ARROW:
|
|
|
|
case PARCOMMA:
|
|
|
|
return 1;
|
|
|
|
case '!':
|
|
|
|
case PLUSPLUS:
|
|
|
|
case MINMIN:
|
|
|
|
case CAST:
|
|
|
|
case SIZEOF:
|
1989-11-22 16:41:09 +00:00
|
|
|
case ADDRESSOF:
|
2019-02-18 16:42:15 +00:00
|
|
|
return 2; /* monadic */
|
1989-02-07 11:04:05 +00:00
|
|
|
case '*':
|
|
|
|
case '/':
|
|
|
|
case '%':
|
|
|
|
return 3;
|
|
|
|
case '+':
|
|
|
|
case '-':
|
|
|
|
return 4;
|
|
|
|
case LEFT:
|
|
|
|
case RIGHT:
|
|
|
|
return 5;
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case LESSEQ:
|
|
|
|
case GREATEREQ:
|
|
|
|
return 6;
|
|
|
|
case EQUAL:
|
|
|
|
case NOTEQUAL:
|
|
|
|
return 7;
|
|
|
|
case '&':
|
|
|
|
return 8;
|
|
|
|
case '^':
|
|
|
|
return 9;
|
|
|
|
case '|':
|
|
|
|
return 10;
|
|
|
|
case AND:
|
|
|
|
return 11;
|
|
|
|
case OR:
|
|
|
|
return 12;
|
|
|
|
case '?':
|
|
|
|
case ':':
|
|
|
|
return 13;
|
|
|
|
case '=':
|
|
|
|
case PLUSAB:
|
|
|
|
case MINAB:
|
|
|
|
case TIMESAB:
|
|
|
|
case DIVAB:
|
|
|
|
case MODAB:
|
|
|
|
case RIGHTAB:
|
|
|
|
case LEFTAB:
|
|
|
|
case ANDAB:
|
|
|
|
case XORAB:
|
|
|
|
case ORAB:
|
|
|
|
return 14;
|
|
|
|
case ',':
|
|
|
|
return 15;
|
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
void dot2expr(struct expr **expp)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* The token in dot is converted into an expression, a
|
2019-02-18 16:42:15 +00:00
|
|
|
pointer to which is stored in *expp.
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
register struct expr *ex = new_expr();
|
|
|
|
|
|
|
|
*expp = ex;
|
|
|
|
ex->ex_file = dot.tk_file;
|
|
|
|
ex->ex_line = dot.tk_line;
|
2019-02-18 16:42:15 +00:00
|
|
|
switch (DOT)
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
case IDENTIFIER:
|
|
|
|
idf2expr(ex);
|
|
|
|
break;
|
|
|
|
case INTEGER:
|
|
|
|
int2expr(ex);
|
|
|
|
break;
|
|
|
|
case FLOATING:
|
|
|
|
float2expr(ex);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
crash("bad conversion to expression");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
void idf2expr(register struct expr *expr)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* Dot contains an identifier which is turned into an
|
2019-02-18 16:42:15 +00:00
|
|
|
expression.
|
|
|
|
Note that this constitutes an applied occurrence of
|
|
|
|
the identifier.
|
|
|
|
*/
|
|
|
|
register struct idf *idf = dot.tk_idf; /* != 0*/
|
1989-02-07 11:04:05 +00:00
|
|
|
register struct def *def = idf->id_def;
|
2019-02-18 16:42:15 +00:00
|
|
|
|
|
|
|
if (def == 0)
|
|
|
|
{
|
|
|
|
if (AHEAD == '(')
|
|
|
|
{
|
1990-11-02 09:23:27 +00:00
|
|
|
/* function call, declare name implicitly (3.3.2.2) */
|
1989-10-27 13:33:10 +00:00
|
|
|
if (!options['o'])
|
2019-02-18 16:42:15 +00:00
|
|
|
warning("implicit declaration of function %s", idf->id_text);
|
1990-11-02 09:23:27 +00:00
|
|
|
add_def(idf, EXTERN, funint_type, level);
|
2019-02-18 16:42:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
if (!is_anon_idf(idf))
|
|
|
|
error("%s undefined", idf->id_text);
|
|
|
|
/* declare idf anyway */
|
|
|
|
add_def(idf, 0, error_type, level);
|
|
|
|
}
|
|
|
|
def = idf->id_def;
|
|
|
|
}
|
|
|
|
/* now def != 0 */
|
|
|
|
#ifndef LINT
|
2019-02-18 16:42:15 +00:00
|
|
|
if (!InSizeof)
|
|
|
|
{
|
|
|
|
if (!def->df_used)
|
|
|
|
{
|
1989-11-08 16:52:34 +00:00
|
|
|
#ifndef PREPEND_SCOPES
|
1991-06-19 16:12:56 +00:00
|
|
|
code_scope(idf->id_text, def);
|
1989-11-08 16:52:34 +00:00
|
|
|
#endif /* PREPEND_SCOPES */
|
1991-06-19 16:12:56 +00:00
|
|
|
def->df_used = 1;
|
1989-11-08 16:52:34 +00:00
|
|
|
}
|
1991-06-19 16:12:56 +00:00
|
|
|
}
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* LINT */
|
1991-06-19 16:12:56 +00:00
|
|
|
expr->ex_type = def->df_type;
|
2019-02-18 16:42:15 +00:00
|
|
|
if (expr->ex_type == error_type)
|
|
|
|
{
|
1991-06-19 16:12:56 +00:00
|
|
|
expr->ex_flags |= EX_ERROR;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
expr->ex_lvalue =
|
2019-02-18 16:42:15 +00:00
|
|
|
(def->df_type->tp_fund == FUNCTION || def->df_type->tp_fund == ARRAY
|
|
|
|
|| def->df_sc == ENUM) ? 0 : 1;
|
1989-02-07 11:04:05 +00:00
|
|
|
if (def->df_type->tp_typequal & TQ_CONST)
|
|
|
|
expr->ex_flags |= EX_READONLY;
|
|
|
|
if (def->df_type->tp_typequal & TQ_VOLATILE)
|
|
|
|
expr->ex_flags |= EX_VOLATILE;
|
|
|
|
expr->ex_class = Value;
|
2019-02-18 16:42:15 +00:00
|
|
|
if (def->df_sc == ENUM)
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
expr->VL_CLASS = Const;
|
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
|
|
|
expr->VL_VALUE = (writh)def->df_address;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
#ifndef LINT
|
2019-02-18 16:42:15 +00:00
|
|
|
else if (def->df_sc == STATIC && def->df_level >= L_LOCAL)
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
expr->VL_CLASS = Label;
|
|
|
|
expr->VL_LBL = def->df_address;
|
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
|
|
|
expr->VL_VALUE = 0;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* LINT */
|
2019-02-18 16:42:15 +00:00
|
|
|
else
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
expr->VL_CLASS = Name;
|
|
|
|
expr->VL_IDF = idf;
|
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
|
|
|
expr->VL_VALUE = 0;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
void string2expr(register struct expr **expp, char *str, int len)
|
|
|
|
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* The string in the argument is converted into an expression,
|
2019-02-18 16:42:15 +00:00
|
|
|
a pointer to which is stored in *expp.
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
register struct expr *ex = new_expr();
|
|
|
|
|
|
|
|
*expp = ex;
|
|
|
|
ex->ex_file = dot.tk_file;
|
|
|
|
ex->ex_line = dot.tk_line;
|
|
|
|
ex->ex_type = string_type;
|
1989-09-19 16:13:23 +00:00
|
|
|
/* ex->ex_type = qualifier_type(ex->ex_type, TQ_CONST); */
|
1989-02-07 11:04:05 +00:00
|
|
|
ex->ex_flags |= EX_READONLY;
|
1989-10-20 16:16:06 +00:00
|
|
|
/* ex->ex_lvalue = 0; */
|
1989-02-07 11:04:05 +00:00
|
|
|
ex->ex_class = String;
|
|
|
|
ex->SG_VALUE = str;
|
|
|
|
ex->SG_LEN = len;
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
void int2expr(struct expr *expr)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* Dot contains an integer constant which is turned
|
2019-02-18 16:42:15 +00:00
|
|
|
into an expression.
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
fill_int_expr(expr, dot.tk_ival, dot.tk_fund);
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
void float2expr(register struct expr *expr)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* Dot contains a floating point constant which is turned
|
2019-02-18 16:42:15 +00:00
|
|
|
into an expression.
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
register int fund;
|
|
|
|
|
|
|
|
fund = dot.tk_fund;
|
2019-02-18 16:42:15 +00:00
|
|
|
switch (fund)
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
case FLOAT:
|
|
|
|
expr->ex_type = float_type;
|
|
|
|
break;
|
|
|
|
case DOUBLE:
|
|
|
|
expr->ex_type = double_type;
|
|
|
|
break;
|
|
|
|
case LNGDBL:
|
|
|
|
expr->ex_type = lngdbl_type;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
crash("(float2expr) bad fund %s\n", symbol2str(fund));
|
|
|
|
}
|
|
|
|
expr->ex_class = Float;
|
1991-01-15 18:21:16 +00:00
|
|
|
flt_str2flt(dot.tk_fval, &(expr->FL_ARITH));
|
|
|
|
free(dot.tk_fval);
|
2017-11-10 03:22:13 +00:00
|
|
|
assert(flt_status != FLT_NOFLT);
|
1989-09-19 16:13:23 +00:00
|
|
|
if (flt_status == FLT_OVFL)
|
2019-02-18 16:42:15 +00:00
|
|
|
expr_warning(expr, "internal floating point overflow");
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
struct expr*intexpr(
|
|
|
|
arith ivalue, int fund)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* The value ivalue is turned into an integer expression of
|
2019-02-18 16:42:15 +00:00
|
|
|
the size indicated by fund.
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
register struct expr *expr = new_expr();
|
1989-09-19 16:13:23 +00:00
|
|
|
|
1989-02-07 11:04:05 +00:00
|
|
|
expr->ex_file = dot.tk_file;
|
|
|
|
expr->ex_line = dot.tk_line;
|
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
|
|
|
fill_int_expr(expr, (writh)ivalue, fund);
|
1989-02-07 11:04:05 +00:00
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
void fill_int_expr(register struct expr *ex,
|
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 ivalue, int fund)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* Details derived from ivalue and fund are put into the
|
2019-02-18 16:42:15 +00:00
|
|
|
constant integer expression ex.
|
|
|
|
*/
|
|
|
|
switch (fund)
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
case INT:
|
|
|
|
ex->ex_type = int_type;
|
|
|
|
break;
|
1990-11-12 15:28:44 +00:00
|
|
|
case UNSIGNED:
|
|
|
|
ex->ex_type = uint_type;
|
|
|
|
break;
|
1989-02-07 11:04:05 +00:00
|
|
|
case LONG:
|
2019-02-18 16:42:15 +00:00
|
|
|
ex->ex_type = long_type;
|
1989-02-07 11:04:05 +00:00
|
|
|
break;
|
|
|
|
case ULONG:
|
|
|
|
ex->ex_type = ulong_type;
|
|
|
|
break;
|
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
|
|
|
case LNGLNG:
|
|
|
|
ex->ex_type = lnglng_type;
|
|
|
|
break;
|
|
|
|
case ULNGLNG:
|
|
|
|
ex->ex_type = ulnglng_type;
|
|
|
|
break;
|
|
|
|
case ERRONEOUS: /* 123LL when no_long_long() */
|
|
|
|
ex->ex_type = error_type;
|
|
|
|
ex->ex_flags |= EX_ERROR;
|
|
|
|
break;
|
1989-02-07 11:04:05 +00:00
|
|
|
default:
|
1990-11-12 15:28:44 +00:00
|
|
|
crash("(fill_int_expr) bad fund %s\n", symbol2str(fund));
|
1989-02-07 11:04:05 +00:00
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
ex->ex_class = Value;
|
|
|
|
ex->VL_CLASS = Const;
|
|
|
|
ex->VL_VALUE = ivalue;
|
|
|
|
cut_size(ex);
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
struct expr *new_oper(struct type *tp, register struct expr *e1, int oper,
|
|
|
|
register struct expr *e2)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* A new expression is constructed which consists of the
|
2019-02-18 16:42:15 +00:00
|
|
|
operator oper which has e1 and e2 as operands; for a
|
|
|
|
monadic operator e1 == NILEXPR.
|
|
|
|
During the construction of the right recursive initialisation
|
|
|
|
tree it is possible for e2 to be NILEXPR.
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
register struct expr *expr = new_expr();
|
|
|
|
register struct oper *op;
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
if (e2)
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
register struct expr *e = e2;
|
2019-02-18 16:42:15 +00:00
|
|
|
|
1989-02-07 11:04:05 +00:00
|
|
|
while (e->ex_class == Oper && e->OP_LEFT)
|
|
|
|
e = e->OP_LEFT;
|
|
|
|
expr->ex_file = e->ex_file;
|
|
|
|
expr->ex_line = e->ex_line;
|
|
|
|
}
|
2019-02-18 16:42:15 +00:00
|
|
|
else if (e1)
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
register struct expr *e = e1;
|
2019-02-18 16:42:15 +00:00
|
|
|
|
1989-02-07 11:04:05 +00:00
|
|
|
while (e->ex_class == Oper && e->OP_RIGHT)
|
|
|
|
e = e->OP_RIGHT;
|
|
|
|
expr->ex_file = e->ex_file;
|
|
|
|
expr->ex_line = e->ex_line;
|
|
|
|
}
|
2019-02-18 16:42:15 +00:00
|
|
|
else
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
expr->ex_file = dot.tk_file;
|
|
|
|
expr->ex_line = dot.tk_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr->ex_type = tp;
|
|
|
|
expr->ex_class = Oper;
|
|
|
|
/* combine depths and flags of both expressions */
|
2019-02-18 16:42:15 +00:00
|
|
|
if (e2)
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
int e1_depth = e1 ? e1->ex_depth : 0;
|
|
|
|
int e1_flags = e1 ? e1->ex_flags : 0;
|
2019-02-18 16:42:15 +00:00
|
|
|
|
|
|
|
expr->ex_depth = (e1_depth > e2->ex_depth ? e1_depth : e2->ex_depth)
|
|
|
|
+ 1;
|
1989-09-19 16:13:23 +00:00
|
|
|
expr->ex_flags = (e1_flags | e2->ex_flags)
|
2019-02-18 16:42:15 +00:00
|
|
|
& ~(EX_PARENS | EX_READONLY | EX_VOLATILE);
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
1990-09-06 11:32:51 +00:00
|
|
|
/*
|
|
|
|
* A function call should be evaluated first when possible. Just say
|
|
|
|
* that the expression tree is very deep.
|
|
|
|
*/
|
2019-02-18 16:42:15 +00:00
|
|
|
if (oper == '(')
|
|
|
|
{
|
1990-09-06 11:32:51 +00:00
|
|
|
expr->ex_depth = 50;
|
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
op = &expr->ex_object.ex_oper;
|
|
|
|
op->op_type = tp;
|
|
|
|
op->op_oper = oper;
|
|
|
|
op->op_left = e1;
|
|
|
|
op->op_right = e2;
|
|
|
|
#ifdef LINT
|
|
|
|
lint_new_oper(expr);
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* LINT */
|
1989-02-07 11:04:05 +00:00
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
void chk_cst_expr(struct expr **expp)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* The expression expr is checked for constancy.
|
2019-02-18 16:42:15 +00:00
|
|
|
|
|
|
|
There are 6 places where constant expressions occur in C:
|
|
|
|
1. after #if
|
|
|
|
2. in a global initialization
|
|
|
|
3. as size in an array declaration
|
|
|
|
4. as value in an enum declaration
|
|
|
|
5. as width in a bit field
|
|
|
|
6. as case value in a switch
|
|
|
|
|
|
|
|
The constant expression in a global initialization is
|
|
|
|
handled separately (by IVAL()).
|
|
|
|
|
|
|
|
There are various disparate restrictions on each of
|
|
|
|
the others in the various C compilers. I have tried some
|
|
|
|
hypotheses to unify them, but all have failed.
|
|
|
|
|
|
|
|
Special problems (of which there is only one, sizeof in
|
|
|
|
Preprocessor #if) have to be dealt with locally
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
register struct expr *expr = *expp;
|
2019-02-18 16:42:15 +00:00
|
|
|
|
1989-02-07 11:04:05 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
print_expr("constant_expression", expr);
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* DEBUG */
|
2019-02-18 16:42:15 +00:00
|
|
|
switch (expr->ex_type->tp_fund)
|
|
|
|
{
|
1989-11-09 14:06:35 +00:00
|
|
|
case CHAR:
|
|
|
|
case SHORT:
|
|
|
|
case INT:
|
|
|
|
case ENUM:
|
|
|
|
case LONG:
|
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
|
|
|
case LNGLNG:
|
2019-02-18 16:42:15 +00:00
|
|
|
if (is_ld_cst(expr))
|
|
|
|
{
|
1989-11-09 14:06:35 +00:00
|
|
|
return;
|
|
|
|
}
|
1989-09-19 16:13:23 +00:00
|
|
|
expr_error(expr, "expression is not constant");
|
1989-11-09 14:06:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
expr_error(expr, "non-numerical constant expression");
|
|
|
|
break;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
1989-11-09 14:06:35 +00:00
|
|
|
erroneous2int(expp);
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
void init_expression(register struct expr ***eppp, struct expr *expr)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* The expression expr is added to the tree designated
|
2019-02-18 16:42:15 +00:00
|
|
|
indirectly by **eppp.
|
|
|
|
The natural form of a tree representing an
|
|
|
|
initial_value_list is right-recursive, ie. with the
|
|
|
|
left-most comma as main operator. The iterative grammar in
|
|
|
|
expression.g, however, tends to produce a left-recursive
|
|
|
|
tree, ie. one with the right-most comma as its main
|
|
|
|
operator.
|
|
|
|
To produce a right-recursive tree from the iterative
|
|
|
|
grammar, we keep track of the address of the pointer where
|
|
|
|
the next expression must be hooked in.
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
**eppp = new_oper(void_type, expr, INITCOMMA, NILEXPR);
|
|
|
|
*eppp = &(**eppp)->OP_RIGHT;
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
int is_ld_cst(register struct expr *expr)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* An expression is a `load-time constant' if it is of the form
|
2019-02-18 16:42:15 +00:00
|
|
|
<idf> +/- <integral> or <integral>.
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
#ifdef LINT
|
|
|
|
if (expr->ex_class == String)
|
2019-02-18 16:42:15 +00:00
|
|
|
return 1;
|
1991-12-17 13:12:22 +00:00
|
|
|
#endif /* LINT */
|
1989-02-07 11:04:05 +00:00
|
|
|
return expr->ex_lvalue == 0 && expr->ex_class == Value;
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
int is_cp_cst(struct expr *expr)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* An expression is a `compile-time constant' if it is a
|
2019-02-18 16:42:15 +00:00
|
|
|
load-time constant, and the idf is not there.
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
return is_ld_cst(expr) && expr->VL_CLASS == Const;
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
int is_fp_cst(struct expr *expr)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* An expression is a `floating-point constant' if it consists
|
2019-02-18 16:42:15 +00:00
|
|
|
of the float only.
|
|
|
|
*/
|
1989-02-07 11:04:05 +00:00
|
|
|
return expr->ex_class == Float;
|
|
|
|
}
|
1989-09-19 16:13:23 +00:00
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
int is_zero_cst(register struct expr *expr)
|
1989-09-19 16:13:23 +00:00
|
|
|
{
|
|
|
|
flt_arith var;
|
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
switch (expr->ex_class)
|
|
|
|
{
|
1989-09-19 16:13:23 +00:00
|
|
|
case Value:
|
|
|
|
return expr->VL_VALUE == 0;
|
|
|
|
case Float:
|
1989-11-22 13:58:36 +00:00
|
|
|
flt_arith2flt((arith) 0, &var, 0);
|
1989-09-19 16:13:23 +00:00
|
|
|
return flt_cmp(&var, &(expr->FL_ARITH)) == 0;
|
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
|
2019-02-18 16:42:15 +00:00
|
|
|
void free_expression(register struct expr *expr)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* The expression expr is freed recursively.
|
2019-02-18 16:42:15 +00:00
|
|
|
*/
|
|
|
|
if (expr)
|
|
|
|
{
|
|
|
|
if (expr->ex_class == Oper)
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
free_expression(expr->OP_LEFT);
|
|
|
|
free_expression(expr->OP_RIGHT);
|
|
|
|
}
|
|
|
|
free_expr(expr);
|
|
|
|
}
|
|
|
|
}
|