Use glibc_likely instead __builtin_expect.
[jlayton/glibc.git] / ports / sysdeps / unix / sysv / linux / tile / makecontext.c
1 /* Copyright (C) 2011-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library.  If not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <ucontext.h>
24 #include <arch/abi.h>
25
26 void
27 __makecontext (ucontext_t *ucp, void (*func) (void), int argc, ...)
28 {
29   extern void __startcontext (void);
30   uint_reg_t *sp, *args;
31   va_list ap;
32   int i;
33
34   /* Initialize the top of stack. */
35   sp = (uint_reg_t *) ((((intptr_t) ucp->uc_stack.ss_sp
36                          + ucp->uc_stack.ss_size) & -16L) - 16);
37
38   /* Allow room for memory-passed arguments if necessary. */
39   if (argc > 10)
40     sp -= 2 + (argc - 10);
41
42   sp[0] = sp[1] = 0;
43
44   /* Set parameters.  */
45   va_start (ap, argc);
46   args = &ucp->uc_mcontext.gregs[0];
47   for (i = 0; i < argc; i++)
48     {
49       if (i == 10)
50         args = &sp[2];
51       *args++ = va_arg (ap, long);
52     }
53   va_end (ap);
54
55   /* Pass (*func) to __startcontext in pc.  */
56   ucp->uc_mcontext.pc = (long) func;
57
58   /* Set stack pointer.  */
59   ucp->uc_mcontext.sp = (long) sp;
60
61   /* Set the return address to trampoline.  */
62   ucp->uc_mcontext.lr = (long) __startcontext;
63
64   /* Pass ucp->uc_link to __startcontext in r30.  */
65   ucp->uc_mcontext.gregs[30] = (long) ucp->uc_link;
66 }
67 weak_alias (__makecontext, makecontext)