Rewrote some headers from scratch to avoid EULA/patent concerns.
[wine] / include / stackframe.h
1 /*
2  * 16-bit and 32-bit mode stack frame layout
3  *
4  * Copyright 1995, 1998 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #ifndef __WINE_STACKFRAME_H
22 #define __WINE_STACKFRAME_H
23
24 #include <string.h>
25
26 #include "thread.h"
27 #include "winnt.h"
28 #include "wine/winbase16.h"
29
30 #include "pshpack1.h"
31
32   /* 32-bit stack layout after CallTo16() */
33 typedef struct _STACK32FRAME
34 {
35     SEGPTR  frame16;        /* 00 16-bit frame from last CallFrom16() */
36     DWORD   restore_addr;   /* 04 return address for restoring code selector */
37     DWORD   codeselector;   /* 08 code selector to restore */
38     DWORD   edi;            /* 0c saved registers */
39     DWORD   esi;            /* 10 */
40     DWORD   edx;            /* 14 */
41     DWORD   ecx;            /* 18 */
42     DWORD   ebx;            /* 1c */
43     DWORD   ebp;            /* 20 saved 32-bit frame pointer */
44     DWORD   retaddr;        /* 24 return address */
45     DWORD   target;         /* 28 target address / CONTEXT86 pointer */
46     DWORD   nb_args;        /* 2c number of 16-bit argument bytes */
47 } STACK32FRAME;
48
49   /* 16-bit stack layout after CallFrom16() */
50 typedef struct _STACK16FRAME
51 {
52     STACK32FRAME *frame32;        /* 00 32-bit frame from last CallTo16() */
53     DWORD         edx;            /* 04 saved registers */
54     DWORD         ecx;            /* 08 */
55     DWORD         ebp;            /* 0c */
56     WORD          ds;             /* 10 */
57     WORD          es;             /* 12 */
58     WORD          fs;             /* 14 */
59     WORD          gs;             /* 16 */
60     DWORD         callfrom_ip;    /* 18 callfrom tail IP */
61     DWORD         module_cs;      /* 1c module code segment */
62     DWORD         relay;          /* 20 relay function address */
63     WORD          entry_ip;       /* 22 entry point IP */
64     DWORD         entry_point;    /* 26 API entry point to call, reused as mutex count */
65     WORD          bp;             /* 2a 16-bit stack frame chain */
66     WORD          ip;             /* 2c return address */
67     WORD          cs;             /* 2e */
68 } STACK16FRAME;
69
70 #include "poppack.h"
71
72 #define THREAD_STACK16(teb)  ((STACK16FRAME*)MapSL((teb)->cur_stack))
73 #define CURRENT_STACK16      (THREAD_STACK16(NtCurrentTeb()))
74 #define CURRENT_DS           (CURRENT_STACK16->ds)
75
76 /* varargs lists on the 16-bit stack */
77
78 typedef void *VA_LIST16;
79
80 #define __VA_ROUNDED16(type) \
81     ((sizeof(type) + sizeof(WORD) - 1) / sizeof(WORD) * sizeof(WORD))
82 #define VA_START16(list) ((list) = (VA_LIST16)(CURRENT_STACK16 + 1))
83 #define VA_ARG16(list,type) \
84     (((list) = (VA_LIST16)((char *)(list) + __VA_ROUNDED16(type))), \
85      *((type *)(void *)((char *)(list) - __VA_ROUNDED16(type))))
86 #define VA_END16(list) ((void)0)
87
88
89 /* Push bytes on the 16-bit stack of a thread;
90  * return a segptr to the first pushed byte
91  */
92 static inline SEGPTR WINE_UNUSED stack16_push( int size )
93 {
94     TEB *teb = NtCurrentTeb();
95     STACK16FRAME *frame = THREAD_STACK16(teb);
96     memmove( (char*)frame - size, frame, sizeof(*frame) );
97     teb->cur_stack -= size;
98     return (SEGPTR)(teb->cur_stack + sizeof(*frame));
99 }
100
101 /* Pop bytes from the 16-bit stack of a thread */
102 static inline void WINE_UNUSED stack16_pop( int size )
103 {
104     TEB *teb = NtCurrentTeb();
105     STACK16FRAME *frame = THREAD_STACK16(teb);
106     memmove( (char*)frame + size, frame, sizeof(*frame) );
107     teb->cur_stack += size;
108 }
109
110 /* Push a DWORD on the 32-bit stack */
111 static inline void WINE_UNUSED stack32_push( CONTEXT86 *context, DWORD val )
112 {
113     context->Esp -= sizeof(DWORD);
114     *(DWORD *)context->Esp = val;
115 }
116
117 /* Pop a DWORD from the 32-bit stack */
118 static inline DWORD WINE_UNUSED stack32_pop( CONTEXT86 *context )
119 {
120     DWORD ret = *(DWORD *)context->Esp;
121     context->Esp += sizeof(DWORD);
122     return ret;
123 }
124
125 #endif /* __WINE_STACKFRAME_H */