nadd.pas 57 KB

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