cgcpu.pas 25 KB

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