cgbase.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. cpuinfo,
  24. symconst;
  25. type
  26. { Location types where value can be stored }
  27. TCGLoc=(
  28. LOC_INVALID, { added for tracking problems}
  29. LOC_VOID, { no value is available }
  30. LOC_CONSTANT, { constant value }
  31. LOC_JUMP, { boolean results only, jump to false or true label }
  32. LOC_FLAGS, { boolean results only, flags are set }
  33. LOC_CREFERENCE, { in memory constant value reference (cannot change) }
  34. LOC_REFERENCE, { in memory value }
  35. LOC_REGISTER, { in a processor register }
  36. LOC_CREGISTER, { Constant register which shouldn't be modified }
  37. LOC_FPUREGISTER, { FPU stack }
  38. LOC_CFPUREGISTER, { if it is a FPU register variable on the fpu stack }
  39. LOC_MMXREGISTER, { MMX register }
  40. { MMX register variable }
  41. LOC_CMMXREGISTER,
  42. { multimedia register }
  43. LOC_MMREGISTER,
  44. { Constant multimedia reg which shouldn't be modified }
  45. LOC_CMMREGISTER
  46. );
  47. {# Generic opcodes, which must be supported by all processors
  48. }
  49. topcg =
  50. (
  51. OP_NONE,
  52. OP_ADD, { simple addition }
  53. OP_AND, { simple logical and }
  54. OP_DIV, { simple unsigned division }
  55. OP_IDIV, { simple signed division }
  56. OP_IMUL, { simple signed multiply }
  57. OP_MUL, { simple unsigned multiply }
  58. OP_NEG, { simple negate }
  59. OP_NOT, { simple logical not }
  60. OP_OR, { simple logical or }
  61. OP_SAR, { arithmetic shift-right }
  62. OP_SHL, { logical shift left }
  63. OP_SHR, { logical shift right }
  64. OP_SUB, { simple subtraction }
  65. OP_XOR { simple exclusive or }
  66. );
  67. {# Generic flag values - used for jump locations }
  68. TOpCmp =
  69. (
  70. OC_NONE,
  71. OC_EQ, { equality comparison }
  72. OC_GT, { greater than (signed) }
  73. OC_LT, { less than (signed) }
  74. OC_GTE, { greater or equal than (signed) }
  75. OC_LTE, { less or equal than (signed) }
  76. OC_NE, { not equal }
  77. OC_BE, { less or equal than (unsigned) }
  78. OC_B, { less than (unsigned) }
  79. OC_AE, { greater or equal than (unsigned) }
  80. OC_A { greater than (unsigned) }
  81. );
  82. { OS_NO is also used memory references with large data that can
  83. not be loaded in a register directly }
  84. TCgSize = (OS_NO,
  85. { integer registers }
  86. OS_8,OS_16,OS_32,OS_64,OS_S8,OS_S16,OS_S32,OS_S64,
  87. { single,double,extended,comp,float128 }
  88. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  89. { multi-media sizes: split in byte, word, dword, ... }
  90. { entities, then the signed counterparts }
  91. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,OS_MS8,OS_MS16,OS_MS32,
  92. OS_MS64,OS_MS128);
  93. { Register types }
  94. TRegisterType = (
  95. R_INVALIDREGISTER, { = 0 }
  96. R_INTREGISTER, { = 1 }
  97. R_FPUREGISTER, { = 2 }
  98. { used by Intel only }
  99. R_MMXREGISTER, { = 3 }
  100. R_MMREGISTER, { = 4 }
  101. R_SPECIALREGISTER, { = 5 }
  102. R_ADDRESSREGISTER { = 6 }
  103. );
  104. { Sub registers }
  105. TSubRegister = (
  106. R_SUBNONE, { = 0; no sub register possible }
  107. R_SUBL, { = 1; 8 bits, Like AL }
  108. R_SUBH, { = 2; 8 bits, Like AH }
  109. R_SUBW, { = 3; 16 bits, Like AX }
  110. R_SUBD, { = 4; 32 bits, Like EAX }
  111. R_SUBQ, { = 5; 64 bits, Like RAX }
  112. R_SUBFD { = 6; Float that allocates 2 FPU registers }
  113. );
  114. TSuperRegister = type word;
  115. {
  116. The new register coding:
  117. SuperRegister (bits 0..15)
  118. Subregister (bits 16..23)
  119. Register type (bits 24..31)
  120. TRegister is defined as an enum to make it incompatible
  121. with TSuperRegister to avoid mixing them
  122. }
  123. TRegister = (
  124. TRegisterLowEnum := Low(longint),
  125. TRegisterHighEnum := High(longint)
  126. );
  127. TRegisterRec=packed record
  128. {$ifdef FPC_BIG_ENDIAN}
  129. regtype : Tregistertype;
  130. subreg : Tsubregister;
  131. supreg : Tsuperregister;
  132. {$else FPC_BIG_ENDIAN}
  133. supreg : Tsuperregister;
  134. subreg : Tsubregister;
  135. regtype : Tregistertype;
  136. {$endif FPC_BIG_ENDIAN}
  137. end;
  138. { A type to store register locations for 64 Bit values. }
  139. {$ifdef cpu64bit}
  140. tregister64 = tregister;
  141. {$else cpu64bit}
  142. tregister64 = packed record
  143. reglo,reghi : tregister;
  144. end;
  145. {$endif cpu64bit}
  146. Tregistermmxset = packed record
  147. reg0,reg1,reg2,reg3:Tregister
  148. end;
  149. { Set type definition for registers }
  150. tcpuregisterset = set of byte;
  151. tsuperregisterset = array[byte] of set of byte;
  152. { Temp types }
  153. ttemptype = (tt_none,
  154. tt_free,tt_normal,tt_persistent,
  155. tt_noreuse,tt_freenoreuse);
  156. pmmshuffle = ^tmmshuffle;
  157. { this record describes shuffle operations for mm operations; if a pointer a shuffle record
  158. passed to an mm operation is nil, it means that the whole location is moved }
  159. tmmshuffle = record
  160. { describes how many shuffles are actually described, if len=0 then
  161. moving the scalar with index 0 to the scalar with index 0 is meant }
  162. len : byte;
  163. { lower nibble of each entry of this array describes index of the source data index while
  164. the upper nibble describes the destination index }
  165. shuffles : array[1..1] of byte;
  166. end;
  167. Tsuperregisterworklist=object
  168. buflength,
  169. buflengthinc,
  170. length:word;
  171. buf : ^tsuperregister;
  172. constructor init;
  173. constructor copyfrom(const x:Tsuperregisterworklist);
  174. destructor done;
  175. procedure clear;
  176. procedure add(s:tsuperregister);
  177. function get:tsuperregister;
  178. procedure deleteidx(i:word);
  179. function delete(s:tsuperregister):boolean;
  180. end;
  181. psuperregisterworklist=^tsuperregisterworklist;
  182. const
  183. { alias for easier understanding }
  184. R_SSEREGISTER = R_MMREGISTER;
  185. { Invalid register number }
  186. RS_INVALID = high(tsuperregister);
  187. { Maximum number of cpu registers per register type,
  188. this must fit in tcpuregisterset }
  189. maxcpuregister = 32;
  190. tcgsize2size : Array[tcgsize] of integer =
  191. { integer values }
  192. (0,1,2,4,8,1,2,4,8,
  193. { floating point values }
  194. 4,8,EXTENDED_SIZE,8,16,
  195. { multimedia values }
  196. 1,2,4,8,16,1,2,4,8,16);
  197. tfloat2tcgsize: array[tfloattype] of tcgsize =
  198. (OS_F32,OS_F64,OS_F80,OS_C64,OS_C64,OS_F128);
  199. tcgsize2tfloat: array[OS_F32..OS_C64] of tfloattype =
  200. (s32real,s64real,s80real,s64comp);
  201. { Table to convert tcgsize variables to the correspondending
  202. unsigned types }
  203. tcgsize2unsigned : array[tcgsize] of tcgsize = (OS_NO,
  204. OS_8,OS_16,OS_32,OS_64,OS_8,OS_16,OS_32,OS_64,
  205. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  206. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,OS_M8,OS_M16,OS_M32,
  207. OS_M64,OS_M128);
  208. tcgloc2str : array[TCGLoc] of string[11] = (
  209. 'LOC_INVALID',
  210. 'LOC_VOID',
  211. 'LOC_CONST',
  212. 'LOC_JUMP',
  213. 'LOC_FLAGS',
  214. 'LOC_CREF',
  215. 'LOC_REF',
  216. 'LOC_REG',
  217. 'LOC_CREG',
  218. 'LOC_FPUREG',
  219. 'LOC_CFPUREG',
  220. 'LOC_MMXREG',
  221. 'LOC_CMMXREG',
  222. 'LOC_MMREG',
  223. 'LOC_CMMREG');
  224. var
  225. mms_movescalar : pmmshuffle;
  226. procedure supregset_reset(var regs:tsuperregisterset;setall:boolean);{$ifdef USEINLINE}inline;{$endif}
  227. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  228. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  229. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  230. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  231. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  232. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  233. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  234. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  235. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  236. function generic_regname(r:tregister):string;
  237. {# From a constant numeric value, return the abstract code generator
  238. size.
  239. }
  240. function int_cgsize(const a: aword): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  241. { return the inverse condition of opcmp }
  242. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  243. { return whether op is commutative }
  244. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  245. { returns true, if shuffle describes a real shuffle operation and not only a move }
  246. function realshuffle(shuffle : pmmshuffle) : boolean;
  247. { returns true, if the shuffle describes only a move of the scalar at index 0 }
  248. function shufflescalar(shuffle : pmmshuffle) : boolean;
  249. { removes shuffling from shuffle, this means that the destenation index of each shuffle is copied to
  250. the source }
  251. procedure removeshuffles(var shuffle : tmmshuffle);
  252. implementation
  253. uses
  254. verbose;
  255. {******************************************************************************
  256. tsuperregisterworklist
  257. ******************************************************************************}
  258. constructor tsuperregisterworklist.init;
  259. begin
  260. length:=0;
  261. buflength:=0;
  262. buflengthinc:=16;
  263. { head:=0;
  264. tail:=0;}
  265. buf:=nil;
  266. end;
  267. constructor Tsuperregisterworklist.copyfrom(const x:Tsuperregisterworklist);
  268. begin
  269. self:=x;
  270. if x.buf<>nil then
  271. begin
  272. getmem(buf,buflength*sizeof(Tsuperregister));
  273. move(x.buf^,buf^,length*sizeof(Tsuperregister));
  274. end;
  275. end;
  276. destructor tsuperregisterworklist.done;
  277. begin
  278. if assigned(buf) then
  279. freemem(buf);
  280. end;
  281. procedure tsuperregisterworklist.add(s:tsuperregister);
  282. begin
  283. inc(length);
  284. { Need to increase buffer length? }
  285. if length>=buflength then
  286. begin
  287. inc(buflength,buflengthinc);
  288. buflengthinc:=buflengthinc*2;
  289. if buflengthinc>256 then
  290. buflengthinc:=256;
  291. reallocmem(buf,buflength*sizeof(Tsuperregister));
  292. end;
  293. buf[length-1]:=s;
  294. end;
  295. procedure tsuperregisterworklist.clear;
  296. begin
  297. length:=0;
  298. end;
  299. procedure tsuperregisterworklist.deleteidx(i:word);
  300. begin
  301. if length=0 then
  302. internalerror(200310144);
  303. buf[i]:=buf[length-1];
  304. dec(length);
  305. end;
  306. function tsuperregisterworklist.get:tsuperregister;
  307. begin
  308. if length=0 then
  309. internalerror(200310142);
  310. get:=buf[0];
  311. buf[0]:=buf[length-1];
  312. dec(length);
  313. end;
  314. function tsuperregisterworklist.delete(s:tsuperregister):boolean;
  315. var i:word;
  316. begin
  317. delete:=false;
  318. for i:=1 to length do
  319. if buf[i-1]=s then
  320. begin
  321. deleteidx(i-1);
  322. delete:=true;
  323. break;
  324. end;
  325. end;
  326. procedure supregset_reset(var regs:tsuperregisterset;setall:boolean);{$ifdef USEINLINE}inline;{$endif}
  327. var
  328. b : byte;
  329. begin
  330. if setall then
  331. b:=$ff
  332. else
  333. b:=0;
  334. fillchar(regs,sizeof(regs),b);
  335. end;
  336. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  337. begin
  338. include(regs[s shr 8],(s and $ff));
  339. end;
  340. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  341. begin
  342. exclude(regs[s shr 8],(s and $ff));
  343. end;
  344. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  345. begin
  346. result:=(s and $ff) in regs[s shr 8];
  347. end;
  348. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  349. begin
  350. tregisterrec(result).regtype:=rt;
  351. tregisterrec(result).supreg:=sr;
  352. tregisterrec(result).subreg:=sb;
  353. end;
  354. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  355. begin
  356. result:=tregisterrec(r).subreg;
  357. end;
  358. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  359. begin
  360. result:=tregisterrec(r).supreg;
  361. end;
  362. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  363. begin
  364. result:=tregisterrec(r).regtype;
  365. end;
  366. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  367. begin
  368. tregisterrec(r).subreg:=sr;
  369. end;
  370. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  371. begin
  372. tregisterrec(r).supreg:=sr;
  373. end;
  374. function generic_regname(r:tregister):string;
  375. var
  376. t,sub : char;
  377. nr : string[12];
  378. begin
  379. case getregtype(r) of
  380. R_INTREGISTER:
  381. t:='i';
  382. R_FPUREGISTER:
  383. t:='f';
  384. R_MMXREGISTER:
  385. t:='x';
  386. R_MMREGISTER:
  387. t:='m';
  388. else
  389. begin
  390. result:='INVALID';
  391. exit;
  392. end;
  393. end;
  394. str(getsupreg(r),nr);
  395. case getsubreg(r) of
  396. R_SUBNONE:
  397. sub:=' ';
  398. R_SUBL:
  399. sub:='l';
  400. R_SUBH:
  401. sub:='h';
  402. R_SUBW:
  403. sub:='w';
  404. R_SUBD:
  405. sub:='d';
  406. R_SUBQ:
  407. sub:='q';
  408. R_SUBFD:
  409. sub:='f';
  410. else
  411. internalerror(200308252);
  412. end;
  413. if sub<>' ' then
  414. result:=t+'reg'+nr+sub
  415. else
  416. result:=t+'reg'+nr;
  417. end;
  418. function int_cgsize(const a: aword): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  419. const
  420. size2cgsize : array[0..8] of tcgsize = (
  421. OS_NO,OS_8,OS_16,OS_32,OS_32,OS_64,OS_64,OS_64,OS_64
  422. );
  423. begin
  424. if a>8 then
  425. result:=OS_NO
  426. else
  427. result:=size2cgsize[a];
  428. end;
  429. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  430. const
  431. list: array[TOpCmp] of TOpCmp =
  432. (OC_NONE,OC_NE,OC_LTE,OC_GTE,OC_LT,OC_GT,OC_EQ,OC_A,OC_AE,
  433. OC_B,OC_BE);
  434. begin
  435. inverse_opcmp := list[opcmp];
  436. end;
  437. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  438. const
  439. list: array[topcg] of boolean =
  440. (true,true,true,false,false,true,true,false,false,
  441. true,false,false,false,false,true);
  442. begin
  443. commutativeop := list[op];
  444. end;
  445. function realshuffle(shuffle : pmmshuffle) : boolean;
  446. var
  447. i : longint;
  448. begin
  449. realshuffle:=true;
  450. if (shuffle=nil) or (shuffle^.len=0) then
  451. realshuffle:=false
  452. else
  453. begin
  454. for i:=1 to shuffle^.len do
  455. begin
  456. if (shuffle^.shuffles[i] and $f)<>((shuffle^.shuffles[i] and $f0) shr 8) then
  457. exit;
  458. end;
  459. realshuffle:=false;
  460. end;
  461. end;
  462. function shufflescalar(shuffle : pmmshuffle) : boolean;
  463. begin
  464. result:=shuffle^.len=0;
  465. end;
  466. procedure removeshuffles(var shuffle : tmmshuffle);
  467. var
  468. i : longint;
  469. begin
  470. if shuffle.len=0 then
  471. exit;
  472. for i:=1 to shuffle.len do
  473. shuffle.shuffles[i]:=(shuffle.shuffles[i] and $f0) or ((shuffle.shuffles[i] and $f0) shr 8);
  474. end;
  475. initialization
  476. new(mms_movescalar);
  477. mms_movescalar^.len:=0;
  478. finalization
  479. dispose(mms_movescalar);
  480. end.
  481. {
  482. $Log$
  483. Revision 1.83 2003-12-25 01:07:09 florian
  484. + $fputype directive support
  485. + single data type operations with sse unit
  486. * fixed more x86-64 stuff
  487. Revision 1.82 2003/12/22 23:10:21 peter
  488. * use low(longint) instead of $8000000
  489. Revision 1.81 2003/12/21 19:42:42 florian
  490. * fixed ppc inlining stuff
  491. * fixed wrong unit writing
  492. + added some sse stuff
  493. Revision 1.80 2003/12/19 22:08:44 daniel
  494. * Some work to restore the MMX capabilities
  495. Revision 1.79 2003/12/15 21:25:48 peter
  496. * reg allocations for imaginary register are now inserted just
  497. before reg allocation
  498. * tregister changed to enum to allow compile time check
  499. * fixed several tregister-tsuperregister errors
  500. Revision 1.78 2003/12/14 20:24:28 daniel
  501. * Register allocator speed optimizations
  502. - Worklist no longer a ringbuffer
  503. - No find operations are left
  504. - Simplify now done in constant time
  505. - unusedregs is now a Tsuperregisterworklist
  506. - Microoptimizations
  507. Revision 1.77 2003/11/04 15:35:13 peter
  508. * fix for referencecounted temps
  509. Revision 1.76 2003/11/03 17:48:04 peter
  510. * int_cgsize returned garbage for a=0
  511. Revision 1.75 2003/10/31 15:51:11 peter
  512. * USEINLINE directive added (not enabled yet)
  513. Revision 1.74 2003/10/30 14:56:40 mazen
  514. + add support for double float register vars
  515. Revision 1.73 2003/10/29 15:07:01 mazen
  516. * 32 registers are available
  517. Revision 1.72 2003/10/24 15:21:31 peter
  518. * renamed R_SUBF64 to R_SUBFD
  519. Revision 1.71 2003/10/17 14:38:32 peter
  520. * 64k registers supported
  521. * fixed some memory leaks
  522. Revision 1.70 2003/10/13 01:10:01 florian
  523. * some ideas for mm support implemented
  524. Revision 1.69 2003/10/11 16:06:42 florian
  525. * fixed some MMX<->SSE
  526. * started to fix ppc, needs an overhaul
  527. + stabs info improve for spilling, not sure if it works correctly/completly
  528. - MMX_SUPPORT removed from Makefile.fpc
  529. Revision 1.68 2003/10/09 21:31:37 daniel
  530. * Register allocator splitted, ans abstract now
  531. Revision 1.67 2003/10/01 20:34:48 peter
  532. * procinfo unit contains tprocinfo
  533. * cginfo renamed to cgbase
  534. * moved cgmessage to verbose
  535. * fixed ppc and sparc compiles
  536. }