nrv32cnv.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate Risc-V32 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 nrv32cnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncnv,ncgcnv,nrvcnv;
  22. type
  23. trv32typeconvnode = class(trvtypeconvnode)
  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 trv32typeconvnode.first_int_to_real: tnode;
  60. var
  61. fname: string[19];
  62. begin
  63. if (cs_fp_emulation in current_settings.moduleswitches) then
  64. result:=inherited first_int_to_real
  65. { converting a 64bit integer to a float requires a helper }
  66. else
  67. begin
  68. if is_64bitint(left.resultdef) or
  69. is_currency(left.resultdef) then
  70. begin
  71. { hack to avoid double division by 10000, as it's }
  72. { already done by typecheckpass.resultdef_int_to_real }
  73. if is_currency(left.resultdef) then
  74. left.resultdef := s64inttype;
  75. if is_signed(left.resultdef) then
  76. fname := 'fpc_int64_to_double'
  77. else
  78. fname := 'fpc_qword_to_double';
  79. result := ccallnode.createintern(fname,ccallparanode.create(
  80. left,nil));
  81. left:=nil;
  82. firstpass(result);
  83. exit;
  84. end
  85. else
  86. { other integers are supposed to be 32 bit }
  87. begin
  88. if is_signed(left.resultdef) then
  89. inserttypeconv(left,s32inttype)
  90. else
  91. inserttypeconv(left,u32inttype);
  92. firstpass(left);
  93. end;
  94. result := nil;
  95. expectloc:=LOC_FPUREGISTER;
  96. end;
  97. end;
  98. {*****************************************************************************
  99. SecondTypeConv
  100. *****************************************************************************}
  101. procedure trv32typeconvnode.second_int_to_real;
  102. const
  103. ops: array[boolean,s32real..s64real] of TAsmOp =
  104. ((A_FCVT_S_WU,A_FCVT_D_WU),
  105. (A_FCVT_S_W,A_FCVT_D_W));
  106. var
  107. restype: tfloattype;
  108. begin
  109. location_reset(location, LOC_FPUREGISTER, def_cgsize(resultdef));
  110. restype:=tfloatdef(resultdef).floattype;
  111. location.Register := cg.getfpuregister(current_asmdata.CurrAsmList, tfloat2tcgsize[restype]);
  112. if (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  113. begin
  114. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(ops[is_signed(left.resultdef),restype], location.register, left.location.register));
  115. end
  116. else
  117. begin
  118. { Load memory in fpu register }
  119. hlcg.location_force_mem(current_asmdata.CurrAsmList, left.location, left.resultdef);
  120. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList, OS_F32, OS_F32, left.location.reference, location.Register);
  121. tg.ungetiftemp(current_asmdata.CurrAsmList, left.location.reference);
  122. case restype of
  123. s64real: cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList, OS_F32, OS_F64, location.register, location.Register);
  124. else
  125. ;
  126. end;
  127. end;
  128. end;
  129. begin
  130. ctypeconvnode:=trv32typeconvnode;
  131. end.