Subversion Repositories tendra.SVN

Rev

Rev 5 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
6 7u83 2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
33
 
34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
42
 
43
	(1) Its Recipients shall ensure that this Notice is
44
	reproduced upon any copies or amended versions of it;
45
 
46
	(2) Any amended version of it shall be clearly marked to
47
	show both the nature of and the organisation responsible
48
	for the relevant amendment or amendments;
49
 
50
	(3) Its onward transfer from a recipient to another
51
	party shall be deemed to be that party's acceptance of
52
	these conditions;
53
 
54
	(4) DERA gives no warranty or assurance as to its
55
	quality or suitability for any purpose and DERA accepts
56
	no liability whatsoever in relation to any use to which
57
	it may be put.
58
*/
59
 
60
 
61
/*
62
$Log: frames.c,v $
63
 * Revision 1.1.1.1  1998/01/17  15:56:02  release
64
 * First version to be checked into rolling release.
65
 *
66
 * Revision 1.6  1996/11/14  15:22:13  wfs
67
 *    Fixed a bug in regexps.c which was common to most of the installers and
68
 * has only just come to light due to PWE's work on powertrans. (There was
69
 * previously only a patch.) Cosmetic changes to other files.
70
 *
71
 * Revision 1.5  1996/10/24  15:51:14  wfs
72
 * Added "~alloc_size" special token. Minor change to alloca_tag - only need
73
 * one word for storage of pointer. Major change to round_tag (rounding to
74
 * unsigned chars) in the fix of avs bug.
75
 *
76
 * Revision 1.4  1996/09/06  10:38:19  wfs
77
 * bug fixes to "offset.pl" and cosmetic changes to the dynamic initialization
78
 * code.
79
 *
80
 * Revision 1.3  1996/09/05  11:05:08  wfs
81
 * "dynamic_init" boolean variable removed - must always be considered true.
82
 *
83
 * Revision 1.2  1995/12/18  13:11:19  wfs
84
 * Put hppatrans uder cvs control. Major Changes made since last release
85
 * include:
86
 * (i) PIC code generation.
87
 * (ii) Profiling.
88
 * (iii) Dynamic Initialization.
89
 * (iv) Debugging of Exception Handling and Diagnostics.
90
 *
91
 * Revision 5.5  1995/10/11  15:52:57  wfs
92
 * "Has_no_vcallers" was defined incorrectly.
93
 *
94
 * Revision 5.4  1995/10/09  13:04:27  wfs
95
 * Cosmetic changes.
96
 *
97
 * Revision 5.3  1995/09/15  14:47:07  wfs
98
 * Removed "#include "extra_expmacs.h"".
99
 *
100
 * Revision 5.2  1995/09/15  12:20:09  wfs
101
 * "extra_expmacs.h" included + minor variable name changes and
102
 * initializations.
103
 *
104
 * Revision 5.1  1995/09/07  15:41:49  wfs
105
 * Removed "extra_expmacs.h" include.
106
 *
107
 * Revision 5.0  1995/08/25  13:42:58  wfs
108
 * Preperation for August 25 Glue release
109
 *
110
 * Revision 3.4  1995/08/25  09:20:13  wfs
111
 * All the stuff (formerly in "proc.c") which set up the variables
112
 * for stack frames has been moved to frames.c. Substantial comments
113
 * have been added.
114
 *
115
 * Revision 3.4  1995/08/25  09:20:13  wfs
116
 * All the stuff (formerly in "proc.c") which set up the variables
117
 * for stack frames has been moved to frames.c. Substantial comments
118
 * have been added.
119
 *
120
 * Revision 3.1  95/04/10  16:26:26  16:26:26  wfs (William Simmonds)
121
 * Apr95 tape version.
122
 *
123
 * Revision 3.0  95/03/30  11:17:06  11:17:06  wfs (William Simmonds)
124
 * Mar95 tape version with CRCR95_178 bug fix.
125
 *
126
 * Revision 2.0  95/03/15  15:26:53  15:26:53  wfs (William Simmonds)
127
 * spec 3.1 changes implemented, tests outstanding.
128
 *
129
 * Revision 1.1  95/01/11  13:06:47  13:06:47  wfs (William Simmonds)
130
 * Initial revision
131
 *
132
*/
133
 
