* pa.md (alternate dbra pattern): Remove incorrect pattern.
[official-gcc.git] / gcc / bc-optab.c
blob3d3e539728d71bb2e6779ec96ff770dafa78cef3
1 /* Bytecode conversion definitions for GNU C-compiler.
2 Copyright (C) 1993, 1994 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 "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"
32 #define obstack_chunk_alloc xmalloc
33 #define obstack_chunk_free free
35 extern char *xmalloc ();
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
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];
49 #define GET_TYPECODE_SIZE(CODE) GET_MODE_SIZE (GET_TYPECODE_MODE (CODE))
51 #define BIG_ARBITRARY_NUMBER 100000
53 /* Table of recipes for conversions among scalar types, to be filled
54 in as needed at run time. */
55 static struct conversion_recipe
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];
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 },
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 },
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. */
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 },
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 },
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 },
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 },
121 struct binary_operator optab_bit_and_expr[] = {
122 { andSI, SIcode, SIcode, SIcode },
123 { andDI, DIcode, DIcode, DIcode },
124 { -1, -1, -1, -1 },
127 struct binary_operator optab_bit_ior_expr[] = {
128 { iorSI, SIcode, SIcode, SIcode },
129 { iorDI, DIcode, DIcode, DIcode },
130 { -1, -1, -1, -1 },
133 struct binary_operator optab_bit_xor_expr[] = {
134 { xorSI, SIcode, SIcode, SIcode },
135 { xorDI, DIcode, DIcode, DIcode },
136 { -1, -1, -1, -1 },
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 },
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 },
155 struct binary_operator optab_truth_and_expr[] = {
156 { andSI, SIcode, Tcode, Tcode },
157 { -1, -1, -1, -1 },
160 struct binary_operator optab_truth_or_expr[] = {
161 { iorSI, SIcode, Tcode, Tcode },
162 { -1, -1, -1, -1 },
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 },
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 },
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 },
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 },
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 },
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 },
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 },
243 struct unary_operator optab_bit_not_expr[] = {
244 { notSI, SIcode, SIcode },
245 { notDI, DIcode, DIcode },
246 { -1, -1, -1 },
249 struct unary_operator optab_truth_not_expr[] = {
250 { notT, SIcode, SIcode },
251 { -1, -1, -1 },
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 },
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 },
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 },
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 },
319 /* Table of conversions supported by the interpreter. */
320 static struct conversion_info
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 },
389 #define NUM_CONVERSIONS (sizeof conversion_info / sizeof (struct conversion_info))
391 /* List form of a conversion recipe. */
392 struct conversion_list
394 enum bytecode_opcode opcode;
395 enum typecode to;
396 int cost;
397 struct conversion_list *prev;
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. */
409 static int
410 conversion_reasonable_p (conversion, list)
411 struct conversion_info *conversion;
412 struct conversion_list *list;
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;
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;
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;
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;
451 for (curr = list; curr && curr->prev; curr = curr->prev)
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;
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;
484 if (has_float_int && has_int_float || has_sign_extend && has_zero_extend)
485 return 0;
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)
492 curr_size = GET_TYPECODE_SIZE (curr->to);
493 if (curr_size != prev_size)
494 break;
496 if (!curr)
497 return 1;
499 if (curr_size < prev_size)
500 for (prev_size = curr_size; curr; curr = curr->prev)
502 curr_size = GET_TYPECODE_SIZE (curr->to);
503 if (curr_size > prev_size)
504 return 0;
505 prev_size = curr_size;
507 else
508 for (prev_size = curr_size; curr; curr = curr->prev)
510 curr_size = GET_TYPECODE_SIZE (curr->to);
511 if (curr_size < prev_size)
512 return 0;
513 prev_size = curr_size;
515 return 1;
519 /* Exhaustively search all reasonable conversions to find one to
520 convert the given types. */
522 static struct conversion_recipe
523 deduce_conversion (from, to)
524 enum typecode from, to;
526 struct rl
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;
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;
549 while (curr.next)
551 /* Remove successful conversions from further consideration. */
552 for (prev = &curr; prev; prev = prev->next)
553 if (prev->next && prev->next->list->to == to)
555 temp = prev->next->next;
556 prev->next->next = good;
557 good = prev->next;
558 prev->next = temp;
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))
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;
582 bestcost = BIG_ARBITRARY_NUMBER;
583 best = 0;
584 for (temp = good; temp; temp = temp->next)
586 for (conv = temp->list, cost = 0; conv; conv = conv->prev)
587 cost += conv->cost;
588 if (cost < bestcost)
590 bestcost = cost;
591 best = temp->list;
595 if (!best)
596 abort ();
598 for (i = 0, conv = best; conv; conv = conv->prev)
599 if (conv->opcode != -1)
600 ++i;
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;
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))
618 /* Emit a conversion between the given scalar types. */
620 void
621 emit_typecode_conversion (from, to)
622 enum typecode from, to;
624 int i;
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]);
632 /* Initialize mode_to_code_map[] */
634 void
635 bc_init_mode_to_code_map ()
637 int mode;
639 for (mode = 0; mode < MAX_MACHINE_MODE + 1; mode++)
641 signed_mode_to_code_map[mode]
642 = unsigned_mode_to_code_map[mode]
643 = LAST_AND_UNUSED_TYPECODE;
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
652 /* Initialize opcode maps for const, load, and store */
653 bc_init_mode_to_opcode_maps ();
656 /* Given a machine mode return the preferred typecode. */
658 enum typecode
659 preferred_typecode (mode, unsignedp)
660 enum machine_mode mode;
661 int unsignedp;
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)];
668 if (code == LAST_AND_UNUSED_TYPECODE)
669 abort ();
671 return code;
675 /* Expand a conversion between the given types. */
677 void
678 bc_expand_conversion (from, to)
679 tree from, to;
681 enum typecode fcode, tcode;
683 fcode = preferred_typecode (TYPE_MODE (from), TREE_UNSIGNED (from));
684 tcode = preferred_typecode (TYPE_MODE (to), TREE_UNSIGNED (to));
686 emit_typecode_conversion (fcode, tcode);
689 /* Expand a conversion of the given type to a truth value. */
691 void
692 bc_expand_truth_conversion (from)
693 tree from;
695 enum typecode fcode;
697 fcode = preferred_typecode (TYPE_MODE (from), TREE_UNSIGNED (from));
698 emit_typecode_conversion (fcode, Tcode);
701 /* Emit an appropriate binary operation. */
703 void
704 bc_expand_binary_operation (optab, resulttype, arg0, arg1)
705 struct binary_operator optab[];
706 tree resulttype, arg0, arg1;
708 int i, besti, cost, bestcost;
709 enum typecode resultcode, arg0code, arg1code;
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));
715 besti = -1;
716 bestcost = BIG_ARBITRARY_NUMBER;
718 for (i = 0; optab[i].opcode != -1; ++i)
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)
727 besti = i;
728 bestcost = cost;
732 if (besti == -1)
733 abort ();
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);
743 /* Emit an appropriate unary operation. */
745 void
746 bc_expand_unary_operation (optab, resulttype, arg0)
747 struct unary_operator optab[];
748 tree resulttype, arg0;
750 int i, besti, cost, bestcost;
751 enum typecode resultcode, arg0code;
753 resultcode = preferred_typecode (TYPE_MODE (resulttype), TREE_UNSIGNED (resulttype));
754 arg0code = preferred_typecode (TYPE_MODE (TREE_TYPE (arg0)), TREE_UNSIGNED (TREE_TYPE (arg0)));
756 besti = -1;
757 bestcost = BIG_ARBITRARY_NUMBER;
759 for (i = 0; optab[i].opcode != -1; ++i)
761 DEDUCE_CONVERSION (arg0code, optab[i].arg0);
762 cost = conversion_recipe[(int) arg0code][(int) optab[i].arg0].cost;
763 if (cost < bestcost)
765 besti = i;
766 bestcost = cost;
770 if (besti == -1)
771 abort ();
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);
780 /* Emit an appropriate increment. */
782 void
783 bc_expand_increment (optab, type)
784 struct increment_operator optab[];
785 tree type;
787 enum typecode code;
788 int i;
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)
794 bc_emit_instruction (optab[i].opcode);
795 return;
797 abort ();