~mcf/cproc

8e47cf45dfed44627fa6f73f8734f2149c661044 — Michael Forney 20 days ago 41e862a master
eval: Allow casting address constants to integer types in constant expressions
3 files changed, 13 insertions(+), 2 deletions(-)

M eval.c
A test/initializer-pointer-int-cast.c
A test/initializer-pointer-int-cast.qbe
M eval.c => eval.c +9 -2
@@ 137,8 137,15 @@ eval(struct expr *expr, enum evalkind kind)
			else
				expr->constant = l->constant;
			cast(expr);
		} else if (l->type->kind == TYPEPOINTER && expr->type->kind == TYPEPOINTER) {
			expr = l;
		} else if (l->type->kind == TYPEPOINTER) {
			/*
			A cast from a pointer to integer is not a valid constant
			expression, but C11 allows implementations to recognize
			other forms of constant expressions (6.6p10), and some
			programs expect this functionality.
			*/
			if (expr->type->kind == TYPEPOINTER || expr->type->prop & PROPINT && expr->type->size == typelong.size)
				expr = l;
		}
		break;
	case EXPRBINARY:

A test/initializer-pointer-int-cast.c => test/initializer-pointer-int-cast.c +2 -0
@@ 0,0 1,2 @@
int x;
long p = (long)&x;

A test/initializer-pointer-int-cast.qbe => test/initializer-pointer-int-cast.qbe +2 -0
@@ 0,0 1,2 @@
export data $p = align 8 { l $x, }
export data $x = align 4 { z 4 }