Menu

#1190 hc08 bug

closed-rejected
5
2015-02-20
2006-08-29
Anonymous
No

I did try to log in. I can't because MSExplorer is in
use in my company.

BUG DESCRIPTION:

version:
SDCC :
mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/d
s400/hc08 2.6.0 #4309 (Jul 28 2006) (MINGW32)
submiter:
Kosta Demirev kdemirev@yahoo.com
command line:
sdcc -c -I. -I../Common/include -I../include -
I../../Source/include -ICODE --data-loc 0x0070
--stack-loc 0x086f --code-loc 0x1860 --
iram-size 0x0780 --stack-size 0x0080
--code-size 0xe7a0 -Dcode=0x1860 -
Dstack=0x086f -Ddata=0x0070 -Dvect=0xffcc
-DSDCC_HCS08 --less-pedantic --stack-
auto --no-peep --int-long-reent --float-reent
-mhc08 Sources/Start08.c
Description:

C code :
Start08.c line 134:
// BUG IN SDCC for HC08
(*((_PFunc*)
startupData.main))(); /* I am not sure SDCC is
correct */
// BUG IN SDCC for HC08
is wrongfully translated to:
Start08.asm lines 227 to 248
;Sources/Start08.c:134: (*
((_PFunc*)startupData.main))(); /* I am not sure SDCC
is correct */
lda _startupData
sta 1,s
lda (_startupData
+ 1)
sta 2,s
lda 1,s
ldx 2,s
psha
pulh
lda ,x
aix #1
sta 1,s
lda ,x
sta 2,s
BSR 00104$
bra 00103$
00104$:
lda 2,s
psha
lda 2,s
psha
rts
00103$:
must be:
Start08.asm lines 239 to 241
sta 2,s
BRA 00104$
bra 00103$

Code:

file Start08.c:

=================================
/******************************************************
************************
FILE : start08.c
PURPOSE : 68HC08 standard startup code
LANGUAGE : ANSI-C / INLINE ASSEMBLER
-----------------------------------------------------
-----------------------
HISTORY
22 oct 93 Created.

*******************************************************
***********************/

#include <string.h>
#include <stddef.h>
#include "PE_Types.h"
#include "FreeRTOS.h"

/******************************************************
****************/
extern void vMain( void);

#define INIT_SP_FROM_STARTUP_DESC()
\ _asm ldx (_startupData+stackOffset_off)
_endasm;\ _asm txs _endasm;

typedef unsigned char INT8U;
typedef unsigned short INT16U;

typedef struct _Range {
INT16U size;
INT8U *beg;
} _Range ;

typedef struct _Copy {
INT16U size;
INT8U * src;
INT8U * dst;
}_Copy;

typedef void (*_PFunc)(void);

#define stackOffset_off 2 /*
startupData.stackOffset - startupData */
#define main_off 0 /*
startupData.main - startupData */

struct _tagStartup {
_PFunc main;
INT16U stackOffset;
_Range *pZeroOut;
_Copy *toCopyDownBeg;
}; /* startupData;*/

/* read-only: startupData is allocated in ROM */
extern _Copy * xinit;
extern _Range * xzero;
extern _PFunc main_init;

static void wrap_asm( void) __naked
{
_asm
_xinit:
.dw l_XINIT ; size of GLOBAL vars
.dw s_XINIT ; source
.dw s_XISEG ; destination
_endasm;
_asm
_xzero:
.dw l_XSEG ; size of BBS vars
.dw s_XSEG ; destination
_endasm;
_asm
_main_init:
.dw s_GSINIT
_endasm;
}

__code struct _tagStartup startupData = {
{main_init},
{stack-2},
{xzero},
{xinit}
};

static void Init(void)
{
/* purpose: 1) zero out RAM-areas where data is
allocated
2) init run-time data
3) copy initialization data from ROM
to RAM
*/
// BBS zeroing
memset( (char*)startupData.pZeroOut->beg,
0x00,
startupData.pZeroOut->size);

/* copy down */
// GLOBAL var initialization
memcpy( (char*)startupData.toCopyDownBeg->dst,
(char*)startupData.toCopyDownBeg->src,
startupData.toCopyDownBeg-
>size);

}

//#define ASM

