nppccnv.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate PowerPC assembler for type converting nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nppccnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncnv,ncgcnv,ngppccnv;
  22. type
  23. tppctypeconvnode = class(tgenppctypeconvnode)
  24. protected
  25. { procedure second_int_to_int;override; }
  26. { procedure second_string_to_string;override; }
  27. { procedure second_cstring_to_pchar;override; }
  28. { procedure second_string_to_chararray;override; }
  29. { procedure second_array_to_pointer;override; }
  30. function first_int_to_real: tnode; override;
  31. { procedure second_pointer_to_array;override; }
  32. { procedure second_chararray_to_string;override; }
  33. { procedure second_char_to_string;override; }
  34. procedure second_int_to_real;override;
  35. { procedure second_real_to_real;override; }
  36. { procedure second_cord_to_pointer;override; }
  37. { procedure second_proc_to_procvar;override; }
  38. { procedure second_bool_to_int;override; }
  39. { procedure second_int_to_bool;override; }
  40. { procedure second_set_to_set;override; }
  41. { procedure second_ansistring_to_pchar;override; }
  42. { procedure second_pchar_to_string;override; }
  43. { procedure second_class_to_intf;override; }
  44. { procedure second_char_to_char;override; }
  45. end;
  46. implementation
  47. uses
  48. verbose,globtype,globals,systems,
  49. symconst,symdef,aasmbase,aasmtai,aasmdata,
  50. defutil,symcpu,
  51. cgbase,cgutils,pass_1,pass_2,
  52. ncon,ncal,
  53. ncgutil,procinfo,
  54. cpubase,aasmcpu,
  55. rgobj,tgobj,cgobj,hlcgobj;
  56. {*****************************************************************************
  57. FirstTypeConv
  58. *****************************************************************************}
  59. function tppctypeconvnode.first_int_to_real: tnode;
  60. var
  61. fname: string[19];
  62. begin
  63. { converting a 64bit integer to a float requires a helper }
  64. if is_64bitint(left.resultdef) or
  65. is_currency(left.resultdef) then
  66. begin
  67. { hack to avoid double division by 10000, as it's }
  68. { already done by typecheckpass.resultdef_int_to_real }
  69. if is_currency(left.resultdef) then
  70. left.resultdef := s64inttype;
  71. if is_signed(left.resultdef) then
  72. fname := 'fpc_int64_to_double'
  73. else
  74. fname := 'fpc_qword_to_double';
  75. result := ccallnode.createintern(fname,ccallparanode.create(
  76. left,nil));
  77. left:=nil;
  78. firstpass(result);
  79. exit;
  80. end
  81. else
  82. { other integers are supposed to be 32 bit }
  83. begin
  84. if is_signed(left.resultdef) then
  85. inserttypeconv(left,s32inttype)
  86. else
  87. inserttypeconv(left,u32inttype);
  88. firstpass(left);
  89. end;
  90. result := nil;
  91. expectloc:=LOC_FPUREGISTER;
  92. end;
  93. {*****************************************************************************
  94. SecondTypeConv
  95. *****************************************************************************}
  96. procedure tppctypeconvnode.second_int_to_real;
  97. type
  98. tdummyarray = packed array[0..7] of byte;
  99. const
  100. dummy1: int64 = $4330000080000000;
  101. dummy2: int64 = $4330000000000000;
  102. var
  103. tempconst: tnode;
  104. ref: treference;
  105. valuereg, tempreg, leftreg, tmpfpureg: tregister;
  106. size: tcgsize;
  107. signed : boolean;
  108. begin
  109. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  110. { the code here comes from the PowerPC Compiler Writer's Guide }
  111. { * longint to double }
  112. { addis R0,R0,0x4330 # R0 = 0x43300000 }
  113. { stw R0,disp(R1) # store upper half }
  114. { xoris R3,R3,0x8000 # flip sign bit }
  115. { stw R3,disp+4(R1) # store lower half }
  116. { lfd FR1,disp(R1) # float load double of value }
  117. { fsub FR1,FR1,FR2 # subtract 0x4330000080000000 }
  118. { * cardinal to double }
  119. { addis R0,R0,0x4330 # R0 = 0x43300000 }
  120. { stw R0,disp(R1) # store upper half }
  121. { stw R3,disp+4(R1) # store lower half }
  122. { lfd FR1,disp(R1) # float load double of value }
  123. { fsub FR1,FR1,FR2 # subtract 0x4330000000000000 }
  124. tg.Gettemp(current_asmdata.CurrAsmList,8,8,tt_normal,ref);
  125. signed := is_signed(left.resultdef);
  126. { we need a certain constant for the conversion, so create it here }
  127. if signed then
  128. tempconst :=
  129. crealconstnode.create(double(tdummyarray(dummy1)),
  130. pbestrealtype^)
  131. else
  132. tempconst :=
  133. crealconstnode.create(double(tdummyarray(dummy2)),
  134. pbestrealtype^);
  135. typecheckpass(tempconst);
  136. firstpass(tempconst);
  137. secondpass(tempconst);
  138. if (tempconst.location.loc <> LOC_CREFERENCE) or
  139. { has to be handled by a helper }
  140. is_64bitint(left.resultdef) then
  141. internalerror(200110011);
  142. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  143. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  144. case left.location.loc of
  145. LOC_REGISTER:
  146. begin
  147. leftreg := left.location.register;
  148. valuereg := leftreg;
  149. end;
  150. LOC_CREGISTER:
  151. begin
  152. leftreg := left.location.register;
  153. if signed then
  154. valuereg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT)
  155. else
  156. valuereg := leftreg;
  157. end;
  158. LOC_REFERENCE,LOC_CREFERENCE:
  159. begin
  160. leftreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  161. valuereg := leftreg;
  162. if signed then
  163. size := OS_S32
  164. else
  165. size := OS_32;
  166. cg.a_load_ref_reg(current_asmdata.CurrAsmList,def_cgsize(left.resultdef),
  167. size,left.location.reference,leftreg);
  168. end
  169. else
  170. internalerror(200110012);
  171. end;
  172. tempreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  173. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_LIS,tempreg,$4330));
  174. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_32,OS_32,tempreg,ref);
  175. if signed then
  176. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_XORIS,valuereg,
  177. { xoris expects a unsigned 16 bit int (FK) }
  178. leftreg,$8000));
  179. inc(ref.offset,4);
  180. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_32,OS_32,valuereg,ref);
  181. dec(ref.offset,4);
  182. tmpfpureg := cg.getfpuregister(current_asmdata.CurrAsmList,OS_F64);
  183. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,OS_F64,OS_F64,tempconst.location.reference,
  184. tmpfpureg);
  185. tempconst.free;
  186. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,OS_F64);
  187. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,OS_F64,OS_F64,ref,location.register);
  188. tg.ungetiftemp(current_asmdata.CurrAsmList,ref);
  189. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_FSUB,location.register,
  190. location.register,tmpfpureg));
  191. { make sure the precision is correct }
  192. if (tfloatdef(resultdef).floattype = s32real) then
  193. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FRSP,location.register,
  194. location.register));
  195. end;
  196. begin
  197. ctypeconvnode:=tppctypeconvnode;
  198. end.