cpubase.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. 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. { we consider B0 as the default flag }
  175. NR_DEFAULTFLAGS = NR_B0;
  176. RS_DEFAULTFLAGS = RS_B0;
  177. {*****************************************************************************
  178. GCC /ABI linking information
  179. *****************************************************************************}
  180. const
  181. { Required parameter alignment when calling a routine declared as
  182. stdcall and cdecl. The alignment value should be the one defined
  183. by GCC or the target ABI.
  184. The value of this constant is equal to the constant
  185. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  186. }
  187. std_param_align = 4;
  188. {*****************************************************************************
  189. Helpers
  190. *****************************************************************************}
  191. { Returns the tcgsize corresponding with the size of reg.}
  192. function reg_cgsize(const reg: tregister) : tcgsize;
  193. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  194. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  195. function findreg_by_number(r:Tregister):tregisterindex;
  196. function std_regnum_search(const s:string):Tregister;
  197. function std_regname(r:Tregister):string;
  198. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  199. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  200. function flags_to_cond(const f: TResFlagsEnum) : TAsmCond;
  201. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  202. function condition_in(const Subset, c: TAsmCond): Boolean;
  203. function dwarf_reg(r:tregister):shortint;
  204. function dwarf_reg_no_error(r:tregister):shortint;
  205. function eh_return_data_regno(nr: longint): longint;
  206. implementation
  207. uses
  208. systems,rgBase,verbose;
  209. const
  210. std_regname_table : TRegNameTable = (
  211. {$i rxtensastd.inc}
  212. );
  213. regnumber_index : array[tregisterindex] of tregisterindex = (
  214. {$i rxtensarni.inc}
  215. );
  216. std_regname_index : array[tregisterindex] of tregisterindex = (
  217. {$i rxtensasri.inc}
  218. );
  219. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  220. begin
  221. case regtype of
  222. R_MMREGISTER:
  223. begin
  224. case s of
  225. { records passed in MM registers }
  226. OS_32,
  227. OS_F32:
  228. cgsize2subreg:=R_SUBFS;
  229. OS_64,
  230. OS_F64:
  231. cgsize2subreg:=R_SUBFD;
  232. else
  233. internalerror(2009112701);
  234. end;
  235. end;
  236. else
  237. cgsize2subreg:=R_SUBWHOLE;
  238. end;
  239. end;
  240. function reg_cgsize(const reg: tregister): tcgsize;
  241. begin
  242. case getregtype(reg) of
  243. R_INTREGISTER :
  244. reg_cgsize:=OS_32;
  245. R_FPUREGISTER :
  246. reg_cgsize:=OS_F32;
  247. else
  248. internalerror(2020040501);
  249. end;
  250. end;
  251. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  252. begin
  253. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  254. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  255. is_calljmp:= o in [A_Bcc,A_BT,A_CALL0,A_CALL4,A_CALL8,A_CALL12,A_CALLX0,A_CALLX4,A_CALLX8,A_CALLX12];
  256. end;
  257. function findreg_by_number(r:Tregister):tregisterindex;
  258. begin
  259. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  260. end;
  261. function std_regnum_search(const s:string):Tregister;
  262. begin
  263. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  264. end;
  265. function std_regname(r:Tregister):string;
  266. var
  267. p : tregisterindex;
  268. begin
  269. p:=findreg_by_number_table(r,regnumber_index);
  270. if p<>0 then
  271. result:=std_regname_table[p]
  272. else
  273. result:=generic_regname(r);
  274. end;
  275. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  276. const
  277. inverse: array[TAsmCond] of TAsmCond=(C_None,
  278. C_NE,C_EQ,
  279. C_LT,C_GE,C_LTU,C_GEU,
  280. C_BNONE,C_ANY,C_NALL,C_BNONE,C_BS,C_BC,
  281. C_NEZ,C_EQZ,C_GEZ,C_LTZ,
  282. C_NEI,C_EQI,C_GEI,C_LTI,C_GEUI,C_LTUI,
  283. C_T,C_F
  284. );
  285. begin
  286. result := inverse[c];
  287. end;
  288. function flags_to_cond(const f: TResFlagsEnum) : TAsmCond;
  289. const flags2cond: array[TResFlagsEnum] of tasmcond = (
  290. C_F,
  291. C_T);
  292. begin
  293. flags_to_cond := flags2cond[f];
  294. end;
  295. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  296. begin
  297. result := c1 = c2;
  298. end;
  299. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  300. function condition_in(const Subset, c: TAsmCond): Boolean;
  301. begin
  302. Result := (c = C_None) or conditions_equal(Subset, c);
  303. { Please update as necessary. [Kit] }
  304. Result := False;
  305. end;
  306. function dwarf_reg(r:tregister):shortint;
  307. begin
  308. result:=regdwarf_table[findreg_by_number(r)];
  309. if result=-1 then
  310. internalerror(200603251);
  311. end;
  312. function dwarf_reg_no_error(r:tregister):shortint;
  313. begin
  314. result:=regdwarf_table[findreg_by_number(r)];
  315. end;
  316. function eh_return_data_regno(nr: longint): longint;
  317. begin
  318. if (nr>=0) and (nr<2) then
  319. result:=nr
  320. else
  321. result:=-1;
  322. end;
  323. end.