tcadd.pas 36 KB

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