Page 1 of 1

LCC v1.45 (Jun 19 2013 19:45:59) generates bad code

Posted: Fri 2013-08-30 6:29
by victor
LCC shipped with VSIDE 2.20 fails to compile the following simple, legit C code. The code is used to implement a ring buffer.

Code: Select all

#include "vsos.h"

u_int16* t;

int main()
{
	register char c;
	c = *t++;
	t = (u_int16 *)((u_int16)t & 0xff);
	return c;
}
Error message:
ERROR 0: command encoding error: Unsuitable 1. source register for alu operation

Code: Select all

	.sect code,main	// 16 words
	.export _main
_main:
	ldc 255,A0	// 255 -> 1 -> ET6
	ldx (I6)+1,NULL	// I6 += 1
	stx A1,(I6) ; sty I0,(I6)+1
	ldc _t,I0	// ET0
	stx I1,(I6) ; sty I2,(I6)
// = *t++;
	ldx (I0),I1/*V*/	// ET0 ==> 't'
	mv I1,I2	// 't' -> ATb
	ldx (I2)+1,NULL	// ATb += 1
	stx I2,(I0)/*V*/	// ATb ==> ET0
	ldx (I1),A1/*V*/	// 't' ==> 'c'
// 	t = (u_int16 *)((u_int16)t & 0xff);
//    THE FOLLOWING "AND" IS WRONG
	and I2,A0,A0	// ATb and ET6 -> ET7
	stx A0,(I0)/*V*/ ; ldy (I6),I2	// ET7 ==> ET0
// 	return c;
	ldx (I6)-1,I1 ; add A1,NULL,A0	// 'c' returns via A0
	ldy (I6),I0 ; ldx (I6)-1,A1
	jr
	nop

Re: LCC v1.45 (Jun 19 2013 19:45:59) generates bad code

Posted: Fri 2013-08-30 11:47
by Panu
Hi!

Ok, we will check it. Thanks for the exact error reporting.

Generally, when there is a bug in the LCC compiler or it fails to compile, it is often useful to switch to the old VCC compiler momentarily to compare the results of both compilers, especially in cases where cpp produces the "Lcc.exe has stopped working" error before you get any information what has gone wrong.

To be able to compile with VCC, depending on the version of Windows, VSIDE and/or the project must be in folders whose names don't have spaces or special characters. For example, having VSIDE in C:\VSIDE and the solutions in C:\VSIDE\SOLUTIONS always works. You can then change "lcc" to "vcc" in the Project Options -> Tools menu.

-Panu

Re: LCC v1.45 (Jun 19 2013 19:45:59) generates bad code

Posted: Mon 2013-09-02 16:45
by Panu
Hi, Victor!

I suspect that your code is at least a bit synthetic (made to show the error) since it would expect the ring buffer to start at address zero (which would be a bad idea under VSOS since the beginning of memory is kept containing zeroes for trapping zero pointer calls via uninitialized objects).

Anyway, Lasse is looking at the compiler, especially the register reduction algorithm.

As far as ring buffers are considered, I can only offer some very simple ideas. (I understand that you don't have problems with ring buffers, but someone else might arrive at this posting by searching by that keyword.) Pasi usually writes buffer handling so that he handles several words at once and splits the operation by checking how many words he can process until the end of buffer occurs. I guess sometimes it's a good idea. In VSDSP obviously there are hardware mechanisms for handling modulo buffers, they are of course useful for running filters (convolutions etc) but maybe the setup is too expensive for single word operation.

I suppose you've noted of course the __align keyword which places the variable at an address which is divisible by as many bits as the buffer's size suggests. It also often helps.

As for the compiler bug, I'm sure Lasse will have a follow-up soon, to one way or the other.. :)

-Panu

Re: LCC v1.45 (Jun 19 2013 19:45:59) generates bad code

Posted: Mon 2013-09-02 19:01
by Lasse
I think I found the problem. We will include the compiler fix in the next VSIDE release. Thank you for letting us know!

Also, if you need to compile it now, you can work around e.g. like this

Code: Select all

t = (u_int16 *)(*(u_int16*)&t & 0xff);