cgbase.pas 32 KB

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