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

   Copyright (C) 2006-2009 Free Software Foundation, Inc.

This file is part of GCC.

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

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

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

Authors:
   Andrea Ornstein
   Erven Rohou
   Gabriele Svelto

Contact information at STMicroelectronics:
Andrea C. Ornstein      <andrea.ornstein@st.com>
Contact information at INRIA:
Erven Rohou             <erven.rohou@inria.fr>
*/

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "errors.h"
#include "tree.h"
#include "vec.h"

#include "cil-refs.h"
#include "cil-stack.h"
#include "cil-stmt.h"
#include "cil-types.h"

/******************************************************************************
 * Local functions prototypes                                                 *
 ******************************************************************************/

static bool value_type_p (const_tree);
static bool vector_type_p (const_tree);
static cil_type_t cil_binary_op_type (enum cil_opcode, cil_type_t, cil_type_t);

/******************************************************************************
 * Stack information functionality                                            *
 ******************************************************************************/

/* Create a new empty stack.  */

cil_stack
cil_stack_create (void)
{
  cil_stack stack = XNEW (struct cil_stack_d);

  stack->vec = VEC_alloc (cil_type_t, heap, 1);

  return stack;
}

/* Create a new stack by copying the contents of stack SRC.  */

cil_stack
cil_stack_copy(const_cil_stack src)
{
  cil_stack stack = XNEW (struct cil_stack_d);

  if (VEC_length (cil_type_t, src->vec) != 0)
    stack->vec = VEC_copy (cil_type_t, heap, src->vec);
  else
    stack->vec = VEC_alloc (cil_type_t, heap, 1);

  return stack;
}

/* Release the stack object pointed by STACK.  */

void
cil_stack_free (cil_stack stack)
{
  VEC_free (cil_type_t, heap, stack->vec);
  XDELETE (stack);
}

/* Update the stack STACK as if its state was altered by executing the
   statement STMT.  */

