2
0

nadd.pas 106 KB

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