cgbase.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. R_SUBFQ { = 8; Float that allocates 4 FPU registers }
  120. );
  121. TSuperRegister = type word;
  122. {
  123. The new register coding:
  124. SuperRegister (bits 0..15)
  125. Subregister (bits 16..23)
  126. Register type (bits 24..31)
  127. TRegister is defined as an enum to make it incompatible
  128. with TSuperRegister to avoid mixing them
  129. }
  130. TRegister = (
  131. TRegisterLowEnum := Low(longint),
  132. TRegisterHighEnum := High(longint)
  133. );
  134. TRegisterRec=packed record
  135. {$ifdef FPC_BIG_ENDIAN}
  136. regtype : Tregistertype;
  137. subreg : Tsubregister;
  138. supreg : Tsuperregister;
  139. {$else FPC_BIG_ENDIAN}
  140. supreg : Tsuperregister;
  141. subreg : Tsubregister;
  142. regtype : Tregistertype;
  143. {$endif FPC_BIG_ENDIAN}
  144. end;
  145. { A type to store register locations for 64 Bit values. }
  146. {$ifdef cpu64bit}
  147. tregister64 = tregister;
  148. {$else cpu64bit}
  149. tregister64 = packed record
  150. reglo,reghi : tregister;
  151. end;
  152. {$endif cpu64bit}
  153. Tregistermmxset = packed record
  154. reg0,reg1,reg2,reg3:Tregister
  155. end;
  156. { Set type definition for registers }
  157. tcpuregisterset = set of byte;
  158. tsuperregisterset = array[byte] of set of byte;
  159. { Temp types }
  160. ttemptype = (tt_none,
  161. tt_free,tt_normal,tt_persistent,
  162. tt_noreuse,tt_freenoreuse);
  163. pmmshuffle = ^tmmshuffle;
  164. { this record describes shuffle operations for mm operations; if a pointer a shuffle record
  165. passed to an mm operation is nil, it means that the whole location is moved }
  166. tmmshuffle = record
  167. { describes how many shuffles are actually described, if len=0 then
  168. moving the scalar with index 0 to the scalar with index 0 is meant }
  169. len : byte;
  170. { lower nibble of each entry of this array describes index of the source data index while
  171. the upper nibble describes the destination index }
  172. shuffles : array[1..1] of byte;
  173. end;
  174. Tsuperregisterarray=array[0..$ffff] of Tsuperregister;
  175. Psuperregisterarray=^Tsuperregisterarray;
  176. Tsuperregisterworklist=object
  177. buflength,
  178. buflengthinc,
  179. length:word;
  180. buf:Psuperregisterarray;
  181. constructor init;
  182. constructor copyfrom(const x:Tsuperregisterworklist);
  183. destructor done;
  184. procedure clear;
  185. procedure add(s:tsuperregister);
  186. function get:tsuperregister;
  187. procedure deleteidx(i:word);
  188. function delete(s:tsuperregister):boolean;
  189. end;
  190. psuperregisterworklist=^tsuperregisterworklist;
  191. const
  192. { alias for easier understanding }
  193. R_SSEREGISTER = R_MMREGISTER;
  194. { Invalid register number }
  195. RS_INVALID = high(tsuperregister);
  196. { Maximum number of cpu registers per register type,
  197. this must fit in tcpuregisterset }
  198. maxcpuregister = 32;
  199. tcgsize2size : Array[tcgsize] of integer =
  200. { integer values }
  201. (0,1,2,4,8,16,1,2,4,8,16,
  202. { floating point values }
  203. 4,8,EXTENDED_SIZE,8,16,
  204. { multimedia values }
  205. 1,2,4,8,16,1,2,4,8,16);
  206. tfloat2tcgsize: array[tfloattype] of tcgsize =
  207. (OS_F32,OS_F64,OS_F80,OS_C64,OS_C64,OS_F128);
  208. tcgsize2tfloat: array[OS_F32..OS_C64] of tfloattype =
  209. (s32real,s64real,s80real,s64comp);
  210. { Table to convert tcgsize variables to the correspondending
  211. unsigned types }
  212. tcgsize2unsigned : array[tcgsize] of tcgsize = (OS_NO,
  213. OS_8,OS_16,OS_32,OS_64,OS_128,OS_8,OS_16,OS_32,OS_64,OS_128,
  214. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  215. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,OS_M8,OS_M16,OS_M32,
  216. OS_M64,OS_M128);
  217. tcgloc2str : array[TCGLoc] of string[11] = (
  218. 'LOC_INVALID',
  219. 'LOC_VOID',
  220. 'LOC_CONST',
  221. 'LOC_JUMP',
  222. 'LOC_FLAGS',
  223. 'LOC_CREF',
  224. 'LOC_REF',
  225. 'LOC_REG',
  226. 'LOC_CREG',
  227. 'LOC_FPUREG',
  228. 'LOC_CFPUREG',
  229. 'LOC_MMXREG',
  230. 'LOC_CMMXREG',
  231. 'LOC_MMREG',
  232. 'LOC_CMMREG');
  233. var
  234. mms_movescalar : pmmshuffle;
  235. procedure supregset_reset(var regs:tsuperregisterset;setall:boolean;
  236. maxreg:Tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  237. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  238. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  239. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  240. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  241. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  242. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  243. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  244. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  245. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  246. function generic_regname(r:tregister):string;
  247. {# From a constant numeric value, return the abstract code generator
  248. size.
  249. }
  250. function int_cgsize(const a: aint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  251. { return the inverse condition of opcmp }
  252. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  253. { return whether op is commutative }
  254. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  255. { returns true, if shuffle describes a real shuffle operation and not only a move }
  256. function realshuffle(shuffle : pmmshuffle) : boolean;
  257. { returns true, if the shuffle describes only a move of the scalar at index 0 }
  258. function shufflescalar(shuffle : pmmshuffle) : boolean;
  259. { removes shuffling from shuffle, this means that the destenation index of each shuffle is copied to
  260. the source }
  261. procedure removeshuffles(var shuffle : tmmshuffle);
  262. implementation
  263. uses
  264. verbose;
  265. {******************************************************************************
  266. tsuperregisterworklist
  267. ******************************************************************************}
  268. constructor tsuperregisterworklist.init;
  269. begin
  270. length:=0;
  271. buflength:=0;
  272. buflengthinc:=16;
  273. buf:=nil;
  274. end;
  275. constructor Tsuperregisterworklist.copyfrom(const x:Tsuperregisterworklist);
  276. begin
  277. self:=x;
  278. if x.buf<>nil then
  279. begin
  280. getmem(buf,buflength*sizeof(Tsuperregister));
  281. move(x.buf^,buf^,length*sizeof(Tsuperregister));
  282. end;
  283. end;
  284. destructor tsuperregisterworklist.done;
  285. begin
  286. if assigned(buf) then
  287. freemem(buf);
  288. end;
  289. procedure tsuperregisterworklist.add(s:tsuperregister);
  290. begin
  291. inc(length);
  292. { Need to increase buffer length? }
  293. if length>=buflength then
  294. begin
  295. inc(buflength,buflengthinc);
  296. buflengthinc:=buflengthinc*2;
  297. if buflengthinc>256 then
  298. buflengthinc:=256;
  299. reallocmem(buf,buflength*sizeof(Tsuperregister));
  300. end;
  301. buf^[length-1]:=s;
  302. end;
  303. procedure tsuperregisterworklist.clear;
  304. begin
  305. length:=0;
  306. end;
  307. procedure tsuperregisterworklist.deleteidx(i:word);
  308. begin
  309. if length=0 then
  310. internalerror(200310144);
  311. buf^[i]:=buf^[length-1];
  312. dec(length);
  313. end;
  314. function tsuperregisterworklist.get:tsuperregister;
  315. begin
  316. if length=0 then
  317. internalerror(200310142);
  318. get:=buf^[0];
  319. buf^[0]:=buf^[length-1];
  320. dec(length);
  321. end;
  322. function tsuperregisterworklist.delete(s:tsuperregister):boolean;
  323. var
  324. i:longint;
  325. begin
  326. delete:=false;
  327. {$ifdef VER1_0}
  328. { indexword in 1.0.x is broken }
  329. for i:=1 to length do
  330. if buf^[i-1]=s then
  331. begin
  332. deleteidx(i-1);
  333. delete:=true;
  334. break;
  335. end;
  336. {$else VER1_0}
  337. {$ifndef FPC}
  338. for i:=1 to length do
  339. if buf^[i-1]=s then
  340. begin
  341. deleteidx(i-1);
  342. delete:=true;
  343. break;
  344. end;
  345. {$else FPC}
  346. i:=indexword(buf^,length,s);
  347. if i<>-1 then
  348. begin
  349. deleteidx(i);
  350. delete := true;
  351. end;
  352. {$endif FPC}
  353. {$endif VER1_0}
  354. end;
  355. procedure supregset_reset(var regs:tsuperregisterset;setall:boolean;
  356. maxreg:Tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  357. begin
  358. fillchar(regs,(maxreg or 7) shr 3,-byte(setall));
  359. end;
  360. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  361. begin
  362. include(regs[s shr 8],(s and $ff));
  363. end;
  364. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  365. begin
  366. exclude(regs[s shr 8],(s and $ff));
  367. end;
  368. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  369. begin
  370. result:=(s and $ff) in regs[s shr 8];
  371. end;
  372. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  373. begin
  374. tregisterrec(result).regtype:=rt;
  375. tregisterrec(result).supreg:=sr;
  376. tregisterrec(result).subreg:=sb;
  377. end;
  378. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  379. begin
  380. result:=tregisterrec(r).subreg;
  381. end;
  382. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  383. begin
  384. result:=tregisterrec(r).supreg;
  385. end;
  386. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  387. begin
  388. result:=tregisterrec(r).regtype;
  389. end;
  390. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  391. begin
  392. tregisterrec(r).subreg:=sr;
  393. end;
  394. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  395. begin
  396. tregisterrec(r).supreg:=sr;
  397. end;
  398. function generic_regname(r:tregister):string;
  399. var
  400. nr : string[12];
  401. begin
  402. str(getsupreg(r),nr);
  403. case getregtype(r) of
  404. R_INTREGISTER:
  405. result:='ireg'+nr;
  406. R_FPUREGISTER:
  407. result:='freg'+nr;
  408. R_MMREGISTER:
  409. result:='mreg'+nr;
  410. R_MMXREGISTER:
  411. result:='xreg'+nr;
  412. else
  413. begin
  414. result:='INVALID';
  415. exit;
  416. end;
  417. end;
  418. case getsubreg(r) of
  419. R_SUBNONE:
  420. ;
  421. R_SUBL:
  422. result:=result+'l';
  423. R_SUBH:
  424. result:=result+'h';
  425. R_SUBW:
  426. result:=result+'w';
  427. R_SUBD:
  428. result:=result+'d';
  429. R_SUBQ:
  430. result:=result+'q';
  431. R_SUBFS:
  432. result:=result+'fs';
  433. R_SUBFD:
  434. result:=result+'fd';
  435. else
  436. internalerror(200308252);
  437. end;
  438. end;
  439. function int_cgsize(const a: aint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  440. const
  441. size2cgsize : array[0..8] of tcgsize = (
  442. OS_NO,OS_8,OS_16,OS_32,OS_32,OS_64,OS_64,OS_64,OS_64
  443. );
  444. begin
  445. if a>8 then
  446. result:=OS_NO
  447. else
  448. result:=size2cgsize[a];
  449. end;
  450. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  451. const
  452. list: array[TOpCmp] of TOpCmp =
  453. (OC_NONE,OC_NE,OC_LTE,OC_GTE,OC_LT,OC_GT,OC_EQ,OC_A,OC_AE,
  454. OC_B,OC_BE);
  455. begin
  456. inverse_opcmp := list[opcmp];
  457. end;
  458. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  459. const
  460. list: array[topcg] of boolean =
  461. (true,true,true,false,false,true,true,false,false,
  462. true,false,false,false,false,true);
  463. begin
  464. commutativeop := list[op];
  465. end;
  466. function realshuffle(shuffle : pmmshuffle) : boolean;
  467. var
  468. i : longint;
  469. begin
  470. realshuffle:=true;
  471. if (shuffle=nil) or (shuffle^.len=0) then
  472. realshuffle:=false
  473. else
  474. begin
  475. for i:=1 to shuffle^.len do
  476. begin
  477. if (shuffle^.shuffles[i] and $f)<>((shuffle^.shuffles[i] and $f0) shr 8) then
  478. exit;
  479. end;
  480. realshuffle:=false;
  481. end;
  482. end;
  483. function shufflescalar(shuffle : pmmshuffle) : boolean;
  484. begin
  485. result:=shuffle^.len=0;
  486. end;
  487. procedure removeshuffles(var shuffle : tmmshuffle);
  488. var
  489. i : longint;
  490. begin
  491. if shuffle.len=0 then
  492. exit;
  493. for i:=1 to shuffle.len do
  494. shuffle.shuffles[i]:=(shuffle.shuffles[i] and $f0) or ((shuffle.shuffles[i] and $f0) shr 8);
  495. end;
  496. initialization
  497. new(mms_movescalar);
  498. mms_movescalar^.len:=0;
  499. finalization
  500. dispose(mms_movescalar);
  501. end.
  502. {
  503. $Log$
  504. Revision 1.94 2004-08-24 21:02:32 florian
  505. * fixed longbool(<int64>) on sparc
  506. Revision 1.93 2004/07/19 19:21:02 florian
  507. * indexword in 1.0.x is broken
  508. Revision 1.92 2004/07/18 15:14:59 jonas
  509. * use indexword() in tsuperregisterworklist.delete, greatly speeds up
  510. compilation of tw2242
  511. Revision 1.91 2004/07/07 17:35:26 daniel
  512. * supregset_reset clears 8kb of memory. However, it is being called in
  513. inner loops, see for example colour_registers. According to profile data
  514. this causes fillchar to be the most time consuming procedure.
  515. Some modifications done to make it clear less than 8kb of memory each
  516. call. Divides time spent in fillchar by two, but it still is the no.1
  517. procedure.
  518. Revision 1.90 2004/06/20 08:55:28 florian
  519. * logs truncated
  520. Revision 1.89 2004/06/16 20:07:07 florian
  521. * dwarf branch merged
  522. Revision 1.88.2.2 2004/05/01 16:02:09 peter
  523. * POINTER_SIZE replaced with sizeof(aint)
  524. * aint,aword,tconst*int moved to globtype
  525. Revision 1.88.2.1 2004/04/27 18:18:25 peter
  526. * aword -> aint
  527. Revision 1.88 2004/02/27 10:21:05 florian
  528. * top_symbol killed
  529. + refaddr to treference added
  530. + refsymbol to treference added
  531. * top_local stuff moved to an extra record to save memory
  532. + aint introduced
  533. * tppufile.get/putint64/aint implemented
  534. Revision 1.87 2004/02/09 22:14:17 peter
  535. * more x86_64 parameter fixes
  536. * tparalocation.lochigh is now used to indicate if registerhigh
  537. is used and what the type is
  538. }