* src/pic/gen.h: Q&D solution for the bug, found by Jim Paris
[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 extern int debug_verbose;
30
31 #define FENTRY do {                                                                     \
32         /*fprintf (stderr, "%s:%u:%s: *{*\n", __FILE__, __LINE__, __FUNCTION__);*/      \
33         if (options.debug || debug_verbose) {                                           \
34                 emitpComment ("; %s:%u:%s *{*", __FILE__, __LINE__, __FUNCTION__);      \
35         }                                                                               \
36 } while (0)
37 #define FEXIT do {                                                                      \
38         /*fprintf (stderr, "%s:%u:%s: *}*\n", __FILE__, __LINE__, __FUNCTION__);*/      \
39         if (options.debug || debug.verbose) {                                           \
40                 emitpComment ("; %s:%u:%s *}*", __FILE__, __LINE__, __FUNCTION__);      \
41         }                                                                               \
42 } while (0)
43
44 struct pCodeOp;
45
46 enum
47 {
48   AOP_LIT = 1,
49   AOP_REG,
50   AOP_DIR,
51   AOP_STK,
52   AOP_IMMD,
53   AOP_STR,
54   AOP_CRY,
55   AOP_ACC,
56   AOP_PCODE = 13
57
58 };
59
60 /* type asmop : a homogenised type for
61    all the different spaces an operand can be
62    in */
63 typedef struct asmop
64 {
65
66   short type;  /* can have values
67                   AOP_LIT    -  operand is a literal value
68                   AOP_REG    -  is in registers
69                   AOP_DIR    -  direct just a name
70                   AOP_STK    -  should be pushed on stack this
71                   can happen only for the result
72                   AOP_IMMD   -  immediate value for eg. remateriazable
73                   AOP_CRY    -  carry contains the value of this
74                   AOP_STR    -  array of strings
75                */
76   short coff;           /* current offset */
77   short size;           /* total size */
78   unsigned code:1;      /* is in Code space */
79   unsigned paged:1;     /* in paged memory  */
80   unsigned freed:1;     /* already freed    */
81   union
82   {
83     value *aop_lit;     /* if literal */
84     regs *aop_reg[4];   /* array of registers */
85     char *aop_dir;      /* if direct  */
86     regs *aop_ptr;      /* either -> to r0 or r1 */
87     char *aop_immd;     /* if immediate others are implied */
88     int aop_stk;        /* stack offset when AOP_STK */
89     char *aop_str[4];   /* just a string array containing the location */
90     pCodeOp *pcop;
91   }
92   aopu;
93 }
94 asmop;
95
96 void genpic14Code (iCode *);
97
98 extern unsigned fReturnSizePic;
99
100
101 #define AOP(op) op->aop
102 #define AOP_TYPE(op) AOP(op)->type
103 #define AOP_SIZE(op) AOP(op)->size
104
105 #define AOP_NEEDSACC(x) (AOP(x) && (AOP_TYPE(x) == AOP_CRY || AOP(x)->paged))
106
107 #define RESULTONSTACK(x) \
108                          (IC_RESULT(x) && IC_RESULT(x)->aop && \
109                          IC_RESULT(x)->aop->type == AOP_STK )
110
111 #define MOVA(x) if (strcmp(x,"a") && strcmp(x,"acc")) pic14_emitcode(";XXX mov","a,%s  %s,%d",x,__FILE__,__LINE__);
112 #define CLRC    pic14_emitcode(";XXX clr","c %s,%d",__FILE__,__LINE__);
113
114 #define BIT_NUMBER(x) (x & 7)
115 #define BIT_REGISTER(x) (x>>3)
116
117
118 #define LSB     0
119 #define MSB16   1
120 #define MSB24   2
121 #define MSB32   3
122
123
124 #define FUNCTION_LABEL_INC  40
125
126 /*-----------------------------------------------------------------*/
127 /* Macros for emitting skip instructions                           */
128 /*-----------------------------------------------------------------*/
129
130 #define emitSKPC    emitpcode(POC_BTFSS,popCopyGPR2Bit(PCOP(&pc_status),PIC_C_BIT))
131 #define emitSKPNC   emitpcode(POC_BTFSC,popCopyGPR2Bit(PCOP(&pc_status),PIC_C_BIT))
132 #define emitSKPZ    emitpcode(POC_BTFSS,popCopyGPR2Bit(PCOP(&pc_status),PIC_Z_BIT))
133 #define emitSKPNZ   emitpcode(POC_BTFSC,popCopyGPR2Bit(PCOP(&pc_status),PIC_Z_BIT))
134 #define emitSKPDC   emitpcode(POC_BTFSS,popCopyGPR2Bit(PCOP(&pc_status),PIC_DC_BIT))
135 #define emitSKPNDC  emitpcode(POC_BTFSC,popCopyGPR2Bit(PCOP(&pc_status),PIC_DC_BIT))
136 #define emitCLRZ    emitpcode(POC_BCF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_Z_BIT))
137 #define emitCLRC    emitpcode(POC_BCF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_C_BIT))
138 #define emitCLRDC   emitpcode(POC_BCF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_DC_BIT))
139 #define emitCLRIRP  emitpcode(POC_BCF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_IRP_BIT))
140 #define emitSETZ    emitpcode(POC_BSF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_Z_BIT))
141 #define emitSETC    emitpcode(POC_BSF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_C_BIT))
142 #define emitSETDC   emitpcode(POC_BSF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_DC_BIT))
143 #define emitSETIRP  emitpcode(POC_BSF,  popCopyGPR2Bit(PCOP(&pc_status),PIC_IRP_BIT))
144
145 int pic14_getDataSize(operand *op);
146 void emitpcode_real(PIC_OPCODE poc, pCodeOp *pcop);
147 #define emitpcode(poc,pcop)     do { if (options.debug || debug_verbose) { emitpComment (" >>> %s:%d:%s", __FILE__, __LINE__, __FUNCTION__); } emitpcode_real(poc,pcop); } while(0)
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 genPlus (iCode *ic);
161 void addSign(operand *result, int offset, int sign);
162 void genMinus (iCode *ic);
163
164
165 pCodeOp *popGetLabel(unsigned int key);
166 pCodeOp *popCopyReg(pCodeOpReg *pc);
167 pCodeOp *popCopyGPR2Bit(pCodeOp *pc, int bitval);
168 pCodeOp *popGetLit(unsigned int lit);
169 pCodeOp *popGetWithString(char *str, int isExtern);
170 pCodeOp *popRegFromString(char *str, int size, int offset);
171 pCodeOp *popGet (asmop *aop, int offset);//, bool bit16, bool dname);
172 pCodeOp *popGetAddr (asmop *aop, int offset, int index);
173 pCodeOp *popGetTempReg(void);
174 void popReleaseTempReg(pCodeOp *pcop);
175
176
177 void aopPut (asmop *aop, char *s, int offset);
178 void pic14_outAcc(operand *result);
179 void aopOp (operand *op, iCode *ic, bool result);
180 void pic14_outBitC(operand *result);
181 void pic14_toBoolean(operand *oper);
182 void freeAsmop (operand *op, asmop *aaop, iCode *ic, bool pop);
183 void mov2w (asmop *aop, int offset);
184 const char *pCodeOpType(  pCodeOp *pcop);
185
186 int aop_isLitLike (asmop *aop);
187 int op_isLitLike (operand *op);
188
189 #endif