void
cil_stack_after_stmt (cil_stack stack, cil_stmt stmt)
{
  VEC (cil_type_t, heap) *vstack = stack->vec;
  enum cil_opcode opcode = cil_opcode (stmt);
  cil_type_t op1, op2, res;
  tree type;
  size_t i;

  switch (opcode)
    {
    case CIL_CPBLK:
    case CIL_INITBLK:
      VEC_pop (cil_type_t, vstack);
      VEC_pop (cil_type_t, vstack);
      VEC_pop (cil_type_t, vstack);
      break;

    case CIL_BEQ:
    case CIL_BGE:
    case CIL_BGE_UN:
    case CIL_BGT:
    case CIL_BGT_UN:
    case CIL_BLE:
    case CIL_BLE_UN:
    case CIL_BLT:
    case CIL_BLT_UN:
    case CIL_BNE_UN:
    case CIL_STFLD:
    case CIL_STIND_I1:
    case CIL_STIND_I2:
    case CIL_STIND_I4:
    case CIL_STIND_I8:
    case CIL_STIND_R4:
    case CIL_STIND_R8:
    case CIL_STIND_I:
    case CIL_STOBJ:
    case CIL_STVEC:
      VEC_pop (cil_type_t, vstack);
      VEC_pop (cil_type_t, vstack);
      break;

    case CIL_ADD:
    case CIL_AND:
    case CIL_CEQ:
    case CIL_CGT:
    case CIL_CGT_UN:
    case CIL_CLT:
    case CIL_CLT_UN:
    case CIL_DIV:
    case CIL_DIV_UN:
    case CIL_MUL:
    case CIL_OR:
    case CIL_REM:
    case CIL_REM_UN:
    case CIL_SHL:
    case CIL_SHR:
    case CIL_SHR_UN:
    case CIL_SUB:
    case CIL_XOR:
      op2 = VEC_pop (cil_type_t, vstack);
      op1 = VEC_pop (cil_type_t, vstack);
      res = cil_binary_op_type (opcode, op1, op2);
      VEC_quick_push (cil_type_t, vstack, res);
      break;

    case CIL_BRFALSE:
    case CIL_BRTRUE:
    case CIL_INITOBJ:
    case CIL_POP:
    case CIL_STARG:
    case CIL_STLOC:
    case CIL_STSFLD:
    case CIL_SWITCH:
      VEC_pop (cil_type_t, vstack);
      break;

    case CIL_CONV_I1:
    case CIL_CONV_I2:
    case CIL_CONV_I4:
    case CIL_CONV_I8:
    case CIL_CONV_R4:
    case CIL_CONV_R8:
    case CIL_CONV_U1:
    case CIL_CONV_U2:
    case CIL_CONV_U4:
    case CIL_CONV_U8:
    case CIL_CONV_I:
    case CIL_CONV_U:
    case CIL_CONV_R_UN:
      VEC_pop (cil_type_t, vstack);

      switch (opcode)
	{
	case CIL_CONV_I1:   res = CIL_INT8;                break;
	case CIL_CONV_I2:   res = CIL_INT16;               break;
	case CIL_CONV_I4:   res = CIL_INT32;               break;
	case CIL_CONV_I8:   res = CIL_INT64;               break;
	case CIL_CONV_R4:   res = CIL_FLOAT32;             break;
	case CIL_CONV_R8:   res = CIL_FLOAT64;             break;
	case CIL_CONV_U1:   res = CIL_UNSIGNED_INT8;       break;
	case CIL_CONV_U2:   res = CIL_UNSIGNED_INT16;      break;
	case CIL_CONV_U4:   res = CIL_UNSIGNED_INT32;      break;
	case CIL_CONV_U8:   res = CIL_UNSIGNED_INT64;      break;
	case CIL_CONV_I:    res = CIL_NATIVE_INT;          break;
	case CIL_CONV_U:    res = CIL_NATIVE_UNSIGNED_INT; break;
	case CIL_CONV_R_UN: res = CIL_FLOAT;               break;
	default:
	  gcc_unreachable ();
	}

      VEC_quick_push (cil_type_t, vstack, res);
      break;

    case CIL_BR:
    case CIL_BREAK:
    case CIL_CKFINITE:
      break;

    case CIL_LDFLD:
    case CIL_LDSFLD:
      type = TREE_TYPE (cil_field (stmt));

      if (opcode == CIL_LDFLD)
	VEC_pop (cil_type_t, vstack);

      VEC_safe_push (cil_type_t, heap, vstack, type_to_cil (type));

      break;

    case CIL_LDIND_I1:
    case CIL_LDIND_I2:
    case CIL_LDIND_I4:
    case CIL_LDIND_I8:
    case CIL_LDIND_U1:
    case CIL_LDIND_U2:
    case CIL_LDIND_U4:
    case CIL_LDIND_U8:
    case CIL_LDIND_R4:
    case CIL_LDIND_R8:
    case CIL_LDIND_I:
      VEC_pop (cil_type_t, vstack);

      switch (opcode)
	{
	case CIL_LDIND_I1: res = CIL_INT8;           break;
	case CIL_LDIND_I2: res = CIL_INT16;          break;
	case CIL_LDIND_I4: res = CIL_INT32;          break;
	case CIL_LDIND_I8: res = CIL_INT64;          break;
	case CIL_LDIND_U1: res = CIL_UNSIGNED_INT8;  break;
	case CIL_LDIND_U2: res = CIL_UNSIGNED_INT16; break;
	case CIL_LDIND_U4: res = CIL_UNSIGNED_INT32; break;
	case CIL_LDIND_U8: res = CIL_UNSIGNED_INT64; break;
	case CIL_LDIND_R4: res = CIL_FLOAT;          break;
	case CIL_LDIND_R8: res = CIL_FLOAT;          break;
	case CIL_LDIND_I:  res = CIL_NATIVE_INT;     break;
	default:
	  gcc_unreachable ();
	}

      VEC_quick_push (cil_type_t, vstack, res);
      break;

    case CIL_LDOBJ:
    case CIL_ARGLIST:
      VEC_pop (cil_type_t, vstack);
      VEC_quick_push (cil_type_t, vstack, CIL_VALUE_TYPE);
      break;

    case CIL_LDVEC:
      {
	tree type = cil_type (stmt);
	cil_type_t cil_type = vector_to_cil (type);
	VEC_pop (cil_type_t, vstack);
	VEC_quick_push (cil_type_t, vstack, cil_type);
      }
      break;

    case CIL_LDFLDA:
    case CIL_LOCALLOC:
      VEC_pop (cil_type_t, vstack);
      VEC_quick_push (cil_type_t, vstack, CIL_POINTER);
      break;

    case CIL_NEG:
    case CIL_NOT:
      op1 = VEC_pop (cil_type_t, vstack);

      if (cil_int_or_smaller_p (op1))
	res = CIL_INT32;
      else if (cil_long_p (op1))
	res = CIL_INT64;
      else if (cil_float_p (op1))
	res = CIL_FLOAT;
      else if (cil_native_integer_p (op1))
	res = CIL_NATIVE_INT;
      else
	gcc_unreachable ();

      VEC_quick_push (cil_type_t, vstack, res);
      break;

    case CIL_DUP:
      op1 = VEC_last (cil_type_t, vstack);
      VEC_safe_push (cil_type_t, heap, vstack, op1);
      break;

    case CIL_LDARG:
      type = DECL_ARG_TYPE (cil_var (stmt));
      VEC_safe_push (cil_type_t, heap, vstack, type_to_cil (type));
      break;

    case CIL_LDLOC:
      type = TREE_TYPE (cil_var (stmt));
      VEC_safe_push (cil_type_t, heap, vstack, type_to_cil (type));
      break;

    case CIL_LDARGA:
    case CIL_LDLOCA:
    case CIL_LDSFLDA:
      VEC_safe_push (cil_type_t, heap, vstack, CIL_POINTER);
      break;

    case CIL_LDC_I4:
      VEC_safe_push (cil_type_t, heap, vstack, CIL_INT32);
      break;

    case CIL_LDC_I8:
      VEC_safe_push (cil_type_t, heap, vstack, CIL_INT64);
      break;

    case CIL_LDC_R4:
    case CIL_LDC_R8:
      VEC_safe_push (cil_type_t, heap, vstack, CIL_FLOAT);
      break;

    case CIL_LDFTN:
      VEC_safe_push (cil_type_t, heap, vstack, CIL_NATIVE_INT);
      break;

    case CIL_RET:
      if (!VOID_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl))))
	VEC_pop (cil_type_t, vstack);

      break;

    case CIL_CALL:
    case CIL_JMP:
    case CIL_NEWOBJ:
    case CIL_CALLI:
      i = cil_call_nargs (stmt);

      while (i-- != 0)
	VEC_pop (cil_type_t, vstack);

      type = TREE_TYPE (cil_call_ftype (stmt));

      if (!VOID_TYPE_P (type))
	VEC_safe_push (cil_type_t, heap, vstack, type_to_cil (type));

      break;

    case CIL_VEC_CTOR:
      {
	tree type = cil_type (stmt);
	cil_type_t cil_type = vector_to_cil (type);
	unsigned int num_elts = TYPE_VECTOR_SUBPARTS (type);

	while (num_elts-- != 0)
	  VEC_pop (cil_type_t, vstack);

	VEC_safe_push (cil_type_t, heap, vstack, cil_type);
      }
      break;

    case CIL_ASM:
      /* TODO: Specify a way for asm statements to tell the compiler how
	 many stack slots they need.  */
      break;

    default:
      gcc_unreachable ();
    }

  /* Update the STACK field as VEC_* operations may alter the source.  */
  stack->vec = vstack;
}

