static bitfield initialization

General FreeBASIC programming questions.
Post Reply
Mysoft
Posts: 836
Joined: Jul 28, 2005 13:56
Location: Brazil, Santa Catarina, Indaial (ouch!)
Contact:

static bitfield initialization

Post by Mysoft »

tried to search, but got no useful results...
there's a way to initialize an UDT with a bitfield statically?

otherwise i will need to workaround using "flags" as member and joint constants

Code: Select all

Type MyType
  A   as ushort
  B:1 as ubyte
  C:1 as ubyte
end type

'this is ok... as no bitfield is set
static as MyType ThisWorks = type(0)

'this works... but generate runtime code to fill instead of const 0
dim    as MyType GeneratesUnecessaryInitCode = type(0,0)


'those generate error messages...
'error 24: Invalid data types in "static as MyType ThisDoesNotWork = type(0,0)"
dim shared as MyType t = type(0,0)
static as MyType ThisDoesNotWork = type(0,0)

'this crashes (unrelated but worth o a note)
'Aborting due to runtime error 12 ("segmentation violation" signal)
static as MyType ThisCrash = type(0,type(0))
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: static bitfield initialization

Post by fxm »

This works if you define a constructor:

Code: Select all

Type MyType
  A   as ushort
  B:1 as ubyte
  C:1 as ubyte
  declare constructor (byval us as ushort = 0, byval ub as ubyte = 0, byval uc as ubyte = 0)
end type

constructor MyType (byval us as ushort = 0, byval ub as ubyte = 0, byval uc as ubyte = 0)
  A = us
  B = ub
  C = uc
end constructor
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: static bitfield initialization

Post by badidea »

Mysoft does not want to hear 'constructor' I think. Am I right?

Alternative:

Code: Select all

Type MyType
	A as ushort
	union
		bits as ushort
		type
			B:1 as ubyte
			C:1 as ubyte
		end type
	end union
end type

static as MyType ThisWorks = type<MyType>(&hffff, 2)

'print sizeof(ThisWorks)

print ThisWorks.A
print ThisWorks.bits
print ThisWorks.B
print ThisWorks.C
I could have misunderstood the question. I don't use static that often.
Mysoft
Posts: 836
Joined: Jul 28, 2005 13:56
Location: Brazil, Santa Catarina, Indaial (ouch!)
Contact:

Re: static bitfield initialization

Post by Mysoft »

fxm wrote:This works if you define a constructor:
well, that end worse than as doing it locally (dim as MyType) ...
because not only it generates code... it also calls a function
to do this code initialization...

Code: Select all

##static as MyType  t  = type(0,1,2)
	cmp dword ptr [_Lt_0011], 0
	jne .Lt_000E
	mov dword ptr [_Lt_0011], 1
	push 2
	push 1
	push 0
	push offset _Lt_0012
	call __ZN6MYTYPEC1Ethh
	add esp, 16
.Lt_000E:
.stabn 68,0,22,.Lt_0010-_fb_ctor__chk
.Lt_0013:
while the correct way with the initializer should just be

Code: Select all

##static as MyType t = type(0,1,2)
##
with the direct values on the DATA section

Code: Select all

_Lt_00xx:
.short 0
.byte 1
Mysoft
Posts: 836
Joined: Jul 28, 2005 13:56
Location: Brazil, Santa Catarina, Indaial (ouch!)
Contact:

Re: static bitfield initialization

Post by Mysoft »

badidea wrote: Alternative:

Code: Select all

Type MyType
	A as ushort
	union
		bits as ushort
		type
			B:1 as ubyte
			C:1 as ubyte
		end type
	end union
end type

static as MyType ThisWorks = type<MyType>(&hffff, 2)

hum... well since the workaround would be using constants flags and manually oring the values to init.... it becomes almost as the workaround
but with the benefit of being able to at least allow the compiler generate code to query/set... with the added simplicity to access the full flags... so yeah... i guess that's little better... i just hope FBC will not get confuse on which type to initialize on the union
Post Reply