cpubase.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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[7];
  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. Instruction post fixes
  77. *****************************************************************************}
  78. type
  79. { Xtensa instructions can have several instruction post fixes }
  80. TOpPostfix = (PF_None,
  81. { On big-endian processors, convert encoded immediate value to little-endian.
  82. For J.L, assembler tries to convert into J if target is within reach, else convert to JX }
  83. PF_L,
  84. { Assembler to generate narrow version of instruction if possible }
  85. PF_N,
  86. { Opcode operates on single precision floating point register(s)}
  87. PF_S,
  88. { Indicate MUL operations involving MAC16 accumulator option }
  89. PF_AA_LL, PF_AA_HL, PF_AA_LH, PF_AA_HH, PF_AD_LL, PF_AD_HL,
  90. PF_AD_LH, PF_AD_HH, PF_DA_LL, PF_DA_HL, PF_DA_LH, PF_DA_HH,
  91. PF_DD_LL, PF_DD_HL, PF_DD_LH, PF_DD_HH,
  92. PF_DA_LL_LDDEC, PF_DA_HL_LDDEC, PF_DA_LH_LDDEC, PF_DA_HH_LDDEC,
  93. PF_DA_LL_LDINC, PF_DA_HL_LDINC, PF_DA_LH_LDINC, PF_DA_HH_LDINC,
  94. PF_DD_LL_LDDEC, PF_DD_HL_LDDEC, PF_DD_LH_LDDEC, PF_DD_HH_LDDEC,
  95. PF_DD_LL_LDINC, PF_DD_HL_LDINC, PF_DD_LH_LDINC, PF_DD_HH_LDINC,
  96. { Special registers accessible via RSR, WSR & XSR instructions }
  97. PF_ACCHI, PF_ACCLO, PF_ATOMCTL, PF_BR, PF_CCOMPARE0, PF_CCOMPARE1,
  98. PF_CCOMPARE2, PF_CCOUNT, PF_CPENABLE, PF_DBREAKA0, PF_DBREAKA1,
  99. PF_DBREAKC0, PF_DBREAKC1, PF_DDR, PF_DEBUGCAUSE, PF_DEPC,
  100. PF_DTLBCFG, PF_EPC1, PF_EPC2, PF_EPC3, PF_EPC4, PF_EPC5, PF_EPC6,
  101. PF_EPC7, PF_EPS2, PF_EPS3, PF_EPS4, PF_EPS5, PF_EPS6, PF_EPS7,
  102. PF_EXCCAUSE, PF_EXCSAVE1, PF_EXCSAVE2, PF_EXCSAVE3, PF_EXCSAVE4,
  103. PF_EXCSAVE5, PF_EXCSAVE6, PF_EXCSAVE7, PF_EXCVADDR, PF_IBREAKA0,
  104. PF_IBREAKA1, PF_IBREAKENABLE, PF_ICOUNT, PF_ICOUNTLEVEL,
  105. PF_INTCLEAR, PF_INTENABLE, PF_INTERRUPT, PF_INTSET, PF_ITLBCFG,
  106. PF_LBEG, PF_LCOUNT, PF_LEND, PF_LITBASE, PF_M0, PF_M1, PF_M2,
  107. PF_M3, PF_MECR, PF_MEPC, PF_MEPS, PF_MESAVE, PF_MESR, PF_MEVADDR,
  108. PF_MISC0, PF_MISC1, PF_MISC2, PF_MISC3, PF_MMID, PF_PRID, PF_PS,
  109. PF_PTEVADDR, PF_RASID, PF_SAR, PF_SCOMPARE1, PF_VECBASE,
  110. PF_WINDOWBASE, PF_WINDOWSTART);
  111. TOpPostfixes = set of TOpPostfix;
  112. const
  113. oppostfix2str : array[TOpPostfix] of string[12] = ('',
  114. 'l', 'n', 's',
  115. 'aa.ll', 'aa.hl', 'aa.lh', 'aa.hh', 'ad.ll', 'ad.hl',
  116. 'ad.lh', 'ad.hh', 'da.ll', 'da.hl', 'da.lh', 'da.hh',
  117. 'dd.ll', 'dd.hl', 'dd.lh', 'dd.hh', 'da.ll.lddec',
  118. 'da.hl.lddec', 'da.lh.lddec', 'da.hh.lddec', 'da.ll.ldinc',
  119. 'da.hl.ldinc', 'da.lh.ldinc', 'da.hh.ldinc', 'dd.ll.lddec',
  120. 'dd.hl.lddec', 'dd.lh.lddec', 'dd.hh.lddec', 'dd.ll.ldinc',
  121. 'dd.hl.ldinc', 'dd.lh.ldinc', 'dd.hh.ldinc',
  122. 'acchi', 'acclo', 'atomctl', 'br', 'ccompare0', 'ccompare1',
  123. 'ccompare2', 'ccount', 'cpenable', 'dbreaka0', 'dbreaka1',
  124. 'dbreakc0', 'dbreakc1', 'ddr', 'debugcause', 'depc',
  125. 'dtlbcfg', 'epc1', 'epc2', 'epc3', 'epc4', 'epc5', 'epc6',
  126. 'epc7', 'eps2', 'eps3', 'eps4', 'eps5', 'eps6', 'eps7',
  127. 'exccause', 'excsave1', 'excsave2', 'excsave3', 'excsave4',
  128. 'excsave5', 'excsave6', 'excsave7', 'excvaddr', 'ibreaka0',
  129. 'ibreaka1', 'ibreakenable', 'icount', 'icountlevel',
  130. 'intclear', 'intenable', 'interrupt', 'intset', 'itlbcfg',
  131. 'lbeg', 'lcount', 'lend', 'litbase', 'm0', 'm1', 'm2',
  132. 'm3', 'mecr', 'mepc', 'meps', 'mesave', 'mesr', 'mevaddr',
  133. 'misc0', 'misc1', 'misc2', 'misc3', 'mmid', 'prid', 'ps',
  134. 'ptevaddr', 'rasid', 'sar', 'scompare1', 'vecbase',
  135. 'windowbase', 'windowstart');
  136. {*****************************************************************************
  137. Conditions
  138. *****************************************************************************}
  139. type
  140. TAsmCond=(C_None,
  141. C_EQ,C_NE,
  142. C_GE,C_LT,C_GEU,C_LTU,
  143. C_ANY,C_BNONE,C_ALL,C_NALL,C_BC,C_BS,C_BCI,C_BSI,
  144. C_EQZ,C_NEZ,C_LTZ,C_GEZ,
  145. C_EQI,C_NEI,C_LTI,C_GEI,C_LTUI,C_GEUI,
  146. C_F,C_T
  147. );
  148. TAsmConds = set of TAsmCond;
  149. TResFlagsEnum = (F_Z,F_NZ);
  150. TResFlags = record
  151. register: TRegister;
  152. flag: TResFlagsEnum;
  153. end;
  154. const
  155. cond2str : array[TAsmCond] of string[4]=('',
  156. 'eq','ne',
  157. 'ge','lt','geu','ltu',
  158. 'any','none','all','nall','bc','bs','bci','bsi',
  159. 'eqz','nez','ltz','gez',
  160. 'eqi','nei','lti','gei','ltui','geui',
  161. 'f','t'
  162. );
  163. uppercond2str : array[TAsmCond] of string[4]=('',
  164. 'EQ','NE',
  165. 'GE','LT','GEU','LTU',
  166. 'ANY','NONE','ALL','NALL','BC','BS', 'BCI','BSI',
  167. 'EQZ','NEZ','LTZ','GEZ',
  168. 'EQI','NEI','LTI','GEI','LTUI','GEUI',
  169. 'F','T'
  170. );
  171. {*****************************************************************************
  172. Operands
  173. *****************************************************************************}
  174. type
  175. tupdatereg = (UR_None,UR_Update);
  176. tcpumodeflag = (mfA, mfI, mfF);
  177. tcpumodeflags = set of tcpumodeflag;
  178. tspecialregflag = (srC, srX, srS, srF);
  179. tspecialregflags = set of tspecialregflag;
  180. {*****************************************************************************
  181. Constants
  182. *****************************************************************************}
  183. const
  184. max_operands = 6;
  185. maxintregs = 15;
  186. maxfpuregs = 8;
  187. maxaddrregs = 0;
  188. {*****************************************************************************
  189. Operand Sizes
  190. *****************************************************************************}
  191. type
  192. topsize = (S_NO,
  193. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  194. S_IS,S_IL,S_IQ,
  195. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  196. );
  197. {*****************************************************************************
  198. Default generic sizes
  199. *****************************************************************************}
  200. const
  201. { Defines the default address size for a processor, }
  202. OS_ADDR = OS_32;
  203. { the natural int size for a processor,
  204. has to match osuinttype/ossinttype as initialized in psystem }
  205. OS_INT = OS_32;
  206. OS_SINT = OS_S32;
  207. { the maximum float size for a processor, }
  208. OS_FLOAT = OS_F64;
  209. { the size of a vector register for a processor }
  210. OS_VECTOR = OS_M32;
  211. {*****************************************************************************
  212. Generic Register names
  213. *****************************************************************************}
  214. { Stack pointer register }
  215. NR_STACK_POINTER_REG = NR_A1;
  216. RS_STACK_POINTER_REG = RS_A1;
  217. { Frame pointer register (initialized in tcpuprocinfo.init_framepointer) }
  218. RS_FRAME_POINTER_REG: tsuperregister = RS_A7;
  219. NR_FRAME_POINTER_REG: tregister = NR_A7;
  220. { Register for addressing absolute data in a position independant way,
  221. such as in PIC code. The exact meaning is ABI specific. For
  222. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  223. }
  224. { Results are returned in this register (32-bit values) }
  225. NR_FUNCTION_RETURN_REG = NR_A2;
  226. RS_FUNCTION_RETURN_REG = RS_A2;
  227. { The value returned from a function is available in this register }
  228. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  229. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  230. NR_FPU_RESULT_REG = NR_INVALID;
  231. NR_MM_RESULT_REG = NR_INVALID;
  232. NR_RETURN_ADDRESS_REG = NR_FUNCTION_RETURN_REG;
  233. { Offset where the parent framepointer is pushed }
  234. PARENT_FRAMEPOINTER_OFFSET = 0;
  235. { we consider B0 as the default flag }
  236. NR_DEFAULTFLAGS = NR_B0;
  237. RS_DEFAULTFLAGS = RS_B0;
  238. {*****************************************************************************
  239. GCC /ABI linking information
  240. *****************************************************************************}
  241. const
  242. { Required parameter alignment when calling a routine declared as
  243. stdcall and cdecl. The alignment value should be the one defined
  244. by GCC or the target ABI.
  245. The value of this constant is equal to the constant
  246. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  247. }
  248. std_param_align = 4;
  249. {*****************************************************************************
  250. Helpers
  251. *****************************************************************************}
  252. { Returns the tcgsize corresponding with the size of reg.}
  253. function reg_cgsize(const reg: tregister) : tcgsize;
  254. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  255. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  256. function findreg_by_number(r:Tregister):tregisterindex;
  257. function std_regnum_search(const s:string):Tregister;
  258. function std_regname(r:Tregister):string;
  259. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  260. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  261. function flags_to_cond(const f: TResFlagsEnum) : TAsmCond;
  262. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  263. function condition_in(const Subset, c: TAsmCond): Boolean;
  264. function dwarf_reg(r:tregister):shortint;
  265. function dwarf_reg_no_error(r:tregister):shortint;
  266. function eh_return_data_regno(nr: longint): longint;
  267. implementation
  268. uses
  269. systems,rgBase,verbose;
  270. const
  271. std_regname_table : TRegNameTable = (
  272. {$i rxtensastd.inc}
  273. );
  274. regnumber_index : array[tregisterindex] of tregisterindex = (
  275. {$i rxtensarni.inc}
  276. );
  277. std_regname_index : array[tregisterindex] of tregisterindex = (
  278. {$i rxtensasri.inc}
  279. );
  280. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  281. begin
  282. case regtype of
  283. R_MMREGISTER:
  284. begin
  285. case s of
  286. { records passed in MM registers }
  287. OS_32,
  288. OS_F32:
  289. cgsize2subreg:=R_SUBFS;
  290. OS_64,
  291. OS_F64:
  292. cgsize2subreg:=R_SUBFD;
  293. else
  294. internalerror(2009112701);
  295. end;
  296. end;
  297. else
  298. cgsize2subreg:=R_SUBWHOLE;
  299. end;
  300. end;
  301. function reg_cgsize(const reg: tregister): tcgsize;
  302. begin
  303. case getregtype(reg) of
  304. R_INTREGISTER :
  305. reg_cgsize:=OS_32;
  306. R_FPUREGISTER :
  307. reg_cgsize:=OS_F32;
  308. else
  309. internalerror(2020040501);
  310. end;
  311. end;
  312. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  313. begin
  314. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  315. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  316. is_calljmp:= o in [A_B,A_CALL0,A_CALL4,A_CALL8,A_CALL12,A_CALLX0,A_CALLX4,A_CALLX8,A_CALLX12];
  317. end;
  318. function findreg_by_number(r:Tregister):tregisterindex;
  319. begin
  320. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  321. end;
  322. function std_regnum_search(const s:string):Tregister;
  323. begin
  324. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  325. end;
  326. function std_regname(r:Tregister):string;
  327. var
  328. p : tregisterindex;
  329. begin
  330. p:=findreg_by_number_table(r,regnumber_index);
  331. if p<>0 then
  332. result:=std_regname_table[p]
  333. else
  334. result:=generic_regname(r);
  335. end;
  336. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  337. const
  338. inverse: array[TAsmCond] of TAsmCond=(C_None,
  339. C_NE,C_EQ,
  340. C_LT,C_GE,C_LTU,C_GEU,
  341. C_BNONE,C_ANY,C_NALL,C_BNONE,C_BS,C_BC,C_BSI,C_BCI,
  342. C_NEZ,C_EQZ,C_GEZ,C_LTZ,
  343. C_NEI,C_EQI,C_GEI,C_LTI,C_GEUI,C_LTUI,
  344. C_T,C_F
  345. );
  346. begin
  347. result := inverse[c];
  348. end;
  349. function flags_to_cond(const f: TResFlagsEnum) : TAsmCond;
  350. const flags2cond: array[TResFlagsEnum] of tasmcond = (
  351. C_F,
  352. C_T);
  353. begin
  354. flags_to_cond := flags2cond[f];
  355. end;
  356. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  357. begin
  358. result := c1 = c2;
  359. end;
  360. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  361. function condition_in(const Subset, c: TAsmCond): Boolean;
  362. begin
  363. Result := (c = C_None) or conditions_equal(Subset, c);
  364. { Please update as necessary. [Kit] }
  365. Result := False;
  366. end;
  367. function dwarf_reg(r:tregister):shortint;
  368. begin
  369. result:=regdwarf_table[findreg_by_number(r)];
  370. if result=-1 then
  371. internalerror(200603251);
  372. end;
  373. function dwarf_reg_no_error(r:tregister):shortint;
  374. begin
  375. result:=regdwarf_table[findreg_by_number(r)];
  376. end;
  377. function eh_return_data_regno(nr: longint): longint;
  378. begin
  379. if (nr>=0) and (nr<2) then
  380. result:=nr
  381. else
  382. result:=-1;
  383. end;
  384. end.