cpubase.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. Contains the base types for the i8086, i386 and x86-64 architecture
  4. * This code was inspired by the NASM sources
  5. The Netwide Assembler is Copyright (c) 1996 Simon Tatham and
  6. Julian Hall. All rights reserved.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. {# Base unit for processor information. This unit contains
  21. enumerations of registers, opcodes, sizes, and other
  22. such things which are processor specific.
  23. }
  24. unit cpubase;
  25. {$i fpcdefs.inc}
  26. interface
  27. uses
  28. globals,
  29. cgbase
  30. ;
  31. {*****************************************************************************
  32. Assembler Opcodes
  33. *****************************************************************************}
  34. type
  35. {$if defined(x86_64)}
  36. TAsmOp={$i x8664op.inc}
  37. {$elseif defined(i386)}
  38. TAsmOp={$i i386op.inc}
  39. {$elseif defined(i8086)}
  40. TAsmOp={$i i8086op.inc}
  41. {$endif}
  42. { This should define the array of instructions as string }
  43. op2strtable=array[tasmop] of string[16];
  44. {$ifdef i8086}
  45. ImmInt = SmallInt;
  46. {$else i8086}
  47. ImmInt = Longint;
  48. {$endif i8086}
  49. const
  50. { First value of opcode enumeration }
  51. firstop = low(tasmop);
  52. { Last value of opcode enumeration }
  53. lastop = high(tasmop);
  54. {*****************************************************************************
  55. Registers
  56. *****************************************************************************}
  57. const
  58. { Integer Super registers }
  59. RS_NO = $ffffffff;
  60. RS_RAX = $00; {EAX}
  61. RS_RCX = $01; {ECX}
  62. RS_RDX = $02; {EDX}
  63. RS_RBX = $03; {EBX}
  64. RS_RSI = $04; {ESI}
  65. RS_RDI = $05; {EDI}
  66. RS_RBP = $06; {EBP}
  67. RS_RSP = $07; {ESP}
  68. RS_R8 = $08; {R8}
  69. RS_R9 = $09; {R9}
  70. RS_R10 = $0a; {R10}
  71. RS_R11 = $0b; {R11}
  72. RS_R12 = $0c; {R12}
  73. RS_R13 = $0d; {R13}
  74. RS_R14 = $0e; {R14}
  75. RS_R15 = $0f; {R15}
  76. { create aliases to allow code sharing between x86-64 and i386 }
  77. RS_EAX = RS_RAX;
  78. RS_EBX = RS_RBX;
  79. RS_ECX = RS_RCX;
  80. RS_EDX = RS_RDX;
  81. RS_ESI = RS_RSI;
  82. RS_EDI = RS_RDI;
  83. RS_EBP = RS_RBP;
  84. RS_ESP = RS_RSP;
  85. { create aliases to allow code sharing between i386 and i8086 }
  86. RS_AX = RS_RAX;
  87. RS_BX = RS_RBX;
  88. RS_CX = RS_RCX;
  89. RS_DX = RS_RDX;
  90. RS_SI = RS_RSI;
  91. RS_DI = RS_RDI;
  92. RS_BP = RS_RBP;
  93. RS_SP = RS_RSP;
  94. { Number of first imaginary register }
  95. first_int_imreg = $10;
  96. { Float Super registers }
  97. RS_ST0 = $00;
  98. RS_ST1 = $01;
  99. RS_ST2 = $02;
  100. RS_ST3 = $03;
  101. RS_ST4 = $04;
  102. RS_ST5 = $05;
  103. RS_ST6 = $06;
  104. RS_ST7 = $07;
  105. RS_ST = $08;
  106. { Number of first imaginary register }
  107. first_fpu_imreg = $09;
  108. { MM Super registers }
  109. RS_XMM0 = $00;
  110. RS_XMM1 = $01;
  111. RS_XMM2 = $02;
  112. RS_XMM3 = $03;
  113. RS_XMM4 = $04;
  114. RS_XMM5 = $05;
  115. RS_XMM6 = $06;
  116. RS_XMM7 = $07;
  117. RS_XMM8 = $08;
  118. RS_XMM9 = $09;
  119. RS_XMM10 = $0a;
  120. RS_XMM11 = $0b;
  121. RS_XMM12 = $0c;
  122. RS_XMM13 = $0d;
  123. RS_XMM14 = $0e;
  124. RS_XMM15 = $0f;
  125. RS_XMM16 = $10;
  126. RS_XMM17 = $11;
  127. RS_XMM18 = $12;
  128. RS_XMM19 = $13;
  129. RS_XMM20 = $14;
  130. RS_XMM21 = $15;
  131. RS_XMM22 = $16;
  132. RS_XMM23 = $17;
  133. RS_XMM24 = $18;
  134. RS_XMM25 = $19;
  135. RS_XMM26 = $1a;
  136. RS_XMM27 = $1b;
  137. RS_XMM28 = $1c;
  138. RS_XMM29 = $1d;
  139. RS_XMM30 = $1e;
  140. RS_XMM31 = $1f;
  141. {$if defined(x86_64)}
  142. RS_RFLAGS = $06;
  143. {$elseif defined(i386)}
  144. RS_EFLAGS = $06;
  145. {$elseif defined(i8086)}
  146. RS_FLAGS = $06;
  147. {$endif}
  148. { Number of first imaginary register }
  149. {$ifdef x86_64}
  150. first_mm_imreg = $20;
  151. {$else x86_64}
  152. first_mm_imreg = $08;
  153. {$endif x86_64}
  154. { The subregister that specifies the entire register and an address }
  155. {$if defined(x86_64)}
  156. { Hammer }
  157. R_SUBWHOLE = R_SUBQ;
  158. R_SUBADDR = R_SUBQ;
  159. {$elseif defined(i386)}
  160. { i386 }
  161. R_SUBWHOLE = R_SUBD;
  162. R_SUBADDR = R_SUBD;
  163. {$elseif defined(i8086)}
  164. { i8086 }
  165. R_SUBWHOLE = R_SUBW;
  166. R_SUBADDR = R_SUBW;
  167. {$endif}
  168. { Available Registers }
  169. {$if defined(x86_64)}
  170. {$i r8664con.inc}
  171. {$elseif defined(i386)}
  172. {$i r386con.inc}
  173. {$elseif defined(i8086)}
  174. {$i r8086con.inc}
  175. {$endif}
  176. type
  177. { Number of registers used for indexing in tables }
  178. {$if defined(x86_64)}
  179. tregisterindex=0..{$i r8664nor.inc}-1;
  180. {$elseif defined(i386)}
  181. tregisterindex=0..{$i r386nor.inc}-1;
  182. {$elseif defined(i8086)}
  183. tregisterindex=0..{$i r8086nor.inc}-1;
  184. {$endif}
  185. const
  186. regnumber_table : array[tregisterindex] of tregister = (
  187. {$if defined(x86_64)}
  188. {$i r8664num.inc}
  189. {$elseif defined(i386)}
  190. {$i r386num.inc}
  191. {$elseif defined(i8086)}
  192. {$i r8086num.inc}
  193. {$endif}
  194. );
  195. regstabs_table : array[tregisterindex] of shortint = (
  196. {$if defined(x86_64)}
  197. {$i r8664stab.inc}
  198. {$elseif defined(i386)}
  199. {$i r386stab.inc}
  200. {$elseif defined(i8086)}
  201. {$i r8086stab.inc}
  202. {$endif}
  203. );
  204. regdwarf_table : array[tregisterindex] of shortint = (
  205. {$if defined(x86_64)}
  206. {$i r8664dwrf.inc}
  207. {$elseif defined(i386)}
  208. {$i r386dwrf.inc}
  209. {$elseif defined(i8086)}
  210. {$i r8086dwrf.inc}
  211. {$endif}
  212. );
  213. {$if defined(x86_64)}
  214. RS_DEFAULTFLAGS = RS_RFLAGS;
  215. NR_DEFAULTFLAGS = NR_RFLAGS;
  216. {$elseif defined(i386)}
  217. RS_DEFAULTFLAGS = RS_EFLAGS;
  218. NR_DEFAULTFLAGS = NR_EFLAGS;
  219. {$elseif defined(i8086)}
  220. RS_DEFAULTFLAGS = RS_FLAGS;
  221. NR_DEFAULTFLAGS = NR_FLAGS;
  222. {$endif}
  223. {*****************************************************************************
  224. Conditions
  225. *****************************************************************************}
  226. type
  227. TAsmCond=(C_None,
  228. C_A,C_AE,C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_NA,C_NAE,
  229. C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_NO,C_NP,
  230. C_NS,C_NZ,C_O,C_P,C_PE,C_PO,C_S,C_Z
  231. );
  232. const
  233. cond2str:array[TAsmCond] of string[3]=('',
  234. 'a','ae','b','be','c','e','g','ge','l','le','na','nae',
  235. 'nb','nbe','nc','ne','ng','nge','nl','nle','no','np',
  236. 'ns','nz','o','p','pe','po','s','z'
  237. );
  238. {*****************************************************************************
  239. Flags
  240. *****************************************************************************}
  241. type
  242. TResFlags = (F_E,F_NE,F_G,F_L,F_GE,F_LE,F_C,F_NC,
  243. F_A,F_AE,F_B,F_BE,
  244. F_S,F_NS,F_O,F_NO,
  245. { For IEEE-compliant floating-point compares,
  246. same as normal counterparts but additionally check PF }
  247. F_FE,F_FNE,F_FA,F_FAE,F_FB,F_FBE);
  248. const
  249. FPUFlags = [F_FE,F_FNE,F_FA,F_FAE,F_FB,F_FBE];
  250. FPUFlags2Flags: array[F_FE..F_FBE] of TResFlags = (
  251. F_E,F_NE,F_A,F_AE,F_B,F_BE
  252. );
  253. {*****************************************************************************
  254. Constants
  255. *****************************************************************************}
  256. const
  257. { declare aliases }
  258. LOC_SSEREGISTER = LOC_MMREGISTER;
  259. LOC_CSSEREGISTER = LOC_CMMREGISTER;
  260. max_operands = 4;
  261. maxfpuregs = 8;
  262. {*****************************************************************************
  263. CPU Dependent Constants
  264. *****************************************************************************}
  265. {$i cpubase.inc}
  266. const
  267. {$ifdef x86_64}
  268. topsize2memsize: array[topsize] of integer =
  269. (0, 8,16,32,64,8,8,16,8,16,32,
  270. 16,32,64,
  271. 16,32,64,0,0,
  272. 64,
  273. 0,0,0,
  274. 80,
  275. 128,
  276. 256,
  277. 512
  278. );
  279. {$else}
  280. topsize2memsize: array[topsize] of integer =
  281. (0, 8,16,32,64,8,8,16,
  282. 16,32,64,
  283. 16,32,64,0,0,
  284. 64,
  285. 0,0,0,
  286. 80,
  287. 128,
  288. 256,
  289. 512
  290. );
  291. {$endif}
  292. {*****************************************************************************
  293. Helpers
  294. *****************************************************************************}
  295. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  296. function reg2opsize(r:Tregister):topsize;
  297. function subreg2opsize(sr : tsubregister):topsize;
  298. function reg_cgsize(const reg: tregister): tcgsize;
  299. function is_calljmp(o:tasmop):boolean;
  300. function is_calljmpuncondret(o:tasmop):boolean;
  301. procedure inverse_flags(var f: TResFlags);
  302. function flags_to_cond(const f: TResFlags) : TAsmCond;
  303. function is_segment_reg(r:tregister):boolean;
  304. function findreg_by_number(r:Tregister):tregisterindex;
  305. function std_regnum_search(const s:string):Tregister;
  306. function std_regname(r:Tregister):string;
  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 inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  311. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  312. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  313. function condition_in(const Subset, c: TAsmCond): Boolean;
  314. { checks whether two segment registers are normally equal in the current memory model }
  315. function segment_regs_equal(r1,r2:tregister):boolean;
  316. { checks whether the specified op is an x86 string instruction (e.g. cmpsb, movsd, scasw, etc.) }
  317. function is_x86_string_op(op: TAsmOp): boolean;
  318. { checks whether the specified op is an x86 parameterless string instruction
  319. (e.g. returns true for movsb, cmpsw, etc, but returns false for movs, cmps, etc.) }
  320. function is_x86_parameterless_string_op(op: TAsmOp): boolean;
  321. { checks whether the specified op is an x86 parameterized string instruction
  322. (e.g. returns true for movs, cmps, etc, but returns false for movsb, cmpsb, etc.) }
  323. function is_x86_parameterized_string_op(op: TAsmOp): boolean;
  324. function x86_parameterized_string_op_param_count(op: TAsmOp): shortint;
  325. function x86_param2paramless_string_op(op: TAsmOp): TAsmOp;
  326. function get_x86_string_op_size(op: TAsmOp): TOpSize;
  327. { returns the 0-based operand number (intel syntax) of the ds:[si] param of
  328. a x86 string instruction }
  329. function get_x86_string_op_si_param(op: TAsmOp):shortint;
  330. { returns the 0-based operand number (intel syntax) of the es:[di] param of
  331. a x86 string instruction }
  332. function get_x86_string_op_di_param(op: TAsmOp):shortint;
  333. {$ifdef i8086}
  334. { return whether we need to add an extra FWAIT instruction before the given
  335. instruction, when we're targeting the i8087. This includes almost all x87
  336. instructions, but certain ones, which always have or have not a built in
  337. FWAIT prefix are excluded (e.g. FINIT,FNINIT,etc.). }
  338. function requires_fwait_on_8087(op: TAsmOp): boolean;
  339. {$endif i8086}
  340. function UseAVX: boolean;
  341. function UseAVX512: boolean;
  342. implementation
  343. uses
  344. globtype,
  345. rgbase,verbose,
  346. cpuinfo;
  347. const
  348. {$if defined(x86_64)}
  349. std_regname_table : TRegNameTable = (
  350. {$i r8664std.inc}
  351. );
  352. regnumber_index : array[tregisterindex] of tregisterindex = (
  353. {$i r8664rni.inc}
  354. );
  355. std_regname_index : array[tregisterindex] of tregisterindex = (
  356. {$i r8664sri.inc}
  357. );
  358. {$elseif defined(i386)}
  359. std_regname_table : TRegNameTable = (
  360. {$i r386std.inc}
  361. );
  362. regnumber_index : array[tregisterindex] of tregisterindex = (
  363. {$i r386rni.inc}
  364. );
  365. std_regname_index : array[tregisterindex] of tregisterindex = (
  366. {$i r386sri.inc}
  367. );
  368. {$elseif defined(i8086)}
  369. std_regname_table : TRegNameTable = (
  370. {$i r8086std.inc}
  371. );
  372. regnumber_index : array[tregisterindex] of tregisterindex = (
  373. {$i r8086rni.inc}
  374. );
  375. std_regname_index : array[tregisterindex] of tregisterindex = (
  376. {$i r8086sri.inc}
  377. );
  378. {$endif}
  379. {*****************************************************************************
  380. Helpers
  381. *****************************************************************************}
  382. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  383. begin
  384. case s of
  385. OS_8,OS_S8:
  386. cgsize2subreg:=R_SUBL;
  387. OS_16,OS_S16:
  388. cgsize2subreg:=R_SUBW;
  389. OS_32,OS_S32:
  390. cgsize2subreg:=R_SUBD;
  391. OS_64,OS_S64:
  392. cgsize2subreg:=R_SUBQ;
  393. OS_M64:
  394. cgsize2subreg:=R_SUBNONE;
  395. OS_F32,OS_F64,OS_C64:
  396. case regtype of
  397. R_FPUREGISTER:
  398. cgsize2subreg:=R_SUBWHOLE;
  399. R_MMREGISTER:
  400. case s of
  401. OS_F32:
  402. cgsize2subreg:=R_SUBMMS;
  403. OS_F64:
  404. cgsize2subreg:=R_SUBMMD;
  405. else
  406. internalerror(2009071901);
  407. end;
  408. else
  409. internalerror(2009071902);
  410. end;
  411. OS_M128:
  412. cgsize2subreg:=R_SUBMMX;
  413. OS_M256:
  414. cgsize2subreg:=R_SUBMMY;
  415. OS_M512:
  416. cgsize2subreg:=R_SUBMMZ;
  417. OS_S128,
  418. OS_128,
  419. OS_NO:
  420. { error message should have been thrown already before, so avoid only
  421. an internal error }
  422. cgsize2subreg:=R_SUBNONE;
  423. else
  424. internalerror(200301231);
  425. end;
  426. end;
  427. function reg_cgsize(const reg: tregister): tcgsize;
  428. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  429. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO,OS_NO,OS_F32,OS_F64,OS_NO,OS_M128,OS_M256,OS_M512,OS_NO,OS_NO,OS_NO,OS_NO,OS_NO,OS_NO,OS_NO,OS_NO,OS_NO);
  430. begin
  431. case getregtype(reg) of
  432. R_INTREGISTER :
  433. reg_cgsize:=subreg2cgsize[getsubreg(reg)];
  434. R_FPUREGISTER :
  435. reg_cgsize:=OS_F80;
  436. R_MMXREGISTER:
  437. reg_cgsize:=OS_M64;
  438. R_MMREGISTER:
  439. reg_cgsize:=subreg2cgsize[getsubreg(reg)];
  440. R_SPECIALREGISTER :
  441. case reg of
  442. NR_CS,NR_DS,NR_ES,NR_SS,NR_FS,NR_GS:
  443. reg_cgsize:=OS_16;
  444. {$ifdef x86_64}
  445. NR_DR0..NR_TR7:
  446. reg_cgsize:=OS_64;
  447. {$endif x86_64}
  448. else
  449. reg_cgsize:=OS_32
  450. end;
  451. R_ADDRESSREGISTER:
  452. case reg of
  453. NR_K0..NR_K7: reg_cgsize:=OS_NO;
  454. else internalerror(2003031801);
  455. end;
  456. else
  457. internalerror(2003031802);
  458. end;
  459. end;
  460. function subreg2opsize(sr : tsubregister):topsize;
  461. const
  462. _subreg2opsize : array[tsubregister] of topsize =
  463. (S_NO,S_B,S_B,S_W,S_L,S_Q,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO);
  464. begin
  465. result:=_subreg2opsize[sr];
  466. end;
  467. function reg2opsize(r:Tregister):topsize;
  468. begin
  469. reg2opsize:=S_L;
  470. case getregtype(r) of
  471. R_INTREGISTER :
  472. reg2opsize:=subreg2opsize(getsubreg(r));
  473. R_FPUREGISTER :
  474. reg2opsize:=S_FL;
  475. R_MMXREGISTER,
  476. R_MMREGISTER :
  477. reg2opsize:=S_MD;
  478. R_SPECIALREGISTER :
  479. begin
  480. case r of
  481. NR_CS,NR_DS,NR_ES,
  482. NR_SS,NR_FS,NR_GS :
  483. reg2opsize:=S_W;
  484. else
  485. ;
  486. end;
  487. end;
  488. else
  489. internalerror(200303181);
  490. end;
  491. end;
  492. function is_calljmp(o:tasmop):boolean;
  493. begin
  494. case o of
  495. A_CALL,
  496. {$if defined(i386) or defined(i8086)}
  497. A_JCXZ,
  498. {$endif defined(i386) or defined(i8086)}
  499. A_JECXZ,
  500. {$ifdef x86_64}
  501. A_JRCXZ,
  502. {$endif x86_64}
  503. A_JMP,
  504. A_LOOP,
  505. A_LOOPE,
  506. A_LOOPNE,
  507. A_LOOPNZ,
  508. A_LOOPZ,
  509. A_LCALL,
  510. A_LJMP,
  511. A_Jcc :
  512. is_calljmp:=true;
  513. else
  514. is_calljmp:=false;
  515. end;
  516. end;
  517. function is_calljmpuncondret(o:tasmop):boolean;
  518. begin
  519. case o of
  520. A_CALL,
  521. A_JMP,
  522. A_RET,
  523. A_LCALL,
  524. A_LJMP:
  525. Result:=true;
  526. else
  527. Result:=false;
  528. end;
  529. end;
  530. procedure inverse_flags(var f: TResFlags);
  531. const
  532. inv_flags: array[TResFlags] of TResFlags =
  533. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_NC,F_C,
  534. F_BE,F_B,F_AE,F_A,
  535. F_NS,F_S,F_NO,F_O,
  536. F_FNE,F_FE,F_FBE,F_FB,F_FAE,F_FA);
  537. begin
  538. f:=inv_flags[f];
  539. end;
  540. function flags_to_cond(const f: TResFlags) : TAsmCond;
  541. const
  542. flags_2_cond : array[TResFlags] of TAsmCond =
  543. (C_E,C_NE,C_G,C_L,C_GE,C_LE,C_C,C_NC,C_A,C_AE,C_B,C_BE,C_S,C_NS,C_O,C_NO,
  544. C_None,C_None,C_None,C_None,C_None,C_None);
  545. begin
  546. result := flags_2_cond[f];
  547. if (result=C_None) then
  548. InternalError(2014041302);
  549. end;
  550. function is_segment_reg(r:tregister):boolean;
  551. begin
  552. case r of
  553. NR_CS,NR_DS,NR_ES,
  554. NR_SS,NR_FS,NR_GS :
  555. result:=true;
  556. else
  557. result:=false;
  558. end;
  559. end;
  560. function findreg_by_number(r:Tregister):tregisterindex;
  561. var
  562. hr : tregister;
  563. begin
  564. { for the name the sub reg doesn't matter }
  565. hr:=r;
  566. if (getregtype(hr)=R_MMREGISTER) and
  567. (getsubreg(hr)<>R_SUBMMY) and
  568. (getsubreg(hr)<>R_SUBMMZ) then
  569. setsubreg(hr,R_SUBMMX);
  570. //// TG TODO check
  571. //if (getregtype(hr)=R_MMREGISTER) then
  572. // case getsubreg(hr) of
  573. // R_SUBMMX: setsubreg(hr,R_SUBMMX);
  574. // R_SUBMMY: setsubreg(hr,R_SUBMMY);
  575. // R_SUBMMZ: setsubreg(hr,R_SUBMMZ);
  576. // else setsubreg(hr,R_SUBMMX);
  577. // end;
  578. result:=findreg_by_number_table(hr,regnumber_index);
  579. end;
  580. function std_regnum_search(const s:string):Tregister;
  581. begin
  582. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  583. end;
  584. function std_regname(r:Tregister):string;
  585. var
  586. p : tregisterindex;
  587. begin
  588. if (getregtype(r)=R_MMXREGISTER) or
  589. ((getregtype(r)=R_MMREGISTER) and not(getsubreg(r) in [R_SUBMMX,R_SUBMMY])) then
  590. r:=newreg(getregtype(r),getsupreg(r),R_SUBNONE);
  591. p:=findreg_by_number(r);
  592. if p<>0 then
  593. result:=std_regname_table[p]
  594. else
  595. result:=generic_regname(r);
  596. end;
  597. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  598. const
  599. inverse: array[TAsmCond] of TAsmCond=(C_None,
  600. C_NA,C_NAE,C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_A,C_AE,
  601. C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_O,C_P,
  602. C_S,C_Z,C_NO,C_NP,C_NP,C_P,C_NS,C_NZ
  603. );
  604. begin
  605. result := inverse[c];
  606. end;
  607. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  608. begin
  609. result := c1 = c2;
  610. end;
  611. { Checks if Subset is a subset of c (e.g. "less than" is a subset of "less than or equal" }
  612. function condition_in(const Subset, c: TAsmCond): Boolean;
  613. begin
  614. Result := (c = C_None) or conditions_equal(Subset, c);
  615. if not Result then
  616. case Subset of
  617. C_A, C_NBE:
  618. Result := (c in [C_A, C_AE, C_NB, C_NC, C_NBE,C_NE, C_NZ]);
  619. C_AE, C_NB, C_NC:
  620. { C_A / C_NBE: CF = 0 and ZF = 0; not a subset because ZF has to be zero as well
  621. C_AE / C_NB: CF = 0 }
  622. Result := (c in [C_AE, C_NB, C_NC]);
  623. C_B, C_C, C_NAE:
  624. { C_B / C_NAE: CF = 1
  625. C_BE / C_NA: CF = 1 or ZF = 1 }
  626. Result := (c in [C_B, C_BE, C_C, C_NA, C_NAE]);
  627. C_BE, C_NA:
  628. Result := (c in [C_BE, C_NA]);
  629. C_E, C_Z:
  630. Result := (c in [C_AE, C_BE, C_E, C_NA, C_NG, C_Z]);
  631. C_G, C_NLE:
  632. { Not-equal can be considered equivalent to less than or greater than }
  633. Result := (c in [C_G, C_GE, C_NE, C_NL, C_NLE,C_NZ]);
  634. C_GE, C_NL:
  635. Result := (c in [C_GE, C_NL]);
  636. C_L, C_NGE:
  637. Result := (c in [C_L, C_LE, C_NE, C_NG, C_NGE,C_NZ]);
  638. C_LE, C_NG:
  639. Result := (c in [C_LE, C_NG]);
  640. C_NE, C_NZ:
  641. { Note that not equal is NOT a subset of greater/less than because
  642. not equal is less than OR greater than. Same with above and below }
  643. Result := (c in [C_NE, C_NZ]);
  644. C_NP, C_PO:
  645. Result := (c in [C_NP, C_PO]);
  646. C_P, C_PE:
  647. Result := (c in [C_P, C_PE]);
  648. else
  649. Result := False;
  650. end;
  651. end;
  652. function dwarf_reg(r:tregister):shortint;
  653. begin
  654. result:=regdwarf_table[findreg_by_number(r)];
  655. if result=-1 then
  656. internalerror(200603251);
  657. end;
  658. function dwarf_reg_no_error(r:tregister):shortint;
  659. begin
  660. result:=regdwarf_table[findreg_by_number(r)];
  661. end;
  662. function eh_return_data_regno(nr: longint): longint;
  663. begin
  664. case nr of
  665. 0: result:=0;
  666. {$ifdef x86_64}
  667. 1: result:=1;
  668. {$else}
  669. 1: result:=2;
  670. {$endif}
  671. else
  672. result:=-1;
  673. end;
  674. end;
  675. function segment_regs_equal(r1, r2: tregister): boolean;
  676. begin
  677. if not is_segment_reg(r1) or not is_segment_reg(r2) then
  678. internalerror(2013062301);
  679. { every segment register is equal to itself }
  680. if r1=r2 then
  681. exit(true);
  682. {$if defined(i8086)}
  683. case current_settings.x86memorymodel of
  684. mm_tiny:
  685. begin
  686. { CS=DS=SS }
  687. if ((r1=NR_CS) or (r1=NR_DS) or (r1=NR_SS)) and
  688. ((r2=NR_CS) or (r2=NR_DS) or (r2=NR_SS)) then
  689. exit(true);
  690. { the remaining are distinct from each other }
  691. exit(false);
  692. end;
  693. mm_small,mm_medium:
  694. begin
  695. { DS=SS }
  696. if ((r1=NR_DS) or (r1=NR_SS)) and
  697. ((r2=NR_DS) or (r2=NR_SS)) then
  698. exit(true);
  699. { the remaining are distinct from each other }
  700. exit(false);
  701. end;
  702. mm_compact,mm_large,mm_huge:
  703. { all segment registers are different in these models }
  704. exit(false);
  705. end;
  706. {$elseif defined(i386) or defined(x86_64)}
  707. { DS=SS=ES }
  708. if ((r1=NR_DS) or (r1=NR_SS) or (r1=NR_ES)) and
  709. ((r2=NR_DS) or (r2=NR_SS) or (r2=NR_ES)) then
  710. exit(true);
  711. { the remaining are distinct from each other }
  712. exit(false);
  713. {$endif}
  714. end;
  715. function is_x86_string_op(op: TAsmOp): boolean;
  716. begin
  717. case op of
  718. {$ifdef x86_64}
  719. A_MOVSQ,
  720. A_CMPSQ,
  721. A_SCASQ,
  722. A_LODSQ,
  723. A_STOSQ,
  724. {$endif x86_64}
  725. A_MOVSB,A_MOVSW,A_MOVSD,
  726. A_CMPSB,A_CMPSW,A_CMPSD,
  727. A_SCASB,A_SCASW,A_SCASD,
  728. A_LODSB,A_LODSW,A_LODSD,
  729. A_STOSB,A_STOSW,A_STOSD,
  730. A_INSB, A_INSW, A_INSD,
  731. A_OUTSB,A_OUTSW,A_OUTSD,
  732. A_MOVS,A_CMPS,A_SCAS,A_LODS,A_STOS,A_INS,A_OUTS:
  733. result:=true;
  734. else
  735. result:=false;
  736. end;
  737. end;
  738. function is_x86_parameterless_string_op(op: TAsmOp): boolean;
  739. begin
  740. case op of
  741. {$ifdef x86_64}
  742. A_MOVSQ,
  743. A_CMPSQ,
  744. A_SCASQ,
  745. A_LODSQ,
  746. A_STOSQ,
  747. {$endif x86_64}
  748. A_MOVSB,A_MOVSW,A_MOVSD,
  749. A_CMPSB,A_CMPSW,A_CMPSD,
  750. A_SCASB,A_SCASW,A_SCASD,
  751. A_LODSB,A_LODSW,A_LODSD,
  752. A_STOSB,A_STOSW,A_STOSD,
  753. A_INSB, A_INSW, A_INSD,
  754. A_OUTSB,A_OUTSW,A_OUTSD:
  755. result:=true;
  756. else
  757. result:=false;
  758. end;
  759. end;
  760. function is_x86_parameterized_string_op(op: TAsmOp): boolean;
  761. begin
  762. case op of
  763. A_MOVS,A_CMPS,A_SCAS,A_LODS,A_STOS,A_INS,A_OUTS:
  764. result:=true;
  765. else
  766. result:=false;
  767. end;
  768. end;
  769. function x86_parameterized_string_op_param_count(op: TAsmOp): shortint;
  770. begin
  771. case op of
  772. A_MOVS,A_CMPS,A_INS,A_OUTS:
  773. result:=2;
  774. A_SCAS,A_LODS,A_STOS:
  775. result:=1;
  776. else
  777. internalerror(2017101203);
  778. end;
  779. end;
  780. function x86_param2paramless_string_op(op: TAsmOp): TAsmOp;
  781. begin
  782. case op of
  783. A_MOVSB,A_MOVSW,A_MOVSD{$ifdef x86_64},A_MOVSQ{$endif}:
  784. result:=A_MOVS;
  785. A_CMPSB,A_CMPSW,A_CMPSD{$ifdef x86_64},A_CMPSQ{$endif}:
  786. result:=A_CMPS;
  787. A_SCASB,A_SCASW,A_SCASD{$ifdef x86_64},A_SCASQ{$endif}:
  788. result:=A_SCAS;
  789. A_LODSB,A_LODSW,A_LODSD{$ifdef x86_64},A_LODSQ{$endif}:
  790. result:=A_LODS;
  791. A_STOSB,A_STOSW,A_STOSD{$ifdef x86_64},A_STOSQ{$endif}:
  792. result:=A_STOS;
  793. A_INSB, A_INSW, A_INSD:
  794. result:=A_INS;
  795. A_OUTSB,A_OUTSW,A_OUTSD:
  796. result:=A_OUTS;
  797. else
  798. internalerror(2017101201);
  799. end;
  800. end;
  801. function get_x86_string_op_size(op: TAsmOp): TOpSize;
  802. begin
  803. case op of
  804. A_MOVSB,A_CMPSB,A_SCASB,A_LODSB,A_STOSB,A_INSB,A_OUTSB:
  805. result:=S_B;
  806. A_MOVSW,A_CMPSW,A_SCASW,A_LODSW,A_STOSW,A_INSW,A_OUTSW:
  807. result:=S_W;
  808. A_MOVSD,A_CMPSD,A_SCASD,A_LODSD,A_STOSD,A_INSD,A_OUTSD:
  809. result:=S_L;
  810. {$ifdef x86_64}
  811. A_MOVSQ,A_CMPSQ,A_SCASQ,A_LODSQ,A_STOSQ:
  812. result:=S_Q;
  813. {$endif x86_64}
  814. else
  815. internalerror(2017101202);
  816. end;
  817. end;
  818. function get_x86_string_op_si_param(op: TAsmOp):shortint;
  819. begin
  820. case op of
  821. A_MOVS,A_OUTS:
  822. result:=1;
  823. A_CMPS,A_LODS:
  824. result:=0;
  825. A_SCAS,A_STOS,A_INS:
  826. result:=-1;
  827. else
  828. internalerror(2017101102);
  829. end;
  830. end;
  831. function get_x86_string_op_di_param(op: TAsmOp):shortint;
  832. begin
  833. case op of
  834. A_MOVS,A_SCAS,A_STOS,A_INS:
  835. result:=0;
  836. A_CMPS:
  837. result:=1;
  838. A_LODS,A_OUTS:
  839. result:=-1;
  840. else
  841. internalerror(2017101204);
  842. end;
  843. end;
  844. {$ifdef i8086}
  845. function requires_fwait_on_8087(op: TAsmOp): boolean;
  846. begin
  847. case op of
  848. A_F2XM1,A_FABS,A_FADD,A_FADDP,A_FBLD,A_FBSTP,A_FCHS,A_FCOM,A_FCOMP,
  849. A_FCOMPP,A_FDECSTP,A_FDIV,A_FDIVP,A_FDIVR,A_FDIVRP,
  850. A_FFREE,A_FIADD,A_FICOM,A_FICOMP,A_FIDIV,A_FIDIVR,A_FILD,
  851. A_FIMUL,A_FINCSTP,A_FIST,A_FISTP,A_FISUB,A_FISUBR,A_FLD,A_FLD1,
  852. A_FLDCW,A_FLDENV,A_FLDL2E,A_FLDL2T,A_FLDLG2,A_FLDLN2,A_FLDPI,A_FLDZ,
  853. A_FMUL,A_FMULP,A_FNOP,A_FPATAN,A_FPREM,A_FPTAN,A_FRNDINT,
  854. A_FRSTOR,A_FSCALE,A_FSQRT,A_FST,
  855. A_FSTP,A_FSUB,A_FSUBP,A_FSUBR,A_FSUBRP,A_FTST,
  856. A_FXAM,A_FXCH,A_FXTRACT,A_FYL2X,A_FYL2XP1:
  857. result:=true;
  858. else
  859. result:=false;
  860. end;
  861. end;
  862. {$endif i8086}
  863. function UseAVX: boolean;
  864. begin
  865. Result:={$ifdef i8086}false{$else i8086}(FPUX86_HAS_AVXUNIT in fpu_capabilities[current_settings.fputype]){$endif i8086};
  866. end;
  867. function UseAVX512: boolean;
  868. begin
  869. Result:={$ifdef i8086}false{$else i8086}UseAVX and (FPUX86_HAS_AVX512F in fpu_capabilities[current_settings.fputype]){$endif i8086};
  870. end;
  871. end.