1 {
2     Copyright (c) 1998-2002 by Florian Klaempfl
3 
4     Generate PowerPC assembler for type converting nodes
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 
20  ****************************************************************************
21 }
22 unit ngppccnv;
23 
24 {$i fpcdefs.inc}
25 
26 interface
27 
28     uses
29       node,ncnv,ncgcnv;
30 
31     type
32        tgenppctypeconvnode = class(tcgtypeconvnode)
33          protected
34          { procedure second_int_to_int;override; }
35          { procedure second_string_to_string;override; }
36          { procedure second_cstring_to_pchar;override; }
37          { procedure second_string_to_chararray;override; }
38          { procedure second_array_to_pointer;override; }
39          { function first_int_to_real: tnode; override; }
40          { procedure second_pointer_to_array;override; }
41          { procedure second_chararray_to_string;override; }
42          { procedure second_char_to_string;override; }
43          { procedure second_int_to_real;override; }
44          { procedure second_real_to_real;override; }
45          { procedure second_cord_to_pointer;override; }
46          { procedure second_proc_to_procvar;override; }
47          { procedure second_bool_to_int;override; }
48           procedure second_int_to_bool;override;
49          { procedure second_load_smallset;override;  }
50          { procedure second_ansistring_to_pchar;override; }
51          { procedure second_pchar_to_string;override; }
52          { procedure second_class_to_intf;override; }
53          { procedure second_char_to_char;override; }
54        end;
55 
56 
57 implementation
58 
59    uses
60       verbose,globtype,globals,systems,
61       symconst,symdef,aasmbase,aasmtai,aasmdata,
62       defutil,cutils,
63       cgbase,cgutils,pass_1,pass_2,
64       ncgutil,procinfo,
65       cpubase,aasmcpu,
66       rgobj,tgobj,cgobj,hlcgobj;
67 
68 
69     procedure tgenppctypeconvnode.second_int_to_bool;
70       var
71         hreg1,
72         hreg2    : tregister;
73 {$ifndef cpu64bitalu}
74         href     : treference;
75         oldloc   : tlocation;
76 {$endif not cpu64bitalu}
77         resflags : tresflags;
78         opsize   : tcgsize;
79         hlabel   : tasmlabel;
80         newsize  : tcgsize;
81       begin
82          secondpass(left);
83          if codegenerror then
84           exit;
85 
86          { Explicit typecasts from any ordinal type to a boolean type }
87          { must not change the ordinal value                          }
88          if (nf_explicit in flags) and
89             not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
90            begin
91               location_copy(location,left.location);
92               newsize:=def_cgsize(resultdef);
93               { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
94               if (tcgsize2size[newsize]>tcgsize2size[left.location.size]) or
95                  ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
96                 hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
97               else
98                 begin
99                   location.size:=newsize;
100                   if (target_info.endian = ENDIAN_BIG) and
101                      (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
102                     begin
103                       inc(location.reference.offset,TCGSize2Size[left.location.size]-TCGSize2Size[location.size]);
104                       location.reference.alignment:=newalignment(location.reference.alignment,TCGSize2Size[left.location.size]-TCGSize2Size[location.size]);
105                     end;
106                 end;
107               exit;
108            end;
109 
110          location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
111          opsize := def_cgsize(left.resultdef);
112 {$ifndef cpu64bitalu}
113          if (opsize in [OS_64,OS_S64]) then
114            opsize:=OS_32;
115 {$endif not cpu64bitalu}
116 
117         if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
118           hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
119 
120          case left.location.loc of
121             LOC_CREFERENCE,LOC_REFERENCE,LOC_REGISTER,LOC_CREGISTER :
122               begin
123                 if left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
124                   begin
125                     hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
126 {$ifndef cpu64bitalu}
127                     if left.location.size in [OS_64,OS_S64] then
128                       begin
129                         cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,left.location.reference,hreg1);
130                         hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
131                         href:=left.location.reference;
132                         inc(href.offset,4);
133                         cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,href,hreg2);
134                         cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hreg1,hreg2,hreg1);
135                       end
136                     else
137 {$endif not cpu64bitalu}
138                       cg.a_load_ref_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.reference,hreg1);
139                   end
140                 else
141                   begin
142                     hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
143 {$ifndef cpu64bitalu}
144                      if left.location.size in [OS_64,OS_S64] then
145                        begin
146                           hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
147                           cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,left.location.register64.reglo,hreg1);
148                        end
149                      else
150 {$endif not cpu64bitalu}
151                        cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,hreg1);
152                   end;
153                 hreg2 := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
154 
155                 if not(is_cbool(resultdef)) then
156                   begin
157                     { hreg2:=hreg1-1; carry:=hreg1=0 }
158                     current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBIC,hreg2,hreg1,1));
159                     { hreg1:=hreg1-hreg2+carry (= hreg1-(hreg1-1)-carry) }
160                     current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBFE,hreg1,hreg2,hreg1));
161                   end
162                 else
163                   begin
164                     { carry:=hreg1<>0 }
165                     current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC,hreg2,hreg1,0));
166                     { hreg1:=hreg1-hreg1-carry }
167                     current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBFE,hreg1,hreg1,hreg1));
168                   end;
169               end;
170             LOC_FLAGS :
171               begin
172                 hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
173                 resflags:=left.location.resflags;
174                 cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
175                 if (is_cbool(resultdef)) then
176                   cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,hreg1,hreg1);
177               end;
178             LOC_JUMP :
179               begin
180                 hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
181                 current_asmdata.getjumplabel(hlabel);
182                 cg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
183                 if not(is_cbool(resultdef)) then
184                   cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hreg1)
185                 else
186                   cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,-1,hreg1);
187                 cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
188                 cg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
189                 cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hreg1);
190                 cg.a_label(current_asmdata.CurrAsmList,hlabel);
191               end;
192             else
193               internalerror(10062);
194          end;
195 {$ifndef cpu64bitalu}
196          if (location.size in [OS_64,OS_S64]) then
197            begin
198              location.register64.reglo:=hreg1;
199              location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
200              if (is_cbool(resultdef)) then
201                { reglo is either 0 or -1 -> reghi has to become the same }
202                cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
203              else
204                { unsigned }
205                cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
206            end
207          else
208 {$endif cpu64bitalu}
209            location.register:=hreg1;
210       end;
211 
212 end.
213