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