irmode.c 22 KB
Newer Older
Götz Lindenmaier's avatar
Götz Lindenmaier committed
1
2
3
4
5
6
7
8
9
10
11
/*
 * 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
added    
Boris Boesler committed
12
#ifdef HAVE_CONFIG_H
Michael Beck's avatar
Michael Beck committed
13
# include "config.h"
Boris Boesler's avatar
added    
Boris Boesler committed
14
15
#endif

Michael Beck's avatar
Michael Beck committed
16
#ifdef HAVE_STDLIB_H
17
# include <stdlib.h>
Michael Beck's avatar
Michael Beck committed
18
19
#endif
#ifdef HAVE_STRING_H
20
# include <string.h>
Michael Beck's avatar
Michael Beck committed
21
22
23
#endif

# include <stddef.h>
24

25
# include "irprog_t.h"
26
27
# include "irmode_t.h"
# include "ident.h"
Michael Beck's avatar
Michael Beck committed
28
# include "tv_t.h"
29
# include "obst.h"
30
# include "irhooks.h"
31
# include "irtools.h"
Christian Schäfer's avatar
Christian Schäfer committed
32

33
#if 0
Götz Lindenmaier's avatar
Götz Lindenmaier committed
34
static long long count = 0;
35
36
37
38
#  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
39

40
41
42
/* * *
 * local values
 * * */
43

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

Michael Beck's avatar
Michael Beck committed
45
/** dynamic array to hold all modes */
46
static struct obstack modes;
Christian Schäfer's avatar
Christian Schäfer committed
47

Michael Beck's avatar
Michael Beck committed
48
/** number of defined modes */
49
static int num_modes;
Christian Schäfer's avatar
Christian Schäfer committed
50

51
52
53
/* * *
 * local functions
 * * */
Christian Schäfer's avatar
Christian Schäfer committed
54

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

72
73
  return 0;
}
Matthias Heil's avatar
Matthias Heil committed
74

75
76
77
78
79
/*
 * calculates the next obstack address
 */
static void *next_obstack_adr(struct obstack *o, void *p, size_t s)
{
80
  PTR_INT_TYPE adr = PTR_TO_INT((char *)p);
81
82
  int mask = obstack_alignment_mask(o);

83
  adr += s + mask;
84

85
  return INT_TO_PTR(adr & ~mask);
86
87
}

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

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

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

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

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

123
124
  return NULL;
}
Christian Schäfer's avatar
Christian Schäfer committed
125

126
127
128
/**
 * sets special values of modes
 */
129
130
static void set_mode_values(ir_mode* mode)
{
131
132
  switch (get_mode_sort(mode))
  {
133
    case irms_character:
134
135
    case irms_int_number:
    case irms_float_number:
Michael Beck's avatar
Michael Beck committed
136
137
      mode->min  = get_tarval_min(mode);
      mode->max  = get_tarval_max(mode);
138
      mode->null = get_tarval_null(mode);
Michael Beck's avatar
Michael Beck committed
139
140
      mode->one  = get_tarval_one(mode);
      mode->minus_one = get_tarval_minus_one(mode);
141
142
143
      break;

    case irms_internal_boolean:
Michael Beck's avatar
Michael Beck committed
144
145
      mode->min  = tarval_b_false;
      mode->max  = tarval_b_true;
146
      mode->null = tarval_b_false;
Michael Beck's avatar
Michael Beck committed
147
148
      mode->one  = tarval_b_true;
      mode->minus_one = tarval_bad;
149
150
151
      break;

    case irms_reference:
Michael Beck's avatar
Michael Beck committed
152
153
154
155
156
      mode->min  = tarval_bad;
      mode->max  = tarval_bad;
      mode->null = get_tarval_null(mode);
      mode->one  = tarval_bad;
      mode->minus_one = tarval_bad;
157
158
159
160
161
      break;

    case irms_auxiliary:
    case irms_memory:
    case irms_control_flow:
Michael Beck's avatar
Michael Beck committed
162
163
      mode->min  = tarval_bad;
      mode->max  = tarval_bad;
164
      mode->null = tarval_bad;
Michael Beck's avatar
Michael Beck committed
165
166
      mode->one  = tarval_bad;
      mode->minus_one = tarval_bad;
167
168
      break;
  }
169
}
170

