cgbase.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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;
  235. maxreg:Tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  236. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  237. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  238. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  239. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  240. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  241. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  242. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  243. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  244. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  245. function generic_regname(r:tregister):string;
  246. {# From a constant numeric value, return the abstract code generator
  247. size.
  248. }
  249. function int_cgsize(const a: aint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  250. { return the inverse condition of opcmp }
  251. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  252. { return whether op is commutative }
  253. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  254. { returns true, if shuffle describes a real shuffle operation and not only a move }
  255. function realshuffle(shuffle : pmmshuffle) : boolean;
  256. { returns true, if the shuffle describes only a move of the scalar at index 0 }
  257. function shufflescalar(shuffle : pmmshuffle) : boolean;
  258. { removes shuffling from shuffle, this means that the destenation index of each shuffle is copied to
  259. the source }
  260. procedure removeshuffles(var shuffle : tmmshuffle);
  261. implementation
  262. uses
  263. verbose;
  264. {******************************************************************************
  265. tsuperregisterworklist
  266. ******************************************************************************}
  267. constructor tsuperregisterworklist.init;
  268. begin
  269. length:=0;
  270. buflength:=0;
  271. buflengthinc:=16;
  272. buf:=nil;
  273. end;
  274. constructor Tsuperregisterworklist.copyfrom(const x:Tsuperregisterworklist);
  275. begin
  276. self:=x;
  277. if x.buf<>nil then
  278. begin
  279. getmem(buf,buflength*sizeof(Tsuperregister));
  280. move(x.buf^,buf^,length*sizeof(Tsuperregister));
  281. end;
  282. end;
  283. destructor tsuperregisterworklist.done;
  284. begin
  285. if assigned(buf) then
  286. freemem(buf);
  287. end;
  288. procedure tsuperregisterworklist.add(s:tsuperregister);
  289. begin
  290. inc(length);
  291. { Need to increase buffer length? }
  292. if length>=buflength then
  293. begin
  294. inc(buflength,buflengthinc);
  295. buflengthinc:=buflengthinc*2;
  296. if buflengthinc>256 then
  297. buflengthinc:=256;
  298. reallocmem(buf,buflength*sizeof(Tsuperregister));
  299. end;
  300. buf^[length-1]:=s;
  301. end;
  302. procedure tsuperregisterworklist.clear;
  303. begin
  304. length:=0;
  305. end;
  306. procedure tsuperregisterworklist.deleteidx(i:word);
  307. begin
  308. if length=0 then
  309. internalerror(200310144);
  310. buf^[i]:=buf^[length-1];
  311. dec(length);
  312. end;
  313. function tsuperregisterworklist.get:tsuperregister;
  314. begin
  315. if length=0 then
  316. internalerror(200310142);
  317. get:=buf^[0];
  318. buf^[0]:=buf^[length-1];
  319. dec(length);
  320. end;
  321. function tsuperregisterworklist.delete(s:tsuperregister):boolean;
  322. var i:word;
  323. begin
  324. delete:=false;
  325. for i:=1 to length do
  326. if buf^[i-1]=s then
  327. begin
  328. deleteidx(i-1);
  329. delete:=true;
  330. break;
  331. end;
  332. end;
  333. procedure supregset_reset(var regs:tsuperregisterset;setall:boolean;
  334. maxreg:Tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  335. begin
  336. fillchar(regs,(maxreg or 7) shr 3,-byte(setall));
  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. nr : string[12];
  379. begin
  380. str(getsupreg(r),nr);
  381. case getregtype(r) of
  382. R_INTREGISTER:
  383. result:='ireg'+nr;
  384. R_FPUREGISTER:
  385. result:='freg'+nr;
  386. R_MMREGISTER:
  387. result:='mreg'+nr;
  388. R_MMXREGISTER:
  389. result:='xreg'+nr;
  390. else
  391. begin
  392. result:='INVALID';
  393. exit;
  394. end;
  395. end;
  396. case getsubreg(r) of
  397. R_SUBNONE:
  398. ;
  399. R_SUBL:
  400. result:=result+'l';
  401. R_SUBH:
  402. result:=result+'h';
  403. R_SUBW:
  404. result:=result+'w';
  405. R_SUBD:
  406. result:=result+'d';
  407. R_SUBQ:
  408. result:=result+'q';
  409. R_SUBFS:
  410. result:=result+'fs';
  411. R_SUBFD:
  412. result:=result+'fd';
  413. else
  414. internalerror(200308252);
  415. end;
  416. end;
  417. function int_cgsize(const a: aint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  418. const
  419. size2cgsize : array[0..8] of tcgsize = (
  420. OS_NO,OS_8,OS_16,OS_32,OS_32,OS_64,OS_64,OS_64,OS_64
  421. );
  422. begin
  423. if a>8 then
  424. result:=OS_NO
  425. else
  426. result:=size2cgsize[a];
  427. end;
  428. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  429. const
  430. list: array[TOpCmp] of TOpCmp =
  431. (OC_NONE,OC_NE,OC_LTE,OC_GTE,OC_LT,OC_GT,OC_EQ,OC_A,OC_AE,
  432. OC_B,OC_BE);
  433. begin
  434. inverse_opcmp := list[opcmp];
  435. end;
  436. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  437. const
  438. list: array[topcg] of boolean =
  439. (true,true,true,false,false,true,true,false,false,
  440. true,false,false,false,false,true);
  441. begin
  442. commutativeop := list[op];
  443. end;
  444. function realshuffle(shuffle : pmmshuffle) : boolean;
  445. var
  446. i : longint;
  447. begin
  448. realshuffle:=true;
  449. if (shuffle=nil) or (shuffle^.len=0) then
  450. realshuffle:=false
  451. else
  452. begin
  453. for i:=1 to shuffle^.len do
  454. begin
  455. if (shuffle^.shuffles[i] and $f)<>((shuffle^.shuffles[i] and $f0) shr 8) then
  456. exit;
  457. end;
  458. realshuffle:=false;
  459. end;
  460. end;
  461. function shufflescalar(shuffle : pmmshuffle) : boolean;
  462. begin
  463. result:=shuffle^.len=0;
  464. end;
  465. procedure removeshuffles(var shuffle : tmmshuffle);
  466. var
  467. i : longint;
  468. begin
  469. if shuffle.len=0 then
  470. exit;
  471. for i:=1 to shuffle.len do
  472. shuffle.shuffles[i]:=(shuffle.shuffles[i] and $f0) or ((shuffle.shuffles[i] and $f0) shr 8);
  473. end;
  474. initialization
  475. new(mms_movescalar);
  476. mms_movescalar^.len:=0;
  477. finalization
  478. dispose(mms_movescalar);
  479. end.
  480. {
  481. $Log$
  482. Revision 1.91 2004-07-07 17:35:26 daniel
  483. * supregset_reset clears 8kb of memory. However, it is being called in
  484. inner loops, see for example colour_registers. According to profile data
  485. this causes fillchar to be the most time consuming procedure.
  486. Some modifications done to make it clear less than 8kb of memory each
  487. call. Divides time spent in fillchar by two, but it still is the no.1
  488. procedure.
  489. Revision 1.90 2004/06/20 08:55:28 florian
  490. * logs truncated
  491. Revision 1.89 2004/06/16 20:07:07 florian
  492. * dwarf branch merged
  493. Revision 1.88.2.2 2004/05/01 16:02:09 peter
  494. * POINTER_SIZE replaced with sizeof(aint)
  495. * aint,aword,tconst*int moved to globtype
  496. Revision 1.88.2.1 2004/04/27 18:18:25 peter
  497. * aword -> aint
  498. Revision 1.88 2004/02/27 10:21:05 florian
  499. * top_symbol killed
  500. + refaddr to treference added
  501. + refsymbol to treference added
  502. * top_local stuff moved to an extra record to save memory
  503. + aint introduced
  504. * tppufile.get/putint64/aint implemented
  505. Revision 1.87 2004/02/09 22:14:17 peter
  506. * more x86_64 parameter fixes
  507. * tparalocation.lochigh is now used to indicate if registerhigh
  508. is used and what the type is
  509. }