cpubase.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 armop.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. { Last value of opcode for TCommonAsmOps set below }
  43. LastCommonAsmOp = A_UADD16;
  44. type
  45. {This is a bit of a hack, because there are more than 256 ARM Assembly Ops
  46. But FPC currently can't handle more than 256 elements in a set.}
  47. TCommonAsmOps = Set of A_None .. LastCommonAsmOp;
  48. {*****************************************************************************
  49. Registers
  50. *****************************************************************************}
  51. type
  52. { Number of registers used for indexing in tables }
  53. tregisterindex=0..{$i rarmnor.inc}-1;
  54. const
  55. { Available Superregisters }
  56. {$i rarmsup.inc}
  57. RS_PC = RS_R15;
  58. { No Subregisters }
  59. R_SUBWHOLE = R_SUBNONE;
  60. { Available Registers }
  61. {$i rarmcon.inc}
  62. { aliases }
  63. NR_PC = NR_R15;
  64. { Integer Super registers first and last }
  65. first_int_supreg = RS_R0;
  66. first_int_imreg = $10;
  67. { Float Super register first and last }
  68. first_fpu_supreg = RS_F0;
  69. first_fpu_imreg = $08;
  70. { MM Super register first and last }
  71. first_mm_supreg = RS_S0;
  72. first_mm_imreg = $30;
  73. { TODO: Calculate bsstart}
  74. regnumber_count_bsstart = 128;
  75. regnumber_table : array[tregisterindex] of tregister = (
  76. {$i rarmnum.inc}
  77. );
  78. regstabs_table : array[tregisterindex] of shortint = (
  79. {$i rarmsta.inc}
  80. );
  81. regdwarf_table : array[tregisterindex] of shortint = (
  82. {$i rarmdwa.inc}
  83. );
  84. { registers which may be destroyed by calls }
  85. VOLATILE_INTREGISTERS = [RS_R0..RS_R3,RS_R12..RS_R14];
  86. VOLATILE_FPUREGISTERS = [RS_F0..RS_F3];
  87. VOLATILE_MMREGISTERS = [RS_D0..RS_D7,RS_D16..RS_D31];
  88. VOLATILE_INTREGISTERS_DARWIN = [RS_R0..RS_R3,RS_R9,RS_R12..RS_R14];
  89. {*****************************************************************************
  90. Instruction post fixes
  91. *****************************************************************************}
  92. type
  93. { ARM instructions load/store and arithmetic instructions
  94. can have several instruction post fixes which are collected
  95. in this enumeration
  96. }
  97. TOpPostfix = (PF_None,
  98. { update condition flags
  99. or floating point single }
  100. PF_S,
  101. { floating point size }
  102. PF_D,PF_E,PF_P,PF_EP,
  103. { exchange }
  104. PF_X,
  105. { rounding }
  106. PF_R,
  107. { load/store }
  108. PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T,
  109. { multiple load/store address modes }
  110. PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA,
  111. { multiple load/store vfp address modes }
  112. PF_IAD,PF_DBD,PF_FDD,PF_EAD,
  113. PF_IAS,PF_DBS,PF_FDS,PF_EAS,
  114. PF_IAX,PF_DBX,PF_FDX,PF_EAX,
  115. { VFP postfixes }
  116. PF_8,PF_16,PF_32,PF_64,
  117. PF_I8,PF_I16,PF_I32,PF_I64,
  118. PF_S8,PF_S16,PF_S32,PF_S64,
  119. PF_U8,PF_U16,PF_U32,PF_U64,
  120. PF_P8, // polynomial
  121. PF_F32,PF_F64,
  122. PF_F32F64,PF_F64F32,
  123. PF_F32S16,PF_F32U16,PF_S16F32,PF_U16F32,
  124. PF_F64S16,PF_F64U16,PF_S16F64,PF_U16F64,
  125. PF_F32S32,PF_F32U32,PF_S32F32,PF_U32F32,
  126. PF_F64S32,PF_F64U32,PF_S32F64,PF_U32F64
  127. );
  128. TOpPostfixes = set of TOpPostfix;
  129. TRoundingMode = (RM_None,RM_P,RM_M,RM_Z);
  130. const
  131. cgsize2fpuoppostfix : array[OS_NO..OS_F128] of toppostfix = (
  132. PF_None,
  133. PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,
  134. PF_S,PF_D,PF_E,PF_None,PF_None);
  135. oppostfix2str : array[TOpPostfix] of string[8] = ('',
  136. 's',
  137. 'd','e','p','ep',
  138. 'x',
  139. 'r',
  140. 'b','sb','bt','h','sh','t',
  141. 'ia','ib','da','db','fd','fa','ed','ea',
  142. 'iad','dbd','fdd','ead',
  143. 'ias','dbs','fds','eas',
  144. 'iax','dbx','fdx','eax',
  145. '.8','.16','.32','.64',
  146. '.i8','.i16','.i32','.i64',
  147. '.s8','.s16','.s32','.s64',
  148. '.u8','.u16','.u32','.u64',
  149. '.p8',
  150. '.f32','.f64',
  151. '.f32.f64','.f64.f32',
  152. '.f32.s16','.f32.u16','.s16.f32','.u16.f32',
  153. '.f64.s16','.f64.u16','.s16.f64','.u16.f64',
  154. '.f32.s32','.f32.u32','.s32.f32','.u32.f32',
  155. '.f64.s32','.f64.u32','.s32.f64','.u32.f64');
  156. roundingmode2str : array[TRoundingMode] of string[1] = ('',
  157. 'p','m','z');
  158. {*****************************************************************************
  159. Conditions
  160. *****************************************************************************}
  161. type
  162. TAsmCond=(C_None,
  163. C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  164. C_GE,C_LT,C_GT,C_LE,C_AL,C_NV
  165. );
  166. TAsmConds = set of TAsmCond;
  167. const
  168. cond2str : array[TAsmCond] of string[2]=('',
  169. 'eq','ne','cs','cc','mi','pl','vs','vc','hi','ls',
  170. 'ge','lt','gt','le','al','nv'
  171. );
  172. uppercond2str : array[TAsmCond] of string[2]=('',
  173. 'EQ','NE','CS','CC','MI','PL','VS','VC','HI','LS',
  174. 'GE','LT','GT','LE','AL','NV'
  175. );
  176. {*****************************************************************************
  177. Flags
  178. *****************************************************************************}
  179. type
  180. TResFlags = (F_EQ,F_NE,F_CS,F_CC,F_MI,F_PL,F_VS,F_VC,F_HI,F_LS,
  181. F_GE,F_LT,F_GT,F_LE);
  182. {*****************************************************************************
  183. Operands
  184. *****************************************************************************}
  185. taddressmode = (AM_OFFSET,AM_PREINDEXED,AM_POSTINDEXED);
  186. tshiftmode = (SM_None,SM_LSL,SM_LSR,SM_ASR,SM_ROR,SM_RRX);
  187. tupdatereg = (UR_None,UR_Update);
  188. pshifterop = ^tshifterop;
  189. tshifterop = record
  190. shiftmode : tshiftmode;
  191. rs : tregister;
  192. shiftimm : byte;
  193. end;
  194. tcpumodeflag = (mfA, mfI, mfF);
  195. tcpumodeflags = set of tcpumodeflag;
  196. tspecialregflag = (srC, srX, srS, srF);
  197. tspecialregflags = set of tspecialregflag;
  198. {*****************************************************************************
  199. Constants
  200. *****************************************************************************}
  201. const
  202. max_operands = 6;
  203. maxintregs = 15;
  204. maxfpuregs = 8;
  205. maxaddrregs = 0;
  206. {*****************************************************************************
  207. Operand Sizes
  208. *****************************************************************************}
  209. type
  210. topsize = (S_NO,
  211. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  212. S_IS,S_IL,S_IQ,
  213. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  214. );
  215. {*****************************************************************************
  216. Constants
  217. *****************************************************************************}
  218. const
  219. maxvarregs = 7;
  220. varregs : Array [1..maxvarregs] of tsuperregister =
  221. (RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,RS_R9,RS_R10);
  222. maxfpuvarregs = 4;
  223. fpuvarregs : Array [1..maxfpuvarregs] of tsuperregister =
  224. (RS_F4,RS_F5,RS_F6,RS_F7);
  225. {*****************************************************************************
  226. Default generic sizes
  227. *****************************************************************************}
  228. { Defines the default address size for a processor, }
  229. OS_ADDR = OS_32;
  230. { the natural int size for a processor,
  231. has to match osuinttype/ossinttype as initialized in psystem }
  232. OS_INT = OS_32;
  233. OS_SINT = OS_S32;
  234. { the maximum float size for a processor, }
  235. OS_FLOAT = OS_F64;
  236. { the size of a vector register for a processor }
  237. OS_VECTOR = OS_M32;
  238. {*****************************************************************************
  239. Generic Register names
  240. *****************************************************************************}
  241. { Stack pointer register }
  242. NR_STACK_POINTER_REG = NR_R13;
  243. RS_STACK_POINTER_REG = RS_R13;
  244. { Frame pointer register (initialized in tcpuprocinfo.init_framepointer) }
  245. RS_FRAME_POINTER_REG: tsuperregister = RS_NO;
  246. NR_FRAME_POINTER_REG: tregister = NR_NO;
  247. { Register for addressing absolute data in a position independant way,
  248. such as in PIC code. The exact meaning is ABI specific. For
  249. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  250. }
  251. NR_PIC_OFFSET_REG = NR_R9;
  252. { Results are returned in this register (32-bit values) }
  253. NR_FUNCTION_RETURN_REG = NR_R0;
  254. RS_FUNCTION_RETURN_REG = RS_R0;
  255. { The value returned from a function is available in this register }
  256. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  257. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  258. NR_FPU_RESULT_REG = NR_F0;
  259. NR_MM_RESULT_REG = NR_D0;
  260. NR_RETURN_ADDRESS_REG = NR_R14;
  261. { Offset where the parent framepointer is pushed }
  262. PARENT_FRAMEPOINTER_OFFSET = 0;
  263. NR_DEFAULTFLAGS = NR_CPSR;
  264. RS_DEFAULTFLAGS = RS_CPSR;
  265. { Low part of 64bit return value }
  266. function NR_FUNCTION_RESULT64_LOW_REG: tregister;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  267. function RS_FUNCTION_RESULT64_LOW_REG: shortint;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  268. { High part of 64bit return value }
  269. function NR_FUNCTION_RESULT64_HIGH_REG: tregister;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  270. function RS_FUNCTION_RESULT64_HIGH_REG: shortint;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  271. {*****************************************************************************
  272. GCC /ABI linking information
  273. *****************************************************************************}
  274. const
  275. { Required parameter alignment when calling a routine declared as
  276. stdcall and cdecl. The alignment value should be the one defined
  277. by GCC or the target ABI.
  278. The value of this constant is equal to the constant
  279. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  280. }
  281. std_param_align = 4;
  282. {*****************************************************************************
  283. Helpers
  284. *****************************************************************************}
  285. { Returns the tcgsize corresponding with the size of reg.}
  286. function reg_cgsize(const reg: tregister) : tcgsize;
  287. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  288. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  289. procedure inverse_flags(var f: TResFlags);
  290. function flags_to_cond(const f: TResFlags) : TAsmCond;
  291. function findreg_by_number(r:Tregister):tregisterindex;
  292. function std_regnum_search(const s:string):Tregister;
  293. function std_regname(r:Tregister):string;
  294. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  295. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  296. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  297. function condition_in(const Subset, c: TAsmCond): Boolean;
  298. procedure shifterop_reset(var so : tshifterop); {$ifdef USEINLINE}inline;{$endif USEINLINE}
  299. function is_pc(const r : tregister) : boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  300. function is_shifter_const(d : aint;var imm_shift : byte) : boolean;
  301. function is_thumb_imm(d: aint): boolean;
  302. { Returns true if d is a valid constant for thumb 32 bit,
  303. doesn't handle ROR_C detection }
  304. function is_thumb32_imm(d : aint) : boolean;
  305. function split_into_shifter_const(value : aint;var imm1: dword; var imm2: dword):boolean;
  306. function is_continuous_mask(d : aword;var lsb, width: byte) : boolean;
  307. function dwarf_reg(r:tregister):shortint;
  308. function dwarf_reg_no_error(r:tregister):shortint;
  309. function eh_return_data_regno(nr: longint): longint;
  310. function IsIT(op: TAsmOp) : boolean;
  311. function GetITLevels(op: TAsmOp) : longint;
  312. function GenerateARMCode : boolean;
  313. function GenerateThumbCode : boolean;
  314. function GenerateThumb2Code : boolean;
  315. function IsVFPFloatImmediate(ft : tfloattype;value : bestreal) : boolean;
  316. implementation
  317. uses
  318. systems,rgBase,verbose;
  319. const
  320. std_regname_table : TRegNameTable = (
  321. {$i rarmstd.inc}
  322. );
  323. regnumber_index : array[tregisterindex] of tregisterindex = (
  324. {$i rarmrni.inc}
  325. );
  326. std_regname_index : array[tregisterindex] of tregisterindex = (
  327. {$i rarmsri.inc}
  328. );
  329. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  330. begin
  331. case regtype of
  332. R_MMREGISTER:
  333. begin
  334. case s of
  335. { records passed in MM registers }
  336. OS_32,
  337. OS_F32:
  338. cgsize2subreg:=R_SUBFS;
  339. OS_64,
  340. OS_F64:
  341. cgsize2subreg:=R_SUBFD;
  342. else
  343. internalerror(2009112701);
  344. end;
  345. end;
  346. else
  347. cgsize2subreg:=R_SUBWHOLE;
  348. end;
  349. end;
  350. function reg_cgsize(const reg: tregister): tcgsize;
  351. begin
  352. case getregtype(reg) of
  353. R_INTREGISTER :
  354. reg_cgsize:=OS_32;
  355. R_FPUREGISTER :
  356. reg_cgsize:=OS_F80;
  357. R_MMREGISTER :
  358. begin
  359. case getsubreg(reg) of
  360. R_SUBFD,
  361. R_SUBMMWHOLE:
  362. result:=OS_F64;
  363. R_SUBFS:
  364. result:=OS_F32;
  365. else
  366. internalerror(2009112903);
  367. end;
  368. end;
  369. else
  370. internalerror(200303181);
  371. end;
  372. end;
  373. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  374. begin
  375. { This isn't 100% perfect because the arm allows jumps also by writing to PC=R15.
  376. To overcome this problem we simply forbid that FPC generates jumps by loading R15 }
  377. is_calljmp:= o in [A_B,A_BL,A_BX,A_BLX];
  378. end;
  379. procedure inverse_flags(var f: TResFlags);
  380. const
  381. inv_flags: array[TResFlags] of TResFlags =
  382. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  383. F_LT,F_GE,F_LE,F_GT);
  384. begin
  385. f:=inv_flags[f];
  386. end;
  387. function flags_to_cond(const f: TResFlags) : TAsmCond;
  388. const
  389. flag_2_cond: array[F_EQ..F_LE] of TAsmCond =
  390. (C_EQ,C_NE,C_CS,C_CC,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  391. C_GE,C_LT,C_GT,C_LE);
  392. begin
  393. if f>high(flag_2_cond) then
  394. internalerror(200112301);
  395. result:=flag_2_cond[f];
  396. end;
  397. function findreg_by_number(r:Tregister):tregisterindex;
  398. begin
  399. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  400. end;
  401. function std_regnum_search(const s:string):Tregister;
  402. begin
  403. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  404. end;
  405. function std_regname(r:Tregister):string;
  406. var
  407. p : tregisterindex;
  408. begin
  409. p:=findreg_by_number_table(r,regnumber_index);
  410. if p<>0 then
  411. result:=std_regname_table[p]
  412. else
  413. result:=generic_regname(r);
  414. end;
  415. procedure shifterop_reset(var so : tshifterop);{$ifdef USEINLINE}inline;{$endif USEINLINE}
  416. begin
  417. FillChar(so,sizeof(so),0);
  418. end;
  419. function is_pc(const r : tregister) : boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  420. begin
  421. is_pc:=(r=NR_R15);
  422. end;
  423. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  424. const
  425. inverse: array[TAsmCond] of TAsmCond=(C_None,
  426. C_NE,C_EQ,C_CC,C_CS,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  427. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  428. );
  429. begin
  430. result := inverse[c];
  431. end;
  432. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  433. begin
  434. result := c1 = c2;
  435. end;
  436. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  437. function condition_in(const Subset, c: TAsmCond): Boolean;
  438. begin
  439. Result := (c = C_None) or conditions_equal(Subset, c);
  440. { Please update as necessary. [Kit] }
  441. if not Result then
  442. case Subset of
  443. C_EQ:
  444. Result := (c in [C_GE, C_LE]);
  445. C_LT:
  446. Result := (c in [C_LE]);
  447. C_GT:
  448. Result := (c in [C_GE]);
  449. else
  450. Result := False;
  451. end;
  452. end;
  453. function is_shifter_const(d : aint;var imm_shift : byte) : boolean;
  454. var
  455. i : longint;
  456. begin
  457. if GenerateThumb2Code then
  458. begin
  459. for i:=0 to 24 do
  460. begin
  461. if (dword(d) and not($ff shl i))=0 then
  462. begin
  463. imm_shift:=i;
  464. result:=true;
  465. exit;
  466. end;
  467. end;
  468. end
  469. else
  470. begin
  471. for i:=0 to 15 do
  472. begin
  473. if (dword(d) and not(roldword($ff,i*2)))=0 then
  474. begin
  475. imm_shift:=i*2;
  476. result:=true;
  477. exit;
  478. end;
  479. end;
  480. end;
  481. result:=false;
  482. end;
  483. function is_thumb_imm(d: aint): boolean;
  484. begin
  485. result:=(d and $FF) = d;
  486. end;
  487. function is_thumb32_imm(d: aint): boolean;
  488. var
  489. t : aint;
  490. i : longint;
  491. begin
  492. {Loading 0-255 is simple}
  493. if (d and $FF) = d then
  494. result:=true
  495. { If top and bottom are equal, check if either all 4 bytes are equal
  496. or byte 0 and 2 or byte 1 and 3 are equal }
  497. else if ((d shr 16)=(d and $FFFF)) and
  498. (
  499. ((d and $FF00FF00) = 0) or
  500. ((d and $00FF00FF) = 0) or
  501. ((d shr 8)=(d and $FF))
  502. ) then
  503. result:=true
  504. {Can an 8-bit value be shifted accordingly?}
  505. else
  506. begin
  507. result:=false;
  508. for i:=8 to 31 do
  509. begin
  510. t:=RolDWord(d,i);
  511. if ((t and $FF)=t) and
  512. ((t and $80)=$80) then
  513. begin
  514. result:=true;
  515. exit;
  516. end;
  517. end;
  518. end;
  519. end;
  520. {$push}
  521. { Disable range and overflow checking here }
  522. {$R-}{$Q-}
  523. function is_continuous_mask(d : aword;var lsb, width: byte) : boolean;
  524. var
  525. msb : byte;
  526. begin
  527. lsb:=BsfDword(d);
  528. msb:=BsrDword(d);
  529. width:=msb-lsb+1;
  530. result:=(lsb<>255) and (msb<>255) and (aword(((1 shl (msb-lsb+1))-1) shl lsb) = d);
  531. end;
  532. {$pop}
  533. function split_into_shifter_const(value : aint;var imm1: dword; var imm2: dword) : boolean;
  534. var
  535. d, i, i2: Dword;
  536. begin
  537. Result:=false;
  538. {Thumb2 is not supported (YET?)}
  539. if GenerateThumb2Code then exit;
  540. d:=DWord(value);
  541. for i:=0 to 15 do
  542. begin
  543. imm1:=d and rordword($FF, I*2);
  544. imm2:=d and not (imm1); {remove already found bits}
  545. {is the remainder a shifterconst? YAY! we've done it!}
  546. {Could we start from i instead of 0?}
  547. for i2:=0 to 15 do
  548. begin
  549. if (imm2 and not(rordword($FF,i2*2)))=0 then
  550. begin
  551. result:=true;
  552. exit;
  553. end;
  554. end;
  555. end;
  556. end;
  557. function dwarf_reg(r:tregister):shortint;
  558. begin
  559. result:=regdwarf_table[findreg_by_number(r)];
  560. if result=-1 then
  561. internalerror(200603251);
  562. end;
  563. function dwarf_reg_no_error(r:tregister):shortint;
  564. begin
  565. result:=regdwarf_table[findreg_by_number(r)];
  566. end;
  567. function eh_return_data_regno(nr: longint): longint;
  568. begin
  569. if (nr>=0) and (nr<2) then
  570. result:=nr
  571. else
  572. result:=-1;
  573. end;
  574. { Low part of 64bit return value }
  575. function NR_FUNCTION_RESULT64_LOW_REG: tregister; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  576. begin
  577. if target_info.endian=endian_little then
  578. result:=NR_R0
  579. else
  580. result:=NR_R1;
  581. end;
  582. function RS_FUNCTION_RESULT64_LOW_REG: shortint; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  583. begin
  584. if target_info.endian=endian_little then
  585. result:=RS_R0
  586. else
  587. result:=RS_R1;
  588. end;
  589. { High part of 64bit return value }
  590. function NR_FUNCTION_RESULT64_HIGH_REG: tregister; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  591. begin
  592. if target_info.endian=endian_little then
  593. result:=NR_R1
  594. else
  595. result:=NR_R0;
  596. end;
  597. function RS_FUNCTION_RESULT64_HIGH_REG: shortint; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  598. begin
  599. if target_info.endian=endian_little then
  600. result:=RS_R1
  601. else
  602. result:=RS_R0;
  603. end;
  604. function IsIT(op: TAsmOp) : boolean;
  605. begin
  606. case op of
  607. A_IT,
  608. A_ITE, A_ITT,
  609. A_ITEE, A_ITTE, A_ITET, A_ITTT,
  610. A_ITEEE, A_ITTEE, A_ITETE, A_ITTTE,
  611. A_ITEET, A_ITTET, A_ITETT, A_ITTTT:
  612. result:=true;
  613. else
  614. result:=false;
  615. end;
  616. end;
  617. function GetITLevels(op: TAsmOp) : longint;
  618. begin
  619. case op of
  620. A_IT:
  621. result:=1;
  622. A_ITE, A_ITT:
  623. result:=2;
  624. A_ITEE, A_ITTE, A_ITET, A_ITTT:
  625. result:=3;
  626. A_ITEEE, A_ITTEE, A_ITETE, A_ITTTE,
  627. A_ITEET, A_ITTET, A_ITETT, A_ITTTT:
  628. result:=4;
  629. else
  630. result:=0;
  631. end;
  632. end;
  633. function GenerateARMCode : boolean;
  634. begin
  635. Result:=current_settings.instructionset=is_arm;
  636. end;
  637. function GenerateThumbCode : boolean;
  638. begin
  639. Result:=(current_settings.instructionset=is_thumb) and not(CPUARM_HAS_THUMB2 in cpu_capabilities[current_settings.cputype]);
  640. end;
  641. function GenerateThumb2Code : boolean;
  642. begin
  643. Result:=(current_settings.instructionset=is_thumb) and (CPUARM_HAS_THUMB2 in cpu_capabilities[current_settings.cputype]);
  644. end;
  645. function IsVFPFloatImmediate(ft : tfloattype;value : bestreal) : boolean;
  646. var
  647. singlerec : tcompsinglerec;
  648. doublerec : tcompdoublerec;
  649. begin
  650. Result:=false;
  651. case ft of
  652. s32real:
  653. begin
  654. singlerec.value:=value;
  655. singlerec:=tcompsinglerec(NtoLE(DWord(singlerec)));
  656. Result:=(singlerec.bytes[0]=0) and (singlerec.bytes[1]=0) and ((singlerec.bytes[2] and 7)=0) and
  657. (((singlerec.bytes[3] and $7e)=$40) or ((singlerec.bytes[3] and $7e)=$3e));
  658. end;
  659. s64real:
  660. begin
  661. doublerec.value:=value;
  662. doublerec:=tcompdoublerec(NtoLE(QWord(doublerec)));
  663. Result:=(doublerec.bytes[0]=0) and (doublerec.bytes[1]=0) and (doublerec.bytes[2]=0) and
  664. (doublerec.bytes[3]=0) and (doublerec.bytes[4]=0) and (doublerec.bytes[5]=0) and
  665. ((((doublerec.bytes[6] and $c0)=$0) and ((doublerec.bytes[7] and $7f)=$40)) or
  666. (((doublerec.bytes[6] and $c0)=$c0) and ((doublerec.bytes[7] and $7f)=$3f)));
  667. end;
  668. else
  669. ;
  670. end;
  671. end;
  672. end.