nadd.pas 138 KB

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