nadd.pas 126 KB

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