#ifdef ASM
void Startup (void) __naked
#else
void Startup (void)
#endif
{
/* To set in the linker parameter file: 'VECTOR 0
_Startup' */
/* purpose: 1) initialize the stack
2) initialize run-time, ...
initialize the RAM, copy down init
dat etc (Init)
3) call main;
called from: _PRESTART-code generated by the Linker
*/

__DI();
/* initialize the stack pointer */
INIT_SP_FROM_STARTUP_DESC();
Init();

#ifdef ASM
_asm
ldx (_startupData + main_off)
; ldhx 0,x ; not a mnemonic in ASxxx,
follows code
.dw 0x9eae ; code for "ldhx ,x"
lda 1,x
psha
lda 0,x
psha
rts
_endasm;
#else
// BUG IN SDCC for HC08
(*((_PFunc*)startupData.main))(); /* I am not
sure SDCC is correct */
// BUG IN SDCC for HC08
#endif
}
=======================================================
=================================
file Start08.asm
=======================================================
=================================
;------------------------------------------------------
--
; File Created by SDCC : FreeWare ANSI-C Compiler
; Version 2.6.0 #4309 (Jul 28 2006)
; This file generated Tue Aug 29 09:30:54 2006
;------------------------------------------------------
--
.module Sources_Start08
.optsdcc -mhc08

