Strange Error when compiling StructureUnion in C-Backend

Just starting out? Need help? Post your questions and find answers here.
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Strange Error when compiling StructureUnion in C-Backend

Post by SMaag »

I have a strange Error when compiling a Structure Union Construction in C-Backend.
In ASM-Backend it is working perfect!

I use actual PB 6.11
IDE build on 06/06/2024 [09:41] by Fred
Branch: v6.11 Revision: 5a52d0b14a87

THIS IS THE ERROR MESSAGE


---------------------------
PureBasic - Assembler error
---------------------------
error: incompatible types when initializing type 'float' using type 'vecfXs_tvector' {aka 'struct vecfXs_tvector'}

673 | vecfXs_tmatrix v_matrix={0};

| ^~~~~~~~~~~~~~

purebasic.c: In function 'draw3dXf__draw3dpoint':

purebasic.c:693:30: warning: ex

---------------------------

I figured out that the problem is the initialisation of a var.TVector

Code: Select all

 Procedure _Draw3DPoint(*Vertex.TDraw3D_Vertex)
    ;TODO! Find the Problem in the Draw3D Project in C-Backend 
    
    ; This line creates the Compiler Error in C-Bakcen but only in the real projct not in this demo!
    Protected Projection.TVector   
    
  EndProcedure
I created a test code what shows the situation but in the test code tere is no problem!
Until now I'm not able to locate the problem. I checked C-Output but I could not find anything.
Then I reinstalled PB but same problem (and I killed my Show C-Output)


To see the Error you have to download the orignal Project here:
https://github.com/Maagic7/PureBasicFra ... main/Demos
Draw3D_with_VECf.zip
It is a modification from STARGATEs Draw3D to test my Vector Module

Here is the test code what shows the situation but not the error.

Code: Select all


DeclareModule Vecf
  
  EnableExplicit
  
  Structure TPoint2D Align 4    ; to be sure it will be 4Byte Aligned at x64
    X.f          
    Y.f          
  EndStructure
   
  Structure TVector Align 4 ; Single precicion Vector [16 Bytes / 128 Bit]
    StructureUnion
      v.f[0]                ; virutal Array  v[0]=x, v[1]=y, v[2]=z, v[3]=w
      Pt2D.TPoint2D[0]
    EndStructureUnion
    x.f
    y.f
    z.f
    w.f
  EndStructure 
  
  Debug "SyzeOf(VECf::TVector) = " + SizeOf(TVector)
    
  ; Single precicion Matrix
  Structure TMatrix Align 4    ; to be sure it will be 4Byte Aligned at x64 
    StructureUnion
      v.TVector[0]   ; Vector interpretation of the Matrix Structure
      Pt2D.TPoint2D[0]
    EndStructureUnion
    m11.f : m12.f : m13.f : m14.f    
    m21.f : m22.f : m23.f : m24.f
    m31.f : m32.f : m33.f : m34.f   
    m41.f : m42.f : m43.f : m44.f
  EndStructure
  
  Declare.i Vector_Set(*OUT.TVector, X.f=0.0, Y.f=0.0, Z.f=0.0, W.f=0.0)

EndDeclareModule

Module Vecf
  
   Procedure.i Vector_Set(*OUT.TVector, X.f=0.0, Y.f=0.0, Z.f=0.0, W.f=0.0)
     With *OUT
       \x = X
       \y = Y
       \z = Z
       \w = W
     EndWith
     
   EndProcedure
  
EndModule

; ----------------------------------------------------------------------

DeclareModule Draw3D

  EnableExplicit
    

 	Declare DrawPoint3D(X.f, Y.f, Z.f, Color.q)
  
EndDeclareModule

Module Draw3D
  ; Zeichnet einen Punkt in die Szene
  UseModule Vecf
  
  Structure TColor4
  	Alpha.f               ; Intensität [0.0, 1.0]
  	Blue.f                ; Blau [0.0, Intensität]
  	Green.f               ; Grün [0.0, Intensität]
  	Red.f                 ; Rot [0.0, Intensität]
  EndStructure
    
  Structure TDraw3D_Vertex Extends TVector
  	Color.TColor4 ; Farbe
  EndStructure

  Procedure _Draw3DPoint(*Vertex.TDraw3D_Vertex)
    ;TODO! Find the Problem in the Draw3D Project in C-Backend 
    
    ; This line creates the Compiler Error in C-Bakcen but only in the real projct not in this demo!
    Protected Projection.TVector   
    
;     Protected Distance.f
;   	Protected X.i, Y.i
;   	Protected Visible.f
;   	Protected *Pixel.TDraw3D_Pixel
;   	
;   	_Draw3D_Projection(Projection, *Vertex)
;   	
;   	Draw3DInclude\CurrentColor[0] = *Vertex\Color
;   	X = Projection\X
;   	Y = Projection\Y
;   	If X >= 0 And (X <= Draw3DInclude\MaxX) And (Y >= 0) And (Y <= Draw3DInclude\MaxX)
;   		*Pixel = _Draw3D_AddPixel(X, Y, *Vertex\Z)
;   		If *Pixel
;   			_ColorMix(*Pixel\Color, 1.0)
;   		EndIf
;   	EndIf
  EndProcedure
  
  Procedure DrawPoint3D(X.f, Y.f, Z.f, Color.q)  	
  	Protected Vertex.TDraw3D_Vertex
  	
   	Vector_Set(Vertex, X, Y, Z)
