cgcpu.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation); override;
  34. procedure g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef); override;
  35. procedure g_concatcopy(list: TAsmList; const source, dest: treference; len: aint); override;
  36. end;
  37. procedure create_codegen;
  38. implementation
  39. uses
  40. sysutils, cclasses,
  41. globals, verbose, systems, cutils,
  42. symconst, fmodule, symtable,
  43. rgobj, tgobj, cpupi, procinfo, paramgr, cpupara;
  44. { Range check must be disabled explicitly as conversions between signed and unsigned
  45. 64-bit and 32-bit values are done without explicit typecasts }
  46. {$R-}
  47. procedure tcgrv64.init_register_allocators;
  48. begin
  49. inherited init_register_allocators;
  50. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  51. [RS_X10,RS_X11,RS_X12,RS_X13,RS_X14,RS_X15,RS_X16,RS_X17,
  52. RS_X31,RS_X30,RS_X29,RS_X28,
  53. RS_X5,RS_X6,RS_X7,
  54. RS_X9,RS_X27,RS_X26,RS_X25,RS_X24,RS_X23,RS_X22,
  55. RS_X21,RS_X20,RS_X19,RS_X18],first_int_imreg,[]);
  56. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  57. [RS_F10,RS_F11,RS_F12,RS_F13,RS_F14,RS_F15,RS_F16,RS_F17,
  58. RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,
  59. RS_F28,RS_F29,RS_F30,RS_F31,
  60. RS_F8,RS_F9,
  61. RS_F27,
  62. RS_F26,RS_F25,RS_F24,RS_F23,RS_F22,RS_F21,RS_F20,RS_F19,RS_F18],first_fpu_imreg,[]);
  63. end;
  64. procedure tcgrv64.done_register_allocators;
  65. begin
  66. rg[R_INTREGISTER].free;
  67. rg[R_FPUREGISTER].free;
  68. inherited done_register_allocators;
  69. end;
  70. procedure tcgrv64.a_load_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  71. var
  72. ai: taicpu;
  73. begin
  74. {$ifdef EXTDEBUG}
  75. list.concat(tai_comment.Create(strpnew('Move '+tcgsize2str(fromsize)+'->'+tcgsize2str(tosize))));
  76. {$endif EXTDEBUG}
  77. if (tcgsize2unsigned[tosize]=OS_64) and (fromsize=OS_S32) then
  78. list.Concat(taicpu.op_reg_reg_const(A_ADDIW,reg2,reg1,0))
  79. else if (tosize=OS_S32) and (tcgsize2unsigned[fromsize]=OS_64) then
  80. list.Concat(taicpu.op_reg_reg_const(A_ADDIW,reg2,reg1,0))
  81. else if (CPURV_HAS_ZBA in cpu_capabilities[current_settings.cputype]) and (tosize=OS_32) and (tcgsize2unsigned[fromsize]=OS_64) then
  82. list.Concat(taicpu.op_reg_reg(A_ZEXT_W,reg2,reg1))
  83. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tcgsize2unsigned[tosize]=OS_64) and (fromsize=OS_S8) 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 (tosize=OS_S8) and (tcgsize2unsigned[fromsize]=OS_64) then
  86. list.Concat(taicpu.op_reg_reg(A_SEXT_B,reg2,reg1))
  87. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tcgsize2unsigned[tosize]=OS_64) and (fromsize=OS_S16) then
  88. list.Concat(taicpu.op_reg_reg(A_SEXT_H,reg2,reg1))
  89. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tosize=OS_S16) and (tcgsize2unsigned[fromsize]=OS_64) then
  90. list.Concat(taicpu.op_reg_reg(A_SEXT_H,reg2,reg1))
  91. else if (tosize=OS_S32) and (fromsize=OS_32) then
  92. list.Concat(taicpu.op_reg_reg_const(A_ADDIW,reg2,reg1,0))
  93. else if (tcgsize2unsigned[tosize]=OS_64) and (fromsize=OS_8) then
  94. list.Concat(taicpu.op_reg_reg_const(A_ANDI,reg2,reg1,$FF))
  95. else if (tosize=OS_8) and (fromsize<>OS_8) then
  96. list.Concat(taicpu.op_reg_reg_const(A_ANDI,reg2,reg1,$FF))
  97. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tcgsize2unsigned[tosize]=OS_64) and (fromsize=OS_16) then
  98. list.Concat(taicpu.op_reg_reg(A_ZEXT_H,reg2,reg1))
  99. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tosize=OS_16) and (fromsize<>OS_16) then
  100. list.Concat(taicpu.op_reg_reg(A_ZEXT_H,reg2,reg1))
  101. else if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  102. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and (fromsize <> tosize)) or
  103. { do we need to mask out the sign when loading from smaller signed to larger unsigned type? }
  104. ((tcgsize2unsigned[fromsize]<>fromsize) and ((tcgsize2unsigned[tosize]=tosize)) and
  105. (tcgsize2size[fromsize] < tcgsize2size[tosize]) and (tcgsize2size[tosize] <> sizeof(pint)) ) then
  106. begin
  107. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  108. begin
  109. list.Concat(taicpu.op_reg_reg_const(A_SLLI,reg2,reg1,8*(8-tcgsize2size[fromsize])));
  110. if tcgsize2unsigned[fromsize]<>fromsize then
  111. list.Concat(taicpu.op_reg_reg_const(A_SRAI,reg2,reg2,8*(tcgsize2size[tosize]-tcgsize2size[fromsize])))
  112. else
  113. list.Concat(taicpu.op_reg_reg_const(A_SRLI,reg2,reg2,8*(tcgsize2size[tosize]-tcgsize2size[fromsize])));
  114. end
  115. else if tcgsize2unsigned[tosize]<>OS_64 then
  116. list.Concat(taicpu.op_reg_reg_const(A_SLLI,reg2,reg1,8*(8-tcgsize2size[tosize])))
  117. else
  118. a_load_reg_reg(list,tosize,tosize,reg1,reg2);
  119. if tcgsize2unsigned[tosize]=tosize then
  120. list.Concat(taicpu.op_reg_reg_const(A_SRLI,reg2,reg2,8*(8-tcgsize2size[tosize])))
  121. else
  122. list.Concat(taicpu.op_reg_reg_const(A_SRAI,reg2,reg2,8*(8-tcgsize2size[tosize])));
  123. end
  124. else
  125. begin
  126. ai:=taicpu.op_reg_reg_const(A_ADDI,reg2,reg1,0);
  127. list.concat(ai);
  128. rg[R_INTREGISTER].add_move_instruction(ai);
  129. end;
  130. end;
  131. procedure tcgrv64.a_load_const_reg(list: TAsmList; size: tcgsize; a: tcgint; register: tregister);
  132. var
  133. l: TAsmLabel;
  134. hr: treference;
  135. begin
  136. if a=0 then
  137. a_load_reg_reg(list,size,size,NR_X0,register)
  138. else
  139. begin
  140. if is_imm12(a) then
  141. list.concat(taicpu.op_reg_reg_const(A_ADDI,register,NR_X0,a))
  142. else if is_lui_imm(a) then
  143. list.concat(taicpu.op_reg_const(A_LUI,register,(a shr 12) and $FFFFF))
  144. else if (int64(longint(a))=a) then
  145. begin
  146. if (a and $800)<>0 then
  147. list.concat(taicpu.op_reg_const(A_LUI,register,((a shr 12)+1) and $FFFFF))
  148. else
  149. list.concat(taicpu.op_reg_const(A_LUI,register,(a shr 12) and $FFFFF));
  150. list.concat(taicpu.op_reg_reg_const(A_ADDIW,register,register,SarSmallint(smallint(a shl 4),4)));
  151. end
  152. else
  153. begin
  154. reference_reset(hr,8,[]);
  155. current_asmdata.getjumplabel(l);
  156. current_procinfo.aktlocaldata.Concat(cai_align.Create(8));
  157. cg.a_label(current_procinfo.aktlocaldata,l);
  158. hr.symboldata:=current_procinfo.aktlocaldata.last;
  159. current_procinfo.aktlocaldata.concat(tai_const.Create_64bit(a));
  160. hr.symbol:=l;
  161. hr.refaddr:=addr_pcrel_hi20;
  162. current_asmdata.getjumplabel(l);
  163. a_label(list,l);
  164. list.concat(taicpu.op_reg_ref(A_AUIPC,register,hr));
  165. reference_reset_symbol(hr,l,0,0,[]);
  166. hr.refaddr:=addr_pcrel_lo12;
  167. hr.base:=register;
  168. list.concat(taicpu.op_reg_ref(A_LD,register,hr));
  169. end;
  170. end;
  171. end;
  172. procedure tcgrv64.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  173. var
  174. signed: Boolean;
  175. l: TAsmLabel;
  176. tmpreg, tmpreg0: tregister;
  177. ai: taicpu;
  178. begin
  179. signed:=tcgsize2unsigned[size]<>size;
  180. ovloc.loc:=LOC_VOID;
  181. if setflags then
  182. case op of
  183. OP_ADD:
  184. begin
  185. current_asmdata.getjumplabel(l);
  186. list.Concat(taicpu.op_reg_reg_reg(A_ADD,dst,src2,src1));
  187. if signed then
  188. begin
  189. {
  190. t0=src1<0
  191. t1=result<src2
  192. overflow if t0<>t1
  193. }
  194. tmpreg0:=getintregister(list,OS_INT);
  195. tmpreg:=getintregister(list,OS_INT);
  196. list.Concat(taicpu.op_reg_reg_reg(A_SLT,tmpreg0,src1,NR_X0));
  197. list.Concat(taicpu.op_reg_reg_reg(A_SLT,tmpreg,dst,src2));
  198. ai:=taicpu.op_reg_reg_sym_ofs(A_Bxx,tmpreg,tmpreg0,l,0);
  199. ai.condition:=C_EQ;
  200. list.concat(ai);
  201. end
  202. else
  203. begin
  204. {
  205. jump if sum>=x
  206. }
  207. if size in [OS_S32,OS_32] then
  208. begin
  209. tmpreg:=getintregister(list,OS_INT);
  210. a_load_reg_reg(list,size,OS_64,dst,tmpreg);
  211. dst:=tmpreg;
  212. end;
  213. ai:=taicpu.op_reg_reg_sym_ofs(A_Bxx,dst,src2,l,0);
  214. ai.condition:=C_GEU;
  215. list.concat(ai);
  216. end;
  217. a_call_name(list,'FPC_OVERFLOW',false);
  218. a_label(list,l);
  219. end;
  220. OP_SUB:
  221. begin
  222. current_asmdata.getjumplabel(l);
  223. if size in [OS_S32,OS_32] then
  224. list.Concat(taicpu.op_reg_reg_reg(A_SUBW,dst,src2,src1))
  225. else
  226. list.Concat(taicpu.op_reg_reg_reg(A_SUB,dst,src2,src1));
  227. if signed then
  228. begin
  229. tmpreg0:=getintregister(list,OS_INT);
  230. tmpreg:=getintregister(list,OS_INT);
  231. list.Concat(taicpu.op_reg_reg_reg(A_SLT,tmpreg0,NR_X0,src1));
  232. list.Concat(taicpu.op_reg_reg_reg(A_SLT,tmpreg,dst,src2));
  233. ai:=taicpu.op_reg_reg_sym_ofs(A_Bxx,tmpreg,tmpreg0,l,0);
  234. ai.condition:=C_EQ;
  235. list.concat(ai);
  236. end
  237. else
  238. begin
  239. { no overflow if result<=src2 }
  240. if size in [OS_S32,OS_32] then
  241. begin
  242. tmpreg:=getintregister(list,OS_INT);
  243. a_load_reg_reg(list,size,OS_64,dst,tmpreg);
  244. dst:=tmpreg;
  245. end;
  246. ai:=taicpu.op_reg_reg_sym_ofs(A_Bxx,src2,dst,l,0);
  247. ai.condition:=C_GEU;
  248. list.concat(ai);
  249. end;
  250. a_call_name(list,'FPC_OVERFLOW',false);
  251. a_label(list,l);
  252. end;
  253. OP_IMUL:
  254. begin
  255. { No overflow if upper result is same as sign of result }
  256. current_asmdata.getjumplabel(l);
  257. tmpreg:=getintregister(list,OS_INT);
  258. tmpreg0:=getintregister(list,OS_INT);
  259. list.Concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2));
  260. list.Concat(taicpu.op_reg_reg_reg(A_MULH,tmpreg,src1,src2));
  261. list.concat(taicpu.op_reg_reg_const(A_SRAI,tmpreg0,dst,63));
  262. a_cmp_reg_reg_label(list,OS_INT,OC_EQ,tmpreg,tmpreg0,l);
  263. a_call_name(list,'FPC_OVERFLOW',false);
  264. a_label(list,l);
  265. end;
  266. OP_MUL:
  267. begin
  268. { No overflow if upper result is 0 }
  269. current_asmdata.getjumplabel(l);
  270. tmpreg:=getintregister(list,OS_INT);
  271. list.Concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2));
  272. list.Concat(taicpu.op_reg_reg_reg(A_MULHU,tmpreg,src1,src2));
  273. a_cmp_reg_reg_label(list,OS_INT,OC_EQ,tmpreg,NR_X0,l);
  274. a_call_name(list,'FPC_OVERFLOW',false);
  275. a_label(list,l);
  276. end;
  277. OP_IDIV:
  278. begin
  279. { Only overflow if dst is all 1's }
  280. current_asmdata.getjumplabel(l);
  281. tmpreg:=getintregister(list,OS_INT);
  282. list.Concat(taicpu.op_reg_reg_reg(A_DIV,dst,src1,src2));
  283. list.Concat(taicpu.op_reg_reg_const(A_ADDI,tmpreg,dst,1));
  284. a_cmp_reg_reg_label(list,OS_INT,OC_NE,tmpreg,NR_X0,l);
  285. a_call_name(list,'FPC_OVERFLOW',false);
  286. a_label(list,l);
  287. end;
  288. else
  289. internalerror(2019051032);
  290. end
  291. else
  292. inherited a_op_reg_reg_reg_checkoverflow(list,op,size,src1,src2,dst,false,ovloc);
  293. end;
  294. procedure tcgrv64.g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef);
  295. begin
  296. end;
  297. procedure tcgrv64.g_concatcopy(list: TAsmList; const source, dest: treference; len: aint);
  298. var
  299. tmpreg1, hreg, countreg: TRegister;
  300. src, dst, src2, dst2: TReference;
  301. lab: tasmlabel;
  302. Count, count2: aint;
  303. begin
  304. src2:=source;
  305. fixref(list,src2);
  306. dst2:=dest;
  307. fixref(list,dst2);
  308. if len > high(longint) then
  309. internalerror(2002072704);
  310. { A call (to FPC_MOVE) requires the outgoing parameter area to be properly
  311. allocated on stack. This can only be done before tmipsprocinfo.set_first_temp_offset,
  312. i.e. before secondpass. Other internal procedures request correct stack frame
  313. by setting pi_do_call during firstpass, but for this particular one it is impossible.
  314. Therefore, if the current procedure is a leaf one, we have to leave it that way. }
  315. { anybody wants to determine a good value here :)? }
  316. if (len > 100) and
  317. assigned(current_procinfo) and
  318. (pi_do_call in current_procinfo.flags) then
  319. g_concatcopy_move(list, src2, dst2, len)
  320. else
  321. begin
  322. Count := len div 8;
  323. reference_reset(src,sizeof(aint),[]);
  324. { load the address of src2 into src.base }
  325. src.base := GetAddressRegister(list);
  326. a_loadaddr_ref_reg(list, src2, src.base);
  327. reference_reset(dst,sizeof(aint),[]);
  328. { load the address of dst2 into dst.base }
  329. dst.base := GetAddressRegister(list);
  330. a_loadaddr_ref_reg(list, dst2, dst.base);
  331. { generate a loop }
  332. if Count > 4 then
  333. begin
  334. countreg := GetIntRegister(list, OS_INT);
  335. tmpreg1 := GetIntRegister(list, OS_INT);
  336. a_load_const_reg(list, OS_INT, Count, countreg);
  337. current_asmdata.getjumplabel(lab);
  338. a_label(list, lab);
  339. list.concat(taicpu.op_reg_ref(A_LD, tmpreg1, src));
  340. list.concat(taicpu.op_reg_ref(A_SD, tmpreg1, dst));
  341. list.concat(taicpu.op_reg_reg_const(A_ADDI, src.base, src.base, 8));
  342. list.concat(taicpu.op_reg_reg_const(A_ADDI, dst.base, dst.base, 8));
  343. list.concat(taicpu.op_reg_reg_const(A_ADDI, countreg, countreg, -1));
  344. a_cmp_reg_reg_label(list,OS_INT,OC_GT,NR_X0,countreg,lab);
  345. len := len mod 8;
  346. end;
  347. { unrolled loop }
  348. Count := len div 8;
  349. if Count > 0 then
  350. begin
  351. tmpreg1 := GetIntRegister(list, OS_INT);
  352. count2 := 1;
  353. while count2 <= Count do
  354. begin
  355. list.concat(taicpu.op_reg_ref(A_LD, tmpreg1, src));
  356. list.concat(taicpu.op_reg_ref(A_SD, tmpreg1, dst));
  357. Inc(src.offset, 8);
  358. Inc(dst.offset, 8);
  359. Inc(count2);
  360. end;
  361. len := len mod 8;
  362. end;
  363. if (len and 4) <> 0 then
  364. begin
  365. hreg := GetIntRegister(list, OS_INT);
  366. a_load_ref_reg(list, OS_32, OS_32, src, hreg);
  367. a_load_reg_ref(list, OS_32, OS_32, hreg, dst);
  368. Inc(src.offset, 4);
  369. Inc(dst.offset, 4);
  370. end;
  371. { copy the leftovers }
  372. if (len and 2) <> 0 then
  373. begin
  374. hreg := GetIntRegister(list, OS_INT);
  375. a_load_ref_reg(list, OS_16, OS_16, src, hreg);
  376. a_load_reg_ref(list, OS_16, OS_16, hreg, dst);
  377. Inc(src.offset, 2);
  378. Inc(dst.offset, 2);
  379. end;
  380. if (len and 1) <> 0 then
  381. begin
  382. hreg := GetIntRegister(list, OS_INT);
  383. a_load_ref_reg(list, OS_8, OS_8, src, hreg);
  384. a_load_reg_ref(list, OS_8, OS_8, hreg, dst);
  385. end;
  386. end;
  387. end;
  388. procedure create_codegen;
  389. begin
  390. cg := tcgrv64.create;
  391. cg128:=tcg128.create;
  392. end;
  393. end.