nadd.pas 127 KB

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