[NXC][Bug] Different type comparisons

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC][Bug] Different type comparisons

Post by afanofosc »

muntoo wrote:This bug was torturing me in NXTFileManager. :)
This is definitely a bug. But please, please, please stop mixing signed and unsigned types willy-nilly. It is nonsense. Only use an unsigned type when it is absolutely necessary. It is only absolutely necessary when you are dealing with values > MaxInt and generally that only involves things like CurrentTick(). Mixing signed and unsigned types in a ?: expression does not make any sense (and, apparently, is currently broken).

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC][Bug] Different type comparisons

Post by afanofosc »

I think that I have a fix in place for this defect. Basically I added a call to CastToLHSType after the two CommaExpression calls in the code block that handles ?: expressions. If the lhs type is defined and not equal to the statement type of the last CommaExpression then the value in the register used last is moved into the register for the lhs type (signed, unsigned, or float).

With this change the ASM code without optimizations looks like this:

Code: Select all

	set __D0main, 441
	mov __main_7qG2_ui_7qG2_000, __D0main
	set __D0main, 64
	mov __main_7qG2_l_7qG2_000, __D0main
	mov __D0main, __main_7qG2_l_7qG2_000
	mov __signed_stack_001main, __D0main
	mov __DU0main, __main_7qG2_ui_7qG2_000
	cmp 0, __D0main, __signed_stack_001main, __DU0main
	brtst 4, __ASM_Label_610, __D0main
	mov __D0main, __main_7qG2_l_7qG2_000
	mov __DU0main, __D0main ; this line of code is generated by CastToLHSType
	jmp __ASM_Label_611
__ASM_Label_610:
	mov __DU0main, __main_7qG2_ui_7qG2_000
__ASM_Label_611:
	mov __main_7qG2_len_7qG2_000, __DU0main
At optimization level 3 it looks like this:

Code: Select all

	set __main_7qG2_ui_7qG2_000, 441
	set __main_7qG2_l_7qG2_000, 64
	mov __DU0main, __main_7qG2_ui_7qG2_000
	cmp 0, __D0main, __constVal64, __DU0main
	brtst 4, __ASM_Label_610, __D0main
	mov __DU0main, __main_7qG2_l_7qG2_000 ; extra mov is optimized out
	jmp __ASM_Label_611
__ASM_Label_610:
	mov __DU0main, __main_7qG2_ui_7qG2_000
__ASM_Label_611:
	mov __main_7qG2_len_7qG2_000, __DU0main
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests