nadd.pas 114 KB

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