cgbase.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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. 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. { multimedia register }
  43. LOC_MMREGISTER,
  44. { Constant multimedia reg which shouldn't be modified }
  45. LOC_CMMREGISTER
  46. );
  47. { since we have only 16 offsets, we need to be able to specify the high
  48. and low 16 bits of the address of a symbol }
  49. trefaddr = (addr_no,addr_full,addr_hi,addr_lo,addr_pic);
  50. {# Generic opcodes, which must be supported by all processors
  51. }
  52. topcg =
  53. (
  54. OP_NONE,
  55. OP_ADD, { simple addition }
  56. OP_AND, { simple logical and }
  57. OP_DIV, { simple unsigned division }
  58. OP_IDIV, { simple signed division }
  59. OP_IMUL, { simple signed multiply }
  60. OP_MUL, { simple unsigned multiply }
  61. OP_NEG, { simple negate }
  62. OP_NOT, { simple logical not }
  63. OP_OR, { simple logical or }
  64. OP_SAR, { arithmetic shift-right }
  65. OP_SHL, { logical shift left }
  66. OP_SHR, { logical shift right }
  67. OP_SUB, { simple subtraction }
  68. OP_XOR { simple exclusive or }
  69. );
  70. {# Generic flag values - used for jump locations }
  71. TOpCmp =
  72. (
  73. OC_NONE,
  74. OC_EQ, { equality comparison }
  75. OC_GT, { greater than (signed) }
  76. OC_LT, { less than (signed) }
  77. OC_GTE, { greater or equal than (signed) }
  78. OC_LTE, { less or equal than (signed) }
  79. OC_NE, { not equal }
  80. OC_BE, { less or equal than (unsigned) }
  81. OC_B, { less than (unsigned) }
  82. OC_AE, { greater or equal than (unsigned) }
  83. OC_A { greater than (unsigned) }
  84. );
  85. { OS_NO is also used memory references with large data that can
  86. not be loaded in a register directly }
  87. TCgSize = (OS_NO,
  88. { integer registers }
  89. OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128,
  90. { single,double,extended,comp,float128 }
  91. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  92. { multi-media sizes: split in byte, word, dword, ... }
  93. { entities, then the signed counterparts }
  94. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,
  95. OS_MS8,OS_MS16,OS_MS32,OS_MS64,OS_MS128);
  96. { Register types }
  97. TRegisterType = (
  98. R_INVALIDREGISTER, { = 0 }
  99. R_INTREGISTER, { = 1 }
  100. R_FPUREGISTER, { = 2 }
  101. { used by Intel only }
  102. R_MMXREGISTER, { = 3 }
  103. R_MMREGISTER, { = 4 }
  104. R_SPECIALREGISTER, { = 5 }
  105. R_ADDRESSREGISTER { = 6 }
  106. );
  107. { Sub registers }
  108. TSubRegister = (
  109. R_SUBNONE, { = 0; no sub register possible }
  110. R_SUBL, { = 1; 8 bits, Like AL }
  111. R_SUBH, { = 2; 8 bits, Like AH }
  112. R_SUBW, { = 3; 16 bits, Like AX }
  113. R_SUBD, { = 4; 32 bits, Like EAX }
  114. R_SUBQ, { = 5; 64 bits, Like RAX }
  115. { For Sparc floats that use F0:F1 to store doubles }
  116. R_SUBFS, { = 6; Float that allocates 1 FPU register }
  117. R_SUBFD, { = 7; Float that allocates 2 FPU registers }
  118. R_SUBFQ { = 8; Float that allocates 4 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 = record
  149. reglo,reghi : tregister;
  150. end;
  151. {$endif cpu64bit}
  152. Tregistermmxset = 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. 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. Tsuperregisterarray=array[0..$ffff] of Tsuperregister;
  170. Psuperregisterarray=^Tsuperregisterarray;
  171. Tsuperregisterworklist=object
  172. buflength,
  173. buflengthinc,
  174. length:word;
  175. buf:Psuperregisterarray;
  176. constructor init;
  177. constructor copyfrom(const x:Tsuperregisterworklist);
  178. destructor done;
  179. procedure clear;
  180. procedure add(s:tsuperregister);
  181. function get:tsuperregister;
  182. procedure deleteidx(i:word);
  183. function delete(s:tsuperregister):boolean;
  184. end;
  185. psuperregisterworklist=^tsuperregisterworklist;
  186. const
  187. { alias for easier understanding }
  188. R_SSEREGISTER = R_MMREGISTER;
  189. { Invalid register number }
  190. RS_INVALID = high(tsuperregister);
  191. { Maximum number of cpu registers per register type,
  192. this must fit in tcpuregisterset }
  193. maxcpuregister = 32;
  194. tcgsize2size : Array[tcgsize] of integer =
  195. { integer values }
  196. (0,1,2,4,8,16,1,2,4,8,16,
  197. { floating point values }
  198. 4,8,10,8,16,
  199. { multimedia values }
  200. 1,2,4,8,16,1,2,4,8,16);
  201. tfloat2tcgsize: array[tfloattype] of tcgsize =
  202. (OS_F32,OS_F64,OS_F80,OS_C64,OS_C64,OS_F128);
  203. tcgsize2tfloat: array[OS_F32..OS_C64] of tfloattype =
  204. (s32real,s64real,s80real,s64comp);
  205. { Table to convert tcgsize variables to the correspondending
  206. unsigned types }
  207. tcgsize2unsigned : array[tcgsize] of tcgsize = (OS_NO,
  208. OS_8,OS_16,OS_32,OS_64,OS_128,OS_8,OS_16,OS_32,OS_64,OS_128,
  209. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  210. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,OS_M8,OS_M16,OS_M32,
  211. OS_M64,OS_M128);
  212. tcgloc2str : array[TCGLoc] of string[11] = (
  213. 'LOC_INVALID',
  214. 'LOC_VOID',
  215. 'LOC_CONST',
  216. 'LOC_JUMP',
  217. 'LOC_FLAGS',
  218. 'LOC_CREF',
  219. 'LOC_REF',
  220. 'LOC_REG',
  221. 'LOC_CREG',
  222. 'LOC_FPUREG',
  223. 'LOC_CFPUREG',
  224. 'LOC_MMXREG',
  225. 'LOC_CMMXREG',
  226. 'LOC_MMREG',
  227. 'LOC_CMMREG');
  228. var
  229. mms_movescalar : pmmshuffle;
  230. procedure supregset_reset(var regs:tsuperregisterset;setall:boolean;
  231. maxreg:Tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  232. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  233. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  234. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  235. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  236. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  237. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  238. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  239. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  240. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  241. function generic_regname(r:tregister):string;
  242. {# From a constant numeric value, return the abstract code generator
  243. size.
  244. }
  245. function int_cgsize(const a: aint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  246. { return the inverse condition of opcmp }
  247. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  248. { return whether op is commutative }
  249. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  250. { returns true, if shuffle describes a real shuffle operation and not only a move }
  251. function realshuffle(shuffle : pmmshuffle) : boolean;
  252. { returns true, if the shuffle describes only a move of the scalar at index 0 }
  253. function shufflescalar(shuffle : pmmshuffle) : boolean;
  254. { removes shuffling from shuffle, this means that the destenation index of each shuffle is copied to
  255. the source }
  256. procedure removeshuffles(var shuffle : tmmshuffle);
  257. implementation
  258. uses
  259. verbose;
  260. {******************************************************************************
  261. tsuperregisterworklist
  262. ******************************************************************************}
  263. constructor tsuperregisterworklist.init;
  264. begin
  265. length:=0;
  266. buflength:=0;
  267. buflengthinc:=16;
  268. buf:=nil;
  269. end;
  270. constructor Tsuperregisterworklist.copyfrom(const x:Tsuperregisterworklist);
  271. begin
  272. self:=x;
  273. if x.buf<>nil then
  274. begin
  275. getmem(buf,buflength*sizeof(Tsuperregister));
  276. move(x.buf^,buf^,length*sizeof(Tsuperregister));
  277. end;
  278. end;
  279. destructor tsuperregisterworklist.done;
  280. begin
  281. if assigned(buf) then
  282. freemem(buf);
  283. end;
  284. procedure tsuperregisterworklist.add(s:tsuperregister);
  285. begin
  286. inc(length);
  287. { Need to increase buffer length? }
  288. if length>=buflength then
  289. begin
  290. inc(buflength,buflengthinc);
  291. buflengthinc:=buflengthinc*2;
  292. if buflengthinc>256 then
  293. buflengthinc:=256;
  294. reallocmem(buf,buflength*sizeof(Tsuperregister));
  295. end;
  296. buf^[length-1]:=s;
  297. end;
  298. procedure tsuperregisterworklist.clear;
  299. begin
  300. length:=0;
  301. end;
  302. procedure tsuperregisterworklist.deleteidx(i:word);
  303. begin
  304. if length=0 then
  305. internalerror(200310144);
  306. buf^[i]:=buf^[length-1];
  307. dec(length);
  308. end;
  309. function tsuperregisterworklist.get:tsuperregister;
  310. begin
  311. if length=0 then
  312. internalerror(200310142);
  313. get:=buf^[0];
  314. buf^[0]:=buf^[length-1];
  315. dec(length);
  316. end;
  317. function tsuperregisterworklist.delete(s:tsuperregister):boolean;
  318. var
  319. i:longint;
  320. begin
  321. delete:=false;
  322. { indexword in 1.0.x and 1.9.4 is broken }
  323. {$ifndef VER1_0}
  324. {$ifndef VER1_9_4}
  325. {$define USEINDEXWORD}
  326. {$endif}
  327. {$endif}
  328. {$ifdef USEINDEXWORD}
  329. i:=indexword(buf^,length,s);
  330. if i<>-1 then
  331. begin
  332. deleteidx(i);
  333. delete := true;
  334. end;
  335. {$else USEINDEXWORD}
  336. for i:=1 to length do
  337. if buf^[i-1]=s then
  338. begin
  339. deleteidx(i-1);
  340. delete:=true;
  341. break;
  342. end;
  343. {$endif USEINDEXWORD}
  344. {$undef USEINDEXWORD}
  345. end;
  346. procedure supregset_reset(var regs:tsuperregisterset;setall:boolean;
  347. maxreg:Tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  348. begin
  349. fillchar(regs,(maxreg+7) shr 3,-byte(setall));
  350. end;
  351. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  352. begin
  353. include(regs[s shr 8],(s and $ff));
  354. end;
  355. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  356. begin
  357. exclude(regs[s shr 8],(s and $ff));
  358. end;
  359. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  360. begin
  361. result:=(s and $ff) in regs[s shr 8];
  362. end;
  363. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  364. begin
  365. tregisterrec(result).regtype:=rt;
  366. tregisterrec(result).supreg:=sr;
  367. tregisterrec(result).subreg:=sb;
  368. end;
  369. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  370. begin
  371. result:=tregisterrec(r).subreg;
  372. end;
  373. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  374. begin
  375. result:=tregisterrec(r).supreg;
  376. end;
  377. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  378. begin
  379. result:=tregisterrec(r).regtype;
  380. end;
  381. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  382. begin
  383. tregisterrec(r).subreg:=sr;
  384. end;
  385. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  386. begin
  387. tregisterrec(r).supreg:=sr;
  388. end;
  389. function generic_regname(r:tregister):string;
  390. var
  391. nr : string[12];
  392. begin
  393. str(getsupreg(r),nr);
  394. case getregtype(r) of
  395. R_INTREGISTER:
  396. result:='ireg'+nr;
  397. R_FPUREGISTER:
  398. result:='freg'+nr;
  399. R_MMREGISTER:
  400. result:='mreg'+nr;
  401. R_MMXREGISTER:
  402. result:='xreg'+nr;
  403. else
  404. begin
  405. result:='INVALID';
  406. exit;
  407. end;
  408. end;
  409. case getsubreg(r) of
  410. R_SUBNONE:
  411. ;
  412. R_SUBL:
  413. result:=result+'l';
  414. R_SUBH:
  415. result:=result+'h';
  416. R_SUBW:
  417. result:=result+'w';
  418. R_SUBD:
  419. result:=result+'d';
  420. R_SUBQ:
  421. result:=result+'q';
  422. R_SUBFS:
  423. result:=result+'fs';
  424. R_SUBFD:
  425. result:=result+'fd';
  426. else
  427. internalerror(200308252);
  428. end;
  429. end;
  430. function int_cgsize(const a: aint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  431. const
  432. size2cgsize : array[0..8] of tcgsize = (
  433. OS_NO,OS_8,OS_16,OS_32,OS_32,OS_64,OS_64,OS_64,OS_64
  434. );
  435. begin
  436. if a>8 then
  437. result:=OS_NO
  438. else
  439. result:=size2cgsize[a];
  440. end;
  441. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  442. const
  443. list: array[TOpCmp] of TOpCmp =
  444. (OC_NONE,OC_NE,OC_LTE,OC_GTE,OC_LT,OC_GT,OC_EQ,OC_A,OC_AE,
  445. OC_B,OC_BE);
  446. begin
  447. inverse_opcmp := list[opcmp];
  448. end;
  449. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  450. const
  451. list: array[topcg] of boolean =
  452. (true,true,true,false,false,true,true,false,false,
  453. true,false,false,false,false,true);
  454. begin
  455. commutativeop := list[op];
  456. end;
  457. function realshuffle(shuffle : pmmshuffle) : boolean;
  458. var
  459. i : longint;
  460. begin
  461. realshuffle:=true;
  462. if (shuffle=nil) or (shuffle^.len=0) then
  463. realshuffle:=false
  464. else
  465. begin
  466. for i:=1 to shuffle^.len do
  467. begin
  468. if (shuffle^.shuffles[i] and $f)<>((shuffle^.shuffles[i] and $f0) shr 8) then
  469. exit;
  470. end;
  471. realshuffle:=false;
  472. end;
  473. end;
  474. function shufflescalar(shuffle : pmmshuffle) : boolean;
  475. begin
  476. result:=shuffle^.len=0;
  477. end;
  478. procedure removeshuffles(var shuffle : tmmshuffle);
  479. var
  480. i : longint;
  481. begin
  482. if shuffle.len=0 then
  483. exit;
  484. for i:=1 to shuffle.len do
  485. shuffle.shuffles[i]:=(shuffle.shuffles[i] and $f0) or ((shuffle.shuffles[i] and $f0) shr 8);
  486. end;
  487. initialization
  488. new(mms_movescalar);
  489. mms_movescalar^.len:=0;
  490. finalization
  491. dispose(mms_movescalar);
  492. end.
  493. {
  494. $Log$
  495. Revision 1.101 2005-02-14 17:13:06 peter
  496. * truncate log
  497. }