OSDN Git Service

* tree.c (print_lang_statistics): #if 0 call to
[pf3gnuchains/gcc-fork.git] / gcc / bc-optab.c
1 /* Bytecode conversion definitions for GNU C-compiler.
2    Copyright (C) 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
22 #include "config.h"
23 #include "tree.h"
24 #include "rtl.h"
25 #include "machmode.h"
26 #include "obstack.h"
27 #include "bytecode.h"
28 #include "bc-typecd.h"
29 #include "bc-opcode.h"
30 #include "bc-optab.h"
31
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35
36 #ifdef NEED_DECLARATION_FREE
37 extern void free PROTO((void *));
38 #endif
39
40 #define obstack_chunk_alloc xmalloc
41 #define obstack_chunk_free free
42
43 extern char *xmalloc ();
44
45 /* Table relating interpreter typecodes to machine modes.  */
46 #define GET_TYPECODE_MODE(CODE) (typecode_mode[((int) CODE)])
47 enum machine_mode typecode_mode[] = {
48 #define DEFTYPECODE(CODE, NAME, MODE, TYPE) MODE,
49 #include "bc-typecd.def"
50 #undef DEFTYPECODE
51 };
52
53 /* Machine mode to type code map */
54 static enum typecode signed_mode_to_code_map[MAX_MACHINE_MODE+1];
55 static enum typecode unsigned_mode_to_code_map[MAX_MACHINE_MODE+1];
56
57 #define GET_TYPECODE_SIZE(CODE) GET_MODE_SIZE (GET_TYPECODE_MODE (CODE))
58
59 #define BIG_ARBITRARY_NUMBER 100000
60
61 /* Table of recipes for conversions among scalar types, to be filled
62    in as needed at run time.  */
63 static struct conversion_recipe
64 {
65   unsigned char *opcodes;       /* Bytecodes to emit in order.  */
66   int nopcodes;                 /* Count of bytecodes.  */
67   int cost;                     /* A rather arbitrary cost function.  */
68 } conversion_recipe[NUM_TYPECODES][NUM_TYPECODES];
69
70 /* Binary operator tables.  */
71 struct binary_operator optab_plus_expr[] = {
72   { addSI, SIcode, SIcode, SIcode },
73   { addDI, DIcode, DIcode, DIcode },
74   { addSF, SFcode, SFcode, SFcode },
75   { addDF, DFcode, DFcode, DFcode },
76   { addXF, XFcode, XFcode, XFcode },
77   { addPSI, Pcode, Pcode, SIcode },
78   { -1, -1, -1, -1 },
79 };
80
81 struct binary_operator optab_minus_expr[] = {
82   { subSI, SIcode, SIcode, SIcode },
83   { subDI, DIcode, DIcode, DIcode },
84   { subSF, SFcode, SFcode, SFcode },
85   { subDF, DFcode, DFcode, DFcode },
86   { subXF, XFcode, XFcode, XFcode },
87   { subPP, SIcode, Pcode, Pcode },
88   { -1, -1, -1, -1 },
89 };
90
91 /* The ordering of the tables for multiplicative operators
92    is such that unsigned operations will be preferred to signed
93    operations when one argument is unsigned.  */
94
95 struct binary_operator optab_mult_expr[] = {
96   { mulSU, SUcode, SUcode, SUcode },
97   { mulDU, DUcode, DUcode, DUcode },
98   { mulSI, SIcode, SIcode, SIcode },
99   { mulDI, DIcode, DIcode, DIcode },
100   { mulSF, SFcode, SFcode, SFcode },
101   { mulDF, DFcode, DFcode, DFcode },
102   { mulXF, XFcode, XFcode, XFcode },
103   { -1, -1, -1, -1 },
104 };
105
106 struct binary_operator optab_trunc_div_expr[] = {
107   { divSU, SUcode, SUcode, SUcode },
108   { divDU, DUcode, DUcode, DUcode },
109   { divSI, SIcode, SIcode, SIcode },
110   { divDI, DIcode, DIcode, DIcode },
111   { -1, -1, -1, -1 },
112 };
113
114 struct binary_operator optab_trunc_mod_expr[] = {
115   { modSU, SUcode, SUcode, SUcode },
116   { modDU, DUcode, DUcode, DUcode },
117   { modSI, SIcode, SIcode, SIcode },
118   { modDI, DIcode, DIcode, DIcode },
119   { -1, -1, -1, -1 },
120 };
121
122 struct binary_operator optab_rdiv_expr[] = {
123   { divSF, SFcode, SFcode, SFcode },
124   { divDF, DFcode, DFcode, DFcode },
125   { divXF, XFcode, XFcode, XFcode },
126   { -1, -1, -1, -1 },
127 };
128
129 struct binary_operator optab_bit_and_expr[] = {
130   { andSI, SIcode, SIcode, SIcode },
131   { andDI, DIcode, DIcode, DIcode },
132   { -1, -1, -1, -1 },
133 };
134
135 struct binary_operator optab_bit_ior_expr[] = {
136   { iorSI, SIcode, SIcode, SIcode },
137   { iorDI, DIcode, DIcode, DIcode },
138   { -1, -1, -1, -1 },
139 };
140
141 struct binary_operator optab_bit_xor_expr[] = {
142   { xorSI, SIcode, SIcode, SIcode },
143   { xorDI, DIcode, DIcode, DIcode },
144   { -1, -1, -1, -1 },
145 };
146
147 struct binary_operator optab_lshift_expr[] = {
148   { lshiftSI, SIcode, SIcode, SIcode },
149   { lshiftSU, SUcode, SUcode, SIcode },
150   { lshiftDI, DIcode, DIcode, SIcode },
151   { lshiftDU, DUcode, DUcode, SIcode },
152   { -1, -1, -1, -1 },
153 };
154
155 struct binary_operator optab_rshift_expr[] = {
156   { rshiftSI, SIcode, SIcode, SIcode },
157   { rshiftSU, SUcode, SUcode, SIcode },
158   { rshiftDI, DIcode, DIcode, SIcode },
159   { rshiftDU, DUcode, DUcode, SIcode },
160   { -1, -1, -1, -1 },
161 };
162
163 struct binary_operator optab_truth_and_expr[] = {
164   { andSI, SIcode, Tcode, Tcode },
165   { -1, -1, -1, -1 },
166 };
167
168 struct binary_operator optab_truth_or_expr[] = {
169   { iorSI, SIcode, Tcode, Tcode },
170   { -1, -1, -1, -1 },
171 };
172
173 struct binary_operator optab_lt_expr[] = {
174   { ltSI, Tcode, SIcode, SIcode },
175   { ltSU, Tcode, SUcode, SUcode },
176   { ltDI, Tcode, DIcode, DIcode },
177   { ltDU, Tcode, DUcode, DUcode },
178   { ltSF, Tcode, SFcode, SFcode },
179   { ltDF, Tcode, DFcode, DFcode },
180   { ltXF, Tcode, XFcode, XFcode },
181   { ltP, Tcode, Pcode, Pcode },
182   { -1, -1, -1, -1 },
183 };
184
185 struct binary_operator optab_le_expr[] = {
186   { leSI, Tcode, SIcode, SIcode },
187   { leSU, Tcode, SUcode, SUcode },
188   { leDI, Tcode, DIcode, DIcode },
189   { leDU, Tcode, DUcode, DUcode },
190   { leSF, Tcode, SFcode, SFcode },
191   { leDF, Tcode, DFcode, DFcode },
192   { leXF, Tcode, XFcode, XFcode },
193   { leP, Tcode, Pcode, Pcode },
194   { -1, -1, -1, -1 },
195 };
196
197 struct binary_operator optab_ge_expr[] = {
198   { geSI, Tcode, SIcode, SIcode },
199   { geSU, Tcode, SUcode, SUcode },
200   { geDI, Tcode, DIcode, DIcode },
201   { geDU, Tcode, DUcode, DUcode },
202   { geSF, Tcode, SFcode, SFcode },
203   { geDF, Tcode, DFcode, DFcode },
204   { geXF, Tcode, XFcode, XFcode },
205   { geP, Tcode, Pcode, Pcode },
206   { -1, -1, -1, -1 },
207 };
208
209 struct binary_operator optab_gt_expr[] = {
210   { gtSI, Tcode, SIcode, SIcode },
211   { gtSU, Tcode, SUcode, SUcode },
212   { gtDI, Tcode, DIcode, DIcode },
213   { gtDU, Tcode, DUcode, DUcode },
214   { gtSF, Tcode, SFcode, SFcode },
215   { gtDF, Tcode, DFcode, DFcode },
216   { gtXF, Tcode, XFcode, XFcode },
217   { gtP, Tcode, Pcode, Pcode },
218   { -1, -1, -1, -1 },
219 };
220
221 struct binary_operator optab_eq_expr[] = {
222   { eqSI, Tcode, SIcode, SIcode },
223   { eqDI, Tcode, DIcode, DIcode },
224   { eqSF, Tcode, SFcode, SFcode },
225   { eqDF, Tcode, DFcode, DFcode },
226   { eqXF, Tcode, XFcode, XFcode },
227   { eqP, Tcode, Pcode, Pcode },
228   { -1, -1, -1, -1 },
229 };
230
231 struct binary_operator optab_ne_expr[] = {
232   { neSI, Tcode, SIcode, SIcode },
233   { neDI, Tcode, DIcode, DIcode },
234   { neSF, Tcode, SFcode, SFcode },
235   { neDF, Tcode, DFcode, DFcode },
236   { neXF, Tcode, XFcode, XFcode },
237   { neP, Tcode, Pcode, Pcode },
238   { -1, -1, -1, -1 },
239 };
240
241 /* Unary operator tables.  */
242 struct unary_operator optab_negate_expr[] = {
243   { negSI, SIcode, SIcode },
244   { negDI, DIcode, DIcode },
245   { negSF, SFcode, SFcode },
246   { negDF, DFcode, DFcode },
247   { negXF, XFcode, XFcode },
248   { -1, -1, -1 },
249 };
250
251 struct unary_operator optab_bit_not_expr[] = {
252   { notSI, SIcode, SIcode },
253   { notDI, DIcode, DIcode },
254   { -1, -1, -1 },
255 };
256
257 struct unary_operator optab_truth_not_expr[] = {
258   { notT, SIcode, SIcode },
259   { -1, -1, -1 },
260 };
261
262 /* Increment operator tables.  */
263 struct increment_operator optab_predecrement_expr[] = {
264   { predecQI, QIcode },
265   { predecQI, QUcode },
266   { predecHI, HIcode },
267   { predecHI, HUcode },
268   { predecSI, SIcode },
269   { predecSI, SUcode },
270   { predecDI, DIcode },
271   { predecDI, DUcode },
272   { predecP, Pcode },
273   { predecSF, SFcode },
274   { predecDF, DFcode },
275   { predecXF, XFcode },
276   { -1, -1 },
277 };
278
279 struct increment_operator optab_preincrement_expr[] = {
280   { preincQI, QIcode },
281   { preincQI, QUcode },
282   { preincHI, HIcode },
283   { preincHI, HUcode },
284   { preincSI, SIcode },
285   { preincSI, SUcode },
286   { preincDI, DIcode },
287   { preincDI, DUcode },
288   { preincP, Pcode },
289   { preincSF, SFcode },
290   { preincDF, DFcode },
291   { preincXF, XFcode },
292   { -1, -1 },
293 };
294
295 struct increment_operator optab_postdecrement_expr[] = {
296   { postdecQI, QIcode },
297   { postdecQI, QUcode },
298   { postdecHI, HIcode },
299   { postdecHI, HUcode },
300   { postdecSI, SIcode },
301   { postdecSI, SUcode },
302   { postdecDI, DIcode },
303   { postdecDI, DUcode },
304   { postdecP, Pcode },
305   { postdecSF, SFcode },
306   { postdecDF, DFcode },
307   { postdecXF, XFcode },
308   { -1, -1 },
309 };
310
311 struct increment_operator optab_postincrement_expr[] = {
312   { postincQI, QIcode },
313   { postincQI, QUcode },
314   { postincHI, HIcode },
315   { postincHI, HUcode },
316   { postincSI, SIcode },
317   { postincSI, SUcode },
318   { postincDI, DIcode },
319   { postincDI, DUcode },
320   { postincP, Pcode },
321   { postincSF, SFcode },
322   { postincDF, DFcode },
323   { postincXF, XFcode },
324   { -1, -1 },
325 };
326
327 /* Table of conversions supported by the interpreter.  */
328 static struct conversion_info
329 {
330   enum bytecode_opcode opcode;  /*  here indicates the conversion needs no opcode.  */
331   enum typecode from;
332   enum typecode to;
333   int cost;                     /* 1 for no-op conversions, 2 for widening conversions,
334                                    4 for int/float conversions, 8 for narrowing conversions.  */
335 } conversion_info[] = {
336   { -1, QIcode, QUcode, 1 },
337   { -1, HIcode, HUcode, 1 },
338   { -1, SIcode, SUcode, 1 },
339   { -1, DIcode, DUcode, 1 },
340   { -1, QUcode, QIcode, 1 },
341   { -1, HUcode, HIcode, 1 },
342   { -1, SUcode, SIcode, 1 },
343   { -1, DUcode, DIcode, 1 },
344   { -1, Tcode, SIcode, 1 },
345   { convertQIHI, QIcode, HIcode, 2 },
346   { convertQUHU, QUcode, HUcode, 2 },
347   { convertQUSU, QUcode, SUcode, 2 },
348   { convertHISI, HIcode, SIcode, 2 },
349   { convertHUSU, HUcode, SUcode, 2 },
350   { convertSIDI, SIcode, DIcode, 2 },
351   { convertSUDU, SUcode, DUcode, 2 },
352   { convertSFDF, SFcode, DFcode, 2 },
353   { convertDFXF, DFcode, XFcode, 2 },
354   { convertHIQI, HIcode, QIcode, 8 },
355   { convertSIQI, SIcode, QIcode, 8 },
356   { convertSIHI, SIcode, HIcode, 8 },
357   { convertSUQU, SUcode, QUcode, 8 },
358   { convertDISI, DIcode, SIcode, 8 },
359   { convertDFSF, DFcode, SFcode, 8 },
360   { convertXFDF, XFcode, DFcode, 8 },
361   { convertPSI, Pcode, SIcode, 2 },
362   { convertSIP, SIcode, Pcode, 2 },
363   { convertSIT, SIcode, Tcode, 2 },
364   { convertDIT, DIcode, Tcode, 2 },
365   { convertSFT, SFcode, Tcode, 2 },
366   { convertDFT, DFcode, Tcode, 2 },
367   { convertXFT, XFcode, Tcode, 2 },
368   { convertQISI, QIcode, SIcode, 2 },
369   { convertPT, Pcode, Tcode, 2 },
370   { convertSISF, SIcode, SFcode, 4 },
371   { convertSIDF, SIcode, DFcode, 4 },
372   { convertSIXF, SIcode, XFcode, 4 },
373   { convertSUSF, SUcode, SFcode, 4 },
374   { convertSUDF, SUcode, DFcode, 4 },
375   { convertSUXF, SUcode, XFcode, 4 },
376   { convertDISF, DIcode, SFcode, 4 },
377   { convertDIDF, DIcode, DFcode, 4 },
378   { convertDIXF, DIcode, XFcode, 4 },
379   { convertDUSF, DUcode, SFcode, 4 },
380   { convertDUDF, DUcode, DFcode, 4 },
381   { convertDUXF, DUcode, XFcode, 4 },
382   { convertSFSI, SFcode, SIcode, 4 },
383   { convertDFSI, DFcode, SIcode, 4 },
384   { convertXFSI, XFcode, SIcode, 4 },
385   { convertSFSU, SFcode, SUcode, 4 },
386   { convertDFSU, DFcode, SUcode, 4 },
387   { convertXFSU, XFcode, SUcode, 4 },
388   { convertSFDI, SFcode, DIcode, 4 },
389   { convertDFDI, DFcode, DIcode, 4 },
390   { convertXFDI, XFcode, DIcode, 4 },
391   { convertSFDU, SFcode, DUcode, 4 },
392   { convertDFDU, DFcode, DUcode, 4 },
393   { convertXFDU, XFcode, DUcode, 4 },
394   { convertSIQI, SIcode, QIcode, 8 },
395 };
396
397 #define NUM_CONVERSIONS (sizeof conversion_info / sizeof (struct conversion_info))
398
399 /* List form of a conversion recipe.  */
400 struct conversion_list
401 {
402   enum bytecode_opcode opcode;
403   enum typecode to;
404   int cost;
405   struct conversion_list *prev;
406 };
407
408 /* Determine if it is "reasonable" to add a given conversion to
409    a given list of conversions.  The following criteria define
410    "reasonable" conversion lists:
411    * No typecode appears more than once in the sequence (no loops).
412    * At most one conversion from integer to float or vice versa is present.
413    * Either sign extensions or zero extensions may be present, but not both.
414    * No widening conversions occur after a signed/unsigned conversion.
415    * The sequence of sizes must be strict nonincreasing or nondecreasing.  */
416
417 static int
418 conversion_reasonable_p (conversion, list)
419      struct conversion_info *conversion;
420      struct conversion_list *list;
421 {
422   struct conversion_list *curr;
423   int curr_size, prev_size;
424   int has_int_float, has_float_int;
425   int has_sign_extend, has_zero_extend;
426   int has_signed_unsigned, has_unsigned_signed;
427
428   has_int_float = 0;
429   has_float_int = 0;
430   has_sign_extend = 0;
431   has_zero_extend = 0;
432   has_signed_unsigned = 0;
433   has_unsigned_signed = 0;
434
435   /* Make sure the destination typecode doesn't already appear in
436      the list.  */
437   for (curr = list; curr; curr = curr->prev)
438     if (conversion->to == curr->to)
439       return 0;
440
441   /* Check for certain kinds of conversions.  */
442   if (TYPECODE_INTEGER_P (conversion->from)
443       && TYPECODE_FLOAT_P (conversion->to))
444     has_int_float = 1;
445   if (TYPECODE_FLOAT_P (conversion->from)
446       && TYPECODE_INTEGER_P (conversion->to))
447     has_float_int = 1;
448   if (TYPECODE_SIGNED_P (conversion->from)
449       && TYPECODE_SIGNED_P (conversion->to)
450       && GET_TYPECODE_SIZE (conversion->from)
451       < GET_TYPECODE_SIZE (conversion->to))
452     has_sign_extend = 1;
453   if (TYPECODE_UNSIGNED_P (conversion->from)
454       && TYPECODE_UNSIGNED_P (conversion->to)
455       && GET_TYPECODE_SIZE (conversion->from)
456       < GET_TYPECODE_SIZE (conversion->to))
457     has_zero_extend = 1;
458
459   for (curr = list; curr && curr->prev; curr = curr->prev)
460     {
461       if (TYPECODE_INTEGER_P (curr->prev->to)
462           && TYPECODE_FLOAT_P (curr->to))
463         has_int_float = 1;
464       if (TYPECODE_FLOAT_P (curr->prev->to)
465           && TYPECODE_INTEGER_P (curr->to))
466         has_float_int = 1;
467       if (TYPECODE_SIGNED_P (curr->prev->to)
468           && TYPECODE_SIGNED_P (curr->to)
469           && GET_TYPECODE_SIZE (curr->prev->to)
470           < GET_TYPECODE_SIZE (curr->to))
471         has_sign_extend = 1;
472       if (TYPECODE_UNSIGNED_P (curr->prev->to)
473           && TYPECODE_UNSIGNED_P (curr->to)
474           && GET_TYPECODE_SIZE (curr->prev->to)
475           < GET_TYPECODE_SIZE (curr->to))
476         has_zero_extend = 1;
477       if (TYPECODE_SIGNED_P (curr->prev->to)
478           && TYPECODE_UNSIGNED_P (curr->to))
479         has_signed_unsigned = 1;
480       if (TYPECODE_UNSIGNED_P (curr->prev->to)
481           && TYPECODE_SIGNED_P (curr->to))
482         has_unsigned_signed = 1;
483     }
484
485   if (TYPECODE_INTEGER_P (conversion->from)
486       && TYPECODE_INTEGER_P (conversion->to)
487       && GET_TYPECODE_SIZE (conversion->to)
488       > GET_TYPECODE_SIZE (conversion->from)
489       && (has_signed_unsigned || has_unsigned_signed))
490     return 0;
491
492   if (has_float_int && has_int_float || has_sign_extend && has_zero_extend)
493     return 0;
494
495   /* Make sure the sequence of destination typecode sizes is
496      strictly nondecreasing or strictly nonincreasing.  */
497   prev_size = GET_TYPECODE_SIZE (conversion->to);
498   for (curr = list; curr; curr = curr->prev)
499     {
500       curr_size = GET_TYPECODE_SIZE (curr->to);
501       if (curr_size != prev_size)
502         break;
503     }
504   if (!curr)
505     return 1;
506
507   if (curr_size < prev_size)
508     for (prev_size = curr_size; curr; curr = curr->prev)
509       {
510         curr_size = GET_TYPECODE_SIZE (curr->to);
511         if (curr_size > prev_size)
512           return 0;
513         prev_size = curr_size;
514       }
515   else
516     for (prev_size = curr_size; curr; curr = curr->prev)
517       {
518         curr_size = GET_TYPECODE_SIZE (curr->to);
519         if (curr_size < prev_size)
520           return 0;
521         prev_size = curr_size;
522       }
523   return 1;
524 }
525
526
527 /* Exhaustively search all reasonable conversions to find one to
528    convert the given types.  */
529
530 static struct conversion_recipe
531 deduce_conversion (from, to)
532      enum typecode from, to;
533 {
534   struct rl
535     {
536       struct conversion_list *list;
537       struct rl *next;
538     } *prev, curr, *good, *temp;
539   struct conversion_list *conv, *best;
540   int i, cost, bestcost;
541   struct conversion_recipe result;
542   struct obstack recipe_obstack;
543
544
545   obstack_init (&recipe_obstack);
546   curr.next = (struct rl *) obstack_alloc (&recipe_obstack, sizeof (struct rl));
547   curr.next->list
548     = (struct conversion_list *) obstack_alloc (&recipe_obstack,
549                                                 sizeof (struct conversion_list));
550   curr.next->list->opcode = -1;
551   curr.next->list->to = from;
552   curr.next->list->cost = 0;
553   curr.next->list->prev = 0;
554   curr.next->next = 0;
555   good = 0;
556
557   while (curr.next)
558     {
559       /* Remove successful conversions from further consideration.  */
560       for (prev = &curr; prev; prev = prev->next)
561         if (prev->next && prev->next->list->to == to)
562           {
563             temp = prev->next->next;
564             prev->next->next = good;
565             good = prev->next;
566             prev->next = temp;
567           }
568
569       /* Go through each of the pending conversion chains, trying
570          all possible candidate conversions on them.  */
571       for (prev = curr.next, curr.next = 0; prev; prev = prev->next)
572         for (i = 0; i < NUM_CONVERSIONS; ++i)
573           if (conversion_info[i].from == prev->list->to
574               && conversion_reasonable_p (&conversion_info[i], prev->list))
575             {
576               temp = (struct rl *) obstack_alloc (&recipe_obstack,
577                                                   sizeof (struct rl));
578               temp->list = (struct conversion_list *)
579                 obstack_alloc (&recipe_obstack,
580                                sizeof (struct conversion_list));
581               temp->list->opcode = conversion_info[i].opcode;
582               temp->list->to = conversion_info[i].to;
583               temp->list->cost = conversion_info[i].cost;
584               temp->list->prev = prev->list;
585               temp->next = curr.next;
586               curr.next = temp;
587             }
588     }
589
590   bestcost = BIG_ARBITRARY_NUMBER;
591   best = 0;
592   for (temp = good; temp; temp = temp->next)
593     {
594       for (conv = temp->list, cost = 0; conv; conv = conv->prev)
595         cost += conv->cost;
596       if (cost < bestcost)
597         {
598           bestcost = cost;
599           best = temp->list;
600         }
601     }
602
603   if (!best)
604     abort ();
605
606   for (i = 0, conv = best; conv; conv = conv->prev)
607     if (conv->opcode != -1)
608       ++i;
609
610   result.opcodes = (unsigned char *) xmalloc (i);
611   result.nopcodes = i;
612   for (conv = best; conv; conv = conv->prev)
613     if (conv->opcode != -1)
614       result.opcodes[--i] = conv->opcode;
615   result.cost = bestcost;
616   obstack_free (&recipe_obstack, 0);
617   return result;
618 }
619
620 #define DEDUCE_CONVERSION(FROM, TO)                             \
621   (conversion_recipe[(int) FROM][(int) TO].opcodes ? 0          \
622    : (conversion_recipe[(int) FROM][(int) TO]                   \
623        = deduce_conversion (FROM, TO), 0))
624
625
626 /* Emit a conversion between the given scalar types.  */
627
628 void
629 emit_typecode_conversion (from, to)
630      enum typecode from, to;
631 {
632   int i;
633
634   DEDUCE_CONVERSION (from, to);
635   for (i = 0; i < conversion_recipe[(int) from][(int) to].nopcodes; ++i)
636     bc_emit_instruction (conversion_recipe[(int) from][(int) to].opcodes[i]);
637 }
638
639
640 /* Initialize mode_to_code_map[] */
641
642 void
643 bc_init_mode_to_code_map ()
644 {
645   int mode;
646
647   for (mode = 0; mode < MAX_MACHINE_MODE + 1; mode++)
648     {
649       signed_mode_to_code_map[mode]
650         = unsigned_mode_to_code_map[mode]
651         = LAST_AND_UNUSED_TYPECODE;
652     }
653
654 #define DEF_MODEMAP(SYM, CODE, UCODE, CONST, LOAD, STORE) \
655   { signed_mode_to_code_map[(int) SYM] = CODE; \
656     unsigned_mode_to_code_map[(int) SYM] = UCODE; }
657 #include "modemap.def"
658 #undef DEF_MODEMAP
659
660   /* Initialize opcode maps for const, load, and store */
661   bc_init_mode_to_opcode_maps ();
662 }
663
664 /* Given a machine mode return the preferred typecode.  */
665
666 enum typecode
667 preferred_typecode (mode, unsignedp)
668      enum machine_mode mode;
669      int unsignedp;
670 {
671   enum typecode code = (unsignedp
672                         ? unsigned_mode_to_code_map
673                         : signed_mode_to_code_map) [MIN ((int) mode,
674                                                          (int) MAX_MACHINE_MODE)];
675
676   if (code == LAST_AND_UNUSED_TYPECODE)
677     abort ();
678
679   return code;
680 }
681
682
683 /* Expand a conversion between the given types.  */
684
685 void
686 bc_expand_conversion (from, to)
687      tree from, to;
688 {
689   enum typecode fcode, tcode;
690
691   fcode = preferred_typecode (TYPE_MODE (from), TREE_UNSIGNED (from));
692   tcode = preferred_typecode (TYPE_MODE (to), TREE_UNSIGNED (to));
693
694   emit_typecode_conversion (fcode, tcode);
695 }
696
697 /* Expand a conversion of the given type to a truth value.  */
698
699 void
700 bc_expand_truth_conversion (from)
701      tree from;
702 {
703   enum typecode fcode;
704
705   fcode = preferred_typecode (TYPE_MODE (from), TREE_UNSIGNED (from));
706   emit_typecode_conversion (fcode, Tcode);
707 }
708
709 /* Emit an appropriate binary operation.  */
710
711 void
712 bc_expand_binary_operation (optab, resulttype, arg0, arg1)
713      struct binary_operator optab[];
714      tree resulttype, arg0, arg1;
715 {
716   int i, besti, cost, bestcost;
717   enum typecode resultcode, arg0code, arg1code;
718   
719   resultcode = preferred_typecode (TYPE_MODE (resulttype), TREE_UNSIGNED (resulttype));
720   arg0code = preferred_typecode (TYPE_MODE (TREE_TYPE (arg0)), TREE_UNSIGNED (resulttype));
721   arg1code = preferred_typecode (TYPE_MODE (TREE_TYPE (arg1)), TREE_UNSIGNED (resulttype));
722
723   besti = -1;
724   bestcost = BIG_ARBITRARY_NUMBER;
725
726   for (i = 0; optab[i].opcode != -1; ++i)
727     {
728       cost = 0;
729       DEDUCE_CONVERSION (arg0code, optab[i].arg0);
730       cost += conversion_recipe[(int) arg0code][(int) optab[i].arg0].cost;
731       DEDUCE_CONVERSION (arg1code, optab[i].arg1);
732       cost += conversion_recipe[(int) arg1code][(int) optab[i].arg1].cost;
733       if (cost < bestcost)
734         {
735           besti = i;
736           bestcost = cost;
737         }
738     }
739
740   if (besti == -1)
741     abort ();
742
743   expand_expr (arg1, 0, VOIDmode, 0);
744   emit_typecode_conversion (arg1code, optab[besti].arg1);
745   expand_expr (arg0, 0, VOIDmode, 0);
746   emit_typecode_conversion (arg0code, optab[besti].arg0);
747   bc_emit_instruction (optab[besti].opcode);
748   emit_typecode_conversion (optab[besti].result, resultcode);
749 }
750
751 /* Emit an appropriate unary operation.  */
752
753 void
754 bc_expand_unary_operation (optab, resulttype, arg0)
755      struct unary_operator optab[];
756      tree resulttype, arg0;
757 {
758   int i, besti, cost, bestcost;
759   enum typecode resultcode, arg0code;
760   
761   resultcode = preferred_typecode (TYPE_MODE (resulttype), TREE_UNSIGNED (resulttype));
762   arg0code = preferred_typecode (TYPE_MODE (TREE_TYPE (arg0)), TREE_UNSIGNED (TREE_TYPE (arg0)));
763
764   besti = -1;
765   bestcost = BIG_ARBITRARY_NUMBER;
766
767   for (i = 0; optab[i].opcode != -1; ++i)
768     {
769       DEDUCE_CONVERSION (arg0code, optab[i].arg0);
770       cost = conversion_recipe[(int) arg0code][(int) optab[i].arg0].cost;
771       if (cost < bestcost)
772         {
773           besti = i;
774           bestcost = cost;
775         }
776     }
777
778   if (besti == -1)
779     abort ();
780
781   expand_expr (arg0, 0, VOIDmode, 0);
782   emit_typecode_conversion (arg0code, optab[besti].arg0);
783   bc_emit_instruction (optab[besti].opcode);
784   emit_typecode_conversion (optab[besti].result, resultcode);
785 }
786
787
788 /* Emit an appropriate increment.  */
789
790 void
791 bc_expand_increment (optab, type)
792      struct increment_operator optab[];
793      tree type;
794 {
795   enum typecode code;
796   int i;
797
798   code = preferred_typecode (TYPE_MODE (type), TREE_UNSIGNED (type));
799   for (i = 0; (int) optab[i].opcode >= 0; ++i)
800     if (code == optab[i].arg)
801       {
802         bc_emit_instruction (optab[i].opcode);
803         return;
804       }
805   abort ();
806 }