cgcpu.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean); override;
  37. procedure g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean); override;
  38. procedure g_concatcopy_move(list: tasmlist; const Source, dest: treference; len: tcgint);
  39. procedure g_concatcopy(list: TAsmList; const source, dest: treference; len: aint); override;
  40. end;
  41. procedure create_codegen;
  42. implementation
  43. uses
  44. sysutils, cclasses,
  45. globals, verbose, systems, cutils,
  46. symconst, fmodule, symtable,
  47. rgobj, tgobj, cpupi, procinfo, paramgr, cpupara;
  48. { Range check must be disabled explicitly as conversions between signed and unsigned
  49. 64-bit and 32-bit values are done without explicit typecasts }
  50. {$R-}
  51. procedure tcgrv64.init_register_allocators;
  52. begin
  53. inherited init_register_allocators;
  54. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  55. [RS_X10,RS_X11,RS_X12,RS_X13,RS_X14,RS_X15,RS_X16,RS_X17,
  56. RS_X31,RS_X30,RS_X29,RS_X28,
  57. RS_X5,RS_X6,RS_X7,
  58. RS_X9,RS_X27,RS_X26,RS_X25,RS_X24,RS_X23,RS_X22,
  59. RS_X21,RS_X20,RS_X19,RS_X18],first_int_imreg,[]);
  60. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  61. [RS_F10,RS_F11,RS_F12,RS_F13,RS_F14,RS_F15,RS_F16,RS_F17,
  62. RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,
  63. RS_F28,RS_F29,RS_F30,RS_F31,
  64. RS_F8,RS_F9,
  65. RS_F27,
  66. RS_F26,RS_F25,RS_F24,RS_F23,RS_F22,RS_F21,RS_F20,RS_F19,RS_F18],first_fpu_imreg,[]);
  67. end;
  68. procedure tcgrv64.done_register_allocators;
  69. begin
  70. rg[R_INTREGISTER].free;
  71. rg[R_FPUREGISTER].free;
  72. inherited done_register_allocators;
  73. end;
  74. procedure tcgrv64.a_load_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  75. var
  76. ai: taicpu;
  77. begin
  78. list.concat(tai_comment.Create(strpnew('Move '+tcgsize2str(fromsize)+'->'+tcgsize2str(tosize))));
  79. if (tcgsize2unsigned[tosize]=OS_64) and (fromsize=OS_S32) then
  80. list.Concat(taicpu.op_reg_reg_const(A_ADDIW,reg2,reg1,0))
  81. else if (tosize=OS_S32) and (tcgsize2unsigned[fromsize]=OS_64) then
  82. list.Concat(taicpu.op_reg_reg_const(A_ADDIW,reg2,reg1,0))
  83. else if (tosize=OS_S32) and (fromsize=OS_32) then
  84. list.Concat(taicpu.op_reg_reg_const(A_ADDIW,reg2,reg1,0))
  85. else if (tcgsize2unsigned[tosize]=OS_64) and (fromsize=OS_8) then
  86. list.Concat(taicpu.op_reg_reg_const(A_ANDI,reg2,reg1,$FF))
  87. else if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  88. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and (fromsize <> tosize)) or
  89. { do we need to mask out the sign when loading from smaller signed to larger unsigned type? }
  90. ((tcgsize2unsigned[fromsize]<>fromsize) and ((tcgsize2unsigned[tosize]=tosize)) and
  91. (tcgsize2size[fromsize] < tcgsize2size[tosize]) and (tcgsize2size[tosize] <> sizeof(pint)) ) then
  92. begin
  93. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  94. begin
  95. list.Concat(taicpu.op_reg_reg_const(A_SLLI,reg2,reg1,8*(8-tcgsize2size[fromsize])));
  96. if tcgsize2unsigned[fromsize]<>fromsize then
  97. list.Concat(taicpu.op_reg_reg_const(A_SRAI,reg2,reg2,8*(tcgsize2size[tosize]-tcgsize2size[fromsize])))
  98. else
  99. list.Concat(taicpu.op_reg_reg_const(A_SRLI,reg2,reg2,8*(tcgsize2size[tosize]-tcgsize2size[fromsize])));
  100. end
  101. else if tcgsize2unsigned[tosize]<>OS_64 then
  102. list.Concat(taicpu.op_reg_reg_const(A_SLLI,reg2,reg1,8*(8-tcgsize2size[tosize])))
  103. else
  104. a_load_reg_reg(list,tosize,tosize,reg1,reg2);
  105. if tcgsize2unsigned[tosize]=tosize then
  106. list.Concat(taicpu.op_reg_reg_const(A_SRLI,reg2,reg2,8*(8-tcgsize2size[tosize])))
  107. else
  108. list.Concat(taicpu.op_reg_reg_const(A_SRAI,reg2,reg2,8*(8-tcgsize2size[tosize])));
  109. end
  110. else
  111. begin
  112. ai:=taicpu.op_reg_reg_const(A_ADDI,reg2,reg1,0);
  113. list.concat(ai);
  114. rg[R_INTREGISTER].add_move_instruction(ai);
  115. end;
  116. end;
  117. procedure tcgrv64.a_load_const_reg(list: TAsmList; size: tcgsize; a: tcgint; register: tregister);
  118. var
  119. l: TAsmLabel;
  120. hr: treference;
  121. begin
  122. if a=0 then
  123. a_load_reg_reg(list,size,size,NR_X0,register)
  124. else
  125. begin
  126. if is_imm12(a) then
  127. list.concat(taicpu.op_reg_reg_const(A_ADDI,register,NR_X0,a))
  128. else if is_lui_imm(a) then
  129. list.concat(taicpu.op_reg_const(A_LUI,register,(a shr 12) and $FFFFF))
  130. else if (int64(longint(a))=a) then
  131. begin
  132. if (a and $800)<>0 then
  133. list.concat(taicpu.op_reg_const(A_LUI,register,((a shr 12)+1) and $FFFFF))
  134. else
  135. list.concat(taicpu.op_reg_const(A_LUI,register,(a shr 12) and $FFFFF));
  136. list.concat(taicpu.op_reg_reg_const(A_ADDIW,register,register,SarSmallint(smallint(a shl 4),4)));
  137. end
  138. else
  139. begin
  140. reference_reset(hr,8,[]);
  141. current_asmdata.getjumplabel(l);
  142. current_procinfo.aktlocaldata.Concat(cai_align.Create(8));
  143. cg.a_label(current_procinfo.aktlocaldata,l);
  144. hr.symboldata:=current_procinfo.aktlocaldata.last;
  145. current_procinfo.aktlocaldata.concat(tai_const.Create_64bit(a));
  146. hr.symbol:=l;
  147. hr.refaddr:=addr_pcrel_hi20;
  148. current_asmdata.getjumplabel(l);
  149. a_label(list,l);
  150. list.concat(taicpu.op_reg_ref(A_AUIPC,register,hr));
  151. reference_reset_symbol(hr,l,0,0,[]);
  152. hr.refaddr:=addr_pcrel_lo12;
  153. hr.base:=register;
  154. list.concat(taicpu.op_reg_ref(A_LD,register,hr));
  155. end;
  156. end;
  157. end;
  158. procedure tcgrv64.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  159. var
  160. signed: Boolean;
  161. l: TAsmLabel;
  162. tmpreg: tregister;
  163. ai: taicpu;
  164. begin
  165. if setflags then
  166. begin
  167. tmpreg:=getintregister(list,size);
  168. a_load_const_reg(list,size,a,tmpreg);
  169. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  170. end
  171. else
  172. a_op_const_reg_reg(list,op,size,a,src,dst);
  173. end;
  174. procedure tcgrv64.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  175. var
  176. signed: Boolean;
  177. l: TAsmLabel;
  178. tmpreg, tmpreg0: tregister;
  179. ai: taicpu;
  180. begin
  181. signed:=tcgsize2unsigned[size]<>size;
  182. if setflags then
  183. case op of
  184. OP_ADD:
  185. begin
  186. current_asmdata.getjumplabel(l);
  187. list.Concat(taicpu.op_reg_reg_reg(A_ADD,dst,src2,src1));
  188. if signed then
  189. begin
  190. {
  191. t0=src1<0
  192. t1=result<src2
  193. overflow if t0<>t1
  194. }
  195. tmpreg0:=getintregister(list,OS_INT);
  196. tmpreg:=getintregister(list,OS_INT);
  197. list.Concat(taicpu.op_reg_reg_reg(A_SLT,tmpreg0,src1,NR_X0));
  198. list.Concat(taicpu.op_reg_reg_reg(A_SLT,tmpreg,dst,src2));
  199. ai:=taicpu.op_reg_reg_sym_ofs(A_Bxx,tmpreg,tmpreg0,l,0);
  200. ai.condition:=C_EQ;
  201. list.concat(ai);
  202. end
  203. else
  204. begin
  205. {
  206. jump if sum>=x
  207. }
  208. if size in [OS_S32,OS_32] then
  209. begin
  210. tmpreg:=getintregister(list,OS_INT);
  211. a_load_reg_reg(list,size,OS_64,dst,tmpreg);
  212. dst:=tmpreg;
  213. end;
  214. ai:=taicpu.op_reg_reg_sym_ofs(A_Bxx,dst,src2,l,0);
  215. ai.condition:=C_GEU;
  216. list.concat(ai);
  217. end;
  218. a_call_name(list,'FPC_OVERFLOW',false);
  219. a_label(list,l);
  220. end;
  221. OP_SUB:
  222. begin
  223. current_asmdata.getjumplabel(l);
  224. list.Concat(taicpu.op_reg_reg_reg(A_SUB,dst,src2,src1));
  225. if signed then
  226. begin
  227. tmpreg0:=getintregister(list,OS_INT);
  228. tmpreg:=getintregister(list,OS_INT);
  229. list.Concat(taicpu.op_reg_reg_reg(A_SLT,tmpreg0,NR_X0,src1));
  230. list.Concat(taicpu.op_reg_reg_reg(A_SLT,tmpreg,dst,src2));
  231. ai:=taicpu.op_reg_reg_sym_ofs(A_Bxx,tmpreg,tmpreg0,l,0);
  232. ai.condition:=C_EQ;
  233. list.concat(ai);
  234. end
  235. else
  236. begin
  237. { no overflow if result<=src2 }
  238. if size in [OS_S32,OS_32] then
  239. begin
  240. tmpreg:=getintregister(list,OS_INT);
  241. a_load_reg_reg(list,size,OS_64,dst,tmpreg);
  242. dst:=tmpreg;
  243. end;
  244. ai:=taicpu.op_reg_reg_sym_ofs(A_Bxx,src2,dst,l,0);
  245. ai.condition:=C_GEU;
  246. list.concat(ai);
  247. end;
  248. a_call_name(list,'FPC_OVERFLOW',false);
  249. a_label(list,l);
  250. end;
  251. OP_IMUL:
  252. begin
  253. { No overflow if upper result is same as sign of result }
  254. current_asmdata.getjumplabel(l);
  255. tmpreg:=getintregister(list,OS_INT);
  256. tmpreg0:=getintregister(list,OS_INT);
  257. list.Concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2));
  258. list.Concat(taicpu.op_reg_reg_reg(A_MULH,tmpreg,src1,src2));
  259. list.concat(taicpu.op_reg_reg_const(A_SRAI,tmpreg0,dst,63));
  260. a_cmp_reg_reg_label(list,OS_INT,OC_EQ,tmpreg,tmpreg0,l);
  261. a_call_name(list,'FPC_OVERFLOW',false);
  262. a_label(list,l);
  263. end;
  264. OP_MUL:
  265. begin
  266. { No overflow if upper result is 0 }
  267. current_asmdata.getjumplabel(l);
  268. tmpreg:=getintregister(list,OS_INT);
  269. list.Concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2));
  270. list.Concat(taicpu.op_reg_reg_reg(A_MULHU,tmpreg,src1,src2));
  271. a_cmp_reg_reg_label(list,OS_INT,OC_EQ,tmpreg,NR_X0,l);
  272. a_call_name(list,'FPC_OVERFLOW',false);
  273. a_label(list,l);
  274. end;
  275. OP_IDIV:
  276. begin
  277. { Only overflow if dst is all 1's }
  278. current_asmdata.getjumplabel(l);
  279. tmpreg:=getintregister(list,OS_INT);
  280. list.Concat(taicpu.op_reg_reg_reg(A_DIV,dst,src1,src2));
  281. list.Concat(taicpu.op_reg_reg_const(A_ADDI,tmpreg,dst,1));
  282. a_cmp_reg_reg_label(list,OS_INT,OC_NE,tmpreg,NR_X0,l);
  283. a_call_name(list,'FPC_OVERFLOW',false);
  284. a_label(list,l);
  285. end;
  286. else
  287. internalerror(2019051032);
  288. end
  289. else
  290. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  291. end;
  292. procedure tcgrv64.g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef);
  293. begin
  294. end;
  295. procedure tcgrv64.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  296. var
  297. regs, fregs: tcpuregisterset;
  298. r: TSuperRegister;
  299. href: treference;
  300. stackcount, stackAdjust: longint;
  301. begin
  302. if not(nostackframe) then
  303. begin
  304. a_reg_alloc(list,NR_STACK_POINTER_REG);
  305. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  306. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  307. reference_reset_base(href,NR_STACK_POINTER_REG,-8,ctempposinvalid,0,[]);
  308. { Int registers }
  309. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  310. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  311. regs:=regs+[RS_FRAME_POINTER_REG,RS_RETURN_ADDRESS_REG];
  312. if (pi_do_call in current_procinfo.flags) then
  313. regs:=regs+[RS_RETURN_ADDRESS_REG];
  314. stackcount:=0;
  315. for r:=RS_X0 to RS_X31 do
  316. if r in regs then
  317. inc(stackcount,8);
  318. { Float registers }
  319. fregs:=rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  320. for r:=RS_F0 to RS_F31 do
  321. if r in fregs then
  322. inc(stackcount,8);
  323. inc(localsize,stackcount);
  324. if not is_imm12(-(localsize-stackcount)) then
  325. begin
  326. if not (RS_RETURN_ADDRESS_REG in regs) then
  327. begin
  328. include(regs,RS_RETURN_ADDRESS_REG);
  329. inc(localsize,8);
  330. inc(stackcount,8);
  331. end;
  332. end;
  333. stackAdjust:=0;
  334. if (CPURV_HAS_COMPACT in cpu_capabilities[current_settings.cputype]) and
  335. (stackcount>0) then
  336. begin
  337. list.concat(taicpu.op_reg_reg_const(A_ADDI,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,-stackcount));
  338. inc(href.offset,stackcount);
  339. stackAdjust:=stackcount;
  340. dec(localsize,stackcount);
  341. end;
  342. for r:=RS_X0 to RS_X31 do
  343. if r in regs then
  344. begin
  345. list.concat(taicpu.op_reg_ref(A_SD,newreg(R_INTREGISTER,r,R_SUBWHOLE),href));
  346. dec(href.offset,8);
  347. end;
  348. { Float registers }
  349. for r:=RS_F0 to RS_F31 do
  350. if r in fregs then
  351. begin
  352. list.concat(taicpu.op_reg_ref(A_FSD,newreg(R_FPUREGISTER,r,R_SUBWHOLE),href));
  353. dec(href.offset,8);
  354. end;
  355. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  356. list.concat(taicpu.op_reg_reg_const(A_ADDI,NR_FRAME_POINTER_REG,NR_STACK_POINTER_REG,stackAdjust));
  357. if localsize>0 then
  358. begin
  359. localsize:=align(localsize,8);
  360. if is_imm12(-localsize) then
  361. list.concat(taicpu.op_reg_reg_const(A_ADDI,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,-localsize))
  362. else
  363. begin
  364. a_load_const_reg(list,OS_INT,localsize,NR_RETURN_ADDRESS_REG);
  365. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_RETURN_ADDRESS_REG));
  366. end;
  367. end;
  368. end;
  369. end;
  370. procedure tcgrv64.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  371. var
  372. r: tsuperregister;
  373. regs, fregs: tcpuregisterset;
  374. localsize: longint;
  375. href: treference;
  376. begin
  377. if not(nostackframe) then
  378. begin
  379. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  380. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  381. regs:=regs+[RS_FRAME_POINTER_REG,RS_RETURN_ADDRESS_REG];
  382. if (pi_do_call in current_procinfo.flags) then
  383. regs:=regs+[RS_RETURN_ADDRESS_REG];
  384. reference_reset_base(href,NR_STACK_POINTER_REG,-8,ctempposinvalid,0,[]);
  385. for r:=RS_X31 downto RS_X0 do
  386. if r in regs then
  387. dec(href.offset,8);
  388. { Float registers }
  389. fregs:=rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  390. for r:=RS_F0 to RS_F31 do
  391. if r in fregs then
  392. dec(href.offset,8);
  393. localsize:=current_procinfo.calc_stackframe_size+(-href.offset-8);
  394. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  395. list.concat(taicpu.op_reg_reg_const(A_ADDI,NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG,0))
  396. else if localsize>0 then
  397. begin
  398. localsize:=align(localsize,8);
  399. if is_imm12(localsize) then
  400. list.concat(taicpu.op_reg_reg_const(A_ADDI,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize))
  401. else
  402. begin
  403. if not (RS_RETURN_ADDRESS_REG in regs) then
  404. begin
  405. include(regs,RS_RETURN_ADDRESS_REG);
  406. dec(href.offset,8);
  407. inc(localsize,8);
  408. end;
  409. a_load_const_reg(list,OS_INT,localsize,NR_RETURN_ADDRESS_REG);
  410. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_RETURN_ADDRESS_REG));
  411. end;
  412. end;
  413. { Float registers }
  414. for r:=RS_F31 downto RS_F0 do
  415. if r in fregs then
  416. begin
  417. inc(href.offset,8);
  418. list.concat(taicpu.op_reg_ref(A_FLD,newreg(R_FPUREGISTER,r,R_SUBWHOLE),href));
  419. end;
  420. for r:=RS_X31 downto RS_X0 do
  421. if r in regs then
  422. begin
  423. inc(href.offset,8);
  424. list.concat(taicpu.op_reg_ref(A_LD,newreg(R_INTREGISTER,r,R_SUBWHOLE),href));
  425. end;
  426. end;
  427. list.concat(taicpu.op_reg_reg(A_JALR,NR_X0,NR_RETURN_ADDRESS_REG));
  428. end;
  429. procedure tcgrv64.g_concatcopy_move(list: tasmlist; const Source, dest: treference; len: tcgint);
  430. var
  431. paraloc1, paraloc2, paraloc3: TCGPara;
  432. pd: tprocdef;
  433. begin
  434. pd:=search_system_proc('MOVE');
  435. paraloc1.init;
  436. paraloc2.init;
  437. paraloc3.init;
  438. paramanager.getcgtempparaloc(list, pd, 1, paraloc1);
  439. paramanager.getcgtempparaloc(list, pd, 2, paraloc2);
  440. paramanager.getcgtempparaloc(list, pd, 3, paraloc3);
  441. a_load_const_cgpara(list, OS_SINT, len, paraloc3);
  442. a_loadaddr_ref_cgpara(list, dest, paraloc2);
  443. a_loadaddr_ref_cgpara(list, Source, paraloc1);
  444. paramanager.freecgpara(list, paraloc3);
  445. paramanager.freecgpara(list, paraloc2);
  446. paramanager.freecgpara(list, paraloc1);
  447. alloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  448. alloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  449. a_call_name(list, 'FPC_MOVE', false);
  450. dealloccpuregisters(list, R_FPUREGISTER, paramanager.get_volatile_registers_fpu(pocall_default));
  451. dealloccpuregisters(list, R_INTREGISTER, paramanager.get_volatile_registers_int(pocall_default));
  452. paraloc3.done;
  453. paraloc2.done;
  454. paraloc1.done;
  455. end;
  456. procedure tcgrv64.g_concatcopy(list: TAsmList; const source, dest: treference; len: aint);
  457. var
  458. tmpreg1, hreg, countreg: TRegister;
  459. src, dst, src2, dst2: TReference;
  460. lab: tasmlabel;
  461. Count, count2: aint;
  462. begin
  463. src2:=source;
  464. fixref(list,src2);
  465. dst2:=dest;
  466. fixref(list,dst2);
  467. if len > high(longint) then
  468. internalerror(2002072704);
  469. { A call (to FPC_MOVE) requires the outgoing parameter area to be properly
  470. allocated on stack. This can only be done before tmipsprocinfo.set_first_temp_offset,
  471. i.e. before secondpass. Other internal procedures request correct stack frame
  472. by setting pi_do_call during firstpass, but for this particular one it is impossible.
  473. Therefore, if the current procedure is a leaf one, we have to leave it that way. }
  474. { anybody wants to determine a good value here :)? }
  475. if (len > 100) and
  476. assigned(current_procinfo) and
  477. (pi_do_call in current_procinfo.flags) then
  478. g_concatcopy_move(list, src2, dst2, len)
  479. else
  480. begin
  481. Count := len div 8;
  482. reference_reset(src,sizeof(aint),[]);
  483. { load the address of src2 into src.base }
  484. src.base := GetAddressRegister(list);
  485. a_loadaddr_ref_reg(list, src2, src.base);
  486. reference_reset(dst,sizeof(aint),[]);
  487. { load the address of dst2 into dst.base }
  488. dst.base := GetAddressRegister(list);
  489. a_loadaddr_ref_reg(list, dst2, dst.base);
  490. { generate a loop }
  491. if Count > 4 then
  492. begin
  493. countreg := GetIntRegister(list, OS_INT);
  494. tmpreg1 := GetIntRegister(list, OS_INT);
  495. a_load_const_reg(list, OS_INT, Count, countreg);
  496. current_asmdata.getjumplabel(lab);
  497. a_label(list, lab);
  498. list.concat(taicpu.op_reg_ref(A_LD, tmpreg1, src));
  499. list.concat(taicpu.op_reg_ref(A_SD, tmpreg1, dst));
  500. list.concat(taicpu.op_reg_reg_const(A_ADDI, src.base, src.base, 8));
  501. list.concat(taicpu.op_reg_reg_const(A_ADDI, dst.base, dst.base, 8));
  502. list.concat(taicpu.op_reg_reg_const(A_ADDI, countreg, countreg, -1));
  503. a_cmp_reg_reg_label(list,OS_INT,OC_GT,NR_X0,countreg,lab);
  504. len := len mod 8;
  505. end;
  506. { unrolled loop }
  507. Count := len div 8;
  508. if Count > 0 then
  509. begin
  510. tmpreg1 := GetIntRegister(list, OS_INT);
  511. count2 := 1;
  512. while count2 <= Count do
  513. begin
  514. list.concat(taicpu.op_reg_ref(A_LD, tmpreg1, src));
  515. list.concat(taicpu.op_reg_ref(A_SD, tmpreg1, dst));
  516. Inc(src.offset, 8);
  517. Inc(dst.offset, 8);
  518. Inc(count2);
  519. end;
  520. len := len mod 8;
  521. end;
  522. if (len and 4) <> 0 then
  523. begin
  524. hreg := GetIntRegister(list, OS_INT);
  525. a_load_ref_reg(list, OS_32, OS_32, src, hreg);
  526. a_load_reg_ref(list, OS_32, OS_32, hreg, dst);
  527. Inc(src.offset, 4);
  528. Inc(dst.offset, 4);
  529. end;
  530. { copy the leftovers }
  531. if (len and 2) <> 0 then
  532. begin
  533. hreg := GetIntRegister(list, OS_INT);
  534. a_load_ref_reg(list, OS_16, OS_16, src, hreg);
  535. a_load_reg_ref(list, OS_16, OS_16, hreg, dst);
  536. Inc(src.offset, 2);
  537. Inc(dst.offset, 2);
  538. end;
  539. if (len and 1) <> 0 then
  540. begin
  541. hreg := GetIntRegister(list, OS_INT);
  542. a_load_ref_reg(list, OS_8, OS_8, src, hreg);
  543. a_load_reg_ref(list, OS_8, OS_8, hreg, dst);
  544. end;
  545. end;
  546. end;
  547. procedure create_codegen;
  548. begin
  549. cg := tcgrv64.create;
  550. cg128:=tcg128.create;
  551. end;
  552. end.