nadd.pas 125 KB

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