cpubase.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. cutils,cclasses,
  29. globtype,globals,
  30. cgbase
  31. ;
  32. {*****************************************************************************
  33. Assembler Opcodes
  34. *****************************************************************************}
  35. type
  36. {$if defined(x86_64)}
  37. TAsmOp={$i x8664op.inc}
  38. {$elseif defined(i386)}
  39. TAsmOp={$i i386op.inc}
  40. {$elseif defined(i8086)}
  41. TAsmOp={$i i8086op.inc}
  42. {$endif}
  43. { This should define the array of instructions as string }
  44. op2strtable=array[tasmop] of string[16];
  45. const
  46. { First value of opcode enumeration }
  47. firstop = low(tasmop);
  48. { Last value of opcode enumeration }
  49. lastop = high(tasmop);
  50. {*****************************************************************************
  51. Registers
  52. *****************************************************************************}
  53. const
  54. { Integer Super registers }
  55. RS_NO = $ffffffff;
  56. RS_RAX = $00; {EAX}
  57. RS_RCX = $01; {ECX}
  58. RS_RDX = $02; {EDX}
  59. RS_RBX = $03; {EBX}
  60. RS_RSI = $04; {ESI}
  61. RS_RDI = $05; {EDI}
  62. RS_RBP = $06; {EBP}
  63. RS_RSP = $07; {ESP}
  64. RS_R8 = $08; {R8}
  65. RS_R9 = $09; {R9}
  66. RS_R10 = $0a; {R10}
  67. RS_R11 = $0b; {R11}
  68. RS_R12 = $0c; {R12}
  69. RS_R13 = $0d; {R13}
  70. RS_R14 = $0e; {R14}
  71. RS_R15 = $0f; {R15}
  72. { create aliases to allow code sharing between x86-64 and i386 }
  73. RS_EAX = RS_RAX;
  74. RS_EBX = RS_RBX;
  75. RS_ECX = RS_RCX;
  76. RS_EDX = RS_RDX;
  77. RS_ESI = RS_RSI;
  78. RS_EDI = RS_RDI;
  79. RS_EBP = RS_RBP;
  80. RS_ESP = RS_RSP;
  81. { create aliases to allow code sharing between i386 and i8086 }
  82. RS_AX = RS_RAX;
  83. RS_BX = RS_RBX;
  84. RS_CX = RS_RCX;
  85. RS_DX = RS_RDX;
  86. RS_SI = RS_RSI;
  87. RS_DI = RS_RDI;
  88. RS_BP = RS_RBP;
  89. RS_SP = RS_RSP;
  90. { Number of first imaginary register }
  91. first_int_imreg = $10;
  92. { Float Super registers }
  93. RS_ST0 = $00;
  94. RS_ST1 = $01;
  95. RS_ST2 = $02;
  96. RS_ST3 = $03;
  97. RS_ST4 = $04;
  98. RS_ST5 = $05;
  99. RS_ST6 = $06;
  100. RS_ST7 = $07;
  101. { Number of first imaginary register }
  102. first_fpu_imreg = $08;
  103. { MM Super registers }
  104. RS_XMM0 = $00;
  105. RS_XMM1 = $01;
  106. RS_XMM2 = $02;
  107. RS_XMM3 = $03;
  108. RS_XMM4 = $04;
  109. RS_XMM5 = $05;
  110. RS_XMM6 = $06;
  111. RS_XMM7 = $07;
  112. RS_XMM8 = $08;
  113. RS_XMM9 = $09;
  114. RS_XMM10 = $0a;
  115. RS_XMM11 = $0b;
  116. RS_XMM12 = $0c;
  117. RS_XMM13 = $0d;
  118. RS_XMM14 = $0e;
  119. RS_XMM15 = $0f;
  120. RS_FLAGS = $07;
  121. { Number of first imaginary register }
  122. {$ifdef x86_64}
  123. first_mm_imreg = $10;
  124. {$else x86_64}
  125. first_mm_imreg = $08;
  126. {$endif x86_64}
  127. { The subregister that specifies the entire register and an address }
  128. {$if defined(x86_64)}
  129. { Hammer }
  130. R_SUBWHOLE = R_SUBQ;
  131. R_SUBADDR = R_SUBQ;
  132. {$elseif defined(i386)}
  133. { i386 }
  134. R_SUBWHOLE = R_SUBD;
  135. R_SUBADDR = R_SUBD;
  136. {$elseif defined(i8086)}
  137. { i8086 }
  138. R_SUBWHOLE = R_SUBW;
  139. R_SUBADDR = R_SUBW;
  140. {$endif}
  141. { Available Registers }
  142. {$if defined(x86_64)}
  143. {$i r8664con.inc}
  144. {$elseif defined(i386)}
  145. {$i r386con.inc}
  146. {$elseif defined(i8086)}
  147. {$i r8086con.inc}
  148. {$endif}
  149. type
  150. { Number of registers used for indexing in tables }
  151. {$if defined(x86_64)}
  152. tregisterindex=0..{$i r8664nor.inc}-1;
  153. {$elseif defined(i386)}
  154. tregisterindex=0..{$i r386nor.inc}-1;
  155. {$elseif defined(i8086)}
  156. tregisterindex=0..{$i r8086nor.inc}-1;
  157. {$endif}
  158. const
  159. { TODO: Calculate bsstart}
  160. regnumber_count_bsstart = 64;
  161. regnumber_table : array[tregisterindex] of tregister = (
  162. {$if defined(x86_64)}
  163. {$i r8664num.inc}
  164. {$elseif defined(i386)}
  165. {$i r386num.inc}
  166. {$elseif defined(i8086)}
  167. {$i r8086num.inc}
  168. {$endif}
  169. );
  170. regstabs_table : array[tregisterindex] of shortint = (
  171. {$if defined(x86_64)}
  172. {$i r8664stab.inc}
  173. {$elseif defined(i386)}
  174. {$i r386stab.inc}
  175. {$elseif defined(i8086)}
  176. {$i r8086stab.inc}
  177. {$endif}
  178. );
  179. regdwarf_table : array[tregisterindex] of shortint = (
  180. {$if defined(x86_64)}
  181. {$i r8664dwrf.inc}
  182. {$elseif defined(i386)}
  183. {$i r386dwrf.inc}
  184. {$elseif defined(i8086)}
  185. {$i r8086dwrf.inc}
  186. {$endif}
  187. );
  188. RS_DEFAULTFLAGS = RS_FLAGS;
  189. NR_DEFAULTFLAGS = NR_FLAGS;
  190. type
  191. totherregisterset = set of tregisterindex;
  192. {*****************************************************************************
  193. Conditions
  194. *****************************************************************************}
  195. type
  196. TAsmCond=(C_None,
  197. C_A,C_AE,C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_NA,C_NAE,
  198. C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_NO,C_NP,
  199. C_NS,C_NZ,C_O,C_P,C_PE,C_PO,C_S,C_Z
  200. );
  201. const
  202. cond2str:array[TAsmCond] of string[3]=('',
  203. 'a','ae','b','be','c','e','g','ge','l','le','na','nae',
  204. 'nb','nbe','nc','ne','ng','nge','nl','nle','no','np',
  205. 'ns','nz','o','p','pe','po','s','z'
  206. );
  207. {*****************************************************************************
  208. Flags
  209. *****************************************************************************}
  210. type
  211. TResFlags = (F_E,F_NE,F_G,F_L,F_GE,F_LE,F_C,F_NC,
  212. F_A,F_AE,F_B,F_BE,
  213. F_S,F_NS,F_O,F_NO);
  214. {*****************************************************************************
  215. Constants
  216. *****************************************************************************}
  217. const
  218. { declare aliases }
  219. LOC_SSEREGISTER = LOC_MMREGISTER;
  220. LOC_CSSEREGISTER = LOC_CMMREGISTER;
  221. max_operands = 4;
  222. maxfpuregs = 8;
  223. {*****************************************************************************
  224. CPU Dependent Constants
  225. *****************************************************************************}
  226. {$i cpubase.inc}
  227. {*****************************************************************************
  228. Helpers
  229. *****************************************************************************}
  230. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  231. function reg2opsize(r:Tregister):topsize;
  232. function reg_cgsize(const reg: tregister): tcgsize;
  233. function is_calljmp(o:tasmop):boolean;
  234. procedure inverse_flags(var f: TResFlags);
  235. function flags_to_cond(const f: TResFlags) : TAsmCond;
  236. function is_segment_reg(r:tregister):boolean;
  237. function findreg_by_number(r:Tregister):tregisterindex;
  238. function std_regnum_search(const s:string):Tregister;
  239. function std_regname(r:Tregister):string;
  240. function dwarf_reg(r:tregister):shortint;
  241. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  242. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  243. { checks whether two segment registers are normally equal in the current memory model }
  244. function segment_regs_equal(r1,r2:tregister):boolean;
  245. {$ifdef i8086}
  246. { returns the next virtual register }
  247. function GetNextReg(const r : TRegister) : TRegister;
  248. { return whether we need to add an extra FWAIT instruction before the given
  249. instruction, when we're targeting the i8087. This includes almost all x87
  250. instructions, but certain ones, which always have or have not a built in
  251. FWAIT prefix are excluded (e.g. FINIT,FNINIT,etc.). }
  252. function requires_fwait_on_8087(op: TAsmOp): boolean;
  253. {$endif i8086}
  254. implementation
  255. uses
  256. rgbase,verbose;
  257. const
  258. {$if defined(x86_64)}
  259. std_regname_table : TRegNameTable = (
  260. {$i r8664std.inc}
  261. );
  262. regnumber_index : array[tregisterindex] of tregisterindex = (
  263. {$i r8664rni.inc}
  264. );
  265. std_regname_index : array[tregisterindex] of tregisterindex = (
  266. {$i r8664sri.inc}
  267. );
  268. {$elseif defined(i386)}
  269. std_regname_table : TRegNameTable = (
  270. {$i r386std.inc}
  271. );
  272. regnumber_index : array[tregisterindex] of tregisterindex = (
  273. {$i r386rni.inc}
  274. );
  275. std_regname_index : array[tregisterindex] of tregisterindex = (
  276. {$i r386sri.inc}
  277. );
  278. {$elseif defined(i8086)}
  279. std_regname_table : TRegNameTable = (
  280. {$i r8086std.inc}
  281. );
  282. regnumber_index : array[tregisterindex] of tregisterindex = (
  283. {$i r8086rni.inc}
  284. );
  285. std_regname_index : array[tregisterindex] of tregisterindex = (
  286. {$i r8086sri.inc}
  287. );
  288. {$endif}
  289. {*****************************************************************************
  290. Helpers
  291. *****************************************************************************}
  292. function cgsize2subreg(regtype: tregistertype; s:Tcgsize):Tsubregister;
  293. begin
  294. case s of
  295. OS_8,OS_S8:
  296. cgsize2subreg:=R_SUBL;
  297. OS_16,OS_S16:
  298. cgsize2subreg:=R_SUBW;
  299. OS_32,OS_S32:
  300. cgsize2subreg:=R_SUBD;
  301. OS_64,OS_S64:
  302. cgsize2subreg:=R_SUBQ;
  303. OS_M64:
  304. cgsize2subreg:=R_SUBNONE;
  305. OS_F32,OS_F64,OS_C64:
  306. case regtype of
  307. R_FPUREGISTER:
  308. cgsize2subreg:=R_SUBWHOLE;
  309. R_MMREGISTER:
  310. case s of
  311. OS_F32:
  312. cgsize2subreg:=R_SUBMMS;
  313. OS_F64:
  314. cgsize2subreg:=R_SUBMMD;
  315. else
  316. internalerror(2009071901);
  317. end;
  318. else
  319. internalerror(2009071902);
  320. end;
  321. OS_M128,OS_MS128:
  322. cgsize2subreg:=R_SUBMMX;
  323. OS_M256,OS_MS256:
  324. cgsize2subreg:=R_SUBMMY;
  325. else
  326. internalerror(200301231);
  327. end;
  328. end;
  329. function reg_cgsize(const reg: tregister): tcgsize;
  330. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  331. (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);
  332. begin
  333. case getregtype(reg) of
  334. R_INTREGISTER :
  335. reg_cgsize:=subreg2cgsize[getsubreg(reg)];
  336. R_FPUREGISTER :
  337. reg_cgsize:=OS_F80;
  338. R_MMXREGISTER:
  339. reg_cgsize:=OS_M64;
  340. R_MMREGISTER:
  341. reg_cgsize:=subreg2cgsize[getsubreg(reg)];
  342. R_SPECIALREGISTER :
  343. case reg of
  344. NR_CS,NR_DS,NR_ES,NR_SS,NR_FS,NR_GS:
  345. reg_cgsize:=OS_16;
  346. {$ifdef x86_64}
  347. NR_DR0..NR_TR7:
  348. reg_cgsize:=OS_64;
  349. {$endif x86_64}
  350. else
  351. reg_cgsize:=OS_32
  352. end
  353. else
  354. internalerror(2003031801);
  355. end;
  356. end;
  357. function reg2opsize(r:Tregister):topsize;
  358. const
  359. subreg2opsize : array[tsubregister] of topsize =
  360. (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);
  361. begin
  362. reg2opsize:=S_L;
  363. case getregtype(r) of
  364. R_INTREGISTER :
  365. reg2opsize:=subreg2opsize[getsubreg(r)];
  366. R_FPUREGISTER :
  367. reg2opsize:=S_FL;
  368. R_MMXREGISTER,
  369. R_MMREGISTER :
  370. reg2opsize:=S_MD;
  371. R_SPECIALREGISTER :
  372. begin
  373. case r of
  374. NR_CS,NR_DS,NR_ES,
  375. NR_SS,NR_FS,NR_GS :
  376. reg2opsize:=S_W;
  377. end;
  378. end;
  379. else
  380. internalerror(200303181);
  381. end;
  382. end;
  383. function is_calljmp(o:tasmop):boolean;
  384. begin
  385. case o of
  386. A_CALL,
  387. {$if defined(i386) or defined(i8086)}
  388. A_JCXZ,
  389. {$endif defined(i386) or defined(i8086)}
  390. A_JECXZ,
  391. {$ifdef x86_64}
  392. A_JRCXZ,
  393. {$endif x86_64}
  394. A_JMP,
  395. A_LOOP,
  396. A_LOOPE,
  397. A_LOOPNE,
  398. A_LOOPNZ,
  399. A_LOOPZ,
  400. A_LCALL,
  401. A_LJMP,
  402. A_Jcc :
  403. is_calljmp:=true;
  404. else
  405. is_calljmp:=false;
  406. end;
  407. end;
  408. procedure inverse_flags(var f: TResFlags);
  409. const
  410. inv_flags: array[TResFlags] of TResFlags =
  411. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_NC,F_C,
  412. F_BE,F_B,F_AE,F_A,
  413. F_NS,F_S,F_NO,F_O);
  414. begin
  415. f:=inv_flags[f];
  416. end;
  417. function flags_to_cond(const f: TResFlags) : TAsmCond;
  418. const
  419. flags_2_cond : array[TResFlags] of TAsmCond =
  420. (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);
  421. begin
  422. result := flags_2_cond[f];
  423. end;
  424. function is_segment_reg(r:tregister):boolean;
  425. begin
  426. result:=false;
  427. case r of
  428. NR_CS,NR_DS,NR_ES,
  429. NR_SS,NR_FS,NR_GS :
  430. result:=true;
  431. end;
  432. end;
  433. function findreg_by_number(r:Tregister):tregisterindex;
  434. var
  435. hr : tregister;
  436. begin
  437. { for the name the sub reg doesn't matter }
  438. hr:=r;
  439. if (getregtype(hr)=R_MMREGISTER) and
  440. (getsubreg(hr)<>R_SUBMMY) then
  441. setsubreg(hr,R_SUBMMX);
  442. result:=findreg_by_number_table(hr,regnumber_index);
  443. end;
  444. function std_regnum_search(const s:string):Tregister;
  445. begin
  446. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  447. end;
  448. function std_regname(r:Tregister):string;
  449. var
  450. p : tregisterindex;
  451. begin
  452. if getregtype(r) in [R_MMREGISTER,R_MMXREGISTER] then
  453. r:=newreg(getregtype(r),getsupreg(r),R_SUBNONE);
  454. p:=findreg_by_number(r);
  455. if p<>0 then
  456. result:=std_regname_table[p]
  457. else
  458. result:=generic_regname(r);
  459. end;
  460. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  461. const
  462. inverse: array[TAsmCond] of TAsmCond=(C_None,
  463. C_NA,C_NAE,C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_A,C_AE,
  464. C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_O,C_P,
  465. C_S,C_Z,C_NO,C_NP,C_NP,C_P,C_NS,C_NZ
  466. );
  467. begin
  468. result := inverse[c];
  469. end;
  470. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  471. begin
  472. result := c1 = c2;
  473. end;
  474. function dwarf_reg(r:tregister):shortint;
  475. begin
  476. result:=regdwarf_table[findreg_by_number(r)];
  477. if result=-1 then
  478. internalerror(200603251);
  479. end;
  480. function segment_regs_equal(r1, r2: tregister): boolean;
  481. begin
  482. if not is_segment_reg(r1) or not is_segment_reg(r2) then
  483. internalerror(2013062301);
  484. { every segment register is equal to itself }
  485. if r1=r2 then
  486. exit(true);
  487. {$if defined(i8086)}
  488. case current_settings.x86memorymodel of
  489. mm_tiny:
  490. begin
  491. { CS=DS=SS }
  492. if ((r1=NR_CS) or (r1=NR_DS) or (r1=NR_SS)) and
  493. ((r2=NR_CS) or (r2=NR_DS) or (r2=NR_SS)) then
  494. exit(true);
  495. { the remaining are distinct from each other }
  496. exit(false);
  497. end;
  498. mm_small,mm_medium:
  499. begin
  500. { DS=SS }
  501. if ((r1=NR_DS) or (r1=NR_SS)) and
  502. ((r2=NR_DS) or (r2=NR_SS)) then
  503. exit(true);
  504. { the remaining are distinct from each other }
  505. exit(false);
  506. end;
  507. mm_compact,mm_large,mm_huge:
  508. { all segment registers are different in these models }
  509. exit(false);
  510. else
  511. internalerror(2013062302);
  512. end;
  513. {$elseif defined(i386) or defined(x86_64)}
  514. { DS=SS=ES }
  515. if ((r1=NR_DS) or (r1=NR_SS) or (r1=NR_ES)) and
  516. ((r2=NR_DS) or (r2=NR_SS) or (r2=NR_ES)) then
  517. exit(true);
  518. { the remaining are distinct from each other }
  519. exit(false);
  520. {$endif}
  521. end;
  522. {$ifdef i8086}
  523. function GetNextReg(const r: TRegister): TRegister;
  524. begin
  525. if getsupreg(r)<first_int_imreg then
  526. internalerror(2013051401);
  527. result:=TRegister(longint(r)+1);
  528. end;
  529. function requires_fwait_on_8087(op: TAsmOp): boolean;
  530. begin
  531. case op of
  532. A_F2XM1,A_FABS,A_FADD,A_FADDP,A_FBLD,A_FBSTP,A_FCHS,A_FCOM,A_FCOMP,
  533. A_FCOMPP,A_FDECSTP,A_FDIV,A_FDIVP,A_FDIVR,A_FDIVRP,
  534. A_FFREE,A_FIADD,A_FICOM,A_FICOMP,A_FIDIV,A_FIDIVR,A_FILD,
  535. A_FIMUL,A_FINCSTP,A_FIST,A_FISTP,A_FISUB,A_FISUBR,A_FLD,A_FLD1,
  536. A_FLDCW,A_FLDENV,A_FLDL2E,A_FLDL2T,A_FLDLG2,A_FLDLN2,A_FLDPI,A_FLDZ,
  537. A_FMUL,A_FMULP,A_FNOP,A_FPATAN,A_FPREM,A_FPTAN,A_FRNDINT,
  538. A_FRSTOR,A_FSCALE,A_FSQRT,A_FST,
  539. A_FSTP,A_FSUB,A_FSUBP,A_FSUBR,A_FSUBRP,A_FTST,
  540. A_FXAM,A_FXCH,A_FXTRACT,A_FYL2X,A_FYL2XP1:
  541. result:=true;
  542. else
  543. result:=false;
  544. end;
  545. end;
  546. {$endif i8086}
  547. end.