defutil.pas 33 KB

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