bearch.c 3.09 KB
Newer Older
1
2
3
4
5
6
7
/**
 * Processor architecture specification.
 * @author Sebastian Hack
 * @date 11.2.2005
 *
 * $Id$
 */
Michael Beck's avatar
Michael Beck committed
8
9
10
11
12
13
14
15
16
17
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
18

19
20
#include <string.h>

Michael Beck's avatar
Michael Beck committed
21
#include "bearch.h"
Sebastian Hack's avatar
Sebastian Hack committed
22
#include "ircons_t.h"
23

Sebastian Hack's avatar
Sebastian Hack committed
24
#include "bitset.h"
Sebastian Hack's avatar
Sebastian Hack committed
25
#include "pset.h"
26
27
#include "entity.h"

Sebastian Hack's avatar
Sebastian Hack committed
28
arch_env_t *arch_env_init(arch_env_t *env, const arch_isa_if_t *isa)
29
{
Sebastian Hack's avatar
Sebastian Hack committed
30
31
32
  memset(env, 0, sizeof(*env));
  env->isa = isa;
  return env;
Sebastian Hack's avatar
Sebastian Hack committed
33
34
}

Sebastian Hack's avatar
Sebastian Hack committed
35
36
arch_env_t *arch_env_add_irn_handler(arch_env_t *env,
    const arch_irn_handler_t *handler)
Sebastian Hack's avatar
Sebastian Hack committed
37
{
Sebastian Hack's avatar
Sebastian Hack committed
38
39
40
  assert(env->handlers_tos <= ARCH_MAX_HANDLERS);
  env->handlers[env->handlers_tos++] = handler;
  return env;
41
42
}

Sebastian Hack's avatar
Sebastian Hack committed
43
static const arch_irn_ops_t *fallback_irn_ops = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
44

Sebastian Hack's avatar
Sebastian Hack committed
45
int arch_register_class_put(const arch_register_class_t *cls, struct _bitset_t *bs)
46
{
Sebastian Hack's avatar
Sebastian Hack committed
47
48
49
50
51
  if(bs) {
    int i, n;
    for(i = 0, n = cls->n_regs; i < n; ++i)
      bitset_set(bs, i);
  }
52

Sebastian Hack's avatar
Sebastian Hack committed
53
  return cls->n_regs;
54
55
}

Sebastian Hack's avatar
Sebastian Hack committed
56
/**
Sebastian Hack's avatar
Sebastian Hack committed
57
58
59
60
 * Get the isa responsible for a node.
 * @param env The arch environment with the isa stack.
 * @param irn The node to get the responsible isa for.
 * @return The irn operations given by the responsible isa.
Sebastian Hack's avatar
Sebastian Hack committed
61
 */
Sebastian Hack's avatar
Sebastian Hack committed
62
63
static INLINE const arch_irn_ops_t *
get_irn_ops(const arch_env_t *env, const ir_node *irn)
64
{
Sebastian Hack's avatar
Sebastian Hack committed
65
  int i;
66

Sebastian Hack's avatar
Sebastian Hack committed
67
68
69
70
71
  for(i = env->handlers_tos - 1; i >= 0; --i) {
    const arch_irn_ops_t *ops = env->handlers[i]->get_irn_ops(irn);
    if(ops)
      return ops;
  }
Sebastian Hack's avatar
Sebastian Hack committed
72

Sebastian Hack's avatar
Sebastian Hack committed
73
  return fallback_irn_ops;
74
75
}

Sebastian Hack's avatar
Sebastian Hack committed
76
77
int arch_get_allocatable_regs(const arch_env_t *env, const ir_node *irn,
    int pos, const arch_register_class_t *cls, bitset_t *bs)
Sebastian Hack's avatar
Sebastian Hack committed
78
{
Sebastian Hack's avatar
Sebastian Hack committed
79
80
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
  const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
Sebastian Hack's avatar
Sebastian Hack committed
81

Sebastian Hack's avatar
Sebastian Hack committed
82
83
84
85
  switch(req->type) {
    case arch_register_req_type_normal:
      arch_register_class_put(req->cls, bs);
      return req->cls->n_regs;
Sebastian Hack's avatar
Sebastian Hack committed
86

Sebastian Hack's avatar
Sebastian Hack committed
87
88
    case arch_register_req_type_limited:
      return req->data.limited(irn, pos, bs);
Sebastian Hack's avatar
Sebastian Hack committed
89

Sebastian Hack's avatar
Sebastian Hack committed
90
91
92
    default:
      assert(0 && "This register requirement case is not covered");
  }
Sebastian Hack's avatar
Sebastian Hack committed
93

Sebastian Hack's avatar
Sebastian Hack committed
94
  return 0;
Sebastian Hack's avatar
Sebastian Hack committed
95
96
}

Sebastian Hack's avatar
Sebastian Hack committed
97
98
int arch_reg_is_allocatable(const arch_env_t *env, const ir_node *irn,
    int pos, const arch_register_t *reg)
Sebastian Hack's avatar
Sebastian Hack committed
99
{
Sebastian Hack's avatar
Sebastian Hack committed
100
101
102
  const arch_register_class_t *cls = arch_register_get_class(reg);
  int n_regs = arch_register_class_n_regs(cls);
  bitset_t *bs = bitset_alloca(n_regs);
Sebastian Hack's avatar
Sebastian Hack committed
103

Sebastian Hack's avatar
Sebastian Hack committed
104
105
  arch_get_allocatable_regs(env, irn, pos, cls, bs);
  return bitset_is_set(bs, arch_register_get_index(reg));
Sebastian Hack's avatar
Sebastian Hack committed
106
107
}

Sebastian Hack's avatar
Sebastian Hack committed
108
109
const arch_register_class_t *
arch_get_irn_reg_class(const arch_env_t *env, const ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
110
{
Sebastian Hack's avatar
Sebastian Hack committed
111
112
113
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
  const arch_register_req_t *req = ops->get_irn_reg_req(irn, pos);
  return req ? req->cls : NULL;
Sebastian Hack's avatar
Sebastian Hack committed
114
115
}

Sebastian Hack's avatar
Sebastian Hack committed
116
117
extern const arch_register_t *
arch_get_irn_register(const arch_env_t *env, const ir_node *irn, int idx)
118
{
Sebastian Hack's avatar
Sebastian Hack committed
119
120
121
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
  assert(idx >= 0);
  return ops->get_irn_reg(irn, idx);
122
}
123

Sebastian Hack's avatar
Sebastian Hack committed
124
125
extern void arch_set_irn_register(const arch_env_t *env,
    ir_node *irn, int idx, const arch_register_t *reg)
126
{
Sebastian Hack's avatar
Sebastian Hack committed
127
128
129
  const arch_irn_ops_t *ops = get_irn_ops(env, irn);
  assert(idx >= 0);
  ops->set_irn_reg(irn, idx, reg);
130
}