cpubase.pas 16 KB

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