nadd.pas 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Type checking and register allocation for add nodes
  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 nadd;
  19. interface
  20. uses
  21. node;
  22. type
  23. taddnode = class(tbinopnode)
  24. procedure make_bool_equal_size;
  25. function firstpass : tnode;override;
  26. procedure make_bool_equal_size;
  27. end;
  28. tcaddnode : class of taddnode;
  29. var
  30. { caddnode is used to create nodes of the add type }
  31. { the virtual constructor allows to assign }
  32. { another class type to caddnode => processor }
  33. { specific node types can be created }
  34. caddnode : tcaddnode;
  35. function isbinaryoverloaded(var p : pnode) : boolean;
  36. implementation
  37. uses
  38. globtype,systems,tokens,
  39. cobjects,verbose,globals,
  40. symconst,symtable,aasm,types,
  41. cpuinfo,
  42. {$ifdef newcg}
  43. cgbase,
  44. {$else newcg}
  45. hcodegen,
  46. {$endif newcg}
  47. htypechk,pass_1,
  48. cpubase,ncnv,ncal,
  49. ;
  50. {*****************************************************************************
  51. FirstAdd
  52. *****************************************************************************}
  53. {$ifdef fpc}
  54. {$maxfpuregisters 0}
  55. {$endif fpc}
  56. procedure taddnode.make_bool_equal_size;
  57. begin
  58. if porddef(left.resulttype)^.typ>porddef(right.resulttype)^.typ then
  59. begin
  60. right:=gentypeconvnode(right,porddef(left.resulttype));
  61. right.convtyp:=tc_bool_2_int;
  62. right.explizit:=true;
  63. firstpass(right);
  64. end
  65. else
  66. if porddef(left.resulttype)^.typ<porddef(right.resulttype)^.typ then
  67. begin
  68. left:=gentypeconvnode(left,porddef(right.resulttype));
  69. left.convtyp:=tc_bool_2_int;
  70. left.explizit:=true;
  71. firstpass(left);
  72. end;
  73. end;
  74. function taddnode.pass_1 : tnode;
  75. var
  76. t,hp : tnode;
  77. ot,
  78. lt,rt : ttreetyp;
  79. rv,lv : longint;
  80. rvd,lvd : bestreal;
  81. resdef,
  82. rd,ld : pdef;
  83. tempdef : pdef;
  84. concatstrings : boolean;
  85. { to evalute const sets }
  86. resultset : pconstset;
  87. i : longint;
  88. b : boolean;
  89. convdone : boolean;
  90. s1,s2 : pchar;
  91. l1,l2 : longint;
  92. begin
  93. pass_1:=nil;
  94. { first do the two subtrees }
  95. firstpass(left);
  96. firstpass(right);
  97. if codegenerror then
  98. exit;
  99. { convert array constructors to sets, because there is no other operator
  100. possible for array constructors }
  101. if is_array_constructor(left.resulttype) then
  102. arrayconstructor_to_set(left);
  103. if is_array_constructor(right.resulttype) then
  104. arrayconstructor_to_set(right);
  105. { both left and right need to be valid }
  106. set_varstate(left,true);
  107. set_varstate(right,true);
  108. { load easier access variables }
  109. lt:=left.treetype;
  110. rt:=right.treetype;
  111. rd:=right.resulttype;
  112. ld:=left.resulttype;
  113. convdone:=false;
  114. if isbinaryoverloaded(hp) then
  115. begin
  116. pass_1:=hp;
  117. exit;
  118. end
  119. { compact consts }
  120. { convert int consts to real consts, if the }
  121. { other operand is a real const }
  122. if (rt=realconstn) and is_constintnode(left) then
  123. begin
  124. t:=genrealconstnode(left.value,right.resulttype);
  125. disposetree(left);
  126. left:=t;
  127. lt:=realconstn;
  128. end;
  129. if (lt=realconstn) and is_constintnode(right) then
  130. begin
  131. t:=genrealconstnode(right.value,left.resulttype);
  132. disposetree(right);
  133. right:=t;
  134. rt:=realconstn;
  135. end;
  136. { both are int constants, also allow operations on two equal enums
  137. in fpc mode (Needed for conversion of C code) }
  138. if ((lt=ordconstn) and (rt=ordconstn)) and
  139. ((is_constintnode(left) and is_constintnode(right)) or
  140. (is_constboolnode(left) and is_constboolnode(right) and
  141. (treetype in [ltn,lten,gtn,gten,equaln,unequaln,andn,xorn,orn]))) then
  142. begin
  143. { xor, and, or are handled different from arithmetic }
  144. { operations regarding the result type }
  145. { return a boolean for boolean operations (and,xor,or) }
  146. if is_constboolnode(left) then
  147. resdef:=booldef
  148. else if is_64bitint(rd) or is_64bitint(ld) then
  149. resdef:=cs64bitdef
  150. else
  151. resdef:=s32bitdef;
  152. lv:=left.value;
  153. rv:=right.value;
  154. case treetype of
  155. addn : t:=genintconstnode(lv+rv);
  156. subn : t:=genintconstnode(lv-rv);
  157. muln : t:=genintconstnode(lv*rv);
  158. xorn : t:=genordinalconstnode(lv xor rv,resdef);
  159. orn: t:=genordinalconstnode(lv or rv,resdef);
  160. andn: t:=genordinalconstnode(lv and rv,resdef);
  161. ltn : t:=genordinalconstnode(ord(lv<rv),booldef);
  162. lten : t:=genordinalconstnode(ord(lv<=rv),booldef);
  163. gtn : t:=genordinalconstnode(ord(lv>rv),booldef);
  164. gten : t:=genordinalconstnode(ord(lv>=rv),booldef);
  165. equaln : t:=genordinalconstnode(ord(lv=rv),booldef);
  166. unequaln : t:=genordinalconstnode(ord(lv<>rv),booldef);
  167. slashn : begin
  168. { int/int becomes a real }
  169. if int(rv)=0 then
  170. begin
  171. Message(parser_e_invalid_float_operation);
  172. t:=genrealconstnode(0,bestrealdef^);
  173. end
  174. else
  175. t:=genrealconstnode(int(lv)/int(rv),bestrealdef^);
  176. firstpass(t);
  177. end;
  178. else
  179. CGMessage(type_e_mismatch);
  180. end;
  181. pass_1:=t
  182. exit;
  183. end;
  184. { both real constants ? }
  185. if (lt=realconstn) and (rt=realconstn) then
  186. begin
  187. lvd:=left.value_real;
  188. rvd:=right.value_real;
  189. case treetype of
  190. addn : t:=genrealconstnode(lvd+rvd,bestrealdef^);
  191. subn : t:=genrealconstnode(lvd-rvd,bestrealdef^);
  192. muln : t:=genrealconstnode(lvd*rvd,bestrealdef^);
  193. starstarn,
  194. caretn : begin
  195. if lvd<0 then
  196. begin
  197. Message(parser_e_invalid_float_operation);
  198. t:=genrealconstnode(0,bestrealdef^);
  199. end
  200. else if lvd=0 then
  201. t:=genrealconstnode(1.0,bestrealdef^)
  202. else
  203. t:=genrealconstnode(exp(ln(lvd)*rvd),bestrealdef^);
  204. end;
  205. slashn :
  206. begin
  207. if rvd=0 then
  208. begin
  209. Message(parser_e_invalid_float_operation);
  210. t:=genrealconstnode(0,bestrealdef^);
  211. end
  212. else
  213. t:=genrealconstnode(lvd/rvd,bestrealdef^);
  214. end;
  215. ltn : t:=genordinalconstnode(ord(lvd<rvd),booldef);
  216. lten : t:=genordinalconstnode(ord(lvd<=rvd),booldef);
  217. gtn : t:=genordinalconstnode(ord(lvd>rvd),booldef);
  218. gten : t:=genordinalconstnode(ord(lvd>=rvd),booldef);
  219. equaln : t:=genordinalconstnode(ord(lvd=rvd),booldef);
  220. unequaln : t:=genordinalconstnode(ord(lvd<>rvd),booldef);
  221. else
  222. CGMessage(type_e_mismatch);
  223. end;
  224. pass_1:=t;
  225. exit;
  226. end;
  227. { concating strings ? }
  228. concatstrings:=false;
  229. s1:=nil;
  230. s2:=nil;
  231. if (lt=ordconstn) and (rt=ordconstn) and
  232. is_char(ld) and is_char(rd) then
  233. begin
  234. s1:=strpnew(char(byte(left.value)));
  235. s2:=strpnew(char(byte(right.value)));
  236. l1:=1;
  237. l2:=1;
  238. concatstrings:=true;
  239. end
  240. else
  241. if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  242. begin
  243. s1:=getpcharcopy(left);
  244. l1:=left.length;
  245. s2:=strpnew(char(byte(right.value)));
  246. l2:=1;
  247. concatstrings:=true;
  248. end
  249. else
  250. if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  251. begin
  252. s1:=strpnew(char(byte(left.value)));
  253. l1:=1;
  254. s2:=getpcharcopy(right);
  255. l2:=right.length;
  256. concatstrings:=true;
  257. end
  258. else if (lt=stringconstn) and (rt=stringconstn) then
  259. begin
  260. s1:=getpcharcopy(left);
  261. l1:=left.length;
  262. s2:=getpcharcopy(right);
  263. l2:=right.length;
  264. concatstrings:=true;
  265. end;
  266. { I will need to translate all this to ansistrings !!! }
  267. if concatstrings then
  268. begin
  269. case treetype of
  270. addn :
  271. t:=genpcharconstnode(concatansistrings(s1,s2,l1,l2),l1+l2);
  272. ltn :
  273. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)<0),booldef);
  274. lten :
  275. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)<=0),booldef);
  276. gtn :
  277. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)>0),booldef);
  278. gten :
  279. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)>=0),booldef);
  280. equaln :
  281. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)=0),booldef);
  282. unequaln :
  283. t:=genordinalconstnode(byte(compareansistrings(s1,s2,l1,l2)<>0),booldef);
  284. end;
  285. ansistringdispose(s1,l1);
  286. ansistringdispose(s2,l2);
  287. pass_1:=t;
  288. exit;
  289. end;
  290. { if both are orddefs then check sub types }
  291. if (ld^.deftype=orddef) and (rd^.deftype=orddef) then
  292. begin
  293. { 2 booleans ? }
  294. if is_boolean(ld) and is_boolean(rd) then
  295. begin
  296. case treetype of
  297. andn,
  298. orn:
  299. begin
  300. make_bool_equal_size(p);
  301. calcregisters(p,0,0,0);
  302. location.loc:=LOC_JUMP;
  303. end;
  304. xorn,ltn,lten,gtn,gten:
  305. begin
  306. make_bool_equal_size(p);
  307. if (left.location.loc in [LOC_JUMP,LOC_FLAGS]) and
  308. (left.location.loc in [LOC_JUMP,LOC_FLAGS]) then
  309. calcregisters(p,2,0,0)
  310. else
  311. calcregisters(p,1,0,0);
  312. end;
  313. unequaln,
  314. equaln:
  315. begin
  316. make_bool_equal_size(p);
  317. { Remove any compares with constants }
  318. if (left.treetype=ordconstn) then
  319. begin
  320. hp:=right;
  321. b:=(left.value<>0);
  322. ot:=treetype;
  323. disposetree(left);
  324. putnode(p);
  325. p:=hp;
  326. if (not(b) and (ot=equaln)) or
  327. (b and (ot=unequaln)) then
  328. begin
  329. p:=gensinglenode(notn,hp);
  330. firstpass(hp);
  331. end;
  332. exit;
  333. end;
  334. if (right.treetype=ordconstn) then
  335. begin
  336. hp:=left;
  337. b:=(right.value<>0);
  338. ot:=treetype;
  339. disposetree(right);
  340. putnode(p);
  341. p:=hp;
  342. if (not(b) and (ot=equaln)) or
  343. (b and (ot=unequaln)) then
  344. begin
  345. p:=gensinglenode(notn,p);
  346. firstpass(p);
  347. end;
  348. exit;
  349. end;
  350. if (left.location.loc in [LOC_JUMP,LOC_FLAGS]) and
  351. (left.location.loc in [LOC_JUMP,LOC_FLAGS]) then
  352. calcregisters(p,2,0,0)
  353. else
  354. calcregisters(p,1,0,0);
  355. end;
  356. else
  357. CGMessage(type_e_mismatch);
  358. end;
  359. (*
  360. { these one can't be in flags! }
  361. Yes they can, secondadd converts the loc_flags to a register.
  362. The typeconversions below are simply removed by firsttypeconv()
  363. because the resulttype of left = left.resulttype
  364. (surprise! :) (JM)
  365. if treetype in [xorn,unequaln,equaln] then
  366. begin
  367. if left.location.loc=LOC_FLAGS then
  368. begin
  369. left:=gentypeconvnode(left,porddef(left.resulttype));
  370. left.convtyp:=tc_bool_2_int;
  371. left.explizit:=true;
  372. firstpass(left);
  373. end;
  374. if right.location.loc=LOC_FLAGS then
  375. begin
  376. right:=gentypeconvnode(right,porddef(right.resulttype));
  377. right.convtyp:=tc_bool_2_int;
  378. right.explizit:=true;
  379. firstpass(right);
  380. end;
  381. { readjust registers }
  382. calcregisters(p,1,0,0);
  383. end;
  384. *)
  385. convdone:=true;
  386. end
  387. else
  388. { Both are chars? only convert to shortstrings for addn }
  389. if is_char(rd) and is_char(ld) then
  390. begin
  391. if treetype=addn then
  392. begin
  393. left:=gentypeconvnode(left,cshortstringdef);
  394. right:=gentypeconvnode(right,cshortstringdef);
  395. firstpass(left);
  396. firstpass(right);
  397. { here we call STRCOPY }
  398. procinfo^.flags:=procinfo^.flags or pi_do_call;
  399. calcregisters(p,0,0,0);
  400. location.loc:=LOC_MEM;
  401. end
  402. else
  403. calcregisters(p,1,0,0);
  404. convdone:=true;
  405. end
  406. { is there a 64 bit type ? }
  407. else if ((porddef(rd)^.typ=s64bit) or (porddef(ld)^.typ=s64bit)) and
  408. { the / operator is handled later }
  409. (treetype<>slashn) then
  410. begin
  411. if (porddef(ld)^.typ<>s64bit) then
  412. begin
  413. left:=gentypeconvnode(left,cs64bitdef);
  414. firstpass(left);
  415. end;
  416. if (porddef(rd)^.typ<>s64bit) then
  417. begin
  418. right:=gentypeconvnode(right,cs64bitdef);
  419. firstpass(right);
  420. end;
  421. calcregisters(p,2,0,0);
  422. convdone:=true;
  423. end
  424. else if ((porddef(rd)^.typ=u64bit) or (porddef(ld)^.typ=u64bit)) and
  425. { the / operator is handled later }
  426. (treetype<>slashn) then
  427. begin
  428. if (porddef(ld)^.typ<>u64bit) then
  429. begin
  430. left:=gentypeconvnode(left,cu64bitdef);
  431. firstpass(left);
  432. end;
  433. if (porddef(rd)^.typ<>u64bit) then
  434. begin
  435. right:=gentypeconvnode(right,cu64bitdef);
  436. firstpass(right);
  437. end;
  438. calcregisters(p,2,0,0);
  439. convdone:=true;
  440. end
  441. else
  442. { is there a cardinal? }
  443. if ((porddef(rd)^.typ=u32bit) or (porddef(ld)^.typ=u32bit)) and
  444. { the / operator is handled later }
  445. (treetype<>slashn) then
  446. begin
  447. { convert constants to u32bit }
  448. {$ifndef cardinalmulfix}
  449. if (porddef(ld)^.typ<>u32bit) then
  450. begin
  451. { s32bit will be used for when the other is also s32bit }
  452. { the following line doesn't make any sense: it's the same as }
  453. { if ((porddef(rd)^.typ=u32bit) or (porddef(ld)^.typ=u32bit)) and }
  454. { (porddef(ld)^.typ<>u32bit) and (porddef(rd)^.typ=s32bit) then }
  455. { which can be simplified to }
  456. { if ((porddef(rd)^.typ=u32bit) and (porddef(rd)^.typ=s32bit) then }
  457. { which can never be true (JM) }
  458. if (porddef(rd)^.typ=s32bit) and (lt<>ordconstn) then
  459. left:=gentypeconvnode(left,s32bitdef)
  460. else
  461. left:=gentypeconvnode(left,u32bitdef);
  462. firstpass(left);
  463. end;
  464. if (porddef(rd)^.typ<>u32bit) then
  465. begin
  466. { s32bit will be used for when the other is also s32bit }
  467. if (porddef(ld)^.typ=s32bit) and (rt<>ordconstn) then
  468. right:=gentypeconvnode(right,s32bitdef)
  469. else
  470. right:=gentypeconvnode(right,u32bitdef);
  471. firstpass(right);
  472. end;
  473. {$else cardinalmulfix}
  474. { only do a conversion if the nodes have different signs }
  475. if (porddef(rd)^.typ=u32bit) xor (porddef(ld)^.typ=u32bit) then
  476. if (porddef(rd)^.typ=u32bit) then
  477. begin
  478. { can we make them both unsigned? }
  479. if (porddef(ld)^.typ in [u8bit,u16bit]) or
  480. (is_constintnode(left) and
  481. (treetype <> subn) and
  482. (left.value > 0)) then
  483. left:=gentypeconvnode(left,u32bitdef)
  484. else
  485. left:=gentypeconvnode(left,s32bitdef);
  486. firstpass(left);
  487. end
  488. else {if (porddef(ld)^.typ=u32bit) then}
  489. begin
  490. { can we make them both unsigned? }
  491. if (porddef(rd)^.typ in [u8bit,u16bit]) or
  492. (is_constintnode(right) and
  493. (right.value > 0)) then
  494. right:=gentypeconvnode(right,u32bitdef)
  495. else
  496. right:=gentypeconvnode(right,s32bitdef);
  497. firstpass(right);
  498. end;
  499. {$endif cardinalmulfix}
  500. calcregisters(p,1,0,0);
  501. { for unsigned mul we need an extra register }
  502. { registers32:=left.registers32+right.registers32; }
  503. if treetype=muln then
  504. inc(registers32);
  505. convdone:=true;
  506. end;
  507. end
  508. else
  509. { left side a setdef, must be before string processing,
  510. else array constructor can be seen as array of char (PFV) }
  511. if (ld^.deftype=setdef) {or is_array_constructor(ld)} then
  512. begin
  513. { trying to add a set element? }
  514. if (treetype=addn) and (rd^.deftype<>setdef) then
  515. begin
  516. if (rt=setelementn) then
  517. begin
  518. if not(is_equal(psetdef(ld)^.elementtype.def,rd)) then
  519. CGMessage(type_e_set_element_are_not_comp);
  520. end
  521. else
  522. CGMessage(type_e_mismatch)
  523. end
  524. else
  525. begin
  526. if not(treetype in [addn,subn,symdifn,muln,equaln,unequaln
  527. {$IfNDef NoSetInclusion}
  528. ,lten,gten
  529. {$EndIf NoSetInclusion}
  530. ]) then
  531. CGMessage(type_e_set_operation_unknown);
  532. { right def must be a also be set }
  533. if (rd^.deftype<>setdef) or not(is_equal(rd,ld)) then
  534. CGMessage(type_e_set_element_are_not_comp);
  535. end;
  536. { ranges require normsets }
  537. if (psetdef(ld)^.settype=smallset) and
  538. (rt=setelementn) and
  539. assigned(right.right) then
  540. begin
  541. { generate a temporary normset def, it'll be destroyed
  542. when the symtable is unloaded }
  543. tempdef:=new(psetdef,init(psetdef(ld)^.elementtype.def,255));
  544. left:=gentypeconvnode(left,tempdef);
  545. firstpass(left);
  546. ld:=left.resulttype;
  547. end;
  548. { if the destination is not a smallset then insert a typeconv
  549. which loads a smallset into a normal set }
  550. if (psetdef(ld)^.settype<>smallset) and
  551. (psetdef(rd)^.settype=smallset) then
  552. begin
  553. if (right.treetype=setconstn) then
  554. begin
  555. t:=gensetconstnode(right.value_set,psetdef(left.resulttype));
  556. t^.left:=right.left;
  557. putnode(right);
  558. right:=t;
  559. end
  560. else
  561. right:=gentypeconvnode(right,psetdef(left.resulttype));
  562. firstpass(right);
  563. end;
  564. { do constant evaluation }
  565. if (right.treetype=setconstn) and
  566. not assigned(right.left) and
  567. (left.treetype=setconstn) and
  568. not assigned(left.left) then
  569. begin
  570. new(resultset);
  571. case treetype of
  572. addn : begin
  573. for i:=0 to 31 do
  574. resultset^[i]:=
  575. right.value_set^[i] or left.value_set^[i];
  576. t:=gensetconstnode(resultset,psetdef(ld));
  577. end;
  578. muln : begin
  579. for i:=0 to 31 do
  580. resultset^[i]:=
  581. right.value_set^[i] and left.value_set^[i];
  582. t:=gensetconstnode(resultset,psetdef(ld));
  583. end;
  584. subn : begin
  585. for i:=0 to 31 do
  586. resultset^[i]:=
  587. left.value_set^[i] and not(right.value_set^[i]);
  588. t:=gensetconstnode(resultset,psetdef(ld));
  589. end;
  590. symdifn : begin
  591. for i:=0 to 31 do
  592. resultset^[i]:=
  593. left.value_set^[i] xor right.value_set^[i];
  594. t:=gensetconstnode(resultset,psetdef(ld));
  595. end;
  596. unequaln : begin
  597. b:=true;
  598. for i:=0 to 31 do
  599. if right.value_set^[i]=left.value_set^[i] then
  600. begin
  601. b:=false;
  602. break;
  603. end;
  604. t:=genordinalconstnode(ord(b),booldef);
  605. end;
  606. equaln : begin
  607. b:=true;
  608. for i:=0 to 31 do
  609. if right.value_set^[i]<>left.value_set^[i] then
  610. begin
  611. b:=false;
  612. break;
  613. end;
  614. t:=genordinalconstnode(ord(b),booldef);
  615. end;
  616. {$IfNDef NoSetInclusion}
  617. lten : Begin
  618. b := true;
  619. For i := 0 to 31 Do
  620. If (right.value_set^[i] And left.value_set^[i]) <>
  621. left.value_set^[i] Then
  622. Begin
  623. b := false;
  624. Break
  625. End;
  626. t := genordinalconstnode(ord(b),booldef);
  627. End;
  628. gten : Begin
  629. b := true;
  630. For i := 0 to 31 Do
  631. If (left.value_set^[i] And right.value_set^[i]) <>
  632. right.value_set^[i] Then
  633. Begin
  634. b := false;
  635. Break
  636. End;
  637. t := genordinalconstnode(ord(b),booldef);
  638. End;
  639. {$EndIf NoSetInclusion}
  640. end;
  641. dispose(resultset);
  642. disposetree(p);
  643. p:=t;
  644. firstpass(p);
  645. exit;
  646. end
  647. else
  648. if psetdef(ld)^.settype=smallset then
  649. begin
  650. { are we adding set elements ? }
  651. if right.treetype=setelementn then
  652. calcregisters(p,2,0,0)
  653. else
  654. calcregisters(p,1,0,0);
  655. location.loc:=LOC_REGISTER;
  656. end
  657. else
  658. begin
  659. calcregisters(p,0,0,0);
  660. { here we call SET... }
  661. procinfo^.flags:=procinfo^.flags or pi_do_call;
  662. location.loc:=LOC_MEM;
  663. end;
  664. convdone:=true;
  665. end
  666. else
  667. { compare pchar to char arrays by addresses
  668. like BP/Delphi }
  669. if (is_pchar(ld) and is_chararray(rd)) or
  670. (is_pchar(rd) and is_chararray(ld)) then
  671. begin
  672. if is_chararray(rd) then
  673. begin
  674. right:=gentypeconvnode(right,ld);
  675. firstpass(right);
  676. end
  677. else
  678. begin
  679. left:=gentypeconvnode(left,rd);
  680. firstpass(left);
  681. end;
  682. location.loc:=LOC_REGISTER;
  683. calcregisters(p,1,0,0);
  684. convdone:=true;
  685. end
  686. else
  687. { is one of the operands a string?,
  688. chararrays are also handled as strings (after conversion) }
  689. if (rd^.deftype=stringdef) or (ld^.deftype=stringdef) or
  690. ((is_chararray(rd) or is_char(rd)) and
  691. (is_chararray(ld) or is_char(ld))) then
  692. begin
  693. if is_widestring(rd) or is_widestring(ld) then
  694. begin
  695. if not(is_widestring(rd)) then
  696. right:=gentypeconvnode(right,cwidestringdef);
  697. if not(is_widestring(ld)) then
  698. left:=gentypeconvnode(left,cwidestringdef);
  699. resulttype:=cwidestringdef;
  700. { this is only for add, the comparisaion is handled later }
  701. location.loc:=LOC_REGISTER;
  702. end
  703. else if is_ansistring(rd) or is_ansistring(ld) then
  704. begin
  705. if not(is_ansistring(rd)) then
  706. right:=gentypeconvnode(right,cansistringdef);
  707. if not(is_ansistring(ld)) then
  708. left:=gentypeconvnode(left,cansistringdef);
  709. { we use ansistrings so no fast exit here }
  710. procinfo^.no_fast_exit:=true;
  711. resulttype:=cansistringdef;
  712. { this is only for add, the comparisaion is handled later }
  713. location.loc:=LOC_REGISTER;
  714. end
  715. else if is_longstring(rd) or is_longstring(ld) then
  716. begin
  717. if not(is_longstring(rd)) then
  718. right:=gentypeconvnode(right,clongstringdef);
  719. if not(is_longstring(ld)) then
  720. left:=gentypeconvnode(left,clongstringdef);
  721. resulttype:=clongstringdef;
  722. { this is only for add, the comparisaion is handled later }
  723. location.loc:=LOC_MEM;
  724. end
  725. else
  726. begin
  727. if not(is_shortstring(rd))
  728. {$ifdef newoptimizations2}
  729. {$ifdef i386}
  730. { shortstring + char handled seperately (JM) }
  731. and (not(cs_optimize in aktglobalswitches) or
  732. (treetype <> addn) or not(is_char(rd)))
  733. {$endif i386}
  734. {$endif newoptimizations2}
  735. then
  736. right:=gentypeconvnode(right,cshortstringdef);
  737. if not(is_shortstring(ld)) then
  738. left:=gentypeconvnode(left,cshortstringdef);
  739. resulttype:=cshortstringdef;
  740. { this is only for add, the comparisaion is handled later }
  741. location.loc:=LOC_MEM;
  742. end;
  743. { only if there is a type cast we need to do again }
  744. { the first pass }
  745. if left.treetype=typeconvn then
  746. firstpass(left);
  747. if right.treetype=typeconvn then
  748. firstpass(right);
  749. { here we call STRCONCAT or STRCMP or STRCOPY }
  750. procinfo^.flags:=procinfo^.flags or pi_do_call;
  751. if location.loc=LOC_MEM then
  752. calcregisters(p,0,0,0)
  753. else
  754. calcregisters(p,1,0,0);
  755. {$ifdef i386}
  756. { not always necessary, only if it is not a constant char and }
  757. { not a regvar, but don't know how to check this here (JM) }
  758. if is_char(rd) then
  759. inc(registers32);
  760. {$endif i386}
  761. convdone:=true;
  762. end
  763. else
  764. { is one a real float ? }
  765. if (rd^.deftype=floatdef) or (ld^.deftype=floatdef) then
  766. begin
  767. { if one is a fixed, then convert to f32bit }
  768. if ((rd^.deftype=floatdef) and (pfloatdef(rd)^.typ=f32bit)) or
  769. ((ld^.deftype=floatdef) and (pfloatdef(ld)^.typ=f32bit)) then
  770. begin
  771. if not is_integer(rd) or (treetype<>muln) then
  772. right:=gentypeconvnode(right,s32fixeddef);
  773. if not is_integer(ld) or (treetype<>muln) then
  774. left:=gentypeconvnode(left,s32fixeddef);
  775. firstpass(left);
  776. firstpass(right);
  777. calcregisters(p,1,0,0);
  778. location.loc:=LOC_REGISTER;
  779. end
  780. else
  781. { convert both to bestreal }
  782. begin
  783. right:=gentypeconvnode(right,bestrealdef^);
  784. left:=gentypeconvnode(left,bestrealdef^);
  785. firstpass(left);
  786. firstpass(right);
  787. calcregisters(p,0,1,0);
  788. location.loc:=LOC_FPU;
  789. end;
  790. convdone:=true;
  791. end
  792. else
  793. { pointer comperation and subtraction }
  794. if (rd^.deftype=pointerdef) and (ld^.deftype=pointerdef) then
  795. begin
  796. location.loc:=LOC_REGISTER;
  797. { right:=gentypeconvnode(right,ld); }
  798. { firstpass(right); }
  799. calcregisters(p,1,0,0);
  800. case treetype of
  801. equaln,unequaln :
  802. begin
  803. if is_equal(right.resulttype,voidpointerdef) then
  804. begin
  805. right:=gentypeconvnode(right,ld);
  806. firstpass(right);
  807. end
  808. else if is_equal(left.resulttype,voidpointerdef) then
  809. begin
  810. left:=gentypeconvnode(left,rd);
  811. firstpass(left);
  812. end
  813. else if not(is_equal(ld,rd)) then
  814. CGMessage(type_e_mismatch);
  815. end;
  816. ltn,lten,gtn,gten:
  817. begin
  818. if is_equal(right.resulttype,voidpointerdef) then
  819. begin
  820. right:=gentypeconvnode(right,ld);
  821. firstpass(right);
  822. end
  823. else if is_equal(left.resulttype,voidpointerdef) then
  824. begin
  825. left:=gentypeconvnode(left,rd);
  826. firstpass(left);
  827. end
  828. else if not(is_equal(ld,rd)) then
  829. CGMessage(type_e_mismatch);
  830. if not(cs_extsyntax in aktmoduleswitches) then
  831. CGMessage(type_e_mismatch);
  832. end;
  833. subn:
  834. begin
  835. if not(is_equal(ld,rd)) then
  836. CGMessage(type_e_mismatch);
  837. if not(cs_extsyntax in aktmoduleswitches) then
  838. CGMessage(type_e_mismatch);
  839. resulttype:=s32bitdef;
  840. exit;
  841. end;
  842. else CGMessage(type_e_mismatch);
  843. end;
  844. convdone:=true;
  845. end
  846. else
  847. if (rd^.deftype=objectdef) and (ld^.deftype=objectdef) and
  848. pobjectdef(rd)^.is_class and pobjectdef(ld)^.is_class then
  849. begin
  850. location.loc:=LOC_REGISTER;
  851. if pobjectdef(rd)^.is_related(pobjectdef(ld)) then
  852. right:=gentypeconvnode(right,ld)
  853. else
  854. left:=gentypeconvnode(left,rd);
  855. firstpass(right);
  856. firstpass(left);
  857. calcregisters(p,1,0,0);
  858. case treetype of
  859. equaln,unequaln : ;
  860. else CGMessage(type_e_mismatch);
  861. end;
  862. convdone:=true;
  863. end
  864. else
  865. if (rd^.deftype=classrefdef) and (ld^.deftype=classrefdef) then
  866. begin
  867. location.loc:=LOC_REGISTER;
  868. if pobjectdef(pclassrefdef(rd)^.pointertype.def)^.is_related(pobjectdef(
  869. pclassrefdef(ld)^.pointertype.def)) then
  870. right:=gentypeconvnode(right,ld)
  871. else
  872. left:=gentypeconvnode(left,rd);
  873. firstpass(right);
  874. firstpass(left);
  875. calcregisters(p,1,0,0);
  876. case treetype of
  877. equaln,unequaln : ;
  878. else CGMessage(type_e_mismatch);
  879. end;
  880. convdone:=true;
  881. end
  882. else
  883. { allows comperasion with nil pointer }
  884. if (rd^.deftype=objectdef) and
  885. pobjectdef(rd)^.is_class then
  886. begin
  887. location.loc:=LOC_REGISTER;
  888. left:=gentypeconvnode(left,rd);
  889. firstpass(left);
  890. calcregisters(p,1,0,0);
  891. case treetype of
  892. equaln,unequaln : ;
  893. else CGMessage(type_e_mismatch);
  894. end;
  895. convdone:=true;
  896. end
  897. else
  898. if (ld^.deftype=objectdef) and
  899. pobjectdef(ld)^.is_class then
  900. begin
  901. location.loc:=LOC_REGISTER;
  902. right:=gentypeconvnode(right,ld);
  903. firstpass(right);
  904. calcregisters(p,1,0,0);
  905. case treetype of
  906. equaln,unequaln : ;
  907. else CGMessage(type_e_mismatch);
  908. end;
  909. convdone:=true;
  910. end
  911. else
  912. if (rd^.deftype=classrefdef) then
  913. begin
  914. left:=gentypeconvnode(left,rd);
  915. firstpass(left);
  916. calcregisters(p,1,0,0);
  917. case treetype of
  918. equaln,unequaln : ;
  919. else CGMessage(type_e_mismatch);
  920. end;
  921. convdone:=true;
  922. end
  923. else
  924. if (ld^.deftype=classrefdef) then
  925. begin
  926. right:=gentypeconvnode(right,ld);
  927. firstpass(right);
  928. calcregisters(p,1,0,0);
  929. case treetype of
  930. equaln,unequaln : ;
  931. else
  932. CGMessage(type_e_mismatch);
  933. end;
  934. convdone:=true;
  935. end
  936. else
  937. { support procvar=nil,procvar<>nil }
  938. if ((ld^.deftype=procvardef) and (rt=niln)) or
  939. ((rd^.deftype=procvardef) and (lt=niln)) then
  940. begin
  941. calcregisters(p,1,0,0);
  942. location.loc:=LOC_REGISTER;
  943. case treetype of
  944. equaln,unequaln : ;
  945. else
  946. CGMessage(type_e_mismatch);
  947. end;
  948. convdone:=true;
  949. end
  950. else
  951. {$ifdef SUPPORT_MMX}
  952. if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  953. is_mmx_able_array(rd) and is_equal(ld,rd) then
  954. begin
  955. firstpass(right);
  956. firstpass(left);
  957. case treetype of
  958. addn,subn,xorn,orn,andn:
  959. ;
  960. { mul is a little bit restricted }
  961. muln:
  962. if not(mmx_type(left.resulttype) in
  963. [mmxu16bit,mmxs16bit,mmxfixed16]) then
  964. CGMessage(type_e_mismatch);
  965. else
  966. CGMessage(type_e_mismatch);
  967. end;
  968. location.loc:=LOC_MMXREGISTER;
  969. calcregisters(p,0,0,1);
  970. convdone:=true;
  971. end
  972. else
  973. {$endif SUPPORT_MMX}
  974. { this is a little bit dangerous, also the left type }
  975. { should be checked! This broke the mmx support }
  976. if (rd^.deftype=pointerdef) or
  977. is_zero_based_array(rd) then
  978. begin
  979. if is_zero_based_array(rd) then
  980. begin
  981. resulttype:=new(ppointerdef,init(parraydef(rd)^.elementtype));
  982. right:=gentypeconvnode(right,resulttype);
  983. firstpass(right);
  984. end;
  985. location.loc:=LOC_REGISTER;
  986. left:=gentypeconvnode(left,s32bitdef);
  987. firstpass(left);
  988. calcregisters(p,1,0,0);
  989. if treetype=addn then
  990. begin
  991. if not(cs_extsyntax in aktmoduleswitches) or
  992. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  993. CGMessage(type_e_mismatch);
  994. { Dirty hack, to support multiple firstpasses (PFV) }
  995. if (resulttype=nil) and
  996. (rd^.deftype=pointerdef) and
  997. (ppointerdef(rd)^.pointertype.def^.size>1) then
  998. begin
  999. left:=gennode(muln,left,genordinalconstnode(ppointerdef(rd)^.pointertype.def^.size,s32bitdef));
  1000. firstpass(left);
  1001. end;
  1002. end
  1003. else
  1004. CGMessage(type_e_mismatch);
  1005. convdone:=true;
  1006. end
  1007. else
  1008. if (ld^.deftype=pointerdef) or
  1009. is_zero_based_array(ld) then
  1010. begin
  1011. if is_zero_based_array(ld) then
  1012. begin
  1013. resulttype:=new(ppointerdef,init(parraydef(ld)^.elementtype));
  1014. left:=gentypeconvnode(left,resulttype);
  1015. firstpass(left);
  1016. end;
  1017. location.loc:=LOC_REGISTER;
  1018. right:=gentypeconvnode(right,s32bitdef);
  1019. firstpass(right);
  1020. calcregisters(p,1,0,0);
  1021. case treetype of
  1022. addn,subn : begin
  1023. if not(cs_extsyntax in aktmoduleswitches) or
  1024. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1025. CGMessage(type_e_mismatch);
  1026. { Dirty hack, to support multiple firstpasses (PFV) }
  1027. if (resulttype=nil) and
  1028. (ld^.deftype=pointerdef) and
  1029. (ppointerdef(ld)^.pointertype.def^.size>1) then
  1030. begin
  1031. right:=gennode(muln,right,
  1032. genordinalconstnode(ppointerdef(ld)^.pointertype.def^.size,s32bitdef));
  1033. firstpass(right);
  1034. end;
  1035. end;
  1036. else
  1037. CGMessage(type_e_mismatch);
  1038. end;
  1039. convdone:=true;
  1040. end
  1041. else
  1042. if (rd^.deftype=procvardef) and (ld^.deftype=procvardef) and is_equal(rd,ld) then
  1043. begin
  1044. calcregisters(p,1,0,0);
  1045. location.loc:=LOC_REGISTER;
  1046. case treetype of
  1047. equaln,unequaln : ;
  1048. else
  1049. CGMessage(type_e_mismatch);
  1050. end;
  1051. convdone:=true;
  1052. end
  1053. else
  1054. if (ld^.deftype=enumdef) and (rd^.deftype=enumdef) then
  1055. begin
  1056. if not(is_equal(ld,rd)) then
  1057. begin
  1058. right:=gentypeconvnode(right,ld);
  1059. firstpass(right);
  1060. end;
  1061. calcregisters(p,1,0,0);
  1062. case treetype of
  1063. equaln,unequaln,
  1064. ltn,lten,gtn,gten : ;
  1065. else CGMessage(type_e_mismatch);
  1066. end;
  1067. convdone:=true;
  1068. end;
  1069. { the general solution is to convert to 32 bit int }
  1070. if not convdone then
  1071. begin
  1072. { but an int/int gives real/real! }
  1073. if treetype=slashn then
  1074. begin
  1075. CGMessage(type_h_use_div_for_int);
  1076. right:=gentypeconvnode(right,bestrealdef^);
  1077. left:=gentypeconvnode(left,bestrealdef^);
  1078. firstpass(left);
  1079. firstpass(right);
  1080. { maybe we need an integer register to save }
  1081. { a reference }
  1082. if ((left.location.loc<>LOC_FPU) or
  1083. (right.location.loc<>LOC_FPU)) and
  1084. (left.registers32=right.registers32) then
  1085. calcregisters(p,1,1,0)
  1086. else
  1087. calcregisters(p,0,1,0);
  1088. location.loc:=LOC_FPU;
  1089. end
  1090. else
  1091. begin
  1092. right:=gentypeconvnode(right,s32bitdef);
  1093. left:=gentypeconvnode(left,s32bitdef);
  1094. firstpass(left);
  1095. firstpass(right);
  1096. calcregisters(p,1,0,0);
  1097. location.loc:=LOC_REGISTER;
  1098. end;
  1099. end;
  1100. if codegenerror then
  1101. exit;
  1102. { determines result type for comparions }
  1103. { here the is a problem with multiple passes }
  1104. { example length(s)+1 gets internal 'longint' type first }
  1105. { if it is a arg it is converted to 'LONGINT' }
  1106. { but a second first pass will reset this to 'longint' }
  1107. case treetype of
  1108. ltn,lten,gtn,gten,equaln,unequaln:
  1109. begin
  1110. if (not assigned(resulttype)) or
  1111. (resulttype^.deftype=stringdef) then
  1112. resulttype:=booldef;
  1113. if is_64bitint(left.resulttype) then
  1114. location.loc:=LOC_JUMP
  1115. else
  1116. location.loc:=LOC_FLAGS;
  1117. end;
  1118. xorn:
  1119. begin
  1120. if not assigned(resulttype) then
  1121. resulttype:=left.resulttype;
  1122. location.loc:=LOC_REGISTER;
  1123. end;
  1124. addn:
  1125. begin
  1126. if not assigned(resulttype) then
  1127. begin
  1128. { for strings, return is always a 255 char string }
  1129. if is_shortstring(left.resulttype) then
  1130. resulttype:=cshortstringdef
  1131. else
  1132. resulttype:=left.resulttype;
  1133. end;
  1134. end;
  1135. {$ifdef cardinalmulfix}
  1136. muln:
  1137. { if we multiply an unsigned with a signed number, the result is signed }
  1138. { in the other cases, the result remains signed or unsigned depending on }
  1139. { the multiplication factors (JM) }
  1140. if (left.resulttype^.deftype = orddef) and
  1141. (right.resulttype^.deftype = orddef) and
  1142. is_signed(right.resulttype) then
  1143. resulttype := right.resulttype
  1144. else resulttype := left.resulttype;
  1145. (*
  1146. subn:
  1147. { if we substract a u32bit from a positive constant, the result becomes }
  1148. { s32bit as well (JM) }
  1149. begin
  1150. if (right.resulttype^.deftype = orddef) and
  1151. (left.resulttype^.deftype = orddef) and
  1152. (porddef(right.resulttype)^.typ = u32bit) and
  1153. is_constintnode(left) and
  1154. { (porddef(left.resulttype)^.typ <> u32bit) and}
  1155. (left.value > 0) then
  1156. begin
  1157. left := gentypeconvnode(left,u32bitdef);
  1158. firstpass(left);
  1159. end;
  1160. resulttype:=left.resulttype;
  1161. end;
  1162. *)
  1163. {$endif cardinalmulfix}
  1164. else
  1165. resulttype:=left.resulttype;
  1166. end;
  1167. end;
  1168. begin
  1169. caddnode:=taddnode;
  1170. end.
  1171. {
  1172. $Log$
  1173. Revision 1.3 2000-09-20 21:50:59 florian
  1174. * updated
  1175. Revision 1.2 2000/08/29 08:24:45 jonas
  1176. * some modifications to -dcardinalmulfix code
  1177. Revision 1.1 2000/08/26 12:24:20 florian
  1178. * initial release
  1179. }