nadd.pas 136 KB

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