Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
6 7u83 1
/*
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
/*
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
/* 80x86/weights.c */
62
 
63
/**********************************************************************
64
$Author: release $
65
$Date: 1998/01/17 15:55:52 $
66
$Revision: 1.1.1.1 $
67
$Log: weights.c,v $
68
 * Revision 1.1.1.1  1998/01/17  15:55:52  release
69
 * First version to be checked into rolling release.
70
 *
71
 * Revision 1.14  1997/10/10  18:25:40  pwe
72
 * prep ANDF-DE revision
73
 *
74
 * Revision 1.13  1996/11/08  16:19:22  pwe
75
 * check_stack to check before modifying stack
76
 *
77
 * Revision 1.12  1996/05/20  14:30:45  pwe
78
 * improved 64-bit handling
79
 *
80
 * Revision 1.11  1996/05/13  12:52:10  pwe
81
 * undo premature commit
82
 *
83
 * Revision 1.9  1996/01/22  14:31:13  pwe
84
 * PIC const*const, contop top_tag & linux 64-bit ints
85
 *
86
 * Revision 1.8  1996/01/17  11:24:42  pwe
87
 * resurrect performance
88
 *
89
 * Revision 1.7  1995/09/05  16:25:14  pwe
90
 * specials and exception changes
91
 *
92
 * Revision 1.6  1995/08/04  08:29:56  pwe
93
 * 4.0 general procs implemented
94
 *
95
 * Revision 1.5  1995/05/05  12:47:32  pwe
96
 * missing bracket
97
 *
98
 * Revision 1.4  1995/05/02  13:27:42  pwe
99
 * strengthen test for no side effect
100
 *
101
 * Revision 1.3  1995/03/23  13:25:39  pwe
102
 * limit scale in deeply nested repeats
103
 *
104
 * Revision 1.2  1995/01/30  12:56:59  pwe
105
 * Ownership -> PWE, tidy banners
106
 *
107
 * Revision 1.1  1994/10/27  14:15:22  jmf
108
 * Initial revision
109
 *
110
 * Revision 1.1  1994/07/12  14:43:24  jmf
111
 * Initial revision
112
 *
113
**********************************************************************/
114
 
115
 
116
#include "config.h"
117
#include <limits.h>
118
#include "common_types.h"
119
#include "expmacs.h"
120
#include "tags.h"
121
#include "exp.h"
122
#include "shapemacs.h"
123
#include "basicread.h"
124
#include "flags.h"
125
#include "localflags.h"
126
#include "coder.h"
127
#include "weights.h"
128
 
129
 
130
/* MACROS */
131
 
132
#define max(x,y)((x>=y)?(x):(y))
133
 
134
#define wno 6
135
 /* number of available registers */
136
#define wfno 7
137
 /* number of available floating point registers */
138
 
139
/* IDENTITIES */
140
 
141
float  vzeros[wno + wfno] = {
142
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
143
};
144
float  vmoveregs[wno + wfno] = {	/* for the move instruction */
145
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
146
};
147
float  vcmpregs[wno + wfno] = {	/* for the cmp instruction */
148
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
149
};
150
 
151
float  vdivregs[wno + wfno] = {	/* for the div instruction */
152
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
153
};
154
 
155
float  vapplyregs[wno + wfno] = {/* for apply */
156
  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
157
};
158
 
159
 
160
struct wt {
161
  float  w_weights[wno + wfno];
162
  int  booked;
163
  int  flbooked;
164
};
165
 
166
typedef struct wt   weights;
167
				/* weights[i] is the value of putting a
168
				   declaration in a register if there are
169
				   i registers available. */
170
 
171
struct wpt {
172
  weights wp_weights;
173
  int  wp_break;		/* break is the number of registers that
174
				   must be available at this point for it
175
				   to be worthwhile putting this value in
176
				   a register */
177
};
178
 
179
typedef struct wpt  wp;
180
 
181
struct elt {
182
  exp wident;
183
  struct elt *etl;
184
};
185
typedef struct elt  explist;
186
				/* list of identity declarations in force
187
				   at this point */
188
 
189
weights weightsv(exp, explist *);
190
 
191
/* VARIABLES */
192
 
193
weights zeros, moveregs, cmpregs, divregs, applyregs;
194
	/* init by init_weights */
195
 
196
 
197
 
198
 
199
/* PROCEDURES */
200
 
201
static int no_side_aux
202
(exp e)
203
{
204
  exp arg;
205
  if (name(e) ==name_tag || name(e) ==env_offset_tag || name(e) ==top_tag || son(e) ==nilexp)
206
    return 1;
207
  for (arg=son(e);; arg=bro(arg))
208
  {
209
    if ((!is_a(name(arg)) && name(arg)!= ident_tag) || !no_side_aux(arg))
210
      return 0;
211
    if (last(arg))
212
      return 1;
213
  }
214
}
215
 
