cpubase.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. {
  2. Copyright (C) 2022 Loongson Technology Corporation Limited.
  3. Contains the base types for the LoongArch64.
  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 cpubase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. strings,globtype,
  22. cutils,cclasses,aasmbase,cpuinfo,cgbase;
  23. {*****************************************************************************
  24. Assembler Opcodes
  25. *****************************************************************************}
  26. type
  27. TAsmOp= {$i loongarch64op.inc}
  28. {# This should define the array of instructions as string }
  29. op2strtable=array[tasmop] of string[16];
  30. Const
  31. {# First value of opcode enumeration }
  32. firstop = low(tasmop);
  33. {# Last value of opcode enumeration }
  34. lastop = high(tasmop);
  35. {*****************************************************************************
  36. Registers
  37. *****************************************************************************}
  38. type
  39. { Number of registers used for indexing in tables }
  40. tregisterindex=0..{$i rloongarch64nor.inc}-1;
  41. const
  42. maxvarregs = 32-6; { 32 int registers - r0 - stackpointer - r2 - 3 scratch registers }
  43. maxfpuvarregs = 28; { 32 fpuregisters - some scratch registers (minimally 2) }
  44. { Available Superregisters }
  45. {$i rloongarch64sup.inc}
  46. { No Subregisters }
  47. R_SUBWHOLE=R_SUBNONE;
  48. { Available Registers }
  49. {$i rloongarch64con.inc}
  50. { Integer Super registers first and last }
  51. first_int_supreg = RS_R0;
  52. first_int_imreg = $20;
  53. { Float Super register first and last }
  54. first_fpu_supreg = RS_F0;
  55. first_fpu_imreg = $20;
  56. { MM Super register first and last }
  57. first_mm_supreg = 0;
  58. first_mm_imreg = 1;
  59. { TODO: Calculate bsstart}
  60. regnumber_count_bsstart = 64;
  61. regnumber_table : array[tregisterindex] of tregister = (
  62. {$i rloongarch64num.inc}
  63. );
  64. regstabs_table : array[tregisterindex] of shortint = (
  65. {$i rloongarch64sta.inc}
  66. );
  67. regdwarf_table : array[tregisterindex] of shortint = (
  68. {$i rloongarch64dwa.inc}
  69. );
  70. {*****************************************************************************
  71. Conditions
  72. *****************************************************************************}
  73. type
  74. TAsmCond = (C_NONE,C_EQ,C_GT,C_LT,C_GE,C_LE,C_NE,C_LEU,C_LTU,C_GEU,C_GTU,
  75. C_EQZ,C_GTZ,C_LTZ,C_GEZ,C_LEZ,C_NEZ);
  76. TAsmConds = set of TAsmCond;
  77. const
  78. cond2str: Array[TAsmCond] of string[4] = ('',
  79. 'eq','gt','lt','ge','le','ne','leu','ltu','geu','gtu',
  80. 'eqz','gtz','ltz','gez','lez','nez');
  81. {*****************************************************************************
  82. Flags
  83. *****************************************************************************}
  84. type
  85. TResFlagsEnum = (F_EQ,F_NE,F_LT,F_LTU,F_GE,F_GEU);
  86. {*****************************************************************************
  87. Reference
  88. *****************************************************************************}
  89. {*****************************************************************************
  90. Operands
  91. *****************************************************************************}
  92. {*****************************************************************************
  93. Constants
  94. *****************************************************************************}
  95. const
  96. max_operands = 5;
  97. {*****************************************************************************
  98. Default generic sizes
  99. *****************************************************************************}
  100. {# Defines the default address size for a processor, }
  101. OS_ADDR = OS_64;
  102. {# the natural int size for a processor,
  103. has to match osuinttype/ossinttype as initialized in psystem }
  104. OS_INT = OS_64;
  105. OS_SINT = OS_S64;
  106. {# the maximum float size for a processor, }
  107. OS_FLOAT = OS_F64;
  108. {# the size of a vector register for a processor }
  109. OS_VECTOR = OS_M128;
  110. {*****************************************************************************
  111. GDB Information
  112. *****************************************************************************}
  113. stab_regindex : array[tregisterindex] of shortint = (
  114. {$i rloongarch64sta.inc}
  115. );
  116. {*****************************************************************************
  117. Generic Register names
  118. *****************************************************************************}
  119. {# Stack pointer register }
  120. NR_STACK_POINTER_REG = NR_R3;
  121. RS_STACK_POINTER_REG = RS_R3;
  122. {# Frame pointer register }
  123. NR_FRAME_POINTER_REG = NR_R22;
  124. RS_FRAME_POINTER_REG = RS_R22;
  125. { Return address of a function }
  126. NR_RETURN_ADDRESS_REG = NR_R1;
  127. RS_RETURN_ADDRESS_REG = RS_R1;
  128. { Results are returned in this register (32-bit values) }
  129. NR_FUNCTION_RETURN_REG = NR_R4;
  130. RS_FUNCTION_RETURN_REG = RS_R4;
  131. { Low part of 64bit return value }
  132. NR_FUNCTION_RETURN64_LOW_REG = NR_R4;
  133. RS_FUNCTION_RETURN64_LOW_REG = RS_R4;
  134. { High part of 64bit return value }
  135. NR_FUNCTION_RETURN64_HIGH_REG = NR_R4;
  136. RS_FUNCTION_RETURN64_HIGH_REG = RS_R4;
  137. { The value returned from a function is available in this register }
  138. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  139. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  140. { The lowh part of 64bit value returned from a function }
  141. NR_FUNCTION_RESULT64_LOW_REG = NR_FUNCTION_RETURN64_LOW_REG;
  142. RS_FUNCTION_RESULT64_LOW_REG = RS_FUNCTION_RETURN64_LOW_REG;
  143. { The high part of 64bit value returned from a function }
  144. NR_FUNCTION_RESULT64_HIGH_REG = NR_FUNCTION_RETURN64_HIGH_REG;
  145. RS_FUNCTION_RESULT64_HIGH_REG = RS_FUNCTION_RETURN64_HIGH_REG;
  146. NR_FPU_RESULT_REG = NR_F0;
  147. NR_MM_RESULT_REG = NR_NO;
  148. NR_DEFAULTFLAGS = NR_NO;
  149. RS_DEFAULTFLAGS = RS_NO;
  150. RS_FIRST_INT_PARAM_SUPREG = RS_R4;
  151. RS_FIRST_FLOAT_PARAM_SUPREG = RS_F0;
  152. RS_FIRST_MM_PARAM_SUPREG = RS_NO;
  153. {*****************************************************************************
  154. GCC /ABI linking information
  155. *****************************************************************************}
  156. {# Registers which must be saved when calling a routine declared as
  157. cppdecl, cdecl, stdcall, safecall, palmossyscall. The registers
  158. saved should be the ones as defined in the target ABI and / or GCC.
  159. This value can be deduced from CALLED_USED_REGISTERS array in the
  160. GCC source.
  161. }
  162. saved_standard_registers : array[0..10] of tsuperregister = (
  163. RS_R3, RS_R22,
  164. RS_R23, RS_R24, RS_R25, RS_R26, RS_R27, RS_R28, RS_R29, RS_R30, RS_R31
  165. );
  166. { this is only for the generic code which is not used for this architecture }
  167. saved_address_registers : array[0..0] of tsuperregister = (RS_INVALID);
  168. saved_mm_registers : array[0..0] of tsuperregister = (RS_INVALID);
  169. {# Required parameter alignment when calling a routine declared as
  170. stdcall and cdecl. The alignment value should be the one defined
  171. by GCC or the target ABI.
  172. The value of this constant is equal to the constant
  173. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  174. }
  175. std_param_align = 8;
  176. {*****************************************************************************
  177. CPU Dependent Constants
  178. *****************************************************************************}
  179. maxfpuregs = 8;
  180. {*****************************************************************************
  181. Helpers
  182. *****************************************************************************}
  183. function is_simm12(value: tcgint): boolean;
  184. function is_uimm12(value: tcgint): boolean;
  185. function is_simm16_and_quadruple(value: tcgint): boolean;
  186. function is_calljmp(o:tasmop):boolean;
  187. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  188. { Returns the tcgsize corresponding with the size of reg.}
  189. function reg_cgsize(const reg: tregister) : tcgsize;
  190. function findreg_by_number(r:Tregister):tregisterindex;
  191. function std_regnum_search(const s:string):Tregister;
  192. function std_regname(r:Tregister):string;
  193. function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  194. function dwarf_reg(r:tregister):shortint;
  195. function dwarf_reg_no_error(r:tregister):shortint;
  196. function eh_return_data_regno(nr: longint): longint;
  197. function conditions_equal(const c1,c2: TAsmCond): boolean;
  198. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  199. function condition_in(const Subset, c: TAsmCond): Boolean;
  200. function is_extra_reg(const s : string) : tregister;
  201. implementation
  202. uses
  203. rgbase,verbose;
  204. const
  205. std_regname_table : TRegNameTable = (
  206. {$i rloongarch64std.inc}
  207. );
  208. abi_regname_table : TRegNameTable = (
  209. {$i rloongarch64abi.inc}
  210. );
  211. regnumber_index : array[tregisterindex] of tregisterindex = (
  212. {$i rloongarch64rni.inc}
  213. );
  214. std_regname_index : array[tregisterindex] of tregisterindex = (
  215. {$i rloongarch64sri.inc}
  216. );
  217. {*****************************************************************************
  218. Helpers
  219. *****************************************************************************}
  220. function is_simm12(value: tcgint): boolean;
  221. begin
  222. result:=(value >= -2048) and (value <= 2047);
  223. end;
  224. function is_uimm12(value: tcgint): boolean;
  225. begin
  226. result:=(value >= 0) and (value <= 4095);
  227. end;
  228. function is_simm16_and_quadruple(value: tcgint): boolean;
  229. begin
  230. result:=(value >= -32768) and (value <= 32767) and ((value mod 4) = 0);
  231. end;
  232. function is_calljmp(o:tasmop):boolean;
  233. begin
  234. case o of
  235. { Most of time is call. }
  236. A_JIRL,A_BL: result:=true;
  237. { Most of time is jump. }
  238. A_JR,A_B,A_BEQ,A_BNE,A_BLT,A_BLTU,A_BGE,
  239. A_BGEU,A_BEQZ,A_BNEZ,A_BCEQZ,A_BCNEZ,
  240. A_BLTZ,A_BGTZ,A_BGEZ,A_BLEZ,A_BGT,A_BLE,
  241. A_BGTU,A_BLEU,A_BXX: result:=true;
  242. else
  243. result:=false;
  244. end;
  245. end;
  246. function inverse_cond(const c: TAsmCond): Tasmcond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  247. const
  248. inv_condflags:array[TAsmCond] of TAsmCond=(C_NONE,
  249. C_NE,C_LE,C_GE,C_LT,C_GT,C_EQ,C_GTU,C_GEU,C_LTU,C_LEU,
  250. C_NEZ,C_LEZ,C_GEZ,C_LTZ,C_GTZ,C_EQZ);
  251. begin
  252. result := inv_condflags[c];
  253. end;
  254. function reg_cgsize(const reg: tregister): tcgsize;
  255. begin
  256. case getregtype(reg) of
  257. R_INTREGISTER :
  258. result:=OS_INT;
  259. R_MMREGISTER:
  260. result:=OS_M128;
  261. R_FPUREGISTER:
  262. result:=OS_F64;
  263. else
  264. internalerror(2022111902);
  265. end;
  266. end;
  267. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  268. begin
  269. cgsize2subreg:=R_SUBWHOLE;
  270. end;
  271. function findreg_by_number(r:Tregister):tregisterindex;
  272. begin
  273. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  274. end;
  275. function std_regnum_search(const s:string):Tregister;
  276. begin
  277. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  278. end;
  279. function std_regname(r:Tregister):string;
  280. var
  281. p : tregisterindex;
  282. begin
  283. p:=findreg_by_number_table(r,regnumber_index);
  284. if p<>0 then
  285. result:=std_regname_table[p]
  286. else
  287. result:=generic_regname(r);
  288. end;
  289. function dwarf_reg(r:tregister):shortint;
  290. begin
  291. result:=regdwarf_table[findreg_by_number(r)];
  292. if result=-1 then
  293. internalerror(2022111903);
  294. end;
  295. function dwarf_reg_no_error(r:tregister):shortint;
  296. begin
  297. result:=regdwarf_table[findreg_by_number(r)];
  298. end;
  299. function eh_return_data_regno(nr: longint): longint;
  300. begin
  301. if (nr>=0) and (nr<4) then
  302. result:=nr+10
  303. else
  304. result:=-1;
  305. end;
  306. function conditions_equal(const c1, c2: TAsmCond): boolean;
  307. begin
  308. result:=c1=c2;
  309. end;
  310. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  311. function condition_in(const Subset, c: TAsmCond): Boolean;
  312. begin
  313. Result := (c = C_None) or conditions_equal(Subset, c);
  314. if not Result then
  315. case Subset of
  316. C_EQ:
  317. Result := (c in [C_GE,C_GEU,C_LE,C_LEU]);
  318. C_EQZ:
  319. Result := (c in [C_GEZ,C_LEZ]);
  320. else
  321. Result := False;
  322. end;
  323. end;
  324. function is_extra_reg(const s: string): tregister;
  325. const abiname2reg : array[tregisterindex] of tregister = (NR_NO,
  326. NR_R0,NR_R1,NR_R2,NR_R3,NR_R4,NR_R5,NR_R6,NR_R7,
  327. NR_R8,NR_R9,NR_R10,NR_R11,NR_R12,NR_R13,NR_R14,NR_R15,
  328. NR_R16,NR_R17,NR_R18,NR_R19,NR_R20,NR_R21,NR_R22,NR_R23,
  329. NR_R24,NR_R25,NR_R26,NR_R27,NR_R28,NR_R29,NR_R30,NR_R31,
  330. NR_F0,NR_F1,NR_F2,NR_F3,NR_F4,NR_F5,NR_F6,NR_F7,
  331. NR_F8,NR_F9,NR_F10,NR_F11,NR_F12,NR_F13,NR_F14,NR_F15,
  332. NR_F16,NR_F17,NR_F18,NR_F19,NR_F20,NR_F21,NR_F22,NR_F23,
  333. NR_F24,NR_F25,NR_F26,NR_F27,NR_F28,NR_F29,NR_F30,NR_F31,
  334. NR_FCC0,NR_FCC1,NR_FCC2,NR_FCC3,NR_FCC4,NR_FCC5,NR_FCC6,NR_FCC7);
  335. var
  336. i : longint;
  337. begin
  338. result:=NR_NO;
  339. { LoongArch registers start by '$' and abiname length <= 5 }
  340. if not(length(s) in [2..5]) and (s[1]<>'$') then
  341. exit;
  342. for i:=low(abi_regname_table) to high(abi_regname_table) do
  343. begin
  344. if s=abi_regname_table[i] then
  345. begin
  346. result:=abiname2reg[i];
  347. exit;
  348. end;
  349. end;
  350. end;
  351. begin
  352. end.