cgbase.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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
  323. i:longint;
  324. begin
  325. delete:=false;
  326. {$ifndef FPC}
  327. for i:=1 to length do
  328. if buf^[i-1]=s then
  329. begin
  330. deleteidx(i-1);
  331. delete:=true;
  332. break;
  333. end;
  334. {$else FPC}
  335. i := indexword(buf^,length,s);
  336. if i <> -1 then
  337. begin
  338. deleteidx(i);
  339. delete := true;
  340. end;
  341. {$endif FPC}
  342. end;
  343. procedure supregset_reset(var regs:tsuperregisterset;setall:boolean;
  344. maxreg:Tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  345. begin
  346. fillchar(regs,(maxreg or 7) shr 3,-byte(setall));
  347. end;
  348. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  349. begin
  350. include(regs[s shr 8],(s and $ff));
  351. end;
  352. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  353. begin
  354. exclude(regs[s shr 8],(s and $ff));
  355. end;
  356. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  357. begin
  358. result:=(s and $ff) in regs[s shr 8];
  359. end;
  360. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  361. begin
  362. tregisterrec(result).regtype:=rt;
  363. tregisterrec(result).supreg:=sr;
  364. tregisterrec(result).subreg:=sb;
  365. end;
  366. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  367. begin
  368. result:=tregisterrec(r).subreg;
  369. end;
  370. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  371. begin
  372. result:=tregisterrec(r).supreg;
  373. end;
  374. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  375. begin
  376. result:=tregisterrec(r).regtype;
  377. end;
  378. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  379. begin
  380. tregisterrec(r).subreg:=sr;
  381. end;
  382. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  383. begin
  384. tregisterrec(r).supreg:=sr;
  385. end;
  386. function generic_regname(r:tregister):string;
  387. var
  388. nr : string[12];
  389. begin
  390. str(getsupreg(r),nr);
  391. case getregtype(r) of
  392. R_INTREGISTER:
  393. result:='ireg'+nr;
  394. R_FPUREGISTER:
  395. result:='freg'+nr;
  396. R_MMREGISTER:
  397. result:='mreg'+nr;
  398. R_MMXREGISTER:
  399. result:='xreg'+nr;
  400. else
  401. begin
  402. result:='INVALID';
  403. exit;
  404. end;
  405. end;
  406. case getsubreg(r) of
  407. R_SUBNONE:
  408. ;
  409. R_SUBL:
  410. result:=result+'l';
  411. R_SUBH:
  412. result:=result+'h';
  413. R_SUBW:
  414. result:=result+'w';
  415. R_SUBD:
  416. result:=result+'d';
  417. R_SUBQ:
  418. result:=result+'q';
  419. R_SUBFS:
  420. result:=result+'fs';
  421. R_SUBFD:
  422. result:=result+'fd';
  423. else
  424. internalerror(200308252);
  425. end;
  426. end;
  427. function int_cgsize(const a: aint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  428. const
  429. size2cgsize : array[0..8] of tcgsize = (
  430. OS_NO,OS_8,OS_16,OS_32,OS_32,OS_64,OS_64,OS_64,OS_64
  431. );
  432. begin
  433. if a>8 then
  434. result:=OS_NO
  435. else
  436. result:=size2cgsize[a];
  437. end;
  438. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  439. const
  440. list: array[TOpCmp] of TOpCmp =
  441. (OC_NONE,OC_NE,OC_LTE,OC_GTE,OC_LT,OC_GT,OC_EQ,OC_A,OC_AE,
  442. OC_B,OC_BE);
  443. begin
  444. inverse_opcmp := list[opcmp];
  445. end;
  446. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  447. const
  448. list: array[topcg] of boolean =
  449. (true,true,true,false,false,true,true,false,false,
  450. true,false,false,false,false,true);
  451. begin
  452. commutativeop := list[op];
  453. end;
  454. function realshuffle(shuffle : pmmshuffle) : boolean;
  455. var
  456. i : longint;
  457. begin
  458. realshuffle:=true;
  459. if (shuffle=nil) or (shuffle^.len=0) then
  460. realshuffle:=false
  461. else
  462. begin
  463. for i:=1 to shuffle^.len do
  464. begin
  465. if (shuffle^.shuffles[i] and $f)<>((shuffle^.shuffles[i] and $f0) shr 8) then
  466. exit;
  467. end;
  468. realshuffle:=false;
  469. end;
  470. end;
  471. function shufflescalar(shuffle : pmmshuffle) : boolean;
  472. begin
  473. result:=shuffle^.len=0;
  474. end;
  475. procedure removeshuffles(var shuffle : tmmshuffle);
  476. var
  477. i : longint;
  478. begin
  479. if shuffle.len=0 then
  480. exit;
  481. for i:=1 to shuffle.len do
  482. shuffle.shuffles[i]:=(shuffle.shuffles[i] and $f0) or ((shuffle.shuffles[i] and $f0) shr 8);
  483. end;
  484. initialization
  485. new(mms_movescalar);
  486. mms_movescalar^.len:=0;
  487. finalization
  488. dispose(mms_movescalar);
  489. end.
  490. {
  491. $Log$
  492. Revision 1.92 2004-07-18 15:14:59 jonas
  493. * use indexword() in tsuperregisterworklist.delete, greatly speeds up
  494. compilation of tw2242
  495. Revision 1.91 2004/07/07 17:35:26 daniel
  496. * supregset_reset clears 8kb of memory. However, it is being called in
  497. inner loops, see for example colour_registers. According to profile data
  498. this causes fillchar to be the most time consuming procedure.
  499. Some modifications done to make it clear less than 8kb of memory each
  500. call. Divides time spent in fillchar by two, but it still is the no.1
  501. procedure.
  502. Revision 1.90 2004/06/20 08:55:28 florian
  503. * logs truncated
  504. Revision 1.89 2004/06/16 20:07:07 florian
  505. * dwarf branch merged
  506. Revision 1.88.2.2 2004/05/01 16:02:09 peter
  507. * POINTER_SIZE replaced with sizeof(aint)
  508. * aint,aword,tconst*int moved to globtype
  509. Revision 1.88.2.1 2004/04/27 18:18:25 peter
  510. * aword -> aint
  511. Revision 1.88 2004/02/27 10:21:05 florian
  512. * top_symbol killed
  513. + refaddr to treference added
  514. + refsymbol to treference added
  515. * top_local stuff moved to an extra record to save memory
  516. + aint introduced
  517. * tppufile.get/putint64/aint implemented
  518. Revision 1.87 2004/02/09 22:14:17 peter
  519. * more x86_64 parameter fixes
  520. * tparalocation.lochigh is now used to indicate if registerhigh
  521. is used and what the type is
  522. }