nadd.pas 127 KB

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