cpubase.pas 13 KB

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