aboutsummaryrefslogtreecommitdiff
path: root/gcc/bc-optab.c
blob: b7ff48652377f2e475020d1609d473a75b52013c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/* Bytecode conversion definitions for GNU C-compiler.
   Copyright (C) 1993, 1994 Free Software Foundation, Inc.

This file is part of GNU CC.

GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */


#include "config.h"
#include "tree.h"
#include "rtl.h"
#include "machmode.h"
#include "obstack.h"
#include "bytecode.h"
#include "bc-typecd.h"
#include "bc-opcode.h"
#include "bc-optab.h"

#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free

extern char *xmalloc ();
extern void free ();

/* Table relating interpreter typecodes to machine modes.  */
#define GET_TYPECODE_MODE(CODE) (typecode_mode[((int) CODE)])
enum machine_mode typecode_mode[] = {
#define DEFTYPECODE(CODE, NAME, MODE, TYPE) MODE,
#include "bc-typecd.def"
#undef DEFTYPECODE
};

/* Machine mode to type code map */
static enum typecode signed_mode_to_code_map[MAX_MACHINE_MODE+1];
static enum typecode unsigned_mode_to_code_map[MAX_MACHINE_MODE+1];

#define GET_TYPECODE_SIZE(CODE) GET_MODE_SIZE (GET_TYPECODE_MODE (CODE))

#define BIG_ARBITRARY_NUMBER 100000

/* Table of recipes for conversions among scalar types, to be filled
   in as needed at run time.  */
static struct conversion_recipe
{
  unsigned char *opcodes;	/* Bytecodes to emit in order.  */
  int nopcodes;			/* Count of bytecodes. */
  int cost;			/* A rather arbitrary cost function. */
} conversion_recipe[NUM_TYPECODES][NUM_TYPECODES];

