nadd.pas 70 KB

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