b6ffb7ff1607189ab558fe738f0ed9b6c38a45c2
[libfirm] / ir / be / bearch.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Processor architecture specification.
23  * @author      Sebastian Hack
24  * @version     $Id$
25  */
26 #ifndef FIRM_BE_BEARCH_H
27 #define FIRM_BE_BEARCH_H
28
29 #include <stdbool.h>
30
31 #include "firm_types.h"
32 #include "bitset.h"
33 #include "obst.h"
34 #include "raw_bitset.h"
35 #include "irop_t.h"
36
37 #include "be_types.h"
38 #include "beinfo.h"
39 #include "be.h"
40 #include "beirg.h"
41
42 typedef enum arch_register_class_flags_t {
43         arch_register_class_flag_none      = 0,
44         /**< don't do automatic register allocation for this class */
45         arch_register_class_flag_manual_ra = 1U << 0,
46         /**< the register models an abstract state (example: fpu rounding mode) */
47         arch_register_class_flag_state     = 1U << 1
48 } arch_register_class_flags_t;
49
50 typedef enum arch_register_type_t {
51         arch_register_type_none         = 0,
52         /** The register must be saved by the caller upon a function call. It thus
53          * can be overwritten in the called function. */
54         arch_register_type_caller_save  = 1U << 0,
55         /** The register must be saved by the caller upon a function call. It thus
56          * can be overwritten in the called function. */
57         arch_register_type_callee_save  = 1U << 1,
58         /** Do not consider this register when allocating. */
59         arch_register_type_ignore       = 1U << 2,
60         /** The emitter can choose an arbitrary register. The register fulfills any
61          * register constraints as long as the register class matches */
62         arch_register_type_joker        = 1U << 3,
63         /** This is just a virtual register. Virtual registers fulfill any register
64          * constraints as long as the register class matches. It is a allowed to
65          * have multiple definitions for the same virtual register at a point */
66         arch_register_type_virtual      = 1U << 4,
67         /** The register represents a state that should be handled by bestate
68          * code */
69         arch_register_type_state        = 1U << 5,
70 } arch_register_type_t;
71
72 /**
73  * Different types of register allocation requirements.
74  */
75 typedef enum arch_register_req_type_t {
76         /** No register requirement. */
77         arch_register_req_type_none              = 0,
78         /** All registers in the class are allowed. */
79         arch_register_req_type_normal            = 1U << 0,
80         /** Only a real subset of the class is allowed. */
81         arch_register_req_type_limited           = 1U << 1,
82         /** The register should be equal to another one at the node. */
83         arch_register_req_type_should_be_same    = 1U << 2,
84         /** The register must be unequal from some other at the node. */
85         arch_register_req_type_must_be_different = 1U << 3,
86         /** ignore while allocating registers */
87         arch_register_req_type_ignore            = 1U << 4,
88         /** the output produces a new value for the stack pointer
89          * (this is not really a constraint but a marker to guide the stackpointer
90          * rewiring logic) */
91         arch_register_req_type_produces_sp       = 1U << 5,
92 } arch_register_req_type_t;
93
94 extern const arch_register_req_t *arch_no_register_req;
95
96 /**
97  * Print information about a register requirement in human readable form
98  * @param F   output stream/file
99  * @param req The requirements structure to format.
100  */
101 void arch_dump_register_req(FILE *F, const arch_register_req_t *req,
102                             const ir_node *node);
103
104 void arch_dump_register_reqs(FILE *F, const ir_node *node);
105 void arch_dump_reqs_and_registers(FILE *F, const ir_node *node);
106
107 /**
108  * Node classification. Used for statistics.
109  */
110 typedef enum arch_irn_class_t {
111         arch_irn_class_spill  = 1 << 0,
112         arch_irn_class_reload = 1 << 1,
113         arch_irn_class_remat  = 1 << 2,
114         arch_irn_class_copy   = 1 << 3,
115         arch_irn_class_perm   = 1 << 4
116 } arch_irn_class_t;
117
118 void arch_set_frame_offset(ir_node *irn, int bias);
119
120 ir_entity *arch_get_frame_entity(const ir_node *irn);
121 void       arch_set_frame_entity(ir_node *irn, ir_entity *ent);
122 int        arch_get_sp_bias(ir_node *irn);
123
124 int             arch_get_op_estimated_cost(const ir_node *irn);
125 arch_inverse_t *arch_get_inverse(const ir_node *irn, int i,
126                                  arch_inverse_t *inverse,
127                                  struct obstack *obstack);
128 int             arch_possible_memory_operand(const ir_node *irn,
129                                              unsigned int i);
130 void            arch_perform_memory_operand(ir_node *irn, ir_node *spill,
131                                             unsigned int i);
132
133 /**
134  * Get the register requirements for a node.
135  * @note Deprecated API! Preferably use
136  *       arch_get_in_register_req and
137  *       arch_get_out_register_req.
138  *
139  * @param irn The node.
140  * @param pos The position of the operand you're interested in.
141  * @return    A pointer to the register requirements.  If NULL is returned, the
142  *            operand was no register operand.
143  */
144 const arch_register_req_t *arch_get_register_req(const ir_node *irn, int pos);
145
146 /**
147  * Put all registers which shall not be ignored by the register
148  * allocator in a bit set.
149  * @param cls The register class to consider.
150  * @param bs  The bit set to put the registers to.
151  */
152 extern void arch_put_non_ignore_regs(const arch_register_class_t *cls,
153                                      bitset_t *bs);
154
155 /**
156  * Check, if a register is assignable to an operand of a node.
157  * @param irn The node.
158  * @param pos The position of the operand.
159  * @param reg The register.
160  * @return    1, if the register might be allocated to the operand 0 if not.
161  */
162 int arch_reg_is_allocatable(const ir_node *irn, int pos,
163                             const arch_register_t *reg);
164
165 #define arch_reg_out_is_allocatable(irn, reg) arch_reg_is_allocatable(irn, -1, reg)
166
167 /**
168  * Get the register class of an operand of a node.
169  * @param irn The node.
170  * @param pos The position of the operand, -1 for the output.
171  * @return    The register class of the operand or NULL, if
172  *            operand is a non-register operand.
173  */
174 const arch_register_class_t *arch_get_irn_reg_class(const ir_node *irn,
175                                                     int pos);
176
177 #define arch_get_irn_reg_class_out(irn) arch_get_irn_reg_class(irn, -1)
178
179 /**
180  * Get the register allocated at a certain output operand of a node.
181  * @param irn The node.
182  * @return    The register allocated for this operand
183  */
184 const arch_register_t *arch_get_irn_register(const ir_node *irn);
185 const arch_register_t *arch_irn_get_register(const ir_node *irn, int pos);
186
187 /**
188  * Set the register for a certain output operand.
189  * @param irn The node.
190  * @param reg The register.
191  */
192 void arch_set_irn_register(ir_node *irn, const arch_register_t *reg);
193 void arch_irn_set_register(ir_node *irn, int pos, const arch_register_t *reg);
194
195 /**
196  * Classify a node.
197  * @param irn The node.
198  * @return A classification of the node.
199  */
200 arch_irn_class_t arch_irn_classify(const ir_node *irn);
201
202 #define arch_irn_class_is(irn, irn_class) ((arch_irn_classify(irn) & arch_irn_class_ ## irn_class) != 0)
203
204 /**
205  * Get the flags of a node.
206  * @param irn The node.
207  * @return The flags.
208  */
209 arch_irn_flags_t arch_irn_get_flags(const ir_node *irn);
210
211 void arch_irn_set_flags(ir_node *node, arch_irn_flags_t flags);
212 void arch_irn_add_flags(ir_node *node, arch_irn_flags_t flags);
213
214 #define arch_irn_is(irn, flag) ((arch_irn_get_flags(irn) & arch_irn_flags_ ## flag) != 0)
215
216 /**
217  * Get the operations of an irn.
218  * @param self The handler from which the method is invoked.
219  * @param irn Some node.
220  * @return Operations for that irn.
221  */
222 typedef const void *(arch_get_irn_ops_t)(const ir_node *irn);
223
224 /**
225  * Initialize the architecture environment struct.
226  * @param isa           The isa which shall be put into the environment.
227  * @param file_handle   The file handle
228  * @return The environment.
229  */
230 extern arch_env_t *arch_env_init(const arch_isa_if_t *isa,
231                                  FILE *file_handle, be_main_env_t *main_env);
232
233 /**
234  * Register an instruction set architecture
235  */
236 void be_register_isa_if(const char *name, const arch_isa_if_t *isa);
237
238 /**
239  * A register.
240  */
241 struct arch_register_t {
242         const char                  *name;       /**< The name of the register. */
243         const arch_register_class_t *reg_class;  /**< The class of the register */
244         unsigned                     index;      /**< The index of the register in
245                                                       the class. */
246         arch_register_type_t         type;       /**< The type of the register. */
247         /** register constraint allowing just this register */
248         const arch_register_req_t   *single_req;
249 };
250
251 static inline const arch_register_class_t *_arch_register_get_class(
252                 const arch_register_t *reg)
253 {
254         return reg->reg_class;
255 }
256
257 static inline unsigned _arch_register_get_index(const arch_register_t *reg)
258 {
259         return reg->index;
260 }
261
262 static inline const char *_arch_register_get_name(const arch_register_t *reg)
263 {
264         return reg->name;
265 }
266
267 #define arch_register_get_class(reg)        _arch_register_get_class(reg)
268 #define arch_register_get_index(reg)        _arch_register_get_index(reg)
269 #define arch_register_get_name(reg)         _arch_register_get_name(reg)
270
271 /**
272  * Convenience macro to check for register type.
273  * @param req   A pointer to register.
274  * @param kind  The kind of type to check for (see arch_register_type_t).
275  * @return      1, If register is of given kind, 0 if not.
276  */
277 #define arch_register_type_is(reg, kind) \
278   (((reg)->type & arch_register_type_ ## kind) != 0)
279
280 /**
281  * A class of registers.
282  * Like general purpose or floating point.
283  */
284 struct arch_register_class_t {
285         unsigned                     index;   /**< index of this register class */
286         const char                  *name;    /**< The name of the register class.*/
287         unsigned                     n_regs;  /**< Number of registers in this
288                                                    class. */
289         ir_mode                     *mode;    /**< The mode of the register class.*/
290         const arch_register_t       *regs;    /**< The array of registers. */
291         arch_register_class_flags_t  flags;   /**< register class flags. */
292         const arch_register_req_t   *class_req;
293 };
294
295 /** return the number of registers in this register class */
296 #define arch_register_class_n_regs(cls) ((cls)->n_regs)
297
298 /** return the largest mode of this register class */
299 #define arch_register_class_mode(cls) ((cls)->mode)
300
301 /** return the name of this register class */
302 #define arch_register_class_name(cls) ((cls)->name)
303
304 /** return the index of this register class */
305 #define arch_register_class_index(cls)  ((cls)->index)
306
307 /** return the register class flags */
308 #define arch_register_class_flags(cls) ((cls)->flags)
309
310 static inline const arch_register_t *_arch_register_for_index(
311                 const arch_register_class_t *cls, unsigned idx)
312 {
313         assert(idx < cls->n_regs);
314         return &cls->regs[idx];
315 }
316
317 #define arch_register_for_index(cls, idx)   _arch_register_for_index(cls, idx)
318
319 /**
320  * Convenience macro to check for set constraints.
321  * @param req   A pointer to register requirements.
322  * @param kind  The kind of constraint to check for
323  *              (see arch_register_req_type_t).
324  * @return      1, If the kind of constraint is present, 0 if not.
325  */
326 #define arch_register_req_is(req, kind) \
327         (((req)->type & (arch_register_req_type_ ## kind)) != 0)
328
329 /**
330  * Expresses requirements to register allocation for an operand.
331  */
332 struct arch_register_req_t {
333         arch_register_req_type_t     type; /**< The type of the constraint. */
334         const arch_register_class_t *cls;  /**< The register class this constraint
335                                                 belongs to. */
336         const unsigned *limited;            /**< allowed register bitset */
337         unsigned other_same;                /**< Bitmask of ins which should use the
338                                                  same register (should_be_same). */
339         unsigned other_different;           /**< Bitmask of ins which shall use a
340                                                  different register
341                                                  (must_be_different) */
342 };
343
344 static inline int reg_reqs_equal(const arch_register_req_t *req1,
345                                  const arch_register_req_t *req2)
346 {
347         if (req1 == req2)
348                 return 1;
349
350         if (req1->type != req2->type
351                         || req1->cls != req2->cls
352                         || req1->other_same != req2->other_same
353                         || req1->other_different != req2->other_different)
354                 return 0;
355
356         if (req1->limited != NULL) {
357                 size_t n_regs;
358
359                 if (req2->limited == NULL)
360                         return 0;
361
362                 n_regs = arch_register_class_n_regs(req1->cls);
363                 if (!rbitsets_equal(req1->limited, req2->limited, n_regs))
364                         return 0;
365         }
366
367         return 1;
368 }
369
370 /**
371  * An inverse operation returned by the backend
372  */
373 struct arch_inverse_t {
374         int      n;       /**< count of nodes returned in nodes array */
375         int      costs;   /**< costs of this remat */
376
377         /** nodes for this inverse operation. shall be in schedule order.
378          * last element is the target value */
379         ir_node  **nodes;
380 };
381
382 struct arch_irn_ops_t {
383
384         /**
385          * Get the register requirements for a given operand.
386          * @param irn The node.
387          * @param pos The operand's position
388          * @return    The register requirements for the selected operand.
389          *            The pointer returned is never NULL.
390          */
391         const arch_register_req_t *(*get_irn_reg_req_in)(const ir_node *irn,
392                                                          int pos);
393
394         /**
395          * Classify the node.
396          * @param irn The node.
397          * @return A classification.
398          */
399         arch_irn_class_t (*classify)(const ir_node *irn);
400
401         /**
402          * Get the entity on the stack frame this node depends on.
403          * @param irn  The node in question.
404          * @return The entity on the stack frame or NULL, if the node does not have
405          *         a stack frame entity.
406          */
407         ir_entity *(*get_frame_entity)(const ir_node *irn);
408
409         /**
410          * Set the entity on the stack frame this node depends on.
411          * @param irn  The node in question.
412          * @param ent  The entity to set
413          */
414         void (*set_frame_entity)(ir_node *irn, ir_entity *ent);
415
416         /**
417          * Set the offset of a node carrying an entity on the stack frame.
418          * @param irn  The node.
419          * @param offset The offset of the node's stack frame entity.
420          */
421         void (*set_frame_offset)(ir_node *irn, int offset);
422
423         /**
424          * Returns the delta of the stackpointer for nodes that increment or
425          * decrement the stackpointer with a constant value. (push, pop
426          * nodes on most architectures).
427          * A positive value stands for an expanding stack area, a negative value for
428          * a shrinking one.
429          *
430          * @param irn       The node
431          * @return          0 if the stackpointer is not modified with a constant
432          *                  value, otherwise the increment/decrement value
433          */
434         int (*get_sp_bias)(const ir_node *irn);
435
436         /**
437          * Returns an inverse operation which yields the i-th argument
438          * of the given node as result.
439          *
440          * @param irn       The original operation
441          * @param i         Index of the argument we want the inverse operation to
442          *                  yield
443          * @param inverse   struct to be filled with the resulting inverse op
444          * @param obstack   The obstack to use for allocation of the returned nodes
445          *                  array
446          * @return          The inverse operation or NULL if operation invertible
447          */
448         arch_inverse_t *(*get_inverse)(const ir_node *irn, int i,
449                                        arch_inverse_t *inverse,
450                                        struct obstack *obstack);
451
452         /**
453          * Get the estimated cycle count for @p irn.
454          *
455          * @param irn  The node.
456          * @return     The estimated cycle count for this operation
457          */
458         int (*get_op_estimated_cost)(const ir_node *irn);
459
460         /**
461          * Asks the backend whether operand @p i of @p irn can be loaded form memory
462          * internally
463          *
464          * @param irn  The node.
465          * @param i    Index of the argument we would like to know whether @p irn
466          *             can load it form memory internally
467          * @return     nonzero if argument can be loaded or zero otherwise
468          */
469         int (*possible_memory_operand)(const ir_node *irn, unsigned int i);
470
471         /**
472          * Ask the backend to assimilate @p reload of operand @p i into @p irn.
473          *
474          * @param irn    The node.
475          * @param spill  The spill.
476          * @param i      The position of the reload.
477          */
478         void (*perform_memory_operand)(ir_node *irn, ir_node *spill,
479                                        unsigned int i);
480 };
481
482 /**
483  * The code generator interface.
484  */
485 struct arch_code_generator_if_t {
486         /**
487          * Initialize the code generator.
488          * @param birg A backend IRG session.
489          * @return     A newly created code generator.
490          */
491         void *(*init)(be_irg_t *birg);
492
493         /**
494          * return node used as base in pic code addresses
495          */
496         ir_node* (*get_pic_base)(void *self);
497
498         /**
499          * Called before abi introduce.
500          */
501         void (*before_abi)(void *self);
502
503         /**
504          * Called, when the graph is being normalized.
505          */
506         void (*prepare_graph)(void *self);
507
508         /**
509          * Backend may provide an own spiller.
510          * This spiller needs to spill all register classes.
511          */
512         void (*spill)(void *self, be_irg_t *birg);
513
514         /**
515          * Called before register allocation.
516          */
517         void (*before_ra)(void *self);
518
519         /**
520          * Called after register allocation.
521          */
522         void (*after_ra)(void *self);
523
524         /**
525          * Called directly before done is called. This should be the last place
526          * where the irg is modified.
527          */
528         void (*finish)(void *self);
529
530         /**
531          * Called after everything happened. This call should emit the final
532          * assembly code but avoid changing the irg.
533          * The code generator must also be de-allocated here.
534          */
535         void (*done)(void *self);
536 };
537
538 /**
539  * helper macro: call function func from the code generator
540  * if it's implemented.
541  */
542 #define _arch_cg_call(cg, func) \
543 do { \
544         if((cg)->impl->func) \
545                 (cg)->impl->func(cg); \
546 } while(0)
547
548 #define _arch_cg_call_env(cg, env, func) \
549 do { \
550         if((cg)->impl->func) \
551                 (cg)->impl->func(cg, env); \
552 } while(0)
553
554 #define arch_code_generator_before_abi(cg)      _arch_cg_call(cg, before_abi)
555 #define arch_code_generator_prepare_graph(cg)   _arch_cg_call(cg, prepare_graph)
556 #define arch_code_generator_before_ra(cg)       _arch_cg_call(cg, before_ra)
557 #define arch_code_generator_after_ra(cg)        _arch_cg_call(cg, after_ra)
558 #define arch_code_generator_finish(cg)          _arch_cg_call(cg, finish)
559 #define arch_code_generator_done(cg)            _arch_cg_call(cg, done)
560 #define arch_code_generator_spill(cg, birg)     _arch_cg_call_env(cg, birg, spill)
561 #define arch_code_generator_has_spiller(cg)     ((cg)->impl->spill != NULL)
562 #define arch_code_generator_get_pic_base(cg)    \
563         ((cg)->impl->get_pic_base != NULL ? (cg)->impl->get_pic_base(cg) : NULL)
564
565 /**
566  * Code generator base class.
567  */
568 struct arch_code_generator_t {
569         const arch_code_generator_if_t *impl;
570 };
571
572 /**
573  * Architecture interface.
574  */
575 struct arch_isa_if_t {
576         /**
577          * Initialize the isa interface.
578          * @param file_handle  the file handle to write the output to
579          * @return a new isa instance
580          */
581         arch_env_t *(*init)(FILE *file_handle);
582
583         /**
584          * Free the isa instance.
585          */
586         void (*done)(void *self);
587
588         /**
589          * Called directly after initialization. Backend should handle all
590          * intrinsics here.
591          */
592         void (*handle_intrinsics)(void);
593
594         /**
595          * Get the the number of register classes in the isa.
596          * @return The number of register classes.
597          */
598         unsigned (*get_n_reg_class)(void);
599
600         /**
601          * Get the i-th register class.
602          * @param i The number of the register class.
603          * @return The register class.
604          */
605         const arch_register_class_t *(*get_reg_class)(unsigned i);
606
607         /**
608          * Get the register class which shall be used to store a value of a given
609          * mode.
610          * @param self The this pointer.
611          * @param mode The mode in question.
612          * @return A register class which can hold values of the given mode.
613          */
614         const arch_register_class_t *(*get_reg_class_for_mode)(const ir_mode *mode);
615
616         /**
617          * Get the ABI restrictions for procedure calls.
618          * @param self        The this pointer.
619          * @param call_type   The call type of the method (procedure) in question.
620          * @param p           The array of parameter locations to be filled.
621          */
622         void (*get_call_abi)(const void *self, ir_type *call_type,
623                              be_abi_call_t *abi);
624
625         /**
626          * Get the code generator interface.
627          * @param self The this pointer.
628          * @return     Some code generator interface.
629          */
630         const arch_code_generator_if_t *(*get_code_generator_if)(void *self);
631
632         /**
633          * Get the list scheduler to use. There is already a selector given, the
634          * backend is free to modify and/or ignore it.
635          *
636          * @param self     The isa object.
637          * @param selector The selector given by options.
638          * @return         The list scheduler selector.
639          */
640         const list_sched_selector_t *(*get_list_sched_selector)(const void *self,
641                         list_sched_selector_t *selector);
642
643         /**
644          * Get the ILP scheduler to use.
645          * @param self  The isa object.
646          * @return      The ILP scheduler selector
647          */
648         const ilp_sched_selector_t *(*get_ilp_sched_selector)(const void *self);
649
650         /**
651          * Get the necessary alignment for storing a register of given class.
652          * @param self  The isa object.
653          * @param cls   The register class.
654          * @return      The alignment in bytes.
655          */
656         int (*get_reg_class_alignment)(const arch_register_class_t *cls);
657
658         /**
659          * A "static" function, returns the frontend settings
660          * needed for this backend.
661          */
662         const backend_params *(*get_params)(void);
663
664         /**
665          * Returns an 2-dim array of execution units, @p irn can be executed on.
666          * The first dimension is the type, the second the allowed units of this
667          * type.
668          * Each dimension is a NULL terminated list.
669          * @param self  The isa object.
670          * @param irn   The node.
671          * @return An array of allowed execution units.
672          *         exec_unit = {
673          *                       { unit1_of_tp1, ..., unitX1_of_tp1, NULL },
674          *                       ...,
675          *                       { unit1_of_tpY, ..., unitXn_of_tpY, NULL },
676          *                       NULL
677          *                     };
678          */
679         const be_execution_unit_t ***(*get_allowed_execution_units)(
680                         const ir_node *irn);
681
682         /**
683          * Return the abstract machine for this isa.
684          * @param self  The isa object.
685          */
686         const be_machine_t *(*get_machine)(const void *self);
687
688         /**
689          * Return an ordered list of irgs where code should be generated for.
690          * If NULL is returned, all irg will be taken into account and they will be
691          * generated in an arbitrary order.
692          * @param self   The isa object.
693          * @param irgs   A flexible array ARR_F of length 0 where the backend can
694          *               append the desired irgs.
695          * @return A flexible array ARR_F containing all desired irgs in the
696          *         desired order.
697          */
698         ir_graph **(*get_backend_irg_list)(const void *self, ir_graph ***irgs);
699
700         /**
701          * mark node as rematerialized
702          */
703         void (*mark_remat)(ir_node *node);
704
705         /**
706          * parse an assembler constraint part and set flags according to its nature
707          * advances the *c pointer to point to the last parsed character (so if you
708          * parse a single character don't advance c)
709          */
710         asm_constraint_flags_t (*parse_asm_constraint)(const char **c);
711
712         /**
713          * returns true if the string is a valid clobbered (register) in this
714          * backend
715          */
716         int (*is_valid_clobber)(const char *clobber);
717 };
718
719 #define arch_env_done(env)                             ((env)->impl->done(env))
720 #define arch_env_handle_intrinsics(env)                \
721         do { if((env)->impl->handle_intrinsics != NULL) (env)->impl->handle_intrinsics(); } while(0)
722 #define arch_env_get_n_reg_class(env)                  ((env)->impl->get_n_reg_class())
723 #define arch_env_get_reg_class(env,i)                  ((env)->impl->get_reg_class(i))
724 #define arch_env_get_reg_class_for_mode(env,mode)      ((env)->impl->get_reg_class_for_mode((mode)))
725 #define arch_env_get_call_abi(env,tp,abi)              ((env)->impl->get_call_abi((env), (tp), (abi)))
726 #define arch_env_get_code_generator_if(env)            ((env)->impl->get_code_generator_if((env)))
727 #define arch_env_get_list_sched_selector(env,selector) ((env)->impl->get_list_sched_selector((env), (selector)))
728 #define arch_env_get_ilp_sched_selector(env)           ((env)->impl->get_ilp_sched_selector(env))
729 #define arch_env_get_reg_class_alignment(env,cls)      ((env)->impl->get_reg_class_alignment((cls)))
730 #define arch_env_get_params(env)                       ((env)->impl->get_params())
731 #define arch_env_get_allowed_execution_units(env,irn)  ((env)->impl->get_allowed_execution_units((irn)))
732 #define arch_env_get_machine(env)                      ((env)->impl->get_machine(env))
733 #define arch_env_get_backend_irg_list(env,irgs)        ((env)->impl->get_backend_irg_list((env), (irgs)))
734 #define arch_env_parse_asm_constraint(env,c)           ((env)->impl->parse_asm_constraint((c))
735 #define arch_env_is_valid_clobber(env,clobber)         ((env)->impl->is_valid_clobber((clobber))
736 #define arch_env_mark_remat(env,node) \
737         do { if ((env)->impl->mark_remat != NULL) (env)->impl->mark_remat((node)); } while(0)
738
739 /**
740  * ISA base class.
741  */
742 struct arch_env_t {
743         const arch_isa_if_t   *impl;
744         const arch_register_t *sp;               /**< The stack pointer register. */
745         const arch_register_t *bp;               /**< The base pointer register. */
746         const arch_register_class_t *link_class; /**< The static link pointer
747                                                       register class. */
748         int                    stack_dir;        /**< -1 for decreasing, 1 for
749                                                       increasing. */
750         int                    stack_alignment;  /**< power of 2 stack alignment */
751         const be_main_env_t   *main_env;         /**< the be main environment */
752         int                    spill_cost;       /**< cost for a be_Spill node */
753         int                    reload_cost;      /**< cost for a be_Reload node */
754 };
755
756 static inline unsigned arch_irn_get_n_outs(const ir_node *node)
757 {
758         backend_info_t *info = be_get_info(node);
759         if (info->out_infos == NULL)
760                 return 0;
761
762         return ARR_LEN(info->out_infos);
763 }
764
765 static inline const arch_irn_ops_t *get_irn_ops_simple(const ir_node *node)
766 {
767         const ir_op          *ops    = get_irn_op(node);
768         const arch_irn_ops_t *be_ops = get_op_ops(ops)->be_ops;
769         assert(!is_Proj(node));
770         return be_ops;
771 }
772
773 static inline const arch_register_req_t *arch_get_register_req_out(
774                 const ir_node *irn)
775 {
776         int             pos = 0;
777         backend_info_t *info;
778
779         if (is_Proj(irn)) {
780                 pos = get_Proj_proj(irn);
781                 irn = get_Proj_pred(irn);
782         } else if (get_irn_mode(irn) == mode_T) {
783                 /* TODO: find out who does this and fix the caller! */
784                 return arch_no_register_req;
785         }
786         info = be_get_info(irn);
787         if (info->out_infos == NULL)
788                 return arch_no_register_req;
789
790         return info->out_infos[pos].req;
791 }
792
793 static inline bool arch_irn_is_ignore(const ir_node *irn)
794 {
795         const arch_register_req_t *req = arch_get_register_req_out(irn);
796         return !!(req->type & arch_register_req_type_ignore);
797 }
798
799 static inline bool arch_irn_consider_in_reg_alloc(
800                 const arch_register_class_t *cls, const ir_node *node)
801 {
802         const arch_register_req_t *req = arch_get_register_req_out(node);
803         return
804                 req->cls == cls &&
805                 !(req->type & arch_register_req_type_ignore);
806 }
807
808 /**
809  * Get register constraints for an operand at position @p
810  */
811 static inline const arch_register_req_t *arch_get_in_register_req(
812                 const ir_node *node, int pos)
813 {
814         const arch_irn_ops_t *ops = get_irn_ops_simple(node);
815         return ops->get_irn_reg_req_in(node, pos);
816 }
817
818 /**
819  * Get register constraint for a produced result (the @p pos result)
820  */
821 static inline const arch_register_req_t *arch_get_out_register_req(
822                 const ir_node *node, int pos)
823 {
824         const backend_info_t *info = be_get_info(node);
825         if (info->out_infos == NULL)
826                 return arch_no_register_req;
827         return info->out_infos[pos].req;
828 }
829
830 static inline void arch_set_out_register_req(ir_node *node, int pos,
831                 const arch_register_req_t *req)
832 {
833         backend_info_t *info = be_get_info(node);
834         assert(pos < (int) arch_irn_get_n_outs(node));
835         info->out_infos[pos].req = req;
836 }
837
838 #endif