tcadd.pas 33 KB

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