nadd.pas 136 KB

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