defutil.pas 32 KB

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