Hot!XC8 1.44 allocates variables at same address causing endless loop

Page: 12 > Showing page 1 of 2
Author
PANAMATIK
Junior Member
  • Total Posts : 57
  • Reward points : 0
  • Joined: 2015/02/19 04:00:35
  • Location: 0
  • Status: offline
2017/11/24 12:29:54 (permalink)
0

XC8 1.44 allocates variables at same address causing endless loop

PIC16LF1519 XC8 1.44 MPLAB 8
I tracked down a compiler error, which causes an endless loop in my project. Sorry, I cannot simplify the situation, because it occures only in a broad context. As long as I don't know how this can happen, I don't have a good feeling. Perhaps there is the same problem somewhere else in the project and not yet spotted. I tried to attach the .lst file, but it is >500k. Here the code, which causes the problem. I don't see any problems.
 
void ShowMemoryDetailed()
 
{
uint8_t i,j,n,regs;
      regs=8;
       n= number from 0-9;
        for(i=0;i<regs;i++) // regs is allocated at x0026, will be overwritten by calling IsRegisterEmpty() !!!!!
        {
          j=n*regs+i; // register number 0-79
          if(IsRegisterEmpty(j)) // causes endless loop, because regs will be overwritten by call
          {
// something
          }
        }
}
extern uint8_t rombuf8[64];
 
 
 
extern void SST25Read(uint8_t page, uint16_t addr,uint8_t *data, uint8_t length);

