nadd.pas 126 KB

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