tcadd.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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 shortstrings for addn }
  350. if is_char(rd) and is_char(ld) then
  351. begin
  352. if p^.treetype=addn then
  353. begin
  354. p^.left:=gentypeconvnode(p^.left,cshortstringdef);
  355. p^.right:=gentypeconvnode(p^.right,cshortstringdef);
  356. firstpass(p^.left);
  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. chararrays are also handled as strings (after conversion) }
  397. if (rd^.deftype=stringdef) or (ld^.deftype=stringdef) or
  398. is_chararray(rd) or is_chararray(ld) then
  399. begin
  400. if is_widestring(rd) or is_widestring(ld) then
  401. begin
  402. if not(is_widestring(rd)) then
  403. p^.right:=gentypeconvnode(p^.right,cwidestringdef);
  404. if not(is_widestring(ld)) then
  405. p^.left:=gentypeconvnode(p^.left,cwidestringdef);
  406. p^.resulttype:=cwidestringdef;
  407. { this is only for add, the comparisaion is handled later }
  408. p^.location.loc:=LOC_REGISTER;
  409. end
  410. else if is_ansistring(rd) or is_ansistring(ld) then
  411. begin
  412. if not(is_ansistring(rd)) then
  413. p^.right:=gentypeconvnode(p^.right,cansistringdef);
  414. if not(is_ansistring(ld)) then
  415. p^.left:=gentypeconvnode(p^.left,cansistringdef);
  416. p^.resulttype:=cansistringdef;
  417. { this is only for add, the comparisaion is handled later }
  418. p^.location.loc:=LOC_REGISTER;
  419. end
  420. else if is_longstring(rd) or is_longstring(ld) then
  421. begin
  422. if not(is_longstring(rd)) then
  423. p^.right:=gentypeconvnode(p^.right,clongstringdef);
  424. if not(is_longstring(ld)) then
  425. p^.left:=gentypeconvnode(p^.left,clongstringdef);
  426. p^.resulttype:=clongstringdef;
  427. { this is only for add, the comparisaion is handled later }
  428. p^.location.loc:=LOC_MEM;
  429. end
  430. else
  431. begin
  432. if not(is_shortstring(rd)) then
  433. p^.right:=gentypeconvnode(p^.right,cshortstringdef);
  434. if not(is_shortstring(ld)) then
  435. p^.left:=gentypeconvnode(p^.left,cshortstringdef);
  436. p^.resulttype:=cshortstringdef;
  437. { this is only for add, the comparisaion is handled later }
  438. p^.location.loc:=LOC_MEM;
  439. end;
  440. { only if there is a type cast we need to do again }
  441. { the first pass }
  442. if p^.left^.treetype=typeconvn then
  443. firstpass(p^.left);
  444. if p^.right^.treetype=typeconvn then
  445. firstpass(p^.right);
  446. { here we call STRCONCAT or STRCMP or STRCOPY }
  447. procinfo.flags:=procinfo.flags or pi_do_call;
  448. if p^.location.loc=LOC_MEM then
  449. calcregisters(p,0,0,0)
  450. else
  451. calcregisters(p,1,0,0);
  452. convdone:=true;
  453. end
  454. else
  455. { left side a setdef ? }
  456. if (ld^.deftype=setdef) then
  457. begin
  458. { trying to add a set element? }
  459. if (p^.treetype=addn) and (rd^.deftype<>setdef) then
  460. begin
  461. if (rt=setelementn) then
  462. begin
  463. if not(is_equal(psetdef(ld)^.setof,rd)) then
  464. CGMessage(type_e_set_element_are_not_comp);
  465. end
  466. else
  467. CGMessage(type_e_mismatch)
  468. end
  469. else
  470. begin
  471. if not(p^.treetype in [addn,subn,symdifn,muln,equaln,unequaln]) then
  472. CGMessage(type_e_set_operation_unknown);
  473. { right def must be a also be set }
  474. if (rd^.deftype<>setdef) or not(is_equal(rd,ld)) then
  475. CGMessage(type_e_set_element_are_not_comp);
  476. end;
  477. { ranges require normsets }
  478. if (psetdef(ld)^.settype=smallset) and
  479. (rt=setelementn) and
  480. assigned(p^.right^.right) then
  481. begin
  482. { generate a temporary normset def }
  483. tempdef:=new(psetdef,init(psetdef(ld)^.setof,255));
  484. p^.left:=gentypeconvnode(p^.left,tempdef);
  485. firstpass(p^.left);
  486. dispose(tempdef,done);
  487. ld:=p^.left^.resulttype;
  488. end;
  489. { if the destination is not a smallset then insert a typeconv
  490. which loads a smallset into a normal set }
  491. if (psetdef(ld)^.settype<>smallset) and
  492. (psetdef(rd)^.settype=smallset) then
  493. begin
  494. if (p^.right^.treetype=setconstn) then
  495. begin
  496. t:=gensetconstnode(p^.right^.value_set,psetdef(p^.left^.resulttype));
  497. t^.left:=p^.right^.left;
  498. putnode(p^.right);
  499. p^.right:=t;
  500. end
  501. else
  502. p^.right:=gentypeconvnode(p^.right,psetdef(p^.left^.resulttype));
  503. firstpass(p^.right);
  504. end;
  505. { do constant evaluation }
  506. if (p^.right^.treetype=setconstn) and
  507. not assigned(p^.right^.left) and
  508. (p^.left^.treetype=setconstn) and
  509. not assigned(p^.left^.left) then
  510. begin
  511. new(resultset);
  512. case p^.treetype of
  513. addn : begin
  514. for i:=0 to 31 do
  515. resultset^[i]:=
  516. p^.right^.value_set^[i] or p^.left^.value_set^[i];
  517. t:=gensetconstnode(resultset,psetdef(ld));
  518. end;
  519. muln : begin
  520. for i:=0 to 31 do
  521. resultset^[i]:=
  522. p^.right^.value_set^[i] and p^.left^.value_set^[i];
  523. t:=gensetconstnode(resultset,psetdef(ld));
  524. end;
  525. subn : begin
  526. for i:=0 to 31 do
  527. resultset^[i]:=
  528. p^.left^.value_set^[i] and not(p^.right^.value_set^[i]);
  529. t:=gensetconstnode(resultset,psetdef(ld));
  530. end;
  531. symdifn : begin
  532. for i:=0 to 31 do
  533. resultset^[i]:=
  534. p^.left^.value_set^[i] xor p^.right^.value_set^[i];
  535. t:=gensetconstnode(resultset,psetdef(ld));
  536. end;
  537. unequaln : begin
  538. b:=true;
  539. for i:=0 to 31 do
  540. if p^.right^.value_set^[i]=p^.left^.value_set^[i] then
  541. begin
  542. b:=false;
  543. break;
  544. end;
  545. t:=genordinalconstnode(ord(b),booldef);
  546. end;
  547. equaln : begin
  548. b:=true;
  549. for i:=0 to 31 do
  550. if p^.right^.value_set^[i]<>p^.left^.value_set^[i] then
  551. begin
  552. b:=false;
  553. break;
  554. end;
  555. t:=genordinalconstnode(ord(b),booldef);
  556. end;
  557. end;
  558. dispose(resultset);
  559. disposetree(p);
  560. p:=t;
  561. firstpass(p);
  562. exit;
  563. end
  564. else
  565. if psetdef(ld)^.settype=smallset then
  566. begin
  567. calcregisters(p,1,0,0);
  568. p^.location.loc:=LOC_REGISTER;
  569. end
  570. else
  571. begin
  572. calcregisters(p,0,0,0);
  573. { here we call SET... }
  574. procinfo.flags:=procinfo.flags or pi_do_call;
  575. p^.location.loc:=LOC_MEM;
  576. end;
  577. convdone:=true;
  578. end
  579. else
  580. { is one a real float ? }
  581. if (rd^.deftype=floatdef) or (ld^.deftype=floatdef) then
  582. begin
  583. { if one is a fixed, then convert to f32bit }
  584. if ((rd^.deftype=floatdef) and (pfloatdef(rd)^.typ=f32bit)) or
  585. ((ld^.deftype=floatdef) and (pfloatdef(ld)^.typ=f32bit)) then
  586. begin
  587. if not is_integer(rd) or (p^.treetype<>muln) then
  588. p^.right:=gentypeconvnode(p^.right,s32fixeddef);
  589. if not is_integer(ld) or (p^.treetype<>muln) then
  590. p^.left:=gentypeconvnode(p^.left,s32fixeddef);
  591. firstpass(p^.left);
  592. firstpass(p^.right);
  593. calcregisters(p,1,0,0);
  594. p^.location.loc:=LOC_REGISTER;
  595. end
  596. else
  597. { convert both to c64float }
  598. begin
  599. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  600. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  601. firstpass(p^.left);
  602. firstpass(p^.right);
  603. calcregisters(p,1,1,0);
  604. p^.location.loc:=LOC_FPU;
  605. end;
  606. convdone:=true;
  607. end
  608. else
  609. { pointer comperation and subtraction }
  610. if (rd^.deftype=pointerdef) and (ld^.deftype=pointerdef) then
  611. begin
  612. p^.location.loc:=LOC_REGISTER;
  613. p^.right:=gentypeconvnode(p^.right,ld);
  614. firstpass(p^.right);
  615. calcregisters(p,1,0,0);
  616. case p^.treetype of
  617. equaln,unequaln : ;
  618. ltn,lten,gtn,gten:
  619. begin
  620. if not(cs_extsyntax in aktmoduleswitches) then
  621. CGMessage(type_e_mismatch);
  622. end;
  623. subn:
  624. begin
  625. if not(cs_extsyntax in aktmoduleswitches) then
  626. CGMessage(type_e_mismatch);
  627. p^.resulttype:=s32bitdef;
  628. exit;
  629. end;
  630. else CGMessage(type_e_mismatch);
  631. end;
  632. convdone:=true;
  633. end
  634. else
  635. if (rd^.deftype=objectdef) and (ld^.deftype=objectdef) and
  636. pobjectdef(rd)^.isclass and pobjectdef(ld)^.isclass then
  637. begin
  638. p^.location.loc:=LOC_REGISTER;
  639. if pobjectdef(rd)^.isrelated(pobjectdef(ld)) then
  640. p^.right:=gentypeconvnode(p^.right,ld)
  641. else
  642. p^.left:=gentypeconvnode(p^.left,rd);
  643. firstpass(p^.right);
  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 (rd^.deftype=classrefdef) and (ld^.deftype=classrefdef) then
  654. begin
  655. p^.location.loc:=LOC_REGISTER;
  656. if pobjectdef(pclassrefdef(rd)^.definition)^.isrelated(pobjectdef(
  657. pclassrefdef(ld)^.definition)) then
  658. p^.right:=gentypeconvnode(p^.right,ld)
  659. else
  660. p^.left:=gentypeconvnode(p^.left,rd);
  661. firstpass(p^.right);
  662. firstpass(p^.left);
  663. calcregisters(p,1,0,0);
  664. case p^.treetype of
  665. equaln,unequaln : ;
  666. else CGMessage(type_e_mismatch);
  667. end;
  668. convdone:=true;
  669. end
  670. else
  671. { allows comperasion with nil pointer }
  672. if (rd^.deftype=objectdef) and
  673. pobjectdef(rd)^.isclass then
  674. begin
  675. p^.location.loc:=LOC_REGISTER;
  676. p^.left:=gentypeconvnode(p^.left,rd);
  677. firstpass(p^.left);
  678. calcregisters(p,1,0,0);
  679. case p^.treetype of
  680. equaln,unequaln : ;
  681. else CGMessage(type_e_mismatch);
  682. end;
  683. convdone:=true;
  684. end
  685. else
  686. if (ld^.deftype=objectdef) and
  687. pobjectdef(ld)^.isclass then
  688. begin
  689. p^.location.loc:=LOC_REGISTER;
  690. p^.right:=gentypeconvnode(p^.right,ld);
  691. firstpass(p^.right);
  692. calcregisters(p,1,0,0);
  693. case p^.treetype of
  694. equaln,unequaln : ;
  695. else CGMessage(type_e_mismatch);
  696. end;
  697. convdone:=true;
  698. end
  699. else
  700. if (rd^.deftype=classrefdef) then
  701. begin
  702. p^.left:=gentypeconvnode(p^.left,rd);
  703. firstpass(p^.left);
  704. calcregisters(p,1,0,0);
  705. case p^.treetype of
  706. equaln,unequaln : ;
  707. else CGMessage(type_e_mismatch);
  708. end;
  709. convdone:=true;
  710. end
  711. else
  712. if (ld^.deftype=classrefdef) then
  713. begin
  714. p^.right:=gentypeconvnode(p^.right,ld);
  715. firstpass(p^.right);
  716. calcregisters(p,1,0,0);
  717. case p^.treetype of
  718. equaln,unequaln : ;
  719. else
  720. CGMessage(type_e_mismatch);
  721. end;
  722. convdone:=true;
  723. end
  724. else
  725. if (rd^.deftype=pointerdef) then
  726. begin
  727. p^.location.loc:=LOC_REGISTER;
  728. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  729. firstpass(p^.left);
  730. calcregisters(p,1,0,0);
  731. if p^.treetype=addn then
  732. begin
  733. if not(cs_extsyntax in aktmoduleswitches) then
  734. CGMessage(type_e_mismatch);
  735. end
  736. else
  737. CGMessage(type_e_mismatch);
  738. convdone:=true;
  739. end
  740. else
  741. if (ld^.deftype=pointerdef) then
  742. begin
  743. p^.location.loc:=LOC_REGISTER;
  744. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  745. firstpass(p^.right);
  746. calcregisters(p,1,0,0);
  747. case p^.treetype of
  748. addn,subn : if not(cs_extsyntax in aktmoduleswitches) then
  749. CGMessage(type_e_mismatch);
  750. else
  751. CGMessage(type_e_mismatch);
  752. end;
  753. convdone:=true;
  754. end
  755. else
  756. if (rd^.deftype=procvardef) and (ld^.deftype=procvardef) and is_equal(rd,ld) then
  757. begin
  758. calcregisters(p,1,0,0);
  759. p^.location.loc:=LOC_REGISTER;
  760. case p^.treetype of
  761. equaln,unequaln : ;
  762. else
  763. CGMessage(type_e_mismatch);
  764. end;
  765. convdone:=true;
  766. end
  767. else
  768. {$ifdef SUPPORT_MMX}
  769. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  770. is_mmx_able_array(rd) and is_equal(ld,rd) then
  771. begin
  772. firstpass(p^.right);
  773. firstpass(p^.left);
  774. case p^.treetype of
  775. addn,subn,xorn,orn,andn:
  776. ;
  777. { mul is a little bit restricted }
  778. muln:
  779. if not(mmx_type(p^.left^.resulttype) in
  780. [mmxu16bit,mmxs16bit,mmxfixed16]) then
  781. CGMessage(type_e_mismatch);
  782. else
  783. CGMessage(type_e_mismatch);
  784. end;
  785. p^.location.loc:=LOC_MMXREGISTER;
  786. calcregisters(p,0,0,1);
  787. convdone:=true;
  788. end
  789. else
  790. {$endif SUPPORT_MMX}
  791. if (ld^.deftype=enumdef) and (rd^.deftype=enumdef) and (is_equal(ld,rd)) then
  792. begin
  793. calcregisters(p,1,0,0);
  794. case p^.treetype of
  795. equaln,unequaln,
  796. ltn,lten,gtn,gten : ;
  797. else CGMessage(type_e_mismatch);
  798. end;
  799. convdone:=true;
  800. end;
  801. { the general solution is to convert to 32 bit int }
  802. if not convdone then
  803. begin
  804. { but an int/int gives real/real! }
  805. if p^.treetype=slashn then
  806. begin
  807. CGMessage(type_w_int_slash_int);
  808. CGMessage(type_h_use_div_for_int);
  809. p^.right:=gentypeconvnode(p^.right,c64floatdef);
  810. p^.left:=gentypeconvnode(p^.left,c64floatdef);
  811. firstpass(p^.left);
  812. firstpass(p^.right);
  813. { maybe we need an integer register to save }
  814. { a reference }
  815. if ((p^.left^.location.loc<>LOC_FPU) or
  816. (p^.right^.location.loc<>LOC_FPU)) and
  817. (p^.left^.registers32=p^.right^.registers32) then
  818. calcregisters(p,1,1,0)
  819. else
  820. calcregisters(p,0,1,0);
  821. p^.location.loc:=LOC_FPU;
  822. end
  823. else
  824. begin
  825. p^.right:=gentypeconvnode(p^.right,s32bitdef);
  826. p^.left:=gentypeconvnode(p^.left,s32bitdef);
  827. firstpass(p^.left);
  828. firstpass(p^.right);
  829. calcregisters(p,1,0,0);
  830. p^.location.loc:=LOC_REGISTER;
  831. end;
  832. end;
  833. if codegenerror then
  834. exit;
  835. { determines result type for comparions }
  836. { here the is a problem with multiple passes }
  837. { example length(s)+1 gets internal 'longint' type first }
  838. { if it is a arg it is converted to 'LONGINT' }
  839. { but a second first pass will reset this to 'longint' }
  840. case p^.treetype of
  841. ltn,lten,gtn,gten,equaln,unequaln:
  842. begin
  843. if (not assigned(p^.resulttype)) or
  844. (p^.resulttype^.deftype=stringdef) then
  845. p^.resulttype:=booldef;
  846. p^.location.loc:=LOC_FLAGS;
  847. end;
  848. xorn:
  849. begin
  850. if not assigned(p^.resulttype) then
  851. p^.resulttype:=p^.left^.resulttype;
  852. p^.location.loc:=LOC_REGISTER;
  853. end;
  854. addn:
  855. begin
  856. if not assigned(p^.resulttype) then
  857. begin
  858. { for strings, return is always a 255 char string }
  859. if is_shortstring(p^.left^.resulttype) then
  860. p^.resulttype:=cshortstringdef
  861. else
  862. p^.resulttype:=p^.left^.resulttype;
  863. end;
  864. end;
  865. else
  866. p^.resulttype:=p^.left^.resulttype;
  867. end;
  868. end;
  869. end.
  870. {
  871. $Log$
  872. Revision 1.15 1998-11-24 22:59:05 peter
  873. * handle array of char the same as strings
  874. Revision 1.14 1998/11/17 00:36:47 peter
  875. * more ansistring fixes
  876. Revision 1.13 1998/11/16 15:33:05 peter
  877. * fixed return for ansistrings
  878. Revision 1.12 1998/11/05 14:28:16 peter
  879. * fixed unknown set operation msg
  880. Revision 1.11 1998/11/05 12:03:02 peter
  881. * released useansistring
  882. * removed -Sv, its now available in fpc modes
  883. Revision 1.10 1998/11/04 10:11:46 peter
  884. * ansistring fixes
  885. Revision 1.9 1998/10/25 23:32:04 peter
  886. * fixed u32bit - s32bit conversion problems
  887. Revision 1.8 1998/10/22 12:12:28 pierre
  888. + better error info on unimplemented set operators
  889. Revision 1.7 1998/10/21 15:12:57 pierre
  890. * bug fix for IOCHECK inside a procedure with iocheck modifier
  891. * removed the GPF for unexistant overloading
  892. (firstcall was called with procedinition=nil !)
  893. * changed typen to what Florian proposed
  894. gentypenode(p : pdef) sets the typenodetype field
  895. and resulttype is only set if inside bt_type block !
  896. Revision 1.6 1998/10/20 15:09:24 florian
  897. + binary operators for ansi strings
  898. Revision 1.5 1998/10/20 08:07:05 pierre
  899. * several memory corruptions due to double freemem solved
  900. => never use p^.loc.location:=p^.left^.loc.location;
  901. + finally I added now by default
  902. that ra386dir translates global and unit symbols
  903. + added a first field in tsymtable and
  904. a nextsym field in tsym
  905. (this allows to obtain ordered type info for
  906. records and objects in gdb !)
  907. Revision 1.4 1998/10/14 12:53:39 peter
  908. * fixed small tp7 things
  909. * boolean:=longbool and longbool fixed
  910. Revision 1.3 1998/10/11 14:31:19 peter
  911. + checks for division by zero
  912. Revision 1.2 1998/10/05 21:33:31 peter
  913. * fixed 161,165,166,167,168
  914. Revision 1.1 1998/09/23 20:42:24 peter
  915. * splitted pass_1
  916. }