171
172
173
/* * *
 * globals defined in irmode.h
 * * */
Matthias Heil's avatar
Matthias Heil committed
174

175
/* --- Predefined modes --- */
Christian Schäfer's avatar
Christian Schäfer committed
176

177
178
179
180
181
/* FIRM internal modes: */
ir_mode *mode_T;
ir_mode *mode_X;
ir_mode *mode_M;
ir_mode *mode_BB;
182
183
ir_mode *mode_ANY;
ir_mode *mode_BAD;
Christian Schäfer's avatar
Christian Schäfer committed
184

185
186
187
188
/* 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
189

190
191
192
193
194
195
196
197
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
198

199
200
201
202
ir_mode *mode_C;
ir_mode *mode_U;
ir_mode *mode_b;
ir_mode *mode_P;
Michael Beck's avatar
fixed:    
Michael Beck committed
203
204

/* machine specific modes */
205
206
ir_mode *mode_P_code;	/**< machine specific pointer mode for code addresses */
ir_mode *mode_P_data;	/**< machine specific pointer mode for data addresses */
207

208
209
210
211
212
/* * *
 * functions defined in irmode.h
 * * */

/* JNI access functions */
Michael Beck's avatar
Michael Beck committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
ir_mode *get_modeT(void) { ANNOUNCE(); return mode_T; }
ir_mode *get_modeF(void) { ANNOUNCE(); return mode_F; }
ir_mode *get_modeD(void) { ANNOUNCE(); return mode_D; }
ir_mode *get_modeE(void) { ANNOUNCE(); return mode_E; }
ir_mode *get_modeBs(void) { ANNOUNCE(); return mode_Bs; }
ir_mode *get_modeBu(void) { ANNOUNCE(); return mode_Bu; }
ir_mode *get_modeHs(void) { ANNOUNCE(); return mode_Hs; }
ir_mode *get_modeHu(void) { ANNOUNCE(); return mode_Hu; }
ir_mode *get_modeIs(void) { ANNOUNCE(); return mode_Is; }
ir_mode *get_modeIu(void) { ANNOUNCE(); return mode_Iu; }
ir_mode *get_modeLs(void) { ANNOUNCE(); return mode_Ls; }
ir_mode *get_modeLu(void) { ANNOUNCE(); return mode_Lu; }
ir_mode *get_modeC(void) { ANNOUNCE(); return mode_C; }
ir_mode *get_modeU(void) { ANNOUNCE(); return mode_U; }
ir_mode *get_modeb(void) { ANNOUNCE(); return mode_b; }
ir_mode *get_modeP(void) { ANNOUNCE(); return mode_P; }
ir_mode *get_modeX(void) { ANNOUNCE(); return mode_X; }
ir_mode *get_modeM(void) { ANNOUNCE(); return mode_M; }
ir_mode *get_modeBB(void) { ANNOUNCE(); return mode_BB; }
ir_mode *get_modeANY(void) { ANNOUNCE(); return mode_ANY; }
ir_mode *get_modeBAD(void) { ANNOUNCE(); return mode_BAD; }
234

235

236
ir_mode *(get_modeP_code)(void) {
237
  ANNOUNCE();
238
  return _get_modeP_code();
Michael Beck's avatar
Michael Beck committed
239
240
}

241
ir_mode *(get_modeP_data)(void) {
Michael Beck's avatar
Michael Beck committed
242
  ANNOUNCE();
243
244
245
246
247
248
249
250
251
252
253
254
255
  return _get_modeP_data();
}

void set_modeP_code(ir_mode *p) {
  assert(mode_is_reference(p));

  mode_P_code = p;
}

void set_modeP_data(ir_mode *p) {
  assert(mode_is_reference(p));

  mode_P_data = p;
Michael Beck's avatar
Michael Beck committed
256
}
257

258
/**
259
260
261
 * Registers a new mode.
 *
 * @param new_mode  The new mode template.
262
 */
