nrv64mat.pas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate RiscV64 assembler for math 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 nrv64mat;
  18. {$I fpcdefs.inc}
  19. interface
  20. uses
  21. node, nmat, ncgmat,
  22. nrvmat,
  23. cgbase;
  24. type
  25. trv64moddivnode = class(tcgmoddivnode)
  26. function use_moddiv64bitint_helper: boolean; override;
  27. procedure emit_div_reg_reg_reg(signed: boolean; denum, num, res: tregister); override;
  28. procedure emit_mod_reg_reg_reg(signed: boolean; denum, num, res: tregister); override;
  29. function first_moddiv64bitint: tnode; override;
  30. end;
  31. trv64shlshrnode = class(tcgshlshrnode)
  32. end;
  33. trv64unaryminusnode = class(trvunaryminusnode)
  34. end;
  35. trv64notnode = class(tcgnotnode)
  36. procedure second_boolean; override;
  37. end;
  38. implementation
  39. uses
  40. nadd,ninl,ncal,ncnv,
  41. globtype,systems,constexp,
  42. cutils,verbose,globals,
  43. cpuinfo,
  44. symconst,symdef,
  45. aasmbase,aasmcpu,aasmtai,aasmdata,
  46. defutil,
  47. cgutils,cgobj,hlcgobj,
  48. pass_1,pass_2,htypechk,
  49. ncon,procinfo,
  50. cpubase,
  51. ncgutil,cgcpu;
  52. procedure trv64notnode.second_boolean;
  53. var
  54. tlabel, flabel: tasmlabel;
  55. begin
  56. secondpass(left);
  57. if not handle_locjump then
  58. begin
  59. case left.location.loc of
  60. LOC_FLAGS :
  61. begin
  62. Internalerror(2016060601);
  63. //location_copy(location,left.location);
  64. //inverse_flags(location.resflags);
  65. end;
  66. LOC_REGISTER, LOC_CREGISTER,
  67. LOC_REFERENCE, LOC_CREFERENCE,
  68. LOC_SUBSETREG, LOC_CSUBSETREG,
  69. LOC_SUBSETREF, LOC_CSUBSETREF:
  70. begin
  71. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  72. location_reset(location,LOC_REGISTER,OS_INT);
  73. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,s64inttype);
  74. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_SLTIU,location.register,left.location.register,1));
  75. end;
  76. else
  77. internalerror(2003042401);
  78. end;
  79. end;
  80. end;
  81. function trv64moddivnode.use_moddiv64bitint_helper: boolean;
  82. begin
  83. Result:=true;
  84. end;
  85. procedure trv64moddivnode.emit_div_reg_reg_reg(signed: boolean; denum, num, res: tregister);
  86. var
  87. op: TAsmOp;
  88. begin
  89. if signed then
  90. op:=A_DIV
  91. else
  92. op:=A_DIVU;
  93. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,res,num,denum));
  94. end;
  95. procedure trv64moddivnode.emit_mod_reg_reg_reg(signed: boolean; denum, num, res: tregister);
  96. var
  97. op: TAsmOp;
  98. begin
  99. if signed then
  100. op:=A_REM
  101. else
  102. op:=A_REMU;
  103. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,res,num,denum));
  104. end;
  105. function trv64moddivnode.first_moddiv64bitint: tnode;
  106. var
  107. power: longint;
  108. begin
  109. {We can handle all cases of constant division}
  110. if not(cs_check_overflow in current_settings.localswitches) and
  111. (right.nodetype=ordconstn) and
  112. (nodetype=divn) and
  113. ((CPURV_HAS_MUL in cpu_capabilities[current_settings.cputype]) and
  114. (ispowerof2(tordconstnode(right).value,power) or
  115. (tordconstnode(right).value=1) or
  116. (tordconstnode(right).value=int64(-1))
  117. )
  118. ) then
  119. result:=nil
  120. else if (CPURV_HAS_MUL in cpu_capabilities[current_settings.cputype]) and
  121. (nodetype in [divn,modn]) then
  122. result:=nil
  123. else
  124. result:=inherited;
  125. { we may not change the result type here }
  126. if assigned(result) and (torddef(result.resultdef).ordtype<>torddef(resultdef).ordtype) then
  127. inserttypeconv(result,resultdef);
  128. end;
  129. begin
  130. cmoddivnode := trv64moddivnode;
  131. cshlshrnode := trv64shlshrnode;
  132. cunaryminusnode := trv64unaryminusnode;
  133. cnotnode := trv64notnode;
  134. end.