cgcpu.pas 25 KB

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