cpubase.pas 14 KB

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