defutil.pas 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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 type passed is a normalset }
  162. function is_normalset(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>=-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>=-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>=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>=-128) and (h<=127) then
  232. def:=s8inttype
  233. else if (l>=0) and (h<=255) then
  234. def:=u8inttype
  235. else if (l>=-32768) and (h<=32767) then
  236. def:=s16inttype
  237. else if (l>=0) and (h<=65535) then
  238. def:=u16inttype
  239. else if (l>=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
  244. def:=s64inttype;
  245. end;
  246. procedure int_to_type(v:TConstExprInt;var def:tdef);
  247. begin
  248. range_to_type(v,v,def);
  249. end;
  250. { true if p is an ordinal }
  251. function is_ordinal(def : tdef) : boolean;
  252. var
  253. dt : tordtype;
  254. begin
  255. case def.typ of
  256. orddef :
  257. begin
  258. dt:=torddef(def).ordtype;
  259. is_ordinal:=dt in [uchar,uwidechar,
  260. u8bit,u16bit,u32bit,u64bit,
  261. s8bit,s16bit,s32bit,s64bit,
  262. bool8bit,bool16bit,bool32bit,bool64bit];
  263. end;
  264. enumdef :
  265. is_ordinal:=true;
  266. else
  267. is_ordinal:=false;
  268. end;
  269. end;
  270. { returns the min. value of the type }
  271. function get_min_value(def : tdef) : TConstExprInt;
  272. begin
  273. case def.typ of
  274. orddef:
  275. result:=torddef(def).low;
  276. enumdef:
  277. result:=tenumdef(def).min;
  278. else
  279. result:=0;
  280. end;
  281. end;
  282. { returns the max. value of the type }
  283. function get_max_value(def : tdef) : TConstExprInt;
  284. begin
  285. case def.typ of
  286. orddef:
  287. result:=torddef(def).high;
  288. enumdef:
  289. result:=tenumdef(def).max;
  290. else
  291. result:=0;
  292. end;
  293. end;
  294. { true if p is an integer }
  295. function is_integer(def : tdef) : boolean;
  296. begin
  297. result:=(def.typ=orddef) and
  298. (torddef(def).ordtype in [u8bit,u16bit,u32bit,u64bit,
  299. s8bit,s16bit,s32bit,s64bit]);
  300. end;
  301. { true if p is a boolean }
  302. function is_boolean(def : tdef) : boolean;
  303. begin
  304. result:=(def.typ=orddef) and
  305. (torddef(def).ordtype in [bool8bit,bool16bit,bool32bit,bool64bit]);
  306. end;
  307. { true if p is a void }
  308. function is_void(def : tdef) : boolean;
  309. begin
  310. result:=(def.typ=orddef) and
  311. (torddef(def).ordtype=uvoid);
  312. end;
  313. { true if p is a char }
  314. function is_char(def : tdef) : boolean;
  315. begin
  316. result:=(def.typ=orddef) and
  317. (torddef(def).ordtype=uchar);
  318. end;
  319. { true if p is a wchar }
  320. function is_widechar(def : tdef) : boolean;
  321. begin
  322. result:=(def.typ=orddef) and
  323. (torddef(def).ordtype=uwidechar);
  324. end;
  325. { true if p is signed (integer) }
  326. function is_signed(def : tdef) : boolean;
  327. begin
  328. case def.typ of
  329. orddef :
  330. result:=torddef(def).low < 0;
  331. enumdef :
  332. result:=tenumdef(def).min < 0;
  333. arraydef :
  334. result:=is_signed(tarraydef(def).rangedef);
  335. else
  336. result:=false;
  337. end;
  338. end;
  339. function is_in_limit(def_from,def_to : tdef) : boolean;
  340. var
  341. fromqword, toqword: boolean;
  342. begin
  343. if (def_from.typ <> orddef) or
  344. (def_to.typ <> orddef) then
  345. begin
  346. is_in_limit := false;
  347. exit;
  348. end;
  349. fromqword := torddef(def_from).ordtype = u64bit;
  350. toqword := torddef(def_to).ordtype = u64bit;
  351. is_in_limit:=(toqword and is_signed(def_from)) or
  352. ((not fromqword) and
  353. (torddef(def_from).low>=torddef(def_to).low) and
  354. (torddef(def_from).high<=torddef(def_to).high));
  355. end;
  356. function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;
  357. begin
  358. if (def_from.typ <> orddef) and
  359. (def_to.typ <> orddef) then
  360. internalerror(200210062);
  361. if (torddef(def_to).ordtype = u64bit) then
  362. begin
  363. is_in_limit_value:=((TConstExprUInt(val_from)>=TConstExprUInt(torddef(def_to).low)) and
  364. (TConstExprUInt(val_from)<=TConstExprUInt(torddef(def_to).high)));
  365. end
  366. else
  367. begin;
  368. is_in_limit_value:=((val_from>=torddef(def_to).low) and
  369. (val_from<=torddef(def_to).high));
  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 p is a smallset def }
  518. function is_smallset(p : tdef) : boolean;
  519. begin
  520. is_smallset:=(p.typ=setdef) and
  521. (tsetdef(p).settype=smallset);
  522. end;
  523. { true, if def is a 32 bit int type }
  524. function is_32bitint(def : tdef) : boolean;
  525. begin
  526. result:=(def.typ=orddef) and (torddef(def).ordtype in [u32bit,s32bit])
  527. end;
  528. { true, if def is a 64 bit int type }
  529. function is_64bitint(def : tdef) : boolean;
  530. begin
  531. is_64bitint:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit])
  532. end;
  533. { true, if def is a 64 bit type }
  534. function is_64bit(def : tdef) : boolean;
  535. begin
  536. is_64bit:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit,scurrency])
  537. end;
  538. { if l isn't in the range of todef a range check error (if not explicit) is generated and
  539. the value is placed within the range }
  540. procedure testrange(fromdef, todef : tdef;var l : tconstexprint;explicit:boolean);
  541. var
  542. lv,hv: TConstExprInt;
  543. error: boolean;
  544. begin
  545. error := false;
  546. { for 64 bit types we need only to check if it is less than }
  547. { zero, if def is a qword node }
  548. if is_64bitint(todef) then
  549. begin
  550. if (l<0) and
  551. (torddef(todef).ordtype=u64bit) and
  552. { since tconstexprint is an int64, values > high(int64) will }
  553. { always be stored as negative numbers }
  554. (not is_64bitint(fromdef) or
  555. (torddef(fromdef).ordtype<>u64bit)) then
  556. begin
  557. { don't zero the result, because it may come from hex notation
  558. like $ffffffffffffffff! (JM)
  559. l:=0; }
  560. if not explicit then
  561. begin
  562. if (cs_check_range in current_settings.localswitches) then
  563. Message(parser_e_range_check_error)
  564. else
  565. Message(parser_w_range_check_error);
  566. end;
  567. error := true;
  568. end;
  569. end
  570. else
  571. begin
  572. getrange(todef,lv,hv);
  573. if (l<lv) or (l>hv) then
  574. begin
  575. if not explicit then
  576. begin
  577. if ((todef.typ=enumdef) and
  578. { delphi allows range check errors in
  579. enumeration type casts FK }
  580. not(m_delphi in current_settings.modeswitches)) or
  581. (cs_check_range in current_settings.localswitches) then
  582. Message(parser_e_range_check_error)
  583. else
  584. Message(parser_w_range_check_error);
  585. end;
  586. error := true;
  587. end;
  588. end;
  589. if error then
  590. begin
  591. { Fix the value to fit in the allocated space for this type of variable }
  592. case longint(todef.size) of
  593. 1: l := l and $ff;
  594. 2: l := l and $ffff;
  595. { work around sign extension bug (to be fixed) (JM) }
  596. 4: l := l and (int64($fffffff) shl 4 + $f);
  597. end;
  598. { do sign extension if necessary (JM) }
  599. if is_signed(todef) then
  600. begin
  601. case longint(todef.size) of
  602. 1: l := shortint(l);
  603. 2: l := smallint(l);
  604. 4: l := longint(l);
  605. end;
  606. end;
  607. end;
  608. end;
  609. { return the range from def in l and h }
  610. procedure getrange(def : tdef;out l, h : TConstExprInt);
  611. begin
  612. case def.typ of
  613. orddef :
  614. begin
  615. l:=torddef(def).low;
  616. h:=torddef(def).high;
  617. end;
  618. enumdef :
  619. begin
  620. l:=tenumdef(def).min;
  621. h:=tenumdef(def).max;
  622. end;
  623. arraydef :
  624. begin
  625. l:=tarraydef(def).lowrange;
  626. h:=tarraydef(def).highrange;
  627. end;
  628. else
  629. internalerror(200611054);
  630. end;
  631. end;
  632. function mmx_type(p : tdef) : tmmxtype;
  633. begin
  634. mmx_type:=mmxno;
  635. if is_mmx_able_array(p) then
  636. begin
  637. if tarraydef(p).elementdef.typ=floatdef then
  638. case tfloatdef(tarraydef(p).elementdef).floattype of
  639. s32real:
  640. mmx_type:=mmxsingle;
  641. end
  642. else
  643. case torddef(tarraydef(p).elementdef).ordtype of
  644. u8bit:
  645. mmx_type:=mmxu8bit;
  646. s8bit:
  647. mmx_type:=mmxs8bit;
  648. u16bit:
  649. mmx_type:=mmxu16bit;
  650. s16bit:
  651. mmx_type:=mmxs16bit;
  652. u32bit:
  653. mmx_type:=mmxu32bit;
  654. s32bit:
  655. mmx_type:=mmxs32bit;
  656. end;
  657. end;
  658. end;
  659. function is_vector(p : tdef) : boolean;
  660. begin
  661. result:=(p.typ=arraydef) and
  662. not(is_special_array(p)) and
  663. (tarraydef(p).elementdef.typ=floatdef) and
  664. (tfloatdef(tarraydef(p).elementdef).floattype in [s32real,s64real]);
  665. end;
  666. { returns if the passed type (array) fits into an mm register }
  667. function fits_in_mm_register(p : tdef) : boolean;
  668. begin
  669. {$ifdef x86}
  670. result:= is_vector(p) and
  671. (
  672. (tarraydef(p).elementdef.typ=floatdef) and
  673. (
  674. (tarraydef(p).lowrange=0) and
  675. (tarraydef(p).highrange=3) and
  676. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  677. )
  678. ) or
  679. (
  680. (tarraydef(p).elementdef.typ=floatdef) and
  681. (
  682. (tarraydef(p).lowrange=0) and
  683. (tarraydef(p).highrange=1) and
  684. (tfloatdef(tarraydef(p).elementdef).floattype=s64real)
  685. )
  686. );
  687. {$else x86}
  688. result:=false;
  689. {$endif x86}
  690. end;
  691. function is_mmx_able_array(p : tdef) : boolean;
  692. begin
  693. {$ifdef SUPPORT_MMX}
  694. if (cs_mmx_saturation in current_settings.localswitches) then
  695. begin
  696. is_mmx_able_array:=(p.typ=arraydef) and
  697. not(is_special_array(p)) and
  698. (
  699. (
  700. (tarraydef(p).elementdef.typ=orddef) and
  701. (
  702. (
  703. (tarraydef(p).lowrange=0) and
  704. (tarraydef(p).highrange=1) and
  705. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  706. )
  707. or
  708. (
  709. (tarraydef(p).lowrange=0) and
  710. (tarraydef(p).highrange=3) and
  711. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  712. )
  713. )
  714. )
  715. or
  716. (
  717. (
  718. (tarraydef(p).elementdef.typ=floatdef) and
  719. (
  720. (tarraydef(p).lowrange=0) and
  721. (tarraydef(p).highrange=1) and
  722. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  723. )
  724. )
  725. )
  726. );
  727. end
  728. else
  729. begin
  730. is_mmx_able_array:=(p.typ=arraydef) and
  731. (
  732. (
  733. (tarraydef(p).elementdef.typ=orddef) and
  734. (
  735. (
  736. (tarraydef(p).lowrange=0) and
  737. (tarraydef(p).highrange=1) and
  738. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  739. )
  740. or
  741. (
  742. (tarraydef(p).lowrange=0) and
  743. (tarraydef(p).highrange=3) and
  744. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  745. )
  746. or
  747. (
  748. (tarraydef(p).lowrange=0) and
  749. (tarraydef(p).highrange=7) and
  750. (torddef(tarraydef(p).elementdef).ordtype in [u8bit,s8bit])
  751. )
  752. )
  753. )
  754. or
  755. (
  756. (tarraydef(p).elementdef.typ=floatdef) and
  757. (
  758. (tarraydef(p).lowrange=0) and
  759. (tarraydef(p).highrange=1) and
  760. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  761. )
  762. )
  763. );
  764. end;
  765. {$else SUPPORT_MMX}
  766. is_mmx_able_array:=false;
  767. {$endif SUPPORT_MMX}
  768. end;
  769. function def_cgsize(def: tdef): tcgsize;
  770. begin
  771. case def.typ of
  772. orddef,
  773. enumdef,
  774. setdef:
  775. begin
  776. result:=int_cgsize(def.size);
  777. if is_signed(def) then
  778. result:=tcgsize(ord(result)+(ord(OS_S8)-ord(OS_8)));
  779. end;
  780. classrefdef,
  781. pointerdef:
  782. result := OS_ADDR;
  783. procvardef:
  784. begin
  785. if tprocvardef(def).is_methodpointer and
  786. (not tprocvardef(def).is_addressonly) then
  787. if (sizeof(aint) = 4) then
  788. result:=OS_64
  789. else if (sizeof(aint) = 8) then
  790. result:=OS_128
  791. else
  792. internalerror(200707141)
  793. else
  794. result:=OS_ADDR;
  795. end;
  796. stringdef :
  797. begin
  798. if is_ansistring(def) or is_widestring(def) then
  799. result := OS_ADDR
  800. else
  801. result:=int_cgsize(def.size);
  802. end;
  803. objectdef :
  804. begin
  805. if is_class_or_interface(def) then
  806. result := OS_ADDR
  807. else
  808. result:=int_cgsize(def.size);
  809. end;
  810. floatdef:
  811. if cs_fp_emulation in current_settings.moduleswitches then
  812. result:=int_cgsize(def.size)
  813. else
  814. result:=tfloat2tcgsize[tfloatdef(def).floattype];
  815. recorddef :
  816. result:=int_cgsize(def.size);
  817. arraydef :
  818. begin
  819. if not is_special_array(def) then
  820. result := int_cgsize(def.size)
  821. else
  822. begin
  823. if is_dynamic_array(def) then
  824. result := OS_ADDR
  825. else
  826. result := OS_NO;
  827. end;
  828. end;
  829. else
  830. begin
  831. { undefined size }
  832. result:=OS_NO;
  833. end;
  834. end;
  835. end;
  836. function is_automatable(p : tdef) : boolean;
  837. begin
  838. result:=false;
  839. case p.typ of
  840. orddef:
  841. result:=torddef(p).ordtype in [u8bit,s32bit,s16bit,bool16bit];
  842. floatdef:
  843. result:=tfloatdef(p).floattype in [s64currency,s64real,s32real];
  844. stringdef:
  845. result:=tstringdef(p).stringtype in [st_ansistring,st_widestring];
  846. variantdef:
  847. result:=true;
  848. arraydef:
  849. result:=(ado_IsConstString in tarraydef(p).arrayoptions);
  850. objectdef:
  851. result:=tobjectdef(p).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba];
  852. end;
  853. end;
  854. {# returns true, if the type passed is a varset }
  855. function is_varset(p : tdef) : boolean;
  856. begin
  857. if target_info.endian=endian_little then
  858. result:=(p.typ=setdef) and not(p.size in [1,2,4])
  859. else
  860. result:=false;
  861. end;
  862. function is_normalset(p : tdef) : boolean;
  863. begin
  864. if target_info.endian=endian_big then
  865. result:=(p.typ=setdef) and (tsetdef(p).size=32)
  866. else
  867. result:=false;
  868. end;
  869. function is_bareprocdef(pd : tprocdef): boolean;
  870. begin
  871. result:=(pd.maxparacount=0) and
  872. (is_void(pd.returndef) or
  873. (pd.proctypeoption = potype_constructor));
  874. end;
  875. end.