diff -urN psyco-dist.old/c/codegen.c psyco-dist.new/c/codegen.c --- psyco-dist.old/c/codegen.c 2006-04-17 04:34:34.000000000 -0500 +++ psyco-dist.new/c/codegen.c 2007-11-15 16:36:01.000000000 -0600 @@ -308,7 +308,12 @@ #endif } } + #ifdef __APPLE__ + /* Adjust # of arguments for MacOS 16-byte stack alignment */ + result = psyco_call_var(c_function, (count+3)&~3, args); + #else result = psyco_call_var(c_function, count, args); + #endif if (PyErr_Occurred()) { if (has_refs) for (i = 0; i < count; i++) @@ -387,6 +392,7 @@ stackbase = po->stack_depth; po->stack_depth += totalstackspace; STACK_CORRECTION(totalstackspace); + CALL_STACK_ALIGN(count); for (i=count; i--; ) { switch (argtags[i]) { @@ -467,6 +473,7 @@ } } } + return NULL; } diff -urN psyco-dist.old/c/i386/iencoding.c psyco-dist.new/c/i386/iencoding.c --- psyco-dist.old/c/i386/iencoding.c 2006-02-15 08:04:12.000000000 -0600 +++ psyco-dist.new/c/i386/iencoding.c 2007-11-15 16:36:01.000000000 -0600 @@ -48,12 +48,17 @@ code_t* code = po->code; void* result; code_t* fixvalue; + #ifdef __APPLE__ + int aligndelta; + #endif if (restore) TEMP_SAVE_REGS_FN_CALLS; else SAVE_REGS_FN_CALLS(true); + CALL_STACK_ALIGN_DELTA(1+(extraarg != SOURCE_DUMMY), aligndelta); + /* first pushed argument */ if (extraarg != SOURCE_DUMMY) CALL_SET_ARG_FROM_RT(extraarg, 1, 2); /* argument index 1 out of total 2 */ @@ -79,6 +84,7 @@ 4*nb_args, /* 4*nb_args */ 0); /* not used */ code += 3; + CALL_STACK_ALIGN_RESTORE(aligndelta); TEMP_RESTORE_REGS_FN_CALLS_AND_JUMP; } else @@ -109,10 +115,25 @@ int i, initial_depth; Source* p; bool ccflags; + #ifdef __APPLE__ + int aligncount=0; + #endif BEGIN_CODE /* cannot use NEED_CC(): it might clobber one of the registers mentioned in argsources */ ccflags = HAS_CCREG(po); + #ifdef __APPLE__ + /* Calculate number of registers that will be pushed by + NEED_REGISTER */ + for (i=0; istack_depth-INITIAL_STACK_DEPTH+(nbargs)*4; \ + delta = ((sp+15)&~15)-sp; \ + po->stack_depth += delta; \ + STACK_CORRECTION(delta); \ +} while (0) + +#define CALL_STACK_ALIGN(nbargs) do { \ + int delta; \ + CALL_STACK_ALIGN_DELTA(nbargs, delta); \ +} while (0) + +#define CALL_STACK_ALIGN_RESTORE(delta) do { \ + po->stack_depth -= delta; \ + STACK_CORRECTION(-delta); \ +} while (0) +#else +/* Dummy stack alignment for non-MacOS X */ +#define CALL_STACK_ALIGN_DELTA(nbargs, delta) +#define CALL_STACK_ALIGN(nbargs) +#define CALL_STACK_ALIGN_RESTORE(delta) +#endif + /* load the 'dst' register with the run-time address of 'source' which must be in the stack */ #define LOAD_ADDRESS_FROM_RT(source, dst) do { \ diff -urN psyco-dist.old/c/i386/iprocessor.c psyco-dist.new/c/i386/iprocessor.c --- psyco-dist.old/c/i386/iprocessor.c 2006-11-26 07:03:26.000000000 -0600 +++ psyco-dist.new/c/i386/iprocessor.c 2007-11-15 16:36:01.000000000 -0600 @@ -21,6 +21,10 @@ PUSH_REG_INSTR(REG_386_ESI), /* PUSH ESI */ PUSH_REG_INSTR(REG_386_EDI), /* PUSH EDI */ 0x8B, 0x5C, 0x24, 32, /* MOV EBX, [ESP+32] (finfo frame stack ptr) */ +#ifdef __APPLE__ + /* Align stack on 16-byte boundary for MacOS X */ + 0x83, 0xEC, 8, /* SUB ESP, 8 */ +#endif 0x6A, -1, /* PUSH -1 */ 0x89, 0x23, /* MOV [EBX], ESP */ 0xEB, +5, /* JMP Label2 */ @@ -31,6 +35,10 @@ 0x39, 0xCA, /* CMP EDX, ECX */ 0x75, -9, /* JNE Label1 */ 0xFF, 0xD0, /* CALL *EAX (callee removes args) */ +#ifdef __APPLE__ + /* Restore stack from 16-byte alignment on MacOS X */ + 0x83, 0xC4, 8, /* ADD ESP, 8 */ +#endif POP_REG_INSTR(REG_386_EDI), /* POP EDI */ POP_REG_INSTR(REG_386_ESI), /* POP ESI */ POP_REG_INSTR(REG_386_EBX), /* POP EBX */ @@ -67,9 +75,18 @@ 0x53, /* PUSH EBX */ 0x8B, 0x5C, 0x24, 12, /* MOV EBX, [ESP+12] (argcount) */ 0x8B, 0x44, 0x24, 8, /* MOV EAX, [ESP+8] (c_func) */ +#ifdef __APPLE__ + /* Align stack on 16-byte boundary for MacOS X */ + 0x83, 0xEC, 8, /* SUB ESP, 8 */ +#endif 0x09, 0xDB, /* OR EBX, EBX */ 0x74, +16, /* JZ Label1 */ +#ifdef __APPLE__ + /* Arguments are 8 bytes further up stack on MacOS X */ + 0x8B, 0x54, 0x24, 24, /* MOV EDX, [ESP+24] (arguments) */ +#else 0x8B, 0x54, 0x24, 16, /* MOV EDX, [ESP+16] (arguments) */ +#endif 0x8D, 0x0C, 0x9A, /* LEA ECX, [EDX+4*EBX] */ /* Label2: */ 0x83, 0xE9, 4, /* SUB ECX, 4 */ @@ -78,6 +95,10 @@ 0x75, -9, /* JNE Label2 */ /* Label1: */ 0xFF, 0xD0, /* CALL *EAX */ +#ifdef __APPLE__ + /* Restore stack from 16-byte alignment on MacOS X */ + 0x83, 0xC4, 8, /* ADD ESP, 8 */ +#endif 0x8D, 0x24, 0x9C, /* LEA ESP, [ESP+4*EBX] */ 0x5B, /* POP EBX */ 0xC3, /* RET */ diff -urN psyco-dist.old/c/ivm/iencoding.h psyco-dist.new/c/ivm/iencoding.h --- psyco-dist.old/c/ivm/iencoding.h 2006-02-13 18:21:38.000000000 -0600 +++ psyco-dist.new/c/ivm/iencoding.h 2007-11-15 16:36:01.000000000 -0600 @@ -248,6 +248,10 @@ else if (_stackcorr > 0) \ INSN_pushn(_stackcorr / sizeof(long)); \ } while (0) +/* Dummy stack alignment for non-MacOS X */ +#define CALL_STACK_ALIGN_DELTA(nbargs, delta) +#define CALL_STACK_ALIGN(nbargs) +#define CALL_STACK_ALIGN_RESTORE(delta) #define FUNCTION_RET(popbytes) do { \ INSN_ret((popbytes) / sizeof(long) + 1); /* +1 for the retaddr itself */ \