/* Return the depth of the stack STACK.  */

size_t
cil_stack_depth (const_cil_stack stack)
{
  return VEC_length (cil_type_t, stack->vec);
}

/* Return the type of the element on top of the stack.  */

cil_type_t
cil_stack_top (const_cil_stack stack)
{
  return VEC_last (cil_type_t, stack->vec);
}

/* Peek the value I-th location of the stack STACK.  The location is considered
   in reverse order, i.e. location 0 is the top of the stack, 1 the location
   just below it and so on.  */

cil_type_t
cil_stack_peek (const_cil_stack stack, size_t i)
{
  size_t depth = VEC_length (cil_type_t, stack->vec);

  return VEC_index (cil_type_t, stack->vec, depth - 1 - i);
}

/******************************************************************************
 * CIL types functions                                                        *
 ******************************************************************************/

/* Return TRUE if the type TYPE is a non-native integer type, FALSE
   otherwise.  */

bool
cil_integer_p (cil_type_t type)
{
  switch (type)
    {
    case CIL_INT8:
    case CIL_INT16:
    case CIL_INT32:
    case CIL_INT64:
    case CIL_UNSIGNED_INT8:
    case CIL_UNSIGNED_INT16:
    case CIL_UNSIGNED_INT32:
    case CIL_UNSIGNED_INT64:
      return true;

    default:
      return false;
    }
}

