post increment in XC

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
jai
Member
Posts: 15
Joined: Wed Jun 15, 2011 9:20 am

post increment in XC

Post by jai »

I was checking multiple channel ends in an XS1-L1, in XDE Version: 11.2.0 (build 1871).

Code: Select all

void sendThem(chanend s1, chanend s2, chanend s3)
{
	char item = 0;
	while(1)
	{
		s1 <: (char) (item++);
		s2 <: (char) (item++);
		s3 <: (char) (item++);
		if (item > 25)
			break;
	}
}
Here problem is that 'item' is not getting incremented. What might be the reason behind this issue?

PS:
If i use item = item + 1; it works fine.
Or if i use % operator like this --> s1 <: (char)(item++%5); it works.
Optimization level is 0 in all the cases.


richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

jai wrote:Here problem is that 'item' is not getting incremented. What might be the reason behind this issue?
This looks like a compiler bug. The compiler isn't emitting code to increment the variable if the post increment is the operand of a cast expression. The following example exhibits the same problem.

Code: Select all

int f(int x) {
  (char)x++;
  return x;
}
This should be easy to fix in future releases. In the meantime you can work around this problem by doing performing increment in a separate statement after the cast. Alternatively the following should work:

Code: Select all

s1 <: (char) (0 + item++);
s2 <: (char) (0 + item++);
s3 <: (char) (0 + item++);
User avatar
jai
Member
Posts: 15
Joined: Wed Jun 15, 2011 9:20 am

Post by jai »

Thank you richard.
this information helps.
(it saved a part of debug time)