tcadd.pas 38 KB

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