1 /***************************************************************************
2  *   Copyright (C) 2006 by Matteo Franchin                                 *
3  *   fnch@libero.it                                                        *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 
21 /**
22  * @file registers.h
23  * @brief The register allocation system.
24  *
25  * This file provides the functions needed to associate registers
26  * to expressions, guaranting that occupied registers will never be used
27  * for other new expressions, unless they have been first released
28  * with the appropriate function.
29  * The system keeps track of registers indipendently for different
30  * pieces of code. For this purpose the concept of frame is used.
31  * Registers in different frames are completely independent. This means
32  * that there is a different occupation list for each different frame.
33  * Frames can be created with the function Reg_Frame_Push and destroyed
34  * with Reg_Frame_Pop. Registers with different types are also independent.
35  * This means that there is a different occupation list for each different
36  * type of register. There are NUM_TYPES kinds of registers.
37  * NOTE: In all the functions defined here, when the given type exceed
38  * the maximum value, the default type TYPE_OBJ is considered.
39  */
40 
41 #ifndef _BOX_REGISTERS_H
42 #  define _BOX_REGISTERS_H
43 
44 #  include <box/types.h>
45 #  include <box/defaults.h>
46 #  include <box/array.h>
47 #  include <box/occupation.h>
48 
49 /** Register number (an alias for integer) */
50 typedef BoxInt BoxVMRegNum;
51 
52 typedef struct {
53   BoxInt    chain; /**< Chain of released registers */
54   BoxInt    max;   /**< Total number of registers to allocate for variables */
55   BoxArr regs;  /**< Registers corresponding to variables */
56 } VarFrame;
57 
58 /** @brief The state of allocation of the registers in a single function.
59  */
60 typedef struct {
61   BoxOcc reg_occ[NUM_TYPES]; /**< Registers occupation */
62   VarFrame lvar[NUM_TYPES];  /**< Local variables */
63 } RegFrame;
64 
65 /** @brief This structure keeps the full state of the register allocator.
66  */
67 typedef struct {
68   BoxArr reg_frame;         /**< Array of register frames */
69   VarFrame gvar[NUM_TYPES]; /**< Global variables */
70 } RegAlloc;
71 
72 void Reg_Init(RegAlloc *ra);
73 void Reg_Finish(RegAlloc *ra);
74 void Reg_Frame_Push(RegAlloc *ra);
75 void Reg_Frame_Pop(RegAlloc *ra);
76 BoxInt Reg_Frame_Get(RegAlloc *ra);
77 BoxInt Reg_Occupy(RegAlloc *ra, BoxTypeId t);
78 void Reg_Release(RegAlloc *ra, BoxInt t, BoxUInt regnum);
79 BoxInt Reg_Num(RegAlloc *ra, BoxInt t);
80 BoxInt Var_Occupy(RegAlloc *ra, BoxTypeId type, BoxInt level);
81 void Var_Release(RegAlloc *ra, BoxInt type, BoxUInt varnum);
82 BoxInt Var_Num(RegAlloc *ra, BoxInt type);
83 BoxInt GVar_Occupy(RegAlloc *ra, BoxTypeId type);
84 void GVar_Release(RegAlloc *ra, BoxInt type, BoxUInt varnum);
85 BoxInt GReg_Num(RegAlloc *ra, BoxInt type);
86 BoxInt GVar_Num(RegAlloc *ra, BoxInt type);
87 void Reg_Get_Local_Nums(RegAlloc *ra, BoxInt *num_regs, BoxInt *num_vars);
88 void Reg_Get_Global_Nums(RegAlloc *ra, BoxInt *num_regs, BoxInt *num_vars);
89 
90 #endif /* _BOX_REGISTERS_H */
91