OSDN Git Service

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