tcadd.pas 38 KB

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