2
0

cgcpu.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the RiscV64
  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 cgcpu;
  18. {$I fpcdefs.inc}
  19. interface
  20. uses
  21. globtype, symtype, symdef, symsym,
  22. cgbase, cgobj,cgrv,
  23. aasmbase, aasmcpu, aasmtai,aasmdata,
  24. cpubase, cpuinfo, cgutils, rgcpu,
  25. parabase;
  26. type
  27. tcgrv64 = class(tcgrv)
  28. procedure init_register_allocators; override;
  29. procedure done_register_allocators; override;
  30. { move instructions }
  31. procedure a_load_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  32. procedure a_load_const_reg(list: TAsmList; size: tcgsize; a: tcgint; register: tregister); override;
  33. procedure g_concatcopy(list: TAsmList; const source, dest: treference; len: aint); override;
  34. end;
  35. procedure create_codegen;
  36. implementation
  37. uses
  38. sysutils, cclasses,
  39. globals, verbose, systems, cutils,
  40. symconst, fmodule, symtable,
  41. rgobj, tgobj, cpupi, procinfo, paramgr, cpupara;
  42. { Range check must be disabled explicitly as conversions between signed and unsigned
  43. 64-bit and 32-bit values are done without explicit typecasts }
  44. {$R-}
  45. procedure tcgrv64.init_register_allocators;
  46. begin
  47. inherited init_register_allocators;
  48. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  49. [RS_X10,RS_X11,RS_X12,RS_X13,RS_X14,RS_X15,RS_X16,RS_X17,
  50. RS_X31,RS_X30,RS_X29,RS_X28,
  51. RS_X5,RS_X6,RS_X7,
  52. RS_X9,RS_X27,RS_X26,RS_X25,RS_X24,RS_X23,RS_X22,
  53. RS_X21,RS_X20,RS_X19,RS_X18],first_int_imreg,[]);
  54. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  55. [RS_F10,RS_F11,RS_F12,RS_F13,RS_F14,RS_F15,RS_F16,RS_F17,
  56. RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,
  57. RS_F28,RS_F29,RS_F30,RS_F31,
  58. RS_F8,RS_F9,
  59. RS_F27,
  60. RS_F26,RS_F25,RS_F24,RS_F23,RS_F22,RS_F21,RS_F20,RS_F19,RS_F18],first_fpu_imreg,[]);
  61. end;
  62. procedure tcgrv64.done_register_allocators;
  63. begin
  64. rg[R_INTREGISTER].free;
  65. rg[R_FPUREGISTER].free;
  66. inherited done_register_allocators;
  67. end;
  68. procedure tcgrv64.a_load_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  69. var
  70. ai: taicpu;
  71. begin
  72. {$ifdef EXTDEBUG}
  73. list.concat(tai_comment.Create(strpnew('Move '+tcgsize2str(fromsize)+'->'+tcgsize2str(tosize))));
  74. {$endif EXTDEBUG}
  75. if (tcgsize2unsigned[tosize]=OS_64) and (fromsize=OS_S32) then
  76. list.Concat(taicpu.op_reg_reg_const(A_ADDIW,reg2,reg1,0))
  77. else if (tosize=OS_S32) and (tcgsize2unsigned[fromsize]=OS_64) then
  78. list.Concat(taicpu.op_reg_reg_const(A_ADDIW,reg2,reg1,0))
  79. else if (CPURV_HAS_ZBA in cpu_capabilities[current_settings.cputype]) and (tosize=OS_32) and (tcgsize2unsigned[fromsize]=OS_64) then
  80. list.Concat(taicpu.op_reg_reg(A_ZEXT_W,reg2,reg1))
  81. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tcgsize2unsigned[tosize]=OS_64) and (fromsize=OS_S8) then
  82. list.Concat(taicpu.op_reg_reg(A_SEXT_B,reg2,reg1))
  83. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tosize=OS_S8) and (tcgsize2unsigned[fromsize]=OS_64) then
  84. list.Concat(taicpu.op_reg_reg(A_SEXT_B,reg2,reg1))
  85. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tcgsize2unsigned[tosize]=OS_64) and (fromsize=OS_S16) then
  86. list.Concat(taicpu.op_reg_reg(A_SEXT_H,reg2,reg1))
  87. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tosize=OS_S16) and (tcgsize2unsigned[fromsize]=OS_64) then
  88. list.Concat(taicpu.op_reg_reg(A_SEXT_H,reg2,reg1))
  89. else if (tosize=OS_S32) and (fromsize=OS_32) then
  90. list.Concat(taicpu.op_reg_reg_const(A_ADDIW,reg2,reg1,0))
  91. else if (tcgsize2unsigned[tosize]=OS_64) and (fromsize=OS_8) then
  92. list.Concat(taicpu.op_reg_reg_const(A_ANDI,reg2,reg1,$FF))
  93. else if (tosize=OS_8) and (fromsize<>OS_8) then
  94. list.Concat(taicpu.op_reg_reg_const(A_ANDI,reg2,reg1,$FF))
  95. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tcgsize2unsigned[tosize]=OS_64) and (fromsize=OS_16) then
  96. list.Concat(taicpu.op_reg_reg(A_ZEXT_H,reg2,reg1))
  97. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tosize=OS_16) and (fromsize<>OS_16) then
  98. list.Concat(taicpu.op_reg_reg(A_ZEXT_H,reg2,reg1))
  99. else if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  100. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and (fromsize <> tosize)) or
  101. { do we need to mask out the sign when loading from smaller signed to larger unsigned type? }
  102. ((tcgsize2unsigned[fromsize]<>fromsize) and ((tcgsize2unsigned[tosize]=tosize)) and
  103. (tcgsize2size[fromsize] < tcgsize2size[tosize]) and (tcgsize2size[tosize] <> sizeof(pint)) ) then
  104. begin
  105. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  106. begin
  107. list.Concat(taicpu.op_reg_reg_const(A_SLLI,reg2,reg1,8*(8-tcgsize2size[fromsize])));
  108. if tcgsize2unsigned[fromsize]<>fromsize then
  109. list.Concat(taicpu.op_reg_reg_const(A_SRAI,reg2,reg2,8*(tcgsize2size[tosize]-tcgsize2size[fromsize])))
  110. else
  111. list.Concat(taicpu.op_reg_reg_const(A_SRLI,reg2,reg2,8*(tcgsize2size[tosize]-tcgsize2size[fromsize])));
  112. end
  113. else if tcgsize2unsigned[tosize]<>OS_64 then
  114. list.Concat(taicpu.op_reg_reg_const(A_SLLI,reg2,reg1,8*(8-tcgsize2size[tosize])))
  115. else
  116. a_load_reg_reg(list,tosize,tosize,reg1,reg2);
  117. if tcgsize2unsigned[tosize]=tosize then
  118. list.Concat(taicpu.op_reg_reg_const(A_SRLI,reg2,reg2,8*(8-tcgsize2size[tosize])))
  119. else
  120. list.Concat(taicpu.op_reg_reg_const(A_SRAI,reg2,reg2,8*(8-tcgsize2size[tosize])));
  121. end
  122. else
  123. begin
  124. ai:=taicpu.op_reg_reg_const(A_ADDI,reg2,reg1,0);
  125. list.concat(ai);
  126. rg[R_INTREGISTER].add_move_instruction(ai);
  127. end;
  128. end;
  129. procedure tcgrv64.a_load_const_reg(list: TAsmList; size: tcgsize; a: tcgint; register: tregister);
  130. var
  131. l: TAsmLabel;
  132. hr: treference;
  133. begin
  134. if a=0 then
  135. a_load_reg_reg(list,size,size,NR_X0,register)
  136. else
  137. begin
  138. if is_imm12(a) then
  139. list.concat(taicpu.op_reg_reg_const(A_ADDI,register,NR_X0,a))
  140. else if is_lui_imm(a) then
  141. list.concat(taicpu.op_reg_const(A_LUI,register,(a shr 12) and $FFFFF))
  142. else if (int64(longint(a))=a) then
  143. begin
  144. if (a and $800)<>0 then
  145. list.concat(taicpu.op_reg_const(A_LUI,register,((a shr 12)+1) and $FFFFF))
  146. else
  147. list.concat(taicpu.op_reg_const(A_LUI,register,(a shr 12) and $FFFFF));
  148. list.concat(taicpu.op_reg_reg_const(A_ADDIW,register,register,SarSmallint(smallint(a shl 4),4)));
  149. end
  150. else
  151. begin
  152. reference_reset(hr,8,[]);
  153. current_asmdata.getjumplabel(l);
  154. current_procinfo.aktlocaldata.Concat(cai_align.Create(8));
  155. cg.a_label(current_procinfo.aktlocaldata,l);
  156. hr.symboldata:=current_procinfo.aktlocaldata.last;
  157. current_procinfo.aktlocaldata.concat(tai_const.Create_64bit(a));
  158. hr.symbol:=l;
  159. hr.refaddr:=addr_pcrel_hi20;
  160. current_asmdata.getjumplabel(l);
  161. a_label(list,l);
  162. list.concat(taicpu.op_reg_ref(A_AUIPC,register,hr));
  163. reference_reset_symbol(hr,l,0,0,[]);
  164. hr.refaddr:=addr_pcrel_lo12;
  165. hr.base:=register;
  166. list.concat(taicpu.op_reg_ref(A_LD,register,hr));
  167. end;
  168. end;
  169. end;
  170. procedure tcgrv64.g_concatcopy(list: TAsmList; const source, dest: treference; len: aint);
  171. var
  172. tmpreg1, hreg, countreg: TRegister;
  173. src, dst, src2, dst2: TReference;
  174. lab: tasmlabel;
  175. Count, count2: aint;
  176. begin
  177. src2:=source;
  178. fixref(list,src2);
  179. dst2:=dest;
  180. fixref(list,dst2);
  181. if len > high(longint) then
  182. internalerror(2002072704);
  183. { A call (to FPC_MOVE) requires the outgoing parameter area to be properly
  184. allocated on stack. This can only be done before tmipsprocinfo.set_first_temp_offset,
  185. i.e. before secondpass. Other internal procedures request correct stack frame
  186. by setting pi_do_call during firstpass, but for this particular one it is impossible.
  187. Therefore, if the current procedure is a leaf one, we have to leave it that way. }
  188. { anybody wants to determine a good value here :)? }
  189. if (len > 100) and
  190. assigned(current_procinfo) and
  191. (pi_do_call in current_procinfo.flags) then
  192. g_concatcopy_move(list, src2, dst2, len)
  193. else
  194. begin
  195. Count := len div 8;
  196. reference_reset(src,sizeof(aint),[]);
  197. { load the address of src2 into src.base }
  198. src.base := GetAddressRegister(list);
  199. a_loadaddr_ref_reg(list, src2, src.base);
  200. reference_reset(dst,sizeof(aint),[]);
  201. { load the address of dst2 into dst.base }
  202. dst.base := GetAddressRegister(list);
  203. a_loadaddr_ref_reg(list, dst2, dst.base);
  204. { generate a loop }
  205. if Count > 4 then
  206. begin
  207. countreg := GetIntRegister(list, OS_INT);
  208. tmpreg1 := GetIntRegister(list, OS_INT);
  209. a_load_const_reg(list, OS_INT, Count, countreg);
  210. current_asmdata.getjumplabel(lab);
  211. a_label(list, lab);
  212. list.concat(taicpu.op_reg_ref(A_LD, tmpreg1, src));
  213. list.concat(taicpu.op_reg_ref(A_SD, tmpreg1, dst));
  214. list.concat(taicpu.op_reg_reg_const(A_ADDI, src.base, src.base, 8));
  215. list.concat(taicpu.op_reg_reg_const(A_ADDI, dst.base, dst.base, 8));
  216. list.concat(taicpu.op_reg_reg_const(A_ADDI, countreg, countreg, -1));
  217. a_cmp_reg_reg_label(list,OS_INT,OC_GT,NR_X0,countreg,lab);
  218. len := len mod 8;
  219. end;
  220. { unrolled loop }
  221. Count := len div 8;
  222. if Count > 0 then
  223. begin
  224. tmpreg1 := GetIntRegister(list, OS_INT);
  225. count2 := 1;
  226. while count2 <= Count do
  227. begin
  228. list.concat(taicpu.op_reg_ref(A_LD, tmpreg1, src));
  229. list.concat(taicpu.op_reg_ref(A_SD, tmpreg1, dst));
  230. Inc(src.offset, 8);
  231. Inc(dst.offset, 8);
  232. Inc(count2);
  233. end;
  234. len := len mod 8;
  235. end;
  236. if (len and 4) <> 0 then
  237. begin
  238. hreg := GetIntRegister(list, OS_INT);
  239. a_load_ref_reg(list, OS_32, OS_32, src, hreg);
  240. a_load_reg_ref(list, OS_32, OS_32, hreg, dst);
  241. Inc(src.offset, 4);
  242. Inc(dst.offset, 4);
  243. end;
  244. { copy the leftovers }
  245. if (len and 2) <> 0 then
  246. begin
  247. hreg := GetIntRegister(list, OS_INT);
  248. a_load_ref_reg(list, OS_16, OS_16, src, hreg);
  249. a_load_reg_ref(list, OS_16, OS_16, hreg, dst);
  250. Inc(src.offset, 2);
  251. Inc(dst.offset, 2);
  252. end;
  253. if (len and 1) <> 0 then
  254. begin
  255. hreg := GetIntRegister(list, OS_INT);
  256. a_load_ref_reg(list, OS_8, OS_8, src, hreg);
  257. a_load_reg_ref(list, OS_8, OS_8, hreg, dst);
  258. end;
  259. end;
  260. end;
  261. procedure create_codegen;
  262. begin
  263. cg := tcgrv64.create;
  264. cg128:=tcg128.create;
  265. end;
  266. end.