ngppccnv.pas 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 ngppccnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncnv,ncgcnv;
  22. type
  23. tgenppctypeconvnode = class(tcgtypeconvnode)
  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_load_smallset;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,cutils,
  51. cgbase,cgutils,pass_1,pass_2,
  52. ncgutil,procinfo,
  53. cpubase,aasmcpu,
  54. rgobj,tgobj,cgobj,hlcgobj;
  55. procedure tgenppctypeconvnode.second_int_to_bool;
  56. var
  57. hreg1,
  58. hreg2 : tregister;
  59. {$ifndef cpu64bitalu}
  60. href : treference;
  61. oldloc : tlocation;
  62. {$endif not cpu64bitalu}
  63. resflags : tresflags;
  64. opsize : tcgsize;
  65. hlabel : tasmlabel;
  66. newsize : tcgsize;
  67. begin
  68. secondpass(left);
  69. if codegenerror then
  70. exit;
  71. { Explicit typecasts from any ordinal type to a boolean type }
  72. { must not change the ordinal value }
  73. if (nf_explicit in flags) and
  74. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  75. begin
  76. location_copy(location,left.location);
  77. newsize:=def_cgsize(resultdef);
  78. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  79. if (tcgsize2size[newsize]>tcgsize2size[left.location.size]) or
  80. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  81. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  82. else
  83. begin
  84. location.size:=newsize;
  85. if (target_info.endian = ENDIAN_BIG) and
  86. (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  87. begin
  88. inc(location.reference.offset,TCGSize2Size[left.location.size]-TCGSize2Size[location.size]);
  89. location.reference.alignment:=newalignment(location.reference.alignment,TCGSize2Size[left.location.size]-TCGSize2Size[location.size]);
  90. end;
  91. end;
  92. exit;
  93. end;
  94. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  95. opsize := def_cgsize(left.resultdef);
  96. {$ifndef cpu64bitalu}
  97. if (opsize in [OS_64,OS_S64]) then
  98. opsize:=OS_32;
  99. {$endif not cpu64bitalu}
  100. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  101. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  102. case left.location.loc of
  103. LOC_CREFERENCE,LOC_REFERENCE,LOC_REGISTER,LOC_CREGISTER :
  104. begin
  105. if left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE] then
  106. begin
  107. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  108. {$ifndef cpu64bitalu}
  109. if left.location.size in [OS_64,OS_S64] then
  110. begin
  111. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,left.location.reference,hreg1);
  112. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  113. href:=left.location.reference;
  114. inc(href.offset,4);
  115. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,href,hreg2);
  116. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,hreg1,hreg2,hreg1);
  117. end
  118. else
  119. {$endif not cpu64bitalu}
  120. cg.a_load_ref_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.reference,hreg1);
  121. end
  122. else
  123. begin
  124. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  125. {$ifndef cpu64bitalu}
  126. if left.location.size in [OS_64,OS_S64] then
  127. begin
  128. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  129. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,left.location.register64.reglo,hreg1);
  130. end
  131. else
  132. {$endif not cpu64bitalu}
  133. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,hreg1);
  134. end;
  135. hreg2 := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  136. if not(is_cbool(resultdef)) then
  137. begin
  138. { hreg2:=hreg1-1; carry:=hreg1=0 }
  139. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBIC,hreg2,hreg1,1));
  140. { hreg1:=hreg1-hreg2+carry (= hreg1-(hreg1-1)-carry) }
  141. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBFE,hreg1,hreg2,hreg1));
  142. end
  143. else
  144. begin
  145. { carry:=hreg1<>0 }
  146. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_SUBFIC,hreg2,hreg1,0));
  147. { hreg1:=hreg1-hreg1-carry }
  148. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBFE,hreg1,hreg1,hreg1));
  149. end;
  150. end;
  151. LOC_FLAGS :
  152. begin
  153. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  154. resflags:=left.location.resflags;
  155. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,hreg1);
  156. if (is_cbool(resultdef)) then
  157. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,hreg1,hreg1);
  158. end;
  159. LOC_JUMP :
  160. begin
  161. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  162. current_asmdata.getjumplabel(hlabel);
  163. cg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
  164. if not(is_cbool(resultdef)) then
  165. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hreg1)
  166. else
  167. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,-1,hreg1);
  168. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  169. cg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  170. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hreg1);
  171. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  172. end;
  173. else
  174. internalerror(10062);
  175. end;
  176. {$ifndef cpu64bitalu}
  177. if (location.size in [OS_64,OS_S64]) then
  178. begin
  179. location.register64.reglo:=hreg1;
  180. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  181. if (is_cbool(resultdef)) then
  182. { reglo is either 0 or -1 -> reghi has to become the same }
  183. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  184. else
  185. { unsigned }
  186. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  187. end
  188. else
  189. {$endif cpu64bitalu}
  190. location.register:=hreg1;
  191. end;
  192. end.