Compilation error? (value<32 ) compiled as (value<31)

mozufferey
Posts: 8
Joined: Thu Aug 10, 2017 7:21 pm

Compilation error? (value<32 ) compiled as (value<31)

Postby mozufferey » Wed Sep 13, 2017 9:38 am

I'm not sure about what I found here but this is quite worrying.

I actually work with the "blinky" example of the esp32_IDF and debug it.

Here is small extract of the code to make easier to understand the problem:

Code: Select all

void blink_task(void *pvParameter)
	{
		......
		while(1) {
			gpio_set_level(BLINK_GPIO, 0);
                       ..........
		}
	}
If you searcg for the API function "gpio_set_level" you have

Code: Select all

esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level)
	{
		GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG);
		.........
	}
where the macro GPIO_CHECK could be expanded to

Code: Select all

if (! (((gpio_num < GPIO_PIN_COUNT && GPIO_PIN_MUX_REG[gpio_num] != 0)) && (gpio_num < 34))) 
{
   Errror
   return
}
the boolean expression is rewritten here for more clarity:
if (! ( (gpio_num<40) && ( GPIO_PIN_MUX_REG[gpio_num] != 0) && (gpio_num < 34))
The assembler code is

Code: Select all

    400f49a3:   movi.n  a8, 39
    400f49a5:   bltu    a8, a2, 0x400f49b8 <gpio_set_level+24>
    400f49a8:   l32r    a8, 0x400d0504 <_stext+1260>
    400f49ab:   addx4   a8, a2, a8
    400f49ae:   l32i.n  a8, a8, 0
    400f49b0:   beqz    a8, 0x400f49b8 <gpio_set_level+24>
    400f49b3:   movi.n  a8, 33
    400f49b5:   bgeu    a8, a2, 0x400f49de <gpio_set_level+62>
where a2 contains the value gpio_num

here we have three conditional jumps corresponding to the three Boolean expressions.

bltu = Branch if Less Than Unsigned -> (39<gpio_num)
beqz = Branch if Equal to Zero -> (a8==0)
bgeu = Branch if Greater Than or Equal Unsigned -> (33>=gpio_num)

we kann see that the constants 40 (GPIO_PIN_COUNT) and 34 have been replaced by 39 and 33 respectively.

and we have the following

(gpio_num < 34) is similar to (33 >= gpio_num)
(gpio_num < 40) is not similar to !(39 < gpio_num) should be !(39 <= gpio_num)

Actually if I replace in blinky the code:

gpio_set_level(BLINK_GPIO, 0);
with
gpio_set_level(39, 0);

I run into the error.
(console com4 >> E (24044) gpio: gpio_set_level(189): GPIO output gpio_num error)


This seems to me very strange and if confirmed has potential to produce many bugged code.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Compilation error? (value<32 ) compiled as (value<31)

Postby WiFive » Wed Sep 13, 2017 6:59 pm

(gpio_num < 34) test means 39 should fail anyway.

Actually
(39 < 40) = true
!(39 < 39) = !(false) = true

mozufferey
Posts: 8
Joined: Thu Aug 10, 2017 7:21 pm

Re: Compilation error? (value<32 ) compiled as (value<31)

Postby mozufferey » Wed Sep 13, 2017 10:28 pm

Many thanks to Wifive for his answer.

You completely correct to point that the test is redundant and that the test with 40 (39) will be overwritten by the test with 34 (33). This is why my final test failed.

I retested it with the debug and trace the code to see that it run correctly.

Basically the boolean functions

Code: Select all

   ( x <  40 ) 
   ( x <= 39 )
   !(39 < x )
are identical.

Next time I will check it thoroughly.

NB:
40 is the number of Input/Output
34 is the number of Output

Who is online

Users browsing this forum: Google [Bot] and 42 guests