[NXC][ArrayLen] Inconcistencies

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

[NXC][ArrayLen] Inconcistencies

Post by muntoo »

Compiler bug. The following code:

Code: Select all

string InsertSpaces(string a, string b, unsigned long len = 16)
{
	char chSpaces[];
	unsigned int chSpaces_len = len - (strlen(a) + strlen(b));
	ArrayInit(chSpaces, 0x20, chSpaces_len);
	
	return(a + ByteArrayToStr(chSpaces) + b);
}


void DisplayMenu(string title, string &list[], unsigned long pos = 0)
{
	TextOut(0, LCD_LINE1, title, DRAW_OPT_INVERT);
}


void DisplayFileList(unsigned long pos, ListFilesType &lftFiles)
{
	unsigned int FileCount = ArrayLen(lftFiles.FileList);
	
	string fl[];
	ArrayBuild(fl, lftFiles.FileList);
	
	DisplayMenu(InsertSpaces("File Count:", NumToStr(FileCount), 16), fl, pos);
}


void SelectFile(ListFilesType &lftFiles)
{
	unsigned int FileCount = ArrayLen(lftFiles.FileList);

	ClearScreen();
	DisplayFileList(0, lftFiles);
	TextOut(0, 0, InsertSpaces("File Count:", NumToStr(FileCount), 16));
}


task main()
{
	ListFilesType lftFiles;
	lftFiles.Pattern = "*.*";
	SysListFiles(lftFiles);
		
	SelectFile(lftFiles);
	
	Wait(5000);
}
Displays:

Code: Select all

File Count:   10






File Count:    0
Specifications:
  • Next Byte Codes Compiler version 1.2 (1.2.1.5, built Thu Jul 7 07:08:23 CDT 2011)
  • Optimization Level 2
  • nbc -sm- -ER=5 -Z2 -EF
Optimization Level 2 NBC output:

Code: Select all

dseg	segment
	;------- definitions -------
	main_lftFiles_def	struct
		Result	sbyte
		Pattern	byte[]
		FileList	byte[][]
	main_lftFiles_def	ends
	DFL_lftFiles_def	struct
		Result	sbyte
		Pattern	byte[]
		FileList	byte[][]
	DFL_lftFiles_def	ends
	SF_lftFiles_def	struct
		Result	sbyte
		Pattern	byte[]
		FileList	byte[][]
	SF_lftFiles_def	ends
	Location_def	struct
		X	sword
		Y	sword
	Location_def	ends
	PointOutArgs_def	struct
		Result	sbyte
		Location	Location_def	
		Options	dword
	PointOutArgs_def	ends
	Location_def	struct
		X	sword
		Y	sword
	Location_def	ends
	TextOutArgs_def	struct
		Result	sbyte
		Location	Location_def	
		Text	byte[]
		Options	dword
	TextOutArgs_def	ends
	;------- declarations -------
	DU0SF	dword	
	D0strlen_inline_IS	sdword	
	sstack_001DM	sdword	
	D0DM	sdword	
	D0IS	sdword	
	D0SF	sdword	
	sstack_001SF	sdword	
	sstack_002SF	sdword	
	IS_len	dword	
	sstack_002DM	sdword	
	sstack_002IS	sdword	
	usstack_001IS	dword	
	TextOutMutex	mutex
	DU0DFL	dword	
	IS_chSpaces_len	word	
	DFL_FileCount	word	
	SF_FileCount	word	
	IS_return	byte	
	SF_return	byte	
	initialize_global_data_return	byte	
	DM_return	byte	
	DFL_return	byte	
	main_lftFiles	main_lftFiles_def	
	DFL_lftFiles	DFL_lftFiles_def	
	SF_lftFiles	SF_lftFiles_def	
	PointOutArgs	PointOutArgs_def	
	TextOutArgs	TextOutArgs_def	
	DFL_fl	byte[][]	
	strbufIS	byte[]	
	ArrHelperIS_2	byte[]	
	strbufmain	byte[]	
	strretvalDFL	byte[]	
	strbufDM	byte[]	
	strbufDFL	byte[]	
	IS_b	byte[]	0x0
	strbufBATS_inline_IS	byte[]	
	strretvalSF	byte[]	
	ArrHelperIS_0	byte[]	
	ArrHelperIS_1	byte[]	
	IS_a	byte[]	0x0
	BATS_data_inline_IS	byte[]	
	DM_title	byte[]	0x0
	DM_list	byte[][]	
	IS_chSpaces	sbyte[]	
	strbufSF	byte[]	
	strlen_str_inline_IS	byte[]	0x0
dseg	ends
;------- code -------
thread main
	subcall initialize_global_data, initialize_global_data_return
	
	
	strcat strbufmain, '*.*'
	mov main_lftFiles.Pattern, strbufmain