216
 
217
/* test for guaranteed no side effect */
218
/* simple assignment is permitted */
219
int no_side
220
(exp e)
221
{
222
  return((is_a(name(e)) || name(e) == test_tag || name(e) == ass_tag ||
223
	   name(e) == testbit_tag || name(e) == ident_tag)
224
	&& no_side_aux(e));
225
}
226
 
227
/* add two weight vectors */
228
weights add_weights
229
(weights w1, weights w2)
230
{
231
  weights r;
232
  float  wa,
233
         wb;
234
  int i;
235
  for (i = 0; i < (wno + wfno); ++i) {
236
    wa = (w1.w_weights)[i];
237
    wb = (w2.w_weights)[i];
238
   (r.w_weights)[i] = wa + wb;
239
  };
240
  r.booked = max(w1.booked, w2.booked);
241
  r.flbooked = max(w1.flbooked, w2.flbooked);
242
  return(r);
243
}
244
 
245
void init_weights
246
(void)
247
{
248
		/* initialisation of constants */
249
  int  i;
250
  for (i = 0; i < (wno + wfno); ++i) {
251
   (zeros.w_weights)[i] = vzeros[i];
252
   (moveregs.w_weights)[i] = vmoveregs[i];
253
   (cmpregs.w_weights)[i] = vcmpregs[i];
254
   (divregs.w_weights)[i] = vdivregs[i];
255
   (applyregs.w_weights)[i] = vapplyregs[i];
256
  };
257
  zeros.booked = -1;
258
  moveregs.booked = 1;
259
  cmpregs.booked = 1;
260
  divregs.booked = 1;
261
  applyregs.booked = 1;
262
 
263
  zeros.flbooked = -1;
264
  moveregs.flbooked = -1;
265
  cmpregs.flbooked = -1;
266
  divregs.flbooked = -1;
267
  applyregs.flbooked = 2;
268
 
269
  return;
270
}
271
 
272
void
273
markcall(explist *el)
274
{
275
  explist *t = el;
276
  while (t != (explist *)0) {
277
    set_intnl_call(t -> wident);
278
    t = t -> etl;
279
  };
280
}
281
 
282
/* mark all the declarations in the list
283
   of currently active declarations, to
284
   show that there is a call, movc3 etc.
285
   within their scope */
286
void
287
markmove(explist *el)
288
{
289
  explist *t = el;
290
  while (t != (explist *)0) {
291
    set_intnl_call(t -> wident);
292
    t = t -> etl;
293
  };
294
}
295
 
296
/* mark to show reg1 may be needed */
297
void
298
markreg1(explist *el)
299
{
300
  explist *t = el;
301
  while (t != (explist *)0) {
302
    set_intnl_call(t -> wident);
303
    t = t -> etl;
304
  };
305
}
306
 
307
 
308
/* work out weights for a declaration and
309
   set up the break point to put in the no
310
   field of the declaration */
311
wp max_weights
312
(int size, float locp, weights ws, int isfl)
313
{
314
  int  k = (size + 31) / 32;
315
  int  bk = 11;
316
  int bkset = 0;
317
  int  q;
318
  int  i;
319
  float *w = &(ws.w_weights)[(isfl) ? wno : 0];
320
  wp res;
321
  float *pw = &((res.wp_weights).w_weights)[(isfl)? wno : 0];
322
  int  bkd = (isfl)? ws.flbooked : ws.booked;
323
  int  lwno = (isfl)? wfno : wno;
324
  res.wp_weights.booked = ws.booked;
325
  res.wp_weights.flbooked = ws.flbooked;
326
 
327
  for (i = 0; i < (wno + wfno); ++i)
328
   ((res.wp_weights).w_weights)[i] = (ws.w_weights)[i];
329
 
330
   {
331
    float  loc = locp *k;
332
    q = -1;
333
    for (i = 0; i < lwno; ++i) {
334
      {
335
	if (i < (k + q))
336
	  pw[i] = w[i];
337
	else {
338
	  if (i == (k + q)) {
339
	    if (loc >= w[i] && bkd <= q) {
340
	      pw[i] = loc;
341
	      bk = i + 1;
342
	      bkset = 1;
343
	    }
344
	    else
345
	      pw[i] = w[i];
346
	  }
347
	  else {
348
	    if ((loc + w[i - k]) >= w[i]) {
349
	      pw[i] = loc + w[i - k];
350
	      if (!bkset) {
351
		bk = i + 1;
352
		bkset = 1;
353
	      };
354
	    }
355
	    else
356
	      pw[i] = w[i];
357
	  };
358
	};
359
      };
360
    };
361
  };
362
 
363
 
364
  res.wp_break = bk;
365
  return(res);
366
}
367
 
