cgbase.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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_REGISTER, { in a processor register }
  33. LOC_CREGISTER, { Constant register which shouldn't be modified }
  34. LOC_FPUREGISTER, { FPU stack }
  35. LOC_CFPUREGISTER, { if it is a FPU register variable on the fpu stack }
  36. LOC_MMXREGISTER, { MMX register }
  37. { MMX register variable }
  38. LOC_CMMXREGISTER,
  39. { multimedia register }
  40. LOC_MMREGISTER,
  41. { Constant multimedia reg which shouldn't be modified }
  42. LOC_CMMREGISTER,
  43. { contiguous subset of bits of an integer register }
  44. LOC_SUBSETREG,
  45. LOC_CSUBSETREG,
  46. { contiguous subset of bits in memory }
  47. LOC_SUBSETREF,
  48. LOC_CSUBSETREF,
  49. { keep these last for range checking purposes }
  50. LOC_CREFERENCE, { in memory constant value reference (cannot change) }
  51. LOC_REFERENCE { in memory value }
  52. );
  53. TCGNonRefLoc=low(TCGLoc)..pred(LOC_CREFERENCE);
  54. TCGRefLoc=LOC_CREFERENCE..LOC_REFERENCE;
  55. trefaddr = (
  56. addr_no,
  57. addr_full,
  58. addr_pic,
  59. addr_pic_no_got
  60. {$IF defined(POWERPC) or defined(POWERPC64) or defined(SPARC) or defined(MIPS) or defined(SPARC64)}
  61. ,
  62. { since we have only 16bit offsets, we need to be able to specify the high
  63. and lower 16 bits of the address of a symbol of up to 64 bit }
  64. addr_low, // bits 48-63
  65. addr_high, // bits 32-47
  66. {$IF defined(POWERPC64)}
  67. addr_higher, // bits 16-31
  68. addr_highest, // bits 00-15
  69. {$ENDIF}
  70. addr_higha // bits 16-31, adjusted
  71. {$IF defined(POWERPC64)}
  72. ,
  73. addr_highera, // bits 32-47, adjusted
  74. addr_highesta // bits 48-63, adjusted
  75. {$ENDIF}
  76. {$ENDIF POWERPC or POWERPC64 or SPARC or MIPS or SPARC64}
  77. {$IFDEF MIPS}
  78. ,
  79. addr_pic_call16, // like addr_pic, but generates call16 reloc instead of got16
  80. addr_low_pic, // for large GOT model, generate got_hi16 and got_lo16 relocs
  81. addr_high_pic,
  82. addr_low_call, // counterpart of two above, generate call_hi16 and call_lo16 relocs
  83. addr_high_call
  84. {$ENDIF}
  85. {$if defined(RISCV32) or defined(RISCV64)}
  86. ,
  87. addr_hi20,
  88. addr_lo12,
  89. addr_pcrel_hi20,
  90. addr_pcrel_lo12,
  91. addr_pcrel,
  92. addr_got_pcrel_hi,
  93. addr_plt
  94. {$endif RISCV}
  95. {$if defined(LOONGARCH64)}
  96. ,
  97. addr_b16, { %b16(sym) }
  98. addr_b21, { %b21(sym) }
  99. addr_b26, { %b26(sym) }
  100. addr_pcrel, { Some times we only use sym like 'bxx rd,rj,sym'. And la.pcrel..sym }
  101. addr_plt, { %plt(sym) }
  102. addr_abs_hi20, { %abs_hi20(sym) }
  103. addr_abs_lo12, { %abs_lo12(sym) }
  104. addr_abs64_lo20, { %abs_lo20(sym) }
  105. addr_abs64_hi12, { %abs_hi12(sym) }
  106. addr_pc_hi20, { %pc_hi20(sym) }
  107. addr_got_pc_hi20, { %got_pc_hi20(sym) }
  108. addr_got_pc_lo12, { %got_pc_lo12(sym) }
  109. addr_pc_lo12, { %pc_lo12(sym) }
  110. addr_got, { la.got..sym }
  111. addr_abs, { la.abs..sym }
  112. addr_reg_reg, { use by [ld/st]x }
  113. addr_reg_12i, { use by [ld/st] }
  114. addr_reg_14i, { use by [ldptr/stptr] }
  115. addr_reg { use by jr.. }
  116. {$endif LOONGARCH64}
  117. {$IFDEF AVR}
  118. ,addr_lo8
  119. ,addr_lo8_gs
  120. ,addr_hi8
  121. ,addr_hi8_gs
  122. {$ENDIF}
  123. {$IFDEF Z80}
  124. ,addr_lo8
  125. ,addr_hi8
  126. {$ENDIF}
  127. {$IFDEF MOS6502}
  128. ,addr_lo8
  129. ,addr_hi8
  130. {$ENDIF}
  131. {$IFDEF i8086}
  132. ,addr_dgroup // the data segment group
  133. ,addr_fardataseg // the far data segment of the current pascal module (unit or program)
  134. ,addr_seg // used for getting the segment of an object, e.g. 'mov ax, SEG symbol'
  135. {$ENDIF}
  136. {$IFDEF AARCH64}
  137. ,addr_page
  138. ,addr_pageoffset
  139. ,addr_gotpage
  140. ,addr_gotpageoffset
  141. {$ENDIF AARCH64}
  142. {$ifdef SPARC64}
  143. ,addr_gdop_hix22
  144. ,addr_gdop_lox22
  145. {$endif SPARC64}
  146. {$IFDEF ARM}
  147. ,addr_gottpoff
  148. ,addr_tpoff
  149. ,addr_tlsgd
  150. ,addr_tlsdesc
  151. ,addr_tlscall
  152. {$ENDIF}
  153. {$IFDEF i386}
  154. ,addr_ntpoff
  155. ,addr_tlsgd
  156. {$ENDIF}
  157. {$ifdef x86_64}
  158. ,addr_tpoff
  159. ,addr_tlsgd
  160. {$endif x86_64}
  161. {$ifdef wasm32}
  162. ,addr_got_tls
  163. {$endif wasm32}
  164. );
  165. {# Generic opcodes, which must be supported by all processors
  166. }
  167. topcg =
  168. (
  169. OP_NONE,
  170. OP_MOVE, { replaced operation with direct load }
  171. OP_ADD, { simple addition }
  172. OP_AND, { simple logical and }
  173. OP_DIV, { simple unsigned division }
  174. OP_IDIV, { simple signed division }
  175. OP_IMUL, { simple signed multiply }
  176. OP_MUL, { simple unsigned multiply }
  177. OP_NEG, { simple negate }
  178. OP_NOT, { simple logical not }
  179. OP_OR, { simple logical or }
  180. OP_SAR, { arithmetic shift-right }
  181. OP_SHL, { logical shift left }
  182. OP_SHR, { logical shift right }
  183. OP_SUB, { simple subtraction }
  184. OP_XOR, { simple exclusive or }
  185. OP_ROL, { rotate left }
  186. OP_ROR { rotate right }
  187. );
  188. {# Generic flag values - used for jump locations }
  189. TOpCmp =
  190. (
  191. OC_NONE,
  192. OC_EQ, { equality comparison }
  193. OC_GT, { greater than (signed) }
  194. OC_LT, { less than (signed) }
  195. OC_GTE, { greater or equal than (signed) }
  196. OC_LTE, { less or equal than (signed) }
  197. OC_NE, { not equal }
  198. OC_BE, { less or equal than (unsigned) }
  199. OC_B, { less than (unsigned) }
  200. OC_AE, { greater or equal than (unsigned) }
  201. OC_A { greater than (unsigned) }
  202. );
  203. { indirect symbol flags }
  204. tindsymflag = (is_data,is_weak);
  205. tindsymflags = set of tindsymflag;
  206. { OS_NO is also used memory references with large data that can
  207. not be loaded in a register directly }
  208. TCgSize = (OS_NO,
  209. OS_8, OS_16, OS_32, OS_64, OS_128,
  210. OS_S8, OS_S16, OS_S32, OS_S64, OS_S128,
  211. { single, double, extended, comp, float128 }
  212. OS_F32, OS_F64, OS_F80, OS_C64, OS_F128,
  213. { multi-media sizes, describes only the register size but not how it is split,
  214. this information must be passed separately }
  215. OS_M8, OS_M16, OS_M32, OS_M64, OS_M128, OS_M256, OS_M512);
  216. { Register types }
  217. TRegisterType = (
  218. R_INVALIDREGISTER, { = 0 }
  219. R_INTREGISTER, { = 1 }
  220. R_FPUREGISTER, { = 2 }
  221. { used by Intel only }
  222. R_MMXREGISTER, { = 3 }
  223. R_MMREGISTER, { = 4 }
  224. R_SPECIALREGISTER, { = 5 }
  225. R_ADDRESSREGISTER, { = 6 }
  226. { used on llvm, every temp gets its own "base register" }
  227. R_TEMPREGISTER, { = 7 }
  228. { used on llvm for tracking metadata (every unique metadata has its own base register) }
  229. R_METADATAREGISTER,{ = 8 }
  230. { optional MAC16 (16 bit multiply-accumulate) registers on Xtensa }
  231. R_MAC16REGISTER, { = 9 }
  232. { WebAssembly externref }
  233. R_EXTERNREFREGISTER, { = 10 }
  234. { WebAssembly funcref }
  235. R_FUNCREFREGISTER { = 11 }
  236. { do not add more than 16 elements (ifdef by cpu type if needed)
  237. so we can store this in one nibble and pack TRegister
  238. if the supreg width should be extended }
  239. );
  240. { Sub registers }
  241. TSubRegister = (
  242. R_SUBNONE, { = 0; no sub register possible }
  243. R_SUBL, { = 1; 8 bits, Like AL }
  244. R_SUBH, { = 2; 8 bits, Like AH }
  245. R_SUBW, { = 3; 16 bits, Like AX }
  246. R_SUBD, { = 4; 32 bits, Like EAX }
  247. R_SUBQ, { = 5; 64 bits, Like RAX }
  248. { For Sparc floats that use F0:F1 to store doubles }
  249. R_SUBFS, { = 6; Float that allocates 1 FPU register }
  250. R_SUBFD, { = 7; Float that allocates 2 FPU registers }
  251. R_SUBFQ, { = 8; Float that allocates 4 FPU registers }
  252. R_SUBMMS, { = 9; single scalar in multi media register }
  253. R_SUBMMD, { = 10; double scalar in multi media register }
  254. R_SUBMMWHOLE, { = 11; complete MM register, size depends on CPU }
  255. { For Intel X86 AVX-Register }
  256. R_SUBMMX, { = 12; 128 BITS }
  257. R_SUBMMY, { = 13; 256 BITS }
  258. R_SUBMMZ, { = 14; 512 BITS }
  259. {$if defined(Z80)}
  260. { Subregisters for the flags register (Z80) }
  261. R_SUBFLAGCARRY, { = 15; Carry flag }
  262. R_SUBFLAGADDSUBTRACT, { = 16; Add/Subtract flag }
  263. R_SUBFLAGPARITYOVERFLOW, { = 17; Parity/Overflow flag }
  264. R_SUBFLAGUNUSEDBIT3, { = 18; Unused flag (bit 3) }
  265. R_SUBFLAGHALFCARRY, { = 19; Half Carry flag }
  266. R_SUBFLAGUNUSEDBIT5, { = 20; Unused flag (bit 5) }
  267. R_SUBFLAGZERO, { = 21; Zero flag }
  268. R_SUBFLAGSIGN, { = 22; Sign flag }
  269. {$elseif defined(MOS6502)}
  270. R_SUBFLAGCARRY, { = 15; Carry flag }
  271. R_SUBFLAGZERO, { = 16; Zero flag }
  272. R_SUBFLAGIRQDISABLE, { = 17; IRQ disable. Set if maskable interrupts are disabled }
  273. R_SUBFLAGDECIMALMODE, { = 18; Decimal mode flag. }
  274. R_SUBFLAGBRKCOMMAND, { = 19; Set if an interrupt caused by BRK, reset if caused by an external interrupt }
  275. R_SUBFLAGUNUSEDBIT5, { = 20; Unused flag (bit 5) }
  276. R_SUBFLAGOVERFLOW, { = 21; Overflow flag }
  277. R_SUBFLAGNEGATIVE, { = 22; Negative flag }
  278. {$else}
  279. { Subregisters for the flags register (x86) }
  280. R_SUBFLAGCARRY, { = 15; Carry flag }
  281. R_SUBFLAGPARITY, { = 16; Parity flag }
  282. R_SUBFLAGAUXILIARY, { = 17; Auxiliary flag }
  283. R_SUBFLAGZERO, { = 18; Zero flag }
  284. R_SUBFLAGSIGN, { = 19; Sign flag }
  285. R_SUBFLAGOVERFLOW, { = 20; Overflow flag }
  286. R_SUBFLAGINTERRUPT, { = 21; Interrupt enable flag }
  287. R_SUBFLAGDIRECTION, { = 22; Direction flag }
  288. {$endif}
  289. { subregisters for the metadata register (llvm) }
  290. R_SUBMETASTRING { = 23 }
  291. {$ifdef aarch64}
  292. , R_SUBMM8B { = 24; for arrangement of v regs on aarch64 }
  293. , R_SUBMM16B { = 25; for arrangement of v regs on aarch64 }
  294. , R_SUBMM4H { = 26; for arrangement of v regs on aarch64 }
  295. , R_SUBMM8H { = 27; for arrangement of v regs on aarch64 }
  296. , R_SUBMM2S { = 28; for arrangement of v regs on aarch64 }
  297. , R_SUBMM4S { = 29; for arrangement of v regs on aarch64 }
  298. , R_SUBMM1D { = 30; for arrangement of v regs on aarch64 }
  299. , R_SUBMM2D { = 31; for arrangement of v regs on aarch64 }
  300. , R_SUBMMB1 { = 32; for arrangement of v regs on aarch64; for use with ldN/stN }
  301. , R_SUBMMH1 { = 33; for arrangement of v regs on aarch64; for use with ldN/stN }
  302. , R_SUBMMS1 { = 34; for arrangement of v regs on aarch64; for use with ldN/stN }
  303. , R_SUBMMD1 { = 35; for arrangement of v regs on aarch64; for use with ldN/stN }
  304. {$endif aarch64}
  305. {$ifdef x86}
  306. , R_SUBMMT { = 24; For Intel X86 AMX-Register }
  307. {$endif x86}
  308. );
  309. TSubRegisterSet = set of TSubRegister;
  310. TSuperRegister = type word;
  311. {
  312. The new register coding:
  313. SuperRegister (bits 0..15)
  314. Subregister (bits 16..23)
  315. Register type (bits 24..31)
  316. TRegister is defined as an enum to make it incompatible
  317. with TSuperRegister to avoid mixing them
  318. }
  319. TRegister = (
  320. TRegisterLowEnum := Low(longint),
  321. TRegisterHighEnum := High(longint)
  322. );
  323. TRegisterRec=packed record
  324. {$ifdef FPC_BIG_ENDIAN}
  325. regtype : Tregistertype;
  326. subreg : Tsubregister;
  327. supreg : Tsuperregister;
  328. {$else FPC_BIG_ENDIAN}
  329. supreg : Tsuperregister;
  330. subreg : Tsubregister;
  331. regtype : Tregistertype;
  332. {$endif FPC_BIG_ENDIAN}
  333. end;
  334. { A type to store register locations for 64 Bit values. }
  335. {$ifdef cpu64bitalu}
  336. tregister64 = tregister;
  337. tregister128 = record
  338. reglo,reghi : tregister;
  339. end;
  340. {$else cpu64bitalu}
  341. tregister64 = record
  342. reglo,reghi : tregister;
  343. end;
  344. {$endif cpu64bitalu}
  345. { Set type definition for registers }
  346. tsuperregisterset = array[byte] of set of byte;
  347. pmmshuffle = ^tmmshuffle;
  348. { this record describes shuffle operations for mm operations; if a pointer a shuffle record
  349. passed to an mm operation is nil, it means that the whole location is moved }
  350. tmmshuffle = record
  351. { describes how many shuffles are actually described, if len=0 then
  352. moving the scalar with index 0 to the scalar with index 0 is meant,
  353. if len=-1, then a variable/unknown length is assumed }
  354. len : Shortint;
  355. { lower byte of each entry of this array describes index of the source data index while
  356. the upper byte describes the destination index }
  357. shuffles : array[1..1] of word;
  358. end;
  359. Tsuperregisterarray=array of Tsuperregister;
  360. Tsuperregisterworklist=object
  361. buflength,
  362. buflengthinc,
  363. length:word;
  364. buf:tsuperregisterarray;
  365. constructor init;
  366. constructor copyfrom(const x:Tsuperregisterworklist);
  367. destructor done;
  368. procedure clear;
  369. procedure add(s:tsuperregister);
  370. function addnodup(s:tsuperregister): boolean;
  371. { returns the last element and removes it from the list }
  372. function get:tsuperregister;
  373. function readidx(i:word):tsuperregister;
  374. procedure deleteidx(i:word);
  375. function delete(s:tsuperregister):boolean;
  376. end;
  377. psuperregisterworklist=^tsuperregisterworklist;
  378. const
  379. { alias for easier understanding }
  380. R_SSEREGISTER = R_MMREGISTER;
  381. { Invalid register number }
  382. RS_INVALID = high(tsuperregister);
  383. NR_INVALID = tregister($ffffffff);
  384. tcgsize2size : Array[tcgsize] of integer =
  385. (0,
  386. { integer values }
  387. 1, 2, 4, 8, 16,
  388. 1, 2, 4, 8, 16,
  389. { floating point values }
  390. 4, 8, 10, 8, 16,
  391. { multimedia values }
  392. 1, 2, 4, 8, 16, 32, 64);
  393. tfloat2tcgsize: array[tfloattype] of tcgsize =
  394. (OS_F32,OS_F64,OS_F80,OS_F80,OS_C64,OS_C64,OS_F128);
  395. tcgsize2tfloat: array[OS_F32..OS_C64] of tfloattype =
  396. (s32real,s64real,s80real,s64comp);
  397. tvarregable2tcgloc : array[tvarregable] of tcgloc = (LOC_VOID,
  398. LOC_CREGISTER,LOC_CFPUREGISTER,LOC_CMMREGISTER,LOC_CREGISTER);
  399. {$if defined(cpu64bitalu)}
  400. { operand size describing an unsigned value in a pair of int registers }
  401. OS_PAIR = OS_128;
  402. { operand size describing an signed value in a pair of int registers }
  403. OS_SPAIR = OS_S128;
  404. {$elseif defined(cpu32bitalu)}
  405. { operand size describing an unsigned value in a pair of int registers }
  406. OS_PAIR = OS_64;
  407. { operand size describing an signed value in a pair of int registers }
  408. OS_SPAIR = OS_S64;
  409. {$elseif defined(cpu16bitalu)}
  410. { operand size describing an unsigned value in a pair of int registers }
  411. OS_PAIR = OS_32;
  412. { operand size describing an signed value in a pair of int registers }
  413. OS_SPAIR = OS_S32;
  414. {$elseif defined(cpu8bitalu)}
  415. { operand size describing an unsigned value in a pair of int registers }
  416. OS_PAIR = OS_16;
  417. { operand size describing an signed value in a pair of int registers }
  418. OS_SPAIR = OS_S16;
  419. {$endif}
  420. { Table to convert tcgsize variables to the correspondending
  421. unsigned types }
  422. tcgsize2unsigned : array[tcgsize] of tcgsize = (OS_NO,
  423. OS_8, OS_16, OS_32, OS_64, OS_128,
  424. OS_8, OS_16, OS_32, OS_64, OS_128,
  425. OS_F32, OS_F64, OS_F80, OS_C64, OS_F128,
  426. OS_M8, OS_M16, OS_M32, OS_M64, OS_M128, OS_M256, OS_M512);
  427. tcgsize2signed : array[tcgsize] of tcgsize = (OS_NO,
  428. OS_S8, OS_S16, OS_S32, OS_S64, OS_S128,
  429. OS_S8, OS_S16, OS_S32, OS_S64, OS_S128,
  430. OS_F32, OS_F64, OS_F80, OS_C64, OS_F128,
  431. OS_M8, OS_M16, OS_M32, OS_M64, OS_M128, OS_M256,OS_M512);
  432. tcgloc2str : array[TCGLoc] of string[12] = (
  433. 'LOC_INVALID',
  434. 'LOC_VOID',
  435. 'LOC_CONST',
  436. 'LOC_JUMP',
  437. 'LOC_FLAGS',
  438. 'LOC_REG',
  439. 'LOC_CREG',
  440. 'LOC_FPUREG',
  441. 'LOC_CFPUREG',
  442. 'LOC_MMXREG',
  443. 'LOC_CMMXREG',
  444. 'LOC_MMREG',
  445. 'LOC_CMMREG',
  446. 'LOC_SSETREG',
  447. 'LOC_CSSETREG',
  448. 'LOC_SSETREF',
  449. 'LOC_CSSETREF',
  450. 'LOC_CREF',
  451. 'LOC_REF'
  452. );
  453. var
  454. mms_movescalar,
  455. mms_variable,
  456. mms_2,
  457. mms_4,
  458. mms_8,
  459. mms_16,
  460. mms_32 : pmmshuffle;
  461. procedure supregset_reset(out regs:tsuperregisterset;setall:boolean;
  462. maxreg:Tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  463. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  464. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  465. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  466. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  467. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  468. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  469. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  470. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  471. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  472. function generic_regname(r:tregister):string;
  473. {# From a constant numeric value, return the abstract code generator
  474. size.
  475. }
  476. function int_cgsize(const a: tcgint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  477. function int_float_cgsize(const a: tcgint): tcgsize;
  478. function float_array_cgsize(const a: tcgint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  479. function double_array_cgsize(const a: tcgint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  480. function tcgsize2str(cgsize: tcgsize):string;
  481. function topcg2str(opcg: topcg):string;
  482. function topcmp2str(opcmp: topcmp):string;
  483. { return the inverse condition of opcmp }
  484. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  485. { return the opcmp needed when swapping the operands }
  486. function swap_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  487. { return whether op is commutative }
  488. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  489. { returns true, if shuffle describes a real shuffle operation and not only a move }
  490. function realshuffle(shuffle : pmmshuffle) : boolean;
  491. { returns true, if the shuffle describes only a move of the scalar at index 0 }
  492. function shufflescalar(shuffle : pmmshuffle) : boolean;
  493. { removes shuffling from shuffle, this means that the destenation index of each shuffle is copied to
  494. the source }
  495. procedure removeshuffles(var shuffle : tmmshuffle);
  496. function is_float_cgsize(size: tcgsize): boolean;{$ifdef USEINLINE}inline;{$endif}
  497. implementation
  498. uses
  499. verbose,
  500. cutils;
  501. {******************************************************************************
  502. tsuperregisterworklist
  503. ******************************************************************************}
  504. constructor tsuperregisterworklist.init;
  505. begin
  506. length:=0;
  507. buflength:=0;
  508. buflengthinc:=16;
  509. buf:=nil;
  510. end;
  511. constructor Tsuperregisterworklist.copyfrom(const x:Tsuperregisterworklist);
  512. begin
  513. // self.x cannot be used, we'd copy over the dyn array
  514. buflength:=x.buflength;
  515. buflengthinc:=x.buflengthinc;
  516. length:=x.length;
  517. if x.buf<>nil then
  518. begin
  519. setlength(buf,buflength);
  520. move(x.buf[0],buf[0],length*sizeof(Tsuperregister));
  521. end;
  522. end;
  523. destructor tsuperregisterworklist.done;
  524. begin
  525. buf:=nil;
  526. end;
  527. procedure tsuperregisterworklist.add(s:tsuperregister);
  528. begin
  529. inc(length);
  530. { Need to increase buffer length? }
  531. if length>=buflength then
  532. begin
  533. inc(buflength,buflengthinc);
  534. buflengthinc:=buflengthinc*2;
  535. if buflengthinc>256 then
  536. buflengthinc:=256;
  537. setlength(buf,buflength);
  538. end;
  539. buf[length-1]:=s;
  540. end;
  541. function tsuperregisterworklist.addnodup(s:tsuperregister): boolean;
  542. begin
  543. addnodup := false;
  544. if (length=0) or (indexword(buf[0],length,s) = -1) then
  545. begin
  546. add(s);
  547. addnodup := true;
  548. end;
  549. end;
  550. procedure tsuperregisterworklist.clear;
  551. begin
  552. length:=0;
  553. end;
  554. procedure tsuperregisterworklist.deleteidx(i:word);
  555. begin
  556. if i>=length then
  557. internalerror(200310144);
  558. buf[i]:=buf[length-1];
  559. dec(length);
  560. end;
  561. function tsuperregisterworklist.readidx(i:word):tsuperregister;
  562. begin
  563. if (i >= length) then
  564. internalerror(2005010601);
  565. result := buf[i];
  566. end;
  567. function tsuperregisterworklist.get:tsuperregister;
  568. begin
  569. if length=0 then
  570. internalerror(200310142);
  571. dec(length);
  572. get:=buf[length];
  573. end;
  574. function tsuperregisterworklist.delete(s:tsuperregister):boolean;
  575. var
  576. i:longint;
  577. begin
  578. delete:=false;
  579. if (system.length(buf)=0) then
  580. exit;
  581. { indexword in 1.0.x and 1.9.4 is broken }
  582. i:=indexword(buf[0],length,s);
  583. if i<>-1 then
  584. begin
  585. deleteidx(i);
  586. delete := true;
  587. end;
  588. end;
  589. procedure supregset_reset(out regs:tsuperregisterset;setall:boolean;
  590. maxreg:Tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  591. begin
  592. fillchar(regs,(maxreg+7) shr 3,-byte(setall));
  593. end;
  594. procedure supregset_include(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  595. begin
  596. include(regs[s shr 8],(s and $ff));
  597. end;
  598. procedure supregset_exclude(var regs:tsuperregisterset;s:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  599. begin
  600. exclude(regs[s shr 8],(s and $ff));
  601. end;
  602. function supregset_in(const regs:tsuperregisterset;s:tsuperregister):boolean;{$ifdef USEINLINE}inline;{$endif}
  603. begin
  604. result:=(s and $ff) in regs[s shr 8];
  605. end;
  606. function newreg(rt:tregistertype;sr:tsuperregister;sb:tsubregister):tregister;{$ifdef USEINLINE}inline;{$endif}
  607. begin
  608. tregisterrec(result).regtype:=rt;
  609. tregisterrec(result).supreg:=sr;
  610. tregisterrec(result).subreg:=sb;
  611. end;
  612. function getsubreg(r:tregister):tsubregister;{$ifdef USEINLINE}inline;{$endif}
  613. begin
  614. result:=tregisterrec(r).subreg;
  615. end;
  616. function getsupreg(r:tregister):tsuperregister;{$ifdef USEINLINE}inline;{$endif}
  617. begin
  618. result:=tregisterrec(r).supreg;
  619. end;
  620. function getregtype(r:tregister):tregistertype;{$ifdef USEINLINE}inline;{$endif}
  621. begin
  622. result:=tregisterrec(r).regtype;
  623. end;
  624. procedure setsubreg(var r:tregister;sr:tsubregister);{$ifdef USEINLINE}inline;{$endif}
  625. begin
  626. tregisterrec(r).subreg:=sr;
  627. end;
  628. procedure setsupreg(var r:tregister;sr:tsuperregister);{$ifdef USEINLINE}inline;{$endif}
  629. begin
  630. tregisterrec(r).supreg:=sr;
  631. end;
  632. function generic_regname(r:tregister):string;
  633. var
  634. nr : string[12];
  635. begin
  636. str(getsupreg(r),nr);
  637. case getregtype(r) of
  638. R_INTREGISTER:
  639. result:='ireg'+nr;
  640. R_FPUREGISTER:
  641. result:='freg'+nr;
  642. R_MMREGISTER:
  643. result:='mreg'+nr;
  644. R_MMXREGISTER:
  645. result:='xreg'+nr;
  646. R_ADDRESSREGISTER:
  647. result:='areg'+nr;
  648. R_SPECIALREGISTER:
  649. result:='sreg'+nr;
  650. else
  651. begin
  652. result:='INVALID';
  653. exit;
  654. end;
  655. end;
  656. case getsubreg(r) of
  657. R_SUBNONE:
  658. ;
  659. R_SUBL:
  660. result:=result+'l';
  661. R_SUBH:
  662. result:=result+'h';
  663. R_SUBW:
  664. result:=result+'w';
  665. R_SUBD:
  666. result:=result+'d';
  667. R_SUBQ:
  668. result:=result+'q';
  669. R_SUBFS:
  670. result:=result+'fs';
  671. R_SUBFD:
  672. result:=result+'fd';
  673. R_SUBMMD:
  674. result:=result+'md';
  675. R_SUBMMS:
  676. result:=result+'ms';
  677. R_SUBMMWHOLE:
  678. result:=result+'ma';
  679. R_SUBMMX:
  680. result:=result+'mx';
  681. R_SUBMMY:
  682. result:=result+'my';
  683. R_SUBMMZ:
  684. result:=result+'mz';
  685. {$ifdef aarch64}
  686. R_SUBMM8B:
  687. result:=result+'m8b';
  688. R_SUBMM16B:
  689. result:=result+'m16b';
  690. R_SUBMM4H:
  691. result:=result+'m4h';
  692. R_SUBMM8H:
  693. result:=result+'m8h';
  694. R_SUBMM2S:
  695. result:=result+'m2s';
  696. R_SUBMM4S:
  697. result:=result+'m4s';
  698. R_SUBMM2D:
  699. result:=result+'m2d';
  700. R_SUBMMB1:
  701. result:=result+'mb1';
  702. R_SUBMMH1:
  703. result:=result+'mh1';
  704. R_SUBMMS1:
  705. result:=result+'ms1';
  706. R_SUBMMD1:
  707. result:=result+'md1';
  708. {$endif}
  709. else
  710. internalerror(200308252);
  711. end;
  712. end;
  713. function int_cgsize(const a: tcgint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  714. const
  715. size2cgsize : array[0..8] of tcgsize = (
  716. OS_NO,OS_8,OS_16,OS_NO,OS_32,OS_NO,OS_NO,OS_NO,OS_64
  717. );
  718. begin
  719. {$ifdef cpu64bitalu}
  720. if a=16 then
  721. result:=OS_128
  722. else
  723. {$endif cpu64bitalu}
  724. if a>8 then
  725. result:=OS_NO
  726. else
  727. result:=size2cgsize[a];
  728. end;
  729. function int_float_cgsize(const a: tcgint): tcgsize;
  730. begin
  731. case a of
  732. 4 :
  733. result:=OS_F32;
  734. 8 :
  735. result:=OS_F64;
  736. 10 :
  737. result:=OS_F80;
  738. 16 :
  739. result:=OS_F128;
  740. else
  741. internalerror(200603211);
  742. end;
  743. end;
  744. function float_array_cgsize(const a: tcgint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  745. begin
  746. case a of
  747. 4:
  748. result := OS_M32;
  749. 16:
  750. result := OS_M128;
  751. 32:
  752. result := OS_M256;
  753. 64:
  754. result := OS_M512;
  755. else
  756. result := int_cgsize(a);
  757. end;
  758. end;
  759. function double_array_cgsize(const a: tcgint): tcgsize;{$ifdef USEINLINE}inline;{$endif}
  760. begin
  761. case a of
  762. 8:
  763. result := OS_M64;
  764. 16:
  765. result := OS_M128;
  766. 32:
  767. result := OS_M256;
  768. 64:
  769. result := OS_M512;
  770. else
  771. result := int_cgsize(a);
  772. end;
  773. end;
  774. function tcgsize2str(cgsize: tcgsize):string;
  775. begin
  776. Str(cgsize, Result);
  777. end;
  778. function topcg2str(opcg: topcg):string;
  779. begin
  780. Str(opcg, Result);
  781. end;
  782. function topcmp2str(opcmp: topcmp):string;
  783. begin
  784. Str(opcmp, Result);
  785. end;
  786. function inverse_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  787. const
  788. list: array[TOpCmp] of TOpCmp =
  789. (OC_NONE,OC_NE,OC_LTE,OC_GTE,OC_LT,OC_GT,OC_EQ,OC_A,OC_AE,
  790. OC_B,OC_BE);
  791. begin
  792. inverse_opcmp := list[opcmp];
  793. end;
  794. function swap_opcmp(opcmp: topcmp): topcmp;{$ifdef USEINLINE}inline;{$endif}
  795. const
  796. list: array[TOpCmp] of TOpCmp =
  797. (OC_NONE,OC_EQ,OC_LT,OC_GT,OC_LTE,OC_GTE,OC_NE,OC_AE,OC_A,
  798. OC_BE,OC_B);
  799. begin
  800. swap_opcmp := list[opcmp];
  801. end;
  802. function commutativeop(op: topcg): boolean;{$ifdef USEINLINE}inline;{$endif}
  803. const
  804. list: array[topcg] of boolean =
  805. (true,false,true,true,false,false,true,true,false,false,
  806. true,false,false,false,false,true,false,false);
  807. begin
  808. commutativeop := list[op];
  809. end;
  810. function realshuffle(shuffle : pmmshuffle) : boolean;
  811. var
  812. i : longint;
  813. begin
  814. realshuffle:=true;
  815. if (shuffle=nil) or (shuffle^.len<1) then
  816. realshuffle:=false
  817. else
  818. begin
  819. for i:=1 to shuffle^.len do
  820. begin
  821. if (shuffle^.shuffles[i] and $ff)<>((shuffle^.shuffles[i] and $ff00) shr 8) then
  822. exit;
  823. end;
  824. realshuffle:=false;
  825. end;
  826. end;
  827. function shufflescalar(shuffle : pmmshuffle) : boolean;
  828. begin
  829. result:=shuffle^.len=0;
  830. end;
  831. procedure removeshuffles(var shuffle : tmmshuffle);
  832. var
  833. i : longint;
  834. begin
  835. if shuffle.len=0 then
  836. exit;
  837. for i:=1 to shuffle.len do
  838. shuffle.shuffles[i]:=(shuffle.shuffles[i] and $f) or ((shuffle.shuffles[i] and $f0) shr 4);
  839. end;
  840. function is_float_cgsize(size: tcgsize): boolean;{$ifdef USEINLINE}inline;{$endif}
  841. begin
  842. result:=size in [OS_F32..OS_F128];
  843. end;
  844. procedure Initmms(var p : pmmshuffle;len : ShortInt);
  845. var
  846. i : Integer;
  847. begin
  848. Getmem(p,sizeof(tmmshuffle)+(max(len,0)-1)*2);
  849. p^.len:=len;
  850. for i:=1 to len do
  851. {$push}
  852. {$R-}
  853. p^.shuffles[i]:=i;
  854. {$pop}
  855. end;
  856. initialization
  857. Initmms(mms_movescalar,0);
  858. Initmms(mms_variable,-1);
  859. Initmms(mms_2,2);
  860. Initmms(mms_4,4);
  861. Initmms(mms_8,8);
  862. Initmms(mms_16,16);
  863. Initmms(mms_32,32);
  864. finalization
  865. Freemem(mms_movescalar);
  866. Freemem(mms_variable);
  867. Freemem(mms_2);
  868. Freemem(mms_4);
  869. Freemem(mms_8);
  870. Freemem(mms_16);
  871. Freemem(mms_32);
  872. end.