nadd.pas 126 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Type checking and simplification for add nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nadd;
  18. {$i fpcdefs.inc}
  19. { define addstringopt}
  20. interface
  21. uses
  22. node,symtype;
  23. type
  24. taddnode = class(tbinopnode)
  25. private
  26. resultrealdefderef: tderef;
  27. function pass_typecheck_internal:tnode;
  28. public
  29. resultrealdef : tdef;
  30. constructor create(tt : tnodetype;l,r : tnode);override;
  31. constructor create_internal(tt:tnodetype;l,r:tnode);
  32. constructor ppuload(t:tnodetype;ppufile:tcompilerppufile);override;
  33. procedure ppuwrite(ppufile:tcompilerppufile);override;
  34. procedure buildderefimpl;override;
  35. procedure derefimpl;override;
  36. function pass_1 : tnode;override;
  37. function pass_typecheck:tnode;override;
  38. function simplify(forinline: boolean) : tnode;override;
  39. function dogetcopy : tnode;override;
  40. function docompare(p: tnode): boolean; override;
  41. {$ifdef state_tracking}
  42. function track_state_pass(exec_known:boolean):boolean;override;
  43. {$endif}
  44. protected
  45. { override the following if you want to implement }
  46. { parts explicitely in the code generator (JM) }
  47. function first_addstring: tnode; virtual;
  48. function first_addset: tnode; virtual;
  49. { only implements "muln" nodes, the rest always has to be done in }
  50. { the code generator for performance reasons (JM) }
  51. function first_add64bitint: tnode; virtual;
  52. { override and return false if you can handle 32x32->64 }
  53. { bit multiplies directly in your code generator. If }
  54. { this function is overridden to return false, you can }
  55. { get multiplies with left/right both s32bit or u32bit, }
  56. { and resultdef of the muln s64bit or u64bit }
  57. function use_generic_mul32to64: boolean; virtual;
  58. { This routine calls internal runtime library helpers
  59. for all floating point arithmetic in the case
  60. where the emulation switches is on. Otherwise
  61. returns nil, and everything must be done in
  62. the code generation phase.
  63. }
  64. function first_addfloat : tnode; virtual;
  65. private
  66. { checks whether a muln can be calculated as a 32bit }
  67. { * 32bit -> 64 bit }
  68. function try_make_mul32to64: boolean;
  69. { Match against the ranges, i.e.:
  70. var a:1..10;
  71. begin
  72. if a>0 then
  73. ...
  74. always evaluates to true. (DM)
  75. }
  76. function cmp_of_disjunct_ranges(var res : boolean) : boolean;
  77. end;
  78. taddnodeclass = class of taddnode;
  79. var
  80. { caddnode is used to create nodes of the add type }
  81. { the virtual constructor allows to assign }
  82. { another class type to caddnode => processor }
  83. { specific node types can be created }
  84. caddnode : taddnodeclass = taddnode;
  85. implementation
  86. uses
  87. {$IFNDEF USE_FAKE_SYSUTILS}
  88. sysutils,
  89. {$ELSE}
  90. fksysutl,
  91. {$ENDIF}
  92. globtype,systems,constexp,
  93. cutils,verbose,globals,widestr,
  94. symconst,symdef,symsym,symtable,defutil,defcmp,
  95. cgbase,
  96. htypechk,pass_1,
  97. nld,nbas,nmat,ncnv,ncon,nset,nopt,ncal,ninl,nmem,nutils,
  98. {$ifdef state_tracking}
  99. nstate,
  100. {$endif}
  101. cpuinfo,procinfo;
  102. {*****************************************************************************
  103. TADDNODE
  104. *****************************************************************************}
  105. {$maxfpuregisters 0}
  106. function getbestreal(t1,t2 : tdef) : tdef;
  107. const
  108. floatweight : array[tfloattype] of byte =
  109. (2,3,4,5,0,1,6);
  110. begin
  111. if t1.typ=floatdef then
  112. begin
  113. result:=t1;
  114. if t2.typ=floatdef then
  115. begin
  116. { when a comp or currency is used, use always the
  117. best float type to calculate the result }
  118. if (tfloatdef(t2).floattype in [s64comp,s64currency]) or
  119. (tfloatdef(t2).floattype in [s64comp,s64currency]) then
  120. result:=pbestrealtype^
  121. else
  122. if floatweight[tfloatdef(t2).floattype]>floatweight[tfloatdef(t1).floattype] then
  123. result:=t2;
  124. end;
  125. end
  126. else if t2.typ=floatdef then
  127. result:=t2
  128. else internalerror(200508061);
  129. end;
  130. constructor taddnode.create(tt : tnodetype;l,r : tnode);
  131. begin
  132. inherited create(tt,l,r);
  133. end;
  134. constructor taddnode.create_internal(tt:tnodetype;l,r:tnode);
  135. begin
  136. create(tt,l,r);
  137. include(flags,nf_internal);
  138. end;
  139. constructor taddnode.ppuload(t: tnodetype; ppufile: tcompilerppufile);
  140. begin
  141. inherited ppuload(t, ppufile);
  142. ppufile.getderef(resultrealdefderef);
  143. end;
  144. procedure taddnode.ppuwrite(ppufile: tcompilerppufile);
  145. begin
  146. inherited ppuwrite(ppufile);
  147. ppufile.putderef(resultrealdefderef);
  148. end;
  149. procedure taddnode.buildderefimpl;
  150. begin
  151. inherited buildderefimpl;
  152. resultrealdefderef.build(resultrealdef);
  153. end;
  154. procedure taddnode.derefimpl;
  155. begin
  156. inherited derefimpl;
  157. resultrealdef:=tdef(resultrealdefderef.resolve);
  158. end;
  159. function taddnode.cmp_of_disjunct_ranges(var res : boolean) : boolean;
  160. var
  161. hp : tnode;
  162. realdef : tdef;
  163. v : tconstexprint;
  164. begin
  165. result:=false;
  166. { check for comparision with known result because the ranges of the operands don't overlap }
  167. if (is_constintnode(right) and (left.resultdef.typ=orddef) and
  168. { don't ignore type checks }
  169. is_subequal(right.resultdef,left.resultdef)) or
  170. (is_constintnode(left) and (right.resultdef.typ=orddef) and
  171. { don't ignore type checks }
  172. is_subequal(left.resultdef,right.resultdef)) then
  173. begin
  174. if is_constintnode(right) then
  175. begin
  176. hp:=left;
  177. v:=Tordconstnode(right).value;
  178. end
  179. else
  180. begin
  181. hp:=right;
  182. v:=Tordconstnode(left).value;
  183. end;
  184. realdef:=hp.resultdef;
  185. { stop with finding the real def when we either encounter
  186. a) an explicit type conversion (then the value has to be
  187. re-interpreted)
  188. b) an "absolute" type conversion (also requires
  189. re-interpretation)
  190. }
  191. while (hp.nodetype=typeconvn) and
  192. ([nf_internal,nf_explicit,nf_absolute] * hp.flags = []) do
  193. begin
  194. hp:=ttypeconvnode(hp).left;
  195. realdef:=hp.resultdef;
  196. end;
  197. if is_constintnode(left) then
  198. with torddef(realdef) do
  199. case nodetype of
  200. ltn:
  201. if v<low then
  202. begin
  203. result:=true;
  204. res:=true;
  205. end
  206. else if v>=high then
  207. begin
  208. result:=true;
  209. res:=false;
  210. end;
  211. lten:
  212. if v<=low then
  213. begin
  214. result:=true;
  215. res:=true;
  216. end
  217. else if v>high then
  218. begin
  219. result:=true;
  220. res:=false;
  221. end;
  222. gtn:
  223. if v<=low then
  224. begin
  225. result:=true;
  226. res:=false;
  227. end
  228. else if v>high then
  229. begin
  230. result:=true;
  231. res:=true;
  232. end;
  233. gten :
  234. if v<low then
  235. begin
  236. result:=true;
  237. res:=false;
  238. end
  239. else if v>=high then
  240. begin
  241. result:=true;
  242. res:=true;
  243. end;
  244. equaln:
  245. if (v<low) or (v>high) then
  246. begin
  247. result:=true;
  248. res:=false;
  249. end;
  250. unequaln:
  251. if (v<low) or (v>high) then
  252. begin
  253. result:=true;
  254. res:=true;
  255. end;
  256. end
  257. else
  258. with torddef(realdef) do
  259. case nodetype of
  260. ltn:
  261. if high<v then
  262. begin
  263. result:=true;
  264. res:=true;
  265. end
  266. else if low>=v then
  267. begin
  268. result:=true;
  269. res:=false;
  270. end;
  271. lten:
  272. if high<=v then
  273. begin
  274. result:=true;
  275. res:=true;
  276. end
  277. else if low>v then
  278. begin
  279. result:=true;
  280. res:=false;
  281. end;
  282. gtn:
  283. if high<=v then
  284. begin
  285. result:=true;
  286. res:=false;
  287. end
  288. else if low>v then
  289. begin
  290. result:=true;
  291. res:=true;
  292. end;
  293. gten:
  294. if high<v then
  295. begin
  296. result:=true;
  297. res:=false;
  298. end
  299. else if low>=v then
  300. begin
  301. result:=true;
  302. res:=true;
  303. end;
  304. equaln:
  305. if (v<low) or (v>high) then
  306. begin
  307. result:=true;
  308. res:=false;
  309. end;
  310. unequaln:
  311. if (v<low) or (v>high) then
  312. begin
  313. result:=true;
  314. res:=true;
  315. end;
  316. end;
  317. end;
  318. end;
  319. function taddnode.simplify(forinline : boolean) : tnode;
  320. var
  321. t : tnode;
  322. lt,rt : tnodetype;
  323. rd,ld : tdef;
  324. rv,lv,v : tconstexprint;
  325. rvd,lvd : bestreal;
  326. ws1,ws2 : pcompilerwidestring;
  327. concatstrings : boolean;
  328. c1,c2 : array[0..1] of char;
  329. s1,s2 : pchar;
  330. l1,l2 : longint;
  331. resultset : Tconstset;
  332. res,
  333. b : boolean;
  334. begin
  335. result:=nil;
  336. { load easier access variables }
  337. rd:=right.resultdef;
  338. ld:=left.resultdef;
  339. rt:=right.nodetype;
  340. lt:=left.nodetype;
  341. if (nodetype = slashn) and
  342. (((rt = ordconstn) and
  343. (tordconstnode(right).value = 0)) or
  344. ((rt = realconstn) and
  345. (trealconstnode(right).value_real = 0.0))) then
  346. begin
  347. if floating_point_range_check_error then
  348. begin
  349. result:=crealconstnode.create(1,pbestrealtype^);
  350. Message(parser_e_division_by_zero);
  351. exit;
  352. end;
  353. end;
  354. { both are int constants }
  355. if (
  356. (
  357. is_constintnode(left) and
  358. is_constintnode(right)
  359. ) or
  360. (
  361. is_constboolnode(left) and
  362. is_constboolnode(right) and
  363. (nodetype in [slashn,ltn,lten,gtn,gten,equaln,unequaln,andn,xorn,orn])
  364. ) or
  365. (
  366. is_constenumnode(left) and
  367. is_constenumnode(right) and
  368. allowenumop(nodetype))
  369. ) or
  370. (
  371. (lt = pointerconstn) and
  372. is_constintnode(right) and
  373. (nodetype in [addn,subn])
  374. ) or
  375. (
  376. (lt in [pointerconstn,niln]) and
  377. (rt in [pointerconstn,niln]) and
  378. (nodetype in [ltn,lten,gtn,gten,equaln,unequaln,subn])
  379. ) then
  380. begin
  381. t:=nil;
  382. { load values }
  383. case lt of
  384. ordconstn:
  385. lv:=tordconstnode(left).value;
  386. pointerconstn:
  387. lv:=tpointerconstnode(left).value;
  388. niln:
  389. lv:=0;
  390. else
  391. internalerror(2002080202);
  392. end;
  393. case rt of
  394. ordconstn:
  395. rv:=tordconstnode(right).value;
  396. pointerconstn:
  397. rv:=tpointerconstnode(right).value;
  398. niln:
  399. rv:=0;
  400. else
  401. internalerror(2002080203);
  402. end;
  403. { type checking already took care of multiplying }
  404. { integer constants with pointeddef.size if necessary }
  405. case nodetype of
  406. addn :
  407. begin
  408. v:=lv+rv;
  409. if v.overflow then
  410. begin
  411. Message(parser_e_arithmetic_operation_overflow);
  412. { Recover }
  413. t:=genintconstnode(0)
  414. end
  415. else if (lt=pointerconstn) then
  416. t := cpointerconstnode.create(qword(v),resultdef)
  417. else
  418. if is_integer(ld) then
  419. t := create_simplified_ord_const(v,resultdef,forinline)
  420. else
  421. t := cordconstnode.create(v,resultdef,(ld.typ<>enumdef));
  422. end;
  423. subn :
  424. begin
  425. v:=lv-rv;
  426. if v.overflow then
  427. begin
  428. Message(parser_e_arithmetic_operation_overflow);
  429. { Recover }
  430. t:=genintconstnode(0)
  431. end
  432. else if (lt=pointerconstn) then
  433. { pointer-pointer results in an integer }
  434. if (rt=pointerconstn) then
  435. begin
  436. if not(nf_has_pointerdiv in flags) then
  437. internalerror(2008030101);
  438. t := cpointerconstnode.create(qword(v),resultdef)
  439. end
  440. else
  441. t := cpointerconstnode.create(qword(v),resultdef)
  442. else
  443. if is_integer(ld) then
  444. t := create_simplified_ord_const(v,resultdef,forinline)
  445. else
  446. t:=cordconstnode.create(v,resultdef,(ld.typ<>enumdef));
  447. end;
  448. muln :
  449. begin
  450. v:=lv*rv;
  451. if v.overflow then
  452. begin
  453. message(parser_e_arithmetic_operation_overflow);
  454. { Recover }
  455. t:=genintconstnode(0)
  456. end
  457. else
  458. t := create_simplified_ord_const(v,resultdef,forinline)
  459. end;
  460. xorn :
  461. if is_integer(ld) then
  462. t := create_simplified_ord_const(lv xor rv,resultdef,forinline)
  463. else
  464. t:=cordconstnode.create(lv xor rv,resultdef,true);
  465. orn :
  466. if is_integer(ld) then
  467. t:=create_simplified_ord_const(lv or rv,resultdef,forinline)
  468. else
  469. t:=cordconstnode.create(lv or rv,resultdef,true);
  470. andn :
  471. if is_integer(ld) then
  472. t:=create_simplified_ord_const(lv and rv,resultdef,forinline)
  473. else
  474. t:=cordconstnode.create(lv and rv,resultdef,true);
  475. ltn :
  476. t:=cordconstnode.create(ord(lv<rv),pasbool8type,true);
  477. lten :
  478. t:=cordconstnode.create(ord(lv<=rv),pasbool8type,true);
  479. gtn :
  480. t:=cordconstnode.create(ord(lv>rv),pasbool8type,true);
  481. gten :
  482. t:=cordconstnode.create(ord(lv>=rv),pasbool8type,true);
  483. equaln :
  484. t:=cordconstnode.create(ord(lv=rv),pasbool8type,true);
  485. unequaln :
  486. t:=cordconstnode.create(ord(lv<>rv),pasbool8type,true);
  487. slashn :
  488. begin
  489. { int/int becomes a real }
  490. rvd:=rv;
  491. lvd:=lv;
  492. t:=crealconstnode.create(lvd/rvd,resultrealdef);
  493. end;
  494. else
  495. internalerror(2008022101);
  496. end;
  497. result:=t;
  498. exit;
  499. end
  500. else if cmp_of_disjunct_ranges(res) then
  501. begin
  502. if res then
  503. t:=Cordconstnode.create(1,pasbool8type,true)
  504. else
  505. t:=Cordconstnode.create(0,pasbool8type,true);
  506. { don't do this optimization, if the variable expression might
  507. have a side effect }
  508. if (is_constintnode(left) and might_have_sideeffects(right)) or
  509. (is_constintnode(right) and might_have_sideeffects(left)) then
  510. t.free
  511. else
  512. result:=t;
  513. exit;
  514. end;
  515. { Add,Sub,Mul,Or,Xor,Andn with constant 0, 1 or -1? }
  516. if is_constintnode(right) and is_integer(left.resultdef) then
  517. begin
  518. if tordconstnode(right).value = 0 then
  519. begin
  520. case nodetype of
  521. addn,subn,orn,xorn:
  522. result := left.getcopy;
  523. andn,muln:
  524. result:=cordconstnode.create(0,resultdef,true);
  525. end;
  526. end
  527. else if tordconstnode(right).value = 1 then
  528. begin
  529. case nodetype of
  530. muln:
  531. result := left.getcopy;
  532. end;
  533. end
  534. {$ifdef VER2_2}
  535. else if (tordconstnode(right).value.svalue = -1) and (tordconstnode(right).value.signed) then
  536. {$else}
  537. else if tordconstnode(right).value = -1 then
  538. {$endif}
  539. begin
  540. case nodetype of
  541. muln:
  542. result := cunaryminusnode.create(left.getcopy);
  543. end;
  544. end;
  545. if assigned(result) then
  546. exit;
  547. end;
  548. if is_constintnode(left) and is_integer(right.resultdef) then
  549. begin
  550. if tordconstnode(left).value = 0 then
  551. begin
  552. case nodetype of
  553. addn,orn,xorn:
  554. result := right.getcopy;
  555. subn:
  556. result := cunaryminusnode.create(right.getcopy);
  557. andn,muln:
  558. result:=cordconstnode.create(0,right.resultdef,true);
  559. end;
  560. end
  561. else if tordconstnode(left).value = 1 then
  562. begin
  563. case nodetype of
  564. muln:
  565. result := right.getcopy;
  566. end;
  567. end
  568. {$ifdef VER2_2}
  569. else if (tordconstnode(left).value.svalue = -1) and (tordconstnode(left).value.signed) then
  570. {$else}
  571. else if tordconstnode(left).value = -1 then
  572. {$endif}
  573. begin
  574. case nodetype of
  575. muln:
  576. result := cunaryminusnode.create(right.getcopy);
  577. end;
  578. end;
  579. if assigned(result) then
  580. exit;
  581. end;
  582. { both real constants ? }
  583. if (lt=realconstn) and (rt=realconstn) then
  584. begin
  585. lvd:=trealconstnode(left).value_real;
  586. rvd:=trealconstnode(right).value_real;
  587. case nodetype of
  588. addn :
  589. t:=crealconstnode.create(lvd+rvd,resultrealdef);
  590. subn :
  591. t:=crealconstnode.create(lvd-rvd,resultrealdef);
  592. muln :
  593. t:=crealconstnode.create(lvd*rvd,resultrealdef);
  594. starstarn:
  595. begin
  596. if lvd<0 then
  597. begin
  598. Message(parser_e_invalid_float_operation);
  599. t:=crealconstnode.create(0,resultrealdef);
  600. end
  601. else if lvd=0 then
  602. t:=crealconstnode.create(1.0,resultrealdef)
  603. else
  604. t:=crealconstnode.create(exp(ln(lvd)*rvd),resultrealdef);
  605. end;
  606. slashn :
  607. t:=crealconstnode.create(lvd/rvd,resultrealdef);
  608. ltn :
  609. t:=cordconstnode.create(ord(lvd<rvd),pasbool8type,true);
  610. lten :
  611. t:=cordconstnode.create(ord(lvd<=rvd),pasbool8type,true);
  612. gtn :
  613. t:=cordconstnode.create(ord(lvd>rvd),pasbool8type,true);
  614. gten :
  615. t:=cordconstnode.create(ord(lvd>=rvd),pasbool8type,true);
  616. equaln :
  617. t:=cordconstnode.create(ord(lvd=rvd),pasbool8type,true);
  618. unequaln :
  619. t:=cordconstnode.create(ord(lvd<>rvd),pasbool8type,true);
  620. else
  621. internalerror(2008022102);
  622. end;
  623. result:=t;
  624. exit;
  625. end;
  626. { first, we handle widestrings, so we can check later for }
  627. { stringconstn only }
  628. { widechars are converted above to widestrings too }
  629. { this isn't ver y efficient, but I don't think }
  630. { that it does matter that much (FK) }
  631. if (lt=stringconstn) and (rt=stringconstn) and
  632. (tstringconstnode(left).cst_type in [cst_widestring,cst_unicodestring]) and
  633. (tstringconstnode(right).cst_type in [cst_widestring,cst_unicodestring]) then
  634. begin
  635. initwidestring(ws1);
  636. initwidestring(ws2);
  637. copywidestring(pcompilerwidestring(tstringconstnode(left).value_str),ws1);
  638. copywidestring(pcompilerwidestring(tstringconstnode(right).value_str),ws2);
  639. case nodetype of
  640. addn :
  641. begin
  642. concatwidestrings(ws1,ws2);
  643. t:=cstringconstnode.createunistr(ws1);
  644. end;
  645. ltn :
  646. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<0),pasbool8type,true);
  647. lten :
  648. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<=0),pasbool8type,true);
  649. gtn :
  650. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>0),pasbool8type,true);
  651. gten :
  652. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>=0),pasbool8type,true);
  653. equaln :
  654. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)=0),pasbool8type,true);
  655. unequaln :
  656. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<>0),pasbool8type,true);
  657. else
  658. internalerror(2008022103);
  659. end;
  660. donewidestring(ws1);
  661. donewidestring(ws2);
  662. result:=t;
  663. exit;
  664. end;
  665. { concating strings ? }
  666. concatstrings:=false;
  667. if (lt=ordconstn) and (rt=ordconstn) and
  668. is_char(ld) and is_char(rd) then
  669. begin
  670. c1[0]:=char(int64(tordconstnode(left).value));
  671. c1[1]:=#0;
  672. l1:=1;
  673. c2[0]:=char(int64(tordconstnode(right).value));
  674. c2[1]:=#0;
  675. l2:=1;
  676. s1:=@c1[0];
  677. s2:=@c2[0];
  678. concatstrings:=true;
  679. end
  680. else if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  681. begin
  682. s1:=tstringconstnode(left).value_str;
  683. l1:=tstringconstnode(left).len;
  684. c2[0]:=char(int64(tordconstnode(right).value));
  685. c2[1]:=#0;
  686. s2:=@c2[0];
  687. l2:=1;
  688. concatstrings:=true;
  689. end
  690. else if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  691. begin
  692. c1[0]:=char(int64(tordconstnode(left).value));
  693. c1[1]:=#0;
  694. l1:=1;
  695. s1:=@c1[0];
  696. s2:=tstringconstnode(right).value_str;
  697. l2:=tstringconstnode(right).len;
  698. concatstrings:=true;
  699. end
  700. else if (lt=stringconstn) and (rt=stringconstn) then
  701. begin
  702. s1:=tstringconstnode(left).value_str;
  703. l1:=tstringconstnode(left).len;
  704. s2:=tstringconstnode(right).value_str;
  705. l2:=tstringconstnode(right).len;
  706. concatstrings:=true;
  707. end;
  708. if concatstrings then
  709. begin
  710. case nodetype of
  711. addn :
  712. begin
  713. t:=cstringconstnode.createpchar(concatansistrings(s1,s2,l1,l2),l1+l2,nil);
  714. typecheckpass(t);
  715. tstringconstnode(t).changestringtype(resultdef);
  716. end;
  717. ltn :
  718. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<0),pasbool8type,true);
  719. lten :
  720. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<=0),pasbool8type,true);
  721. gtn :
  722. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>0),pasbool8type,true);
  723. gten :
  724. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>=0),pasbool8type,true);
  725. equaln :
  726. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)=0),pasbool8type,true);
  727. unequaln :
  728. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<>0),pasbool8type,true);
  729. else
  730. internalerror(2008022104);
  731. end;
  732. result:=t;
  733. exit;
  734. end;
  735. { set constant evaluation }
  736. if (right.nodetype=setconstn) and
  737. not assigned(tsetconstnode(right).left) and
  738. (left.nodetype=setconstn) and
  739. not assigned(tsetconstnode(left).left) then
  740. begin
  741. case nodetype of
  742. addn :
  743. begin
  744. resultset:=tsetconstnode(right).value_set^ + tsetconstnode(left).value_set^;
  745. t:=csetconstnode.create(@resultset,resultdef);
  746. end;
  747. muln :
  748. begin
  749. resultset:=tsetconstnode(right).value_set^ * tsetconstnode(left).value_set^;
  750. t:=csetconstnode.create(@resultset,resultdef);
  751. end;
  752. subn :
  753. begin
  754. resultset:=tsetconstnode(left).value_set^ - tsetconstnode(right).value_set^;
  755. t:=csetconstnode.create(@resultset,resultdef);
  756. end;
  757. symdifn :
  758. begin
  759. resultset:=tsetconstnode(right).value_set^ >< tsetconstnode(left).value_set^;
  760. t:=csetconstnode.create(@resultset,resultdef);
  761. end;
  762. unequaln :
  763. begin
  764. b:=tsetconstnode(right).value_set^ <> tsetconstnode(left).value_set^;
  765. t:=cordconstnode.create(byte(b),pasbool8type,true);
  766. end;
  767. equaln :
  768. begin
  769. b:=tsetconstnode(right).value_set^ = tsetconstnode(left).value_set^;
  770. t:=cordconstnode.create(byte(b),pasbool8type,true);
  771. end;
  772. lten :
  773. begin
  774. b:=tsetconstnode(left).value_set^ <= tsetconstnode(right).value_set^;
  775. t:=cordconstnode.create(byte(b),pasbool8type,true);
  776. end;
  777. gten :
  778. begin
  779. b:=tsetconstnode(left).value_set^ >= tsetconstnode(right).value_set^;
  780. t:=cordconstnode.create(byte(b),pasbool8type,true);
  781. end;
  782. else
  783. internalerror(2008022105);
  784. end;
  785. result:=t;
  786. exit;
  787. end;
  788. { slow simplifications }
  789. if (cs_opt_level2 in current_settings.optimizerswitches) then
  790. begin
  791. { the comparison is might be expensive and the nodes are usually only
  792. equal if some previous optimizations were done so don't check
  793. this simplification always
  794. }
  795. if is_boolean(left.resultdef) and is_boolean(right.resultdef) and
  796. { even when short circuit boolean evaluation is active, this
  797. optimization cannot be performed in case the node has
  798. side effects, because this can change the result (e.g., in an
  799. or-node that calls the same function twice and first returns
  800. false and then true because of a global state change }
  801. not might_have_sideeffects(left) then
  802. begin
  803. if left.isequal(right) then
  804. begin
  805. case nodetype of
  806. andn,orn:
  807. begin
  808. result:=left;
  809. left:=nil;
  810. exit;
  811. end;
  812. {
  813. xorn:
  814. begin
  815. result:=cordconstnode.create(0,resultdef,true);
  816. exit;
  817. end;
  818. }
  819. end;
  820. end;
  821. end;
  822. { using sqr(x) for reals instead of x*x might reduces register pressure and/or
  823. memory accesses while sqr(<real>) has no drawback }
  824. if
  825. {$ifdef cpufpemu}
  826. (current_settings.fputype<>fpu_soft) and
  827. not(cs_fp_emulation in current_settings.moduleswitches) and
  828. {$endif cpufpemu}
  829. (nodetype=muln) and
  830. is_real(left.resultdef) and is_real(right.resultdef) and
  831. left.isequal(right) and
  832. not(might_have_sideeffects(left)) then
  833. begin
  834. result:=cinlinenode.create(in_sqr_real,false,left);
  835. left:=nil;
  836. exit;
  837. end;
  838. {$ifdef cpurox}
  839. { optimize (i shl x) or (i shr (bitsizeof(i)-x)) into rol(x,i) (and different flavours with shl/shr swapped etc.) }
  840. if (nodetype=orn)
  841. {$ifndef cpu64bitalu}
  842. and (left.resultdef.typ=orddef) and
  843. not(torddef(left.resultdef).ordtype in [s64bit,u64bit,scurrency])
  844. {$endif cpu64bitalu}
  845. then
  846. begin
  847. if (left.nodetype=shrn) and (right.nodetype=shln) and
  848. is_constintnode(tshlshrnode(left).right) and
  849. is_constintnode(tshlshrnode(right).right) and
  850. (tordconstnode(tshlshrnode(right).right).value>0) and
  851. (tordconstnode(tshlshrnode(left).right).value>0) and
  852. tshlshrnode(left).left.isequal(tshlshrnode(right).left) and
  853. not(might_have_sideeffects(tshlshrnode(left).left)) then
  854. begin
  855. if tordconstnode(tshlshrnode(left).right).value=
  856. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(right).right).value then
  857. begin
  858. result:=cinlinenode.create(in_ror_x_y,false,
  859. ccallparanode.create(tshlshrnode(left).right,
  860. ccallparanode.create(tshlshrnode(left).left,nil)));
  861. tshlshrnode(left).left:=nil;
  862. tshlshrnode(left).right:=nil;
  863. exit;
  864. end
  865. else if tordconstnode(tshlshrnode(right).right).value=
  866. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(left).right).value then
  867. begin
  868. result:=cinlinenode.create(in_rol_x_y,false,
  869. ccallparanode.create(tshlshrnode(right).right,
  870. ccallparanode.create(tshlshrnode(left).left,nil)));
  871. tshlshrnode(left).left:=nil;
  872. tshlshrnode(right).right:=nil;
  873. exit;
  874. end;
  875. end;
  876. if (left.nodetype=shln) and (right.nodetype=shrn) and
  877. is_constintnode(tshlshrnode(left).right) and
  878. is_constintnode(tshlshrnode(right).right) and
  879. (tordconstnode(tshlshrnode(right).right).value>0) and
  880. (tordconstnode(tshlshrnode(left).right).value>0) and
  881. tshlshrnode(left).left.isequal(tshlshrnode(right).left) and
  882. not(might_have_sideeffects(tshlshrnode(left).left)) then
  883. begin
  884. if tordconstnode(tshlshrnode(left).right).value=
  885. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(right).right).value then
  886. begin
  887. result:=cinlinenode.create(in_rol_x_y,false,
  888. ccallparanode.create(tshlshrnode(left).right,
  889. ccallparanode.create(tshlshrnode(left).left,nil)));
  890. tshlshrnode(left).left:=nil;
  891. tshlshrnode(left).right:=nil;
  892. exit;
  893. end
  894. else if tordconstnode(tshlshrnode(right).right).value=
  895. tshlshrnode(left).left.resultdef.size*8-tordconstnode(tshlshrnode(left).right).value then
  896. begin
  897. result:=cinlinenode.create(in_ror_x_y,false,
  898. ccallparanode.create(tshlshrnode(right).right,
  899. ccallparanode.create(tshlshrnode(left).left,nil)));
  900. tshlshrnode(left).left:=nil;
  901. tshlshrnode(right).right:=nil;
  902. exit;
  903. end;
  904. end;
  905. end;
  906. {$endif cpurox}
  907. end;
  908. end;
  909. function taddnode.dogetcopy: tnode;
  910. var
  911. n: taddnode;
  912. begin
  913. n:=taddnode(inherited dogetcopy);
  914. n.resultrealdef:=resultrealdef;
  915. result:=n;
  916. end;
  917. function taddnode.docompare(p: tnode): boolean;
  918. begin
  919. result:=
  920. inherited docompare(p) and
  921. equal_defs(taddnode(p).resultrealdef,resultrealdef);
  922. end;
  923. function taddnode.pass_typecheck:tnode;
  924. begin
  925. { This function is small to keep the stack small for recursive of
  926. large + operations }
  927. typecheckpass(left);
  928. typecheckpass(right);
  929. result:=pass_typecheck_internal;
  930. end;
  931. function taddnode.pass_typecheck_internal:tnode;
  932. var
  933. hp : tnode;
  934. rd,ld,nd : tdef;
  935. hsym : tfieldvarsym;
  936. llow,lhigh,
  937. rlow,rhigh : tconstexprint;
  938. strtype : tstringtype;
  939. res,
  940. b : boolean;
  941. lt,rt : tnodetype;
  942. ot : tnodetype;
  943. {$ifdef state_tracking}
  944. factval : Tnode;
  945. change : boolean;
  946. {$endif}
  947. begin
  948. result:=nil;
  949. { avoid any problems with type parameters later on }
  950. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  951. begin
  952. resultdef:=cundefinedtype;
  953. exit;
  954. end;
  955. { both left and right need to be valid }
  956. set_varstate(left,vs_read,[vsf_must_be_valid]);
  957. set_varstate(right,vs_read,[vsf_must_be_valid]);
  958. if codegenerror then
  959. exit;
  960. { tp procvar support. Omit for converted assigned() nodes }
  961. if not (nf_load_procvar in flags) then
  962. begin
  963. maybe_call_procvar(left,true);
  964. maybe_call_procvar(right,true);
  965. end
  966. else
  967. if not (nodetype in [equaln,unequaln]) then
  968. InternalError(2013091601);
  969. { convert array constructors to sets, because there is no other operator
  970. possible for array constructors }
  971. if is_array_constructor(left.resultdef) then
  972. begin
  973. arrayconstructor_to_set(left);
  974. typecheckpass(left);
  975. end;
  976. if is_array_constructor(right.resultdef) then
  977. begin
  978. arrayconstructor_to_set(right);
  979. typecheckpass(right);
  980. end;
  981. { allow operator overloading }
  982. hp:=self;
  983. if isbinaryoverloaded(hp) then
  984. begin
  985. result:=hp;
  986. exit;
  987. end;
  988. { Stop checking when an error was found in the operator checking }
  989. if codegenerror then
  990. begin
  991. result:=cerrornode.create;
  992. exit;
  993. end;
  994. { Kylix allows enum+ordconstn in an enum type declaration, we need to do
  995. the conversion here before the constant folding }
  996. if (m_delphi in current_settings.modeswitches) and
  997. (blocktype in [bt_type,bt_const_type,bt_var_type]) then
  998. begin
  999. if (left.resultdef.typ=enumdef) and
  1000. (right.resultdef.typ=orddef) then
  1001. begin
  1002. { insert explicit typecast to default signed int }
  1003. left:=ctypeconvnode.create_internal(left,sinttype);
  1004. typecheckpass(left);
  1005. end
  1006. else
  1007. if (left.resultdef.typ=orddef) and
  1008. (right.resultdef.typ=enumdef) then
  1009. begin
  1010. { insert explicit typecast to default signed int }
  1011. right:=ctypeconvnode.create_internal(right,sinttype);
  1012. typecheckpass(right);
  1013. end;
  1014. end;
  1015. { is one a real float, then both need to be floats, this
  1016. need to be done before the constant folding so constant
  1017. operation on a float and int are also handled }
  1018. {$ifdef x86}
  1019. { use extended as default real type only when the x87 fpu is used }
  1020. {$if defined(i386) or defined(i8086)}
  1021. if not(current_settings.fputype=fpu_x87) then
  1022. resultrealdef:=s64floattype
  1023. else
  1024. resultrealdef:=pbestrealtype^;
  1025. {$endif i386 or i8086}
  1026. {$ifdef x86_64}
  1027. { x86-64 has no x87 only mode, so use always double as default }
  1028. resultrealdef:=s64floattype;
  1029. {$endif x86_6}
  1030. {$else not x86}
  1031. resultrealdef:=pbestrealtype^;
  1032. {$endif not x86}
  1033. if (right.resultdef.typ=floatdef) or (left.resultdef.typ=floatdef) then
  1034. begin
  1035. { when both floattypes are already equal then use that
  1036. floattype for results }
  1037. if (right.resultdef.typ=floatdef) and
  1038. (left.resultdef.typ=floatdef) and
  1039. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype) then
  1040. resultrealdef:=left.resultdef
  1041. { when there is a currency type then use currency, but
  1042. only when currency is defined as float }
  1043. else
  1044. if (is_currency(right.resultdef) or
  1045. is_currency(left.resultdef)) and
  1046. ((s64currencytype.typ = floatdef) or
  1047. (nodetype <> slashn)) then
  1048. begin
  1049. resultrealdef:=s64currencytype;
  1050. inserttypeconv(right,resultrealdef);
  1051. inserttypeconv(left,resultrealdef);
  1052. end
  1053. else
  1054. begin
  1055. resultrealdef:=getbestreal(left.resultdef,right.resultdef);
  1056. inserttypeconv(right,resultrealdef);
  1057. inserttypeconv(left,resultrealdef);
  1058. end;
  1059. end;
  1060. { If both operands are constant and there is a unicodestring
  1061. or unicodestring then convert everything to unicodestring }
  1062. if is_constnode(right) and is_constnode(left) and
  1063. (is_unicodestring(right.resultdef) or
  1064. is_unicodestring(left.resultdef)) then
  1065. begin
  1066. inserttypeconv(right,cunicodestringtype);
  1067. inserttypeconv(left,cunicodestringtype);
  1068. end;
  1069. { If both operands are constant and there is a widechar
  1070. or widestring then convert everything to widestring. This
  1071. allows constant folding like char+widechar }
  1072. if is_constnode(right) and is_constnode(left) and
  1073. (is_widestring(right.resultdef) or
  1074. is_widestring(left.resultdef) or
  1075. is_widechar(right.resultdef) or
  1076. is_widechar(left.resultdef)) then
  1077. begin
  1078. inserttypeconv(right,cwidestringtype);
  1079. inserttypeconv(left,cwidestringtype);
  1080. end;
  1081. { load easier access variables }
  1082. rd:=right.resultdef;
  1083. ld:=left.resultdef;
  1084. rt:=right.nodetype;
  1085. lt:=left.nodetype;
  1086. { 4 character constant strings are compatible with orddef }
  1087. { in macpas mode (become cardinals) }
  1088. if (m_mac in current_settings.modeswitches) and
  1089. { only allow for comparisons, additions etc are }
  1090. { normally program errors }
  1091. (nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) and
  1092. (((lt=stringconstn) and
  1093. (tstringconstnode(left).len=4) and
  1094. (rd.typ=orddef)) or
  1095. ((rt=stringconstn) and
  1096. (tstringconstnode(right).len=4) and
  1097. (ld.typ=orddef))) then
  1098. begin
  1099. if (rt=stringconstn) then
  1100. begin
  1101. inserttypeconv(right,u32inttype);
  1102. rt:=right.nodetype;
  1103. rd:=right.resultdef;
  1104. end
  1105. else
  1106. begin
  1107. inserttypeconv(left,u32inttype);
  1108. lt:=left.nodetype;
  1109. ld:=left.resultdef;
  1110. end;
  1111. end;
  1112. { but an int/int gives real/real! }
  1113. if (nodetype=slashn) and not(is_vector(left.resultdef)) and not(is_vector(right.resultdef)) then
  1114. begin
  1115. if is_currency(left.resultdef) and
  1116. is_currency(right.resultdef) then
  1117. { In case of currency, converting to float means dividing by 10000 }
  1118. { However, since this is already a division, both divisions by }
  1119. { 10000 are eliminated when we divide the results -> we can skip }
  1120. { them. }
  1121. if s64currencytype.typ = floatdef then
  1122. begin
  1123. { there's no s64comptype or so, how do we avoid the type conversion?
  1124. left.resultdef := s64comptype;
  1125. right.resultdef := s64comptype; }
  1126. end
  1127. else
  1128. begin
  1129. left.resultdef := s64inttype;
  1130. right.resultdef := s64inttype;
  1131. end;
  1132. inserttypeconv(right,resultrealdef);
  1133. inserttypeconv(left,resultrealdef);
  1134. end
  1135. { if both are orddefs then check sub types }
  1136. else if (ld.typ=orddef) and (rd.typ=orddef) then
  1137. begin
  1138. { set for & and | operations in macpas mode: they only work on }
  1139. { booleans, and always short circuit evaluation }
  1140. if (nf_short_bool in flags) then
  1141. begin
  1142. if not is_boolean(ld) then
  1143. begin
  1144. inserttypeconv(left,pasbool8type);
  1145. ld := left.resultdef;
  1146. end;
  1147. if not is_boolean(rd) then
  1148. begin
  1149. inserttypeconv(right,pasbool8type);
  1150. rd := right.resultdef;
  1151. end;
  1152. end;
  1153. { 2 booleans? }
  1154. if (is_boolean(ld) and is_boolean(rd)) then
  1155. begin
  1156. case nodetype of
  1157. xorn,
  1158. andn,
  1159. orn:
  1160. begin
  1161. { Make sides equal to the largest boolean }
  1162. if (torddef(left.resultdef).size>torddef(right.resultdef).size) or
  1163. (is_cbool(left.resultdef) and not is_cbool(right.resultdef)) then
  1164. begin
  1165. right:=ctypeconvnode.create_internal(right,left.resultdef);
  1166. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  1167. typecheckpass(right);
  1168. end
  1169. else if (torddef(left.resultdef).size<torddef(right.resultdef).size) or
  1170. (not is_cbool(left.resultdef) and is_cbool(right.resultdef)) then
  1171. begin
  1172. left:=ctypeconvnode.create_internal(left,right.resultdef);
  1173. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  1174. typecheckpass(left);
  1175. end;
  1176. end;
  1177. ltn,
  1178. lten,
  1179. gtn,
  1180. gten:
  1181. begin
  1182. { convert both to pasbool to perform the comparison (so
  1183. that longbool(4) = longbool(2), since both represent
  1184. "true" }
  1185. inserttypeconv(left,pasbool8type);
  1186. inserttypeconv(right,pasbool8type);
  1187. end;
  1188. unequaln,
  1189. equaln:
  1190. begin
  1191. if not(cs_full_boolean_eval in current_settings.localswitches) or
  1192. (nf_short_bool in flags) then
  1193. begin
  1194. { Remove any compares with constants }
  1195. if (left.nodetype=ordconstn) then
  1196. begin
  1197. hp:=right;
  1198. b:=(tordconstnode(left).value<>0);
  1199. ot:=nodetype;
  1200. left.free;
  1201. left:=nil;
  1202. right:=nil;
  1203. if (not(b) and (ot=equaln)) or
  1204. (b and (ot=unequaln)) then
  1205. begin
  1206. hp:=cnotnode.create(hp);
  1207. end;
  1208. result:=hp;
  1209. exit;
  1210. end;
  1211. if (right.nodetype=ordconstn) then
  1212. begin
  1213. hp:=left;
  1214. b:=(tordconstnode(right).value<>0);
  1215. ot:=nodetype;
  1216. right.free;
  1217. right:=nil;
  1218. left:=nil;
  1219. if (not(b) and (ot=equaln)) or
  1220. (b and (ot=unequaln)) then
  1221. begin
  1222. hp:=cnotnode.create(hp);
  1223. end;
  1224. result:=hp;
  1225. exit;
  1226. end;
  1227. end;
  1228. { Delphi-compatibility: convert both to pasbool to
  1229. perform the equality comparison }
  1230. inserttypeconv(left,pasbool8type);
  1231. inserttypeconv(right,pasbool8type);
  1232. end;
  1233. else
  1234. begin
  1235. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1236. result:=cnothingnode.create;
  1237. exit;
  1238. end;
  1239. end;
  1240. end
  1241. { Both are chars? }
  1242. else if is_char(rd) and is_char(ld) then
  1243. begin
  1244. if nodetype=addn then
  1245. begin
  1246. resultdef:=cshortstringtype;
  1247. if not(is_constcharnode(left) and is_constcharnode(right)) then
  1248. begin
  1249. inserttypeconv(left,cshortstringtype);
  1250. {$ifdef addstringopt}
  1251. hp := genaddsstringcharoptnode(self);
  1252. result := hp;
  1253. exit;
  1254. {$endif addstringopt}
  1255. end
  1256. end
  1257. else if not(nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) then
  1258. begin
  1259. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1260. result:=cnothingnode.create;
  1261. exit;
  1262. end;
  1263. end
  1264. { There is a widechar? }
  1265. else if is_widechar(rd) or is_widechar(ld) then
  1266. begin
  1267. { widechar+widechar gives unicodestring }
  1268. if nodetype=addn then
  1269. begin
  1270. inserttypeconv(left,cunicodestringtype);
  1271. if (torddef(rd).ordtype<>uwidechar) then
  1272. inserttypeconv(right,cwidechartype);
  1273. resultdef:=cunicodestringtype;
  1274. end
  1275. else
  1276. begin
  1277. if (torddef(ld).ordtype<>uwidechar) then
  1278. inserttypeconv(left,cwidechartype);
  1279. if (torddef(rd).ordtype<>uwidechar) then
  1280. inserttypeconv(right,cwidechartype);
  1281. end;
  1282. end
  1283. { is there a currency type ? }
  1284. else if ((torddef(rd).ordtype=scurrency) or (torddef(ld).ordtype=scurrency)) then
  1285. begin
  1286. if (torddef(ld).ordtype<>scurrency) then
  1287. inserttypeconv(left,s64currencytype);
  1288. if (torddef(rd).ordtype<>scurrency) then
  1289. inserttypeconv(right,s64currencytype);
  1290. end
  1291. { leave some constant integer expressions alone in case the
  1292. resultdef of the integer types doesn't influence the outcome,
  1293. because the forced type conversions below can otherwise result
  1294. in unexpected results (such as high(qword)<high(int64) returning
  1295. true because high(qword) gets converted to int64) }
  1296. else if is_integer(ld) and is_integer(rd) and
  1297. (lt=ordconstn) and (rt=ordconstn) and
  1298. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) then
  1299. begin
  1300. end
  1301. { "and" does't care about the sign of integers }
  1302. { "xor", "or" and compares don't need extension to native int }
  1303. { size either as long as both values are signed or unsigned }
  1304. { "xor" and "or" also don't care about the sign if the values }
  1305. { occupy an entire register }
  1306. { don't do it if either type is 64 bit, since in that case we }
  1307. { can't safely find a "common" type }
  1308. else if is_integer(ld) and is_integer(rd) and
  1309. not is_64bitint(ld) and not is_64bitint(rd) and
  1310. ((nodetype=andn) or
  1311. ((nodetype in [orn,xorn,equaln,unequaln,gtn,gten,ltn,lten]) and
  1312. not(is_signed(ld) xor is_signed(rd)))) then
  1313. begin
  1314. if (rd.size>ld.size) or
  1315. { Delphi-compatible: prefer unsigned type for "and" with equal size }
  1316. ((rd.size=ld.size) and
  1317. not is_signed(rd)) then
  1318. begin
  1319. if (rd.size=ld.size) and
  1320. is_signed(ld) then
  1321. inserttypeconv_internal(left,rd)
  1322. else
  1323. begin
  1324. { not to left right.resultdef, because that may
  1325. cause a range error if left and right's def don't
  1326. completely overlap }
  1327. nd:=get_common_intdef(torddef(ld),torddef(rd),true);
  1328. inserttypeconv(left,nd);
  1329. inserttypeconv(right,nd);
  1330. end;
  1331. end
  1332. else
  1333. begin
  1334. if (rd.size=ld.size) and
  1335. is_signed(rd) then
  1336. inserttypeconv_internal(right,ld)
  1337. else
  1338. begin
  1339. nd:=get_common_intdef(torddef(ld),torddef(rd),true);
  1340. inserttypeconv(left,nd);
  1341. inserttypeconv(right,nd);
  1342. end;
  1343. end
  1344. end
  1345. { is there a signed 64 bit type ? }
  1346. else if ((torddef(rd).ordtype=s64bit) or (torddef(ld).ordtype=s64bit)) then
  1347. begin
  1348. if (torddef(ld).ordtype<>s64bit) then
  1349. inserttypeconv(left,s64inttype);
  1350. if (torddef(rd).ordtype<>s64bit) then
  1351. inserttypeconv(right,s64inttype);
  1352. end
  1353. { is there a unsigned 64 bit type ? }
  1354. else if ((torddef(rd).ordtype=u64bit) or (torddef(ld).ordtype=u64bit)) then
  1355. begin
  1356. if (torddef(ld).ordtype<>u64bit) then
  1357. inserttypeconv(left,u64inttype);
  1358. if (torddef(rd).ordtype<>u64bit) then
  1359. inserttypeconv(right,u64inttype);
  1360. end
  1361. { is there a larger int? }
  1362. else if is_oversizedint(rd) or is_oversizedint(ld) then
  1363. begin
  1364. nd:=get_common_intdef(torddef(ld),torddef(rd),false);
  1365. inserttypeconv(right,nd);
  1366. inserttypeconv(left,nd);
  1367. end
  1368. { is there a native unsigned int? }
  1369. else if is_nativeuint(rd) or is_nativeuint(ld) then
  1370. begin
  1371. { convert positive constants to uinttype }
  1372. if (not is_nativeuint(ld)) and
  1373. is_constintnode(left) and
  1374. (tordconstnode(left).value >= 0) then
  1375. inserttypeconv(left,uinttype);
  1376. if (not is_nativeuint(rd)) and
  1377. is_constintnode(right) and
  1378. (tordconstnode(right).value >= 0) then
  1379. inserttypeconv(right,uinttype);
  1380. { when one of the operand is signed or the operation is subn then perform
  1381. the operation in a larger signed type, can't use rd/ld here because there
  1382. could be already typeconvs inserted.
  1383. This is compatible with the code below for other unsigned types (PFV) }
  1384. if is_signed(left.resultdef) or
  1385. is_signed(right.resultdef) or
  1386. (nodetype=subn) then
  1387. begin
  1388. if nodetype<>subn then
  1389. CGMessage(type_h_mixed_signed_unsigned);
  1390. { mark as internal in case added for a subn, so }
  1391. { ttypeconvnode.simplify can remove the larger }
  1392. { typecast again if semantically correct. Even }
  1393. { if we could detect that here already, we }
  1394. { mustn't do it here because that would change }
  1395. { overload choosing behaviour etc. The code in }
  1396. { ncnv.pas is run after that is already decided }
  1397. if (not is_signed(left.resultdef) and
  1398. not is_signed(right.resultdef)) or
  1399. (nodetype in [orn,xorn]) then
  1400. include(flags,nf_internal);
  1401. { get next larger signed int type }
  1402. nd:=get_common_intdef(torddef(sinttype),torddef(uinttype),false);
  1403. inserttypeconv(left,nd);
  1404. inserttypeconv(right,nd);
  1405. end
  1406. else
  1407. begin
  1408. if not is_nativeuint(left.resultdef) then
  1409. inserttypeconv(left,uinttype);
  1410. if not is_nativeuint(right.resultdef) then
  1411. inserttypeconv(right,uinttype);
  1412. end;
  1413. end
  1414. { generic ord conversion is sinttype }
  1415. else
  1416. begin
  1417. { When there is a signed type or there is a minus operation
  1418. we convert to signed int. Otherwise (both are unsigned) we keep
  1419. the result also unsigned. This is compatible with Delphi (PFV) }
  1420. if is_signed(ld) or
  1421. is_signed(rd) or
  1422. (nodetype=subn) then
  1423. begin
  1424. inserttypeconv(right,sinttype);
  1425. inserttypeconv(left,sinttype);
  1426. end
  1427. else
  1428. begin
  1429. inserttypeconv(right,uinttype);
  1430. inserttypeconv(left,uinttype);
  1431. end;
  1432. end;
  1433. end
  1434. { if both are floatdefs, conversion is already done before constant folding }
  1435. else if (ld.typ=floatdef) then
  1436. begin
  1437. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  1438. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1439. end
  1440. { left side a setdef, must be before string processing,
  1441. else array constructor can be seen as array of char (PFV) }
  1442. else if (ld.typ=setdef) then
  1443. begin
  1444. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1445. CGMessage(type_e_set_operation_unknown);
  1446. { right must either be a set or a set element }
  1447. if (rd.typ<>setdef) and
  1448. (rt<>setelementn) then
  1449. CGMessage(type_e_mismatch)
  1450. { Make operands the same setdef. If one's elementtype fits }
  1451. { entirely inside the other's, pick the one with the largest }
  1452. { range. Otherwise create a new setdef with a range which }
  1453. { can contain both. }
  1454. else if not(equal_defs(ld,rd)) then
  1455. begin
  1456. { note: ld cannot be an empty set with elementdef=nil in }
  1457. { case right is not a set, arrayconstructor_to_set takes }
  1458. { care of that }
  1459. { 1: rd is a set with an assigned elementdef, and ld is }
  1460. { either an empty set without elementdef or a set whose }
  1461. { elementdef fits in rd's elementdef -> convert to rd }
  1462. if ((rd.typ=setdef) and
  1463. assigned(tsetdef(rd).elementdef) and
  1464. (not assigned(tsetdef(ld).elementdef) or
  1465. is_in_limit(ld,rd))) then
  1466. inserttypeconv(left,rd)
  1467. { 2: rd is either an empty set without elementdef or a set }
  1468. { whose elementdef fits in ld's elementdef, or a set }
  1469. { element whose def fits in ld's elementdef -> convert }
  1470. { to ld. ld's elementdef can't be nil here, is caught }
  1471. { previous case and "note:" above }
  1472. else if ((rd.typ=setdef) and
  1473. (not assigned(tsetdef(rd).elementdef) or
  1474. is_in_limit(rd,ld))) or
  1475. ((rd.typ<>setdef) and
  1476. is_in_limit(rd,tsetdef(ld).elementdef)) then
  1477. if (rd.typ=setdef) then
  1478. inserttypeconv(right,ld)
  1479. else
  1480. inserttypeconv(right,tsetdef(ld).elementdef)
  1481. { 3: otherwise create setdef which encompasses both, taking }
  1482. { into account empty sets without elementdef }
  1483. else
  1484. begin
  1485. if assigned(tsetdef(ld).elementdef) then
  1486. begin
  1487. llow:=tsetdef(ld).setbase;
  1488. lhigh:=tsetdef(ld).setmax;
  1489. end;
  1490. if (rd.typ=setdef) then
  1491. if assigned(tsetdef(rd).elementdef) then
  1492. begin
  1493. rlow:=tsetdef(rd).setbase;
  1494. rhigh:=tsetdef(rd).setmax;
  1495. end
  1496. else
  1497. begin
  1498. { ld's elementdef must have been valid }
  1499. rlow:=llow;
  1500. rhigh:=lhigh;
  1501. end
  1502. else
  1503. getrange(rd,rlow,rhigh);
  1504. if not assigned(tsetdef(ld).elementdef) then
  1505. begin
  1506. llow:=rlow;
  1507. lhigh:=rhigh;
  1508. end;
  1509. nd:=tsetdef.create(tsetdef(ld).elementdef,min(llow,rlow).svalue,max(lhigh,rhigh).svalue);
  1510. inserttypeconv(left,nd);
  1511. if (rd.typ=setdef) then
  1512. inserttypeconv(right,nd)
  1513. else
  1514. inserttypeconv(right,tsetdef(nd).elementdef);
  1515. end;
  1516. end;
  1517. end
  1518. { pointer comparision and subtraction }
  1519. else if (
  1520. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  1521. ) or
  1522. { compare/add pchar to variable (not stringconst) char arrays
  1523. by addresses like BP/Delphi }
  1524. (
  1525. (nodetype in [equaln,unequaln,subn,addn]) and
  1526. (
  1527. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1528. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1529. )
  1530. ) then
  1531. begin
  1532. { convert char array to pointer }
  1533. if is_chararray(rd) then
  1534. begin
  1535. inserttypeconv(right,charpointertype);
  1536. rd:=right.resultdef;
  1537. end
  1538. else if is_chararray(ld) then
  1539. begin
  1540. inserttypeconv(left,charpointertype);
  1541. ld:=left.resultdef;
  1542. end;
  1543. case nodetype of
  1544. equaln,unequaln :
  1545. begin
  1546. if is_voidpointer(right.resultdef) then
  1547. inserttypeconv(right,left.resultdef)
  1548. else if is_voidpointer(left.resultdef) then
  1549. inserttypeconv(left,right.resultdef)
  1550. else if not(equal_defs(ld,rd)) then
  1551. IncompatibleTypes(ld,rd);
  1552. { now that the type checking is done, convert both to charpointer, }
  1553. { because methodpointers are 8 bytes even though only the first 4 }
  1554. { bytes must be compared. This can happen here if we are in }
  1555. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1556. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1557. { optimized away, since the result already was a voidpointer, so }
  1558. { use a charpointer instead (JM) }
  1559. {$if defined(jvm)}
  1560. inserttypeconv_internal(left,java_jlobject);
  1561. inserttypeconv_internal(right,java_jlobject);
  1562. {$elseif defined(i8086)}
  1563. { we don't have a charfarpointertype yet, so for far pointers we use bytefarpointertype }
  1564. if is_farpointer(left.resultdef) then
  1565. inserttypeconv_internal(left,bytefarpointertype)
  1566. else
  1567. inserttypeconv_internal(left,charpointertype);
  1568. if is_farpointer(right.resultdef) then
  1569. inserttypeconv_internal(right,bytefarpointertype)
  1570. else
  1571. inserttypeconv_internal(right,charpointertype);
  1572. {$else}
  1573. inserttypeconv_internal(left,charpointertype);
  1574. inserttypeconv_internal(right,charpointertype);
  1575. {$endif jvm}
  1576. end;
  1577. ltn,lten,gtn,gten:
  1578. begin
  1579. if (cs_extsyntax in current_settings.moduleswitches) or
  1580. (nf_internal in flags) then
  1581. begin
  1582. if is_voidpointer(right.resultdef) then
  1583. inserttypeconv(right,left.resultdef)
  1584. else if is_voidpointer(left.resultdef) then
  1585. inserttypeconv(left,right.resultdef)
  1586. else if not(equal_defs(ld,rd)) then
  1587. IncompatibleTypes(ld,rd);
  1588. end
  1589. else
  1590. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1591. end;
  1592. subn:
  1593. begin
  1594. if (cs_extsyntax in current_settings.moduleswitches) then
  1595. begin
  1596. if is_voidpointer(right.resultdef) then
  1597. begin
  1598. if is_big_untyped_addrnode(right) then
  1599. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1600. inserttypeconv(right,left.resultdef)
  1601. end
  1602. else if is_voidpointer(left.resultdef) then
  1603. inserttypeconv(left,right.resultdef)
  1604. else if not(equal_defs(ld,rd)) then
  1605. IncompatibleTypes(ld,rd);
  1606. end
  1607. else
  1608. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1609. if not(nf_has_pointerdiv in flags) and
  1610. (tpointerdef(rd).pointeddef.size>1) then
  1611. begin
  1612. hp:=getcopy;
  1613. include(hp.flags,nf_has_pointerdiv);
  1614. result:=cmoddivnode.create(divn,hp,cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,false));
  1615. end;
  1616. resultdef:=sinttype;
  1617. exit;
  1618. end;
  1619. else
  1620. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1621. end;
  1622. end
  1623. { is one of the operands a string?,
  1624. chararrays are also handled as strings (after conversion), also take
  1625. care of chararray+chararray and chararray+char.
  1626. Note: Must be done after pointerdef+pointerdef has been checked, else
  1627. pchar is converted to string }
  1628. else if (rd.typ=stringdef) or
  1629. (ld.typ=stringdef) or
  1630. { stringconstn's can be arraydefs }
  1631. (lt=stringconstn) or
  1632. (rt=stringconstn) or
  1633. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1634. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1635. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1636. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1637. begin
  1638. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1639. begin
  1640. { Is there a unicodestring? }
  1641. if is_unicodestring(rd) or is_unicodestring(ld) or
  1642. ((m_default_unicodestring in current_settings.modeswitches) and
  1643. (cs_refcountedstrings in current_settings.localswitches) and
  1644. (
  1645. is_pwidechar(rd) or is_widechararray(rd) or is_open_widechararray(rd) or (lt = stringconstn) or
  1646. is_pwidechar(ld) or is_widechararray(ld) or is_open_widechararray(ld) or (rt = stringconstn)
  1647. )
  1648. ) then
  1649. strtype:=st_unicodestring
  1650. else
  1651. { Is there a widestring? }
  1652. if is_widestring(rd) or is_widestring(ld) or
  1653. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1654. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1655. strtype:=st_widestring
  1656. else
  1657. if is_ansistring(rd) or is_ansistring(ld) or
  1658. ((cs_refcountedstrings in current_settings.localswitches) and
  1659. //todo: Move some of this to longstring's then they are implemented?
  1660. (
  1661. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or (lt = stringconstn) or
  1662. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld) or (rt = stringconstn)
  1663. )
  1664. ) then
  1665. strtype:=st_ansistring
  1666. else
  1667. if is_longstring(rd) or is_longstring(ld) then
  1668. strtype:=st_longstring
  1669. else
  1670. begin
  1671. { TODO: todo: add a warning/hint here if one converting a too large array}
  1672. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1673. Note: Delphi halts with error if "array [0..xx] of char"
  1674. is assigned to ShortString and string length is less
  1675. then array size }
  1676. strtype:= st_shortstring;
  1677. end;
  1678. // Now convert nodes to common string type
  1679. case strtype of
  1680. st_widestring :
  1681. begin
  1682. if not(is_widestring(rd)) then
  1683. inserttypeconv(right,cwidestringtype);
  1684. if not(is_widestring(ld)) then
  1685. inserttypeconv(left,cwidestringtype);
  1686. end;
  1687. st_unicodestring :
  1688. begin
  1689. if not(is_unicodestring(rd)) then
  1690. inserttypeconv(right,cunicodestringtype);
  1691. if not(is_unicodestring(ld)) then
  1692. inserttypeconv(left,cunicodestringtype);
  1693. end;
  1694. st_ansistring :
  1695. begin
  1696. { use same code page if possible (don't force same code
  1697. page in case both are ansistrings with code page <>
  1698. CP_NONE, since then data loss can occur (the ansistring
  1699. helpers will convert them at run time to an encoding
  1700. that can represent both encodings) }
  1701. if is_ansistring(ld) and
  1702. (tstringdef(ld).encoding<>0) and
  1703. (tstringdef(ld).encoding<>globals.CP_NONE) and
  1704. (not is_ansistring(rd) or
  1705. (tstringdef(rd).encoding=0) or
  1706. (tstringdef(rd).encoding=globals.CP_NONE)) then
  1707. inserttypeconv(right,ld)
  1708. else if is_ansistring(rd) and
  1709. (tstringdef(rd).encoding<>0) and
  1710. (tstringdef(rd).encoding<>globals.CP_NONE) and
  1711. (not is_ansistring(ld) or
  1712. (tstringdef(ld).encoding=0) or
  1713. (tstringdef(ld).encoding=globals.CP_NONE)) then
  1714. inserttypeconv(left,rd)
  1715. else
  1716. begin
  1717. if not is_ansistring(ld) then
  1718. inserttypeconv(left,getansistringdef);
  1719. if not is_ansistring(rd) then
  1720. inserttypeconv(right,getansistringdef);
  1721. end;
  1722. end;
  1723. st_longstring :
  1724. begin
  1725. if not(is_longstring(rd)) then
  1726. inserttypeconv(right,clongstringtype);
  1727. if not(is_longstring(ld)) then
  1728. inserttypeconv(left,clongstringtype);
  1729. end;
  1730. st_shortstring :
  1731. begin
  1732. if not(is_shortstring(ld)) then
  1733. inserttypeconv(left,cshortstringtype);
  1734. { don't convert char, that can be handled by the optimized node }
  1735. if not(is_shortstring(rd) or is_char(rd)) then
  1736. inserttypeconv(right,cshortstringtype);
  1737. end;
  1738. else
  1739. internalerror(2005101);
  1740. end;
  1741. end
  1742. else
  1743. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1744. end
  1745. { implicit pointer object type comparison }
  1746. else if is_implicit_pointer_object_type(rd) or is_implicit_pointer_object_type(ld) then
  1747. begin
  1748. if (nodetype in [equaln,unequaln]) then
  1749. begin
  1750. if is_implicit_pointer_object_type(rd) and is_implicit_pointer_object_type(ld) then
  1751. begin
  1752. if def_is_related(tobjectdef(rd),tobjectdef(ld)) then
  1753. inserttypeconv(right,left.resultdef)
  1754. else
  1755. inserttypeconv(left,right.resultdef);
  1756. end
  1757. else if is_implicit_pointer_object_type(rd) then
  1758. inserttypeconv(left,right.resultdef)
  1759. else
  1760. inserttypeconv(right,left.resultdef);
  1761. end
  1762. else
  1763. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1764. end
  1765. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  1766. begin
  1767. if (nodetype in [equaln,unequaln]) then
  1768. begin
  1769. if def_is_related(tobjectdef(tclassrefdef(rd).pointeddef),
  1770. tobjectdef(tclassrefdef(ld).pointeddef)) then
  1771. inserttypeconv(right,left.resultdef)
  1772. else
  1773. inserttypeconv(left,right.resultdef);
  1774. end
  1775. else
  1776. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1777. end
  1778. { allow comparison with nil pointer }
  1779. else if is_implicit_pointer_object_type(rd) or (rd.typ=classrefdef) then
  1780. begin
  1781. if (nodetype in [equaln,unequaln]) then
  1782. inserttypeconv(left,right.resultdef)
  1783. else
  1784. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1785. end
  1786. else if is_implicit_pointer_object_type(ld) or (ld.typ=classrefdef) then
  1787. begin
  1788. if (nodetype in [equaln,unequaln]) then
  1789. inserttypeconv(right,left.resultdef)
  1790. else
  1791. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1792. end
  1793. { support procvar=nil,procvar<>nil }
  1794. else if ((ld.typ=procvardef) and (rt=niln)) or
  1795. ((rd.typ=procvardef) and (lt=niln)) then
  1796. begin
  1797. if not(nodetype in [equaln,unequaln]) then
  1798. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1799. { find proc field in methodpointer record }
  1800. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1801. if not assigned(hsym) then
  1802. internalerror(200412043);
  1803. { For methodpointers compare only tmethodpointer.proc }
  1804. if (rd.typ=procvardef) and
  1805. (not tprocvardef(rd).is_addressonly) then
  1806. begin
  1807. right:=csubscriptnode.create(
  1808. hsym,
  1809. ctypeconvnode.create_internal(right,methodpointertype));
  1810. typecheckpass(right);
  1811. end;
  1812. if (ld.typ=procvardef) and
  1813. (not tprocvardef(ld).is_addressonly) then
  1814. begin
  1815. left:=csubscriptnode.create(
  1816. hsym,
  1817. ctypeconvnode.create_internal(left,methodpointertype));
  1818. typecheckpass(left);
  1819. end;
  1820. end
  1821. { support dynamicarray=nil,dynamicarray<>nil }
  1822. else if (is_dynamic_array(ld) and (rt=niln)) or
  1823. (is_dynamic_array(rd) and (lt=niln)) or
  1824. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1825. begin
  1826. if not(nodetype in [equaln,unequaln]) then
  1827. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1828. end
  1829. {$ifdef SUPPORT_MMX}
  1830. { mmx support, this must be before the zero based array
  1831. check }
  1832. else if (cs_mmx in current_settings.localswitches) and
  1833. is_mmx_able_array(ld) and
  1834. is_mmx_able_array(rd) and
  1835. equal_defs(ld,rd) then
  1836. begin
  1837. case nodetype of
  1838. addn,subn,xorn,orn,andn:
  1839. ;
  1840. { mul is a little bit restricted }
  1841. muln:
  1842. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1843. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1844. else
  1845. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1846. end;
  1847. end
  1848. {$endif SUPPORT_MMX}
  1849. { vector support, this must be before the zero based array
  1850. check }
  1851. else if (cs_support_vectors in current_settings.globalswitches) and
  1852. is_vector(ld) and
  1853. is_vector(rd) and
  1854. equal_defs(ld,rd) then
  1855. begin
  1856. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  1857. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1858. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  1859. resultdef:=left.resultdef;
  1860. end
  1861. { this is a little bit dangerous, also the left type }
  1862. { pointer to should be checked! This broke the mmx support }
  1863. else if (rd.typ=pointerdef) or
  1864. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  1865. begin
  1866. if is_zero_based_array(rd) then
  1867. begin
  1868. resultdef:=getpointerdef(tarraydef(rd).elementdef);
  1869. inserttypeconv(right,resultdef);
  1870. end
  1871. else
  1872. resultdef:=right.resultdef;
  1873. inserttypeconv(left,sinttype);
  1874. if nodetype=addn then
  1875. begin
  1876. if not(cs_extsyntax in current_settings.moduleswitches) or
  1877. (not (is_pchar(ld) or is_chararray(ld) or is_open_chararray(ld) or is_widechar(ld) or is_widechararray(ld) or is_open_widechararray(ld)) and
  1878. not(cs_pointermath in current_settings.localswitches) and
  1879. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  1880. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1881. if (rd.typ=pointerdef) and
  1882. (tpointerdef(rd).pointeddef.size>1) then
  1883. begin
  1884. left:=caddnode.create(muln,left,
  1885. cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,true));
  1886. typecheckpass(left);
  1887. end;
  1888. end
  1889. else
  1890. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1891. end
  1892. else if (ld.typ=pointerdef) or
  1893. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  1894. begin
  1895. if is_zero_based_array(ld) then
  1896. begin
  1897. resultdef:=getpointerdef(tarraydef(ld).elementdef);
  1898. inserttypeconv(left,resultdef);
  1899. end
  1900. else
  1901. resultdef:=left.resultdef;
  1902. inserttypeconv(right,sinttype);
  1903. if nodetype in [addn,subn] then
  1904. begin
  1905. if (lt=niln) then
  1906. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),'NIL',rd.typename);
  1907. if not(cs_extsyntax in current_settings.moduleswitches) or
  1908. (not (is_pchar(ld) or is_chararray(ld) or is_open_chararray(ld) or is_widechar(ld) or is_widechararray(ld) or is_open_widechararray(ld)) and
  1909. not(cs_pointermath in current_settings.localswitches) and
  1910. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  1911. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1912. if (ld.typ=pointerdef) then
  1913. begin
  1914. if is_big_untyped_addrnode(left) then
  1915. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1916. if (tpointerdef(ld).pointeddef.size>1) then
  1917. begin
  1918. right:=caddnode.create(muln,right,
  1919. cordconstnode.create(tpointerdef(ld).pointeddef.size,sinttype,true));
  1920. typecheckpass(right);
  1921. end
  1922. end else
  1923. if is_zero_based_array(ld) and
  1924. (tarraydef(ld).elementdef.size>1) then
  1925. begin
  1926. right:=caddnode.create(muln,right,
  1927. cordconstnode.create(tarraydef(ld).elementdef.size,sinttype,true));
  1928. typecheckpass(right);
  1929. end;
  1930. end
  1931. else
  1932. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1933. end
  1934. else if (rd.typ=procvardef) and
  1935. (ld.typ=procvardef) and
  1936. equal_defs(rd,ld) then
  1937. begin
  1938. if (nodetype in [equaln,unequaln]) then
  1939. begin
  1940. if tprocvardef(rd).is_addressonly then
  1941. begin
  1942. inserttypeconv_internal(right,voidpointertype);
  1943. inserttypeconv_internal(left,voidpointertype);
  1944. end
  1945. else
  1946. begin
  1947. { find proc field in methodpointer record }
  1948. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1949. if not assigned(hsym) then
  1950. internalerror(200412043);
  1951. { Compare tmehodpointer(left).proc }
  1952. right:=csubscriptnode.create(
  1953. hsym,
  1954. ctypeconvnode.create_internal(right,methodpointertype));
  1955. typecheckpass(right);
  1956. left:=csubscriptnode.create(
  1957. hsym,
  1958. ctypeconvnode.create_internal(left,methodpointertype));
  1959. typecheckpass(left);
  1960. end;
  1961. end
  1962. else
  1963. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1964. end
  1965. { enums }
  1966. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  1967. begin
  1968. if allowenumop(nodetype) then
  1969. inserttypeconv(right,left.resultdef)
  1970. else
  1971. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1972. end
  1973. { generic conversion, this is for error recovery }
  1974. else
  1975. begin
  1976. inserttypeconv(left,sinttype);
  1977. inserttypeconv(right,sinttype);
  1978. end;
  1979. if cmp_of_disjunct_ranges(res) then
  1980. begin
  1981. if res then
  1982. CGMessage(type_w_comparison_always_true)
  1983. else
  1984. CGMessage(type_w_comparison_always_false);
  1985. end;
  1986. { set resultdef if not already done }
  1987. if not assigned(resultdef) then
  1988. begin
  1989. case nodetype of
  1990. ltn,lten,gtn,gten,equaln,unequaln :
  1991. resultdef:=pasbool8type;
  1992. slashn :
  1993. resultdef:=resultrealdef;
  1994. addn:
  1995. begin
  1996. { for strings, return is always a 255 char string }
  1997. if is_shortstring(left.resultdef) then
  1998. resultdef:=cshortstringtype
  1999. else
  2000. { for ansistrings set resultdef to assignment left node
  2001. if it is an assignment and left node expects ansistring }
  2002. if is_ansistring(left.resultdef) and
  2003. assigned(aktassignmentnode) and
  2004. (aktassignmentnode.right=self) and
  2005. is_ansistring(aktassignmentnode.left.resultdef) then
  2006. resultdef:=aktassignmentnode.left.resultdef
  2007. else
  2008. resultdef:=left.resultdef;
  2009. end;
  2010. else
  2011. resultdef:=left.resultdef;
  2012. end;
  2013. end;
  2014. { when the result is currency we need some extra code for
  2015. multiplication and division. this should not be done when
  2016. the muln or slashn node is created internally }
  2017. if not(nf_is_currency in flags) and
  2018. is_currency(resultdef) then
  2019. begin
  2020. case nodetype of
  2021. slashn :
  2022. begin
  2023. { slashn will only work with floats }
  2024. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  2025. include(hp.flags,nf_is_currency);
  2026. result:=hp;
  2027. end;
  2028. muln :
  2029. begin
  2030. if s64currencytype.typ=floatdef then
  2031. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  2032. else
  2033. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  2034. include(hp.flags,nf_is_currency);
  2035. result:=hp
  2036. end;
  2037. end;
  2038. end;
  2039. if not codegenerror and
  2040. not assigned(result) then
  2041. result:=simplify(false);
  2042. end;
  2043. function taddnode.first_addstring: tnode;
  2044. const
  2045. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  2046. var
  2047. p: tnode;
  2048. newstatement : tstatementnode;
  2049. tempnode (*,tempnode2*) : ttempcreatenode;
  2050. cmpfuncname: string;
  2051. para: tcallparanode;
  2052. begin
  2053. { when we get here, we are sure that both the left and the right }
  2054. { node are both strings of the same stringtype (JM) }
  2055. case nodetype of
  2056. addn:
  2057. begin
  2058. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  2059. begin
  2060. result:=right;
  2061. left.free;
  2062. left:=nil;
  2063. right:=nil;
  2064. exit;
  2065. end;
  2066. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  2067. begin
  2068. result:=left;
  2069. left:=nil;
  2070. right.free;
  2071. right:=nil;
  2072. exit;
  2073. end;
  2074. { create the call to the concat routine both strings as arguments }
  2075. if assigned(aktassignmentnode) and
  2076. (aktassignmentnode.right=self) and
  2077. (aktassignmentnode.left.resultdef=resultdef) and
  2078. valid_for_var(aktassignmentnode.left,false) then
  2079. begin
  2080. para:=ccallparanode.create(
  2081. right,
  2082. ccallparanode.create(
  2083. left,
  2084. ccallparanode.create(aktassignmentnode.left.getcopy,nil)
  2085. )
  2086. );
  2087. if is_ansistring(resultdef) then
  2088. para:=ccallparanode.create(
  2089. cordconstnode.create(
  2090. { don't use getparaencoding(), we have to know
  2091. when the result is rawbytestring }
  2092. tstringdef(resultdef).encoding,
  2093. u16inttype,
  2094. true
  2095. ),
  2096. para
  2097. );
  2098. result:=ccallnode.createintern(
  2099. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2100. para
  2101. );
  2102. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2103. firstpass(result);
  2104. end
  2105. else
  2106. begin
  2107. result:=internalstatements(newstatement);
  2108. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2109. addstatement(newstatement,tempnode);
  2110. { initialize the temp, since it will be passed to a
  2111. var-parameter (and finalization, which is performed by the
  2112. ttempcreate node and which takes care of the initialization
  2113. on native targets, is a noop on managed VM targets) }
  2114. if (target_info.system in systems_managed_vm) and
  2115. is_managed_type(resultdef) then
  2116. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  2117. false,
  2118. ccallparanode.create(genintconstnode(0),
  2119. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  2120. para:=ccallparanode.create(
  2121. right,
  2122. ccallparanode.create(
  2123. left,
  2124. ccallparanode.create(ctemprefnode.create(tempnode),nil)
  2125. )
  2126. );
  2127. if is_ansistring(resultdef) then
  2128. para:=ccallparanode.create(
  2129. cordconstnode.create(
  2130. { don't use getparaencoding(), we have to know
  2131. when the result is rawbytestring }
  2132. tstringdef(resultdef).encoding,
  2133. u16inttype,
  2134. true
  2135. ),
  2136. para
  2137. );
  2138. addstatement(
  2139. newstatement,
  2140. ccallnode.createintern(
  2141. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2142. para
  2143. )
  2144. );
  2145. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2146. addstatement(newstatement,ctemprefnode.create(tempnode));
  2147. end;
  2148. { we reused the arguments }
  2149. left := nil;
  2150. right := nil;
  2151. end;
  2152. ltn,lten,gtn,gten,equaln,unequaln :
  2153. begin
  2154. { generate better code for comparison with empty string, we
  2155. only need to compare the length with 0 }
  2156. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  2157. { windows widestrings are too complicated to be handled optimized }
  2158. not(is_widestring(left.resultdef) and (target_info.system in systems_windows)) and
  2159. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  2160. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  2161. begin
  2162. { switch so that the constant is always on the right }
  2163. if left.nodetype = stringconstn then
  2164. begin
  2165. p := left;
  2166. left := right;
  2167. right := p;
  2168. nodetype:=swap_relation[nodetype];
  2169. end;
  2170. if is_shortstring(left.resultdef) or
  2171. (nodetype in [gtn,gten,ltn,lten]) or
  2172. (target_info.system in systems_managed_vm) then
  2173. { compare the length with 0 }
  2174. result := caddnode.create(nodetype,
  2175. cinlinenode.create(in_length_x,false,left),
  2176. cordconstnode.create(0,s32inttype,false))
  2177. else
  2178. begin
  2179. (*
  2180. if is_widestring(left.resultdef) and
  2181. (target_info.system in system_windows) then
  2182. begin
  2183. { windows like widestrings requires that we also check the length }
  2184. result:=internalstatements(newstatement);
  2185. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2186. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2187. addstatement(newstatement,tempnode);
  2188. addstatement(newstatement,tempnode2);
  2189. { poor man's cse }
  2190. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2191. ctypeconvnode.create_internal(left,voidpointertype))
  2192. );
  2193. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  2194. caddnode.create(orn,
  2195. caddnode.create(nodetype,
  2196. ctemprefnode.create(tempnode),
  2197. cpointerconstnode.create(0,voidpointertype)
  2198. ),
  2199. caddnode.create(nodetype,
  2200. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  2201. cordconstnode.create(0,s32inttype,false)
  2202. )
  2203. )
  2204. ));
  2205. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2206. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  2207. addstatement(newstatement,ctemprefnode.create(tempnode2));
  2208. end
  2209. else
  2210. *)
  2211. begin
  2212. { compare the pointer with nil (for ansistrings etc), }
  2213. { faster than getting the length (JM) }
  2214. result:= caddnode.create(nodetype,
  2215. ctypeconvnode.create_internal(left,voidpointertype),
  2216. cpointerconstnode.create(0,voidpointertype));
  2217. end;
  2218. end;
  2219. { left is reused }
  2220. left := nil;
  2221. { right isn't }
  2222. right.free;
  2223. right := nil;
  2224. exit;
  2225. end;
  2226. { no string constant -> call compare routine }
  2227. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  2228. { for equality checks use optimized version }
  2229. if nodetype in [equaln,unequaln] then
  2230. cmpfuncname := cmpfuncname + '_equal';
  2231. result := ccallnode.createintern(cmpfuncname,
  2232. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2233. { and compare its result with 0 according to the original operator }
  2234. result := caddnode.create(nodetype,result,
  2235. cordconstnode.create(0,s32inttype,false));
  2236. left := nil;
  2237. right := nil;
  2238. end;
  2239. end;
  2240. end;
  2241. function taddnode.first_addset : tnode;
  2242. procedure call_varset_helper(const n : string);
  2243. var
  2244. newstatement : tstatementnode;
  2245. temp : ttempcreatenode;
  2246. begin
  2247. { add two var sets }
  2248. result:=internalstatements(newstatement);
  2249. { create temp for result }
  2250. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2251. addstatement(newstatement,temp);
  2252. addstatement(newstatement,ccallnode.createintern(n,
  2253. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2254. ccallparanode.create(ctemprefnode.create(temp),
  2255. ccallparanode.create(right,
  2256. ccallparanode.create(left,nil)))))
  2257. );
  2258. { remove reused parts from original node }
  2259. left:=nil;
  2260. right:=nil;
  2261. { the last statement should return the value as
  2262. location and type, this is done be referencing the
  2263. temp and converting it first from a persistent temp to
  2264. normal temp }
  2265. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2266. addstatement(newstatement,ctemprefnode.create(temp));
  2267. end;
  2268. var
  2269. procname: string[31];
  2270. tempn: tnode;
  2271. newstatement : tstatementnode;
  2272. temp : ttempcreatenode;
  2273. begin
  2274. result:=nil;
  2275. case nodetype of
  2276. equaln,unequaln,lten,gten:
  2277. begin
  2278. case nodetype of
  2279. equaln,unequaln:
  2280. procname := 'fpc_varset_comp_sets';
  2281. lten,gten:
  2282. begin
  2283. procname := 'fpc_varset_contains_sets';
  2284. { (left >= right) = (right <= left) }
  2285. if nodetype = gten then
  2286. begin
  2287. tempn := left;
  2288. left := right;
  2289. right := tempn;
  2290. end;
  2291. end;
  2292. end;
  2293. result := ccallnode.createinternres(procname,
  2294. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2295. ccallparanode.create(right,
  2296. ccallparanode.create(left,nil))),resultdef);
  2297. { left and right are reused as parameters }
  2298. left := nil;
  2299. right := nil;
  2300. { for an unequaln, we have to negate the result of comp_sets }
  2301. if nodetype = unequaln then
  2302. result := cnotnode.create(result);
  2303. end;
  2304. addn:
  2305. begin
  2306. { optimize first loading of a set }
  2307. if (right.nodetype=setelementn) and
  2308. not(assigned(tsetelementnode(right).right)) and
  2309. is_emptyset(left) then
  2310. begin
  2311. result:=internalstatements(newstatement);
  2312. { create temp for result }
  2313. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2314. addstatement(newstatement,temp);
  2315. { adjust for set base }
  2316. tsetelementnode(right).left:=caddnode.create(subn,
  2317. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2318. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2319. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  2320. ccallparanode.create(ctemprefnode.create(temp),
  2321. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2322. ccallparanode.create(tsetelementnode(right).left,nil))))
  2323. );
  2324. { the last statement should return the value as
  2325. location and type, this is done be referencing the
  2326. temp and converting it first from a persistent temp to
  2327. normal temp }
  2328. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2329. addstatement(newstatement,ctemprefnode.create(temp));
  2330. tsetelementnode(right).left := nil;
  2331. end
  2332. else
  2333. begin
  2334. if right.nodetype=setelementn then
  2335. begin
  2336. result:=internalstatements(newstatement);
  2337. { create temp for result }
  2338. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2339. addstatement(newstatement,temp);
  2340. { adjust for set base }
  2341. tsetelementnode(right).left:=caddnode.create(subn,
  2342. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2343. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2344. { add a range or a single element? }
  2345. if assigned(tsetelementnode(right).right) then
  2346. begin
  2347. { adjust for set base }
  2348. tsetelementnode(right).right:=caddnode.create(subn,
  2349. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  2350. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2351. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  2352. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2353. ccallparanode.create(tsetelementnode(right).right,
  2354. ccallparanode.create(tsetelementnode(right).left,
  2355. ccallparanode.create(ctemprefnode.create(temp),
  2356. ccallparanode.create(left,nil))))))
  2357. );
  2358. end
  2359. else
  2360. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  2361. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2362. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2363. ccallparanode.create(ctemprefnode.create(temp),
  2364. ccallparanode.create(left,nil)))))
  2365. );
  2366. { remove reused parts from original node }
  2367. tsetelementnode(right).right:=nil;
  2368. tsetelementnode(right).left:=nil;
  2369. left:=nil;
  2370. { the last statement should return the value as
  2371. location and type, this is done be referencing the
  2372. temp and converting it first from a persistent temp to
  2373. normal temp }
  2374. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2375. addstatement(newstatement,ctemprefnode.create(temp));
  2376. end
  2377. else
  2378. call_varset_helper('fpc_varset_add_sets');
  2379. end
  2380. end;
  2381. subn:
  2382. call_varset_helper('fpc_varset_sub_sets');
  2383. symdifn:
  2384. call_varset_helper('fpc_varset_symdif_sets');
  2385. muln:
  2386. call_varset_helper('fpc_varset_mul_sets');
  2387. else
  2388. internalerror(200609241);
  2389. end;
  2390. end;
  2391. function taddnode.use_generic_mul32to64: boolean;
  2392. begin
  2393. result := true;
  2394. end;
  2395. function taddnode.try_make_mul32to64: boolean;
  2396. function canbe32bitint(v: tconstexprint): boolean;
  2397. begin
  2398. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  2399. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))))
  2400. end;
  2401. var
  2402. temp: tnode;
  2403. begin
  2404. result := false;
  2405. if ((left.nodetype = typeconvn) and
  2406. is_integer(ttypeconvnode(left).left.resultdef) and
  2407. (not(torddef(ttypeconvnode(left).left.resultdef).ordtype in [u64bit,s64bit,scurrency])) and
  2408. (((right.nodetype = ordconstn) and canbe32bitint(tordconstnode(right).value)) or
  2409. ((right.nodetype = typeconvn) and
  2410. is_integer(ttypeconvnode(right).left.resultdef) and
  2411. not(torddef(ttypeconvnode(right).left.resultdef).ordtype in [u64bit,s64bit,scurrency])) and
  2412. ((is_signed(ttypeconvnode(left).left.resultdef) =
  2413. is_signed(ttypeconvnode(right).left.resultdef)) or
  2414. (is_signed(ttypeconvnode(left).left.resultdef) and
  2415. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit]))))) then
  2416. begin
  2417. temp := ttypeconvnode(left).left;
  2418. ttypeconvnode(left).left := nil;
  2419. left.free;
  2420. left := temp;
  2421. if (right.nodetype = typeconvn) then
  2422. begin
  2423. temp := ttypeconvnode(right).left;
  2424. ttypeconvnode(right).left := nil;
  2425. right.free;
  2426. right := temp;
  2427. end;
  2428. if (is_signed(left.resultdef)) then
  2429. begin
  2430. inserttypeconv_internal(left,s32inttype);
  2431. inserttypeconv_internal(right,s32inttype);
  2432. end
  2433. else
  2434. begin
  2435. inserttypeconv_internal(left,u32inttype);
  2436. inserttypeconv_internal(right,u32inttype);
  2437. end;
  2438. firstpass(left);
  2439. firstpass(right);
  2440. result := true;
  2441. end;
  2442. end;
  2443. function taddnode.first_add64bitint: tnode;
  2444. var
  2445. procname: string[31];
  2446. temp: tnode;
  2447. power: longint;
  2448. begin
  2449. result := nil;
  2450. { create helper calls mul }
  2451. if nodetype <> muln then
  2452. exit;
  2453. { make sure that if there is a constant, that it's on the right }
  2454. if left.nodetype = ordconstn then
  2455. begin
  2456. temp := right;
  2457. right := left;
  2458. left := temp;
  2459. end;
  2460. { can we use a shift instead of a mul? }
  2461. if not (cs_check_overflow in current_settings.localswitches) and
  2462. (right.nodetype = ordconstn) and
  2463. ispowerof2(tordconstnode(right).value,power) then
  2464. begin
  2465. tordconstnode(right).value := power;
  2466. result := cshlshrnode.create(shln,left,right);
  2467. { left and right are reused }
  2468. left := nil;
  2469. right := nil;
  2470. { return firstpassed new node }
  2471. exit;
  2472. end;
  2473. if not(use_generic_mul32to64) and
  2474. try_make_mul32to64 then
  2475. exit;
  2476. { when currency is used set the result of the
  2477. parameters to s64bit, so they are not converted }
  2478. if is_currency(resultdef) then
  2479. begin
  2480. left.resultdef:=s64inttype;
  2481. right.resultdef:=s64inttype;
  2482. end;
  2483. { otherwise, create the parameters for the helper }
  2484. right := ccallparanode.create(
  2485. cordconstnode.create(ord(cs_check_overflow in current_settings.localswitches),pasbool8type,true),
  2486. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2487. left := nil;
  2488. { only qword needs the unsigned code, the
  2489. signed code is also used for currency }
  2490. if is_signed(resultdef) then
  2491. procname := 'fpc_mul_int64'
  2492. else
  2493. procname := 'fpc_mul_qword';
  2494. result := ccallnode.createintern(procname,right);
  2495. right := nil;
  2496. end;
  2497. function taddnode.first_addfloat : tnode;
  2498. var
  2499. procname: string[31];
  2500. { do we need to reverse the result ? }
  2501. notnode : boolean;
  2502. fdef : tdef;
  2503. begin
  2504. result := nil;
  2505. notnode := false;
  2506. { In non-emulation mode, real opcodes are
  2507. emitted for floating point values.
  2508. }
  2509. if not ((cs_fp_emulation in current_settings.moduleswitches)
  2510. {$ifdef cpufpemu}
  2511. or (current_settings.fputype=fpu_soft)
  2512. {$endif cpufpemu}
  2513. ) then
  2514. exit;
  2515. if not(target_info.system in systems_wince) then
  2516. begin
  2517. case tfloatdef(left.resultdef).floattype of
  2518. s32real:
  2519. begin
  2520. fdef:=search_system_type('FLOAT32REC').typedef;
  2521. procname:='float32';
  2522. end;
  2523. s64real:
  2524. begin
  2525. fdef:=search_system_type('FLOAT64').typedef;
  2526. procname:='float64';
  2527. end;
  2528. {!!! not yet implemented
  2529. s128real:
  2530. }
  2531. else
  2532. internalerror(2005082601);
  2533. end;
  2534. case nodetype of
  2535. addn:
  2536. procname:=procname+'_add';
  2537. muln:
  2538. procname:=procname+'_mul';
  2539. subn:
  2540. procname:=procname+'_sub';
  2541. slashn:
  2542. procname:=procname+'_div';
  2543. ltn:
  2544. procname:=procname+'_lt';
  2545. lten:
  2546. procname:=procname+'_le';
  2547. gtn:
  2548. begin
  2549. procname:=procname+'_le';
  2550. notnode:=true;
  2551. end;
  2552. gten:
  2553. begin
  2554. procname:=procname+'_lt';
  2555. notnode:=true;
  2556. end;
  2557. equaln:
  2558. procname:=procname+'_eq';
  2559. unequaln:
  2560. begin
  2561. procname:=procname+'_eq';
  2562. notnode:=true;
  2563. end;
  2564. else
  2565. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2566. end;
  2567. end
  2568. else
  2569. begin
  2570. case nodetype of
  2571. addn:
  2572. procname:='ADD';
  2573. muln:
  2574. procname:='MUL';
  2575. subn:
  2576. procname:='SUB';
  2577. slashn:
  2578. procname:='DIV';
  2579. ltn:
  2580. procname:='LT';
  2581. lten:
  2582. procname:='LE';
  2583. gtn:
  2584. procname:='GT';
  2585. gten:
  2586. procname:='GE';
  2587. equaln:
  2588. procname:='EQ';
  2589. unequaln:
  2590. procname:='NE';
  2591. else
  2592. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2593. end;
  2594. case tfloatdef(left.resultdef).floattype of
  2595. s32real:
  2596. begin
  2597. procname:=procname+'S';
  2598. if nodetype in [addn,muln,subn,slashn] then
  2599. procname:=lower(procname);
  2600. end;
  2601. s64real:
  2602. procname:=procname+'D';
  2603. {!!! not yet implemented
  2604. s128real:
  2605. }
  2606. else
  2607. internalerror(2005082602);
  2608. end;
  2609. end;
  2610. { cast softfpu result? }
  2611. if not(target_info.system in systems_wince) then
  2612. begin
  2613. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2614. resultdef:=pasbool8type;
  2615. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  2616. ctypeconvnode.create_internal(right,fdef),
  2617. ccallparanode.create(
  2618. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  2619. end
  2620. else
  2621. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  2622. ccallparanode.create(left,nil)));
  2623. left:=nil;
  2624. right:=nil;
  2625. { do we need to reverse the result }
  2626. if notnode then
  2627. result:=cnotnode.create(result);
  2628. end;
  2629. function taddnode.pass_1 : tnode;
  2630. var
  2631. {$ifdef addstringopt}
  2632. hp : tnode;
  2633. {$endif addstringopt}
  2634. rd,ld : tdef;
  2635. i,i2 : longint;
  2636. lt,rt : tnodetype;
  2637. {$ifdef cpuneedsmulhelper}
  2638. procname : string[32];
  2639. {$endif cpuneedsmulhelper}
  2640. begin
  2641. result:=nil;
  2642. { Can we optimize multiple string additions into a single call?
  2643. This need to be done on a complete tree to detect the multiple
  2644. add nodes and is therefor done before the subtrees are processed }
  2645. if canbemultistringadd(self) then
  2646. begin
  2647. result:=genmultistringadd(self);
  2648. exit;
  2649. end;
  2650. { first do the two subtrees }
  2651. firstpass(left);
  2652. firstpass(right);
  2653. if codegenerror then
  2654. exit;
  2655. { load easier access variables }
  2656. rd:=right.resultdef;
  2657. ld:=left.resultdef;
  2658. rt:=right.nodetype;
  2659. lt:=left.nodetype;
  2660. { int/int gives real/real! }
  2661. if nodetype=slashn then
  2662. begin
  2663. {$ifdef cpufpemu}
  2664. result:=first_addfloat;
  2665. if assigned(result) then
  2666. exit;
  2667. {$endif cpufpemu}
  2668. expectloc:=LOC_FPUREGISTER;
  2669. end
  2670. { if both are orddefs then check sub types }
  2671. else if (ld.typ=orddef) and (rd.typ=orddef) then
  2672. begin
  2673. { optimize multiplacation by a power of 2 }
  2674. if not(cs_check_overflow in current_settings.localswitches) and
  2675. (nodetype = muln) and
  2676. (((left.nodetype = ordconstn) and
  2677. ispowerof2(tordconstnode(left).value,i)) or
  2678. ((right.nodetype = ordconstn) and
  2679. ispowerof2(tordconstnode(right).value,i2))) then
  2680. begin
  2681. if ((left.nodetype = ordconstn) and
  2682. ispowerof2(tordconstnode(left).value,i)) then
  2683. begin
  2684. tordconstnode(left).value := i;
  2685. result := cshlshrnode.create(shln,right,left);
  2686. end
  2687. else
  2688. begin
  2689. tordconstnode(right).value := i2;
  2690. result := cshlshrnode.create(shln,left,right);
  2691. end;
  2692. result.resultdef := resultdef;
  2693. left := nil;
  2694. right := nil;
  2695. exit;
  2696. end;
  2697. { 2 booleans ? }
  2698. if is_boolean(ld) and is_boolean(rd) then
  2699. begin
  2700. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  2701. (nf_short_bool in flags)) and
  2702. (nodetype in [andn,orn]) then
  2703. expectloc:=LOC_JUMP
  2704. else
  2705. begin
  2706. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2707. expectloc:=LOC_FLAGS
  2708. else
  2709. expectloc:=LOC_REGISTER;
  2710. end;
  2711. end
  2712. else
  2713. { Both are chars? only convert to shortstrings for addn }
  2714. if is_char(ld) then
  2715. begin
  2716. if nodetype=addn then
  2717. internalerror(200103291);
  2718. expectloc:=LOC_FLAGS;
  2719. end
  2720. {$ifndef cpu64bitalu}
  2721. { is there a 64 bit type ? }
  2722. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  2723. begin
  2724. result := first_add64bitint;
  2725. if assigned(result) then
  2726. exit;
  2727. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2728. expectloc:=LOC_REGISTER
  2729. else
  2730. expectloc:=LOC_JUMP;
  2731. end
  2732. {$endif cpu64bitalu}
  2733. {$ifndef cpuneedsmulhelper}
  2734. { is there a cardinal? }
  2735. else if (torddef(ld).ordtype=u32bit) then
  2736. begin
  2737. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2738. expectloc:=LOC_REGISTER
  2739. else
  2740. {$ifdef cpu16bitalu}
  2741. expectloc:=LOC_JUMP;
  2742. {$else cpu16bitalu}
  2743. expectloc:=LOC_FLAGS;
  2744. {$endif cpu16bitalu}
  2745. end
  2746. {$endif cpuneedsmulhelper}
  2747. { generic s32bit conversion }
  2748. else
  2749. begin
  2750. {$ifdef cpuneedsmulhelper}
  2751. if (nodetype=muln) and not(torddef(resultdef).ordtype in [u8bit,s8bit{$ifdef cpu16bitalu},u16bit,s16bit{$endif}]) then
  2752. begin
  2753. result := nil;
  2754. case torddef(resultdef).ordtype of
  2755. s16bit:
  2756. procname := 'fpc_mul_integer';
  2757. u16bit:
  2758. procname := 'fpc_mul_word';
  2759. s32bit:
  2760. procname := 'fpc_mul_longint';
  2761. u32bit:
  2762. procname := 'fpc_mul_dword';
  2763. else
  2764. internalerror(2011022301);
  2765. end;
  2766. result := ccallnode.createintern(procname,
  2767. ccallparanode.create(cordconstnode.create(0,pasbool8type,false),
  2768. ccallparanode.create(right,
  2769. ccallparanode.create(left,nil))));
  2770. left := nil;
  2771. right := nil;
  2772. firstpass(result);
  2773. exit;
  2774. end;
  2775. {$endif cpuneedsmulhelper}
  2776. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2777. expectloc:=LOC_REGISTER
  2778. else
  2779. {$ifdef cpu16bitalu}
  2780. expectloc:=LOC_JUMP;
  2781. {$else cpu16bitalu}
  2782. expectloc:=LOC_FLAGS;
  2783. {$endif cpu16bitalu}
  2784. end;
  2785. end
  2786. { left side a setdef, must be before string processing,
  2787. else array constructor can be seen as array of char (PFV) }
  2788. else if (ld.typ=setdef) then
  2789. begin
  2790. { small sets are handled inline by the compiler.
  2791. small set doesn't have support for adding ranges }
  2792. if is_smallset(ld) and
  2793. not(
  2794. (right.nodetype=setelementn) and
  2795. assigned(tsetelementnode(right).right)
  2796. ) then
  2797. begin
  2798. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2799. expectloc:=LOC_FLAGS
  2800. else
  2801. expectloc:=LOC_REGISTER;
  2802. end
  2803. else
  2804. begin
  2805. result := first_addset;
  2806. if assigned(result) then
  2807. exit;
  2808. expectloc:=LOC_CREFERENCE;
  2809. end;
  2810. end
  2811. { compare pchar by addresses like BP/Delphi }
  2812. else if is_pchar(ld) then
  2813. begin
  2814. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2815. expectloc:=LOC_REGISTER
  2816. else
  2817. expectloc:=LOC_FLAGS;
  2818. end
  2819. { is one of the operands a string }
  2820. else if (ld.typ=stringdef) then
  2821. begin
  2822. if is_widestring(ld) then
  2823. begin
  2824. { this is only for add, the comparisaion is handled later }
  2825. expectloc:=LOC_REGISTER;
  2826. end
  2827. else if is_unicodestring(ld) then
  2828. begin
  2829. { this is only for add, the comparisaion is handled later }
  2830. expectloc:=LOC_REGISTER;
  2831. end
  2832. else if is_ansistring(ld) then
  2833. begin
  2834. { this is only for add, the comparisaion is handled later }
  2835. expectloc:=LOC_REGISTER;
  2836. end
  2837. else if is_longstring(ld) then
  2838. begin
  2839. { this is only for add, the comparisaion is handled later }
  2840. expectloc:=LOC_REFERENCE;
  2841. end
  2842. else
  2843. begin
  2844. {$ifdef addstringopt}
  2845. { can create a call which isn't handled by callparatemp }
  2846. if canbeaddsstringcharoptnode(self) then
  2847. begin
  2848. hp := genaddsstringcharoptnode(self);
  2849. pass_1 := hp;
  2850. exit;
  2851. end
  2852. else
  2853. {$endif addstringopt}
  2854. begin
  2855. { Fix right to be shortstring }
  2856. if is_char(right.resultdef) then
  2857. begin
  2858. inserttypeconv(right,cshortstringtype);
  2859. firstpass(right);
  2860. end;
  2861. end;
  2862. {$ifdef addstringopt}
  2863. { can create a call which isn't handled by callparatemp }
  2864. if canbeaddsstringcsstringoptnode(self) then
  2865. begin
  2866. hp := genaddsstringcsstringoptnode(self);
  2867. pass_1 := hp;
  2868. exit;
  2869. end;
  2870. {$endif addstringopt}
  2871. end;
  2872. { otherwise, let addstring convert everything }
  2873. result := first_addstring;
  2874. exit;
  2875. end
  2876. { is one a real float ? }
  2877. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  2878. begin
  2879. {$ifdef cpufpemu}
  2880. result:=first_addfloat;
  2881. if assigned(result) then
  2882. exit;
  2883. {$endif cpufpemu}
  2884. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2885. expectloc:=LOC_FPUREGISTER
  2886. else
  2887. expectloc:=LOC_FLAGS;
  2888. end
  2889. { pointer comperation and subtraction }
  2890. else if (ld.typ=pointerdef) then
  2891. begin
  2892. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2893. expectloc:=LOC_REGISTER
  2894. else
  2895. expectloc:=LOC_FLAGS;
  2896. end
  2897. else if is_implicit_pointer_object_type(ld) then
  2898. begin
  2899. expectloc:=LOC_FLAGS;
  2900. end
  2901. else if (ld.typ=classrefdef) then
  2902. begin
  2903. expectloc:=LOC_FLAGS;
  2904. end
  2905. { support procvar=nil,procvar<>nil }
  2906. else if ((ld.typ=procvardef) and (rt=niln)) or
  2907. ((rd.typ=procvardef) and (lt=niln)) then
  2908. begin
  2909. expectloc:=LOC_FLAGS;
  2910. end
  2911. {$ifdef SUPPORT_MMX}
  2912. { mmx support, this must be before the zero based array
  2913. check }
  2914. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  2915. is_mmx_able_array(rd) then
  2916. begin
  2917. expectloc:=LOC_MMXREGISTER;
  2918. end
  2919. {$endif SUPPORT_MMX}
  2920. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  2921. begin
  2922. expectloc:=LOC_REGISTER;
  2923. end
  2924. else if (rd.typ=procvardef) and
  2925. (ld.typ=procvardef) and
  2926. equal_defs(rd,ld) then
  2927. begin
  2928. expectloc:=LOC_FLAGS;
  2929. end
  2930. else if (ld.typ=enumdef) then
  2931. begin
  2932. expectloc:=LOC_FLAGS;
  2933. end
  2934. {$ifdef SUPPORT_MMX}
  2935. else if (cs_mmx in current_settings.localswitches) and
  2936. is_mmx_able_array(ld) and
  2937. is_mmx_able_array(rd) then
  2938. begin
  2939. expectloc:=LOC_MMXREGISTER;
  2940. end
  2941. {$endif SUPPORT_MMX}
  2942. { the general solution is to convert to 32 bit int }
  2943. else
  2944. begin
  2945. expectloc:=LOC_REGISTER;
  2946. end;
  2947. end;
  2948. {$ifdef state_tracking}
  2949. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2950. var factval:Tnode;
  2951. begin
  2952. track_state_pass:=false;
  2953. if left.track_state_pass(exec_known) then
  2954. begin
  2955. track_state_pass:=true;
  2956. left.resultdef:=nil;
  2957. do_typecheckpass(left);
  2958. end;
  2959. factval:=aktstate.find_fact(left);
  2960. if factval<>nil then
  2961. begin
  2962. track_state_pass:=true;
  2963. left.destroy;
  2964. left:=factval.getcopy;
  2965. end;
  2966. if right.track_state_pass(exec_known) then
  2967. begin
  2968. track_state_pass:=true;
  2969. right.resultdef:=nil;
  2970. do_typecheckpass(right);
  2971. end;
  2972. factval:=aktstate.find_fact(right);
  2973. if factval<>nil then
  2974. begin
  2975. track_state_pass:=true;
  2976. right.destroy;
  2977. right:=factval.getcopy;
  2978. end;
  2979. end;
  2980. {$endif}
  2981. end.