/* Return TRUE if the type TYPE is a native integer type, FALSE otherwise.  */

bool
cil_native_integer_p (cil_type_t type)
{
  switch (type)
    {
    case CIL_NATIVE_INT:
    case CIL_NATIVE_UNSIGNED_INT:
      return true;

    default:
      return false;
    }
}

/* Return TRUE if the type TYPE is a floating-point type, FALSE otherwise.  */

bool
cil_float_p (cil_type_t type)
{
  switch (type)
    {
    case CIL_FLOAT:
    case CIL_FLOAT32:
    case CIL_FLOAT64:
      return true;

    default:
      return false;
    }
}

/* Return TRUE if the type TYPE is a vector type, FALSE otherwise.  */

bool
cil_vector_p (cil_type_t type)
{
  switch (type)
    {
    case CIL_V2SI:
    case CIL_V4HI:
    case CIL_V8QI:
    case CIL_V2SF:
    case CIL_V2DI:
    case CIL_V4SI:
    case CIL_V8HI:
    case CIL_V16QI:
    case CIL_V4SF:
    case CIL_V2DF:
      return true;

    default:
      return false;
    }
}

/* Return TRUE if the type TYPE is a pointer type (or native integer),
   FALSE otherwise.  */

bool
cil_pointer_p (cil_type_t type)
{
  switch (type)
    {
    case CIL_NATIVE_INT:
    case CIL_NATIVE_UNSIGNED_INT:
    case CIL_POINTER:
      return true;

    default:
      return false;
    }
}

/* Return TRUE if the type TYPE is a 32-bit or smaller integer, FALSE
   otherwise.  */

bool
cil_int_or_smaller_p (cil_type_t type)
{
  switch (type)
    {
    case CIL_INT8:
    case CIL_INT16:
    case CIL_INT32:
    case CIL_UNSIGNED_INT8:
    case CIL_UNSIGNED_INT16:
    case CIL_UNSIGNED_INT32:
      return true;

    default:
      return false;
    }
}

/* Return TRUE if the type TYPE is a 64-bit integer, FALSE otherwise.  */

bool
cil_long_p (cil_type_t type)
{
  switch (type)
    {
    case CIL_INT64:
    case CIL_UNSIGNED_INT64:
      return true;

    default:
      return false;
    }
}

/* Returns TRUE if the type TYPE is unsigned, FALSE otherwise.  */

bool
cil_unsigned_int_p (cil_type_t type)
{
  switch (type)
    {
    case CIL_UNSIGNED_INT8:
    case CIL_UNSIGNED_INT16:
    case CIL_UNSIGNED_INT32:
    case CIL_UNSIGNED_INT64:
    case CIL_NATIVE_UNSIGNED_INT:
      return true;

    default:
      return true;
    }
}

/* Return the CIL stack representation for scalar type TYPE.  */

