tcadd.pas 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. Type checking and register allocation for add node
  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 tcadd;
  19. interface
  20. uses
  21. tree;
  22. procedure firstadd(var p : ptree);
  23. implementation
  24. uses
  25. globtype,systems,tokens,
  26. cobjects,verbose,globals,
  27. symtable,aasm,types,
  28. hcodegen,htypechk,pass_1
  29. {$ifdef i386}
  30. {$ifdef ag386bin}
  31. ,i386base
  32. {$else}
  33. ,i386
  34. {$endif}
  35. {$endif}
  36. {$ifdef m68k}
  37. ,m68k
  38. {$endif}
  39. ,tccnv
  40. ;
  41. {*****************************************************************************
  42. FirstAdd
  43. *****************************************************************************}
  44. procedure firstadd(var p : ptree);
  45. procedure make_bool_equal_size(var p:ptree);
  46. begin
  47. if porddef(p^.left^.resulttype)^.typ>porddef(p^.right^.resulttype)^.typ then
  48. begin
  49. p^.right:=gentypeconvnode(p^.right,porddef(p^.left^.resulttype));
  50. p^.right^.convtyp:=tc_bool_2_int;
  51. p^.right^.explizit:=true;
  52. firstpass(p^.right);
  53. end
  54. else
  55. if porddef(p^.left^.resulttype)^.typ<porddef(p^.right^.resulttype)^.typ then
  56. begin
  57. p^.left:=gentypeconvnode(p^.left,porddef(p^.right^.resulttype));
  58. p^.left^.convtyp:=tc_bool_2_int;
  59. p^.left^.explizit:=true;
  60. firstpass(p^.left);
  61. end;
  62. end;
  63. var
  64. t,hp : ptree;
  65. ot,
  66. lt,rt : ttreetyp;
  67. rv,lv : longint;
  68. rvd,lvd : bestreal;
  69. rd,ld : pdef;
  70. tempdef : pdef;
  71. concatstrings : boolean;
  72. { to evalute const sets }
  73. resultset : pconstset;
  74. i : longint;
  75. b : boolean;
  76. convdone : boolean;
  77. s1,s2 : pchar;
  78. l1,l2 : longint;
  79. { this totally forgets to set the pi_do_call flag !! }
  80. label
  81. no_overload;
  82. begin
  83. { first do the two subtrees }
  84. firstpass(p^.left);
  85. firstpass(p^.right);
  86. lt:=p^.left^.treetype;
  87. rt:=p^.right^.treetype;
  88. rd:=p^.right^.resulttype;
  89. ld:=p^.left^.resulttype;
  90. convdone:=false;
  91. if codegenerror then
  92. exit;
  93. { overloaded operator ? }
  94. if (p^.treetype=starstarn) or
  95. (ld^.deftype=recorddef) or
  96. { <> and = are defined for classes }
  97. ((ld^.deftype=objectdef) and
  98. (not(pobjectdef(ld)^.isclass) or
  99. not(p^.treetype in [equaln,unequaln])
  100. )
  101. ) or
  102. (rd^.deftype=recorddef) or
  103. { <> and = are defined for classes }
  104. ((rd^.deftype=objectdef) and
  105. (not(pobjectdef(rd)^.isclass) or
  106. not(p^.treetype in [equaln,unequaln])
  107. )
  108. ) then
  109. begin
  110. {!!!!!!!!! handle paras }
  111. case p^.treetype of
  112. { the nil as symtable signs firstcalln that this is
  113. an overloaded operator }
  114. addn:
  115. t:=gencallnode(overloaded_operators[plus],nil);
  116. subn:
  117. t:=gencallnode(overloaded_operators[minus],nil);
  118. muln:
  119. t:=gencallnode(overloaded_operators[star],nil);
  120. starstarn:
  121. t:=gencallnode(overloaded_operators[starstar],nil);
  122. slashn:
  123. t:=gencallnode(overloaded_operators[slash],nil);
  124. ltn:
  125. t:=gencallnode(overloaded_operators[tokens.lt],nil);
  126. gtn:
  127. t:=gencallnode(overloaded_operators[gt],nil);
  128. lten:
  129. t:=gencallnode(overloaded_operators[lte],nil);
  130. gten:
  131. t:=gencallnode(overloaded_operators[gte],nil);
  132. equaln,unequaln :
  133. t:=gencallnode(overloaded_operators[equal],nil);
  134. else goto no_overload;
  135. end;
  136. { we have to convert p^.left and p^.right into
  137. callparanodes }
  138. if t^.symtableprocentry=nil then
  139. begin
  140. CGMessage(parser_e_operator_not_overloaded);
  141. putnode(t);
  142. end
  143. else
  144. begin
  145. t^.left:=gencallparanode(p^.left,nil);
  146. t^.left:=gencallparanode(p^.right,t^.left);
  147. if p^.treetype=unequaln then
  148. t:=gensinglenode(notn,t);
  149. firstpass(t);
  150. putnode(p);
  151. p:=t;
  152. exit;
  153. end;
  154. end;
  155. no_overload:
  156. { compact consts }
  157. { convert int consts to real consts, if the }
  158. { other operand is a real const }
  159. if (rt=realconstn) and is_constintnode(p^.left) then
  160. begin
  161. t:=genrealconstnode(p^.left^.value);
  162. disposetree(p^.left);
  163. p^.left:=t;
  164. lt:=realconstn;
  165. end;
  166. if (lt=realconstn) and is_constintnode(p^.right) then
  167. begin
  168. t:=genrealconstnode(p^.right^.value);
  169. disposetree(p^.right);
  170. p^.right:=t;
  171. rt:=realconstn;
  172. end;
  173. { both are int constants ? }
  174. if is_constintnode(p^.left) and is_constintnode(p^.right) then
  175. begin
  176. lv:=p^.left^.value;
  177. rv:=p^.right^.value;
  178. case p^.treetype of
  179. addn : t:=genordinalconstnode(lv+rv,s32bitdef);
  180. subn : t:=genordinalconstnode(lv-rv,s32bitdef);
  181. muln : t:=genordinalconstnode(lv*rv,s32bitdef);
  182. xorn : t:=genordinalconstnode(lv xor rv,s32bitdef);
  183. orn : t:=genordinalconstnode(lv or rv,s32bitdef);
  184. andn : t:=genordinalconstnode(lv and rv,s32bitdef);
  185. ltn : t:=genordinalconstnode(ord(lv<rv),booldef);
  186. lten : t:=genordinalconstnode(ord(lv<=rv),booldef);
  187. gtn : t:=genordinalconstnode(ord(lv>rv),booldef);
  188. gten : t:=genordinalconstnode(ord(lv>=rv),booldef);
  189. equaln : t:=genordinalconstnode(ord(lv=rv),booldef);
  190. unequaln : t:=genordinalconstnode(ord(lv<>rv),booldef);
  191. slashn : begin
  192. { int/int becomes a real }
  193. if int(rv)=0 then
  194. begin
  195. Message(parser_e_invalid_float_operation);
  196. t:=genrealconstnode(0);
  197. end
  198. else
  199. t:=genrealconstnode(int(lv)/int(rv));
  200. firstpass(t);
  201. end;
  202. else
  203. CGMessage(type_e_mismatch);
  204. end;
  205. disposetree(p);
  206. firstpass(t);
  207. p:=t;
  208. exit;
  209. end;
  210. { both real constants ? }
  211. if (lt=realconstn) and (rt=realconstn) then
  212. begin
  213. lvd:=p^.left^.value_real;
  214. rvd:=p^.right^.value_real;
  215. case p^.treetype of
  216. addn : t:=genrealconstnode(lvd+rvd);
  217. subn : t:=genrealconstnode(lvd-rvd);
  218. muln : t:=genrealconstnode(lvd*rvd);
  219. caretn : t:=genrealconstnode(exp(ln(lvd)*rvd));
  220. slashn : begin
  221. if rvd=0 then
  222. begin
  223. Message(parser_e_invalid_float_operation);
  224. t:=genrealconstnode(0);
  225. end
  226. else
  227. t:=genrealconstnode(lvd/rvd);
  228. end;
  229. ltn : t:=genordinalconstnode(ord(lvd<rvd),booldef);
  230. lten : t:=genordinalconstnode(ord(lvd<=rvd),booldef);
  231. gtn : t:=genordinalconstnode(ord(lvd>rvd),booldef);
  232. gten : t:=genordinalconstnode(ord(lvd>=rvd),booldef);
  233. equaln : t:=genordinalconstnode(ord(lvd=rvd),booldef);
  234. unequaln : t:=genordinalconstnode(ord(lvd<>rvd),booldef);
  235. else
  236. CGMessage(type_e_mismatch);
  237. end;
  238. disposetree(p);
  239. p:=t;
  240. firstpass(p);
  241. exit;
  242. end;
  243. { concating strings ? }
  244. concatstrings:=false;
  245. s1:=nil;
  246. s2:=nil;
  247. if (lt=ordconstn) and (rt=ordconstn) and
  248. is_char(ld) and is_char(rd) then
  249. begin
  250. s1:=strpnew(char(byte(p^.left^.value)));
  251. s2:=strpnew(char(byte(p^.right^.value)));
  252. l1:=1;
  253. l2:=1;
  254. concatstrings:=true;
  255. end
  256. else
  257. if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  258. begin
  259. s1:=getpcharcopy(p^.left);
  260. l1:=p^.left^.length;
  261. s2:=strpnew(char(byte(p^.right^.value)));
  262. l2:=1;
  263. concatstrings:=true;
  264. end
  265. else
  266. if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  267. begin
  268. s1:=strpnew(char(byte(p^.left^.value)));
  269. l1:=1;
  270. s2:=getpcharcopy(p^.right);
  271. l2:=p^.right^.length;
  272. concatstrings:=true;
  273. end
  274. else if (lt=stringconstn) and (rt=stringconstn) then
  275. begin
  276. s1:=getpcharcopy(p^.left);
  277. l1:=p^.left^.length;
  278. s2:=getpcharcopy(p^.right);
  279. l2:=p^.right^.length;
  280. concatstrings:=true;
  281. end;
  282. { I will need to translate all this to ansistrings !!! }
  283. if concatstrings then
  284. begin
  285. case p^.treetype of
  286. addn :
  287. t:=genpcharconstnode(concatansistrings(s1,s2,l1,l2),l1+l2);
  288. ltn :
  289. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)<0),booldef);
  290. lten :
  291. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)<=0),booldef);
  292. gtn :
  293. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)>0),booldef);
  294. gten :
  295. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)>=0),booldef);
  296. equaln :
  297. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)=0),booldef);
  298. unequaln :
  299. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)<>0),booldef);
  300. end;
  301. ansistringdispose(s1,l1);
  302. ansistringdispose(s2,l2);
  303. disposetree(p);
  304. firstpass(t);
  305. p:=t;
  306. exit;
  307. end;
  308. { if both are orddefs then check sub types }
  309. if (ld^.deftype=orddef) and (rd^.deftype=orddef) then
  310. begin
  311. { 2 booleans ? }
  312. if is_boolean(ld) and is_boolean(rd) then
  313. begin
  314. case p^.treetype of
  315. andn,
  316. orn:
  317. begin
  318. calcregisters(p,0,0,0);
  319. make_bool_equal_size(p);
  320. p^.location.loc:=LOC_JUMP;
  321. end;
  322. xorn:
  323. begin
  324. make_bool_equal_size(p);
  325. calcregisters(p,1,0,0);
  326. end;
  327. unequaln,
  328. equaln:
  329. begin
  330. make_bool_equal_size(p);
  331. { Remove any compares with constants, becuase then
  332. we get a compare with Flags in the codegen which
  333. is not supported (PFV) }
  334. if (p^.left^.treetype=ordconstn) then
  335. begin
  336. hp:=p^.right;
  337. b:=(p^.left^.value<>0);
  338. ot:=p^.treetype;
  339. disposetree(p^.left);
  340. putnode(p);
  341. p:=hp;
  342. if (not(b) and (ot=equaln)) or
  343. (b and (ot=unequaln)) then
  344. begin
  345. p:=gensinglenode(notn,p);
  346. firstpass(p);
  347. end;
  348. exit;
  349. end;
  350. if (p^.right^.treetype=ordconstn) then
  351. begin
  352. hp:=p^.left;
  353. b:=(p^.right^.value<>0);
  354. ot:=p^.treetype;
  355. disposetree(p^.right);
  356. putnode(p);
  357. p:=hp;
  358. if (not(b) and (ot=equaln)) or
  359. (b and (ot=unequaln)) then
  360. begin
  361. p:=gensinglenode(notn,p);
  362. firstpass(p);
  363. end;
  364. exit;
  365. end;
  366. calcregisters(p,1,0,0);
  367. end;
  368. else
  369. CGMessage(type_e_mismatch);
  370. end;
  371. convdone:=true;
  372. end
  373. else
  374. { Both are chars? only convert to shortstrings for addn }
  375. if is_char(rd) and is_char(ld) then
  376. begin
  377. if p^.treetype=addn then
  378. begin
  379. p^.left:=gentypeconvnode(p^.left,cshortstringdef);
  380. p^.right:=gentypeconvnode(p^.right,cshortstringdef);
  381. firstpass(p^.left);
  382. firstpass(p^.right);
  383. { here we call STRCOPY }
  384. procinfo.flags:=procinfo.flags or pi_do_call;
  385. calcregisters(p,0,0,0);
  386. p^.location.loc:=LOC_MEM;
  387. end
  388. else
  389. calcregisters(p,1,0,0);
  390. convdone:=true;
  391. end
  392. else
  393. { is there a cardinal? }
  394. if (porddef(rd)^.typ=u32bit) or (porddef(ld)^.typ=u32bit) then
  395. begin
  396. { convert constants to u32bit }
  397. if (porddef(ld)^.typ<>u32bit) then
  398. begin
  399. { s32bit will be used for when the other is also s32bit }
  400. if (porddef(rd)^.typ=s32bit) and (lt<>ordconstn) then
  401. p^.left:=gentypeconvnode(p^.left,s32bitdef)
  402. else
  403. p^.left:=gentypeconvnode(p^.left,u32bitdef);
  404. firstpass(p^.left);
  405. end;
  406. if (porddef(rd)^.typ<>u32bit) then
  407. begin
  408. { s32bit will be used for when the other is also s32bit }
  409. if (porddef(ld)^.typ=s32bit) and (rt<>ordconstn) then
  410. p^.right:=gentypeconvnode(p^.right,s32bitdef)
  411. else
  412. p^.right:=gentypeconvnode(p^.right,u32bitdef);
  413. firstpass(p^.right);
  414. end;
  415. calcregisters(p,1,0,0);
  416. convdone:=true;
  417. end
  418. else if (porddef(rd)^.typ=s64bitint) or (porddef(ld)^.typ=s64bitint) then
  419. begin
  420. if (porddef(ld)^.typ<>s64bitint) then
  421. begin
  422. p^.left:=gentypeconvnode(p^.left,cs64bitintdef);
  423. firstpass(p^.left);
  424. end;
  425. if (porddef(rd)^.typ<>s64bitint) then
  426. begin
  427. p^.right:=gentypeconvnode(p^.right,cs64bitintdef);
  428. firstpass(p^.right);
  429. end;
  430. calcregisters(p,2,0,0);
  431. convdone:=true;
  432. end
  433. else if (porddef(rd)^.typ=u64bit) or (porddef(ld)^.typ=u64bit) then
  434. begin
  435. if (porddef(ld)^.typ<>u64bit) then
  436. begin
  437. p^.left:=gentypeconvnode(p^.left,cu64bitdef);
  438. firstpass(p^.left);
  439. end;
  440. if (porddef(rd)^.typ<>u64bit) then
  441. begin
  442. p^.right:=gentypeconvnode(p^.right,cu64bitdef);
  443. firstpass(p^.right);
  444. end;
  445. calcregisters(p,2,0,0);
  446. convdone:=true;
  447. end;
  448. end
  449. else
  450. { left side a setdef, must be before string processing,
  451. else array constructor can be seen as array of char (PFV) }
  452. if (ld^.deftype=setdef) or is_array_constructor(ld) then
  453. begin
  454. { convert array constructors to sets }
  455. if is_array_constructor(ld) then
  456. begin
  457. arrayconstructor_to_set(p^.left);
  458. ld:=p^.left^.resulttype;
  459. end;
  460. if is_array_constructor(rd) then
  461. begin
  462. arrayconstructor_to_set(p^.right);
  463. rd:=p^.right^.resulttype;
  464. end;
  465. { trying to add a set element? }
  466. if (p^.treetype=addn) and (rd^.deftype<>setdef) then
  467. begin
  468. if (rt=setelementn) then
  469. begin
  470. if not(is_equal(psetdef(ld)^.setof,rd)) then
  471. CGMessage(type_e_set_element_are_not_comp);
  472. end
  473. else
  474. CGMessage(type_e_mismatch)
  475. end
  476. else
  477. begin
  478. if not(p^.treetype in [addn,subn,symdifn,muln,equaln,unequaln
  479. {$IfNDef NoSetInclusion}
  480. ,lten,gten
  481. {$EndIf NoSetInclusion}
  482. ]) then
  483. CGMessage(type_e_set_operation_unknown);
  484. { right def must be a also be set }
  485. if (rd^.deftype<>setdef) or not(is_equal(rd,ld)) then
  486. CGMessage(type_e_set_element_are_not_comp);
  487. end;
  488. { ranges require normsets }
  489. if (psetdef(ld)^.settype=smallset) and
  490. (rt=setelementn) and
  491. assigned(p^.right^.right) then
  492. begin
  493. { generate a temporary normset def }
  494. tempdef:=new(psetdef,init(psetdef(ld)^.setof,255));
  495. p^.left:=gentypeconvnode(p^.left,tempdef);
  496. firstpass(p^.left);
  497. dispose(tempdef,done);
  498. ld:=p^.left^.resulttype;
  499. end;
  500. { if the destination is not a smallset then insert a typeconv
  501. which loads a smallset into a normal set }
  502. if (psetdef(ld)^.settype<>smallset) and
  503. (psetdef(rd)^.settype=smallset) then
  504. begin
  505. if (p^.right^.treetype=setconstn) then
  506. begin
  507. t:=gensetconstnode(p^.right^.value_set,psetdef(p^.left^.resulttype));
  508. t^.left:=p^.right^.left;
  509. putnode(p^.right);
  510. p^.right:=t;
  511. end
  512. else
  513. p^.right:=gentypeconvnode(p^.right,psetdef(p^.left^.resulttype));
  514. firstpass(p^.right);
  515. end;
  516. { do constant evaluation }
  517. if (p^.right^.treetype=setconstn) and
  518. not assigned(p^.right^.left) and
  519. (p^.left^.treetype=setconstn) and
  520. not assigned(p^.left^.left) then
  521. begin
  522. new(resultset);
  523. case p^.treetype of
  524. addn : begin
  525. for i:=0 to 31 do
  526. resultset^[i]:=
  527. p^.right^.value_set^[i] or p^.left^.value_set^[i];
  528. t:=gensetconstnode(resultset,psetdef(ld));
  529. end;
  530. muln : begin
  531. for i:=0 to 31 do
  532. resultset^[i]:=
  533. p^.right^.value_set^[i] and p^.left^.value_set^[i];
  534. t:=gensetconstnode(resultset,psetdef(ld));
  535. end;
  536. subn : begin
  537. for i:=0 to 31 do
  538. resultset^[i]:=
  539. p^.left^.value_set^[i] and not(p^.right^.value_set^[i]);
  540. t:=gensetconstnode(resultset,psetdef(ld));
  541. end;
  542. symdifn : begin
  543. for i:=0 to 31 do
  544. resultset^[i]:=
  545. p^.left^.value_set^[i] xor p^.right^.value_set^[i];
  546. t:=gensetconstnode(resultset,psetdef(ld));
  547. end;
  548. unequaln : begin
  549. b:=true;
  550. for i:=0 to 31 do
  551. if p^.right^.value_set^[i]=p^.left^.value_set^[i] then
  552. begin
  553. b:=false;
  554. break;
  555. end;
  556. t:=genordinalconstnode(ord(b),booldef);
  557. end;
  558. equaln : begin
  559. b:=true;
  560. for i:=0 to 31 do
  561. if p^.right^.value_set^[i]<>p^.left^.value_set^[i] then
  562. begin
  563. b:=false;
  564. break;
  565. end;
  566. t:=genordinalconstnode(ord(b),booldef);
  567. end;
  568. {$IfNDef NoSetInclusion}
  569. lten : Begin
  570. b := true;
  571. For i := 0 to 31 Do
  572. If (p^.right^.value_set^[i] And p^.left^.value_set^[i]) <>
  573. p^.left^.value_set^[i] Then
  574. Begin
  575. b := false;
  576. Break
  577. End;
  578. t := genordinalconstnode(ord(b),booldef);
  579. End;
  580. gten : Begin
  581. b := true;
  582. For i := 0 to 31 Do
  583. If (p^.left^.value_set^[i] And p^.right^.value_set^[i]) <>
  584. p^.right^.value_set^[i] Then
  585. Begin
  586. b := false;
  587. Break
  588. End;
  589. t := genordinalconstnode(ord(b),booldef);
  590. End;
  591. {$EndIf NoSetInclusion}
  592. end;
  593. dispose(resultset);
  594. disposetree(p);
  595. p:=t;
  596. firstpass(p);
  597. exit;
  598. end
  599. else
  600. if psetdef(ld)^.settype=smallset then
  601. begin
  602. calcregisters(p,1,0,0);
  603. p^.location.loc:=LOC_REGISTER;
  604. end
  605. else
  606. begin
  607. calcregisters(p,0,0,0);
  608. { here we call SET... }
  609. procinfo.flags:=procinfo.flags or pi_do_call;
  610. p^.location.loc:=LOC_MEM;
  611. end;
  612. convdone:=true;
  613. end
  614. else
  615. { is one of the operands a string?,
  616. chararrays are also handled as strings (after conversion) }
  617. if (rd^.deftype=stringdef) or (ld^.deftype=stringdef) or
  618. (is_chararray(rd) and is_chararray(ld)) then
  619. begin
  620. if is_widestring(rd) or is_widestring(ld) then
  621. begin
  622. if not(is_widestring(rd)) then
  623. p^.right:=gentypeconvnode(p^.right,cwidestringdef);
  624. if not(is_widestring(ld)) then
  625. p^.left:=gentypeconvnode(p^.left,cwidestringdef);
  626. p^.resulttype:=cwidestringdef;
  627. { this is only for add, the comparisaion is handled later }
  628. p^.location.loc:=LOC_REGISTER;
  629. end
  630. else if is_ansistring(rd) or is_ansistring(ld) then
  631. begin
  632. if not(is_ansistring(rd)) then
  633. p^.right:=gentypeconvnode(p^.right,cansistringdef);
  634. if not(is_ansistring(ld)) then
  635. p^.left:=gentypeconvnode(p^.left,cansistringdef);
  636. p^.resulttype:=cansistringdef;
  637. { this is only for add, the comparisaion is handled later }
  638. p^.location.loc:=LOC_REGISTER;
  639. end
  640. else if is_longstring(rd) or is_longstring(ld) then
  641. begin
  642. if not(is_longstring(rd)) then
  643. p^.right:=gentypeconvnode(p^.right,clongstringdef);
  644. if not(is_longstring(ld)) then
  645. p^.left:=gentypeconvnode(p^.left,clongstringdef);
  646. p^.resulttype:=clongstringdef;
  647. { this is only for add, the comparisaion is handled later }
  648. p^.location.loc:=LOC_MEM;
  649. end
  650. else
  651. begin
  652. if not(is_shortstring(rd)) then
  653. p^.right:=gentypeconvnode(p^.right,cshortstringdef);
  654. if not(is_shortstring(ld)) then
  655. p^.left:=gentypeconvnode(p^.left,cshortstringdef);
  656. p^.resulttype:=cshortstringdef;
  657. { this is only for add, the comparisaion is handled later }
  658. p^.location.loc:=LOC_MEM;
  659. end;
  660. { only if there is a type cast we need to do again }
  661. { the first pass }
  662. if p^.left^.treetype=typeconvn then
  663. firstpass(p^.left);
  664. if p^.right^.treetype=typeconvn then
  665. firstpass(p^.right);
  666. { here we call STRCONCAT or STRCMP or STRCOPY }
  667. procinfo.flags:=procinfo.flags or pi_do_call;
  668. if p^.location.loc=LOC_MEM then
  669. calcregisters(p,0,0,0)
  670. else
  671. calcregisters(p,1,0,0);
  672. convdone:=true;
  673. end
  674. else
  675. { is one a real float ? }
  676. if (rd^.deftype=floatdef) or (ld^.deftype=floatdef) then
  677. begin
  678. { if one is a fixed, then convert to f32bit }
  679. if ((rd^.deftype=floatdef) and (pfloatdef(rd)^.typ=f32bit)) or
  680. ((ld^.deftype=floatdef) and (pfloatdef(ld)^.typ=f32bit)) then
  681. begin
  682. if not is_integer(rd) or (p^.treetype<>muln) then
  683. p^.right:=gentypeconvnode(p^.right,s32fixeddef);
  684. if not is_integer(ld) or (p^.treetype<>muln) then
  685. p^.left:=gentypeconvnode(p^.left,s32fixeddef);
  686. firstpass(p^.left);
  687. firstpass(p^.right);
  688. calcregisters(p,1,0,0);
  689. p^.location.loc:=LOC_REGISTER;
  690. end
  691. else
  692. { convert both to c64float }
  693. begin
  694. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  695. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  696. firstpass(p^.left);
  697. firstpass(p^.right);
  698. calcregisters(p,1,1,0);
  699. p^.location.loc:=LOC_FPU;
  700. end;
  701. convdone:=true;
  702. end
  703. else
  704. { pointer comperation and subtraction }
  705. if (rd^.deftype=pointerdef) and (ld^.deftype=pointerdef) then
  706. begin
  707. p^.location.loc:=LOC_REGISTER;
  708. p^.right:=gentypeconvnode(p^.right,ld);
  709. firstpass(p^.right);
  710. calcregisters(p,1,0,0);
  711. case p^.treetype of
  712. equaln,unequaln : ;
  713. ltn,lten,gtn,gten:
  714. begin
  715. if not(cs_extsyntax in aktmoduleswitches) then
  716. CGMessage(type_e_mismatch);
  717. end;
  718. subn:
  719. begin
  720. if not(cs_extsyntax in aktmoduleswitches) then
  721. CGMessage(type_e_mismatch);
  722. p^.resulttype:=s32bitdef;
  723. exit;
  724. end;
  725. else CGMessage(type_e_mismatch);
  726. end;
  727. convdone:=true;
  728. end
  729. else
  730. if (rd^.deftype=objectdef) and (ld^.deftype=objectdef) and
  731. pobjectdef(rd)^.isclass and pobjectdef(ld)^.isclass then
  732. begin
  733. p^.location.loc:=LOC_REGISTER;
  734. if pobjectdef(rd)^.isrelated(pobjectdef(ld)) then
  735. p^.right:=gentypeconvnode(p^.right,ld)
  736. else
  737. p^.left:=gentypeconvnode(p^.left,rd);
  738. firstpass(p^.right);
  739. firstpass(p^.left);
  740. calcregisters(p,1,0,0);
  741. case p^.treetype of
  742. equaln,unequaln : ;
  743. else CGMessage(type_e_mismatch);
  744. end;
  745. convdone:=true;
  746. end
  747. else
  748. if (rd^.deftype=classrefdef) and (ld^.deftype=classrefdef) then
  749. begin
  750. p^.location.loc:=LOC_REGISTER;
  751. if pobjectdef(pclassrefdef(rd)^.definition)^.isrelated(pobjectdef(
  752. pclassrefdef(ld)^.definition)) then
  753. p^.right:=gentypeconvnode(p^.right,ld)
  754. else
  755. p^.left:=gentypeconvnode(p^.left,rd);
  756. firstpass(p^.right);
  757. firstpass(p^.left);
  758. calcregisters(p,1,0,0);
  759. case p^.treetype of
  760. equaln,unequaln : ;
  761. else CGMessage(type_e_mismatch);
  762. end;
  763. convdone:=true;
  764. end
  765. else
  766. { allows comperasion with nil pointer }
  767. if (rd^.deftype=objectdef) and
  768. pobjectdef(rd)^.isclass then
  769. begin
  770. p^.location.loc:=LOC_REGISTER;
  771. p^.left:=gentypeconvnode(p^.left,rd);
  772. firstpass(p^.left);
  773. calcregisters(p,1,0,0);
  774. case p^.treetype of
  775. equaln,unequaln : ;
  776. else CGMessage(type_e_mismatch);
  777. end;
  778. convdone:=true;
  779. end
  780. else
  781. if (ld^.deftype=objectdef) and
  782. pobjectdef(ld)^.isclass then
  783. begin
  784. p^.location.loc:=LOC_REGISTER;
  785. p^.right:=gentypeconvnode(p^.right,ld);
  786. firstpass(p^.right);
  787. calcregisters(p,1,0,0);
  788. case p^.treetype of
  789. equaln,unequaln : ;
  790. else CGMessage(type_e_mismatch);
  791. end;
  792. convdone:=true;
  793. end
  794. else
  795. if (rd^.deftype=classrefdef) then
  796. begin
  797. p^.left:=gentypeconvnode(p^.left,rd);
  798. firstpass(p^.left);
  799. calcregisters(p,1,0,0);
  800. case p^.treetype of
  801. equaln,unequaln : ;
  802. else CGMessage(type_e_mismatch);
  803. end;
  804. convdone:=true;
  805. end
  806. else
  807. if (ld^.deftype=classrefdef) then
  808. begin
  809. p^.right:=gentypeconvnode(p^.right,ld);
  810. firstpass(p^.right);
  811. calcregisters(p,1,0,0);
  812. case p^.treetype of
  813. equaln,unequaln : ;
  814. else
  815. CGMessage(type_e_mismatch);
  816. end;
  817. convdone:=true;
  818. end
  819. else
  820. if (rd^.deftype=pointerdef) then
  821. begin
  822. p^.location.loc:=LOC_REGISTER;
  823. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  824. firstpass(p^.left);
  825. calcregisters(p,1,0,0);
  826. if p^.treetype=addn then
  827. begin
  828. if not(cs_extsyntax in aktmoduleswitches) then
  829. CGMessage(type_e_mismatch);
  830. end
  831. else
  832. CGMessage(type_e_mismatch);
  833. convdone:=true;
  834. end
  835. else
  836. if (ld^.deftype=pointerdef) then
  837. begin
  838. p^.location.loc:=LOC_REGISTER;
  839. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  840. firstpass(p^.right);
  841. calcregisters(p,1,0,0);
  842. case p^.treetype of
  843. addn,subn : begin
  844. if not(cs_extsyntax in aktmoduleswitches) or
  845. (not(is_pchar(ld)) and (m_tp in aktmodeswitches)) then
  846. CGMessage(type_e_mismatch);
  847. end;
  848. else
  849. CGMessage(type_e_mismatch);
  850. end;
  851. convdone:=true;
  852. end
  853. else
  854. if (rd^.deftype=procvardef) and (ld^.deftype=procvardef) and is_equal(rd,ld) then
  855. begin
  856. calcregisters(p,1,0,0);
  857. p^.location.loc:=LOC_REGISTER;
  858. case p^.treetype of
  859. equaln,unequaln : ;
  860. else
  861. CGMessage(type_e_mismatch);
  862. end;
  863. convdone:=true;
  864. end
  865. else
  866. {$ifdef SUPPORT_MMX}
  867. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  868. is_mmx_able_array(rd) and is_equal(ld,rd) then
  869. begin
  870. firstpass(p^.right);
  871. firstpass(p^.left);
  872. case p^.treetype of
  873. addn,subn,xorn,orn,andn:
  874. ;
  875. { mul is a little bit restricted }
  876. muln:
  877. if not(mmx_type(p^.left^.resulttype) in
  878. [mmxu16bit,mmxs16bit,mmxfixed16]) then
  879. CGMessage(type_e_mismatch);
  880. else
  881. CGMessage(type_e_mismatch);
  882. end;
  883. p^.location.loc:=LOC_MMXREGISTER;
  884. calcregisters(p,0,0,1);
  885. convdone:=true;
  886. end
  887. else
  888. {$endif SUPPORT_MMX}
  889. if (ld^.deftype=enumdef) and (rd^.deftype=enumdef) and (is_equal(ld,rd)) then
  890. begin
  891. calcregisters(p,1,0,0);
  892. case p^.treetype of
  893. equaln,unequaln,
  894. ltn,lten,gtn,gten : ;
  895. else CGMessage(type_e_mismatch);
  896. end;
  897. convdone:=true;
  898. end;
  899. { the general solution is to convert to 32 bit int }
  900. if not convdone then
  901. begin
  902. { but an int/int gives real/real! }
  903. if p^.treetype=slashn then
  904. begin
  905. CGMessage(type_h_use_div_for_int);
  906. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  907. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  908. firstpass(p^.left);
  909. firstpass(p^.right);
  910. { maybe we need an integer register to save }
  911. { a reference }
  912. if ((p^.left^.location.loc<>LOC_FPU) or
  913. (p^.right^.location.loc<>LOC_FPU)) and
  914. (p^.left^.registers32=p^.right^.registers32) then
  915. calcregisters(p,1,1,0)
  916. else
  917. calcregisters(p,0,1,0);
  918. p^.location.loc:=LOC_FPU;
  919. end
  920. else
  921. begin
  922. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  923. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  924. firstpass(p^.left);
  925. firstpass(p^.right);
  926. calcregisters(p,1,0,0);
  927. p^.location.loc:=LOC_REGISTER;
  928. end;
  929. end;
  930. if codegenerror then
  931. exit;
  932. { determines result type for comparions }
  933. { here the is a problem with multiple passes }
  934. { example length(s)+1 gets internal 'longint' type first }
  935. { if it is a arg it is converted to 'LONGINT' }
  936. { but a second first pass will reset this to 'longint' }
  937. case p^.treetype of
  938. ltn,lten,gtn,gten,equaln,unequaln:
  939. begin
  940. if (not assigned(p^.resulttype)) or
  941. (p^.resulttype^.deftype=stringdef) then
  942. p^.resulttype:=booldef;
  943. if is_64bitint(p^.left^.resulttype) then
  944. p^.location.loc:=LOC_JUMP
  945. else
  946. p^.location.loc:=LOC_FLAGS;
  947. end;
  948. xorn:
  949. begin
  950. if not assigned(p^.resulttype) then
  951. p^.resulttype:=p^.left^.resulttype;
  952. p^.location.loc:=LOC_REGISTER;
  953. end;
  954. addn:
  955. begin
  956. if not assigned(p^.resulttype) then
  957. begin
  958. { for strings, return is always a 255 char string }
  959. if is_shortstring(p^.left^.resulttype) then
  960. p^.resulttype:=cshortstringdef
  961. else
  962. p^.resulttype:=p^.left^.resulttype;
  963. end;
  964. end;
  965. else
  966. p^.resulttype:=p^.left^.resulttype;
  967. end;
  968. end;
  969. end.
  970. {
  971. $Log$
  972. Revision 1.25 1999-04-15 09:01:34 peter
  973. * fixed set loading
  974. * object inheritance support for browser
  975. Revision 1.24 1999/04/08 11:34:00 peter
  976. * int/int warning removed, only the hint is left
  977. Revision 1.23 1999/03/02 22:52:19 peter
  978. * fixed char array, which can start with all possible values
  979. Revision 1.22 1999/02/22 02:15:43 peter
  980. * updates for ag386bin
  981. Revision 1.21 1999/01/20 21:05:09 peter
  982. * fixed set operations which still had array constructor as type
  983. Revision 1.20 1999/01/20 17:39:26 jonas
  984. + fixed bug0163 (set1 <= set2 support)
  985. Revision 1.19 1998/12/30 13:35:35 peter
  986. * fix for boolean=true compares
  987. Revision 1.18 1998/12/15 17:12:35 peter
  988. * pointer+ord not allowed in tp mode
  989. Revision 1.17 1998/12/11 00:03:51 peter
  990. + globtype,tokens,version unit splitted from globals
  991. Revision 1.16 1998/12/10 09:47:31 florian
  992. + basic operations with int64/qord (compiler with -dint64)
  993. + rtti of enumerations extended: names are now written
  994. Revision 1.15 1998/11/24 22:59:05 peter
  995. * handle array of char the same as strings
  996. Revision 1.14 1998/11/17 00:36:47 peter
  997. * more ansistring fixes
  998. Revision 1.13 1998/11/16 15:33:05 peter
  999. * fixed return for ansistrings
  1000. Revision 1.12 1998/11/05 14:28:16 peter
  1001. * fixed unknown set operation msg
  1002. Revision 1.11 1998/11/05 12:03:02 peter
  1003. * released useansistring
  1004. * removed -Sv, its now available in fpc modes
  1005. Revision 1.10 1998/11/04 10:11:46 peter
  1006. * ansistring fixes
  1007. Revision 1.9 1998/10/25 23:32:04 peter
  1008. * fixed u32bit - s32bit conversion problems
  1009. Revision 1.8 1998/10/22 12:12:28 pierre
  1010. + better error info on unimplemented set operators
  1011. Revision 1.7 1998/10/21 15:12:57 pierre
  1012. * bug fix for IOCHECK inside a procedure with iocheck modifier
  1013. * removed the GPF for unexistant overloading
  1014. (firstcall was called with procedinition=nil !)
  1015. * changed typen to what Florian proposed
  1016. gentypenode(p : pdef) sets the typenodetype field
  1017. and resulttype is only set if inside bt_type block !
  1018. Revision 1.6 1998/10/20 15:09:24 florian
  1019. + binary operators for ansi strings
  1020. Revision 1.5 1998/10/20 08:07:05 pierre
  1021. * several memory corruptions due to double freemem solved
  1022. => never use p^.loc.location:=p^.left^.loc.location;
  1023. + finally I added now by default
  1024. that ra386dir translates global and unit symbols
  1025. + added a first field in tsymtable and
  1026. a nextsym field in tsym
  1027. (this allows to obtain ordered type info for
  1028. records and objects in gdb !)
  1029. Revision 1.4 1998/10/14 12:53:39 peter
  1030. * fixed small tp7 things
  1031. * boolean:=longbool and longbool fixed
  1032. Revision 1.3 1998/10/11 14:31:19 peter
  1033. + checks for division by zero
  1034. Revision 1.2 1998/10/05 21:33:31 peter
  1035. * fixed 161,165,166,167,168
  1036. Revision 1.1 1998/09/23 20:42:24 peter
  1037. * splitted pass_1
  1038. }