#1470 PIC14, Static variables problem

open
PIC14
5
2014-07-19
2008-07-22
No

Using SDCC for PIC16F877 microcontroller.
There is a bug with static variables.
In generated assembly code, the 'main' procedure
does not initialize static variables.

Command:
sdcc -S -mpic14 -p16f877a main.c

SDCC version:
msc51/gbz80/z80/avr/ds390/pic16/pic14/TININative/
xa51/ds400/hc08 2.0.8 #5117 (Mar 23 2008) (MINGW32)

E-mail: stf_dimitrov@mail.bg

Discussion

  • Raphael Neider

    Raphael Neider - 2008-07-26

    Logged In: YES
    user_id=1115835
    Originator: NO

    main() never initializes static variables, __sdcc_gsinit_startup() from the library module idata.[co] does, if the project links against the libsdcc.lib (the default behaviour without -c or -S).

    Could you post more information about the problem? Do you observe runtime errors? Can you upload a failing source file? If there is a problem, just reopen this item and post more info.

    Kind regards,
    Raphael

     
  • Raphael Neider

    Raphael Neider - 2008-07-26
    • labels: 101552 --> pic14 target
    • milestone: --> 100455
    • status: open --> closed-invalid
     
  • Raphael Neider

    Raphael Neider - 2008-07-26
    • milestone: 100455 -->
    • assigned_to: nobody --> tecodev
    • status: closed-invalid --> open
     
  • Raphael Neider

    Raphael Neider - 2008-07-26

    Logged In: YES
    user_id=1115835
    Originator: NO

    Oh, I realized your problem: Initializers for static variables are not emitted in the .asm file at all... This needs to be fixed. Thanks for the report!

    Workaround: Use initialized global variables for now:
    int foo(void) { static int bar = 42; return bar++; }
    becomes
    /*static*/ int __foo_bar = 42;
    int foo(void) { return __foo_bar++; }

     
  • Stefan Dimitrov

    Stefan Dimitrov - 2008-07-28

    Static var. problem. source code.

     
  • Stefan Dimitrov

    Stefan Dimitrov - 2008-07-28

    Logged In: YES
    user_id=2155117
    Originator: YES

    I don`t think this will be very usefull, but just in case here is the full source code of my program + Makefile used to generate the objects.

    As you will see, if I use 'dpos' as static variable, my 'display()' procedure does not work.

    File Added: static.rar

     
  • Stefan Dimitrov

    Stefan Dimitrov - 2008-07-28

    Static var. problem. source code.

     
  • Stefan Dimitrov

    Stefan Dimitrov - 2008-07-28

    Logged In: YES
    user_id=2155117
    Originator: YES

    I don`t think this will be very usefull, but just in case here is the full source code of my program + Makefile used to generate the objects.

    As you will see, if I use 'dpos' as static variable, my 'display()' procedure does not work.

    File Added: static.rar

     
  • jpa

    jpa - 2010-06-11

    I think I have the same problem. Global variables are initialized correctly, but local static variables are not. The local static variables are also marked in assembler output as udata.

    See the attached file sdcc-test.c, which I compiled using:
    sdcc -mpic14 -p16f689 sdcc-test.c

    I'm using:
    SDCC : mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08 2.9.0 #5416 (Jan 10 2010) (UNIX)

     
  • jpa

    jpa - 2010-06-11

    Hm, file attaching failed, here it is as text:

    #include <pic16f689.h>

    static char global_test = 10; /* This is initialized correctly */
    void foo() {
    static char local_test = 20; /* This is not initialized at all */
    PORTA = global_test;
    PORTC = local_test;
    }

    void main() {
    TRISA = 0;
    TRISC = 0;
    foo();
    while(1);
    }

     
  • jpa

    jpa - 2010-06-16

    Just a few things I found out after trying to debug this:

    Local static initializers work in the PIC16 port, but not in PIC14.

    I think the relevant place is in SDCCast.c:1322 (function gatherAutoInit) that has a special case for PIC16 port:
    /* if we are PIC16 port,
    * and this is a static,
    * and have initial value,
    * and not S_CODE, don't emit in gs segment,
    * but allow glue.c:pic16emitRegularMap to put symbol
    * in idata section */
    if(TARGET_IS_PIC16 &&
    IS_STATIC (sym->etype) && sym->ival
    && SPEC_SCLS(sym->etype) != S_CODE) {
    SPEC_SCLS (sym->etype) = S_DATA;
    continue;
    }

    Later in this function sym->ival is set to NULL. This causes that in pic/glue.c:1107 (emitSymbolSet) the variable is emitted as udata instead of idata.

    PIC16's version of ralloc.c is quite different from PIC14's version. Therefore I'm not sure whether PIC14 port needs a special case or not. This patch seems to fix the problem, but doubt remains whether is the correct solution or not:

    --- SDCCast.c (revision 5850)
    +++ SDCCast.c (working copy)
    @@ -1325,7 +1325,7 @@
    * and not S_CODE, don't emit in gs segment,
    * but allow glue.c:pic16emitRegularMap to put symbol
    * in idata section */
    - if(TARGET_IS_PIC16 &&
    + if((TARGET_IS_PIC16 || TARGET_IS_PIC) &&
    IS_STATIC (sym->etype) && sym->ival
    && SPEC_SCLS(sym->etype) != S_CODE) {
    SPEC_SCLS (sym->etype) = S_DATA;

     
  • Borut Ražem

    Borut Ražem - 2013-02-21

    Local static initializers for PIC14 target are handeled in file glue.c, function pic14printLocals(). Probably it would be enough to replace udata / res instructions with idata / db instructions?

    Borut

     
  • Borut Ražem

    Borut Ražem - 2013-02-21
    • summary: PIC, Static variables problem --> PIC14, Static variables problem
     
  • Philipp Klaus Krause

    • Category: --> PIC14
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks