nadd.pas 127 KB

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