irmode.c 20.7 KB
Newer Older
Götz Lindenmaier's avatar
Götz Lindenmaier committed
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * Project:     libFIRM
 * File name:   ir/ir/irmode.c
 * Purpose:     Data modes of operations.
 * Author:      Martin Trapp, Christian Schaefer
 * Modified by: Goetz Lindenmaier, Mathias Heil
 * Created:
 * CVS-ID:      $Id$
 * Copyright:   (c) 1998-2003 Universitt Karlsruhe
 * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
 */

Boris Boesler's avatar
Boris Boesler committed
13

Boris Boesler's avatar
added    
Boris Boesler committed
14
15
16
17
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

18
# include <stdlib.h>
Christian Schäfer's avatar
Christian Schäfer committed
19
# include <stddef.h>
20
# include <string.h>
21
22
23
24
# include <stdbool.h>

# include "irmode_t.h"
# include "ident.h"
Christian Schäfer's avatar
Christian Schäfer committed
25
# include "tv.h"
26
# include "obst.h"
Christian Schäfer's avatar
Christian Schäfer committed
27

28
#if 0
Götz Lindenmaier's avatar
Götz Lindenmaier committed
29
static long long count = 0;
30
31
32
33
#  define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__)
#else
#  define ANNOUNCE() ((void)0)
#endif
Christian Schäfer's avatar
Christian Schäfer committed
34

35
36
37
/* * *
 * local values
 * * */
38

Christian Schäfer's avatar
Christian Schäfer committed
39

Michael Beck's avatar
Michael Beck committed
40
/** dynamic array to hold all modes */
41
static struct obstack modes;
Christian Schäfer's avatar
Christian Schäfer committed
42

Michael Beck's avatar
Michael Beck committed
43
/** number of defined modes */
44
static int num_modes;
Christian Schäfer's avatar
Christian Schäfer committed
45

46
47
48
/* * *
 * local functions
 * * */
Christian Schäfer's avatar
Christian Schäfer committed
49

50
/**
51
 * Compare modes that don't need to have their code field
52
 * correctly set
53
54
55
 *
 * TODO: Add other fields
 **/
Till Riedel's avatar
Till Riedel committed
56
INLINE static int modes_are_equal(const ir_mode *m, const ir_mode *n)
57
58
{
  if (m == n) return 1;
59
60
61
62
63
64
65
  if (m->sort         == n->sort &&
      m->arithmetic   == n->arithmetic &&
      m->size         == n->size &&
      m->align        == n->align &&
      m->sign         == n->sign  &&
      m->modulo_shift == n->modulo_shift &&
      m->vector_elem  == n->vector_elem)
66
    return 1;
Christian Schäfer's avatar
Christian Schäfer committed
67

68
69
  return 0;
}
Matthias Heil's avatar
Matthias Heil committed
70

71
72
73
74
75
76
77
78
/*
 * calculates the next obstack address
 */
static void *next_obstack_adr(struct obstack *o, void *p, size_t s)
{
  PTR_INT_TYPE adr = __PTR_TO_INT((char *)p);
  int mask = obstack_alignment_mask(o);

79
  adr += s + mask;
80

81
  return __INT_TO_PTR(adr & ~mask);
82
83
}

84
/**
85
 * searches the modes obstack for the given mode and returns
86
 * a pointer on an equal mode already in the array, NULL if
87
88
 * none found
 */
89
static ir_mode *find_mode(const ir_mode *m)
90
{
91
  ir_mode *n, *nn;
92
93
  struct _obstack_chunk	*p;

94
95
96
97
  p  = modes.chunk;
  n  = (ir_mode *)p->contents;
  nn = next_obstack_adr(&modes, n, sizeof(*n));
  for (; (char *)nn <= modes.next_free;) {
98
99
100
    assert(is_mode(n));
    if (modes_are_equal(n, m))
      return n;
101
102
103

    n  = nn;
    nn = next_obstack_adr(&modes, n, sizeof(*n));
104
  }
Matthias Heil's avatar
Matthias Heil committed
105

106
  for (p = p->prev; p; p = p->prev) {
107
108
109
    n  = (ir_mode *)p->contents;
    nn = next_obstack_adr(&modes, n, sizeof(*n));
    for (; (char *)nn < p->limit;) {
110
111
112
      assert(is_mode(n));
      if (modes_are_equal(n, m))
	return n;
113
114
115
116

      n  = nn;
      nn = next_obstack_adr(&modes, n, sizeof(*n));
    }
117
  }
Matthias Heil's avatar
Matthias Heil committed
118

119
120
  return NULL;
}
Christian Schäfer's avatar
Christian Schäfer committed
121

