2
0

nadd.pas 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  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. {$i defines.inc}
  20. interface
  21. uses
  22. node;
  23. type
  24. taddnode = class(tbinopnode)
  25. constructor create(tt : tnodetype;l,r : tnode);override;
  26. function pass_1 : tnode;override;
  27. function det_resulttype:tnode;override;
  28. protected
  29. { override the following if you want to implement }
  30. { parts explicitely in the code generator (JM) }
  31. function first_addstring: tnode; virtual;
  32. function first_addset: tnode; virtual;
  33. end;
  34. taddnodeclass = class of taddnode;
  35. var
  36. { caddnode is used to create nodes of the add type }
  37. { the virtual constructor allows to assign }
  38. { another class type to caddnode => processor }
  39. { specific node types can be created }
  40. caddnode : taddnodeclass;
  41. implementation
  42. uses
  43. globtype,systems,
  44. cutils,verbose,globals,widestr,
  45. symconst,symtype,symbase,symdef,symsym,symtable,types,
  46. cpuinfo,
  47. cgbase,
  48. htypechk,pass_1,
  49. nmat,ncnv,nld,ncon,nset,nopt,ncal,ninl,
  50. cpubase;
  51. {*****************************************************************************
  52. TADDNODE
  53. *****************************************************************************}
  54. {$ifdef fpc}
  55. {$maxfpuregisters 0}
  56. {$endif fpc}
  57. constructor taddnode.create(tt : tnodetype;l,r : tnode);
  58. begin
  59. inherited create(tt,l,r);
  60. end;
  61. function taddnode.det_resulttype:tnode;
  62. var
  63. hp,t : tnode;
  64. lt,rt : tnodetype;
  65. rd,ld : tdef;
  66. htype : ttype;
  67. ot : tnodetype;
  68. concatstrings : boolean;
  69. resultset : pconstset;
  70. i : longint;
  71. b : boolean;
  72. s1,s2 : pchar;
  73. ws1,ws2 : pcompilerwidestring;
  74. l1,l2 : longint;
  75. rv,lv : tconstexprint;
  76. rvd,lvd : bestreal;
  77. begin
  78. result:=nil;
  79. { first do the two subtrees }
  80. resulttypepass(left);
  81. resulttypepass(right);
  82. { both left and right need to be valid }
  83. set_varstate(left,true);
  84. set_varstate(right,true);
  85. if codegenerror then
  86. exit;
  87. { convert array constructors to sets, because there is no other operator
  88. possible for array constructors }
  89. if is_array_constructor(left.resulttype.def) then
  90. begin
  91. arrayconstructor_to_set(tarrayconstructornode(left));
  92. resulttypepass(left);
  93. end;
  94. if is_array_constructor(right.resulttype.def) then
  95. begin
  96. arrayconstructor_to_set(tarrayconstructornode(right));
  97. resulttypepass(right);
  98. end;
  99. { allow operator overloading }
  100. hp:=self;
  101. if isbinaryoverloaded(hp) then
  102. begin
  103. result:=hp;
  104. exit;
  105. end;
  106. { Kylix allows enum+ordconstn in an enum declaration (blocktype
  107. is bt_type), we need to do the conversion here before the
  108. constant folding }
  109. if (m_delphi in aktmodeswitches) and
  110. (blocktype=bt_type) then
  111. begin
  112. if (left.resulttype.def.deftype=enumdef) and
  113. (right.resulttype.def.deftype=orddef) then
  114. begin
  115. { insert explicit typecast to s32bit }
  116. left:=ctypeconvnode.create(left,s32bittype);
  117. include(left.flags,nf_explizit);
  118. resulttypepass(left);
  119. end
  120. else
  121. if (left.resulttype.def.deftype=orddef) and
  122. (right.resulttype.def.deftype=enumdef) then
  123. begin
  124. { insert explicit typecast to s32bit }
  125. right:=ctypeconvnode.create(right,s32bittype);
  126. include(right.flags,nf_explizit);
  127. resulttypepass(right);
  128. end;
  129. end;
  130. { is one a real float, then both need to be floats, this
  131. need to be done before the constant folding so constant
  132. operation on a float and int are also handled }
  133. if (right.resulttype.def.deftype=floatdef) or (left.resulttype.def.deftype=floatdef) then
  134. begin
  135. inserttypeconv(right,pbestrealtype^);
  136. inserttypeconv(left,pbestrealtype^);
  137. end;
  138. { if one operand is a widechar or a widestring, both operands }
  139. { are converted to widestring. This must be done before constant }
  140. { folding to allow char+widechar etc. }
  141. if is_widestring(right.resulttype.def) or
  142. is_widestring(left.resulttype.def) or
  143. is_widechar(right.resulttype.def) or
  144. is_widechar(left.resulttype.def) then
  145. begin
  146. inserttypeconv(right,cwidestringtype);
  147. inserttypeconv(left,cwidestringtype);
  148. end;
  149. { load easier access variables }
  150. rd:=right.resulttype.def;
  151. ld:=left.resulttype.def;
  152. rt:=right.nodetype;
  153. lt:=left.nodetype;
  154. { both are int constants }
  155. if (((is_constintnode(left) and is_constintnode(right)) or
  156. (is_constboolnode(left) and is_constboolnode(right) and
  157. (nodetype in [ltn,lten,gtn,gten,equaln,unequaln,andn,xorn,orn])))) or
  158. { support pointer arithmetics on constants (JM) }
  159. ((lt = pointerconstn) and is_constintnode(right) and
  160. (nodetype in [addn,subn])) or
  161. ((lt = pointerconstn) and (rt = pointerconstn) and
  162. (nodetype in [ltn,lten,gtn,gten,equaln,unequaln,subn])) then
  163. begin
  164. { when comparing/substracting pointers, make sure they are }
  165. { of the same type (JM) }
  166. if (lt = pointerconstn) and (rt = pointerconstn) then
  167. begin
  168. if not(cs_extsyntax in aktmoduleswitches) and
  169. not(nodetype in [equaln,unequaln]) then
  170. CGMessage(type_e_mismatch)
  171. else
  172. if (nodetype <> subn) and
  173. is_voidpointer(rd) then
  174. inserttypeconv(right,left.resulttype)
  175. else if (nodetype <> subn) and
  176. is_voidpointer(ld) then
  177. inserttypeconv(left,right.resulttype)
  178. else if not(is_equal(ld,rd)) then
  179. CGMessage(type_e_mismatch);
  180. end
  181. else if (lt=ordconstn) and (rt=ordconstn) then
  182. begin
  183. { make left const type the biggest, this type will be used
  184. for orn,andn,xorn }
  185. if rd.size>ld.size then
  186. inserttypeconv(left,right.resulttype);
  187. end;
  188. { load values }
  189. if (lt = ordconstn) then
  190. lv:=tordconstnode(left).value
  191. else
  192. lv:=tpointerconstnode(left).value;
  193. if (rt = ordconstn) then
  194. rv:=tordconstnode(right).value
  195. else
  196. rv:=tpointerconstnode(right).value;
  197. if (lt = pointerconstn) and
  198. (rt <> pointerconstn) then
  199. rv := rv * tpointerdef(left.resulttype.def).pointertype.def.size;
  200. if (rt = pointerconstn) and
  201. (lt <> pointerconstn) then
  202. lv := lv * tpointerdef(right.resulttype.def).pointertype.def.size;
  203. case nodetype of
  204. addn :
  205. if (lt <> pointerconstn) then
  206. t := genintconstnode(lv+rv)
  207. else
  208. t := cpointerconstnode.create(lv+rv,left.resulttype);
  209. subn :
  210. if (lt <> pointerconstn) or (rt = pointerconstn) then
  211. t := genintconstnode(lv-rv)
  212. else
  213. t := cpointerconstnode.create(lv-rv,left.resulttype);
  214. muln :
  215. t:=genintconstnode(lv*rv);
  216. xorn :
  217. t:=cordconstnode.create(lv xor rv,left.resulttype);
  218. orn :
  219. t:=cordconstnode.create(lv or rv,left.resulttype);
  220. andn :
  221. t:=cordconstnode.create(lv and rv,left.resulttype);
  222. ltn :
  223. t:=cordconstnode.create(ord(lv<rv),booltype);
  224. lten :
  225. t:=cordconstnode.create(ord(lv<=rv),booltype);
  226. gtn :
  227. t:=cordconstnode.create(ord(lv>rv),booltype);
  228. gten :
  229. t:=cordconstnode.create(ord(lv>=rv),booltype);
  230. equaln :
  231. t:=cordconstnode.create(ord(lv=rv),booltype);
  232. unequaln :
  233. t:=cordconstnode.create(ord(lv<>rv),booltype);
  234. slashn :
  235. begin
  236. { int/int becomes a real }
  237. rvd:=rv;
  238. lvd:=lv;
  239. if int(rvd)=0 then
  240. begin
  241. Message(parser_e_invalid_float_operation);
  242. t:=crealconstnode.create(0,pbestrealtype^);
  243. end
  244. else
  245. t:=crealconstnode.create(int(lvd)/int(rvd),pbestrealtype^);
  246. end;
  247. else
  248. CGMessage(type_e_mismatch);
  249. end;
  250. result:=t;
  251. exit;
  252. end;
  253. { both real constants ? }
  254. if (lt=realconstn) and (rt=realconstn) then
  255. begin
  256. lvd:=trealconstnode(left).value_real;
  257. rvd:=trealconstnode(right).value_real;
  258. case nodetype of
  259. addn :
  260. t:=crealconstnode.create(lvd+rvd,pbestrealtype^);
  261. subn :
  262. t:=crealconstnode.create(lvd-rvd,pbestrealtype^);
  263. muln :
  264. t:=crealconstnode.create(lvd*rvd,pbestrealtype^);
  265. starstarn,
  266. caretn :
  267. begin
  268. if lvd<0 then
  269. begin
  270. Message(parser_e_invalid_float_operation);
  271. t:=crealconstnode.create(0,pbestrealtype^);
  272. end
  273. else if lvd=0 then
  274. t:=crealconstnode.create(1.0,pbestrealtype^)
  275. else
  276. t:=crealconstnode.create(exp(ln(lvd)*rvd),pbestrealtype^);
  277. end;
  278. slashn :
  279. begin
  280. if rvd=0 then
  281. begin
  282. Message(parser_e_invalid_float_operation);
  283. t:=crealconstnode.create(0,pbestrealtype^);
  284. end
  285. else
  286. t:=crealconstnode.create(lvd/rvd,pbestrealtype^);
  287. end;
  288. ltn :
  289. t:=cordconstnode.create(ord(lvd<rvd),booltype);
  290. lten :
  291. t:=cordconstnode.create(ord(lvd<=rvd),booltype);
  292. gtn :
  293. t:=cordconstnode.create(ord(lvd>rvd),booltype);
  294. gten :
  295. t:=cordconstnode.create(ord(lvd>=rvd),booltype);
  296. equaln :
  297. t:=cordconstnode.create(ord(lvd=rvd),booltype);
  298. unequaln :
  299. t:=cordconstnode.create(ord(lvd<>rvd),booltype);
  300. else
  301. CGMessage(type_e_mismatch);
  302. end;
  303. result:=t;
  304. exit;
  305. end;
  306. { first, we handle widestrings, so we can check later for }
  307. { stringconstn only }
  308. { widechars are converted above to widestrings too }
  309. { this isn't veryy efficient, but I don't think }
  310. { that it does matter that much (FK) }
  311. if (lt=stringconstn) and (rt=stringconstn) and
  312. (tstringconstnode(left).st_type=st_widestring) and
  313. (tstringconstnode(right).st_type=st_widestring) then
  314. begin
  315. initwidestring(ws1);
  316. initwidestring(ws2);
  317. copywidestring(pcompilerwidestring(tstringconstnode(left).value_str),ws1);
  318. copywidestring(pcompilerwidestring(tstringconstnode(right).value_str),ws2);
  319. case nodetype of
  320. addn :
  321. begin
  322. concatwidestrings(ws1,ws2);
  323. t:=cstringconstnode.createwstr(ws1);
  324. end;
  325. ltn :
  326. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<0),booltype);
  327. lten :
  328. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<=0),booltype);
  329. gtn :
  330. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>0),booltype);
  331. gten :
  332. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>=0),booltype);
  333. equaln :
  334. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)=0),booltype);
  335. unequaln :
  336. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<>0),booltype);
  337. end;
  338. donewidestring(ws1);
  339. donewidestring(ws2);
  340. result:=t;
  341. exit;
  342. end;
  343. { concating strings ? }
  344. concatstrings:=false;
  345. s1:=nil;
  346. s2:=nil;
  347. if (lt=ordconstn) and (rt=ordconstn) and
  348. is_char(ld) and is_char(rd) then
  349. begin
  350. s1:=strpnew(char(byte(tordconstnode(left).value)));
  351. s2:=strpnew(char(byte(tordconstnode(right).value)));
  352. l1:=1;
  353. l2:=1;
  354. concatstrings:=true;
  355. end
  356. else
  357. if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  358. begin
  359. s1:=tstringconstnode(left).getpcharcopy;
  360. l1:=tstringconstnode(left).len;
  361. s2:=strpnew(char(byte(tordconstnode(right).value)));
  362. l2:=1;
  363. concatstrings:=true;
  364. end
  365. else
  366. if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  367. begin
  368. s1:=strpnew(char(byte(tordconstnode(left).value)));
  369. l1:=1;
  370. s2:=tstringconstnode(right).getpcharcopy;
  371. l2:=tstringconstnode(right).len;
  372. concatstrings:=true;
  373. end
  374. else if (lt=stringconstn) and (rt=stringconstn) then
  375. begin
  376. s1:=tstringconstnode(left).getpcharcopy;
  377. l1:=tstringconstnode(left).len;
  378. s2:=tstringconstnode(right).getpcharcopy;
  379. l2:=tstringconstnode(right).len;
  380. concatstrings:=true;
  381. end;
  382. if concatstrings then
  383. begin
  384. case nodetype of
  385. addn :
  386. t:=cstringconstnode.createpchar(concatansistrings(s1,s2,l1,l2),l1+l2);
  387. ltn :
  388. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<0),booltype);
  389. lten :
  390. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<=0),booltype);
  391. gtn :
  392. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>0),booltype);
  393. gten :
  394. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>=0),booltype);
  395. equaln :
  396. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)=0),booltype);
  397. unequaln :
  398. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<>0),booltype);
  399. end;
  400. ansistringdispose(s1,l1);
  401. ansistringdispose(s2,l2);
  402. result:=t;
  403. exit;
  404. end;
  405. { set constant evaluation }
  406. if (right.nodetype=setconstn) and
  407. not assigned(tsetconstnode(right).left) and
  408. (left.nodetype=setconstn) and
  409. not assigned(tsetconstnode(left).left) then
  410. begin
  411. { check types }
  412. inserttypeconv(left,right.resulttype);
  413. if codegenerror then
  414. begin
  415. { recover by only returning the left part }
  416. result:=left;
  417. left:=nil;
  418. exit;
  419. end;
  420. new(resultset);
  421. case nodetype of
  422. addn :
  423. begin
  424. for i:=0 to 31 do
  425. resultset^[i]:=tsetconstnode(right).value_set^[i] or tsetconstnode(left).value_set^[i];
  426. t:=csetconstnode.create(resultset,left.resulttype);
  427. end;
  428. muln :
  429. begin
  430. for i:=0 to 31 do
  431. resultset^[i]:=tsetconstnode(right).value_set^[i] and tsetconstnode(left).value_set^[i];
  432. t:=csetconstnode.create(resultset,left.resulttype);
  433. end;
  434. subn :
  435. begin
  436. for i:=0 to 31 do
  437. resultset^[i]:=tsetconstnode(left).value_set^[i] and not(tsetconstnode(right).value_set^[i]);
  438. t:=csetconstnode.create(resultset,left.resulttype);
  439. end;
  440. symdifn :
  441. begin
  442. for i:=0 to 31 do
  443. resultset^[i]:=tsetconstnode(left).value_set^[i] xor tsetconstnode(right).value_set^[i];
  444. t:=csetconstnode.create(resultset,left.resulttype);
  445. end;
  446. unequaln :
  447. begin
  448. b:=true;
  449. for i:=0 to 31 do
  450. if tsetconstnode(right).value_set^[i]=tsetconstnode(left).value_set^[i] then
  451. begin
  452. b:=false;
  453. break;
  454. end;
  455. t:=cordconstnode.create(ord(b),booltype);
  456. end;
  457. equaln :
  458. begin
  459. b:=true;
  460. for i:=0 to 31 do
  461. if tsetconstnode(right).value_set^[i]<>tsetconstnode(left).value_set^[i] then
  462. begin
  463. b:=false;
  464. break;
  465. end;
  466. t:=cordconstnode.create(ord(b),booltype);
  467. end;
  468. lten :
  469. begin
  470. b := true;
  471. For i := 0 to 31 Do
  472. If (tsetconstnode(right).value_set^[i] And tsetconstnode(left).value_set^[i]) <>
  473. tsetconstnode(left).value_set^[i] Then
  474. Begin
  475. b := false;
  476. Break
  477. End;
  478. t := cordconstnode.create(ord(b),booltype);
  479. End;
  480. gten :
  481. Begin
  482. b := true;
  483. For i := 0 to 31 Do
  484. If (tsetconstnode(left).value_set^[i] And tsetconstnode(right).value_set^[i]) <>
  485. tsetconstnode(right).value_set^[i] Then
  486. Begin
  487. b := false;
  488. Break
  489. End;
  490. t := cordconstnode.create(ord(b),booltype);
  491. End;
  492. end;
  493. dispose(resultset);
  494. result:=t;
  495. exit;
  496. end;
  497. { but an int/int gives real/real! }
  498. if nodetype=slashn then
  499. begin
  500. CGMessage(type_h_use_div_for_int);
  501. inserttypeconv(right,pbestrealtype^);
  502. inserttypeconv(left,pbestrealtype^);
  503. end
  504. { if both are orddefs then check sub types }
  505. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  506. begin
  507. { 2 booleans? Make them equal to the largest boolean }
  508. if is_boolean(ld) and is_boolean(rd) then
  509. begin
  510. if torddef(left.resulttype.def).size>torddef(right.resulttype.def).size then
  511. begin
  512. inserttypeconv(right,left.resulttype);
  513. ttypeconvnode(right).convtype:=tc_bool_2_int;
  514. include(right.flags,nf_explizit);
  515. end
  516. else if torddef(left.resulttype.def).size<torddef(right.resulttype.def).size then
  517. begin
  518. inserttypeconv(left,right.resulttype);
  519. ttypeconvnode(left).convtype:=tc_bool_2_int;
  520. include(left.flags,nf_explizit);
  521. end;
  522. case nodetype of
  523. xorn,
  524. ltn,
  525. lten,
  526. gtn,
  527. gten,
  528. andn,
  529. orn:
  530. begin
  531. end;
  532. unequaln,
  533. equaln:
  534. begin
  535. if not(cs_full_boolean_eval in aktlocalswitches) then
  536. begin
  537. { Remove any compares with constants }
  538. if (left.nodetype=ordconstn) then
  539. begin
  540. hp:=right;
  541. b:=(tordconstnode(left).value<>0);
  542. ot:=nodetype;
  543. left.free;
  544. left:=nil;
  545. right:=nil;
  546. if (not(b) and (ot=equaln)) or
  547. (b and (ot=unequaln)) then
  548. begin
  549. hp:=cnotnode.create(hp);
  550. end;
  551. result:=hp;
  552. exit;
  553. end;
  554. if (right.nodetype=ordconstn) then
  555. begin
  556. hp:=left;
  557. b:=(tordconstnode(right).value<>0);
  558. ot:=nodetype;
  559. right.free;
  560. right:=nil;
  561. left:=nil;
  562. if (not(b) and (ot=equaln)) or
  563. (b and (ot=unequaln)) then
  564. begin
  565. hp:=cnotnode.create(hp);
  566. end;
  567. result:=hp;
  568. exit;
  569. end;
  570. end;
  571. end;
  572. else
  573. CGMessage(type_e_mismatch);
  574. end;
  575. end
  576. { Both are chars? }
  577. else if is_char(rd) and is_char(ld) then
  578. begin
  579. if nodetype=addn then
  580. begin
  581. resulttype:=cshortstringtype;
  582. if not(is_constcharnode(left) and is_constcharnode(right)) then
  583. begin
  584. inserttypeconv(left,cshortstringtype);
  585. hp := genaddsstringcharoptnode(self);
  586. result := hp;
  587. exit;
  588. end;
  589. end;
  590. end
  591. { is there a signed 64 bit type ? }
  592. else if ((torddef(rd).typ=s64bit) or (torddef(ld).typ=s64bit)) then
  593. begin
  594. if (torddef(ld).typ<>s64bit) then
  595. inserttypeconv(left,cs64bittype);
  596. if (torddef(rd).typ<>s64bit) then
  597. inserttypeconv(right,cs64bittype);
  598. end
  599. { is there a unsigned 64 bit type ? }
  600. else if ((torddef(rd).typ=u64bit) or (torddef(ld).typ=u64bit)) then
  601. begin
  602. if (torddef(ld).typ<>u64bit) then
  603. inserttypeconv(left,cu64bittype);
  604. if (torddef(rd).typ<>u64bit) then
  605. inserttypeconv(right,cu64bittype);
  606. end
  607. { is there a cardinal? }
  608. else if ((torddef(rd).typ=u32bit) or (torddef(ld).typ=u32bit)) then
  609. begin
  610. if is_signed(ld) and
  611. { then rd = u32bit }
  612. { convert positive constants to u32bit }
  613. not(is_constintnode(left) and
  614. (tordconstnode(left).value >= 0)) and
  615. { range/overflow checking on mixed signed/cardinal expressions }
  616. { is only possible if you convert everything to 64bit (JM) }
  617. ((aktlocalswitches * [cs_check_overflow,cs_check_range] <> []) and
  618. (nodetype in [addn,subn,muln])) then
  619. begin
  620. { perform the operation in 64bit }
  621. CGMessage(type_w_mixed_signed_unsigned);
  622. inserttypeconv(left,cs64bittype);
  623. inserttypeconv(right,cs64bittype);
  624. end
  625. else
  626. begin
  627. if is_signed(ld) and
  628. not(is_constintnode(left) and
  629. (tordconstnode(left).value >= 0)) and
  630. (cs_check_range in aktlocalswitches) then
  631. CGMessage(type_w_mixed_signed_unsigned2);
  632. inserttypeconv(left,u32bittype);
  633. if is_signed(rd) and
  634. { then ld = u32bit }
  635. { convert positive constants to u32bit }
  636. not(is_constintnode(right) and
  637. (tordconstnode(right).value >= 0)) and
  638. ((aktlocalswitches * [cs_check_overflow,cs_check_range] <> []) and
  639. (nodetype in [addn,subn,muln])) then
  640. begin
  641. { perform the operation in 64bit }
  642. CGMessage(type_w_mixed_signed_unsigned);
  643. inserttypeconv(left,cs64bittype);
  644. inserttypeconv(right,cs64bittype);
  645. end
  646. else
  647. begin
  648. if is_signed(rd) and
  649. not(is_constintnode(right) and
  650. (tordconstnode(right).value >= 0)) and
  651. (cs_check_range in aktlocalswitches) then
  652. CGMessage(type_w_mixed_signed_unsigned2);
  653. inserttypeconv(right,u32bittype);
  654. end;
  655. end;
  656. end
  657. { generic ord conversion is s32bit }
  658. else
  659. begin
  660. inserttypeconv(right,s32bittype);
  661. inserttypeconv(left,s32bittype);
  662. end;
  663. end
  664. { if both are floatdefs, conversion is already done before constant folding }
  665. else if (ld.deftype=floatdef) then
  666. begin
  667. { already converted }
  668. end
  669. { left side a setdef, must be before string processing,
  670. else array constructor can be seen as array of char (PFV) }
  671. else if (ld.deftype=setdef) then
  672. begin
  673. { trying to add a set element? }
  674. if (nodetype=addn) and (rd.deftype<>setdef) then
  675. begin
  676. if (rt=setelementn) then
  677. begin
  678. if not(is_equal(tsetdef(ld).elementtype.def,rd)) then
  679. CGMessage(type_e_set_element_are_not_comp);
  680. end
  681. else
  682. CGMessage(type_e_mismatch)
  683. end
  684. else
  685. begin
  686. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  687. CGMessage(type_e_set_operation_unknown);
  688. { right def must be a also be set }
  689. if (rd.deftype<>setdef) or not(is_equal(rd,ld)) then
  690. CGMessage(type_e_set_element_are_not_comp);
  691. end;
  692. { ranges require normsets }
  693. if (tsetdef(ld).settype=smallset) and
  694. (rt=setelementn) and
  695. assigned(tsetelementnode(right).right) then
  696. begin
  697. { generate a temporary normset def, it'll be destroyed
  698. when the symtable is unloaded }
  699. htype.setdef(tsetdef.create(tsetdef(ld).elementtype,255));
  700. inserttypeconv(left,htype);
  701. end;
  702. { if the destination is not a smallset then insert a typeconv
  703. which loads a smallset into a normal set }
  704. if (tsetdef(ld).settype<>smallset) and
  705. (tsetdef(rd).settype=smallset) then
  706. begin
  707. if (right.nodetype=setconstn) then
  708. begin
  709. t:=csetconstnode.create(tsetconstnode(right).value_set,left.resulttype);
  710. tsetconstnode(t).left:=tsetconstnode(right).left;
  711. tsetconstnode(right).left := nil;
  712. right.free;
  713. right:=t;
  714. end
  715. else
  716. inserttypeconv(right,left.resulttype);
  717. resulttypepass(right);
  718. end;
  719. end
  720. { compare pchar to char arrays by addresses like BP/Delphi }
  721. else if (is_pchar(ld) and is_chararray(rd)) or
  722. (is_pchar(rd) and is_chararray(ld)) then
  723. begin
  724. if is_chararray(rd) then
  725. inserttypeconv(right,left.resulttype)
  726. else
  727. inserttypeconv(left,right.resulttype);
  728. end
  729. { is one of the operands a string?,
  730. chararrays are also handled as strings (after conversion), also take
  731. care of chararray+chararray and chararray+char }
  732. else if (rd.deftype=stringdef) or (ld.deftype=stringdef) or
  733. ((is_chararray(rd) or is_char(rd)) and
  734. (is_chararray(ld) or is_char(ld))) then
  735. begin
  736. if is_widestring(rd) or is_widestring(ld) then
  737. begin
  738. if not(is_widestring(rd)) then
  739. inserttypeconv(right,cwidestringtype);
  740. if not(is_widestring(ld)) then
  741. inserttypeconv(left,cwidestringtype);
  742. end
  743. else if is_ansistring(rd) or is_ansistring(ld) then
  744. begin
  745. if not(is_ansistring(rd)) then
  746. inserttypeconv(right,cansistringtype);
  747. if not(is_ansistring(ld)) then
  748. inserttypeconv(left,cansistringtype);
  749. end
  750. else if is_longstring(rd) or is_longstring(ld) then
  751. begin
  752. if not(is_longstring(rd)) then
  753. inserttypeconv(right,clongstringtype);
  754. if not(is_longstring(ld)) then
  755. inserttypeconv(left,clongstringtype);
  756. location.loc:=LOC_MEM;
  757. end
  758. else
  759. begin
  760. if not(is_shortstring(ld)) then
  761. inserttypeconv(left,cshortstringtype);
  762. { don't convert char, that can be handled by the optimized node }
  763. if not(is_shortstring(rd) or is_char(rd)) then
  764. inserttypeconv(right,cshortstringtype);
  765. end;
  766. end
  767. { pointer comparision and subtraction }
  768. else if (rd.deftype=pointerdef) and (ld.deftype=pointerdef) then
  769. begin
  770. case nodetype of
  771. equaln,unequaln :
  772. begin
  773. if is_voidpointer(right.resulttype.def) then
  774. inserttypeconv(right,left.resulttype)
  775. else if is_voidpointer(left.resulttype.def) then
  776. inserttypeconv(left,right.resulttype)
  777. else if not(is_equal(ld,rd)) then
  778. CGMessage(type_e_mismatch);
  779. end;
  780. ltn,lten,gtn,gten:
  781. begin
  782. if (cs_extsyntax in aktmoduleswitches) then
  783. begin
  784. if is_voidpointer(right.resulttype.def) then
  785. inserttypeconv(right,left.resulttype)
  786. else if is_voidpointer(left.resulttype.def) then
  787. inserttypeconv(left,right.resulttype)
  788. else if not(is_equal(ld,rd)) then
  789. CGMessage(type_e_mismatch);
  790. end
  791. else
  792. CGMessage(type_e_mismatch);
  793. end;
  794. subn:
  795. begin
  796. if (cs_extsyntax in aktmoduleswitches) then
  797. begin
  798. if is_voidpointer(right.resulttype.def) then
  799. inserttypeconv(right,left.resulttype)
  800. else if is_voidpointer(left.resulttype.def) then
  801. inserttypeconv(left,right.resulttype)
  802. else if not(is_equal(ld,rd)) then
  803. CGMessage(type_e_mismatch);
  804. end
  805. else
  806. CGMessage(type_e_mismatch);
  807. resulttype:=s32bittype;
  808. exit;
  809. end;
  810. addn:
  811. begin
  812. if (cs_extsyntax in aktmoduleswitches) then
  813. begin
  814. if is_voidpointer(right.resulttype.def) then
  815. inserttypeconv(right,left.resulttype)
  816. else if is_voidpointer(left.resulttype.def) then
  817. inserttypeconv(left,right.resulttype)
  818. else if not(is_equal(ld,rd)) then
  819. CGMessage(type_e_mismatch);
  820. end
  821. else
  822. CGMessage(type_e_mismatch);
  823. resulttype:=s32bittype;
  824. exit;
  825. end;
  826. else
  827. CGMessage(type_e_mismatch);
  828. end;
  829. end
  830. { class or interface equation }
  831. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  832. begin
  833. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  834. begin
  835. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  836. inserttypeconv(right,left.resulttype)
  837. else
  838. inserttypeconv(left,right.resulttype);
  839. end
  840. else if is_class_or_interface(rd) then
  841. inserttypeconv(left,right.resulttype)
  842. else
  843. inserttypeconv(right,left.resulttype);
  844. if not(nodetype in [equaln,unequaln]) then
  845. CGMessage(type_e_mismatch);
  846. end
  847. else if (rd.deftype=classrefdef) and (ld.deftype=classrefdef) then
  848. begin
  849. if tobjectdef(tclassrefdef(rd).pointertype.def).is_related(
  850. tobjectdef(tclassrefdef(ld).pointertype.def)) then
  851. inserttypeconv(right,left.resulttype)
  852. else
  853. inserttypeconv(left,right.resulttype);
  854. if not(nodetype in [equaln,unequaln]) then
  855. CGMessage(type_e_mismatch);
  856. end
  857. { allows comperasion with nil pointer }
  858. else if is_class_or_interface(rd) or (rd.deftype=classrefdef) then
  859. begin
  860. inserttypeconv(left,right.resulttype);
  861. if not(nodetype in [equaln,unequaln]) then
  862. CGMessage(type_e_mismatch);
  863. end
  864. else if is_class_or_interface(ld) or (ld.deftype=classrefdef) then
  865. begin
  866. inserttypeconv(right,left.resulttype);
  867. if not(nodetype in [equaln,unequaln]) then
  868. CGMessage(type_e_mismatch);
  869. end
  870. { support procvar=nil,procvar<>nil }
  871. else if ((ld.deftype=procvardef) and (rt=niln)) or
  872. ((rd.deftype=procvardef) and (lt=niln)) then
  873. begin
  874. if not(nodetype in [equaln,unequaln]) then
  875. CGMessage(type_e_mismatch);
  876. end
  877. {$ifdef SUPPORT_MMX}
  878. { mmx support, this must be before the zero based array
  879. check }
  880. else if (cs_mmx in aktlocalswitches) and
  881. is_mmx_able_array(ld) and
  882. is_mmx_able_array(rd) and
  883. is_equal(ld,rd) then
  884. begin
  885. case nodetype of
  886. addn,subn,xorn,orn,andn:
  887. ;
  888. { mul is a little bit restricted }
  889. muln:
  890. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  891. CGMessage(type_e_mismatch);
  892. else
  893. CGMessage(type_e_mismatch);
  894. end;
  895. end
  896. {$endif SUPPORT_MMX}
  897. { this is a little bit dangerous, also the left type }
  898. { pointer to should be checked! This broke the mmx support }
  899. else if (rd.deftype=pointerdef) or is_zero_based_array(rd) then
  900. begin
  901. if is_zero_based_array(rd) then
  902. begin
  903. resulttype.setdef(tpointerdef.create(tarraydef(rd).elementtype));
  904. inserttypeconv(right,resulttype);
  905. end;
  906. inserttypeconv(left,s32bittype);
  907. if nodetype=addn then
  908. begin
  909. if not(cs_extsyntax in aktmoduleswitches) or
  910. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  911. CGMessage(type_e_mismatch);
  912. if (rd.deftype=pointerdef) and
  913. (tpointerdef(rd).pointertype.def.size>1) then
  914. left:=caddnode.create(muln,left,cordconstnode.create(tpointerdef(rd).pointertype.def.size,s32bittype));
  915. end
  916. else
  917. CGMessage(type_e_mismatch);
  918. end
  919. else if (ld.deftype=pointerdef) or is_zero_based_array(ld) then
  920. begin
  921. if is_zero_based_array(ld) then
  922. begin
  923. resulttype.setdef(tpointerdef.create(tarraydef(ld).elementtype));
  924. inserttypeconv(left,resulttype);
  925. end;
  926. inserttypeconv(right,s32bittype);
  927. if nodetype in [addn,subn] then
  928. begin
  929. if not(cs_extsyntax in aktmoduleswitches) or
  930. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  931. CGMessage(type_e_mismatch);
  932. if (ld.deftype=pointerdef) and
  933. (tpointerdef(ld).pointertype.def.size>1) then
  934. right:=caddnode.create(muln,right,cordconstnode.create(tpointerdef(ld).pointertype.def.size,s32bittype));
  935. end
  936. else
  937. CGMessage(type_e_mismatch);
  938. end
  939. else if (rd.deftype=procvardef) and (ld.deftype=procvardef) and is_equal(rd,ld) then
  940. begin
  941. if not (nodetype in [equaln,unequaln]) then
  942. CGMessage(type_e_mismatch);
  943. end
  944. { enums }
  945. else if (ld.deftype=enumdef) and (rd.deftype=enumdef) then
  946. begin
  947. if not(is_equal(ld,rd)) then
  948. inserttypeconv(right,left.resulttype);
  949. if not(nodetype in [equaln,unequaln,ltn,lten,gtn,gten]) then
  950. CGMessage(type_e_mismatch);
  951. end
  952. { generic conversion, this is for error recovery }
  953. else
  954. begin
  955. inserttypeconv(left,s32bittype);
  956. inserttypeconv(right,s32bittype);
  957. end;
  958. { set resulttype if not already done }
  959. if not assigned(resulttype.def) then
  960. begin
  961. case nodetype of
  962. ltn,lten,gtn,gten,equaln,unequaln :
  963. resulttype:=booltype;
  964. slashn :
  965. resulttype:=pbestrealtype^;
  966. addn:
  967. begin
  968. { for strings, return is always a 255 char string }
  969. if is_shortstring(left.resulttype.def) then
  970. resulttype:=cshortstringtype
  971. else
  972. resulttype:=left.resulttype;
  973. end;
  974. else
  975. resulttype:=left.resulttype;
  976. end;
  977. end;
  978. end;
  979. function taddnode.first_addstring: tnode;
  980. var
  981. p: tnode;
  982. begin
  983. { when we get here, we are sure that both the left and the right }
  984. { node are both strings of the same stringtype (JM) }
  985. case nodetype of
  986. addn:
  987. begin
  988. { note: if you implemented an fpc_shortstr_concat similar to the }
  989. { one in i386.inc, you have to override first_addstring like in }
  990. { ti386addnode.first_string and implement the shortstring concat }
  991. { manually! The generic routine is different from the i386 one (JM) }
  992. { create the call to the concat routine both strings as arguments }
  993. result := ccallnode.createintern('fpc_'+
  994. lower(tstringdef(resulttype.def).stringtypname)+'_concat',
  995. ccallparanode.create(right,ccallparanode.create(left,nil)));
  996. { we reused the arguments }
  997. left := nil;
  998. right := nil;
  999. firstpass(result);
  1000. end;
  1001. ltn,lten,gtn,gten,equaln,unequaln :
  1002. begin
  1003. { generate better code for s='' and s<>'' }
  1004. if (nodetype in [equaln,unequaln]) and
  1005. (((left.nodetype=stringconstn) and (str_length(left)=0)) or
  1006. ((right.nodetype=stringconstn) and (str_length(right)=0))) then
  1007. begin
  1008. { switch so that the constant is always on the right }
  1009. if left.nodetype = stringconstn then
  1010. begin
  1011. p := left;
  1012. left := right;
  1013. right := p;
  1014. end;
  1015. if is_shortstring(left.resulttype.def) then
  1016. { compare the length with 0 }
  1017. result := caddnode.create(nodetype,
  1018. cinlinenode.create(in_length_x,false,left),
  1019. cordconstnode.create(0,s32bittype))
  1020. else
  1021. begin
  1022. { compare the pointer with nil (for ansistrings etc), }
  1023. { faster than getting the length (JM) }
  1024. result:= caddnode.create(nodetype,
  1025. ctypeconvnode.create(left,voidpointertype),
  1026. cpointerconstnode.create(0,voidpointertype));
  1027. taddnode(result).left.toggleflag(nf_explizit);
  1028. end;
  1029. { left is reused }
  1030. left := nil;
  1031. { right isn't }
  1032. right.free;
  1033. right := nil;
  1034. firstpass(result);
  1035. exit;
  1036. end;
  1037. { no string constant -> call compare routine }
  1038. result := ccallnode.createintern('fpc_'+
  1039. lower(tstringdef(left.resulttype.def).stringtypname)+'_compare',
  1040. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1041. { and compare its result with 0 according to the original operator }
  1042. result := caddnode.create(nodetype,result,
  1043. cordconstnode.create(0,s32bittype));
  1044. left := nil;
  1045. right := nil;
  1046. firstpass(result);
  1047. end;
  1048. end;
  1049. end;
  1050. function taddnode.first_addset: tnode;
  1051. var
  1052. procname: string[31];
  1053. tempn: tnode;
  1054. paras: tcallparanode;
  1055. srsym: ttypesym;
  1056. createset: boolean;
  1057. begin
  1058. { get the sym that represents the fpc_normal_set type }
  1059. if not searchsystype('FPC_NORMAL_SET',srsym) then
  1060. internalerror(200108313);
  1061. case nodetype of
  1062. equaln,unequaln,lten,gten:
  1063. begin
  1064. case nodetype of
  1065. equaln,unequaln:
  1066. procname := 'fpc_set_comp_sets';
  1067. lten,gten:
  1068. begin
  1069. procname := 'fpc_set_contains_sets';
  1070. { (left >= right) = (right <= left) }
  1071. if nodetype = gten then
  1072. begin
  1073. tempn := left;
  1074. left := right;
  1075. right := tempn;
  1076. end;
  1077. end;
  1078. end;
  1079. { convert the arguments (explicitely) to fpc_normal_set's }
  1080. left := ctypeconvnode.create(left,srsym.restype);
  1081. left.toggleflag(nf_explizit);
  1082. right := ctypeconvnode.create(right,srsym.restype);
  1083. right.toggleflag(nf_explizit);
  1084. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1085. ccallparanode.create(left,nil)));
  1086. { left and right are reused as parameters }
  1087. left := nil;
  1088. right := nil;
  1089. { for an unequaln, we have to negate the result of comp_sets }
  1090. if nodetype = unequaln then
  1091. result := cnotnode.create(result);
  1092. end;
  1093. addn:
  1094. begin
  1095. { optimize first loading of a set }
  1096. if (right.nodetype=setelementn) and
  1097. not(assigned(tsetelementnode(right).right)) and
  1098. is_emptyset(left) then
  1099. begin
  1100. { type cast the value to pass as argument to a byte, }
  1101. { since that's what the helper expects }
  1102. tsetelementnode(right).left :=
  1103. ctypeconvnode.create(tsetelementnode(right).left,u8bittype);
  1104. tsetelementnode(right).left.toggleflag(nf_explizit);
  1105. { set the resulttype to the actual one (otherwise it's }
  1106. { "fpc_normal_set") }
  1107. result := ccallnode.createinternres('fpc_set_create_element',
  1108. ccallparanode.create(tsetelementnode(right).left,nil),
  1109. resulttype);
  1110. { reused }
  1111. tsetelementnode(right).left := nil;
  1112. end
  1113. else
  1114. begin
  1115. if right.nodetype=setelementn then
  1116. begin
  1117. { convert the arguments to bytes, since that's what }
  1118. { the helper expects }
  1119. tsetelementnode(right).left :=
  1120. ctypeconvnode.create(tsetelementnode(right).left,
  1121. u8bittype);
  1122. tsetelementnode(right).left.toggleflag(nf_explizit);
  1123. { convert the original set (explicitely) to an }
  1124. { fpc_normal_set so we can pass it to the helper }
  1125. left := ctypeconvnode.create(left,srsym.restype);
  1126. left.toggleflag(nf_explizit);
  1127. { add a range or a single element? }
  1128. if assigned(tsetelementnode(right).right) then
  1129. begin
  1130. tsetelementnode(right).right :=
  1131. ctypeconvnode.create(tsetelementnode(right).right,
  1132. u8bittype);
  1133. tsetelementnode(right).right.toggleflag(nf_explizit);
  1134. { create the call }
  1135. result := ccallnode.createinternres('fpc_set_set_range',
  1136. ccallparanode.create(tsetelementnode(right).right,
  1137. ccallparanode.create(tsetelementnode(right).left,
  1138. ccallparanode.create(left,nil))),resulttype);
  1139. end
  1140. else
  1141. begin
  1142. result := ccallnode.createinternres('fpc_set_set_byte',
  1143. ccallparanode.create(tsetelementnode(right).left,
  1144. ccallparanode.create(left,nil)),resulttype);
  1145. end;
  1146. { remove reused parts from original node }
  1147. tsetelementnode(right).right := nil;
  1148. tsetelementnode(right).left := nil;
  1149. left := nil;
  1150. end
  1151. else
  1152. begin
  1153. { add two sets }
  1154. { convert the sets to fpc_normal_set's }
  1155. left := ctypeconvnode.create(left,srsym.restype);
  1156. left.toggleflag(nf_explizit);
  1157. right := ctypeconvnode.create(right,srsym.restype);
  1158. right.toggleflag(nf_explizit);
  1159. result := ccallnode.createinternres('fpc_set_add_sets',
  1160. ccallparanode.create(right,
  1161. ccallparanode.create(left,nil)),resulttype);
  1162. { remove reused parts from original node }
  1163. left := nil;
  1164. right := nil;
  1165. end;
  1166. end
  1167. end;
  1168. subn,symdifn,muln:
  1169. begin
  1170. { convert the sets to fpc_normal_set's }
  1171. left := ctypeconvnode.create(left,srsym.restype);
  1172. left.toggleflag(nf_explizit);
  1173. right := ctypeconvnode.create(right,srsym.restype);
  1174. right.toggleflag(nf_explizit);
  1175. paras := ccallparanode.create(right,
  1176. ccallparanode.create(left,nil));
  1177. case nodetype of
  1178. subn:
  1179. result := ccallnode.createinternres('fpc_set_sub_sets',
  1180. paras,resulttype);
  1181. symdifn:
  1182. result := ccallnode.createinternres('fpc_set_symdif_sets',
  1183. paras,resulttype);
  1184. muln:
  1185. result := ccallnode.createinternres('fpc_set_mul_sets',
  1186. paras,resulttype);
  1187. end;
  1188. { remove reused parts from original node }
  1189. left := nil;
  1190. right := nil;
  1191. end;
  1192. else
  1193. internalerror(200108311);
  1194. end;
  1195. firstpass(result);
  1196. end;
  1197. function taddnode.pass_1 : tnode;
  1198. var
  1199. hp : tnode;
  1200. lt,rt : tnodetype;
  1201. rd,ld : tdef;
  1202. begin
  1203. result:=nil;
  1204. { first do the two subtrees }
  1205. firstpass(left);
  1206. firstpass(right);
  1207. if codegenerror then
  1208. exit;
  1209. { load easier access variables }
  1210. rd:=right.resulttype.def;
  1211. ld:=left.resulttype.def;
  1212. rt:=right.nodetype;
  1213. lt:=left.nodetype;
  1214. { int/int gives real/real! }
  1215. if nodetype=slashn then
  1216. begin
  1217. { maybe we need an integer register to save }
  1218. { a reference }
  1219. if ((left.location.loc<>LOC_FPU) or
  1220. (right.location.loc<>LOC_FPU)) and
  1221. (left.registers32=right.registers32) then
  1222. calcregisters(self,1,1,0)
  1223. else
  1224. calcregisters(self,0,1,0);
  1225. location.loc:=LOC_FPU;
  1226. end
  1227. { if both are orddefs then check sub types }
  1228. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  1229. begin
  1230. { 2 booleans ? }
  1231. if is_boolean(ld) and is_boolean(rd) then
  1232. begin
  1233. if not(cs_full_boolean_eval in aktlocalswitches) and
  1234. (nodetype in [andn,orn]) then
  1235. begin
  1236. calcregisters(self,0,0,0);
  1237. location.loc:=LOC_JUMP;
  1238. end
  1239. else
  1240. begin
  1241. if (left.location.loc in [LOC_JUMP,LOC_FLAGS]) and
  1242. (left.location.loc in [LOC_JUMP,LOC_FLAGS]) then
  1243. calcregisters(self,2,0,0)
  1244. else
  1245. calcregisters(self,1,0,0);
  1246. end;
  1247. end
  1248. else
  1249. { Both are chars? only convert to shortstrings for addn }
  1250. if is_char(ld) then
  1251. begin
  1252. if nodetype=addn then
  1253. internalerror(200103291);
  1254. calcregisters(self,1,0,0);
  1255. end
  1256. { is there a 64 bit type ? }
  1257. else if (torddef(ld).typ in [s64bit,u64bit]) then
  1258. calcregisters(self,2,0,0)
  1259. { is there a cardinal? }
  1260. else if (torddef(ld).typ=u32bit) then
  1261. begin
  1262. calcregisters(self,1,0,0);
  1263. { for unsigned mul we need an extra register }
  1264. if nodetype=muln then
  1265. inc(registers32);
  1266. end
  1267. { generic s32bit conversion }
  1268. else
  1269. calcregisters(self,1,0,0);
  1270. end
  1271. { left side a setdef, must be before string processing,
  1272. else array constructor can be seen as array of char (PFV) }
  1273. else if (ld.deftype=setdef) then
  1274. begin
  1275. if tsetdef(ld).settype=smallset then
  1276. begin
  1277. { are we adding set elements ? }
  1278. if right.nodetype=setelementn then
  1279. calcregisters(self,2,0,0)
  1280. else
  1281. calcregisters(self,1,0,0);
  1282. location.loc:=LOC_REGISTER;
  1283. end
  1284. else
  1285. begin
  1286. result := first_addset;
  1287. if assigned(result) then
  1288. exit;
  1289. calcregisters(self,0,0,0);
  1290. { here we call SET... }
  1291. procinfo^.flags:=procinfo^.flags or pi_do_call;
  1292. location.loc:=LOC_MEM;
  1293. end;
  1294. end
  1295. { compare pchar by addresses like BP/Delphi }
  1296. else if is_pchar(ld) then
  1297. begin
  1298. location.loc:=LOC_REGISTER;
  1299. calcregisters(self,1,0,0);
  1300. end
  1301. { is one of the operands a string }
  1302. else if (ld.deftype=stringdef) then
  1303. begin
  1304. if is_widestring(ld) then
  1305. begin
  1306. { we use reference counted widestrings so no fast exit here }
  1307. procinfo^.no_fast_exit:=true;
  1308. { this is only for add, the comparisaion is handled later }
  1309. location.loc:=LOC_REGISTER;
  1310. end
  1311. else if is_ansistring(ld) then
  1312. begin
  1313. { we use ansistrings so no fast exit here }
  1314. procinfo^.no_fast_exit:=true;
  1315. { this is only for add, the comparisaion is handled later }
  1316. location.loc:=LOC_REGISTER;
  1317. end
  1318. else if is_longstring(ld) then
  1319. begin
  1320. { this is only for add, the comparisaion is handled later }
  1321. location.loc:=LOC_MEM;
  1322. end
  1323. else
  1324. begin
  1325. if canbeaddsstringcharoptnode(self) then
  1326. begin
  1327. hp := genaddsstringcharoptnode(self);
  1328. firstpass(hp);
  1329. pass_1 := hp;
  1330. exit;
  1331. end
  1332. else
  1333. begin
  1334. { Fix right to be shortstring }
  1335. if is_char(right.resulttype.def) then
  1336. begin
  1337. inserttypeconv(right,cshortstringtype);
  1338. firstpass(right);
  1339. end;
  1340. end;
  1341. if canbeaddsstringcsstringoptnode(self) then
  1342. begin
  1343. hp := genaddsstringcsstringoptnode(self);
  1344. firstpass(hp);
  1345. pass_1 := hp;
  1346. exit;
  1347. end;
  1348. end;
  1349. { otherwise, let addstring convert everything }
  1350. result := first_addstring;
  1351. exit;
  1352. end
  1353. { is one a real float ? }
  1354. else if (rd.deftype=floatdef) or (ld.deftype=floatdef) then
  1355. begin
  1356. calcregisters(self,0,1,0);
  1357. location.loc:=LOC_FPU;
  1358. end
  1359. { pointer comperation and subtraction }
  1360. else if (ld.deftype=pointerdef) then
  1361. begin
  1362. location.loc:=LOC_REGISTER;
  1363. calcregisters(self,1,0,0);
  1364. end
  1365. else if is_class_or_interface(ld) then
  1366. begin
  1367. location.loc:=LOC_REGISTER;
  1368. calcregisters(self,1,0,0);
  1369. end
  1370. else if (ld.deftype=classrefdef) then
  1371. begin
  1372. location.loc:=LOC_REGISTER;
  1373. calcregisters(self,1,0,0);
  1374. end
  1375. { support procvar=nil,procvar<>nil }
  1376. else if ((ld.deftype=procvardef) and (rt=niln)) or
  1377. ((rd.deftype=procvardef) and (lt=niln)) then
  1378. begin
  1379. calcregisters(self,1,0,0);
  1380. location.loc:=LOC_REGISTER;
  1381. end
  1382. {$ifdef SUPPORT_MMX}
  1383. { mmx support, this must be before the zero based array
  1384. check }
  1385. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  1386. is_mmx_able_array(rd) then
  1387. begin
  1388. location.loc:=LOC_MMXREGISTER;
  1389. calcregisters(self,0,0,1);
  1390. end
  1391. {$endif SUPPORT_MMX}
  1392. else if (rd.deftype=pointerdef) or (ld.deftype=pointerdef) then
  1393. begin
  1394. location.loc:=LOC_REGISTER;
  1395. calcregisters(self,1,0,0);
  1396. end
  1397. else if (rd.deftype=procvardef) and (ld.deftype=procvardef) and is_equal(rd,ld) then
  1398. begin
  1399. calcregisters(self,1,0,0);
  1400. location.loc:=LOC_REGISTER;
  1401. end
  1402. else if (ld.deftype=enumdef) then
  1403. begin
  1404. calcregisters(self,1,0,0);
  1405. end
  1406. {$ifdef SUPPORT_MMX}
  1407. else if (cs_mmx in aktlocalswitches) and
  1408. is_mmx_able_array(ld) and
  1409. is_mmx_able_array(rd) then
  1410. begin
  1411. location.loc:=LOC_MMXREGISTER;
  1412. calcregisters(self,0,0,1);
  1413. end
  1414. {$endif SUPPORT_MMX}
  1415. { the general solution is to convert to 32 bit int }
  1416. else
  1417. begin
  1418. calcregisters(self,1,0,0);
  1419. location.loc:=LOC_REGISTER;
  1420. end;
  1421. case nodetype of
  1422. ltn,lten,gtn,gten,equaln,unequaln:
  1423. begin
  1424. if is_64bitint(left.resulttype.def) then
  1425. location.loc:=LOC_JUMP
  1426. else
  1427. location.loc:=LOC_FLAGS;
  1428. end;
  1429. xorn:
  1430. begin
  1431. location.loc:=LOC_REGISTER;
  1432. end;
  1433. end;
  1434. end;
  1435. begin
  1436. caddnode:=taddnode;
  1437. end.
  1438. {
  1439. $Log$
  1440. Revision 1.38 2001-09-04 11:38:54 jonas
  1441. + searchsystype() and searchsystype() functions in symtable
  1442. * changed ninl and nadd to use these functions
  1443. * i386 set comparison functions now return their results in al instead
  1444. of in the flags so that they can be sued as compilerprocs
  1445. - removed all processor specific code from n386add.pas that has to do
  1446. with set handling, it's now all done in nadd.pas
  1447. * fixed fpc_set_contains_sets in genset.inc
  1448. * fpc_set_in_byte is now coded inline in n386set.pas and doesn't use a
  1449. helper anymore
  1450. * some small fixes in compproc.inc/set.inc regarding the declaration of
  1451. internal helper types (fpc_small_set and fpc_normal_set)
  1452. Revision 1.37 2001/09/03 13:27:42 jonas
  1453. * compilerproc implementation of set addition/substraction/...
  1454. * changed the declaration of some set helpers somewhat to accomodate the
  1455. above change
  1456. * i386 still uses the old code for comparisons of sets, because its
  1457. helpers return the results in the flags
  1458. * dummy tc_normal_2_small_set type conversion because I need the original
  1459. resulttype of the set add nodes
  1460. NOTE: you have to start a cycle with 1.0.5!
  1461. Revision 1.36 2001/09/02 21:12:06 peter
  1462. * move class of definitions into type section for delphi
  1463. Revision 1.35 2001/08/31 15:42:15 jonas
  1464. * added missing type conversion from small to normal sets
  1465. Revision 1.34 2001/08/30 15:43:14 jonas
  1466. * converted adding/comparing of strings to compileproc. Note that due
  1467. to the way the shortstring helpers for i386 are written, they are
  1468. still handled by the old code (reason: fpc_shortstr_compare returns
  1469. results in the flags instead of in eax and fpc_shortstr_concat
  1470. has wierd parameter conventions). The compilerproc stuff should work
  1471. fine with the generic implementations though.
  1472. * removed some nested comments warnings
  1473. Revision 1.33 2001/08/26 13:36:38 florian
  1474. * some cg reorganisation
  1475. * some PPC updates
  1476. Revision 1.32 2001/08/06 21:40:46 peter
  1477. * funcret moved from tprocinfo to tprocdef
  1478. Revision 1.31 2001/07/08 21:00:14 peter
  1479. * various widestring updates, it works now mostly without charset
  1480. mapping supported
  1481. Revision 1.30 2001/06/04 21:41:26 peter
  1482. * readded generic conversion to s32bit that i removed yesterday. It
  1483. is still used for error recovery, added a small note about that
  1484. Revision 1.29 2001/06/04 18:13:53 peter
  1485. * Support kylix hack of having enum+integer in a enum declaration.
  1486. Revision 1.28 2001/05/27 14:30:55 florian
  1487. + some widestring stuff added
  1488. Revision 1.27 2001/05/19 21:11:50 peter
  1489. * first check for overloaded operator before doing inserting any
  1490. typeconvs
  1491. Revision 1.26 2001/05/19 12:53:52 peter
  1492. * check set types when doing constant set evaluation
  1493. Revision 1.25 2001/04/13 01:22:08 peter
  1494. * symtable change to classes
  1495. * range check generation and errors fixed, make cycle DEBUG=1 works
  1496. * memory leaks fixed
  1497. Revision 1.24 2001/04/04 22:42:39 peter
  1498. * move constant folding into det_resulttype
  1499. Revision 1.23 2001/04/02 21:20:30 peter
  1500. * resulttype rewrite
  1501. Revision 1.22 2001/02/04 11:12:17 jonas
  1502. * fixed web bug 1377 & const pointer arithmtic
  1503. Revision 1.21 2001/01/14 22:13:13 peter
  1504. * constant calculation fixed. The type of the new constant is now
  1505. defined after the calculation is done. This should remove a lot
  1506. of wrong warnings (and errors with -Cr).
  1507. Revision 1.20 2000/12/31 11:14:10 jonas
  1508. + implemented/fixed docompare() mathods for all nodes (not tested)
  1509. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  1510. and constant strings/chars together
  1511. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  1512. when adding
  1513. Revision 1.19 2000/12/16 15:55:32 jonas
  1514. + warning when there is a chance to get a range check error because of
  1515. automatic type conversion to u32bit
  1516. * arithmetic operations with a cardinal and a signed operand are carried
  1517. out in 64bit when range checking is on ("merged" from fixes branch)
  1518. Revision 1.18 2000/11/29 00:30:31 florian
  1519. * unused units removed from uses clause
  1520. * some changes for widestrings
  1521. Revision 1.17 2000/11/20 15:30:42 jonas
  1522. * changed types of values used for constant expression evaluation to
  1523. tconstexprint
  1524. Revision 1.16 2000/11/13 11:30:55 florian
  1525. * some bugs with interfaces and NIL fixed
  1526. Revision 1.15 2000/11/04 14:25:20 florian
  1527. + merged Attila's changes for interfaces, not tested yet
  1528. Revision 1.14 2000/10/31 22:02:47 peter
  1529. * symtable splitted, no real code changes
  1530. Revision 1.13 2000/10/14 10:14:50 peter
  1531. * moehrendorf oct 2000 rewrite
  1532. Revision 1.12 2000/10/01 19:48:23 peter
  1533. * lot of compile updates for cg11
  1534. Revision 1.11 2000/09/30 16:08:45 peter
  1535. * more cg11 updates
  1536. Revision 1.10 2000/09/28 19:49:52 florian
  1537. *** empty log message ***
  1538. Revision 1.9 2000/09/27 21:33:22 florian
  1539. * finally nadd.pas compiles
  1540. Revision 1.8 2000/09/27 20:25:44 florian
  1541. * more stuff fixed
  1542. Revision 1.7 2000/09/27 18:14:31 florian
  1543. * fixed a lot of syntax errors in the n*.pas stuff
  1544. Revision 1.6 2000/09/24 15:06:19 peter
  1545. * use defines.inc
  1546. Revision 1.5 2000/09/22 22:42:52 florian
  1547. * more fixes
  1548. Revision 1.4 2000/09/21 12:22:42 jonas
  1549. * put piece of code between -dnewoptimizations2 since it wasn't
  1550. necessary otherwise
  1551. + support for full boolean evaluation (from tcadd)
  1552. Revision 1.3 2000/09/20 21:50:59 florian
  1553. * updated
  1554. Revision 1.2 2000/08/29 08:24:45 jonas
  1555. * some modifications to -dcardinalmulfix code
  1556. Revision 1.1 2000/08/26 12:24:20 florian
  1557. * initial release
  1558. }