cgbase.pas 20 KB

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