Imported Upstream version 2.9.0
[debian/cc1111] / src / pic16 / gen.h
1 /*-------------------------------------------------------------------------
2   gen.h - header file for code generation for PIC16
3
4              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
5              PIC port   - T. Scott Dattalo scott@dattalo.com (2000)
6              PIC16 port   - Martin Dubuc m.dubuc@rogers.com (2000)
7
8    This program is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by the
10    Free Software Foundation; either version 2, or (at your option) any
11    later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21    
22    In other words, you are welcome to use, share and improve this program.
23    You are forbidden to forbid anyone else to use, share and improve
24    what you give them.   Help stamp out software-hoarding!  
25 -------------------------------------------------------------------------*/
26
27 #ifndef SDCCGENPIC16_H
28 #define SDCCGENPIC16_H
29
30 /* If you change these, you also have to update the library files
31  * device/lib/pic16/libsdcc/gptr{get,put}{1,2,3,4}.c */
32 #define GPTR_TAG_DATA   0x80
33 #define GPTR_TAG_EEPROM 0x40
34 #define GPTR_TAG_CODE   0x00    /* must be 0 becaue of UPPER(sym)==0 */
35
36 struct pCodeOp;
37
38 enum
39   {
40     AOP_LIT = 1,
41     AOP_REG,
42     AOP_DIR,
43     AOP_STK,
44     AOP_STR,
45     AOP_CRY,
46     AOP_ACC,
47     AOP_PCODE,
48     AOP_STA             // asmop on stack
49   };
50
51 /* type asmop : a homogenised type for 
52    all the different spaces an operand can be
53    in */
54 typedef struct asmop
55   {
56
57     short type;                 /* can have values
58                                    AOP_LIT    -  operand is a literal value
59                                    AOP_REG    -  is in registers
60                                    AOP_DIR    -  direct just a name
61                                    AOP_STK    -  should be pushed on stack this
62                                    can happen only for the result
63                                    AOP_CRY    -  carry contains the value of this
64                                    AOP_STR    -  array of strings
65                                    AOP_ACC    -  result is in the acc:b pair
66                                  */
67     short coff;                 /* current offset */
68     short size;                 /* total size */
69     unsigned code:1;            /* is in Code space */
70     unsigned paged:1;           /* in paged memory  */
71     unsigned freed:1;           /* already freed    */
72     union
73       {
74         value *aop_lit;         /* if literal */
75         regs *aop_reg[4];       /* array of registers */
76         char *aop_dir;          /* if direct  */
77         regs *aop_ptr;          /* either -> to r0 or r1 */
78         int aop_stk;            /* stack offset when AOP_STK */
79         char *aop_str[4];       /* just a string array containing the location */
80 /*      regs *aop_alloc_reg;     * points to a dynamically allocated register */
81         pCodeOp *pcop;
82         struct {
83           int stk;
84           pCodeOp *pop[4];
85         } stk;
86       }
87     aopu;
88   }
89 asmop;
90
91 void genpic16Code (iCode *);
92
93 extern unsigned pic16_fReturnSizePic;
94
95
96 #define AOP(op) op->aop
97 #define AOP_TYPE(op) AOP(op)->type
98 #define AOP_SIZE(op) AOP(op)->size
99
100 #define AOP_NEEDSACC(x) (AOP(x) && (AOP_TYPE(x) == AOP_CRY ||  \
101                          AOP(x)->paged)) 
102
103 #define RESULTONSTACK(x) \
104                          (IC_RESULT(x) && IC_RESULT(x)->aop && \
105                          IC_RESULT(x)->aop->type == AOP_STK )
106 #define RESULTONSTA(x)  (IC_RESULT(x) && IC_RESULT(x)->aop && IC_RESULT(x)->aop->type == AOP_STA)
107
108
109 #define MOVA(x) if (strcmp(x,"a") && strcmp(x,"acc")) pic16_emitcode(";XXX mov","a,%s  %s,%d",x,__FILE__,__LINE__);
110 #define CLRC    pic16_emitcode(";XXX clr","c %s,%d",__FILE__,__LINE__);
111
112
113 #define BIT_NUMBER(x) (x & 7)
114 #define BIT_REGISTER(x) (x>>3)
115
116
117 #define LSB     0
118 #define MSB16   1
119 #define MSB24   2
120 #define MSB32   3
121
122
123 #define FUNCTION_LABEL_INC  40
124
125 /*-----------------------------------------------------------------*/
126 /* Macros for emitting skip instructions                           */
127 /*-----------------------------------------------------------------*/
128
129 #define emitSKPC    pic16_emitpcode(POC_BTFSS,pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_C_BIT))
130 #define emitSKPNC   pic16_emitpcode(POC_BTFSC,pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_C_BIT))
131 #define emitSKPZ    pic16_emitpcode(POC_BTFSS,pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_Z_BIT))
132 #define emitSKPNZ   pic16_emitpcode(POC_BTFSC,pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_Z_BIT))
133 #define emitSKPDC   pic16_emitpcode(POC_BTFSS,pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_DC_BIT))
134 #define emitSKPNDC  pic16_emitpcode(POC_BTFSC,pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_DC_BIT))
135 #define emitCLRZ    pic16_emitpcode(POC_BCF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_Z_BIT))
136 #define emitCLRC    pic16_emitpcode(POC_BCF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_C_BIT))
137 #define emitCLRDC   pic16_emitpcode(POC_BCF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_DC_BIT))
138 #define emitSETZ    pic16_emitpcode(POC_BSF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_Z_BIT))
139 #define emitSETC    pic16_emitpcode(POC_BSF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_C_BIT))
140 #define emitSETDC   pic16_emitpcode(POC_BSF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_DC_BIT))
141
142 #define emitTOGC    pic16_emitpcode(POC_BTG,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_C_BIT))
143
144 int pic16_getDataSize(operand *op);
145 void pic16_emitpcode_real(PIC_OPCODE poc, pCodeOp *pcop);
146 #define pic16_emitpcode(poc,pcop)       do { if (pic16_pcode_verbose) pic16_emitpcomment ("%s:%u(%s):", __FILE__, __LINE__, __FUNCTION__); pic16_emitpcode_real(poc,pcop); } while(0)
147 void pic16_emitpLabel(int key);
148 void pic16_emitcode (char *inst,char *fmt, ...);
149 void DEBUGpic16_emitcode (char *inst,char *fmt, ...);
150 void pic16_emitDebuggerSymbol (char *);
151 bool pic16_sameRegs (asmop *aop1, asmop *aop2 );
152 char *pic16_aopGet (asmop *aop, int offset, bool bit16, bool dname);
153 void DEBUGpic16_pic16_AopType(int line_no, operand *left, operand *right, operand *result);
154 void DEBUGpic16_pic16_AopTypeSign(int line_no, operand *left, operand *right, operand *result);
155
156
157 bool pic16_genPlusIncr (iCode *ic);
158 void pic16_outBitAcc(operand *result);
159 void pic16_genPlusBits (iCode *ic);
160 void pic16_genPlus (iCode *ic);
161 bool pic16_genMinusDec (iCode *ic);
162 void pic16_addSign(operand *result, int offset, int sign);
163 void pic16_genMinusBits (iCode *ic);
164 void pic16_genMinus (iCode *ic);
165 void pic16_genLeftShiftLiteral (operand *left, operand *right, operand *result, iCode *ic);
166
167 pCodeOp *pic16_popGet2p(pCodeOp *src, pCodeOp *dst);
168 void pic16_emitpcomment (char *fmt, ...);
169
170 pCodeOp *pic16_popGetLabel(int key);
171 pCodeOp *pic16_popCopyReg(pCodeOpReg *pc);
172 pCodeOp *pic16_popCopyGPR2Bit(pCodeOp *pc, int bitval);
173 pCodeOp *pic16_popGetLit(int lit);
174 pCodeOp *pic16_popGetLit2(int lit, pCodeOp *arg2);
175 pCodeOp *popGetWithString(char *str);
176 pCodeOp *pic16_popGet (asmop *aop, int offset);//, bool bit16, bool dname);
177 pCodeOp *pic16_popGetTempReg(int lock);
178 pCodeOp *pic16_popGetTempRegCond(bitVect *, bitVect *, int lock);
179 void pic16_popReleaseTempReg(pCodeOp *pcop, int lock);
180
181 pCodeOp *pic16_popCombine2(pCodeOpReg *src, pCodeOpReg *dst, int noalloc);
182
183 void pic16_aopPut (asmop *aop, char *s, int offset);
184 void pic16_outAcc(operand *result);
185 void pic16_aopOp (operand *op, iCode *ic, bool result);
186 void pic16_outBitC(operand *result);
187 void pic16_toBoolean(operand *oper);
188 void pic16_freeAsmop (operand *op, asmop *aaop, iCode *ic, bool pop);
189 const char *pic16_pCodeOpType(  pCodeOp *pcop);
190 int pic16_my_powof2 (unsigned long num);
191
192 void pic16_mov2w (asmop *aop, int offset);
193 void pic16_mov2f(asmop *dst, asmop *src, int offset);
194
195 bool pic16_isLitOp(operand *op);
196 bool pic16_isLitAop(asmop *aop);
197
198 void dumpiCode(iCode *lic);
199
200 int inWparamList(char *s);
201
202 #include "device.h"
203
204 #define DUMP_FUNCTION_ENTRY     1
205 #define DUMP_FUNCTION_EXIT      0
206
207 #if DUMP_FUNCTION_ENTRY
208 #define FENTRY  if(pic16_options.debgen&2)pic16_emitpcomment("**{\t%d %s", __LINE__, __FUNCTION__)
209 #define FENTRY2 if(pic16_options.debgen&2)pic16_emitpcomment("**{\t%d %s", __LINE__, __FUNCTION__)
210 #else
211 #define FENTRY
212 #define FENTRY2
213 #endif
214
215 #if DUMP_FUNCTION_EXIT
216 #define FEXIT   if(pic16_options.debgen&2)pic16_emitpcomment("; **}", "%d %s", __LINE__, __FUNCTION__)
217 #define FEXIT2  if(pic16_options.debgen&2)pic16_emitpcomment("**{\t%d %s", __LINE__, __FUNCTION__)
218 #else
219 #define FEXIT
220 #define FEXIT2
221 #endif
222
223 #define ERROR   werror(W_POSSBUG2, __FILE__, __LINE__)
224 #endif