tcadd.pas 40 KB

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