263
static ir_mode *register_mode(const ir_mode *new_mode)
264
{
265
  ir_mode *mode = NULL;
266
267
268
269

  ANNOUNCE();
  assert(new_mode);

270
  /* copy mode struct to modes array */
271
  mode = (ir_mode *)obstack_copy(&modes, new_mode, sizeof(*mode));
272
273

  mode->kind = k_ir_mode;
274
  if (num_modes >= irm_max) mode->code = num_modes;
275
276
  num_modes++;

277
278
279
  /* add the new mode to the irp list of modes */
  add_irp_mode(mode);

280
  set_mode_values(mode);
281

Michael Beck's avatar
Michael Beck committed
282
  hook_new_mode(new_mode, mode);
283
284
285
286
287
288
  return mode;
}

/*
 * Creates a new mode.
 */
Michael Beck's avatar
Michael Beck committed
289
ir_mode *new_ir_mode(const char *name, mode_sort sort, int bit_size, int sign,
290
		     mode_arithmetic arithmetic, unsigned int modulo_shift )
291
292
{
  ir_mode mode_tmpl;
293
  ir_mode *mode = NULL;
294

295
296
297
298
299
  mode_tmpl.name         = new_id_from_str(name);
  mode_tmpl.sort         = sort;
  mode_tmpl.size         = bit_size;
  mode_tmpl.sign         = sign ? 1 : 0;
  mode_tmpl.modulo_shift = (mode_tmpl.sort == irms_int_number) ? modulo_shift : 0;
300
  mode_tmpl.vector_elem  = 1;
301
302
303
  mode_tmpl.arithmetic   = arithmetic;
  mode_tmpl.link         = NULL;
  mode_tmpl.tv_priv      = NULL;
Christian Schäfer's avatar
Christian Schäfer committed
304

305
  mode = find_mode(&mode_tmpl);
306
307
  if (mode) {
    hook_new_mode(&mode_tmpl, mode);
308
309
    return mode;
  }
310
311
312

  /* sanity checks */
  switch (sort)
313
  {
314
315
316
317
318
    case irms_auxiliary:
    case irms_control_flow:
    case irms_memory:
    case irms_internal_boolean:
      assert(0 && "internal modes cannot be user defined");
319
      break;
320
321
322
323
324

    case irms_float_number:
    case irms_int_number:
    case irms_reference:
    case irms_character:
325
      mode = register_mode(&mode_tmpl);
326
  }
327
  return mode;
328
329
}

330
331
332
/*
 * Creates a new vector mode.
 */
Michael Beck's avatar
Michael Beck committed
333
ir_mode *new_ir_vector_mode(const char *name, mode_sort sort, int bit_size, unsigned num_of_elem, int sign,
334
335
336
		     mode_arithmetic arithmetic, unsigned int modulo_shift )
{
  ir_mode mode_tmpl;
337
  ir_mode *mode = NULL;
338
339
340
341
342
343
344
345
346
347
348
349

  mode_tmpl.name         = new_id_from_str(name);
  mode_tmpl.sort         = sort;
  mode_tmpl.size         = bit_size * num_of_elem;
  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);
350
351
  if (mode) {
    hook_new_mode(&mode_tmpl, mode);
352
    return mode;
353
  }
