tcadd.pas 45 KB

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