nadd.pas 136 KB

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