cpubase.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  4. Contains the base types for the i386 and x86-64 architecture
  5. * This code was inspired by the NASM sources
  6. The Netwide Assembler is Copyright (c) 1996 Simon Tatham and
  7. Julian Hall. All rights reserved.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. ****************************************************************************
  20. }
  21. {# Base unit for processor information. This unit contains
  22. enumerations of registers, opcodes, sizes, and other
  23. such things which are processor specific.
  24. }
  25. unit cpubase;
  26. {$i fpcdefs.inc}
  27. interface
  28. uses
  29. cutils,cclasses,
  30. globtype,globals,
  31. cpuinfo,
  32. aasmbase,
  33. cgbase
  34. ;
  35. {*****************************************************************************
  36. Assembler Opcodes
  37. *****************************************************************************}
  38. type
  39. {$ifdef x86_64}
  40. TAsmOp={$i x8664op.inc}
  41. {$else x86_64}
  42. TAsmOp={$i i386op.inc}
  43. {$endif x86_64}
  44. { This should define the array of instructions as string }
  45. op2strtable=array[tasmop] of string[11];
  46. const
  47. { First value of opcode enumeration }
  48. firstop = low(tasmop);
  49. { Last value of opcode enumeration }
  50. lastop = high(tasmop);
  51. {*****************************************************************************
  52. Registers
  53. *****************************************************************************}
  54. const
  55. { Invalid register number }
  56. RS_INVALID = $ff;
  57. { Integer Super registers }
  58. RS_RAX = $00; {EAX}
  59. RS_RCX = $01; {ECX}
  60. RS_RDX = $02; {EDX}
  61. RS_RBX = $03; {EBX}
  62. RS_RSI = $04; {ESI}
  63. RS_RDI = $05; {EDI}
  64. RS_RBP = $06; {EBP}
  65. RS_RSP = $07; {ESP}
  66. RS_R8 = $08; {R8}
  67. RS_R9 = $09; {R9}
  68. RS_R10 = $0a; {R10}
  69. RS_R11 = $0b; {R11}
  70. RS_R12 = $0c; {R12}
  71. RS_R13 = $0d; {R13}
  72. RS_R14 = $0e; {R14}
  73. RS_R15 = $0f; {R15}
  74. { create aliases to allow code sharing between x86-64 and i386 }
  75. RS_EAX = RS_RAX;
  76. RS_EBX = RS_RBX;
  77. RS_ECX = RS_RCX;
  78. RS_EDX = RS_RDX;
  79. RS_ESI = RS_RSI;
  80. RS_EDI = RS_RDI;
  81. RS_EBP = RS_RBP;
  82. RS_ESP = RS_RSP;
  83. { Number of first imaginary register }
  84. first_int_imreg = $10;
  85. { Float Super registers }
  86. RS_ST0 = $00;
  87. RS_ST1 = $01;
  88. RS_ST2 = $02;
  89. RS_ST3 = $03;
  90. RS_ST4 = $04;
  91. RS_ST5 = $05;
  92. RS_ST6 = $06;
  93. RS_ST7 = $07;
  94. { Number of first imaginary register }
  95. first_fpu_imreg = $08;
  96. { MM Super registers }
  97. RS_XMM0 = $00;
  98. RS_XMM1 = $01;
  99. RS_XMM2 = $02;
  100. RS_XMM3 = $03;
  101. RS_XMM4 = $04;
  102. RS_XMM5 = $05;
  103. RS_XMM6 = $06;
  104. RS_XMM7 = $07;
  105. RS_XMM8 = $08;
  106. RS_XMM9 = $09;
  107. RS_XMM10 = $0a;
  108. RS_XMM11 = $0b;
  109. RS_XMM12 = $0c;
  110. RS_XMM13 = $0d;
  111. RS_XMM14 = $0e;
  112. RS_XMM15 = $0f;
  113. { Number of first imaginary register }
  114. {$ifdef x86_64}
  115. first_mm_imreg = $10;
  116. {$else x86_64}
  117. first_mm_imreg = $08;
  118. {$endif x86_64}
  119. { The subregister that specifies the entire register }
  120. {$ifdef x86_64}
  121. R_SUBWHOLE = R_SUBQ; {Hammer}
  122. {$else x86_64}
  123. R_SUBWHOLE = R_SUBD; {i386}
  124. {$endif x86_64}
  125. { Available Registers }
  126. {$ifdef x86_64}
  127. {$i r8664con.inc}
  128. {$else x86_64}
  129. {$i r386con.inc}
  130. {$endif x86_64}
  131. type
  132. { Number of registers used for indexing in tables }
  133. {$ifdef x86_64}
  134. tregisterindex=0..{$i r8664nor.inc}-1;
  135. {$else x86_64}
  136. tregisterindex=0..{$i r386nor.inc}-1;
  137. {$endif x86_64}
  138. const
  139. {$warning TODO Calculate bsstart}
  140. regnumber_count_bsstart = 64;
  141. regnumber_table : array[tregisterindex] of tregister = (
  142. {$ifdef x86_64}
  143. {$i r8664num.inc}
  144. {$else x86_64}
  145. {$i r386num.inc}
  146. {$endif x86_64}
  147. );
  148. regstabs_table : array[tregisterindex] of shortint = (
  149. {$ifdef x86_64}
  150. {$i r8664stab.inc}
  151. {$else x86_64}
  152. {$i r386stab.inc}
  153. {$endif x86_64}
  154. );
  155. regdwarf_table : array[tregisterindex] of shortint = (
  156. {$ifdef x86_64}
  157. {$i r8664dwrf.inc}
  158. {$else x86_64}
  159. {$i r386dwrf.inc}
  160. {$endif x86_64}
  161. );
  162. type
  163. totherregisterset = set of tregisterindex;
  164. {*****************************************************************************
  165. Conditions
  166. *****************************************************************************}
  167. type
  168. TAsmCond=(C_None,
  169. C_A,C_AE,C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_NA,C_NAE,
  170. C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_NO,C_NP,
  171. C_NS,C_NZ,C_O,C_P,C_PE,C_PO,C_S,C_Z
  172. );
  173. const
  174. cond2str:array[TAsmCond] of string[3]=('',
  175. 'a','ae','b','be','c','e','g','ge','l','le','na','nae',
  176. 'nb','nbe','nc','ne','ng','nge','nl','nle','no','np',
  177. 'ns','nz','o','p','pe','po','s','z'
  178. );
  179. inverse_cond:array[TAsmCond] of TAsmCond=(C_None,
  180. C_NA,C_NAE,C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_A,C_AE,
  181. C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_O,C_P,
  182. C_S,C_Z,C_NO,C_NP,C_NP,C_P,C_NS,C_NZ
  183. );
  184. {*****************************************************************************
  185. Flags
  186. *****************************************************************************}
  187. type
  188. TResFlags = (F_E,F_NE,F_G,F_L,F_GE,F_LE,F_C,F_NC,
  189. F_A,F_AE,F_B,F_BE,
  190. F_S,F_NS,F_O,F_NO);
  191. {*****************************************************************************
  192. Reference
  193. *****************************************************************************}
  194. type
  195. { reference record, reordered for best alignment }
  196. preference = ^treference;
  197. treference = record
  198. offset : aint;
  199. symbol,
  200. relsymbol : tasmsymbol;
  201. segment,
  202. base,
  203. index : tregister;
  204. refaddr : trefaddr;
  205. scalefactor : byte;
  206. end;
  207. { reference record }
  208. pparareference = ^tparareference;
  209. tparareference = packed record
  210. index : tregister;
  211. offset : aint;
  212. end;
  213. {*****************************************************************************
  214. Generic Location
  215. *****************************************************************************}
  216. type
  217. { tparamlocation describes where a parameter for a procedure is stored.
  218. References are given from the caller's point of view. The usual
  219. TLocation isn't used, because contains a lot of unnessary fields.
  220. }
  221. tparalocation = record
  222. size : TCGSize;
  223. loc : TCGLoc;
  224. { Location type of registerhigh, for x86_64 this can
  225. be different from loc when pushing structures of 16 bytes }
  226. lochigh : TCGLoc;
  227. alignment : byte;
  228. case TCGLoc of
  229. LOC_REFERENCE : (reference : tparareference);
  230. { segment in reference at the same place as in loc_register }
  231. LOC_REGISTER,LOC_CREGISTER : (
  232. case longint of
  233. 1 : (register,registerhigh : tregister);
  234. { overlay a registerlow }
  235. 2 : (registerlow : tregister);
  236. {$ifndef cpu64bit}
  237. { overlay a 64 Bit register type }
  238. 3 : (register64 : tregister64);
  239. {$endif cpu64bit}
  240. );
  241. { it's only for better handling }
  242. LOC_MMXREGISTER,LOC_CMMXREGISTER : (
  243. case longint of
  244. 0: (mmxreg : tregister);
  245. 1: (mmxregset : Tregistermmxset);
  246. );
  247. end;
  248. tlocation = packed record
  249. loc : TCGLoc;
  250. size : TCGSize;
  251. case TCGLoc of
  252. LOC_FLAGS : (resflags : tresflags);
  253. LOC_CONSTANT : (
  254. case longint of
  255. 1 : (value : AInt);
  256. { can't do this, this layout depends on the host cpu. Use }
  257. { lo(valueqword)/hi(valueqword) instead (JM) }
  258. { overlay a complete 64 Bit value }
  259. 2 : (value64 : Int64);
  260. );
  261. LOC_CREFERENCE,
  262. LOC_REFERENCE : (reference : treference);
  263. { segment in reference at the same place as in loc_register }
  264. LOC_REGISTER,LOC_CREGISTER : (
  265. case longint of
  266. 1 : (register,registerhigh,segment : tregister);
  267. { overlay a registerlow }
  268. 2 : (registerlow : tregister);
  269. { overlay a 64 Bit register type }
  270. 3 : (reg64 : tregister64);
  271. 4 : (register64 : tregister64);
  272. );
  273. { it's only for better handling }
  274. LOC_MMXREGISTER,LOC_CMMXREGISTER : (mmxreg : tregister);
  275. end;
  276. {*****************************************************************************
  277. Constants
  278. *****************************************************************************}
  279. const
  280. { declare aliases }
  281. LOC_SSEREGISTER = LOC_MMREGISTER;
  282. LOC_CSSEREGISTER = LOC_CMMREGISTER;
  283. max_operands = 3;
  284. maxfpuregs = 8;
  285. {*****************************************************************************
  286. CPU Dependent Constants
  287. *****************************************************************************}
  288. {$i cpubase.inc}
  289. {*****************************************************************************
  290. Helpers
  291. *****************************************************************************}
  292. function cgsize2subreg(s:Tcgsize):Tsubregister;
  293. function reg2opsize(r:Tregister):topsize;
  294. function reg_cgsize(const reg: tregister): tcgsize;
  295. function is_calljmp(o:tasmop):boolean;
  296. procedure inverse_flags(var f: TResFlags);
  297. function flags_to_cond(const f: TResFlags) : TAsmCond;
  298. function is_segment_reg(r:tregister):boolean;
  299. function findreg_by_number(r:Tregister):tregisterindex;
  300. function std_regnum_search(const s:string):Tregister;
  301. function std_regname(r:Tregister):string;
  302. implementation
  303. uses
  304. rgbase,verbose;
  305. const
  306. {$ifdef x86_64}
  307. std_regname_table : array[tregisterindex] of string[7] = (
  308. {$i r8664std.inc}
  309. );
  310. regnumber_index : array[tregisterindex] of tregisterindex = (
  311. {$i r8664rni.inc}
  312. );
  313. std_regname_index : array[tregisterindex] of tregisterindex = (
  314. {$i r8664sri.inc}
  315. );
  316. {$else x86_64}
  317. std_regname_table : array[tregisterindex] of string[7] = (
  318. {$i r386std.inc}
  319. );
  320. regnumber_index : array[tregisterindex] of tregisterindex = (
  321. {$i r386rni.inc}
  322. );
  323. std_regname_index : array[tregisterindex] of tregisterindex = (
  324. {$i r386sri.inc}
  325. );
  326. {$endif x86_64}
  327. {*****************************************************************************
  328. Helpers
  329. *****************************************************************************}
  330. function cgsize2subreg(s:Tcgsize):Tsubregister;
  331. begin
  332. case s of
  333. OS_8,OS_S8:
  334. cgsize2subreg:=R_SUBL;
  335. OS_16,OS_S16:
  336. cgsize2subreg:=R_SUBW;
  337. OS_32,OS_S32:
  338. cgsize2subreg:=R_SUBD;
  339. OS_64,OS_S64:
  340. cgsize2subreg:=R_SUBQ;
  341. OS_M64:
  342. cgsize2subreg:=R_SUBNONE;
  343. OS_F32,OS_F64,OS_C64,
  344. OS_M128,OS_MS128:
  345. cgsize2subreg:=R_SUBWHOLE;
  346. else
  347. internalerror(200301231);
  348. end;
  349. end;
  350. function reg_cgsize(const reg: tregister): tcgsize;
  351. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  352. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO,OS_NO);
  353. begin
  354. case getregtype(reg) of
  355. R_INTREGISTER :
  356. reg_cgsize:=subreg2cgsize[getsubreg(reg)];
  357. R_FPUREGISTER :
  358. reg_cgsize:=OS_F80;
  359. R_MMXREGISTER:
  360. reg_cgsize:=OS_M64;
  361. R_MMREGISTER:
  362. reg_cgsize:=OS_M128;
  363. R_SPECIALREGISTER :
  364. case reg of
  365. NR_CS,NR_DS,NR_ES,NR_SS,NR_FS,NR_GS:
  366. reg_cgsize:=OS_16
  367. else
  368. reg_cgsize:=OS_32
  369. end
  370. else
  371. internalerror(200303181);
  372. end;
  373. end;
  374. function reg2opsize(r:Tregister):topsize;
  375. const
  376. subreg2opsize : array[tsubregister] of topsize =
  377. (S_NO,S_B,S_B,S_W,S_L,S_Q,S_NO,S_NO,S_NO);
  378. begin
  379. reg2opsize:=S_L;
  380. case getregtype(r) of
  381. R_INTREGISTER :
  382. reg2opsize:=subreg2opsize[getsubreg(r)];
  383. R_FPUREGISTER :
  384. reg2opsize:=S_FL;
  385. R_MMXREGISTER,
  386. R_MMREGISTER :
  387. reg2opsize:=S_MD;
  388. R_SPECIALREGISTER :
  389. begin
  390. case r of
  391. NR_CS,NR_DS,NR_ES,
  392. NR_SS,NR_FS,NR_GS :
  393. reg2opsize:=S_W;
  394. end;
  395. end;
  396. else
  397. internalerror(200303181);
  398. end;
  399. end;
  400. function is_calljmp(o:tasmop):boolean;
  401. begin
  402. case o of
  403. A_CALL,
  404. A_JCXZ,
  405. A_JECXZ,
  406. A_JMP,
  407. A_LOOP,
  408. A_LOOPE,
  409. A_LOOPNE,
  410. A_LOOPNZ,
  411. A_LOOPZ,
  412. A_Jcc :
  413. is_calljmp:=true;
  414. else
  415. is_calljmp:=false;
  416. end;
  417. end;
  418. procedure inverse_flags(var f: TResFlags);
  419. const
  420. inv_flags: array[TResFlags] of TResFlags =
  421. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_NC,F_C,
  422. F_BE,F_B,F_AE,F_A,
  423. F_NS,F_S,F_NO,F_O);
  424. begin
  425. f:=inv_flags[f];
  426. end;
  427. function flags_to_cond(const f: TResFlags) : TAsmCond;
  428. const
  429. flags_2_cond : array[TResFlags] of TAsmCond =
  430. (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);
  431. begin
  432. result := flags_2_cond[f];
  433. end;
  434. function is_segment_reg(r:tregister):boolean;
  435. begin
  436. result:=false;
  437. case r of
  438. NR_CS,NR_DS,NR_ES,
  439. NR_SS,NR_FS,NR_GS :
  440. result:=true;
  441. end;
  442. end;
  443. function findreg_by_number(r:Tregister):tregisterindex;
  444. begin
  445. result:=findreg_by_number_table(r,regnumber_index);
  446. end;
  447. function std_regnum_search(const s:string):Tregister;
  448. begin
  449. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  450. end;
  451. function std_regname(r:Tregister):string;
  452. var
  453. p : tregisterindex;
  454. begin
  455. p:=findreg_by_number_table(r,regnumber_index);
  456. if p<>0 then
  457. result:=std_regname_table[p]
  458. else
  459. result:=generic_regname(r);
  460. end;
  461. end.
  462. {
  463. $Log$
  464. Revision 1.47 2004-10-15 09:22:23 mazen
  465. - remove $IFDEF DELPHI and related code
  466. - remove $IFDEF FPCPROCVAR and related code
  467. Revision 1.46 2004/08/24 21:23:11 florian
  468. * fixed i386 compilation
  469. Revision 1.45 2004/07/09 23:30:13 jonas
  470. * changed first_sse_imreg to first_mm_imreg
  471. Revision 1.44 2004/06/20 08:55:32 florian
  472. * logs truncated
  473. Revision 1.43 2004/06/16 20:07:11 florian
  474. * dwarf branch merged
  475. Revision 1.42.2.5 2004/05/28 20:29:50 florian
  476. * fixed currency trouble on x86-64
  477. Revision 1.42.2.4 2004/05/01 16:02:10 peter
  478. * POINTER_SIZE replaced with sizeof(aint)
  479. * aint,aword,tconst*int moved to globtype
  480. Revision 1.42.2.3 2004/05/01 11:12:24 florian
  481. * spilling of registers with size<>4 fixed
  482. Revision 1.42.2.2 2004/04/27 18:18:26 peter
  483. * aword -> aint
  484. }