defutil.pas 29 KB

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