.area HOME (CODE)
.area GSINIT0 (CODE)
.area GSINIT (CODE)
.area GSFINAL (CODE)
.area CSEG (CODE)
.area XINIT
.area CONST (CODE)
.area DSEG
.area OSEG (OVR)
.area BSEG
.area XSEG
.area XISEG
;------------------------------------------------------
--
; Public variables in this module
;------------------------------------------------------
--
.globl _startupData
.globl _Startup
;------------------------------------------------------
--
; ram data
;------------------------------------------------------
--
.area DSEG
;------------------------------------------------------
--
; overlayable items in ram
;------------------------------------------------------
--
.area OSEG (OVR)
;------------------------------------------------------
--
; external initialized ram data
;------------------------------------------------------
--
.area XISEG
;------------------------------------------------------
--
; extended address mode data
;------------------------------------------------------
--
.area XSEG
;------------------------------------------------------
--
; global & static initialisations
;------------------------------------------------------
--
.area HOME (CODE)
.area GSINIT (CODE)
.area GSFINAL (CODE)
.area GSINIT (CODE)
;------------------------------------------------------
--
; Home
;------------------------------------------------------
--
.area HOME (CODE)
.area CSEG (CODE)
;------------------------------------------------------
--
; code
;------------------------------------------------------
--
.area CSEG (CODE)
;------------------------------------------------------
------
;Allocation info for local variables in
function 'wrap_asm'
;------------------------------------------------------
------
;------------------------------------------------------
------
;Sources/Start08.c:54: static void wrap_asm( void)
__naked
; -----------------------------------------
; function wrap_asm
; -----------------------------------------
_wrap_asm:
; naked function: no prologue.
;Sources/Start08.c:61: _endasm;
_xinit:
.dw l_XINIT ; size of GLOBAL vars
.dw s_XINIT ; source
.dw s_XISEG ; destination
;Sources/Start08.c:66: _endasm;
_xzero:
.dw l_XSEG ; size of BBS vars
.dw s_XSEG ; destination
;Sources/Start08.c:70: _endasm;
_main_init:
.dw s_GSINIT
00101$:
; naked function: no epilogue.
;------------------------------------------------------
------
;Allocation info for local variables in function 'Init'
;------------------------------------------------------
------
;sloc0 Allocated to stack -
offset -2
;sloc1 Allocated to stack -
offset -4
;sloc2 Allocated to stack -
offset -6
;------------------------------------------------------
------
;Sources/Start08.c:81: static void Init(void)
; -----------------------------------------
; function Init
; -----------------------------------------
_Init:
ais #-6
;Sources/Start08.c:90: startupData.pZeroOut->size);
lda (_startupData + 0x0004)
sta 5,s
lda ((_startupData + 0x0004) + 1)
sta 6,s
lda 5,s
ldx 6,s
psha
pulh
lda ,x
aix #1
sta 5,s
lda ,x
sta 6,s
;Sources/Start08.c:88: memset( (char*)
startupData.pZeroOut->beg,
lda (_startupData + 0x0004)
sta 3,s
lda ((_startupData + 0x0004) + 1)
sta 4,s
lda 4,s
add #0x02
sta 4,s
bcc 00103$
inc 3,s
00103$:
lda 3,s
ldx 4,s
psha
pulh
lda ,x
aix #1
sta 3,s
lda ,x
sta 4,s
lda 6,s
psha
lda 6,s
psha
clra
psha
ldx 6,s
lda 7,s
jsr _memset
ais #3
;Sources/Start08.c:96: startupData.toCopyDownBeg-
>size);
lda (_startupData + 0x0006)
sta 3,s
lda ((_startupData + 0x0006) + 1)
sta 4,s
lda 3,s
ldx 4,s
psha
pulh
lda ,x
aix #1
sta 3,s
lda ,x
sta 4,s
;Sources/Start08.c:95: (char*)
startupData.toCopyDownBeg->src,
lda (_startupData + 0x0006)
sta 5,s
lda ((_startupData + 0x0006) + 1)
sta 6,s
lda 6,s
add #0x02
sta 6,s
bcc 00104$
inc 5,s
00104$:
lda 5,s
ldx 6,s
psha
pulh
lda ,x
aix #1
sta 5,s
lda ,x
sta 6,s
;Sources/Start08.c:94: memcpy( (char*)
startupData.toCopyDownBeg->dst,
lda (_startupData + 0x0006)
sta 1,s
lda ((_startupData + 0x0006) + 1)
sta 2,s
lda 2,s
add #0x04
sta 2,s
bcc 00105$
inc 1,s
00105$:
lda 1,s
ldx 2,s
psha
pulh
lda ,x
aix #1
sta 1,s
lda ,x
sta 2,s
lda 4,s
psha
lda 4,s
psha
lda 8,s
psha
lda 8,s
psha
ldx 5,s
lda 6,s
jsr _memcpy
ais #4
00101$:
ais #6
rts
;------------------------------------------------------
------
;Allocation info for local variables in
function 'Startup'
;------------------------------------------------------
------
;sloc0 Allocated to stack -
offset -2
;------------------------------------------------------
------
;Sources/Start08.c:105: void Startup (void)
; -----------------------------------------
; function Startup
; -----------------------------------------
_Startup:
ais #-2
;Sources/Start08.c:116: __DI();
sei
;Sources/Start08.c:118: INIT_SP_FROM_STARTUP_DESC();
ldx (_startupData+2)
txs
;Sources/Start08.c:119: Init();
jsr _Init
;Sources/Start08.c:134: (*((_PFunc*)startupData.main))
(); /* I am not sure SDCC is correct */
lda _startupData
sta 1,s
lda (_startupData + 1)
sta 2,s
lda 1,s
ldx 2,s
psha
pulh
lda ,x
aix #1
sta 1,s
lda ,x
sta 2,s
bsr 00104$
bra 00103$
00104$:
lda 2,s
psha
lda 2,s
psha
rts
00103$:
00101$:
ais #2
rts
.area CSEG (CODE)
.area CONST (CODE)
_startupData:
.dw _main_init
.dw #0x086D
.dw _xzero
.dw _xinit
.area XINIT
=======================================================
=================================

Kosta Demirev

Discussion

  • Maarten Brock

    Maarten Brock - 2006-08-29

    Logged In: YES
    user_id=888171

    Kosta,

    I'm not fluent in hc08 asm but I think your analysis is
    wrong. It needs the BSR in that place. My guess is that
    the compiler has trouble interpreting the cast and
    dereference. I think it reads the first two bytes of the
    function code and interprets that as a function pointer.

    Since startupData.main is already a proper function
    pointer simply remove the cast and dereference and I think
    all will work well.

    Maarten

     
  • Maarten Brock

    Maarten Brock - 2009-10-09

    Even GCC turns this into code that generates a Segmentation Fault at runtime due to the double dereference of the function pointer.

     
  • Maarten Brock

    Maarten Brock - 2009-10-09
    • milestone: --> non_bugs
    • assigned_to: nobody --> maartenbrock
    • status: open --> closed-rejected
     

Log in to post a comment.