cpubase.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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 : (register64 : tregister64);
  271. );
  272. { it's only for better handling }
  273. LOC_MMXREGISTER,LOC_CMMXREGISTER : (mmxreg : tregister);
  274. end;
  275. {*****************************************************************************
  276. Constants
  277. *****************************************************************************}
  278. const
  279. { declare aliases }
  280. LOC_SSEREGISTER = LOC_MMREGISTER;
  281. LOC_CSSEREGISTER = LOC_CMMREGISTER;
  282. max_operands = 3;
  283. maxfpuregs = 8;
  284. {*****************************************************************************
  285. CPU Dependent Constants
  286. *****************************************************************************}
  287. {$i cpubase.inc}
  288. {*****************************************************************************
  289. Helpers
  290. *****************************************************************************}
  291. function cgsize2subreg(s:Tcgsize):Tsubregister;
  292. function reg2opsize(r:Tregister):topsize;
  293. function reg_cgsize(const reg: tregister): tcgsize;
  294. function is_calljmp(o:tasmop):boolean;
  295. procedure inverse_flags(var f: TResFlags);
  296. function flags_to_cond(const f: TResFlags) : TAsmCond;
  297. function is_segment_reg(r:tregister):boolean;
  298. function findreg_by_number(r:Tregister):tregisterindex;
  299. function std_regnum_search(const s:string):Tregister;
  300. function std_regname(r:Tregister):string;
  301. implementation
  302. uses
  303. rgbase,verbose;
  304. const
  305. {$ifdef x86_64}
  306. std_regname_table : array[tregisterindex] of string[7] = (
  307. {$i r8664std.inc}
  308. );
  309. regnumber_index : array[tregisterindex] of tregisterindex = (
  310. {$i r8664rni.inc}
  311. );
  312. std_regname_index : array[tregisterindex] of tregisterindex = (
  313. {$i r8664sri.inc}
  314. );
  315. {$else x86_64}
  316. std_regname_table : array[tregisterindex] of string[7] = (
  317. {$i r386std.inc}
  318. );
  319. regnumber_index : array[tregisterindex] of tregisterindex = (
  320. {$i r386rni.inc}
  321. );
  322. std_regname_index : array[tregisterindex] of tregisterindex = (
  323. {$i r386sri.inc}
  324. );
  325. {$endif x86_64}
  326. {*****************************************************************************
  327. Helpers
  328. *****************************************************************************}
  329. function cgsize2subreg(s:Tcgsize):Tsubregister;
  330. begin
  331. case s of
  332. OS_8,OS_S8:
  333. cgsize2subreg:=R_SUBL;
  334. OS_16,OS_S16:
  335. cgsize2subreg:=R_SUBW;
  336. OS_32,OS_S32:
  337. cgsize2subreg:=R_SUBD;
  338. OS_64,OS_S64:
  339. cgsize2subreg:=R_SUBQ;
  340. OS_M64:
  341. cgsize2subreg:=R_SUBNONE;
  342. OS_F32,OS_F64,OS_C64,
  343. OS_M128,OS_MS128:
  344. cgsize2subreg:=R_SUBWHOLE;
  345. else
  346. internalerror(200301231);
  347. end;
  348. end;
  349. function reg_cgsize(const reg: tregister): tcgsize;
  350. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  351. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO,OS_NO);
  352. begin
  353. case getregtype(reg) of
  354. R_INTREGISTER :
  355. reg_cgsize:=subreg2cgsize[getsubreg(reg)];
  356. R_FPUREGISTER :
  357. reg_cgsize:=OS_F80;
  358. R_MMXREGISTER:
  359. reg_cgsize:=OS_M64;
  360. R_MMREGISTER:
  361. reg_cgsize:=OS_M128;
  362. R_SPECIALREGISTER :
  363. case reg of
  364. NR_CS,NR_DS,NR_ES,NR_SS,NR_FS,NR_GS:
  365. reg_cgsize:=OS_16
  366. else
  367. reg_cgsize:=OS_32
  368. end
  369. else
  370. internalerror(200303181);
  371. end;
  372. end;
  373. function reg2opsize(r:Tregister):topsize;
  374. const
  375. subreg2opsize : array[tsubregister] of topsize =
  376. (S_NO,S_B,S_B,S_W,S_L,S_Q,S_NO,S_NO,S_NO);
  377. begin
  378. reg2opsize:=S_L;
  379. case getregtype(r) of
  380. R_INTREGISTER :
  381. reg2opsize:=subreg2opsize[getsubreg(r)];
  382. R_FPUREGISTER :
  383. reg2opsize:=S_FL;
  384. R_MMXREGISTER,
  385. R_MMREGISTER :
  386. reg2opsize:=S_MD;
  387. R_SPECIALREGISTER :
  388. begin
  389. case r of
  390. NR_CS,NR_DS,NR_ES,
  391. NR_SS,NR_FS,NR_GS :
  392. reg2opsize:=S_W;
  393. end;
  394. end;
  395. else
  396. internalerror(200303181);
  397. end;
  398. end;
  399. function is_calljmp(o:tasmop):boolean;
  400. begin
  401. case o of
  402. A_CALL,
  403. A_JCXZ,
  404. A_JECXZ,
  405. A_JMP,
  406. A_LOOP,
  407. A_LOOPE,
  408. A_LOOPNE,
  409. A_LOOPNZ,
  410. A_LOOPZ,
  411. A_Jcc :
  412. is_calljmp:=true;
  413. else
  414. is_calljmp:=false;
  415. end;
  416. end;
  417. procedure inverse_flags(var f: TResFlags);
  418. const
  419. inv_flags: array[TResFlags] of TResFlags =
  420. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_NC,F_C,
  421. F_BE,F_B,F_AE,F_A,
  422. F_NS,F_S,F_NO,F_O);
  423. begin
  424. f:=inv_flags[f];
  425. end;
  426. function flags_to_cond(const f: TResFlags) : TAsmCond;
  427. const
  428. flags_2_cond : array[TResFlags] of TAsmCond =
  429. (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);
  430. begin
  431. result := flags_2_cond[f];
  432. end;
  433. function is_segment_reg(r:tregister):boolean;
  434. begin
  435. result:=false;
  436. case r of
  437. NR_CS,NR_DS,NR_ES,
  438. NR_SS,NR_FS,NR_GS :
  439. result:=true;
  440. end;
  441. end;
  442. function findreg_by_number(r:Tregister):tregisterindex;
  443. begin
  444. result:=findreg_by_number_table(r,regnumber_index);
  445. end;
  446. function std_regnum_search(const s:string):Tregister;
  447. begin
  448. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  449. end;
  450. function std_regname(r:Tregister):string;
  451. var
  452. p : tregisterindex;
  453. begin
  454. p:=findreg_by_number_table(r,regnumber_index);
  455. if p<>0 then
  456. result:=std_regname_table[p]
  457. else
  458. result:=generic_regname(r);
  459. end;
  460. end.
  461. {
  462. $Log$
  463. Revision 1.48 2004-10-25 15:36:47 peter
  464. * save standard registers moved to tcgobj
  465. Revision 1.47 2004/10/15 09:22:23 mazen
  466. - remove $IFDEF DELPHI and related code
  467. - remove $IFDEF FPCPROCVAR and related code
  468. Revision 1.46 2004/08/24 21:23:11 florian
  469. * fixed i386 compilation
  470. Revision 1.45 2004/07/09 23:30:13 jonas
  471. * changed first_sse_imreg to first_mm_imreg
  472. Revision 1.44 2004/06/20 08:55:32 florian
  473. * logs truncated
  474. Revision 1.43 2004/06/16 20:07:11 florian
  475. * dwarf branch merged
  476. Revision 1.42.2.5 2004/05/28 20:29:50 florian
  477. * fixed currency trouble on x86-64
  478. Revision 1.42.2.4 2004/05/01 16:02:10 peter
  479. * POINTER_SIZE replaced with sizeof(aint)
  480. * aint,aword,tconst*int moved to globtype
  481. Revision 1.42.2.3 2004/05/01 11:12:24 florian
  482. * spilling of registers with size<>4 fixed
  483. Revision 1.42.2.2 2004/04/27 18:18:26 peter
  484. * aword -> aint
  485. }