cgcpu.pas 25 KB

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