1987-04-29 10:22:07 +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".
|
|
|
|
*
|
|
|
|
* Author: Ceriel J.H. Jacobs
|
|
|
|
*/
|
|
|
|
|
1986-04-07 22:15:08 +00:00
|
|
|
/* C O N S T A N T E X P R E S S I O N H A N D L I N G */
|
1986-04-07 17:40:38 +00:00
|
|
|
|
1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1987-04-29 10:22:07 +00:00
|
|
|
|
2013-05-14 21:24:38 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "parameters.h"
|
1986-05-01 19:06:53 +00:00
|
|
|
#include "debug.h"
|
1986-04-07 17:40:38 +00:00
|
|
|
|
|
|
|
#include <em_arith.h>
|
|
|
|
#include <em_label.h>
|
|
|
|
#include <assert.h>
|
1989-12-19 09:40:25 +00:00
|
|
|
#include <alloc.h>
|
1986-04-17 09:28:09 +00:00
|
|
|
|
1986-04-07 17:40:38 +00:00
|
|
|
#include "idf.h"
|
|
|
|
#include "type.h"
|
|
|
|
#include "LLlex.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "Lpars.h"
|
1986-04-11 11:57:19 +00:00
|
|
|
#include "standards.h"
|
1986-11-05 14:33:00 +00:00
|
|
|
#include "warning.h"
|
1986-04-07 17:40:38 +00:00
|
|
|
|
1989-12-19 09:40:25 +00:00
|
|
|
extern char *symbol2str();
|
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
#define arith_sign ((arith) (1L << (sizeof(arith) * 8 - 1)))
|
|
|
|
|
1991-03-13 17:26:07 +00:00
|
|
|
#ifndef NOCROSS
|
1991-03-14 13:16:32 +00:00
|
|
|
arith full_mask[MAXSIZE+1];/* full_mask[1] == 0xFF, full_mask[2] == 0xFFFF, .. */
|
|
|
|
arith max_int[MAXSIZE+1]; /* max_int[1] == 0x7F, max_int[2] == 0x7FFF, .. */
|
|
|
|
arith min_int[MAXSIZE+1]; /* min_int[1] == 0xFFFFFF80, min_int[2] = 0xFFFF8000,
|
|
|
|
...
|
|
|
|
*/
|
1987-07-21 13:54:33 +00:00
|
|
|
unsigned int wrd_bits; /* number of bits in a word */
|
1991-03-13 17:26:07 +00:00
|
|
|
#else
|
1991-03-14 09:31:32 +00:00
|
|
|
arith full_mask[] = { 0L, 0xFFL, 0xFFFFL, 0L, 0xFFFFFFFFL };
|
|
|
|
arith max_int[] = { 0L, 0x7FL, 0x7FFFL, 0L, 0x7FFFFFFFL };
|
|
|
|
arith min_int[] = { 0L, -128L, -32768L, 0L, -2147483647L-1 };
|
1991-03-12 16:52:00 +00:00
|
|
|
#endif
|
1986-04-07 17:40:38 +00:00
|
|
|
|
1987-06-23 17:12:25 +00:00
|
|
|
extern char options[];
|
|
|
|
|
2016-11-10 21:04:18 +00:00
|
|
|
void CutSize();
|
|
|
|
|
1987-11-26 14:15:24 +00:00
|
|
|
overflow(expp)
|
|
|
|
t_node *expp;
|
|
|
|
{
|
1988-03-21 17:06:20 +00:00
|
|
|
if (expp->nd_type != address_type) {
|
|
|
|
node_warning(expp, W_ORDINARY, "overflow in constant expression");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1987-12-02 10:41:38 +00:00
|
|
|
STATIC
|
|
|
|
commonbin(expp)
|
1991-03-13 13:49:56 +00:00
|
|
|
t_node **expp;
|
1987-12-02 10:41:38 +00:00
|
|
|
{
|
1991-03-13 13:49:56 +00:00
|
|
|
register t_node *exp = *expp;
|
|
|
|
t_type *tp = exp->nd_type;
|
|
|
|
register t_node *right = exp->nd_RIGHT;
|
1991-03-12 16:52:00 +00:00
|
|
|
|
1991-03-13 13:49:56 +00:00
|
|
|
exp->nd_RIGHT = 0;
|
|
|
|
FreeNode(exp);
|
1991-03-12 16:52:00 +00:00
|
|
|
*expp = right;
|
|
|
|
right->nd_type = tp;
|
1987-11-26 14:15:24 +00:00
|
|
|
}
|
1986-11-05 14:33:00 +00:00
|
|
|
|
1986-04-08 18:15:46 +00:00
|
|
|
cstunary(expp)
|
1991-03-12 16:52:00 +00:00
|
|
|
t_node **expp;
|
1986-04-07 22:15:08 +00:00
|
|
|
{
|
1986-04-08 18:15:46 +00:00
|
|
|
/* The unary operation in "expp" is performed on the constant
|
|
|
|
expression below it, and the result restored in expp.
|
1986-04-07 22:15:08 +00:00
|
|
|
*/
|
1991-03-12 16:52:00 +00:00
|
|
|
register t_node *exp = *expp;
|
|
|
|
register t_node *right = exp->nd_RIGHT;
|
1989-08-08 09:11:32 +00:00
|
|
|
register arith o1 = right->nd_INT;
|
1986-04-07 22:15:08 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
switch(exp->nd_symb) {
|
1986-10-06 20:36:30 +00:00
|
|
|
/* Should not get here
|
1986-04-07 22:15:08 +00:00
|
|
|
case '+':
|
1986-04-08 18:15:46 +00:00
|
|
|
break;
|
1986-10-06 20:36:30 +00:00
|
|
|
*/
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-07 22:15:08 +00:00
|
|
|
case '-':
|
1991-03-13 13:49:56 +00:00
|
|
|
if (! options['s'] &&
|
|
|
|
o1 == min_int[(int)(right->nd_type->tp_size)]) {
|
1991-03-12 16:52:00 +00:00
|
|
|
overflow(exp);
|
1989-08-08 09:11:32 +00:00
|
|
|
}
|
1988-10-13 15:43:23 +00:00
|
|
|
o1 = -o1;
|
1986-04-07 22:15:08 +00:00
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-07 22:15:08 +00:00
|
|
|
case NOT:
|
1986-04-22 22:36:16 +00:00
|
|
|
case '~':
|
1988-10-13 15:43:23 +00:00
|
|
|
o1 = !o1;
|
1986-04-07 22:15:08 +00:00
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-07 22:15:08 +00:00
|
|
|
default:
|
1986-05-28 18:36:51 +00:00
|
|
|
crash("(cstunary)");
|
1986-04-07 22:15:08 +00:00
|
|
|
}
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1987-12-02 10:41:38 +00:00
|
|
|
commonbin(expp);
|
1991-03-12 16:52:00 +00:00
|
|
|
(*expp)->nd_INT = o1;
|
|
|
|
CutSize(*expp);
|
1986-04-07 22:15:08 +00:00
|
|
|
}
|
1986-04-07 17:40:38 +00:00
|
|
|
|
1987-10-19 11:28:37 +00:00
|
|
|
STATIC
|
1987-11-26 14:15:24 +00:00
|
|
|
divide(pdiv, prem)
|
1987-10-19 11:28:37 +00:00
|
|
|
arith *pdiv, *prem;
|
|
|
|
{
|
1987-11-26 14:15:24 +00:00
|
|
|
/* Unsigned divide *pdiv by *prem, and store result in *pdiv,
|
1987-10-19 11:28:37 +00:00
|
|
|
remainder in *prem
|
|
|
|
*/
|
|
|
|
register arith o1 = *pdiv;
|
|
|
|
register arith o2 = *prem;
|
|
|
|
|
1991-03-13 13:49:56 +00:00
|
|
|
#ifndef UNSIGNED_ARITH
|
1987-11-26 14:15:24 +00:00
|
|
|
/* this is more of a problem than you might
|
|
|
|
think on C compilers which do not have
|
|
|
|
unsigned long.
|
|
|
|
*/
|
1989-08-08 09:11:32 +00:00
|
|
|
if (o2 & arith_sign) {/* o2 > max_arith */
|
1987-11-26 14:15:24 +00:00
|
|
|
if (! (o1 >= 0 || o1 < o2)) {
|
|
|
|
/* this is the unsigned test
|
1989-08-08 09:11:32 +00:00
|
|
|
o1 < o2 for o2 > max_arith
|
1987-11-26 14:15:24 +00:00
|
|
|
*/
|
|
|
|
*prem = o2 - o1;
|
|
|
|
*pdiv = 1;
|
1987-10-19 11:28:37 +00:00
|
|
|
}
|
1987-11-26 14:15:24 +00:00
|
|
|
else {
|
|
|
|
*pdiv = 0;
|
|
|
|
}
|
|
|
|
}
|
1989-08-08 09:11:32 +00:00
|
|
|
else { /* o2 <= max_arith */
|
|
|
|
arith half, bit, hdiv, hrem, rem;
|
1987-11-26 14:15:24 +00:00
|
|
|
|
1989-08-08 09:11:32 +00:00
|
|
|
half = (o1 >> 1) & ~arith_sign;
|
1987-11-26 14:15:24 +00:00
|
|
|
bit = o1 & 01;
|
|
|
|
/* now o1 == 2 * half + bit
|
1989-08-08 09:11:32 +00:00
|
|
|
and half <= max_arith
|
|
|
|
and bit <= max_arith
|
1987-11-26 14:15:24 +00:00
|
|
|
*/
|
|
|
|
hdiv = half / o2;
|
|
|
|
hrem = half % o2;
|
|
|
|
rem = 2 * hrem + bit;
|
|
|
|
*pdiv = 2*hdiv;
|
|
|
|
*prem = rem;
|
|
|
|
if (rem < 0 || rem >= o2) {
|
|
|
|
/* that is the unsigned compare
|
1989-08-08 09:11:32 +00:00
|
|
|
rem >= o2 for o2 <= max_arith
|
1987-10-19 11:28:37 +00:00
|
|
|
*/
|
1987-11-26 14:15:24 +00:00
|
|
|
*pdiv += 1;
|
|
|
|
*prem -= o2;
|
1987-10-19 11:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
1991-03-13 13:49:56 +00:00
|
|
|
#else
|
|
|
|
*pdiv = (UNSIGNED_ARITH) o1 / (UNSIGNED_ARITH) o2;
|
|
|
|
*prem = (UNSIGNED_ARITH) o1 % (UNSIGNED_ARITH) o2;
|
|
|
|
#endif
|
1987-11-26 14:15:24 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 21:04:18 +00:00
|
|
|
void
|
1987-11-26 14:15:24 +00:00
|
|
|
cstibin(expp)
|
1991-03-12 16:52:00 +00:00
|
|
|
t_node **expp;
|
1987-11-26 14:15:24 +00:00
|
|
|
{
|
|
|
|
/* The binary operation in "expp" is performed on the constant
|
|
|
|
expressions below it, and the result restored in expp.
|
|
|
|
This version is for INTEGER expressions.
|
|
|
|
*/
|
1991-03-12 16:52:00 +00:00
|
|
|
register t_node *exp = *expp;
|
|
|
|
register arith o1 = exp->nd_LEFT->nd_INT;
|
|
|
|
register arith o2 = exp->nd_RIGHT->nd_INT;
|
|
|
|
register int sz = exp->nd_type->tp_size;
|
1987-11-26 14:15:24 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
assert(exp->nd_class == Oper);
|
|
|
|
assert(exp->nd_LEFT->nd_class == Value);
|
|
|
|
assert(exp->nd_RIGHT->nd_class == Value);
|
1987-11-26 14:15:24 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
switch (exp->nd_symb) {
|
1987-11-26 14:15:24 +00:00
|
|
|
case '*':
|
1991-03-13 17:26:07 +00:00
|
|
|
if (o1 > 0) {
|
|
|
|
if (o2 > 0) {
|
|
|
|
if (max_int[sz] / o1 < o2) overflow(exp);
|
|
|
|
}
|
|
|
|
else if (min_int[sz] / o1 > o2) overflow(exp);
|
1987-11-26 14:15:24 +00:00
|
|
|
}
|
1991-03-13 17:26:07 +00:00
|
|
|
else if (o1 < 0) {
|
|
|
|
if (o2 < 0) {
|
|
|
|
if (o1 == min_int[sz] || o2 == min_int[sz] ||
|
|
|
|
max_int[sz] / (-o1) < (-o2)) overflow(exp);
|
|
|
|
}
|
|
|
|
else if (o2 > 0) {
|
|
|
|
if (min_int[sz] / o2 > o1) overflow(exp);
|
|
|
|
}
|
1987-12-02 10:41:38 +00:00
|
|
|
}
|
1987-11-26 14:15:24 +00:00
|
|
|
o1 *= o2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIV:
|
|
|
|
case MOD:
|
|
|
|
if (o2 == 0) {
|
1991-03-13 13:49:56 +00:00
|
|
|
node_error(exp, exp->nd_symb == DIV ?
|
|
|
|
"division by 0" :
|
|
|
|
"modulo by 0");
|
1987-11-26 14:15:24 +00:00
|
|
|
return;
|
|
|
|
}
|
1989-08-08 09:11:32 +00:00
|
|
|
if ((o1 < 0) != (o2 < 0)) {
|
1990-07-16 09:05:19 +00:00
|
|
|
if (o1 < 0) o1 = -o1;
|
|
|
|
else o2 = -o2;
|
1991-03-13 13:49:56 +00:00
|
|
|
if (exp->nd_symb == DIV) o1 = -((o1+o2-1)/o2);
|
|
|
|
else o1 = ((o1+o2-1)/o2) * o2 - o1;
|
1989-08-08 09:11:32 +00:00
|
|
|
}
|
|
|
|
else {
|
1991-03-13 13:49:56 +00:00
|
|
|
if (exp->nd_symb == DIV) o1 /= o2;
|
|
|
|
else o1 %= o2;
|
1989-08-08 09:11:32 +00:00
|
|
|
}
|
1987-11-26 14:15:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '+':
|
1991-03-13 17:26:07 +00:00
|
|
|
if ( (o1 > 0 && o2 > 0 && max_int[sz] - o1 < o2)
|
|
|
|
|| (o1 < 0 && o2 < 0 && min_int[sz] - o1 > o2)
|
|
|
|
) overflow(exp);
|
1987-11-26 14:15:24 +00:00
|
|
|
o1 += o2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
1991-03-13 17:26:07 +00:00
|
|
|
if ( (o1 >= 0 && o2 < 0 && max_int[sz] + o2 < o1)
|
|
|
|
|| (o1 < 0 && o2 >= 0 && min_int[sz] + o2 > o1)
|
|
|
|
) overflow(exp);
|
1987-11-26 14:15:24 +00:00
|
|
|
o1 -= o2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '<':
|
1987-12-02 10:41:38 +00:00
|
|
|
o1 = (o1 < o2);
|
|
|
|
break;
|
1987-11-26 14:15:24 +00:00
|
|
|
|
|
|
|
case '>':
|
|
|
|
o1 = (o1 > o2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LESSEQUAL:
|
1988-08-05 11:22:39 +00:00
|
|
|
o1 = (o1 <= o2);
|
1987-12-02 10:41:38 +00:00
|
|
|
break;
|
1987-11-26 14:15:24 +00:00
|
|
|
|
|
|
|
case GREATEREQUAL:
|
1988-08-05 11:22:39 +00:00
|
|
|
o1 = (o1 >= o2);
|
1987-11-26 14:15:24 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '=':
|
|
|
|
o1 = (o1 == o2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '#':
|
|
|
|
o1 = (o1 != o2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
crash("(cstibin)");
|
1987-10-19 11:28:37 +00:00
|
|
|
}
|
1987-11-26 14:15:24 +00:00
|
|
|
|
1987-12-02 10:41:38 +00:00
|
|
|
commonbin(expp);
|
1991-03-12 16:52:00 +00:00
|
|
|
(*expp)->nd_INT = o1;
|
|
|
|
CutSize(*expp);
|
1987-10-19 11:28:37 +00:00
|
|
|
}
|
|
|
|
|
1989-12-19 09:40:25 +00:00
|
|
|
cstfbin(expp)
|
1991-03-12 16:52:00 +00:00
|
|
|
t_node **expp;
|
1989-12-19 09:40:25 +00:00
|
|
|
{
|
|
|
|
/* The binary operation in "expp" is performed on the constant
|
|
|
|
expressions below it, and the result restored in expp.
|
|
|
|
This version is for REAL expressions.
|
|
|
|
*/
|
1991-03-12 16:52:00 +00:00
|
|
|
register t_node *exp = *expp;
|
|
|
|
register struct real *p = exp->nd_LEFT->nd_REAL;
|
1989-12-19 09:40:25 +00:00
|
|
|
register flt_arith *o1 = &p->r_val;
|
1991-03-12 16:52:00 +00:00
|
|
|
register flt_arith *o2 = &exp->nd_RIGHT->nd_RVAL;
|
1989-12-19 09:40:25 +00:00
|
|
|
int compar = 0;
|
|
|
|
int cmpval = 0;
|
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
assert(exp->nd_class == Oper);
|
|
|
|
assert(exp->nd_LEFT->nd_class == Value);
|
|
|
|
assert(exp->nd_RIGHT->nd_class == Value);
|
1989-12-19 09:40:25 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
switch (exp->nd_symb) {
|
1989-12-19 09:40:25 +00:00
|
|
|
case '*':
|
|
|
|
flt_mul(o1, o2, o1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '/':
|
|
|
|
flt_div(o1, o2, o1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
flt_add(o1, o2, o1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
flt_sub(o1, o2, o1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case LESSEQUAL:
|
|
|
|
case GREATEREQUAL:
|
|
|
|
case '=':
|
|
|
|
case '#':
|
|
|
|
compar++;
|
|
|
|
cmpval = flt_cmp(o1, o2);
|
1991-03-12 16:52:00 +00:00
|
|
|
switch(exp->nd_symb) {
|
1989-12-19 09:40:25 +00:00
|
|
|
case '<': cmpval = (cmpval < 0); break;
|
|
|
|
case '>': cmpval = (cmpval > 0); break;
|
|
|
|
case LESSEQUAL: cmpval = (cmpval <= 0); break;
|
|
|
|
case GREATEREQUAL: cmpval = (cmpval >= 0); break;
|
|
|
|
case '=': cmpval = (cmpval == 0); break;
|
|
|
|
case '#': cmpval = (cmpval != 0); break;
|
|
|
|
}
|
1991-03-12 16:52:00 +00:00
|
|
|
if (exp->nd_RIGHT->nd_RSTR) free(exp->nd_RIGHT->nd_RSTR);
|
|
|
|
free_real(exp->nd_RIGHT->nd_REAL);
|
1989-12-19 09:40:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
crash("(cstfbin)");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(flt_status) {
|
|
|
|
case FLT_OVFL:
|
1991-03-12 16:52:00 +00:00
|
|
|
node_warning(exp, "floating point overflow on %s",
|
|
|
|
symbol2str(exp->nd_symb));
|
1989-12-19 09:40:25 +00:00
|
|
|
break;
|
|
|
|
case FLT_DIV0:
|
1991-03-12 16:52:00 +00:00
|
|
|
node_error(exp, "division by 0.0");
|
1989-12-19 09:40:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->r_real) {
|
|
|
|
free(p->r_real);
|
|
|
|
p->r_real = 0;
|
|
|
|
}
|
|
|
|
if (compar) {
|
|
|
|
free_real(p);
|
|
|
|
}
|
|
|
|
commonbin(expp);
|
1991-03-12 16:52:00 +00:00
|
|
|
exp = *expp;
|
1989-12-19 09:40:25 +00:00
|
|
|
if (compar) {
|
1991-03-12 16:52:00 +00:00
|
|
|
exp->nd_symb = INTEGER;
|
|
|
|
exp->nd_INT = cmpval;
|
1989-12-19 09:40:25 +00:00
|
|
|
}
|
|
|
|
else {
|
1991-03-12 16:52:00 +00:00
|
|
|
exp->nd_REAL = p;
|
1989-12-19 09:40:25 +00:00
|
|
|
}
|
1991-03-12 16:52:00 +00:00
|
|
|
CutSize(exp);
|
1989-12-19 09:40:25 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 21:04:18 +00:00
|
|
|
void
|
1987-11-26 14:15:24 +00:00
|
|
|
cstubin(expp)
|
1991-03-12 16:52:00 +00:00
|
|
|
t_node **expp;
|
1986-04-07 17:40:38 +00:00
|
|
|
{
|
1986-04-08 18:15:46 +00:00
|
|
|
/* The binary operation in "expp" is performed on the constant
|
|
|
|
expressions below it, and the result restored in
|
1986-04-07 22:15:08 +00:00
|
|
|
expp.
|
1986-04-07 17:40:38 +00:00
|
|
|
*/
|
1991-03-12 16:52:00 +00:00
|
|
|
register t_node *exp = *expp;
|
|
|
|
arith o1 = exp->nd_LEFT->nd_INT;
|
|
|
|
arith o2 = exp->nd_RIGHT->nd_INT;
|
|
|
|
register int sz = exp->nd_type->tp_size;
|
1987-11-26 14:15:24 +00:00
|
|
|
arith tmp1, tmp2;
|
1986-04-07 17:40:38 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
assert(exp->nd_class == Oper);
|
|
|
|
assert(exp->nd_LEFT->nd_class == Value);
|
|
|
|
assert(exp->nd_RIGHT->nd_class == Value);
|
1986-05-01 19:06:53 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
switch (exp->nd_symb) {
|
1986-04-07 17:40:38 +00:00
|
|
|
case '*':
|
1987-11-26 14:15:24 +00:00
|
|
|
if (o1 == 0 || o2 == 0) {
|
|
|
|
o1 = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmp1 = full_mask[sz];
|
|
|
|
tmp2 = o2;
|
|
|
|
divide(&tmp1, &tmp2);
|
1991-03-12 16:52:00 +00:00
|
|
|
if (! chk_bounds(o1, tmp1, T_CARDINAL)) overflow(exp);
|
1986-04-07 17:40:38 +00:00
|
|
|
o1 *= o2;
|
|
|
|
break;
|
1986-05-01 19:06:53 +00:00
|
|
|
|
1986-04-07 22:15:08 +00:00
|
|
|
case DIV:
|
|
|
|
case MOD:
|
1986-04-07 17:40:38 +00:00
|
|
|
if (o2 == 0) {
|
1991-03-13 17:26:07 +00:00
|
|
|
node_error(exp, exp->nd_symb == DIV ?
|
|
|
|
"division by 0" :
|
|
|
|
"modulo by 0");
|
1986-04-07 22:15:08 +00:00
|
|
|
return;
|
1986-04-07 17:40:38 +00:00
|
|
|
}
|
1987-11-26 14:15:24 +00:00
|
|
|
divide(&o1, &o2);
|
1991-03-13 17:26:07 +00:00
|
|
|
if (exp->nd_symb == MOD) o1 = o2;
|
1986-04-07 17:40:38 +00:00
|
|
|
break;
|
1986-05-01 19:06:53 +00:00
|
|
|
|
1986-04-07 17:40:38 +00:00
|
|
|
case '+':
|
1987-11-26 14:15:24 +00:00
|
|
|
if (! chk_bounds(o2, full_mask[sz] - o1, T_CARDINAL)) {
|
1991-03-12 16:52:00 +00:00
|
|
|
overflow(exp);
|
1987-11-26 14:15:24 +00:00
|
|
|
}
|
1986-04-07 17:40:38 +00:00
|
|
|
o1 += o2;
|
|
|
|
break;
|
1986-05-01 19:06:53 +00:00
|
|
|
|
1986-04-07 17:40:38 +00:00
|
|
|
case '-':
|
1991-03-13 17:26:07 +00:00
|
|
|
if ( exp->nd_type != address_type
|
|
|
|
&& !chk_bounds(o2, o1, T_CARDINAL)
|
|
|
|
&& ( exp->nd_type->tp_fund != T_INTORCARD
|
|
|
|
|| ( exp->nd_type = int_type
|
|
|
|
, !chk_bounds(min_int[sz], o1 - o2, T_CARDINAL) ) )
|
|
|
|
) {
|
|
|
|
node_warning(exp, W_ORDINARY,
|
|
|
|
"underflow in constant expression");
|
1986-06-06 02:22:09 +00:00
|
|
|
}
|
1987-11-26 14:15:24 +00:00
|
|
|
o1 -= o2;
|
1986-04-07 17:40:38 +00:00
|
|
|
break;
|
1986-05-01 19:06:53 +00:00
|
|
|
|
1986-04-07 17:40:38 +00:00
|
|
|
case '<':
|
1987-12-02 10:41:38 +00:00
|
|
|
o1 = ! chk_bounds(o2, o1, T_CARDINAL);
|
|
|
|
break;
|
1986-05-01 19:06:53 +00:00
|
|
|
|
1986-04-07 17:40:38 +00:00
|
|
|
case '>':
|
1987-11-26 14:15:24 +00:00
|
|
|
o1 = ! chk_bounds(o1, o2, T_CARDINAL);
|
1986-04-07 17:40:38 +00:00
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-07 22:15:08 +00:00
|
|
|
case LESSEQUAL:
|
1987-12-02 10:41:38 +00:00
|
|
|
o1 = chk_bounds(o1, o2, T_CARDINAL);
|
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-07 22:15:08 +00:00
|
|
|
case GREATEREQUAL:
|
1987-11-26 14:15:24 +00:00
|
|
|
o1 = chk_bounds(o2, o1, T_CARDINAL);
|
1986-04-07 17:40:38 +00:00
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-07 22:15:08 +00:00
|
|
|
case '=':
|
1986-05-28 18:36:51 +00:00
|
|
|
o1 = (o1 == o2);
|
1986-04-07 17:40:38 +00:00
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-07 22:15:08 +00:00
|
|
|
case '#':
|
1986-05-28 18:36:51 +00:00
|
|
|
o1 = (o1 != o2);
|
1986-04-07 17:40:38 +00:00
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-07 22:15:08 +00:00
|
|
|
case AND:
|
1986-04-22 22:36:16 +00:00
|
|
|
case '&':
|
1986-05-28 18:36:51 +00:00
|
|
|
o1 = (o1 && o2);
|
1986-04-07 17:40:38 +00:00
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-07 22:15:08 +00:00
|
|
|
case OR:
|
1986-05-28 18:36:51 +00:00
|
|
|
o1 = (o1 || o2);
|
1986-04-07 17:40:38 +00:00
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-07 22:15:08 +00:00
|
|
|
default:
|
1987-11-26 14:15:24 +00:00
|
|
|
crash("(cstubin)");
|
1986-04-07 17:40:38 +00:00
|
|
|
}
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1987-12-02 10:41:38 +00:00
|
|
|
commonbin(expp);
|
1991-03-12 16:52:00 +00:00
|
|
|
exp = *expp;
|
|
|
|
exp->nd_INT = o1;
|
|
|
|
if (exp->nd_type == bool_type) exp->nd_symb = INTEGER;
|
|
|
|
CutSize(exp);
|
1986-04-08 18:15:46 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 21:04:18 +00:00
|
|
|
void
|
1986-04-08 18:15:46 +00:00
|
|
|
cstset(expp)
|
1991-03-12 16:52:00 +00:00
|
|
|
t_node **expp;
|
1986-04-08 18:15:46 +00:00
|
|
|
{
|
1987-09-23 16:39:43 +00:00
|
|
|
extern arith *MkSet();
|
1991-03-12 16:52:00 +00:00
|
|
|
register t_node *exp = *expp;
|
|
|
|
register arith *set1, *set2, *set3;
|
1987-07-21 13:54:33 +00:00
|
|
|
register unsigned int setsize;
|
|
|
|
register int j;
|
1986-04-09 18:14:49 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
assert(exp->nd_RIGHT->nd_class == Set);
|
|
|
|
assert(exp->nd_symb == IN || exp->nd_LEFT->nd_class == Set);
|
1987-07-30 13:37:39 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
set2 = exp->nd_RIGHT->nd_set;
|
|
|
|
setsize = (unsigned) (exp->nd_RIGHT->nd_type->tp_size) / (unsigned) word_size;
|
1986-04-09 18:14:49 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
if (exp->nd_symb == IN) {
|
1989-03-20 13:32:06 +00:00
|
|
|
/* The setsize must fit in an unsigned, as it is
|
|
|
|
allocated with Malloc, so we can do the arithmetic
|
|
|
|
in an unsigned too.
|
|
|
|
*/
|
1987-07-21 13:54:33 +00:00
|
|
|
unsigned i;
|
1986-04-09 18:14:49 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
assert(exp->nd_LEFT->nd_class == Value);
|
1986-05-28 18:36:51 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
exp->nd_LEFT->nd_INT -= exp->nd_RIGHT->nd_type->set_low;
|
1991-03-13 13:49:56 +00:00
|
|
|
exp = exp->nd_LEFT;
|
|
|
|
i = exp->nd_INT;
|
1991-03-12 16:52:00 +00:00
|
|
|
/* Careful here; use exp->nd_LEFT->nd_INT to see if
|
1989-03-20 13:32:06 +00:00
|
|
|
it falls in the range of the set. Do not use i
|
|
|
|
for this, as i may be truncated.
|
|
|
|
*/
|
1991-03-13 13:49:56 +00:00
|
|
|
i = (exp->nd_INT >= 0 &&
|
|
|
|
exp->nd_INT < setsize * wrd_bits &&
|
1986-04-09 18:14:49 +00:00
|
|
|
(set2[i / wrd_bits] & (1 << (i % wrd_bits))));
|
1987-09-23 16:39:43 +00:00
|
|
|
FreeSet(set2);
|
1991-03-12 16:52:00 +00:00
|
|
|
exp = getnode(Value);
|
|
|
|
exp->nd_symb = INTEGER;
|
|
|
|
exp->nd_lineno = (*expp)->nd_lineno;
|
|
|
|
exp->nd_INT = i;
|
|
|
|
exp->nd_type = bool_type;
|
|
|
|
FreeNode(*expp);
|
|
|
|
*expp = exp;
|
1987-09-23 16:39:43 +00:00
|
|
|
return;
|
1986-04-09 18:14:49 +00:00
|
|
|
}
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
set1 = exp->nd_LEFT->nd_set;
|
1991-03-18 16:30:49 +00:00
|
|
|
*expp = getnode(Set);
|
1991-03-12 16:52:00 +00:00
|
|
|
(*expp)->nd_type = exp->nd_type;
|
1991-03-18 16:30:49 +00:00
|
|
|
(*expp)->nd_lineno = exp->nd_lineno;
|
1991-03-12 16:52:00 +00:00
|
|
|
switch(exp->nd_symb) {
|
1987-09-23 16:39:43 +00:00
|
|
|
case '+': /* Set union */
|
|
|
|
case '-': /* Set difference */
|
|
|
|
case '*': /* Set intersection */
|
|
|
|
case '/': /* Symmetric set difference */
|
1991-03-12 16:52:00 +00:00
|
|
|
(*expp)->nd_set = set3 = MkSet(exp->nd_type->set_sz);
|
1987-09-23 16:39:43 +00:00
|
|
|
for (j = 0; j < setsize; j++) {
|
1991-03-12 16:52:00 +00:00
|
|
|
switch(exp->nd_symb) {
|
1987-09-23 16:39:43 +00:00
|
|
|
case '+':
|
1991-03-12 16:52:00 +00:00
|
|
|
*set3++ = *set1++ | *set2++;
|
1987-09-23 16:39:43 +00:00
|
|
|
break;
|
|
|
|
case '-':
|
1991-03-12 16:52:00 +00:00
|
|
|
*set3++ = *set1++ & ~*set2++;
|
1987-09-23 16:39:43 +00:00
|
|
|
break;
|
|
|
|
case '*':
|
1991-03-12 16:52:00 +00:00
|
|
|
*set3++ = *set1++ & *set2++;
|
1987-09-23 16:39:43 +00:00
|
|
|
break;
|
|
|
|
case '/':
|
1991-03-12 16:52:00 +00:00
|
|
|
*set3++ = *set1++ ^ *set2++;
|
1986-04-09 18:14:49 +00:00
|
|
|
break;
|
|
|
|
}
|
1986-05-28 18:36:51 +00:00
|
|
|
}
|
1987-09-23 16:39:43 +00:00
|
|
|
break;
|
1987-05-21 09:37:28 +00:00
|
|
|
|
1987-09-23 16:39:43 +00:00
|
|
|
case GREATEREQUAL:
|
|
|
|
case LESSEQUAL:
|
|
|
|
case '=':
|
|
|
|
case '#':
|
|
|
|
/* Constant set comparisons
|
|
|
|
*/
|
|
|
|
for (j = 0; j < setsize; j++) {
|
1991-03-12 16:52:00 +00:00
|
|
|
switch(exp->nd_symb) {
|
1987-09-23 16:39:43 +00:00
|
|
|
case GREATEREQUAL:
|
|
|
|
if ((*set1 | *set2++) != *set1) break;
|
|
|
|
set1++;
|
|
|
|
continue;
|
|
|
|
case LESSEQUAL:
|
|
|
|
if ((*set2 | *set1++) != *set2) break;
|
|
|
|
set2++;
|
|
|
|
continue;
|
|
|
|
case '=':
|
|
|
|
case '#':
|
|
|
|
if (*set1++ != *set2++) break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (j < setsize) {
|
1991-03-12 16:52:00 +00:00
|
|
|
j = exp->nd_symb == '#';
|
1987-09-23 16:39:43 +00:00
|
|
|
}
|
|
|
|
else {
|
1991-03-12 16:52:00 +00:00
|
|
|
j = exp->nd_symb != '#';
|
1987-09-23 16:39:43 +00:00
|
|
|
}
|
1991-03-12 16:52:00 +00:00
|
|
|
*expp = getnode(Value);
|
|
|
|
(*expp)->nd_symb = INTEGER;
|
|
|
|
(*expp)->nd_INT = j;
|
|
|
|
(*expp)->nd_type = bool_type;
|
|
|
|
(*expp)->nd_lineno = (*expp)->nd_lineno;
|
1987-09-23 16:39:43 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
crash("(cstset)");
|
1986-04-08 18:15:46 +00:00
|
|
|
}
|
1991-03-12 16:52:00 +00:00
|
|
|
FreeSet(exp->nd_LEFT->nd_set);
|
|
|
|
FreeSet(exp->nd_RIGHT->nd_set);
|
|
|
|
FreeNode(exp);
|
1986-04-07 17:40:38 +00:00
|
|
|
}
|
|
|
|
|
1986-04-11 11:57:19 +00:00
|
|
|
cstcall(expp, call)
|
1991-03-12 16:52:00 +00:00
|
|
|
t_node **expp;
|
1986-04-11 11:57:19 +00:00
|
|
|
{
|
|
|
|
/* a standard procedure call is found that can be evaluated
|
|
|
|
compile time, so do so.
|
|
|
|
*/
|
1987-10-19 11:28:37 +00:00
|
|
|
register t_node *expr;
|
|
|
|
register t_type *tp;
|
1986-04-11 11:57:19 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
assert((*expp)->nd_class == Call);
|
|
|
|
expr = (*expp)->nd_RIGHT->nd_LEFT;
|
1987-10-19 11:28:37 +00:00
|
|
|
tp = expr->nd_type;
|
1991-03-12 16:52:00 +00:00
|
|
|
expr->nd_type = (*expp)->nd_type;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
(*expp)->nd_RIGHT->nd_LEFT = 0;
|
|
|
|
FreeNode(*expp);
|
|
|
|
*expp = expr;
|
|
|
|
expr->nd_symb = INTEGER;
|
|
|
|
expr->nd_class = Value;
|
1986-04-11 11:57:19 +00:00
|
|
|
switch(call) {
|
|
|
|
case S_ABS:
|
1991-03-12 16:52:00 +00:00
|
|
|
if (expr->nd_INT < 0) {
|
1991-03-13 13:49:56 +00:00
|
|
|
if (! options['s'] &&
|
|
|
|
expr->nd_INT <= min_int[(int)(tp->tp_size)]) {
|
1987-11-26 14:15:24 +00:00
|
|
|
overflow(expr);
|
|
|
|
}
|
1991-03-12 16:52:00 +00:00
|
|
|
expr->nd_INT = - expr->nd_INT;
|
1987-11-26 14:15:24 +00:00
|
|
|
}
|
1991-03-12 16:52:00 +00:00
|
|
|
CutSize(expr);
|
1986-04-11 11:57:19 +00:00
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-11 11:57:19 +00:00
|
|
|
case S_CAP:
|
1991-03-12 16:52:00 +00:00
|
|
|
if (expr->nd_INT >= 'a' && expr->nd_INT <= 'z') {
|
|
|
|
expr->nd_INT += ('A' - 'a');
|
1986-04-11 11:57:19 +00:00
|
|
|
}
|
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
case S_HIGH:
|
1986-04-11 11:57:19 +00:00
|
|
|
case S_MAX:
|
1987-10-19 11:28:37 +00:00
|
|
|
if (tp->tp_fund == T_INTEGER) {
|
1991-03-12 16:52:00 +00:00
|
|
|
expr->nd_INT = max_int[(int)(tp->tp_size)];
|
1986-04-11 11:57:19 +00:00
|
|
|
}
|
1996-08-14 07:42:40 +00:00
|
|
|
else if (tp->tp_fund == T_CARDINAL) {
|
|
|
|
expr->nd_INT = full_mask[(int)(tp->tp_size)];
|
1986-04-11 11:57:19 +00:00
|
|
|
}
|
1987-10-19 11:28:37 +00:00
|
|
|
else if (tp->tp_fund == T_SUBRANGE) {
|
1991-03-12 16:52:00 +00:00
|
|
|
expr->nd_INT = tp->sub_ub;
|
1986-04-11 11:57:19 +00:00
|
|
|
}
|
1991-03-12 16:52:00 +00:00
|
|
|
else expr->nd_INT = tp->enm_ncst - 1;
|
1986-04-11 11:57:19 +00:00
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-11 11:57:19 +00:00
|
|
|
case S_MIN:
|
1987-10-19 11:28:37 +00:00
|
|
|
if (tp->tp_fund == T_INTEGER) {
|
1991-03-12 16:52:00 +00:00
|
|
|
expr->nd_INT = min_int[(int)(tp->tp_size)];
|
1986-04-11 11:57:19 +00:00
|
|
|
}
|
1987-10-19 11:28:37 +00:00
|
|
|
else if (tp->tp_fund == T_SUBRANGE) {
|
1991-03-12 16:52:00 +00:00
|
|
|
expr->nd_INT = tp->sub_lb;
|
1986-04-11 11:57:19 +00:00
|
|
|
}
|
1991-03-12 16:52:00 +00:00
|
|
|
else expr->nd_INT = 0;
|
1986-04-11 11:57:19 +00:00
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-11 11:57:19 +00:00
|
|
|
case S_ODD:
|
1991-03-12 16:52:00 +00:00
|
|
|
expr->nd_INT &= 1;
|
1986-04-11 11:57:19 +00:00
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1991-03-12 16:52:00 +00:00
|
|
|
case S_TSIZE:
|
1986-04-11 11:57:19 +00:00
|
|
|
case S_SIZE:
|
1991-03-12 16:52:00 +00:00
|
|
|
expr->nd_INT = tp->tp_size;
|
1986-04-11 11:57:19 +00:00
|
|
|
break;
|
1986-07-08 14:59:02 +00:00
|
|
|
|
1986-04-11 11:57:19 +00:00
|
|
|
default:
|
1986-05-28 18:36:51 +00:00
|
|
|
crash("(cstcall)");
|
1986-04-11 11:57:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 21:04:18 +00:00
|
|
|
void
|
1986-05-01 19:06:53 +00:00
|
|
|
CutSize(expr)
|
1987-09-23 16:39:43 +00:00
|
|
|
register t_node *expr;
|
1986-04-07 17:40:38 +00:00
|
|
|
{
|
|
|
|
/* The constant value of the expression expr is made to
|
|
|
|
conform to the size of the type of the expression.
|
|
|
|
*/
|
1987-09-23 16:39:43 +00:00
|
|
|
register t_type *tp = BaseType(expr->nd_type);
|
1986-04-07 17:40:38 +00:00
|
|
|
|
1986-04-07 22:15:08 +00:00
|
|
|
assert(expr->nd_class == Value);
|
1989-12-19 09:40:25 +00:00
|
|
|
if (tp->tp_fund == T_REAL) return;
|
1987-11-26 14:15:24 +00:00
|
|
|
if (tp->tp_fund != T_INTEGER) {
|
1988-01-15 16:55:01 +00:00
|
|
|
expr->nd_INT &= full_mask[(int)(tp->tp_size)];
|
1986-04-07 17:40:38 +00:00
|
|
|
}
|
|
|
|
else {
|
1989-08-08 09:11:32 +00:00
|
|
|
int nbits = (int) (sizeof(arith) - tp->tp_size) * 8;
|
1986-04-07 17:40:38 +00:00
|
|
|
|
1987-12-02 10:41:38 +00:00
|
|
|
expr->nd_INT = (expr->nd_INT << nbits) >> nbits;
|
1986-04-07 17:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1986-05-01 19:06:53 +00:00
|
|
|
InitCst()
|
1986-04-07 17:40:38 +00:00
|
|
|
{
|
1986-07-08 14:59:02 +00:00
|
|
|
register int i = 0;
|
1991-03-13 17:26:07 +00:00
|
|
|
#ifndef NOCROSS
|
1986-07-08 14:59:02 +00:00
|
|
|
register arith bt = (arith)0;
|
1986-04-07 17:40:38 +00:00
|
|
|
|
|
|
|
while (!(bt < 0)) {
|
1987-12-02 10:41:38 +00:00
|
|
|
i++;
|
|
|
|
bt = (bt << 8) + 0377;
|
1991-03-14 13:16:32 +00:00
|
|
|
if (i == MAXSIZE+1)
|
1986-04-07 17:40:38 +00:00
|
|
|
fatal("array full_mask too small for this machine");
|
|
|
|
full_mask[i] = bt;
|
1989-08-08 09:11:32 +00:00
|
|
|
max_int[i] = bt & ~(1L << ((8 * i) - 1));
|
1987-11-26 14:15:24 +00:00
|
|
|
min_int[i] = - max_int[i];
|
|
|
|
if (! options['s']) min_int[i]--;
|
1986-04-07 17:40:38 +00:00
|
|
|
}
|
1989-08-08 09:11:32 +00:00
|
|
|
if ((int)long_size > sizeof(arith)) {
|
|
|
|
fatal("sizeof (arith) insufficient on this machine");
|
1986-04-08 23:34:10 +00:00
|
|
|
}
|
1986-04-07 17:40:38 +00:00
|
|
|
|
1987-12-02 10:41:38 +00:00
|
|
|
wrd_bits = 8 * (int) word_size;
|
1991-03-13 17:26:07 +00:00
|
|
|
#else
|
|
|
|
if (options['s']) {
|
|
|
|
for (i = 0; i < sizeof(long); i++) min_int[i] = - max_int[i];
|
|
|
|
}
|
1991-03-12 16:52:00 +00:00
|
|
|
#endif
|
1986-04-07 17:40:38 +00:00
|
|
|
}
|