defutil.pas 30 KB

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