tcadd.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. { right site must also be a setdef, unless addn is used }
  436. if not(p^.treetype in [subn,symdifn,addn,muln,equaln,unequaln]) or
  437. ((rd^.deftype<>setdef) and (p^.treetype<>addn)) then
  438. CGMessage(type_e_mismatch);
  439. if ((rd^.deftype=setdef) and not(is_equal(rd,ld))) and
  440. not((rt=setelementn) and is_equal(psetdef(ld)^.setof,rd)) then
  441. CGMessage(type_e_set_element_are_not_comp);
  442. { ranges require normsets }
  443. if (psetdef(ld)^.settype=smallset) and
  444. (rt=setelementn) and
  445. assigned(p^.right^.right) then
  446. begin
  447. { generate a temporary normset def }
  448. tempdef:=new(psetdef,init(psetdef(ld)^.setof,255));
  449. p^.left:=gentypeconvnode(p^.left,tempdef);
  450. firstpass(p^.left);
  451. dispose(tempdef,done);
  452. ld:=p^.left^.resulttype;
  453. end;
  454. { if the destination is not a smallset then insert a typeconv
  455. which loads a smallset into a normal set }
  456. if (psetdef(ld)^.settype<>smallset) and
  457. (psetdef(rd)^.settype=smallset) then
  458. begin
  459. p^.right:=gentypeconvnode(p^.right,psetdef(p^.left^.resulttype));
  460. firstpass(p^.right);
  461. end;
  462. { do constant evalution }
  463. if (p^.right^.treetype=setconstn) and
  464. (p^.left^.treetype=setconstn) then
  465. begin
  466. new(resultset);
  467. case p^.treetype of
  468. addn : begin
  469. for i:=0 to 31 do
  470. resultset^[i]:=
  471. p^.right^.value_set^[i] or p^.left^.value_set^[i];
  472. t:=gensetconstnode(resultset,psetdef(ld));
  473. end;
  474. muln : begin
  475. for i:=0 to 31 do
  476. resultset^[i]:=
  477. p^.right^.value_set^[i] and p^.left^.value_set^[i];
  478. t:=gensetconstnode(resultset,psetdef(ld));
  479. end;
  480. subn : begin
  481. for i:=0 to 31 do
  482. resultset^[i]:=
  483. p^.left^.value_set^[i] and not(p^.right^.value_set^[i]);
  484. t:=gensetconstnode(resultset,psetdef(ld));
  485. end;
  486. symdifn : begin
  487. for i:=0 to 31 do
  488. resultset^[i]:=
  489. p^.left^.value_set^[i] xor p^.right^.value_set^[i];
  490. t:=gensetconstnode(resultset,psetdef(ld));
  491. end;
  492. unequaln : begin
  493. b:=true;
  494. for i:=0 to 31 do
  495. if p^.right^.value_set^[i]=p^.left^.value_set^[i] then
  496. begin
  497. b:=false;
  498. break;
  499. end;
  500. t:=genordinalconstnode(ord(b),booldef);
  501. end;
  502. equaln : begin
  503. b:=true;
  504. for i:=0 to 31 do
  505. if p^.right^.value_set^[i]<>p^.left^.value_set^[i] then
  506. begin
  507. b:=false;
  508. break;
  509. end;
  510. t:=genordinalconstnode(ord(b),booldef);
  511. end;
  512. end;
  513. dispose(resultset);
  514. disposetree(p);
  515. p:=t;
  516. firstpass(p);
  517. exit;
  518. end
  519. else
  520. if psetdef(ld)^.settype=smallset then
  521. begin
  522. calcregisters(p,1,0,0);
  523. p^.location.loc:=LOC_REGISTER;
  524. end
  525. else
  526. begin
  527. calcregisters(p,0,0,0);
  528. { here we call SET... }
  529. procinfo.flags:=procinfo.flags or pi_do_call;
  530. p^.location.loc:=LOC_MEM;
  531. end;
  532. convdone:=true;
  533. end
  534. else
  535. { is one a real float ? }
  536. if (rd^.deftype=floatdef) or (ld^.deftype=floatdef) then
  537. begin
  538. { if one is a fixed, then convert to f32bit }
  539. if ((rd^.deftype=floatdef) and (pfloatdef(rd)^.typ=f32bit)) or
  540. ((ld^.deftype=floatdef) and (pfloatdef(ld)^.typ=f32bit)) then
  541. begin
  542. if not(porddef(rd)^.typ in [u8bit,s8bit,u16bit,s16bit,s32bit,u32bit]) or (p^.treetype<>muln) then
  543. p^.right:=gentypeconvnode(p^.right,s32fixeddef);
  544. if not(porddef(rd)^.typ in [u8bit,s8bit,u16bit,s16bit,s32bit,u32bit]) or (p^.treetype<>muln) then
  545. p^.left:=gentypeconvnode(p^.left,s32fixeddef);
  546. firstpass(p^.left);
  547. firstpass(p^.right);
  548. calcregisters(p,1,0,0);
  549. p^.location.loc:=LOC_REGISTER;
  550. end
  551. else
  552. { convert both to c64float }
  553. begin
  554. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  555. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  556. firstpass(p^.left);
  557. firstpass(p^.right);
  558. calcregisters(p,1,1,0);
  559. p^.location.loc:=LOC_FPU;
  560. end;
  561. convdone:=true;
  562. end
  563. else
  564. { pointer comperation and subtraction }
  565. if (rd^.deftype=pointerdef) and (ld^.deftype=pointerdef) then
  566. begin
  567. p^.location.loc:=LOC_REGISTER;
  568. p^.right:=gentypeconvnode(p^.right,ld);
  569. firstpass(p^.right);
  570. calcregisters(p,1,0,0);
  571. case p^.treetype of
  572. equaln,unequaln : ;
  573. ltn,lten,gtn,gten:
  574. begin
  575. if not(cs_extsyntax in aktmoduleswitches) then
  576. CGMessage(type_e_mismatch);
  577. end;
  578. subn:
  579. begin
  580. if not(cs_extsyntax in aktmoduleswitches) then
  581. CGMessage(type_e_mismatch);
  582. p^.resulttype:=s32bitdef;
  583. exit;
  584. end;
  585. else CGMessage(type_e_mismatch);
  586. end;
  587. convdone:=true;
  588. end
  589. else
  590. if (rd^.deftype=objectdef) and (ld^.deftype=objectdef) and
  591. pobjectdef(rd)^.isclass and pobjectdef(ld)^.isclass then
  592. begin
  593. p^.location.loc:=LOC_REGISTER;
  594. if pobjectdef(rd)^.isrelated(pobjectdef(ld)) then
  595. p^.right:=gentypeconvnode(p^.right,ld)
  596. else
  597. p^.left:=gentypeconvnode(p^.left,rd);
  598. firstpass(p^.right);
  599. firstpass(p^.left);
  600. calcregisters(p,1,0,0);
  601. case p^.treetype of
  602. equaln,unequaln : ;
  603. else CGMessage(type_e_mismatch);
  604. end;
  605. convdone:=true;
  606. end
  607. else
  608. if (rd^.deftype=classrefdef) and (ld^.deftype=classrefdef) then
  609. begin
  610. p^.location.loc:=LOC_REGISTER;
  611. if pobjectdef(pclassrefdef(rd)^.definition)^.isrelated(pobjectdef(
  612. pclassrefdef(ld)^.definition)) then
  613. p^.right:=gentypeconvnode(p^.right,ld)
  614. else
  615. p^.left:=gentypeconvnode(p^.left,rd);
  616. firstpass(p^.right);
  617. firstpass(p^.left);
  618. calcregisters(p,1,0,0);
  619. case p^.treetype of
  620. equaln,unequaln : ;
  621. else CGMessage(type_e_mismatch);
  622. end;
  623. convdone:=true;
  624. end
  625. else
  626. { allows comperasion with nil pointer }
  627. if (rd^.deftype=objectdef) and
  628. pobjectdef(rd)^.isclass then
  629. begin
  630. p^.location.loc:=LOC_REGISTER;
  631. p^.left:=gentypeconvnode(p^.left,rd);
  632. firstpass(p^.left);
  633. calcregisters(p,1,0,0);
  634. case p^.treetype of
  635. equaln,unequaln : ;
  636. else CGMessage(type_e_mismatch);
  637. end;
  638. convdone:=true;
  639. end
  640. else
  641. if (ld^.deftype=objectdef) and
  642. pobjectdef(ld)^.isclass then
  643. begin
  644. p^.location.loc:=LOC_REGISTER;
  645. p^.right:=gentypeconvnode(p^.right,ld);
  646. firstpass(p^.right);
  647. calcregisters(p,1,0,0);
  648. case p^.treetype of
  649. equaln,unequaln : ;
  650. else CGMessage(type_e_mismatch);
  651. end;
  652. convdone:=true;
  653. end
  654. else
  655. if (rd^.deftype=classrefdef) then
  656. begin
  657. p^.left:=gentypeconvnode(p^.left,rd);
  658. firstpass(p^.left);
  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 (ld^.deftype=classrefdef) then
  668. begin
  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
  675. CGMessage(type_e_mismatch);
  676. end;
  677. convdone:=true;
  678. end
  679. else
  680. if (rd^.deftype=pointerdef) then
  681. begin
  682. p^.location.loc:=LOC_REGISTER;
  683. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  684. firstpass(p^.left);
  685. calcregisters(p,1,0,0);
  686. if p^.treetype=addn then
  687. begin
  688. if not(cs_extsyntax in aktmoduleswitches) then
  689. CGMessage(type_e_mismatch);
  690. end
  691. else
  692. CGMessage(type_e_mismatch);
  693. convdone:=true;
  694. end
  695. else
  696. if (ld^.deftype=pointerdef) then
  697. begin
  698. p^.location.loc:=LOC_REGISTER;
  699. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  700. firstpass(p^.right);
  701. calcregisters(p,1,0,0);
  702. case p^.treetype of
  703. addn,subn : if not(cs_extsyntax in aktmoduleswitches) then
  704. CGMessage(type_e_mismatch);
  705. else
  706. CGMessage(type_e_mismatch);
  707. end;
  708. convdone:=true;
  709. end
  710. else
  711. if (rd^.deftype=procvardef) and (ld^.deftype=procvardef) and is_equal(rd,ld) then
  712. begin
  713. calcregisters(p,1,0,0);
  714. p^.location.loc:=LOC_REGISTER;
  715. case p^.treetype of
  716. equaln,unequaln : ;
  717. else
  718. CGMessage(type_e_mismatch);
  719. end;
  720. convdone:=true;
  721. end
  722. else
  723. {$ifdef SUPPORT_MMX}
  724. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  725. is_mmx_able_array(rd) and is_equal(ld,rd) then
  726. begin
  727. firstpass(p^.right);
  728. firstpass(p^.left);
  729. case p^.treetype of
  730. addn,subn,xorn,orn,andn:
  731. ;
  732. { mul is a little bit restricted }
  733. muln:
  734. if not(mmx_type(p^.left^.resulttype) in
  735. [mmxu16bit,mmxs16bit,mmxfixed16]) then
  736. CGMessage(type_e_mismatch);
  737. else
  738. CGMessage(type_e_mismatch);
  739. end;
  740. p^.location.loc:=LOC_MMXREGISTER;
  741. calcregisters(p,0,0,1);
  742. convdone:=true;
  743. end
  744. else
  745. {$endif SUPPORT_MMX}
  746. if (ld^.deftype=enumdef) and (rd^.deftype=enumdef) and (is_equal(ld,rd)) then
  747. begin
  748. calcregisters(p,1,0,0);
  749. case p^.treetype of
  750. equaln,unequaln,
  751. ltn,lten,gtn,gten : ;
  752. else CGMessage(type_e_mismatch);
  753. end;
  754. convdone:=true;
  755. end;
  756. { the general solution is to convert to 32 bit int }
  757. if not convdone then
  758. begin
  759. { but an int/int gives real/real! }
  760. if p^.treetype=slashn then
  761. begin
  762. CGMessage(type_w_int_slash_int);
  763. CGMessage(type_h_use_div_for_int);
  764. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  765. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  766. firstpass(p^.left);
  767. firstpass(p^.right);
  768. { maybe we need an integer register to save }
  769. { a reference }
  770. if ((p^.left^.location.loc<>LOC_FPU) or
  771. (p^.right^.location.loc<>LOC_FPU)) and
  772. (p^.left^.registers32=p^.right^.registers32) then
  773. calcregisters(p,1,1,0)
  774. else
  775. calcregisters(p,0,1,0);
  776. p^.location.loc:=LOC_FPU;
  777. end
  778. else
  779. begin
  780. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  781. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  782. firstpass(p^.left);
  783. firstpass(p^.right);
  784. calcregisters(p,1,0,0);
  785. p^.location.loc:=LOC_REGISTER;
  786. end;
  787. end;
  788. if codegenerror then
  789. exit;
  790. { determines result type for comparions }
  791. { here the is a problem with multiple passes }
  792. { example length(s)+1 gets internal 'longint' type first }
  793. { if it is a arg it is converted to 'LONGINT' }
  794. { but a second first pass will reset this to 'longint' }
  795. case p^.treetype of
  796. ltn,lten,gtn,gten,equaln,unequaln:
  797. begin
  798. if not assigned(p^.resulttype) then
  799. p^.resulttype:=booldef;
  800. p^.location.loc:=LOC_FLAGS;
  801. end;
  802. xorn:
  803. begin
  804. if not assigned(p^.resulttype) then
  805. p^.resulttype:=p^.left^.resulttype;
  806. p^.location.loc:=LOC_REGISTER;
  807. end;
  808. addn:
  809. begin
  810. { the result of a string addition is a string of length 255 }
  811. if (p^.left^.resulttype^.deftype=stringdef) or
  812. (p^.right^.resulttype^.deftype=stringdef) then
  813. begin
  814. {$ifndef UseAnsiString}
  815. if not assigned(p^.resulttype) then
  816. p^.resulttype:=cstringdef
  817. {$else UseAnsiString}
  818. if is_ansistring(p^.left^.resulttype) or
  819. is_ansistring(p^.right^.resulttype) then
  820. p^.resulttype:=cansistringdef
  821. else
  822. p^.resulttype:=cstringdef;
  823. {$endif UseAnsiString}
  824. end
  825. else
  826. if not assigned(p^.resulttype) then
  827. p^.resulttype:=p^.left^.resulttype;
  828. end;
  829. else if not assigned(p^.resulttype) then
  830. p^.resulttype:=p^.left^.resulttype;
  831. end;
  832. end;
  833. end.
  834. {
  835. $Log$
  836. Revision 1.1 1998-09-23 20:42:24 peter
  837. * splitted pass_1
  838. }