[PATCH v2] gas: fix MAX_ISTACK for loopgtz L32R widening

Alexey Lapshin alexey.lapshin@espressif.com
Tue Aug 25 07:46:37 GMT 2026


Commit 403b0b61f6d4 ("gas: use literals/const16 for xtensa loop
relaxation") replaced loop widening with an L32R-based sequence.
The loopgtz variant of that sequence needs 13 istack entries, but
MAX_ISTACK remained 12, so relaxation of an out-of-range loopgtz
overflowed the instruction stack. Increase MAX_ISTACK and report
overflow with as_fatal that names MAX_ISTACK. Add a test.

gas/
	* config/xtensa-istack.h (MAX_ISTACK): Increase to 16. Document
	requirement.
	* config/tc-xtensa.c (istack_push, istack_push_space): Report
	stack overflow with as_fatal.
	* testsuite/gas/xtensa/loopgtz-relax.d: New test.
	* testsuite/gas/xtensa/loopgtz-relax.s: New test source.
---
 gas/config/tc-xtensa.c                   |  8 ++++++--
 gas/config/xtensa-istack.h               |  5 ++++-
 gas/testsuite/gas/xtensa/loopgtz-relax.d | 16 ++++++++++++++++
 gas/testsuite/gas/xtensa/loopgtz-relax.s | 10 ++++++++++
 4 files changed, 36 insertions(+), 3 deletions(-)
 create mode 100644 gas/testsuite/gas/xtensa/loopgtz-relax.d
 create mode 100644 gas/testsuite/gas/xtensa/loopgtz-relax.s

diff --git a/gas/config/tc-xtensa.c b/gas/config/tc-xtensa.c
index 397744f742b..414d1dda316 100644
--- a/gas/config/tc-xtensa.c
+++ b/gas/config/tc-xtensa.c
@@ -12520,7 +12520,9 @@ void
 istack_push (IStack *stack, TInsn *insn)
 {
   int rec = stack->ninsn;
-  gas_assert (!istack_full (stack));
+  if (istack_full (stack))
+    as_fatal (_("internal error: instruction stack overflow; "
+		"increase MAX_ISTACK"));
   stack->insn[rec] = *insn;
   stack->ninsn++;
 }
@@ -12534,7 +12536,9 @@ istack_push_space (IStack *stack)
 {
   int rec = stack->ninsn;
   TInsn *insn;
-  gas_assert (!istack_full (stack));
+  if (istack_full (stack))
+    as_fatal (_("internal error: instruction stack overflow; "
+		"increase MAX_ISTACK"));
   insn = &stack->insn[rec];
   tinsn_init (insn);
   stack->ninsn++;
diff --git a/gas/config/xtensa-istack.h b/gas/config/xtensa-istack.h
index 15e34c2b9d8..67367f16f12 100644
--- a/gas/config/xtensa-istack.h
+++ b/gas/config/xtensa-istack.h
@@ -23,7 +23,10 @@
 
 #include "xtensa-isa.h"
 
-#define MAX_ISTACK 12
+/* Must hold the largest multi-instruction widening expansion from
+   widen_spec_list in xtensa-relax.c.  Currently that is the loopgtz
+   + L32R sequence (13 entries including LITERAL and LABEL).  */
+#define MAX_ISTACK 16
 #define MAX_INSN_ARGS 64
 
 enum itype_enum
diff --git a/gas/testsuite/gas/xtensa/loopgtz-relax.d b/gas/testsuite/gas/xtensa/loopgtz-relax.d
new file mode 100644
index 00000000000..1374186c6d6
--- /dev/null
+++ b/gas/testsuite/gas/xtensa/loopgtz-relax.d
@@ -0,0 +1,16 @@
+#as:
+#objdump: -d
+
+#...
+.*beqz.*a9,.*
+.*bltz.*a9,.*
+.*loopgtz.*a9,.*
+.*rsr.lend.*a9
+.*wsr.lbeg.*a9
+.*l32r.*a9,.*
+.*nop
+.*wsr.lend.*a9
+.*isync
+.*rsr.lcount.*a9
+.*addi.*a9, a9, 1
+#...
diff --git a/gas/testsuite/gas/xtensa/loopgtz-relax.s b/gas/testsuite/gas/xtensa/loopgtz-relax.s
new file mode 100644
index 00000000000..a03528f306c
--- /dev/null
+++ b/gas/testsuite/gas/xtensa/loopgtz-relax.s
@@ -0,0 +1,10 @@
+	.text
+	.globl main
+	.align 4
+main:
+	loopgtz a9, .Lloop_end
+	.rep 200
+	nop
+	.endr
+.Lloop_end:
+	nop
-- 
2.55.0



More information about the Binutils mailing list