This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/BEAGLEBK: BBB PRU-ICSS Compiler: GCC "labels as value" extension?

Part Number: BEAGLEBK

Tool/software: TI C/C++ Compiler

Hi,

I'm optimizing a state machine for PRU on BeagleBoneBlack.

The classic approach for executing state code and changing states is a big switch() statement, like following:

<slowcode>
#define STATE_STOP 0
#define STATE_A 1

#define STATE_B 2
#define STATE_C 3
...

int statemachine(int state) {

switch(state) {
case STATE_A:
   do_something() ;
   if (some_condition)
     return STATE_B ;
   else
    return STATE_STOP ;
    
case STATE_B:
 ...
 ..
 }

}   

main() {

  ...
   state = STATE_A ;
   while (state = statmachine(state) );

   ...
}
</slowcode>

 

I saw the PRU-ICSS clpru compiler (2.3.1 here) compiles the "switch() ... case " as list of tests, like this pseudo code

if (state == 1) then
...
else if (state == 2) then
....
else if (state == 3) then
...

As I have some cases here, this chain of tests eats up a percentage of PRU execution time.

 

Much faster code could be generated if using an array of "computed gotos". GCC supports that as "labels as values" extension, as in this StackOverflow example:

<fastcode>
int main(){
    int value  = 2;
    const void *labels[] = {&&val_0, &&val_1, &&val_2};
    goto *labels[value];
    val_0:
        printf("The value is 0\n");
        goto end;
    val_1:
        printf("The value is 1\n");
        goto end;
    val_2:
        printf("The value is 2\n");
        goto end;
    end:
    return(0);
}
</fastcode>
 

"Labels as values" is a strange feature of course, its justification seems limited to writing zero-overhead command processors.

Sadly, the "Labels as values" extension seems not supported in clpru 2.3.1 ... clpru complains like

Invoking: PRU Compiler
..... /ti-cgt-pru_2.3.1//bin/clpru --include_path= ....
"pru1_statemachine_data_slave.c", line 51: error #1489: GCC && operator not
          supported
      const void* labelarray[] = &&lab1, &&lab2, &&lab3} ;
                          
Any chance to get "labels as values" implemented in clpru?

best regards,
Joerg