[Pythonmac-SIG] psyco and Mac OS X i386

Bob Ippolito bob at redivi.com
Tue Jun 6 21:44:11 CEST 2006


On Jun 6, 2006, at 10:35 AM, Bob Ippolito wrote:

>
> On Jun 6, 2006, at 9:56 AM, Michael Glassford wrote:
>
>> Bob Ippolito wrote:
>>
>>>> What's the right way to detect within the application whether its
>>>> running on an Intel or a PowerPC Mac? I can figure out a way on
>>>> my own
>>>> (such as looking at sys.byteorder), but I wonder if there's an
>>>> "official" way.
>>>
>>> The right way to detect is to not detect at all.
>>
>> I realize that's generally preferable, but...
>>
>>> Otherwise, it depends on what reason you're doing it.
>>
>> I'm experimenting with getting Psyco working in the application, and
>> obviously only want to enable it if I'm running on an Intel
>> machine. It
>> may turn out to do this check on its own; at the moment I can't tell,
>> because enabling it automatically crashes my application on both  
>> Intel
>> and PowerPC Macs.
>
> psyco doesn't quite work on Mac OS X i386. I think it's because Mac
> OS X requires a 16 byte stack alignment, and other i386 platforms
> expect much less (4 I think) so psyco ends up misaligning the stack
> on some function calls and ends up crashing somewhat non-
> deterministically. It didn't look very easy to fix, there's a lot of
> gnarly code in psyco... someone would more or less have to ask Armin
> how to fix it.
>
> psyco's ivm backend should work on both if it works at all, but I'm
> not sure how stable it is or how much of a performance boost it's
> going to get, and I don't think it's ever really seen any real-world
> use.
>
> Note that it'd be possible to simply change psyco a bit make the PPC
> part of psyco compile to a binary that raises ImportError.. which
> would alleviate any need to do platform detection.

I went through and confirmed that indeed the stack alignment is the  
reason why psyco doesn't function properly on Mac OS X i386. You can  
confirm this by running the bpnn.py example under gdb. You'll  
eventually get an Illegal Instruction (GP(0) exception) during the  
call to the libSystem version of pow(). The reason for this is that  
pow() uses a SSE2 instruction that requires 16 byte alignment  
(specifically, "movapd %xmm1,-104(%ebp)") and the way that psyco set  
up the stack does not make it so.

I can't really commit any more time to fixing this right now, but  
Armin has said that:

arigo: let me see, you need to hack in c/i386/iencoding.h
arigo: CALL_SET_ARG_* macros
arigo: and CALL_C_FUNCTION*
arigo: or maybe better yet, add a new macro CALL_ALIGN_STACK(nb_args)
arigo: which should be called before any of the CALL_SET_ARG

The IA-32 calling conventions for Mac OS X are documented here:
http://developer.apple.com/documentation/DeveloperTools/Conceptual/ 
LowLevelABI/Articles/IA32.html

And you'll probably also want to look at the disassembly (otool -tVv  
is good for this, or just gcc -S) of a trivial C program or two to  
get an idea of what it looks like in practice.

-bob



More information about the Pythonmac-SIG mailing list