nrv64mat.pas 4.8 KB

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