tcadd.pas 41 KB

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