+2021-01-23  Tom Tromey  <tom@tromey.com>
+
+       PR compile/25575
+       * compile/compile-loc2c.c (note_register): New function.
+       (pushf_register_address, pushf_register): Use it.
+
 2021-01-23  Tom Tromey  <tom@tromey.com>
 
        * symtab.h (struct symbol_computed_ops) <generate_c_location>:
 
   stream->printf ("__label_%u_%s", scope, pulongest (target));
 }
 
+/* Note that a register was used.  */
+
+static void
+note_register (int regnum, std::vector<bool> ®isters_used)
+{
+  gdb_assert (regnum >= 0);
+  /* If the expression uses a cooked register, then we currently can't
+     compile it.  We would need a gdbarch method to handle this
+     situation.  */
+  if (regnum >= registers_used.size ())
+    error (_("Expression uses \"cooked\" register and cannot be compiled."));
+  registers_used[regnum] = true;
+}
+
 /* Emit code that pushes a register's address on the stack.
    REGISTERS_USED is an out parameter which is updated to note which
    register was needed by this expression.  */
 {
   std::string regname = compile_register_name_mangled (gdbarch, regnum);
 
-  registers_used[regnum] = true;
+  note_register (regnum, registers_used);
   pushf (indent, stream,
         "(" GCC_UINTPTR ") &" COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
         regname.c_str ());
 {
   std::string regname = compile_register_name_mangled (gdbarch, regnum);
 
-  registers_used[regnum] = true;
+  note_register (regnum, registers_used);
   if (offset == 0)
     pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
           regname.c_str ());