'error: unable to find a register to spill in class' when changing array size

Hello everyone,

I’m getting the following error when compiling my code:

error: unable to find a register to spill in class ‘NO_REGS’

You can see my code below, this code does compile, but if I change the ‘formated_string’ that is declared inside function ‘print_inventory()’ from 255 to 100 then the error appears.

My code is not that large that I need to change the array size, so this is not urgent but I would like to know what is causing this error.

I have done some research and some say that this is caused by a GCC bug, I have tried to change the optimization, the default is ‘-Os’ and I have tried ‘-O1’, ‘-O2’, and ‘-O3’ but that did not worked.

Thank you

#include <stdarg.h>

// Definitions
#define NUM_FLOORS            3
#define NUM_SPACES_PER_FLOOR  3
#define AVAILABLE_SPACES      NUM_FLOORS * NUM_SPACES_PER_FLOOR
#define QTY_PART_NUMBERS      AVAILABLE_SPACES - 1 //1 space for retrieving area
#define MAX_CHAR              20
#define MAX_CHAR_PN           MAX_CHAR
#define MAX_CHAR_DESC         50
#define STARTING_FLOOR        1 //1 Based, 1 - 2 - 3
#define STARTING_SPACE        1 //1 Based, 1 - 2 - 3

//print definition
//#define MAX_STRING_LEN                100
//#define PRINTF1(buf, str, A)          sprintf(buf, str, A); \
//                                      Serial.print(A)
#define PRINTF3(buf, str, A, B, C)    sprintf(buf, str, A, B, C); \
                                      Serial.print(A)

// Structure to store each item
typedef struct
{
  char part_number[MAX_CHAR_PN];
  char description[MAX_CHAR_DESC];
  int  available_qty;
  int  _floor;
  int  _space;
} INVENTORY;

INVENTORY current_inventory [QTY_PART_NUMBERS] =
{ //Part Number  Description                                              Qty Floor Space
  {"91290A170", "Steel Socket Head Screw M4 x 0.7 mm Thread 22 mm Long",  10, 1,    2 },
  {"6408K9",    "Flex Shaft Coup Hub for 1/8\" Dia Shaft, 13/16\" Iron",  1 , 1,    3 },
  {"6408K61",   "Buna-N Spider for 5/8\" Out Diam Flex Shaft Coupling",   33, 2,    1 },
  {"98410A665", "Extern Retaining Ring Copper for 3/8\" Shaft Diam",      49, 2,    2 },
  {"90591A255", "Steel Hex Nut, Class 8, M4 x 0.7 mm Thread",             80, 2,    3 },
  {"60355K22",  "Ball Bearing Trade Number R20, for 1-1/4\" Shaft Diam",  0 , 3,    1 },
  {"7422K22",   "Cable SJOOW, Black Outer Insulation, 16 Gauge, 3 Wires", 54, 3,    2 },
  {"5789T7",    "DIN-Rail Mount AC Branch Circuit Breaker 1 Pole-Toggle", 5 , 3,    3 }
};

// Read commands and part numbers from serial
String serial_string = "";



void setup()
{
  Serial.begin(9600);
  Serial.setTimeout(100);
}

void loop()
{
  if (Serial.available() > 0)
  {
    serial_string = Serial.readString();

    if (serial_string.equals("help"))
    {
      // Help command, print all commands
      Serial.print("help:      prints all commands available\n");
      Serial.print("inventory: prints the inventory\n");
    }
    else if (serial_string.equals("inventory"))
    {
      print_inventory();
    }
    else
    {
      // No command, lest search for the product number
      int indx = 0;
      while (indx < QTY_PART_NUMBERS)
      {
        //if (serial_string.equals(current_inventory[indx].part_number))
        if (1)
        {
          break;
        }
        indx++;
      }

      //did we find something?
      if (indx >= QTY_PART_NUMBERS)
      {
        Serial.print("Part found on:\n");
//                      PRINTF1("Floor %d\n", current_inventory[indx]._floor);
//                      PRINTF1("Space %d\n", current_inventory[indx]._space);
      }
      else
      {
        // No command, and no part number so lets talk to the
        Serial.print("Command not recognized or part number not found\n");
      }
    }
  }
} //void loop()


void print_inventory()
{
  //Store the formated text.
  char formated_string[255];

  Serial.print("\n      |Space         1|              2|              3|\n");
  Serial.print("     --------------------------------------------------\n");
  PRINTF3(formated_string, "Floor |%-14.14s |%-14.14s |%-14.14s |\n", current_inventory[5].part_number, current_inventory[6].part_number, current_inventory[7].part_number);
  PRINTF3(formated_string, "      |%-14.14s |%-14.14s |%-14.14s |\n", current_inventory[5].description, current_inventory[6].description, current_inventory[7].description);
  PRINTF3(formated_string, "    3 |%-14d |%-14d |%-14d |\n", current_inventory[5].available_qty, current_inventory[6].available_qty, current_inventory[7].available_qty);
  Serial.print("     --------------------------------------------------\n");
  PRINTF3(formated_string, "      |%-14.14s |%-14.14s |%-14.14s |\n", current_inventory[2].part_number, current_inventory[3].part_number, current_inventory[4].part_number);
  PRINTF3(formated_string, "      |%-14.14s |%-14.14s |%-14.14s |\n", current_inventory[2].description, current_inventory[3].description, current_inventory[4].description);
  PRINTF3(formated_string, "    2 |%-14d |%-14d |%-14d |\n", current_inventory[2].available_qty, current_inventory[3].available_qty, current_inventory[4].available_qty);
  Serial.print("     --------------------------------------------------\n");
  PRINTF3(formated_string, "      |%-14.14s |%-14.14s |%-14.14s |\n", "", current_inventory[0].part_number, current_inventory[1].part_number);
  PRINTF3(formated_string, "      |%-14.14s |%-14.14s |%-14.14s |\n", "retrieving", current_inventory[0].description, current_inventory[1].description);
  PRINTF3(formated_string, "    1 |%-14.14s |%-14d |%-14d |\n", "area", current_inventory[0].available_qty, current_inventory[1].available_qty);
  Serial.print("     --------------------------------------------------\n\n");
}

Is this with latest IDE version and board package? I was under the impression that at least some of the things that can cause this were fixed.

DrAzzy: Is this with latest IDE version and board package? I was under the impression that at least some of the things that can cause this were fixed.

Yes, I have just updated the board package to latest and have IDE 1.8.4 (don't know if its latest)

UPDATE: if I move that variable declaration from automatic (inside print_inventory() function) to global (outside any function) the error shows up regardless of the array size.