94603bda2fa1c87fd797cbda1d9522a81cc90c76
[fw/sdcc] / src / pic / gen.h
1 /*-------------------------------------------------------------------------
2   SDCCgen51.h - header file for code generation for 8051
3
4              Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
5              PIC port   - T. Scott Dattalo scott@dattalo.com (2000)
6
7    This program is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by the
9    Free Software Foundation; either version 2, or (at your option) any
10    later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20    
21    In other words, you are welcome to use, share and improve this program.
22    You are forbidden to forbid anyone else to use, share and improve
23    what you give them.   Help stamp out software-hoarding!  
24 -------------------------------------------------------------------------*/
25
26 #ifndef SDCCGENPIC14_H
27 #define SDCCGENPIC14_H
28
29 #define FENTRY do { \
30         /*fprintf (stderr, "%s:%u:%s: *{*\n", __FILE__, __LINE__, __FUNCTION__);*/      \
31         emitpComment ("; %s:%u:%s *{*\n", __FILE__, __LINE__, __FUNCTION__);    \
32 } while (0)
33 #define FEXIT do { \
34         /*fprintf (stderr, "%s:%u:%s: *}*\n", __FILE__, __LINE__, __FUNCTION__);*/      \
35         emitpComment ("; %s:%u:%s *}*\n", __FILE__, __LINE__, __FUNCTION__);    \
36 } while (0)
37
38 struct pCodeOp;
39
40 enum
41 {
42   AOP_LIT = 1,
43   AOP_REG, AOP_DIR,
44   AOP_DPTR, AOP_DPTR2, AOP_R0, AOP_R1,
45   AOP_STK, AOP_IMMD, AOP_STR,
46   AOP_CRY, AOP_ACC,
47   AOP_PCODE
48
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_DPTR   -  dptr contains address of operand
62                   AOP_DPTR2  -  dptr2 contains address of operand (DS80C390 only).
63                   AOP_R0/R1  -  r0/r1 contains address of operand               
64                   AOP_STK    -  should be pushed on stack this
65                   can happen only for the result
66                   AOP_IMMD   -  immediate value for eg. remateriazable 
67                   AOP_CRY    -  carry contains the value of this
68                   AOP_STR    -  array of strings
69                   AOP_ACC    -  result is in the acc:b pair
70                */
71   short coff;           /* current offset */
72   short size;           /* total size */
73   unsigned code:1;      /* is in Code space */
74   unsigned paged:1;     /* in paged memory  */
75   unsigned freed:1;     /* already freed    */
76   union
77   {
78     value *aop_lit;     /* if literal */
79     regs *aop_reg[4];   /* array of registers */
80     char *aop_dir;      /* if direct  */
81     regs *aop_ptr;      /* either -> to r0 or r1 */
82     char *aop_immd;     /* if immediate others are implied */
83     int aop_stk;        /* stack offset when AOP_STK */
84     char *aop_str[4];   /* just a string array containing the location */
85     pCodeOp *pcop;
86   }
87   aopu;
88 }
89 asmop;
90
91 void genpic14Code (iCode *);
92
93 extern unsigned 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 #define IS_AOP_PREG(x) (AOP(x) && (AOP_TYPE(x) == AOP_R1 || \
100                        AOP_TYPE(x) == AOP_R0))
101
102 #define AOP_NEEDSACC(x) (AOP(x) && (AOP_TYPE(x) == AOP_CRY ||  \
103                         AOP_TYPE(x) == AOP_DPTR || AOP_TYPE(x) == AOP_DPTR2 || \
104                          AOP(x)->paged)) 
105
106 #define AOP_INPREG(x) (x && (x->type == AOP_REG &&                        \
107                       (x->aopu.aop_reg[0] == pic14_regWithIdx(R0_IDX) || \
108                       x->aopu.aop_reg[0] == pic14_regWithIdx(R1_IDX) )))
109
110 #define RESULTONSTACK(x) \
111                          (IC_RESULT(x) && IC_RESULT(x)->aop && \
112                          IC_RESULT(x)->aop->type == AOP_STK )
113
114 #define MOVA(x) if (strcmp(x,"a") && strcmp(x,"acc")) pic14_emitcode(";XXX mov","a,%s  %s,%d",x,__FILE__,__LINE__);
115 #define CLRC    pic14_emitcode(";XXX clr","c %s,%d",__FILE__,__LINE__);
116
117 #define BIT_NUMBER(x) (x & 7)
118 #define BIT_REGISTER(x) (x>>3)
119
120
121 #define LSB     0
122 #define MSB16   1
123 #define MSB24   2
124 #define MSB32   3
125
126
127 #define FUNCTION_LABEL_INC  40
128
129 /*-----------------------------------------------------------------*/
130 /* Macros for emitting skip instructions                           */
131 /*-----------------------------------------------------------------*/
132
133 #define emitSKPC    emitpcode(POC_BTFSS,popCopyGPR2Bit(PCOP(&pc_status),PIC_C_BIT))
134 #define emitSKPNC   emitpcode(POC_BTFSC,popCopyGPR2Bit(PCOP(&pc_status),PIC_C_BIT))
135 #define emitSKPZ    emitpcode(POC_BTFSS,popCopyGPR2Bit(PCOP(&pc_status),PIC_Z_BIT))
136 #define emitSKPNZ   emitpcode(POC_BTFSC,popCopyGPR2Bit(PCOP(&pc_status),PIC_Z_BIT))
137 #define emitSKPDC   emitpcode(POC_BTFSS,popCopyGPR2Bit(PCOP(&pc_status),PIC_DC_BIT))
138 #define emitSKPNDC  emitpcode(POC_BTFSC,popCopyGPR2Bit(PCOP(&pc_status),PIC_DC_BIT))
139 #define emitCLRZ    emitpcode(POC_BCF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_Z_BIT))
140 #define emitCLRC    emitpcode(POC_BCF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_C_BIT))
141 #define emitCLRDC   emitpcode(POC_BCF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_DC_BIT))
142 #define emitSETZ    emitpcode(POC_BSF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_Z_BIT))
143 #define emitSETC    emitpcode(POC_BSF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_C_BIT))
144 #define emitSETDC   emitpcode(POC_BSF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_DC_BIT))
145
146 int pic14_getDataSize(operand *op);
147 void emitpcode(PIC_OPCODE poc, pCodeOp *pcop);
148 void emitpComment (const char *fmt, ...);
149 void emitpLabel(int key);
150 void pic14_emitcode (char *inst,char *fmt, ...);
151 void DEBUGpic14_emitcode (char *inst,char *fmt, ...);
152 void pic14_emitDebuggerSymbol (char *);
153 asmop *newAsmop (short type);
154 bool pic14_sameRegs (asmop *aop1, asmop *aop2 );
155 char *aopGet (asmop *aop, int offset, bool bit16, bool dname);
156
157
158 bool genPlusIncr (iCode *ic);
159 void pic14_outBitAcc(operand *result);
160 void genPlusBits (iCode *ic);
161 void genPlus (iCode *ic);
162 bool genMinusDec (iCode *ic);
163 void addSign(operand *result, int offset, int sign);
164 void genMinusBits (iCode *ic);
165 void genMinus (iCode *ic);
166
167
168 pCodeOp *popGetLabel(unsigned int key);
169 pCodeOp *popCopyReg(pCodeOpReg *pc);
170 pCodeOp *popCopyGPR2Bit(pCodeOp *pc, int bitval);
171 pCodeOp *popGetLit(unsigned int lit);
172 pCodeOp *popGetWithString(char *str, int isExtern);
173 pCodeOp *popRegFromString(char *str, int size, int offset);
174 pCodeOp *popGet (asmop *aop, int offset);//, bool bit16, bool dname);
175 pCodeOp *popGetAddr (asmop *aop, int offset);
176 pCodeOp *popGetTempReg(void);
177 void popReleaseTempReg(pCodeOp *pcop);
178
179
180 void aopPut (asmop *aop, char *s, int offset);
181 void pic14_outAcc(operand *result);
182 void aopOp (operand *op, iCode *ic, bool result);
183 void pic14_outBitC(operand *result);
184 void pic14_toBoolean(operand *oper);
185 void freeAsmop (operand *op, asmop *aaop, iCode *ic, bool pop);
186 void mov2w (asmop *aop, int offset);
187 const char *pCodeOpType(  pCodeOp *pcop);
188
189
190
191 #endif