2004-01-13 Vangelis Rokas <vrokas@otenet.gr>
[fw/sdcc] / src / pic16 / genutils.c
1 /*-------------------------------------------------------------------------
2  genutils.c - source file for code generation for pic16
3         code generation utility functions
4
5         Created by Vangelis Rokas (vrokas@otenet.gr) [Nov-2003]
6
7         Based on :
8
9   Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
10          and -  Jean-Louis VERN.jlvern@writeme.com (1999)
11   Bug Fixes  -  Wojciech Stryjewski  wstryj1@tiger.lsu.edu (1999 v2.1.9a)
12   PIC port   -  Scott Dattalo scott@dattalo.com (2000)
13   PIC16 port -  Martin Dubuc m.dubuc@rogers.com (2002)
14   
15   This program is free software; you can redistribute it and/or modify it
16   under the terms of the GNU General Public License as published by the
17   Free Software Foundation; either version 2, or (at your option) any
18   later version.
19   
20   This program is distributed in the hope that it will be useful,
21   but WITHOUT ANY WARRANTY; without even the implied warranty of
22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   GNU General Public License for more details.
24   
25   You should have received a copy of the GNU General Public License
26   along with this program; if not, write to the Free Software
27   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28   
29   In other words, you are welcome to use, share and improve this program.
30   You are forbidden to forbid anyone else to use, share and improve
31   what you give them.   Help stamp out software-hoarding!
32   
33   Notes:
34   000123 mlh    Moved aopLiteral to SDCCglue.c to help the split
35                 Made everything static
36 -------------------------------------------------------------------------*/
37
38 /**********************************************************
39  * Here is a list with completed genXXXXX functions
40  *
41  * genNot
42  *
43  */
44
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <ctype.h>
50 #include "SDCCglobl.h"
51 #include "newalloc.h"
52
53 #include "common.h"
54 #include "SDCCpeeph.h"
55 #include "ralloc.h"
56 #include "pcode.h"
57 #include "gen.h"
58
59 #include "genutils.h"
60
61 #if 1
62 #define pic16_emitcode  DEBUGpic16_emitcode
63 #endif
64
65 #if defined(GEN_Not)
66 /*-----------------------------------------------------------------*/
67 /* pic16_genNot - generate code for ! operation                    */
68 /*-----------------------------------------------------------------*/
69 void pic16_genNot (iCode *ic)
70 {
71   int size;
72
73 /*
74  * result[AOP_CRY,AOP_REG]  = ! left[AOP_CRY, AOP_REG]
75  */
76
77         DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
78         /* assign asmOps to operand & result */
79         pic16_aopOp (IC_LEFT(ic),ic,FALSE);
80         pic16_aopOp (IC_RESULT(ic),ic,TRUE);
81         DEBUGpic16_pic16_AopType(__LINE__,IC_LEFT(ic),NULL,IC_RESULT(ic));
82
83         /* if in bit space then a special case */
84         if (AOP_TYPE(IC_LEFT(ic)) == AOP_CRY) {
85                 if (AOP_TYPE(IC_RESULT(ic)) == AOP_CRY) {
86                         pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(IC_LEFT(ic)),0));
87                         pic16_emitpcode(POC_XORWF,pic16_popGet(AOP(IC_RESULT(ic)),0));
88                 } else {
89                         pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(IC_RESULT(ic)),0));
90                         pic16_emitpcode(POC_BTFSS,pic16_popGet(AOP(IC_LEFT(ic)),0));
91                         pic16_emitpcode(POC_INCF,pic16_popGet(AOP(IC_RESULT(ic)),0));
92                 }
93                 goto release;
94         }
95
96         size = AOP_SIZE(IC_LEFT(ic));
97         if(size == 1) {
98                 pic16_emitpcode(POC_COMFW,pic16_popGet(AOP(IC_LEFT(ic)),0));
99                 pic16_emitpcode(POC_ANDLW,pic16_popGetLit(1));
100                 pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(IC_RESULT(ic)),0));
101                 goto release;
102         }
103
104 release:    
105         /* release the aops */
106         pic16_freeAsmop(IC_LEFT(ic),NULL,ic,(RESULTONSTACK(ic) ? 0 : 1));
107         pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
108 }
109
110 #endif  /* defined(GEN_Not) */
111
112
113
114 #if defined(GEN_Cpl)
115 /*-----------------------------------------------------------------*/
116 /* pic16_genCpl - generate code for complement                     */
117 /*-----------------------------------------------------------------*/
118 void pic16_genCpl (iCode *ic)
119 {
120   int offset = 0;
121   int size ;
122
123 /*
124  * result[CRY,REG] = ~left[CRY,REG]
125  */
126  
127
128         DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
129         /* assign asmOps to operand & result */
130         pic16_aopOp (IC_LEFT(ic),ic,FALSE);
131         pic16_aopOp (IC_RESULT(ic),ic,TRUE);
132         DEBUGpic16_pic16_AopType(__LINE__,IC_LEFT(ic),NULL,IC_RESULT(ic));
133
134         /* if both are in bit space then 
135            a special case */
136         if (AOP_TYPE(IC_RESULT(ic)) == AOP_CRY &&
137                 AOP_TYPE(IC_LEFT(ic)) == AOP_CRY ) { 
138
139                 /* FIXME */
140                 pic16_emitcode("mov","c,%s",IC_LEFT(ic)->aop->aopu.aop_dir); 
141                 pic16_emitcode("cpl","c"); 
142                 pic16_emitcode("mov","%s,c",IC_RESULT(ic)->aop->aopu.aop_dir); 
143                 goto release; 
144         } 
145
146         size = AOP_SIZE(IC_RESULT(ic));
147         while (size--) {
148
149                 if (pic16_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) ) {
150                         pic16_emitpcode(POC_COMF,  pic16_popGet(AOP(IC_LEFT(ic)), offset));
151                 } else {
152                         pic16_emitpcode(POC_COMFW, pic16_popGet(AOP(IC_LEFT(ic)),offset));
153                         pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(IC_RESULT(ic)),offset));
154                 }
155                 offset++;
156         }
157
158 release:
159     /* release the aops */
160     pic16_freeAsmop(IC_LEFT(ic),NULL,ic,(RESULTONSTACK(ic) ? 0 : 1));
161     pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
162 }
163 #endif  /* defined(GEN_Cpl) */
164