Topic : strange problem

Forum : ST7/STM8

Original Post
Post Information Post
January 25, 2010 - 4:29pm
Guest

hi I have found a problem that I don´t understand:

I have a switch case sentence with an enum:



enum codigos_funcion{codigo_direccionar=0, codigo_comienzo_direccionamiento=1,
               codigo_fin_direccionamiento=2, codigo_guarda_peso_averia=3, 
               codigo_carga_peso_averia=4, codigo_carga_peso=5, codigo_conecta_unidad=6, codigo_desconecta_unidad=7, codigo_valor_unidad=8,
               codigo_error=9, codigo_pregunta_estado_sistema=10,
               max_codigos};           

u8 buffer_recepcion_trama[BYTES_DATOS_EN_LA_TRAMA+2]={0,0,0,0,0,0,0,0};


enum eventos_maquina_modbus_esclavo processing_required_action(void)
   {
   enum codigos_funcion codigo_funcion_recibido;
   enum eventos_maquina_modbus_esclavo evento_maquina_modbus_esclavo_procesado;
   u8 peso_averia_recibido[4];
   
   
   codigo_funcion_recibido = buffer_recepcion_trama[1];
   
   //guarda_en_la_eeprom(3,codigo_funcion_recibido);
   
   switch (codigo_funcion_recibido)
      {
                ...
                ...
                ...

I receive the "codigo_funcion_recibido" in a buffer and I then I use it in the switch but the thing is that it doesn´t work. However if I add the function that is commented, which saves in eeprom this "codigo_funcion_redibido" then it works. I have been checking the .lst and it is:

with that function included (it works):

  ; FUNCTION ?processing_required_action (BEGIN)
              ; SOURCE LINE # 27 
0000 5206                              SUB    SP,#006H
              ; SOURCE LINE # 34 
0002 B600       F                      LD     A,buffer_recepcion_trama + 01H
0004 5F                                CLRW   X
0005 97                                LD     XL,A
0006 1F01       F                      LDW    (001H,SP),X   ; [ codigo_funcion_recibido ]
              ; SOURCE LINE # 36 
0008 AE0003                            LDW    X,#00003H
000B 8D000000   F                      CALLF  ?guarda_en_la_eeprom
              ; SOURCE LINE # 38 
000F 1E01       F                      LDW    X,(001H,SP)   ; [ codigo_funcion_recibido ]
0011 2750                              JREQ   ?CASE_0108
0013 A30001                            CPW    X,#00001H
0016 2730                              JREQ   ?CASE_0109
0018 A30002                            CPW    X,#00002H
001B 2761                              JREQ   ?CASE_0110
001D A30003                            CPW    X,#00003H
0020 276A                              JREQ   ?CASE_0111
0022 A30004                            CPW    X,#00004H
0025 2603                              JRNE   ?LAB_0184
0027 CC0000     F                      JP     ?NXT_0604
002A         ?LAB_0184:
002A A30005                            CPW    X,#00005H
002D 2603                              JRNE   ?LAB_0185
002F CC0000     F                      JP     ?NXT_0604
0032         ?LAB_0185:
0032 A30006                            CPW    X,#00006H
0035 276F                              JREQ   ?CASE_0114
0037 A30007                            CPW    X,#00007H
003A 2775                              JREQ   ?CASE_0115
003C A30008                            CPW    X,#00008H
003F 2778                              JREQ   ?NXT_0604
0041 A3000A                            CPW    X,#0000AH
0044 2673                              JRNE   ?NXT_0604
0046 2071                              JRA    ?NXT_0604
0048         ?CASE_0109:
...
...
...

with that function commented (it doesn´t work):


              ; FUNCTION ?processing_required_action (BEGIN)
              ; SOURCE LINE # 27 
0000 5206                              SUB    SP,#006H
              ; SOURCE LINE # 34 
0002 B600       F                      LD     A,buffer_recepcion_trama + 01H
0004 5F                                CLRW   X
0005 97                                LD     XL,A
0006 1F01       F                      LDW    (001H,SP),X   ; [ codigo_funcion_recibido ]
              ; SOURCE LINE # 38 
0008 4D                                TNZ    A
0009 2746                              JREQ   ?CASE_0108
000B 4A                                DEC    A
000C 2728                              JREQ   ?CASE_0109
000E A102                              CP     A,#002H
0010 275A                              JREQ   ?CASE_0110
0012 A103                              CP     A,#003H
0014 2764                              JREQ   ?CASE_0111
0016 A104                              CP     A,#004H
0018 2603                              JRNE   ?LAB_0184
001A CC0000     F                      JP     ?NXT_0604
001D         ?LAB_0184:
001D A105                              CP     A,#005H
001F 2603                              JRNE   ?LAB_0185
0021 CC0000     F                      JP     ?NXT_0604
0024         ?LAB_0185:
0024 A106                              CP     A,#006H
0026 276C                              JREQ   ?CASE_0114
0028 A107                              CP     A,#007H
002A 2773                              JREQ   ?CASE_0115
002C A108                              CP     A,#008H
002E 2777                              JREQ   ?NXT_0604
0030 A10A                              CP     A,#00AH
0032 2673                              JRNE   ?NXT_0604
0034 2071                              JRA    ?NXT_0604
0036         ?CASE_0109:
...
...
...

I don´t know why it uses the Accumulator the second time instead of X like the first. It seems to me that could be some kind of types thing or signs thing but i don´t know how to fix it, could you help me please? thank you very much.

I´m using STVD with the Discovery (STM8S105C6) and rkit RKit-STM8_2.26.09.0317 with the patch.

Regards

Alberto

Replies
Post Information Post
+1
0
-1
January 26, 2010 - 9:11am
Raisonance Support Team

Hi Alberto,

The compiler makes a lot of optimizations. Amonst them, it follows the constants, and in your second example, it knows that even if codigo_funcion_recibido is an int, it is actually loaded by a u8, so its high byte is 0. This is why the switch is made on A.
In the first exmple, the contents comes from the guarda_en_la_eeprom() function return, so it is an actual 16-bit value.

So I do not think there is an issue in here.

Try to run your program with disabled optimizations. If it works without opti but does not when optimized, then the code can be suspected. In such a case, please send us the full listing files (with/without opti) and if possible a whole project that shows up the problem, so that we can reproduce it in our labs.

Let us know what happens.
Regards,

+1
0
-1
January 26, 2010 - 9:52am
Guest

Hi bruno!

thank you for the response. If I disable the peephole optimization it works fine, the .lst looks similar to the first example:


 ; FUNCTION ?processing_required_action (BEGIN)
              ; SOURCE LINE # 28 
0000 5206                              SUB    SP,#006H
              ; SOURCE LINE # 34 
0002 5F                                CLRW   X
0003 B600       F                      LD     A,buffer_recepcion_trama + 01H
0005 97                                LD     XL,A
0006 1F01       F                      LDW    (001H,SP),X   ; [ codigo_funcion_recibido ]
              ; SOURCE LINE # 39 
0008 1E01       F                      LDW    X,(001H,SP)   ; [ codigo_funcion_recibido ]
000A A30000                            CPW    X,#00000H
000D 2755                              JREQ   ?CASE_0108
000F A30001                            CPW    X,#00001H
0012 2735                              JREQ   ?CASE_0109
0014 A30002                            CPW    X,#00002H
0017 2767                              JREQ   ?CASE_0110
0019 A30003                            CPW    X,#00003H
001C 2770                              JREQ   ?CASE_0111
001E A30004                            CPW    X,#00004H
0021 2603                              JRNE   ?LAB_0171
0023 CC0000     F                      JP     ?CASE_0112
0026         ?LAB_0171:
0026 A30005                            CPW    X,#00005H
0029 277F                              JREQ   ?CASE_0113
002B A30006                            CPW    X,#00006H
002E 277C                              JREQ   ?CASE_0114
0030 A30007                            CPW    X,#00007H
0033 2603                              JRNE   ?LAB_0172
0035 CC0000     F                      JP     ?CASE_0115
0038         ?LAB_0172:
0038 A30008                            CPW    X,#00008H
003B 2603                              JRNE   ?LAB_0173
003D CC0000     F                      JP     ?CASE_0116
0040         ?LAB_0173:
0040 A3000A                            CPW    X,#0000AH
0043 2602                              JRNE   ?LAB_0161
0045 207D                              JRA    ?CASE_0117
0047         ?LAB_0161:
0047 207D                              JRA    ?NXT_0604
0049         ?CASE_0109:

I don´t understand a lot of assembler but it seems to me that this sentences:

0008 4D                                TNZ    A
0009 2746                              JREQ   ?CASE_0108
000B 4A                                DEC    A
000C 2728                              JREQ   ?CASE_0109
000E A102                              CP     A,#002H

are wrong because at first A is compared with 0, secondly with 1 but it's decremented so in the third comparison it will be CP A,#001H or also DEC A, it isn´t?

I have a lot of enum/switch in the project but this strange thing only happens here and in other with the same switch.

tell me what you think please,

regards,
Alberto.

+1
0
-1
January 27, 2010 - 4:17pm
Raisonance Support Team

Hi Alberto!

I was able to reproduce your issue and indeed, it is a peephole bug.

Unfortunately, we are not able to provide a fix in the next weeks.

The workaround is to disable the peephole optimization, at least for that function. But a better
way will be to use char for enums (if possible). As there is a bug in the STVD interface (compiler
always uses int for enums), you must set it by yourself with the command-line option/pragma
ENUMTYPE(CHAR)

Stéphane

PS: enumtype(char) is a good choice anyhow, as generated code is (usually) smaller.

+1
0
-1
January 27, 2010 - 5:29pm
Guest

Ok thank you!!

I´m working with STVD, then I have the two bugs... I'll try ENUMTYPE(CHAR), thank you!!

regards

Alberto