2004-01-06 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
62 #if defined(GEN_Not)
63 /*-----------------------------------------------------------------*/
64 /* pic16_genNot - generate code for ! operation                    */
65 /*-----------------------------------------------------------------*/
66 void pic16_genNot (iCode *ic)
67 {
68   int size;
69
70 /*
71  * result[AOP_CRY,AOP_REG]  = ! left[AOP_CRY, AOP_REG]
72  */
73
74         DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
75         /* assign asmOps to operand & result */
76         pic16_aopOp (IC_LEFT(ic),ic,FALSE);
77         pic16_aopOp (IC_RESULT(ic),ic,TRUE);
78         DEBUGpic16_pic16_AopType(__LINE__,IC_LEFT(ic),NULL,IC_RESULT(ic));
79
80         /* if in bit space then a special case */
81         if (AOP_TYPE(IC_LEFT(ic)) == AOP_CRY) {
82                 if (AOP_TYPE(IC_RESULT(ic)) == AOP_CRY) {
83                         pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(IC_LEFT(ic)),0));
84                         pic16_emitpcode(POC_XORWF,pic16_popGet(AOP(IC_RESULT(ic)),0));
85                 } else {
86                         pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(IC_RESULT(ic)),0));
87                         pic16_emitpcode(POC_BTFSS,pic16_popGet(AOP(IC_LEFT(ic)),0));
88                         pic16_emitpcode(POC_INCF,pic16_popGet(AOP(IC_RESULT(ic)),0));
89                 }
90                 goto release;
91         }
92
93         size = AOP_SIZE(IC_LEFT(ic));
94         if(size == 1) {
95                 pic16_emitpcode(POC_COMFW,pic16_popGet(AOP(IC_LEFT(ic)),0));
96                 pic16_emitpcode(POC_ANDLW,pic16_popGetLit(1));
97                 pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(IC_RESULT(ic)),0));
98                 goto release;
99         }
100
101 release:    
102         /* release the aops */
103         pic16_freeAsmop(IC_LEFT(ic),NULL,ic,(RESULTONSTACK(ic) ? 0 : 1));
104         pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
105 }
106
107 #endif  /* defined(GEN_Not) */
108
109
110
111 #if defined(GEN_Cpl)
112 /*-----------------------------------------------------------------*/
113 /* pic16_genCpl - generate code for complement                     */
114 /*-----------------------------------------------------------------*/
115 void pic16_genCpl (iCode *ic)
116 {
117   int offset = 0;
118   int size ;
119
120 /*
121  * result[CRY,REG] = ~left[CRY,REG]
122  */
123  
124
125         DEBUGpic16_emitcode ("; ***","%s  %d",__FUNCTION__,__LINE__);
126         /* assign asmOps to operand & result */
127         pic16_aopOp (IC_LEFT(ic),ic,FALSE);
128         pic16_aopOp (IC_RESULT(ic),ic,TRUE);
129         DEBUGpic16_pic16_AopType(__LINE__,IC_LEFT(ic),NULL,IC_RESULT(ic));
130
131         /* if both are in bit space then 
132            a special case */
133         if (AOP_TYPE(IC_RESULT(ic)) == AOP_CRY &&
134                 AOP_TYPE(IC_LEFT(ic)) == AOP_CRY ) { 
135
136                 /* FIXME */
137                 pic16_emitcode("mov","c,%s",IC_LEFT(ic)->aop->aopu.aop_dir); 
138                 pic16_emitcode("cpl","c"); 
139                 pic16_emitcode("mov","%s,c",IC_RESULT(ic)->aop->aopu.aop_dir); 
140                 goto release; 
141         } 
142
143         size = AOP_SIZE(IC_RESULT(ic));
144         while (size--) {
145
146                 if (pic16_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) ) {
147                         pic16_emitpcode(POC_COMF,  pic16_popGet(AOP(IC_LEFT(ic)), offset));
148                 } else {
149                         pic16_emitpcode(POC_COMFW, pic16_popGet(AOP(IC_LEFT(ic)),offset));
150                         pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(IC_RESULT(ic)),offset));
151                 }
152                 offset++;
153         }
154
155 release:
156     /* release the aops */
157     pic16_freeAsmop(IC_LEFT(ic),NULL,ic,(RESULTONSTACK(ic) ? 0 : 1));
158     pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
159 }
160 #endif  /* defined(GEN_Cpl) */
161