368
 
369
/* see if we must use movc3?? */
370
weights try_mc3
371
(exp e, weights ws, explist *el)
372
{
373
  int  sz = shape_size(sh(e));
374
 
375
  if (sz <= 128)
376
    return(ws);
377
 
378
  markmove(el);
379
  return(add_weights(ws, moveregs));
380
}
381
 
382
/* work out the weights for a list of exp.
383
   usemc3 is 1 if movc3 may be used. */
384
weights add_wlist
385
(exp re, int usemc3, explist *el)
386
{
387
  weights wl1, wl2;
388
  if (re == nilexp)
389
    return(zeros);
390
 
391
  wl1 = weightsv(re, el);
392
  if (usemc3)
393
    wl1 = try_mc3(re, wl1, el);
394
 
395
  while (!last(re)) {
396
    re = bro(re);
397
    wl2 = weightsv(re, el);
398
    if (usemc3)
399
      wl1 = add_weights(wl1, try_mc3(re, wl2, el));
400
    else
401
      wl1 = add_weights(wl1, wl2);
402
  };
403
  return(wl1);
404
}
405
 
406
 
407
 
408
/* can the value defined by e be put in a register */
409
int regable
410
(exp e)
411
{
412
  unsigned char  n;
413
  shape sha = sh(son(e));
414
  n = name(sha);
415
  if (isvis(e) || n == cpdhd || n == nofhd || n == s64hd || n == u64hd)
416
    return(0);
417
  if (all_variables_visible && isvar(e))
418
    return 0;
419
  return(1);
420
}
421
 
422
int isflsh
423
(shape s)
424
{
425
  unsigned char  n = name(s);
426
  return(n >= shrealhd && n <= doublehd);
427
}
428
 
429
 
430
/* Work out weights and set break points
431
   scale is the expected number of times
432
   that this operation will be done.
433
   During the scan the expected number of
434
   times use is made of something declared
435
   is computed in the no of the
436
   declaration. After the scan the break
437
   point is put into the no of the
438
   declaration */
