tcadd.pas 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. { is one of the operands a string?,
  451. chararrays are also handled as strings (after conversion) }
  452. if (rd^.deftype=stringdef) or (ld^.deftype=stringdef) or
  453. (is_chararray(rd) and is_chararray(ld)) then
  454. begin
  455. if is_widestring(rd) or is_widestring(ld) then
  456. begin
  457. if not(is_widestring(rd)) then
  458. p^.right:=gentypeconvnode(p^.right,cwidestringdef);
  459. if not(is_widestring(ld)) then
  460. p^.left:=gentypeconvnode(p^.left,cwidestringdef);
  461. p^.resulttype:=cwidestringdef;
  462. { this is only for add, the comparisaion is handled later }
  463. p^.location.loc:=LOC_REGISTER;
  464. end
  465. else if is_ansistring(rd) or is_ansistring(ld) then
  466. begin
  467. if not(is_ansistring(rd)) then
  468. p^.right:=gentypeconvnode(p^.right,cansistringdef);
  469. if not(is_ansistring(ld)) then
  470. p^.left:=gentypeconvnode(p^.left,cansistringdef);
  471. p^.resulttype:=cansistringdef;
  472. { this is only for add, the comparisaion is handled later }
  473. p^.location.loc:=LOC_REGISTER;
  474. end
  475. else if is_longstring(rd) or is_longstring(ld) then
  476. begin
  477. if not(is_longstring(rd)) then
  478. p^.right:=gentypeconvnode(p^.right,clongstringdef);
  479. if not(is_longstring(ld)) then
  480. p^.left:=gentypeconvnode(p^.left,clongstringdef);
  481. p^.resulttype:=clongstringdef;
  482. { this is only for add, the comparisaion is handled later }
  483. p^.location.loc:=LOC_MEM;
  484. end
  485. else
  486. begin
  487. if not(is_shortstring(rd)) then
  488. p^.right:=gentypeconvnode(p^.right,cshortstringdef);
  489. if not(is_shortstring(ld)) then
  490. p^.left:=gentypeconvnode(p^.left,cshortstringdef);
  491. p^.resulttype:=cshortstringdef;
  492. { this is only for add, the comparisaion is handled later }
  493. p^.location.loc:=LOC_MEM;
  494. end;
  495. { only if there is a type cast we need to do again }
  496. { the first pass }
  497. if p^.left^.treetype=typeconvn then
  498. firstpass(p^.left);
  499. if p^.right^.treetype=typeconvn then
  500. firstpass(p^.right);
  501. { here we call STRCONCAT or STRCMP or STRCOPY }
  502. procinfo.flags:=procinfo.flags or pi_do_call;
  503. if p^.location.loc=LOC_MEM then
  504. calcregisters(p,0,0,0)
  505. else
  506. calcregisters(p,1,0,0);
  507. convdone:=true;
  508. end
  509. else
  510. { left side a setdef ? }
  511. if (ld^.deftype=setdef) or is_array_constructor(ld) then
  512. begin
  513. { convert array constructors to sets }
  514. if is_array_constructor(ld) then
  515. begin
  516. arrayconstructor_to_set(p^.left);
  517. ld:=p^.left^.resulttype;
  518. end;
  519. if is_array_constructor(rd) then
  520. begin
  521. arrayconstructor_to_set(p^.right);
  522. rd:=p^.right^.resulttype;
  523. end;
  524. { trying to add a set element? }
  525. if (p^.treetype=addn) and (rd^.deftype<>setdef) then
  526. begin
  527. if (rt=setelementn) then
  528. begin
  529. if not(is_equal(psetdef(ld)^.setof,rd)) then
  530. CGMessage(type_e_set_element_are_not_comp);
  531. end
  532. else
  533. CGMessage(type_e_mismatch)
  534. end
  535. else
  536. begin
  537. if not(p^.treetype in [addn,subn,symdifn,muln,equaln,unequaln
  538. {$IfNDef NoSetInclusion}
  539. ,lten,gten
  540. {$EndIf NoSetInclusion}
  541. ]) then
  542. CGMessage(type_e_set_operation_unknown);
  543. { right def must be a also be set }
  544. if (rd^.deftype<>setdef) or not(is_equal(rd,ld)) then
  545. CGMessage(type_e_set_element_are_not_comp);
  546. end;
  547. { ranges require normsets }
  548. if (psetdef(ld)^.settype=smallset) and
  549. (rt=setelementn) and
  550. assigned(p^.right^.right) then
  551. begin
  552. { generate a temporary normset def }
  553. tempdef:=new(psetdef,init(psetdef(ld)^.setof,255));
  554. p^.left:=gentypeconvnode(p^.left,tempdef);
  555. firstpass(p^.left);
  556. dispose(tempdef,done);
  557. ld:=p^.left^.resulttype;
  558. end;
  559. { if the destination is not a smallset then insert a typeconv
  560. which loads a smallset into a normal set }
  561. if (psetdef(ld)^.settype<>smallset) and
  562. (psetdef(rd)^.settype=smallset) then
  563. begin
  564. if (p^.right^.treetype=setconstn) then
  565. begin
  566. t:=gensetconstnode(p^.right^.value_set,psetdef(p^.left^.resulttype));
  567. t^.left:=p^.right^.left;
  568. putnode(p^.right);
  569. p^.right:=t;
  570. end
  571. else
  572. p^.right:=gentypeconvnode(p^.right,psetdef(p^.left^.resulttype));
  573. firstpass(p^.right);
  574. end;
  575. { do constant evaluation }
  576. if (p^.right^.treetype=setconstn) and
  577. not assigned(p^.right^.left) and
  578. (p^.left^.treetype=setconstn) and
  579. not assigned(p^.left^.left) then
  580. begin
  581. new(resultset);
  582. case p^.treetype of
  583. addn : begin
  584. for i:=0 to 31 do
  585. resultset^[i]:=
  586. p^.right^.value_set^[i] or p^.left^.value_set^[i];
  587. t:=gensetconstnode(resultset,psetdef(ld));
  588. end;
  589. muln : begin
  590. for i:=0 to 31 do
  591. resultset^[i]:=
  592. p^.right^.value_set^[i] and p^.left^.value_set^[i];
  593. t:=gensetconstnode(resultset,psetdef(ld));
  594. end;
  595. subn : begin
  596. for i:=0 to 31 do
  597. resultset^[i]:=
  598. p^.left^.value_set^[i] and not(p^.right^.value_set^[i]);
  599. t:=gensetconstnode(resultset,psetdef(ld));
  600. end;
  601. symdifn : begin
  602. for i:=0 to 31 do
  603. resultset^[i]:=
  604. p^.left^.value_set^[i] xor p^.right^.value_set^[i];
  605. t:=gensetconstnode(resultset,psetdef(ld));
  606. end;
  607. unequaln : begin
  608. b:=true;
  609. for i:=0 to 31 do
  610. if p^.right^.value_set^[i]=p^.left^.value_set^[i] then
  611. begin
  612. b:=false;
  613. break;
  614. end;
  615. t:=genordinalconstnode(ord(b),booldef);
  616. end;
  617. equaln : begin
  618. b:=true;
  619. for i:=0 to 31 do
  620. if p^.right^.value_set^[i]<>p^.left^.value_set^[i] then
  621. begin
  622. b:=false;
  623. break;
  624. end;
  625. t:=genordinalconstnode(ord(b),booldef);
  626. end;
  627. {$IfNDef NoSetInclusion}
  628. lten : Begin
  629. b := true;
  630. For i := 0 to 31 Do
  631. If (p^.right^.value_set^[i] And p^.left^.value_set^[i]) <>
  632. p^.left^.value_set^[i] Then
  633. Begin
  634. b := false;
  635. Break
  636. End;
  637. t := genordinalconstnode(ord(b),booldef);
  638. End;
  639. gten : Begin
  640. b := true;
  641. For i := 0 to 31 Do
  642. If (p^.left^.value_set^[i] And p^.right^.value_set^[i]) <>
  643. p^.right^.value_set^[i] Then
  644. Begin
  645. b := false;
  646. Break
  647. End;
  648. t := genordinalconstnode(ord(b),booldef);
  649. End;
  650. {$EndIf NoSetInclusion}
  651. end;
  652. dispose(resultset);
  653. disposetree(p);
  654. p:=t;
  655. firstpass(p);
  656. exit;
  657. end
  658. else
  659. if psetdef(ld)^.settype=smallset then
  660. begin
  661. calcregisters(p,1,0,0);
  662. p^.location.loc:=LOC_REGISTER;
  663. end
  664. else
  665. begin
  666. calcregisters(p,0,0,0);
  667. { here we call SET... }
  668. procinfo.flags:=procinfo.flags or pi_do_call;
  669. p^.location.loc:=LOC_MEM;
  670. end;
  671. convdone:=true;
  672. end
  673. else
  674. { is one a real float ? }
  675. if (rd^.deftype=floatdef) or (ld^.deftype=floatdef) then
  676. begin
  677. { if one is a fixed, then convert to f32bit }
  678. if ((rd^.deftype=floatdef) and (pfloatdef(rd)^.typ=f32bit)) or
  679. ((ld^.deftype=floatdef) and (pfloatdef(ld)^.typ=f32bit)) then
  680. begin
  681. if not is_integer(rd) or (p^.treetype<>muln) then
  682. p^.right:=gentypeconvnode(p^.right,s32fixeddef);
  683. if not is_integer(ld) or (p^.treetype<>muln) then
  684. p^.left:=gentypeconvnode(p^.left,s32fixeddef);
  685. firstpass(p^.left);
  686. firstpass(p^.right);
  687. calcregisters(p,1,0,0);
  688. p^.location.loc:=LOC_REGISTER;
  689. end
  690. else
  691. { convert both to c64float }
  692. begin
  693. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  694. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  695. firstpass(p^.left);
  696. firstpass(p^.right);
  697. calcregisters(p,1,1,0);
  698. p^.location.loc:=LOC_FPU;
  699. end;
  700. convdone:=true;
  701. end
  702. else
  703. { pointer comperation and subtraction }
  704. if (rd^.deftype=pointerdef) and (ld^.deftype=pointerdef) then
  705. begin
  706. p^.location.loc:=LOC_REGISTER;
  707. p^.right:=gentypeconvnode(p^.right,ld);
  708. firstpass(p^.right);
  709. calcregisters(p,1,0,0);
  710. case p^.treetype of
  711. equaln,unequaln : ;
  712. ltn,lten,gtn,gten:
  713. begin
  714. if not(cs_extsyntax in aktmoduleswitches) then
  715. CGMessage(type_e_mismatch);
  716. end;
  717. subn:
  718. begin
  719. if not(cs_extsyntax in aktmoduleswitches) then
  720. CGMessage(type_e_mismatch);
  721. p^.resulttype:=s32bitdef;
  722. exit;
  723. end;
  724. else CGMessage(type_e_mismatch);
  725. end;
  726. convdone:=true;
  727. end
  728. else
  729. if (rd^.deftype=objectdef) and (ld^.deftype=objectdef) and
  730. pobjectdef(rd)^.isclass and pobjectdef(ld)^.isclass then
  731. begin
  732. p^.location.loc:=LOC_REGISTER;
  733. if pobjectdef(rd)^.isrelated(pobjectdef(ld)) then
  734. p^.right:=gentypeconvnode(p^.right,ld)
  735. else
  736. p^.left:=gentypeconvnode(p^.left,rd);
  737. firstpass(p^.right);
  738. firstpass(p^.left);
  739. calcregisters(p,1,0,0);
  740. case p^.treetype of
  741. equaln,unequaln : ;
  742. else CGMessage(type_e_mismatch);
  743. end;
  744. convdone:=true;
  745. end
  746. else
  747. if (rd^.deftype=classrefdef) and (ld^.deftype=classrefdef) then
  748. begin
  749. p^.location.loc:=LOC_REGISTER;
  750. if pobjectdef(pclassrefdef(rd)^.definition)^.isrelated(pobjectdef(
  751. pclassrefdef(ld)^.definition)) then
  752. p^.right:=gentypeconvnode(p^.right,ld)
  753. else
  754. p^.left:=gentypeconvnode(p^.left,rd);
  755. firstpass(p^.right);
  756. firstpass(p^.left);
  757. calcregisters(p,1,0,0);
  758. case p^.treetype of
  759. equaln,unequaln : ;
  760. else CGMessage(type_e_mismatch);
  761. end;
  762. convdone:=true;
  763. end
  764. else
  765. { allows comperasion with nil pointer }
  766. if (rd^.deftype=objectdef) and
  767. pobjectdef(rd)^.isclass then
  768. begin
  769. p^.location.loc:=LOC_REGISTER;
  770. p^.left:=gentypeconvnode(p^.left,rd);
  771. firstpass(p^.left);
  772. calcregisters(p,1,0,0);
  773. case p^.treetype of
  774. equaln,unequaln : ;
  775. else CGMessage(type_e_mismatch);
  776. end;
  777. convdone:=true;
  778. end
  779. else
  780. if (ld^.deftype=objectdef) and
  781. pobjectdef(ld)^.isclass then
  782. begin
  783. p^.location.loc:=LOC_REGISTER;
  784. p^.right:=gentypeconvnode(p^.right,ld);
  785. firstpass(p^.right);
  786. calcregisters(p,1,0,0);
  787. case p^.treetype of
  788. equaln,unequaln : ;
  789. else CGMessage(type_e_mismatch);
  790. end;
  791. convdone:=true;
  792. end
  793. else
  794. if (rd^.deftype=classrefdef) then
  795. begin
  796. p^.left:=gentypeconvnode(p^.left,rd);
  797. firstpass(p^.left);
  798. calcregisters(p,1,0,0);
  799. case p^.treetype of
  800. equaln,unequaln : ;
  801. else CGMessage(type_e_mismatch);
  802. end;
  803. convdone:=true;
  804. end
  805. else
  806. if (ld^.deftype=classrefdef) then
  807. begin
  808. p^.right:=gentypeconvnode(p^.right,ld);
  809. firstpass(p^.right);
  810. calcregisters(p,1,0,0);
  811. case p^.treetype of
  812. equaln,unequaln : ;
  813. else
  814. CGMessage(type_e_mismatch);
  815. end;
  816. convdone:=true;
  817. end
  818. else
  819. if (rd^.deftype=pointerdef) then
  820. begin
  821. p^.location.loc:=LOC_REGISTER;
  822. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  823. firstpass(p^.left);
  824. calcregisters(p,1,0,0);
  825. if p^.treetype=addn then
  826. begin
  827. if not(cs_extsyntax in aktmoduleswitches) then
  828. CGMessage(type_e_mismatch);
  829. end
  830. else
  831. CGMessage(type_e_mismatch);
  832. convdone:=true;
  833. end
  834. else
  835. if (ld^.deftype=pointerdef) then
  836. begin
  837. p^.location.loc:=LOC_REGISTER;
  838. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  839. firstpass(p^.right);
  840. calcregisters(p,1,0,0);
  841. case p^.treetype of
  842. addn,subn : begin
  843. if not(cs_extsyntax in aktmoduleswitches) or
  844. (not(is_pchar(ld)) and (m_tp in aktmodeswitches)) then
  845. CGMessage(type_e_mismatch);
  846. end;
  847. else
  848. CGMessage(type_e_mismatch);
  849. end;
  850. convdone:=true;
  851. end
  852. else
  853. if (rd^.deftype=procvardef) and (ld^.deftype=procvardef) and is_equal(rd,ld) then
  854. begin
  855. calcregisters(p,1,0,0);
  856. p^.location.loc:=LOC_REGISTER;
  857. case p^.treetype of
  858. equaln,unequaln : ;
  859. else
  860. CGMessage(type_e_mismatch);
  861. end;
  862. convdone:=true;
  863. end
  864. else
  865. {$ifdef SUPPORT_MMX}
  866. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  867. is_mmx_able_array(rd) and is_equal(ld,rd) then
  868. begin
  869. firstpass(p^.right);
  870. firstpass(p^.left);
  871. case p^.treetype of
  872. addn,subn,xorn,orn,andn:
  873. ;
  874. { mul is a little bit restricted }
  875. muln:
  876. if not(mmx_type(p^.left^.resulttype) in
  877. [mmxu16bit,mmxs16bit,mmxfixed16]) then
  878. CGMessage(type_e_mismatch);
  879. else
  880. CGMessage(type_e_mismatch);
  881. end;
  882. p^.location.loc:=LOC_MMXREGISTER;
  883. calcregisters(p,0,0,1);
  884. convdone:=true;
  885. end
  886. else
  887. {$endif SUPPORT_MMX}
  888. if (ld^.deftype=enumdef) and (rd^.deftype=enumdef) and (is_equal(ld,rd)) then
  889. begin
  890. calcregisters(p,1,0,0);
  891. case p^.treetype of
  892. equaln,unequaln,
  893. ltn,lten,gtn,gten : ;
  894. else CGMessage(type_e_mismatch);
  895. end;
  896. convdone:=true;
  897. end;
  898. { the general solution is to convert to 32 bit int }
  899. if not convdone then
  900. begin
  901. { but an int/int gives real/real! }
  902. if p^.treetype=slashn then
  903. begin
  904. CGMessage(type_h_use_div_for_int);
  905. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  906. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  907. firstpass(p^.left);
  908. firstpass(p^.right);
  909. { maybe we need an integer register to save }
  910. { a reference }
  911. if ((p^.left^.location.loc<>LOC_FPU) or
  912. (p^.right^.location.loc<>LOC_FPU)) and
  913. (p^.left^.registers32=p^.right^.registers32) then
  914. calcregisters(p,1,1,0)
  915. else
  916. calcregisters(p,0,1,0);
  917. p^.location.loc:=LOC_FPU;
  918. end
  919. else
  920. begin
  921. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  922. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  923. firstpass(p^.left);
  924. firstpass(p^.right);
  925. calcregisters(p,1,0,0);
  926. p^.location.loc:=LOC_REGISTER;
  927. end;
  928. end;
  929. if codegenerror then
  930. exit;
  931. { determines result type for comparions }
  932. { here the is a problem with multiple passes }
  933. { example length(s)+1 gets internal 'longint' type first }
  934. { if it is a arg it is converted to 'LONGINT' }
  935. { but a second first pass will reset this to 'longint' }
  936. case p^.treetype of
  937. ltn,lten,gtn,gten,equaln,unequaln:
  938. begin
  939. if (not assigned(p^.resulttype)) or
  940. (p^.resulttype^.deftype=stringdef) then
  941. p^.resulttype:=booldef;
  942. if is_64bitint(p^.left^.resulttype) then
  943. p^.location.loc:=LOC_JUMP
  944. else
  945. p^.location.loc:=LOC_FLAGS;
  946. end;
  947. xorn:
  948. begin
  949. if not assigned(p^.resulttype) then
  950. p^.resulttype:=p^.left^.resulttype;
  951. p^.location.loc:=LOC_REGISTER;
  952. end;
  953. addn:
  954. begin
  955. if not assigned(p^.resulttype) then
  956. begin
  957. { for strings, return is always a 255 char string }
  958. if is_shortstring(p^.left^.resulttype) then
  959. p^.resulttype:=cshortstringdef
  960. else
  961. p^.resulttype:=p^.left^.resulttype;
  962. end;
  963. end;
  964. else
  965. p^.resulttype:=p^.left^.resulttype;
  966. end;
  967. end;
  968. end.
  969. {
  970. $Log$
  971. Revision 1.24 1999-04-08 11:34:00 peter
  972. * int/int warning removed, only the hint is left
  973. Revision 1.23 1999/03/02 22:52:19 peter
  974. * fixed char array, which can start with all possible values
  975. Revision 1.22 1999/02/22 02:15:43 peter
  976. * updates for ag386bin
  977. Revision 1.21 1999/01/20 21:05:09 peter
  978. * fixed set operations which still had array constructor as type
  979. Revision 1.20 1999/01/20 17:39:26 jonas
  980. + fixed bug0163 (set1 <= set2 support)
  981. Revision 1.19 1998/12/30 13:35:35 peter
  982. * fix for boolean=true compares
  983. Revision 1.18 1998/12/15 17:12:35 peter
  984. * pointer+ord not allowed in tp mode
  985. Revision 1.17 1998/12/11 00:03:51 peter
  986. + globtype,tokens,version unit splitted from globals
  987. Revision 1.16 1998/12/10 09:47:31 florian
  988. + basic operations with int64/qord (compiler with -dint64)
  989. + rtti of enumerations extended: names are now written
  990. Revision 1.15 1998/11/24 22:59:05 peter
  991. * handle array of char the same as strings
  992. Revision 1.14 1998/11/17 00:36:47 peter
  993. * more ansistring fixes
  994. Revision 1.13 1998/11/16 15:33:05 peter
  995. * fixed return for ansistrings
  996. Revision 1.12 1998/11/05 14:28:16 peter
  997. * fixed unknown set operation msg
  998. Revision 1.11 1998/11/05 12:03:02 peter
  999. * released useansistring
  1000. * removed -Sv, its now available in fpc modes
  1001. Revision 1.10 1998/11/04 10:11:46 peter
  1002. * ansistring fixes
  1003. Revision 1.9 1998/10/25 23:32:04 peter
  1004. * fixed u32bit - s32bit conversion problems
  1005. Revision 1.8 1998/10/22 12:12:28 pierre
  1006. + better error info on unimplemented set operators
  1007. Revision 1.7 1998/10/21 15:12:57 pierre
  1008. * bug fix for IOCHECK inside a procedure with iocheck modifier
  1009. * removed the GPF for unexistant overloading
  1010. (firstcall was called with procedinition=nil !)
  1011. * changed typen to what Florian proposed
  1012. gentypenode(p : pdef) sets the typenodetype field
  1013. and resulttype is only set if inside bt_type block !
  1014. Revision 1.6 1998/10/20 15:09:24 florian
  1015. + binary operators for ansi strings
  1016. Revision 1.5 1998/10/20 08:07:05 pierre
  1017. * several memory corruptions due to double freemem solved
  1018. => never use p^.loc.location:=p^.left^.loc.location;
  1019. + finally I added now by default
  1020. that ra386dir translates global and unit symbols
  1021. + added a first field in tsymtable and
  1022. a nextsym field in tsym
  1023. (this allows to obtain ordered type info for
  1024. records and objects in gdb !)
  1025. Revision 1.4 1998/10/14 12:53:39 peter
  1026. * fixed small tp7 things
  1027. * boolean:=longbool and longbool fixed
  1028. Revision 1.3 1998/10/11 14:31:19 peter
  1029. + checks for division by zero
  1030. Revision 1.2 1998/10/05 21:33:31 peter
  1031. * fixed 161,165,166,167,168
  1032. Revision 1.1 1998/09/23 20:42:24 peter
  1033. * splitted pass_1
  1034. }