tccpe: support leading underscore for symbols
[tinycc.git] / win32 / lib / chkstk.S
blobd53b31e0a188c5ba6c82ad303ca7ffab33fe302b
1 /* ---------------------------------------------- */
2 /* chkstk86.s */
4 /* ---------------------------------------------- */
5 #ifndef TCC_TARGET_X86_64
6 /* ---------------------------------------------- */
8 .globl __chkstk
10 __chkstk:
11     xchg    (%esp),%ebp     /* store ebp, get ret.addr */
12     push    %ebp            /* push ret.addr */
13     lea     4(%esp),%ebp    /* setup frame ptr */
14     push    %ecx            /* save ecx */
15     mov     %ebp,%ecx
16 P0:
17     sub     $4096,%ecx
18     test    %eax,(%ecx)
19     sub     $4096,%eax
20     cmp     $4096,%eax
21     jge     P0
22     sub     %eax,%ecx
23     test    %eax,(%ecx)
25     mov     %esp,%eax
26     mov     %ecx,%esp
27     mov     (%eax),%ecx     /* restore ecx */
28     jmp     *4(%eax)
30 /* ---------------------------------------------- */
31 #else
32 /* ---------------------------------------------- */
34 .globl __chkstk
36 __chkstk:
37     xchg    (%rsp),%rbp     /* store ebp, get ret.addr */
38     push    %rbp            /* push ret.addr */
39     lea     8(%rsp),%rbp    /* setup frame ptr */
40     push    %rcx            /* save ecx */
41     mov     %rbp,%rcx
42     movslq  %eax,%rax
43 P0:
44     sub     $4096,%rcx
45     test    %rax,(%rcx)
46     sub     $4096,%rax
47     cmp     $4096,%rax
48     jge     P0
49     sub     %rax,%rcx
50     test    %rax,(%rcx)
52     mov     %rsp,%rax
53     mov     %rcx,%rsp
54     mov     (%rax),%rcx     /* restore ecx */
55     jmp     *8(%rax)
57 /* ---------------------------------------------- */
58 /* setjmp/longjmp support */
60 .globl tinyc_no_getbp
61 tinyc_no_getbp:
62     .byte 0x90
64 .globl tinyc_getbp
65 tinyc_getbp:
66     xor %rax,%rax
67     cmp %al,tinyc_no_getbp(%rax)
68     je t1
69     mov %rbp,%rax
70 t1:
71     ret
73 /* ---------------------------------------------- */
74 #endif
75 /* ---------------------------------------------- */
78 /* ---------------------------------------------- */
79 #ifndef TCC_TARGET_X86_64
80 /* ---------------------------------------------- */
83     int _except_handler3(
84        PEXCEPTION_RECORD exception_record,
85        PEXCEPTION_REGISTRATION registration,
86        PCONTEXT context,
87        PEXCEPTION_REGISTRATION dispatcher
88     );
90     int __cdecl _XcptFilter(
91        unsigned long xcptnum,
92        PEXCEPTION_POINTERS pxcptinfoptrs
93     );
95     struct _sehrec {
96        void *esp;                // 0
97        void *exception_pointers; // 1
98        void *prev;               // 2
99        void *handler;            // 3
100        void *scopetable;         // 4
101        int trylevel;             // 5
102        void *ebp                 // 6
103     };
105     // this is what the assembler code below means:
106     __try
107     {
108          // ...
109     }
110     __except (_XcptFilter(GetExceptionCode(), GetExceptionInformation()))
111     {
112          exit(GetExceptionCode());
113     }
116 .globl _exception_info
117 _exception_info:
118     mov 1*4-24(%ebp),%eax
119     ret
121 .globl _exception_code
122 _exception_code:
123     call _exception_info
124     mov (%eax),%eax
125     mov (%eax),%eax
126     ret
128 seh_filter:
129     call _exception_info
130     push %eax
131     call _exception_code
132     push %eax
133     call _XcptFilter
134     add $ 8,%esp
135     ret
137 seh_except:
138     mov 0*4-24(%ebp),%esp
139     call _exception_code
140     push %eax
141     call _exit
143 // msvcrt wants scopetables aligned and in read-only segment (using .text)
144 .align 4
145 seh_scopetable:
146     .long -1
147     .long seh_filter
148     .long seh_except
150 seh_handler:
151     jmp _except_handler3
153 .globl ___try__
154 ___try__:
155 .globl __try__
156 __try__:
157     push %ebp
158     mov 8(%esp),%ebp
160 //    void *esp;
161     lea 12(%esp),%eax
162     mov %eax,0*4(%ebp)
164 //    void *exception_pointers;
165     xor %eax,%eax
166     mov %eax,1*4(%ebp)
168 //    void *prev;
169     mov %fs:0,%eax
170     mov %eax,2*4(%ebp)
172 //    void *handler;
173     mov $ seh_handler,%eax
174     mov %eax,3*4(%ebp)
176 //    void *scopetable;
177     mov $ seh_scopetable,%eax
178     mov %eax,4*4(%ebp)
180 //    int trylevel;
181     xor %eax,%eax
182     mov %eax,5*4(%ebp)
184 //    register new SEH
185     lea 2*4(%ebp),%eax
186     mov %eax,%fs:0
188     pop %ebp
189     ret
191 /* ---------------------------------------------- */
192 #else
193 /* ---------------------------------------------- */
195 /* SEH on x86-64 not implemented */
197 /* ---------------------------------------------- */
198 #endif
199 /* ---------------------------------------------- */