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