aasmcpu.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. {
  2. Copyright (c) 1999-2002 by Jonas Maebe and Thomas Schatzl
  3. Contains the assembler object for the Risc-V 32 and Risc-V 64
  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 aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,verbose,
  22. aasmbase,aasmtai,aasmdata,aasmsym,
  23. cpubase,cgbase,cgutils;
  24. const
  25. { "mov reg,reg" source operand number }
  26. O_MOV_SOURCE = 1;
  27. { "mov reg,reg" source operand number }
  28. O_MOV_DEST = 0;
  29. type
  30. taicpu = class(tai_cpu_abstract_sym)
  31. memoryordering: TMemoryOrdering;
  32. constructor op_none(op : tasmop);
  33. constructor op_reg(op : tasmop;_op1 : tregister);
  34. constructor op_const(op : tasmop;_op1 : aint);
  35. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  36. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  37. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  38. constructor op_const_reg(op:tasmop; _op1: aint; _op2: tregister);
  39. constructor op_const_const(op : tasmop;_op1,_op2 : aint);
  40. constructor op_reg_reg_const_const(op: tasmop; _op1, _op2: tregister; _op3, _op4: aint);
  41. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  42. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  43. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: aint);
  44. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  45. constructor op_const_reg_reg(op : tasmop;_op1 : aint;_op2, _op3 : tregister);
  46. constructor op_const_reg_const(op : tasmop;_op1 : aint;_op2 : tregister;_op3 : aint);
  47. constructor op_const_const_const(op : tasmop;_op1 : aint;_op2 : aint;_op3 : aint);
  48. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  49. constructor op_reg_bool_reg_reg(op : tasmop;_op1: tregister;_op2:boolean;_op3,_op4:tregister);
  50. constructor op_reg_bool_reg_const(op : tasmop;_op1: tregister;_op2:boolean;_op3:tregister;_op4: aint);
  51. constructor op_reg_reg_reg_const(op : tasmop; _op1, _op2, _op3 : tregister; _op4 : aint);
  52. constructor op_reg_reg_reg_const_const(op : tasmop;_op1,_op2,_op3 : tregister;_op4,_op5 : aint);
  53. constructor op_reg_reg_const_const_const(op : tasmop;_op1,_op2 : tregister;_op3,_op4,_op5 : aint);
  54. { this is for Jmp instructions }
  55. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  56. constructor op_const_const_sym(op : tasmop;_op1,_op2 : aint;_op3: tasmsymbol);
  57. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  58. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint);
  59. constructor op_reg_sym(op : tasmop;_op1 : tregister;_op2:tasmsymbol);
  60. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : aint);
  61. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint;const _op2 : treference);
  62. procedure loadfenceflags(opidx:aint;_flags:TFenceFlags);
  63. procedure loadbool(opidx:aint;_b:boolean);
  64. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  65. { register spilling code }
  66. function spilling_get_operation_type(opnr: longint): topertype;override;
  67. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;override;
  68. end;
  69. tai_align = class(tai_align_abstract)
  70. { nothing to add }
  71. end;
  72. procedure InitAsm;
  73. procedure DoneAsm;
  74. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  75. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  76. implementation
  77. uses cutils, cclasses;
  78. {*****************************************************************************
  79. taicpu Constructors
  80. *****************************************************************************}
  81. procedure taicpu.loadbool(opidx:aint;_b:boolean);
  82. begin
  83. if opidx>=ops then
  84. ops:=opidx+1;
  85. with oper[opidx]^ do
  86. begin
  87. if typ=top_ref then
  88. dispose(ref);
  89. b:=_b;
  90. typ:=top_bool;
  91. end;
  92. end;
  93. constructor taicpu.op_none(op : tasmop);
  94. begin
  95. inherited create(op);
  96. end;
  97. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  98. begin
  99. inherited create(op);
  100. ops:=1;
  101. loadreg(0,_op1);
  102. end;
  103. constructor taicpu.op_const(op : tasmop;_op1 : aint);
  104. begin
  105. inherited create(op);
  106. ops:=1;
  107. loadconst(0,_op1);
  108. end;
  109. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  110. begin
  111. inherited create(op);
  112. ops:=2;
  113. loadreg(0,_op1);
  114. loadreg(1,_op2);
  115. end;
  116. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  117. begin
  118. inherited create(op);
  119. ops:=2;
  120. loadreg(0,_op1);
  121. loadconst(1,_op2);
  122. end;
  123. constructor taicpu.op_const_reg(op:tasmop; _op1: aint; _op2: tregister);
  124. begin
  125. inherited create(op);
  126. ops:=2;
  127. loadconst(0,_op1);
  128. loadreg(1,_op2);
  129. end;
  130. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  131. begin
  132. inherited create(op);
  133. ops:=2;
  134. loadreg(0,_op1);
  135. loadref(1,_op2);
  136. end;
  137. constructor taicpu.op_const_const(op : tasmop;_op1,_op2 : aint);
  138. begin
  139. inherited create(op);
  140. ops:=2;
  141. loadconst(0,_op1);
  142. loadconst(1,_op2);
  143. end;
  144. constructor taicpu.op_reg_reg_const_const(op: tasmop; _op1, _op2: tregister; _op3, _op4: aint);
  145. begin
  146. inherited create(op);
  147. ops := 4;
  148. loadreg(0, _op1);
  149. loadreg(1, _op2);
  150. loadconst(2, _op3);
  151. loadconst(3, _op4);
  152. end;
  153. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  154. begin
  155. inherited create(op);
  156. ops:=3;
  157. loadreg(0,_op1);
  158. loadreg(1,_op2);
  159. loadreg(2,_op3);
  160. end;
  161. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  162. begin
  163. inherited create(op);
  164. ops:=3;
  165. loadreg(0,_op1);
  166. loadreg(1,_op2);
  167. loadconst(2,_op3);
  168. end;
  169. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: aint);
  170. begin
  171. inherited create(op);
  172. ops:=3;
  173. loadreg(0,_op1);
  174. loadreg(1,_op2);
  175. loadsymbol(2,_op3,_op3ofs);
  176. end;
  177. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  178. begin
  179. inherited create(op);
  180. ops:=3;
  181. loadreg(0,_op1);
  182. loadreg(1,_op2);
  183. loadref(2,_op3);
  184. end;
  185. constructor taicpu.op_const_reg_reg(op : tasmop;_op1 : aint;_op2, _op3 : tregister);
  186. begin
  187. inherited create(op);
  188. ops:=3;
  189. loadconst(0,_op1);
  190. loadreg(1,_op2);
  191. loadreg(2,_op3);
  192. end;
  193. constructor taicpu.op_const_reg_const(op : tasmop;_op1 : aint;_op2 : tregister;_op3 : aint);
  194. begin
  195. inherited create(op);
  196. ops:=3;
  197. loadconst(0,_op1);
  198. loadreg(1,_op2);
  199. loadconst(2,_op3);
  200. end;
  201. constructor taicpu.op_const_const_const(op : tasmop;_op1 : aint;_op2 : aint;_op3 : aint);
  202. begin
  203. inherited create(op);
  204. ops:=3;
  205. loadconst(0,_op1);
  206. loadconst(1,_op2);
  207. loadconst(2,_op3);
  208. end;
  209. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  210. begin
  211. inherited create(op);
  212. ops:=4;
  213. loadreg(0,_op1);
  214. loadreg(1,_op2);
  215. loadreg(2,_op3);
  216. loadreg(3,_op4);
  217. end;
  218. constructor taicpu.op_reg_bool_reg_reg(op : tasmop;_op1: tregister;_op2:boolean;_op3,_op4:tregister);
  219. begin
  220. inherited create(op);
  221. ops:=4;
  222. loadreg(0,_op1);
  223. loadbool(1,_op2);
  224. loadreg(2,_op3);
  225. loadreg(3,_op4);
  226. end;
  227. constructor taicpu.op_reg_bool_reg_const(op : tasmop;_op1: tregister;_op2:boolean;_op3:tregister;_op4: aint);
  228. begin
  229. inherited create(op);
  230. ops:=4;
  231. loadreg(0,_op1);
  232. loadbool(0,_op2);
  233. loadreg(0,_op3);
  234. loadconst(0,cardinal(_op4));
  235. end;
  236. constructor taicpu.op_reg_reg_reg_const(op : tasmop; _op1, _op2, _op3 : tregister; _op4 : aint);
  237. begin
  238. inherited create(op);
  239. ops := 4;
  240. loadreg(0, _op1);
  241. loadreg(1, _op2);
  242. loadreg(2, _op3);
  243. loadconst(3, cardinal(_op4));
  244. end;
  245. constructor taicpu.op_reg_reg_reg_const_const(op : tasmop;_op1,_op2,_op3 : tregister;_op4,_op5 : aint);
  246. begin
  247. inherited create(op);
  248. ops:=5;
  249. loadreg(0,_op1);
  250. loadreg(1,_op2);
  251. loadreg(2,_op3);
  252. loadconst(3,cardinal(_op4));
  253. loadconst(4,cardinal(_op5));
  254. end;
  255. constructor taicpu.op_reg_reg_const_const_const(op : tasmop;_op1,_op2 : tregister;_op3,_op4,_op5 : aint);
  256. begin
  257. inherited create(op);
  258. ops:=5;
  259. loadreg(0,_op1);
  260. loadreg(1,_op2);
  261. loadconst(2,_op3);
  262. loadconst(3,_op4);
  263. loadconst(4,_op5);
  264. end;
  265. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  266. begin
  267. inherited create(op);
  268. condition:=cond;
  269. ops:=1;
  270. loadsymbol(0,_op1,0);
  271. end;
  272. constructor taicpu.op_const_const_sym(op : tasmop;_op1,_op2 : aint; _op3: tasmsymbol);
  273. begin
  274. inherited create(op);
  275. ops:=3;
  276. loadconst(0,_op1);
  277. loadconst(1,_op2);
  278. loadsymbol(2,_op3,0);
  279. end;
  280. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  281. begin
  282. inherited create(op);
  283. ops:=1;
  284. loadsymbol(0,_op1,0);
  285. end;
  286. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint);
  287. begin
  288. inherited create(op);
  289. ops:=1;
  290. loadsymbol(0,_op1,_op1ofs);
  291. end;
  292. constructor taicpu.op_reg_sym(op: tasmop; _op1: tregister; _op2: tasmsymbol);
  293. begin
  294. inherited create(op);
  295. ops:=2;
  296. loadreg(0,_op1);
  297. loadsymbol(1,_op2,0);
  298. end;
  299. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : aint);
  300. begin
  301. inherited create(op);
  302. ops:=2;
  303. loadreg(0,_op1);
  304. loadsymbol(1,_op2,_op2ofs);
  305. end;
  306. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:aint;const _op2 : treference);
  307. begin
  308. inherited create(op);
  309. ops:=2;
  310. loadsymbol(0,_op1,_op1ofs);
  311. loadref(1,_op2);
  312. end;
  313. procedure taicpu.loadfenceflags(opidx: aint; _flags: TFenceFlags);
  314. begin
  315. allocate_oper(opidx+1);
  316. with oper[opidx]^ do
  317. begin
  318. if typ<>top_fenceflags then
  319. clearop(opidx);
  320. fenceflags:=_flags;
  321. typ:=top_fenceflags;
  322. end;
  323. end;
  324. { ****************************** newra stuff *************************** }
  325. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  326. begin
  327. case regtype of
  328. R_INTREGISTER:
  329. result:=
  330. (opcode=A_ADDI) and
  331. (oper[0]^.reg=oper[1]^.reg) and
  332. (oper[2]^.val=0);
  333. R_FPUREGISTER:
  334. result:=
  335. (opcode in [A_FSGNJ_S,A_FSGNJ_D]) and
  336. (oper[0]^.reg=oper[1]^.reg) and
  337. (oper[0]^.reg=oper[2]^.reg);
  338. end;
  339. end;
  340. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  341. begin
  342. result := operand_read;
  343. case opcode of
  344. // U type
  345. A_LUI,
  346. A_AUIPC:
  347. if opnr=0 then
  348. result:=operand_write
  349. else
  350. result:=operand_read;
  351. // UJ type
  352. A_JAL:
  353. if opnr=0 then
  354. result:=operand_write
  355. else
  356. result:=operand_read;
  357. // I type
  358. A_JALR,
  359. A_LB,A_LH,A_LW,A_LBU,A_LHU,
  360. A_ADDI,A_SLTI,A_SLTIU,
  361. A_XORI,A_ORI,A_ANDI,
  362. A_SLLI,A_SRLI,A_SRAI,
  363. A_FENCE,A_FENCE_I,
  364. A_ECALL,A_EBREAK,
  365. A_CSRRW,A_CSRRS,A_CSRRC,A_CSRRWI,A_CSRRSI,A_CSRRCI,
  366. A_FRCSR,A_FRRM,A_FRFLAGS,A_FSCSR,A_FSRM,
  367. A_FSFLAGS,A_FSRMI,A_FSFLAGSI:
  368. if opnr=0 then
  369. result:=operand_write
  370. else
  371. result:=operand_read;
  372. A_FLW,
  373. A_FLD:
  374. if opnr=0 then
  375. result:=operand_write
  376. else
  377. result:=operand_read;
  378. // SB type
  379. A_Bxx:
  380. result:=operand_read;
  381. // S type
  382. A_SB,A_SH,A_SW,
  383. A_FSW,
  384. A_FSD:
  385. result:=operand_read;
  386. // R type
  387. A_ADD,A_SUB,A_SLL,A_SLT,A_SLTU,
  388. A_XOR,A_OR,A_AND,A_SRL,A_SRA,
  389. A_MUL,A_MULH,A_MULHSU,A_MULHU,
  390. A_DIV,A_DIVU,A_REM,A_REMU,
  391. A_LR_W,A_SC_W,A_AMOSWAP_W,A_AMOADD_W,A_AMOXOR_W,A_AMOAND_W,
  392. A_AMOOR_W,A_AMOMIN_W,A_AMOMAX_W,A_AMOMINU_W,A_AMOMAXU_W,
  393. A_FADD_S,A_FSUB_S,A_FMUL_S,A_FDIV_S,
  394. A_FSQRT_S,A_FSGNJ_S,A_FSGNJN_S,A_FSGNJX_S,
  395. A_FMIN_S,A_FMAX_S,
  396. A_FMV_X_S,A_FEQ_S,A_FLT_S,A_FLE_S,A_FCLASS_S,
  397. A_FCVT_W_S,A_FCVT_WU_S,A_FCVT_S_W,A_FCVT_S_WU,
  398. A_FMV_S_X,
  399. A_FADD_D,A_FSUB_D,A_FMUL_D,A_FDIV_D,
  400. A_FSQRT_D,A_FSGNJ_D,A_FSGNJN_D,A_FSGNJX_D,
  401. A_FMIN_D,A_FMAX_D,
  402. A_FEQ_D,A_FLT_D,A_FLE_D,A_FCLASS_D,
  403. A_FCVT_D_S,A_FCVT_S_D,
  404. A_FCVT_W_D,A_FCVT_WU_D,A_FCVT_D_W,A_FCVT_D_WU:
  405. if opnr=0 then
  406. result:=operand_write
  407. else
  408. result:=operand_read;
  409. // R4 type
  410. A_FMADD_S,A_FMSUB_S,A_FNMSUB_S,A_FNMADD_S,
  411. A_FMADD_D,A_FMSUB_D,A_FNMSUB_D,A_FNMADD_D:
  412. if opnr=0 then
  413. result:=operand_write
  414. else
  415. result:=operand_read;
  416. {$ifdef RISCV64}
  417. A_LR_D,A_SC_D,A_AMOSWAP_D,A_AMOADD_D,A_AMOXOR_D,A_AMOAND_D,
  418. A_AMOOR_D,A_AMOMIN_D,A_AMOMAX_D,A_AMOMINU_D,A_AMOMAXU_D,
  419. A_MULW,
  420. A_DIVW,A_DIVUW,A_REMW,A_REMUW,
  421. A_FCVT_L_S,A_FCVT_LU_S,
  422. A_FCVT_S_L,A_FCVT_S_LU,
  423. A_FCVT_L_D,A_FCVT_LU_D,A_FMV_X_D,
  424. A_FCVT_D_L,A_FCVT_D_LU,A_FMV_D_X,
  425. A_ADDIW,A_SLLIW,A_SRLIW,A_SRAIW,
  426. A_ADDW,A_SLLW,A_SRLW,A_SUBW,A_SRAW,
  427. A_LD,A_LWU:
  428. if opnr=0 then
  429. result:=operand_write
  430. else
  431. result:=operand_read;
  432. A_SD:
  433. result:=operand_read;
  434. {$endif RISCV64}
  435. end;
  436. end;
  437. function taicpu.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  438. begin
  439. result := operand_read;
  440. end;
  441. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  442. begin
  443. case getregtype(r) of
  444. R_INTREGISTER:
  445. {$ifdef cpu64bitalu}
  446. result:=taicpu.op_reg_ref(A_LD,r,ref);
  447. {$else cpu64bitalu}
  448. result:=taicpu.op_reg_ref(A_LW,r,ref);
  449. {$endif cpu64bitalu}
  450. R_FPUREGISTER:
  451. result:=taicpu.op_reg_ref(A_FLD,r,ref);
  452. else
  453. internalerror(2005123101);
  454. end;
  455. end;
  456. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  457. begin
  458. case getregtype(r) of
  459. R_INTREGISTER:
  460. {$ifdef cpu64bitalu}
  461. result:=taicpu.op_reg_ref(A_SD,r,ref);
  462. {$else cpu64bitalu}
  463. result:=taicpu.op_reg_ref(A_SW,r,ref);
  464. {$endif cpu64bitalu}
  465. R_FPUREGISTER:
  466. result:=taicpu.op_reg_ref(A_FSD,r,ref);
  467. else
  468. internalerror(2005123102);
  469. end;
  470. end;
  471. procedure InitAsm;
  472. begin
  473. end;
  474. procedure DoneAsm;
  475. begin
  476. end;
  477. begin
  478. cai_align:=tai_align;
  479. cai_cpu:=taicpu;
  480. end.