nadd.pas 144 KB

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