122
123
124
/**
 * sets special values of modes
 */
125
126
static void set_mode_values(ir_mode* mode)
{
127
128
  switch (get_mode_sort(mode))
  {
129
    case irms_character:
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    case irms_int_number:
    case irms_float_number:
      mode->min = get_tarval_min(mode);
      mode->max = get_tarval_max(mode);
      mode->null = get_tarval_null(mode);
      mode->one = get_tarval_one(mode);
      break;

    case irms_internal_boolean:
      mode->min = tarval_b_false;
      mode->max = tarval_b_true;
      mode->null = tarval_b_false;
      mode->one = tarval_b_true;
      break;

    case irms_reference:
      mode->min = tarval_bad;
      mode->max = tarval_bad;
148
      mode->null = (get_mode_modecode(mode) == irm_P) ? tarval_P_void : tarval_bad;
149
150
151
152
153
154
155
156
157
158
159
160
      mode->one = tarval_bad;
      break;

    case irms_auxiliary:
    case irms_memory:
    case irms_control_flow:
      mode->min = tarval_bad;
      mode->max = tarval_bad;
      mode->null = tarval_bad;
      mode->one = tarval_bad;
      break;
  }
161
}
162

163
164
165
/* * *
 * globals defined in irmode.h
 * * */
Matthias Heil's avatar
Matthias Heil committed
166

167
/* --- Predefined modes --- */
Christian Schäfer's avatar
Christian Schäfer committed
168

169
170
171
172
173
/* FIRM internal modes: */
ir_mode *mode_T;
ir_mode *mode_X;
ir_mode *mode_M;
ir_mode *mode_BB;
174
175
ir_mode *mode_ANY;
ir_mode *mode_BAD;
Christian Schäfer's avatar
Christian Schäfer committed
176

177
178
179
180
/* predefined numerical modes: */
ir_mode *mode_F;    /* float */
ir_mode *mode_D;    /* double */
ir_mode *mode_E;    /* long double */
Christian Schäfer's avatar
Christian Schäfer committed
181

182
183
184
185
186
187
188
189
ir_mode *mode_Bs;   /* integral values, signed and unsigned */
ir_mode *mode_Bu;   /* 8 bit */
ir_mode *mode_Hs;   /* 16 bit */
ir_mode *mode_Hu;
ir_mode *mode_Is;   /* 32 bit */
ir_mode *mode_Iu;
ir_mode *mode_Ls;   /* 64 bit */
ir_mode *mode_Lu;
Christian Schäfer's avatar
Christian Schäfer committed
190

191
192
193
194
ir_mode *mode_C;
ir_mode *mode_U;
ir_mode *mode_b;
ir_mode *mode_P;
Michael Beck's avatar
fixed:    
Michael Beck committed
195
196
197

/* machine specific modes */
ir_mode *mode_P_mach;	/* machine specific pointer mode */
198

199
200
201
202
203
/* * *
 * functions defined in irmode.h
 * * */

/* JNI access functions */
Michael Beck's avatar
Michael Beck committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
INLINE ir_mode *get_modeT(void) { ANNOUNCE(); return mode_T; }
INLINE ir_mode *get_modeF(void) { ANNOUNCE(); return mode_F; }
INLINE ir_mode *get_modeD(void) { ANNOUNCE(); return mode_D; }
INLINE ir_mode *get_modeE(void) { ANNOUNCE(); return mode_E; }
INLINE ir_mode *get_modeBs(void) { ANNOUNCE(); return mode_Bs; }
INLINE ir_mode *get_modeBu(void) { ANNOUNCE(); return mode_Bu; }
INLINE ir_mode *get_modeHs(void) { ANNOUNCE(); return mode_Hs; }
INLINE ir_mode *get_modeHu(void) { ANNOUNCE(); return mode_Hu; }
INLINE ir_mode *get_modeIs(void) { ANNOUNCE(); return mode_Is; }
INLINE ir_mode *get_modeIu(void) { ANNOUNCE(); return mode_Iu; }
INLINE ir_mode *get_modeLs(void) { ANNOUNCE(); return mode_Ls; }
INLINE ir_mode *get_modeLu(void) { ANNOUNCE(); return mode_Lu; }
INLINE ir_mode *get_modeC(void) { ANNOUNCE(); return mode_C; }
INLINE ir_mode *get_modeU(void) { ANNOUNCE(); return mode_U; }
INLINE ir_mode *get_modeb(void) { ANNOUNCE(); return mode_b; }
INLINE ir_mode *get_modeP(void) { ANNOUNCE(); return mode_P; }
INLINE ir_mode *get_modeX(void) { ANNOUNCE(); return mode_X; }
INLINE ir_mode *get_modeM(void) { ANNOUNCE(); return mode_M; }
INLINE ir_mode *get_modeBB(void) { ANNOUNCE(); return mode_BB; }
223
224
INLINE ir_mode *get_modeANY(void) { ANNOUNCE(); return mode_ANY; }
INLINE ir_mode *get_modeBAD(void) { ANNOUNCE(); return mode_BAD; }
225

