blech-lang / blech

Blech is a language for developing reactive, real-time critical embedded software.
Apache License 2.0
64 stars 5 forks source link

wrong code generation when using when/reset #1

Closed MohamedGhardallou closed 2 years ago

MohamedGhardallou commented 2 years ago

Hello,

Describe the bug consider the following stripped code

@[EntryPoint]
activity Ctrl(stop_btn :bool)(bug_led: bool , state:nat8) //we declare the variable state as output just for debug 

    bug_led = false // should remain always false
    var a: bool = true
    var b: bool = true
    var requested_state : nat8 = 0

  cobegin
    repeat
        a = true
        await true
    end
  with
    state = 0
    when state != requested_state reset
      state = requested_state

      if state == 0 then

        cobegin 
          requested_state = 2
          await true

        with
            await stop_btn
            bug_led = true
        end

      elseif state == 1 then

        //infinite loop        
        repeat
            await true
        end

        await a 
        when b  abort
            await true
        end

      elseif state == 2 then

        await stop_btn 
        requested_state = 1
        await true

      end

      await true

    end // end when 

  end

end

To Reproduce

compile this code set the input stop_btn to false execute 10 cycles set the input stop_btn to true execute 1 cycle set the input stop_btn to false execute x cycles

you could use this code c to test this behaviour:

#include <stdbool.h>
#include <stdint.h>
#include <assert.h>
#include <stdio.h>

#include "control.h"

int main() {

  int bug_led = 0;
  uint8_t state = 0;
  bool stop_btn = false;

  blc_blech_01control01_init();

  stop_btn = false;

  for (int i = 0; i < 20; i++) {
    blc_blech_01control01_tick(stop_btn, &bug_led, &state);
    assert(bug_led == 0);

    if (i  == 10 )
    {
      stop_btn = true;
    } else {
      stop_btn = false;
    }
    printf("%u\n", state);
  }
}

Expected behaviour bug_led should always remain false.

The when/reset construct has been used here to implement a simple state machine

schorg commented 2 years ago

It seems that the embedded cobegin with two strong branches in state == 0 is not preempted by the when reset. I will consult @FriedrichGretz

FriedrichGretz commented 2 years ago

@MohamedGhardallou thank you very much for the bug report. We had a bug which however did not manifest itself on simpler test programs. The issue should be solved now. You can simply rebuild your Blech compiler from this repo. I hope we will soon publish a release that includes this fix but that may take some more days... ;-)

MohamedGhardallou commented 2 years ago

@FriedrichGretz The bug is fixed. Thanks :)