defutil.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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 <> orddef) or
  342. (def_to.typ <> orddef) then
  343. begin
  344. is_in_limit := false;
  345. exit;
  346. end;
  347. fromqword := torddef(def_from).ordtype = u64bit;
  348. toqword := torddef(def_to).ordtype = u64bit;
  349. is_in_limit:=(toqword and is_signed(def_from)) or
  350. ((not fromqword) and
  351. (torddef(def_from).low>=torddef(def_to).low) and
  352. (torddef(def_from).high<=torddef(def_to).high));
  353. end;
  354. function is_in_limit_value(val_from:TConstExprInt;def_from,def_to : tdef) : boolean;
  355. begin
  356. if (def_from.typ <> orddef) and
  357. (def_to.typ <> orddef) then
  358. internalerror(200210062);
  359. if (torddef(def_to).ordtype = u64bit) then
  360. begin
  361. is_in_limit_value:=((TConstExprUInt(val_from)>=TConstExprUInt(torddef(def_to).low)) and
  362. (TConstExprUInt(val_from)<=TConstExprUInt(torddef(def_to).high)));
  363. end
  364. else
  365. begin;
  366. is_in_limit_value:=((val_from>=torddef(def_to).low) and
  367. (val_from<=torddef(def_to).high));
  368. end;
  369. end;
  370. { true, if p points to an open array def }
  371. function is_open_string(p : tdef) : boolean;
  372. begin
  373. is_open_string:=(p.typ=stringdef) and
  374. (tstringdef(p).stringtype=st_shortstring) and
  375. (tstringdef(p).len=0);
  376. end;
  377. { true, if p points to a zero based array def }
  378. function is_zero_based_array(p : tdef) : boolean;
  379. begin
  380. result:=(p.typ=arraydef) and
  381. (tarraydef(p).lowrange=0) and
  382. not(is_special_array(p));
  383. end;
  384. { true if p points to a dynamic array def }
  385. function is_dynamic_array(p : tdef) : boolean;
  386. begin
  387. result:=(p.typ=arraydef) and
  388. (ado_IsDynamicArray in tarraydef(p).arrayoptions);
  389. end;
  390. { true, if p points to an open array def }
  391. function is_open_array(p : tdef) : boolean;
  392. begin
  393. { check for s32inttype is needed, because for u32bit the high
  394. range is also -1 ! (PFV) }
  395. result:=(p.typ=arraydef) and
  396. (tarraydef(p).rangedef=s32inttype) and
  397. (tarraydef(p).lowrange=0) and
  398. (tarraydef(p).highrange=-1) and
  399. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])=[]);
  400. end;
  401. { true, if p points to an array of const def }
  402. function is_array_constructor(p : tdef) : boolean;
  403. begin
  404. result:=(p.typ=arraydef) and
  405. (ado_IsConstructor in tarraydef(p).arrayoptions);
  406. end;
  407. { true, if p points to a variant array }
  408. function is_variant_array(p : tdef) : boolean;
  409. begin
  410. result:=(p.typ=arraydef) and
  411. (ado_IsVariant in tarraydef(p).arrayoptions);
  412. end;
  413. { true, if p points to an array of const }
  414. function is_array_of_const(p : tdef) : boolean;
  415. begin
  416. result:=(p.typ=arraydef) and
  417. (ado_IsArrayOfConst in tarraydef(p).arrayoptions);
  418. end;
  419. { true, if p points to a special array, bitpacked arrays aren't special in this regard though }
  420. function is_special_array(p : tdef) : boolean;
  421. begin
  422. result:=(p.typ=arraydef) and
  423. (
  424. ((tarraydef(p).arrayoptions * [ado_IsVariant,ado_IsArrayOfConst,ado_IsConstructor,ado_IsDynamicArray])<>[]) or
  425. is_open_array(p)
  426. );
  427. end;
  428. { true if p is an ansi string def }
  429. function is_ansistring(p : tdef) : boolean;
  430. begin
  431. is_ansistring:=(p.typ=stringdef) and
  432. (tstringdef(p).stringtype=st_ansistring);
  433. end;
  434. { true if p is an long string def }
  435. function is_longstring(p : tdef) : boolean;
  436. begin
  437. is_longstring:=(p.typ=stringdef) and
  438. (tstringdef(p).stringtype=st_longstring);
  439. end;
  440. { true if p is an wide string def }
  441. function is_widestring(p : tdef) : boolean;
  442. begin
  443. is_widestring:=(p.typ=stringdef) and
  444. (tstringdef(p).stringtype=st_widestring);
  445. end;
  446. { true if p is an short string def }
  447. function is_shortstring(p : tdef) : boolean;
  448. begin
  449. is_shortstring:=(p.typ=stringdef) and
  450. (tstringdef(p).stringtype=st_shortstring);
  451. end;
  452. { true if p is bit packed array def }
  453. function is_packed_array(p: tdef) : boolean;
  454. begin
  455. is_packed_array :=
  456. (p.typ = arraydef) and
  457. (ado_IsBitPacked in tarraydef(p).arrayoptions);
  458. end;
  459. { true if p is bit packed record def }
  460. function is_packed_record_or_object(p: tdef) : boolean;
  461. begin
  462. is_packed_record_or_object :=
  463. (p.typ in [recorddef,objectdef]) and
  464. (tabstractrecorddef(p).is_packed);
  465. end;
  466. { true if p is a char array def }
  467. function is_chararray(p : tdef) : boolean;
  468. begin
  469. is_chararray:=(p.typ=arraydef) and
  470. is_char(tarraydef(p).elementdef) and
  471. not(is_special_array(p));
  472. end;
  473. { true if p is a widechar array def }
  474. function is_widechararray(p : tdef) : boolean;
  475. begin
  476. is_widechararray:=(p.typ=arraydef) and
  477. is_widechar(tarraydef(p).elementdef) and
  478. not(is_special_array(p));
  479. end;
  480. { true if p is a open char array def }
  481. function is_open_chararray(p : tdef) : boolean;
  482. begin
  483. is_open_chararray:= is_open_array(p) and
  484. is_char(tarraydef(p).elementdef);
  485. end;
  486. { true if p is a open wide char array def }
  487. function is_open_widechararray(p : tdef) : boolean;
  488. begin
  489. is_open_widechararray:= is_open_array(p) and
  490. is_widechar(tarraydef(p).elementdef);
  491. end;
  492. { true if p is a pchar def }
  493. function is_pchar(p : tdef) : boolean;
  494. begin
  495. is_pchar:=(p.typ=pointerdef) and
  496. (is_char(tpointerdef(p).pointeddef) or
  497. (is_zero_based_array(tpointerdef(p).pointeddef) and
  498. is_chararray(tpointerdef(p).pointeddef)));
  499. end;
  500. { true if p is a pchar def }
  501. function is_pwidechar(p : tdef) : boolean;
  502. begin
  503. is_pwidechar:=(p.typ=pointerdef) and
  504. (is_widechar(tpointerdef(p).pointeddef) or
  505. (is_zero_based_array(tpointerdef(p).pointeddef) and
  506. is_widechararray(tpointerdef(p).pointeddef)));
  507. end;
  508. { true if p is a voidpointer def }
  509. function is_voidpointer(p : tdef) : boolean;
  510. begin
  511. is_voidpointer:=(p.typ=pointerdef) and
  512. (tpointerdef(p).pointeddef.typ=orddef) and
  513. (torddef(tpointerdef(p).pointeddef).ordtype=uvoid);
  514. end;
  515. { true if p is a smallset def }
  516. function is_smallset(p : tdef) : boolean;
  517. begin
  518. is_smallset:=(p.typ=setdef) and
  519. (tsetdef(p).settype=smallset);
  520. end;
  521. { true, if def is a 32 bit int type }
  522. function is_32bitint(def : tdef) : boolean;
  523. begin
  524. result:=(def.typ=orddef) and (torddef(def).ordtype in [u32bit,s32bit])
  525. end;
  526. { true, if def is a 64 bit int type }
  527. function is_64bitint(def : tdef) : boolean;
  528. begin
  529. is_64bitint:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit])
  530. end;
  531. { true, if def is a 64 bit type }
  532. function is_64bit(def : tdef) : boolean;
  533. begin
  534. is_64bit:=(def.typ=orddef) and (torddef(def).ordtype in [u64bit,s64bit,scurrency])
  535. end;
  536. { if l isn't in the range of todef a range check error (if not explicit) is generated and
  537. the value is placed within the range }
  538. procedure testrange(fromdef, todef : tdef;var l : tconstexprint;explicit:boolean);
  539. var
  540. lv,hv: TConstExprInt;
  541. error: boolean;
  542. begin
  543. error := false;
  544. { for 64 bit types we need only to check if it is less than }
  545. { zero, if def is a qword node }
  546. if is_64bitint(todef) then
  547. begin
  548. if (l<0) and
  549. (torddef(todef).ordtype=u64bit) and
  550. { since tconstexprint is an int64, values > high(int64) will }
  551. { always be stored as negative numbers }
  552. (not is_64bitint(fromdef) or
  553. (torddef(fromdef).ordtype<>u64bit)) then
  554. begin
  555. { don't zero the result, because it may come from hex notation
  556. like $ffffffffffffffff! (JM)
  557. l:=0; }
  558. if not explicit then
  559. begin
  560. if (cs_check_range in current_settings.localswitches) then
  561. Message(parser_e_range_check_error)
  562. else
  563. Message(parser_w_range_check_error);
  564. end;
  565. error := true;
  566. end;
  567. end
  568. else
  569. begin
  570. getrange(todef,lv,hv);
  571. if (l<lv) or (l>hv) then
  572. begin
  573. if not explicit then
  574. begin
  575. if ((todef.typ=enumdef) and
  576. { delphi allows range check errors in
  577. enumeration type casts FK }
  578. not(m_delphi in current_settings.modeswitches)) or
  579. (cs_check_range in current_settings.localswitches) then
  580. Message(parser_e_range_check_error)
  581. else
  582. Message(parser_w_range_check_error);
  583. end;
  584. error := true;
  585. end;
  586. end;
  587. if error then
  588. begin
  589. { Fix the value to fit in the allocated space for this type of variable }
  590. case longint(todef.size) of
  591. 1: l := l and $ff;
  592. 2: l := l and $ffff;
  593. { work around sign extension bug (to be fixed) (JM) }
  594. 4: l := l and (int64($fffffff) shl 4 + $f);
  595. end;
  596. { do sign extension if necessary (JM) }
  597. if is_signed(todef) then
  598. begin
  599. case longint(todef.size) of
  600. 1: l := shortint(l);
  601. 2: l := smallint(l);
  602. 4: l := longint(l);
  603. end;
  604. end;
  605. end;
  606. end;
  607. { return the range from def in l and h }
  608. procedure getrange(def : tdef;out l, h : TConstExprInt);
  609. begin
  610. case def.typ of
  611. orddef :
  612. begin
  613. l:=torddef(def).low;
  614. h:=torddef(def).high;
  615. end;
  616. enumdef :
  617. begin
  618. l:=tenumdef(def).min;
  619. h:=tenumdef(def).max;
  620. end;
  621. arraydef :
  622. begin
  623. l:=tarraydef(def).lowrange;
  624. h:=tarraydef(def).highrange;
  625. end;
  626. else
  627. internalerror(200611054);
  628. end;
  629. end;
  630. function mmx_type(p : tdef) : tmmxtype;
  631. begin
  632. mmx_type:=mmxno;
  633. if is_mmx_able_array(p) then
  634. begin
  635. if tarraydef(p).elementdef.typ=floatdef then
  636. case tfloatdef(tarraydef(p).elementdef).floattype of
  637. s32real:
  638. mmx_type:=mmxsingle;
  639. end
  640. else
  641. case torddef(tarraydef(p).elementdef).ordtype of
  642. u8bit:
  643. mmx_type:=mmxu8bit;
  644. s8bit:
  645. mmx_type:=mmxs8bit;
  646. u16bit:
  647. mmx_type:=mmxu16bit;
  648. s16bit:
  649. mmx_type:=mmxs16bit;
  650. u32bit:
  651. mmx_type:=mmxu32bit;
  652. s32bit:
  653. mmx_type:=mmxs32bit;
  654. end;
  655. end;
  656. end;
  657. function is_vector(p : tdef) : boolean;
  658. begin
  659. result:=(p.typ=arraydef) and
  660. not(is_special_array(p)) and
  661. (tarraydef(p).elementdef.typ=floatdef) and
  662. (tfloatdef(tarraydef(p).elementdef).floattype in [s32real,s64real]);
  663. end;
  664. { returns if the passed type (array) fits into an mm register }
  665. function fits_in_mm_register(p : tdef) : boolean;
  666. begin
  667. {$ifdef x86}
  668. result:= is_vector(p) and
  669. (
  670. (tarraydef(p).elementdef.typ=floatdef) and
  671. (
  672. (tarraydef(p).lowrange=0) and
  673. (tarraydef(p).highrange=3) and
  674. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  675. )
  676. ) or
  677. (
  678. (tarraydef(p).elementdef.typ=floatdef) and
  679. (
  680. (tarraydef(p).lowrange=0) and
  681. (tarraydef(p).highrange=1) and
  682. (tfloatdef(tarraydef(p).elementdef).floattype=s64real)
  683. )
  684. );
  685. {$else x86}
  686. result:=false;
  687. {$endif x86}
  688. end;
  689. function is_mmx_able_array(p : tdef) : boolean;
  690. begin
  691. {$ifdef SUPPORT_MMX}
  692. if (cs_mmx_saturation in current_settings.localswitches) then
  693. begin
  694. is_mmx_able_array:=(p.typ=arraydef) and
  695. not(is_special_array(p)) and
  696. (
  697. (
  698. (tarraydef(p).elementdef.typ=orddef) and
  699. (
  700. (
  701. (tarraydef(p).lowrange=0) and
  702. (tarraydef(p).highrange=1) and
  703. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  704. )
  705. or
  706. (
  707. (tarraydef(p).lowrange=0) and
  708. (tarraydef(p).highrange=3) and
  709. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  710. )
  711. )
  712. )
  713. or
  714. (
  715. (
  716. (tarraydef(p).elementdef.typ=floatdef) and
  717. (
  718. (tarraydef(p).lowrange=0) and
  719. (tarraydef(p).highrange=1) and
  720. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  721. )
  722. )
  723. )
  724. );
  725. end
  726. else
  727. begin
  728. is_mmx_able_array:=(p.typ=arraydef) and
  729. (
  730. (
  731. (tarraydef(p).elementdef.typ=orddef) and
  732. (
  733. (
  734. (tarraydef(p).lowrange=0) and
  735. (tarraydef(p).highrange=1) and
  736. (torddef(tarraydef(p).elementdef).ordtype in [u32bit,s32bit])
  737. )
  738. or
  739. (
  740. (tarraydef(p).lowrange=0) and
  741. (tarraydef(p).highrange=3) and
  742. (torddef(tarraydef(p).elementdef).ordtype in [u16bit,s16bit])
  743. )
  744. or
  745. (
  746. (tarraydef(p).lowrange=0) and
  747. (tarraydef(p).highrange=7) and
  748. (torddef(tarraydef(p).elementdef).ordtype in [u8bit,s8bit])
  749. )
  750. )
  751. )
  752. or
  753. (
  754. (tarraydef(p).elementdef.typ=floatdef) and
  755. (
  756. (tarraydef(p).lowrange=0) and
  757. (tarraydef(p).highrange=1) and
  758. (tfloatdef(tarraydef(p).elementdef).floattype=s32real)
  759. )
  760. )
  761. );
  762. end;
  763. {$else SUPPORT_MMX}
  764. is_mmx_able_array:=false;
  765. {$endif SUPPORT_MMX}
  766. end;
  767. function def_cgsize(def: tdef): tcgsize;
  768. begin
  769. case def.typ of
  770. orddef,
  771. enumdef,
  772. setdef:
  773. begin
  774. result:=int_cgsize(def.size);
  775. if is_signed(def) then
  776. result:=tcgsize(ord(result)+(ord(OS_S8)-ord(OS_8)));
  777. end;
  778. classrefdef,
  779. pointerdef:
  780. result := OS_ADDR;
  781. procvardef:
  782. begin
  783. if tprocvardef(def).is_methodpointer and
  784. (not tprocvardef(def).is_addressonly) then
  785. result := OS_64
  786. else
  787. result := OS_ADDR;
  788. end;
  789. stringdef :
  790. begin
  791. if is_ansistring(def) or is_widestring(def) then
  792. result := OS_ADDR
  793. else
  794. result:=int_cgsize(def.size);
  795. end;
  796. objectdef :
  797. begin
  798. if is_class_or_interface(def) then
  799. result := OS_ADDR
  800. else
  801. result:=int_cgsize(def.size);
  802. end;
  803. floatdef:
  804. if cs_fp_emulation in current_settings.moduleswitches then
  805. result:=int_cgsize(def.size)
  806. else
  807. result:=tfloat2tcgsize[tfloatdef(def).floattype];
  808. recorddef :
  809. result:=int_cgsize(def.size);
  810. arraydef :
  811. begin
  812. if not is_special_array(def) then
  813. result := int_cgsize(def.size)
  814. else
  815. begin
  816. if is_dynamic_array(def) then
  817. result := OS_ADDR
  818. else
  819. result := OS_NO;
  820. end;
  821. end;
  822. else
  823. begin
  824. { undefined size }
  825. result:=OS_NO;
  826. end;
  827. end;
  828. end;
  829. function is_automatable(p : tdef) : boolean;
  830. begin
  831. result:=false;
  832. case p.typ of
  833. orddef:
  834. result:=torddef(p).ordtype in [u8bit,s32bit,s16bit,bool16bit];
  835. floatdef:
  836. result:=tfloatdef(p).floattype in [s64currency,s64real,s32real];
  837. stringdef:
  838. result:=tstringdef(p).stringtype in [st_ansistring,st_widestring];
  839. variantdef:
  840. result:=true;
  841. arraydef:
  842. result:=(ado_IsConstString in tarraydef(p).arrayoptions);
  843. objectdef:
  844. result:=tobjectdef(p).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba];
  845. end;
  846. end;
  847. {# returns true, if the type passed is a varset }
  848. function is_varset(p : tdef) : boolean;
  849. begin
  850. result:=(p.typ=setdef) and not(p.size in [1,2,4])
  851. end;
  852. function is_bareprocdef(pd : tprocdef): boolean;
  853. begin
  854. result:=(pd.maxparacount=0) and
  855. (is_void(pd.returndef) or
  856. (pd.proctypeoption = potype_constructor));
  857. end;
  858. end.