#pragma macro 4

	syscall 47, main_lftFiles


	mov SF_lftFiles, main_lftFiles
	subcall SF, SF_return
	mov main_lftFiles, SF_lftFiles


	wait 5000

	exit -1, -1
endt
;------------------------
subroutine IS
	mov usstack_001IS, IS_len


	strcat strbufIS, IS_a
	mov strlen_str_inline_IS, strbufIS
	
	arrsize D0strlen_inline_IS, strlen_str_inline_IS
	sub sstack_002IS, D0strlen_inline_IS, 1
	


	strcat strbufIS, IS_b
	mov strlen_str_inline_IS, strbufIS
	
	arrsize D0strlen_inline_IS, strlen_str_inline_IS
	sub D0IS, D0strlen_inline_IS, 1


	add D0IS, sstack_002IS, D0IS
	sub IS_chSpaces_len, usstack_001IS, D0IS


	arrinit IS_chSpaces, 32, IS_chSpaces_len



	mov ArrHelperIS_0, IS_a
	mov BATS_data_inline_IS, IS_chSpaces
	arrtostr strbufBATS_inline_IS, BATS_data_inline_IS



	mov ArrHelperIS_1, strbufBATS_inline_IS
	strcat strbufIS, IS_b
	mov ArrHelperIS_2, strbufIS
	strcat strbufIS, ArrHelperIS_1, ArrHelperIS_2
	mov ArrHelperIS_1, strbufIS
	strcat strbufIS, ArrHelperIS_0, ArrHelperIS_1
	
	
	subret IS_return


	subret IS_return
ends
;------------------------
subroutine DM
	set sstack_001DM, 0
	set sstack_002DM, 56


	strcat strbufDM, DM_title
	set D0DM, 4
	
	
	acquire TextOutMutex
	mov TextOutArgs.Location.X, sstack_001DM
	mov TextOutArgs.Location.Y, sstack_002DM
	mov TextOutArgs.Options, D0DM
	mov TextOutArgs.Text, strbufDM
	syscall 13, TextOutArgs
	mov D0DM, TextOutArgs.Result
	release TextOutMutex


	subret DM_return
ends
;------------------------
subroutine DFL
	arrsize DFL_FileCount, DFL_lftFiles.FileList
	arrbuild DFL_fl, DFL_lftFiles.FileList


	strcat strbufDFL, 'File Count:'
	mov IS_a, strbufDFL
	mov DU0DFL, DFL_FileCount
	numtostr strretvalDFL, DU0DFL
	strcat strbufDFL, strretvalDFL
	mov IS_b, strbufDFL
	set IS_len, 16


	subcall IS, IS_return
	mov strretvalDFL, strbufIS
	strcat strbufDFL, strretvalDFL
	mov DM_title, strbufDFL
	mov DM_list, DFL_fl


	subcall DM, DM_return
	mov DFL_fl, DM_list


	subret DFL_return
ends
;------------------------
subroutine SF
	arrsize D0SF, SF_lftFiles.FileList
	mov SF_FileCount, DU0SF

#pragma macro 6
	mov PointOutArgs.Location.X, 200
	mov PointOutArgs.Location.Y, 200
	mov PointOutArgs.Options, 1
	syscall 14, PointOutArgs


	mov DFL_lftFiles, SF_lftFiles
	subcall DFL, DFL_return
	mov SF_lftFiles, DFL_lftFiles


	set sstack_001SF, 0
	set sstack_002SF, 0


	strcat strbufSF, 'File Count:'
	mov IS_a, strbufSF
	mov DU0SF, SF_FileCount
	numtostr strretvalSF, DU0SF
	strcat strbufSF, strretvalSF
	mov IS_b, strbufSF
	set IS_len, 16

	subcall IS, IS_return
	
	mov strretvalSF, strbufIS
	strcat strbufSF, strretvalSF
	
	
	acquire TextOutMutex
	mov TextOutArgs.Location.X, sstack_001SF
	mov TextOutArgs.Location.Y, sstack_002SF
	set TextOutArgs.Options, 0
	mov TextOutArgs.Text, strbufSF
	syscall 13, TextOutArgs
	mov D0SF, TextOutArgs.Result
	release TextOutMutex


	subret SF_return
ends
;------------------------
subroutine initialize_global_data
	subret initialize_global_data_return
ends
;------------------------

Side note: Removing pos seems to get rid of the bug...
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: [NXC][ArrayLen] Inconcistencies

Post by muntoo »

Found the bug.

Code: Select all

subroutine SF
	arrsize D0SF, SF_lftFiles.FileList
	mov SF_FileCount, DU0SF
(Where SF is SelectFile.)

As you can see, the arrsize is stored in D0SF, but SF_FileCount is initalized to DU0SF.


The correct code would be:

Code: Select all

	arrsize DU0SF, SF_lftFiles.FileList
	mov SF_FileCount, DU0SF
Which works perfectly.


P.S. Didn't John say before that he'd fixed this? :)
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest