nadd.pas 137 KB

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