~mcf/qbe

a3a4390fa0466c1d2df17cd666214122e0db0531 — Michael Forney 6 months ago 0da0930
fold: Don't fold invalid addition/subtraction rather than failing

This may happen in a branch QBE doesn't realize is unreachable,
for example (simplified from real code found in ncurses)

	data $str = { b "abcdef", b 0 }
	function l $f(w %x) {
	@start
		%.1 =w ceqw %x, 0
		jnz %.1, @logic_join, @logic_right
	@logic_right
		%p =l call $strchr(l $str, w %x)
		%.2 =w ceql %p, 0
	@logic_join
		%.3 =w phi @start %.1, @logic_right %.2
		jnz %.3, @fail, @return
	@fail
		ret 0
	@return
		%.4 =l sub %p, $str
		ret %.4
	}
1 files changed, 3 insertions(+), 3 deletions(-)

M fold.c
M fold.c => fold.c +3 -3
@@ 343,7 343,7 @@ foldint(Con *res, int op, int w, Con *cl, Con *cr)
	if (op == Oadd) {
		if (cl->type == CAddr) {
			if (cr->type == CAddr)
				err("undefined addition (addr + addr)");
				return 1;
			lab = cl->label;
			typ = CAddr;
		}


@@ 358,10 358,10 @@ foldint(Con *res, int op, int w, Con *cl, Con *cr)
				lab = cl->label;
				typ = CAddr;
			} else if (cl->label != cr->label)
				err("undefined substraction (addr1 - addr2)");
				return 1;
		}
		else if (cr->type == CAddr)
			err("undefined substraction (num - addr)");
			return 1;
	}
	else if (cl->type == CAddr || cr->type == CAddr)
		return 1;