tcadd.pas 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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. {$ifndef OLDASM}
  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,p^.right^.resulttype);
  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,p^.left^.resulttype);
  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,bestrealdef^);
  197. end
  198. else
  199. t:=genrealconstnode(int(lv)/int(rv),bestrealdef^);
  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,bestrealdef^);
  217. subn : t:=genrealconstnode(lvd-rvd,bestrealdef^);
  218. muln : t:=genrealconstnode(lvd*rvd,bestrealdef^);
  219. caretn : t:=genrealconstnode(exp(ln(lvd)*rvd),bestrealdef^);
  220. slashn : begin
  221. if rvd=0 then
  222. begin
  223. Message(parser_e_invalid_float_operation);
  224. t:=genrealconstnode(0,bestrealdef^);
  225. end
  226. else
  227. t:=genrealconstnode(lvd/rvd,bestrealdef^);
  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,cs64bitdef);
  451. firstpass(p^.left);
  452. end;
  453. if (porddef(rd)^.typ<>s64bitint) then
  454. begin
  455. p^.right:=gentypeconvnode(p^.right,cs64bitdef);
  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 bestreal }
  721. begin
  722. p^.right:=gentypeconvnode(p^.right,bestrealdef^);
  723. p^.left:=gentypeconvnode(p^.left,bestrealdef^);
  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,bestrealdef^);
  963. p^.left:=gentypeconvnode(p^.left,bestrealdef^);
  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.29 1999-05-06 09:05:32 peter
  1029. * generic write_float and str_float
  1030. * fixed constant float conversions
  1031. Revision 1.28 1999/05/01 13:24:46 peter
  1032. * merged nasm compiler
  1033. * old asm moved to oldasm/
  1034. Revision 1.27 1999/04/28 06:02:14 florian
  1035. * changes of Bruessel:
  1036. + message handler can now take an explicit self
  1037. * typinfo fixed: sometimes the type names weren't written
  1038. * the type checking for pointer comparisations and subtraction
  1039. and are now more strict (was also buggy)
  1040. * small bug fix to link.pas to support compiling on another
  1041. drive
  1042. * probable bug in popt386 fixed: call/jmp => push/jmp
  1043. transformation didn't count correctly the jmp references
  1044. + threadvar support
  1045. * warning if ln/sqrt gets an invalid constant argument
  1046. Revision 1.26 1999/04/16 20:44:37 florian
  1047. * the boolean operators =;<>;xor with LOC_JUMP and LOC_FLAGS
  1048. operands fixed, small things for new ansistring management
  1049. Revision 1.25 1999/04/15 09:01:34 peter
  1050. * fixed set loading
  1051. * object inheritance support for browser
  1052. Revision 1.24 1999/04/08 11:34:00 peter
  1053. * int/int warning removed, only the hint is left
  1054. Revision 1.23 1999/03/02 22:52:19 peter
  1055. * fixed char array, which can start with all possible values
  1056. Revision 1.22 1999/02/22 02:15:43 peter
  1057. * updates for ag386bin
  1058. Revision 1.21 1999/01/20 21:05:09 peter
  1059. * fixed set operations which still had array constructor as type
  1060. Revision 1.20 1999/01/20 17:39:26 jonas
  1061. + fixed bug0163 (set1 <= set2 support)
  1062. Revision 1.19 1998/12/30 13:35:35 peter
  1063. * fix for boolean=true compares
  1064. Revision 1.18 1998/12/15 17:12:35 peter
  1065. * pointer+ord not allowed in tp mode
  1066. Revision 1.17 1998/12/11 00:03:51 peter
  1067. + globtype,tokens,version unit splitted from globals
  1068. Revision 1.16 1998/12/10 09:47:31 florian
  1069. + basic operations with int64/qord (compiler with -dint64)
  1070. + rtti of enumerations extended: names are now written
  1071. Revision 1.15 1998/11/24 22:59:05 peter
  1072. * handle array of char the same as strings
  1073. Revision 1.14 1998/11/17 00:36:47 peter
  1074. * more ansistring fixes
  1075. Revision 1.13 1998/11/16 15:33:05 peter
  1076. * fixed return for ansistrings
  1077. Revision 1.12 1998/11/05 14:28:16 peter
  1078. * fixed unknown set operation msg
  1079. Revision 1.11 1998/11/05 12:03:02 peter
  1080. * released useansistring
  1081. * removed -Sv, its now available in fpc modes
  1082. Revision 1.10 1998/11/04 10:11:46 peter
  1083. * ansistring fixes
  1084. Revision 1.9 1998/10/25 23:32:04 peter
  1085. * fixed u32bit - s32bit conversion problems
  1086. Revision 1.8 1998/10/22 12:12:28 pierre
  1087. + better error info on unimplemented set operators
  1088. Revision 1.7 1998/10/21 15:12:57 pierre
  1089. * bug fix for IOCHECK inside a procedure with iocheck modifier
  1090. * removed the GPF for unexistant overloading
  1091. (firstcall was called with procedinition=nil !)
  1092. * changed typen to what Florian proposed
  1093. gentypenode(p : pdef) sets the typenodetype field
  1094. and resulttype is only set if inside bt_type block !
  1095. Revision 1.6 1998/10/20 15:09:24 florian
  1096. + binary operators for ansi strings
  1097. Revision 1.5 1998/10/20 08:07:05 pierre
  1098. * several memory corruptions due to double freemem solved
  1099. => never use p^.loc.location:=p^.left^.loc.location;
  1100. + finally I added now by default
  1101. that ra386dir translates global and unit symbols
  1102. + added a first field in tsymtable and
  1103. a nextsym field in tsym
  1104. (this allows to obtain ordered type info for
  1105. records and objects in gdb !)
  1106. Revision 1.4 1998/10/14 12:53:39 peter
  1107. * fixed small tp7 things
  1108. * boolean:=longbool and longbool fixed
  1109. Revision 1.3 1998/10/11 14:31:19 peter
  1110. + checks for division by zero
  1111. Revision 1.2 1998/10/05 21:33:31 peter
  1112. * fixed 161,165,166,167,168
  1113. Revision 1.1 1998/09/23 20:42:24 peter
  1114. * splitted pass_1
  1115. }