defutil.pas 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. {
  2. Copyright (c) 1998-2006 by Florian Klaempfl
  3. This unit provides some help routines for type handling
  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. unit defutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,constexp,
  23. symconst,symbase,symtype,symdef,
  24. cgbase,cpubase;
  25. type
  26. tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
  27. mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle);
  28. {*****************************************************************************
  29. Basic type functions
  30. *****************************************************************************}
  31. {# Returns true, if definition defines an ordinal type }
  32. function is_ordinal(def : tdef) : boolean;
  33. {# Returns the minimal integer value of the type }
  34. function get_min_value(def : tdef) : TConstExprInt;
  35. {# Returns the maximal integer value of the type }
  36. function get_max_value(def : tdef) : TConstExprInt;
  37. {# Returns basetype of the specified integer range }
  38. function range_to_basetype(l,h:TConstExprInt):tordtype;
  39. procedure range_to_type(l,h:TConstExprInt;var def:tdef);
  40. procedure int_to_type(v:TConstExprInt;var def:tdef);
  41. {# Returns true, if definition defines an integer type }
  42. function is_integer(def : tdef) : boolean;
  43. {# Returns true if definition is a boolean }
  44. function is_boolean(def : tdef) : boolean;
  45. {# Returns true if definition is a Pascal-style boolean (1 = true, zero = false) }
  46. function is_pasbool(def : tdef) : boolean;
  47. {# Returns true if definition is a C-style boolean (non-zero value = true, zero = false) }
  48. function is_cbool(def : tdef) : boolean;
  49. {# Returns true if definition is a char
  50. This excludes the unicode char.
  51. }
  52. function is_char(def : tdef) : boolean;
  53. {# Returns true if definition is a widechar }
  54. function is_widechar(def : tdef) : boolean;
  55. {# Returns true if definition is a void}
  56. function is_void(def : tdef) : boolean;
  57. {# Returns true if definition is a smallset}
  58. function is_smallset(p : tdef) : boolean;
  59. {# Returns true, if def defines a signed data type
  60. (only for ordinal types)
  61. }
  62. function is_signed(def : tdef) : boolean;
  63. {# Returns true whether def_from's range is comprised in def_to's if both are
  64. orddefs, false otherwise }
  65. function is_in_limit(def_from,def_to : tdef) : boolean;
  66. { function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;}
  67. {*****************************************************************************
  68. Array helper functions
  69. *****************************************************************************}
  70. {# Returns true, if p points to a zero based (non special like open or
  71. dynamic array def).
  72. This is mainly used to see if the array
  73. is convertable to a pointer
  74. }
  75. function is_zero_based_array(p : tdef) : boolean;
  76. {# Returns true if p points to an open array definition }
  77. function is_open_array(p : tdef) : boolean;
  78. {# Returns true if p points to a dynamic array definition }
  79. function is_dynamic_array(p : tdef) : boolean;
  80. {# Returns true, if p points to an array of const definition }
  81. function is_array_constructor(p : tdef) : boolean;
  82. {# Returns true, if p points to a variant array }
  83. function is_variant_array(p : tdef) : boolean;
  84. {# Returns true, if p points to an array of const }
  85. function is_array_of_const(p : tdef) : boolean;
  86. {# Returns true, if p points any kind of special array
  87. That is if the array is an open array, a variant
  88. array, an array constants constructor, or an
  89. array of const.
  90. Bitpacked arrays aren't special in this regard though.
  91. }
  92. function is_special_array(p : tdef) : boolean;
  93. {# Returns true if p is a bitpacked array }
  94. function is_packed_array(p: tdef) : boolean;
  95. {# Returns true if p is a bitpacked record }
  96. function is_packed_record_or_object(p: tdef) : boolean;
  97. {# Returns true if p is a char array def }
  98. function is_chararray(p : tdef) : boolean;
  99. {# Returns true if p is a wide char array def }
  100. function is_widechararray(p : tdef) : boolean;
  101. {# Returns true if p is a open char array def }
  102. function is_open_chararray(p : tdef) : boolean;
  103. {# Returns true if p is a open wide char array def }
  104. function is_open_widechararray(p : tdef) : boolean;
  105. {*****************************************************************************
  106. String helper functions
  107. *****************************************************************************}
  108. {# Returns true if p points to an open string type }
  109. function is_open_string(p : tdef) : boolean;
  110. {# Returns true if p is an ansi string type }
  111. function is_ansistring(p : tdef) : boolean;
  112. {# Returns true if p is a long string type }
  113. function is_longstring(p : tdef) : boolean;
  114. {# returns true if p is a wide string type }
  115. function is_widestring(p : tdef) : boolean;
  116. {# Returns true if p is a short string type }
  117. function is_shortstring(p : tdef) : boolean;
  118. {# Returns true if p is a pchar def }
  119. function is_pchar(p : tdef) : boolean;
  120. {# Returns true if p is a pwidechar def }
  121. function is_pwidechar(p : tdef) : boolean;
  122. {# Returns true if p is a voidpointer def }
  123. function is_voidpointer(p : tdef) : boolean;
  124. {# Returns true, if definition is a float }
  125. function is_fpu(def : tdef) : boolean;
  126. {# Returns true, if def is a currency type }
  127. function is_currency(def : tdef) : boolean;
  128. {# Returns true, if def is a single type }
  129. function is_single(def : tdef) : boolean;
  130. {# Returns true, if def is a double type }
  131. function is_double(def : tdef) : boolean;
  132. {# Returns true, if def is an extended type }
  133. function is_extended(def : tdef) : boolean;
  134. {# Returns true, if definition is a "real" real (i.e. single/double/extended) }
  135. function is_real(def : tdef) : boolean;
  136. {# Returns true, if def is a 32 bit integer type }
  137. function is_32bitint(def : tdef) : boolean;
  138. {# Returns true, if def is a 64 bit integer type }
  139. function is_64bitint(def : tdef) : boolean;
  140. {# Returns true, if def is a 64 bit type }
  141. function is_64bit(def : tdef) : boolean;
  142. {# If @var(l) isn't in the range of todef a range check error (if not explicit) is generated and
  143. the value is placed within the range
  144. }
  145. procedure testrange(todef : tdef;var l : tconstexprint;explicit:boolean);
  146. {# Returns the range of def, where @var(l) is the low-range and @var(h) is
  147. the high-range.
  148. }
  149. procedure getrange(def : tdef;out l, h : TConstExprInt);
  150. { type being a vector? }
  151. function is_vector(p : tdef) : boolean;
  152. { some type helper routines for MMX support }
  153. function is_mmx_able_array(p : tdef) : boolean;
  154. {# returns the mmx type }
  155. function mmx_type(p : tdef) : tmmxtype;
  156. { returns if the passed type (array) fits into an mm register }
  157. function fits_in_mm_register(p : tdef) : boolean;
  158. {# From a definition return the abstract code generator size enum. It is
  159. to note that the value returned can be @var(OS_NO) }
  160. function def_cgsize(def: tdef): tcgsize;
  161. {# returns true, if the type passed is can be used with windows automation }
  162. function is_automatable(p : tdef) : boolean;
  163. { # returns true if the procdef has no parameters and no specified return type }
  164. function is_bareprocdef(pd : tprocdef): boolean;
  165. implementation
  166. uses
  167. systems,verbose;
  168. { returns true, if def uses FPU }
  169. function is_fpu(def : tdef) : boolean;
  170. begin
  171. is_fpu:=(def.typ=floatdef);
  172. end;
  173. { returns true, if def is a currency type }
  174. function is_currency(def : tdef) : boolean;
  175. begin
  176. case s64currencytype.typ of
  177. orddef :
  178. result:=(def.typ=orddef) and
  179. (torddef(s64currencytype).ordtype=torddef(def).ordtype);
  180. floatdef :
  181. result:=(def.typ=floatdef) and
  182. (tfloatdef(s64currencytype).floattype=tfloatdef(def).floattype);
  183. else
  184. internalerror(200304222);
  185. end;
  186. end;
  187. { returns true, if def is a single type }
  188. function is_single(def : tdef) : boolean;
  189. begin
  190. result:=(def.typ=floatdef) and
  191. (tfloatdef(def).floattype=s32real);
  192. end;
  193. { returns true, if def is a double type }
  194. function is_double(def : tdef) : boolean;
  195. begin
  196. result:=(def.typ=floatdef) and
  197. (tfloatdef(def).floattype=s64real);
  198. end;
  199. function is_extended(def : tdef) : boolean;
  200. begin
  201. result:=(def.typ=floatdef) and
  202. (tfloatdef(def).floattype=s80real);
  203. end;
  204. { returns true, if definition is a "real" real (i.e. single/double/extended) }
  205. function is_real(def : tdef) : boolean;
  206. begin
  207. result:=(def.typ=floatdef) and
  208. (tfloatdef(def).floattype in [s32real,s64real,s80real]);
  209. end;
  210. function range_to_basetype(l,h:TConstExprInt):tordtype;
  211. begin
  212. { prefer signed over unsigned }
  213. if (l>=int64(-128)) and (h<=127) then
  214. range_to_basetype:=s8bit
  215. else if (l>=0) and (h<=255) then
  216. range_to_basetype:=u8bit
  217. else if (l>=int64(-32768)) and (h<=32767) then
  218. range_to_basetype:=s16bit
  219. else if (l>=0) and (h<=65535) then
  220. range_to_basetype:=u16bit
  221. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  222. range_to_basetype:=s32bit
  223. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  224. range_to_basetype:=u32bit
  225. else
  226. range_to_basetype:=s64bit;
  227. end;
  228. procedure range_to_type(l,h:TConstExprInt;var def:tdef);
  229. begin
  230. { prefer signed over unsigned }
  231. if (l>=int64(-128)) and (h<=127) then
  232. def:=s8inttype
  233. else if (l>=0) and (h<=255) then
  234. def:=u8inttype
  235. else if (l>=int64(-32768)) and (h<=32767) then
  236. def:=s16inttype
  237. else if (l>=0) and (h<=65535) then
  238. def:=u16inttype
  239. else if (l>=int64(low(longint))) and (h<=high(longint)) then
  240. def:=s32inttype
  241. else if (l>=low(cardinal)) and (h<=high(cardinal)) then
  242. def:=u32inttype
  243. else if (l>=low(int64)) and (h<=high(int64)) then
  244. def:=s64inttype
  245. else
  246. def:=u64inttype;
  247. end;
  248. procedure int_to_type(v:TConstExprInt;var def:tdef);
  249. begin
  250. range_to_type(v,v,def);
  251. end;
  252. { true if p is an ordinal }
  253. function is_ordinal(def : tdef) : boolean;
  254. var
  255. dt : tordtype;
  256. begin
  257. case def.typ of
  258. orddef :
  259. begin
  260. dt:=torddef(def).ordtype;
  261. is_ordinal:=dt in [uchar,uwidechar,
  262. u8bit,u16bit,u32bit,u64bit,
  263. s8bit,s16bit,s32bit,s64bit,
  264. pasbool,bool8bit,bool16bit,bool32bit,bool64bit];
  265. end;
  266. enumdef :
  267. is_ordinal:=true;
  268. else
  269. is_ordinal:=false;
  270. end;
  271. end;
  272. { returns the min. value of the type }
  273. function get_min_value(def : tdef) : TConstExprInt;
  274. begin
  275. case def.typ of
  276. orddef:
  277. result:=torddef(def).low;
  278. enumdef:
  279. result:=int64(tenumdef(def).min);
  280. else
  281. result:=0;
  282. end;
  283. end;
  284. { returns the max. value of the type }
  285. function get_max_value(def : tdef) : TConstExprInt;
  286. begin
  287. case def.typ of
  288. orddef:
  289. result:=torddef(def).high;
  290. enumdef:
  291. result:=tenumdef(def).max;
  292. else
  293. result:=0;
  294. end;
  295. end;
  296. { true if p is an integer }
  297. function is_integer(def : tdef) : boolean;
  298. begin
  299. result:=(def.typ=orddef) and
  300. (torddef(def).ordtype in [u8bit,u16bit,u32bit,u64bit,
  301. s8bit,s16bit,s32bit,s64bit]);
  302. end;
  303. { true if p is a boolean }
  304. function is_boolean(def : tdef) : boolean;
  305. begin
  306. result:=(def.typ=orddef) and
  307. (torddef(def).ordtype in [pasbool,bool8bit,bool16bit,bool32bit,bool64bit]);
  308. end;
  309. function is_pasbool(def : tdef) : boolean;
  310. begin
  311. result:=(def.typ=orddef) and
  312. (torddef(def).ordtype = pasbool);
  313. end;
  314. { true if def is a C-style boolean (non-zero value = true, zero = false) }
  315. function is_cbool(def : tdef) : boolean;
  316. begin
  317. result:=(def.typ=orddef) and
  318. (torddef(def).ordtype in [bool8bit,bool16bit,bool32bit,bool64bit]);
  319. end;
  320. { true if p is a void }
  321. function is_void(def : tdef) : boolean;
  322. begin
  323. result:=(def.typ=orddef) and
  324. (torddef(def).ordtype=uvoid);
  325. end;
  326. { true if p is a char }
  327. function is_char(def : tdef) : boolean;
  328. begin
  329. result:=(def.typ=orddef) and
  330. (torddef(def).ordtype=uchar);
  331. end;
  332. { true if p is a wchar }
  333. function is_widechar(def : tdef) : boolean;
  334. begin
  335. result:=(def.typ=orddef) and
  336. (torddef(def).ordtype=uwidechar);
  337. end;
  338. { true if p is signed (integer) }
  339. function is_signed(def : tdef) : boolean;
  340. begin
  341. case def.typ of
  342. orddef :
  343. result:=torddef(def).low < 0;
  344. enumdef :
  345. result:=tenumdef(def).min < 0;
  346. arraydef :
  347. result:=is_signed(tarraydef(def).rangedef);
  348. else
  349. result:=false;
  350. end;
  351. end;
  352. function is_in_limit(def_from,def_to : tdef) : boolean;
  353. begin
  354. if (def_from.typ<>def_to.typ) or
  355. not(def_from.typ in [orddef,enumdef,setdef]) then
  356. begin
  357. is_in_limit := false;
  358. exit;
  359. end;
  360. case def_from.typ of
  361. orddef:
  362. is_in_limit:=(torddef(def_from).low>=torddef(def_to).low) and
  363. (torddef(def_from).high<=torddef(def_to).high);
  364. enumdef:
  365. is_in_limit:=(tenumdef(def_from).min>=tenumdef(def_to).min) and
  366. (tenumdef(def_from).max<=tenumdef(def_to).max);
  367. setdef:
  368. is_in_limit:=(tsetdef(def_from).setbase>=tsetdef(def_to).setbase) and
  369. (tsetdef(def_from).setmax<=tsetdef(def_to).setmax);
  370. end;
  371. end;
  372. { true, if p points to an open array def }
  373. function is_open_string(p : tdef) : boolean;
  374. begin
  375. is_open_string:=(p.typ=stringdef) and
  376. (tstringdef(p).stringtype=st_shortstring) and
  377. (tstringdef(p).len=0);
  378. end;
  379. { true, if p points to a zero based array def }
  380. function is_zero_based_array(p : tdef) : boolean;
  381. begin
  382. result:=(p.typ=arraydef) and
  383. (tarraydef(p).lowrange=0) and
  384. not(is_special_array(p));
  385. end;
  386. { true if p points to a dynamic array def }
  387. function is_dynamic_array(p : tdef) : boolean;
  388. begin
  389. result:=(p.typ=arraydef) and
  390. (ado_IsDynamicArray in tarraydef(p).arrayoptions);
  391. end;
  392. { true, if p points to an open array def }
  393. function is_open_array(p : tdef) : boolean;
  394. begin
  395. { check for s32inttype is needed, because for u32bit the high
  396. range is also -1 ! (PFV) }
  397. result:=(p.typ=arraydef) and
  398. (tarraydef(p).rangedef=s32inttype) and
  399. (tarraydef(p).lowrange=0) and
  400. (tarraydef(p).highrange=-1) and
  401. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])=[]);
  402. end;
  403. { true, if p points to an array of const def }
  404. function is_array_constructor(p : tdef) : boolean;
  405. begin
  406. result:=(p.typ=arraydef) and
  407. (ado_IsConstructor in tarraydef(p).arrayoptions);
  408. end;
  409. { true, if p points to a variant array }
  410. function is_variant_array(p : tdef) : boolean;
  411. begin
  412. result:=(p.typ=arraydef) and
  413. (ado_IsVariant in tarraydef(p).arrayoptions);
  414. end;
  415. { true, if p points to an array of const }
  416. function is_array_of_const(p : tdef) : boolean;
  417. begin
  418. result:=(p.typ=arraydef) and
  419. (ado_IsArrayOfConst in tarraydef(p).arrayoptions);
  420. end;
  421. { true, if p points to a special array, bitpacked arrays aren't special in this regard though }
  422. function is_special_array(p : tdef) : boolean;
  423. begin
  424. result:=(p.typ=arraydef) and
  425. (
  426. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])<>[]) or
  427. is_open_array(p)
  428. );
  429. end;
  430. { true if p is an ansi string def }
  431. function is_ansistring(p : tdef) : boolean;
  432. begin
  433. is_ansistring:=(p.typ=stringdef) and
  434. (tstringdef(p).stringtype=st_ansistring);
  435. end;
  436. { true if p is an long string def }
  437. function is_longstring(p : tdef) : boolean;
  438. begin
  439. is_longstring:=(p.typ=stringdef) and
  440. (tstringdef(p).stringtype=st_longstring);
  441. end;
  442. { true if p is an wide string def }
  443. function is_widestring(p : tdef) : boolean;
  444. begin
  445. is_widestring:=(p.typ=stringdef) and
  446. (tstringdef(p).stringtype=st_widestring);
  447. end;
  448. { true if p is an short string def }
  449. function is_shortstring(p : tdef) : boolean;
  450. begin
  451. is_shortstring:=(p.typ=stringdef) and
  452. (tstringdef(p).stringtype=st_shortstring);
  453. end;
  454. { true if p is bit packed array def }
  455. function is_packed_array(p: tdef) : boolean;
  456. begin
  457. is_packed_array :=
  458. (p.typ = arraydef) and
  459. (ado_IsBitPacked in tarraydef(p).arrayoptions);
  460. end;
  461. { true if p is bit packed record def }
  462. function is_packed_record_or_object(p: tdef) : boolean;
  463. begin
  464. is_packed_record_or_object :=
  465. (p.typ in [recorddef,objectdef]) and
  466. (tabstractrecorddef(p).is_packed);
  467. end;
  468. { true if p is a char array def }
  469. function is_chararray(p : tdef) : boolean;
  470. begin
  471. is_chararray:=(p.typ=arraydef) and
  472. is_char(tarraydef(p).elementdef) and
  473. not(is_special_array(p));
  474. end;
  475. { true if p is a widechar array def }
  476. function is_widechararray(p : tdef) : boolean;
  477. begin
  478. is_widechararray:=(p.typ=arraydef) and
  479. is_widechar(tarraydef(p).elementdef) and
  480. not(is_special_array(p));
  481. end;
  482. { true if p is a open char array def }
  483. function is_open_chararray(p : tdef) : boolean;
  484. begin
  485. is_open_chararray:= is_open_array(p) and
  486. is_char(tarraydef(p).elementdef);
  487. end;
  488. { true if p is a open wide char array def }
  489. function is_open_widechararray(p : tdef) : boolean;
  490. begin
  491. is_open_widechararray:= is_open_array(p) and
  492. is_widechar(tarraydef(p).elementdef);
  493. end;
  494. { true if p is a pchar def }
  495. function is_pchar(p : tdef) : boolean;
  496. begin
  497. is_pchar:=(p.typ=pointerdef) and
  498. (is_char(tpointerdef(p).pointeddef) or
  499. (is_zero_based_array(tpointerdef(p).pointeddef) and
  500. is_chararray(tpointerdef(p).pointeddef)));
  501. end;
  502. { true if p is a pchar def }
  503. function is_pwidechar(p : tdef) : boolean;
  504. begin
  505. is_pwidechar:=(p.typ=pointerdef) and
  506. (is_widechar(tpointerdef(p).pointeddef) or
  507. (is_zero_based_array(tpointerdef(p).pointeddef) and
  508. is_widechararray(tpointerdef(p).pointeddef)));
  509. end;
  510. { true if p is a voidpointer def }
  511. function is_voidpointer(p : tdef) : boolean;
  512. begin
  513. is_voidpointer:=(p.typ=pointerdef) and
  514. (tpointerdef(p).pointeddef.typ=orddef) and
  515. (torddef(tpointerdef(p).pointeddef).ordtype=uvoid);
  516. end;
  517. { true, if def is a 32 bit int type }
  518. function is_32bitint(def : tdef) : boolean;
  519. begin
  520. result:=(def.typ=orddef) and (torddef(def).ordtype in [u32bit,s32bit])
  521. end;
  522. { true, if def is a 64 bit int type }
  523. function is_64bitint(def : tdef) : boolean;
  524. begin
  525. is_64bitint:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit])
  526. end;
  527. { true, if def is a 64 bit type }
  528. function is_64bit(def : tdef) : boolean;
  529. begin
  530. is_64bit:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit,scurrency])
  531. end;
  532. { if l isn't in the range of todef a range check error (if not explicit) is generated and
  533. the value is placed within the range }
  534. procedure testrange(todef : tdef;var l : tconstexprint;explicit:boolean);
  535. var
  536. lv,hv: TConstExprInt;
  537. begin
  538. { for 64 bit types we need only to check if it is less than }
  539. { zero, if def is a qword node }
  540. getrange(todef,lv,hv);
  541. if (l<lv) or (l>hv) then
  542. begin
  543. if not explicit then
  544. begin
  545. if ((todef.typ=enumdef) and
  546. { delphi allows range check errors in
  547. enumeration type casts FK }
  548. not(m_delphi in current_settings.modeswitches)) or
  549. (cs_check_range in current_settings.localswitches) then
  550. Message(parser_e_range_check_error)
  551. else
  552. Message(parser_w_range_check_error);
  553. end;
  554. { Fix the value to fit in the allocated space for this type of variable }
  555. case longint(todef.size) of
  556. 1: l := l and $ff;
  557. 2: l := l and $ffff;
  558. 4: l := l and $ffffffff;
  559. end;
  560. {reset sign, i.e. converting -1 to qword changes the value to high(qword)}
  561. l.signed:=false;
  562. { do sign extension if necessary (JM) }
  563. if is_signed(todef) then
  564. begin
  565. case longint(todef.size) of
  566. 1: l.svalue := shortint(l.svalue);
  567. 2: l.svalue := smallint(l.svalue);
  568. 4: l.svalue := longint(l.svalue);
  569. end;
  570. l.signed:=true;
  571. end;
  572. end;
  573. end;
  574. { return the range from def in l and h }
  575. procedure getrange(def : tdef;out l, h : TConstExprInt);
  576. begin
  577. case def.typ of
  578. orddef :
  579. begin
  580. l:=torddef(def).low;
  581. h:=torddef(def).high;
  582. end;
  583. enumdef :
  584. begin
  585. l:=int64(tenumdef(def).min);
  586. h:=int64(tenumdef(def).max);
  587. end;
  588. arraydef :
  589. begin
  590. l:=int64(tarraydef(def).lowrange);
  591. h:=int64(tarraydef(def).highrange);
  592. end;
  593. else
  594. internalerror(200611054);
  595. end;
  596. end;
  597. function mmx_type(p : tdef) : tmmxtype;
  598. begin
  599. mmx_type:=mmxno;
  600. if is_mmx_able_array(p) then
  601. begin
  602. if tarraydef(p).elementdef.typ=floatdef then
  603. case tfloatdef(tarraydef(p).elementdef).floattype of
  604. s32real:
  605. mmx_type:=mmxsingle;
  606. end
  607. else
  608. case torddef(tarraydef(p).elementdef).ordtype of
  609. u8bit:
  610. mmx_type:=mmxu8bit;
  611. s8bit:
  612. mmx_type:=mmxs8bit;
  613. u16bit:
  614. mmx_type:=mmxu16bit;
  615. s16bit:
  616. mmx_type:=mmxs16bit;
  617. u32bit:
  618. mmx_type:=mmxu32bit;
  619. s32bit:
  620. mmx_type:=mmxs32bit;
  621. end;
  622. end;
  623. end;
  624. function is_vector(p : tdef) : boolean;
  625. begin
  626. result:=(p.typ=arraydef) and
  627. not(is_special_array(p)) and
  628. (tarraydef(p).elementdef.typ=floatdef) and
  629. (tfloatdef(tarraydef(p).elementdef).floattype in [s32real,s64real]);
  630. end;
  631. { returns if the passed type (array) fits into an mm register }
  632. function fits_in_mm_register(p : tdef) : boolean;
  633. begin
  634. {$ifdef x86}
  635. result:= is_vector(p) and
  636. (
  637. (tarraydef(p).elementdef.typ=floatdef) and
  638. (
  639. (tarraydef(p).lowrange=0) and
  640. (tarraydef(p).highrange=3) and
  641. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  642. )
  643. ) or
  644. (
  645. (tarraydef(p).elementdef.typ=floatdef) and
  646. (
  647. (tarraydef(p).lowrange=0) and
  648. (tarraydef(p).highrange=1) and
  649. (tfloatdef(tarraydef(p).elementdef).floattype=s64real)
  650. )
  651. );
  652. {$else x86}
  653. result:=false;
  654. {$endif x86}
  655. end;
  656. function is_mmx_able_array(p : tdef) : boolean;
  657. begin
  658. {$ifdef SUPPORT_MMX}
  659. if (cs_mmx_saturation in current_settings.localswitches) then
  660. begin
  661. is_mmx_able_array:=(p.typ=arraydef) and
  662. not(is_special_array(p)) and
  663. (
  664. (
  665. (tarraydef(p).elementdef.typ=orddef) and
  666. (
  667. (
  668. (tarraydef(p).lowrange=0) and
  669. (tarraydef(p).highrange=1) and
  670. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  671. )
  672. or
  673. (
  674. (tarraydef(p).lowrange=0) and
  675. (tarraydef(p).highrange=3) and
  676. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  677. )
  678. )
  679. )
  680. or
  681. (
  682. (
  683. (tarraydef(p).elementdef.typ=floatdef) and
  684. (
  685. (tarraydef(p).lowrange=0) and
  686. (tarraydef(p).highrange=1) and
  687. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  688. )
  689. )
  690. )
  691. );
  692. end
  693. else
  694. begin
  695. is_mmx_able_array:=(p.typ=arraydef) and
  696. (
  697. (
  698. (tarraydef(p).elementdef.typ=orddef) and
  699. (
  700. (
  701. (tarraydef(p).lowrange=0) and
  702. (tarraydef(p).highrange=1) and
  703. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  704. )
  705. or
  706. (
  707. (tarraydef(p).lowrange=0) and
  708. (tarraydef(p).highrange=3) and
  709. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  710. )
  711. or
  712. (
  713. (tarraydef(p).lowrange=0) and
  714. (tarraydef(p).highrange=7) and
  715. (torddef(tarraydef(p).elementdef).ordtype in [u8bit,s8bit])
  716. )
  717. )
  718. )
  719. or
  720. (
  721. (tarraydef(p).elementdef.typ=floatdef) and
  722. (
  723. (tarraydef(p).lowrange=0) and
  724. (tarraydef(p).highrange=1) and
  725. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  726. )
  727. )
  728. );
  729. end;
  730. {$else SUPPORT_MMX}
  731. is_mmx_able_array:=false;
  732. {$endif SUPPORT_MMX}
  733. end;
  734. function def_cgsize(def: tdef): tcgsize;
  735. begin
  736. case def.typ of
  737. orddef,
  738. enumdef,
  739. setdef:
  740. begin
  741. result:=int_cgsize(def.size);
  742. if is_signed(def) then
  743. result:=tcgsize(ord(result)+(ord(OS_S8)-ord(OS_8)));
  744. end;
  745. classrefdef,
  746. pointerdef:
  747. result := OS_ADDR;
  748. procvardef:
  749. begin
  750. if tprocvardef(def).is_methodpointer and
  751. (not tprocvardef(def).is_addressonly) then
  752. if (sizeof(pint) = 4) then
  753. result:=OS_64
  754. else if (sizeof(pint) = 8) then
  755. result:=OS_128
  756. else
  757. internalerror(200707141)
  758. else
  759. result:=OS_ADDR;
  760. end;
  761. stringdef :
  762. begin
  763. if is_ansistring(def) or is_widestring(def) then
  764. result := OS_ADDR
  765. else
  766. result:=int_cgsize(def.size);
  767. end;
  768. objectdef :
  769. begin
  770. if is_class_or_interface(def) then
  771. result := OS_ADDR
  772. else
  773. result:=int_cgsize(def.size);
  774. end;
  775. floatdef:
  776. if cs_fp_emulation in current_settings.moduleswitches then
  777. result:=int_cgsize(def.size)
  778. else
  779. result:=tfloat2tcgsize[tfloatdef(def).floattype];
  780. recorddef :
  781. result:=int_cgsize(def.size);
  782. arraydef :
  783. begin
  784. if not is_special_array(def) then
  785. result := int_cgsize(def.size)
  786. else
  787. begin
  788. if is_dynamic_array(def) then
  789. result := OS_ADDR
  790. else
  791. result := OS_NO;
  792. end;
  793. end;
  794. else
  795. begin
  796. { undefined size }
  797. result:=OS_NO;
  798. end;
  799. end;
  800. end;
  801. function is_automatable(p : tdef) : boolean;
  802. begin
  803. result:=false;
  804. case p.typ of
  805. orddef:
  806. result:=torddef(p).ordtype in [u8bit,s32bit,s16bit,bool16bit];
  807. floatdef:
  808. result:=tfloatdef(p).floattype in [s64currency,s64real,s32real];
  809. stringdef:
  810. result:=tstringdef(p).stringtype in [st_ansistring,st_widestring];
  811. variantdef:
  812. result:=true;
  813. arraydef:
  814. result:=(ado_IsConstString in tarraydef(p).arrayoptions);
  815. objectdef:
  816. result:=tobjectdef(p).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba];
  817. end;
  818. end;
  819. {# returns true, if the type passed is a varset }
  820. function is_smallset(p : tdef) : boolean;
  821. begin
  822. result:=(p.typ=setdef) and (p.size in [1,2,4])
  823. end;
  824. function is_bareprocdef(pd : tprocdef): boolean;
  825. begin
  826. result:=(pd.maxparacount=0) and
  827. (is_void(pd.returndef) or
  828. (pd.proctypeoption = potype_constructor));
  829. end;
  830. end.