Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Trunk/i386/include/architecture/ppc/asm_help.h

1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 *
5 * This file contains Original Code and/or Modifications of Original Code
6 * as defined in and that are subject to the Apple Public Source License
7 * Version 2.0 (the 'License'). You may not use this file except in
8 * compliance with the License. Please obtain a copy of the License at
9 * http://www.opensource.apple.com/apsl/ and read it before using this
10 * file.
11 *
12 * The Original Code and all software distributed under the License are
13 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
17 * Please see the License for the specific language governing rights and
18 * limitations under the License.
19 *
20 * Copyright (c) 1996 NeXT Software, Inc. All rights reserved.
21 *
22 *File:architecture/ppc/asm_help.h
23 *Author:Mike DeMoney, NeXT Software, Inc.
24 *
25 *This header file defines macros useful when writing assembly code
26 *for the PowerPC processors.
27 *r12 is used as the tmp register / PICIFY base.
28 *
29 * HISTORY
30 * 20-May-97 Umesh Vaishampayan (umeshv@apple.com)
31 *Implemented Dynamic / PIC macros.
32 *
33 * 28-Dec-96 Umesh Vaishampayan (umeshv@NeXT.com)
34 *added ".align" directive to various macros to avoid alignment
35 * faults. Moved Register Usage #defines to reg_help.h as that's
36 *where they should have been in the first place.
37 *Added Dynamic / PIC macroes for routines which refernce external
38 *symbols. Not implemented fully as yet.
39 *
40 * 05-Nov-92 Mike DeMoney (mike@next.com)
41 *Created.
42 */
43
44#ifndef_ARCH_PPC_ASM_HELP_H_
45#define_ARCH_PPC_ASM_HELP_H_
46
47#include<architecture/ppc/reg_help.h>
48
49#ifdef__ASSEMBLER__
50/*
51 * ppc stack frames look like this after procedure prolog has
52 * been executed:
53 *
54 * Higher address:
55 *.........
56 *+-------------------------------+
57 * | caller's LR|
58 *+-------------------------------+
59 * | caller's CR|
60 *+-------------------------------+
61 * Caller's SP->| caller's caller's sp| ^^ Caller's Frame ^^
62 *+===============================+ vv Called Rtn Frame vv
63 *|Save Area for| FPF 31
64 *..........
65 *| Caller's FPF's| FPF n
66 *+-------------------------------+
67 *|Save Area for| GRF 31
68 *..........
69 *| Caller's GRF's| GRF n
70 *+-------------------------------+
71 *|alignment pad|
72 *............
73 *|(if necessary)|
74 *+-------------------------------+
75 *|Local|
76 *........
77 *| Variables|
78 *+-------------------------------+
79 * SP + X ->| aN for FUTURE call|
80 *+-------------------------------+
81 *..........
82 *+-------------------------------+
83 * SP + 28 ->| a1 for FUTURE call|
84 *+-------------------------------+
85 * SP + 24 ->| a0 for FUTURE call|
86 *+-------------------------------+
87 * SP + 20 ->| caller's TOC|
88 *+-------------------------------+
89 * SP + 16 ->| reserved|
90 *+-------------------------------+
91 * SP + 12 ->| reserved|
92 *+-------------------------------+
93 * SP + 8 ->| LR callee-save for FUTURE call|
94 *+-------------------------------+
95 * SP + 4 ->| CR callee-save for FUTURE call|
96 *+-------------------------------+
97 * SP ->| caller's sp|
98 *+===============================+
99 * Lower address:
100 *
101 * NOTE: All state with the exception of LR and CR are saved in the
102 * called routines frame. LR and CR are saved in the CALLER'S FRAME.
103 *
104 * ALSO NOTE: Args to the called routine are found in the caller's frame.
105 */
106
107/*
108 * ARG(n) -- stack offset to n'th argument
109 *
110 * NOTE CAREFULLY! These macros start numbering arguments at 1 (NOT 0)
111 * The first argument is ARG(1).
112 *
113 * ALSO NOTE: This stack offset is only valid if using routine
114 * DOES NOT alter SP.
115 *
116 */
117#defineARG(n)((((n) - 1) * 4) + 24)
118
119/*
120 * Macros for building stack frame according to C calling conventions.
121 * lr, cr, and sp are saved.
122 *
123 * NOTE WELL: localvarsize is in bytes, maxargsout is a count of words,
124 * grfsaved and fpfsaved is a count of registers. BE SURE TO COUNT
125 * BOTH FP (r31) AND sN REGISTERS IN THE COUNT OF GRF REGISTERS SAVED!
126 * This will be TWO more than the N of the highest sN register you
127 * save: s2 implies you are saving s2, s1, s0, and fp => grfsaved
128 * should be 4!
129 *
130 * FURTHER NOTE: These macros do NOT SAVE GRF or FPF registers. User
131 * must do that. GRF sN regs should be saved via
132 *stmwsN,SAVED_GRF_S(N)(sp)
133 * where N is the highest numbered s* register to be saved. E.g. if
134 * s0, s1, and s2 are to be saved use:
135 *stmws2,SAVED_GRF_S(2)(sp)
136 * Note that this also saves fp.
137 * An individual saved grf can be loaded via:
138 *lwzs2,SAVED_GRF_S(2)(sp)
139 * Analogous stuff works for fpf's.
140 *
141 * NOTE: these simple routines will be replaced with more complicated
142 * ones once we know what the linker and gdb will require as for as
143 * register use masks and frame declarations.
144 *
145 * Warning: ROUND_TO_STACK is only to be used in assembly language;
146 * for C usage, use ROUND_FRAME() in reg_help.h.
147 */
148#defineROUND_TO_STACK(len)\
149(((len) + STACK_INCR - 1) / STACK_INCR * STACK_INCR)
150
151#defineBUILD_FRAME(localvarsize, maxargsout, grfsaved, fpfsaved)\
152.set__argoutsize, ROUND_TO_STACK((maxargsout) * 4)@\
153.if__argoutsize < 32@\
154 .set__argoutsize,32@\
155.endif@\
156.set__framesize, ROUND_TO_STACK(\
15724 + __argoutsize + (localvarsize)\
158+ 4*(grfsaved) + 8*(fpfsaved))@\
159.set__grfbase,(__framesize - 4*(grfsaved) - 8*(fpfsaved))@\
160.set__fpfbase,(__framesize - 8*(fpfsaved))@\
161mflrr0@\
162mfcrr12@\
163stwr0,8(sp)@\
164stwr12,4(sp)@\
165stwur1,-__framesize(r1)
166
167/*
168 * Macros for referencing data in stack frame.
169 *
170 * NOTE WELL: ARG's and VAR's start at 1, NOT 0. Why ??? (FIXME)
171 */
172#defineLOCAL_VAR(n)(((n)-1)*4 + __argoutsize + 24)
173#defineSAVED_GRF_S(n)(__grfbase + ((grfsaved) - (n) - 2) * 4)
174#defineSAVED_FRF_FS(n)(__fpfbase + ((fpfsaved) - (n) - 1) * 4)
175#defineARG_IN(n)(ARG(n) + __framesize)
176#defineARG_OUT(n)(ARG(n) + 0)
177#defineSAVED_FP(__grfbase + ((grfsaved) - 1) * 4)
178#defineSAVED_LR(__framesize + 8)
179#defineSAVED_CR(__framesize + 4)
180
181/*
182 * Macros for unwinding stack frame.
183 * NOTE: GRF's and FPF's are NOT RESTORED. User must do this before
184 * using this macro.
185 */
186#defineRETURN\
187.if__framesize@\
188 lwz32r0,r1,SAVED_LR@\
189 lwz32r12,r1,SAVED_CR@\
190 addicsp,r1,__framesize@\
191 mtlrr0@\
192 mtcrf0xff,r12@\
193 blr@\
194.else@\
195 blr@\
196.endif
197
198
199/*
200 * Macros for declaring procedures
201 *
202 * Use of these macros allows ctags to have a predictable way
203 * to find various types of declarations. They also simplify
204 * inserting appropriate symbol table information.
205 *
206 * NOTE: these simple stubs will be replaced with more
207 * complicated versions once we know what the linker and gdb
208 * will require as far as register use masks and frame declarations.
209 * These macros may also be ifdef'ed in the future to contain profiling
210 * code.
211 *
212 * FIXME: Document what makes a leaf a LEAF and a handler a HANDLER.
213 * (E.g. leaf's have return pc in lr, NESTED's have rpc in offset off
214 * sp, handlers have rpc in exception frame which is found via exception
215 * link, etc etc.)
216 */
217
218/*
219 * TEXT -- declare start of text segment
220 */
221#defineTEXT\
222.text@\
223.align 2
224
225/*
226 * LEAF -- declare global leaf procedure
227 * NOTE: Control SHOULD NOT FLOW into a LEAF! A LEAF should only
228 * be jumped to. (A leaf may do an align.) Use a LABEL() if you
229 * need control to flow into the label.
230 */
231#defineLEAF(name)\
232.align 2@\
233.globlname@\
234name:@\
235.set__framesize,0
236
237/*
238 * X_LEAF -- declare alternate global label for leaf
239 */
240#defineX_LEAF(name, value)\
241.globlname@\
242.setname,value
243
244/*
245 * P_LEAF -- declare private leaf procedure
246 */
247#defineP_LEAF(name)\
248.align 2@\
249name:@\
250.set__framesize,0
251
252/*
253 * LABEL -- declare a global code label
254 * MUST be used (rather than LEAF, NESTED, etc) if control
255 * "flows into" the label.
256 */
257#defineLABEL(name)\
258.align 2@\
259.globlname@\
260name:
261
262/*
263 * NESTED -- declare procedure that invokes other procedures
264 */
265#defineNESTED(name, localvarsize, maxargsout, grfsaved, fpfsaved)\
266.align 2@\
267.globlname@\
268name:@\
269BUILD_FRAME(localvarsize, maxargsout, grfsaved, fpfsaved)
270
271/*
272 * X_NESTED -- declare alternate global label for nested proc
273 */
274#defineX_NESTED(name, value)\
275.globlname@\
276.setname,value
277
278/*
279 * P_NESTED -- declare private nested procedure
280 */
281#defineP_NESTED(name, localvarsize, maxargsout, grfsaved, fpfsaved)\
282.align 2@\
283name:@\
284BUILD_FRAME(locavarsize, maxargsout, grfsaved, fpfsaved)
285
286/*
287 * HANDLER -- declare procedure with exception frame rather than
288 * standard C frame
289 */
290#defineHANDLER(name)\
291.align 2@\
292.globlname@\
293name:
294
295/*
296 * X_HANDLER -- declare alternate name for exception handler
297 * (Should appear immediately before a HANDLER declaration or
298 * another X_HANDLER declaration)
299 */
300#defineX_HANDLER(name)\
301.align 2@\
302.globlname@\
303name:
304
305/*
306 * P_HANDLER -- declare private handler
307 */
308#defineP_HANDLER(name)\
309.align 2@\
310name:
311
312/*
313 * END -- mark end of procedure
314 * FIXME: Unimplemented for now.
315 */
316#defineEND(name)
317
318/*
319 * BL -- call procedure (relative)
320 */
321#defineBL(name)\
322blname
323
324/*
325 * Storage definition macros
326 * The main purpose of these is to allow an easy handle for ctags
327 */
328
329/*
330 * IMPORT -- import symbol
331 */
332#defineIMPORT(name)\
333.referencename
334
335/*
336 * ABS -- declare global absolute symbol
337 */
338#defineABS(name, value)\
339.globlname@\
340.setname,value
341
342/*
343 * P_ABS -- declare private absolute symbol
344 */
345#defineP_ABS(name, value)\
346.setname,value
347
348/*
349 * EXPORT -- declare global label for data
350 */
351#defineEXPORT(name)\
352.align 2@\
353.globlname@\
354name:
355
356/*
357 * BSS -- declare global zero'ed storage
358 */
359#defineBSS(name,size)\
360.commname,size
361
362
363/*
364 * P_BSS -- declare private zero'ed storage
365 */
366#defineP_BSS(name,size)\
367.lcommname,size
368
369/*
370 * dynamic/PIC macros for routines which reference external symbols
371 */
372#if defined(__DYNAMIC__)
373#define PICIFY_REG r12
374
375/* Assume that the lr is saved before calling any of these macros */
376/* using PICIFY() */
377
378#define PICIFY(var)\
379mflrr0@\
380bcl20,31,1f@\
3811:mflrPICIFY_REG@\
382mtlrr0@\
383addisPICIFY_REG, PICIFY_REG, ha16(L ## var ## __non_lazy_ptr - 1b) @\
384lwzPICIFY_REG, lo16(L ## var ## __non_lazy_ptr - 1b)(PICIFY_REG)
385
386#define CALL_EXTERN_AGAIN(var)\
387PICIFY(var)@\
388mtctrPICIFY_REG@\
389mflrr0@\
390stwr0,8(r1)@\
391stwur1,-64(r1)@\
392bctrl@\
393addicr1,r1,64 @\
394lwzr0,8(r1)@\
395mtlrr0
396
397#define NON_LAZY_STUB(var)\
398.non_lazy_symbol_pointer@\
399.align 2@\
400L ## var ## __non_lazy_ptr:@\
401.indirect_symbol var@\
402.long 0@\
403.text@\
404.align 2
405
406#defineBRANCH_EXTERN(var)\
407PICIFY(var)@\
408mtctrPICIFY_REG@\
409bctr@\
410NON_LAZY_STUB(var)
411
412#define CALL_EXTERN(var)\
413CALL_EXTERN_AGAIN(var)@\
414NON_LAZY_STUB(var)
415
416#define REG_TO_EXTERN(reg, var)\
417PICIFY(var)@\
418stw reg, 0(PICIFY_REG)@\
419NON_LAZY_STUB(var)
420
421#define EXTERN_TO_REG(reg, var)\
422PICIFY(var)@\
423lwzreg, 0(PICIFY_REG)@\
424NON_LAZY_STUB(var)
425
426#else /* ! __DYNAMIC__ */
427#define TMP_REG r12
428#define BRANCH_EXTERN(var)\
429bvar
430
431#define CALL_EXTERN(var)\
432blvar
433
434#define CALL_EXTERN_AGAIN(var)\
435CALL_EXTERN(var)
436
437#define REG_TO_EXTERN(reg, var)\
438lisTMP_REG, ha16(var)@\
439stwreg, lo16(var)(TMP_REG)
440
441#define EXTERN_TO_REG(reg, var)\
442lisreg, ha16(var)@\
443lwzreg, lo16(var)(reg)
444
445#endif/* __DYNAMIC__ */
446
447#endif/* __ASSEMBLER__ */
448#endif/* _ARCH_PPC_ASM_HELP_H_ */
449

Archive Download this file

Revision: 2045