Debian Codegraph @

orc/orctarget.c

raw
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <orc/orcdebug.h>
9 #include <orc/orcutils.h>
10 #include <orc/orcutils-private.h>
11 #include <orc/orctarget.h>
12 #include <orc/orcinternal.h>
13
14 static OrcTarget *targets[ORC_N_TARGETS];
15 static int n_targets;
16
17 static OrcTarget *default_target;
18
19 void
20 orc_target_register (OrcTarget *target)
21 {
22 if (n_targets == ORC_N_TARGETS) {
23 ORC_ERROR ("Can not register more targets");
24 return;
25 }
26
27 targets[n_targets] = target;
28 n_targets++;
29
30 if (target->executable) {
31 default_target = target;
32 }
33 }
34
35 OrcTarget *
36 orc_target_get_by_name (const char *name)
37 {
38 int i;
39
40 if (name == NULL) return orc_target_get_default();
41
42 for(i=0;i<n_targets;i++){
43 if (strcmp (name, targets[i]->name) == 0) {
44 return targets[i];
45 }
46 }
47
48 return NULL;
49 }
50
51 OrcTarget *
52 orc_target_get_default (void)
53 {
54 const char *envvar = _orc_getenv ("ORC_TARGET");
55
56 if (!envvar) {
57 /* Since version 0.4.39, ORC_BACKEND was used instead */
58 envvar = _orc_getenv ("ORC_BACKEND");
59 }
60
61 if (envvar != NULL) {
62 OrcTarget *const target = orc_target_get_by_name (envvar);
63
64 free ((char *)envvar);
65 if (target != NULL)
66 return target;
67 }
68
69 return default_target;
70 }
71
72 const char *
73 orc_target_get_name (OrcTarget *target)
74 {
75 if (target == NULL) return NULL;
76 return target->name;
77 }
78
79 unsigned int
80 orc_target_get_default_flags (OrcTarget *target)
81 {
82 if (target == NULL) return 0;
83 return target->get_default_flags();
84 }
85
86 const char *
87 orc_target_get_preamble (OrcTarget *target)
88 {
89 if (target->get_asm_preamble == NULL) return "";
90
91 return target->get_asm_preamble ();
92 }
93
94 const char *
95 orc_target_get_asm_preamble (const char *target)
96 {
97 OrcTarget *t;
98
99 t = orc_target_get_by_name (target);
100 if (t == NULL) return "";
101
102 return orc_target_get_preamble (t);
103 }
104
105 const char *
106 orc_target_get_flag_name (OrcTarget *target, int shift)
107 {
108 if (target->get_flag_name == NULL) return "";
109
110 return target->get_flag_name (shift);
111 }
112
113 /* FIXME the order of parameters should not be this,
114 * first the target + flags, then the opcode
115 */
116 OrcRule *
117 orc_target_get_rule (OrcTarget *target, OrcStaticOpcode *opcode,
118 unsigned int target_flags)
119 {
120 OrcOpcodeSet *opcode_set;
121 OrcRule *rule;
122 int i;
123 int j;
124
125
126 /* FIXME yes, we are iterating twice */
127 opcode_set = orc_opcode_set_find_by_opcode (opcode);
128 j = orc_opcode_set_find_by_name (opcode_set, opcode->name);
129
130 for (i = target->n_rule_sets - 1; i >= 0; i--) {
131 if (target->rule_sets[i].opcode_major != opcode_set->opcode_major) continue;
132 if (target->rule_sets[i].required_target_flags & (~target_flags)) continue;
133
134 /* A rule set has the same number of rules as the opcode_set has opcodes */
135 rule = target->rule_sets[i].rules + j;
136 if (rule->emit) return rule;
137 }
138
139 return NULL;
140 }
141
142 /**
143 * orc_target_add_rule_set:
144 *
145 * Adds a new #OrcRuleSet to the target
146 *
147 * @target: The #OrcTarget to register the #OrcRuleSet in
148 * @opcode_set: The #OrcOpcodeSet the #OrcRuleSet will implement by registering
149 * #OrcRules with orc_rule_register()
150 * @required_flags: The #OrcTarget flags the #OrcRuleSet must implement to use
151 * the #OrcRules
152 * @returns: The #OrcRuleSet just created
153 */
154 OrcRuleSet * orc_target_add_rule_set (OrcTarget *target,
155 OrcOpcodeSet *opcode_set, unsigned int required_flags)
156 {
157 OrcRuleSet *rule_set;
158
159 if (target->n_rule_sets == ORC_N_RULE_SETS) {
160 ORC_ERROR ("Can not allocate more rule sets");
161 return NULL;
162 }
163
164 rule_set = target->rule_sets + target->n_rule_sets;
165 target->n_rule_sets++;
166
167 memset (rule_set, 0, sizeof(OrcRuleSet));
168
169 rule_set->opcode_major = opcode_set->opcode_major;
170 rule_set->required_target_flags = required_flags;
171
172 rule_set->rules = orc_malloc (sizeof(OrcRule) * opcode_set->n_opcodes);
173 memset (rule_set->rules, 0, sizeof(OrcRule) * opcode_set->n_opcodes);
174
175 return rule_set;
176 }