/* Binary operator tables.  */
struct binary_operator optab_plus_expr[] = {
  { addSI, SIcode, SIcode, SIcode },
  { addDI, DIcode, DIcode, DIcode },
  { addSF, SFcode, SFcode, SFcode },
  { addDF, DFcode, DFcode, DFcode },
  { addXF, XFcode, XFcode, XFcode },
  { addPSI, Pcode, Pcode, SIcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_minus_expr[] = {
  { subSI, SIcode, SIcode, SIcode },
  { subDI, DIcode, DIcode, DIcode },
  { subSF, SFcode, SFcode, SFcode },
  { subDF, DFcode, DFcode, DFcode },
  { subXF, XFcode, XFcode, XFcode },
  { subPP, SIcode, Pcode, Pcode },
  { -1, -1, -1, -1 },
};

/* The ordering of the tables for multiplicative operators
   is such that unsigned operations will be preferred to signed
   operations when one argument is unsigned.  */

struct binary_operator optab_mult_expr[] = {
  { mulSU, SUcode, SUcode, SUcode },
  { mulDU, DUcode, DUcode, DUcode },
  { mulSI, SIcode, SIcode, SIcode },
  { mulDI, DIcode, DIcode, DIcode },
  { mulSF, SFcode, SFcode, SFcode },
  { mulDF, DFcode, DFcode, DFcode },
  { mulXF, XFcode, XFcode, XFcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_trunc_div_expr[] = {
  { divSU, SUcode, SUcode, SUcode },
  { divDU, DUcode, DUcode, DUcode },
  { divSI, SIcode, SIcode, SIcode },
  { divDI, DIcode, DIcode, DIcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_trunc_mod_expr[] = {
  { modSU, SUcode, SUcode, SUcode },
  { modDU, DUcode, DUcode, DUcode },
  { modSI, SIcode, SIcode, SIcode },
  { modDI, DIcode, DIcode, DIcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_rdiv_expr[] = {
  { divSF, SFcode, SFcode, SFcode },
  { divDF, DFcode, DFcode, DFcode },
  { divXF, XFcode, XFcode, XFcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_bit_and_expr[] = {
  { andSI, SIcode, SIcode, SIcode },
  { andDI, DIcode, DIcode, DIcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_bit_ior_expr[] = {
  { iorSI, SIcode, SIcode, SIcode },
  { iorDI, DIcode, DIcode, DIcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_bit_xor_expr[] = {
  { xorSI, SIcode, SIcode, SIcode },
  { xorDI, DIcode, DIcode, DIcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_lshift_expr[] = {
  { lshiftSI, SIcode, SIcode, SIcode },
  { lshiftSU, SUcode, SUcode, SIcode },
  { lshiftDI, DIcode, DIcode, SIcode },
  { lshiftDU, DUcode, DUcode, SIcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_rshift_expr[] = {
  { rshiftSI, SIcode, SIcode, SIcode },
  { rshiftSU, SUcode, SUcode, SIcode },
  { rshiftDI, DIcode, DIcode, SIcode },
  { rshiftDU, DUcode, DUcode, SIcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_truth_and_expr[] = {
  { andSI, SIcode, Tcode, Tcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_truth_or_expr[] = {
  { iorSI, SIcode, Tcode, Tcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_lt_expr[] = {
  { ltSI, Tcode, SIcode, SIcode },
  { ltSU, Tcode, SUcode, SUcode },
  { ltDI, Tcode, DIcode, DIcode },
  { ltDU, Tcode, DUcode, DUcode },
  { ltSF, Tcode, SFcode, SFcode },
  { ltDF, Tcode, DFcode, DFcode },
  { ltXF, Tcode, XFcode, XFcode },
  { ltP, Tcode, Pcode, Pcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_le_expr[] = {
  { leSI, Tcode, SIcode, SIcode },
  { leSU, Tcode, SUcode, SUcode },
  { leDI, Tcode, DIcode, DIcode },
  { leDU, Tcode, DUcode, DUcode },
  { leSF, Tcode, SFcode, SFcode },
  { leDF, Tcode, DFcode, DFcode },
  { leXF, Tcode, XFcode, XFcode },
  { leP, Tcode, Pcode, Pcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_ge_expr[] = {
  { geSI, Tcode, SIcode, SIcode },
  { geSU, Tcode, SUcode, SUcode },
  { geDI, Tcode, DIcode, DIcode },
  { geDU, Tcode, DUcode, DUcode },
  { geSF, Tcode, SFcode, SFcode },
  { geDF, Tcode, DFcode, DFcode },
  { geXF, Tcode, XFcode, XFcode },
  { geP, Tcode, Pcode, Pcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_gt_expr[] = {
  { gtSI, Tcode, SIcode, SIcode },
  { gtSU, Tcode, SUcode, SUcode },
  { gtDI, Tcode, DIcode, DIcode },
  { gtDU, Tcode, DUcode, DUcode },
  { gtSF, Tcode, SFcode, SFcode },
  { gtDF, Tcode, DFcode, DFcode },
  { gtXF, Tcode, XFcode, XFcode },
  { gtP, Tcode, Pcode, Pcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_eq_expr[] = {
  { eqSI, Tcode, SIcode, SIcode },
  { eqDI, Tcode, DIcode, DIcode },
  { eqSF, Tcode, SFcode, SFcode },
  { eqDF, Tcode, DFcode, DFcode },
  { eqXF, Tcode, XFcode, XFcode },
  { eqP, Tcode, Pcode, Pcode },
  { -1, -1, -1, -1 },
};

struct binary_operator optab_ne_expr[] = {
  { neSI, Tcode, SIcode, SIcode },
  { neDI, Tcode, DIcode, DIcode },
  { neSF, Tcode, SFcode, SFcode },
  { neDF, Tcode, DFcode, DFcode },
  { neXF, Tcode, XFcode, XFcode },
  { neP, Tcode, Pcode, Pcode },
  { -1, -1, -1, -1 },
};

/* Unary operator tables.  */
struct unary_operator optab_negate_expr[] = {
  { negSI, SIcode, SIcode },
  { negDI, DIcode, DIcode },
  { negSF, SFcode, SFcode },
  { negDF, DFcode, DFcode },
  { negXF, XFcode, XFcode },
  { -1, -1, -1 },
};

struct unary_operator optab_bit_not_expr[] = {
  { notSI, SIcode, SIcode },
  { notDI, DIcode, DIcode },
  { -1, -1, -1 },
};

struct unary_operator optab_truth_not_expr[] = {
  { notT, SIcode, SIcode },
  { -1, -1, -1 },
};

/* Increment operator tables.  */
struct increment_operator optab_predecrement_expr[] = {
  { predecQI, QIcode },
  { predecQI, QUcode },
  { predecHI, HIcode },
  { predecHI, HUcode },
  { predecSI, SIcode },
  { predecSI, SUcode },
  { predecDI, DIcode },
  { predecDI, DUcode },
  { predecP, Pcode },
  { predecSF, SFcode },
  { predecDF, DFcode },
  { predecXF, XFcode },
  { -1, -1 },
};

struct increment_operator optab_preincrement_expr[] = {
  { preincQI, QIcode },
  { preincQI, QUcode },
  { preincHI, HIcode },
  { preincHI, HUcode },
  { preincSI, SIcode },
  { preincSI, SUcode },
  { preincDI, DIcode },
  { preincDI, DUcode },
  { preincP, Pcode },
  { preincSF, SFcode },
  { preincDF, DFcode },
  { preincXF, XFcode },
  { -1, -1 },
};

struct increment_operator optab_postdecrement_expr[] = {
  { postdecQI, QIcode },
  { postdecQI, QUcode },
  { postdecHI, HIcode },
  { postdecHI, HUcode },
  { postdecSI, SIcode },
  { postdecSI, SUcode },
  { postdecDI, DIcode },
  { postdecDI, DUcode },
  { postdecP, Pcode },
  { postdecSF, SFcode },
  { postdecDF, DFcode },
  { postdecXF, XFcode },
  { -1, -1 },
};

struct increment_operator optab_postincrement_expr[] = {
  { postincQI, QIcode },
  { postincQI, QUcode },
  { postincHI, HIcode },
  { postincHI, HUcode },
  { postincSI, SIcode },
  { postincSI, SUcode },
  { postincDI, DIcode },
  { postincDI, DUcode },
  { postincP, Pcode },
  { postincSF, SFcode },
  { postincDF, DFcode },
  { postincXF, XFcode },
  { -1, -1 },
};

/* Table of conversions supported by the interpreter.  */
static struct conversion_info
{
  enum bytecode_opcode opcode;	/*  here indicates the conversion needs no opcode.  */
  enum typecode from;
  enum typecode to;
  int cost;			/* 1 for no-op conversions, 2 for widening conversions,
				   4 for int/float conversions, 8 for narrowing conversions.  */
} conversion_info[] = {
  { -1, QIcode, QUcode, 1 },
  { -1, HIcode, HUcode, 1 },
  { -1, SIcode, SUcode, 1 },
  { -1, DIcode, DUcode, 1 },
  { -1, QUcode, QIcode, 1 },
  { -1, HUcode, HIcode, 1 },
  { -1, SUcode, SIcode, 1 },
  { -1, DUcode, DIcode, 1 },
  { -1, Tcode, SIcode, 1 },
  { convertQIHI, QIcode, HIcode, 2 },
  { convertQUHU, QUcode, HUcode, 2 },
  { convertQUSU, QUcode, SUcode, 2 },
  { convertHISI, HIcode, SIcode, 2 },
  { convertHUSU, HUcode, SUcode, 2 },
  { convertSIDI, SIcode, DIcode, 2 },
  { convertSUDU, SUcode, DUcode, 2 },
  { convertSFDF, SFcode, DFcode, 2 },
  { convertDFXF, DFcode, XFcode, 2 },
  { convertHIQI, HIcode, QIcode, 8 },
  { convertSIQI, SIcode, QIcode, 8 },
  { convertSIHI, SIcode, HIcode, 8 },
  { convertSUQU, SUcode, QUcode, 8 },
  { convertDISI, DIcode, SIcode, 8 },
  { convertDFSF, DFcode, SFcode, 8 },
  { convertXFDF, XFcode, DFcode, 8 },
  { convertPSI, Pcode, SIcode, 2 },
  { convertSIP, SIcode, Pcode, 2 },
  { convertSIT, SIcode, Tcode, 2 },
  { convertDIT, DIcode, Tcode, 2 },
  { convertSFT, SFcode, Tcode, 2 },
  { convertDFT, DFcode, Tcode, 2 },
  { convertXFT, XFcode, Tcode, 2 },
  { convertQISI, QIcode, SIcode, 2 },
  { convertPT, Pcode, Tcode, 2 },
  { convertSISF, SIcode, SFcode, 4 },
  { convertSIDF, SIcode, DFcode, 4 },
  { convertSIXF, SIcode, XFcode, 4 },
  { convertSUSF, SUcode, SFcode, 4 },
  { convertSUDF, SUcode, DFcode, 4 },
  { convertSUXF, SUcode, XFcode, 4 },
  { convertDISF, DIcode, SFcode, 4 },
  { convertDIDF, DIcode, DFcode, 4 },
  { convertDIXF, DIcode, XFcode, 4 },
  { convertDUSF, DUcode, SFcode, 4 },
  { convertDUDF, DUcode, DFcode, 4 },
  { convertDUXF, DUcode, XFcode, 4 },
  { convertSFSI, SFcode, SIcode, 4 },
  { convertDFSI, DFcode, SIcode, 4 },
  { convertXFSI, XFcode, SIcode, 4 },
  { convertSFSU, SFcode, SUcode, 4 },
  { convertDFSU, DFcode, SUcode, 4 },
  { convertXFSU, XFcode, SUcode, 4 },
  { convertSFDI, SFcode, DIcode, 4 },
  { convertDFDI, DFcode, DIcode, 4 },
  { convertXFDI, XFcode, DIcode, 4 },
  { convertSFDU, SFcode, DUcode, 4 },
  { convertDFDU, DFcode, DUcode, 4 },
  { convertXFDU, XFcode, DUcode, 4 },
  { convertSIQI, SIcode, QIcode, 8 },
};

#define NUM_CONVERSIONS (sizeof conversion_info / sizeof (struct conversion_info))

/* List form of a conversion recipe.  */
struct conversion_list
{
  enum bytecode_opcode opcode;
  enum typecode to;
  int cost;
  struct conversion_list *prev;
};

/* Determine if it is "reasonable" to add a given conversion to
   a given list of conversions.  The following criteria define
   "reasonable" conversion lists:
   * No typecode appears more than once in the sequence (no loops).
   * At most one conversion from integer to float or vice versa is present.
   * Either sign extensions or zero extensions may be present, but not both.
   * No widening conversions occur after a signed/unsigned conversion.
   * The sequence of sizes must be strict nonincreasing or nondecreasing.  */
static int
conversion_reasonable_p (conversion, list)
     struct conversion_info *conversion;
     struct conversion_list *list;
{
  struct conversion_list *curr;
  int curr_size, prev_size;
  int has_int_float, has_float_int;
  int has_sign_extend, has_zero_extend;
  int has_signed_unsigned, has_unsigned_signed;

  has_int_float = 0;
  has_float_int = 0;
  has_sign_extend = 0;
  has_zero_extend = 0;
  has_signed_unsigned = 0;
  has_unsigned_signed = 0;

  /* Make sure the destination typecode doesn't already appear in
     the list.  */
  for (curr = list; curr; curr = curr->prev)
    if (conversion->to == curr->to)
      return 0;

  /* Check for certain kinds of conversions.  */
  if (TYPECODE_INTEGER_P (conversion->from)
      && TYPECODE_FLOAT_P (conversion->to))
    has_int_float = 1;
  if (TYPECODE_FLOAT_P (conversion->from)
      && TYPECODE_INTEGER_P (conversion->to))
    has_float_int = 1;
  if (TYPECODE_SIGNED_P (conversion->from)
      && TYPECODE_SIGNED_P (conversion->to)
      && GET_TYPECODE_SIZE (conversion->from)
      < GET_TYPECODE_SIZE (conversion->to))
    has_sign_extend = 1;
  if (TYPECODE_UNSIGNED_P (conversion->from)
      && TYPECODE_UNSIGNED_P (conversion->to)
      && GET_TYPECODE_SIZE (conversion->from)
      < GET_TYPECODE_SIZE (conversion->to))
    has_zero_extend = 1;

  for (curr = list; curr && curr->prev; curr = curr->prev)
    {
      if (TYPECODE_INTEGER_P (curr->prev->to)
	  && TYPECODE_FLOAT_P (curr->to))
	has_int_float = 1;
      if (TYPECODE_FLOAT_P (curr->prev->to)
	  && TYPECODE_INTEGER_P (curr->to))
	has_float_int = 1;
      if (TYPECODE_SIGNED_P (curr->prev->to)
	  && TYPECODE_SIGNED_P (curr->to)
	  && GET_TYPECODE_SIZE (curr->prev->to)
	  < GET_TYPECODE_SIZE (curr->to))
	has_sign_extend = 1;
      if (TYPECODE_UNSIGNED_P (curr->prev->to)
	  && TYPECODE_UNSIGNED_P (curr->to)
	  && GET_TYPECODE_SIZE (curr->prev->to)
	  < GET_TYPECODE_SIZE (curr->to))
	has_zero_extend = 1;
      if (TYPECODE_SIGNED_P (curr->prev->to)
	  && TYPECODE_UNSIGNED_P (curr->to))
	has_signed_unsigned = 1;
      if (TYPECODE_UNSIGNED_P (curr->prev->to)
	  && TYPECODE_SIGNED_P (curr->to))
	has_unsigned_signed = 1;
    }

  if (TYPECODE_INTEGER_P (conversion->from)
      && TYPECODE_INTEGER_P (conversion->to)
      && GET_TYPECODE_SIZE (conversion->to)
      > GET_TYPECODE_SIZE (conversion->from)
      && (has_signed_unsigned || has_unsigned_signed))
    return 0;

  if (has_float_int && has_int_float || has_sign_extend && has_zero_extend)
    return 0;

  /* Make sure the sequence of destination typecode sizes is
     strictly nondecreasing or strictly nonincreasing.  */
  prev_size = GET_TYPECODE_SIZE (conversion->to);
  for (curr = list; curr; curr = curr->prev)
    {
      curr_size = GET_TYPECODE_SIZE (curr->to);
      if (curr_size != prev_size)
	break;
    }
  if (!curr)
    return 1;

  if (curr_size < prev_size)
    for (prev_size = curr_size; curr; curr = curr->prev)
      {
	curr_size = GET_TYPECODE_SIZE (curr->to);
	if (curr_size > prev_size)
	  return 0;
	prev_size = curr_size;
      }
  else
    for (prev_size = curr_size; curr; curr = curr->prev)
      {
	curr_size = GET_TYPECODE_SIZE (curr->to);
	if (curr_size < prev_size)
	  return 0;
	prev_size = curr_size;
      }
  return 1;
}


/* Exhaustively search all reasonable conversions to find one to
   convert the given types.  */
static struct conversion_recipe
deduce_conversion (from, to)
     enum typecode from, to;
{
  struct rl
    {
      struct conversion_list *list;
      struct rl *next;
    } *prev, curr, *good, *temp;
  struct conversion_list *conv, *best;
  int i, cost, bestcost;
  struct conversion_recipe result;
  struct obstack recipe_obstack;


  obstack_init (&recipe_obstack);
  curr.next = (struct rl *) obstack_alloc (&recipe_obstack, sizeof (struct rl));
  curr.next->list =
    (struct conversion_list *) obstack_alloc (&recipe_obstack,
					      sizeof (struct conversion_list));
  curr.next->list->opcode = -1;
  curr.next->list->to = from;
  curr.next->list->cost = 0;
  curr.next->list->prev = 0;
  curr.next->next = 0;
  good = 0;

  while (curr.next)
    {
      /* Remove successful conversions from further consideration.  */
      for (prev = &curr; prev; prev = prev->next)
	if (prev->next && prev->next->list->to == to)
	  {
	    temp = prev->next->next;
	    prev->next->next = good;
	    good = prev->next;
	    prev->next = temp;
	  }

      /* Go through each of the pending conversion chains, trying
	 all possible candidate conversions on them.  */
      for (prev = curr.next, curr.next = 0; prev; prev = prev->next)
	for (i = 0; i < NUM_CONVERSIONS; ++i)
	  if (conversion_info[i].from == prev->list->to
	      && conversion_reasonable_p (&conversion_info[i], prev->list))
	    {
	      temp = (struct rl *) obstack_alloc (&recipe_obstack,
						  sizeof (struct rl));
	      temp->list = (struct conversion_list *)
		obstack_alloc (&recipe_obstack,
			       sizeof (struct conversion_list));
	      temp->list->opcode = conversion_info[i].opcode;
	      temp->list->to = conversion_info[i].to;
	      temp->list->cost = conversion_info[i].cost;
	      temp->list->prev = prev->list;
	      temp->next = curr.next;
	      curr.next = temp;
	    }
    }

  bestcost = BIG_ARBITRARY_NUMBER;
  best = 0;
  for (temp = good; temp; temp = temp->next)
    {
      for (conv = temp->list, cost = 0; conv; conv = conv->prev)
	cost += conv->cost;
      if (cost < bestcost)
	{
	  bestcost = cost;
	  best = temp->list;
	}
    }

  if (!best)
    abort ();

  for (i = 0, conv = best; conv; conv = conv->prev)
    if (conv->opcode != -1)
      ++i;

  result.opcodes = (unsigned char *) xmalloc (i);
  result.nopcodes = i;
  for (conv = best; conv; conv = conv->prev)
    if (conv->opcode != -1)
      result.opcodes[--i] = conv->opcode;
  result.cost = bestcost;
  obstack_free (&recipe_obstack, 0);
  return result;
}

#define DEDUCE_CONVERSION(FROM, TO)				\
  (conversion_recipe[(int) FROM][(int) TO].opcodes ? 0		\
   : (conversion_recipe[(int) FROM][(int) TO]			\
       = deduce_conversion (FROM, TO), 0))


/* Emit a conversion between the given scalar types.  */
void
emit_typecode_conversion (from, to)
     enum typecode from, to;
{
  int i;

  DEDUCE_CONVERSION (from, to);
  for (i = 0; i < conversion_recipe[(int) from][(int) to].nopcodes; ++i)
    bc_emit_instruction (conversion_recipe[(int) from][(int) to].opcodes[i]);
}


/* Initialize mode_to_code_map[] */
void
bc_init_mode_to_code_map ()
{
  int mode;

  for (mode = 0; mode < MAX_MACHINE_MODE + 1; mode++)
    {
      signed_mode_to_code_map[mode] = 
	unsigned_mode_to_code_map[mode] =
	  LAST_AND_UNUSED_TYPECODE;
    }

#define DEF_MODEMAP(SYM, CODE, UCODE, CONST, LOAD, STORE) \
  { signed_mode_to_code_map[(int) SYM] = CODE; \
    unsigned_mode_to_code_map[(int) SYM] = UCODE; }
#include "modemap.def"
#undef DEF_MODEMAP

  /* Initialize opcode maps for const, load, and store */
  bc_init_mode_to_opcode_maps ();
}

/* Given a machine mode return the preferred typecode.  */
enum typecode
preferred_typecode (mode, unsignedp)
     enum machine_mode mode;
     int unsignedp;
{
  enum typecode code = (unsignedp
			? unsigned_mode_to_code_map
			: signed_mode_to_code_map) [MIN ((int) mode,
							 (int) MAX_MACHINE_MODE)];

  if (code == LAST_AND_UNUSED_TYPECODE)
    abort ();

  return code;
}


/* Expand a conversion between the given types.  */
void
bc_expand_conversion (from, to)
     tree from, to;
{
  enum typecode fcode, tcode;

  fcode = preferred_typecode (TYPE_MODE (from), TREE_UNSIGNED (from));
  tcode = preferred_typecode (TYPE_MODE (to), TREE_UNSIGNED (to));

  emit_typecode_conversion (fcode, tcode);
}

/* Expand a conversion of the given type to a truth value.  */
void
bc_expand_truth_conversion (from)
     tree from;
{
  enum typecode fcode;

  fcode = preferred_typecode (TYPE_MODE (from), TREE_UNSIGNED (from));
  emit_typecode_conversion (fcode, Tcode);
}

/* Emit an appropriate binary operation.  */
void
bc_expand_binary_operation (optab, resulttype, arg0, arg1)
     struct binary_operator optab[];
     tree resulttype, arg0, arg1;
{
  int i, besti, cost, bestcost;
  enum typecode resultcode, arg0code, arg1code;
  
  resultcode = preferred_typecode (TYPE_MODE (resulttype), TREE_UNSIGNED (resulttype));
  arg0code = preferred_typecode (TYPE_MODE (TREE_TYPE (arg0)), TREE_UNSIGNED (resulttype));
  arg1code = preferred_typecode (TYPE_MODE (TREE_TYPE (arg1)), TREE_UNSIGNED (resulttype));

  besti = -1;
  bestcost = BIG_ARBITRARY_NUMBER;

  for (i = 0; optab[i].opcode != -1; ++i)
    {
      cost = 0;
      DEDUCE_CONVERSION (arg0code, optab[i].arg0);
      cost += conversion_recipe[(int) arg0code][(int) optab[i].arg0].cost;
      DEDUCE_CONVERSION (arg1code, optab[i].arg1);
      cost += conversion_recipe[(int) arg1code][(int) optab[i].arg1].cost;
      if (cost < bestcost)
	{
	  besti = i;
	  bestcost = cost;
	}
    }

  if (besti == -1)
    abort ();

  expand_expr (arg1, 0, VOIDmode, 0);
  emit_typecode_conversion (arg1code, optab[besti].arg1);
  expand_expr (arg0, 0, VOIDmode, 0);
  emit_typecode_conversion (arg0code, optab[besti].arg0);
  bc_emit_instruction (optab[besti].opcode);
  emit_typecode_conversion (optab[besti].result, resultcode);
}

/* Emit an appropriate unary operation.  */
void
bc_expand_unary_operation (optab, resulttype, arg0)
     struct unary_operator optab[];
     tree resulttype, arg0;
{
  int i, besti, cost, bestcost;
  enum typecode resultcode, arg0code;
  
  resultcode = preferred_typecode (TYPE_MODE (resulttype), TREE_UNSIGNED (resulttype));
  arg0code = preferred_typecode (TYPE_MODE (TREE_TYPE (arg0)), TREE_UNSIGNED (TREE_TYPE (arg0)));

  besti = -1;
  bestcost = BIG_ARBITRARY_NUMBER;

  for (i = 0; optab[i].opcode != -1; ++i)
    {
      DEDUCE_CONVERSION (arg0code, optab[i].arg0);
      cost = conversion_recipe[(int) arg0code][(int) optab[i].arg0].cost;
      if (cost < bestcost)
	{
	  besti = i;
	  bestcost = cost;
	}
    }

  if (besti == -1)
    abort ();

  expand_expr (arg0, 0, VOIDmode, 0);
  emit_typecode_conversion (arg0code, optab[besti].arg0);
  bc_emit_instruction (optab[besti].opcode);
  emit_typecode_conversion (optab[besti].result, resultcode);
}


/* Emit an appropriate increment.  */
void
bc_expand_increment (optab, type)
     struct increment_operator optab[];
     tree type;
{
  enum typecode code;
  int i;

  code = preferred_typecode (TYPE_MODE (type), TREE_UNSIGNED (type));
  for (i = 0; (int) optab[i].opcode >= 0; ++i)
    if (code == optab[i].arg)
      {
	bc_emit_instruction (optab[i].opcode);
	return;
      }
  abort ();
}