cgbase.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Some basic types and constants for the code generation
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {# This unit exports some types which are used across the code generator }
  19. unit cgbase;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,
  24. cpuinfo,
  25. symconst;
  26. type
  27. { Location types where value can be stored }
  28. TCGLoc=(
  29. LOC_INVALID, { added for tracking problems}
  30. LOC_VOID, { no value is available }
  31. LOC_CONSTANT, { constant value }
  32. LOC_JUMP, { boolean results only, jump to false or true label }
  33. LOC_FLAGS, { boolean results only, flags are set }
  34. LOC_CREFERENCE, { in memory constant value reference (cannot change) }
  35. LOC_REFERENCE, { in memory value }
  36. LOC_REGISTER, { in a processor register }
  37. LOC_CREGISTER, { Constant register which shouldn't be modified }
  38. LOC_FPUREGISTER, { FPU stack }
  39. LOC_CFPUREGISTER, { if it is a FPU register variable on the fpu stack }
  40. LOC_MMXREGISTER, { MMX register }
  41. { MMX register variable }
  42. LOC_CMMXREGISTER,
  43. { multimedia register }
  44. LOC_MMREGISTER,
  45. { Constant multimedia reg which shouldn't be modified }
  46. LOC_CMMREGISTER
  47. );
  48. { since we have only 16 offsets, we need to be able to specify the high
  49. and low 16 bits of the address of a symbol }
  50. trefaddr = (addr_no,addr_full,addr_hi,addr_lo);
  51. {# Generic opcodes, which must be supported by all processors
  52. }
  53. topcg =
  54. (
  55. OP_NONE,
  56. OP_ADD, { simple addition }
  57. OP_AND, { simple logical and }
  58. OP_DIV, { simple unsigned division }
  59. OP_IDIV, { simple signed division }
  60. OP_IMUL, { simple signed multiply }
  61. OP_MUL, { simple unsigned multiply }
  62. OP_NEG, { simple negate }
  63. OP_NOT, { simple logical not }
  64. OP_OR, { simple logical or }
  65. OP_SAR, { arithmetic shift-right }
  66. OP_SHL, { logical shift left }
  67. OP_SHR, { logical shift right }
  68. OP_SUB, { simple subtraction }
  69. OP_XOR { simple exclusive or }
  70. );
  71. {# Generic flag values - used for jump locations }
  72. TOpCmp =
  73. (
  74. OC_NONE,
  75. OC_EQ, { equality comparison }
  76. OC_GT, { greater than (signed) }
  77. OC_LT, { less than (signed) }
  78. OC_GTE, { greater or equal than (signed) }
  79. OC_LTE, { less or equal than (signed) }
  80. OC_NE, { not equal }
  81. OC_BE, { less or equal than (unsigned) }
  82. OC_B, { less than (unsigned) }
  83. OC_AE, { greater or equal than (unsigned) }
  84. OC_A { greater than (unsigned) }
  85. );
  86. { OS_NO is also used memory references with large data that can
  87. not be loaded in a register directly }
  88. TCgSize = (OS_NO,
  89. { integer registers }
  90. OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128,
  91. { single,double,extended,comp,float128 }
  92. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  93. { multi-media sizes: split in byte, word, dword, ... }
  94. { entities, then the signed counterparts }
  95. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,OS_MS8,OS_MS16,OS_MS32,
  96. OS_MS64,OS_MS128);
  97. { Register types }
  98. TRegisterType = (
  99. R_INVALIDREGISTER, { = 0 }
  100. R_INTREGISTER, { = 1 }
  101. R_FPUREGISTER, { = 2 }
  102. { used by Intel only }
  103. R_MMXREGISTER, { = 3 }
  104. R_MMREGISTER, { = 4 }
  105. R_SPECIALREGISTER, { = 5 }
  106. R_ADDRESSREGISTER { = 6 }
  107. );
  108. { Sub registers }
  109. TSubRegister = (
  110. R_SUBNONE, { = 0; no sub register possible }
  111. R_SUBL, { = 1; 8 bits, Like AL }
  112. R_SUBH, { = 2; 8 bits, Like AH }
  113. R_SUBW, { = 3; 16 bits, Like AX }
  114. R_SUBD, { = 4; 32 bits, Like EAX }
  115. R_SUBQ, { = 5; 64 bits, Like RAX }
  116. { For Sparc floats that use F0:F1 to store doubles }
  117. R_SUBFS, { = 6; Float that allocates 1 FPU register }
  118. R_SUBFD { = 7; Float that allocates 2 FPU registers }
  119. );
  120. TSuperRegister = type word;
  121. {
  122. The new register coding:
  123. SuperRegister (bits 0..15)
  124. Subregister (bits 16..23)
  125. Register type (bits 24..31)
  126. TRegister is defined as an enum to make it incompatible
  127. with TSuperRegister to avoid mixing them
  128. }
  129. TRegister = (
  130. TRegisterLowEnum := Low(longint),
  131. TRegisterHighEnum := High(longint)
  132. );
  133. TRegisterRec=packed record
  134. {$ifdef FPC_BIG_ENDIAN}
  135. regtype : Tregistertype;
  136. subreg : Tsubregister;
  137. supreg : Tsuperregister;
  138. {$else FPC_BIG_ENDIAN}
  139. supreg : Tsuperregister;
  140. subreg : Tsubregister;
  141. regtype : Tregistertype;
  142. {$endif FPC_BIG_ENDIAN}
  143. end;
  144. { A type to store register locations for 64 Bit values. }
  145. {$ifdef cpu64bit}
  146. tregister64 = tregister;
  147. {$else cpu64bit}
  148. tregister64 = packed record
  149. reglo,reghi : tregister;
  150. end;
  151. {$endif cpu64bit}
  152. Tregistermmxset = packed record
  153. reg0,reg1,reg2,reg3:Tregister
  154. end;
  155. { Set type definition for registers }
  156. tcpuregisterset = set of byte;
  157. tsuperregisterset = array[byte] of set of byte;
  158. { Temp types }
  159. ttemptype = (tt_none,
  160. tt_free,tt_normal,tt_persistent,
  161. tt_noreuse,tt_freenoreuse);
  162. pmmshuffle = ^tmmshuffle;
  163. { this record describes shuffle operations for mm operations; if a pointer a shuffle record
  164. passed to an mm operation is nil, it means that the whole location is moved }
  165. tmmshuffle = record
  166. { describes how many shuffles are actually described, if len=0 then
  167. moving the scalar with index 0 to the scalar with index 0 is meant }
  168. len : byte;
  169. { lower nibble of each entry of this array describes index of the source data index while
  170. the upper nibble describes the destination index }
  171. shuffles : array[1..1] of byte;
  172. end;
  173. Tsuperregisterarray=array[0..$ffff] of Tsuperregister;
  174. Psuperregisterarray=^Tsuperregisterarray;
  175. Tsuperregisterworklist=object
  176. buflength,
  177. buflengthinc,
  178. length:word;
  179. buf:Psuperregisterarray;
  180. constructor init;
  181. constructor copyfrom(const x:Tsuperregisterworklist);
  182. destructor done;
  183. procedure clear;
  184. procedure add(s:tsuperregister);
  185. function get:tsuperregister;
  186. procedure deleteidx(i:word);
  187. function delete(s:tsuperregister):boolean;
  188. end;
  189. psuperregisterworklist=^tsuperregisterworklist;
  190. const
  191. { alias for easier understanding }
  192. R_SSEREGISTER = R_MMREGISTER;
  193. { Invalid register number }
  194. RS_INVALID = high(tsuperregister);
  195. { Maximum number of cpu registers per register type,
  196. this must fit in tcpuregisterset }
  197. maxcpuregister = 32;
  198. tcgsize2size : Array[tcgsize] of integer =
  199. { integer values }
  200. (0,1,2,4,8,16,1,2,4,8,16,
  201. { floating point values }
  202. 4,8,EXTENDED_SIZE,8,16,
  203. { multimedia values }
  204. 1,2,4,8,16,1,2,4,8,16);
  205. tfloat2tcgsize: array[tfloattype] of tcgsize =
  206. (OS_F32,OS_F64,OS_F80,OS_C64,OS_C64,OS_F128);
  207. tcgsize2tfloat: array[OS_F32..OS_C64] of tfloattype =
  208. (s32real,s64real,s80real,s64comp);
  209. { Table to convert tcgsize variables to the correspondending
  210. unsigned types }
  211. tcgsize2unsigned : array[tcgsize] of tcgsize = (OS_NO,
  212. OS_8,OS_16,OS_32,OS_64,OS_128,OS_8,OS_16,OS_32,OS_64,OS_128,
  213. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  214. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,OS_M8,OS_M16,OS_M32,
  215. OS_M64,OS_M128);
  216. tcgloc2str : array[TCGLoc] of string[11] = (
  217. 'LOC_INVALID',
  218. 'LOC_VOID',
  219. 'LOC_CONST',
  220. 'LOC_JUMP',
  221. 'LOC_FLAGS',
  222. 'LOC_CREF',
  223. 'LOC_REF',
  224. 'LOC_REG',
  225. 'LOC_CREG',
  226. 'LOC_FPUREG',
  227. 'LOC_CFPUREG',
  228. 'LOC_MMXREG',
  229. 'LOC_CMMXREG',
  230. 'LOC_MMREG',
  231. 'LOC_CMMREG');
  232. var
  233. mms_movescalar : pmmshuffle;
  234. procedure supregset_reset(var regs:tsuperregisterset;setall:boolean);{$ifdef USEINLINE}inline;{$endif}
  235. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  236. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  237. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  238. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  239. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  240. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  241. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  242. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  243. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  244. function generic_regname(r:tregister):string;
  245. {# From a constant numeric value, return the abstract code generator
  246. size.
  247. }
  248. function int_cgsize(const a: aint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  249. { return the inverse condition of opcmp }
  250. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  251. { return whether op is commutative }
  252. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  253. { returns true, if shuffle describes a real shuffle operation and not only a move }
  254. function realshuffle(shuffle : pmmshuffle) : boolean;
  255. { returns true, if the shuffle describes only a move of the scalar at index 0 }
  256. function shufflescalar(shuffle : pmmshuffle) : boolean;
  257. { removes shuffling from shuffle, this means that the destenation index of each shuffle is copied to
  258. the source }
  259. procedure removeshuffles(var shuffle : tmmshuffle);
  260. implementation
  261. uses
  262. verbose;
  263. {******************************************************************************
  264. tsuperregisterworklist
  265. ******************************************************************************}
  266. constructor tsuperregisterworklist.init;
  267. begin
  268. length:=0;
  269. buflength:=0;
  270. buflengthinc:=16;
  271. buf:=nil;
  272. end;
  273. constructor Tsuperregisterworklist.copyfrom(const x:Tsuperregisterworklist);
  274. begin
  275. self:=x;
  276. if x.buf<>nil then
  277. begin
  278. getmem(buf,buflength*sizeof(Tsuperregister));
  279. move(x.buf^,buf^,length*sizeof(Tsuperregister));
  280. end;
  281. end;
  282. destructor tsuperregisterworklist.done;
  283. begin
  284. if assigned(buf) then
  285. freemem(buf);
  286. end;
  287. procedure tsuperregisterworklist.add(s:tsuperregister);
  288. begin
  289. inc(length);
  290. { Need to increase buffer length? }
  291. if length>=buflength then
  292. begin
  293. inc(buflength,buflengthinc);
  294. buflengthinc:=buflengthinc*2;
  295. if buflengthinc>256 then
  296. buflengthinc:=256;
  297. reallocmem(buf,buflength*sizeof(Tsuperregister));
  298. end;
  299. buf^[length-1]:=s;
  300. end;
  301. procedure tsuperregisterworklist.clear;
  302. begin
  303. length:=0;
  304. end;
  305. procedure tsuperregisterworklist.deleteidx(i:word);
  306. begin
  307. if length=0 then
  308. internalerror(200310144);
  309. buf^[i]:=buf^[length-1];
  310. dec(length);
  311. end;
  312. function tsuperregisterworklist.get:tsuperregister;
  313. begin
  314. if length=0 then
  315. internalerror(200310142);
  316. get:=buf^[0];
  317. buf^[0]:=buf^[length-1];
  318. dec(length);
  319. end;
  320. function tsuperregisterworklist.delete(s:tsuperregister):boolean;
  321. var i:word;
  322. begin
  323. delete:=false;
  324. for i:=1 to length do
  325. if buf^[i-1]=s then
  326. begin
  327. deleteidx(i-1);
  328. delete:=true;
  329. break;
  330. end;
  331. end;
  332. procedure supregset_reset(var regs:tsuperregisterset;setall:boolean);{$ifdef USEINLINE}inline;{$endif}
  333. var
  334. b : byte;
  335. begin
  336. if setall then
  337. b:=$ff
  338. else
  339. b:=0;
  340. fillchar(regs,sizeof(regs),b);
  341. end;
  342. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  343. begin
  344. include(regs[s shr 8],(s and $ff));
  345. end;
  346. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  347. begin
  348. exclude(regs[s shr 8],(s and $ff));
  349. end;
  350. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  351. begin
  352. result:=(s and $ff) in regs[s shr 8];
  353. end;
  354. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  355. begin
  356. tregisterrec(result).regtype:=rt;
  357. tregisterrec(result).supreg:=sr;
  358. tregisterrec(result).subreg:=sb;
  359. end;
  360. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  361. begin
  362. result:=tregisterrec(r).subreg;
  363. end;
  364. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  365. begin
  366. result:=tregisterrec(r).supreg;
  367. end;
  368. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  369. begin
  370. result:=tregisterrec(r).regtype;
  371. end;
  372. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  373. begin
  374. tregisterrec(r).subreg:=sr;
  375. end;
  376. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  377. begin
  378. tregisterrec(r).supreg:=sr;
  379. end;
  380. function generic_regname(r:tregister):string;
  381. var
  382. nr : string[12];
  383. begin
  384. str(getsupreg(r),nr);
  385. case getregtype(r) of
  386. R_INTREGISTER:
  387. result:='ireg'+nr;
  388. R_FPUREGISTER:
  389. result:='freg'+nr;
  390. R_MMREGISTER:
  391. result:='mreg'+nr;
  392. R_MMXREGISTER:
  393. result:='xreg'+nr;
  394. else
  395. begin
  396. result:='INVALID';
  397. exit;
  398. end;
  399. end;
  400. case getsubreg(r) of
  401. R_SUBNONE:
  402. ;
  403. R_SUBL:
  404. result:=result+'l';
  405. R_SUBH:
  406. result:=result+'h';
  407. R_SUBW:
  408. result:=result+'w';
  409. R_SUBD:
  410. result:=result+'d';
  411. R_SUBQ:
  412. result:=result+'q';
  413. R_SUBFS:
  414. result:=result+'fs';
  415. R_SUBFD:
  416. result:=result+'fd';
  417. else
  418. internalerror(200308252);
  419. end;
  420. end;
  421. function int_cgsize(const a: aint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  422. const
  423. size2cgsize : array[0..8] of tcgsize = (
  424. OS_NO,OS_8,OS_16,OS_32,OS_32,OS_64,OS_64,OS_64,OS_64
  425. );
  426. begin
  427. if a>8 then
  428. result:=OS_NO
  429. else
  430. result:=size2cgsize[a];
  431. end;
  432. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  433. const
  434. list: array[TOpCmp] of TOpCmp =
  435. (OC_NONE,OC_NE,OC_LTE,OC_GTE,OC_LT,OC_GT,OC_EQ,OC_A,OC_AE,
  436. OC_B,OC_BE);
  437. begin
  438. inverse_opcmp := list[opcmp];
  439. end;
  440. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  441. const
  442. list: array[topcg] of boolean =
  443. (true,true,true,false,false,true,true,false,false,
  444. true,false,false,false,false,true);
  445. begin
  446. commutativeop := list[op];
  447. end;
  448. function realshuffle(shuffle : pmmshuffle) : boolean;
  449. var
  450. i : longint;
  451. begin
  452. realshuffle:=true;
  453. if (shuffle=nil) or (shuffle^.len=0) then
  454. realshuffle:=false
  455. else
  456. begin
  457. for i:=1 to shuffle^.len do
  458. begin
  459. if (shuffle^.shuffles[i] and $f)<>((shuffle^.shuffles[i] and $f0) shr 8) then
  460. exit;
  461. end;
  462. realshuffle:=false;
  463. end;
  464. end;
  465. function shufflescalar(shuffle : pmmshuffle) : boolean;
  466. begin
  467. result:=shuffle^.len=0;
  468. end;
  469. procedure removeshuffles(var shuffle : tmmshuffle);
  470. var
  471. i : longint;
  472. begin
  473. if shuffle.len=0 then
  474. exit;
  475. for i:=1 to shuffle.len do
  476. shuffle.shuffles[i]:=(shuffle.shuffles[i] and $f0) or ((shuffle.shuffles[i] and $f0) shr 8);
  477. end;
  478. initialization
  479. new(mms_movescalar);
  480. mms_movescalar^.len:=0;
  481. finalization
  482. dispose(mms_movescalar);
  483. end.
  484. {
  485. $Log$
  486. Revision 1.90 2004-06-20 08:55:28 florian
  487. * logs truncated
  488. Revision 1.89 2004/06/16 20:07:07 florian
  489. * dwarf branch merged
  490. Revision 1.88.2.2 2004/05/01 16:02:09 peter
  491. * POINTER_SIZE replaced with sizeof(aint)
  492. * aint,aword,tconst*int moved to globtype
  493. Revision 1.88.2.1 2004/04/27 18:18:25 peter
  494. * aword -> aint
  495. Revision 1.88 2004/02/27 10:21:05 florian
  496. * top_symbol killed
  497. + refaddr to treference added
  498. + refsymbol to treference added
  499. * top_local stuff moved to an extra record to save memory
  500. + aint introduced
  501. * tppufile.get/putint64/aint implemented
  502. Revision 1.87 2004/02/09 22:14:17 peter
  503. * more x86_64 parameter fixes
  504. * tparalocation.lochigh is now used to indicate if registerhigh
  505. is used and what the type is
  506. }