nadd.pas 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219
  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. function maybe_cast_ordconst(var n: tnode; adef: tdef): boolean;
  958. begin
  959. result:=(tordconstnode(n).value>=torddef(adef).low) and
  960. (tordconstnode(n).value<=torddef(adef).high);
  961. if result then
  962. inserttypeconv(n,adef);
  963. end;
  964. begin
  965. result:=nil;
  966. rlow:=0;
  967. llow:=0;
  968. rhigh:=0;
  969. lhigh:=0;
  970. { avoid any problems with type parameters later on }
  971. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  972. begin
  973. resultdef:=cundefinedtype;
  974. exit;
  975. end;
  976. { both left and right need to be valid }
  977. set_varstate(left,vs_read,[vsf_must_be_valid]);
  978. set_varstate(right,vs_read,[vsf_must_be_valid]);
  979. if codegenerror then
  980. exit;
  981. { tp procvar support. Omit for converted assigned() nodes }
  982. if not (nf_load_procvar in flags) then
  983. begin
  984. maybe_call_procvar(left,true);
  985. maybe_call_procvar(right,true);
  986. end
  987. else
  988. if not (nodetype in [equaln,unequaln]) then
  989. InternalError(2013091601);
  990. { convert array constructors to sets, because there is no other operator
  991. possible for array constructors }
  992. if is_array_constructor(left.resultdef) then
  993. begin
  994. arrayconstructor_to_set(left);
  995. typecheckpass(left);
  996. end;
  997. if is_array_constructor(right.resultdef) then
  998. begin
  999. arrayconstructor_to_set(right);
  1000. typecheckpass(right);
  1001. end;
  1002. { allow operator overloading }
  1003. hp:=self;
  1004. if isbinaryoverloaded(hp) then
  1005. begin
  1006. result:=hp;
  1007. exit;
  1008. end;
  1009. { Stop checking when an error was found in the operator checking }
  1010. if codegenerror then
  1011. begin
  1012. result:=cerrornode.create;
  1013. exit;
  1014. end;
  1015. { Kylix allows enum+ordconstn in an enum type declaration, we need to do
  1016. the conversion here before the constant folding }
  1017. if (m_delphi in current_settings.modeswitches) and
  1018. (blocktype in [bt_type,bt_const_type,bt_var_type]) then
  1019. begin
  1020. if (left.resultdef.typ=enumdef) and
  1021. (right.resultdef.typ=orddef) then
  1022. begin
  1023. { insert explicit typecast to default signed int }
  1024. left:=ctypeconvnode.create_internal(left,sinttype);
  1025. typecheckpass(left);
  1026. end
  1027. else
  1028. if (left.resultdef.typ=orddef) and
  1029. (right.resultdef.typ=enumdef) then
  1030. begin
  1031. { insert explicit typecast to default signed int }
  1032. right:=ctypeconvnode.create_internal(right,sinttype);
  1033. typecheckpass(right);
  1034. end;
  1035. end;
  1036. { is one a real float, then both need to be floats, this
  1037. need to be done before the constant folding so constant
  1038. operation on a float and int are also handled }
  1039. {$ifdef x86}
  1040. { use extended as default real type only when the x87 fpu is used }
  1041. {$if defined(i386) or defined(i8086)}
  1042. if not(current_settings.fputype=fpu_x87) then
  1043. resultrealdef:=s64floattype
  1044. else
  1045. resultrealdef:=pbestrealtype^;
  1046. {$endif i386 or i8086}
  1047. {$ifdef x86_64}
  1048. { x86-64 has no x87 only mode, so use always double as default }
  1049. resultrealdef:=s64floattype;
  1050. {$endif x86_6}
  1051. {$else not x86}
  1052. resultrealdef:=pbestrealtype^;
  1053. {$endif not x86}
  1054. if (right.resultdef.typ=floatdef) or (left.resultdef.typ=floatdef) then
  1055. begin
  1056. { when both floattypes are already equal then use that
  1057. floattype for results }
  1058. if (right.resultdef.typ=floatdef) and
  1059. (left.resultdef.typ=floatdef) and
  1060. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype) then
  1061. resultrealdef:=left.resultdef
  1062. { when there is a currency type then use currency, but
  1063. only when currency is defined as float }
  1064. else
  1065. if (is_currency(right.resultdef) or
  1066. is_currency(left.resultdef)) and
  1067. ((s64currencytype.typ = floatdef) or
  1068. (nodetype <> slashn)) then
  1069. begin
  1070. resultrealdef:=s64currencytype;
  1071. inserttypeconv(right,resultrealdef);
  1072. inserttypeconv(left,resultrealdef);
  1073. end
  1074. else
  1075. begin
  1076. resultrealdef:=getbestreal(left.resultdef,right.resultdef);
  1077. inserttypeconv(right,resultrealdef);
  1078. inserttypeconv(left,resultrealdef);
  1079. end;
  1080. end;
  1081. { If both operands are constant and there is a unicodestring
  1082. or unicodestring then convert everything to unicodestring }
  1083. if is_constnode(right) and is_constnode(left) and
  1084. (is_unicodestring(right.resultdef) or
  1085. is_unicodestring(left.resultdef)) then
  1086. begin
  1087. inserttypeconv(right,cunicodestringtype);
  1088. inserttypeconv(left,cunicodestringtype);
  1089. end;
  1090. { If both operands are constant and there is a widechar
  1091. or widestring then convert everything to widestring. This
  1092. allows constant folding like char+widechar }
  1093. if is_constnode(right) and is_constnode(left) and
  1094. (is_widestring(right.resultdef) or
  1095. is_widestring(left.resultdef) or
  1096. is_widechar(right.resultdef) or
  1097. is_widechar(left.resultdef)) then
  1098. begin
  1099. inserttypeconv(right,cwidestringtype);
  1100. inserttypeconv(left,cwidestringtype);
  1101. end;
  1102. { load easier access variables }
  1103. rd:=right.resultdef;
  1104. ld:=left.resultdef;
  1105. rt:=right.nodetype;
  1106. lt:=left.nodetype;
  1107. { 4 character constant strings are compatible with orddef }
  1108. { in macpas mode (become cardinals) }
  1109. if (m_mac in current_settings.modeswitches) and
  1110. { only allow for comparisons, additions etc are }
  1111. { normally program errors }
  1112. (nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) and
  1113. (((lt=stringconstn) and
  1114. (tstringconstnode(left).len=4) and
  1115. (rd.typ=orddef)) or
  1116. ((rt=stringconstn) and
  1117. (tstringconstnode(right).len=4) and
  1118. (ld.typ=orddef))) then
  1119. begin
  1120. if (rt=stringconstn) then
  1121. begin
  1122. inserttypeconv(right,u32inttype);
  1123. rt:=right.nodetype;
  1124. rd:=right.resultdef;
  1125. end
  1126. else
  1127. begin
  1128. inserttypeconv(left,u32inttype);
  1129. lt:=left.nodetype;
  1130. ld:=left.resultdef;
  1131. end;
  1132. end;
  1133. { but an int/int gives real/real! }
  1134. if (nodetype=slashn) and not(is_vector(left.resultdef)) and not(is_vector(right.resultdef)) then
  1135. begin
  1136. if is_currency(left.resultdef) and
  1137. is_currency(right.resultdef) then
  1138. { In case of currency, converting to float means dividing by 10000 }
  1139. { However, since this is already a division, both divisions by }
  1140. { 10000 are eliminated when we divide the results -> we can skip }
  1141. { them. }
  1142. if s64currencytype.typ = floatdef then
  1143. begin
  1144. { there's no s64comptype or so, how do we avoid the type conversion?
  1145. left.resultdef := s64comptype;
  1146. right.resultdef := s64comptype; }
  1147. end
  1148. else
  1149. begin
  1150. left.resultdef := s64inttype;
  1151. right.resultdef := s64inttype;
  1152. end;
  1153. inserttypeconv(right,resultrealdef);
  1154. inserttypeconv(left,resultrealdef);
  1155. end
  1156. { if both are orddefs then check sub types }
  1157. else if (ld.typ=orddef) and (rd.typ=orddef) then
  1158. begin
  1159. { set for & and | operations in macpas mode: they only work on }
  1160. { booleans, and always short circuit evaluation }
  1161. if (nf_short_bool in flags) then
  1162. begin
  1163. if not is_boolean(ld) then
  1164. begin
  1165. inserttypeconv(left,pasbool8type);
  1166. ld := left.resultdef;
  1167. end;
  1168. if not is_boolean(rd) then
  1169. begin
  1170. inserttypeconv(right,pasbool8type);
  1171. rd := right.resultdef;
  1172. end;
  1173. end;
  1174. { 2 booleans? }
  1175. if (is_boolean(ld) and is_boolean(rd)) then
  1176. begin
  1177. case nodetype of
  1178. xorn,
  1179. andn,
  1180. orn:
  1181. begin
  1182. { Make sides equal to the largest boolean }
  1183. if (torddef(left.resultdef).size>torddef(right.resultdef).size) or
  1184. (is_cbool(left.resultdef) and not is_cbool(right.resultdef)) then
  1185. begin
  1186. right:=ctypeconvnode.create_internal(right,left.resultdef);
  1187. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  1188. typecheckpass(right);
  1189. end
  1190. else if (torddef(left.resultdef).size<torddef(right.resultdef).size) or
  1191. (not is_cbool(left.resultdef) and is_cbool(right.resultdef)) then
  1192. begin
  1193. left:=ctypeconvnode.create_internal(left,right.resultdef);
  1194. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  1195. typecheckpass(left);
  1196. end;
  1197. end;
  1198. ltn,
  1199. lten,
  1200. gtn,
  1201. gten:
  1202. begin
  1203. { convert both to pasbool to perform the comparison (so
  1204. that longbool(4) = longbool(2), since both represent
  1205. "true" }
  1206. inserttypeconv(left,pasbool8type);
  1207. inserttypeconv(right,pasbool8type);
  1208. end;
  1209. unequaln,
  1210. equaln:
  1211. begin
  1212. if not(cs_full_boolean_eval in current_settings.localswitches) or
  1213. (nf_short_bool in flags) then
  1214. begin
  1215. { Remove any compares with constants }
  1216. if (left.nodetype=ordconstn) then
  1217. begin
  1218. hp:=right;
  1219. b:=(tordconstnode(left).value<>0);
  1220. ot:=nodetype;
  1221. left.free;
  1222. left:=nil;
  1223. right:=nil;
  1224. if (not(b) and (ot=equaln)) or
  1225. (b and (ot=unequaln)) then
  1226. begin
  1227. hp:=cnotnode.create(hp);
  1228. end;
  1229. result:=hp;
  1230. exit;
  1231. end;
  1232. if (right.nodetype=ordconstn) then
  1233. begin
  1234. hp:=left;
  1235. b:=(tordconstnode(right).value<>0);
  1236. ot:=nodetype;
  1237. right.free;
  1238. right:=nil;
  1239. left:=nil;
  1240. if (not(b) and (ot=equaln)) or
  1241. (b and (ot=unequaln)) then
  1242. begin
  1243. hp:=cnotnode.create(hp);
  1244. end;
  1245. result:=hp;
  1246. exit;
  1247. end;
  1248. end;
  1249. { Delphi-compatibility: convert both to pasbool to
  1250. perform the equality comparison }
  1251. inserttypeconv(left,pasbool8type);
  1252. inserttypeconv(right,pasbool8type);
  1253. end;
  1254. else
  1255. begin
  1256. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1257. result:=cnothingnode.create;
  1258. exit;
  1259. end;
  1260. end;
  1261. end
  1262. { Both are chars? }
  1263. else if is_char(rd) and is_char(ld) then
  1264. begin
  1265. if nodetype=addn then
  1266. begin
  1267. resultdef:=cshortstringtype;
  1268. if not(is_constcharnode(left) and is_constcharnode(right)) then
  1269. begin
  1270. inserttypeconv(left,cshortstringtype);
  1271. {$ifdef addstringopt}
  1272. hp := genaddsstringcharoptnode(self);
  1273. result := hp;
  1274. exit;
  1275. {$endif addstringopt}
  1276. end
  1277. end
  1278. else if not(nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) then
  1279. begin
  1280. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1281. result:=cnothingnode.create;
  1282. exit;
  1283. end;
  1284. end
  1285. { There is a widechar? }
  1286. else if is_widechar(rd) or is_widechar(ld) then
  1287. begin
  1288. { widechar+widechar gives unicodestring }
  1289. if nodetype=addn then
  1290. begin
  1291. inserttypeconv(left,cunicodestringtype);
  1292. if (torddef(rd).ordtype<>uwidechar) then
  1293. inserttypeconv(right,cwidechartype);
  1294. resultdef:=cunicodestringtype;
  1295. end
  1296. else
  1297. begin
  1298. if (torddef(ld).ordtype<>uwidechar) then
  1299. inserttypeconv(left,cwidechartype);
  1300. if (torddef(rd).ordtype<>uwidechar) then
  1301. inserttypeconv(right,cwidechartype);
  1302. end;
  1303. end
  1304. { is there a currency type ? }
  1305. else if ((torddef(rd).ordtype=scurrency) or (torddef(ld).ordtype=scurrency)) then
  1306. begin
  1307. if (torddef(ld).ordtype<>scurrency) then
  1308. inserttypeconv(left,s64currencytype);
  1309. if (torddef(rd).ordtype<>scurrency) then
  1310. inserttypeconv(right,s64currencytype);
  1311. end
  1312. { leave some constant integer expressions alone in case the
  1313. resultdef of the integer types doesn't influence the outcome,
  1314. because the forced type conversions below can otherwise result
  1315. in unexpected results (such as high(qword)<high(int64) returning
  1316. true because high(qword) gets converted to int64) }
  1317. else if is_integer(ld) and is_integer(rd) and
  1318. (lt=ordconstn) and (rt=ordconstn) and
  1319. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) then
  1320. begin
  1321. end
  1322. { "and" does't care about the sign of integers }
  1323. { "xor", "or" and compares don't need extension to native int }
  1324. { size either as long as both values are signed or unsigned }
  1325. { "xor" and "or" also don't care about the sign if the values }
  1326. { occupy an entire register }
  1327. { don't do it if either type is 64 bit (except for "and"), }
  1328. { since in that case we can't safely find a "common" type }
  1329. else if is_integer(ld) and is_integer(rd) and
  1330. ((nodetype=andn) or
  1331. ((nodetype in [orn,xorn,equaln,unequaln,gtn,gten,ltn,lten]) and
  1332. not is_64bitint(ld) and not is_64bitint(rd) and
  1333. (is_signed(ld)=is_signed(rd)))) then
  1334. begin
  1335. { Delphi-compatible: prefer unsigned type for "and", when the
  1336. unsigned type is bigger than the signed one, and also bigger
  1337. than min(native_int, 32-bit) }
  1338. if (is_oversizedint(rd) or is_nativeint(rd) or is_32bitint(rd)) and
  1339. (rd.size>=ld.size) and
  1340. not is_signed(rd) and is_signed(ld) then
  1341. inserttypeconv_internal(left,rd)
  1342. else if (is_oversizedint(ld) or is_nativeint(ld) or is_32bitint(ld)) and
  1343. (ld.size>=rd.size) and
  1344. not is_signed(ld) and is_signed(rd) then
  1345. inserttypeconv_internal(right,ld)
  1346. else
  1347. begin
  1348. { not to left right.resultdef, because that may
  1349. cause a range error if left and right's def don't
  1350. completely overlap }
  1351. nd:=get_common_intdef(torddef(ld),torddef(rd),true);
  1352. inserttypeconv(left,nd);
  1353. inserttypeconv(right,nd);
  1354. end;
  1355. end
  1356. { don't extend (sign-mismatched) comparisons if either side is a constant
  1357. whose value is within range of opposite side }
  1358. else if is_integer(ld) and is_integer(rd) and
  1359. (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1360. (is_signed(ld)<>is_signed(rd)) and
  1361. (
  1362. ((lt=ordconstn) and maybe_cast_ordconst(left,rd)) or
  1363. ((rt=ordconstn) and maybe_cast_ordconst(right,ld))
  1364. ) then
  1365. begin
  1366. { done here }
  1367. end
  1368. { is there a signed 64 bit type ? }
  1369. else if ((torddef(rd).ordtype=s64bit) or (torddef(ld).ordtype=s64bit)) then
  1370. begin
  1371. if (torddef(ld).ordtype<>s64bit) then
  1372. inserttypeconv(left,s64inttype);
  1373. if (torddef(rd).ordtype<>s64bit) then
  1374. inserttypeconv(right,s64inttype);
  1375. end
  1376. { is there a unsigned 64 bit type ? }
  1377. else if ((torddef(rd).ordtype=u64bit) or (torddef(ld).ordtype=u64bit)) then
  1378. begin
  1379. if (torddef(ld).ordtype<>u64bit) then
  1380. inserttypeconv(left,u64inttype);
  1381. if (torddef(rd).ordtype<>u64bit) then
  1382. inserttypeconv(right,u64inttype);
  1383. end
  1384. { is there a larger int? }
  1385. else if is_oversizedint(rd) or is_oversizedint(ld) then
  1386. begin
  1387. nd:=get_common_intdef(torddef(ld),torddef(rd),false);
  1388. inserttypeconv(right,nd);
  1389. inserttypeconv(left,nd);
  1390. end
  1391. { is there a native unsigned int? }
  1392. else if is_nativeuint(rd) or is_nativeuint(ld) then
  1393. begin
  1394. { convert positive constants to uinttype }
  1395. if (not is_nativeuint(ld)) and
  1396. is_constintnode(left) and
  1397. (tordconstnode(left).value >= 0) then
  1398. inserttypeconv(left,uinttype);
  1399. if (not is_nativeuint(rd)) and
  1400. is_constintnode(right) and
  1401. (tordconstnode(right).value >= 0) then
  1402. inserttypeconv(right,uinttype);
  1403. { when one of the operand is signed or the operation is subn then perform
  1404. the operation in a larger signed type, can't use rd/ld here because there
  1405. could be already typeconvs inserted.
  1406. This is compatible with the code below for other unsigned types (PFV) }
  1407. if is_signed(left.resultdef) or
  1408. is_signed(right.resultdef) or
  1409. (nodetype=subn) then
  1410. begin
  1411. if nodetype<>subn then
  1412. CGMessage(type_h_mixed_signed_unsigned);
  1413. { mark as internal in case added for a subn, so }
  1414. { ttypeconvnode.simplify can remove the larger }
  1415. { typecast again if semantically correct. Even }
  1416. { if we could detect that here already, we }
  1417. { mustn't do it here because that would change }
  1418. { overload choosing behaviour etc. The code in }
  1419. { ncnv.pas is run after that is already decided }
  1420. if (not is_signed(left.resultdef) and
  1421. not is_signed(right.resultdef)) or
  1422. (nodetype in [orn,xorn]) then
  1423. include(flags,nf_internal);
  1424. { get next larger signed int type }
  1425. nd:=get_common_intdef(torddef(sinttype),torddef(uinttype),false);
  1426. inserttypeconv(left,nd);
  1427. inserttypeconv(right,nd);
  1428. end
  1429. else
  1430. begin
  1431. if not is_nativeuint(left.resultdef) then
  1432. inserttypeconv(left,uinttype);
  1433. if not is_nativeuint(right.resultdef) then
  1434. inserttypeconv(right,uinttype);
  1435. end;
  1436. end
  1437. { generic ord conversion is sinttype }
  1438. else
  1439. begin
  1440. { When there is a signed type or there is a minus operation
  1441. we convert to signed int. Otherwise (both are unsigned) we keep
  1442. the result also unsigned. This is compatible with Delphi (PFV) }
  1443. if is_signed(ld) or
  1444. is_signed(rd) or
  1445. (nodetype=subn) then
  1446. begin
  1447. inserttypeconv(right,sinttype);
  1448. inserttypeconv(left,sinttype);
  1449. end
  1450. else
  1451. begin
  1452. inserttypeconv(right,uinttype);
  1453. inserttypeconv(left,uinttype);
  1454. end;
  1455. end;
  1456. end
  1457. { if both are floatdefs, conversion is already done before constant folding }
  1458. else if (ld.typ=floatdef) then
  1459. begin
  1460. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  1461. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1462. end
  1463. { left side a setdef, must be before string processing,
  1464. else array constructor can be seen as array of char (PFV) }
  1465. else if (ld.typ=setdef) then
  1466. begin
  1467. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1468. CGMessage(type_e_set_operation_unknown);
  1469. { right must either be a set or a set element }
  1470. if (rd.typ<>setdef) and
  1471. (rt<>setelementn) then
  1472. CGMessage(type_e_mismatch)
  1473. { Make operands the same setdef. If one's elementtype fits }
  1474. { entirely inside the other's, pick the one with the largest }
  1475. { range. Otherwise create a new setdef with a range which }
  1476. { can contain both. }
  1477. else if not(equal_defs(ld,rd)) then
  1478. begin
  1479. { note: ld cannot be an empty set with elementdef=nil in }
  1480. { case right is not a set, arrayconstructor_to_set takes }
  1481. { care of that }
  1482. { 1: rd is a set with an assigned elementdef, and ld is }
  1483. { either an empty set without elementdef or a set whose }
  1484. { elementdef fits in rd's elementdef -> convert to rd }
  1485. if ((rd.typ=setdef) and
  1486. assigned(tsetdef(rd).elementdef) and
  1487. (not assigned(tsetdef(ld).elementdef) or
  1488. is_in_limit(ld,rd))) then
  1489. inserttypeconv(left,rd)
  1490. { 2: rd is either an empty set without elementdef or a set }
  1491. { whose elementdef fits in ld's elementdef, or a set }
  1492. { element whose def fits in ld's elementdef -> convert }
  1493. { to ld. ld's elementdef can't be nil here, is caught }
  1494. { previous case and "note:" above }
  1495. else if ((rd.typ=setdef) and
  1496. (not assigned(tsetdef(rd).elementdef) or
  1497. is_in_limit(rd,ld))) or
  1498. ((rd.typ<>setdef) and
  1499. is_in_limit(rd,tsetdef(ld).elementdef)) then
  1500. if (rd.typ=setdef) then
  1501. inserttypeconv(right,ld)
  1502. else
  1503. inserttypeconv(right,tsetdef(ld).elementdef)
  1504. { 3: otherwise create setdef which encompasses both, taking }
  1505. { into account empty sets without elementdef }
  1506. else
  1507. begin
  1508. if assigned(tsetdef(ld).elementdef) then
  1509. begin
  1510. llow:=tsetdef(ld).setbase;
  1511. lhigh:=tsetdef(ld).setmax;
  1512. end;
  1513. if (rd.typ=setdef) then
  1514. if assigned(tsetdef(rd).elementdef) then
  1515. begin
  1516. rlow:=tsetdef(rd).setbase;
  1517. rhigh:=tsetdef(rd).setmax;
  1518. end
  1519. else
  1520. begin
  1521. { ld's elementdef must have been valid }
  1522. rlow:=llow;
  1523. rhigh:=lhigh;
  1524. end
  1525. else
  1526. getrange(rd,rlow,rhigh);
  1527. if not assigned(tsetdef(ld).elementdef) then
  1528. begin
  1529. llow:=rlow;
  1530. lhigh:=rhigh;
  1531. end;
  1532. nd:=csetdef.create(tsetdef(ld).elementdef,min(llow,rlow).svalue,max(lhigh,rhigh).svalue);
  1533. inserttypeconv(left,nd);
  1534. if (rd.typ=setdef) then
  1535. inserttypeconv(right,nd)
  1536. else
  1537. inserttypeconv(right,tsetdef(nd).elementdef);
  1538. end;
  1539. end;
  1540. end
  1541. { pointer comparision and subtraction }
  1542. else if (
  1543. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  1544. ) or
  1545. { compare/add pchar to variable (not stringconst) char arrays
  1546. by addresses like BP/Delphi }
  1547. (
  1548. (nodetype in [equaln,unequaln,subn,addn]) and
  1549. (
  1550. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1551. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1552. )
  1553. ) then
  1554. begin
  1555. { convert char array to pointer }
  1556. if is_chararray(rd) then
  1557. begin
  1558. inserttypeconv(right,charpointertype);
  1559. rd:=right.resultdef;
  1560. end
  1561. else if is_chararray(ld) then
  1562. begin
  1563. inserttypeconv(left,charpointertype);
  1564. ld:=left.resultdef;
  1565. end;
  1566. case nodetype of
  1567. equaln,unequaln :
  1568. begin
  1569. if is_voidpointer(right.resultdef) then
  1570. inserttypeconv(right,left.resultdef)
  1571. else if is_voidpointer(left.resultdef) then
  1572. inserttypeconv(left,right.resultdef)
  1573. else if not(equal_defs(ld,rd)) then
  1574. IncompatibleTypes(ld,rd);
  1575. { now that the type checking is done, convert both to charpointer, }
  1576. { because methodpointers are 8 bytes even though only the first 4 }
  1577. { bytes must be compared. This can happen here if we are in }
  1578. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1579. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1580. { optimized away, since the result already was a voidpointer, so }
  1581. { use a charpointer instead (JM) }
  1582. {$if defined(jvm)}
  1583. inserttypeconv_internal(left,java_jlobject);
  1584. inserttypeconv_internal(right,java_jlobject);
  1585. {$elseif defined(i8086)}
  1586. if is_hugepointer(left.resultdef) then
  1587. inserttypeconv_internal(left,charhugepointertype)
  1588. else if is_farpointer(left.resultdef) then
  1589. inserttypeconv_internal(left,charfarpointertype)
  1590. else
  1591. inserttypeconv_internal(left,charnearpointertype);
  1592. if is_hugepointer(right.resultdef) then
  1593. inserttypeconv_internal(right,charhugepointertype)
  1594. else if is_farpointer(right.resultdef) then
  1595. inserttypeconv_internal(right,charfarpointertype)
  1596. else
  1597. inserttypeconv_internal(right,charnearpointertype);
  1598. {$else}
  1599. inserttypeconv_internal(left,charpointertype);
  1600. inserttypeconv_internal(right,charpointertype);
  1601. {$endif jvm}
  1602. end;
  1603. ltn,lten,gtn,gten:
  1604. begin
  1605. if (cs_extsyntax in current_settings.moduleswitches) or
  1606. (nf_internal in flags) then
  1607. begin
  1608. if is_voidpointer(right.resultdef) then
  1609. inserttypeconv(right,left.resultdef)
  1610. else if is_voidpointer(left.resultdef) then
  1611. inserttypeconv(left,right.resultdef)
  1612. else if not(equal_defs(ld,rd)) then
  1613. IncompatibleTypes(ld,rd);
  1614. end
  1615. else
  1616. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1617. end;
  1618. subn:
  1619. begin
  1620. if (cs_extsyntax in current_settings.moduleswitches) then
  1621. begin
  1622. if is_voidpointer(right.resultdef) then
  1623. begin
  1624. if is_big_untyped_addrnode(right) then
  1625. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1626. inserttypeconv(right,left.resultdef)
  1627. end
  1628. else if is_voidpointer(left.resultdef) then
  1629. inserttypeconv(left,right.resultdef)
  1630. else if not(equal_defs(ld,rd)) then
  1631. IncompatibleTypes(ld,rd);
  1632. end
  1633. else
  1634. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1635. if not(nf_has_pointerdiv in flags) and
  1636. (tpointerdef(rd).pointeddef.size>1) then
  1637. begin
  1638. hp:=getcopy;
  1639. include(hp.flags,nf_has_pointerdiv);
  1640. result:=cmoddivnode.create(divn,hp,
  1641. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(rd).pointer_subtraction_result_type,false));
  1642. end;
  1643. resultdef:=tpointerdef(rd).pointer_subtraction_result_type;
  1644. exit;
  1645. end;
  1646. else
  1647. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1648. end;
  1649. end
  1650. { is one of the operands a string?,
  1651. chararrays are also handled as strings (after conversion), also take
  1652. care of chararray+chararray and chararray+char.
  1653. Note: Must be done after pointerdef+pointerdef has been checked, else
  1654. pchar is converted to string }
  1655. else if (rd.typ=stringdef) or
  1656. (ld.typ=stringdef) or
  1657. { stringconstn's can be arraydefs }
  1658. (lt=stringconstn) or
  1659. (rt=stringconstn) or
  1660. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1661. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1662. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1663. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1664. begin
  1665. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1666. begin
  1667. { Is there a unicodestring? }
  1668. if is_unicodestring(rd) or is_unicodestring(ld) or
  1669. ((m_default_unicodestring in current_settings.modeswitches) and
  1670. (cs_refcountedstrings in current_settings.localswitches) and
  1671. (
  1672. is_pwidechar(rd) or is_widechararray(rd) or is_open_widechararray(rd) or (lt = stringconstn) or
  1673. is_pwidechar(ld) or is_widechararray(ld) or is_open_widechararray(ld) or (rt = stringconstn)
  1674. )
  1675. ) then
  1676. strtype:=st_unicodestring
  1677. else
  1678. { Is there a widestring? }
  1679. if is_widestring(rd) or is_widestring(ld) or
  1680. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1681. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1682. strtype:=st_widestring
  1683. else
  1684. if is_ansistring(rd) or is_ansistring(ld) or
  1685. ((cs_refcountedstrings in current_settings.localswitches) and
  1686. //todo: Move some of this to longstring's then they are implemented?
  1687. (
  1688. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or (lt = stringconstn) or
  1689. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld) or (rt = stringconstn)
  1690. )
  1691. ) then
  1692. strtype:=st_ansistring
  1693. else
  1694. if is_longstring(rd) or is_longstring(ld) then
  1695. strtype:=st_longstring
  1696. else
  1697. begin
  1698. { TODO: todo: add a warning/hint here if one converting a too large array}
  1699. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1700. Note: Delphi halts with error if "array [0..xx] of char"
  1701. is assigned to ShortString and string length is less
  1702. then array size }
  1703. strtype:= st_shortstring;
  1704. end;
  1705. // Now convert nodes to common string type
  1706. case strtype of
  1707. st_widestring :
  1708. begin
  1709. if not(is_widestring(rd)) then
  1710. inserttypeconv(right,cwidestringtype);
  1711. if not(is_widestring(ld)) then
  1712. inserttypeconv(left,cwidestringtype);
  1713. end;
  1714. st_unicodestring :
  1715. begin
  1716. if not(is_unicodestring(rd)) then
  1717. inserttypeconv(right,cunicodestringtype);
  1718. if not(is_unicodestring(ld)) then
  1719. inserttypeconv(left,cunicodestringtype);
  1720. end;
  1721. st_ansistring :
  1722. begin
  1723. { use same code page if possible (don't force same code
  1724. page in case both are ansistrings with code page <>
  1725. CP_NONE, since then data loss can occur (the ansistring
  1726. helpers will convert them at run time to an encoding
  1727. that can represent both encodings) }
  1728. if is_ansistring(ld) and
  1729. (tstringdef(ld).encoding<>0) and
  1730. (tstringdef(ld).encoding<>globals.CP_NONE) and
  1731. (not is_ansistring(rd) or
  1732. (tstringdef(rd).encoding=0) or
  1733. (tstringdef(rd).encoding=globals.CP_NONE)) then
  1734. inserttypeconv(right,ld)
  1735. else if is_ansistring(rd) and
  1736. (tstringdef(rd).encoding<>0) and
  1737. (tstringdef(rd).encoding<>globals.CP_NONE) and
  1738. (not is_ansistring(ld) or
  1739. (tstringdef(ld).encoding=0) or
  1740. (tstringdef(ld).encoding=globals.CP_NONE)) then
  1741. inserttypeconv(left,rd)
  1742. else
  1743. begin
  1744. if not is_ansistring(ld) then
  1745. inserttypeconv(left,getansistringdef);
  1746. if not is_ansistring(rd) then
  1747. inserttypeconv(right,getansistringdef);
  1748. end;
  1749. end;
  1750. st_longstring :
  1751. begin
  1752. if not(is_longstring(rd)) then
  1753. inserttypeconv(right,clongstringtype);
  1754. if not(is_longstring(ld)) then
  1755. inserttypeconv(left,clongstringtype);
  1756. end;
  1757. st_shortstring :
  1758. begin
  1759. if not(is_shortstring(ld)) then
  1760. inserttypeconv(left,cshortstringtype);
  1761. { don't convert char, that can be handled by the optimized node }
  1762. if not(is_shortstring(rd) or is_char(rd)) then
  1763. inserttypeconv(right,cshortstringtype);
  1764. end;
  1765. else
  1766. internalerror(2005101);
  1767. end;
  1768. end
  1769. else
  1770. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1771. end
  1772. { implicit pointer object type comparison }
  1773. else if is_implicit_pointer_object_type(rd) or is_implicit_pointer_object_type(ld) then
  1774. begin
  1775. if (nodetype in [equaln,unequaln]) then
  1776. begin
  1777. if is_implicit_pointer_object_type(rd) and is_implicit_pointer_object_type(ld) then
  1778. begin
  1779. if def_is_related(tobjectdef(rd),tobjectdef(ld)) then
  1780. inserttypeconv(right,left.resultdef)
  1781. else
  1782. inserttypeconv(left,right.resultdef);
  1783. end
  1784. else if is_implicit_pointer_object_type(rd) then
  1785. inserttypeconv(left,right.resultdef)
  1786. else
  1787. inserttypeconv(right,left.resultdef);
  1788. end
  1789. else
  1790. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1791. end
  1792. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  1793. begin
  1794. if (nodetype in [equaln,unequaln]) then
  1795. begin
  1796. if def_is_related(tobjectdef(tclassrefdef(rd).pointeddef),
  1797. tobjectdef(tclassrefdef(ld).pointeddef)) then
  1798. inserttypeconv(right,left.resultdef)
  1799. else
  1800. inserttypeconv(left,right.resultdef);
  1801. end
  1802. else
  1803. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1804. end
  1805. { allow comparison with nil pointer }
  1806. else if is_implicit_pointer_object_type(rd) or (rd.typ=classrefdef) then
  1807. begin
  1808. if (nodetype in [equaln,unequaln]) then
  1809. inserttypeconv(left,right.resultdef)
  1810. else
  1811. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1812. end
  1813. else if is_implicit_pointer_object_type(ld) or (ld.typ=classrefdef) then
  1814. begin
  1815. if (nodetype in [equaln,unequaln]) then
  1816. inserttypeconv(right,left.resultdef)
  1817. else
  1818. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1819. end
  1820. { support procvar=nil,procvar<>nil }
  1821. else if ((ld.typ=procvardef) and (rt=niln)) or
  1822. ((rd.typ=procvardef) and (lt=niln)) then
  1823. begin
  1824. if not(nodetype in [equaln,unequaln]) then
  1825. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1826. { find proc field in methodpointer record }
  1827. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1828. if not assigned(hsym) then
  1829. internalerror(200412043);
  1830. { For methodpointers compare only tmethodpointer.proc }
  1831. if (rd.typ=procvardef) and
  1832. (not tprocvardef(rd).is_addressonly) then
  1833. begin
  1834. right:=csubscriptnode.create(
  1835. hsym,
  1836. ctypeconvnode.create_internal(right,methodpointertype));
  1837. typecheckpass(right);
  1838. end;
  1839. if (ld.typ=procvardef) and
  1840. (not tprocvardef(ld).is_addressonly) then
  1841. begin
  1842. left:=csubscriptnode.create(
  1843. hsym,
  1844. ctypeconvnode.create_internal(left,methodpointertype));
  1845. typecheckpass(left);
  1846. end;
  1847. end
  1848. { support dynamicarray=nil,dynamicarray<>nil }
  1849. else if (is_dynamic_array(ld) and (rt=niln)) or
  1850. (is_dynamic_array(rd) and (lt=niln)) or
  1851. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1852. begin
  1853. if not(nodetype in [equaln,unequaln]) then
  1854. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1855. end
  1856. {$ifdef SUPPORT_MMX}
  1857. { mmx support, this must be before the zero based array
  1858. check }
  1859. else if (cs_mmx in current_settings.localswitches) and
  1860. is_mmx_able_array(ld) and
  1861. is_mmx_able_array(rd) and
  1862. equal_defs(ld,rd) then
  1863. begin
  1864. case nodetype of
  1865. addn,subn,xorn,orn,andn:
  1866. ;
  1867. { mul is a little bit restricted }
  1868. muln:
  1869. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1870. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1871. else
  1872. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1873. end;
  1874. end
  1875. {$endif SUPPORT_MMX}
  1876. { vector support, this must be before the zero based array
  1877. check }
  1878. else if (cs_support_vectors in current_settings.globalswitches) and
  1879. is_vector(ld) and
  1880. is_vector(rd) and
  1881. equal_defs(ld,rd) then
  1882. begin
  1883. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  1884. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1885. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  1886. resultdef:=left.resultdef;
  1887. end
  1888. { this is a little bit dangerous, also the left type }
  1889. { pointer to should be checked! This broke the mmx support }
  1890. else if (rd.typ=pointerdef) or
  1891. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  1892. begin
  1893. if is_zero_based_array(rd) then
  1894. begin
  1895. resultdef:=getpointerdef(tarraydef(rd).elementdef);
  1896. inserttypeconv(right,resultdef);
  1897. end
  1898. else
  1899. resultdef:=right.resultdef;
  1900. inserttypeconv(left,tpointerdef(right.resultdef).pointer_arithmetic_int_type);
  1901. if nodetype=addn then
  1902. begin
  1903. if (rt=niln) then
  1904. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,'NIL');
  1905. if not(cs_extsyntax in current_settings.moduleswitches) or
  1906. (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
  1907. not(cs_pointermath in current_settings.localswitches) and
  1908. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  1909. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1910. if (rd.typ=pointerdef) and
  1911. (tpointerdef(rd).pointeddef.size>1) then
  1912. begin
  1913. left:=caddnode.create(muln,left,
  1914. cordconstnode.create(tpointerdef(rd).pointeddef.size,tpointerdef(right.resultdef).pointer_arithmetic_int_type,true));
  1915. typecheckpass(left);
  1916. end;
  1917. end
  1918. else
  1919. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1920. end
  1921. else if (ld.typ=pointerdef) or
  1922. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  1923. begin
  1924. if is_zero_based_array(ld) then
  1925. begin
  1926. resultdef:=getpointerdef(tarraydef(ld).elementdef);
  1927. inserttypeconv(left,resultdef);
  1928. end
  1929. else
  1930. resultdef:=left.resultdef;
  1931. inserttypeconv(right,tpointerdef(left.resultdef).pointer_arithmetic_int_type);
  1932. if nodetype in [addn,subn] then
  1933. begin
  1934. if (lt=niln) then
  1935. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),'NIL',rd.typename);
  1936. if not(cs_extsyntax in current_settings.moduleswitches) or
  1937. (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
  1938. not(cs_pointermath in current_settings.localswitches) and
  1939. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  1940. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1941. if (ld.typ=pointerdef) then
  1942. begin
  1943. if is_big_untyped_addrnode(left) then
  1944. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1945. if (tpointerdef(ld).pointeddef.size>1) then
  1946. begin
  1947. right:=caddnode.create(muln,right,
  1948. cordconstnode.create(tpointerdef(ld).pointeddef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  1949. typecheckpass(right);
  1950. end
  1951. end else
  1952. if is_zero_based_array(ld) and
  1953. (tarraydef(ld).elementdef.size>1) then
  1954. begin
  1955. right:=caddnode.create(muln,right,
  1956. cordconstnode.create(tarraydef(ld).elementdef.size,tpointerdef(left.resultdef).pointer_arithmetic_int_type,true));
  1957. typecheckpass(right);
  1958. end;
  1959. end
  1960. else
  1961. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1962. end
  1963. else if (rd.typ=procvardef) and
  1964. (ld.typ=procvardef) and
  1965. equal_defs(rd,ld) then
  1966. begin
  1967. if (nodetype in [equaln,unequaln]) then
  1968. begin
  1969. if tprocvardef(rd).is_addressonly then
  1970. begin
  1971. inserttypeconv_internal(right,voidpointertype);
  1972. inserttypeconv_internal(left,voidpointertype);
  1973. end
  1974. else
  1975. begin
  1976. { find proc field in methodpointer record }
  1977. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1978. if not assigned(hsym) then
  1979. internalerror(200412043);
  1980. { Compare tmehodpointer(left).proc }
  1981. right:=csubscriptnode.create(
  1982. hsym,
  1983. ctypeconvnode.create_internal(right,methodpointertype));
  1984. typecheckpass(right);
  1985. left:=csubscriptnode.create(
  1986. hsym,
  1987. ctypeconvnode.create_internal(left,methodpointertype));
  1988. typecheckpass(left);
  1989. end;
  1990. end
  1991. else
  1992. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1993. end
  1994. { enums }
  1995. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  1996. begin
  1997. if allowenumop(nodetype) then
  1998. inserttypeconv(right,left.resultdef)
  1999. else
  2000. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  2001. end
  2002. { generic conversion, this is for error recovery }
  2003. else
  2004. begin
  2005. inserttypeconv(left,sinttype);
  2006. inserttypeconv(right,sinttype);
  2007. end;
  2008. if cmp_of_disjunct_ranges(res) then
  2009. begin
  2010. if res then
  2011. CGMessage(type_w_comparison_always_true)
  2012. else
  2013. CGMessage(type_w_comparison_always_false);
  2014. end;
  2015. { set resultdef if not already done }
  2016. if not assigned(resultdef) then
  2017. begin
  2018. case nodetype of
  2019. ltn,lten,gtn,gten,equaln,unequaln :
  2020. resultdef:=pasbool8type;
  2021. slashn :
  2022. resultdef:=resultrealdef;
  2023. addn:
  2024. begin
  2025. { for strings, return is always a 255 char string }
  2026. if is_shortstring(left.resultdef) then
  2027. resultdef:=cshortstringtype
  2028. else
  2029. { for ansistrings set resultdef to assignment left node
  2030. if it is an assignment and left node expects ansistring }
  2031. if is_ansistring(left.resultdef) and
  2032. assigned(aktassignmentnode) and
  2033. (aktassignmentnode.right=self) and
  2034. is_ansistring(aktassignmentnode.left.resultdef) then
  2035. resultdef:=aktassignmentnode.left.resultdef
  2036. else
  2037. resultdef:=left.resultdef;
  2038. end;
  2039. else
  2040. resultdef:=left.resultdef;
  2041. end;
  2042. end;
  2043. { when the result is currency we need some extra code for
  2044. multiplication and division. this should not be done when
  2045. the muln or slashn node is created internally }
  2046. if not(nf_is_currency in flags) and
  2047. is_currency(resultdef) then
  2048. begin
  2049. case nodetype of
  2050. slashn :
  2051. begin
  2052. { slashn will only work with floats }
  2053. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  2054. include(hp.flags,nf_is_currency);
  2055. result:=hp;
  2056. end;
  2057. muln :
  2058. begin
  2059. if s64currencytype.typ=floatdef then
  2060. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  2061. else
  2062. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  2063. include(hp.flags,nf_is_currency);
  2064. result:=hp
  2065. end;
  2066. end;
  2067. end;
  2068. if not codegenerror and
  2069. not assigned(result) then
  2070. result:=simplify(false);
  2071. end;
  2072. function taddnode.first_addstring: tnode;
  2073. const
  2074. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  2075. var
  2076. p: tnode;
  2077. newstatement : tstatementnode;
  2078. tempnode (*,tempnode2*) : ttempcreatenode;
  2079. cmpfuncname: string;
  2080. para: tcallparanode;
  2081. begin
  2082. result:=nil;
  2083. { when we get here, we are sure that both the left and the right }
  2084. { node are both strings of the same stringtype (JM) }
  2085. case nodetype of
  2086. addn:
  2087. begin
  2088. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  2089. begin
  2090. result:=right;
  2091. left.free;
  2092. left:=nil;
  2093. right:=nil;
  2094. exit;
  2095. end;
  2096. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  2097. begin
  2098. result:=left;
  2099. left:=nil;
  2100. right.free;
  2101. right:=nil;
  2102. exit;
  2103. end;
  2104. { create the call to the concat routine both strings as arguments }
  2105. if assigned(aktassignmentnode) and
  2106. (aktassignmentnode.right=self) and
  2107. (aktassignmentnode.left.resultdef=resultdef) and
  2108. valid_for_var(aktassignmentnode.left,false) then
  2109. begin
  2110. para:=ccallparanode.create(
  2111. right,
  2112. ccallparanode.create(
  2113. left,
  2114. ccallparanode.create(aktassignmentnode.left.getcopy,nil)
  2115. )
  2116. );
  2117. if is_ansistring(resultdef) then
  2118. para:=ccallparanode.create(
  2119. cordconstnode.create(
  2120. { don't use getparaencoding(), we have to know
  2121. when the result is rawbytestring }
  2122. tstringdef(resultdef).encoding,
  2123. u16inttype,
  2124. true
  2125. ),
  2126. para
  2127. );
  2128. result:=ccallnode.createintern(
  2129. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2130. para
  2131. );
  2132. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2133. firstpass(result);
  2134. end
  2135. else
  2136. begin
  2137. result:=internalstatements(newstatement);
  2138. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2139. addstatement(newstatement,tempnode);
  2140. { initialize the temp, since it will be passed to a
  2141. var-parameter (and finalization, which is performed by the
  2142. ttempcreate node and which takes care of the initialization
  2143. on native targets, is a noop on managed VM targets) }
  2144. if (target_info.system in systems_managed_vm) and
  2145. is_managed_type(resultdef) then
  2146. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  2147. false,
  2148. ccallparanode.create(genintconstnode(0),
  2149. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  2150. para:=ccallparanode.create(
  2151. right,
  2152. ccallparanode.create(
  2153. left,
  2154. ccallparanode.create(ctemprefnode.create(tempnode),nil)
  2155. )
  2156. );
  2157. if is_ansistring(resultdef) then
  2158. para:=ccallparanode.create(
  2159. cordconstnode.create(
  2160. { don't use getparaencoding(), we have to know
  2161. when the result is rawbytestring }
  2162. tstringdef(resultdef).encoding,
  2163. u16inttype,
  2164. true
  2165. ),
  2166. para
  2167. );
  2168. addstatement(
  2169. newstatement,
  2170. ccallnode.createintern(
  2171. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2172. para
  2173. )
  2174. );
  2175. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2176. addstatement(newstatement,ctemprefnode.create(tempnode));
  2177. end;
  2178. { we reused the arguments }
  2179. left := nil;
  2180. right := nil;
  2181. end;
  2182. ltn,lten,gtn,gten,equaln,unequaln :
  2183. begin
  2184. { generate better code for comparison with empty string, we
  2185. only need to compare the length with 0 }
  2186. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  2187. { windows widestrings are too complicated to be handled optimized }
  2188. not(is_widestring(left.resultdef) and (target_info.system in systems_windows)) and
  2189. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  2190. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  2191. begin
  2192. { switch so that the constant is always on the right }
  2193. if left.nodetype = stringconstn then
  2194. begin
  2195. p := left;
  2196. left := right;
  2197. right := p;
  2198. nodetype:=swap_relation[nodetype];
  2199. end;
  2200. if is_shortstring(left.resultdef) or
  2201. (nodetype in [gtn,gten,ltn,lten]) or
  2202. (target_info.system in systems_managed_vm) then
  2203. { compare the length with 0 }
  2204. result := caddnode.create(nodetype,
  2205. cinlinenode.create(in_length_x,false,left),
  2206. cordconstnode.create(0,s32inttype,false))
  2207. else
  2208. begin
  2209. (*
  2210. if is_widestring(left.resultdef) and
  2211. (target_info.system in system_windows) then
  2212. begin
  2213. { windows like widestrings requires that we also check the length }
  2214. result:=internalstatements(newstatement);
  2215. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2216. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2217. addstatement(newstatement,tempnode);
  2218. addstatement(newstatement,tempnode2);
  2219. { poor man's cse }
  2220. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2221. ctypeconvnode.create_internal(left,voidpointertype))
  2222. );
  2223. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  2224. caddnode.create(orn,
  2225. caddnode.create(nodetype,
  2226. ctemprefnode.create(tempnode),
  2227. cpointerconstnode.create(0,voidpointertype)
  2228. ),
  2229. caddnode.create(nodetype,
  2230. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  2231. cordconstnode.create(0,s32inttype,false)
  2232. )
  2233. )
  2234. ));
  2235. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2236. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  2237. addstatement(newstatement,ctemprefnode.create(tempnode2));
  2238. end
  2239. else
  2240. *)
  2241. begin
  2242. { compare the pointer with nil (for ansistrings etc), }
  2243. { faster than getting the length (JM) }
  2244. result:= caddnode.create(nodetype,
  2245. ctypeconvnode.create_internal(left,voidpointertype),
  2246. cpointerconstnode.create(0,voidpointertype));
  2247. end;
  2248. end;
  2249. { left is reused }
  2250. left := nil;
  2251. { right isn't }
  2252. right.free;
  2253. right := nil;
  2254. exit;
  2255. end;
  2256. { no string constant -> call compare routine }
  2257. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  2258. { for equality checks use optimized version }
  2259. if nodetype in [equaln,unequaln] then
  2260. cmpfuncname := cmpfuncname + '_equal';
  2261. result := ccallnode.createintern(cmpfuncname,
  2262. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2263. { and compare its result with 0 according to the original operator }
  2264. result := caddnode.create(nodetype,result,
  2265. cordconstnode.create(0,s32inttype,false));
  2266. left := nil;
  2267. right := nil;
  2268. end;
  2269. end;
  2270. end;
  2271. function taddnode.first_addset : tnode;
  2272. procedure call_varset_helper(const n : string);
  2273. var
  2274. newstatement : tstatementnode;
  2275. temp : ttempcreatenode;
  2276. begin
  2277. { add two var sets }
  2278. result:=internalstatements(newstatement);
  2279. { create temp for result }
  2280. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2281. addstatement(newstatement,temp);
  2282. addstatement(newstatement,ccallnode.createintern(n,
  2283. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2284. ccallparanode.create(ctemprefnode.create(temp),
  2285. ccallparanode.create(right,
  2286. ccallparanode.create(left,nil)))))
  2287. );
  2288. { remove reused parts from original node }
  2289. left:=nil;
  2290. right:=nil;
  2291. { the last statement should return the value as
  2292. location and type, this is done be referencing the
  2293. temp and converting it first from a persistent temp to
  2294. normal temp }
  2295. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2296. addstatement(newstatement,ctemprefnode.create(temp));
  2297. end;
  2298. var
  2299. procname: string[31];
  2300. tempn: tnode;
  2301. newstatement : tstatementnode;
  2302. temp : ttempcreatenode;
  2303. begin
  2304. result:=nil;
  2305. case nodetype of
  2306. equaln,unequaln,lten,gten:
  2307. begin
  2308. case nodetype of
  2309. equaln,unequaln:
  2310. procname := 'fpc_varset_comp_sets';
  2311. lten,gten:
  2312. begin
  2313. procname := 'fpc_varset_contains_sets';
  2314. { (left >= right) = (right <= left) }
  2315. if nodetype = gten then
  2316. begin
  2317. tempn := left;
  2318. left := right;
  2319. right := tempn;
  2320. end;
  2321. end;
  2322. else
  2323. internalerror(2013112911);
  2324. end;
  2325. result := ccallnode.createinternres(procname,
  2326. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2327. ccallparanode.create(right,
  2328. ccallparanode.create(left,nil))),resultdef);
  2329. { left and right are reused as parameters }
  2330. left := nil;
  2331. right := nil;
  2332. { for an unequaln, we have to negate the result of comp_sets }
  2333. if nodetype = unequaln then
  2334. result := cnotnode.create(result);
  2335. end;
  2336. addn:
  2337. begin
  2338. { optimize first loading of a set }
  2339. if (right.nodetype=setelementn) and
  2340. not(assigned(tsetelementnode(right).right)) and
  2341. is_emptyset(left) then
  2342. begin
  2343. result:=internalstatements(newstatement);
  2344. { create temp for result }
  2345. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2346. addstatement(newstatement,temp);
  2347. { adjust for set base }
  2348. tsetelementnode(right).left:=caddnode.create(subn,
  2349. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2350. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2351. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  2352. ccallparanode.create(ctemprefnode.create(temp),
  2353. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2354. ccallparanode.create(tsetelementnode(right).left,nil))))
  2355. );
  2356. { the last statement should return the value as
  2357. location and type, this is done be referencing the
  2358. temp and converting it first from a persistent temp to
  2359. normal temp }
  2360. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2361. addstatement(newstatement,ctemprefnode.create(temp));
  2362. tsetelementnode(right).left := nil;
  2363. end
  2364. else
  2365. begin
  2366. if right.nodetype=setelementn then
  2367. begin
  2368. result:=internalstatements(newstatement);
  2369. { create temp for result }
  2370. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2371. addstatement(newstatement,temp);
  2372. { adjust for set base }
  2373. tsetelementnode(right).left:=caddnode.create(subn,
  2374. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2375. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2376. { add a range or a single element? }
  2377. if assigned(tsetelementnode(right).right) then
  2378. begin
  2379. { adjust for set base }
  2380. tsetelementnode(right).right:=caddnode.create(subn,
  2381. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  2382. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2383. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  2384. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2385. ccallparanode.create(tsetelementnode(right).right,
  2386. ccallparanode.create(tsetelementnode(right).left,
  2387. ccallparanode.create(ctemprefnode.create(temp),
  2388. ccallparanode.create(left,nil))))))
  2389. );
  2390. end
  2391. else
  2392. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  2393. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2394. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2395. ccallparanode.create(ctemprefnode.create(temp),
  2396. ccallparanode.create(left,nil)))))
  2397. );
  2398. { remove reused parts from original node }
  2399. tsetelementnode(right).right:=nil;
  2400. tsetelementnode(right).left:=nil;
  2401. left:=nil;
  2402. { the last statement should return the value as
  2403. location and type, this is done be referencing the
  2404. temp and converting it first from a persistent temp to
  2405. normal temp }
  2406. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2407. addstatement(newstatement,ctemprefnode.create(temp));
  2408. end
  2409. else
  2410. call_varset_helper('fpc_varset_add_sets');
  2411. end
  2412. end;
  2413. subn:
  2414. call_varset_helper('fpc_varset_sub_sets');
  2415. symdifn:
  2416. call_varset_helper('fpc_varset_symdif_sets');
  2417. muln:
  2418. call_varset_helper('fpc_varset_mul_sets');
  2419. else
  2420. internalerror(200609241);
  2421. end;
  2422. end;
  2423. function taddnode.use_generic_mul32to64: boolean;
  2424. begin
  2425. result := true;
  2426. end;
  2427. function taddnode.use_generic_mul64bit: boolean;
  2428. begin
  2429. result := true;
  2430. end;
  2431. function taddnode.try_make_mul32to64: boolean;
  2432. function canbe32bitint(v: tconstexprint): boolean;
  2433. begin
  2434. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  2435. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))))
  2436. end;
  2437. function is_32bitordconst(n: tnode): boolean;
  2438. begin
  2439. result := (n.nodetype = ordconstn) and
  2440. canbe32bitint(tordconstnode(n).value);
  2441. end;
  2442. function is_32to64typeconv(n: tnode): boolean;
  2443. begin
  2444. result := (n.nodetype = typeconvn) and
  2445. is_integer(ttypeconvnode(n).left.resultdef) and
  2446. not is_64bit(ttypeconvnode(n).left.resultdef);
  2447. end;
  2448. var
  2449. temp: tnode;
  2450. begin
  2451. result := false;
  2452. if is_32to64typeconv(left) and
  2453. (is_32bitordconst(right) or
  2454. is_32to64typeconv(right) and
  2455. ((is_signed(ttypeconvnode(left).left.resultdef) =
  2456. is_signed(ttypeconvnode(right).left.resultdef)) or
  2457. (is_signed(ttypeconvnode(left).left.resultdef) and
  2458. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit])))) then
  2459. begin
  2460. temp := ttypeconvnode(left).left;
  2461. ttypeconvnode(left).left := nil;
  2462. left.free;
  2463. left := temp;
  2464. if (right.nodetype = typeconvn) then
  2465. begin
  2466. temp := ttypeconvnode(right).left;
  2467. ttypeconvnode(right).left := nil;
  2468. right.free;
  2469. right := temp;
  2470. end;
  2471. if (is_signed(left.resultdef)) then
  2472. begin
  2473. inserttypeconv_internal(left,s32inttype);
  2474. inserttypeconv_internal(right,s32inttype);
  2475. end
  2476. else
  2477. begin
  2478. inserttypeconv_internal(left,u32inttype);
  2479. inserttypeconv_internal(right,u32inttype);
  2480. end;
  2481. firstpass(left);
  2482. firstpass(right);
  2483. result := true;
  2484. end;
  2485. end;
  2486. function taddnode.first_add64bitint: tnode;
  2487. var
  2488. procname: string[31];
  2489. temp: tnode;
  2490. power: longint;
  2491. begin
  2492. result := nil;
  2493. { create helper calls mul }
  2494. if nodetype <> muln then
  2495. exit;
  2496. { make sure that if there is a constant, that it's on the right }
  2497. if left.nodetype = ordconstn then
  2498. begin
  2499. temp := right;
  2500. right := left;
  2501. left := temp;
  2502. end;
  2503. { can we use a shift instead of a mul? }
  2504. if not (cs_check_overflow in current_settings.localswitches) and
  2505. (right.nodetype = ordconstn) and
  2506. ispowerof2(tordconstnode(right).value,power) then
  2507. begin
  2508. tordconstnode(right).value := power;
  2509. result := cshlshrnode.create(shln,left,right);
  2510. { left and right are reused }
  2511. left := nil;
  2512. right := nil;
  2513. { return firstpassed new node }
  2514. exit;
  2515. end;
  2516. if try_make_mul32to64 then
  2517. begin
  2518. { if the code generator can handle 32 to 64-bit muls, we're done here }
  2519. if not use_generic_mul32to64 then
  2520. exit;
  2521. { this uses the same criteria for signedness as the 32 to 64-bit mul
  2522. handling in the i386 code generator }
  2523. if is_signed(left.resultdef) and is_signed(right.resultdef) then
  2524. procname := 'fpc_mul_longint_to_int64'
  2525. else
  2526. procname := 'fpc_mul_dword_to_qword';
  2527. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  2528. result := ccallnode.createintern(procname,right);
  2529. left := nil;
  2530. right := nil;
  2531. end
  2532. else
  2533. begin
  2534. { can full 64-bit multiplication be handled inline? }
  2535. if not use_generic_mul64bit then
  2536. begin
  2537. { generic handling replaces this node with call to fpc_mul_int64,
  2538. whose result is int64 }
  2539. if is_currency(resultdef) then
  2540. resultdef:=s64inttype;
  2541. exit;
  2542. end;
  2543. { when currency is used set the result of the
  2544. parameters to s64bit, so they are not converted }
  2545. if is_currency(resultdef) then
  2546. begin
  2547. left.resultdef:=s64inttype;
  2548. right.resultdef:=s64inttype;
  2549. end;
  2550. { otherwise, create the parameters for the helper }
  2551. right := ccallparanode.create(
  2552. cordconstnode.create(ord(cs_check_overflow in current_settings.localswitches),pasbool8type,true),
  2553. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2554. left := nil;
  2555. { only qword needs the unsigned code, the
  2556. signed code is also used for currency }
  2557. if is_signed(resultdef) then
  2558. procname := 'fpc_mul_int64'
  2559. else
  2560. procname := 'fpc_mul_qword';
  2561. result := ccallnode.createintern(procname,right);
  2562. right := nil;
  2563. end;
  2564. end;
  2565. function taddnode.first_addpointer: tnode;
  2566. begin
  2567. result:=nil;
  2568. expectloc:=LOC_REGISTER;
  2569. end;
  2570. function taddnode.first_cmppointer: tnode;
  2571. begin
  2572. result:=nil;
  2573. expectloc:=LOC_FLAGS;
  2574. end;
  2575. function taddnode.first_addfloat : tnode;
  2576. var
  2577. procname: string[31];
  2578. { do we need to reverse the result ? }
  2579. notnode : boolean;
  2580. fdef : tdef;
  2581. begin
  2582. result := nil;
  2583. notnode := false;
  2584. fdef := nil;
  2585. { In non-emulation mode, real opcodes are
  2586. emitted for floating point values.
  2587. }
  2588. if not ((cs_fp_emulation in current_settings.moduleswitches)
  2589. {$ifdef cpufpemu}
  2590. or (current_settings.fputype=fpu_soft)
  2591. {$endif cpufpemu}
  2592. ) then
  2593. exit;
  2594. if not(target_info.system in systems_wince) then
  2595. begin
  2596. case tfloatdef(left.resultdef).floattype of
  2597. s32real:
  2598. begin
  2599. fdef:=search_system_type('FLOAT32REC').typedef;
  2600. procname:='float32';
  2601. end;
  2602. s64real:
  2603. begin
  2604. fdef:=search_system_type('FLOAT64').typedef;
  2605. procname:='float64';
  2606. end;
  2607. {!!! not yet implemented
  2608. s128real:
  2609. }
  2610. else
  2611. internalerror(2005082601);
  2612. end;
  2613. case nodetype of
  2614. addn:
  2615. procname:=procname+'_add';
  2616. muln:
  2617. procname:=procname+'_mul';
  2618. subn:
  2619. procname:=procname+'_sub';
  2620. slashn:
  2621. procname:=procname+'_div';
  2622. ltn:
  2623. procname:=procname+'_lt';
  2624. lten:
  2625. procname:=procname+'_le';
  2626. gtn:
  2627. begin
  2628. procname:=procname+'_lt';
  2629. swapleftright;
  2630. end;
  2631. gten:
  2632. begin
  2633. procname:=procname+'_le';
  2634. swapleftright;
  2635. end;
  2636. equaln:
  2637. procname:=procname+'_eq';
  2638. unequaln:
  2639. begin
  2640. procname:=procname+'_eq';
  2641. notnode:=true;
  2642. end;
  2643. else
  2644. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2645. end;
  2646. end
  2647. else
  2648. begin
  2649. case nodetype of
  2650. addn:
  2651. procname:='ADD';
  2652. muln:
  2653. procname:='MUL';
  2654. subn:
  2655. procname:='SUB';
  2656. slashn:
  2657. procname:='DIV';
  2658. ltn:
  2659. procname:='LT';
  2660. lten:
  2661. procname:='LE';
  2662. gtn:
  2663. procname:='GT';
  2664. gten:
  2665. procname:='GE';
  2666. equaln:
  2667. procname:='EQ';
  2668. unequaln:
  2669. procname:='NE';
  2670. else
  2671. begin
  2672. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2673. exit;
  2674. end;
  2675. end;
  2676. case tfloatdef(left.resultdef).floattype of
  2677. s32real:
  2678. begin
  2679. procname:=procname+'S';
  2680. if nodetype in [addn,muln,subn,slashn] then
  2681. procname:=lower(procname);
  2682. end;
  2683. s64real:
  2684. procname:=procname+'D';
  2685. {!!! not yet implemented
  2686. s128real:
  2687. }
  2688. else
  2689. internalerror(2005082602);
  2690. end;
  2691. end;
  2692. { cast softfpu result? }
  2693. if not(target_info.system in systems_wince) then
  2694. begin
  2695. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2696. resultdef:=pasbool8type;
  2697. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  2698. ctypeconvnode.create_internal(right,fdef),
  2699. ccallparanode.create(
  2700. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  2701. end
  2702. else
  2703. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  2704. ccallparanode.create(left,nil)));
  2705. left:=nil;
  2706. right:=nil;
  2707. { do we need to reverse the result }
  2708. if notnode then
  2709. result:=cnotnode.create(result);
  2710. end;
  2711. function taddnode.pass_1 : tnode;
  2712. var
  2713. {$ifdef addstringopt}
  2714. hp : tnode;
  2715. {$endif addstringopt}
  2716. rd,ld : tdef;
  2717. i,i2 : longint;
  2718. lt,rt : tnodetype;
  2719. {$ifdef cpuneedsmulhelper}
  2720. procname : string[32];
  2721. {$endif cpuneedsmulhelper}
  2722. begin
  2723. result:=nil;
  2724. { Can we optimize multiple string additions into a single call?
  2725. This need to be done on a complete tree to detect the multiple
  2726. add nodes and is therefor done before the subtrees are processed }
  2727. if canbemultistringadd(self) then
  2728. begin
  2729. result:=genmultistringadd(self);
  2730. exit;
  2731. end;
  2732. { first do the two subtrees }
  2733. firstpass(left);
  2734. firstpass(right);
  2735. if codegenerror then
  2736. exit;
  2737. { load easier access variables }
  2738. rd:=right.resultdef;
  2739. ld:=left.resultdef;
  2740. rt:=right.nodetype;
  2741. lt:=left.nodetype;
  2742. { int/int gives real/real! }
  2743. if nodetype=slashn then
  2744. begin
  2745. {$ifdef cpufpemu}
  2746. result:=first_addfloat;
  2747. if assigned(result) then
  2748. exit;
  2749. {$endif cpufpemu}
  2750. expectloc:=LOC_FPUREGISTER;
  2751. end
  2752. { if both are orddefs then check sub types }
  2753. else if (ld.typ=orddef) and (rd.typ=orddef) then
  2754. begin
  2755. { optimize multiplacation by a power of 2 }
  2756. if not(cs_check_overflow in current_settings.localswitches) and
  2757. (nodetype = muln) and
  2758. (((left.nodetype = ordconstn) and
  2759. ispowerof2(tordconstnode(left).value,i)) or
  2760. ((right.nodetype = ordconstn) and
  2761. ispowerof2(tordconstnode(right).value,i2))) then
  2762. begin
  2763. if ((left.nodetype = ordconstn) and
  2764. ispowerof2(tordconstnode(left).value,i)) then
  2765. begin
  2766. tordconstnode(left).value := i;
  2767. result := cshlshrnode.create(shln,right,left);
  2768. end
  2769. else
  2770. begin
  2771. tordconstnode(right).value := i2;
  2772. result := cshlshrnode.create(shln,left,right);
  2773. end;
  2774. result.resultdef := resultdef;
  2775. left := nil;
  2776. right := nil;
  2777. exit;
  2778. end;
  2779. { 2 booleans ? }
  2780. if is_boolean(ld) and is_boolean(rd) then
  2781. begin
  2782. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  2783. (nf_short_bool in flags)) and
  2784. (nodetype in [andn,orn]) then
  2785. expectloc:=LOC_JUMP
  2786. else
  2787. begin
  2788. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2789. expectloc:=LOC_FLAGS
  2790. else
  2791. expectloc:=LOC_REGISTER;
  2792. end;
  2793. end
  2794. else
  2795. { Both are chars? only convert to shortstrings for addn }
  2796. if is_char(ld) then
  2797. begin
  2798. if nodetype=addn then
  2799. internalerror(200103291);
  2800. expectloc:=LOC_FLAGS;
  2801. end
  2802. {$ifndef cpu64bitalu}
  2803. { is there a 64 bit type ? }
  2804. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  2805. begin
  2806. result := first_add64bitint;
  2807. if assigned(result) then
  2808. exit;
  2809. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2810. expectloc:=LOC_REGISTER
  2811. else
  2812. expectloc:=LOC_JUMP;
  2813. end
  2814. {$endif cpu64bitalu}
  2815. { generic 32bit conversion }
  2816. else
  2817. begin
  2818. {$ifdef cpuneedsmulhelper}
  2819. if (nodetype=muln) and not(torddef(resultdef).ordtype in [u8bit,s8bit{$ifdef cpu16bitalu},u16bit,s16bit{$endif}]) then
  2820. begin
  2821. result := nil;
  2822. case torddef(resultdef).ordtype of
  2823. s16bit:
  2824. procname := 'fpc_mul_integer';
  2825. u16bit:
  2826. procname := 'fpc_mul_word';
  2827. s32bit:
  2828. procname := 'fpc_mul_longint';
  2829. u32bit:
  2830. procname := 'fpc_mul_dword';
  2831. else
  2832. internalerror(2011022301);
  2833. end;
  2834. result := ccallnode.createintern(procname,
  2835. ccallparanode.create(cordconstnode.create(ord(cs_check_overflow in current_settings.localswitches),pasbool8type,false),
  2836. ccallparanode.create(right,
  2837. ccallparanode.create(left,nil))));
  2838. left := nil;
  2839. right := nil;
  2840. firstpass(result);
  2841. exit;
  2842. end;
  2843. {$endif cpuneedsmulhelper}
  2844. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2845. expectloc:=LOC_REGISTER
  2846. else if torddef(ld).size>sizeof(aint) then
  2847. expectloc:=LOC_JUMP
  2848. else
  2849. expectloc:=LOC_FLAGS;
  2850. end;
  2851. end
  2852. { left side a setdef, must be before string processing,
  2853. else array constructor can be seen as array of char (PFV) }
  2854. else if (ld.typ=setdef) then
  2855. begin
  2856. { small sets are handled inline by the compiler.
  2857. small set doesn't have support for adding ranges }
  2858. if is_smallset(ld) and
  2859. not(
  2860. (right.nodetype=setelementn) and
  2861. assigned(tsetelementnode(right).right)
  2862. ) then
  2863. begin
  2864. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2865. expectloc:=LOC_FLAGS
  2866. else
  2867. expectloc:=LOC_REGISTER;
  2868. end
  2869. else
  2870. begin
  2871. result := first_addset;
  2872. if assigned(result) then
  2873. exit;
  2874. expectloc:=LOC_CREFERENCE;
  2875. end;
  2876. end
  2877. { compare pchar by addresses like BP/Delphi }
  2878. else if is_pchar(ld) then
  2879. begin
  2880. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2881. result:=first_addpointer
  2882. else
  2883. result:=first_cmppointer;
  2884. end
  2885. { is one of the operands a string }
  2886. else if (ld.typ=stringdef) then
  2887. begin
  2888. if is_widestring(ld) then
  2889. begin
  2890. { this is only for add, the comparisaion is handled later }
  2891. expectloc:=LOC_REGISTER;
  2892. end
  2893. else if is_unicodestring(ld) then
  2894. begin
  2895. { this is only for add, the comparisaion is handled later }
  2896. expectloc:=LOC_REGISTER;
  2897. end
  2898. else if is_ansistring(ld) then
  2899. begin
  2900. { this is only for add, the comparisaion is handled later }
  2901. expectloc:=LOC_REGISTER;
  2902. end
  2903. else if is_longstring(ld) then
  2904. begin
  2905. { this is only for add, the comparisaion is handled later }
  2906. expectloc:=LOC_REFERENCE;
  2907. end
  2908. else
  2909. begin
  2910. {$ifdef addstringopt}
  2911. { can create a call which isn't handled by callparatemp }
  2912. if canbeaddsstringcharoptnode(self) then
  2913. begin
  2914. hp := genaddsstringcharoptnode(self);
  2915. pass_1 := hp;
  2916. exit;
  2917. end
  2918. else
  2919. {$endif addstringopt}
  2920. begin
  2921. { Fix right to be shortstring }
  2922. if is_char(right.resultdef) then
  2923. begin
  2924. inserttypeconv(right,cshortstringtype);
  2925. firstpass(right);
  2926. end;
  2927. end;
  2928. {$ifdef addstringopt}
  2929. { can create a call which isn't handled by callparatemp }
  2930. if canbeaddsstringcsstringoptnode(self) then
  2931. begin
  2932. hp := genaddsstringcsstringoptnode(self);
  2933. pass_1 := hp;
  2934. exit;
  2935. end;
  2936. {$endif addstringopt}
  2937. end;
  2938. { otherwise, let addstring convert everything }
  2939. result := first_addstring;
  2940. exit;
  2941. end
  2942. { is one a real float ? }
  2943. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  2944. begin
  2945. {$ifdef cpufpemu}
  2946. result:=first_addfloat;
  2947. if assigned(result) then
  2948. exit;
  2949. {$endif cpufpemu}
  2950. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2951. expectloc:=LOC_FPUREGISTER
  2952. else
  2953. expectloc:=LOC_FLAGS;
  2954. end
  2955. { pointer comperation and subtraction }
  2956. else if (ld.typ=pointerdef) then
  2957. begin
  2958. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2959. result:=first_addpointer
  2960. else
  2961. result:=first_cmppointer;
  2962. end
  2963. else if is_implicit_pointer_object_type(ld) then
  2964. begin
  2965. expectloc:=LOC_FLAGS;
  2966. end
  2967. else if (ld.typ=classrefdef) then
  2968. begin
  2969. expectloc:=LOC_FLAGS;
  2970. end
  2971. { support procvar=nil,procvar<>nil }
  2972. else if ((ld.typ=procvardef) and (rt=niln)) or
  2973. ((rd.typ=procvardef) and (lt=niln)) then
  2974. begin
  2975. expectloc:=LOC_FLAGS;
  2976. end
  2977. {$ifdef SUPPORT_MMX}
  2978. { mmx support, this must be before the zero based array
  2979. check }
  2980. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  2981. is_mmx_able_array(rd) then
  2982. begin
  2983. expectloc:=LOC_MMXREGISTER;
  2984. end
  2985. {$endif SUPPORT_MMX}
  2986. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  2987. begin
  2988. result:=first_addpointer;
  2989. end
  2990. else if (rd.typ=procvardef) and
  2991. (ld.typ=procvardef) and
  2992. equal_defs(rd,ld) then
  2993. begin
  2994. expectloc:=LOC_FLAGS;
  2995. end
  2996. else if (ld.typ=enumdef) then
  2997. begin
  2998. expectloc:=LOC_FLAGS;
  2999. end
  3000. {$ifdef SUPPORT_MMX}
  3001. else if (cs_mmx in current_settings.localswitches) and
  3002. is_mmx_able_array(ld) and
  3003. is_mmx_able_array(rd) then
  3004. begin
  3005. expectloc:=LOC_MMXREGISTER;
  3006. end
  3007. {$endif SUPPORT_MMX}
  3008. { the general solution is to convert to 32 bit int }
  3009. else
  3010. begin
  3011. expectloc:=LOC_REGISTER;
  3012. end;
  3013. end;
  3014. {$ifdef state_tracking}
  3015. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  3016. var factval:Tnode;
  3017. begin
  3018. track_state_pass:=false;
  3019. if left.track_state_pass(exec_known) then
  3020. begin
  3021. track_state_pass:=true;
  3022. left.resultdef:=nil;
  3023. do_typecheckpass(left);
  3024. end;
  3025. factval:=aktstate.find_fact(left);
  3026. if factval<>nil then
  3027. begin
  3028. track_state_pass:=true;
  3029. left.destroy;
  3030. left:=factval.getcopy;
  3031. end;
  3032. if right.track_state_pass(exec_known) then
  3033. begin
  3034. track_state_pass:=true;
  3035. right.resultdef:=nil;
  3036. do_typecheckpass(right);
  3037. end;
  3038. factval:=aktstate.find_fact(right);
  3039. if factval<>nil then
  3040. begin
  3041. track_state_pass:=true;
  3042. right.destroy;
  3043. right:=factval.getcopy;
  3044. end;
  3045. end;
  3046. {$endif}
  3047. end.