uint8_t IsRegisterEmpty(uint8_t n)
{
  uint8_t i;
  uint16_t addr=HPTypeNr*0x500; //  uint8_t HPTypeNr has value 3
  addr+=(n/8)*64;
  n=(n % 8)*7;
  SST25Read(6,addr+n,rombuf8,7); // SST25Read@addr is allocated at x0026, same as ShowMemory@regs !!!!
  for(i=0;i<7;i++)
    if(rombuf8[i]!=0)
      return 0;
  return 1;
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Microchip Technology PIC Macro Assembler V1.44 build -154322312
Symbol Table                                                                                   Fri Nov 24 19:30:46 2017


 
 
 
 
 
 
 
         ShowMemoryDetailed@regs 0026    !!!! same address                 ShowUserFlags@i 0025                 StoreRegisters@pt 0042  
                  SST25Read@addr 0026   !!!! same address       __size_of_EnterStopwatch 0006          __size_of_CountStopwatch 0016  




 
post edited by PANAMATIK - 2017/11/25 09:49:12
#1

32 Replies Related Threads

    NKurzman
    A Guy on the Net
    • Total Posts : 15119
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/24 14:20:45 (permalink)
    +1 (1)
    Is this copied or typed? You are missing the opening brace of the first function.

    As far a reusing memory locations. The compiler is supposed to do that. If it can be sure it is safe to do.
    #2
    du00000001
    Just Some Member
    • Total Posts : 1429
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: online
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/24 14:50:38 (permalink)
    +1 (1)
    And where is the code of SST25Read() ?
    The overlap is with some variable 'addr' from that function.

    Most times the bug is in front of the keyboard.
    #3
    PANAMATIK
    Junior Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2015/02/19 04:00:35
    • Location: 0
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/24 15:23:16 (permalink)
    0
    Sorry, I forgot the opening brace. The code piece was copied, then modified.
     
    Yes, the compiler reuses memory addresses all the time, but in this case it is not allowed. It changes a variable in a calling function. Analyzing the call tree should prohibit, that local variables and function argument addresses will be reused in called functions.
     
    The function SST25Read() is not included because the error occurs already before the function is executed. The function argument SST25Read(...,uint16_t addr...) will be written to before calling the function.
     
    #4
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 1549
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/24 16:08:22 (permalink)
    0
    How are you getting your memory,
    malloc() or an array?
     
    Try using pointers, that is what C is about. (especially with memory)
     
    "but in this case it is not allowed"
    In all cases it is not allowed.
     
    If you have reserved some memory then it is occupied.
     
    Looks like you are

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #5
    PANAMATIK
    Junior Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2015/02/19 04:00:35
    • Location: 0
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/24 16:37:14 (permalink)
    0
    I don't use malloc(). In this case only local variables of standard types and function arguments are involved. I need to know the circumstances when this allocation problem can occur, because a programmer must rely on an error free compiler, otherwise he cant sleep.
    #6
    du00000001
    Just Some Member
    • Total Posts : 1429
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: online
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/24 17:09:26 (permalink)
    +1 (1)
    I'd like to see at least the prototype of SST25Read() !
    BTW: you're a bit low in variable names. Although this should not pose a problem, . . .

    Most times the bug is in front of the keyboard.
    #7
    PANAMATIK
    Junior Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2015/02/19 04:00:35
    • Location: 0
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/24 18:29:07 (permalink)
    +1 (1)
    Here it is. Nothing special IMO.

    #define CE2     LATEbits.LATE1 // O Chip enable SST25PF040 Flash
    void SST25Read(uint8_t page, uint16_t addr,uint8_t *data, uint8_t length)
    {
      CE2=0;
      SSPBUF=0x03; while(BF==0); // Read Command up to 25MHz at 2.7V, we use 4 MHz
      SSPBUF=page; while(BF==0);
      SSPBUF=addr>>8; while(BF==0);
      SSPBUF=addr; while(BF==0);
      
      while(length--)
      {
       SSPBUF=0; while(BF==0); // send 0 byte to read data, wait until buffer full
        *data++=SSPBUF;
      }
      CE2=1;
    }


    #8
    du00000001
    Just Some Member
    • Total Posts : 1429
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: online
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/25 02:40:32 (permalink)
    0
    Weird.
    Which optimization level do you currently apply? And did you try out other optimization levels?

    Most times the bug is in front of the keyboard.
    #9
    PANAMATIK
    Junior Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2015/02/19 04:00:35
    • Location: 0
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/25 07:33:55 (permalink)
    0
    Yes, weird. I use the PRO mode, optimized for size. Other optimization levels I could not try, they will not fit the project into the chip. The ShowMemory() Function will be called indirectly by function pointer, perhaps this is too much for the compiler? I contacted microchip support. Hopefully they can resolve the issue.
    #10
    DarioG
    farewell.
    • Total Posts : 53177
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/25 07:40:43 (permalink)
    0
    You can try setting for a larger PIC, just to test. Or use that option that uses a "dummy PIC" as large as possible, for testing purposes too (I don't remember the compiler option but it's in there)

    forget about me, subhumans. adieu

    #11
    du00000001
    Just Some Member
    • Total Posts : 1429
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: online
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/25 09:24:00 (permalink)
    +1 (1)
    A call via function pointer might be too much for the "omniscient code generation".
    What about using stack emulation instead of the compiled stack? Would that fit?

    Most times the bug is in front of the keyboard.
    #12
    PANAMATIK
    Junior Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2015/02/19 04:00:35
    • Location: 0
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/25 09:44:15 (permalink)
    0
    As I'm interested in finding the reason behind the behavior, I will try different compile options after the weekend. Indirect function pointer calls alone are not enough, because they are used many times in my project. It must be a special constellation.
    #13
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 1549
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/25 10:55:25 (permalink)
    0
    SSPBUF=page; while(BF==0);

    SPIxBUF=page; while(!SPIxSTAT.SPIRBF); SPIxBUF;
    (replace with 8bit regs)

    Replace your spi write lines with the above. The last statement is a dummy read.
    You've done it in some places but not all.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #14
    PANAMATIK
    Junior Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2015/02/19 04:00:35
    • Location: 0
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/25 14:49:52 (permalink)
    -1 (1)
    Thanks for trying to help.
     
    SPIxBUF=page; while(!SPIxSTAT.SPIRBF); SPIxBUF; is not PIC16F1519 code, but equivalent to SSPBUF=page; while(BF==0); except for the dummy read.
     
    Writing to SSPBUF in SPI master mode does also clear the BF bit of PIC16LF1519, thus the dummy read is not necessary, if SPI is only a transmitter.
     
    #15
    Gort2015
    Klaatu Barada Nikto
    • Total Posts : 1549
    • Reward points : 0
    • Joined: 2015/04/30 10:49:57
    • Location: 0
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/25 16:33:45 (permalink)
    +1 (1)
    You should always read data back.  You might get an error code from the other device telling you that the wrong data was received or even a busy flag.
     
    Plus it can cause buffer overflow with your data still in the buffer if you needed to write back to the slave a command.
     
    If you are getting coms. bugs then it's because you are not following the spi specs.

    MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
    https://www.youtube.com/watch?v=Iu1qa8N2ID0
    + ST:Continues, "What Ships are Made for", Q's back.
    #16
    PANAMATIK
    Junior Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2015/02/19 04:00:35
    • Location: 0
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/27 11:23:22 (permalink)
    -1 (1)
    >> If you are getting coms. bugs then it's because you are not following the spi specs.
     
    Can't agree. This is definitely a wrong deduction. 
    #17
    jack@kksound
    code tags!
    • Total Posts : 2116
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/27 13:26:52 (permalink)
    +2 (2)
    PANAMATIK
     
    Writing to SSPBUF in SPI master mode does also clear the BF bit of PIC16LF1519, thus the dummy read is not necessary, if SPI is only a transmitter.
     


    Really? It says that in the data sheet? I don't see that anywhere. Also there is an errata (errata sheet section 6.1) regarding the BF flag being set early, is that a possible issue here?
    #18
    PANAMATIK
    Junior Member
    • Total Posts : 57
    • Reward points : 0
    • Joined: 2015/02/19 04:00:35
    • Location: 0
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/28 09:13:02 (permalink)
    0
    This is not the issue discussed here, because the issue occurs at compile time not at run time. The SSTRead() function is called several thousand times per second and reads reliably the data via SPI from the external flash memory.
     
    The errata sheet problem does not occur in my constellation with 16 MHz PIC clock and 4 MHz SPI clock, because half SPI clock cycle is 125 ns, wheras a PIC instruction needs 250 ns at 16 MHz, which prevents a WCOL write collision. 
     
    I admit that the data sheet does not clearly state that the BF will be reset when SSPBUF is written, but it does.
     
     
    #19
    1and0
    Access is Denied
    • Total Posts : 7197
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: XC8 1.44 allocates variables at same address causing endless loop 2017/11/28 10:28:24 (permalink)
    +3 (3)
    PANAMATIK
    I admit that the data sheet does not clearly state that the BF will be reset when SSPBUF is written, but it does.

    I'm with Jack. The BF bit is cleared when SSPBUF is read, not when it's written.
     
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2017 APG vNext Commercial Version 4.5