AnsweredAssumed Answered

Blackfin VisualDSP++ C compiler: incorrect function inlining

Question asked by LechLorens on Dec 13, 2011

Incorrect inlining of the toupper() function.

 

If the toupper() function is called with pointer pre- or post-increment at the place of the call, the compiler produces invalid code. Instead of incrementing

the pointer by one character, it increments it by 3. Additionally, incorrect value is returned from the toupper() function:

  • for the preincrement version: the value for the 4th character instead of the 2nd,
  • for the postincrement version: the value for the 3rd character instead of the 1st.

 

Simplified code showing the problem:

 

char *str = "abcd";

int upper;

 

printf("%s\n", str);                    // will print "abcd"

upper = toupper(*str++);

printf("%c\n", (char)upper);     // should print "A" but will print "C" instead

printf("%s\n", str);                    // should print "bcd" but will print "d" instead

 

Simplified code working the problem around:

 

char *str = "abcd";

int upper;

 

printf("%s\n", str);                    // should print "abcd"

upper = toupper(*str);

str++;

printf("%c\n", (char)upper);     // will print "A"

printf("%s\n", str);                    // will print "bcd"

 

The problem shows when executed on hardware, on the simulator or on the emulator (100B). Optimisations need not be enabled in the compiler settings.

 

Inlining is set to "When declared inline".

 

After inspection of the assembly file produced by the compiler it appears that toupper() uses an array of characters to translate lowercase characters to their uppercase counterparts. The problem seems to be that checking whether the input value is not out of the allowed range (i.e. is not <0 or >255). During each of these checks the pointer is incremented.

 

I am attaching a project which shows the problem when compiled with VDSP++ 5.0 update 10 but does not if compiled with VDSP++ 5.0 update 9.1.

 

I am compiling for Blackfin 527.

Attachments

Outcomes