nrv32mat.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate Risc-V32 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 nrv32mat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nmat, ncgmat,
  22. nrvmat,
  23. cgbase;
  24. type
  25. trv32moddivnode = class(tcgmoddivnode)
  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_moddivint: tnode; override;
  29. end;
  30. trv32shlshrnode = class(tcgshlshrnode)
  31. function first_shlshr64bitint: tnode;override;
  32. procedure second_64bit;override;
  33. end;
  34. trv32unaryminusnode = class(trvunaryminusnode)
  35. end;
  36. trv32notnode = class(tcgnotnode)
  37. procedure second_boolean; override;
  38. end;
  39. implementation
  40. uses
  41. globtype,systems,constexp,
  42. cutils,verbose,globals,
  43. symconst,symdef,
  44. aasmbase,aasmcpu,aasmtai,aasmdata,
  45. defutil,
  46. cgutils,cgobj,hlcgobj,pass_2,
  47. cpubase,cpuinfo,
  48. ncon,procinfo,
  49. ncgutil,cgcpu;
  50. procedure trv32notnode.second_boolean;
  51. var
  52. tlabel, flabel: tasmlabel;
  53. begin
  54. secondpass(left);
  55. if not handle_locjump then
  56. begin
  57. case left.location.loc of
  58. LOC_FLAGS :
  59. begin
  60. Internalerror(2016060601);
  61. //location_copy(location,left.location);
  62. //inverse_flags(location.resflags);
  63. end;
  64. LOC_REGISTER, LOC_CREGISTER,
  65. LOC_REFERENCE, LOC_CREFERENCE,
  66. LOC_SUBSETREG, LOC_CSUBSETREG,
  67. LOC_SUBSETREF, LOC_CSUBSETREF:
  68. begin
  69. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  70. location_reset(location,LOC_REGISTER,OS_INT);
  71. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,s32inttype);
  72. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_const(A_SLTIU,location.register,left.location.register,1));
  73. end;
  74. else
  75. internalerror(2003042401);
  76. end;
  77. end;
  78. end;
  79. procedure trv32moddivnode.emit_div_reg_reg(signed: boolean; denum, num: tregister);
  80. var
  81. op: TAsmOp;
  82. begin
  83. if signed then
  84. op:=A_DIV
  85. else
  86. op:=A_DIVU;
  87. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,num,num,denum));
  88. end;
  89. procedure trv32moddivnode.emit_mod_reg_reg(signed: boolean; denum, num: tregister);
  90. var
  91. op: TAsmOp;
  92. begin
  93. if signed then
  94. op:=A_REM
  95. else
  96. op:=A_REMU;
  97. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(op,num,num,denum));
  98. end;
  99. function trv32moddivnode.first_moddivint: tnode;
  100. begin
  101. if (not is_64bitint(resultdef)) and
  102. (CPURV_HAS_MUL in cpu_capabilities[current_settings.cputype]) then
  103. Result:=nil
  104. else
  105. result:=inherited;
  106. end;
  107. function trv32shlshrnode.first_shlshr64bitint: tnode;
  108. begin
  109. result := nil;
  110. end;
  111. procedure trv32shlshrnode.second_64bit;
  112. var
  113. v : TConstExprInt;
  114. lreg, resreg: TRegister64;
  115. procedure emit_instr(p: tai);
  116. begin
  117. current_asmdata.CurrAsmList.concat(p);
  118. end;
  119. var
  120. tmpreg1, tmpreg2: TRegister;
  121. less32, finished: TAsmLabel;
  122. begin
  123. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  124. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  125. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  126. { load left operator in a register }
  127. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  128. (left.location.size<>OS_64) then
  129. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  130. lreg:=left.location.register64;
  131. resreg:=location.register64;
  132. { shifting by a constant directly coded: }
  133. if right.nodetype=ordconstn then
  134. begin
  135. v:=tordconstnode(right).value and 63;
  136. if v >= 32 then
  137. begin
  138. if nodetype=shln then
  139. begin
  140. emit_instr(taicpu.op_reg_const(A_LI, resreg.reglo,0));
  141. emit_instr(taicpu.op_reg_reg_const(A_SLLI,resreg.reghi,lreg.reglo,v.uvalue-32));
  142. end
  143. else
  144. begin
  145. emit_instr(taicpu.op_reg_const(A_LI, resreg.reghi,0));
  146. emit_instr(taicpu.op_reg_reg_const(A_SRLI,resreg.reglo,lreg.reghi,v.uvalue-32));
  147. end
  148. end
  149. else if (v < 32) then
  150. if nodetype=shln then
  151. begin
  152. tmpreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  153. emit_instr(taicpu.op_reg_reg_const(A_SRLI,tmpreg1,lreg.reglo,32-v.uvalue));
  154. emit_instr(taicpu.op_reg_reg_const(A_SLLI,resreg.reglo,lreg.reglo,v.uvalue));
  155. emit_instr(taicpu.op_reg_reg_const(A_SLLI,resreg.reghi,lreg.reghi,v.uvalue));
  156. emit_instr(taicpu.op_reg_reg_reg(A_OR,resreg.reghi,resreg.reghi,tmpreg1));
  157. end
  158. else
  159. begin
  160. tmpreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  161. emit_instr(taicpu.op_reg_reg_const(A_SLLI,tmpreg1,lreg.reghi,32-v.uvalue));
  162. emit_instr(taicpu.op_reg_reg_const(A_SRLI,resreg.reglo,lreg.reglo,v.uvalue));
  163. emit_instr(taicpu.op_reg_reg_const(A_SRLI,resreg.reghi,lreg.reghi,v.uvalue));
  164. emit_instr(taicpu.op_reg_reg_reg(A_OR,resreg.reglo,resreg.reglo,tmpreg1));
  165. end
  166. else
  167. Internalerror(2024072601);
  168. end
  169. else
  170. begin
  171. { force right operator into a register }
  172. if not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) or
  173. (right.location.size<>OS_32) then
  174. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,u32inttype,true);
  175. current_asmdata.getjumplabel(less32);
  176. current_asmdata.getjumplabel(finished);
  177. tmpreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  178. tmpreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  179. if nodetype = shln then
  180. begin
  181. emit_instr(taicpu.op_reg_reg_const(A_ADDI,tmpreg1,right.location.register64.reglo,-32));
  182. emit_instr(taicpu.op_reg_sym(A_BLTZ,tmpreg1,less32));
  183. emit_instr(taicpu.op_reg_const(A_LI,resreg.reglo,0));
  184. emit_instr(taicpu.op_reg_reg_reg(A_SLL,resreg.reghi,lreg.reglo,tmpreg1));
  185. cg.a_jmp_always(current_asmdata.CurrAsmList,finished);
  186. cg.a_label(current_asmdata.CurrAsmList,less32);
  187. { simple case were we know where the bit ends up, usefull when bitmasks are created }
  188. if (left.nodetype=ordconstn) and (tordconstnode(left).value=1) then
  189. begin
  190. emit_instr(taicpu.op_reg_reg_reg(A_SLL,resreg.reglo,lreg.reglo,right.location.register64.reglo));
  191. emit_instr(taicpu.op_reg_const(A_LI,resreg.reghi,0));
  192. end
  193. else
  194. begin
  195. emit_instr(taicpu.op_reg_const(A_LI,tmpreg1,31));
  196. emit_instr(taicpu.op_reg_reg_const(A_SRLI,tmpreg2,lreg.reglo,1));
  197. emit_instr(taicpu.op_reg_reg_reg(A_SUB,tmpreg1,tmpreg1,right.location.register64.reglo));
  198. emit_instr(taicpu.op_reg_reg_reg(A_SLL,resreg.reglo,lreg.reglo,right.location.register64.reglo));
  199. emit_instr(taicpu.op_reg_reg_reg(A_SRL,tmpreg2,tmpreg2,tmpreg1));
  200. emit_instr(taicpu.op_reg_reg_reg(A_SLL,resreg.reghi,lreg.reghi,right.location.register64.reglo));
  201. emit_instr(taicpu.op_reg_reg_reg(A_OR,resreg.reghi,resreg.reghi,tmpreg2));
  202. end;
  203. end
  204. else
  205. begin
  206. emit_instr(taicpu.op_reg_reg_const(A_ADDI,tmpreg1,right.location.register64.reglo,-32));
  207. emit_instr(taicpu.op_reg_sym(A_BLTZ,tmpreg1,less32));
  208. emit_instr(taicpu.op_reg_const(A_LI,resreg.reghi,0));
  209. emit_instr(taicpu.op_reg_reg_reg(A_SRL,resreg.reglo,lreg.reghi,tmpreg1));
  210. cg.a_jmp_always(current_asmdata.CurrAsmList,finished);
  211. cg.a_label(current_asmdata.CurrAsmList,less32);
  212. emit_instr(taicpu.op_reg_const(A_LI,tmpreg1,31));
  213. emit_instr(taicpu.op_reg_reg_const(A_SLLI,tmpreg2,lreg.reghi,1));
  214. emit_instr(taicpu.op_reg_reg_reg(A_SUB,tmpreg1,tmpreg1,right.location.register64.reglo));
  215. emit_instr(taicpu.op_reg_reg_reg(A_SRL,resreg.reglo,lreg.reglo,right.location.register64.reglo));
  216. emit_instr(taicpu.op_reg_reg_reg(A_SLL,tmpreg2,tmpreg2,tmpreg1));
  217. emit_instr(taicpu.op_reg_reg_reg(A_SRL,resreg.reghi,lreg.reghi,right.location.register64.reglo));
  218. emit_instr(taicpu.op_reg_reg_reg(A_OR,resreg.reglo,resreg.reglo,tmpreg2));
  219. end;
  220. cg.a_label(current_asmdata.CurrAsmList,finished);
  221. end;
  222. end;
  223. begin
  224. cmoddivnode:=trv32moddivnode;
  225. cshlshrnode:=trv32shlshrnode;
  226. cunaryminusnode:=trv32unaryminusnode;
  227. cnotnode:=trv32notnode;
  228. end.