nadd.pas 127 KB

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