cpubase.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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,
  31. cgbase
  32. ;
  33. {*****************************************************************************
  34. Assembler Opcodes
  35. *****************************************************************************}
  36. type
  37. {$ifdef x86_64}
  38. TAsmOp={$i x8664op.inc}
  39. {$else x86_64}
  40. TAsmOp={$i i386op.inc}
  41. {$endif x86_64}
  42. { This should define the array of instructions as string }
  43. op2strtable=array[tasmop] of string[11];
  44. const
  45. { First value of opcode enumeration }
  46. firstop = low(tasmop);
  47. { Last value of opcode enumeration }
  48. lastop = high(tasmop);
  49. {*****************************************************************************
  50. Registers
  51. *****************************************************************************}
  52. const
  53. { Invalid register number }
  54. RS_INVALID = $ff;
  55. { Integer Super registers }
  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. { Number of first imaginary register }
  82. first_int_imreg = $10;
  83. { Float Super registers }
  84. RS_ST0 = $00;
  85. RS_ST1 = $01;
  86. RS_ST2 = $02;
  87. RS_ST3 = $03;
  88. RS_ST4 = $04;
  89. RS_ST5 = $05;
  90. RS_ST6 = $06;
  91. RS_ST7 = $07;
  92. { Number of first imaginary register }
  93. first_fpu_imreg = $08;
  94. { MM Super registers }
  95. RS_XMM0 = $00;
  96. RS_XMM1 = $01;
  97. RS_XMM2 = $02;
  98. RS_XMM3 = $03;
  99. RS_XMM4 = $04;
  100. RS_XMM5 = $05;
  101. RS_XMM6 = $06;
  102. RS_XMM7 = $07;
  103. RS_XMM8 = $08;
  104. RS_XMM9 = $09;
  105. RS_XMM10 = $0a;
  106. RS_XMM11 = $0b;
  107. RS_XMM12 = $0c;
  108. RS_XMM13 = $0d;
  109. RS_XMM14 = $0e;
  110. RS_XMM15 = $0f;
  111. { Number of first imaginary register }
  112. {$ifdef x86_64}
  113. first_mm_imreg = $10;
  114. {$else x86_64}
  115. first_mm_imreg = $08;
  116. {$endif x86_64}
  117. { The subregister that specifies the entire register }
  118. {$ifdef x86_64}
  119. R_SUBWHOLE = R_SUBQ; {Hammer}
  120. {$else x86_64}
  121. R_SUBWHOLE = R_SUBD; {i386}
  122. {$endif x86_64}
  123. { Available Registers }
  124. {$ifdef x86_64}
  125. {$i r8664con.inc}
  126. {$else x86_64}
  127. {$i r386con.inc}
  128. {$endif x86_64}
  129. type
  130. { Number of registers used for indexing in tables }
  131. {$ifdef x86_64}
  132. tregisterindex=0..{$i r8664nor.inc}-1;
  133. {$else x86_64}
  134. tregisterindex=0..{$i r386nor.inc}-1;
  135. {$endif x86_64}
  136. const
  137. {$warning TODO Calculate bsstart}
  138. regnumber_count_bsstart = 64;
  139. regnumber_table : array[tregisterindex] of tregister = (
  140. {$ifdef x86_64}
  141. {$i r8664num.inc}
  142. {$else x86_64}
  143. {$i r386num.inc}
  144. {$endif x86_64}
  145. );
  146. regstabs_table : array[tregisterindex] of shortint = (
  147. {$ifdef x86_64}
  148. {$i r8664stab.inc}
  149. {$else x86_64}
  150. {$i r386stab.inc}
  151. {$endif x86_64}
  152. );
  153. regdwarf_table : array[tregisterindex] of shortint = (
  154. {$ifdef x86_64}
  155. {$i r8664dwrf.inc}
  156. {$else x86_64}
  157. {$i r386dwrf.inc}
  158. {$endif x86_64}
  159. );
  160. type
  161. totherregisterset = set of tregisterindex;
  162. {*****************************************************************************
  163. Conditions
  164. *****************************************************************************}
  165. type
  166. TAsmCond=(C_None,
  167. C_A,C_AE,C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_NA,C_NAE,
  168. C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_NO,C_NP,
  169. C_NS,C_NZ,C_O,C_P,C_PE,C_PO,C_S,C_Z
  170. );
  171. const
  172. cond2str:array[TAsmCond] of string[3]=('',
  173. 'a','ae','b','be','c','e','g','ge','l','le','na','nae',
  174. 'nb','nbe','nc','ne','ng','nge','nl','nle','no','np',
  175. 'ns','nz','o','p','pe','po','s','z'
  176. );
  177. {*****************************************************************************
  178. Flags
  179. *****************************************************************************}
  180. type
  181. TResFlags = (F_E,F_NE,F_G,F_L,F_GE,F_LE,F_C,F_NC,
  182. F_A,F_AE,F_B,F_BE,
  183. F_S,F_NS,F_O,F_NO);
  184. {*****************************************************************************
  185. Constants
  186. *****************************************************************************}
  187. const
  188. { declare aliases }
  189. LOC_SSEREGISTER = LOC_MMREGISTER;
  190. LOC_CSSEREGISTER = LOC_CMMREGISTER;
  191. max_operands = 3;
  192. maxfpuregs = 8;
  193. {*****************************************************************************
  194. CPU Dependent Constants
  195. *****************************************************************************}
  196. {$i cpubase.inc}
  197. {*****************************************************************************
  198. Helpers
  199. *****************************************************************************}
  200. function cgsize2subreg(s:Tcgsize):Tsubregister;
  201. function reg2opsize(r:Tregister):topsize;
  202. function reg_cgsize(const reg: tregister): tcgsize;
  203. function is_calljmp(o:tasmop):boolean;
  204. procedure inverse_flags(var f: TResFlags);
  205. function flags_to_cond(const f: TResFlags) : TAsmCond;
  206. function is_segment_reg(r:tregister):boolean;
  207. function findreg_by_number(r:Tregister):tregisterindex;
  208. function std_regnum_search(const s:string):Tregister;
  209. function std_regname(r:Tregister):string;
  210. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  211. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  212. implementation
  213. uses
  214. rgbase,verbose;
  215. const
  216. {$ifdef x86_64}
  217. std_regname_table : array[tregisterindex] of string[7] = (
  218. {$i r8664std.inc}
  219. );
  220. regnumber_index : array[tregisterindex] of tregisterindex = (
  221. {$i r8664rni.inc}
  222. );
  223. std_regname_index : array[tregisterindex] of tregisterindex = (
  224. {$i r8664sri.inc}
  225. );
  226. {$else x86_64}
  227. std_regname_table : array[tregisterindex] of string[7] = (
  228. {$i r386std.inc}
  229. );
  230. regnumber_index : array[tregisterindex] of tregisterindex = (
  231. {$i r386rni.inc}
  232. );
  233. std_regname_index : array[tregisterindex] of tregisterindex = (
  234. {$i r386sri.inc}
  235. );
  236. {$endif x86_64}
  237. {*****************************************************************************
  238. Helpers
  239. *****************************************************************************}
  240. function cgsize2subreg(s:Tcgsize):Tsubregister;
  241. begin
  242. case s of
  243. OS_8,OS_S8:
  244. cgsize2subreg:=R_SUBL;
  245. OS_16,OS_S16:
  246. cgsize2subreg:=R_SUBW;
  247. OS_32,OS_S32:
  248. cgsize2subreg:=R_SUBD;
  249. OS_64,OS_S64:
  250. cgsize2subreg:=R_SUBQ;
  251. OS_M64:
  252. cgsize2subreg:=R_SUBNONE;
  253. OS_F32,OS_F64,OS_C64,
  254. OS_M128,OS_MS128:
  255. cgsize2subreg:=R_SUBWHOLE;
  256. else
  257. internalerror(200301231);
  258. end;
  259. end;
  260. function reg_cgsize(const reg: tregister): tcgsize;
  261. const subreg2cgsize:array[Tsubregister] of Tcgsize =
  262. (OS_NO,OS_8,OS_8,OS_16,OS_32,OS_64,OS_NO,OS_NO,OS_NO);
  263. begin
  264. case getregtype(reg) of
  265. R_INTREGISTER :
  266. reg_cgsize:=subreg2cgsize[getsubreg(reg)];
  267. R_FPUREGISTER :
  268. reg_cgsize:=OS_F80;
  269. R_MMXREGISTER:
  270. reg_cgsize:=OS_M64;
  271. R_MMREGISTER:
  272. reg_cgsize:=OS_M128;
  273. R_SPECIALREGISTER :
  274. case reg of
  275. NR_CS,NR_DS,NR_ES,NR_SS,NR_FS,NR_GS:
  276. reg_cgsize:=OS_16
  277. else
  278. reg_cgsize:=OS_32
  279. end
  280. else
  281. internalerror(200303181);
  282. end;
  283. end;
  284. function reg2opsize(r:Tregister):topsize;
  285. const
  286. subreg2opsize : array[tsubregister] of topsize =
  287. (S_NO,S_B,S_B,S_W,S_L,S_Q,S_NO,S_NO,S_NO);
  288. begin
  289. reg2opsize:=S_L;
  290. case getregtype(r) of
  291. R_INTREGISTER :
  292. reg2opsize:=subreg2opsize[getsubreg(r)];
  293. R_FPUREGISTER :
  294. reg2opsize:=S_FL;
  295. R_MMXREGISTER,
  296. R_MMREGISTER :
  297. reg2opsize:=S_MD;
  298. R_SPECIALREGISTER :
  299. begin
  300. case r of
  301. NR_CS,NR_DS,NR_ES,
  302. NR_SS,NR_FS,NR_GS :
  303. reg2opsize:=S_W;
  304. end;
  305. end;
  306. else
  307. internalerror(200303181);
  308. end;
  309. end;
  310. function is_calljmp(o:tasmop):boolean;
  311. begin
  312. case o of
  313. A_CALL,
  314. A_JCXZ,
  315. A_JECXZ,
  316. A_JMP,
  317. A_LOOP,
  318. A_LOOPE,
  319. A_LOOPNE,
  320. A_LOOPNZ,
  321. A_LOOPZ,
  322. A_Jcc :
  323. is_calljmp:=true;
  324. else
  325. is_calljmp:=false;
  326. end;
  327. end;
  328. procedure inverse_flags(var f: TResFlags);
  329. const
  330. inv_flags: array[TResFlags] of TResFlags =
  331. (F_NE,F_E,F_LE,F_GE,F_L,F_G,F_NC,F_C,
  332. F_BE,F_B,F_AE,F_A,
  333. F_NS,F_S,F_NO,F_O);
  334. begin
  335. f:=inv_flags[f];
  336. end;
  337. function flags_to_cond(const f: TResFlags) : TAsmCond;
  338. const
  339. flags_2_cond : array[TResFlags] of TAsmCond =
  340. (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);
  341. begin
  342. result := flags_2_cond[f];
  343. end;
  344. function is_segment_reg(r:tregister):boolean;
  345. begin
  346. result:=false;
  347. case r of
  348. NR_CS,NR_DS,NR_ES,
  349. NR_SS,NR_FS,NR_GS :
  350. result:=true;
  351. end;
  352. end;
  353. function findreg_by_number(r:Tregister):tregisterindex;
  354. begin
  355. result:=findreg_by_number_table(r,regnumber_index);
  356. end;
  357. function std_regnum_search(const s:string):Tregister;
  358. begin
  359. result:=regnumber_table[findreg_by_name_table(s,std_regname_table,std_regname_index)];
  360. end;
  361. function std_regname(r:Tregister):string;
  362. var
  363. p : tregisterindex;
  364. begin
  365. p:=findreg_by_number_table(r,regnumber_index);
  366. if p<>0 then
  367. result:=std_regname_table[p]
  368. else
  369. result:=generic_regname(r);
  370. end;
  371. function inverse_cond(const c: TAsmCond): TAsmCond; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  372. const
  373. inverse: array[TAsmCond] of TAsmCond=(C_None,
  374. C_NA,C_NAE,C_NB,C_NBE,C_NC,C_NE,C_NG,C_NGE,C_NL,C_NLE,C_A,C_AE,
  375. C_B,C_BE,C_C,C_E,C_G,C_GE,C_L,C_LE,C_O,C_P,
  376. C_S,C_Z,C_NO,C_NP,C_NP,C_P,C_NS,C_NZ
  377. );
  378. begin
  379. result := inverse[c];
  380. end;
  381. function conditions_equal(const c1, c2: TAsmCond): boolean; {$ifdef USEINLINE}inline;{$endif USEINLINE}
  382. begin
  383. result := c1 = c2;
  384. end;
  385. end.
  386. {
  387. $Log$
  388. Revision 1.51 2005-02-26 01:27:00 jonas
  389. * fixed generic jumps optimizer and enabled it for ppc (the label table
  390. was not being initialised -> getfinaldestination always failed, which
  391. caused wrong optimizations in some cases)
  392. * changed the inverse_cond into a function, because tasmcond is a record
  393. on ppc
  394. + added a compare_conditions() function for the same reason
  395. Revision 1.50 2005/02/14 17:13:10 peter
  396. * truncate log
  397. }