cgbase.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Some basic types and constants for the code generation
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {# This unit exports some types which are used across the code generator }
  18. unit cgbase;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,
  23. symconst;
  24. type
  25. { Location types where value can be stored }
  26. TCGLoc=(
  27. LOC_INVALID, { added for tracking problems}
  28. LOC_VOID, { no value is available }
  29. LOC_CONSTANT, { constant value }
  30. LOC_JUMP, { boolean results only, jump to false or true label }
  31. LOC_FLAGS, { boolean results only, flags are set }
  32. LOC_CREFERENCE, { in memory constant value reference (cannot change) }
  33. LOC_REFERENCE, { in memory value }
  34. LOC_REGISTER, { in a processor register }
  35. LOC_CREGISTER, { Constant register which shouldn't be modified }
  36. LOC_FPUREGISTER, { FPU stack }
  37. LOC_CFPUREGISTER, { if it is a FPU register variable on the fpu stack }
  38. LOC_MMXREGISTER, { MMX register }
  39. { MMX register variable }
  40. LOC_CMMXREGISTER,
  41. { multimedia register }
  42. LOC_MMREGISTER,
  43. { Constant multimedia reg which shouldn't be modified }
  44. LOC_CMMREGISTER
  45. );
  46. { since we have only 16 offsets, we need to be able to specify the high
  47. and low 16 bits of the address of a symbol }
  48. trefaddr = (addr_no,addr_full,addr_hi,addr_lo,addr_pic);
  49. {# Generic opcodes, which must be supported by all processors
  50. }
  51. topcg =
  52. (
  53. OP_NONE,
  54. OP_ADD, { simple addition }
  55. OP_AND, { simple logical and }
  56. OP_DIV, { simple unsigned division }
  57. OP_IDIV, { simple signed division }
  58. OP_IMUL, { simple signed multiply }
  59. OP_MUL, { simple unsigned multiply }
  60. OP_NEG, { simple negate }
  61. OP_NOT, { simple logical not }
  62. OP_OR, { simple logical or }
  63. OP_SAR, { arithmetic shift-right }
  64. OP_SHL, { logical shift left }
  65. OP_SHR, { logical shift right }
  66. OP_SUB, { simple subtraction }
  67. OP_XOR { simple exclusive or }
  68. );
  69. {# Generic flag values - used for jump locations }
  70. TOpCmp =
  71. (
  72. OC_NONE,
  73. OC_EQ, { equality comparison }
  74. OC_GT, { greater than (signed) }
  75. OC_LT, { less than (signed) }
  76. OC_GTE, { greater or equal than (signed) }
  77. OC_LTE, { less or equal than (signed) }
  78. OC_NE, { not equal }
  79. OC_BE, { less or equal than (unsigned) }
  80. OC_B, { less than (unsigned) }
  81. OC_AE, { greater or equal than (unsigned) }
  82. OC_A { greater than (unsigned) }
  83. );
  84. { OS_NO is also used memory references with large data that can
  85. not be loaded in a register directly }
  86. TCgSize = (OS_NO,
  87. { integer registers }
  88. OS_8,OS_16,OS_32,OS_64,OS_128,OS_S8,OS_S16,OS_S32,OS_S64,OS_S128,
  89. { single,double,extended,comp,float128 }
  90. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  91. { multi-media sizes: split in byte, word, dword, ... }
  92. { entities, then the signed counterparts }
  93. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,
  94. OS_MS8,OS_MS16,OS_MS32,OS_MS64,OS_MS128);
  95. { Register types }
  96. TRegisterType = (
  97. R_INVALIDREGISTER, { = 0 }
  98. R_INTREGISTER, { = 1 }
  99. R_FPUREGISTER, { = 2 }
  100. { used by Intel only }
  101. R_MMXREGISTER, { = 3 }
  102. R_MMREGISTER, { = 4 }
  103. R_SPECIALREGISTER, { = 5 }
  104. R_ADDRESSREGISTER { = 6 }
  105. );
  106. { Sub registers }
  107. TSubRegister = (
  108. R_SUBNONE, { = 0; no sub register possible }
  109. R_SUBL, { = 1; 8 bits, Like AL }
  110. R_SUBH, { = 2; 8 bits, Like AH }
  111. R_SUBW, { = 3; 16 bits, Like AX }
  112. R_SUBD, { = 4; 32 bits, Like EAX }
  113. R_SUBQ, { = 5; 64 bits, Like RAX }
  114. { For Sparc floats that use F0:F1 to store doubles }
  115. R_SUBFS, { = 6; Float that allocates 1 FPU register }
  116. R_SUBFD, { = 7; Float that allocates 2 FPU registers }
  117. R_SUBFQ, { = 8; Float that allocates 4 FPU registers }
  118. R_SUBMMS, { = 9; single scalar in multi media register }
  119. R_SUBMMD { = 10; double scalar in multi media register }
  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 = record
  150. reglo,reghi : tregister;
  151. end;
  152. {$endif cpu64bit}
  153. Tregistermmxset = 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. pmmshuffle = ^tmmshuffle;
  160. { this record describes shuffle operations for mm operations; if a pointer a shuffle record
  161. passed to an mm operation is nil, it means that the whole location is moved }
  162. tmmshuffle = record
  163. { describes how many shuffles are actually described, if len=0 then
  164. moving the scalar with index 0 to the scalar with index 0 is meant }
  165. len : byte;
  166. { lower nibble of each entry of this array describes index of the source data index while
  167. the upper nibble describes the destination index }
  168. shuffles : array[1..1] of byte;
  169. end;
  170. Tsuperregisterarray=array[0..$ffff] of Tsuperregister;
  171. Psuperregisterarray=^Tsuperregisterarray;
  172. Tsuperregisterworklist=object
  173. buflength,
  174. buflengthinc,
  175. length:word;
  176. buf:Psuperregisterarray;
  177. constructor init;
  178. constructor copyfrom(const x:Tsuperregisterworklist);
  179. destructor done;
  180. procedure clear;
  181. procedure add(s:tsuperregister);
  182. function get:tsuperregister;
  183. procedure deleteidx(i:word);
  184. function delete(s:tsuperregister):boolean;
  185. end;
  186. psuperregisterworklist=^tsuperregisterworklist;
  187. const
  188. { alias for easier understanding }
  189. R_SSEREGISTER = R_MMREGISTER;
  190. { Invalid register number }
  191. RS_INVALID = high(tsuperregister);
  192. { Maximum number of cpu registers per register type,
  193. this must fit in tcpuregisterset }
  194. maxcpuregister = 32;
  195. tcgsize2size : Array[tcgsize] of integer =
  196. { integer values }
  197. (0,1,2,4,8,16,1,2,4,8,16,
  198. { floating point values }
  199. 4,8,10,8,16,
  200. { multimedia values }
  201. 1,2,4,8,16,1,2,4,8,16);
  202. tfloat2tcgsize: array[tfloattype] of tcgsize =
  203. (OS_F32,OS_F64,OS_F80,OS_C64,OS_C64,OS_F128);
  204. tcgsize2tfloat: array[OS_F32..OS_C64] of tfloattype =
  205. (s32real,s64real,s80real,s64comp);
  206. { Table to convert tcgsize variables to the correspondending
  207. unsigned types }
  208. tcgsize2unsigned : array[tcgsize] of tcgsize = (OS_NO,
  209. OS_8,OS_16,OS_32,OS_64,OS_128,OS_8,OS_16,OS_32,OS_64,OS_128,
  210. OS_F32,OS_F64,OS_F80,OS_C64,OS_F128,
  211. OS_M8,OS_M16,OS_M32,OS_M64,OS_M128,OS_M8,OS_M16,OS_M32,
  212. OS_M64,OS_M128);
  213. tcgloc2str : array[TCGLoc] of string[11] = (
  214. 'LOC_INVALID',
  215. 'LOC_VOID',
  216. 'LOC_CONST',
  217. 'LOC_JUMP',
  218. 'LOC_FLAGS',
  219. 'LOC_CREF',
  220. 'LOC_REF',
  221. 'LOC_REG',
  222. 'LOC_CREG',
  223. 'LOC_FPUREG',
  224. 'LOC_CFPUREG',
  225. 'LOC_MMXREG',
  226. 'LOC_CMMXREG',
  227. 'LOC_MMREG',
  228. 'LOC_CMMREG');
  229. var
  230. mms_movescalar : pmmshuffle;
  231. procedure supregset_reset(var regs:tsuperregisterset;setall:boolean;
  232. maxreg:Tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  233. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  234. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  235. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  236. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  237. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  238. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  239. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  240. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  241. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  242. function generic_regname(r:tregister):string;
  243. {# From a constant numeric value, return the abstract code generator
  244. size.
  245. }
  246. function int_cgsize(const a: aint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  247. function int_float_cgsize(const a: aint): tcgsize;
  248. { return the inverse condition of opcmp }
  249. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  250. { return the opcmp needed when swapping the operands }
  251. function swap_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. { indexword in 1.0.x and 1.9.4 is broken }
  327. i:=indexword(buf^,length,s);
  328. if i<>-1 then
  329. begin
  330. deleteidx(i);
  331. delete := true;
  332. end;
  333. end;
  334. procedure supregset_reset(var regs:tsuperregisterset;setall:boolean;
  335. maxreg:Tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  336. begin
  337. fillchar(regs,(maxreg+7) shr 3,-byte(setall));
  338. end;
  339. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  340. begin
  341. include(regs[s shr 8],(s and $ff));
  342. end;
  343. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  344. begin
  345. exclude(regs[s shr 8],(s and $ff));
  346. end;
  347. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  348. begin
  349. result:=(s and $ff) in regs[s shr 8];
  350. end;
  351. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  352. begin
  353. tregisterrec(result).regtype:=rt;
  354. tregisterrec(result).supreg:=sr;
  355. tregisterrec(result).subreg:=sb;
  356. end;
  357. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  358. begin
  359. result:=tregisterrec(r).subreg;
  360. end;
  361. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  362. begin
  363. result:=tregisterrec(r).supreg;
  364. end;
  365. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  366. begin
  367. result:=tregisterrec(r).regtype;
  368. end;
  369. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  370. begin
  371. tregisterrec(r).subreg:=sr;
  372. end;
  373. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  374. begin
  375. tregisterrec(r).supreg:=sr;
  376. end;
  377. function generic_regname(r:tregister):string;
  378. var
  379. nr : string[12];
  380. begin
  381. str(getsupreg(r),nr);
  382. case getregtype(r) of
  383. R_INTREGISTER:
  384. result:='ireg'+nr;
  385. R_FPUREGISTER:
  386. result:='freg'+nr;
  387. R_MMREGISTER:
  388. result:='mreg'+nr;
  389. R_MMXREGISTER:
  390. result:='xreg'+nr;
  391. else
  392. begin
  393. result:='INVALID';
  394. exit;
  395. end;
  396. end;
  397. case getsubreg(r) of
  398. R_SUBNONE:
  399. ;
  400. R_SUBL:
  401. result:=result+'l';
  402. R_SUBH:
  403. result:=result+'h';
  404. R_SUBW:
  405. result:=result+'w';
  406. R_SUBD:
  407. result:=result+'d';
  408. R_SUBQ:
  409. result:=result+'q';
  410. R_SUBFS:
  411. result:=result+'fs';
  412. R_SUBFD:
  413. result:=result+'fd';
  414. R_SUBMMD:
  415. result:=result+'md';
  416. R_SUBMMS:
  417. result:=result+'ms';
  418. else
  419. internalerror(200308252);
  420. end;
  421. end;
  422. function int_cgsize(const a: aint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  423. const
  424. size2cgsize : array[0..8] of tcgsize = (
  425. OS_NO,OS_8,OS_16,OS_NO,OS_32,OS_NO,OS_NO,OS_NO,OS_64
  426. );
  427. begin
  428. if a>8 then
  429. result:=OS_NO
  430. else
  431. result:=size2cgsize[a];
  432. end;
  433. function int_float_cgsize(const a: aint): tcgsize;
  434. begin
  435. case a of
  436. 4 :
  437. result:=OS_F32;
  438. 8 :
  439. result:=OS_F64;
  440. 10 :
  441. result:=OS_F80;
  442. 16 :
  443. result:=OS_F128;
  444. else
  445. internalerror(200603211);
  446. end;
  447. end;
  448. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  449. const
  450. list: array[TOpCmp] of TOpCmp =
  451. (OC_NONE,OC_NE,OC_LTE,OC_GTE,OC_LT,OC_GT,OC_EQ,OC_A,OC_AE,
  452. OC_B,OC_BE);
  453. begin
  454. inverse_opcmp := list[opcmp];
  455. end;
  456. function swap_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  457. const
  458. list: array[TOpCmp] of TOpCmp =
  459. (OC_NONE,OC_EQ,OC_LT,OC_GT,OC_LTE,OC_GTE,OC_NE,OC_AE,OC_A,
  460. OC_BE,OC_B);
  461. begin
  462. swap_opcmp := list[opcmp];
  463. end;
  464. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  465. const
  466. list: array[topcg] of boolean =
  467. (true,true,true,false,false,true,true,false,false,
  468. true,false,false,false,false,true);
  469. begin
  470. commutativeop := list[op];
  471. end;
  472. function realshuffle(shuffle : pmmshuffle) : boolean;
  473. var
  474. i : longint;
  475. begin
  476. realshuffle:=true;
  477. if (shuffle=nil) or (shuffle^.len=0) then
  478. realshuffle:=false
  479. else
  480. begin
  481. for i:=1 to shuffle^.len do
  482. begin
  483. if (shuffle^.shuffles[i] and $f)<>((shuffle^.shuffles[i] and $f0) shr 8) then
  484. exit;
  485. end;
  486. realshuffle:=false;
  487. end;
  488. end;
  489. function shufflescalar(shuffle : pmmshuffle) : boolean;
  490. begin
  491. result:=shuffle^.len=0;
  492. end;
  493. procedure removeshuffles(var shuffle : tmmshuffle);
  494. var
  495. i : longint;
  496. begin
  497. if shuffle.len=0 then
  498. exit;
  499. for i:=1 to shuffle.len do
  500. shuffle.shuffles[i]:=(shuffle.shuffles[i] and $f0) or ((shuffle.shuffles[i] and $f0) shr 8);
  501. end;
  502. initialization
  503. new(mms_movescalar);
  504. mms_movescalar^.len:=0;
  505. finalization
  506. dispose(mms_movescalar);
  507. end.