;   	_SetColor(Vertex\Color, Color)
;   	_Draw3D_Rotate(Vertex, Draw3DInclude\Orientation)
;   	Vector_Sub(Vertex, Vertex, Draw3DInclude\Position); V4_Subtract(Vertex, Draw3DInclude\Position)
  	_Draw3DPoint(Vertex) 	
  EndProcedure

EndModule



Draw3D::DrawPoint3D(5,10,15, RGB(255,0,0))


MessageRequester("C-Backend Structure Union Test", "Done")
I hope anyone of you is able to locate the problem!
Many thanks
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Strange Error when compiling StructureUnion in C-Backend

Post by SMaag »

here the C-Output of Procedure _Draw3DPoint(*Vertex.TDraw3D_Vertex)

Code: Select all

// Procedure.i _Draw3D_Multiply(*Use.TMatrix, *Source.TMatrix)
static integer draw3dXf__draw3dpoint(draw3dXs_tdraw3d_vertex* p_vertex) {
integer r=0;
integer v_x=0;
integer v_y=0;
draw3dXs_tdraw3d_pixel*p_pixel=0;
vecfXs_tvector v_projection={0};
float v_visible=0;
float v_distance=0;
// Protected Projection.TVector    ;
// 
// Protected Distance.f;
// Protected X.i, Y.i;;
// Protected Visible.f;
// Protected *Pixel.TDraw3D_Pixel;
Seems ok

the Structure definition!

Code: Select all

#pragma pack(4)
typedef struct vecfXs_tvector {
union {
float f_v[0];
vecfXs_tpoint2d f_pt2d[0];
float f_x; 
};
float f_y;
float f_z;
float f_w;
} vecfXs_tvector;
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Strange Error when compiling StructureUnion in C-Backend

Post by idle »

think you need to get the union out of the structure if you want to use zero based static arrays
or it's an issue in the c backend processing structureunion as x.f is included in the union in your example .

if just means you need to access with \v like *Vertex\v\v[0]

Code: Select all

DeclareModule Vecf
  
  EnableExplicit
  
  Structure TPoint2D Align 4    ; to be sure it will be 4Byte Aligned at x64
    X.f          
    Y.f          
  EndStructure
  
  Structure UnionTVector 
    StructureUnion
    v.f[0]                ; virutal Array  v[0]=x, v[1]=y, v[2]=z, v[3]=w
    Pt2D.TPoint2D[0]
    EndStructureUnion
  EndStructure  
    
  Structure TVector Align 4 ; Single precicion Vector [16 Bytes / 128 Bit]
    v.UnionTVector               ; virutal Array  v[0]=x, v[1]=y, v[2]=z, v[3]=w
    x.f
    y.f
    z.f
    w.f
  EndStructure 
  
  Debug "SyzeOf(VECf::TVector) = " + SizeOf(TVector)
  
  Structure unionTmatrix 
     StructureUnion
      v.TVector[0]   ; Vector interpretation of the Matrix Structure
      Pt2D.TPoint2D[0]
    EndStructureUnion
  EndStructure   
  
  ; Single precicion Matrix
  Structure TMatrix Align 4    ; to be sure it will be 4Byte Aligned at x64 
    v.unionTmatrix
    m11.f : m12.f : m13.f : m14.f    
    m21.f : m22.f : m23.f : m24.f
    m31.f : m32.f : m33.f : m34.f   
    m41.f : m42.f : m43.f : m44.f
  EndStructure
  
  Declare.i Vector_Set(*OUT.TVector, X.f=0.0, Y.f=0.0, Z.f=0.0, W.f=0.0)

EndDeclareModule
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Strange Error when compiling StructureUnion in C-Backend

Post by SMaag »

Now I did some more resarch!
I guess it is a bug in the C-Backend!

sampels of the C-Output

Code: Select all

#pragma pack(4)
typedef struct vecfXs_tpoint2d {
float f_x;
float f_y;
} vecfXs_tpoint2d;
#pragma pack()

#pragma pack(4)
typedef struct vecfXs_tvector {
union {
float f_v[0];
vecfXs_tpoint2d f_pt2d[0];
float f_x;
};
float f_y;
float f_z;
float f_w;
} vecfXs_tvector;
#pragma pack()

#pragma pack(4)
typedef struct vecfXs_tmatrix {
union {
vecfXs_tvector f_v[0];
vecfXs_tpoint2d f_pt2d[0];
float f_m11;
};
float f_m12;
float f_m13;
float f_m14;
float f_m21;
float f_m22;
float f_m23;
float f_m24;
float f_m31;
float f_m32;
float f_m33;
float f_m34;
float f_m41;
float f_m42;
float f_m43;
float f_m44;
} vecfXs_tmatrix;

static integer f_test_matrix_x_matrix() {
integer r=0;
; Thats the point what is not possible in C-Backend!!!
vecfXs_tmatrix v_mx0={0};
vecfXs_tmatrix v_mx1={0};
vecfXs_tmatrix v_mx2={0};

In my opinion that's the BUG
vecfXs_tmatrix v_mx0={0};

It has to converted from PB to C-Compiler in an other expression because if it is an Array with [0] there is no specific
length!
SMaag
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: Strange Error when compiling StructureUnion in C-Backend

Post by SMaag »

Now I was able to create a demo what shows the Problem.

I opend a thread in the C-Backend Bugreport for that!

https://www.purebasic.fr/english/viewtopic.php?t=85139
Post Reply