|
2 | 2 | * this is the internal transfer function.
|
3 | 3 | *
|
4 | 4 | * HISTORY
|
| 5 | + * 06-Nov-16 Anselm Kruis <a.kruis@atos.net> |
| 6 | + * Reworked based on the i386 ABI spec. |
5 | 7 | * 24-Nov-02 Christian Tismer <tismer@tismer.com>
|
6 | 8 | * needed to add another magic constant to insure
|
7 | 9 | * that f in slp_eval_frame(PyFrameObject *f)
|
|
23 | 25 |
|
24 | 26 | #ifdef SLP_EVAL
|
25 | 27 |
|
26 |
| -/* #define STACK_MAGIC 3 */ |
27 |
| -/* the above works fine with gcc 2.96, but 2.95.3 wants this */ |
28 | 28 | #define STACK_MAGIC 0
|
29 | 29 |
|
30 |
| -static int |
| 30 | +/* |
| 31 | + * In order to switch the stack, we use the fact, that the compiler |
| 32 | + * already knows how to preserve registers accross function calls. |
| 33 | + * |
| 34 | + * The relevant i386 ABI specifigation pecisely defines which registers |
| 35 | + * must be preserved and which registers may be modified. |
| 36 | + * We use a gcc inline assembly feature to pretend that the inline |
| 37 | + * assembly block modifies the registers to be preserved. As a result, |
| 38 | + * the compiler emits code to preserve those registers. |
| 39 | + * |
| 40 | + * The "System V Application Binary Interface Intel386 Architecture Processor Supplment, Fourth Edition" |
| 41 | + * Section 3 chapter "Function Calling Sequence" states: |
| 42 | + * All registers on the Intel386 are global and thus visible to both a calling and a |
| 43 | + * called function. Registers %ebp, %ebx, %edi, %esi, and %esp "belong" to the cal- |
| 44 | + * ling function. In other words, a called function must preserve these registers’ |
| 45 | + * values for its caller. Remaining registers ‘‘belong’’ to the called function. |
| 46 | + * |
| 47 | + * The compiler always preserves the %esp register accros a function call. |
| 48 | + * |
| 49 | + * Depending on the usage of a frame pointer, which is optional |
| 50 | + * for the i386 ABI, the compiler already preserves the %ebp |
| 51 | + * register. Unfortunately, we must not add "ebp" to the clobber list, if |
| 52 | + * ebp is used as a frame pointer (won't compile). Therefore we save |
| 53 | + * ebp manually. |
| 54 | + * |
| 55 | + * For the other registers we tell the compiler, |
| 56 | + * that we are going to clobber the registers. The compiler will then save the registers |
| 57 | + * for us. (However the compiler gives no guarantee, when it will restore |
| 58 | + * the registers.) And the compiler only preserves those registers, that must |
| 59 | + * be preserved according to the calling convention. It does not preserve any other |
| 60 | + * register, that may be modified during a function call. Therefore specifying additional |
| 61 | + * registers has no effect at all. Take a look at the generated assembly code! |
| 62 | + */ |
| 63 | + |
| 64 | +/* Registers marked as clobbered, minimum set according to the ABI spec. */ |
| 65 | +#define REGS_CLOBBERED "ebx", "edi", "esi" |
| 66 | + |
| 67 | +/* |
| 68 | + * You may want to make the function static enable optimizations. |
| 69 | + * However, the ABI SPEC does not apply to static functions. Therefore |
| 70 | + * I make slp_switch a regular global function. |
| 71 | + */ |
| 72 | +#if 0 |
| 73 | +static |
| 74 | +#endif |
| 75 | +int |
31 | 76 | slp_switch(void)
|
32 | 77 | {
|
33 | 78 | register int *stackref, stsizediff;
|
34 | 79 | #if STACKLESS_FRHACK
|
35 |
| - __asm__ volatile ("" : : : "esi", "edi"); |
| 80 | + __asm__ volatile ( |
| 81 | + "" |
| 82 | + : : : REGS_CLOBBERED ); |
36 | 83 | #else
|
37 |
| - __asm__ volatile ("" : : : "ebx", "esi", "edi"); |
| 84 | + void * ebp; |
| 85 | + __asm__ volatile ( |
| 86 | + "movl %%ebp, %0\n\t" |
| 87 | + : "=m" (ebp) : : REGS_CLOBBERED ); |
38 | 88 | #endif
|
39 | 89 | __asm__ ("movl %%esp, %0" : "=g" (stackref));
|
40 | 90 | {
|
41 | 91 | SLP_SAVE_STATE(stackref, stsizediff);
|
42 | 92 | __asm__ volatile (
|
43 |
| - "addl %0, %%esp\n" |
44 |
| - "addl %0, %%ebp\n" |
| 93 | + "addl %0, %%esp\n\t" |
| 94 | + "addl %0, %%ebp\n\t" |
45 | 95 | :
|
46 | 96 | : "r" (stsizediff)
|
47 | 97 | );
|
48 | 98 | SLP_RESTORE_STATE();
|
| 99 | +#if ! STACKLESS_FRHACK |
| 100 | + __asm__ volatile ( |
| 101 | + "movl %0, %%ebp\n\t" |
| 102 | + : : "m" (ebp) ); |
| 103 | +#endif |
49 | 104 | return 0;
|
50 | 105 | }
|
51 |
| -#if STACKLESS_FRHACK |
52 |
| - __asm__ volatile ("" : : : "esi", "edi"); |
53 |
| -#else |
54 |
| - __asm__ volatile ("" : : : "ebx", "esi", "edi"); |
55 |
| -#endif |
56 | 106 | }
|
57 | 107 |
|
58 | 108 |
|
|
0 commit comments