cil_type_t
scalar_to_cil (const_tree type)
{
  unsigned HOST_WIDE_INT size;

  switch (TREE_CODE (type))
    {
    case ENUMERAL_TYPE:
      return CIL_INT32;

    case INTEGER_TYPE:
      size = tree_low_cst (TYPE_SIZE (type), 1);

      switch (size)
	{
	  case 8:  return TYPE_UNSIGNED (type) ? CIL_UNSIGNED_INT8 : CIL_INT8;
	  case 16: return TYPE_UNSIGNED (type) ? CIL_UNSIGNED_INT16 : CIL_INT16;
	  case 32: return TYPE_UNSIGNED (type) ? CIL_UNSIGNED_INT32 : CIL_INT32;
	  case 64: return TYPE_UNSIGNED (type) ? CIL_UNSIGNED_INT64 : CIL_INT64;
	  default:
	    internal_error ("Unsupported integer size "
			    HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
	}

    case REAL_TYPE:
      return CIL_FLOAT;

    case BOOLEAN_TYPE:
      return CIL_INT8;

    case ARRAY_TYPE:
      gcc_assert (!TYPE_DOMAIN (type) || ARRAY_TYPE_VARLENGTH (type));
      /* FALLTHROUGH */

    case POINTER_TYPE:
      return CIL_POINTER;

    default:
      gcc_unreachable ();
    }
}

/* Return the CIL stack representation for vector type TYPE.  */

cil_type_t
vector_to_cil (const_tree type)
{
  cil_type_t ret = CIL_NO_TYPE;
  tree innertype;
  HOST_WIDE_INT innersize;
  HOST_WIDE_INT vec_size;

  gcc_assert (TREE_CODE (type) == VECTOR_TYPE);

  innertype = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  innersize = tree_low_cst (TYPE_SIZE (innertype), 1);
  vec_size  = tree_low_cst (TYPE_SIZE (type), 1);

  if ((TREE_CODE (innertype) == INTEGER_TYPE) ||
      (TREE_CODE (innertype) == POINTER_TYPE))
    {
      if (vec_size == 128)  /* 16-byte vectors */
	{
	  switch (innersize)
	    {
	    case  8: ret = CIL_V16QI; break;
	    case 16: ret = CIL_V8HI;  break;
	    case 32: ret = CIL_V4SI;  break;
	    case 64: ret = CIL_V2DI;  break;
	    default: break;
	    }
	}
      else if (vec_size == 64)  /* 8-byte vectors */
	{
	  switch (innersize)
	    {
	    case  8: ret = CIL_V8QI; break;
	    case 16: ret = CIL_V4HI;  break;
	    case 32: ret = CIL_V2SI;  break;
	    default: break;
	    }
	}
      else if (vec_size == 32)  /* 4-byte vectors */
	{
	  switch (innersize)
	    {
	    case  8: ret = CIL_V4QI; break;
	    case 16: ret = CIL_V2HI;  break;
	    default: break;
	    }
	}
    }
  else if (TREE_CODE (innertype) == REAL_TYPE)
    {
      if (vec_size == 128)
	{
	  if (innersize == 32)
	    ret = CIL_V4SF;
	  else if (innersize == 64)
	    ret = CIL_V2DF;
	}
      else if (vec_size == 64)
	if (innersize == 32)
	  ret = CIL_V2SF;
    }
  else
    {
      fprintf (stderr, "Vector of '%s'\n",
	       tree_code_name[TREE_CODE (innertype)]);
    }
  if (ret == CIL_NO_TYPE)
    internal_error ("Unsupported vector type %ld/%ld",
		    vec_size, innersize);
  return ret;
}

/* Return the CIL stack representation for type TYPE.  */

cil_type_t
type_to_cil (const_tree type)
{
  if (value_type_p (type))
    return CIL_VALUE_TYPE;
  else if (vector_type_p  (type))
    return vector_to_cil(type);
  else
    return scalar_to_cil(type);
}

/* Return TRUE if the type specified by TYPE is loaded on the stack as a
   value type, FALSE otherwise.  */

bool
value_type_p (const_tree type)
{
  switch (TREE_CODE (type))
    {
    case ARRAY_TYPE:
      if (!TYPE_DOMAIN (type) || ARRAY_TYPE_VARLENGTH (type))
	return false; /* Incomplete array types are treated life pointers.  */

      /* FALLTHROUGH */
    case RECORD_TYPE:
    case UNION_TYPE:
    case QUAL_UNION_TYPE:
    case COMPLEX_TYPE:
      return true;

    default:
      return false;
    }
}

/* Return TRUE if the type specified by TYPE is loaded on the stack as a
   vector type, FALSE otherwise.  */

static bool
vector_type_p (const_tree type)
{
  if (TREE_CODE (type) == VECTOR_TYPE)
    return true;
  else
    return false;
}

/* Return the result type of a binary operation OP with operands of types
  TYPE1 and TYPE2.  This function doesn't do any validation of the passed
  operation as it assumes that it is fed valid CIL code.  */

static cil_type_t
cil_binary_op_type (enum cil_opcode op, cil_type_t type1, cil_type_t type2)
{
  /* TODO: When pointers will be specialized this function should be
     altered to deal with them properly.  */

  switch (op)
    {
    case CIL_ADD:
    case CIL_AND: /* TODO: Could be improved */
    case CIL_DIV:
    case CIL_DIV_UN:
    case CIL_MUL:
    case CIL_OR: /* TODO: Could be improved */
    case CIL_REM:
    case CIL_REM_UN:
    case CIL_SUB:
    case CIL_XOR: /* TODO: Could be improved */
      if (cil_int_or_smaller_p (type1))
	{
	  if (cil_int_or_smaller_p (type2))
	    return CIL_INT32;
	  else if (cil_native_integer_p (type2))
	    return CIL_NATIVE_INT;
	  else /* CIL_POINTER */
	    return type2;
	}
      else if (cil_long_p (type1))
	return CIL_INT64;
      else if (cil_native_integer_p (type1))
	{
	  if (type2 == CIL_POINTER)
	    return type2;
	  else
	    return CIL_NATIVE_INT;
	}
      else if (cil_float_p (type1))
	return CIL_FLOAT;
      else if (type1 == CIL_POINTER)
	{
	  if (type2 == CIL_POINTER)
	    return CIL_NATIVE_INT;
	  else
	    return type1;
	}
      else if (cil_vector_p (type1) && (type1 == type2))
	return type1;
      else
	gcc_unreachable ();

      break;

    case CIL_CEQ:
    case CIL_CGT:
    case CIL_CGT_UN:
    case CIL_CLT:
    case CIL_CLT_UN:
      return CIL_INT32;

    case CIL_SHL:
    case CIL_SHR:
    case CIL_SHR_UN:
      if (cil_int_or_smaller_p (type1))
	return CIL_INT32;
      else if (cil_long_p (type1))
	return CIL_INT64;
      else
	return CIL_NATIVE_INT;

      break;

    default:
      internal_error ("Unsupported operation for cil_binary_op_type()");
    }
}

/* Create a new stacks-to-basic-blocks mapping structure.  */

cil_bb_stacks
cil_bb_stacks_create (void)
{
  cil_bb_stacks bbs = XNEW (struct cil_bb_stacks_d);

  bbs->stacks = XCNEWVEC (cil_stack, last_basic_block);

  return bbs;
}

/* Destroy a stacks-to-basic-blocks mapping structure, also free all the stacks
   it holds.  */

void
cil_bb_stacks_destroy (cil_bb_stacks bbs)
{
  size_t i;

  for (i = 0; i < (size_t) last_basic_block; i++)
    {
      if (bbs->stacks[i] != NULL)
	cil_stack_free (bbs->stacks[i]);
    }

  XDELETEVEC (bbs->stacks);
  XDELETE (bbs);
}

/* Returns the stack associated with the basic block BB if present, otherwise
   a new empty stack will be associated with the basic block BB and
   returned.  */

cil_stack
cil_stack_for_bb (const_cil_bb_stacks bbs, basic_block bb)
{
  if (bbs->stacks[bb->index] == NULL)
    bbs->stacks[bb->index] = cil_stack_create ();

  return bbs->stacks[bb->index];
}

/* Sets to STACK the stack associated with the basic block BB.  The stack is
   copied so that the copy can be manipulated later without impacting the
   original.  If a stack was already associated with the basic block then
   it is fred and replaced.  */

void
cil_set_stack_for_bb (cil_bb_stacks bbs, basic_block bb, cil_stack stack)
{
  if (bbs->stacks[bb->index] == stack)
    return;
  else if (bbs->stacks[bb->index] != NULL)
    cil_stack_free (bbs->stacks[bb->index]);

  bbs->stacks[bb->index] = cil_stack_copy (stack);
}

/*
 * Local variables:
 * eval: (c-set-style "gnu")
 * End:
 */