Jump to content
Reynard

Pointer Not Incrementing

Recommended Posts

Reynard    0

I am running a simple circular stack with a head and tail pointer.

 

typedef struct {
uint8 type;
union {
 uint8 bits8;
 uint16 bits16;
} size;
} EVENT_DATA;
#define EVENT_QUEUE_LEN  20
struct {
EVENT_DATA queue[EVENT_QUEUE_LEN];
EVENT_DATA *head;
EVENT_DATA *tail;
} evt;

void Event_Init(void)
{
evt.head = evt.queue;
0D2A  0E00	   MOVLW HIGH(gbl_evt+D'0')
0D2C  6E42	   MOVWF gbl_evt+D'61'
0D2E  0E05	   MOVLW LOW(gbl_evt+D'0')
0D30  6E41	   MOVWF gbl_evt+D'60'
++evt.head;
0D32  0E03	   MOVLW 0x03
0D34  2605	   ADDWF gbl_evt, F
evt.head += 1;
0D36  0E03	   MOVLW 0x03
0D38  2641	   ADDWF gbl_evt+D'60', F
0D3A  B0D8	   BTFSC STATUS,C
0D3C  2A42	   INCF gbl_evt+D'61', F

 

If I use the ++evt.head the head pointer does not get incremented by 1 element length (3 bytes in my case).

 

Using evt.head += 1 all is OK.

 

An element of 1 byte works but not with anything greater.

 

Something to do with the pointer being inside the structure.

 

Cheers

 

Reynard

 

Using 7.11, XP Pro, PIC18F45K22

Edited by Reynard

Share this post


Link to post
Share on other sites
JorgeF    0

Hi

 

It looks like a precedence issue.

"evt" is being incremented instead of "evt.head".

 

Have you tried with a postfix increment ( evt.head++; ) instead?

 

 

 

Best regards

Jorge

Edited by JorgeF

Share this post


Link to post
Share on other sites
Reynard    0

Hi Jorge,

 

Been there done that.

 

Doesn't work for decrement either. :(

 

Cheers

 

Reynard

Edited by Reynard

Share this post


Link to post
Share on other sites
JorgeF    0

Hi

 

I made some more testing on this subject and got to the conclusion that it really is a bug in pointer processing.

The "++" and "--" operators are correctly compiled with multibyte variables, they only fail with pointers.

 

Here are the results of my testing derived from Reynard's sample

;/////////////////////////////////////////////////////////////////////////////////
;// Code Generator: BoostC Compiler - http://www.sourceboost.com
;// Version	   : 7.11
;// License Type  : Pro License
;// Limitations   : PIC18 max code size:Unlimited, max RAM banks:Unlimited
;/////////////////////////////////////////////////////////////////////////////////

#include <system.h>

typedef unsigned char uint8;
typedef unsigned short uint16;

typedef struct
{
unsigned char type;
union
	{
	uint8 bits8;
	uint16 bits16;
	} size;
}EVENT_DATA;


#define EVENT_QUEUE_LEN  20

struct
{
EVENT_DATA queue[EVENT_QUEUE_LEN];
EVENT_DATA *head;
EVENT_DATA *tail;
}evt;


typedef struct
{
unsigned char dummy[8];
uint8 ValX;
uint16 ValY;
} COORDS;

struct
{
char text[16];
COORDS *x;
COORDS *y;
} test_ptr;

COORDS test2;

void Event_Init(void)

{
	evt.head = evt.queue;
0008  0E00		  MOVLW HIGH(gbl_evt+D'0')
000A  6E3E		  MOVWF gbl_evt+D'61'
000C  0E01		  MOVLW LOW(gbl_evt+D'0')
000E  6E3D		  MOVWF gbl_evt+D'60'

	++evt.head;
0010  0E03		  MOVLW 0x03
0012  2601		  ADDWF gbl_evt, F

	evt.head += 1;
0014  0E03		  MOVLW 0x03
0016  263D		  ADDWF gbl_evt+D'60', F
0018  B0D8		  BTFSC STATUS,C
001A  2A3E		  INCF gbl_evt+D'61', F

	evt.head++;
001C  0E03		  MOVLW 0x03
001E  2601		  ADDWF gbl_evt, F

	++(evt.head);
0020  0E03		  MOVLW 0x03
0022  2601		  ADDWF gbl_evt, F

	(evt.head)++;
0024  0E03		  MOVLW 0x03
0026  2601		  ADDWF gbl_evt, F


	++test_ptr.x;
0028  0E0B		  MOVLW 0x0B
002A  2641		  ADDWF gbl_test_ptr, F

	++test_ptr.y;
002C  0E0B		  MOVLW 0x0B
002E  2641		  ADDWF gbl_test_ptr, F

	test_ptr.x++;
0030  0E0B		  MOVLW 0x0B
0032  2641		  ADDWF gbl_test_ptr, F

	test_ptr.y++;
0034  0E0B		  MOVLW 0x0B
0036  2641		  ADDWF gbl_test_ptr, F

	++(test_ptr.x);
0038  0E0B		  MOVLW 0x0B
003A  2641		  ADDWF gbl_test_ptr, F

	++(test_ptr.y);
003C  0E0B		  MOVLW 0x0B
003E  2641		  ADDWF gbl_test_ptr, F

	test_ptr.x+=1;
0040  0E0B		  MOVLW 0x0B
0042  2651		  ADDWF gbl_test_ptr+D'16', F
0044  B0D8		  BTFSC STATUS,C
0046  2A52		  INCF gbl_test_ptr+D'17', F

	test_ptr.y+=1;
0048  0E0B		  MOVLW 0x0B
004A  2653		  ADDWF gbl_test_ptr+D'18', F
004C  B0D8		  BTFSC STATUS,C
004E  2A54		  INCF gbl_test_ptr+D'19', F


	++test2.ValX;
0050  2A5D		  INCF gbl_test2+D'8', F

	++test2.ValY;
0052  4A5E		  INFSNZ gbl_test2+D'9', F
0054  2A5F		  INCF gbl_test2+D'10', F

	test2.ValX++;
0056  2A5D		  INCF gbl_test2+D'8', F

	test2.ValY++;
0058  4A5E		  INFSNZ gbl_test2+D'9', F
005A  2A5F		  INCF gbl_test2+D'10', F

	++(test2.ValX);
005C  2A5D		  INCF gbl_test2+D'8', F

	++(test2.ValY);
005E  4A5E		  INFSNZ gbl_test2+D'9', F
0060  2A5F		  INCF gbl_test2+D'10', F

	test2.ValX+=1;
0062  2A5D		  INCF gbl_test2+D'8', F

	test2.ValY+=1;
0064  4A5E		  INFSNZ gbl_test2+D'9', F
0066  2A5F		  INCF gbl_test2+D'10', F


}
0068  0012		  RETURN


void main()

{

	Event_Init();
006A  EC04F000	  CALL Event_Init_00000


}
006E  0012		  RETURN


////////////////////////////////////////
// Code with no source :-)
////////////////////////////////////////
0000  EF38F000	  GOTO	_startup


0070			_startup
0070  EF35F000	  GOTO	main

 

 

Best regards

Jorge

Edited by JorgeF

Share this post


Link to post
Share on other sites
Pavel    0

Not fixed in 7.12 rc1 :(

 

Fixed. A new rc (or final release) that includes this fix will be available soon.

 

Regards,

Pavel

 

Share this post


Link to post
Share on other sites
Reynard    0

Hi Pavel,

 

I still have the same problem in the released 7.12

 

++evt.head;
1E58  0E03	   MOVLW 0x03
1E5A  273C	   ADDWF gbl_30_evt+D'60', F, 1
evt.head += 1;
1E5C  0E03	   MOVLW 0x03
1E5E  273C	   ADDWF gbl_30_evt+D'60', F, 1
1E60  B0D8	   BTFSC STATUS,C
1E62  2B3D	   INCF gbl_30_evt+D'61', F, 1

 

Doesn't seem to know whether the pointer is 1 or 2 bytes long.

 

Cheers

 

Reynard

Share this post


Link to post
Share on other sites
Pavel    0

I think I see the problem. We fixed the offset part but missed the increment size. Will investigate further. Thanks for being persistent.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
davidb    0

Hi,

 

Not sure if this is related but the following code was extracted from a project and greatly simplified to show the problem with pointers not incrementing using SourceBoost V7.20.

The structures are normally much larger than these. The comments in the code should explain the problem.

#include <system.h>

typedef unsigned char uint8_t;

/* A single receive buffer data structure */
typedef struct _usartRxBuf
{
	uint8_t uData[8];
	uint8_t *pByte;
} usartRxBuf_t;
/* USART module data including all buffers and varibles */
typedef struct _usartData
{
	usartRxBuf_t sRxBuf[1];
	uint8_t      uRxWr;      /* Buffer write index. */
} sUsartData_t;
static volatile uint8_t x;
static volatile sUsartData_t sUsart;	
void main (void)
{
	/* Select buffer to use */	
	sUsart.uRxWr = 0;	
	/* Point to start of first buffer */	
	sUsart.sRxBuf[sUsart.uRxWr].pByte = sUsart.sRxBuf[sUsart.uRxWr].uData;	
	for (x = 1; x < 9; x++)
	{
		/* This should be valid but doesn't work - pointer does not increment
		* so data is overwritten in the first buffer location
		* Doesn't work when the buffer array is present (also would normally be sRxBuf[2])
		* Change the sRxBuf[1] array to just sRxBuf and it works
		*/			
		*sUsart.sRxBuf[sUsart.uRxWr].pByte++ = x;
	}
	for (x = 1; x < 9; x++)
	{
		/* This works - pointer increments correctly */				
		*sUsart.sRxBuf[sUsart.uRxWr].pByte = x;
		sUsart.sRxBuf[sUsart.uRxWr].pByte++;
	}	
	reset ();
}

Another problem:

 

The following code from the example above sets the pointer to the start of the array:

sUsart.sRxBuf[sUsart.uRxWr].pByte = sUsart.sRxBuf[sUsart.uRxWr].uData;

This compiles O.K. as does:

sUsart.sRxBuf[sUsart.uRxWr].pByte = (sUsart.sRxBuf[sUsart.uRxWr].uData);

As does this:

sUsart.sRxBuf[sUsart.uRxWr].pByte = &sUsart.sRxBuf[sUsart.uRxWr].uData;

But this fails to compile:

sUsart.sRxBuf[sUsart.uRxWr].pByte = &(sUsart.sRxBuf[sUsart.uRxWr].uData);

As far as I can see this is all valid C code and compiles on C18. It appears to be related to the structs since &(ptr) will compile O.K.

 

Cheers

 

davidb

 

SourceBoost V7.20 (Not sure if this was actually released or if it is still RC status)

Win XP Pro

PIC18F87K22

Share this post


Link to post
Share on other sites
davidb    0

Hi,

 

Although not a problem that can't be overcome the bugs demonstrated in my above post are still extant in V7.21

 

Regards

 

davidb

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoticons maximum are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×