nadd.pas 137 KB

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