* pcode.c,
[fw/sdcc] / src / pic16 / ralloc.c
1 /*-------------------------------------------------------------------------
2
3   ralloc.h - header file register allocation
4
5         Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
6         PIC port   - T. Scott Dattalo scott@dattalo.com (2000)
7         PIC16 port   - Martin Dubuc m.dubuc@rogers.com (2002)
8
9    This program is free software; you can redistribute it and/or modify it
10    under the terms of the GNU General Public License as published by the
11    Free Software Foundation; either version 2, or (at your option) any
12    later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22    
23    In other words, you are welcome to use, share and improve this program.
24    You are forbidden to forbid anyone else to use, share and improve
25    what you give them.   Help stamp out software-hoarding!  
26 -------------------------------------------------------------------------*/
27 #include "SDCCicode.h"
28 #include "SDCCBBlock.h"
29 #ifndef SDCCRALLOC_H
30 #define SDCCRALLOC_H 1
31
32 #include "pcoderegs.h"
33
34 /* set STACK_SUPPORT to 1 to compile code for stack */
35 #define STACK_SUPPORT 1
36 extern unsigned int stackPos;
37
38 enum
39   {
40     R2_IDX = 0, R3_IDX, R4_IDX,
41     R5_IDX, R6_IDX, R7_IDX,
42     R0_IDX, R1_IDX, X8_IDX,
43     X9_IDX, X10_IDX, X11_IDX,
44     X12_IDX, CND_IDX
45   };
46
47 enum {
48  REG_PTR=1,
49  REG_GPR,
50  REG_CND,
51  REG_SFR,
52  REG_STK,
53  REG_TMP
54 };
55 //#define REG_PTR 0x01
56 //#define REG_GPR 0x02
57 //#define REG_CND 0x04
58 //#define REG_SFR 0x08
59 //#define REG_STK 0x10  /* Use a register as a psuedo stack */
60 //#define REG_TMP 0x20  
61
62 /* definition for the registers */
63 typedef struct regs
64   {
65     short type;                 /* can have value 
66                                  * REG_GPR, REG_PTR or REG_CND 
67                                  * This like the "meta-type" */
68     short pc_type;              /* pcode type */
69     short rIdx;                 /* index into register table */
70     //    short otype;        
71     char *name;                 /* name */
72
73     unsigned isFree:1;          /* is currently unassigned  */
74     unsigned wasUsed:1;         /* becomes true if register has been used */
75     unsigned isFixed:1;         /* True if address can't change */
76 //    unsigned isMapped:1;        /* The Register's address has been mapped to physical RAM */
77     unsigned isBitField:1;      /* True if reg is type bit OR is holder for several bits */
78     unsigned isEmitted:1;       /* True if the reg has been written to a .asm file */
79     unsigned accessBank:1;      /* True if the reg is explicit placed in access bank */
80     unsigned isLocal:1;         /* True if the reg is allocated in function's local frame */
81     unsigned address;           /* reg's address if isFixed | isMapped is true */
82     unsigned size;              /* 0 for byte, 1 for int, 4 for long */
83     unsigned alias;             /* Alias mask if register appears in multiple banks */
84     struct regs *reg_alias;     /* If more than one register share the same address 
85                                  * then they'll point to each other. (primarily for bits)*/
86     operand *regop;             /* reference to the operand used to create the register */
87     pCodeRegLives reglives; /* live range mapping */
88   }
89 regs;
90 extern regs regspic16[];
91 extern int pic16_nRegs;
92 extern int pic16_Gstack_base_addr;
93
94 /*
95   As registers are created, they're added to a set (based on the
96   register type). Here are the sets of registers that are supported
97   in the PIC port:
98 */
99 extern set *pic16_dynAllocRegs;
100 extern set *pic16_dynStackRegs;
101 extern set *pic16_dynProcessorRegs;
102 extern set *pic16_dynDirectRegs;
103 extern set *pic16_dynDirectBitRegs;
104 extern set *pic16_dynInternalRegs;
105
106 extern set *pic16_builtin_functions;
107
108 extern set *pic16_rel_udata;
109 extern set *pic16_fix_udata;
110 extern set *pic16_equ_data;
111
112 regs *pic16_regWithIdx (int);
113 regs *pic16_dirregWithName (char *name );
114 void  pic16_freeAllRegs ();
115 void  pic16_deallocateAllRegs ();
116 regs *pic16_findFreeReg(short type);
117 regs *pic16_allocWithIdx (int idx);
118
119 regs *pic16_allocDirReg (operand *op );
120 regs *pic16_allocRegByName (char *name, int size );
121
122 /* Define register address that are constant across PIC16 family */
123 #define IDX_TMR0    0xfd6
124 #define IDX_PCL     0xff9
125 #define IDX_STATUS  0xfd8
126 #define IDX_PORTA   0xf80
127 #define IDX_PORTB   0xf81
128 #define IDX_PCLATH  0xffa
129 #define IDX_INTCON  0xff2
130 #define IDX_WREG    0xfe8
131
132 #define IDX_FSR0    0xfe9
133 #define IDX_FSR0L   0xfe9
134 #define IDX_FSR0H   0xfea
135 #define IDX_FSR1L       0xfe1
136 #define IDX_FSR1H       0xfe2
137 #define IDX_FSR2L       0xfd9
138 #define IDX_FSR2H       0xfda
139
140 #define IDX_INDF0               0xfef
141 #define IDX_POSTINC0    0xfee
142 #define IDX_POSTDEC0    0xfed
143 #define IDX_PREINC0             0xfec
144 #define IDX_PLUSW0              0xfeb
145
146 #define IDX_INDF1               0xfe7
147 #define IDX_POSTINC1    0xfe6
148 #define IDX_POSTDEC1    0xfe5
149 #define IDX_PREINC1             0xfe4
150 #define IDX_PLUSW1              0xfe3
151
152 #define IDX_INDF2               0xfdf
153 #define IDX_POSTINC2    0xfde
154 #define IDX_POSTDEC2    0xfdd
155 #define IDX_PREINC2             0xfdc
156 #define IDX_PLUSW2              0xfdb
157
158 #define IDX_KZ      0x7fff   /* Known zero - actually just a general purpose reg. */
159 #define IDX_WSAVE   0x7ffe
160 #define IDX_SSAVE   0x7ffd
161
162 #endif