cpubase.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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. {$if defined(x86_64)}
  126. RS_RFLAGS = $06;
  127. {$elseif defined(i386)}
  128. RS_EFLAGS = $06;
  129. {$elseif defined(i8086)}
  130. RS_FLAGS = $06;
  131. {$endif}
  132. { Number of first imaginary register }
  133. {$ifdef x86_64}
  134. first_mm_imreg = $10;
  135. {$else x86_64}
  136. first_mm_imreg = $08;
  137. {$endif x86_64}
  138. { The subregister that specifies the entire register and an address }
  139. {$if defined(x86_64)}
  140. { Hammer }
  141. R_SUBWHOLE = R_SUBQ;
  142. R_SUBADDR = R_SUBQ;
  143. {$elseif defined(i386)}
  144. { i386 }
  145. R_SUBWHOLE = R_SUBD;
  146. R_SUBADDR = R_SUBD;
  147. {$elseif defined(i8086)}
  148. { i8086 }
  149. R_SUBWHOLE = R_SUBW;
  150. R_SUBADDR = R_SUBW;
  151. {$endif}
  152. { Available Registers }
  153. {$if defined(x86_64)}
  154. {$i r8664con.inc}
  155. {$elseif defined(i386)}
  156. {$i r386con.inc}
  157. {$elseif defined(i8086)}
  158. {$i r8086con.inc}
  159. {$endif}
  160. type
  161. { Number of registers used for indexing in tables }
  162. {$if defined(x86_64)}
  163. tregisterindex=0..{$i r8664nor.inc}-1;
  164. {$elseif defined(i386)}
  165. tregisterindex=0..{$i r386nor.inc}-1;
  166. {$elseif defined(i8086)}
  167. tregisterindex=0..{$i r8086nor.inc}-1;
  168. {$endif}
  169. const
  170. regnumber_table : array[tregisterindex] of tregister = (
  171. {$if defined(x86_64)}
  172. {$i r8664num.inc}
  173. {$elseif defined(i386)}
  174. {$i r386num.inc}
  175. {$elseif defined(i8086)}
  176. {$i r8086num.inc}
  177. {$endif}
  178. );
  179. regstabs_table : array[tregisterindex] of shortint = (
  180. {$if defined(x86_64)}
  181. {$i r8664stab.inc}
  182. {$elseif defined(i386)}
  183. {$i r386stab.inc}
  184. {$elseif defined(i8086)}
  185. {$i r8086stab.inc}
  186. {$endif}
  187. );
  188. regdwarf_table : array[tregisterindex] of shortint = (
  189. {$if defined(x86_64)}
  190. {$i r8664dwrf.inc}
  191. {$elseif defined(i386)}
  192. {$i r386dwrf.inc}
  193. {$elseif defined(i8086)}
  194. {$i r8086dwrf.inc}
  195. {$endif}
  196. );
  197. {$if defined(x86_64)}
  198. RS_DEFAULTFLAGS = RS_RFLAGS;
  199. NR_DEFAULTFLAGS = NR_RFLAGS;
  200. {$elseif defined(i386)}
  201. RS_DEFAULTFLAGS = RS_EFLAGS;
  202. NR_DEFAULTFLAGS = NR_EFLAGS;
  203. {$elseif defined(i8086)}
  204. RS_DEFAULTFLAGS = RS_FLAGS;
  205. NR_DEFAULTFLAGS = NR_FLAGS;
  206. {$endif}
  207. type
  208. totherregisterset = set of tregisterindex;
  209. {*****************************************************************************
  210. Conditions
  211. *****************************************************************************}
  212. type
  213. TAsmCond=(C_None,
  214. C_A,C_AE,C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_NA,C_NAE,
  215. C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_NO,C_NP,
  216. C_NS,C_NZ,C_O,C_P,C_PE,C_PO,C_S,C_Z
  217. );
  218. const
  219. cond2str:array[TAsmCond] of string[3]=('',
  220. 'a','ae','b','be','c','e','g','ge','l','le','na','nae',
  221. 'nb','nbe','nc','ne','ng','nge','nl','nle','no','np',
  222. 'ns','nz','o','p','pe','po','s','z'
  223. );
  224. {*****************************************************************************
  225. Flags
  226. *****************************************************************************}
  227. type
  228. TResFlags = (F_E,F_NE,F_G,F_L,F_GE,F_LE,F_C,F_NC,
  229. F_A,F_AE,F_B,F_BE,
  230. F_S,F_NS,F_O,F_NO,
  231. { For IEEE-compliant floating-point compares,
  232. same as normal counterparts but additionally check PF }
  233. F_FE,F_FNE,F_FA,F_FAE,F_FB,F_FBE);
  234. const
  235. FPUFlags = [F_FE,F_FNE,F_FA,F_FAE,F_FB,F_FBE];
  236. FPUFlags2Flags: array[F_FE..F_FBE] of TResFlags = (
  237. F_E,F_NE,F_A,F_AE,F_B,F_BE
  238. );
  239. {*****************************************************************************
  240. Constants
  241. *****************************************************************************}
  242. const
  243. { declare aliases }
  244. LOC_SSEREGISTER = LOC_MMREGISTER;
  245. LOC_CSSEREGISTER = LOC_CMMREGISTER;
  246. max_operands = 4;
  247. maxfpuregs = 8;
  248. {*****************************************************************************
  249. CPU Dependent Constants
  250. *****************************************************************************}
  251. {$i cpubase.inc}
  252. const
  253. {$ifdef x86_64}
  254. topsize2memsize: array[topsize] of integer =
  255. (0, 8,16,32,64,8,8,16,8,16,32,
  256. 16,32,64,
  257. 16,32,64,0,0,
  258. 64,
  259. 0,0,0,
  260. 80,
  261. 128,
  262. 256,
  263. 512
  264. );
  265. {$else}
  266. topsize2memsize: array[topsize] of integer =
  267. (0, 8,16,32,64,8,8,16,
  268. 16,32,64,
  269. 16,32,64,0,0,
  270. 64,
  271. 0,0,0,
  272. 80,
  273. 128,
  274. 256,
  275. 512
  276. );
  277. {$endif}
  278. {*****************************************************************************
  279. Helpers
  280. *****************************************************************************}
  281. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  282. function reg2opsize(r:Tregister):topsize;
  283. function reg_cgsize(const reg: tregister): tcgsize;
  284. function is_calljmp(o:tasmop):boolean;
  285. procedure inverse_flags(var f: TResFlags);
  286. function flags_to_cond(const f: TResFlags) : TAsmCond;
  287. function is_segment_reg(r:tregister):boolean;
  288. function findreg_by_number(r:Tregister):tregisterindex;
  289. function std_regnum_search(const s:string):Tregister;
  290. function std_regname(r:Tregister):string;
  291. function dwarf_reg(r:tregister):shortint;
  292. function dwarf_reg_no_error(r:tregister):shortint;
  293. function eh_return_data_regno(nr: longint): longint;
  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 whether two segment registers are normally equal in the current memory model }
  297. function segment_regs_equal(r1,r2:tregister):boolean;
  298. { checks whether the specified op is an x86 string instruction (e.g. cmpsb, movsd, scasw, etc.) }
  299. function is_x86_string_op(op: TAsmOp): boolean;
  300. { checks whether the specified op is an x86 parameterless string instruction
  301. (e.g. returns true for movsb, cmpsw, etc, but returns false for movs, cmps, etc.) }
  302. function is_x86_parameterless_string_op(op: TAsmOp): boolean;
  303. { checks whether the specified op is an x86 parameterized string instruction
  304. (e.g. returns true for movs, cmps, etc, but returns false for movsb, cmpsb, etc.) }
  305. function is_x86_parameterized_string_op(op: TAsmOp): boolean;
  306. function x86_parameterized_string_op_param_count(op: TAsmOp): shortint;
  307. function x86_param2paramless_string_op(op: TAsmOp): TAsmOp;
  308. function get_x86_string_op_size(op: TAsmOp): TOpSize;
  309. { returns the 0-based operand number (intel syntax) of the ds:[si] param of
  310. a x86 string instruction }
  311. function get_x86_string_op_si_param(op: TAsmOp):shortint;
  312. { returns the 0-based operand number (intel syntax) of the es:[di] param of
  313. a x86 string instruction }
  314. function get_x86_string_op_di_param(op: TAsmOp):shortint;
  315. {$ifdef i8086}
  316. { return whether we need to add an extra FWAIT instruction before the given
  317. instruction, when we're targeting the i8087. This includes almost all x87
  318. instructions, but certain ones, which always have or have not a built in
  319. FWAIT prefix are excluded (e.g. FINIT,FNINIT,etc.). }
  320. function requires_fwait_on_8087(op: TAsmOp): boolean;
  321. {$endif i8086}
  322. implementation
  323. uses
  324. globtype,
  325. rgbase,verbose;
  326. const
  327. {$if defined(x86_64)}
  328. std_regname_table : TRegNameTable = (
  329. {$i r8664std.inc}
  330. );
  331. regnumber_index : array[tregisterindex] of tregisterindex = (
  332. {$i r8664rni.inc}
  333. );
  334. std_regname_index : array[tregisterindex] of tregisterindex = (
  335. {$i r8664sri.inc}
  336. );
  337. {$elseif defined(i386)}
  338. std_regname_table : TRegNameTable = (
  339. {$i r386std.inc}
  340. );
  341. regnumber_index : array[tregisterindex] of tregisterindex = (
  342. {$i r386rni.inc}
  343. );
  344. std_regname_index : array[tregisterindex] of tregisterindex = (
  345. {$i r386sri.inc}
  346. );
  347. {$elseif defined(i8086)}
  348. std_regname_table : TRegNameTable = (
  349. {$i r8086std.inc}
  350. );
  351. regnumber_index : array[tregisterindex] of tregisterindex = (
  352. {$i r8086rni.inc}
  353. );
  354. std_regname_index : array[tregisterindex] of tregisterindex = (
  355. {$i r8086sri.inc}
  356. );
  357. {$endif}
  358. {*****************************************************************************
  359. Helpers
  360. *****************************************************************************}
  361. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  362. begin
  363. case s of
  364. OS_8,OS_S8:
  365. cgsize2subreg:=R_SUBL;
  366. OS_16,OS_S16:
  367. cgsize2subreg:=R_SUBW;
  368. OS_32,OS_S32:
  369. cgsize2subreg:=R_SUBD;
  370. OS_64,OS_S64:
  371. cgsize2subreg:=R_SUBQ;
  372. OS_M64:
  373. cgsize2subreg:=R_SUBNONE;
  374. OS_F32,OS_F64,OS_C64:
  375. case regtype of
  376. R_FPUREGISTER:
  377. cgsize2subreg:=R_SUBWHOLE;
  378. R_MMREGISTER:
  379. case s of
  380. OS_F32:
  381. cgsize2subreg:=R_SUBMMS;
  382. OS_F64:
  383. cgsize2subreg:=R_SUBMMD;
  384. else
  385. internalerror(2009071901);
  386. end;
  387. else
  388. internalerror(2009071902);
  389. end;
  390. OS_M128,OS_MS128,OS_MF128,OS_MD128:
  391. cgsize2subreg:=R_SUBMMX;
  392. OS_M256,OS_MS256,OS_MF256,OS_MD256:
  393. cgsize2subreg:=R_SUBMMY;
  394. OS_M512,OS_MS512,OS_MF512,OS_MD512:
  395. cgsize2subreg:=R_SUBMMZ;
  396. OS_NO:
  397. { error message should have been thrown already before, so avoid only
  398. an internal error }
  399. cgsize2subreg:=R_SUBNONE;
  400. else
  401. internalerror(200301231);
  402. end;
  403. end;
  404. function reg_cgsize(const reg: tregister): tcgsize;
  405. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  406. (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);
  407. begin
  408. case getregtype(reg) of
  409. R_INTREGISTER :
  410. reg_cgsize:=subreg2cgsize[getsubreg(reg)];
  411. R_FPUREGISTER :
  412. reg_cgsize:=OS_F80;
  413. R_MMXREGISTER:
  414. reg_cgsize:=OS_M64;
  415. R_MMREGISTER:
  416. reg_cgsize:=subreg2cgsize[getsubreg(reg)];
  417. R_SPECIALREGISTER :
  418. case reg of
  419. NR_CS,NR_DS,NR_ES,NR_SS,NR_FS,NR_GS:
  420. reg_cgsize:=OS_16;
  421. {$ifdef x86_64}
  422. NR_DR0..NR_TR7:
  423. reg_cgsize:=OS_64;
  424. {$endif x86_64}
  425. else
  426. reg_cgsize:=OS_32
  427. end;
  428. R_ADDRESSREGISTER:
  429. case reg of
  430. NR_K0..NR_K7: reg_cgsize:=OS_64;
  431. else internalerror(2003031801);
  432. end;
  433. else
  434. internalerror(2003031801);
  435. end;
  436. end;
  437. function reg2opsize(r:Tregister):topsize;
  438. const
  439. subreg2opsize : array[tsubregister] of topsize =
  440. (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);
  441. begin
  442. reg2opsize:=S_L;
  443. case getregtype(r) of
  444. R_INTREGISTER :
  445. reg2opsize:=subreg2opsize[getsubreg(r)];
  446. R_FPUREGISTER :
  447. reg2opsize:=S_FL;
  448. R_MMXREGISTER,
  449. R_MMREGISTER :
  450. reg2opsize:=S_MD;
  451. R_SPECIALREGISTER :
  452. begin
  453. case r of
  454. NR_CS,NR_DS,NR_ES,
  455. NR_SS,NR_FS,NR_GS :
  456. reg2opsize:=S_W;
  457. else
  458. ;
  459. end;
  460. end;
  461. else
  462. internalerror(200303181);
  463. end;
  464. end;
  465. function is_calljmp(o:tasmop):boolean;
  466. begin
  467. case o of
  468. A_CALL,
  469. {$if defined(i386) or defined(i8086)}
  470. A_JCXZ,
  471. {$endif defined(i386) or defined(i8086)}
  472. A_JECXZ,
  473. {$ifdef x86_64}
  474. A_JRCXZ,
  475. {$endif x86_64}
  476. A_JMP,
  477. A_LOOP,
  478. A_LOOPE,
  479. A_LOOPNE,
  480. A_LOOPNZ,
  481. A_LOOPZ,
  482. A_LCALL,
  483. A_LJMP,
  484. A_Jcc :
  485. is_calljmp:=true;
  486. else
  487. is_calljmp:=false;
  488. end;
  489. end;
  490. procedure inverse_flags(var f: TResFlags);
  491. const
  492. inv_flags: array[TResFlags] of TResFlags =
  493. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_NC,F_C,
  494. F_BE,F_B,F_AE,F_A,
  495. F_NS,F_S,F_NO,F_O,
  496. F_FNE,F_FE,F_FBE,F_FB,F_FAE,F_FA);
  497. begin
  498. f:=inv_flags[f];
  499. end;
  500. function flags_to_cond(const f: TResFlags) : TAsmCond;
  501. const
  502. flags_2_cond : array[TResFlags] of TAsmCond =
  503. (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,
  504. C_None,C_None,C_None,C_None,C_None,C_None);
  505. begin
  506. result := flags_2_cond[f];
  507. if (result=C_None) then
  508. InternalError(2014041301);
  509. end;
  510. function is_segment_reg(r:tregister):boolean;
  511. begin
  512. case r of
  513. NR_CS,NR_DS,NR_ES,
  514. NR_SS,NR_FS,NR_GS :
  515. result:=true;
  516. else
  517. result:=false;
  518. end;
  519. end;
  520. function findreg_by_number(r:Tregister):tregisterindex;
  521. var
  522. hr : tregister;
  523. begin
  524. { for the name the sub reg doesn't matter }
  525. hr:=r;
  526. if (getregtype(hr)=R_MMREGISTER) and
  527. (getsubreg(hr)<>R_SUBMMY) and
  528. (getsubreg(hr)<>R_SUBMMZ) then
  529. setsubreg(hr,R_SUBMMX);
  530. //// TG TODO check
  531. //if (getregtype(hr)=R_MMREGISTER) then
  532. // case getsubreg(hr) of
  533. // R_SUBMMX: setsubreg(hr,R_SUBMMX);
  534. // R_SUBMMY: setsubreg(hr,R_SUBMMY);
  535. // R_SUBMMZ: setsubreg(hr,R_SUBMMZ);
  536. // else setsubreg(hr,R_SUBMMX);
  537. // end;
  538. result:=findreg_by_number_table(hr,regnumber_index);
  539. end;
  540. function std_regnum_search(const s:string):Tregister;
  541. begin
  542. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  543. end;
  544. function std_regname(r:Tregister):string;
  545. var
  546. p : tregisterindex;
  547. begin
  548. if (getregtype(r)=R_MMXREGISTER) or
  549. ((getregtype(r)=R_MMREGISTER) and not(getsubreg(r) in [R_SUBMMX,R_SUBMMY])) then
  550. r:=newreg(getregtype(r),getsupreg(r),R_SUBNONE);
  551. p:=findreg_by_number(r);
  552. if p<>0 then
  553. result:=std_regname_table[p]
  554. else
  555. result:=generic_regname(r);
  556. end;
  557. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  558. const
  559. inverse: array[TAsmCond] of TAsmCond=(C_None,
  560. C_NA,C_NAE,C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_A,C_AE,
  561. C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_O,C_P,
  562. C_S,C_Z,C_NO,C_NP,C_NP,C_P,C_NS,C_NZ
  563. );
  564. begin
  565. result := inverse[c];
  566. end;
  567. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  568. begin
  569. result := c1 = c2;
  570. end;
  571. function dwarf_reg(r:tregister):shortint;
  572. begin
  573. result:=regdwarf_table[findreg_by_number(r)];
  574. if result=-1 then
  575. internalerror(200603251);
  576. end;
  577. function dwarf_reg_no_error(r:tregister):shortint;
  578. begin
  579. result:=regdwarf_table[findreg_by_number(r)];
  580. end;
  581. function eh_return_data_regno(nr: longint): longint;
  582. begin
  583. case nr of
  584. 0: result:=0;
  585. {$ifdef x86_64}
  586. 1: result:=1;
  587. {$else}
  588. 1: result:=2;
  589. {$endif}
  590. else
  591. result:=-1;
  592. end;
  593. end;
  594. function segment_regs_equal(r1, r2: tregister): boolean;
  595. begin
  596. if not is_segment_reg(r1) or not is_segment_reg(r2) then
  597. internalerror(2013062301);
  598. { every segment register is equal to itself }
  599. if r1=r2 then
  600. exit(true);
  601. {$if defined(i8086)}
  602. case current_settings.x86memorymodel of
  603. mm_tiny:
  604. begin
  605. { CS=DS=SS }
  606. if ((r1=NR_CS) or (r1=NR_DS) or (r1=NR_SS)) and
  607. ((r2=NR_CS) or (r2=NR_DS) or (r2=NR_SS)) then
  608. exit(true);
  609. { the remaining are distinct from each other }
  610. exit(false);
  611. end;
  612. mm_small,mm_medium:
  613. begin
  614. { DS=SS }
  615. if ((r1=NR_DS) or (r1=NR_SS)) and
  616. ((r2=NR_DS) or (r2=NR_SS)) then
  617. exit(true);
  618. { the remaining are distinct from each other }
  619. exit(false);
  620. end;
  621. mm_compact,mm_large,mm_huge:
  622. { all segment registers are different in these models }
  623. exit(false);
  624. end;
  625. {$elseif defined(i386) or defined(x86_64)}
  626. { DS=SS=ES }
  627. if ((r1=NR_DS) or (r1=NR_SS) or (r1=NR_ES)) and
  628. ((r2=NR_DS) or (r2=NR_SS) or (r2=NR_ES)) then
  629. exit(true);
  630. { the remaining are distinct from each other }
  631. exit(false);
  632. {$endif}
  633. end;
  634. function is_x86_string_op(op: TAsmOp): boolean;
  635. begin
  636. case op of
  637. {$ifdef x86_64}
  638. A_MOVSQ,
  639. A_CMPSQ,
  640. A_SCASQ,
  641. A_LODSQ,
  642. A_STOSQ,
  643. {$endif x86_64}
  644. A_MOVSB,A_MOVSW,A_MOVSD,
  645. A_CMPSB,A_CMPSW,A_CMPSD,
  646. A_SCASB,A_SCASW,A_SCASD,
  647. A_LODSB,A_LODSW,A_LODSD,
  648. A_STOSB,A_STOSW,A_STOSD,
  649. A_INSB, A_INSW, A_INSD,
  650. A_OUTSB,A_OUTSW,A_OUTSD,
  651. A_MOVS,A_CMPS,A_SCAS,A_LODS,A_STOS,A_INS,A_OUTS:
  652. result:=true;
  653. else
  654. result:=false;
  655. end;
  656. end;
  657. function is_x86_parameterless_string_op(op: TAsmOp): boolean;
  658. begin
  659. case op of
  660. {$ifdef x86_64}
  661. A_MOVSQ,
  662. A_CMPSQ,
  663. A_SCASQ,
  664. A_LODSQ,
  665. A_STOSQ,
  666. {$endif x86_64}
  667. A_MOVSB,A_MOVSW,A_MOVSD,
  668. A_CMPSB,A_CMPSW,A_CMPSD,
  669. A_SCASB,A_SCASW,A_SCASD,
  670. A_LODSB,A_LODSW,A_LODSD,
  671. A_STOSB,A_STOSW,A_STOSD,
  672. A_INSB, A_INSW, A_INSD,
  673. A_OUTSB,A_OUTSW,A_OUTSD:
  674. result:=true;
  675. else
  676. result:=false;
  677. end;
  678. end;
  679. function is_x86_parameterized_string_op(op: TAsmOp): boolean;
  680. begin
  681. case op of
  682. A_MOVS,A_CMPS,A_SCAS,A_LODS,A_STOS,A_INS,A_OUTS:
  683. result:=true;
  684. else
  685. result:=false;
  686. end;
  687. end;
  688. function x86_parameterized_string_op_param_count(op: TAsmOp): shortint;
  689. begin
  690. case op of
  691. A_MOVS,A_CMPS,A_INS,A_OUTS:
  692. result:=2;
  693. A_SCAS,A_LODS,A_STOS:
  694. result:=1;
  695. else
  696. internalerror(2017101203);
  697. end;
  698. end;
  699. function x86_param2paramless_string_op(op: TAsmOp): TAsmOp;
  700. begin
  701. case op of
  702. A_MOVSB,A_MOVSW,A_MOVSD{$ifdef x86_64},A_MOVSQ{$endif}:
  703. result:=A_MOVS;
  704. A_CMPSB,A_CMPSW,A_CMPSD{$ifdef x86_64},A_CMPSQ{$endif}:
  705. result:=A_CMPS;
  706. A_SCASB,A_SCASW,A_SCASD{$ifdef x86_64},A_SCASQ{$endif}:
  707. result:=A_SCAS;
  708. A_LODSB,A_LODSW,A_LODSD{$ifdef x86_64},A_LODSQ{$endif}:
  709. result:=A_LODS;
  710. A_STOSB,A_STOSW,A_STOSD{$ifdef x86_64},A_STOSQ{$endif}:
  711. result:=A_STOS;
  712. A_INSB, A_INSW, A_INSD:
  713. result:=A_INS;
  714. A_OUTSB,A_OUTSW,A_OUTSD:
  715. result:=A_OUTS;
  716. else
  717. internalerror(2017101201);
  718. end;
  719. end;
  720. function get_x86_string_op_size(op: TAsmOp): TOpSize;
  721. begin
  722. case op of
  723. A_MOVSB,A_CMPSB,A_SCASB,A_LODSB,A_STOSB,A_INSB,A_OUTSB:
  724. result:=S_B;
  725. A_MOVSW,A_CMPSW,A_SCASW,A_LODSW,A_STOSW,A_INSW,A_OUTSW:
  726. result:=S_W;
  727. A_MOVSD,A_CMPSD,A_SCASD,A_LODSD,A_STOSD,A_INSD,A_OUTSD:
  728. result:=S_L;
  729. {$ifdef x86_64}
  730. A_MOVSQ,A_CMPSQ,A_SCASQ,A_LODSQ,A_STOSQ:
  731. result:=S_Q;
  732. {$endif x86_64}
  733. else
  734. internalerror(2017101202);
  735. end;
  736. end;
  737. function get_x86_string_op_si_param(op: TAsmOp):shortint;
  738. begin
  739. case op of
  740. A_MOVS,A_OUTS:
  741. result:=1;
  742. A_CMPS,A_LODS:
  743. result:=0;
  744. A_SCAS,A_STOS,A_INS:
  745. result:=-1;
  746. else
  747. internalerror(2017101102);
  748. end;
  749. end;
  750. function get_x86_string_op_di_param(op: TAsmOp):shortint;
  751. begin
  752. case op of
  753. A_MOVS,A_SCAS,A_STOS,A_INS:
  754. result:=0;
  755. A_CMPS:
  756. result:=1;
  757. A_LODS,A_OUTS:
  758. result:=-1;
  759. else
  760. internalerror(2017101204);
  761. end;
  762. end;
  763. {$ifdef i8086}
  764. function requires_fwait_on_8087(op: TAsmOp): boolean;
  765. begin
  766. case op of
  767. A_F2XM1,A_FABS,A_FADD,A_FADDP,A_FBLD,A_FBSTP,A_FCHS,A_FCOM,A_FCOMP,
  768. A_FCOMPP,A_FDECSTP,A_FDIV,A_FDIVP,A_FDIVR,A_FDIVRP,
  769. A_FFREE,A_FIADD,A_FICOM,A_FICOMP,A_FIDIV,A_FIDIVR,A_FILD,
  770. A_FIMUL,A_FINCSTP,A_FIST,A_FISTP,A_FISUB,A_FISUBR,A_FLD,A_FLD1,
  771. A_FLDCW,A_FLDENV,A_FLDL2E,A_FLDL2T,A_FLDLG2,A_FLDLN2,A_FLDPI,A_FLDZ,
  772. A_FMUL,A_FMULP,A_FNOP,A_FPATAN,A_FPREM,A_FPTAN,A_FRNDINT,
  773. A_FRSTOR,A_FSCALE,A_FSQRT,A_FST,
  774. A_FSTP,A_FSUB,A_FSUBP,A_FSUBR,A_FSUBRP,A_FTST,
  775. A_FXAM,A_FXCH,A_FXTRACT,A_FYL2X,A_FYL2XP1:
  776. result:=true;
  777. else
  778. result:=false;
  779. end;
  780. end;
  781. {$endif i8086}
  782. end.