134
 
135
#include "config.h"
136
#include "bitsmacs.h"
137
#include "common_types.h"
138
#include "expmacs.h"
139
#include "procrec.h"
140
#include "tags.h"
141
#include "addrtypes.h"
142
#include "hppains.h"
143
#include "regmacs.h"
144
#include "flags.h"
145
#include "frames.h"
146
 
147
extern char *proc_name;
6 7u83 148
extern int bitsin(long);
149
extern exp father(exp);
2 7u83 150
extern int diagnose;
151
extern int gdb;
152
 
153
bool Has_fp = 0;
154
bool Has_vsp = 0;
155
bool Has_tos = 0;
156
bool No_S = 0;
157
bool Has_ll = 0;
158
bool Has_checkalloc = 0;
159
bool Has_callees = 0;
160
bool Has_vcallees = 0;
161
bool Has_no_vcallers = 0;
162
bool Uses_crt_env = 0;
163
bool is_PIC_and_calls = 0;
164
bool plusZ;
165
int fixdump = 0;
166
long callee_sz;
167
long callees_offset;
168
long locals_offset;
169
long max_args;
170
long params_offset;
171
long locals_space;
172
long frame_sz;
173
long stackerr_lab;
174
long aritherr_lab;
175
 
176
baseoff MEM_TEMP_BOFF;
177
baseoff LONG_RESULT_BOFF;
178
baseoff SP_BOFF;
179
baseoff FP_BOFF;
180
 
181
/*
182
	The purpose of this file (the 5th revision) is to collect together
183
     all the code which maintains the stack frame. Below is an illustration
184
     of the >>most general<< stack frame.
185
 
186
 
187
 
188
	|                                       |
189
	|---------------------------------------|<--SP
190
	|                                       |
191
	|                                       |
192
	|                                       |
193
	|                                       |
194
	|                                       |
195
	|                                       |
196
	|                                       |
197
	|         8 word "frame marker"         |
198
	|                                       |
199
	|                                       |
200
	|                                       |
201
	|                                       |
202
	|                                       |
203
	|                                       |
204
	|                                       |
205
	|---------------------------------------|
206
	|                                       |
207
	|                                       |
208
	|                                       |
209
	|        4 word "parameter dump"        |
210
	|                                       |
211
	|                                       |
212
	|                                       |
213
	|---------------------------------------|
214
	|                                       |
215
	:                                       :
216
	:          remaining arguments          :
217
	:                                       :
218
	|                                       |
219
	|---------------------------------------|
220
	|                                       |
221
	:                                       :
222
	:      dynamically allocated space      :
223
	:                                       :
224
	|                                       |
225
	|---------------------------------------|
226
	|                                       |
227
	|       2 words of temporary mem        |
228
	|                                       |
229
	|---------------------------------------|
230
	|                                       |
231
	|       2 words for long result         |
232
	|                                       |
233
	|---------------------------------------|
234
	|     1 word to keep track of SP        |
235
	|---------------------------------------|
236
	|                                       |
237
	:                                       :
238
	:                                       :
239
	:                                       :
240
	:            locals space               :
241
	:                                       :
242
	:                                       :
243
	:                                       :
244
	|                                       |
245
	|---------------------------------------|
246
	|                                       |
247
	:                                       :
248
	:                                       :
249
	:                                       :
250
	:            callees space              :
251
	:                                       :
252
	:                                       :
253
	:                                       :
254
	|                                       |
255
	|---------------------------------------|
256
	|        2 words for callee_sz          |
257
	|---------------------------------------|
258
	|                                       |
259
	:                                       :
260
	:  s reg save space (16 words maximum)  :
261
	:                                       :
262
	|                                       |
263
	|---------------------------------------|<--FP (=OLD SP)
264
	|                                       |
265
 
266
 
267
 
268
 
269
	   EP ("enviroment" pointer) is a copy of the SP before (if ever) SP
270
	is incremented to create dynamically allocated space.
271
	   The locals, the temporary memory, the long result words, and the
272
	stack copies of SP and FP (both reset at possible long jump
273
	targets) are all offset relative to EP. If Has_fp==0, then the
274
	parameters and the callees are also offset relative to EP,
275
	otherwise they are offset relative to FP. The callee_sz is stored
276
	on the stack only when Has_vcallees==1, and therefore Has_fp==1,
277
	and is offset relative to FP.
278
 
279
*/
280
 
