[fpc-devel] Bug (Severe): Floating point values completely break GCC Struct compatability with records in 64-bit

2009-12-16 Thread Joshua Cearley
While working on a binding for the development branch for the Allegro 
game library I ran in to a bit of a problem in that one of the methods 
takes a structure of four floating point values (which is how it stores 
colors) and this is not done through a pointer to a color struct but by 
passing the structure directly to the method; this never actually works 
and exploring with GCC shows that the should-be C compatible record 
built by FPC 2.5 was fine but the version C had was corrupted. I went to 
FPC 2.4rc1 and tried again, and the same issue remains. 32-bit builds 
seem to work properly, so I conclude that only the 64-bit version of FPC 
is emitting broken code.


The following was generated with 2.4rc1;

Recreating this problem is fairly simple; first you have a source file 
written in C code `cpart.c`:


/* Start */
#include 

struct Bagel
{
float roundness, tastiness;
};

void PrintBagel(struct Bagel b)
{
printf("[Bagel Round:%f Tasty:%f]\n", b.roundness, b.tastiness);
}
/* End */

Then you have the trivial Pascal application `ppart.pp`:

(* Start *)
program ppart;

{$LINK cpart}

uses ctypes;

type
{$PACKRECORDS C}
TBagel = record
roundness, tastiness : CFloat;
end;

Procedure PrintBagel(bagel : TBagel); cdecl; external;

var
b : TBagel;
begin
b.roundness := 42.0;
b.tastiness := 1.0;
PrintBagel(b);
end.
(* End *)

Then we compile these:
skrylar)fpc-bug % gcc -c cpart.c && ppcx64 ppart
Free Pascal Compiler version 2.4.0rc1 [2009/10/24] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Darwin for x86_64
Compiling ppart.pp
Assembling ppart
Linking ppart
22 lines compiled, 0.1 sec
skrylar)fpc-bug %

Run it:
skrylar)fpc-bug % ./ppart
[Bagel Round:0.00 Tasty:-170141183460469231731687303715884105728.00]
skrylar)fpc-bug %

Disassembling through GDB (AT&T Syntax ahead):
Dump of assembler code for function PASCALMAIN:
0x000109f0 :  push   %rbp
0x000109f1 :  mov%rsp,%rbp
0x000109f4 :  callq  0x100013970 

0x000109f9 :  lea0x1d218(%rip),%rax 
 # 0x10001dc18 <$PPART$_Ld1>

0x00010a00 : mov(%rax),%eax
0x00010a02 : lea0x2329f(%rip),%rdx 
 # 0x100023ca8 

0x00010a09 : mov%eax,(%rdx)
0x00010a0b : lea0x1d20a(%rip),%rax 
 # 0x10001dc1c <$PPART$_Ld2>

0x00010a12 : mov(%rax),%eax
0x00010a14 : lea0x2328d(%rip),%rdx 
 # 0x100023ca8 

0x00010a1b : mov%eax,0x4(%rdx)
0x00010a1e : lea0x23283(%rip),%rax 
 # 0x100023ca8 

0x00010a25 : mov(%rax),%rdi
0x00010a28 : callq  0x10a3c 
0x00010a2d : callq  0x100013d70 
0x00010a32 : leaveq
0x00010a33 : retq
End of assembler dump.
Dump of assembler code for function PrintBagel:
0x00010a3c :  push   %rbp
0x00010a3d :  mov%rsp,%rbp
0x00010a40 :  sub$0x10,%rsp
0x00010a44 :  movq   %xmm0,-0x8(%rbp)
0x00010a49 : movss  -0x4(%rbp),%xmm0
0x00010a4e : cvtss2sd %xmm0,%xmm1
0x00010a52 : movss  -0x8(%rbp),%xmm0
0x00010a57 : cvtss2sd %xmm0,%xmm0
0x00010a5b : lea0x1d164(%rip),%rdi 
 # 0x10001dbc6

0x00010a62 : mov$0x2,%eax
0x00010a67 : callq  0x10001d976 


0x00010a6c : leaveq
0x00010a6d : retq
0x00010a6e : nop
0x00010a6f : nop
End of assembler dump.

I asked in IRC and was referred to here; unfortunately with Snow Leopard 
GCC compiles in 64-bit by default and so falling back to 32-bit is 
exceptionally painful.

___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


Re: [fpc-devel] Class extension

2010-05-28 Thread Joshua Cearley
Speaking of .NET and partial classes, there are also extension methods  
in newer versions if C# that allow a similar level of bolting methods  
on to and is likely even simpler to handle than open classes and  
categories; it amounts to writing a function with a special naming so  
the compiler knows it's an extension and that's only available to  
modules who can see the extension method.


It's effectively syntactic sugar in some ways, though I think an  
extension method might be allowed to access private variables of the  
class being extended but I'm not fully confident about that. In the  
basic form, it's equivalent to having a FooStuff(x) but you can call  
it like it was always there as long as you know if the extension;  
x.Stuff() for instance. If Stuff is in both, issue a warning or error  
and pick the one from the class before an extension method.


I find them useful in C# as the syntactic sugar helps keep things  
readable (I can say x.ToUTF8 instead of ThingToUTF8(x) which in my  
mind routes quite a bit of the point of object relative syntax.)


Sent from my iPod

On May 20, 2010, at 7:29 AM, Matt Emson   
wrote:


Okay, so the whole Observer Pattern discussion this morning went way  
off track after I mentioned this idea, but a few people expressed  
interest in my proposal:


I'd rather see a mechanism for injecting first class extensions to  
existing classes. That way, it really doesn't matter what pattern  
is implemented where.


So, what are these "class extensions"? Some mentioned Class Helpers,  
ala, Delphi. This is not what I envisioned. My idea was more me  
"riffing" on the notions I've encountered in DotNet and Objective-C.
In DotNet, well, C# specifically, we have the concept of "partial  
classes". That is to say, a class that is implemented in more than  
one section. In the world of DotNet, the partial classes are  
assembled at compile time in to a single concrete "class". The  
mechanism is a little clunky, and the rules prohibit some things  
from happening, e.g. not being able to add partial classes cross  
logical code block (in DotNet parlance, assembly.) What a partial  
class does allow the programmer to do is modularise their class  
structure. It is therefore possible to add/remove functionality from  
a class by including or excluding source files at compile time. I  
see this as an extremely basic version of my next item: The  
Objective-C Category.


In Objective-C, the Category often seems a lot like the Partial  
class in a superficial way. It is the same kind of idea - a breaking  
up of a class in to sections. Sections can be added and removed in a  
similar way. However the category goes further (as Objective-C  
always seems to) and allows functionality to be plumbed in to an  
existing class, without the need to alter the original source code -  
indeed you actually do not need the source at all. This is what I  
envisioned when I stated the above "class extension". It could also  
be seen as a mechanism to overcome the issues often encountered with  
an OO system lacking multiple inheritance. One of the more  
interesting features of Categories in Objective-C is the ability to  
replace methods in the base class, but using the same signature.


http://en.wikipedia.org/wiki/Objective-C#Categories

So, yes, this is all pie in the sky. I'm in no way making any offer  
or commitment making this happen. I'm also fully expecting some  
criticism and probably to be told it is unlikely to be implemented  
(or at least, only in the Objective-P dialect), but I though that I  
would try to spin something positive from what was discussed earlier.


To recap - this has little to do with the crippled Class Helper  
functionality that Borland grafted on to Delphi DotNet and then  
Delphi in general. This is all to do with enabling something useful  
and helpful.


M


___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel

___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel