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