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