Commits

Rémy Oudompheng  committed 065fde9

cmd/6g: fix out of registers when chaining integer divisions.

Fixes issue 4201.

R=golang-dev, rsc
CC=golang-dev, remy
http://codereview.appspot.com/6622055

  • Participants
  • Parent commits 49b4150

Comments (0)

Files changed (2)

File src/cmd/6g/cgen.c

 			a = optoas(n->op, nl->type);
 			goto abop;
 		}
-		cgen_div(n->op, nl, nr, res);
+
+		if(nl->ullman >= nr->ullman) {
+			regalloc(&n1, nl->type, res);
+			cgen(nl, &n1);
+			cgen_div(n->op, &n1, nr, res);
+			regfree(&n1);
+		} else {
+			if(!smallintconst(nr)) {
+				regalloc(&n2, nr->type, res);
+				cgen(nr, &n2);
+			} else {
+				n2 = *nr;
+			}
+			cgen_div(n->op, nl, &n2, res);
+			if(n2.op != OLITERAL)
+				regfree(&n2);
+		}
 		break;
 
 	case OLSH:

File test/torture.go

 		Child(0).
 		Child(0).(*U)
 }
+
+// Chains of divisions. See issue 4201.
+
+func ChainDiv(a, b int) int {
+	return a / b / a / b / a / b / a / b /
+		a / b / a / b / a / b / a / b /
+		a / b / a / b / a / b / a / b
+}
+
+func ChainDivRight(a, b int) int {
+	return a / (b / (a / (b /
+            (a / (b / (a / (b /
+            (a / (b / (a / (b /
+            (a / (b / (a / (b /
+            (a / (b / (a / b))))))))))))))))))
+}
+
+func ChainDivConst(a int) int {
+	return a / 17 / 17 / 17 /
+		17 / 17 / 17 / 17 /
+		17 / 17 / 17 / 17
+}