cpubase.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 = $10;
  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,
  81. C_GE,C_LT,C_GEU,C_LTU,
  82. C_ANY,C_BNONE,C_ALL,C_NALL,C_BC,C_BS,
  83. C_EQZ,C_NEZ,C_LTZ,C_GEZ,
  84. C_EQI,C_NEI,C_LTI,C_GEI,C_LTUI,C_GEUI,
  85. C_F,C_T
  86. );
  87. TAsmConds = set of TAsmCond;
  88. TResFlagsEnum = (F_Z,F_NZ);
  89. TResFlags = record
  90. register: TRegister;
  91. flag: TResFlagsEnum;
  92. end;
  93. const
  94. cond2str : array[TAsmCond] of string[4]=('',
  95. 'eq','ne',
  96. 'ge','lt','geu','ltu',
  97. 'any','none','all','nall','bc','bs',
  98. 'eqz','nez','ltz','gez',
  99. 'eqi','nei','lti','gei','ltui','geui',
  100. 'f','t'
  101. );
  102. uppercond2str : array[TAsmCond] of string[4]=('',
  103. 'EQ','NE',
  104. 'GE','LT','GEU','LTU',
  105. 'ANY','NONE','ALL','NALL','BC','BS',
  106. 'EQZ','NEZ','LTZ','GEZ',
  107. 'EQI','NEI','LTI','GEI','LTUI','GEUI',
  108. 'F','T'
  109. );
  110. {*****************************************************************************
  111. Operands
  112. *****************************************************************************}
  113. type
  114. tupdatereg = (UR_None,UR_Update);
  115. tcpumodeflag = (mfA, mfI, mfF);
  116. tcpumodeflags = set of tcpumodeflag;
  117. tspecialregflag = (srC, srX, srS, srF);
  118. tspecialregflags = set of tspecialregflag;
  119. {*****************************************************************************
  120. Constants
  121. *****************************************************************************}
  122. const
  123. max_operands = 6;
  124. maxintregs = 15;
  125. maxfpuregs = 8;
  126. maxaddrregs = 0;
  127. {*****************************************************************************
  128. Operand Sizes
  129. *****************************************************************************}
  130. type
  131. topsize = (S_NO,
  132. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  133. S_IS,S_IL,S_IQ,
  134. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  135. );
  136. {*****************************************************************************
  137. Default generic sizes
  138. *****************************************************************************}
  139. const
  140. { Defines the default address size for a processor, }
  141. OS_ADDR = OS_32;
  142. { the natural int size for a processor,
  143. has to match osuinttype/ossinttype as initialized in psystem }
  144. OS_INT = OS_32;
  145. OS_SINT = OS_S32;
  146. { the maximum float size for a processor, }
  147. OS_FLOAT = OS_F64;
  148. { the size of a vector register for a processor }
  149. OS_VECTOR = OS_M32;
  150. {*****************************************************************************
  151. Generic Register names
  152. *****************************************************************************}
  153. { Stack pointer register }
  154. NR_STACK_POINTER_REG = NR_A1;
  155. RS_STACK_POINTER_REG = RS_A1;
  156. { Frame pointer register (initialized in tcpuprocinfo.init_framepointer) }
  157. RS_FRAME_POINTER_REG: tsuperregister = RS_A7;
  158. NR_FRAME_POINTER_REG: tregister = NR_A7;
  159. { Register for addressing absolute data in a position independant way,
  160. such as in PIC code. The exact meaning is ABI specific. For
  161. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  162. }
  163. { Results are returned in this register (32-bit values) }
  164. NR_FUNCTION_RETURN_REG = NR_A2;
  165. RS_FUNCTION_RETURN_REG = RS_A2;
  166. { The value returned from a function is available in this register }
  167. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  168. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  169. NR_FPU_RESULT_REG = NR_INVALID;
  170. NR_MM_RESULT_REG = NR_INVALID;
  171. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  172. { Offset where the parent framepointer is pushed }
  173. PARENT_FRAMEPOINTER_OFFSET = 0;
  174. NR_DEFAULTFLAGS = NR_INVALID;
  175. RS_DEFAULTFLAGS = RS_INVALID;
  176. {*****************************************************************************
  177. GCC /ABI linking information
  178. *****************************************************************************}
  179. const
  180. { Required parameter alignment when calling a routine declared as
  181. stdcall and cdecl. The alignment value should be the one defined
  182. by GCC or the target ABI.
  183. The value of this constant is equal to the constant
  184. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  185. }
  186. std_param_align = 4;
  187. {*****************************************************************************
  188. Helpers
  189. *****************************************************************************}
  190. { Returns the tcgsize corresponding with the size of reg.}
  191. function reg_cgsize(const reg: tregister) : tcgsize;
  192. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  193. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  194. function findreg_by_number(r:Tregister):tregisterindex;
  195. function std_regnum_search(const s:string):Tregister;
  196. function std_regname(r:Tregister):string;
  197. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  198. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  199. function flags_to_cond(const f: TResFlagsEnum) : TAsmCond;
  200. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  201. function condition_in(const Subset, c: TAsmCond): Boolean;
  202. function dwarf_reg(r:tregister):shortint;
  203. function dwarf_reg_no_error(r:tregister):shortint;
  204. function eh_return_data_regno(nr: longint): longint;
  205. implementation
  206. uses
  207. systems,rgBase,verbose;
  208. const
  209. std_regname_table : TRegNameTable = (
  210. {$i rxtensastd.inc}
  211. );
  212. regnumber_index : array[tregisterindex] of tregisterindex = (
  213. {$i rxtensarni.inc}
  214. );
  215. std_regname_index : array[tregisterindex] of tregisterindex = (
  216. {$i rxtensasri.inc}
  217. );
  218. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  219. begin
  220. case regtype of
  221. R_MMREGISTER:
  222. begin
  223. case s of
  224. { records passed in MM registers }
  225. OS_32,
  226. OS_F32:
  227. cgsize2subreg:=R_SUBFS;
  228. OS_64,
  229. OS_F64:
  230. cgsize2subreg:=R_SUBFD;
  231. else
  232. internalerror(2009112701);
  233. end;
  234. end;
  235. else
  236. cgsize2subreg:=R_SUBWHOLE;
  237. end;
  238. end;
  239. function reg_cgsize(const reg: tregister): tcgsize;
  240. begin
  241. case getregtype(reg) of
  242. R_INTREGISTER :
  243. reg_cgsize:=OS_32;
  244. R_FPUREGISTER :
  245. reg_cgsize:=OS_F80;
  246. R_MMREGISTER :
  247. begin
  248. case getsubreg(reg) of
  249. R_SUBFD,
  250. R_SUBWHOLE:
  251. result:=OS_F64;
  252. R_SUBFS:
  253. result:=OS_F32;
  254. else
  255. internalerror(2009112903);
  256. end;
  257. end;
  258. else
  259. internalerror(200303181);
  260. end;
  261. end;
  262. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  263. begin
  264. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  265. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  266. is_calljmp:= o in [A_Bcc,A_BT,A_CALL0,A_CALL4,A_CALL8,A_CALL12,A_CALLX0,A_CALLX4,A_CALLX8,A_CALLX12];
  267. end;
  268. function findreg_by_number(r:Tregister):tregisterindex;
  269. begin
  270. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  271. end;
  272. function std_regnum_search(const s:string):Tregister;
  273. begin
  274. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  275. end;
  276. function std_regname(r:Tregister):string;
  277. var
  278. p : tregisterindex;
  279. begin
  280. p:=findreg_by_number_table(r,regnumber_index);
  281. if p<>0 then
  282. result:=std_regname_table[p]
  283. else
  284. result:=generic_regname(r);
  285. end;
  286. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  287. const
  288. inverse: array[TAsmCond] of TAsmCond=(C_None,
  289. C_NE,C_EQ,
  290. C_LT,C_GE,C_LTU,C_GEU,
  291. C_BNONE,C_ANY,C_NALL,C_BNONE,C_BS,C_BC,
  292. C_NEZ,C_EQZ,C_GEZ,C_LTZ,
  293. C_NEI,C_EQI,C_GEI,C_LTI,C_GEUI,C_LTUI,
  294. C_T,C_F
  295. );
  296. begin
  297. result := inverse[c];
  298. end;
  299. function flags_to_cond(const f: TResFlagsEnum) : TAsmCond;
  300. const flags2cond: array[TResFlagsEnum] of tasmcond = (
  301. C_F,
  302. C_T);
  303. begin
  304. flags_to_cond := flags2cond[f];
  305. end;
  306. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  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. { Please update as necessary. [Kit] }
  315. Result := False;
  316. end;
  317. function dwarf_reg(r:tregister):shortint;
  318. begin
  319. result:=regdwarf_table[findreg_by_number(r)];
  320. if result=-1 then
  321. internalerror(200603251);
  322. end;
  323. function dwarf_reg_no_error(r:tregister):shortint;
  324. begin
  325. result:=regdwarf_table[findreg_by_number(r)];
  326. end;
  327. function eh_return_data_regno(nr: longint): longint;
  328. begin
  329. if (nr>=0) and (nr<2) then
  330. result:=nr
  331. else
  332. result:=-1;
  333. end;
  334. end.