226

Michael Beck's avatar
Michael Beck committed
227
ir_mode *(get_modeP_mach)(void) {
228
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
229
230
231
232
233
234
235
  return __get_modeP_mach();
}

void (set_modeP_mach)(ir_mode *p) {
  ANNOUNCE();
  __set_modeP_mach(p);
}
236

237
/**
238
239
240
 * Registers a new mode.
 *
 * @param new_mode  The new mode template.
241
 */
242
static ir_mode *register_mode(const ir_mode* new_mode)
243
{
244
  ir_mode *mode = NULL;
245
246
247
248

  ANNOUNCE();
  assert(new_mode);

249
  /* copy mode struct to modes array */
250
  mode = (ir_mode*)obstack_copy(&modes, new_mode, sizeof(ir_mode));
251
252

  mode->kind = k_ir_mode;
253
  if (num_modes >= irm_max) mode->code = num_modes;
254
255
  num_modes++;

256
  set_mode_values(mode);
257
258
259
260
261
262
263

  return mode;
}

/*
 * Creates a new mode.
 */
264
ir_mode *new_ir_mode(const char *name, mode_sort sort, int bit_size, int bit_align, int sign,
265
		     mode_arithmetic arithmetic, unsigned int modulo_shift )
266
267
268
{
  ir_mode mode_tmpl;
  ir_mode *mode;
269

270
271
272
  mode_tmpl.name         = new_id_from_str(name);
  mode_tmpl.sort         = sort;
  mode_tmpl.size         = bit_size;
273
  mode_tmpl.align        = bit_align;
274
275
  mode_tmpl.sign         = sign ? 1 : 0;
  mode_tmpl.modulo_shift = (mode_tmpl.sort == irms_int_number) ? modulo_shift : 0;
276
  mode_tmpl.vector_elem  = 1;
277
278
279
  mode_tmpl.arithmetic   = arithmetic;
  mode_tmpl.link         = NULL;
  mode_tmpl.tv_priv      = NULL;
Christian Schäfer's avatar
Christian Schäfer committed
280

281
282
283
284
285
  mode = find_mode(&mode_tmpl);
  if (mode)
  {
    return mode;
  }
286
287
288

  /* sanity checks */
  switch (sort)
289
  {
290
291
292
293
294
295
296
297
298
299
300
301
    case irms_auxiliary:
    case irms_control_flow:
    case irms_memory:
    case irms_internal_boolean:
      assert(0 && "internal modes cannot be user defined");
      return NULL;

    case irms_float_number:
    case irms_int_number:
    case irms_reference:
    case irms_character:
      return register_mode(&mode_tmpl);
302
  }
303
  return NULL; /* to shut up gcc */
304
305
}

306
307
308
/*
 * Creates a new vector mode.
 */
