boschresearch / blech

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

Incorrect handling of compound literals in function translator #64

Closed schorg closed 3 years ago

schorg commented 3 years ago

Describe the bug The following literal handling is incorrectly code generated in functions but correctly translated in activities.

module 

struct S 
    var i: int32
    var bs: [1] bool
end

function funcWithS ()
    let bs: [1] bool = { [0] = true }
    var s: S = {i = 42, bs = bs}
end

activity ActWithS() 
    let bs: [1] bool = { [0] = true }
    var s: S = {i = 42, bs = bs}
    await true
end

To Reproduce The following code is generated

void blc_01arraybug201_funcWithS (void) {
    blc_bool blc_bs[1];
    blc_bool blc_00_aux_tmpLiteral_0[1];
    memset(blc_00_aux_tmpLiteral_0, 0, 1 * sizeof(blc_bool));
    blc_00_aux_tmpLiteral_0[0] = 1;
    memcpy(blc_bs, blc_00_aux_tmpLiteral_0, 1 * sizeof(blc_bool));
    blc_01arraybug201_S blc_s;
    blc_01arraybug201_S blc_00_aux_tmpLiteral_1;
    memset(&blc_00_aux_tmpLiteral_1, 0, sizeof(blc_01arraybug201_S));
    blc_00_aux_tmpLiteral_1.i = 42;
    blc_00_aux_tmpLiteral_1.bs = blc_bs;
    blc_s = blc_00_aux_tmpLiteral_1;
}

blc_pc_t blc_01arraybug201_ActWithS (struct blc_01arraybug201_ActWithS *blc_00_ctx) {
    if ( blc_00_ctx->pc_2 == 2 ) {
        memset(blc_00_ctx->blc_ActWithS_bs, 0, 1 * sizeof(blc_bool));
        blc_00_ctx->blc_ActWithS_bs[0] = 1;
        memset((&blc_00_ctx->blc_ActWithS_s), 0, sizeof(blc_01arraybug201_S));
        blc_00_ctx->blc_ActWithS_s.i = 42;
        memcpy(blc_00_ctx->blc_ActWithS_s.bs,
               blc_00_ctx->blc_ActWithS_bs,
               1 * sizeof(blc_bool));
        blc_00_ctx->pc_2 = 5; /* proceed from surface to depth */
    }
    if ( blc_00_ctx->pc_2 == 4 ) {
        if ( 1 ) {
            blc_00_ctx->pc_2 = 0; /* end */
        }
    }

    BLC_SWITCH_TO_NEXTSTEP(blc_00_ctx->pc_2);
    return blc_00_ctx->pc_2 ;
}

Expected behaviour In the translated function the assignment

    blc_00_aux_tmpLiteral_1.bs = blc_bs;

should be a memcpy similar to the translated activity

        memcpy(blc_00_aux_tmpLiteral_1.bs,
               blc_bs,
               1 * sizeof(blc_bool));