From f2a079fa1a94b6a99e7ce432b6df17fdb4c9b554 Mon Sep 17 00:00:00 2001
From: Michael Forney <mforney@mforney.org>
Date: Fri, 22 Feb 2019 11:52:29 -0800
Subject: Fix adding integer to pointer

---
 expr.c                    |  2 +-
 tests/add-int-pointer.c   |  4 ++++
 tests/add-int-pointer.qbe | 10 ++++++++++
 3 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 tests/add-int-pointer.c
 create mode 100644 tests/add-int-pointer.qbe

diff --git a/expr.c b/expr.c
index 97b2fd5..a68f5d8 100644
--- a/expr.c
+++ b/expr.c
@@ -165,7 +165,7 @@ mkbinaryexpr(struct location *loc, enum tokenkind op, struct expression *l, stru
 			break;
 		}
 		if (r->type->kind == TYPEPOINTER)
-			e = l, l = r, r = e;
+			e = l, l = r, r = e, rp = lp;
 		if (l->type->kind != TYPEPOINTER || !(rp & PROPINT))
 			error(loc, "invalid operands to '+' operator");
 		t = l->type;
diff --git a/tests/add-int-pointer.c b/tests/add-int-pointer.c
new file mode 100644
index 0000000..be1a60f
--- /dev/null
+++ b/tests/add-int-pointer.c
@@ -0,0 +1,4 @@
+int x[2];
+void f(void) {
+	1 + x;
+}
diff --git a/tests/add-int-pointer.qbe b/tests/add-int-pointer.qbe
new file mode 100644
index 0000000..22825ee
--- /dev/null
+++ b/tests/add-int-pointer.qbe
@@ -0,0 +1,10 @@
+export
+function $f() {
+@start.1
+@body.2
+	%.1 =l extsw 1
+	%.2 =l mul %.1, 4
+	%.3 =l add $x, %.2
+	ret
+}
+export data $x = align 4 { z 8 }
-- 
cgit v1.2.3