309
ir_mode *new_ir_vector_mode(const char *name, mode_sort sort, int bit_size, unsigned num_of_elem, int bit_align, int sign,
310
311
312
313
314
315
316
317
		     mode_arithmetic arithmetic, unsigned int modulo_shift )
{
  ir_mode mode_tmpl;
  ir_mode *mode;

  mode_tmpl.name         = new_id_from_str(name);
  mode_tmpl.sort         = sort;
  mode_tmpl.size         = bit_size * num_of_elem;
318
  mode_tmpl.align        = bit_align;
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
  mode_tmpl.sign         = sign ? 1 : 0;
  mode_tmpl.modulo_shift = (mode_tmpl.sort == irms_int_number) ? modulo_shift : 0;
  mode_tmpl.vector_elem  = num_of_elem;
  mode_tmpl.arithmetic   = arithmetic;
  mode_tmpl.link         = NULL;
  mode_tmpl.tv_priv      = NULL;

  mode = find_mode(&mode_tmpl);
  if (mode)
    return mode;

  if (num_of_elem <= 1) {
    assert(0 && "vector modes should have at least 2 elements");
    return NULL;
  }

  /* sanity checks */
  switch (sort)
  {
    case irms_auxiliary:
    case irms_control_flow:
    case irms_memory:
    case irms_internal_boolean:
      assert(0 && "internal modes cannot be user defined");
      return NULL;

    case irms_reference:
    case irms_character:
      assert(0 && "only integer and floating point modes can be vectorized");
      return NULL;

    case irms_float_number:
      assert(0 && "not yet implemented");
      return NULL;

    case irms_int_number:
      return register_mode(&mode_tmpl);
  }
  return NULL; /* to shut up gcc */
}

