nadd.pas 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123
  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. {$if defined(i386) or defined(i8086)}
  1009. if not(current_settings.fputype=fpu_x87) then
  1010. resultrealdef:=s64floattype
  1011. else
  1012. resultrealdef:=pbestrealtype^;
  1013. {$endif i386 or i8086}
  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. { is there a larger int? }
  1350. else if is_oversizedint(rd) or is_oversizedint(ld) then
  1351. begin
  1352. nd:=get_common_intdef(torddef(ld),torddef(rd),false);
  1353. inserttypeconv(right,nd);
  1354. inserttypeconv(left,nd);
  1355. end
  1356. { is there a native unsigned int? }
  1357. else if is_nativeuint(rd) or is_nativeuint(ld) then
  1358. begin
  1359. { convert positive constants to uinttype }
  1360. if (not is_nativeuint(ld)) and
  1361. is_constintnode(left) and
  1362. (tordconstnode(left).value >= 0) then
  1363. inserttypeconv(left,uinttype);
  1364. if (not is_nativeuint(rd)) and
  1365. is_constintnode(right) and
  1366. (tordconstnode(right).value >= 0) then
  1367. inserttypeconv(right,uinttype);
  1368. { when one of the operand is signed or the operation is subn then perform
  1369. the operation in a larger signed type, can't use rd/ld here because there
  1370. could be already typeconvs inserted.
  1371. This is compatible with the code below for other unsigned types (PFV) }
  1372. if is_signed(left.resultdef) or
  1373. is_signed(right.resultdef) or
  1374. (nodetype=subn) then
  1375. begin
  1376. if nodetype<>subn then
  1377. CGMessage(type_h_mixed_signed_unsigned);
  1378. { mark as internal in case added for a subn, so }
  1379. { ttypeconvnode.simplify can remove the larger }
  1380. { typecast again if semantically correct. Even }
  1381. { if we could detect that here already, we }
  1382. { mustn't do it here because that would change }
  1383. { overload choosing behaviour etc. The code in }
  1384. { ncnv.pas is run after that is already decided }
  1385. if (not is_signed(left.resultdef) and
  1386. not is_signed(right.resultdef)) or
  1387. (nodetype in [orn,xorn]) then
  1388. include(flags,nf_internal);
  1389. { get next larger signed int type }
  1390. nd:=get_common_intdef(torddef(sinttype),torddef(uinttype),false);
  1391. inserttypeconv(left,nd);
  1392. inserttypeconv(right,nd);
  1393. end
  1394. else
  1395. begin
  1396. if not is_nativeuint(left.resultdef) then
  1397. inserttypeconv(left,uinttype);
  1398. if not is_nativeuint(right.resultdef) then
  1399. inserttypeconv(right,uinttype);
  1400. end;
  1401. end
  1402. { generic ord conversion is sinttype }
  1403. else
  1404. begin
  1405. { When there is a signed type or there is a minus operation
  1406. we convert to signed int. Otherwise (both are unsigned) we keep
  1407. the result also unsigned. This is compatible with Delphi (PFV) }
  1408. if is_signed(ld) or
  1409. is_signed(rd) or
  1410. (nodetype=subn) then
  1411. begin
  1412. inserttypeconv(right,sinttype);
  1413. inserttypeconv(left,sinttype);
  1414. end
  1415. else
  1416. begin
  1417. inserttypeconv(right,uinttype);
  1418. inserttypeconv(left,uinttype);
  1419. end;
  1420. end;
  1421. end
  1422. { if both are floatdefs, conversion is already done before constant folding }
  1423. else if (ld.typ=floatdef) then
  1424. begin
  1425. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  1426. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1427. end
  1428. { left side a setdef, must be before string processing,
  1429. else array constructor can be seen as array of char (PFV) }
  1430. else if (ld.typ=setdef) then
  1431. begin
  1432. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1433. CGMessage(type_e_set_operation_unknown);
  1434. { right must either be a set or a set element }
  1435. if (rd.typ<>setdef) and
  1436. (rt<>setelementn) then
  1437. CGMessage(type_e_mismatch)
  1438. { Make operands the same setdef. If one's elementtype fits }
  1439. { entirely inside the other's, pick the one with the largest }
  1440. { range. Otherwise create a new setdef with a range which }
  1441. { can contain both. }
  1442. else if not(equal_defs(ld,rd)) then
  1443. begin
  1444. { note: ld cannot be an empty set with elementdef=nil in }
  1445. { case right is not a set, arrayconstructor_to_set takes }
  1446. { care of that }
  1447. { 1: rd is a set with an assigned elementdef, and ld is }
  1448. { either an empty set without elementdef or a set whose }
  1449. { elementdef fits in rd's elementdef -> convert to rd }
  1450. if ((rd.typ=setdef) and
  1451. assigned(tsetdef(rd).elementdef) and
  1452. (not assigned(tsetdef(ld).elementdef) or
  1453. is_in_limit(ld,rd))) then
  1454. inserttypeconv(left,rd)
  1455. { 2: rd is either an empty set without elementdef or a set }
  1456. { whose elementdef fits in ld's elementdef, or a set }
  1457. { element whose def fits in ld's elementdef -> convert }
  1458. { to ld. ld's elementdef can't be nil here, is caught }
  1459. { previous case and "note:" above }
  1460. else if ((rd.typ=setdef) and
  1461. (not assigned(tsetdef(rd).elementdef) or
  1462. is_in_limit(rd,ld))) or
  1463. ((rd.typ<>setdef) and
  1464. is_in_limit(rd,tsetdef(ld).elementdef)) then
  1465. if (rd.typ=setdef) then
  1466. inserttypeconv(right,ld)
  1467. else
  1468. inserttypeconv(right,tsetdef(ld).elementdef)
  1469. { 3: otherwise create setdef which encompasses both, taking }
  1470. { into account empty sets without elementdef }
  1471. else
  1472. begin
  1473. if assigned(tsetdef(ld).elementdef) then
  1474. begin
  1475. llow:=tsetdef(ld).setbase;
  1476. lhigh:=tsetdef(ld).setmax;
  1477. end;
  1478. if (rd.typ=setdef) then
  1479. if assigned(tsetdef(rd).elementdef) then
  1480. begin
  1481. rlow:=tsetdef(rd).setbase;
  1482. rhigh:=tsetdef(rd).setmax;
  1483. end
  1484. else
  1485. begin
  1486. { ld's elementdef must have been valid }
  1487. rlow:=llow;
  1488. rhigh:=lhigh;
  1489. end
  1490. else
  1491. getrange(rd,rlow,rhigh);
  1492. if not assigned(tsetdef(ld).elementdef) then
  1493. begin
  1494. llow:=rlow;
  1495. lhigh:=rhigh;
  1496. end;
  1497. nd:=tsetdef.create(tsetdef(ld).elementdef,min(llow,rlow).svalue,max(lhigh,rhigh).svalue);
  1498. inserttypeconv(left,nd);
  1499. if (rd.typ=setdef) then
  1500. inserttypeconv(right,nd)
  1501. else
  1502. inserttypeconv(right,tsetdef(nd).elementdef);
  1503. end;
  1504. end;
  1505. end
  1506. { pointer comparision and subtraction }
  1507. else if (
  1508. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  1509. ) or
  1510. { compare/add pchar to variable (not stringconst) char arrays
  1511. by addresses like BP/Delphi }
  1512. (
  1513. (nodetype in [equaln,unequaln,subn,addn]) and
  1514. (
  1515. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1516. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1517. )
  1518. ) then
  1519. begin
  1520. { convert char array to pointer }
  1521. if is_chararray(rd) then
  1522. begin
  1523. inserttypeconv(right,charpointertype);
  1524. rd:=right.resultdef;
  1525. end
  1526. else if is_chararray(ld) then
  1527. begin
  1528. inserttypeconv(left,charpointertype);
  1529. ld:=left.resultdef;
  1530. end;
  1531. case nodetype of
  1532. equaln,unequaln :
  1533. begin
  1534. if is_voidpointer(right.resultdef) then
  1535. inserttypeconv(right,left.resultdef)
  1536. else if is_voidpointer(left.resultdef) then
  1537. inserttypeconv(left,right.resultdef)
  1538. else if not(equal_defs(ld,rd)) then
  1539. IncompatibleTypes(ld,rd);
  1540. { now that the type checking is done, convert both to charpointer, }
  1541. { because methodpointers are 8 bytes even though only the first 4 }
  1542. { bytes must be compared. This can happen here if we are in }
  1543. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1544. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1545. { optimized away, since the result already was a voidpointer, so }
  1546. { use a charpointer instead (JM) }
  1547. {$if defined(jvm)}
  1548. inserttypeconv_internal(left,java_jlobject);
  1549. inserttypeconv_internal(right,java_jlobject);
  1550. {$elseif defined(i8086)}
  1551. { we don't have a charfarpointertype yet, so for far pointers we use bytefarpointertype }
  1552. if is_farpointer(left.resultdef) then
  1553. inserttypeconv_internal(left,bytefarpointertype)
  1554. else
  1555. inserttypeconv_internal(left,charpointertype);
  1556. if is_farpointer(right.resultdef) then
  1557. inserttypeconv_internal(right,bytefarpointertype)
  1558. else
  1559. inserttypeconv_internal(right,charpointertype);
  1560. {$else}
  1561. inserttypeconv_internal(left,charpointertype);
  1562. inserttypeconv_internal(right,charpointertype);
  1563. {$endif jvm}
  1564. end;
  1565. ltn,lten,gtn,gten:
  1566. begin
  1567. if (cs_extsyntax in current_settings.moduleswitches) then
  1568. begin
  1569. if is_voidpointer(right.resultdef) then
  1570. inserttypeconv(right,left.resultdef)
  1571. else if is_voidpointer(left.resultdef) then
  1572. inserttypeconv(left,right.resultdef)
  1573. else if not(equal_defs(ld,rd)) then
  1574. IncompatibleTypes(ld,rd);
  1575. end
  1576. else
  1577. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1578. end;
  1579. subn:
  1580. begin
  1581. if (cs_extsyntax in current_settings.moduleswitches) then
  1582. begin
  1583. if is_voidpointer(right.resultdef) then
  1584. begin
  1585. if is_big_untyped_addrnode(right) then
  1586. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1587. inserttypeconv(right,left.resultdef)
  1588. end
  1589. else if is_voidpointer(left.resultdef) then
  1590. inserttypeconv(left,right.resultdef)
  1591. else if not(equal_defs(ld,rd)) then
  1592. IncompatibleTypes(ld,rd);
  1593. end
  1594. else
  1595. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1596. if not(nf_has_pointerdiv in flags) and
  1597. (tpointerdef(rd).pointeddef.size>1) then
  1598. begin
  1599. hp:=getcopy;
  1600. include(hp.flags,nf_has_pointerdiv);
  1601. result:=cmoddivnode.create(divn,hp,cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,false));
  1602. end;
  1603. resultdef:=sinttype;
  1604. exit;
  1605. end;
  1606. else
  1607. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1608. end;
  1609. end
  1610. { is one of the operands a string?,
  1611. chararrays are also handled as strings (after conversion), also take
  1612. care of chararray+chararray and chararray+char.
  1613. Note: Must be done after pointerdef+pointerdef has been checked, else
  1614. pchar is converted to string }
  1615. else if (rd.typ=stringdef) or
  1616. (ld.typ=stringdef) or
  1617. { stringconstn's can be arraydefs }
  1618. (lt=stringconstn) or
  1619. (rt=stringconstn) or
  1620. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1621. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1622. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1623. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1624. begin
  1625. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1626. begin
  1627. { Is there a unicodestring? }
  1628. if is_unicodestring(rd) or is_unicodestring(ld) or
  1629. ((m_default_unicodestring in current_settings.modeswitches) and
  1630. (cs_refcountedstrings in current_settings.localswitches) and
  1631. (
  1632. is_pwidechar(rd) or is_widechararray(rd) or is_open_widechararray(rd) or (lt = stringconstn) or
  1633. is_pwidechar(ld) or is_widechararray(ld) or is_open_widechararray(ld) or (rt = stringconstn)
  1634. )
  1635. ) then
  1636. strtype:=st_unicodestring
  1637. else
  1638. { Is there a widestring? }
  1639. if is_widestring(rd) or is_widestring(ld) or
  1640. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1641. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1642. strtype:=st_widestring
  1643. else
  1644. if is_ansistring(rd) or is_ansistring(ld) or
  1645. ((cs_refcountedstrings in current_settings.localswitches) and
  1646. //todo: Move some of this to longstring's then they are implemented?
  1647. (
  1648. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or (lt = stringconstn) or
  1649. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld) or (rt = stringconstn)
  1650. )
  1651. ) then
  1652. strtype:=st_ansistring
  1653. else
  1654. if is_longstring(rd) or is_longstring(ld) then
  1655. strtype:=st_longstring
  1656. else
  1657. begin
  1658. { TODO: todo: add a warning/hint here if one converting a too large array}
  1659. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1660. Note: Delphi halts with error if "array [0..xx] of char"
  1661. is assigned to ShortString and string length is less
  1662. then array size }
  1663. strtype:= st_shortstring;
  1664. end;
  1665. // Now convert nodes to common string type
  1666. case strtype of
  1667. st_widestring :
  1668. begin
  1669. if not(is_widestring(rd)) then
  1670. inserttypeconv(right,cwidestringtype);
  1671. if not(is_widestring(ld)) then
  1672. inserttypeconv(left,cwidestringtype);
  1673. end;
  1674. st_unicodestring :
  1675. begin
  1676. if not(is_unicodestring(rd)) then
  1677. inserttypeconv(right,cunicodestringtype);
  1678. if not(is_unicodestring(ld)) then
  1679. inserttypeconv(left,cunicodestringtype);
  1680. end;
  1681. st_ansistring :
  1682. begin
  1683. { use same code page if possible (don't force same code
  1684. page in case both are ansistrings with code page <>
  1685. CP_NONE, since then data loss can occur (the ansistring
  1686. helpers will convert them at run time to an encoding
  1687. that can represent both encodings) }
  1688. if is_ansistring(ld) and
  1689. (tstringdef(ld).encoding<>0) and
  1690. (tstringdef(ld).encoding<>globals.CP_NONE) and
  1691. (not is_ansistring(rd) or
  1692. (tstringdef(rd).encoding=0) or
  1693. (tstringdef(rd).encoding=globals.CP_NONE)) then
  1694. inserttypeconv(right,ld)
  1695. else if is_ansistring(rd) and
  1696. (tstringdef(rd).encoding<>0) and
  1697. (tstringdef(rd).encoding<>globals.CP_NONE) and
  1698. (not is_ansistring(ld) or
  1699. (tstringdef(ld).encoding=0) or
  1700. (tstringdef(ld).encoding=globals.CP_NONE)) then
  1701. inserttypeconv(left,rd)
  1702. else
  1703. begin
  1704. if not is_ansistring(ld) then
  1705. inserttypeconv(left,getansistringdef);
  1706. if not is_ansistring(rd) then
  1707. inserttypeconv(right,getansistringdef);
  1708. end;
  1709. end;
  1710. st_longstring :
  1711. begin
  1712. if not(is_longstring(rd)) then
  1713. inserttypeconv(right,clongstringtype);
  1714. if not(is_longstring(ld)) then
  1715. inserttypeconv(left,clongstringtype);
  1716. end;
  1717. st_shortstring :
  1718. begin
  1719. if not(is_shortstring(ld)) then
  1720. inserttypeconv(left,cshortstringtype);
  1721. { don't convert char, that can be handled by the optimized node }
  1722. if not(is_shortstring(rd) or is_char(rd)) then
  1723. inserttypeconv(right,cshortstringtype);
  1724. end;
  1725. else
  1726. internalerror(2005101);
  1727. end;
  1728. end
  1729. else
  1730. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1731. end
  1732. { implicit pointer object type comparison }
  1733. else if is_implicit_pointer_object_type(rd) or is_implicit_pointer_object_type(ld) then
  1734. begin
  1735. if (nodetype in [equaln,unequaln]) then
  1736. begin
  1737. if is_implicit_pointer_object_type(rd) and is_implicit_pointer_object_type(ld) then
  1738. begin
  1739. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1740. inserttypeconv(right,left.resultdef)
  1741. else
  1742. inserttypeconv(left,right.resultdef);
  1743. end
  1744. else if is_implicit_pointer_object_type(rd) then
  1745. inserttypeconv(left,right.resultdef)
  1746. else
  1747. inserttypeconv(right,left.resultdef);
  1748. end
  1749. else
  1750. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1751. end
  1752. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  1753. begin
  1754. if (nodetype in [equaln,unequaln]) then
  1755. begin
  1756. if tobjectdef(tclassrefdef(rd).pointeddef).is_related(
  1757. tobjectdef(tclassrefdef(ld).pointeddef)) then
  1758. inserttypeconv(right,left.resultdef)
  1759. else
  1760. inserttypeconv(left,right.resultdef);
  1761. end
  1762. else
  1763. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1764. end
  1765. { allow comparison with nil pointer }
  1766. else if is_implicit_pointer_object_type(rd) or (rd.typ=classrefdef) then
  1767. begin
  1768. if (nodetype in [equaln,unequaln]) then
  1769. inserttypeconv(left,right.resultdef)
  1770. else
  1771. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1772. end
  1773. else if is_implicit_pointer_object_type(ld) or (ld.typ=classrefdef) then
  1774. begin
  1775. if (nodetype in [equaln,unequaln]) then
  1776. inserttypeconv(right,left.resultdef)
  1777. else
  1778. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1779. end
  1780. { support procvar=nil,procvar<>nil }
  1781. else if ((ld.typ=procvardef) and (rt=niln)) or
  1782. ((rd.typ=procvardef) and (lt=niln)) then
  1783. begin
  1784. if not(nodetype in [equaln,unequaln]) then
  1785. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1786. { find proc field in methodpointer record }
  1787. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1788. if not assigned(hsym) then
  1789. internalerror(200412043);
  1790. { For methodpointers compare only tmethodpointer.proc }
  1791. if (rd.typ=procvardef) and
  1792. (not tprocvardef(rd).is_addressonly) then
  1793. begin
  1794. right:=csubscriptnode.create(
  1795. hsym,
  1796. ctypeconvnode.create_internal(right,methodpointertype));
  1797. typecheckpass(right);
  1798. end;
  1799. if (ld.typ=procvardef) and
  1800. (not tprocvardef(ld).is_addressonly) then
  1801. begin
  1802. left:=csubscriptnode.create(
  1803. hsym,
  1804. ctypeconvnode.create_internal(left,methodpointertype));
  1805. typecheckpass(left);
  1806. end;
  1807. end
  1808. { support dynamicarray=nil,dynamicarray<>nil }
  1809. else if (is_dynamic_array(ld) and (rt=niln)) or
  1810. (is_dynamic_array(rd) and (lt=niln)) or
  1811. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1812. begin
  1813. if not(nodetype in [equaln,unequaln]) then
  1814. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1815. end
  1816. {$ifdef SUPPORT_MMX}
  1817. { mmx support, this must be before the zero based array
  1818. check }
  1819. else if (cs_mmx in current_settings.localswitches) and
  1820. is_mmx_able_array(ld) and
  1821. is_mmx_able_array(rd) and
  1822. equal_defs(ld,rd) then
  1823. begin
  1824. case nodetype of
  1825. addn,subn,xorn,orn,andn:
  1826. ;
  1827. { mul is a little bit restricted }
  1828. muln:
  1829. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1830. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1831. else
  1832. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1833. end;
  1834. end
  1835. {$endif SUPPORT_MMX}
  1836. { vector support, this must be before the zero based array
  1837. check }
  1838. else if (cs_support_vectors in current_settings.globalswitches) and
  1839. is_vector(ld) and
  1840. is_vector(rd) and
  1841. equal_defs(ld,rd) then
  1842. begin
  1843. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  1844. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1845. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  1846. resultdef:=left.resultdef;
  1847. end
  1848. { this is a little bit dangerous, also the left type }
  1849. { pointer to should be checked! This broke the mmx support }
  1850. else if (rd.typ=pointerdef) or
  1851. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  1852. begin
  1853. if is_zero_based_array(rd) then
  1854. begin
  1855. resultdef:=getpointerdef(tarraydef(rd).elementdef);
  1856. inserttypeconv(right,resultdef);
  1857. end
  1858. else
  1859. resultdef:=right.resultdef;
  1860. inserttypeconv(left,sinttype);
  1861. if nodetype=addn then
  1862. begin
  1863. if not(cs_extsyntax in current_settings.moduleswitches) or
  1864. (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
  1865. not(cs_pointermath in current_settings.localswitches) and
  1866. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  1867. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1868. if (rd.typ=pointerdef) and
  1869. (tpointerdef(rd).pointeddef.size>1) then
  1870. begin
  1871. left:=caddnode.create(muln,left,
  1872. cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,true));
  1873. typecheckpass(left);
  1874. end;
  1875. end
  1876. else
  1877. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1878. end
  1879. else if (ld.typ=pointerdef) or
  1880. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  1881. begin
  1882. if is_zero_based_array(ld) then
  1883. begin
  1884. resultdef:=getpointerdef(tarraydef(ld).elementdef);
  1885. inserttypeconv(left,resultdef);
  1886. end
  1887. else
  1888. resultdef:=left.resultdef;
  1889. inserttypeconv(right,sinttype);
  1890. if nodetype in [addn,subn] then
  1891. begin
  1892. if (lt=niln) then
  1893. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),'NIL',rd.typename);
  1894. if not(cs_extsyntax in current_settings.moduleswitches) or
  1895. (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
  1896. not(cs_pointermath in current_settings.localswitches) and
  1897. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  1898. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1899. if (ld.typ=pointerdef) then
  1900. begin
  1901. if is_big_untyped_addrnode(left) then
  1902. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1903. if (tpointerdef(ld).pointeddef.size>1) then
  1904. begin
  1905. right:=caddnode.create(muln,right,
  1906. cordconstnode.create(tpointerdef(ld).pointeddef.size,sinttype,true));
  1907. typecheckpass(right);
  1908. end
  1909. end else
  1910. if is_zero_based_array(ld) and
  1911. (tarraydef(ld).elementdef.size>1) then
  1912. begin
  1913. right:=caddnode.create(muln,right,
  1914. cordconstnode.create(tarraydef(ld).elementdef.size,sinttype,true));
  1915. typecheckpass(right);
  1916. end;
  1917. end
  1918. else
  1919. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1920. end
  1921. else if (rd.typ=procvardef) and
  1922. (ld.typ=procvardef) and
  1923. equal_defs(rd,ld) then
  1924. begin
  1925. if (nodetype in [equaln,unequaln]) then
  1926. begin
  1927. if tprocvardef(rd).is_addressonly then
  1928. begin
  1929. inserttypeconv_internal(right,voidpointertype);
  1930. inserttypeconv_internal(left,voidpointertype);
  1931. end
  1932. else
  1933. begin
  1934. { find proc field in methodpointer record }
  1935. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1936. if not assigned(hsym) then
  1937. internalerror(200412043);
  1938. { Compare tmehodpointer(left).proc }
  1939. right:=csubscriptnode.create(
  1940. hsym,
  1941. ctypeconvnode.create_internal(right,methodpointertype));
  1942. typecheckpass(right);
  1943. left:=csubscriptnode.create(
  1944. hsym,
  1945. ctypeconvnode.create_internal(left,methodpointertype));
  1946. typecheckpass(left);
  1947. end;
  1948. end
  1949. else
  1950. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1951. end
  1952. { enums }
  1953. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  1954. begin
  1955. if allowenumop(nodetype) then
  1956. inserttypeconv(right,left.resultdef)
  1957. else
  1958. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1959. end
  1960. { generic conversion, this is for error recovery }
  1961. else
  1962. begin
  1963. inserttypeconv(left,sinttype);
  1964. inserttypeconv(right,sinttype);
  1965. end;
  1966. if cmp_of_disjunct_ranges(res) then
  1967. begin
  1968. if res then
  1969. CGMessage(type_w_comparison_always_true)
  1970. else
  1971. CGMessage(type_w_comparison_always_false);
  1972. end;
  1973. { set resultdef if not already done }
  1974. if not assigned(resultdef) then
  1975. begin
  1976. case nodetype of
  1977. ltn,lten,gtn,gten,equaln,unequaln :
  1978. resultdef:=pasbool8type;
  1979. slashn :
  1980. resultdef:=resultrealdef;
  1981. addn:
  1982. begin
  1983. { for strings, return is always a 255 char string }
  1984. if is_shortstring(left.resultdef) then
  1985. resultdef:=cshortstringtype
  1986. else
  1987. { for ansistrings set resultdef to assignment left node
  1988. if it is an assignment and left node expects ansistring }
  1989. if is_ansistring(left.resultdef) and
  1990. assigned(aktassignmentnode) and
  1991. (aktassignmentnode.right=self) and
  1992. is_ansistring(aktassignmentnode.left.resultdef) then
  1993. resultdef:=aktassignmentnode.left.resultdef
  1994. else
  1995. resultdef:=left.resultdef;
  1996. end;
  1997. else
  1998. resultdef:=left.resultdef;
  1999. end;
  2000. end;
  2001. { when the result is currency we need some extra code for
  2002. multiplication and division. this should not be done when
  2003. the muln or slashn node is created internally }
  2004. if not(nf_is_currency in flags) and
  2005. is_currency(resultdef) then
  2006. begin
  2007. case nodetype of
  2008. slashn :
  2009. begin
  2010. { slashn will only work with floats }
  2011. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  2012. include(hp.flags,nf_is_currency);
  2013. result:=hp;
  2014. end;
  2015. muln :
  2016. begin
  2017. if s64currencytype.typ=floatdef then
  2018. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  2019. else
  2020. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  2021. include(hp.flags,nf_is_currency);
  2022. result:=hp
  2023. end;
  2024. end;
  2025. end;
  2026. if not codegenerror and
  2027. not assigned(result) then
  2028. result:=simplify(false);
  2029. end;
  2030. function taddnode.first_addstring: tnode;
  2031. const
  2032. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  2033. var
  2034. p: tnode;
  2035. newstatement : tstatementnode;
  2036. tempnode (*,tempnode2*) : ttempcreatenode;
  2037. cmpfuncname: string;
  2038. para: tcallparanode;
  2039. begin
  2040. { when we get here, we are sure that both the left and the right }
  2041. { node are both strings of the same stringtype (JM) }
  2042. case nodetype of
  2043. addn:
  2044. begin
  2045. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  2046. begin
  2047. result:=right;
  2048. left.free;
  2049. left:=nil;
  2050. right:=nil;
  2051. exit;
  2052. end;
  2053. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  2054. begin
  2055. result:=left;
  2056. left:=nil;
  2057. right.free;
  2058. right:=nil;
  2059. exit;
  2060. end;
  2061. { create the call to the concat routine both strings as arguments }
  2062. if assigned(aktassignmentnode) and
  2063. (aktassignmentnode.right=self) and
  2064. (aktassignmentnode.left.resultdef=resultdef) and
  2065. valid_for_var(aktassignmentnode.left,false) then
  2066. begin
  2067. para:=ccallparanode.create(
  2068. right,
  2069. ccallparanode.create(
  2070. left,
  2071. ccallparanode.create(aktassignmentnode.left.getcopy,nil)
  2072. )
  2073. );
  2074. if is_ansistring(resultdef) then
  2075. para:=ccallparanode.create(
  2076. cordconstnode.create(
  2077. { don't use getparaencoding(), we have to know
  2078. when the result is rawbytestring }
  2079. tstringdef(resultdef).encoding,
  2080. u16inttype,
  2081. true
  2082. ),
  2083. para
  2084. );
  2085. result:=ccallnode.createintern(
  2086. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2087. para
  2088. );
  2089. include(aktassignmentnode.flags,nf_assign_done_in_right);
  2090. firstpass(result);
  2091. end
  2092. else
  2093. begin
  2094. result:=internalstatements(newstatement);
  2095. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2096. addstatement(newstatement,tempnode);
  2097. { initialize the temp, since it will be passed to a
  2098. var-parameter (and finalization, which is performed by the
  2099. ttempcreate node and which takes care of the initialization
  2100. on native targets, is a noop on managed VM targets) }
  2101. if (target_info.system in systems_managed_vm) and
  2102. is_managed_type(resultdef) then
  2103. addstatement(newstatement,cinlinenode.create(in_setlength_x,
  2104. false,
  2105. ccallparanode.create(genintconstnode(0),
  2106. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  2107. para:=ccallparanode.create(
  2108. right,
  2109. ccallparanode.create(
  2110. left,
  2111. ccallparanode.create(ctemprefnode.create(tempnode),nil)
  2112. )
  2113. );
  2114. if is_ansistring(resultdef) then
  2115. para:=ccallparanode.create(
  2116. cordconstnode.create(
  2117. { don't use getparaencoding(), we have to know
  2118. when the result is rawbytestring }
  2119. tstringdef(resultdef).encoding,
  2120. u16inttype,
  2121. true
  2122. ),
  2123. para
  2124. );
  2125. addstatement(
  2126. newstatement,
  2127. ccallnode.createintern(
  2128. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2129. para
  2130. )
  2131. );
  2132. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2133. addstatement(newstatement,ctemprefnode.create(tempnode));
  2134. end;
  2135. { we reused the arguments }
  2136. left := nil;
  2137. right := nil;
  2138. end;
  2139. ltn,lten,gtn,gten,equaln,unequaln :
  2140. begin
  2141. { generate better code for comparison with empty string, we
  2142. only need to compare the length with 0 }
  2143. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  2144. { windows widestrings are too complicated to be handled optimized }
  2145. not(is_widestring(left.resultdef) and (target_info.system in systems_windows)) and
  2146. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  2147. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  2148. begin
  2149. { switch so that the constant is always on the right }
  2150. if left.nodetype = stringconstn then
  2151. begin
  2152. p := left;
  2153. left := right;
  2154. right := p;
  2155. nodetype:=swap_relation[nodetype];
  2156. end;
  2157. if is_shortstring(left.resultdef) or
  2158. (nodetype in [gtn,gten,ltn,lten]) or
  2159. (target_info.system in systems_managed_vm) then
  2160. { compare the length with 0 }
  2161. result := caddnode.create(nodetype,
  2162. cinlinenode.create(in_length_x,false,left),
  2163. cordconstnode.create(0,s32inttype,false))
  2164. else
  2165. begin
  2166. (*
  2167. if is_widestring(left.resultdef) and
  2168. (target_info.system in system_windows) then
  2169. begin
  2170. { windows like widestrings requires that we also check the length }
  2171. result:=internalstatements(newstatement);
  2172. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2173. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2174. addstatement(newstatement,tempnode);
  2175. addstatement(newstatement,tempnode2);
  2176. { poor man's cse }
  2177. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2178. ctypeconvnode.create_internal(left,voidpointertype))
  2179. );
  2180. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  2181. caddnode.create(orn,
  2182. caddnode.create(nodetype,
  2183. ctemprefnode.create(tempnode),
  2184. cpointerconstnode.create(0,voidpointertype)
  2185. ),
  2186. caddnode.create(nodetype,
  2187. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  2188. cordconstnode.create(0,s32inttype,false)
  2189. )
  2190. )
  2191. ));
  2192. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2193. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  2194. addstatement(newstatement,ctemprefnode.create(tempnode2));
  2195. end
  2196. else
  2197. *)
  2198. begin
  2199. { compare the pointer with nil (for ansistrings etc), }
  2200. { faster than getting the length (JM) }
  2201. result:= caddnode.create(nodetype,
  2202. ctypeconvnode.create_internal(left,voidpointertype),
  2203. cpointerconstnode.create(0,voidpointertype));
  2204. end;
  2205. end;
  2206. { left is reused }
  2207. left := nil;
  2208. { right isn't }
  2209. right.free;
  2210. right := nil;
  2211. exit;
  2212. end;
  2213. { no string constant -> call compare routine }
  2214. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  2215. { for equality checks use optimized version }
  2216. if nodetype in [equaln,unequaln] then
  2217. cmpfuncname := cmpfuncname + '_equal';
  2218. result := ccallnode.createintern(cmpfuncname,
  2219. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2220. { and compare its result with 0 according to the original operator }
  2221. result := caddnode.create(nodetype,result,
  2222. cordconstnode.create(0,s32inttype,false));
  2223. left := nil;
  2224. right := nil;
  2225. end;
  2226. end;
  2227. end;
  2228. function taddnode.first_addset : tnode;
  2229. procedure call_varset_helper(const n : string);
  2230. var
  2231. newstatement : tstatementnode;
  2232. temp : ttempcreatenode;
  2233. begin
  2234. { add two var sets }
  2235. result:=internalstatements(newstatement);
  2236. { create temp for result }
  2237. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2238. addstatement(newstatement,temp);
  2239. addstatement(newstatement,ccallnode.createintern(n,
  2240. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2241. ccallparanode.create(ctemprefnode.create(temp),
  2242. ccallparanode.create(right,
  2243. ccallparanode.create(left,nil)))))
  2244. );
  2245. { remove reused parts from original node }
  2246. left:=nil;
  2247. right:=nil;
  2248. { the last statement should return the value as
  2249. location and type, this is done be referencing the
  2250. temp and converting it first from a persistent temp to
  2251. normal temp }
  2252. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2253. addstatement(newstatement,ctemprefnode.create(temp));
  2254. end;
  2255. var
  2256. procname: string[31];
  2257. tempn: tnode;
  2258. newstatement : tstatementnode;
  2259. temp : ttempcreatenode;
  2260. begin
  2261. result:=nil;
  2262. case nodetype of
  2263. equaln,unequaln,lten,gten:
  2264. begin
  2265. case nodetype of
  2266. equaln,unequaln:
  2267. procname := 'fpc_varset_comp_sets';
  2268. lten,gten:
  2269. begin
  2270. procname := 'fpc_varset_contains_sets';
  2271. { (left >= right) = (right <= left) }
  2272. if nodetype = gten then
  2273. begin
  2274. tempn := left;
  2275. left := right;
  2276. right := tempn;
  2277. end;
  2278. end;
  2279. end;
  2280. result := ccallnode.createinternres(procname,
  2281. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2282. ccallparanode.create(right,
  2283. ccallparanode.create(left,nil))),resultdef);
  2284. { left and right are reused as parameters }
  2285. left := nil;
  2286. right := nil;
  2287. { for an unequaln, we have to negate the result of comp_sets }
  2288. if nodetype = unequaln then
  2289. result := cnotnode.create(result);
  2290. end;
  2291. addn:
  2292. begin
  2293. { optimize first loading of a set }
  2294. if (right.nodetype=setelementn) and
  2295. not(assigned(tsetelementnode(right).right)) and
  2296. is_emptyset(left) then
  2297. begin
  2298. result:=internalstatements(newstatement);
  2299. { create temp for result }
  2300. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2301. addstatement(newstatement,temp);
  2302. { adjust for set base }
  2303. tsetelementnode(right).left:=caddnode.create(subn,
  2304. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2305. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2306. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  2307. ccallparanode.create(ctemprefnode.create(temp),
  2308. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2309. ccallparanode.create(tsetelementnode(right).left,nil))))
  2310. );
  2311. { the last statement should return the value as
  2312. location and type, this is done be referencing the
  2313. temp and converting it first from a persistent temp to
  2314. normal temp }
  2315. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2316. addstatement(newstatement,ctemprefnode.create(temp));
  2317. tsetelementnode(right).left := nil;
  2318. end
  2319. else
  2320. begin
  2321. if right.nodetype=setelementn then
  2322. begin
  2323. result:=internalstatements(newstatement);
  2324. { create temp for result }
  2325. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2326. addstatement(newstatement,temp);
  2327. { adjust for set base }
  2328. tsetelementnode(right).left:=caddnode.create(subn,
  2329. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2330. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2331. { add a range or a single element? }
  2332. if assigned(tsetelementnode(right).right) then
  2333. begin
  2334. { adjust for set base }
  2335. tsetelementnode(right).right:=caddnode.create(subn,
  2336. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  2337. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2338. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  2339. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2340. ccallparanode.create(tsetelementnode(right).right,
  2341. ccallparanode.create(tsetelementnode(right).left,
  2342. ccallparanode.create(ctemprefnode.create(temp),
  2343. ccallparanode.create(left,nil))))))
  2344. );
  2345. end
  2346. else
  2347. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  2348. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2349. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2350. ccallparanode.create(ctemprefnode.create(temp),
  2351. ccallparanode.create(left,nil)))))
  2352. );
  2353. { remove reused parts from original node }
  2354. tsetelementnode(right).right:=nil;
  2355. tsetelementnode(right).left:=nil;
  2356. left:=nil;
  2357. { the last statement should return the value as
  2358. location and type, this is done be referencing the
  2359. temp and converting it first from a persistent temp to
  2360. normal temp }
  2361. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2362. addstatement(newstatement,ctemprefnode.create(temp));
  2363. end
  2364. else
  2365. call_varset_helper('fpc_varset_add_sets');
  2366. end
  2367. end;
  2368. subn:
  2369. call_varset_helper('fpc_varset_sub_sets');
  2370. symdifn:
  2371. call_varset_helper('fpc_varset_symdif_sets');
  2372. muln:
  2373. call_varset_helper('fpc_varset_mul_sets');
  2374. else
  2375. internalerror(200609241);
  2376. end;
  2377. end;
  2378. function taddnode.use_generic_mul32to64: boolean;
  2379. begin
  2380. result := true;
  2381. end;
  2382. function taddnode.try_make_mul32to64: boolean;
  2383. function canbe32bitint(v: tconstexprint): boolean;
  2384. begin
  2385. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  2386. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))))
  2387. end;
  2388. var
  2389. temp: tnode;
  2390. begin
  2391. result := false;
  2392. if ((left.nodetype = typeconvn) and
  2393. is_integer(ttypeconvnode(left).left.resultdef) and
  2394. (not(torddef(ttypeconvnode(left).left.resultdef).ordtype in [u64bit,s64bit,scurrency])) and
  2395. (((right.nodetype = ordconstn) and canbe32bitint(tordconstnode(right).value)) or
  2396. ((right.nodetype = typeconvn) and
  2397. is_integer(ttypeconvnode(right).left.resultdef) and
  2398. not(torddef(ttypeconvnode(right).left.resultdef).ordtype in [u64bit,s64bit,scurrency])) and
  2399. ((is_signed(ttypeconvnode(left).left.resultdef) =
  2400. is_signed(ttypeconvnode(right).left.resultdef)) or
  2401. (is_signed(ttypeconvnode(left).left.resultdef) and
  2402. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit]))))) then
  2403. begin
  2404. temp := ttypeconvnode(left).left;
  2405. ttypeconvnode(left).left := nil;
  2406. left.free;
  2407. left := temp;
  2408. if (right.nodetype = typeconvn) then
  2409. begin
  2410. temp := ttypeconvnode(right).left;
  2411. ttypeconvnode(right).left := nil;
  2412. right.free;
  2413. right := temp;
  2414. end;
  2415. if (is_signed(left.resultdef)) then
  2416. begin
  2417. inserttypeconv_internal(left,s32inttype);
  2418. inserttypeconv_internal(right,s32inttype);
  2419. end
  2420. else
  2421. begin
  2422. inserttypeconv_internal(left,u32inttype);
  2423. inserttypeconv_internal(right,u32inttype);
  2424. end;
  2425. firstpass(left);
  2426. firstpass(right);
  2427. result := true;
  2428. end;
  2429. end;
  2430. function taddnode.first_add64bitint: tnode;
  2431. var
  2432. procname: string[31];
  2433. temp: tnode;
  2434. power: longint;
  2435. begin
  2436. result := nil;
  2437. { create helper calls mul }
  2438. if nodetype <> muln then
  2439. exit;
  2440. { make sure that if there is a constant, that it's on the right }
  2441. if left.nodetype = ordconstn then
  2442. begin
  2443. temp := right;
  2444. right := left;
  2445. left := temp;
  2446. end;
  2447. { can we use a shift instead of a mul? }
  2448. if not (cs_check_overflow in current_settings.localswitches) and
  2449. (right.nodetype = ordconstn) and
  2450. ispowerof2(tordconstnode(right).value,power) then
  2451. begin
  2452. tordconstnode(right).value := power;
  2453. result := cshlshrnode.create(shln,left,right);
  2454. { left and right are reused }
  2455. left := nil;
  2456. right := nil;
  2457. { return firstpassed new node }
  2458. exit;
  2459. end;
  2460. if not(use_generic_mul32to64) and
  2461. try_make_mul32to64 then
  2462. exit;
  2463. { when currency is used set the result of the
  2464. parameters to s64bit, so they are not converted }
  2465. if is_currency(resultdef) then
  2466. begin
  2467. left.resultdef:=s64inttype;
  2468. right.resultdef:=s64inttype;
  2469. end;
  2470. { otherwise, create the parameters for the helper }
  2471. right := ccallparanode.create(
  2472. cordconstnode.create(ord(cs_check_overflow in current_settings.localswitches),pasbool8type,true),
  2473. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2474. left := nil;
  2475. { only qword needs the unsigned code, the
  2476. signed code is also used for currency }
  2477. if is_signed(resultdef) then
  2478. procname := 'fpc_mul_int64'
  2479. else
  2480. procname := 'fpc_mul_qword';
  2481. result := ccallnode.createintern(procname,right);
  2482. right := nil;
  2483. end;
  2484. function taddnode.first_addfloat : tnode;
  2485. var
  2486. procname: string[31];
  2487. { do we need to reverse the result ? }
  2488. notnode : boolean;
  2489. fdef : tdef;
  2490. begin
  2491. result := nil;
  2492. notnode := false;
  2493. { In non-emulation mode, real opcodes are
  2494. emitted for floating point values.
  2495. }
  2496. if not ((cs_fp_emulation in current_settings.moduleswitches)
  2497. {$ifdef cpufpemu}
  2498. or (current_settings.fputype=fpu_soft)
  2499. {$endif cpufpemu}
  2500. ) then
  2501. exit;
  2502. if not(target_info.system in systems_wince) then
  2503. begin
  2504. case tfloatdef(left.resultdef).floattype of
  2505. s32real:
  2506. begin
  2507. fdef:=search_system_type('FLOAT32REC').typedef;
  2508. procname:='float32';
  2509. end;
  2510. s64real:
  2511. begin
  2512. fdef:=search_system_type('FLOAT64').typedef;
  2513. procname:='float64';
  2514. end;
  2515. {!!! not yet implemented
  2516. s128real:
  2517. }
  2518. else
  2519. internalerror(2005082601);
  2520. end;
  2521. case nodetype of
  2522. addn:
  2523. procname:=procname+'_add';
  2524. muln:
  2525. procname:=procname+'_mul';
  2526. subn:
  2527. procname:=procname+'_sub';
  2528. slashn:
  2529. procname:=procname+'_div';
  2530. ltn:
  2531. procname:=procname+'_lt';
  2532. lten:
  2533. procname:=procname+'_le';
  2534. gtn:
  2535. begin
  2536. procname:=procname+'_le';
  2537. notnode:=true;
  2538. end;
  2539. gten:
  2540. begin
  2541. procname:=procname+'_lt';
  2542. notnode:=true;
  2543. end;
  2544. equaln:
  2545. procname:=procname+'_eq';
  2546. unequaln:
  2547. begin
  2548. procname:=procname+'_eq';
  2549. notnode:=true;
  2550. end;
  2551. else
  2552. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2553. end;
  2554. end
  2555. else
  2556. begin
  2557. case nodetype of
  2558. addn:
  2559. procname:='ADD';
  2560. muln:
  2561. procname:='MUL';
  2562. subn:
  2563. procname:='SUB';
  2564. slashn:
  2565. procname:='DIV';
  2566. ltn:
  2567. procname:='LT';
  2568. lten:
  2569. procname:='LE';
  2570. gtn:
  2571. procname:='GT';
  2572. gten:
  2573. procname:='GE';
  2574. equaln:
  2575. procname:='EQ';
  2576. unequaln:
  2577. procname:='NE';
  2578. else
  2579. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2580. end;
  2581. case tfloatdef(left.resultdef).floattype of
  2582. s32real:
  2583. begin
  2584. procname:=procname+'S';
  2585. if nodetype in [addn,muln,subn,slashn] then
  2586. procname:=lower(procname);
  2587. end;
  2588. s64real:
  2589. procname:=procname+'D';
  2590. {!!! not yet implemented
  2591. s128real:
  2592. }
  2593. else
  2594. internalerror(2005082602);
  2595. end;
  2596. end;
  2597. { cast softfpu result? }
  2598. if not(target_info.system in systems_wince) then
  2599. begin
  2600. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2601. resultdef:=pasbool8type;
  2602. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  2603. ctypeconvnode.create_internal(right,fdef),
  2604. ccallparanode.create(
  2605. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  2606. end
  2607. else
  2608. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  2609. ccallparanode.create(left,nil)));
  2610. left:=nil;
  2611. right:=nil;
  2612. { do we need to reverse the result }
  2613. if notnode then
  2614. result:=cnotnode.create(result);
  2615. end;
  2616. function taddnode.pass_1 : tnode;
  2617. var
  2618. {$ifdef addstringopt}
  2619. hp : tnode;
  2620. {$endif addstringopt}
  2621. rd,ld : tdef;
  2622. i : longint;
  2623. lt,rt : tnodetype;
  2624. {$ifdef cpuneedsmulhelper}
  2625. procname : string[32];
  2626. {$endif cpuneedsmulhelper}
  2627. begin
  2628. result:=nil;
  2629. { Can we optimize multiple string additions into a single call?
  2630. This need to be done on a complete tree to detect the multiple
  2631. add nodes and is therefor done before the subtrees are processed }
  2632. if canbemultistringadd(self) then
  2633. begin
  2634. result:=genmultistringadd(self);
  2635. exit;
  2636. end;
  2637. { first do the two subtrees }
  2638. firstpass(left);
  2639. firstpass(right);
  2640. if codegenerror then
  2641. exit;
  2642. { load easier access variables }
  2643. rd:=right.resultdef;
  2644. ld:=left.resultdef;
  2645. rt:=right.nodetype;
  2646. lt:=left.nodetype;
  2647. { int/int gives real/real! }
  2648. if nodetype=slashn then
  2649. begin
  2650. {$ifdef cpufpemu}
  2651. result:=first_addfloat;
  2652. if assigned(result) then
  2653. exit;
  2654. {$endif cpufpemu}
  2655. expectloc:=LOC_FPUREGISTER;
  2656. end
  2657. { if both are orddefs then check sub types }
  2658. else if (ld.typ=orddef) and (rd.typ=orddef) then
  2659. begin
  2660. { optimize multiplacation by a power of 2 }
  2661. if not(cs_check_overflow in current_settings.localswitches) and
  2662. (nodetype = muln) and
  2663. (((left.nodetype = ordconstn) and
  2664. ispowerof2(tordconstnode(left).value,i)) or
  2665. ((right.nodetype = ordconstn) and
  2666. ispowerof2(tordconstnode(right).value,i))) then
  2667. begin
  2668. if left.nodetype = ordconstn then
  2669. begin
  2670. tordconstnode(left).value := i;
  2671. result := cshlshrnode.create(shln,right,left);
  2672. end
  2673. else
  2674. begin
  2675. tordconstnode(right).value := i;
  2676. result := cshlshrnode.create(shln,left,right);
  2677. end;
  2678. left := nil;
  2679. right := nil;
  2680. exit;
  2681. end;
  2682. { 2 booleans ? }
  2683. if is_boolean(ld) and is_boolean(rd) then
  2684. begin
  2685. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  2686. (nf_short_bool in flags)) and
  2687. (nodetype in [andn,orn]) then
  2688. expectloc:=LOC_JUMP
  2689. else
  2690. begin
  2691. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2692. expectloc:=LOC_FLAGS
  2693. else
  2694. expectloc:=LOC_REGISTER;
  2695. end;
  2696. end
  2697. else
  2698. { Both are chars? only convert to shortstrings for addn }
  2699. if is_char(ld) then
  2700. begin
  2701. if nodetype=addn then
  2702. internalerror(200103291);
  2703. expectloc:=LOC_FLAGS;
  2704. end
  2705. {$ifndef cpu64bitalu}
  2706. { is there a 64 bit type ? }
  2707. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  2708. begin
  2709. result := first_add64bitint;
  2710. if assigned(result) then
  2711. exit;
  2712. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2713. expectloc:=LOC_REGISTER
  2714. else
  2715. expectloc:=LOC_JUMP;
  2716. end
  2717. {$endif cpu64bitalu}
  2718. {$ifndef cpuneedsmulhelper}
  2719. { is there a cardinal? }
  2720. else if (torddef(ld).ordtype=u32bit) then
  2721. begin
  2722. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2723. expectloc:=LOC_REGISTER
  2724. else
  2725. {$ifdef cpu16bitalu}
  2726. expectloc:=LOC_JUMP;
  2727. {$else cpu16bitalu}
  2728. expectloc:=LOC_FLAGS;
  2729. {$endif cpu16bitalu}
  2730. end
  2731. {$endif cpuneedsmulhelper}
  2732. { generic s32bit conversion }
  2733. else
  2734. begin
  2735. {$ifdef cpuneedsmulhelper}
  2736. if (nodetype=muln) and not(torddef(resultdef).ordtype in [u8bit,s8bit{$ifdef cpu16bitalu},u16bit,s16bit{$endif}]) then
  2737. begin
  2738. result := nil;
  2739. case torddef(resultdef).ordtype of
  2740. s16bit:
  2741. procname := 'fpc_mul_integer';
  2742. u16bit:
  2743. procname := 'fpc_mul_word';
  2744. s32bit:
  2745. procname := 'fpc_mul_longint';
  2746. u32bit:
  2747. procname := 'fpc_mul_dword';
  2748. else
  2749. internalerror(2011022301);
  2750. end;
  2751. result := ccallnode.createintern(procname,
  2752. ccallparanode.create(cordconstnode.create(0,pasbool8type,false),
  2753. ccallparanode.create(right,
  2754. ccallparanode.create(left,nil))));
  2755. left := nil;
  2756. right := nil;
  2757. firstpass(result);
  2758. exit;
  2759. end;
  2760. {$endif cpuneedsmulhelper}
  2761. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2762. expectloc:=LOC_REGISTER
  2763. else
  2764. {$ifdef cpu16bitalu}
  2765. expectloc:=LOC_JUMP;
  2766. {$else cpu16bitalu}
  2767. expectloc:=LOC_FLAGS;
  2768. {$endif cpu16bitalu}
  2769. end;
  2770. end
  2771. { left side a setdef, must be before string processing,
  2772. else array constructor can be seen as array of char (PFV) }
  2773. else if (ld.typ=setdef) then
  2774. begin
  2775. { small sets are handled inline by the compiler.
  2776. small set doesn't have support for adding ranges }
  2777. if is_smallset(ld) and
  2778. not(
  2779. (right.nodetype=setelementn) and
  2780. assigned(tsetelementnode(right).right)
  2781. ) then
  2782. begin
  2783. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2784. expectloc:=LOC_FLAGS
  2785. else
  2786. expectloc:=LOC_REGISTER;
  2787. end
  2788. else
  2789. begin
  2790. result := first_addset;
  2791. if assigned(result) then
  2792. exit;
  2793. expectloc:=LOC_CREFERENCE;
  2794. end;
  2795. end
  2796. { compare pchar by addresses like BP/Delphi }
  2797. else if is_pchar(ld) then
  2798. begin
  2799. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2800. expectloc:=LOC_REGISTER
  2801. else
  2802. expectloc:=LOC_FLAGS;
  2803. end
  2804. { is one of the operands a string }
  2805. else if (ld.typ=stringdef) then
  2806. begin
  2807. if is_widestring(ld) then
  2808. begin
  2809. { this is only for add, the comparisaion is handled later }
  2810. expectloc:=LOC_REGISTER;
  2811. end
  2812. else if is_unicodestring(ld) then
  2813. begin
  2814. { this is only for add, the comparisaion is handled later }
  2815. expectloc:=LOC_REGISTER;
  2816. end
  2817. else if is_ansistring(ld) then
  2818. begin
  2819. { this is only for add, the comparisaion is handled later }
  2820. expectloc:=LOC_REGISTER;
  2821. end
  2822. else if is_longstring(ld) then
  2823. begin
  2824. { this is only for add, the comparisaion is handled later }
  2825. expectloc:=LOC_REFERENCE;
  2826. end
  2827. else
  2828. begin
  2829. {$ifdef addstringopt}
  2830. { can create a call which isn't handled by callparatemp }
  2831. if canbeaddsstringcharoptnode(self) then
  2832. begin
  2833. hp := genaddsstringcharoptnode(self);
  2834. pass_1 := hp;
  2835. exit;
  2836. end
  2837. else
  2838. {$endif addstringopt}
  2839. begin
  2840. { Fix right to be shortstring }
  2841. if is_char(right.resultdef) then
  2842. begin
  2843. inserttypeconv(right,cshortstringtype);
  2844. firstpass(right);
  2845. end;
  2846. end;
  2847. {$ifdef addstringopt}
  2848. { can create a call which isn't handled by callparatemp }
  2849. if canbeaddsstringcsstringoptnode(self) then
  2850. begin
  2851. hp := genaddsstringcsstringoptnode(self);
  2852. pass_1 := hp;
  2853. exit;
  2854. end;
  2855. {$endif addstringopt}
  2856. end;
  2857. { otherwise, let addstring convert everything }
  2858. result := first_addstring;
  2859. exit;
  2860. end
  2861. { is one a real float ? }
  2862. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  2863. begin
  2864. {$ifdef cpufpemu}
  2865. result:=first_addfloat;
  2866. if assigned(result) then
  2867. exit;
  2868. {$endif cpufpemu}
  2869. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2870. expectloc:=LOC_FPUREGISTER
  2871. else
  2872. expectloc:=LOC_FLAGS;
  2873. end
  2874. { pointer comperation and subtraction }
  2875. else if (ld.typ=pointerdef) then
  2876. begin
  2877. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2878. expectloc:=LOC_REGISTER
  2879. else
  2880. expectloc:=LOC_FLAGS;
  2881. end
  2882. else if is_implicit_pointer_object_type(ld) then
  2883. begin
  2884. expectloc:=LOC_FLAGS;
  2885. end
  2886. else if (ld.typ=classrefdef) then
  2887. begin
  2888. expectloc:=LOC_FLAGS;
  2889. end
  2890. { support procvar=nil,procvar<>nil }
  2891. else if ((ld.typ=procvardef) and (rt=niln)) or
  2892. ((rd.typ=procvardef) and (lt=niln)) then
  2893. begin
  2894. expectloc:=LOC_FLAGS;
  2895. end
  2896. {$ifdef SUPPORT_MMX}
  2897. { mmx support, this must be before the zero based array
  2898. check }
  2899. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  2900. is_mmx_able_array(rd) then
  2901. begin
  2902. expectloc:=LOC_MMXREGISTER;
  2903. end
  2904. {$endif SUPPORT_MMX}
  2905. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  2906. begin
  2907. expectloc:=LOC_REGISTER;
  2908. end
  2909. else if (rd.typ=procvardef) and
  2910. (ld.typ=procvardef) and
  2911. equal_defs(rd,ld) then
  2912. begin
  2913. expectloc:=LOC_FLAGS;
  2914. end
  2915. else if (ld.typ=enumdef) then
  2916. begin
  2917. expectloc:=LOC_FLAGS;
  2918. end
  2919. {$ifdef SUPPORT_MMX}
  2920. else if (cs_mmx in current_settings.localswitches) and
  2921. is_mmx_able_array(ld) and
  2922. is_mmx_able_array(rd) then
  2923. begin
  2924. expectloc:=LOC_MMXREGISTER;
  2925. end
  2926. {$endif SUPPORT_MMX}
  2927. { the general solution is to convert to 32 bit int }
  2928. else
  2929. begin
  2930. expectloc:=LOC_REGISTER;
  2931. end;
  2932. end;
  2933. {$ifdef state_tracking}
  2934. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2935. var factval:Tnode;
  2936. begin
  2937. track_state_pass:=false;
  2938. if left.track_state_pass(exec_known) then
  2939. begin
  2940. track_state_pass:=true;
  2941. left.resultdef:=nil;
  2942. do_typecheckpass(left);
  2943. end;
  2944. factval:=aktstate.find_fact(left);
  2945. if factval<>nil then
  2946. begin
  2947. track_state_pass:=true;
  2948. left.destroy;
  2949. left:=factval.getcopy;
  2950. end;
  2951. if right.track_state_pass(exec_known) then
  2952. begin
  2953. track_state_pass:=true;
  2954. right.resultdef:=nil;
  2955. do_typecheckpass(right);
  2956. end;
  2957. factval:=aktstate.find_fact(right);
  2958. if factval<>nil then
  2959. begin
  2960. track_state_pass:=true;
  2961. right.destroy;
  2962. right:=factval.getcopy;
  2963. end;
  2964. end;
  2965. {$endif}
  2966. end.