Why does an unsigned variable for indexing translated to a signed variable?

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
erik
Posts: 39
Joined: Dec 28, 2020 17:27
Location: Krasnoyarsk
Contact:

Why does an unsigned variable for indexing translated to a signed variable?

Post by erik »

Example:

Code: Select all

Const SOME_LENGTH = 10

Type Some
	A As Integer
End Type

Dim p As Some Ptr = Allocate(SOME_LENGTH * SizeOf(Some))

Dim i As UInteger = 0
Do
	p[i].A = 2
Loop While i < SOME_LENGTH
Compiler Options:

Code: Select all

fbc.exe -gen gcc -r unsigned.bas
Intermediate view:

Code: Select all

struct $4SOME {
	int64 A;
};
__attribute__(( constructor )) static void fb_ctor__unsigned( void )
{
	label$0:;
	struct $4SOME* P$0;
	void* vr$0 = malloc( 80ull );
	P$0 = (struct $4SOME*)vr$0;
	uint64 I$0;
	I$0 = 0ull;
	label$2:;
	{
		*(int64*)((uint8*)P$0 + ((int64)I$0 << (3ll & 63ll))) = 2ll;
	}
	label$4:;
	if( I$0 < 10ull ) goto label$2;
	label$3:;
	label$1:;
}
As you can see, the unsigned variable variable i is cast to the signed type int64:

Code: Select all

(int64)I$0
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Why does an unsigned variable for indexing translated to a signed variable?

Post by D.J.Peters »

signed pointer arithmetic !

Joshy

Code: Select all

	mov	ecx, 80                 ; SOME_LENGTH * SizeOf(Some)
	call	malloc
	mov	QWORD PTR -8[rbp], rax  ; tmp = Allocate(SOME_LENGTH * SizeOf(Some))
	mov	rax, QWORD PTR -8[rbp]
	mov	QWORD PTR -16[rbp], rax ; p = tmp
	mov	QWORD PTR -24[rbp], 0   ; i = 0
.L3:                          ; D0
	mov	rax, QWORD PTR -24[rbp]
	sal	rax, 3                  ; i+=8  [0,8,16,24,32 ...]
	mov	rdx, rax
	mov	rax, QWORD PTR -16[rbp] ; @p
	add	rax, rdx                ; p+=i
	mov	QWORD PTR [rax], 2      ; *p=2  (p(i).A = 2)
.L4:
	cmp	QWORD PTR -24[rbp], 9   ; Loop While i < SOME_LENGTH
	ja	.L1
	jmp	.L3
erik
Posts: 39
Joined: Dec 28, 2020 17:27
Location: Krasnoyarsk
Contact:

Re: Why does an unsigned variable for indexing translated to a signed variable?

Post by erik »

Is there a loss of information and the highest bit when an unsigned number is converted to a signed number?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Why does an unsigned variable for indexing translated to a signed variable?

Post by fxm »

In the Operator [] (Pointer Index) syntax declaration, 'Pointer index' (rhs) is declared as INTEGER (so, signed):
Syntax:
[list]declare operator [] ( byref lhs as T pointer, byref rhs as integer ) byref as T[/list]Usage:
[list]result = lhs [ rhs ]
or
lhs [ rhs ] = value[/list]
Parameters:
  • lhs
    The base address.
    rhs
    A signed offset from lhs.
    T
    Any data type.
As this 'index' is passed by reference, the used reference inside the 'Operator []' is of the form:
rhs = cast(integer, index)
a signed Integer reference to the UInteger user 'index' variable.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Why does an unsigned variable for indexing translated to a signed variable?

Post by D.J.Peters »

yes operator[index] is signed
for example the fb image ptr is 16 byte aligned and the original allocated (maybe unaligned) pointer is stored at img[-1]

Joshy
Post Reply