cgcpu.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the Risc-V32
  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,
  22. cgbase,cgobj,cgrv,
  23. aasmbase,aasmcpu,aasmtai,aasmdata,
  24. cpubase,cpuinfo,cgutils,cg64f32,rgcpu,
  25. parabase;
  26. type
  27. tcgrv32 = 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. { 32x32 to 64 bit multiplication }
  33. procedure a_mul_reg_reg_pair(list: TAsmList;size: tcgsize; src1,src2,dstlo,dsthi: tregister); override;
  34. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  35. end;
  36. tcg64frv = class(tcg64f32)
  37. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  38. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  39. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  40. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  41. procedure a_load64_ref_cgpara(list: TAsmList; const r: treference; const paraloc: tcgpara);override;
  42. procedure a_load64_ref_reg(list: TAsmList; const ref: treference; reg: tregister64);override;
  43. procedure a_load64_reg_ref(list: TAsmList; reg: tregister64; const ref: treference);override;
  44. end;
  45. procedure create_codegen;
  46. implementation
  47. uses
  48. symtable,
  49. globals,verbose,systems,cutils,
  50. symconst,symsym,fmodule,
  51. rgobj,tgobj,cpupi,procinfo,paramgr;
  52. {$undef AVOID_OVERFLOW}
  53. {$ifopt Q+}
  54. {$define AVOID_OVERFLOW}
  55. const
  56. max_12_bit = 1 shl 12;
  57. {$endif}
  58. { Range check must be disabled explicitly as conversions between signed and unsigned
  59. 32-bit values are done without explicit typecasts }
  60. {$R-}
  61. procedure tcgrv32.init_register_allocators;
  62. begin
  63. inherited init_register_allocators;
  64. if CPURV_HAS_16REGISTERS in cpu_capabilities[current_settings.cputype] then
  65. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  66. [RS_X10,RS_X11,RS_X12,RS_X13,RS_X14,RS_X15,
  67. RS_X5,RS_X6,RS_X7,
  68. RS_X3,RS_X4,
  69. RS_X9],first_int_imreg,[])
  70. else
  71. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  72. [RS_X10,RS_X11,RS_X12,RS_X13,RS_X14,RS_X15,RS_X16,RS_X17,
  73. RS_X31,RS_X30,RS_X29,RS_X28,
  74. RS_X5,RS_X6,RS_X7,
  75. RS_X3,RS_X4,
  76. RS_X9,RS_X27,RS_X26,RS_X25,RS_X24,RS_X23,RS_X22,
  77. RS_X21,RS_X20,RS_X19,RS_X18],first_int_imreg,[]);
  78. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  79. [RS_F10,RS_F11,RS_F12,RS_F13,RS_F14,RS_F15,RS_F16,RS_F17,
  80. RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,
  81. RS_F28,RS_F29,RS_F30,RS_F31,
  82. RS_F8,RS_F9,
  83. RS_F27,
  84. RS_F26,RS_F25,RS_F24,RS_F23,RS_F22,RS_F21,RS_F20,RS_F19,RS_F18],first_fpu_imreg,[]);
  85. end;
  86. procedure tcgrv32.done_register_allocators;
  87. begin
  88. rg[R_INTREGISTER].free;
  89. rg[R_FPUREGISTER].free;
  90. inherited done_register_allocators;
  91. end;
  92. procedure tcgrv32.a_load_reg_reg(list : TAsmList;fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  93. var
  94. ai: taicpu;
  95. begin
  96. {$ifdef EXTDEBUG}
  97. list.concat(tai_comment.Create(strpnew('Move '+tcgsize2str(fromsize)+'->'+tcgsize2str(tosize))));
  98. {$endif EXTDEBUG}
  99. if (tosize=OS_S32) and (fromsize=OS_32) then
  100. begin
  101. ai:=taicpu.op_reg_reg_const(A_ADDI,reg2,reg1,0);
  102. list.concat(ai);
  103. rg[R_INTREGISTER].add_move_instruction(ai);
  104. end
  105. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tcgsize2unsigned[tosize]=OS_32) and (fromsize=OS_S8) then
  106. list.Concat(taicpu.op_reg_reg(A_SEXT_B,reg2,reg1))
  107. else if (tcgsize2unsigned[tosize]=OS_32) and (fromsize=OS_8) then
  108. list.Concat(taicpu.op_reg_reg_const(A_ANDI,reg2,reg1,$FF))
  109. else if (tosize=OS_8) and (fromsize<>OS_8) then
  110. list.Concat(taicpu.op_reg_reg_const(A_ANDI,reg2,reg1,$FF))
  111. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tcgsize2unsigned[tosize]=OS_32) and (fromsize=OS_S16) then
  112. list.Concat(taicpu.op_reg_reg(A_SEXT_H,reg2,reg1))
  113. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tosize=OS_S16) and (tcgsize2unsigned[fromsize]=OS_32) then
  114. list.Concat(taicpu.op_reg_reg(A_SEXT_H,reg2,reg1))
  115. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tcgsize2unsigned[tosize]=OS_64) and (fromsize=OS_16) then
  116. list.Concat(taicpu.op_reg_reg(A_ZEXT_H,reg2,reg1))
  117. else if (CPURV_HAS_ZBB in cpu_capabilities[current_settings.cputype]) and (tosize=OS_16) and (fromsize<>OS_16) then
  118. list.Concat(taicpu.op_reg_reg(A_ZEXT_H,reg2,reg1))
  119. else if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  120. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and (fromsize <> tosize)) or
  121. { do we need to mask out the sign when loading from smaller signed to larger unsigned type? }
  122. ((tcgsize2unsigned[fromsize]<>fromsize) and ((tcgsize2unsigned[tosize]=tosize)) and
  123. (tcgsize2size[fromsize] < tcgsize2size[tosize]) and (tcgsize2size[tosize] <> sizeof(pint)) ) then
  124. begin
  125. if tcgsize2size[fromsize]<tcgsize2size[tosize] then
  126. begin
  127. list.Concat(taicpu.op_reg_reg_const(A_SLLI,reg2,reg1,8*(4-tcgsize2size[fromsize])));
  128. if tcgsize2unsigned[fromsize]<>fromsize then
  129. list.Concat(taicpu.op_reg_reg_const(A_SRAI,reg2,reg2,8*(tcgsize2size[tosize]-tcgsize2size[fromsize])))
  130. else
  131. list.Concat(taicpu.op_reg_reg_const(A_SRLI,reg2,reg2,8*(tcgsize2size[tosize]-tcgsize2size[fromsize])));
  132. end
  133. else
  134. list.Concat(taicpu.op_reg_reg_const(A_SLLI,reg2,reg1,8*(4-tcgsize2size[tosize])));
  135. if tcgsize2unsigned[tosize]=tosize then
  136. list.Concat(taicpu.op_reg_reg_const(A_SRLI,reg2,reg2,8*(4-tcgsize2size[tosize])))
  137. else
  138. list.Concat(taicpu.op_reg_reg_const(A_SRAI,reg2,reg2,8*(4-tcgsize2size[tosize])));
  139. end
  140. else
  141. begin
  142. ai:=taicpu.op_reg_reg_const(A_ADDI,reg2,reg1,0);
  143. list.concat(ai);
  144. rg[R_INTREGISTER].add_move_instruction(ai);
  145. end;
  146. end;
  147. procedure tcgrv32.a_mul_reg_reg_pair(list: TAsmList;size: tcgsize; src1,src2,dstlo,dsthi: tregister);
  148. var
  149. op: tasmop;
  150. begin
  151. case size of
  152. OS_INT: op:=A_MULHU;
  153. OS_SINT: op:=A_MULH;
  154. else
  155. InternalError(2014061501);
  156. end;
  157. if (dsthi<>NR_NO) then
  158. list.concat(taicpu.op_reg_reg_reg(op,dsthi,src1,src2));
  159. { low word is always unsigned }
  160. if (dstlo<>NR_NO) then
  161. list.concat(taicpu.op_reg_reg_reg(A_MUL,dstlo,src1,src2));
  162. end;
  163. procedure tcgrv32.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  164. var
  165. tmpreg1, hreg, countreg: TRegister;
  166. src, dst, src2, dst2: TReference;
  167. lab: tasmlabel;
  168. Count, count2: aint;
  169. function reference_is_reusable(const ref: treference): boolean;
  170. begin
  171. result:=(ref.base<>NR_NO) and (ref.index=NR_NO) and
  172. (ref.symbol=nil) and
  173. is_imm12(ref.offset);
  174. end;
  175. begin
  176. src2:=source;
  177. fixref(list,src2);
  178. dst2:=dest;
  179. fixref(list,dst2);
  180. if len > high(longint) then
  181. internalerror(2002072704);
  182. { A call (to FPC_MOVE) requires the outgoing parameter area to be properly
  183. allocated on stack. This can only be done before tmipsprocinfo.set_first_temp_offset,
  184. i.e. before secondpass. Other internal procedures request correct stack frame
  185. by setting pi_do_call during firstpass, but for this particular one it is impossible.
  186. Therefore, if the current procedure is a leaf one, we have to leave it that way. }
  187. { anybody wants to determine a good value here :)? }
  188. if (len > 100) and
  189. assigned(current_procinfo) and
  190. (pi_do_call in current_procinfo.flags) then
  191. g_concatcopy_move(list, src2, dst2, len)
  192. else
  193. begin
  194. Count := len div 4;
  195. if (count<=4) and reference_is_reusable(src2) then
  196. src:=src2
  197. else
  198. begin
  199. reference_reset(src,sizeof(aint),[]);
  200. { load the address of src2 into src.base }
  201. src.base := GetAddressRegister(list);
  202. a_loadaddr_ref_reg(list, src2, src.base);
  203. end;
  204. if (count<=4) and reference_is_reusable(dst2) then
  205. dst:=dst2
  206. else
  207. begin
  208. reference_reset(dst,sizeof(aint),[]);
  209. { load the address of dst2 into dst.base }
  210. dst.base := GetAddressRegister(list);
  211. a_loadaddr_ref_reg(list, dst2, dst.base);
  212. end;
  213. { generate a loop }
  214. if Count > 4 then
  215. begin
  216. countreg := GetIntRegister(list, OS_INT);
  217. tmpreg1 := GetIntRegister(list, OS_INT);
  218. a_load_const_reg(list, OS_INT, Count, countreg);
  219. current_asmdata.getjumplabel(lab);
  220. a_label(list, lab);
  221. list.concat(taicpu.op_reg_ref(A_LW, tmpreg1, src));
  222. list.concat(taicpu.op_reg_ref(A_SW, tmpreg1, dst));
  223. list.concat(taicpu.op_reg_reg_const(A_ADDI, src.base, src.base, 4));
  224. list.concat(taicpu.op_reg_reg_const(A_ADDI, dst.base, dst.base, 4));
  225. list.concat(taicpu.op_reg_reg_const(A_ADDI, countreg, countreg, -1));
  226. a_cmp_reg_reg_label(list,OS_INT,OC_GT,NR_X0,countreg,lab);
  227. len := len mod 4;
  228. end;
  229. { unrolled loop }
  230. Count := len div 4;
  231. if Count > 0 then
  232. begin
  233. tmpreg1 := GetIntRegister(list, OS_INT);
  234. for count2 := 1 to Count do
  235. begin
  236. list.concat(taicpu.op_reg_ref(A_LW, tmpreg1, src));
  237. list.concat(taicpu.op_reg_ref(A_SW, tmpreg1, dst));
  238. Inc(src.offset, 4);
  239. Inc(dst.offset, 4);
  240. end;
  241. len := len mod 4;
  242. end;
  243. if (len and 4) <> 0 then
  244. begin
  245. hreg := GetIntRegister(list, OS_INT);
  246. a_load_ref_reg(list, OS_32, OS_32, src, hreg);
  247. a_load_reg_ref(list, OS_32, OS_32, hreg, dst);
  248. Inc(src.offset, 4);
  249. Inc(dst.offset, 4);
  250. end;
  251. { copy the leftovers }
  252. if (len and 2) <> 0 then
  253. begin
  254. hreg := GetIntRegister(list, OS_INT);
  255. a_load_ref_reg(list, OS_16, OS_16, src, hreg);
  256. a_load_reg_ref(list, OS_16, OS_16, hreg, dst);
  257. Inc(src.offset, 2);
  258. Inc(dst.offset, 2);
  259. end;
  260. if (len and 1) <> 0 then
  261. begin
  262. hreg := GetIntRegister(list, OS_INT);
  263. a_load_ref_reg(list, OS_8, OS_8, src, hreg);
  264. a_load_reg_ref(list, OS_8, OS_8, hreg, dst);
  265. end;
  266. end;
  267. end;
  268. procedure tcg64frv.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  269. var
  270. tmpreg1: TRegister;
  271. begin
  272. case op of
  273. OP_NOT:
  274. begin
  275. cg.a_op_reg_reg(list,OP_NOT,OS_32,regsrc.reglo,regdst.reglo);
  276. cg.a_op_reg_reg(list,OP_NOT,OS_32,regsrc.reghi,regdst.reghi);
  277. end;
  278. OP_NEG:
  279. begin
  280. tmpreg1 := cg.GetIntRegister(list, OS_INT);
  281. list.concat(taicpu.op_reg_reg_reg(A_SUB, regdst.reglo, NR_X0, regsrc.reglo));
  282. list.concat(taicpu.op_reg_reg_reg(A_SLTU, tmpreg1, NR_X0, regdst.reglo));
  283. list.concat(taicpu.op_reg_reg_reg(A_SUB, regdst.reghi, NR_X0, regsrc.reghi));
  284. list.concat(taicpu.op_reg_reg_reg(A_SUB, regdst.reghi, regdst.reghi, tmpreg1));
  285. end;
  286. else
  287. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  288. end;
  289. end;
  290. procedure tcg64frv.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  291. begin
  292. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  293. end;
  294. procedure tcg64frv.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  295. var
  296. signed: Boolean;
  297. tmplo, carry, tmphi, hreg: TRegister;
  298. begin
  299. case op of
  300. OP_AND,OP_OR,OP_XOR:
  301. begin
  302. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  303. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  304. end;
  305. OP_ADD:
  306. begin
  307. signed:=(size in [OS_S64]);
  308. tmplo := cg.GetIntRegister(list,OS_S32);
  309. carry := cg.GetIntRegister(list,OS_S32);
  310. // destreg.reglo could be regsrc1.reglo or regsrc2.reglo
  311. list.concat(taicpu.op_reg_reg_reg(A_ADD, tmplo, regsrc2.reglo, regsrc1.reglo));
  312. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, tmplo, regsrc2.reglo));
  313. cg.a_load_reg_reg(list,OS_INT,OS_INT,tmplo,regdst.reglo);
  314. if signed then
  315. begin
  316. list.concat(taicpu.op_reg_reg_reg(A_ADD, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  317. list.concat(taicpu.op_reg_reg_reg(A_ADD, regdst.reghi, regdst.reghi, carry));
  318. end
  319. else
  320. begin
  321. tmphi:=cg.GetIntRegister(list,OS_INT);
  322. hreg:=cg.GetIntRegister(list,OS_INT);
  323. cg.a_load_const_reg(list,OS_INT,$80000000,hreg);
  324. // first add carry to one of the addends
  325. list.concat(taicpu.op_reg_reg_reg(A_ADD, tmphi, regsrc2.reghi, carry));
  326. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, tmphi, regsrc2.reghi));
  327. list.concat(taicpu.op_reg_reg_reg(A_SUB, carry, hreg, carry));
  328. // then add another addend
  329. list.concat(taicpu.op_reg_reg_reg(A_ADD, regdst.reghi, tmphi, regsrc1.reghi));
  330. end;
  331. end;
  332. OP_SUB:
  333. begin
  334. signed:=(size in [OS_S64]);
  335. tmplo := cg.GetIntRegister(list,OS_S32);
  336. carry := cg.GetIntRegister(list,OS_S32);
  337. // destreg.reglo could be regsrc1.reglo or regsrc2.reglo
  338. list.concat(taicpu.op_reg_reg_reg(A_SUB, tmplo, regsrc2.reglo, regsrc1.reglo));
  339. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, regsrc2.reglo,tmplo));
  340. cg.a_load_reg_reg(list,OS_INT,OS_INT,tmplo,regdst.reglo);
  341. if signed then
  342. begin
  343. list.concat(taicpu.op_reg_reg_reg(A_SUB, regdst.reghi, regsrc2.reghi, regsrc1.reghi));
  344. list.concat(taicpu.op_reg_reg_reg(A_SUB, regdst.reghi, regdst.reghi, carry));
  345. end
  346. else
  347. begin
  348. tmphi:=cg.GetIntRegister(list,OS_INT);
  349. hreg:=cg.GetIntRegister(list,OS_INT);
  350. cg.a_load_const_reg(list,OS_INT,$80000000,hreg);
  351. // first subtract the carry...
  352. list.concat(taicpu.op_reg_reg_reg(A_SUB, tmphi, regsrc2.reghi, carry));
  353. list.concat(taicpu.op_reg_reg_reg(A_SLTU, carry, regsrc2.reghi, tmphi));
  354. list.concat(taicpu.op_reg_reg_reg(A_SUB, carry, hreg, carry));
  355. // ...then the subtrahend
  356. list.concat(taicpu.op_reg_reg_reg(A_SUB, regdst.reghi, tmphi, regsrc1.reghi));
  357. end;
  358. end;
  359. else
  360. internalerror(2002072801);
  361. end;
  362. end;
  363. procedure tcg64frv.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  364. var
  365. tmplo,carry: TRegister;
  366. hisize: tcgsize;
  367. begin
  368. carry:=NR_NO;
  369. if (size in [OS_S64]) then
  370. hisize:=OS_S32
  371. else
  372. hisize:=OS_32;
  373. case op of
  374. OP_AND,OP_OR,OP_XOR:
  375. begin
  376. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  377. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  378. end;
  379. OP_ADD:
  380. begin
  381. if lo(value)<>0 then
  382. begin
  383. tmplo:=cg.GetIntRegister(list,OS_32);
  384. carry:=cg.GetIntRegister(list,OS_32);
  385. if is_imm12(aint(lo(value))) then
  386. list.concat(taicpu.op_reg_reg_const(A_ADDI,tmplo,regsrc.reglo,aint(lo(value))))
  387. else
  388. begin
  389. cg.a_load_const_reg(list,OS_INT,aint(lo(value)),tmplo);
  390. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmplo,tmplo,regsrc.reglo))
  391. end;
  392. list.concat(taicpu.op_reg_reg_reg(A_SLTU,carry,tmplo,regsrc.reglo));
  393. cg.a_load_reg_reg(list,OS_32,OS_32,tmplo,regdst.reglo);
  394. end
  395. else
  396. cg.a_load_reg_reg(list,OS_32,OS_32,regsrc.reglo,regdst.reglo);
  397. { With overflow checking and unsigned args, this generates slighly suboptimal code
  398. ($80000000 constant loaded twice). Other cases are fine. Getting it perfect does not
  399. look worth the effort. }
  400. cg.a_op_const_reg_reg(list,OP_ADD,hisize,aint(hi(value)),regsrc.reghi,regdst.reghi);
  401. if carry<>NR_NO then
  402. cg.a_op_reg_reg_reg(list,OP_ADD,hisize,carry,regdst.reghi,regdst.reghi);
  403. end;
  404. OP_SUB:
  405. begin
  406. carry:=NR_NO;
  407. if lo(value)<>0 then
  408. begin
  409. tmplo:=cg.GetIntRegister(list,OS_32);
  410. carry:=cg.GetIntRegister(list,OS_32);
  411. if {$ifdef AVOID_OVERFLOW} (abs(value) <= max_12_bit) and {$endif} is_imm12(-aint(lo(value))) then
  412. list.concat(taicpu.op_reg_reg_const(A_ADDI,tmplo,regsrc.reglo,-aint(lo(value))))
  413. else
  414. begin
  415. cg.a_load_const_reg(list,OS_INT,aint(lo(value)),tmplo);
  416. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmplo,regsrc.reglo,tmplo))
  417. end;
  418. list.concat(taicpu.op_reg_reg_reg(A_SLTU,carry,regsrc.reglo,tmplo));
  419. cg.a_load_reg_reg(list,OS_32,OS_32,tmplo,regdst.reglo);
  420. end
  421. else
  422. cg.a_load_reg_reg(list,OS_32,OS_32,regsrc.reglo,regdst.reglo);
  423. cg.a_op_const_reg_reg(list,OP_SUB,hisize,aint(hi(value)),regsrc.reghi,regdst.reghi);
  424. if carry<>NR_NO then
  425. cg.a_op_reg_reg_reg(list,OP_SUB,hisize,carry,regdst.reghi,regdst.reghi);
  426. end;
  427. else
  428. InternalError(2013050301);
  429. end;
  430. end;
  431. procedure tcg64frv.a_load64_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);
  432. var
  433. hreg64 : tregister64;
  434. begin
  435. { Override this function to prevent loading the reference twice.
  436. Use here some extra registers, but those are optimized away by the RA }
  437. hreg64.reglo:=cg.GetIntRegister(list,OS_32);
  438. hreg64.reghi:=cg.GetIntRegister(list,OS_32);
  439. a_load64_ref_reg(list,r,hreg64);
  440. a_load64_reg_cgpara(list,hreg64,paraloc);
  441. end;
  442. procedure tcg64frv.a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);
  443. var
  444. tmpref: treference;
  445. begin
  446. { Override this function to prevent loading the reference twice }
  447. tmpref:=ref;
  448. tcgrv32(cg).fixref(list,tmpref);
  449. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reglo,tmpref);
  450. inc(tmpref.offset,4);
  451. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reghi,tmpref);
  452. end;
  453. procedure tcg64frv.a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);
  454. var
  455. tmpref: treference;
  456. begin
  457. { Override this function to prevent loading the reference twice }
  458. tmpref:=ref;
  459. tcgrv32(cg).fixref(list,tmpref);
  460. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reglo);
  461. inc(tmpref.offset,4);
  462. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reghi);
  463. end;
  464. procedure create_codegen;
  465. begin
  466. cg := tcgrv32.create;
  467. cg64 :=tcg64frv.create;
  468. end;
  469. end.