* support/Utils/dbuf_string.[ch]: added dbuf_chomp(),
[fw/sdcc] / 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 struct pCodeOp;
31
32 enum
33   {
34     AOP_LIT = 1,
35     AOP_REG,
36     AOP_DIR,
37     AOP_STK,
38     AOP_STR,
39     AOP_CRY,
40     AOP_ACC,
41     AOP_PCODE,
42     AOP_STA             // asmop on stack
43   };
44
45 /* type asmop : a homogenised type for 
46    all the different spaces an operand can be
47    in */
48 typedef struct asmop
49   {
50
51     short type;                 /* can have values
52                                    AOP_LIT    -  operand is a literal value
53                                    AOP_REG    -  is in registers
54                                    AOP_DIR    -  direct just a name
55                                    AOP_STK    -  should be pushed on stack this
56                                    can happen only for the result
57                                    AOP_CRY    -  carry contains the value of this
58                                    AOP_STR    -  array of strings
59                                    AOP_ACC    -  result is in the acc:b pair
60                                  */
61     short coff;                 /* current offset */
62     short size;                 /* total size */
63     unsigned code:1;            /* is in Code space */
64     unsigned paged:1;           /* in paged memory  */
65     unsigned freed:1;           /* already freed    */
66     union
67       {
68         value *aop_lit;         /* if literal */
69         regs *aop_reg[4];       /* array of registers */
70         char *aop_dir;          /* if direct  */
71         regs *aop_ptr;          /* either -> to r0 or r1 */
72         int aop_stk;            /* stack offset when AOP_STK */
73         char *aop_str[4];       /* just a string array containing the location */
74 /*      regs *aop_alloc_reg;     * points to a dynamically allocated register */
75         pCodeOp *pcop;
76         struct {
77           int stk;
78           pCodeOp *pop[4];
79         } stk;
80       }
81     aopu;
82   }
83 asmop;
84
85 void genpic16Code (iCode *);
86
87 extern unsigned pic16_fReturnSizePic;
88
89
90 #define AOP(op) op->aop
91 #define AOP_TYPE(op) AOP(op)->type
92 #define AOP_SIZE(op) AOP(op)->size
93
94 #define AOP_NEEDSACC(x) (AOP(x) && (AOP_TYPE(x) == AOP_CRY ||  \
95                          AOP(x)->paged)) 
96
97 #define RESULTONSTACK(x) \
98                          (IC_RESULT(x) && IC_RESULT(x)->aop && \
99                          IC_RESULT(x)->aop->type == AOP_STK )
100 #define RESULTONSTA(x)  (IC_RESULT(x) && IC_RESULT(x)->aop && IC_RESULT(x)->aop->type == AOP_STA)
101
102
103 #define MOVA(x) if (strcmp(x,"a") && strcmp(x,"acc")) pic16_emitcode(";XXX mov","a,%s  %s,%d",x,__FILE__,__LINE__);
104 #define CLRC    pic16_emitcode(";XXX clr","c %s,%d",__FILE__,__LINE__);
105
106
107 #define BIT_NUMBER(x) (x & 7)
108 #define BIT_REGISTER(x) (x>>3)
109
110
111 #define LSB     0
112 #define MSB16   1
113 #define MSB24   2
114 #define MSB32   3
115
116
117 #define FUNCTION_LABEL_INC  40
118
119 /*-----------------------------------------------------------------*/
120 /* Macros for emitting skip instructions                           */
121 /*-----------------------------------------------------------------*/
122
123 #define emitSKPC    pic16_emitpcode(POC_BTFSS,pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_C_BIT))
124 #define emitSKPNC   pic16_emitpcode(POC_BTFSC,pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_C_BIT))
125 #define emitSKPZ    pic16_emitpcode(POC_BTFSS,pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_Z_BIT))
126 #define emitSKPNZ   pic16_emitpcode(POC_BTFSC,pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_Z_BIT))
127 #define emitSKPDC   pic16_emitpcode(POC_BTFSS,pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_DC_BIT))
128 #define emitSKPNDC  pic16_emitpcode(POC_BTFSC,pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_DC_BIT))
129 #define emitCLRZ    pic16_emitpcode(POC_BCF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_Z_BIT))
130 #define emitCLRC    pic16_emitpcode(POC_BCF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_C_BIT))
131 #define emitCLRDC   pic16_emitpcode(POC_BCF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_DC_BIT))
132 #define emitSETZ    pic16_emitpcode(POC_BSF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_Z_BIT))
133 #define emitSETC    pic16_emitpcode(POC_BSF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_C_BIT))
134 #define emitSETDC   pic16_emitpcode(POC_BSF,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_DC_BIT))
135
136 #define emitTOGC    pic16_emitpcode(POC_BTG,  pic16_popCopyGPR2Bit(PCOP(&pic16_pc_status),PIC_C_BIT))
137
138 int pic16_getDataSize(operand *op);
139 void pic16_emitpcode_real(PIC_OPCODE poc, pCodeOp *pcop);
140 #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)
141 void pic16_emitpLabel(int key);
142 void pic16_emitcode (char *inst,char *fmt, ...);
143 void DEBUGpic16_emitcode (char *inst,char *fmt, ...);
144 void pic16_emitDebuggerSymbol (char *);
145 bool pic16_sameRegs (asmop *aop1, asmop *aop2 );
146 char *pic16_aopGet (asmop *aop, int offset, bool bit16, bool dname);
147 void DEBUGpic16_pic16_AopType(int line_no, operand *left, operand *right, operand *result);
148 void DEBUGpic16_pic16_AopTypeSign(int line_no, operand *left, operand *right, operand *result);
149
150
151 bool pic16_genPlusIncr (iCode *ic);
152 void pic16_outBitAcc(operand *result);
153 void pic16_genPlusBits (iCode *ic);
154 void pic16_genPlus (iCode *ic);
155 bool pic16_genMinusDec (iCode *ic);
156 void pic16_addSign(operand *result, int offset, int sign);
157 void pic16_genMinusBits (iCode *ic);
158 void pic16_genMinus (iCode *ic);
159 void pic16_genLeftShiftLiteral (operand *left, operand *right, operand *result, iCode *ic);
160
161 pCodeOp *pic16_popGet2p(pCodeOp *src, pCodeOp *dst);
162 void pic16_emitpcomment (char *fmt, ...);
163
164 pCodeOp *pic16_popGetLabel(int key);
165 pCodeOp *pic16_popCopyReg(pCodeOpReg *pc);
166 pCodeOp *pic16_popCopyGPR2Bit(pCodeOp *pc, int bitval);
167 pCodeOp *pic16_popGetLit(int lit);
168 pCodeOp *pic16_popGetLit2(int lit, pCodeOp *arg2);
169 pCodeOp *popGetWithString(char *str);
170 pCodeOp *pic16_popGet (asmop *aop, int offset);//, bool bit16, bool dname);
171 pCodeOp *pic16_popGetTempReg(int lock);
172 pCodeOp *pic16_popGetTempRegCond(bitVect *, bitVect *, int lock);
173 void pic16_popReleaseTempReg(pCodeOp *pcop, int lock);
174
175 pCodeOp *pic16_popCombine2(pCodeOpReg *src, pCodeOpReg *dst, int noalloc);
176
177 void pic16_aopPut (asmop *aop, char *s, int offset);
178 void pic16_outAcc(operand *result);
179 void pic16_aopOp (operand *op, iCode *ic, bool result);
180 void pic16_outBitC(operand *result);
181 void pic16_toBoolean(operand *oper);
182 void pic16_freeAsmop (operand *op, asmop *aaop, iCode *ic, bool pop);
183 const char *pic16_pCodeOpType(  pCodeOp *pcop);
184 int pic16_my_powof2 (unsigned long num);
185
186 void pic16_mov2w (asmop *aop, int offset);
187 void pic16_mov2f(asmop *dst, asmop *src, int offset);
188
189 bool pic16_isLitOp(operand *op);
190 bool pic16_isLitAop(asmop *aop);
191
192 void dumpiCode(iCode *lic);
193
194 int inWparamList(char *s);
195
196 #include "device.h"
197
198 #define DUMP_FUNCTION_ENTRY     1
199 #define DUMP_FUNCTION_EXIT      0
200
201 #if DUMP_FUNCTION_ENTRY
202 #define FENTRY  if(pic16_options.debgen&2)pic16_emitpcomment("**{\t%d %s", __LINE__, __FUNCTION__)
203 #define FENTRY2 if(pic16_options.debgen&2)pic16_emitpcomment("**{\t%d %s", __LINE__, __FUNCTION__)
204 #else
205 #define FENTRY
206 #define FENTRY2
207 #endif
208
209 #if DUMP_FUNCTION_EXIT
210 #define FEXIT   if(pic16_options.debgen&2)pic16_emitpcomment("; **}", "%d %s", __LINE__, __FUNCTION__)
211 #define FEXIT2  if(pic16_options.debgen&2)pic16_emitpcomment("**{\t%d %s", __LINE__, __FUNCTION__)
212 #else
213 #define FEXIT
214 #define FEXIT2
215 #endif
216
217 #define ERROR   werror(W_POSSBUG2, __FILE__, __LINE__)
218 #endif