defutil.pas 31 KB

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