tcadd.pas 37 KB

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