439
weights
440
weightsv(exp e, explist *el)
441
{
442
  unsigned char  n = name(e);
443
  float old_scale;
444
  weights swl, bwl;
445
 
446
  switch (n) {
447
    case name_tag: {
448
	if (!isglob(son(e)))
449
	  fno (son (e)) += scale;/* add number of uses to the no field of
450
				   the declaration */
451
	return(zeros);
452
      };
453
    case make_lv_tag:
454
        return zeros;
455
    case ident_tag:
456
       {
457
	explist nel;
458
	exp t = pt(e);
459
	nel.wident = e;
460
	nel.etl = el;
461
	while (isvar(e) && !isvis(e) && t != nilexp) {
462
	  if (!(last(t) && name(bro(t)) == cont_tag) &&
463
              !(last(t) && name(bro(t)) == hold_tag) &&
464
	      !(last(bro(t)) && (name(bro(bro(t))) == ass_tag ||
465
		  name(bro(bro(t))) == assvol_tag
466
		)))
467
	    setvis(e);
468
	  t = pt(t);
469
	};
470
 
471
	if (son(e)!= nilexp) {
472
	  weights wdef, wbody;
473
	  exp def = son(e);
474
	  exp body = bro(def);
475
 
476
	  if (name(sh(def)) == u64hd || name(sh(def)) == s64hd)
477
	    markreg1(el);
478
 
479
	  fno (e) = 0.0;	/* clear the accumulated value field */
480
	  wbody = weightsv(body, &nel);
481
	  /* do body (which will add to the accumulated value field */
482
	  if (regable(e)) {
483
	    wp p;
484
	    float  loc = fno(e);
485
            if (has_intnl_call(e))
486
               loc += 2.0;
487
	    p = max_weights(shape_size(sh(def)),
488
		(name(def) == name_tag && isusereg(e))? 1.0 : loc,
489
		wbody,
490
		isflsh(sh(def)));
491
	    if (name(def) == clear_tag)
492
	      wdef = zeros;
493
	    else {
494
	      float  sp_scale = scale;
495
	      if (!isvar(e) &&
496
		 ((name(def) == name_tag && !isvar(son(def)) &&
497
		     (!isglob(son(def))) && !isloadparam(def)
498
		   ) ||
499
		   (name(def) == cont_tag &&
500
		      name(son(def)) == name_tag &&
501
		      isvar(son(son(def))) &&
502
		     (!isglob(son(son(def)))) &&
503
 
504
		      no_side(body)))) {
505
		if (isusereg(e)) {
506
		  sp_scale = 8.0 *fno(e);
507
		}
508
		else
509
		  sp_scale = fno(e);
510
		p.wp_break = 0;
511
		p.wp_weights = wbody;
512
	      };
513
              old_scale = scale;
514
              scale = sp_scale;
515
	      wdef =
516
		weightsv(def, el);
517
	      wdef = try_mc3(def, wdef, el);
518
              scale = old_scale;
519
	    };
520
	    no (e) = p.wp_break;/* set the break point */
521
	    return(add_weights(wdef, p.wp_weights));
522
	  };
523
 
524
	  if (name(sh(def)) == nofhd && ptno(sh(def)) == realhd &&
525
		shape_size(sh(def)) >= 640)
526
	    useful_double = 1;
527
 
528
	  if (name(def) == clear_tag)
529
	    wdef = zeros;
530
	  else {
531
	    wdef =
532
		weightsv(def, el);
533
	      wdef = try_mc3(def, wdef, el);
534
	  };
535
	  no(e) = 16;
536
	  return(add_weights(wdef, wbody));
537
	};
538
	return(zeros);
539
      };
540
    case labst_tag: {
541
	explist nel;
542
	weights wbody;
543
	nel.wident = e;
544
	nel.etl = el;
545
	old_scale = scale;
546
	wbody = weightsv(bro(son(e)), &nel);
547
	scale = old_scale;
548
	return(wbody);
549
      };
550
    case rep_tag: {
551
	swl = weightsv(son(e), el);
552
 
553
        old_scale = scale;
554
 
555
        if (scale < 1e30)
556
		scale = 20*scale;
557
 
558
	bwl = weightsv(bro(son(e)), el);
559
        scale = old_scale;
560
 
561
	return(add_weights(swl, bwl));
562
      };
563
    case cond_tag:  {
564
        old_scale = scale;
565
 
566
        scale = 0.5*scale;
567
        swl = weightsv(son(e), el);
568
	bwl = weightsv(bro(son(e)), el);
569
 
570
        scale = old_scale;
571
 
572
	return(add_weights(swl, bwl));
573
      };
574
    case case_tag:
575
      return(weightsv(son(e), el));
576
 
577
    case compound_tag:
578
      return(add_wlist(son(e), 1, el));
579
      /* may use movc3 for component */
580
 
581
    case res_tag:
582
    case untidy_return_tag:
583
      return(weightsv(son(e), el));
584
 
585
    case asm_tag:
586
    case apply_tag:
587
    case apply_general_tag:
588
    case tail_call_tag:
589
      {
590
        if (name(sh(e))!= bothd && !builtinproc(e))
591
	  markcall(el);
592
	return(add_weights(add_wlist(son(e), 0, el),
593
	      applyregs));
594
      };
595
 
596
    case ass_tag:
597
    case assvol_tag: {
598
      /* may use movc3 for assigned value */
599
      unsigned char shn = name(sh(bro(son(e))));
600
      weights temp;
601
      temp = weightsv(bro(son(e)), el);
602
      if (shn == u64hd || shn == s64hd)
603
	markreg1(el);
604
      return(add_weights(weightsv(son(e), el),
605
	    try_mc3(bro(son(e)), temp, el)
606
	 )
607
	);
608
      };
609
    case proc_tag:
610
    case general_proc_tag: {
611
	IGNORE weightsv(son(e), (explist *)0);
612
	return(zeros);
613
      };
614
    case movecont_tag:
615
      if (isnooverlap(e))
616
        return(add_weights(add_wlist(son(e), 0, el), moveregs));
617
      else {
618
        markcall(el);
619
        return(add_wlist(son(e), 0, el));
620
      };
621
    case val_tag:
622
    case real_tag:
623
    case env_offset_tag:
624
      return(zeros);
625
 
626
    case test_tag:
627
     {weights wlarg;
628
      if (name(sh(son(e))) == s64hd || name(sh(son(e))) == u64hd)
629
	markreg1 (el);				/* use of reg0 can include reg1 */
630
      wlarg = add_wlist(son(e), 0, el);
631
      return(wlarg);
632
     };
633
    case prof_tag:
634
      scale = no(e);
635
      return zeros;
636
 
637
    case alloca_tag:
638
     {if (checkalloc(e))
639
	markreg1(el);
640
      return(add_wlist(son(e), 0, el));
641
     };
642
 
643
    default:
644
      if (sh(e)!= nilexp &&
645
		(name(sh(e)) == s64hd || name(sh(e)) == u64hd))
646
	markreg1 (el);				/* use of reg0 can include reg1 */
647
      return(add_wlist(son(e), 1, el));
648
  };
649
}
650
 
651
void comp_weights
652
(exp e)
653
{
654
  scale = 1.0;
655
  IGNORE weightsv(e,(explist *)0);
656
  return;
657
}