cgbase.pas 19 KB

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