1/*
2            Copyright Oliver Kowalke 2009.
3   Distributed under the Boost Software License, Version 1.0.
4      (See accompanying file LICENSE_1_0.txt or copy at
5          http://www.boost.org/LICENSE_1_0.txt)
6*/
7
8/*******************************************************
9 *                                                     *
10 *  -------------------------------------------------  *
11 *  |  0  |  4  |  8  |  12 |  16 |  20 |  24 |  28 |  *
12 *  -------------------------------------------------  *
13 *  |bchai|  CR | LR  |compl| link| TOC | R14 | R15 |  *
14 *  -------------------------------------------------  *
15 *  -------------------------------------------------  *
16 *  |  32 |  36 |  40 |  44 |  48 |  52 |  56 |  60 |  *
17 *  -------------------------------------------------  *
18 *  | R16 | R17 | R18 | R19 | R20 | R21 | R22 | R23 |  *
19 *  -------------------------------------------------  *
20 *  -------------------------------------------------  *
21 *  |  64 |  68 |  72 |  76 |  80 |  84 |  88 |  92 |  *
22 *  -------------------------------------------------  *
23 *  | R24 | R25 | R26 | R27 | R28 | R29 | R30 | R31 |  *
24 *  -------------------------------------------------  *
25 *  -------------------------------------------------  *
26 *  |  96 | 100 | 104 | 108 | 112 | 116 | 120 | 124 |  *
27 *  -------------------------------------------------  *
28 *  |    F14    |    F15    |    F16    |    F17    |  *
29 *  -------------------------------------------------  *
30 *  -------------------------------------------------  *
31 *  | 128 | 132 | 136 | 140 | 144 | 148 | 152 | 156 |  *
32 *  -------------------------------------------------  *
33 *  |    F18    |    F19    |    F20    |    F21    |  *
34 *  -------------------------------------------------  *
35 *  -------------------------------------------------  *
36 *  | 160 | 164 | 168 | 172 | 176 | 180 | 184 | 188 |  *
37 *  -------------------------------------------------  *
38 *  |    F22    |    F23    |    F24    |    F25    |  *
39 *  -------------------------------------------------  *
40 *  -------------------------------------------------  *
41 *  | 192 | 196 | 200 | 204 | 208 | 212 | 216 | 220 |  *
42 *  -------------------------------------------------  *
43 *  |    F26    |    F27    |    F28    |    F29    |  *
44 *  -------------------------------------------------  *
45 *  -------------------------------------------------  *
46 *  | 224 | 228 | 232 | 236 | 240 | 244 | 248 | 252 |  *
47 *  -------------------------------------------------  *
48 *  |    F30    |    F31    |  PC |hiddn|  fpscr    |  *
49 *  -------------------------------------------------  *
50 *  -------------------------------------------------  *
51 *  | 256 | 260 | 264 | 268 | 272 | 276 | 280 | 284 |  *
52 *  -------------------------------------------------  *
53 *  |bchai|savLR|savLR|compl| link|svTOC| FCTX| DATA|  *
54 *  -------------------------------------------------  *
55 *                                                     *
56 *******************************************************/
57    .file "make_ppc32_sysv_xcoff_xas.S"
58    .toc
59    .csect .text[PR]
60    .align 2
61    .globl  make_fcontext[DS]
62    .globl .make_fcontext
63    .csect  make_fcontext[DS]
64make_fcontext:
65    .long .make_fcontext[PR], TOC[tc0], 0
66    .csect .text[PR], 5
67.make_fcontext:
68    # save return address into R6
69    mflr  6
70
71    # first arg of make_fcontext() == top address of context-function
72    # shift address in R3 to lower 16 byte boundary
73    clrrwi  3, 3, 4
74
75    # reserve space for context-data on context-stack
76    # including 32 byte of linkage + parameter area (R1 % 16 == 0)
77    subi  3, 3, 288
78
79    # third arg of make_fcontext() == address of context-function descriptor
80    lwz   4, 0(5)
81    stw  4, 240(3)
82    # save TOC of context-function
83    lwz   4, 4(5)
84    stw  4, 20(3)
85
86    # set back-chain to zero
87    li   0, 0
88    stw  0, 256(3)
89
90    # zero in r3 indicates first jump to context-function
91    std  0, 244(3)
92
93    # load LR
94    mflr  0
95    # jump to label 1
96    bl  .Label
97.Label:
98    # load LR into R4
99    mflr  4
100    # compute abs address of label .L_finish
101    addi  4, 4, .L_finish - .Label
102    # restore LR
103    mtlr  0
104    # save address of finish as return-address for context-function
105    # will be entered after context-function returns
106    stw  4, 8(3)
107
108    # restore return address from R6
109    mtlr  6
110
111    blr  # return pointer to context-data
112
113.L_finish:
114    # save return address into R0
115    mflr  0
116    # save return address on stack, set up stack frame
117    stw  0, 4(1)
118    # allocate stack space, R1 % 16 == 0
119    stwu  1, -16(1)
120
121    # exit code is zero
122    li  3, 0
123    # exit application
124    bl  ._exit
125    nop
126