cpubase.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. {
  2. Copyright (c) 1998-2012 by Florian Klaempfl and Peter Vreman
  3. Copyright (c) 2014 by Jonas Maebe and Florian Klaempfl
  4. Contains the base types for Aarch64
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { Base unit for processor information. This unit contains
  19. enumerations of registers, opcodes, sizes, and other
  20. such things which are processor specific.
  21. }
  22. unit cpubase;
  23. {$define USEINLINE}
  24. {$i fpcdefs.inc}
  25. interface
  26. uses
  27. cutils,cclasses,
  28. globtype,globals,
  29. cpuinfo,
  30. aasmbase,
  31. cgbase
  32. ;
  33. {*****************************************************************************
  34. Assembler Opcodes
  35. *****************************************************************************}
  36. type
  37. TAsmOp= {$i a64op.inc}
  38. { This should define the array of instructions as string }
  39. op2strtable=array[tasmop] of string[11];
  40. const
  41. { First value of opcode enumeration }
  42. firstop = low(tasmop);
  43. { Last value of opcode enumeration }
  44. lastop = high(tasmop);
  45. { Last value of opcode for TCommonAsmOps set below }
  46. LastCommonAsmOp = A_MOV;
  47. type
  48. { See comment for this type in arm/cpubase.pas }
  49. TCommonAsmOps = Set of A_None .. LastCommonAsmOp;
  50. {*****************************************************************************
  51. Registers
  52. *****************************************************************************}
  53. type
  54. { Number of registers used for indexing in tables }
  55. tregisterindex=0..{$i ra64nor.inc}-1;
  56. const
  57. { Available Superregisters }
  58. {$i ra64sup.inc}
  59. RS_IP0 = RS_X16;
  60. RS_IP1 = RS_X17;
  61. RS_XR = RS_X8;
  62. R_SUBWHOLE = R_SUBQ;
  63. { Available Registers }
  64. {$i ra64con.inc}
  65. NR_IP0 = NR_X16;
  66. NR_IP1 = NR_X17;
  67. NR_XR = NR_X8;
  68. { Integer Super registers first and last }
  69. first_int_supreg = RS_X0;
  70. { xzr and sp take up a separate super register because some instructions
  71. are ambiguous otherwise }
  72. first_int_imreg = $21;
  73. { Integer Super registers first and last }
  74. first_fpu_supreg = RS_S0;
  75. first_fpu_imreg = $20;
  76. { MM Super register first and last }
  77. first_mm_supreg = RS_S0;
  78. first_mm_imreg = $20;
  79. { Required parameter alignment when calling a routine declared as
  80. stdcall and cdecl. The alignment value should be the one defined
  81. by GCC or the target ABI.
  82. The value of this constant is equal to the constant
  83. PARM_BOUNDARY / BITS_PER_UNIT in the GCC source.
  84. }
  85. std_param_align = 8;
  86. { TODO: Calculate bsstart}
  87. regnumber_count_bsstart = 512;
  88. regnumber_table : array[tregisterindex] of tregister = (
  89. {$i ra64num.inc}
  90. );
  91. regstabs_table : array[tregisterindex] of shortint = (
  92. {$i ra64sta.inc}
  93. );
  94. regdwarf_table : array[tregisterindex] of shortint = (
  95. {$i ra64dwa.inc}
  96. );
  97. { registers which may be destroyed by calls }
  98. VOLATILE_INTREGISTERS = [RS_X0..RS_X18,RS_X30];
  99. VOLATILE_MMREGISTERS = [RS_D0..RS_D7,RS_D16..RS_D31];
  100. {*****************************************************************************
  101. Instruction post fixes
  102. *****************************************************************************}
  103. type
  104. { ARM instructions load/store and arithmetic instructions
  105. can have several instruction post fixes which are collected
  106. in this enumeration
  107. }
  108. TOpPostfix = (PF_None,
  109. { update condition flags }
  110. PF_S,
  111. { load/store sizes }
  112. PF_B,PF_SB,PF_H,PF_SH,PF_W,PF_SW
  113. );
  114. TOpPostfixes = set of TOpPostfix;
  115. const
  116. tcgsizep2size: array[tcgsize] of byte =
  117. {OS_NO }
  118. (0,
  119. {OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128}
  120. 0, 1, 2, 3, 4, 0, 1, 2, 3, 4,
  121. {OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,}
  122. 2, 3, 0, 3, 4,
  123. {OS_M8, OS_M16, OS_M32, OS_M64, OS_M128, OS_M256, OS_M512}
  124. 0, 1, 2, 3, 4, 5, 6);
  125. oppostfix2str: array[TOpPostfix] of string[2] = ('',
  126. 's',
  127. 'b','sb','h','sh','w','sw');
  128. {*****************************************************************************
  129. Conditions
  130. *****************************************************************************}
  131. type
  132. TAsmCond=(C_None,
  133. C_EQ,C_NE,C_HS,C_LO,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  134. C_GE,C_LT,C_GT,C_LE,C_AL,C_NV
  135. );
  136. TAsmConds = set of TAsmCond;
  137. const
  138. C_CS = C_HS;
  139. C_CC = C_LO;
  140. cond2str : array[TAsmCond] of string[2]=('',
  141. 'eq','ne','hs','lo','mi','pl','vs','vc','hi','ls',
  142. 'ge','lt','gt','le','al','nv'
  143. );
  144. uppercond2str : array[TAsmCond] of string[2]=('',
  145. 'EQ','NE','HS','LO','MI','PL','VS','VC','HI','LS',
  146. 'GE','LT','GT','LE','AL','NV'
  147. );
  148. {*****************************************************************************
  149. Flags
  150. *****************************************************************************}
  151. type
  152. TResFlags = (F_EQ,F_NE,F_CS,F_CC,F_MI,F_PL,F_VS,F_VC,F_HI,F_LS,
  153. F_GE,F_LT,F_GT,F_LE);
  154. const
  155. F_HS = F_CS;
  156. F_LO = F_CC;
  157. {*****************************************************************************
  158. Operands
  159. *****************************************************************************}
  160. type
  161. taddressmode = (AM_OFFSET,AM_PREINDEXED,AM_POSTINDEXED);
  162. tshiftmode = (SM_None,
  163. { shifted register instructions. LSL can also be used for
  164. the index register of certain loads/stores }
  165. SM_LSL,SM_LSR,SM_ASR,SM_ROR,
  166. { extended register instructions: zero/sign extension +
  167. optional shift (interpreted as LSL after extension)
  168. -- the index register of certain loads/stores can be
  169. extended via (s|u)xtw with a shiftval of either 0 or
  170. log2(transfer size of the load/store)
  171. }
  172. SM_UXTB,SM_UXTH,SM_UXTW,SM_UXTX,SM_SXTB,SM_SXTH,SM_SXTW,SM_SXTX);
  173. tupdatereg = (UR_None,UR_Update);
  174. pshifterop = ^tshifterop;
  175. tshifterop = record
  176. shiftmode : tshiftmode;
  177. shiftimm : byte;
  178. end;
  179. {*****************************************************************************
  180. Constants
  181. *****************************************************************************}
  182. const
  183. max_operands = 6;
  184. maxintregs = 32;
  185. maxfpuregs = 32;
  186. maxaddrregs = 0;
  187. shiftedregmodes = [SM_LSL,SM_UXTB,SM_UXTH,SM_UXTW,SM_UXTX,SM_SXTB,SM_SXTH,SM_SXTW,SM_SXTX];
  188. extendedregmodes = [SM_LSL,SM_LSR,SM_ASR];
  189. {*****************************************************************************
  190. Operand Sizes
  191. *****************************************************************************}
  192. type
  193. topsize = (S_NO,
  194. S_B,S_W,S_L,S_BW,S_BL,S_WL,
  195. S_IS,S_IL,S_IQ,
  196. S_FS,S_FL,S_FX,S_D,S_Q,S_FV,S_FXX
  197. );
  198. {*****************************************************************************
  199. Default generic sizes
  200. *****************************************************************************}
  201. const
  202. { Defines the default address size for a processor, }
  203. OS_ADDR = OS_64;
  204. { the natural int size for a processor,
  205. has to match osuinttype/ossinttype as initialized in psystem }
  206. OS_INT = OS_64;
  207. OS_SINT = OS_S64;
  208. { the maximum float size for a processor, }
  209. OS_FLOAT = OS_F64;
  210. { the size of a vector register for a processor }
  211. OS_VECTOR = OS_M128;
  212. {*****************************************************************************
  213. Generic Register names
  214. *****************************************************************************}
  215. NR_FP = NR_X29;
  216. RS_FP = RS_X29;
  217. NR_WFP = NR_W29;
  218. RS_WFP = RS_W29;
  219. NR_LR = NR_X30;
  220. RS_LR = RS_X30;
  221. NR_WLR = NR_W30;
  222. RS_WLR = RS_W30;
  223. { Stack pointer register }
  224. NR_STACK_POINTER_REG = NR_SP;
  225. RS_STACK_POINTER_REG = RS_SP;
  226. { Frame pointer register }
  227. NR_FRAME_POINTER_REG = NR_X29;
  228. RS_FRAME_POINTER_REG = RS_X29;
  229. { Register for addressing absolute data in a position independant way,
  230. such as in PIC code. The exact meaning is ABI specific. For
  231. further information look at GCC source : PIC_OFFSET_TABLE_REGNUM
  232. }
  233. NR_PIC_OFFSET_REG = NR_X18;
  234. { Results are returned in this register (32-bit values) }
  235. NR_FUNCTION_RETURN_REG = NR_X0;
  236. RS_FUNCTION_RETURN_REG = RS_X0;
  237. { The value returned from a function is available in this register }
  238. NR_FUNCTION_RESULT_REG = NR_FUNCTION_RETURN_REG;
  239. RS_FUNCTION_RESULT_REG = RS_FUNCTION_RETURN_REG;
  240. NR_FPU_RESULT_REG = NR_NO;
  241. NR_MM_RESULT_REG = NR_D0;
  242. NR_RETURN_ADDRESS_REG = NR_LR;
  243. { Offset where the parent framepointer is pushed }
  244. PARENT_FRAMEPOINTER_OFFSET = 0;
  245. NR_DEFAULTFLAGS = NR_NZCV;
  246. RS_DEFAULTFLAGS = RS_NZCV;
  247. {*****************************************************************************
  248. Helpers
  249. *****************************************************************************}
  250. { Returns the tcgsize corresponding with the size of reg.}
  251. function reg_cgsize(const reg: tregister) : tcgsize;
  252. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  253. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  254. procedure inverse_flags(var f: TResFlags);
  255. function flags_to_cond(const f: TResFlags) : TAsmCond;
  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. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  262. function condition_in(const Subset, c: TAsmCond): Boolean;
  263. procedure shifterop_reset(var so : tshifterop); {$ifdef USEINLINE}inline;{$endif USEINLINE}
  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. function is_shifter_const(d: aint; size: tcgsize): boolean;
  268. function IsFloatImmediate(ft : tfloattype;value : bestreal) : boolean;
  269. implementation
  270. uses
  271. systems,rgBase,verbose;
  272. const
  273. std_regname_table : TRegNameTable = (
  274. {$i ra64std.inc}
  275. );
  276. regnumber_index : array[tregisterindex] of tregisterindex = (
  277. {$i ra64rni.inc}
  278. );
  279. std_regname_index : array[tregisterindex] of tregisterindex = (
  280. {$i ra64sri.inc}
  281. );
  282. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  283. begin
  284. case regtype of
  285. R_INTREGISTER:
  286. begin
  287. case s of
  288. { there's only Wn and Xn }
  289. OS_64,
  290. OS_S64:
  291. cgsize2subreg:=R_SUBWHOLE;
  292. else
  293. cgsize2subreg:=R_SUBD;
  294. end;
  295. end;
  296. R_MMREGISTER:
  297. begin
  298. case s of
  299. { records }
  300. OS_32,
  301. OS_F32:
  302. cgsize2subreg:=R_SUBMMS;
  303. OS_64,
  304. OS_F64:
  305. cgsize2subreg:=R_SUBMMD;
  306. else
  307. internalerror(2009112701);
  308. end;
  309. end;
  310. else
  311. cgsize2subreg:=R_SUBWHOLE;
  312. end;
  313. end;
  314. function reg_cgsize(const reg: tregister): tcgsize;
  315. begin
  316. case getregtype(reg) of
  317. R_INTREGISTER:
  318. case getsubreg(reg) of
  319. R_SUBD:
  320. result:=OS_32
  321. else
  322. result:=OS_64;
  323. end;
  324. R_MMREGISTER :
  325. begin
  326. case getsubreg(reg) of
  327. R_SUBMMD:
  328. result:=OS_F64;
  329. R_SUBMMS:
  330. result:=OS_F32;
  331. { always use OS_M128, because these could be the top or bottom bytes (or middle in some cases) }
  332. R_SUBMM8B:
  333. result:=OS_M128;
  334. R_SUBMM16B:
  335. result:=OS_M128;
  336. R_SUBMM4H:
  337. result:=OS_M128;
  338. R_SUBMM8H:
  339. result:=OS_M128;
  340. R_SUBMM2S:
  341. result:=OS_M128;
  342. R_SUBMM4S:
  343. result:=OS_M128;
  344. R_SUBMM1D:
  345. result:=OS_M128;
  346. R_SUBMM2D:
  347. result:=OS_M128;
  348. R_SUBMMWHOLE:
  349. result:=OS_M128;
  350. else
  351. internalerror(2009112903);
  352. end;
  353. end;
  354. else
  355. internalerror(200303181);
  356. end;
  357. end;
  358. function is_calljmp(o:tasmop):boolean;{$ifdef USEINLINE}inline;{$endif USEINLINE}
  359. begin
  360. is_calljmp:=o in [A_B,A_BL,A_BLR,A_RET,A_CBNZ,A_CBZ,A_TBNZ,A_TBZ];
  361. end;
  362. procedure inverse_flags(var f: TResFlags);
  363. const
  364. inv_flags: array[TResFlags] of TResFlags =
  365. (F_NE,F_EQ,F_CC,F_CS,F_PL,F_MI,F_VC,F_VS,F_LS,F_HI,
  366. F_LT,F_GE,F_LE,F_GT);
  367. begin
  368. f:=inv_flags[f];
  369. end;
  370. function flags_to_cond(const f: TResFlags) : TAsmCond;
  371. const
  372. flag_2_cond: array[TResFlags] of TAsmCond =
  373. (C_EQ,C_NE,C_HS,C_LO,C_MI,C_PL,C_VS,C_VC,C_HI,C_LS,
  374. C_GE,C_LT,C_GT,C_LE);
  375. begin
  376. if f>high(flag_2_cond) then
  377. internalerror(200112301);
  378. result:=flag_2_cond[f];
  379. end;
  380. function findreg_by_number(r:Tregister):tregisterindex;
  381. begin
  382. result:=rgBase.findreg_by_number_table(r,regnumber_index);
  383. end;
  384. function std_regnum_search(const s:string):Tregister;
  385. begin
  386. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  387. end;
  388. function std_regname(r:Tregister):string;
  389. var
  390. p : tregisterindex;
  391. begin
  392. p:=findreg_by_number_table(r,regnumber_index);
  393. if p<>0 then
  394. result:=std_regname_table[p]
  395. else
  396. result:=generic_regname(r);
  397. end;
  398. procedure shifterop_reset(var so : tshifterop);{$ifdef USEINLINE}inline;{$endif USEINLINE}
  399. begin
  400. FillChar(so,sizeof(so),0);
  401. end;
  402. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  403. const
  404. inverse: array[TAsmCond] of TAsmCond=(C_None,
  405. C_NE,C_EQ,C_LO,C_HS,C_PL,C_MI,C_VC,C_VS,C_LS,C_HI,
  406. C_LT,C_GE,C_LE,C_GT,C_None,C_None
  407. );
  408. begin
  409. result := inverse[c];
  410. end;
  411. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  412. begin
  413. result := c1 = c2;
  414. end;
  415. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  416. function condition_in(const Subset, c: TAsmCond): Boolean;
  417. begin
  418. Result := (c = C_None) or conditions_equal(Subset, c);
  419. { Please update as necessary. [Kit] }
  420. if not Result then
  421. case Subset of
  422. C_EQ:
  423. Result := (c in [C_GE, C_LE]);
  424. C_LT:
  425. Result := (c in [C_LE]);
  426. C_GT:
  427. Result := (c in [C_GE]);
  428. else
  429. Result := False;
  430. end;
  431. end;
  432. function dwarf_reg(r:tregister):shortint;
  433. begin
  434. result:=regdwarf_table[findreg_by_number(r)];
  435. if result=-1 then
  436. internalerror(200603251);
  437. end;
  438. function dwarf_reg_no_error(r:tregister):shortint;
  439. begin
  440. result:=regdwarf_table[findreg_by_number(r)];
  441. end;
  442. function is_shifter_const(d: aint; size: tcgsize): boolean;
  443. var
  444. pattern, checkpattern: qword;
  445. patternlen, maxbits, replicatedlen: longint;
  446. rightmostone, rightmostzero, checkbit, secondrightmostbit: longint;
  447. begin
  448. result:=false;
  449. { patterns with all bits 0 or 1 cannot be represented this way }
  450. if (d=0) then
  451. exit;
  452. case size of
  453. OS_64,
  454. OS_S64:
  455. begin
  456. if d=-1 then
  457. exit;
  458. maxbits:=64;
  459. end
  460. else
  461. begin
  462. if longint(d)=-1 then
  463. exit;
  464. { we'll generate a 32 bit pattern -> ignore upper sign bits in
  465. case of negative longint value }
  466. d:=cardinal(d);
  467. maxbits:=32;
  468. end;
  469. end;
  470. { "The Logical (immediate) instructions accept a bitmask immediate value
  471. that is a 32-bit pattern or a 64-bit pattern viewed as a vector of
  472. identical elements of size e = 2, 4, 8, 16, 32 or, 64 bits. Each
  473. element contains the same sub-pattern, that is a single run of
  474. 1 to (e - 1) nonzero bits from bit 0 followed by zero bits, then
  475. rotated by 0 to (e - 1) bits." (ARMv8 ARM)
  476. Rather than generating all possible patterns and checking whether they
  477. match our constant, we check whether the lowest 2/4/8/... bits are
  478. a valid pattern, and if so whether the constant consists of a
  479. replication of this pattern. Such a valid pattern has the form of
  480. either (regexp notation)
  481. * 1+0+1*
  482. * 0+1+0* }
  483. patternlen:=2;
  484. while patternlen<=maxbits do
  485. begin
  486. { try lowest <patternlen> bits of d as pattern }
  487. if patternlen<>64 then
  488. pattern:=qword(d) and ((qword(1) shl patternlen)-1)
  489. else
  490. pattern:=qword(d);
  491. { valid pattern? If it contains too many 1<->0 transitions, larger
  492. parts of d cannot be a valid pattern either }
  493. rightmostone:=BsfQWord(pattern);
  494. rightmostzero:=BsfQWord(not(pattern));
  495. { pattern all ones or zeroes -> not a valid pattern (but larger ones
  496. can still be valid, since we have too few transitions) }
  497. if (rightmostone<patternlen) and
  498. (rightmostzero<patternlen) then
  499. begin
  500. if rightmostone>rightmostzero then
  501. begin
  502. { we have .*1*0* -> check next zero position by shifting
  503. out the existing zeroes (shr rightmostone), inverting and
  504. then again looking for the rightmost one position }
  505. checkpattern:=not(pattern);
  506. checkbit:=rightmostone;
  507. end
  508. else
  509. begin
  510. { same as above, but for .*0*1* }
  511. checkpattern:=pattern;
  512. checkbit:=rightmostzero;
  513. end;
  514. secondrightmostbit:=BsfQWord(checkpattern shr checkbit)+checkbit;
  515. { if this position is >= patternlen -> ok (1 transition),
  516. otherwise we now have 2 transitions and have to check for a
  517. third (if there is one, abort)
  518. bsf returns 255 if no 1 bit is found, so in that case it's
  519. also ok
  520. }
  521. if secondrightmostbit<patternlen then
  522. begin
  523. secondrightmostbit:=BsfQWord(not(checkpattern) shr secondrightmostbit)+secondrightmostbit;
  524. if secondrightmostbit<patternlen then
  525. exit;
  526. end;
  527. { ok, this is a valid pattern, now does d consist of a
  528. repetition of this pattern? }
  529. replicatedlen:=patternlen;
  530. checkpattern:=pattern;
  531. while replicatedlen<maxbits do
  532. begin
  533. { douplicate current pattern }
  534. checkpattern:=checkpattern or (checkpattern shl replicatedlen);
  535. replicatedlen:=replicatedlen*2;
  536. end;
  537. if qword(d)=checkpattern then
  538. begin
  539. { yes! }
  540. result:=true;
  541. exit;
  542. end;
  543. end;
  544. patternlen:=patternlen*2;
  545. end;
  546. end;
  547. function eh_return_data_regno(nr: longint): longint;
  548. begin
  549. if (nr>=0) and (nr<2) then
  550. result:=nr
  551. else
  552. result:=-1;
  553. end;
  554. function IsFloatImmediate(ft : tfloattype;value : bestreal) : boolean;
  555. var
  556. singlerec : tcompsinglerec;
  557. doublerec : tcompdoublerec;
  558. begin
  559. Result:=false;
  560. case ft of
  561. s32real:
  562. begin
  563. singlerec.value:=value;
  564. singlerec:=tcompsinglerec(NtoLE(DWord(singlerec)));
  565. Result:=(singlerec.bytes[0]=0) and (singlerec.bytes[1]=0) and ((singlerec.bytes[2] and 7)=0) and
  566. (((singlerec.bytes[3] and $7e)=$40) or ((singlerec.bytes[3] and $7e)=$3e));
  567. end;
  568. s64real:
  569. begin
  570. doublerec.value:=value;
  571. doublerec:=tcompdoublerec(NtoLE(QWord(doublerec)));
  572. Result:=(doublerec.bytes[0]=0) and (doublerec.bytes[1]=0) and (doublerec.bytes[2]=0) and
  573. (doublerec.bytes[3]=0) and (doublerec.bytes[4]=0) and (doublerec.bytes[5]=0) and
  574. ((((doublerec.bytes[6] and $c0)=$0) and ((doublerec.bytes[7] and $7f)=$40)) or
  575. (((doublerec.bytes[6] and $c0)=$c0) and ((doublerec.bytes[7] and $7f)=$3f)));
  576. end;
  577. else
  578. ;
  579. end;
  580. end;
  581. end.