cpubase.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Contains the base types 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. { This Unit contains the base types for the Risc-V32
  18. }
  19. unit cpubase;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. strings,globtype,
  24. cutils,cclasses,aasmbase,cpuinfo,cgbase;
  25. {*****************************************************************************
  26. Assembler Opcodes
  27. *****************************************************************************}
  28. type
  29. TAsmOp=(A_None,
  30. { Pseudo instructions }
  31. A_NOP,A_CALL,A_LA,A_LLA,A_LGA,A_LI,A_MV,A_NOT,A_NEG,A_NEGW,
  32. A_SEXT_B,A_SEXT_H,A_ZEXT_B,A_ZEXT_H,A_SEQZ,A_SNEG,A_SLTZ,A_SGTZ,
  33. A_FMV_S,A_FABS_S,A_FNEG_S,A_FMV_D,A_FABS_D,A_FNEG_D,
  34. A_BEQZ,A_BNEZ,A_BLEZ,A_BGEZ,A_BLTZ,A_BGTZ,A_GT,A_BLE,
  35. A_BGTU,A_BLEU,A_J,A_JR,A_RET,A_TAIL,
  36. { normal opcodes }
  37. A_LUI,A_AUIPC,A_JAL,A_JALR,
  38. A_Bxx,A_LB,A_LH,A_LW,A_LBU,A_LHU,
  39. A_SB,A_SH,A_SW,
  40. A_ADDI,A_SLTI,A_SLTIU,
  41. A_XORI,A_ORI,A_ANDI,
  42. A_SLLI,A_SRLI,A_SRAI,
  43. A_ADD,A_SUB,A_SLL,A_SLT,A_SLTU,
  44. A_XOR,A_SRL,A_SRA,A_OR,A_AND,
  45. A_FENCE,A_FENCE_I,
  46. A_ECALL,A_EBREAK,
  47. A_CSRRW,A_CSRRS,A_CSRRC,A_CSRRWI,A_CSRRSI,A_CSRRCI,
  48. {$ifdef RISCV64}
  49. { 64-bit }
  50. A_ADDIW,A_SLLIW,A_SRLIW,A_SRAIW,
  51. A_ADDW,A_SLLW,A_SRLW,A_SUBW,A_SRAW,
  52. A_LD,A_SD,A_LWU,
  53. {$endif RISCV64}
  54. { M-extension }
  55. A_MUL,A_MULH,A_MULHSU,A_MULHU,
  56. A_DIV,A_DIVU,A_REM,A_REMU,
  57. {$ifdef RISCV64}
  58. { 64-bit }
  59. A_MULW,
  60. A_DIVW,A_DIVUW,A_REMW,A_REMUW,
  61. {$endif RISCV64}
  62. { A-extension }
  63. A_LR_W,A_SC_W,A_AMOSWAP_W,A_AMOADD_W,A_AMOXOR_W,A_AMOAND_W,
  64. A_AMOOR_W,A_AMOMIN_W,A_AMOMAX_W,A_AMOMINU_W,A_AMOMAXU_W,
  65. {$ifdef RISCV64}
  66. { 64-bit }
  67. A_LR_D,A_SC_D,A_AMOSWAP_D,A_AMOADD_D,A_AMOXOR_D,A_AMOAND_D,
  68. A_AMOOR_D,A_AMOMIN_D,A_AMOMAX_D,A_AMOMINU_D,A_AMOMAXU_D,
  69. {$endif RISCV64}
  70. { F-extension }
  71. A_FLW,A_FSW,
  72. A_FMADD_S,A_FMSUB_S,A_FNMSUB_S,A_FNMADD_S,
  73. A_FADD_S,A_FSUB_S,A_FMUL_S,A_FDIV_S,
  74. A_FSQRT_S,A_FSGNJ_S,A_FSGNJN_S,A_FSGNJX_S,
  75. A_FMIN_S,A_FMAX_S,
  76. A_FMV_X_S,A_FEQ_S,A_FLT_S,A_FLE_S,A_FCLASS_S,
  77. A_FCVT_W_S,A_FCVT_WU_S,A_FCVT_S_W,A_FCVT_S_WU,
  78. A_FMV_S_X,
  79. A_FRCSR,A_FRRM,A_FRFLAGS,A_FSCSR,A_FSRM,
  80. A_FSFLAGS,A_FSRMI,A_FSFLAGSI,
  81. {$ifdef RISCV64}
  82. { 64-bit }
  83. A_FCVT_L_S,A_FCVT_LU_S,
  84. A_FCVT_S_L,A_FCVT_S_LU,
  85. {$endif RISCV64}
  86. { D-extension }
  87. A_FLD,A_FSD,
  88. A_FMADD_D,A_FMSUB_D,A_FNMSUB_D,A_FNMADD_D,
  89. A_FADD_D,A_FSUB_D,A_FMUL_D,A_FDIV_D,
  90. A_FSQRT_D,A_FSGNJ_D,A_FSGNJN_D,A_FSGNJX_D,
  91. A_FMIN_D,A_FMAX_D,
  92. A_FEQ_D,A_FLT_D,A_FLE_D,A_FCLASS_D,
  93. A_FCVT_D_S,A_FCVT_S_D,
  94. A_FCVT_W_D,A_FCVT_WU_D,A_FCVT_D_W,A_FCVT_D_WU,
  95. {$ifdef RISCV64}
  96. { 64-bit }
  97. A_FCVT_L_D,A_FCVT_LU_D,A_FMV_X_D,
  98. A_FCVT_D_L,A_FCVT_D_LU,A_FMV_D_X,
  99. {$endif RISCV64}
  100. { Machine mode }
  101. A_MRET,A_HRET,A_SRET,A_URET,
  102. A_WFI,
  103. { Supervisor }
  104. A_SFENCE_VM,
  105. { pseudo instructions for accessiong control and status registers }
  106. A_RDINSTRET,A_RDCYCLE,A_RDTIME,A_CSRR,A_CSRW,A_CSRS,A_CSRC,A_CSRWI,
  107. A_CSRSI,A_CSRCI
  108. );
  109. TAsmOps = set of TAsmOp;
  110. {# This should define the array of instructions as string }
  111. op2strtable=array[tasmop] of string[8];
  112. Const
  113. {# First value of opcode enumeration }
  114. firstop = low(tasmop);
  115. {# Last value of opcode enumeration }
  116. lastop = high(tasmop);
  117. {*****************************************************************************
  118. Registers
  119. *****************************************************************************}
  120. type
  121. { Number of registers used for indexing in tables }
  122. tregisterindex=0..{$i rrv32nor.inc}-1;
  123. const
  124. maxvarregs = 32-6; { 32 int registers - r0 - stackpointer - r2 - 3 scratch registers }
  125. maxfpuvarregs = 28; { 32 fpuregisters - some scratch registers (minimally 2) }
  126. { Available Superregisters }
  127. {$i rrv32sup.inc}
  128. { No Subregisters }
  129. R_SUBWHOLE=R_SUBNONE;
  130. { Available Registers }
  131. {$i rrv32con.inc}
  132. { Integer Super registers first and last }
  133. first_int_imreg = $20;
  134. { Float Super register first and last }
  135. first_fpu_imreg = $20;
  136. { MM Super register first and last }
  137. first_mm_imreg = $20;
  138. { TODO: Calculate bsstart}
  139. regnumber_count_bsstart = 64;
  140. regnumber_table : array[tregisterindex] of tregister = (
  141. {$i rrv32num.inc}
  142. );
  143. regstabs_table : array[tregisterindex] of shortint = (
  144. {$i rrv32sta.inc}
  145. );
  146. regdwarf_table : array[tregisterindex] of shortint = (
  147. {$i rrv32dwa.inc}
  148. );
  149. {*****************************************************************************
  150. Conditions
  151. *****************************************************************************}
  152. type
  153. TAsmCond = (C_None { unconditional jumps },
  154. C_LT,C_LTU,C_GE,C_GEU,C_NE,C_EQ);
  155. TAsmConds = set of TAsmCond;
  156. const
  157. cond2str: Array[TAsmCond] of string[4] = ({cf_none}'',
  158. { conditions when not using ctr decrement etc}
  159. 'lt','ltu','ge','geu','ne','eq');
  160. uppercond2str: Array[TAsmCond] of string[4] = ({cf_none}'',
  161. { conditions when not using ctr decrement etc}
  162. 'LT','LTU','GE','GEU','NE','EQ');
  163. {*****************************************************************************
  164. Flags
  165. *****************************************************************************}
  166. type
  167. TResFlagsEnum = (F_EQ,F_NE,F_LT,F_LTU,F_GE,F_GEU);
  168. {*****************************************************************************
  169. Reference
  170. *****************************************************************************}
  171. {*****************************************************************************
  172. Operands
  173. *****************************************************************************}
  174. type
  175. TMemoryOrderingFlag = (moRl, moAq);
  176. TMemoryOrdering = set of TMemoryOrderingFlag;
  177. TFenceFlag = (ffI, ffO, ffR, ffW);
  178. TFenceFlags = set of TFenceFlag;
  179. TRoundingMode = (RM_Default,
  180. RM_RNE,
  181. RM_RTZ,
  182. RM_RDN,
  183. RM_RUP,
  184. RM_RMM);
  185. const
  186. roundingmode2str : array[TRoundingMode] of string[3] = ('',
  187. 'rne','rtz','rdn','rup','rmm');
  188. {*****************************************************************************
  189. Constants
  190. *****************************************************************************}
  191. const
  192. max_operands = 5;
  193. {*****************************************************************************
  194. Default generic sizes
  195. *****************************************************************************}
  196. {# Defines the default address size for a processor, }
  197. {$ifdef RISCV64}
  198. OS_ADDR = OS_64;
  199. {# the natural int size for a processor,
  200. has to match osuinttype/ossinttype as initialized in psystem }
  201. OS_INT = OS_64;
  202. OS_SINT = OS_S64;
  203. {$endif RISCV64}
  204. {$ifdef RISCV32}
  205. OS_ADDR = OS_32;
  206. {# the natural int size for a processor,
  207. has to match osuinttype/ossinttype as initialized in psystem }
  208. OS_INT = OS_32;
  209. OS_SINT = OS_S32;
  210. {$endif RISCV64}
  211. {# the maximum float size for a processor, }
  212. OS_FLOAT = OS_F64;
  213. {# the size of a vector register for a processor }
  214. OS_VECTOR = OS_M128;
  215. {*****************************************************************************
  216. GDB Information
  217. *****************************************************************************}
  218. stab_regindex : array[tregisterindex] of shortint = (
  219. {$i rrv32sta.inc}
  220. );
  221. {*****************************************************************************
  222. Generic Register names
  223. *****************************************************************************}
  224. {# Stack pointer register }
  225. NR_STACK_POINTER_REG = NR_X2;
  226. RS_STACK_POINTER_REG = RS_X2;
  227. {# Frame pointer register }
  228. NR_FRAME_POINTER_REG = NR_X8;
  229. RS_FRAME_POINTER_REG = RS_X8;
  230. NR_PIC_OFFSET_REG = NR_X3;
  231. { Return address of a function }
  232. NR_RETURN_ADDRESS_REG = NR_X1;
  233. RS_RETURN_ADDRESS_REG = RS_X1;
  234. { Results are returned in this register (32-bit values) }
  235. NR_FUNCTION_RETURN_REG = NR_X10;
  236. RS_FUNCTION_RETURN_REG = RS_X10;
  237. { Low part of 64bit return value }
  238. NR_FUNCTION_RETURN64_LOW_REG = NR_X10;
  239. RS_FUNCTION_RETURN64_LOW_REG = RS_X10;
  240. { High part of 64bit return value }
  241. NR_FUNCTION_RETURN64_HIGH_REG = NR_X11;
  242. RS_FUNCTION_RETURN64_HIGH_REG = RS_X11;
  243. { The value returned from a function is available in this register }
  244. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  245. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  246. { The lowh part of 64bit value returned from a function }
  247. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  248. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  249. { The high part of 64bit value returned from a function }
  250. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  251. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  252. NR_FPU_RESULT_REG = NR_F10;
  253. NR_MM_RESULT_REG = NR_NO;
  254. NR_DEFAULTFLAGS = NR_NO;
  255. RS_DEFAULTFLAGS = RS_NO;
  256. {*****************************************************************************
  257. GCC /ABI linking information
  258. *****************************************************************************}
  259. {# Registers which must be saved when calling a routine declared as
  260. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  261. saved should be the ones as defined in the target ABI and / or GCC.
  262. This value can be deduced from CALLED_USED_REGISTERS array in the
  263. GCC source.
  264. }
  265. saved_standard_registers : array[0..12] of tsuperregister = (
  266. RS_X2,
  267. RS_X8,RS_X9,
  268. RS_X18,RS_X19,
  269. RS_X20,RS_X21,RS_X22,RS_X23,RS_X24,RS_X25,RS_X26,RS_X27
  270. );
  271. { this is only for the generic code which is not used for this architecture }
  272. saved_address_registers : array[0..0] of tsuperregister = (RS_INVALID);
  273. saved_mm_registers : array[0..0] of tsuperregister = (RS_INVALID);
  274. {# Required parameter alignment when calling a routine declared as
  275. stdcall and cdecl. The alignment value should be the one defined
  276. by GCC or the target ABI.
  277. The value of this constant is equal to the constant
  278. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  279. }
  280. {$ifdef RISCV64}
  281. std_param_align = 8;
  282. {$endif RISCV64}
  283. {$ifdef RISCV32}
  284. std_param_align = 4;
  285. {$endif RISCV32}
  286. {*****************************************************************************
  287. CPU Dependent Constants
  288. *****************************************************************************}
  289. maxfpuregs = 8;
  290. {*****************************************************************************
  291. Helpers
  292. *****************************************************************************}
  293. function is_imm12(value: tcgint): boolean;
  294. function is_lui_imm(value: tcgint): boolean;
  295. function is_calljmp(o:tasmop):boolean;
  296. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  297. { Returns the tcgsize corresponding with the size of reg.}
  298. function reg_cgsize(const reg: tregister) : tcgsize;
  299. function findreg_by_number(r:Tregister):tregisterindex;
  300. function std_regnum_search(const s:string):Tregister;
  301. function std_regname(r:Tregister):string;
  302. function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  303. function dwarf_reg(r:tregister):shortint;
  304. function dwarf_reg_no_error(r:tregister):shortint;
  305. function eh_return_data_regno(nr: longint): longint;
  306. function conditions_equal(const c1,c2: TAsmCond): boolean;
  307. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  308. function condition_in(const Subset, c: TAsmCond): Boolean;
  309. implementation
  310. uses
  311. rgbase,verbose;
  312. const
  313. std_regname_table : TRegNameTable = (
  314. {$i rrv32std.inc}
  315. );
  316. regnumber_index : array[tregisterindex] of tregisterindex = (
  317. {$i rrv32rni.inc}
  318. );
  319. std_regname_index : array[tregisterindex] of tregisterindex = (
  320. {$i rrv32sri.inc}
  321. );
  322. {*****************************************************************************
  323. Helpers
  324. *****************************************************************************}
  325. function is_imm12(value: tcgint): boolean;
  326. begin
  327. result:=(value >= -2048) and (value <= 2047);
  328. end;
  329. function is_lui_imm(value: tcgint): boolean;
  330. begin
  331. result:=SarInt64((value and $FFFFF000) shl 32, 32) = value;
  332. end;
  333. function is_calljmp(o:tasmop):boolean;
  334. begin
  335. is_calljmp:=false;
  336. case o of
  337. A_JAL,A_JALR,A_Bxx,A_CALL:
  338. is_calljmp:=true;
  339. else
  340. ;
  341. end;
  342. end;
  343. function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  344. const
  345. inv_condflags:array[TAsmCond] of TAsmCond=(C_None,
  346. C_GE,C_GEU,C_LT,C_LTU,C_EQ,C_NE);
  347. begin
  348. result := inv_condflags[c];
  349. end;
  350. function reg_cgsize(const reg: tregister): tcgsize;
  351. begin
  352. case getregtype(reg) of
  353. R_INTREGISTER :
  354. result:=OS_INT;
  355. R_MMREGISTER:
  356. result:=OS_M128;
  357. R_FPUREGISTER:
  358. result:=OS_F64;
  359. else
  360. internalerror(200303181);
  361. end;
  362. end;
  363. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  364. begin
  365. cgsize2subreg:=R_SUBWHOLE;
  366. end;
  367. function findreg_by_number(r:Tregister):tregisterindex;
  368. begin
  369. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  370. end;
  371. function std_regnum_search(const s:string):Tregister;
  372. begin
  373. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  374. end;
  375. function std_regname(r:Tregister):string;
  376. var
  377. p : tregisterindex;
  378. begin
  379. p:=findreg_by_number_table(r,regnumber_index);
  380. if p<>0 then
  381. result:=std_regname_table[p]
  382. else
  383. result:=generic_regname(r);
  384. end;
  385. function dwarf_reg(r:tregister):shortint;
  386. begin
  387. result:=regdwarf_table[findreg_by_number(r)];
  388. if result=-1 then
  389. internalerror(200603251);
  390. end;
  391. function dwarf_reg_no_error(r:tregister):shortint;
  392. begin
  393. result:=regdwarf_table[findreg_by_number(r)];
  394. end;
  395. function eh_return_data_regno(nr: longint): longint;
  396. begin
  397. if (nr>=0) and (nr<4) then
  398. result:=nr+10
  399. else
  400. result:=-1;
  401. end;
  402. function conditions_equal(const c1, c2: TAsmCond): boolean;
  403. begin
  404. result:=c1=c2;
  405. end;
  406. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  407. function condition_in(const Subset, c: TAsmCond): Boolean;
  408. begin
  409. Result := (c = C_None) or conditions_equal(Subset, c);
  410. if not Result then
  411. case Subset of
  412. C_EQ:
  413. Result := (c in [C_GE, C_GEU]);
  414. else
  415. Result := False;
  416. end;
  417. end;
  418. end.