OSDN Git Service

gas/opcodes: blackfin: move dsp mac func defines to common header
[pf3gnuchains/sourceware.git] / winsup / cygwin / gendef
1 #!/usr/bin/perl
2 # Copyright 2003, 2004, 2005, 2006, 2008, 2009, 2010 Red Hat, Inc.
3 #
4 # This file is part of Cygwin.
5 #
6 # This software is a copyrighted work licensed under the terms of the
7 # Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
8 # details.
9 #
10 use strict;
11 sub cleanup(@);
12
13 my $in = shift;
14 my $tls_offsets = shift;
15 my $out = shift;
16 my $sigfe = shift;
17
18 $main::first = 0;
19 if (!defined($in) || !defined($out) || !defined($sigfe)) {
20     die "usage: $0 deffile.in cygtls.h deffile.def sigfe.s\n";
21 }
22
23 require $tls_offsets;
24
25 open(IN, $in) or die "$0: couldn't open \"$in\" - $!\n";
26 my @top = ();
27 while (<IN>) {
28     push(@top, cleanup $_);
29     last if /^\s*exports\s*$/i;
30 }
31 my $libline = cleanup scalar(<IN>);
32 my @in = cleanup <IN>;
33 close(IN);
34
35 my %sigfe = ();
36 my @data = ();
37 my @nosigfuncs = ();
38 my @text = ();
39 for (@in) {
40     chomp;
41     s/\sDATA$//o and do {
42         push @data, $_;
43         next;
44     };
45     if (/=/o) {
46         if (s/\s+NOSIGFE\s*$//) {
47             # nothing
48         } elsif (s/ SIGFE(_MAYBE)?$//) {
49             my $func = (split(' '))[2];
50             my $maybe = lc $1 . '_';
51             $sigfe{$func} = '_sigfe' . $maybe . $func;
52         }
53     } else {
54         my ($func, $sigfe) = m%^\s*(\S+)(?:\s+((?:NO)?SIGFE(?:_MAYBE)?))?$%o;
55         if (defined($sigfe) && $sigfe =~ /^NO/o) {
56             $_ = $func;
57         } else {
58             $sigfe ||= 'sigfe';
59             $_ = '_' . lc($sigfe) . '_' . $func;
60             $sigfe{$func} = $_;
61             $_ = $func . ' = ' . $_;
62         }
63     }
64     s/(\S)\s+(\S)/$1 $2/go;
65     s/(\S)\s+$/$1/o;
66     s/^\s+(\S)/$1/o;
67     push @text, $_;
68 }
69
70 for (@text) {
71     my ($alias, $func) = /^(\S+) = (\S+)\s*$/o;
72     $_ = $alias . ' = ' . $sigfe{$func}
73       if defined($func) && $sigfe{$func};
74 }
75
76 open(OUT, '>', $out) or die "$0: couldn't open \"$out\" - $!\n";
77 push @top, (map {$_ . " DATA\n"} @data), (map {$_ . "\n"} @text);
78 print OUT @top;
79 close OUT;
80
81 open(SIGFE, '>', $sigfe) or die "$0: couldn't open sigfe file \"$sigfe\" - $!\n";
82
83 for my $k (sort keys %sigfe) {
84     print SIGFE fefunc($k, $sigfe{$k});
85 }
86 close SIGFE;
87
88 sub fefunc {
89     my $func = '_' . shift;
90     my $fe = '_' . shift;
91     my $sigfe_func = ($fe =~ /^(.*)$func/)[0];
92     my $extra;
93     my $res = <<EOF;
94         .extern $func
95         .global $fe
96 $fe:
97         pushl   \$$func
98         jmp     $sigfe_func
99
100 EOF
101     if (!$main::first++) {
102         $res = <<EOF . longjmp () . $res;
103         .text
104
105 __sigfe_maybe:
106         pushl   %ebx
107         pushl   %edx
108         movl    %fs:4,%ebx                              # location of bottom of stack
109         addl    \$$tls::initialized,%ebx                # where we will be looking
110         cmpl    %ebx,%esp                               # stack loc > than tls
111         jge     0f                                      # yep.  we don't have a tls.
112         subl    \$$tls::initialized,%ebx                # where we will be looking
113         movl    $tls::initialized(%ebx),%eax
114         cmpl    \$0xc763173f,%eax                       # initialized?
115         je      1f
116 0:      popl    %edx
117         popl    %ebx
118         ret
119
120 __sigfe:
121         pushl   %ebx
122         pushl   %edx
123         movl    %fs:4,%ebx                              # location of bottom of stack
124 1:      movl    \$1,%eax                                # potential lock value
125         xchgl   %eax,$tls::stacklock(%ebx)              # see if we can grab it
126         movl    %eax,$tls::spinning(%ebx)               # flag if we are waiting for lock
127         testl   %eax,%eax                               # it will be zero
128         jz      2f                                      #  if so
129         call    _yield                                  # should be a short-time thing, so
130         jmp     1b                                      # sleep and loop
131 2:      movl    \$4,%eax                                # have the lock, now increment the
132         xadd    %eax,$tls::stackptr(%ebx)               #  stack pointer and get pointer
133         leal    __sigbe,%edx                            # new place to return to
134         xchgl   %edx,12(%esp)                           # exchange with real return value
135         movl    %edx,(%eax)                             # store real return value on alt stack
136         incl    $tls::incyg(%ebx)
137         decl    $tls::stacklock(%ebx)                   # remove lock
138         popl    %edx                                    # restore saved value
139         popl    %ebx
140         ret
141
142         .global __sigbe
143 __sigbe:                                                # return here after cygwin syscall
144         pushl   %eax                                    # don't clobber
145         pushl   %ebx                                    # tls pointer
146 1:      movl    %fs:4,%ebx                              # address of bottom of tls
147         movl    \$1,%eax                                # potential lock value
148         xchgl   %eax,$tls::stacklock(%ebx)              # see if we can grab it
149         movl    %eax,$tls::spinning(%ebx)               # flag if we are waiting for lock
150         testl   %eax,%eax                               # it will be zero
151         jz      2f                                      #  if so
152         call    _yield                                  # sleep
153         jmp     1b                                      #  and loop
154 2:      movl    \$-4,%eax                               # now decrement aux stack
155         xadd    %eax,$tls::stackptr(%ebx)               #  and get pointer
156         movl    -4(%eax),%eax                           # get return address from signal stack
157         xchgl   %eax,4(%esp)                            # swap return address with saved eax
158         decl    $tls::incyg(%ebx)
159         decl    $tls::stacklock(%ebx)                   # release lock
160         popl    %ebx
161         ret
162
163         .global _sigreturn
164 _sigreturn:
165         movl    %fs:4,%ebx
166         incl    $tls::incyg(%ebx)
167         addl    \$12,%esp                               # remove arguments
168         call    _set_process_mask\@4
169
170 1:      movl    \$1,%eax                                # potential lock value
171         xchgl   %eax,$tls::stacklock(%ebx)              # see if we can grab it
172         movl    %eax,$tls::spinning(%ebx)               # flag if we are waiting for lock
173         testl   %eax,%eax                               # it will be zero
174         jz      2f                                      #  if so
175         call    _yield                                  # sleep
176         jmp     1b                                      #  and loop
177 2:      popl    %edx                                    # saved errno
178         testl   %edx,%edx                               # Is it < 0
179         jl      3f                                      # yup.  ignore it
180         movl    $tls::errno_addr(%ebx),%eax
181         movl    %edx,(%eax)
182 3:      movl    \$-4,%eax                               # now decrement aux stack
183         xadd    %eax,$tls::stackptr(%ebx)               #  and get pointer
184         xorl    %ebp,%ebp
185         xchgl   %ebp,-4(%eax)                           # get return address from signal stack
186         xchgl   %ebp,28(%esp)                           # store real return address
187         decl    $tls::incyg(%ebx)
188         decl    $tls::stacklock(%ebx)                   # unlock
189
190         popl    %eax
191         popl    %ebx
192         popl    %ecx
193         popl    %edx
194         popl    %edi
195         popl    %esi
196         popf
197         ret
198
199         .global _sigdelayed
200 _sigdelayed:
201         pushl   %ebp
202         movl    %esp,%ebp
203         pushf
204         pushl   %esi
205         pushl   %edi
206         pushl   %edx
207         pushl   %ecx
208         pushl   %ebx
209         pushl   %eax
210         movl    %fs:4,%ebx
211 1:      movl    \$1,%eax
212         xchgl   %eax,$tls::stacklock(%ebx)
213         movl    %eax,$tls::spinning(%ebx) # flag if we are waiting for lock
214                                           # If %eax is 1 then someone else has
215                                           # the lock but we want to flag that
216                                           # we're waiting for it.  If %eax is 0
217                                           # then we're not spinning and 0 will
218                                           # reflect that.
219         testl   %eax,%eax
220         jz      2f
221         call    _yield
222         jmp     1b
223 2:      incl    $tls::incyg(%ebx)
224         pushl   $tls::saved_errno(%ebx) # saved errno
225         call    _set_process_mask_delta
226         pushl   %eax
227
228         # fill out handler arguments
229         xorl    %eax,%eax               # ucontext_t (currently not set)
230         pushl   %eax
231         leal    $tls::infodata(%ebx),%eax
232         pushl   %eax                    # siginfo
233         pushl   $tls::sig(%ebx)         # signal number
234
235         call    _reset_signal_arrived\@0
236         pushl   \$_sigreturn            # where to return
237         pushl   $tls::func(%ebx)        # user-supplied signal func
238         cmpl    \$0,$tls::threadkill(%ebx)#pthread_kill signal?
239         jnz     4f                      # yes.  callee clears signal number
240         movl    \$0,$tls::sig(%ebx)     # zero the signal number as a
241                                         # flag to the signal handler thread
242                                         # that it is ok to set up sigsave
243 4:      decl    $tls::incyg(%ebx)
244         decl    $tls::stacklock(%ebx)
245         ret                             # return via signal handler
246
247         .global __ZN7_cygtls3popEv
248 __ZN7_cygtls3popEv:
249 1:      pushl   %ebx
250         movl    %eax,%ebx                       # this
251         movl    \$-4,%eax
252         xadd    %eax,$tls::pstackptr(%ebx)
253         movl    -4(%eax),%eax
254         popl    %ebx
255         ret
256
257         .global __ZN7_cygtls4lockEv
258 __ZN7_cygtls4lockEv:
259         pushl   %ebx
260         movl    %eax,%ebx
261 1:      movl    \$1,%eax
262         xchgl   %eax,$tls::pstacklock(%ebx)
263         testl   %eax,%eax
264         jz      2f
265         call    _yield
266         jmp     1b
267 2:      popl    %ebx
268         ret
269
270         .global __ZN7_cygtls6unlockEv
271 __ZN7_cygtls6unlockEv:
272         decl    $tls::pstacklock(%eax)
273         ret
274
275         .global __ZN7_cygtls6lockedEv
276 __ZN7_cygtls6lockedEv:
277         movl    $tls::pstacklock(%eax),%eax
278         ret
279
280         .extern __ZN7_cygtls19call_signal_handlerEv
281 stabilize_sig_stack:
282         movl    %fs:4,%ebx
283 1:      movl    \$1,%eax
284         xchgl   %eax,$tls::stacklock(%ebx)
285         movl    %eax,$tls::spinning(%ebx)               # flag if we are waiting for lock
286         testl   %eax,%eax
287         jz      2f
288         call    _yield
289         jmp     1b
290 2:      incl    $tls::incyg(%ebx)
291         cmpl    \$0,$tls::sig(%ebx)
292         jz      3f
293         decl    $tls::stacklock(%ebx)                   # unlock
294         movl    \$-$tls::sizeof__cygtls,%eax            # point to beginning
295         addl    %ebx,%eax                               #  of tls block
296         call    __ZN7_cygtls19call_signal_handlerEv
297         jmp     1b
298 3:      decl    $tls::incyg(%ebx)
299         ret
300 EOF
301     }
302     return $res;
303 }
304
305 sub longjmp {
306     return <<EOF;
307
308         .globl  _setjmp
309 _setjmp:
310         pushl   %ebp
311         movl    %esp,%ebp
312         pushl   %edi
313         movl    8(%ebp),%edi
314         movl    %eax,0(%edi)
315         movl    %ebx,4(%edi)
316         movl    %ecx,8(%edi)
317         movl    %edx,12(%edi)
318         movl    %esi,16(%edi)
319         movl    -4(%ebp),%eax
320         movl    %eax,20(%edi)
321         movl    0(%ebp),%eax
322         movl    %eax,24(%edi)
323         movl    %esp,%eax
324         addl    \$12,%eax
325         movl    %eax,28(%edi)
326         movl    4(%ebp),%eax
327         movl    %eax,32(%edi)
328         movw    %es,%ax
329         movw    %ax,36(%edi)
330         movw    %fs,%ax
331         movw    %ax,38(%edi)
332         movw    %gs,%ax
333         movw    %ax,40(%edi)
334         movw    %ss,%ax
335         movw    %ax,42(%edi)
336         movl    %fs:0,%eax
337         movl    %eax,44(%edi)
338         pushl   %ebx
339         call    stabilize_sig_stack
340         movl    $tls::stackptr(%ebx),%eax               # save stack pointer contents
341         decl    $tls::stacklock(%ebx)
342         popl    %ebx
343         movl    %eax,48(%edi)
344         popl    %edi
345         movl    \$0,%eax
346         leave
347         ret
348
349         .globl  ___sjfault
350 ___sjfault:
351         pushl   %ebp
352         movl    %esp,%ebp
353         pushl   %edi
354         movl    8(%ebp),%edi
355         movl    %eax,0(%edi)
356         movl    %ebx,4(%edi)
357         movl    %ecx,8(%edi)
358         movl    %edx,12(%edi)
359         movl    %esi,16(%edi)
360         movl    -4(%ebp),%eax
361         movl    %eax,20(%edi)
362         movl    0(%ebp),%eax
363         movl    %eax,24(%edi)
364         movl    %esp,%eax
365         addl    \$12,%eax
366         movl    %eax,28(%edi)
367         movl    4(%ebp),%eax
368         movl    %eax,32(%edi)
369         movw    %es,%ax
370         movw    %ax,36(%edi)
371         movw    %fs,%ax
372         movw    %ax,38(%edi)
373         movw    %gs,%ax
374         movw    %ax,40(%edi)
375         movw    %ss,%ax
376         movw    %ax,42(%edi)
377         movl    %fs:0,%eax
378         movl    %eax,44(%edi)
379         popl    %edi
380         movl    \$0,%eax
381         leave
382         ret
383
384         .global ___ljfault
385 ___ljfault:
386         pushl   %ebp
387         movl    %esp,%ebp
388         movl    8(%ebp),%edi
389
390         movl    12(%ebp),%eax
391         testl   %eax,%eax
392         jne     0f
393         incl    %eax
394
395 0:      movl    %eax,0(%edi)
396         movl    24(%edi),%ebp
397         pushfl
398         popl    %ebx
399         movl    44(%edi),%eax
400         movl    %eax,%fs:0
401         movw    42(%edi),%ax
402         movw    %ax,%ss
403         movl    28(%edi),%esp
404         pushl   32(%edi)
405         pushl   %ebx
406         movw    36(%edi),%ax
407         movw    %ax,%es
408         movw    40(%edi),%ax
409         movw    %ax,%gs
410         movl    0(%edi),%eax
411         movl    4(%edi),%ebx
412         movl    8(%edi),%ecx
413         movl    16(%edi),%esi
414         movl    12(%edi),%edx
415         movl    20(%edi),%edi
416         popfl
417         ret
418
419         .globl  _longjmp
420 _longjmp:
421         pushl   %ebp
422         movl    %esp,%ebp
423         movl    8(%ebp),%edi                            # address of buffer
424         call    stabilize_sig_stack
425         movl    48(%edi),%eax                           # get old signal stack
426         movl    %eax,$tls::stackptr(%ebx)               # restore
427         decl    $tls::stacklock(%ebx)                   # relinquish lock
428         xorl    %eax,%eax
429         movl    %eax,$tls::incyg(%ebx)                  # we're definitely not in cygwin anymore
430
431         movl    12(%ebp),%eax
432         testl   %eax,%eax
433         jne     3f
434         incl    %eax
435
436 3:      movl    %eax,0(%edi)
437         movl    24(%edi),%ebp
438         pushfl
439         popl    %ebx
440         movl    44(%edi),%eax
441         movl    %eax,%fs:0
442         movw    42(%edi),%ax
443         movw    %ax,%ss
444         movl    28(%edi),%esp
445         pushl   32(%edi)
446         pushl   %ebx
447         movw    36(%edi),%ax
448         movw    %ax,%es
449         movw    40(%edi),%ax
450         movw    %ax,%gs
451         movl    0(%edi),%eax
452         movl    4(%edi),%ebx
453         movl    8(%edi),%ecx
454         movl    16(%edi),%esi
455         movl    12(%edi),%edx
456         movl    20(%edi),%edi
457         popfl
458         ret
459 EOF
460 }
461
462 sub cleanup(@) {
463     map {s/\r//g; $_} @_;
464     map {s/#.*//g; $_} @_;
465     map {s/[ \t]+$//g; $_} @_;
466 }