281
 
282
void setframe_flags
6 7u83 283
(exp e, bool leaf)
2 7u83 284
{
285
   /* e is a proc_tag */
286
   unsigned char ne = name(e);
287
   Uses_crt_env = proc_uses_crt_env(e);
6 7u83 288
   No_S = (!leaf && Uses_crt_env && proc_has_lv(e)
289
		  && (ne!=general_proc_tag || !proc_has_nolongj(e)));
2 7u83 290
   Has_ll = procrecs[no(e)].Has_ll;
291
   Has_checkalloc = procrecs[no(e)].Has_checkalloc;
6 7u83 292
   Has_vsp = (proc_has_alloca(e) || No_S || ne==general_proc_tag );
293
   Has_tos = (No_S && proc_has_alloca(e));
294
   Has_callees = (ne==general_proc_tag);
295
   Has_vcallees = (ne==general_proc_tag && proc_has_vcallees(e));
296
   Has_no_vcallers = (ne==proc_tag || !proc_has_vcallers(e));
297
   Has_fp = (Has_vcallees || gdb);
2 7u83 298
   /*  n.b. gdb, apparently, tracks all locals and parameters via +ve
299
       offsets relative to a frame pointer = %r3. We comply by putting
300
       Has_fp=1  */
6 7u83 301
   is_PIC_and_calls = (PIC_code && !leaf);
2 7u83 302
 
303
#ifdef Try_No_S
304
   No_S = 1;
305
#endif
306
#ifdef Try_Has_fp
307
   Has_fp = !leaf;
308
#endif
309
#ifdef Try_Has_tos
310
   Has_tos = 1;
311
#endif
312
}
313
 
314
 
