nrv32mat.pas 10 KB

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