cpubase.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. Contains the base types for the ARM
  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. {# Base unit for processor information. This unit contains
  18. enumerations of registers, opcodes, sizes, and other
  19. such things which are processor specific.
  20. }
  21. unit cpubase;
  22. {$define USEINLINE}
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. globtype,globals,
  27. cpuinfo,
  28. cgbase
  29. ;
  30. {*****************************************************************************
  31. Assembler Opcodes
  32. *****************************************************************************}
  33. type
  34. TAsmOp= {$i xtensaop.inc}
  35. { This should define the array of instructions as string }
  36. op2strtable=array[tasmop] of string[11];
  37. const
  38. { First value of opcode enumeration }
  39. firstop = low(tasmop);
  40. { Last value of opcode enumeration }
  41. lastop = high(tasmop);
  42. {*****************************************************************************
  43. Registers
  44. *****************************************************************************}
  45. type
  46. { Number of registers used for indexing in tables }
  47. tregisterindex=0..{$i rxtensanor.inc}-1;
  48. const
  49. { Available Superregisters }
  50. {$i rxtensasup.inc}
  51. { No Subregisters }
  52. R_SUBWHOLE = R_SUBNONE;
  53. { Available Registers }
  54. {$i rxtensacon.inc}
  55. { Integer Super registers first and last }
  56. first_int_supreg = RS_A0;
  57. first_int_imreg = $10;
  58. { Float Super register first and last }
  59. first_fpu_supreg = RS_F0;
  60. first_fpu_imreg = $08;
  61. { MM Super register first and last }
  62. first_mm_supreg = RS_INVALID;
  63. first_mm_imreg = $30;
  64. { TODO: Calculate bsstart}
  65. regnumber_count_bsstart = 16;
  66. regnumber_table : array[tregisterindex] of tregister = (
  67. {$i rxtensanum.inc}
  68. );
  69. regstabs_table : array[tregisterindex] of shortint = (
  70. {$i rxtensasta.inc}
  71. );
  72. regdwarf_table : array[tregisterindex] of shortint = (
  73. {$i rxtensadwa.inc}
  74. );
  75. {*****************************************************************************
  76. Conditions
  77. *****************************************************************************}
  78. type
  79. TAsmCond=(C_None,
  80. C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  81. C_GE,C_LT,C_GT,C_LE,C_AL,C_NV
  82. );
  83. TAsmConds = set of TAsmCond;
  84. const
  85. cond2str : array[TAsmCond] of string[2]=('',
  86. 'eq','ne','cs','cc','mi','pl','vs','vc','hi','ls',
  87. 'ge','lt','gt','le','al','nv'
  88. );
  89. uppercond2str : array[TAsmCond] of string[2]=('',
  90. 'EQ','NE','CS','CC','MI','PL','VS','VC','HI','LS',
  91. 'GE','LT','GT','LE','AL','NV'
  92. );
  93. {*****************************************************************************
  94. Flags
  95. *****************************************************************************}
  96. type
  97. TResFlags = (F_EQ,F_NE,F_CS,F_CC,F_MI,F_PL,F_VS,F_VC,F_HI,F_LS,
  98. F_GE,F_LT,F_GT,F_LE);
  99. {*****************************************************************************
  100. Operands
  101. *****************************************************************************}
  102. taddressmode = (AM_OFFSET,AM_PREINDEXED,AM_POSTINDEXED);
  103. tshiftmode = (SM_None,SM_LSL,SM_LSR,SM_ASR,SM_ROR,SM_RRX);
  104. tupdatereg = (UR_None,UR_Update);
  105. pshifterop = ^tshifterop;
  106. tshifterop = record
  107. shiftmode : tshiftmode;
  108. rs : tregister;
  109. shiftimm : byte;
  110. end;
  111. tcpumodeflag = (mfA, mfI, mfF);
  112. tcpumodeflags = set of tcpumodeflag;
  113. tspecialregflag = (srC, srX, srS, srF);
  114. tspecialregflags = set of tspecialregflag;
  115. {*****************************************************************************
  116. Constants
  117. *****************************************************************************}
  118. const
  119. max_operands = 6;
  120. maxintregs = 15;
  121. maxfpuregs = 8;
  122. maxaddrregs = 0;
  123. {*****************************************************************************
  124. Operand Sizes
  125. *****************************************************************************}
  126. type
  127. topsize = (S_NO,
  128. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  129. S_IS,S_IL,S_IQ,
  130. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  131. );
  132. {*****************************************************************************
  133. Default generic sizes
  134. *****************************************************************************}
  135. const
  136. { Defines the default address size for a processor, }
  137. OS_ADDR = OS_32;
  138. { the natural int size for a processor,
  139. has to match osuinttype/ossinttype as initialized in psystem }
  140. OS_INT = OS_32;
  141. OS_SINT = OS_S32;
  142. { the maximum float size for a processor, }
  143. OS_FLOAT = OS_F64;
  144. { the size of a vector register for a processor }
  145. OS_VECTOR = OS_M32;
  146. {*****************************************************************************
  147. Generic Register names
  148. *****************************************************************************}
  149. { Stack pointer register }
  150. NR_STACK_POINTER_REG = NR_A1;
  151. RS_STACK_POINTER_REG = RS_A1;
  152. { Frame pointer register (initialized in tcpuprocinfo.init_framepointer) }
  153. RS_FRAME_POINTER_REG: tsuperregister = RS_NO;
  154. NR_FRAME_POINTER_REG: tregister = NR_NO;
  155. { Register for addressing absolute data in a position independant way,
  156. such as in PIC code. The exact meaning is ABI specific. For
  157. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  158. }
  159. { Results are returned in this register (32-bit values) }
  160. NR_FUNCTION_RETURN_REG = NR_A2;
  161. RS_FUNCTION_RETURN_REG = RS_A2;
  162. { The value returned from a function is available in this register }
  163. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  164. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  165. NR_FPU_RESULT_REG = NR_INVALID;
  166. NR_MM_RESULT_REG = NR_INVALID;
  167. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  168. { Offset where the parent framepointer is pushed }
  169. PARENT_FRAMEPOINTER_OFFSET = 0;
  170. NR_DEFAULTFLAGS = NR_INVALID;
  171. RS_DEFAULTFLAGS = RS_INVALID;
  172. {*****************************************************************************
  173. GCC /ABI linking information
  174. *****************************************************************************}
  175. const
  176. { Required parameter alignment when calling a routine declared as
  177. stdcall and cdecl. The alignment value should be the one defined
  178. by GCC or the target ABI.
  179. The value of this constant is equal to the constant
  180. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  181. }
  182. std_param_align = 4;
  183. {*****************************************************************************
  184. Helpers
  185. *****************************************************************************}
  186. { Returns the tcgsize corresponding with the size of reg.}
  187. function reg_cgsize(const reg: tregister) : tcgsize;
  188. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  189. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  190. procedure inverse_flags(var f: TResFlags);
  191. function flags_to_cond(const f: TResFlags) : TAsmCond;
  192. function findreg_by_number(r:Tregister):tregisterindex;
  193. function std_regnum_search(const s:string):Tregister;
  194. function std_regname(r:Tregister):string;
  195. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  196. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  197. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  198. function condition_in(const Subset, c: TAsmCond): Boolean;
  199. function dwarf_reg(r:tregister):shortint;
  200. function dwarf_reg_no_error(r:tregister):shortint;
  201. function eh_return_data_regno(nr: longint): longint;
  202. implementation
  203. uses
  204. systems,rgBase,verbose;
  205. const
  206. std_regname_table : TRegNameTable = (
  207. {$i rxtensastd.inc}
  208. );
  209. regnumber_index : array[tregisterindex] of tregisterindex = (
  210. {$i rxtensarni.inc}
  211. );
  212. std_regname_index : array[tregisterindex] of tregisterindex = (
  213. {$i rxtensasri.inc}
  214. );
  215. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  216. begin
  217. case regtype of
  218. R_MMREGISTER:
  219. begin
  220. case s of
  221. { records passed in MM registers }
  222. OS_32,
  223. OS_F32:
  224. cgsize2subreg:=R_SUBFS;
  225. OS_64,
  226. OS_F64:
  227. cgsize2subreg:=R_SUBFD;
  228. else
  229. internalerror(2009112701);
  230. end;
  231. end;
  232. else
  233. cgsize2subreg:=R_SUBWHOLE;
  234. end;
  235. end;
  236. function reg_cgsize(const reg: tregister): tcgsize;
  237. begin
  238. case getregtype(reg) of
  239. R_INTREGISTER :
  240. reg_cgsize:=OS_32;
  241. R_FPUREGISTER :
  242. reg_cgsize:=OS_F80;
  243. R_MMREGISTER :
  244. begin
  245. case getsubreg(reg) of
  246. R_SUBFD,
  247. R_SUBWHOLE:
  248. result:=OS_F64;
  249. R_SUBFS:
  250. result:=OS_F32;
  251. else
  252. internalerror(2009112903);
  253. end;
  254. end;
  255. else
  256. internalerror(200303181);
  257. end;
  258. end;
  259. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  260. begin
  261. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  262. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  263. is_calljmp:= o in [A_Bcc,A_BT,A_CALL0,A_CALL4,A_CALL8,A_CALL12,A_CALLX0,A_CALLX4,A_CALLX8,A_CALLX12];
  264. end;
  265. procedure inverse_flags(var f: TResFlags);
  266. const
  267. inv_flags: array[TResFlags] of TResFlags =
  268. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  269. F_LT,F_GE,F_LE,F_GT);
  270. begin
  271. f:=inv_flags[f];
  272. end;
  273. function flags_to_cond(const f: TResFlags) : TAsmCond;
  274. const
  275. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  276. (C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  277. C_GE,C_LT,C_GT,C_LE);
  278. begin
  279. if f>high(flag_2_cond) then
  280. internalerror(200112301);
  281. result:=flag_2_cond[f];
  282. end;
  283. function findreg_by_number(r:Tregister):tregisterindex;
  284. begin
  285. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  286. end;
  287. function std_regnum_search(const s:string):Tregister;
  288. begin
  289. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  290. end;
  291. function std_regname(r:Tregister):string;
  292. var
  293. p : tregisterindex;
  294. begin
  295. p:=findreg_by_number_table(r,regnumber_index);
  296. if p<>0 then
  297. result:=std_regname_table[p]
  298. else
  299. result:=generic_regname(r);
  300. end;
  301. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  302. const
  303. inverse: array[TAsmCond] of TAsmCond=(C_None,
  304. C_NE,C_EQ,C_CC,C_CS,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  305. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  306. );
  307. begin
  308. result := inverse[c];
  309. end;
  310. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  311. begin
  312. result := c1 = c2;
  313. end;
  314. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  315. function condition_in(const Subset, c: TAsmCond): Boolean;
  316. begin
  317. Result := (c = C_None) or conditions_equal(Subset, c);
  318. { Please update as necessary. [Kit] }
  319. if not Result then
  320. case Subset of
  321. C_EQ:
  322. Result := (c in [C_GE, C_LE]);
  323. C_LT:
  324. Result := (c in [C_LE]);
  325. C_GT:
  326. Result := (c in [C_GE]);
  327. else
  328. Result := False;
  329. end;
  330. end;
  331. function dwarf_reg(r:tregister):shortint;
  332. begin
  333. result:=regdwarf_table[findreg_by_number(r)];
  334. if result=-1 then
  335. internalerror(200603251);
  336. end;
  337. function dwarf_reg_no_error(r:tregister):shortint;
  338. begin
  339. result:=regdwarf_table[findreg_by_number(r)];
  340. end;
  341. function eh_return_data_regno(nr: longint): longint;
  342. begin
  343. if (nr>=0) and (nr<2) then
  344. result:=nr
  345. else
  346. result:=-1;
  347. end;
  348. end.