tcadd.pas 38 KB

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