defutil.pas 29 KB

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