nadd.pas 124 KB

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