types.pas 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. {
  2. $Id$
  3. Copyright (C) 1993-98 by Florian Klaempfl
  4. This unit provides some help routines for type handling
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit types;
  19. interface
  20. uses
  21. cobjects,symtable;
  22. type
  23. tmmxtype = (mmxno,mmxu8bit,mmxs8bit,mmxu16bit,mmxs16bit,
  24. mmxu32bit,mmxs32bit,mmxfixed16,mmxsingle);
  25. const
  26. { true if we must never copy this parameter }
  27. never_copy_const_param : boolean = false;
  28. {*****************************************************************************
  29. Basic type functions
  30. *****************************************************************************}
  31. { returns true, if def defines an ordinal type }
  32. function is_ordinal(def : pdef) : boolean;
  33. { returns the min. value of the type }
  34. function get_min_value(def : pdef) : longint;
  35. { returns true, if def defines an ordinal type }
  36. function is_integer(def : pdef) : boolean;
  37. { true if p is a boolean }
  38. function is_boolean(def : pdef) : boolean;
  39. { true if p is a char }
  40. function is_char(def : pdef) : boolean;
  41. { true if p is a smallset def }
  42. function is_smallset(p : pdef) : boolean;
  43. { returns true, if def defines a signed data type (only for ordinal types) }
  44. function is_signed(def : pdef) : boolean;
  45. {*****************************************************************************
  46. Array helper functions
  47. *****************************************************************************}
  48. { true, if p points to a zero based (non special like open or
  49. dynamic array def, mainly this is used to see if the array
  50. is convertable to a pointer }
  51. function is_zero_based_array(p : pdef) : boolean;
  52. { true if p points to an open array def }
  53. function is_open_array(p : pdef) : boolean;
  54. { true, if p points to an array of const def }
  55. function is_array_constructor(p : pdef) : boolean;
  56. { true, if p points to a variant array }
  57. function is_variant_array(p : pdef) : boolean;
  58. { true, if p points to an array of const }
  59. function is_array_of_const(p : pdef) : boolean;
  60. { true, if p points any kind of special array }
  61. function is_special_array(p : pdef) : boolean;
  62. { true if p is a char array def }
  63. function is_chararray(p : pdef) : boolean;
  64. {*****************************************************************************
  65. String helper functions
  66. *****************************************************************************}
  67. { true if p points to an open string def }
  68. function is_open_string(p : pdef) : boolean;
  69. { true if p is an ansi string def }
  70. function is_ansistring(p : pdef) : boolean;
  71. { true if p is a long string def }
  72. function is_longstring(p : pdef) : boolean;
  73. { true if p is a wide string def }
  74. function is_widestring(p : pdef) : boolean;
  75. { true if p is a short string def }
  76. function is_shortstring(p : pdef) : boolean;
  77. { true if p is a pchar def }
  78. function is_pchar(p : pdef) : boolean;
  79. { returns true, if def uses FPU }
  80. function is_fpu(def : pdef) : boolean;
  81. { true if the return value is in EAX }
  82. function ret_in_acc(def : pdef) : boolean;
  83. { true if uses a parameter as return value }
  84. function ret_in_param(def : pdef) : boolean;
  85. { true, if def is a 64 bit int type }
  86. function is_64bitint(def : pdef) : boolean;
  87. function push_high_param(def : pdef) : boolean;
  88. { true if a parameter is too large to copy and only the address is pushed }
  89. function push_addr_param(def : pdef) : boolean;
  90. { true, if def1 and def2 are semantical the same }
  91. function is_equal(def1,def2 : pdef) : boolean;
  92. { checks for type compatibility (subgroups of type) }
  93. { used for case statements... probably missing stuff }
  94. { to use on other types }
  95. function is_subequal(def1, def2: pdef): boolean;
  96. { same as is_equal, but with error message if failed }
  97. function CheckTypes(def1,def2 : pdef) : boolean;
  98. { true, if two parameter lists are equal }
  99. { if value_equal_const is true, call by value }
  100. { and call by const parameter are assumed as }
  101. { equal }
  102. function equal_paras(def1,def2 : pdefcoll;value_equal_const : boolean) : boolean;
  103. { true if a type can be allowed for another one
  104. in a func var }
  105. function convertable_paras(def1,def2 : pdefcoll;value_equal_const : boolean) : boolean;
  106. { true if a function can be assigned to a procvar }
  107. function proc_to_procvar_equal(def1:pprocdef;def2:pprocvardef) : boolean;
  108. { if l isn't in the range of def a range check error is generated and
  109. the value is placed within the range }
  110. procedure testrange(def : pdef;var l : longint);
  111. { returns the range of def }
  112. procedure getrange(def : pdef;var l : longint;var h : longint);
  113. { some type helper routines for MMX support }
  114. function is_mmx_able_array(p : pdef) : boolean;
  115. { returns the mmx type }
  116. function mmx_type(p : pdef) : tmmxtype;
  117. implementation
  118. uses
  119. strings,globtype,globals,htypechk,
  120. tree,verbose;
  121. function equal_paras(def1,def2 : pdefcoll;value_equal_const : boolean) : boolean;
  122. begin
  123. while (assigned(def1)) and (assigned(def2)) do
  124. begin
  125. if value_equal_const then
  126. begin
  127. if not(is_equal(def1^.data,def2^.data)) or
  128. ((def1^.paratyp<>def2^.paratyp) and
  129. ((def1^.paratyp=vs_var) or
  130. (def1^.paratyp=vs_var)
  131. )
  132. ) then
  133. begin
  134. equal_paras:=false;
  135. exit;
  136. end;
  137. end
  138. else
  139. begin
  140. if not(is_equal(def1^.data,def2^.data)) or
  141. (def1^.paratyp<>def2^.paratyp) then
  142. begin
  143. equal_paras:=false;
  144. exit;
  145. end;
  146. end;
  147. def1:=def1^.next;
  148. def2:=def2^.next;
  149. end;
  150. if (def1=nil) and (def2=nil) then
  151. equal_paras:=true
  152. else
  153. equal_paras:=false;
  154. end;
  155. function convertable_paras(def1,def2 : pdefcoll;value_equal_const : boolean) : boolean;
  156. var doconv : tconverttype;
  157. begin
  158. while (assigned(def1)) and (assigned(def2)) do
  159. begin
  160. if value_equal_const then
  161. begin
  162. if (isconvertable(def1^.data,def2^.data,doconv,callparan,false)=0) or
  163. ((def1^.paratyp<>def2^.paratyp) and
  164. ((def1^.paratyp=vs_var) or
  165. (def1^.paratyp=vs_var)
  166. )
  167. ) then
  168. begin
  169. convertable_paras:=false;
  170. exit;
  171. end;
  172. end
  173. else
  174. begin
  175. if (isconvertable(def1^.data,def2^.data,doconv,callparan,false)=0) or
  176. (def1^.paratyp<>def2^.paratyp) then
  177. begin
  178. convertable_paras:=false;
  179. exit;
  180. end;
  181. end;
  182. def1:=def1^.next;
  183. def2:=def2^.next;
  184. end;
  185. if (def1=nil) and (def2=nil) then
  186. convertable_paras:=true
  187. else
  188. convertable_paras:=false;
  189. end;
  190. { true if a function can be assigned to a procvar }
  191. function proc_to_procvar_equal(def1:pprocdef;def2:pprocvardef) : boolean;
  192. var
  193. ismethod : boolean;
  194. begin
  195. proc_to_procvar_equal:=false;
  196. if not(assigned(def1)) or not(assigned(def2)) then
  197. exit;
  198. { check for method pointer }
  199. ismethod:=assigned(def1^.owner) and
  200. (def1^.owner^.symtabletype=objectsymtable) and
  201. assigned(def1^.owner^.defowner) and
  202. (pobjectdef(def1^.owner^.defowner)^.isclass);
  203. if (ismethod and not ((def2^.options and pomethodpointer)<>0)) or
  204. (not(ismethod) and ((def2^.options and pomethodpointer)<>0)) then
  205. begin
  206. Message(type_e_no_method_and_procedure_not_compatible);
  207. exit;
  208. end;
  209. { check return value and para's and options, methodpointer is already checked
  210. parameters may also be convertable }
  211. if is_equal(def1^.retdef,def2^.retdef) and
  212. (equal_paras(def1^.para1,def2^.para1,false) or
  213. convertable_paras(def1^.para1,def2^.para1,false)) and
  214. ((def1^.options and (po_compatibility_options-pomethodpointer))=
  215. (def2^.options and (po_compatibility_options-pomethodpointer))) then
  216. proc_to_procvar_equal:=true
  217. else
  218. proc_to_procvar_equal:=false;
  219. end;
  220. { returns true, if def uses FPU }
  221. function is_fpu(def : pdef) : boolean;
  222. begin
  223. is_fpu:=(def^.deftype=floatdef) and (pfloatdef(def)^.typ<>f32bit);
  224. end;
  225. { true if p is an ordinal }
  226. function is_ordinal(def : pdef) : boolean;
  227. var
  228. dt : tbasetype;
  229. begin
  230. case def^.deftype of
  231. orddef : begin
  232. dt:=porddef(def)^.typ;
  233. is_ordinal:=dt in [uchar,u8bit,u16bit,u32bit,u64bit,s8bit,s16bit,s32bit,
  234. s64bitint,bool8bit,bool16bit,bool32bit];
  235. end;
  236. enumdef : is_ordinal:=true;
  237. else
  238. is_ordinal:=false;
  239. end;
  240. end;
  241. { returns the min. value of the type }
  242. function get_min_value(def : pdef) : longint;
  243. begin
  244. case def^.deftype of
  245. orddef:
  246. get_min_value:=porddef(def)^.low;
  247. enumdef:
  248. get_min_value:=penumdef(def)^.min;
  249. else
  250. get_min_value:=0;
  251. end;
  252. end;
  253. { true if p is an integer }
  254. function is_integer(def : pdef) : boolean;
  255. begin
  256. is_integer:=(def^.deftype=orddef) and
  257. (porddef(def)^.typ in [uauto,u8bit,u16bit,u32bit,s8bit,
  258. s16bit,s32bit,u64bit,s64bitint]);
  259. end;
  260. { true if p is a boolean }
  261. function is_boolean(def : pdef) : boolean;
  262. begin
  263. is_boolean:=(def^.deftype=orddef) and
  264. (porddef(def)^.typ in [bool8bit,bool16bit,bool32bit]);
  265. end;
  266. { true if p is a char }
  267. function is_char(def : pdef) : boolean;
  268. begin
  269. is_char:=(def^.deftype=orddef) and
  270. (porddef(def)^.typ=uchar);
  271. end;
  272. { true if p is signed (integer) }
  273. function is_signed(def : pdef) : boolean;
  274. var
  275. dt : tbasetype;
  276. begin
  277. case def^.deftype of
  278. orddef : begin
  279. dt:=porddef(def)^.typ;
  280. is_signed:=(dt in [s8bit,s16bit,s32bit,s64bitint]);
  281. end;
  282. enumdef : is_signed:=false;
  283. else
  284. is_signed:=false;
  285. end;
  286. end;
  287. { true, if p points to an open array def }
  288. function is_open_string(p : pdef) : boolean;
  289. begin
  290. is_open_string:=(p^.deftype=stringdef) and
  291. (pstringdef(p)^.string_typ=st_shortstring) and
  292. (pstringdef(p)^.len=0);
  293. end;
  294. { true, if p points to a zero based array def }
  295. function is_zero_based_array(p : pdef) : boolean;
  296. begin
  297. is_zero_based_array:=(p^.deftype=arraydef) and
  298. (parraydef(p)^.lowrange=0) and
  299. not(is_special_array(p));
  300. end;
  301. { true, if p points to an open array def }
  302. function is_open_array(p : pdef) : boolean;
  303. begin
  304. is_open_array:=(p^.deftype=arraydef) and
  305. (parraydef(p)^.lowrange=0) and
  306. (parraydef(p)^.highrange=-1) and
  307. not(parraydef(p)^.IsConstructor) and
  308. not(parraydef(p)^.IsVariant) and
  309. not(parraydef(p)^.IsArrayOfConst);
  310. end;
  311. { true, if p points to an array of const def }
  312. function is_array_constructor(p : pdef) : boolean;
  313. begin
  314. is_array_constructor:=(p^.deftype=arraydef) and
  315. (parraydef(p)^.IsConstructor);
  316. end;
  317. { true, if p points to a variant array }
  318. function is_variant_array(p : pdef) : boolean;
  319. begin
  320. is_variant_array:=(p^.deftype=arraydef) and
  321. (parraydef(p)^.IsVariant);
  322. end;
  323. { true, if p points to an array of const }
  324. function is_array_of_const(p : pdef) : boolean;
  325. begin
  326. is_array_of_const:=(p^.deftype=arraydef) and
  327. (parraydef(p)^.IsArrayOfConst);
  328. end;
  329. { true, if p points to a special array }
  330. function is_special_array(p : pdef) : boolean;
  331. begin
  332. is_special_array:=(p^.deftype=arraydef) and
  333. ((parraydef(p)^.IsVariant) or
  334. (parraydef(p)^.IsArrayOfConst) or
  335. (parraydef(p)^.IsConstructor) or
  336. is_open_array(p)
  337. );
  338. end;
  339. { true if p is an ansi string def }
  340. function is_ansistring(p : pdef) : boolean;
  341. begin
  342. is_ansistring:=(p^.deftype=stringdef) and
  343. (pstringdef(p)^.string_typ=st_ansistring);
  344. end;
  345. { true if p is an long string def }
  346. function is_longstring(p : pdef) : boolean;
  347. begin
  348. is_longstring:=(p^.deftype=stringdef) and
  349. (pstringdef(p)^.string_typ=st_longstring);
  350. end;
  351. { true if p is an wide string def }
  352. function is_widestring(p : pdef) : boolean;
  353. begin
  354. is_widestring:=(p^.deftype=stringdef) and
  355. (pstringdef(p)^.string_typ=st_widestring);
  356. end;
  357. { true if p is an short string def }
  358. function is_shortstring(p : pdef) : boolean;
  359. begin
  360. is_shortstring:=(p^.deftype=stringdef) and
  361. (pstringdef(p)^.string_typ=st_shortstring);
  362. end;
  363. { true if p is a char array def }
  364. function is_chararray(p : pdef) : boolean;
  365. begin
  366. is_chararray:=(p^.deftype=arraydef) and
  367. is_equal(parraydef(p)^.definition,cchardef) and
  368. not(is_special_array(p));
  369. end;
  370. { true if p is a pchar def }
  371. function is_pchar(p : pdef) : boolean;
  372. begin
  373. is_pchar:=(p^.deftype=pointerdef) and
  374. is_equal(Ppointerdef(p)^.definition,cchardef);
  375. end;
  376. { true if p is a smallset def }
  377. function is_smallset(p : pdef) : boolean;
  378. begin
  379. is_smallset:=(p^.deftype=setdef) and
  380. (psetdef(p)^.settype=smallset);
  381. end;
  382. { true if the return value is in accumulator (EAX for i386), D0 for 68k }
  383. function ret_in_acc(def : pdef) : boolean;
  384. begin
  385. ret_in_acc:=(def^.deftype in [orddef,pointerdef,enumdef,classrefdef]) or
  386. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_ansistring,st_widestring])) or
  387. ((def^.deftype=procvardef) and ((pprocvardef(def)^.options and pomethodpointer)=0)) or
  388. ((def^.deftype=objectdef) and pobjectdef(def)^.isclass) or
  389. ((def^.deftype=setdef) and (psetdef(def)^.settype=smallset)) or
  390. ((def^.deftype=floatdef) and (pfloatdef(def)^.typ=f32bit));
  391. end;
  392. { true, if def is a 64 bit int type }
  393. function is_64bitint(def : pdef) : boolean;
  394. begin
  395. is_64bitint:=(def^.deftype=orddef) and (porddef(def)^.typ in [u64bit,s64bitint])
  396. end;
  397. { true if uses a parameter as return value }
  398. function ret_in_param(def : pdef) : boolean;
  399. begin
  400. ret_in_param:=(def^.deftype in [arraydef,recorddef]) or
  401. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_shortstring,st_longstring])) or
  402. ((def^.deftype=procvardef) and ((pprocvardef(def)^.options and pomethodpointer)<>0)) or
  403. ((def^.deftype=objectdef) and not(pobjectdef(def)^.isclass)) or
  404. ((def^.deftype=setdef) and (psetdef(def)^.settype<>smallset));
  405. end;
  406. function push_high_param(def : pdef) : boolean;
  407. begin
  408. push_high_param:=is_open_array(def) or is_open_string(def) or
  409. is_array_of_const(def);
  410. end;
  411. { true if a parameter is too large to copy and only the address is pushed }
  412. function push_addr_param(def : pdef) : boolean;
  413. begin
  414. push_addr_param:=never_copy_const_param or
  415. (def^.deftype = formaldef) or
  416. { copy directly small records or arrays unless array of const ! PM }
  417. ((def^.deftype in [arraydef,recorddef]) and
  418. ((def^.size>4) or
  419. ((def^.deftype=arraydef) and
  420. (parraydef(def)^.IsConstructor or
  421. parraydef(def)^.isArrayOfConst or
  422. is_open_array(def)
  423. )
  424. )
  425. )
  426. ) or
  427. ((def^.deftype=objectdef) and not(pobjectdef(def)^.isclass)) or
  428. ((def^.deftype=stringdef) and (pstringdef(def)^.string_typ in [st_shortstring,st_longstring])) or
  429. ((def^.deftype=procvardef) and ((pprocvardef(def)^.options and pomethodpointer)<>0)) or
  430. ((def^.deftype=setdef) and (psetdef(def)^.settype<>smallset));
  431. end;
  432. { test if l is in the range of def, outputs error if out of range }
  433. procedure testrange(def : pdef;var l : longint);
  434. var
  435. lv,hv: longint;
  436. begin
  437. { for 64 bit types we need only to check if it is less than }
  438. { zero, if def is a qword node }
  439. if is_64bitint(def) then
  440. begin
  441. if (l<0) and (porddef(def)^.typ=u64bit) then
  442. begin
  443. l:=0;
  444. if (cs_check_range in aktlocalswitches) then
  445. Message(parser_e_range_check_error)
  446. else
  447. Message(parser_w_range_check_error);
  448. end;
  449. end
  450. else
  451. begin
  452. getrange(def,lv,hv);
  453. if (def^.deftype=orddef) and
  454. (porddef(def)^.typ=u32bit) then
  455. begin
  456. if lv<=hv then
  457. begin
  458. if (l<lv) or (l>hv) then
  459. begin
  460. if (cs_check_range in aktlocalswitches) then
  461. Message(parser_e_range_check_error)
  462. else
  463. Message(parser_w_range_check_error);
  464. end;
  465. end
  466. else
  467. { this happens with the wrap around problem }
  468. { if lv is positive and hv is over $7ffffff }
  469. { so it seems negative }
  470. begin
  471. if ((l>=0) and (l<lv)) or
  472. ((l<0) and (l>hv)) then
  473. begin
  474. if (cs_check_range in aktlocalswitches) then
  475. Message(parser_e_range_check_error)
  476. else
  477. Message(parser_w_range_check_error);
  478. end;
  479. end;
  480. end
  481. else if (l<lv) or (l>hv) then
  482. begin
  483. if (def^.deftype=enumdef) or
  484. (cs_check_range in aktlocalswitches) then
  485. Message(parser_e_range_check_error)
  486. else
  487. Message(parser_w_range_check_error);
  488. { Fix the value to be in range }
  489. l:=lv+(l mod (hv-lv+1));
  490. end;
  491. end;
  492. end;
  493. { return the range from def in l and h }
  494. procedure getrange(def : pdef;var l : longint;var h : longint);
  495. begin
  496. case def^.deftype of
  497. orddef :
  498. begin
  499. l:=porddef(def)^.low;
  500. h:=porddef(def)^.high;
  501. end;
  502. enumdef :
  503. begin
  504. l:=penumdef(def)^.min;
  505. h:=penumdef(def)^.max;
  506. end;
  507. arraydef :
  508. begin
  509. l:=parraydef(def)^.lowrange;
  510. h:=parraydef(def)^.highrange;
  511. end;
  512. else
  513. internalerror(987);
  514. end;
  515. end;
  516. function mmx_type(p : pdef) : tmmxtype;
  517. begin
  518. mmx_type:=mmxno;
  519. if is_mmx_able_array(p) then
  520. begin
  521. if parraydef(p)^.definition^.deftype=floatdef then
  522. case pfloatdef(parraydef(p)^.definition)^.typ of
  523. s32real:
  524. mmx_type:=mmxsingle;
  525. f16bit:
  526. mmx_type:=mmxfixed16
  527. end
  528. else
  529. case porddef(parraydef(p)^.definition)^.typ of
  530. u8bit:
  531. mmx_type:=mmxu8bit;
  532. s8bit:
  533. mmx_type:=mmxs8bit;
  534. u16bit:
  535. mmx_type:=mmxu16bit;
  536. s16bit:
  537. mmx_type:=mmxs16bit;
  538. u32bit:
  539. mmx_type:=mmxu32bit;
  540. s32bit:
  541. mmx_type:=mmxs32bit;
  542. end;
  543. end;
  544. end;
  545. function is_mmx_able_array(p : pdef) : boolean;
  546. begin
  547. {$ifdef SUPPORT_MMX}
  548. if (cs_mmx_saturation in aktlocalswitches) then
  549. begin
  550. is_mmx_able_array:=(p^.deftype=arraydef) and
  551. not(is_special_array(p)) and
  552. (
  553. (
  554. (parraydef(p)^.definition^.deftype=orddef) and
  555. (
  556. (
  557. (parraydef(p)^.lowrange=0) and
  558. (parraydef(p)^.highrange=1) and
  559. (porddef(parraydef(p)^.definition)^.typ in [u32bit,s32bit])
  560. )
  561. or
  562. (
  563. (parraydef(p)^.lowrange=0) and
  564. (parraydef(p)^.highrange=3) and
  565. (porddef(parraydef(p)^.definition)^.typ in [u16bit,s16bit])
  566. )
  567. )
  568. )
  569. or
  570. (
  571. (
  572. (parraydef(p)^.definition^.deftype=floatdef) and
  573. (
  574. (parraydef(p)^.lowrange=0) and
  575. (parraydef(p)^.highrange=3) and
  576. (pfloatdef(parraydef(p)^.definition)^.typ=f16bit)
  577. ) or
  578. (
  579. (parraydef(p)^.lowrange=0) and
  580. (parraydef(p)^.highrange=1) and
  581. (pfloatdef(parraydef(p)^.definition)^.typ=s32real)
  582. )
  583. )
  584. )
  585. );
  586. end
  587. else
  588. begin
  589. is_mmx_able_array:=(p^.deftype=arraydef) and
  590. (
  591. (
  592. (parraydef(p)^.definition^.deftype=orddef) and
  593. (
  594. (
  595. (parraydef(p)^.lowrange=0) and
  596. (parraydef(p)^.highrange=1) and
  597. (porddef(parraydef(p)^.definition)^.typ in [u32bit,s32bit])
  598. )
  599. or
  600. (
  601. (parraydef(p)^.lowrange=0) and
  602. (parraydef(p)^.highrange=3) and
  603. (porddef(parraydef(p)^.definition)^.typ in [u16bit,s16bit])
  604. )
  605. or
  606. (
  607. (parraydef(p)^.lowrange=0) and
  608. (parraydef(p)^.highrange=7) and
  609. (porddef(parraydef(p)^.definition)^.typ in [u8bit,s8bit])
  610. )
  611. )
  612. )
  613. or
  614. (
  615. (parraydef(p)^.definition^.deftype=floatdef) and
  616. (
  617. (
  618. (parraydef(p)^.lowrange=0) and
  619. (parraydef(p)^.highrange=3) and
  620. (pfloatdef(parraydef(p)^.definition)^.typ=f32bit)
  621. )
  622. or
  623. (
  624. (parraydef(p)^.lowrange=0) and
  625. (parraydef(p)^.highrange=1) and
  626. (pfloatdef(parraydef(p)^.definition)^.typ=s32real)
  627. )
  628. )
  629. )
  630. );
  631. end;
  632. {$else SUPPORT_MMX}
  633. is_mmx_able_array:=false;
  634. {$endif SUPPORT_MMX}
  635. end;
  636. function is_equal(def1,def2 : pdef) : boolean;
  637. var
  638. b : boolean;
  639. hd : pdef;
  640. hp1,hp2 : pdefcoll;
  641. begin
  642. { both types must exists }
  643. if not (assigned(def1) and assigned(def2)) then
  644. begin
  645. is_equal:=false;
  646. exit;
  647. end;
  648. { be sure, that if there is a stringdef, that this is def1 }
  649. if def2^.deftype=stringdef then
  650. begin
  651. hd:=def1;
  652. def1:=def2;
  653. def2:=hd;
  654. end;
  655. b:=false;
  656. { both point to the same definition ? }
  657. if def1=def2 then
  658. b:=true
  659. else
  660. { pointer with an equal definition are equal }
  661. if (def1^.deftype=pointerdef) and (def2^.deftype=pointerdef) then
  662. begin
  663. { here a problem detected in tabsolutesym }
  664. { the types can be forward type !! }
  665. if assigned(def1^.sym) and ((def1^.sym^.properties and sp_forwarddef)<>0) then
  666. b:=(def1^.sym=def2^.sym)
  667. else
  668. b:=ppointerdef(def1)^.definition=ppointerdef(def2)^.definition;
  669. end
  670. else
  671. { ordinals are equal only when the ordinal type is equal }
  672. if (def1^.deftype=orddef) and (def2^.deftype=orddef) then
  673. begin
  674. case porddef(def1)^.typ of
  675. u8bit,u16bit,u32bit,
  676. s8bit,s16bit,s32bit:
  677. b:=((porddef(def1)^.typ=porddef(def2)^.typ) and
  678. (porddef(def1)^.low=porddef(def2)^.low) and
  679. (porddef(def1)^.high=porddef(def2)^.high));
  680. uvoid,uchar,
  681. bool8bit,bool16bit,bool32bit:
  682. b:=(porddef(def1)^.typ=porddef(def2)^.typ);
  683. end;
  684. end
  685. else
  686. if (def1^.deftype=floatdef) and (def2^.deftype=floatdef) then
  687. b:=pfloatdef(def1)^.typ=pfloatdef(def2)^.typ
  688. else
  689. { strings with the same length are equal }
  690. if (def1^.deftype=stringdef) and (def2^.deftype=stringdef) and
  691. (pstringdef(def1)^.string_typ=pstringdef(def2)^.string_typ) then
  692. begin
  693. b:=not(is_shortstring(def1)) or
  694. (pstringdef(def1)^.len=pstringdef(def2)^.len);
  695. end
  696. else
  697. if (def1^.deftype=formaldef) and (def2^.deftype=formaldef) then
  698. b:=true
  699. { file types with the same file element type are equal }
  700. { this is a problem for assign !! }
  701. { changed to allow if one is untyped }
  702. { all typed files are equal to the special }
  703. { typed file that has voiddef as elemnt type }
  704. { but must NOT match for text file !!! }
  705. else
  706. if (def1^.deftype=filedef) and (def2^.deftype=filedef) then
  707. b:=(pfiledef(def1)^.filetype=pfiledef(def2)^.filetype) and
  708. ((
  709. ((pfiledef(def1)^.typed_as=nil) and
  710. (pfiledef(def2)^.typed_as=nil)) or
  711. (
  712. (pfiledef(def1)^.typed_as<>nil) and
  713. (pfiledef(def2)^.typed_as<>nil) and
  714. is_equal(pfiledef(def1)^.typed_as,pfiledef(def2)^.typed_as)
  715. ) or
  716. ( (pfiledef(def1)^.typed_as=pdef(voiddef)) or
  717. (pfiledef(def2)^.typed_as=pdef(voiddef))
  718. )))
  719. { sets with the same element type are equal }
  720. else
  721. if (def1^.deftype=setdef) and (def2^.deftype=setdef) then
  722. begin
  723. if assigned(psetdef(def1)^.setof) and
  724. assigned(psetdef(def2)^.setof) then
  725. b:=(psetdef(def1)^.setof^.deftype=psetdef(def2)^.setof^.deftype)
  726. else
  727. b:=true;
  728. end
  729. else
  730. if (def1^.deftype=procvardef) and (def2^.deftype=procvardef) then
  731. begin
  732. { poassembler isn't important for compatibility }
  733. { if a method is assigned to a methodpointer }
  734. { is checked before }
  735. b:=((pprocvardef(def1)^.options and po_compatibility_options)=
  736. (pprocvardef(def2)^.options and po_compatibility_options)) and
  737. is_equal(pprocvardef(def1)^.retdef,pprocvardef(def2)^.retdef);
  738. { now evalute the parameters }
  739. if b then
  740. begin
  741. hp1:=pprocvardef(def1)^.para1;
  742. hp2:=pprocvardef(def1)^.para1;
  743. while assigned(hp1) and assigned(hp2) do
  744. begin
  745. if not(is_equal(hp1^.data,hp2^.data)) or
  746. not(hp1^.paratyp=hp2^.paratyp) then
  747. begin
  748. b:=false;
  749. break;
  750. end;
  751. hp1:=hp1^.next;
  752. hp2:=hp2^.next;
  753. end;
  754. b:=(hp1=nil) and (hp2=nil);
  755. end;
  756. end
  757. else
  758. if (def1^.deftype=arraydef) and (def2^.deftype=arraydef) and
  759. (is_open_array(def1) or is_open_array(def2) or
  760. is_array_of_const(def1) or is_array_of_const(def2)) then
  761. begin
  762. if parraydef(def1)^.IsArrayOfConst or parraydef(def2)^.IsArrayOfConst then
  763. b:=true
  764. else
  765. b:=is_equal(parraydef(def1)^.definition,parraydef(def2)^.definition);
  766. end
  767. else
  768. if (def1^.deftype=classrefdef) and (def2^.deftype=classrefdef) then
  769. begin
  770. { similar to pointerdef: }
  771. if assigned(def1^.sym) and ((def1^.sym^.properties and sp_forwarddef)<>0) then
  772. b:=(def1^.sym=def2^.sym)
  773. else
  774. b:=is_equal(pclassrefdef(def1)^.definition,pclassrefdef(def2)^.definition);
  775. end;
  776. is_equal:=b;
  777. end;
  778. function is_subequal(def1, def2: pdef): boolean;
  779. Begin
  780. if assigned(def1) and assigned(def2) then
  781. Begin
  782. is_subequal := FALSE;
  783. if (def1^.deftype = orddef) and (def2^.deftype = orddef) then
  784. Begin
  785. { see p.47 of Turbo Pascal 7.01 manual for the separation of types }
  786. { range checking for case statements is done with testrange }
  787. case porddef(def1)^.typ of
  788. u8bit,u16bit,u32bit,
  789. s8bit,s16bit,s32bit :
  790. is_subequal:=(porddef(def2)^.typ in [s32bit,u32bit,u8bit,s8bit,s16bit,u16bit]);
  791. bool8bit,bool16bit,bool32bit :
  792. is_subequal:=(porddef(def2)^.typ in [bool8bit,bool16bit,bool32bit]);
  793. uchar :
  794. is_subequal:=(porddef(def2)^.typ=uchar);
  795. end;
  796. end
  797. else
  798. Begin
  799. { I assume that both enumerations are equal when the first }
  800. { pointers are equal. }
  801. if (def1^.deftype = enumdef) and (def2^.deftype =enumdef) then
  802. Begin
  803. if penumdef(def1)^.firstenum = penumdef(def2)^.firstenum then
  804. is_subequal := TRUE;
  805. end;
  806. end;
  807. end; { endif assigned ... }
  808. end;
  809. function CheckTypes(def1,def2 : pdef) : boolean;
  810. var
  811. s1,s2 : string;
  812. begin
  813. if not is_equal(def1,def2) then
  814. begin
  815. { Crash prevention }
  816. if (not assigned(def1)) or (not assigned(def2)) then
  817. Message(type_e_mismatch)
  818. else
  819. begin
  820. s1:=def1^.typename;
  821. s2:=def2^.typename;
  822. if (s1<>'<unknown type>') and (s2<>'<unknown type>') then
  823. Message2(type_e_not_equal_types,def1^.typename,def2^.typename)
  824. else
  825. Message(type_e_mismatch);
  826. end;
  827. CheckTypes:=false;
  828. end
  829. else
  830. CheckTypes:=true;
  831. end;
  832. end.
  833. {
  834. $Log$
  835. Revision 1.75 1999-07-06 21:48:29 florian
  836. * a lot bug fixes:
  837. - po_external isn't any longer necessary for procedure compatibility
  838. - m_tp_procvar is in -Sd now available
  839. - error messages of procedure variables improved
  840. - return values with init./finalization fixed
  841. - data types with init./finalization aren't any longer allowed in variant
  842. record
  843. Revision 1.74 1999/07/01 15:49:24 florian
  844. * int64/qword type release
  845. + lo/hi for int64/qword
  846. Revision 1.73 1999/06/28 22:29:22 florian
  847. * qword division fixed
  848. + code for qword/int64 type casting added:
  849. range checking isn't implemented yet
  850. Revision 1.72 1999/06/13 22:41:08 peter
  851. * merged from fixes
  852. Revision 1.71.2.1 1999/06/13 22:37:17 peter
  853. * convertable para's doesn't check for equal, added equal para's to
  854. proc2procvar check
  855. Revision 1.71 1999/06/03 09:34:13 peter
  856. * better methodpointer check for proc->procvar
  857. Revision 1.70 1999/06/02 22:25:55 pierre
  858. types.pas
  859. Revision 1.69 1999/06/02 10:11:55 florian
  860. * make cycle fixed i.e. compilation with 0.99.10
  861. * some fixes for qword
  862. * start of register calling conventions
  863. Revision 1.68 1999/06/01 19:27:58 peter
  864. * better checks for procvar and methodpointer
  865. Revision 1.67 1999/05/31 22:54:19 peter
  866. * when range check error is found then fix the value to be within the
  867. range
  868. Revision 1.66 1999/05/28 11:00:51 peter
  869. * removed ungettempoftype
  870. Revision 1.65 1999/05/23 18:42:23 florian
  871. * better error recovering in typed constants
  872. * some problems with arrays of const fixed, some problems
  873. due my previous
  874. - the location type of array constructor is now LOC_MEM
  875. - the pushing of high fixed
  876. - parameter copying fixed
  877. - zero temp. allocation removed
  878. * small problem in the assembler writers fixed:
  879. ref to nil wasn't written correctly
  880. Revision 1.64 1999/05/19 20:55:08 florian
  881. * fix of my previous commit
  882. Revision 1.63 1999/05/19 20:40:15 florian
  883. * fixed a couple of array related bugs:
  884. - var a : array[0..1] of char; p : pchar; p:=a+123; works now
  885. - open arrays with an odd size doesn't work: movsb wasn't generated
  886. - introduced some new array type helper routines (is_special_array) etc.
  887. - made the array type checking in isconvertable more strict, often
  888. open array can be used where is wasn't allowed etc...
  889. Revision 1.62 1999/05/19 16:48:29 florian
  890. * tdef.typename: returns a now a proper type name for the most types
  891. Revision 1.61 1999/05/19 10:31:56 florian
  892. * two bugs reported by Romio (bugs 13) are fixed:
  893. - empty array constructors are now handled correctly (e.g. for sysutils.format)
  894. - comparsion of ansistrings was sometimes coded wrong
  895. Revision 1.60 1999/05/18 14:16:01 peter
  896. * containsself fixes
  897. * checktypes()
  898. Revision 1.59 1999/05/18 09:52:24 peter
  899. * procedure of object and addrn fixes
  900. Revision 1.58 1999/04/19 09:29:51 pierre
  901. + ungettempoftype(pdef) boolean function
  902. returns true (can call ungetiftemp )
  903. unless the temp should be "unget" with temptoremove
  904. (currently ansistring or widestring !)
  905. Revision 1.57 1999/04/14 09:15:08 peter
  906. * first things to store the symbol/def number in the ppu
  907. Revision 1.56 1999/03/24 23:17:42 peter
  908. * fixed bugs 212,222,225,227,229,231,233
  909. Revision 1.55 1999/03/09 11:45:42 pierre
  910. * small arrays and records (size <=4) are copied directly
  911. Revision 1.54 1999/03/02 22:52:20 peter
  912. * fixed char array, which can start with all possible values
  913. Revision 1.53 1999/02/25 21:02:57 peter
  914. * ag386bin updates
  915. + coff writer
  916. Revision 1.52 1999/02/24 09:51:44 florian
  917. * wrong warning fixed, if a non-virtual method was hidden by a virtual
  918. method (repoerted by Matthias Koeppe)
  919. Revision 1.51 1999/02/22 23:33:31 florian
  920. + message directive for integers added
  921. Revision 1.50 1999/02/22 20:13:42 florian
  922. + first implementation of message keyword
  923. Revision 1.49 1999/02/16 00:45:30 peter
  924. * fixed crashes by forgotten strpnew() for init_symbol
  925. Revision 1.48 1999/02/09 23:03:08 florian
  926. * check for duplicate field names in inherited classes/objects
  927. * bug with self from the mailing list solved (the problem
  928. was that classes were sometimes pushed wrong)
  929. Revision 1.47 1999/01/27 00:14:01 florian
  930. * "procedure of object"-stuff fixed
  931. Revision 1.46 1999/01/21 22:10:54 peter
  932. * fixed array of const
  933. * generic platform independent high() support
  934. Revision 1.45 1999/01/20 12:34:22 peter
  935. * fixed typed file read/write
  936. Revision 1.44 1999/01/15 11:33:03 pierre
  937. * bug in mmx code removed
  938. Revision 1.43 1998/12/30 13:41:20 peter
  939. * released valuepara
  940. Revision 1.42 1998/12/11 00:04:03 peter
  941. + globtype,tokens,version unit splitted from globals
  942. Revision 1.41 1998/12/10 09:47:33 florian
  943. + basic operations with int64/qord (compiler with -dint64)
  944. + rtti of enumerations extended: names are now written
  945. Revision 1.40 1998/12/04 10:18:14 florian
  946. * some stuff for procedures of object added
  947. * bug with overridden virtual constructors fixed (reported by Italo Gomes)
  948. Revision 1.39 1998/11/27 14:50:55 peter
  949. + open strings, $P switch support
  950. Revision 1.38 1998/11/18 15:44:24 peter
  951. * VALUEPARA for tp7 compatible value parameters
  952. Revision 1.37 1998/11/13 10:15:50 peter
  953. * fixed ptr() with constants
  954. Revision 1.36 1998/11/10 10:09:21 peter
  955. * va_list -> array of const
  956. Revision 1.35 1998/10/19 08:55:13 pierre
  957. * wrong stabs info corrected once again !!
  958. + variable vmt offset with vmt field only if required
  959. implemented now !!!
  960. Revision 1.34 1998/10/12 09:50:06 florian
  961. + support of <procedure var type>:=<pointer> in delphi mode added
  962. Revision 1.33 1998/10/06 20:43:30 peter
  963. * fixed set of bugs. like set of false..true set of #1..#255 and
  964. set of #1..true which was allowed
  965. Revision 1.32 1998/10/05 21:33:35 peter
  966. * fixed 161,165,166,167,168
  967. Revision 1.31 1998/09/23 09:58:56 peter
  968. * first working array of const things
  969. Revision 1.30 1998/09/22 15:40:58 peter
  970. * some extra ifdef GDB
  971. Revision 1.29 1998/09/16 12:37:31 michael
  972. Added FPC_ prefix to abstracterror
  973. Revision 1.28 1998/09/09 16:44:23 florian
  974. * I hope, the case bug is fixed now
  975. Revision 1.27 1998/09/07 17:37:07 florian
  976. * first fixes for published properties
  977. Revision 1.26 1998/09/04 12:24:31 florian
  978. * bug0159 fixed
  979. Revision 1.25 1998/09/04 09:06:36 florian
  980. * bug0132 fixed
  981. Revision 1.24 1998/09/04 08:36:49 peter
  982. * fixed boolean:=integer which is not explicit
  983. Revision 1.23 1998/09/01 17:39:55 peter
  984. + internal constant functions
  985. Revision 1.22 1998/09/01 12:53:28 peter
  986. + aktpackenum
  987. Revision 1.21 1998/08/19 00:42:45 peter
  988. + subrange types for enums
  989. + checking for bounds type with ranges
  990. Revision 1.20 1998/08/18 14:17:14 pierre
  991. * bug about assigning the return value of a function to
  992. a procvar fixed : warning
  993. assigning a proc to a procvar need @ in FPC mode !!
  994. * missing file/line info restored
  995. Revision 1.19 1998/08/18 09:24:48 pierre
  996. * small warning position bug fixed
  997. * support_mmx switches splitting was missing
  998. * rhide error and warning output corrected
  999. Revision 1.18 1998/08/14 18:18:49 peter
  1000. + dynamic set contruction
  1001. * smallsets are now working (always longint size)
  1002. Revision 1.17 1998/08/05 16:00:17 florian
  1003. * some fixes for ansi strings
  1004. Revision 1.16 1998/07/20 23:35:50 michael
  1005. Const ansistrings are not copied.
  1006. Revision 1.15 1998/07/18 22:54:32 florian
  1007. * some ansi/wide/longstring support fixed:
  1008. o parameter passing
  1009. o returning as result from functions
  1010. Revision 1.14 1998/06/12 14:50:50 peter
  1011. * removed the tree dependency to types.pas
  1012. * long_fil.pas support (not fully tested yet)
  1013. Revision 1.13 1998/06/03 22:49:07 peter
  1014. + wordbool,longbool
  1015. * rename bis,von -> high,low
  1016. * moved some systemunit loading/creating to psystem.pas
  1017. Revision 1.12 1998/05/12 10:47:00 peter
  1018. * moved printstatus to verb_def
  1019. + V_Normal which is between V_Error and V_Warning and doesn't have a
  1020. prefix like error: warning: and is included in V_Default
  1021. * fixed some messages
  1022. * first time parameter scan is only for -v and -T
  1023. - removed old style messages
  1024. Revision 1.11 1998/05/01 16:38:46 florian
  1025. * handling of private and protected fixed
  1026. + change_keywords_to_tp implemented to remove
  1027. keywords which aren't supported by tp
  1028. * break and continue are now symbols of the system unit
  1029. + widestring, longstring and ansistring type released
  1030. Revision 1.10 1998/04/29 10:34:08 pierre
  1031. + added some code for ansistring (not complete nor working yet)
  1032. * corrected operator overloading
  1033. * corrected nasm output
  1034. + started inline procedures
  1035. + added starstarn : use ** for exponentiation (^ gave problems)
  1036. + started UseTokenInfo cond to get accurate positions
  1037. Revision 1.9 1998/04/21 10:16:49 peter
  1038. * patches from strasbourg
  1039. * objects is not used anymore in the fpc compiled version
  1040. Revision 1.8 1998/04/12 22:39:44 florian
  1041. * problem with read access to properties solved
  1042. * correct handling of hidding methods via virtual (COM)
  1043. * correct result type of constructor calls (COM), the resulttype
  1044. depends now on the type of the class reference
  1045. Revision 1.7 1998/04/10 21:36:56 florian
  1046. + some stuff to support method pointers (procedure of object) added
  1047. (declaration, parameter handling)
  1048. Revision 1.6 1998/04/10 15:39:49 florian
  1049. * more fixes to get classes.pas compiled
  1050. Revision 1.5 1998/04/09 23:02:16 florian
  1051. * small problems solved to get remake3 work
  1052. Revision 1.4 1998/04/08 16:58:09 pierre
  1053. * several bugfixes
  1054. ADD ADC and AND are also sign extended
  1055. nasm output OK (program still crashes at end
  1056. and creates wrong assembler files !!)
  1057. procsym types sym in tdef removed !!
  1058. Revision 1.3 1998/04/08 11:34:22 peter
  1059. * nasm works (linux only tested)
  1060. }