tcadd.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. p^.location.loc:=LOC_JUMP;
  358. end;
  359. unequaln,
  360. equaln,xorn : begin
  361. { this forces a better code generation (TEST }
  362. { instead of CMP) }
  363. if p^.treetype<>xorn then
  364. begin
  365. if (p^.left^.treetype=ordconstn) and
  366. (p^.left^.value<>0) then
  367. begin
  368. p^.left^.value:=0;
  369. if p^.treetype=equaln then
  370. p^.treetype:=unequaln
  371. else
  372. p^.treetype:=equaln;
  373. end;
  374. if (p^.right^.treetype=ordconstn) and
  375. (p^.right^.value<>0) then
  376. begin
  377. p^.right^.value:=0;
  378. if p^.treetype=equaln then
  379. p^.treetype:=unequaln
  380. else
  381. p^.treetype:=equaln;
  382. end;
  383. end;
  384. make_bool_equal_size(p);
  385. calcregisters(p,1,0,0);
  386. end
  387. else
  388. CGMessage(type_e_mismatch);
  389. end;
  390. convdone:=true;
  391. end
  392. else
  393. { Both are chars? only convert to strings for addn }
  394. if (porddef(rd)^.typ=uchar) and (porddef(ld)^.typ=uchar) then
  395. begin
  396. if p^.treetype=addn then
  397. begin
  398. p^.left:=gentypeconvnode(p^.left,cstringdef);
  399. firstpass(p^.left);
  400. p^.right:=gentypeconvnode(p^.right,cstringdef);
  401. firstpass(p^.right);
  402. { here we call STRCOPY }
  403. procinfo.flags:=procinfo.flags or pi_do_call;
  404. calcregisters(p,0,0,0);
  405. p^.location.loc:=LOC_MEM;
  406. end
  407. else
  408. calcregisters(p,1,0,0);
  409. convdone:=true;
  410. end;
  411. end
  412. else
  413. { is one of the sides a shortstring ? }
  414. if (rd^.deftype=stringdef) or (ld^.deftype=stringdef) then
  415. begin
  416. {
  417. if is_widestring(rd) or is_widestring(ld) then
  418. begin
  419. end
  420. else if is_ansistring(rd) or is_ansistring(ld) then
  421. begin
  422. end
  423. else if is_longstring(rd) or is_longstring(ld) then
  424. begin
  425. end
  426. }
  427. if not((rd^.deftype=stringdef) and (ld^.deftype=stringdef)) then
  428. begin
  429. if ld^.deftype=stringdef then
  430. p^.right:=gentypeconvnode(p^.right,cstringdef)
  431. else
  432. p^.left:=gentypeconvnode(p^.left,cstringdef);
  433. firstpass(p^.left);
  434. firstpass(p^.right);
  435. end;
  436. { here we call STRCONCAT or STRCMP or STRCOPY }
  437. procinfo.flags:=procinfo.flags or pi_do_call;
  438. calcregisters(p,0,0,0);
  439. p^.location.loc:=LOC_MEM;
  440. convdone:=true;
  441. end
  442. else
  443. { left side a setdef ? }
  444. if (ld^.deftype=setdef) then
  445. begin
  446. { trying to add a set element? }
  447. if (p^.treetype=addn) and (rd^.deftype<>setdef) then
  448. begin
  449. if (rt=setelementn) then
  450. begin
  451. if not(is_equal(psetdef(ld)^.setof,rd)) then
  452. CGMessage(type_e_set_element_are_not_comp);
  453. end
  454. else
  455. CGMessage(type_e_mismatch)
  456. end
  457. else
  458. begin
  459. if not(p^.treetype in [addn,subn,symdifn,muln,equaln,unequaln]) then
  460. CGMessage(type_e_mismatch);
  461. { right def must be a also be set }
  462. if (rd^.deftype<>setdef) or not(is_equal(rd,ld)) then
  463. CGMessage(type_e_set_element_are_not_comp);
  464. end;
  465. { ranges require normsets }
  466. if (psetdef(ld)^.settype=smallset) and
  467. (rt=setelementn) and
  468. assigned(p^.right^.right) then
  469. begin
  470. { generate a temporary normset def }
  471. tempdef:=new(psetdef,init(psetdef(ld)^.setof,255));
  472. p^.left:=gentypeconvnode(p^.left,tempdef);
  473. firstpass(p^.left);
  474. dispose(tempdef,done);
  475. ld:=p^.left^.resulttype;
  476. end;
  477. { if the destination is not a smallset then insert a typeconv
  478. which loads a smallset into a normal set }
  479. if (psetdef(ld)^.settype<>smallset) and
  480. (psetdef(rd)^.settype=smallset) then
  481. begin
  482. p^.right:=gentypeconvnode(p^.right,psetdef(p^.left^.resulttype));
  483. firstpass(p^.right);
  484. end;
  485. { do constant evalution }
  486. if (p^.right^.treetype=setconstn) and
  487. (p^.left^.treetype=setconstn) then
  488. begin
  489. new(resultset);
  490. case p^.treetype of
  491. addn : begin
  492. for i:=0 to 31 do
  493. resultset^[i]:=
  494. p^.right^.value_set^[i] or p^.left^.value_set^[i];
  495. t:=gensetconstnode(resultset,psetdef(ld));
  496. end;
  497. muln : begin
  498. for i:=0 to 31 do
  499. resultset^[i]:=
  500. p^.right^.value_set^[i] and p^.left^.value_set^[i];
  501. t:=gensetconstnode(resultset,psetdef(ld));
  502. end;
  503. subn : begin
  504. for i:=0 to 31 do
  505. resultset^[i]:=
  506. p^.left^.value_set^[i] and not(p^.right^.value_set^[i]);
  507. t:=gensetconstnode(resultset,psetdef(ld));
  508. end;
  509. symdifn : begin
  510. for i:=0 to 31 do
  511. resultset^[i]:=
  512. p^.left^.value_set^[i] xor p^.right^.value_set^[i];
  513. t:=gensetconstnode(resultset,psetdef(ld));
  514. end;
  515. unequaln : begin
  516. b:=true;
  517. for i:=0 to 31 do
  518. if p^.right^.value_set^[i]=p^.left^.value_set^[i] then
  519. begin
  520. b:=false;
  521. break;
  522. end;
  523. t:=genordinalconstnode(ord(b),booldef);
  524. end;
  525. equaln : begin
  526. b:=true;
  527. for i:=0 to 31 do
  528. if p^.right^.value_set^[i]<>p^.left^.value_set^[i] then
  529. begin
  530. b:=false;
  531. break;
  532. end;
  533. t:=genordinalconstnode(ord(b),booldef);
  534. end;
  535. end;
  536. dispose(resultset);
  537. disposetree(p);
  538. p:=t;
  539. firstpass(p);
  540. exit;
  541. end
  542. else
  543. if psetdef(ld)^.settype=smallset then
  544. begin
  545. calcregisters(p,1,0,0);
  546. p^.location.loc:=LOC_REGISTER;
  547. end
  548. else
  549. begin
  550. calcregisters(p,0,0,0);
  551. { here we call SET... }
  552. procinfo.flags:=procinfo.flags or pi_do_call;
  553. p^.location.loc:=LOC_MEM;
  554. end;
  555. convdone:=true;
  556. end
  557. else
  558. { is one a real float ? }
  559. if (rd^.deftype=floatdef) or (ld^.deftype=floatdef) then
  560. begin
  561. { if one is a fixed, then convert to f32bit }
  562. if ((rd^.deftype=floatdef) and (pfloatdef(rd)^.typ=f32bit)) or
  563. ((ld^.deftype=floatdef) and (pfloatdef(ld)^.typ=f32bit)) then
  564. begin
  565. if not(porddef(rd)^.typ in [u8bit,s8bit,u16bit,s16bit,s32bit,u32bit]) or (p^.treetype<>muln) then
  566. p^.right:=gentypeconvnode(p^.right,s32fixeddef);
  567. if not(porddef(rd)^.typ in [u8bit,s8bit,u16bit,s16bit,s32bit,u32bit]) or (p^.treetype<>muln) then
  568. p^.left:=gentypeconvnode(p^.left,s32fixeddef);
  569. firstpass(p^.left);
  570. firstpass(p^.right);
  571. calcregisters(p,1,0,0);
  572. p^.location.loc:=LOC_REGISTER;
  573. end
  574. else
  575. { convert both to c64float }
  576. begin
  577. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  578. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  579. firstpass(p^.left);
  580. firstpass(p^.right);
  581. calcregisters(p,1,1,0);
  582. p^.location.loc:=LOC_FPU;
  583. end;
  584. convdone:=true;
  585. end
  586. else
  587. { pointer comperation and subtraction }
  588. if (rd^.deftype=pointerdef) and (ld^.deftype=pointerdef) then
  589. begin
  590. p^.location.loc:=LOC_REGISTER;
  591. p^.right:=gentypeconvnode(p^.right,ld);
  592. firstpass(p^.right);
  593. calcregisters(p,1,0,0);
  594. case p^.treetype of
  595. equaln,unequaln : ;
  596. ltn,lten,gtn,gten:
  597. begin
  598. if not(cs_extsyntax in aktmoduleswitches) then
  599. CGMessage(type_e_mismatch);
  600. end;
  601. subn:
  602. begin
  603. if not(cs_extsyntax in aktmoduleswitches) then
  604. CGMessage(type_e_mismatch);
  605. p^.resulttype:=s32bitdef;
  606. exit;
  607. end;
  608. else CGMessage(type_e_mismatch);
  609. end;
  610. convdone:=true;
  611. end
  612. else
  613. if (rd^.deftype=objectdef) and (ld^.deftype=objectdef) and
  614. pobjectdef(rd)^.isclass and pobjectdef(ld)^.isclass then
  615. begin
  616. p^.location.loc:=LOC_REGISTER;
  617. if pobjectdef(rd)^.isrelated(pobjectdef(ld)) then
  618. p^.right:=gentypeconvnode(p^.right,ld)
  619. else
  620. p^.left:=gentypeconvnode(p^.left,rd);
  621. firstpass(p^.right);
  622. firstpass(p^.left);
  623. calcregisters(p,1,0,0);
  624. case p^.treetype of
  625. equaln,unequaln : ;
  626. else CGMessage(type_e_mismatch);
  627. end;
  628. convdone:=true;
  629. end
  630. else
  631. if (rd^.deftype=classrefdef) and (ld^.deftype=classrefdef) then
  632. begin
  633. p^.location.loc:=LOC_REGISTER;
  634. if pobjectdef(pclassrefdef(rd)^.definition)^.isrelated(pobjectdef(
  635. pclassrefdef(ld)^.definition)) then
  636. p^.right:=gentypeconvnode(p^.right,ld)
  637. else
  638. p^.left:=gentypeconvnode(p^.left,rd);
  639. firstpass(p^.right);
  640. firstpass(p^.left);
  641. calcregisters(p,1,0,0);
  642. case p^.treetype of
  643. equaln,unequaln : ;
  644. else CGMessage(type_e_mismatch);
  645. end;
  646. convdone:=true;
  647. end
  648. else
  649. { allows comperasion with nil pointer }
  650. if (rd^.deftype=objectdef) and
  651. pobjectdef(rd)^.isclass then
  652. begin
  653. p^.location.loc:=LOC_REGISTER;
  654. p^.left:=gentypeconvnode(p^.left,rd);
  655. firstpass(p^.left);
  656. calcregisters(p,1,0,0);
  657. case p^.treetype of
  658. equaln,unequaln : ;
  659. else CGMessage(type_e_mismatch);
  660. end;
  661. convdone:=true;
  662. end
  663. else
  664. if (ld^.deftype=objectdef) and
  665. pobjectdef(ld)^.isclass then
  666. begin
  667. p^.location.loc:=LOC_REGISTER;
  668. p^.right:=gentypeconvnode(p^.right,ld);
  669. firstpass(p^.right);
  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) then
  679. begin
  680. p^.left:=gentypeconvnode(p^.left,rd);
  681. firstpass(p^.left);
  682. calcregisters(p,1,0,0);
  683. case p^.treetype of
  684. equaln,unequaln : ;
  685. else CGMessage(type_e_mismatch);
  686. end;
  687. convdone:=true;
  688. end
  689. else
  690. if (ld^.deftype=classrefdef) then
  691. begin
  692. p^.right:=gentypeconvnode(p^.right,ld);
  693. firstpass(p^.right);
  694. calcregisters(p,1,0,0);
  695. case p^.treetype of
  696. equaln,unequaln : ;
  697. else
  698. CGMessage(type_e_mismatch);
  699. end;
  700. convdone:=true;
  701. end
  702. else
  703. if (rd^.deftype=pointerdef) then
  704. begin
  705. p^.location.loc:=LOC_REGISTER;
  706. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  707. firstpass(p^.left);
  708. calcregisters(p,1,0,0);
  709. if p^.treetype=addn then
  710. begin
  711. if not(cs_extsyntax in aktmoduleswitches) then
  712. CGMessage(type_e_mismatch);
  713. end
  714. else
  715. CGMessage(type_e_mismatch);
  716. convdone:=true;
  717. end
  718. else
  719. if (ld^.deftype=pointerdef) then
  720. begin
  721. p^.location.loc:=LOC_REGISTER;
  722. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  723. firstpass(p^.right);
  724. calcregisters(p,1,0,0);
  725. case p^.treetype of
  726. addn,subn : if not(cs_extsyntax in aktmoduleswitches) then
  727. CGMessage(type_e_mismatch);
  728. else
  729. CGMessage(type_e_mismatch);
  730. end;
  731. convdone:=true;
  732. end
  733. else
  734. if (rd^.deftype=procvardef) and (ld^.deftype=procvardef) and is_equal(rd,ld) then
  735. begin
  736. calcregisters(p,1,0,0);
  737. p^.location.loc:=LOC_REGISTER;
  738. case p^.treetype of
  739. equaln,unequaln : ;
  740. else
  741. CGMessage(type_e_mismatch);
  742. end;
  743. convdone:=true;
  744. end
  745. else
  746. {$ifdef SUPPORT_MMX}
  747. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  748. is_mmx_able_array(rd) and is_equal(ld,rd) then
  749. begin
  750. firstpass(p^.right);
  751. firstpass(p^.left);
  752. case p^.treetype of
  753. addn,subn,xorn,orn,andn:
  754. ;
  755. { mul is a little bit restricted }
  756. muln:
  757. if not(mmx_type(p^.left^.resulttype) in
  758. [mmxu16bit,mmxs16bit,mmxfixed16]) then
  759. CGMessage(type_e_mismatch);
  760. else
  761. CGMessage(type_e_mismatch);
  762. end;
  763. p^.location.loc:=LOC_MMXREGISTER;
  764. calcregisters(p,0,0,1);
  765. convdone:=true;
  766. end
  767. else
  768. {$endif SUPPORT_MMX}
  769. if (ld^.deftype=enumdef) and (rd^.deftype=enumdef) and (is_equal(ld,rd)) then
  770. begin
  771. calcregisters(p,1,0,0);
  772. case p^.treetype of
  773. equaln,unequaln,
  774. ltn,lten,gtn,gten : ;
  775. else CGMessage(type_e_mismatch);
  776. end;
  777. convdone:=true;
  778. end;
  779. { the general solution is to convert to 32 bit int }
  780. if not convdone then
  781. begin
  782. { but an int/int gives real/real! }
  783. if p^.treetype=slashn then
  784. begin
  785. CGMessage(type_w_int_slash_int);
  786. CGMessage(type_h_use_div_for_int);
  787. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  788. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  789. firstpass(p^.left);
  790. firstpass(p^.right);
  791. { maybe we need an integer register to save }
  792. { a reference }
  793. if ((p^.left^.location.loc<>LOC_FPU) or
  794. (p^.right^.location.loc<>LOC_FPU)) and
  795. (p^.left^.registers32=p^.right^.registers32) then
  796. calcregisters(p,1,1,0)
  797. else
  798. calcregisters(p,0,1,0);
  799. p^.location.loc:=LOC_FPU;
  800. end
  801. else
  802. begin
  803. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  804. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  805. firstpass(p^.left);
  806. firstpass(p^.right);
  807. calcregisters(p,1,0,0);
  808. p^.location.loc:=LOC_REGISTER;
  809. end;
  810. end;
  811. if codegenerror then
  812. exit;
  813. { determines result type for comparions }
  814. { here the is a problem with multiple passes }
  815. { example length(s)+1 gets internal 'longint' type first }
  816. { if it is a arg it is converted to 'LONGINT' }
  817. { but a second first pass will reset this to 'longint' }
  818. case p^.treetype of
  819. ltn,lten,gtn,gten,equaln,unequaln:
  820. begin
  821. if not assigned(p^.resulttype) then
  822. p^.resulttype:=booldef;
  823. p^.location.loc:=LOC_FLAGS;
  824. end;
  825. xorn:
  826. begin
  827. if not assigned(p^.resulttype) then
  828. p^.resulttype:=p^.left^.resulttype;
  829. p^.location.loc:=LOC_REGISTER;
  830. end;
  831. addn:
  832. begin
  833. { the result of a string addition is a string of length 255 }
  834. if (p^.left^.resulttype^.deftype=stringdef) or
  835. (p^.right^.resulttype^.deftype=stringdef) then
  836. begin
  837. {$ifndef UseAnsiString}
  838. if not assigned(p^.resulttype) then
  839. p^.resulttype:=cstringdef
  840. {$else UseAnsiString}
  841. if is_ansistring(p^.left^.resulttype) or
  842. is_ansistring(p^.right^.resulttype) then
  843. p^.resulttype:=cansistringdef
  844. else
  845. p^.resulttype:=cstringdef;
  846. {$endif UseAnsiString}
  847. end
  848. else
  849. if not assigned(p^.resulttype) then
  850. p^.resulttype:=p^.left^.resulttype;
  851. end;
  852. else if not assigned(p^.resulttype) then
  853. p^.resulttype:=p^.left^.resulttype;
  854. end;
  855. end;
  856. end.
  857. {
  858. $Log$
  859. Revision 1.3 1998-10-11 14:31:19 peter
  860. + checks for division by zero
  861. Revision 1.2 1998/10/05 21:33:31 peter
  862. * fixed 161,165,166,167,168
  863. Revision 1.1 1998/09/23 20:42:24 peter
  864. * splitted pass_1
  865. }