cgcpu.pas 25 KB

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