354
355
356

  if (num_of_elem <= 1) {
    assert(0 && "vector modes should have at least 2 elements");
Michael Beck's avatar
Michael Beck committed
357
    return NULL;
358
359
360
361
362
363
364
365
366
367
  }

  /* 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");
368
      break;
369
370
371
372

    case irms_reference:
    case irms_character:
      assert(0 && "only integer and floating point modes can be vectorized");
373
      break;
374
375
376

    case irms_float_number:
      assert(0 && "not yet implemented");
377
      break;
378
379

    case irms_int_number:
380
      mode = register_mode(&mode_tmpl);
381
  }
382
  return mode;
383
384
}

385
/* Functions for the direct access to all attributes of an ir_mode */
Christian Schäfer's avatar
Christian Schäfer committed
386
modecode
Michael Beck's avatar
Michael Beck committed
387
(get_mode_modecode)(const ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
388
{
389
  ANNOUNCE();
390
  return _get_mode_modecode(mode);
Christian Schäfer's avatar
Christian Schäfer committed
391
392
393
}

ident *
Michael Beck's avatar
Michael Beck committed
394
(get_mode_ident)(const ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
395
{
396
  ANNOUNCE();
397
  return _get_mode_ident(mode);
Christian Schäfer's avatar
Christian Schäfer committed
398
399
}

400
const char *
Michael Beck's avatar
Michael Beck committed
401
get_mode_name(const ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
402
{
403
  ANNOUNCE();
404
  return get_id_str(mode->name);
Christian Schäfer's avatar
Christian Schäfer committed
405
406
}

407
mode_sort
Michael Beck's avatar
Michael Beck committed
408
(get_mode_sort)(const ir_mode* mode)
409
410
{
  ANNOUNCE();
411
  return _get_mode_sort(mode);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
412
413
}

Michael Beck's avatar
Michael Beck committed
414
415
int
(get_mode_size_bits)(const ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
416
{
417
  ANNOUNCE();
418
  return _get_mode_size_bits(mode);
Christian Schäfer's avatar
Christian Schäfer committed
419
420
}

Michael Beck's avatar
Michael Beck committed
421
422
int
(get_mode_size_bytes)(const ir_mode *mode) {
423
  ANNOUNCE();
424
  return _get_mode_size_bytes(mode);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
425
426
}

427
int
Michael Beck's avatar
Michael Beck committed
428
(get_mode_sign)(const ir_mode *mode)
429
430
{
  ANNOUNCE();
431
  return _get_mode_sign(mode);
432
433
}

Michael Beck's avatar
Michael Beck committed
434
435
int
(get_mode_arithmetic)(const ir_mode *mode)
436
437
{
  ANNOUNCE();
Michael Beck's avatar
Michael Beck committed
438
  return get_mode_arithmetic(mode);
439
440
}

441
442
443
444
445

/* 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
446
447
unsigned int
(get_mode_modulo_shift)(const ir_mode *mode) {
448
  return _get_mode_modulo_shift(mode);
449
450
}

Michael Beck's avatar
Michael Beck committed
451
unsigned int
452
(get_mode_n_vector_elems)(const ir_mode *mode) {
453
  return _get_mode_vector_elems(mode);
454
455
}

Michael Beck's avatar
Michael Beck committed
456
457
void *
(get_mode_link)(const ir_mode *mode)
458
459
{
  ANNOUNCE();
460
  return _get_mode_link(mode);
461
462
}

Michael Beck's avatar
Michael Beck committed
463
464
void
(set_mode_link)(ir_mode *mode, void *l)
465
{
466
  _set_mode_link(mode, l);
467
468
}

Christian Schäfer's avatar
Christian Schäfer committed
469
tarval *
Götz Lindenmaier's avatar
Götz Lindenmaier committed
470
get_mode_min (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->min;
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_max (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->max;
Christian Schäfer's avatar
Christian Schäfer committed
489
490
491
}

tarval *
Götz Lindenmaier's avatar
Götz Lindenmaier committed
492
get_mode_null (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->null;
Christian Schäfer's avatar
Christian Schäfer committed
500
501
}

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

510
  return mode->one;
Christian Schäfer's avatar
Christian Schäfer committed
511
512
}

Michael Beck's avatar
Michael Beck committed
513
514
515
516
517
518
519
520
521
522
523
tarval *
get_mode_minus_one (ir_mode *mode)
{
  ANNOUNCE();
  assert(mode);
  assert(get_mode_modecode(mode) < num_modes);
  assert(mode_is_data(mode));

  return mode->minus_one;
}

524
525
tarval *
get_mode_infinite(ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
526
{
527
528
529
530
531
  ANNOUNCE();
  assert(mode);
  assert(get_mode_modecode(mode) < num_modes);
  assert(mode_is_float(mode));

532
  return get_tarval_plus_inf(mode);
Christian Schäfer's avatar
Christian Schäfer committed
533
534
}

535
536
tarval *
get_mode_NAN(ir_mode *mode)
Christian Schäfer's avatar
Christian Schäfer committed
537
{
538
539
540
541
542
543
  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
544
545
}

546
547
548
549
550
551
552
553
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
554
int
Michael Beck's avatar
Michael Beck committed
555
(mode_is_signed)(const ir_mode *mode) {
556
  ANNOUNCE();
557
  return _mode_is_signed(mode);
Christian Schäfer's avatar
Christian Schäfer committed
558
559
}

560
int
Michael Beck's avatar
Michael Beck committed
561
(mode_is_float)(const ir_mode *mode) {
562
  ANNOUNCE();
563
  return _mode_is_float(mode);
Christian Schäfer's avatar
Christian Schäfer committed
564
565
}

566
int
Michael Beck's avatar
Michael Beck committed
567
(mode_is_int)(const ir_mode *mode) {
568
  ANNOUNCE();
569
  return _mode_is_int(mode);
Christian Schäfer's avatar
Christian Schäfer committed
570
571
}

Michael Beck's avatar
Michael Beck committed
572
573
int
(mode_is_character)(const ir_mode *mode) {
574
  ANNOUNCE();
575
  return _mode_is_character(mode);
576
577
}

Michael Beck's avatar
Michael Beck committed
578
579
int
(mode_is_reference)(const ir_mode *mode) {
580
  ANNOUNCE();
581
  return _mode_is_reference(mode);
582
583
}

584
int
Michael Beck's avatar
Michael Beck committed
585
(mode_is_num)(const ir_mode *mode) {
586
  ANNOUNCE();
587
  return _mode_is_num(mode);
Christian Schäfer's avatar
Christian Schäfer committed
588
589
}

Michael Beck's avatar
fixed:    
Michael Beck committed
590
int
Michael Beck's avatar
Michael Beck committed
591
(mode_is_numP)(const ir_mode *mode) {
Michael Beck's avatar
fixed:    
Michael Beck committed
592
  ANNOUNCE();
593
  return _mode_is_numP(mode);
Michael Beck's avatar
fixed:    
Michael Beck committed
594
595
}

596
int
Michael Beck's avatar
Michael Beck committed
597
(mode_is_data)(const ir_mode *mode) {
598
  ANNOUNCE();
599
  return _mode_is_data(mode);
Christian Schäfer's avatar
Christian Schäfer committed
600
601
602
}

int
Michael Beck's avatar
Michael Beck committed
603
(mode_is_datab)(const ir_mode *mode) {
604
  ANNOUNCE();
605
  return _mode_is_datab(mode);
Christian Schäfer's avatar
Christian Schäfer committed
606
607
608
}

int
Michael Beck's avatar
Michael Beck committed
609
(mode_is_dataM)(const ir_mode *mode) {
610
  ANNOUNCE();
611
  return _mode_is_dataM(mode);
Christian Schäfer's avatar
Christian Schäfer committed
612
}
613
614

int
Michael Beck's avatar
Michael Beck committed
615
(mode_is_float_vector)(const ir_mode *mode) {
616
  ANNOUNCE();
617
  return _mode_is_float_vector(mode);
618
619
620
}

int
Michael Beck's avatar
Michael Beck committed
621
(mode_is_int_vector)(const ir_mode *mode) {
622
  ANNOUNCE();
623
  return _mode_is_int_vector(mode);
Michael Beck's avatar
Michael Beck committed
624
625
}

626
/* Returns true if sm can be converted to lm without loss. */
627
int
Michael Beck's avatar
Michael Beck committed
628
smaller_mode(const ir_mode *sm, const ir_mode *lm)
629
{
Michael Beck's avatar
Michael Beck committed
630
631
  int sm_bits, lm_bits;

632
633
634
635
636
637
  ANNOUNCE();
  assert(sm);
  assert(lm);

  if (sm == lm) return 1;

Michael Beck's avatar
Michael Beck committed
638
639
640
  sm_bits = get_mode_size_bits(sm);
  lm_bits = get_mode_size_bits(lm);

641
642
  switch(get_mode_sort(sm))
  {
643
    case irms_int_number:
644
645
      switch(get_mode_sort(lm))
      {
646
        case irms_int_number:
647
648
649
650
          /* 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
651
	   *   - sm & lm are two_complement and lm has greater or equal number of bits
652
           */
Michael Beck's avatar
Michael Beck committed
653
654
655
656
657
          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))
658
          {
Michael Beck's avatar
Michael Beck committed
659
            if ( mode_is_signed(lm) && (lm_bits >= sm_bits) )
660
661
662
663
              return 1;
          }
          else if (mode_is_signed(lm))
          {
Michael Beck's avatar
Michael Beck committed
664
            if (lm_bits > sm_bits + 1)
665
666
              return 1;
          }
Michael Beck's avatar
Michael Beck committed
667
          else if (lm_bits >= sm_bits)
668
669
670
671
672
          {
            return 1;
          }
          break;

673
        case irms_float_number:
674
675
676
677
678
679
680
681
          /* int to float works if the float is large enough */
          return 0;

        default:
          break;
      }
      break;

682
    case irms_float_number:
Michael Beck's avatar
Michael Beck committed
683
684
685
686
687
      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;
      }
688
689
      break;

690
    case irms_reference:
691
692
693
694
695
696
697
698
699
700
701
       /* do exist machines out there with different pointer lenghts ?*/
      return 0;

    default:
      break;
  }

  /* else */
  return 0;
}

702
/* initialization, build the default modes */
703
704
705
void
init_mode (void)
{
706
  ir_mode newmode;
707
708
709
  ANNOUNCE();
  /* init flexible array */

710
711
712
  obstack_init(&modes);

  num_modes  =  0;
713
  /* initialize predefined modes */
714
715

  /* Internal Modes */
716
717
718
719
  newmode.arithmetic   = irma_none;
  newmode.size         = 0;
  newmode.sign         = 0;
  newmode.modulo_shift = 0;
720
  newmode.vector_elem  = 0;
721
722
  newmode.link         = NULL;
  newmode.tv_priv      = NULL;
723
724
725
726

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

727
  /* Basic Block */
728
  newmode.name    = new_id_from_chars("BB", 2);
729
730
731
732
733
  newmode.code    = irm_BB;

  mode_BB = register_mode(&newmode);

/* eXecution */
734
  newmode.name    = new_id_from_chars("X", 1);
735
736
737
738
739
740
  newmode.code    = irm_X;

  mode_X = register_mode(&newmode);

  /* Memory Modes */
  newmode.sort    = irms_memory;
741
742

  /* Memory */
743
  newmode.name    = new_id_from_chars("M", 1);
744
745
746
747
748
749
  newmode.code    = irm_M;

  mode_M = register_mode(&newmode);

  /* Auxiliary Modes */
  newmode.sort    = irms_auxiliary,
750
751

  /* Tuple */
752
  newmode.name    = new_id_from_chars("T", 1);
753
754
755
  newmode.code    = irm_T;

  mode_T = register_mode(&newmode);
756

757
  /* ANY */
758
  newmode.name    = new_id_from_chars("ANY", 3);
759
760
761
  newmode.code    = irm_ANY;

  mode_ANY = register_mode(&newmode);
762
763

  /* BAD */
764
  newmode.name    = new_id_from_chars("BAD", 3);
765
766
767
768
769
770
  newmode.code    = irm_BAD;

  mode_BAD = register_mode(&newmode);

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

772
  /* boolean */
773
  newmode.name    = new_id_from_chars("b", 1);
774
775
776
777
778
  newmode.code    = irm_b;

  mode_b = register_mode(&newmode);

/* Data Modes */
779
  newmode.vector_elem = 1;
780
781

  /* Float Number Modes */
782
  newmode.sort       = irms_float_number;
783
  newmode.arithmetic = irma_ieee754;
784
785

  /* float */
786
  newmode.name    = new_id_from_chars("F", 1);
787
788
789
  newmode.code    = irm_F;
  newmode.sign    = 1;
  newmode.size    = 32;
790

791
  mode_F = register_mode(&newmode);
792
793

  /* double */
794
  newmode.name    = new_id_from_chars("D", 1);
795
796
797
  newmode.code    = irm_D;
  newmode.sign    = 1;
  newmode.size    = 64;
798

799
  mode_D = register_mode(&newmode);
800
801

  /* extended */
802
  newmode.name    = new_id_from_chars("E", 1);
803
804
805
806
807
  newmode.code    = irm_E;
  newmode.sign    = 1;
  newmode.size    = 80;

  mode_E = register_mode(&newmode);
808

809
  /* Integer Number Modes */
810
811
  newmode.sort         = irms_int_number;
  newmode.arithmetic   = irma_twos_complement;
812
813

  /* signed byte */
814
  newmode.name         = new_id_from_chars("Bs", 2);
815
816
817
  newmode.code         = irm_Bs;
  newmode.sign         = 1;
  newmode.size         = 8;
818
  newmode.modulo_shift = 32;
819

820
  mode_Bs = register_mode(&newmode);
821
822

  /* unsigned byte */
823
  newmode.name         = new_id_from_chars("Bu", 2);
824
825
826
827
  newmode.code         = irm_Bu;
  newmode.arithmetic   = irma_twos_complement;
  newmode.sign         = 0;
  newmode.size         = 8;
828
  newmode.modulo_shift = 32;
829

830
  mode_Bu = register_mode(&newmode);
831
832

  /* signed short integer */
833
  newmode.name         = new_id_from_chars("Hs", 2);
834
835
836
  newmode.code         = irm_Hs;
  newmode.sign         = 1;
  newmode.size         = 16;
837
  newmode.modulo_shift = 32;
838

839
  mode_Hs = register_mode(&newmode);
840
841

  /* unsigned short integer */
842
  newmode.name         = new_id_from_chars("Hu", 2);
843
844
845
  newmode.code         = irm_Hu;
  newmode.sign         = 0;
  newmode.size         = 16;
846
  newmode.modulo_shift = 32;
847

848
  mode_Hu = register_mode(&newmode);
849
850

  /* signed integer */
851
  newmode.name         = new_id_from_chars("Is", 2);
852
853
854
  newmode.code         = irm_Is;
  newmode.sign         = 1;
  newmode.size         = 32;
855
  newmode.modulo_shift = 32;
856

857
  mode_Is = register_mode(&newmode);
858
859

  /* unsigned integer */
860
  newmode.name         = new_id_from_chars("Iu", 2);
861
862
863
  newmode.code         = irm_Iu;
  newmode.sign         = 0;
  newmode.size         = 32;
864
  newmode.modulo_shift = 32;
865

866
  mode_Iu = register_mode(&newmode);
867
868

  /* signed long integer */
869
  newmode.name         = new_id_from_chars("Ls", 2);
870
871
872
  newmode.code         = irm_Ls;
  newmode.sign         = 1;
  newmode.size         = 64;
873
  newmode.modulo_shift = 64;
874

875
  mode_Ls = register_mode(&newmode);
876
877

  /* unsigned long integer */
878
  newmode.name         = new_id_from_chars("Lu", 2);
879
880
881
  newmode.code         = irm_Lu;
  newmode.sign         = 0;
  newmode.size         = 64;
882
  newmode.modulo_shift = 64;
883
884

  mode_Lu = register_mode(&newmode);
885

886
  /* Character Modes */
887
  newmode.sort         = irms_character;
888
  newmode.arithmetic   = irma_twos_complement;
889
  newmode.modulo_shift = 0;
890
891

  /* Character */
892
  newmode.name         = new_id_from_chars("C", 1);
893
894
895
  newmode.code         = irm_C;
  newmode.sign         = 0;
  newmode.size         = 8;
896

897
  mode_C = register_mode(&newmode);
898
899

  /* Unicode character */
900
  newmode.name         = new_id_from_chars("U", 1);
901
902
903
  newmode.code         = irm_U;
  newmode.sign         = 0;
  newmode.size         = 16;