315
void set_up_frame
6 7u83 316
(exp e)
2 7u83 317
{
318
    procrec * pr = & procrecs[no(e)];
319
    needs * ndpr = & pr->needsproc;
320
    spacereq *sppr = & pr->spacereqproc;
321
    long pprops = (ndpr->propsneeds);
322
    bool leaf = (pprops & anyproccall) == 0;
323
    long ma = ndpr->maxargs;   /* maxargs of proc body in bits  */
324
    long st = sppr->stack;    /*  space for locals in bits     */
325
    bool simpleans = (pprops & long_result_bit) == 0;
326
    int cs = pr->callee_sz;
327
 
328
    setframe_flags(e,leaf);
329
 
330
    if (No_S)
331
    {
332
       fixdump = -65536; /* dump all sregs */
333
    }
334
    else
335
    {
336
       fixdump = sppr->fixdump;
337
    }
338
    if (Has_fp) /* Has frame pointer */
339
    {
340
       /* reserved GR3 for use as frame pointer...     */
341
       fixdump|=(1<<FP) ; /* ...dump and restore FP  */
342
    }
343
    if (Has_vsp) /* Has variable stack pointer */
344
    {
345
       /* reserved GR4 for use as "enviroment pointer"...   */
346
       EP = GR4;
347
       fixdump|=(1<<EP) ; /* ...dump and restore EP    */
348
    }
349
    else
350
    {
351
       EP = SP;
352
    }
353
    if (is_PIC_and_calls)
354
    {
355
       /* reserved GR5 as a copy of GR19...             */
356
       fixdump|=(1<<GR5) ; /* ...dump and restore GR5  */
357
    }
358
 
359
    st+=(2<<5);       /* 2 words of temporary memory */
360
    if (!simpleans)  /*  + 2 words in which to store address of long result */
361
    {
6 7u83 362
       st+= (2<<5);
2 7u83 363
    }
364
    if (Has_tos)
365
    {
366
       st+=(1<<5);  /*  + 1 word in which to store SP (reset at possible  */
367
    }              /*   long jump targets)                               */
368
 
369
    /*   HP PA reserved stack area. c.f. p 3-13 of HP PA reference manual */
370
    if (!leaf)
371
    {
6 7u83 372
       if (ma< (4<<5))
2 7u83 373
	  ma = (4<<5);	 /* 4 words for parameter dump */
374
       ma+=(8<<5);  	/*  + 8 word frame marker     */
375
    }
376
 
377
    /* keep maxargs a multiple of 2 words to ease possible alloca inlining */
378
    ma = (ma+63) & ~63;
379
    /* keep st a multiple of 2 words */
380
    st = (st + 63) & ~63;
381
 
382
    pr->locals_offset = ma + st;  /* relative to EP */
383
 
384
    /*  Budget for sreg save area  */
385
    if (Has_callees)
386
    {
387
       st+=(18<<5); /* 2 words to keep a record of callee sz (2 words      */
388
		   /*  are allocated to guarentee the callees are double  */
389
		  /*   word aligned) + (maximum) 16 word register save   */
390
		 /*    area (because callers must know where to put     */
391
		/*     callees).                                       */
392
    }
393
    else
394
    {
395
       /* Can we be more economical? */
396
       int nos;
397
       if (No_S)
398
       {
399
	  nos = 16;
400
       }
401
       else
402
       {
403
	  nos = bitsin(fixdump); /* = number of s regs used in body of proc */
404
       }
6 7u83 405
       st+= (nos<<5);
2 7u83 406
    }
407
 
408
    if (!Has_vcallees)
409
    {
410
       /* adjust st so that ma + st + cs is a multiple of 16 words
411
	* according to convention */
6 7u83 412
       st = ((ma+st+cs+511) & (~511)) - ma -cs;
2 7u83 413
    }
414
 
415
    pr->locals_space = st;
416
    pr->frame_sz = ma + st + cs;
417
    if (Has_vcallees)
418
    {
419
       /* relative to FP */
420
       pr->params_offset = (8<<5);
6 7u83 421
       pr->callees_offset = - (18<<5);
2 7u83 422
    }
423
    else
424
    {
425
       /* relative to EP */
426
       pr->params_offset = pr->frame_sz + (8<<5);
427
       pr->callees_offset = pr->frame_sz - (18<<5);
428
    }
429
    pr->leaf = leaf;
430
    pr->max_args = ma;
431
 
6 7u83 432
    MEM_TEMP_BOFF.base = EP; MEM_TEMP_BOFF.offset = - (ma>>3) - (2<<2);
433
    LONG_RESULT_BOFF.base = EP; LONG_RESULT_BOFF.offset = - (ma>>3) - (4<<2);
434
    SP_BOFF.base = EP; SP_BOFF.offset = - (ma>>3) - (simpleans ?(3<<2):(5<<2));
2 7u83 435
    FP_BOFF.base = EP; FP_BOFF.offset = -4;
436
 
437
    stackerr_lab=0;
438
    aritherr_lab=0;
439
}
440
 
441
 
442
long frame_offset
6 7u83 443
(exp e)
2 7u83 444
{
445
   exp p;
446
   procrec *pr;
447
   int eo,lo,po,co;
6 7u83 448
   for (p = father(e);(name(p)!=proc_tag && name(p)!=general_proc_tag);                              p = father(p));
2 7u83 449
   pr = &procrecs[no(p)];
450
   lo = pr->locals_offset>>3;
451
   po = pr->params_offset>>3;
452
   co = pr->callees_offset>>3;
453
   if (isparam(e))
454
   {
455
      int nse = no(son(e));
6 7u83 456
      if (name(son(e)) ==formal_callee_tag)
2 7u83 457
      {
6 7u83 458
	 eo = -co+ (nse>>3);
2 7u83 459
      }
460
      else
461
      {
6 7u83 462
	 eo = -po- (nse>>3);
2 7u83 463
      }
464
   }
465
   else
466
   {
467
      int  n = no(e);
468
      int  b = n & 0x3f;
6 7u83 469
      eo = -lo+ ((n-b) >>4);
2 7u83 470
   }
471
   return eo;
472
}
473
 
474
 
475
 
476
 
477
 
478