tcadd.pas 34 KB

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