ncpucnv.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. {
  2. Copyright (c) 2014 by Jonas Maebe
  3. Generate AArch64 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. unit ncpucnv;
  17. {$i fpcdefs.inc}
  18. interface
  19. uses
  20. node,ncnv,ncgcnv;
  21. type
  22. taarch64typeconvnode = class(TCgTypeConvNode)
  23. protected
  24. function typecheck_int_to_real: tnode; override;
  25. function first_int_to_real: tnode; override;
  26. { procedure second_int_to_int;override; }
  27. { procedure second_string_to_string;override; }
  28. { procedure second_cstring_to_pchar;override; }
  29. { procedure second_string_to_chararray;override; }
  30. { procedure second_array_to_pointer;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,globals,
  49. symdef,aasmdata,aasmbase,
  50. defutil,
  51. cgbase,cgutils,procinfo,
  52. cpubase,aasmcpu,
  53. pass_2,cgobj,
  54. hlcgobj;
  55. {*****************************************************************************
  56. FirstTypeConv
  57. *****************************************************************************}
  58. function taarch64typeconvnode.typecheck_int_to_real: tnode;
  59. begin
  60. { aarch64 supports converting everything to floating point, even fixed
  61. point! Unfortunately, it only supports fixed point with a power-of-2
  62. fraction, which is not the case for currency.
  63. Generate the division by 10000 via nodes so the 10000.0 constant can
  64. be reused. }
  65. if is_currency(resultdef) and
  66. not(nf_is_currency in flags) then
  67. begin
  68. { convert the equivalent int64 value to double without conversion
  69. (internal typecast -> will set nf_is_currency flag) }
  70. result:=ctypeconvnode.create_internal(left,s64floattype);
  71. { turn into currency with conversion, which will divide by 10000
  72. (regular typecast) }
  73. result:=ctypeconvnode.create(result,s64currencytype);
  74. exit;
  75. end;
  76. { The only other thing we have to take care of: convert values < 32 bit
  77. to 32 bit }
  78. if left.resultdef.size<4 then
  79. begin
  80. if is_signed(left.resultdef) then
  81. inserttypeconv(left,s32inttype)
  82. else
  83. inserttypeconv(left,u32inttype)
  84. end;
  85. result:=inherited;
  86. end;
  87. function taarch64typeconvnode.first_int_to_real: tnode;
  88. begin
  89. result:=nil;
  90. expectloc:=LOC_MMREGISTER;
  91. end;
  92. {*****************************************************************************
  93. SecondTypeConv
  94. *****************************************************************************}
  95. procedure taarch64typeconvnode.second_int_to_real;
  96. var
  97. op: tasmop;
  98. begin
  99. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  100. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  101. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  102. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  103. internalerror(2014120401);
  104. case left.location.size of
  105. OS_32,
  106. OS_64:
  107. op:=A_UCVTF;
  108. OS_S32,
  109. OS_S64,
  110. { for currency and comp }
  111. OS_F64:
  112. op:=A_SCVTF;
  113. else
  114. internalerror(2014120402);
  115. end;
  116. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,location.register,left.location.register));
  117. { no scaling for currency, that's handled in pass_typecheck }
  118. end;
  119. procedure taarch64typeconvnode.second_int_to_bool;
  120. var
  121. resflags: tresflags;
  122. hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
  123. begin
  124. if (nf_explicit in flags) and
  125. not(left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  126. begin
  127. inherited;
  128. exit;
  129. end;
  130. { can't use the generic code, as it assumes that OP_OR automatically sets
  131. the flags. We can also do things more efficiently directly }
  132. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  133. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  134. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  135. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  136. secondpass(left);
  137. if codegenerror then
  138. exit;
  139. case left.location.loc of
  140. LOC_SUBSETREG,
  141. LOC_CSUBSETREG,
  142. LOC_SUBSETREF,
  143. LOC_CSUBSETREF,
  144. LOC_CREFERENCE,
  145. LOC_REFERENCE,
  146. LOC_REGISTER,
  147. LOC_CREGISTER,
  148. LOC_JUMP:
  149. begin
  150. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  151. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,0));
  152. resflags:=F_NE;
  153. end;
  154. LOC_FLAGS :
  155. resflags:=left.location.resflags;
  156. else
  157. internalerror(2014122902);
  158. end;
  159. { load flags to register }
  160. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  161. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  162. if is_cbool(resultdef) then
  163. begin
  164. current_asmdata.CurrAsmList.concat(taicpu.op_reg_cond(A_CSETM,location.register,flags_to_cond(resflags)));
  165. { truncate? (in case cbools are ever made unsigned) }
  166. if resultdef.size<4 then
  167. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,location.size,location.register,location.register);
  168. end
  169. else
  170. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,location.register);
  171. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  172. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  173. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  174. end;
  175. begin
  176. ctypeconvnode:=taarch64typeconvnode;
  177. end.