cgcpu.pas 19 KB

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