360
/* Functions for the direct access to all attributes od a ir_mode */
Christian Schäfer's avatar
Christian Schäfer committed
361
modecode
Michael Beck's avatar
Michael Beck committed
362
(get_mode_modecode)(const ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
363
{
364
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
365
  return __get_mode_modecode(mode);
Christian Schäfer's avatar
Christian Schäfer committed
366
367
368
}

ident *
Michael Beck's avatar
Michael Beck committed
369
(get_mode_ident)(const ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
370
{
371
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
372
  return __get_mode_ident(mode);
Christian Schäfer's avatar
Christian Schäfer committed
373
374
}

375
const char *
Michael Beck's avatar
Michael Beck committed
376
get_mode_name(const ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
377
{
378
  ANNOUNCE();
379
  return get_id_str(mode->name);
Christian Schäfer's avatar
Christian Schäfer committed
380
381
}

382
mode_sort
Michael Beck's avatar
Michael Beck committed
383
(get_mode_sort)(const ir_mode* mode)
384
385
{
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
386
  return __get_mode_sort(mode);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
387
388
}

Michael Beck's avatar
Michael Beck committed
389
390
int
(get_mode_size_bits)(const ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
391
{
392
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
393
  return __get_mode_size_bits(mode);
Christian Schäfer's avatar
Christian Schäfer committed
394
395
}

Michael Beck's avatar
Michael Beck committed
396
397
int
(get_mode_size_bytes)(const ir_mode *mode) {
398
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
399
  return __get_mode_size_bytes(mode);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
400
401
}

Christian Schäfer's avatar
Christian Schäfer committed
402
int
403
(get_mode_align_bits)(const ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
404
{
405
  ANNOUNCE();
406
407
408
409
410
411
412
413
  return __get_mode_align_bits(mode);
}

int
(get_mode_align_bytes)(const ir_mode *mode)
{
  ANNOUNCE();
  return __get_mode_align_bytes(mode);
Christian Schäfer's avatar
Christian Schäfer committed
414
}
415
416

int
Michael Beck's avatar
Michael Beck committed
417
(get_mode_sign)(const ir_mode *mode)
418
419
{
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
420
  return __get_mode_sign(mode);
421
422
}

Michael Beck's avatar
Michael Beck committed
423
424
int
(get_mode_arithmetic)(const ir_mode *mode)
425
426
{
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
427
  return get_mode_arithmetic(mode);
428
429
}

430
431
432
433
434

/* Attribute modulo shift specifies for modes of kind irms_int_number
 *  whether shift applies modulo to value of bits to shift.  Asserts
 *  if mode is not irms_int_number.
 */
Michael Beck's avatar
Michael Beck committed
435
436
437
unsigned int
(get_mode_modulo_shift)(const ir_mode *mode) {
  return __get_mode_modulo_shift(mode);
438
439
}

Michael Beck's avatar
Michael Beck committed
440
unsigned int
441
(get_mode_n_vector_elems)(const ir_mode *mode) {
Michael Beck's avatar
Michael Beck committed
442
  return __get_mode_vector_elems(mode);
443
444
}

Michael Beck's avatar
Michael Beck committed
445
446
void *
(get_mode_link)(const ir_mode *mode)
447
448
{
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
449
  return __get_mode_link(mode);
450
451
}

Michael Beck's avatar
Michael Beck committed
452
453
void
(set_mode_link)(ir_mode *mode, void *l)
454
{
Michael Beck's avatar
Michael Beck committed
455
  __set_mode_link(mode, l);
456
457
}

Christian Schäfer's avatar
Christian Schäfer committed
458
tarval *
Götz Lindenmaier's avatar
Götz Lindenmaier committed
459
get_mode_min (ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
460
{
461
462
463
464
  ANNOUNCE();
  assert(mode);
  assert(get_mode_modecode(mode) < num_modes);
  assert(mode_is_data(mode));
Christian Schäfer's avatar
Christian Schäfer committed
465

466
  return mode->min;
Christian Schäfer's avatar
Christian Schäfer committed
467
468
469
}

tarval *
Götz Lindenmaier's avatar
Götz Lindenmaier committed
470
get_mode_max (ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
471
{
472
473
474
475
  ANNOUNCE();
  assert(mode);
  assert(get_mode_modecode(mode) < num_modes);
  assert(mode_is_data(mode));
Christian Schäfer's avatar
Christian Schäfer committed
476

477
  return mode->max;
Christian Schäfer's avatar
Christian Schäfer committed
478
479
480
}

tarval *
Götz Lindenmaier's avatar
Götz Lindenmaier committed
481
get_mode_null (ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
482
{
483
484
485
486
  ANNOUNCE();
  assert(mode);
  assert(get_mode_modecode(mode) < num_modes);
  assert(mode_is_data(mode));
Christian Schäfer's avatar
Christian Schäfer committed
487

488
  return mode->null;
Christian Schäfer's avatar
Christian Schäfer committed
489
490
}

491
492
tarval *
get_mode_one (ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
493
{
494
495
496
497
  ANNOUNCE();
  assert(mode);
  assert(get_mode_modecode(mode) < num_modes);
  assert(mode_is_data(mode));
Christian Schäfer's avatar
Christian Schäfer committed
498

499
  return mode->one;
Christian Schäfer's avatar
Christian Schäfer committed
500
501
}

502
503
tarval *
get_mode_infinite(ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
504
{
505
506
507
508
509
510
  ANNOUNCE();
  assert(mode);
  assert(get_mode_modecode(mode) < num_modes);
  assert(mode_is_float(mode));

  return get_tarval_inf(mode);
Christian Schäfer's avatar
Christian Schäfer committed
511
512
}

513
514
tarval *
get_mode_NAN(ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
515
{
516
517
518
519
520
521
  ANNOUNCE();
  assert(mode);
  assert(get_mode_modecode(mode) < num_modes);
  assert(mode_is_float(mode));

  return get_tarval_nan(mode);
Christian Schäfer's avatar
Christian Schäfer committed
522
523
}

524
525
526
527
528
529
530
531
int
is_mode (void *thing) {
  if (get_kind(thing) == k_ir_mode)
    return 1;
  else
    return 0;
}

Christian Schäfer's avatar
Christian Schäfer committed
532
int
Michael Beck's avatar
Michael Beck committed
533
(mode_is_signed)(const ir_mode *mode) {
534
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
535
  return __mode_is_signed(mode);
Christian Schäfer's avatar
Christian Schäfer committed
536
537
}

538
int
Michael Beck's avatar
Michael Beck committed
539
(mode_is_float)(const ir_mode *mode) {
540
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
541
  return __mode_is_float(mode);
Christian Schäfer's avatar
Christian Schäfer committed
542
543
}

544
int
Michael Beck's avatar
Michael Beck committed
545
(mode_is_int)(const ir_mode *mode) {
546
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
547
  return __mode_is_int(mode);
Christian Schäfer's avatar
Christian Schäfer committed
548
549
}

Michael Beck's avatar
Michael Beck committed
550
551
int
(mode_is_character)(const ir_mode *mode) {
552
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
553
  return __mode_is_character(mode);
554
555
}

Michael Beck's avatar
Michael Beck committed
556
557
int
(mode_is_reference)(const ir_mode *mode) {
558
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
559
  return __mode_is_reference(mode);
560
561
}

562
int
Michael Beck's avatar
Michael Beck committed
563
(mode_is_num)(const ir_mode *mode) {
564
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
565
  return __mode_is_num(mode);
Christian Schäfer's avatar
Christian Schäfer committed
566
567
}

Michael Beck's avatar
fixed:    
Michael Beck committed
568
int
Michael Beck's avatar
Michael Beck committed
569
(mode_is_numP)(const ir_mode *mode) {
Michael Beck's avatar
fixed:    
Michael Beck committed
570
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
571
  return __mode_is_numP(mode);
Michael Beck's avatar
fixed:    
Michael Beck committed
572
573
}

574
int
Michael Beck's avatar
Michael Beck committed
575
(mode_is_data)(const ir_mode *mode) {
576
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
577
  return __mode_is_data(mode);
Christian Schäfer's avatar
Christian Schäfer committed
578
579
580
}

int
Michael Beck's avatar
Michael Beck committed
581
(mode_is_datab)(const ir_mode *mode) {
582
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
583
  return __mode_is_datab(mode);
Christian Schäfer's avatar
Christian Schäfer committed
584
585
586
}

int
Michael Beck's avatar
Michael Beck committed
587
(mode_is_dataM)(const ir_mode *mode) {
588
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
589
  return __mode_is_dataM(mode);
Christian Schäfer's avatar
Christian Schäfer committed
590
}
591
592

int
Michael Beck's avatar
Michael Beck committed
593
(mode_is_float_vector)(const ir_mode *mode) {
594
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
595
  return __mode_is_float_vector(mode);
596
597
598
}

int
Michael Beck's avatar
Michael Beck committed
599
(mode_is_int_vector)(const ir_mode *mode) {
600
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
601
602
603
  return __mode_is_int_vector(mode);
}

604
/* Returns true if sm can be converted to lm without loss. */
605
int
Michael Beck's avatar
Michael Beck committed
606
smaller_mode(const ir_mode *sm, const ir_mode *lm)
607
{
Michael Beck's avatar
Michael Beck committed
608
609
  int sm_bits, lm_bits;

610
611
612
613
614
615
  ANNOUNCE();
  assert(sm);
  assert(lm);

  if (sm == lm) return 1;

Michael Beck's avatar
Michael Beck committed
616
617
618
  sm_bits = get_mode_size_bits(sm);
  lm_bits = get_mode_size_bits(lm);

619
620
  switch(get_mode_sort(sm))
  {
621
    case irms_int_number:
622
623
      switch(get_mode_sort(lm))
      {
624
        case irms_int_number:
625
626
627
628
          /* integers are convertable if
           *   - both have the same sign and lm is the larger one
           *   - lm is the signed one and is at least two bits larger
           *     (one for the sign, one for the highest bit of sm)
Michael Beck's avatar
Michael Beck committed
629
	   *   - sm & lm are two_complement and lm has greater or equal number of bits
630
           */
Michael Beck's avatar
Michael Beck committed
631
632
633
634
635
          if (   get_mode_arithmetic(sm) == get_mode_arithmetic(lm)
	      && get_mode_arithmetic(sm) == irma_twos_complement) {
	    return lm_bits >= sm_bits;
	  }
          else if (mode_is_signed(sm))
636
          {
Michael Beck's avatar
Michael Beck committed
637
            if ( mode_is_signed(lm) && (lm_bits >= sm_bits) )
638
639
640
641
              return 1;
          }
          else if (mode_is_signed(lm))
          {
Michael Beck's avatar
Michael Beck committed
642
            if (lm_bits > sm_bits + 1)
643
644
              return 1;
          }
Michael Beck's avatar
Michael Beck committed
645
          else if (lm_bits >= sm_bits)
646
647
648
649
650
          {
            return 1;
          }
          break;

651
        case irms_float_number:
652
653
654
655
656
657
658
659
          /* int to float works if the float is large enough */
          return 0;

        default:
          break;
      }
      break;

660
    case irms_float_number:
Michael Beck's avatar
Michael Beck committed
661
662
663
664
665
      if (get_mode_arithmetic(sm) == get_mode_arithmetic(lm)) {
        if ( (get_mode_sort(lm) == irms_float_number)
           && (get_mode_size_bits(lm) >= get_mode_size_bits(sm)) )
          return 1;
      }
666
667
      break;

668
    case irms_reference:
669
670
671
672
673
674
675
676
677
678
679
       /* do exist machines out there with different pointer lenghts ?*/
      return 0;

    default:
      break;
  }

  /* else */
  return 0;
}

680
/* initialization, build the default modes */
681
682
683
void
init_mode (void)
{
684
  ir_mode newmode;
685
686
687
  ANNOUNCE();
  /* init flexible array */

688
689
690
  obstack_init(&modes);

  num_modes  =  0;
691
  /* initialize predefined modes */
692
693

  /* Internal Modes */
694
695
696
697
698
  newmode.arithmetic   = irma_none;
  newmode.size         = 0;
  newmode.align        = 0;
  newmode.sign         = 0;
  newmode.modulo_shift = 0;
699
  newmode.vector_elem  = 0;
700
701
  newmode.link         = NULL;
  newmode.tv_priv      = NULL;
702
703
704
705

  /* Control Flow Modes*/
  newmode.sort    = irms_control_flow;

706
  /* Basic Block */
707
  newmode.name    = new_id_from_chars("BB", 2);
708
709
710
711
712
  newmode.code    = irm_BB;

  mode_BB = register_mode(&newmode);

/* eXecution */
713
  newmode.name    = new_id_from_chars("X", 1);
714
715
716
717
718
719
  newmode.code    = irm_X;

  mode_X = register_mode(&newmode);

  /* Memory Modes */
  newmode.sort    = irms_memory;
720
721

  /* Memory */
722
  newmode.name    = new_id_from_chars("M", 1);
723
724
725
726
727
728
  newmode.code    = irm_M;

  mode_M = register_mode(&newmode);

  /* Auxiliary Modes */
  newmode.sort    = irms_auxiliary,
729
730

  /* Tuple */
731
  newmode.name    = new_id_from_chars("T", 1);
732
733
734
  newmode.code    = irm_T;

  mode_T = register_mode(&newmode);
735

736
  /* ANY */
737
  newmode.name    = new_id_from_chars("ANY", 3);
738
739
740
  newmode.code    = irm_ANY;

  mode_ANY = register_mode(&newmode);
741
742

  /* BAD */
743
  newmode.name    = new_id_from_chars("BAD", 3);
744
745
746
747
748
749
  newmode.code    = irm_BAD;

  mode_BAD = register_mode(&newmode);

  /* Internal Boolean Modes */
  newmode.sort    = irms_internal_boolean;
750

751
  /* boolean */
752
  newmode.name    = new_id_from_chars("b", 1);
753
754
755
756
757
  newmode.code    = irm_b;

  mode_b = register_mode(&newmode);

/* Data Modes */
758
  newmode.vector_elem = 1;
759
760

  /* Float Number Modes */
761
  newmode.sort       = irms_float_number;
762
  newmode.arithmetic = irma_ieee754;
763
764

  /* float */
765
  newmode.name    = new_id_from_chars("F", 1);
766
767
  newmode.code    = irm_F;
  newmode.sign    = 1;
768
  newmode.align   = 32;
769
  newmode.size    = 32;
770

771
  mode_F = register_mode(&newmode);
772
773

  /* double */
774
  newmode.name    = new_id_from_chars("D", 1);
775
776
  newmode.code    = irm_D;
  newmode.sign    = 1;
777
  newmode.align   = 64;
778
  newmode.size    = 64;
779

780
  mode_D = register_mode(&newmode);
781
782

  /* extended */
783
  newmode.name    = new_id_from_chars("E", 1);
784
785
  newmode.code    = irm_E;
  newmode.sign    = 1;
786
  newmode.align   = 128;
787
788
789
  newmode.size    = 80;

  mode_E = register_mode(&newmode);
790

791
  /* Integer Number Modes */
792
793
  newmode.sort         = irms_int_number;
  newmode.arithmetic   = irma_twos_complement;
794
795

  /* signed byte */
796
  newmode.name         = new_id_from_chars("Bs", 2);
797
798
799
800
  newmode.code         = irm_Bs;
  newmode.sign         = 1;
  newmode.align        = 8;
  newmode.size         = 8;
801
  newmode.modulo_shift = 32;
802

803
  mode_Bs = register_mode(&newmode);
804
805

  /* unsigned byte */
806
  newmode.name         = new_id_from_chars("Bu", 2);
807
808
809
810
811
  newmode.code         = irm_Bu;
  newmode.arithmetic   = irma_twos_complement;
  newmode.sign         = 0;
  newmode.align        = 8;
  newmode.size         = 8;
812
  newmode.modulo_shift = 32;
813

814
  mode_Bu = register_mode(&newmode);
815
816

  /* signed short integer */
817
  newmode.name         = new_id_from_chars("Hs", 2);
818
819
820
821
  newmode.code         = irm_Hs;
  newmode.sign         = 1;
  newmode.align        = 16;
  newmode.size         = 16;
822
  newmode.modulo_shift = 32;
823

824
  mode_Hs = register_mode(&newmode);
825
826

  /* unsigned short integer */
827
  newmode.name         = new_id_from_chars("Hu", 2);
828
829
830
831
  newmode.code         = irm_Hu;
  newmode.sign         = 0;
  newmode.align        = 16;
  newmode.size         = 16;
832
  newmode.modulo_shift = 32;
833

834
  mode_Hu = register_mode(&newmode);
835
836

  /* signed integer */
837
  newmode.name         = new_id_from_chars("Is", 2);
838
839
840
841
  newmode.code         = irm_Is;
  newmode.sign         = 1;
  newmode.align        = 32;
  newmode.size         = 32;
842
  newmode.modulo_shift = 32;
843

844
  mode_Is = register_mode(&newmode);
845
846

  /* unsigned integer */
847
  newmode.name         = new_id_from_chars("Iu", 2);
848
849
850
851
  newmode.code         = irm_Iu;
  newmode.sign         = 0;
  newmode.align        = 32;
  newmode.size         = 32;
852
  newmode.modulo_shift = 32;
853

854
  mode_Iu = register_mode(&newmode);
855
856

  /* signed long integer */
857
  newmode.name         = new_id_from_chars("Ls", 2);
858
859
860
861
  newmode.code         = irm_Ls;
  newmode.sign         = 1;
  newmode.align        = 32;
  newmode.size         = 64;
862
  newmode.modulo_shift = 64;
863

864
  mode_Ls = register_mode(&newmode);
865
866

  /* unsigned long integer */
867
  newmode.name         = new_id_from_chars("Lu", 2);
868
869
870
871
  newmode.code         = irm_Lu;
  newmode.sign         = 0;
  newmode.align        = 32;
  newmode.size         = 64;
872
  newmode.modulo_shift = 64;
873
874

  mode_Lu = register_mode(&newmode);
875

876
  /* Character Modes */
877
878
  newmode.sort         = irms_character;
  newmode.arithmetic   = irma_none;
879
880

  /* Character */
881
  newmode.name         = new_id_from_chars("C", 1);
882
883
884
885
  newmode.code         = irm_C;
  newmode.sign         = 0;
  newmode.align        = 8;
  newmode.size         = 8;
886
  newmode.modulo_shift = 32;
887

888
  mode_C = register_mode(&newmode);
889
890

  /* Unicode character */
891
  newmode.name         = new_id_from_chars("U", 1);
892
893
894
895
  newmode.code         = irm_U;
  newmode.sign         = 0;
  newmode.align        = 16;
  newmode.size         = 16;
896
  newmode.modulo_shift = 32;
897

898
899
900
901
902
  mode_U = register_mode(&newmode);

  /* Reference Modes */
  newmode.sort    = irms_reference;
  newmode.arithmetic = irma_twos_complement;
903
904

  /* pointer */
905
  newmode.name         = new_id_from_chars("P", 1);
906
907
908
909
  newmode.code         = irm_P;
  newmode.sign         = 0;
  newmode.align        = 32;
  newmode.size         = 32;
910
  newmode.modulo_shift = 0;
911
912

  mode_P = register_mode(&newmode);
Michael Beck's avatar
fixed:    
Michael Beck committed
913
914
915

  /* set the machine specific modes to the predifined ones */
  mode_P_mach = mode_P;
916
}
917

918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
ir_mode *find_unsigned_mode(const ir_mode *mode)
{
  ir_mode n = *mode;

  assert(mode->sort == irms_int_number);
  n.sign = 0;
  return find_mode(&n);
}

ir_mode *find_signed_mode(const ir_mode *mode)
{
  ir_mode n = *mode;

  assert(mode->sort == irms_int_number);
  n.sign = 1;
  return find_mode(&n);
}

936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966

void finish_mode(void) {
  obstack_free(&modes, 0);

  mode_T = NULL;
  mode_X = NULL;
  mode_M = NULL;
  mode_BB = NULL;
  mode_ANY = NULL;
  mode_BAD = NULL;

  mode_F = NULL;
  mode_D = NULL;
  mode_E = NULL;

  mode_Bs = NULL;
  mode_Bu = NULL;
  mode_Hs = NULL;
  mode_Hu = NULL;
  mode_Is = NULL;
  mode_Iu = NULL;
  mode_Ls = NULL;
  mode_Lu = NULL;

  mode_C = NULL;
  mode_U = NULL;
  mode_b = NULL;
  mode_P = NULL;

  mode_P_mach = NULL;
}