nadd.pas 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991
  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
  816. {$ifdef cpufpemu}
  817. (current_settings.fputype<>fpu_soft) and
  818. not(cs_fp_emulation in current_settings.moduleswitches) and
  819. {$endif cpufpemu}
  820. (nodetype=muln) and
  821. is_real(left.resultdef) and is_real(right.resultdef) and
  822. left.isequal(right) and
  823. not(might_have_sideeffects(left)) then
  824. begin
  825. result:=cinlinenode.create(in_sqr_real,false,left);
  826. left:=nil;
  827. exit;
  828. end;
  829. end;
  830. end;
  831. function taddnode.dogetcopy: tnode;
  832. var
  833. n: taddnode;
  834. begin
  835. n:=taddnode(inherited dogetcopy);
  836. n.resultrealdef:=resultrealdef;
  837. result:=n;
  838. end;
  839. function taddnode.docompare(p: tnode): boolean;
  840. begin
  841. result:=
  842. inherited docompare(p) and
  843. equal_defs(taddnode(p).resultrealdef,resultrealdef);
  844. end;
  845. function taddnode.pass_typecheck:tnode;
  846. begin
  847. { This function is small to keep the stack small for recursive of
  848. large + operations }
  849. typecheckpass(left);
  850. typecheckpass(right);
  851. result:=pass_typecheck_internal;
  852. end;
  853. function taddnode.pass_typecheck_internal:tnode;
  854. var
  855. hp : tnode;
  856. rd,ld,nd : tdef;
  857. hsym : tfieldvarsym;
  858. llow,lhigh,
  859. rlow,rhigh : tconstexprint;
  860. strtype : tstringtype;
  861. res,
  862. b : boolean;
  863. lt,rt : tnodetype;
  864. ot : tnodetype;
  865. {$ifdef state_tracking}
  866. factval : Tnode;
  867. change : boolean;
  868. {$endif}
  869. begin
  870. result:=nil;
  871. { avoid any problems with type parameters later on }
  872. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  873. begin
  874. resultdef:=cundefinedtype;
  875. exit;
  876. end;
  877. { both left and right need to be valid }
  878. set_varstate(left,vs_read,[vsf_must_be_valid]);
  879. set_varstate(right,vs_read,[vsf_must_be_valid]);
  880. if codegenerror then
  881. exit;
  882. { tp procvar support }
  883. maybe_call_procvar(left,true);
  884. maybe_call_procvar(right,true);
  885. { convert array constructors to sets, because there is no other operator
  886. possible for array constructors }
  887. if is_array_constructor(left.resultdef) then
  888. begin
  889. arrayconstructor_to_set(left);
  890. typecheckpass(left);
  891. end;
  892. if is_array_constructor(right.resultdef) then
  893. begin
  894. arrayconstructor_to_set(right);
  895. typecheckpass(right);
  896. end;
  897. { allow operator overloading }
  898. hp:=self;
  899. if isbinaryoverloaded(hp) then
  900. begin
  901. result:=hp;
  902. exit;
  903. end;
  904. { Stop checking when an error was found in the operator checking }
  905. if codegenerror then
  906. begin
  907. result:=cerrornode.create;
  908. exit;
  909. end;
  910. { Kylix allows enum+ordconstn in an enum type declaration, we need to do
  911. the conversion here before the constant folding }
  912. if (m_delphi in current_settings.modeswitches) and
  913. (blocktype in [bt_type,bt_const_type,bt_var_type]) then
  914. begin
  915. if (left.resultdef.typ=enumdef) and
  916. (right.resultdef.typ=orddef) then
  917. begin
  918. { insert explicit typecast to default signed int }
  919. left:=ctypeconvnode.create_internal(left,sinttype);
  920. typecheckpass(left);
  921. end
  922. else
  923. if (left.resultdef.typ=orddef) and
  924. (right.resultdef.typ=enumdef) then
  925. begin
  926. { insert explicit typecast to default signed int }
  927. right:=ctypeconvnode.create_internal(right,sinttype);
  928. typecheckpass(right);
  929. end;
  930. end;
  931. { is one a real float, then both need to be floats, this
  932. need to be done before the constant folding so constant
  933. operation on a float and int are also handled }
  934. {$ifdef x86}
  935. { use extended as default real type only when the x87 fpu is used }
  936. {$ifdef i386}
  937. if not(current_settings.fputype=fpu_x87) then
  938. resultrealdef:=s64floattype
  939. else
  940. resultrealdef:=pbestrealtype^;
  941. {$endif i386}
  942. {$ifdef x86_64}
  943. { x86-64 has no x87 only mode, so use always double as default }
  944. resultrealdef:=s64floattype;
  945. {$endif x86_6}
  946. {$else not x86}
  947. resultrealdef:=pbestrealtype^;
  948. {$endif not x86}
  949. if (right.resultdef.typ=floatdef) or (left.resultdef.typ=floatdef) then
  950. begin
  951. { when both floattypes are already equal then use that
  952. floattype for results }
  953. if (right.resultdef.typ=floatdef) and
  954. (left.resultdef.typ=floatdef) and
  955. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype) then
  956. resultrealdef:=left.resultdef
  957. { when there is a currency type then use currency, but
  958. only when currency is defined as float }
  959. else
  960. if (is_currency(right.resultdef) or
  961. is_currency(left.resultdef)) and
  962. ((s64currencytype.typ = floatdef) or
  963. (nodetype <> slashn)) then
  964. begin
  965. resultrealdef:=s64currencytype;
  966. inserttypeconv(right,resultrealdef);
  967. inserttypeconv(left,resultrealdef);
  968. end
  969. else
  970. begin
  971. resultrealdef:=getbestreal(left.resultdef,right.resultdef);
  972. inserttypeconv(right,resultrealdef);
  973. inserttypeconv(left,resultrealdef);
  974. end;
  975. end;
  976. { If both operands are constant and there is a unicodestring
  977. or unicodestring then convert everything to unicodestring }
  978. if is_constnode(right) and is_constnode(left) and
  979. (is_unicodestring(right.resultdef) or
  980. is_unicodestring(left.resultdef)) then
  981. begin
  982. inserttypeconv(right,cunicodestringtype);
  983. inserttypeconv(left,cunicodestringtype);
  984. end;
  985. { If both operands are constant and there is a widechar
  986. or widestring then convert everything to widestring. This
  987. allows constant folding like char+widechar }
  988. if is_constnode(right) and is_constnode(left) and
  989. (is_widestring(right.resultdef) or
  990. is_widestring(left.resultdef) or
  991. is_widechar(right.resultdef) or
  992. is_widechar(left.resultdef)) then
  993. begin
  994. inserttypeconv(right,cwidestringtype);
  995. inserttypeconv(left,cwidestringtype);
  996. end;
  997. { load easier access variables }
  998. rd:=right.resultdef;
  999. ld:=left.resultdef;
  1000. rt:=right.nodetype;
  1001. lt:=left.nodetype;
  1002. { 4 character constant strings are compatible with orddef }
  1003. { in macpas mode (become cardinals) }
  1004. if (m_mac in current_settings.modeswitches) and
  1005. { only allow for comparisons, additions etc are }
  1006. { normally program errors }
  1007. (nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) and
  1008. (((lt=stringconstn) and
  1009. (tstringconstnode(left).len=4) and
  1010. (rd.typ=orddef)) or
  1011. ((rt=stringconstn) and
  1012. (tstringconstnode(right).len=4) and
  1013. (ld.typ=orddef))) then
  1014. begin
  1015. if (rt=stringconstn) then
  1016. begin
  1017. inserttypeconv(right,u32inttype);
  1018. rt:=right.nodetype;
  1019. rd:=right.resultdef;
  1020. end
  1021. else
  1022. begin
  1023. inserttypeconv(left,u32inttype);
  1024. lt:=left.nodetype;
  1025. ld:=left.resultdef;
  1026. end;
  1027. end;
  1028. { but an int/int gives real/real! }
  1029. if (nodetype=slashn) and not(is_vector(left.resultdef)) and not(is_vector(right.resultdef)) then
  1030. begin
  1031. if is_currency(left.resultdef) and
  1032. is_currency(right.resultdef) then
  1033. { In case of currency, converting to float means dividing by 10000 }
  1034. { However, since this is already a division, both divisions by }
  1035. { 10000 are eliminated when we divide the results -> we can skip }
  1036. { them. }
  1037. if s64currencytype.typ = floatdef then
  1038. begin
  1039. { there's no s64comptype or so, how do we avoid the type conversion?
  1040. left.resultdef := s64comptype;
  1041. right.resultdef := s64comptype; }
  1042. end
  1043. else
  1044. begin
  1045. left.resultdef := s64inttype;
  1046. right.resultdef := s64inttype;
  1047. end;
  1048. inserttypeconv(right,resultrealdef);
  1049. inserttypeconv(left,resultrealdef);
  1050. end
  1051. { if both are orddefs then check sub types }
  1052. else if (ld.typ=orddef) and (rd.typ=orddef) then
  1053. begin
  1054. { set for & and | operations in macpas mode: they only work on }
  1055. { booleans, and always short circuit evaluation }
  1056. if (nf_short_bool in flags) then
  1057. begin
  1058. if not is_boolean(ld) then
  1059. begin
  1060. inserttypeconv(left,pasbool8type);
  1061. ld := left.resultdef;
  1062. end;
  1063. if not is_boolean(rd) then
  1064. begin
  1065. inserttypeconv(right,pasbool8type);
  1066. rd := right.resultdef;
  1067. end;
  1068. end;
  1069. { 2 booleans? Make them equal to the largest boolean }
  1070. if (is_boolean(ld) and is_boolean(rd)) or
  1071. (nf_short_bool in flags) then
  1072. begin
  1073. if (torddef(left.resultdef).size>torddef(right.resultdef).size) or
  1074. (is_cbool(left.resultdef) and not is_cbool(right.resultdef)) then
  1075. begin
  1076. right:=ctypeconvnode.create_internal(right,left.resultdef);
  1077. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  1078. typecheckpass(right);
  1079. end
  1080. else if (torddef(left.resultdef).size<torddef(right.resultdef).size) or
  1081. (not is_cbool(left.resultdef) and is_cbool(right.resultdef)) then
  1082. begin
  1083. left:=ctypeconvnode.create_internal(left,right.resultdef);
  1084. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  1085. typecheckpass(left);
  1086. end;
  1087. case nodetype of
  1088. xorn,
  1089. ltn,
  1090. lten,
  1091. gtn,
  1092. gten,
  1093. andn,
  1094. orn:
  1095. begin
  1096. end;
  1097. unequaln,
  1098. equaln:
  1099. begin
  1100. if not(cs_full_boolean_eval in current_settings.localswitches) or
  1101. (nf_short_bool in flags) then
  1102. begin
  1103. { Remove any compares with constants }
  1104. if (left.nodetype=ordconstn) then
  1105. begin
  1106. hp:=right;
  1107. b:=(tordconstnode(left).value<>0);
  1108. ot:=nodetype;
  1109. left.free;
  1110. left:=nil;
  1111. right:=nil;
  1112. if (not(b) and (ot=equaln)) or
  1113. (b and (ot=unequaln)) then
  1114. begin
  1115. hp:=cnotnode.create(hp);
  1116. end;
  1117. result:=hp;
  1118. exit;
  1119. end;
  1120. if (right.nodetype=ordconstn) then
  1121. begin
  1122. hp:=left;
  1123. b:=(tordconstnode(right).value<>0);
  1124. ot:=nodetype;
  1125. right.free;
  1126. right:=nil;
  1127. left:=nil;
  1128. if (not(b) and (ot=equaln)) or
  1129. (b and (ot=unequaln)) then
  1130. begin
  1131. hp:=cnotnode.create(hp);
  1132. end;
  1133. result:=hp;
  1134. exit;
  1135. end;
  1136. end;
  1137. end;
  1138. else
  1139. begin
  1140. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1141. result:=cnothingnode.create;
  1142. exit;
  1143. end;
  1144. end;
  1145. end
  1146. { Both are chars? }
  1147. else if is_char(rd) and is_char(ld) then
  1148. begin
  1149. if nodetype=addn then
  1150. begin
  1151. resultdef:=cshortstringtype;
  1152. if not(is_constcharnode(left) and is_constcharnode(right)) then
  1153. begin
  1154. inserttypeconv(left,cshortstringtype);
  1155. {$ifdef addstringopt}
  1156. hp := genaddsstringcharoptnode(self);
  1157. result := hp;
  1158. exit;
  1159. {$endif addstringopt}
  1160. end
  1161. end
  1162. else if not(nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) then
  1163. begin
  1164. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1165. result:=cnothingnode.create;
  1166. exit;
  1167. end;
  1168. end
  1169. { There is a widechar? }
  1170. else if is_widechar(rd) or is_widechar(ld) then
  1171. begin
  1172. { widechar+widechar gives widestring }
  1173. if nodetype=addn then
  1174. begin
  1175. inserttypeconv(left,cwidestringtype);
  1176. if (torddef(rd).ordtype<>uwidechar) then
  1177. inserttypeconv(right,cwidechartype);
  1178. resultdef:=cwidestringtype;
  1179. end
  1180. else
  1181. begin
  1182. if (torddef(ld).ordtype<>uwidechar) then
  1183. inserttypeconv(left,cwidechartype);
  1184. if (torddef(rd).ordtype<>uwidechar) then
  1185. inserttypeconv(right,cwidechartype);
  1186. end;
  1187. end
  1188. { is there a currency type ? }
  1189. else if ((torddef(rd).ordtype=scurrency) or (torddef(ld).ordtype=scurrency)) then
  1190. begin
  1191. if (torddef(ld).ordtype<>scurrency) then
  1192. inserttypeconv(left,s64currencytype);
  1193. if (torddef(rd).ordtype<>scurrency) then
  1194. inserttypeconv(right,s64currencytype);
  1195. end
  1196. { "and" does't care about the sign of integers }
  1197. { "xor", "or" and compares don't need extension to native int }
  1198. { size either as long as both values are signed or unsigned }
  1199. { "xor" and "or" also don't care about the sign if the values }
  1200. { occupy an entire register }
  1201. { don't do it if either type is 64 bit, since in that case we }
  1202. { can't safely find a "common" type }
  1203. else if is_integer(ld) and is_integer(rd) and
  1204. not is_64bitint(ld) and not is_64bitint(rd) and
  1205. ((nodetype=andn) or
  1206. ((nodetype in [orn,xorn,equaln,unequaln,gtn,gten,ltn,lten]) and
  1207. not(is_signed(ld) xor is_signed(rd)))) then
  1208. begin
  1209. if (rd.size>ld.size) or
  1210. { Delphi-compatible: prefer unsigned type for "and" with equal size }
  1211. ((rd.size=ld.size) and
  1212. not is_signed(rd)) then
  1213. begin
  1214. if (rd.size=ld.size) and
  1215. is_signed(ld) then
  1216. inserttypeconv_internal(left,rd)
  1217. else
  1218. begin
  1219. { not to left right.resultdef, because that may
  1220. cause a range error if left and right's def don't
  1221. completely overlap }
  1222. nd:=get_common_intdef(torddef(ld),torddef(rd),true);
  1223. inserttypeconv(left,nd);
  1224. inserttypeconv(right,nd);
  1225. end;
  1226. end
  1227. else
  1228. begin
  1229. if (rd.size=ld.size) and
  1230. is_signed(rd) then
  1231. inserttypeconv_internal(right,ld)
  1232. else
  1233. begin
  1234. nd:=get_common_intdef(torddef(ld),torddef(rd),true);
  1235. inserttypeconv(left,nd);
  1236. inserttypeconv(right,nd);
  1237. end;
  1238. end
  1239. end
  1240. { is there a signed 64 bit type ? }
  1241. else if ((torddef(rd).ordtype=s64bit) or (torddef(ld).ordtype=s64bit)) then
  1242. begin
  1243. if (torddef(ld).ordtype<>s64bit) then
  1244. inserttypeconv(left,s64inttype);
  1245. if (torddef(rd).ordtype<>s64bit) then
  1246. inserttypeconv(right,s64inttype);
  1247. end
  1248. { is there a unsigned 64 bit type ? }
  1249. else if ((torddef(rd).ordtype=u64bit) or (torddef(ld).ordtype=u64bit)) then
  1250. begin
  1251. if (torddef(ld).ordtype<>u64bit) then
  1252. inserttypeconv(left,u64inttype);
  1253. if (torddef(rd).ordtype<>u64bit) then
  1254. inserttypeconv(right,u64inttype);
  1255. end
  1256. { 64 bit cpus do calculations always in 64 bit }
  1257. {$ifndef cpu64bitaddr}
  1258. { is there a cardinal? }
  1259. else if ((torddef(rd).ordtype=u32bit) or (torddef(ld).ordtype=u32bit)) then
  1260. begin
  1261. { convert positive constants to u32bit }
  1262. if (torddef(ld).ordtype<>u32bit) and
  1263. is_constintnode(left) and
  1264. (tordconstnode(left).value >= 0) then
  1265. inserttypeconv(left,u32inttype);
  1266. if (torddef(rd).ordtype<>u32bit) and
  1267. is_constintnode(right) and
  1268. (tordconstnode(right).value >= 0) then
  1269. inserttypeconv(right,u32inttype);
  1270. { when one of the operand is signed or the operation is subn then perform
  1271. the operation in 64bit, can't use rd/ld here because there
  1272. could be already typeconvs inserted.
  1273. This is compatible with the code below for other unsigned types (PFV) }
  1274. if is_signed(left.resultdef) or
  1275. is_signed(right.resultdef) or
  1276. (nodetype=subn) then
  1277. begin
  1278. if nodetype<>subn then
  1279. CGMessage(type_h_mixed_signed_unsigned);
  1280. { mark as internal in case added for a subn, so }
  1281. { ttypeconvnode.simplify can remove the 64 bit }
  1282. { typecast again if semantically correct. Even }
  1283. { if we could detect that here already, we }
  1284. { mustn't do it here because that would change }
  1285. { overload choosing behaviour etc. The code in }
  1286. { ncnv.pas is run after that is already decided }
  1287. if (not is_signed(left.resultdef) and
  1288. not is_signed(right.resultdef)) or
  1289. (nodetype in [orn,xorn]) then
  1290. include(flags,nf_internal);
  1291. inserttypeconv(left,s64inttype);
  1292. inserttypeconv(right,s64inttype);
  1293. end
  1294. else
  1295. begin
  1296. if (torddef(left.resultdef).ordtype<>u32bit) then
  1297. inserttypeconv(left,u32inttype);
  1298. if (torddef(right.resultdef).ordtype<>u32bit) then
  1299. inserttypeconv(right,u32inttype);
  1300. end;
  1301. end
  1302. {$endif cpu64bitaddr}
  1303. { generic ord conversion is sinttype }
  1304. else
  1305. begin
  1306. { When there is a signed type or there is a minus operation
  1307. we convert to signed int. Otherwise (both are unsigned) we keep
  1308. the result also unsigned. This is compatible with Delphi (PFV) }
  1309. if is_signed(ld) or
  1310. is_signed(rd) or
  1311. (nodetype=subn) then
  1312. begin
  1313. {$ifdef cpunodefaultint}
  1314. { for small cpus we use the smallest common type }
  1315. nd:=get_common_intdef(torddef(ld),torddef(rd),false);
  1316. inserttypeconv(right,nd);
  1317. inserttypeconv(left,nd);
  1318. {$else cpunodefaultint}
  1319. inserttypeconv(right,sinttype);
  1320. inserttypeconv(left,sinttype);
  1321. {$endif cpunodefaultint}
  1322. end
  1323. else
  1324. begin
  1325. inserttypeconv(right,uinttype);
  1326. inserttypeconv(left,uinttype);
  1327. end;
  1328. end;
  1329. end
  1330. { if both are floatdefs, conversion is already done before constant folding }
  1331. else if (ld.typ=floatdef) then
  1332. begin
  1333. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  1334. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1335. end
  1336. { left side a setdef, must be before string processing,
  1337. else array constructor can be seen as array of char (PFV) }
  1338. else if (ld.typ=setdef) then
  1339. begin
  1340. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1341. CGMessage(type_e_set_operation_unknown);
  1342. { right must either be a set or a set element }
  1343. if (rd.typ<>setdef) and
  1344. (rt<>setelementn) then
  1345. CGMessage(type_e_mismatch)
  1346. { Make operands the same setdef. If one's elementtype fits }
  1347. { entirely inside the other's, pick the one with the largest }
  1348. { range. Otherwise create a new setdef with a range which }
  1349. { can contain both. }
  1350. else if not(equal_defs(ld,rd)) then
  1351. begin
  1352. { note: ld cannot be an empty set with elementdef=nil in }
  1353. { case right is not a set, arrayconstructor_to_set takes }
  1354. { care of that }
  1355. { 1: rd is a set with an assigned elementdef, and ld is }
  1356. { either an empty set without elementdef or a set whose }
  1357. { elementdef fits in rd's elementdef -> convert to rd }
  1358. if ((rd.typ=setdef) and
  1359. assigned(tsetdef(rd).elementdef) and
  1360. (not assigned(tsetdef(ld).elementdef) or
  1361. is_in_limit(ld,rd))) then
  1362. inserttypeconv(left,rd)
  1363. { 2: rd is either an empty set without elementdef or a set }
  1364. { whose elementdef fits in ld's elementdef, or a set }
  1365. { element whose def fits in ld's elementdef -> convert }
  1366. { to ld. ld's elementdef can't be nil here, is caught }
  1367. { previous case and "note:" above }
  1368. else if ((rd.typ=setdef) and
  1369. (not assigned(tsetdef(rd).elementdef) or
  1370. is_in_limit(rd,ld))) or
  1371. ((rd.typ<>setdef) and
  1372. is_in_limit(rd,tsetdef(ld).elementdef)) then
  1373. if (rd.typ=setdef) then
  1374. inserttypeconv(right,ld)
  1375. else
  1376. inserttypeconv(right,tsetdef(ld).elementdef)
  1377. { 3: otherwise create setdef which encompasses both, taking }
  1378. { into account empty sets without elementdef }
  1379. else
  1380. begin
  1381. if assigned(tsetdef(ld).elementdef) then
  1382. begin
  1383. llow:=tsetdef(ld).setbase;
  1384. lhigh:=tsetdef(ld).setmax;
  1385. end;
  1386. if (rd.typ=setdef) then
  1387. if assigned(tsetdef(rd).elementdef) then
  1388. begin
  1389. rlow:=tsetdef(rd).setbase;
  1390. rhigh:=tsetdef(rd).setmax;
  1391. end
  1392. else
  1393. begin
  1394. { ld's elementdef must have been valid }
  1395. rlow:=llow;
  1396. rhigh:=lhigh;
  1397. end
  1398. else
  1399. getrange(rd,rlow,rhigh);
  1400. if not assigned(tsetdef(ld).elementdef) then
  1401. begin
  1402. llow:=rlow;
  1403. lhigh:=rhigh;
  1404. end;
  1405. nd:=tsetdef.create(tsetdef(ld).elementdef,min(llow,rlow),max(lhigh,rhigh));
  1406. inserttypeconv(left,nd);
  1407. if (rd.typ=setdef) then
  1408. inserttypeconv(right,nd)
  1409. else
  1410. inserttypeconv(right,tsetdef(nd).elementdef);
  1411. end;
  1412. end;
  1413. end
  1414. { pointer comparision and subtraction }
  1415. else if (
  1416. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  1417. ) or
  1418. { compare/add pchar to variable (not stringconst) char arrays
  1419. by addresses like BP/Delphi }
  1420. (
  1421. (nodetype in [equaln,unequaln,subn,addn]) and
  1422. (
  1423. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1424. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1425. )
  1426. ) then
  1427. begin
  1428. { convert char array to pointer }
  1429. if is_chararray(rd) then
  1430. begin
  1431. inserttypeconv(right,charpointertype);
  1432. rd:=right.resultdef;
  1433. end
  1434. else if is_chararray(ld) then
  1435. begin
  1436. inserttypeconv(left,charpointertype);
  1437. ld:=left.resultdef;
  1438. end;
  1439. case nodetype of
  1440. equaln,unequaln :
  1441. begin
  1442. if is_voidpointer(right.resultdef) then
  1443. inserttypeconv(right,left.resultdef)
  1444. else if is_voidpointer(left.resultdef) then
  1445. inserttypeconv(left,right.resultdef)
  1446. else if not(equal_defs(ld,rd)) then
  1447. IncompatibleTypes(ld,rd);
  1448. { now that the type checking is done, convert both to charpointer, }
  1449. { because methodpointers are 8 bytes even though only the first 4 }
  1450. { bytes must be compared. This can happen here if we are in }
  1451. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1452. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1453. { optimized away, since the result already was a voidpointer, so }
  1454. { use a charpointer instead (JM) }
  1455. inserttypeconv_internal(left,charpointertype);
  1456. inserttypeconv_internal(right,charpointertype);
  1457. end;
  1458. ltn,lten,gtn,gten:
  1459. begin
  1460. if (cs_extsyntax in current_settings.moduleswitches) then
  1461. begin
  1462. if is_voidpointer(right.resultdef) then
  1463. inserttypeconv(right,left.resultdef)
  1464. else if is_voidpointer(left.resultdef) then
  1465. inserttypeconv(left,right.resultdef)
  1466. else if not(equal_defs(ld,rd)) then
  1467. IncompatibleTypes(ld,rd);
  1468. end
  1469. else
  1470. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1471. end;
  1472. subn:
  1473. begin
  1474. if (cs_extsyntax in current_settings.moduleswitches) then
  1475. begin
  1476. if is_voidpointer(right.resultdef) then
  1477. begin
  1478. if is_big_untyped_addrnode(right) then
  1479. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1480. inserttypeconv(right,left.resultdef)
  1481. end
  1482. else if is_voidpointer(left.resultdef) then
  1483. inserttypeconv(left,right.resultdef)
  1484. else if not(equal_defs(ld,rd)) then
  1485. IncompatibleTypes(ld,rd);
  1486. end
  1487. else
  1488. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1489. if not(nf_has_pointerdiv in flags) and
  1490. (tpointerdef(rd).pointeddef.size>1) then
  1491. begin
  1492. hp:=getcopy;
  1493. include(hp.flags,nf_has_pointerdiv);
  1494. result:=cmoddivnode.create(divn,hp,cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,false));
  1495. end;
  1496. resultdef:=sinttype;
  1497. exit;
  1498. end;
  1499. else
  1500. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1501. end;
  1502. end
  1503. { is one of the operands a string?,
  1504. chararrays are also handled as strings (after conversion), also take
  1505. care of chararray+chararray and chararray+char.
  1506. Note: Must be done after pointerdef+pointerdef has been checked, else
  1507. pchar is converted to string }
  1508. else if (rd.typ=stringdef) or
  1509. (ld.typ=stringdef) or
  1510. { stringconstn's can be arraydefs }
  1511. (lt=stringconstn) or
  1512. (rt=stringconstn) or
  1513. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1514. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1515. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1516. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1517. begin
  1518. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1519. begin
  1520. { Is there a unicodestring? }
  1521. if is_unicodestring(rd) or is_unicodestring(ld) then
  1522. strtype:=st_unicodestring
  1523. else
  1524. { Is there a widestring? }
  1525. if is_widestring(rd) or is_widestring(ld) or
  1526. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1527. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1528. strtype:=st_widestring
  1529. else
  1530. if is_ansistring(rd) or is_ansistring(ld) or
  1531. ((cs_ansistrings in current_settings.localswitches) and
  1532. //todo: Move some of this to longstring's then they are implemented?
  1533. (
  1534. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or (lt = stringconstn) or
  1535. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld) or (rt = stringconstn)
  1536. )
  1537. ) then
  1538. strtype:=st_ansistring
  1539. else
  1540. if is_longstring(rd) or is_longstring(ld) then
  1541. strtype:=st_longstring
  1542. else
  1543. begin
  1544. { TODO: todo: add a warning/hint here if one converting a too large array}
  1545. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1546. Note: Delphi halts with error if "array [0..xx] of char"
  1547. is assigned to ShortString and string length is less
  1548. then array size }
  1549. strtype:= st_shortstring;
  1550. end;
  1551. // Now convert nodes to common string type
  1552. case strtype of
  1553. st_widestring :
  1554. begin
  1555. if not(is_widestring(rd)) then
  1556. inserttypeconv(right,cwidestringtype);
  1557. if not(is_widestring(ld)) then
  1558. inserttypeconv(left,cwidestringtype);
  1559. end;
  1560. st_unicodestring :
  1561. begin
  1562. if not(is_unicodestring(rd)) then
  1563. inserttypeconv(right,cunicodestringtype);
  1564. if not(is_unicodestring(ld)) then
  1565. inserttypeconv(left,cunicodestringtype);
  1566. end;
  1567. st_ansistring :
  1568. begin
  1569. { use same code page if possible (don't force same code
  1570. page in case both are ansistrings with code page <>
  1571. CP_NONE, since then data loss can occur (the ansistring
  1572. helpers will convert them at run time to an encoding
  1573. that can represent both encodings) }
  1574. if is_ansistring(ld) and
  1575. (tstringdef(ld).encoding<>0) and
  1576. (tstringdef(ld).encoding<>globals.CP_NONE) and
  1577. (not is_ansistring(rd) or
  1578. (tstringdef(rd).encoding=0) or
  1579. (tstringdef(rd).encoding=globals.CP_NONE)) then
  1580. inserttypeconv(right,ld)
  1581. else if is_ansistring(rd) and
  1582. (tstringdef(rd).encoding<>0) and
  1583. (tstringdef(rd).encoding<>globals.CP_NONE) and
  1584. (not is_ansistring(ld) or
  1585. (tstringdef(ld).encoding=0) or
  1586. (tstringdef(ld).encoding=globals.CP_NONE)) then
  1587. inserttypeconv(left,rd)
  1588. else
  1589. begin
  1590. if not is_ansistring(ld) then
  1591. inserttypeconv(left,getansistringdef);
  1592. if not is_ansistring(rd) then
  1593. inserttypeconv(right,getansistringdef);
  1594. end;
  1595. end;
  1596. st_longstring :
  1597. begin
  1598. if not(is_longstring(rd)) then
  1599. inserttypeconv(right,clongstringtype);
  1600. if not(is_longstring(ld)) then
  1601. inserttypeconv(left,clongstringtype);
  1602. end;
  1603. st_shortstring :
  1604. begin
  1605. if not(is_shortstring(ld)) then
  1606. inserttypeconv(left,cshortstringtype);
  1607. { don't convert char, that can be handled by the optimized node }
  1608. if not(is_shortstring(rd) or is_char(rd)) then
  1609. inserttypeconv(right,cshortstringtype);
  1610. end;
  1611. else
  1612. internalerror(2005101);
  1613. end;
  1614. end
  1615. else
  1616. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1617. end
  1618. { implicit pointer object type comparison }
  1619. else if is_implicit_pointer_object_type(rd) or is_implicit_pointer_object_type(ld) then
  1620. begin
  1621. if (nodetype in [equaln,unequaln]) then
  1622. begin
  1623. if is_implicit_pointer_object_type(rd) and is_implicit_pointer_object_type(ld) then
  1624. begin
  1625. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1626. inserttypeconv(right,left.resultdef)
  1627. else
  1628. inserttypeconv(left,right.resultdef);
  1629. end
  1630. else if is_implicit_pointer_object_type(rd) then
  1631. inserttypeconv(left,right.resultdef)
  1632. else
  1633. inserttypeconv(right,left.resultdef);
  1634. end
  1635. else
  1636. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1637. end
  1638. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  1639. begin
  1640. if (nodetype in [equaln,unequaln]) then
  1641. begin
  1642. if tobjectdef(tclassrefdef(rd).pointeddef).is_related(
  1643. tobjectdef(tclassrefdef(ld).pointeddef)) then
  1644. inserttypeconv(right,left.resultdef)
  1645. else
  1646. inserttypeconv(left,right.resultdef);
  1647. end
  1648. else
  1649. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1650. end
  1651. { allow comparison with nil pointer }
  1652. else if is_implicit_pointer_object_type(rd) or (rd.typ=classrefdef) then
  1653. begin
  1654. if (nodetype in [equaln,unequaln]) then
  1655. inserttypeconv(left,right.resultdef)
  1656. else
  1657. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1658. end
  1659. else if is_implicit_pointer_object_type(ld) or (ld.typ=classrefdef) then
  1660. begin
  1661. if (nodetype in [equaln,unequaln]) then
  1662. inserttypeconv(right,left.resultdef)
  1663. else
  1664. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1665. end
  1666. { support procvar=nil,procvar<>nil }
  1667. else if ((ld.typ=procvardef) and (rt=niln)) or
  1668. ((rd.typ=procvardef) and (lt=niln)) then
  1669. begin
  1670. if not(nodetype in [equaln,unequaln]) then
  1671. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1672. { find proc field in methodpointer record }
  1673. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1674. if not assigned(hsym) then
  1675. internalerror(200412043);
  1676. { For methodpointers compare only tmethodpointer.proc }
  1677. if (rd.typ=procvardef) and
  1678. (not tprocvardef(rd).is_addressonly) then
  1679. begin
  1680. right:=csubscriptnode.create(
  1681. hsym,
  1682. ctypeconvnode.create_internal(right,methodpointertype));
  1683. typecheckpass(right);
  1684. end;
  1685. if (ld.typ=procvardef) and
  1686. (not tprocvardef(ld).is_addressonly) then
  1687. begin
  1688. left:=csubscriptnode.create(
  1689. hsym,
  1690. ctypeconvnode.create_internal(left,methodpointertype));
  1691. typecheckpass(left);
  1692. end;
  1693. end
  1694. { support dynamicarray=nil,dynamicarray<>nil }
  1695. else if (is_dynamic_array(ld) and (rt=niln)) or
  1696. (is_dynamic_array(rd) and (lt=niln)) or
  1697. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1698. begin
  1699. if not(nodetype in [equaln,unequaln]) then
  1700. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1701. end
  1702. {$ifdef SUPPORT_MMX}
  1703. { mmx support, this must be before the zero based array
  1704. check }
  1705. else if (cs_mmx in current_settings.localswitches) and
  1706. is_mmx_able_array(ld) and
  1707. is_mmx_able_array(rd) and
  1708. equal_defs(ld,rd) then
  1709. begin
  1710. case nodetype of
  1711. addn,subn,xorn,orn,andn:
  1712. ;
  1713. { mul is a little bit restricted }
  1714. muln:
  1715. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1716. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1717. else
  1718. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1719. end;
  1720. end
  1721. {$endif SUPPORT_MMX}
  1722. { vector support, this must be before the zero based array
  1723. check }
  1724. else if (cs_support_vectors in current_settings.globalswitches) and
  1725. is_vector(ld) and
  1726. is_vector(rd) and
  1727. equal_defs(ld,rd) then
  1728. begin
  1729. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  1730. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1731. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  1732. resultdef:=left.resultdef;
  1733. end
  1734. { this is a little bit dangerous, also the left type }
  1735. { pointer to should be checked! This broke the mmx support }
  1736. else if (rd.typ=pointerdef) or
  1737. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  1738. begin
  1739. if is_zero_based_array(rd) then
  1740. begin
  1741. resultdef:=tpointerdef.create(tarraydef(rd).elementdef);
  1742. inserttypeconv(right,resultdef);
  1743. end
  1744. else
  1745. resultdef:=right.resultdef;
  1746. inserttypeconv(left,sinttype);
  1747. if nodetype=addn then
  1748. begin
  1749. if not(cs_extsyntax in current_settings.moduleswitches) or
  1750. (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
  1751. not(cs_pointermath in current_settings.localswitches) and
  1752. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  1753. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1754. if (rd.typ=pointerdef) and
  1755. (tpointerdef(rd).pointeddef.size>1) then
  1756. begin
  1757. left:=caddnode.create(muln,left,
  1758. cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,true));
  1759. typecheckpass(left);
  1760. end;
  1761. end
  1762. else
  1763. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1764. end
  1765. else if (ld.typ=pointerdef) or
  1766. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  1767. begin
  1768. if is_zero_based_array(ld) then
  1769. begin
  1770. resultdef:=tpointerdef.create(tarraydef(ld).elementdef);
  1771. inserttypeconv(left,resultdef);
  1772. end
  1773. else
  1774. resultdef:=left.resultdef;
  1775. inserttypeconv(right,sinttype);
  1776. if nodetype in [addn,subn] then
  1777. begin
  1778. if (lt=niln) then
  1779. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),'NIL',rd.typename);
  1780. if not(cs_extsyntax in current_settings.moduleswitches) or
  1781. (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
  1782. not(cs_pointermath in current_settings.localswitches) and
  1783. not((ld.typ=pointerdef) and tpointerdef(ld).has_pointer_math)) then
  1784. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1785. if (ld.typ=pointerdef) then
  1786. begin
  1787. if is_big_untyped_addrnode(left) then
  1788. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1789. if (tpointerdef(ld).pointeddef.size>1) then
  1790. begin
  1791. right:=caddnode.create(muln,right,
  1792. cordconstnode.create(tpointerdef(ld).pointeddef.size,sinttype,true));
  1793. typecheckpass(right);
  1794. end
  1795. end else
  1796. if is_zero_based_array(ld) and
  1797. (tarraydef(ld).elementdef.size>1) then
  1798. begin
  1799. right:=caddnode.create(muln,right,
  1800. cordconstnode.create(tarraydef(ld).elementdef.size,sinttype,true));
  1801. typecheckpass(right);
  1802. end;
  1803. end
  1804. else
  1805. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1806. end
  1807. else if (rd.typ=procvardef) and
  1808. (ld.typ=procvardef) and
  1809. equal_defs(rd,ld) then
  1810. begin
  1811. if (nodetype in [equaln,unequaln]) then
  1812. begin
  1813. if tprocvardef(rd).is_addressonly then
  1814. begin
  1815. inserttypeconv_internal(right,voidpointertype);
  1816. inserttypeconv_internal(left,voidpointertype);
  1817. end
  1818. else
  1819. begin
  1820. { find proc field in methodpointer record }
  1821. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1822. if not assigned(hsym) then
  1823. internalerror(200412043);
  1824. { Compare tmehodpointer(left).proc }
  1825. right:=csubscriptnode.create(
  1826. hsym,
  1827. ctypeconvnode.create_internal(right,methodpointertype));
  1828. typecheckpass(right);
  1829. left:=csubscriptnode.create(
  1830. hsym,
  1831. ctypeconvnode.create_internal(left,methodpointertype));
  1832. typecheckpass(left);
  1833. end;
  1834. end
  1835. else
  1836. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1837. end
  1838. { enums }
  1839. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  1840. begin
  1841. if allowenumop(nodetype) then
  1842. inserttypeconv(right,left.resultdef)
  1843. else
  1844. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1845. end
  1846. { generic conversion, this is for error recovery }
  1847. else
  1848. begin
  1849. inserttypeconv(left,sinttype);
  1850. inserttypeconv(right,sinttype);
  1851. end;
  1852. if cmp_of_disjunct_ranges(res) then
  1853. begin
  1854. if res then
  1855. CGMessage(type_w_comparison_always_true)
  1856. else
  1857. CGMessage(type_w_comparison_always_false);
  1858. end;
  1859. { set resultdef if not already done }
  1860. if not assigned(resultdef) then
  1861. begin
  1862. case nodetype of
  1863. ltn,lten,gtn,gten,equaln,unequaln :
  1864. resultdef:=pasbool8type;
  1865. slashn :
  1866. resultdef:=resultrealdef;
  1867. addn:
  1868. begin
  1869. { for strings, return is always a 255 char string }
  1870. if is_shortstring(left.resultdef) then
  1871. resultdef:=cshortstringtype
  1872. else
  1873. { for ansistrings set resultdef to assignment left node
  1874. if it is an assignment and left node expects ansistring }
  1875. if is_ansistring(left.resultdef) and
  1876. assigned(aktassignmentnode) and
  1877. (aktassignmentnode.right=self) and
  1878. is_ansistring(aktassignmentnode.left.resultdef) then
  1879. resultdef:=aktassignmentnode.left.resultdef
  1880. else
  1881. resultdef:=left.resultdef;
  1882. end;
  1883. else
  1884. resultdef:=left.resultdef;
  1885. end;
  1886. end;
  1887. { when the result is currency we need some extra code for
  1888. multiplication and division. this should not be done when
  1889. the muln or slashn node is created internally }
  1890. if not(nf_is_currency in flags) and
  1891. is_currency(resultdef) then
  1892. begin
  1893. case nodetype of
  1894. slashn :
  1895. begin
  1896. { slashn will only work with floats }
  1897. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1898. include(hp.flags,nf_is_currency);
  1899. result:=hp;
  1900. end;
  1901. muln :
  1902. begin
  1903. if s64currencytype.typ=floatdef then
  1904. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1905. else
  1906. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1907. include(hp.flags,nf_is_currency);
  1908. result:=hp
  1909. end;
  1910. end;
  1911. end;
  1912. if not codegenerror and
  1913. not assigned(result) then
  1914. result:=simplify(false);
  1915. end;
  1916. function taddnode.first_addstring: tnode;
  1917. const
  1918. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  1919. var
  1920. p: tnode;
  1921. newstatement : tstatementnode;
  1922. tempnode (*,tempnode2*) : ttempcreatenode;
  1923. cmpfuncname: string;
  1924. para: tcallparanode;
  1925. begin
  1926. { when we get here, we are sure that both the left and the right }
  1927. { node are both strings of the same stringtype (JM) }
  1928. case nodetype of
  1929. addn:
  1930. begin
  1931. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  1932. begin
  1933. result:=right;
  1934. left.free;
  1935. left:=nil;
  1936. right:=nil;
  1937. exit;
  1938. end;
  1939. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  1940. begin
  1941. result:=left;
  1942. left:=nil;
  1943. right.free;
  1944. right:=nil;
  1945. exit;
  1946. end;
  1947. { create the call to the concat routine both strings as arguments }
  1948. if assigned(aktassignmentnode) and
  1949. (aktassignmentnode.right=self) and
  1950. (aktassignmentnode.left.resultdef=resultdef) and
  1951. valid_for_var(aktassignmentnode.left,false) then
  1952. begin
  1953. para:=ccallparanode.create(
  1954. right,
  1955. ccallparanode.create(
  1956. left,
  1957. ccallparanode.create(aktassignmentnode.left.getcopy,nil)
  1958. )
  1959. );
  1960. if is_ansistring(resultdef) then
  1961. para:=ccallparanode.create(
  1962. cordconstnode.create(
  1963. getparaencoding(resultdef),
  1964. u16inttype,
  1965. true
  1966. ),
  1967. para
  1968. );
  1969. result:=ccallnode.createintern(
  1970. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  1971. para
  1972. );
  1973. include(aktassignmentnode.flags,nf_assign_done_in_right);
  1974. firstpass(result);
  1975. end
  1976. else
  1977. begin
  1978. result:=internalstatements(newstatement);
  1979. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1980. addstatement(newstatement,tempnode);
  1981. para:=ccallparanode.create(
  1982. right,
  1983. ccallparanode.create(
  1984. left,
  1985. ccallparanode.create(ctemprefnode.create(tempnode),nil)
  1986. )
  1987. );
  1988. if is_ansistring(resultdef) then
  1989. para:=ccallparanode.create(
  1990. cordconstnode.create(
  1991. getparaencoding(resultdef),
  1992. u16inttype,
  1993. true
  1994. ),
  1995. para
  1996. );
  1997. addstatement(
  1998. newstatement,
  1999. ccallnode.createintern(
  2000. 'fpc_'+tstringdef(resultdef).stringtypname+'_concat',
  2001. para
  2002. )
  2003. );
  2004. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2005. addstatement(newstatement,ctemprefnode.create(tempnode));
  2006. end;
  2007. { we reused the arguments }
  2008. left := nil;
  2009. right := nil;
  2010. end;
  2011. ltn,lten,gtn,gten,equaln,unequaln :
  2012. begin
  2013. { generate better code for comparison with empty string, we
  2014. only need to compare the length with 0 }
  2015. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  2016. { windows widestrings are too complicated to be handled optimized }
  2017. not(is_widestring(left.resultdef) and (target_info.system in systems_windows)) and
  2018. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  2019. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  2020. begin
  2021. { switch so that the constant is always on the right }
  2022. if left.nodetype = stringconstn then
  2023. begin
  2024. p := left;
  2025. left := right;
  2026. right := p;
  2027. nodetype:=swap_relation[nodetype];
  2028. end;
  2029. if is_shortstring(left.resultdef) or
  2030. (nodetype in [gtn,gten,ltn,lten]) then
  2031. { compare the length with 0 }
  2032. result := caddnode.create(nodetype,
  2033. cinlinenode.create(in_length_x,false,left),
  2034. cordconstnode.create(0,s32inttype,false))
  2035. else
  2036. begin
  2037. (*
  2038. if is_widestring(left.resultdef) and
  2039. (target_info.system in system_windows) then
  2040. begin
  2041. { windows like widestrings requires that we also check the length }
  2042. result:=internalstatements(newstatement);
  2043. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  2044. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2045. addstatement(newstatement,tempnode);
  2046. addstatement(newstatement,tempnode2);
  2047. { poor man's cse }
  2048. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  2049. ctypeconvnode.create_internal(left,voidpointertype))
  2050. );
  2051. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  2052. caddnode.create(orn,
  2053. caddnode.create(nodetype,
  2054. ctemprefnode.create(tempnode),
  2055. cpointerconstnode.create(0,voidpointertype)
  2056. ),
  2057. caddnode.create(nodetype,
  2058. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  2059. cordconstnode.create(0,s32inttype,false)
  2060. )
  2061. )
  2062. ));
  2063. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  2064. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  2065. addstatement(newstatement,ctemprefnode.create(tempnode2));
  2066. end
  2067. else
  2068. *)
  2069. begin
  2070. { compare the pointer with nil (for ansistrings etc), }
  2071. { faster than getting the length (JM) }
  2072. result:= caddnode.create(nodetype,
  2073. ctypeconvnode.create_internal(left,voidpointertype),
  2074. cpointerconstnode.create(0,voidpointertype));
  2075. end;
  2076. end;
  2077. { left is reused }
  2078. left := nil;
  2079. { right isn't }
  2080. right.free;
  2081. right := nil;
  2082. exit;
  2083. end;
  2084. { no string constant -> call compare routine }
  2085. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  2086. { for equality checks use optimized version }
  2087. if nodetype in [equaln,unequaln] then
  2088. cmpfuncname := cmpfuncname + '_equal';
  2089. result := ccallnode.createintern(cmpfuncname,
  2090. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2091. { and compare its result with 0 according to the original operator }
  2092. result := caddnode.create(nodetype,result,
  2093. cordconstnode.create(0,s32inttype,false));
  2094. left := nil;
  2095. right := nil;
  2096. end;
  2097. end;
  2098. end;
  2099. function taddnode.first_addset : tnode;
  2100. procedure call_varset_helper(const n : string);
  2101. var
  2102. newstatement : tstatementnode;
  2103. temp : ttempcreatenode;
  2104. begin
  2105. { add two var sets }
  2106. result:=internalstatements(newstatement);
  2107. { create temp for result }
  2108. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2109. addstatement(newstatement,temp);
  2110. addstatement(newstatement,ccallnode.createintern(n,
  2111. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2112. ccallparanode.create(ctemprefnode.create(temp),
  2113. ccallparanode.create(right,
  2114. ccallparanode.create(left,nil)))))
  2115. );
  2116. { remove reused parts from original node }
  2117. left:=nil;
  2118. right:=nil;
  2119. { the last statement should return the value as
  2120. location and type, this is done be referencing the
  2121. temp and converting it first from a persistent temp to
  2122. normal temp }
  2123. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2124. addstatement(newstatement,ctemprefnode.create(temp));
  2125. end;
  2126. var
  2127. procname: string[31];
  2128. tempn: tnode;
  2129. newstatement : tstatementnode;
  2130. temp : ttempcreatenode;
  2131. begin
  2132. result:=nil;
  2133. case nodetype of
  2134. equaln,unequaln,lten,gten:
  2135. begin
  2136. case nodetype of
  2137. equaln,unequaln:
  2138. procname := 'fpc_varset_comp_sets';
  2139. lten,gten:
  2140. begin
  2141. procname := 'fpc_varset_contains_sets';
  2142. { (left >= right) = (right <= left) }
  2143. if nodetype = gten then
  2144. begin
  2145. tempn := left;
  2146. left := right;
  2147. right := tempn;
  2148. end;
  2149. end;
  2150. end;
  2151. result := ccallnode.createinternres(procname,
  2152. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  2153. ccallparanode.create(right,
  2154. ccallparanode.create(left,nil))),resultdef);
  2155. { left and right are reused as parameters }
  2156. left := nil;
  2157. right := nil;
  2158. { for an unequaln, we have to negate the result of comp_sets }
  2159. if nodetype = unequaln then
  2160. result := cnotnode.create(result);
  2161. end;
  2162. addn:
  2163. begin
  2164. { optimize first loading of a set }
  2165. if (right.nodetype=setelementn) and
  2166. not(assigned(tsetelementnode(right).right)) and
  2167. is_emptyset(left) then
  2168. begin
  2169. result:=internalstatements(newstatement);
  2170. { create temp for result }
  2171. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2172. addstatement(newstatement,temp);
  2173. { adjust for set base }
  2174. tsetelementnode(right).left:=caddnode.create(subn,
  2175. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2176. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2177. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  2178. ccallparanode.create(ctemprefnode.create(temp),
  2179. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2180. ccallparanode.create(tsetelementnode(right).left,nil))))
  2181. );
  2182. { the last statement should return the value as
  2183. location and type, this is done be referencing the
  2184. temp and converting it first from a persistent temp to
  2185. normal temp }
  2186. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2187. addstatement(newstatement,ctemprefnode.create(temp));
  2188. tsetelementnode(right).left := nil;
  2189. end
  2190. else
  2191. begin
  2192. if right.nodetype=setelementn then
  2193. begin
  2194. result:=internalstatements(newstatement);
  2195. { create temp for result }
  2196. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2197. addstatement(newstatement,temp);
  2198. { adjust for set base }
  2199. tsetelementnode(right).left:=caddnode.create(subn,
  2200. ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2201. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2202. { add a range or a single element? }
  2203. if assigned(tsetelementnode(right).right) then
  2204. begin
  2205. { adjust for set base }
  2206. tsetelementnode(right).right:=caddnode.create(subn,
  2207. ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  2208. cordconstnode.create(tsetdef(resultdef).setbase,sinttype,false));
  2209. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  2210. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2211. ccallparanode.create(tsetelementnode(right).right,
  2212. ccallparanode.create(tsetelementnode(right).left,
  2213. ccallparanode.create(ctemprefnode.create(temp),
  2214. ccallparanode.create(left,nil))))))
  2215. );
  2216. end
  2217. else
  2218. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  2219. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2220. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2221. ccallparanode.create(ctemprefnode.create(temp),
  2222. ccallparanode.create(left,nil)))))
  2223. );
  2224. { remove reused parts from original node }
  2225. tsetelementnode(right).right:=nil;
  2226. tsetelementnode(right).left:=nil;
  2227. left:=nil;
  2228. { the last statement should return the value as
  2229. location and type, this is done be referencing the
  2230. temp and converting it first from a persistent temp to
  2231. normal temp }
  2232. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2233. addstatement(newstatement,ctemprefnode.create(temp));
  2234. end
  2235. else
  2236. call_varset_helper('fpc_varset_add_sets');
  2237. end
  2238. end;
  2239. subn:
  2240. call_varset_helper('fpc_varset_sub_sets');
  2241. symdifn:
  2242. call_varset_helper('fpc_varset_symdif_sets');
  2243. muln:
  2244. call_varset_helper('fpc_varset_mul_sets');
  2245. else
  2246. internalerror(200609241);
  2247. end;
  2248. end;
  2249. function taddnode.use_generic_mul32to64: boolean;
  2250. begin
  2251. result := true;
  2252. end;
  2253. function taddnode.try_make_mul32to64: boolean;
  2254. function canbe32bitint(v: tconstexprint): boolean;
  2255. begin
  2256. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  2257. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))))
  2258. end;
  2259. var
  2260. temp: tnode;
  2261. begin
  2262. result := false;
  2263. if ((left.nodetype = typeconvn) and
  2264. is_integer(ttypeconvnode(left).left.resultdef) and
  2265. (not(torddef(ttypeconvnode(left).left.resultdef).ordtype in [u64bit,s64bit])) and
  2266. (((right.nodetype = ordconstn) and canbe32bitint(tordconstnode(right).value)) or
  2267. ((right.nodetype = typeconvn) and
  2268. is_integer(ttypeconvnode(right).left.resultdef) and
  2269. not(torddef(ttypeconvnode(right).left.resultdef).ordtype in [u64bit,s64bit])) and
  2270. ((is_signed(ttypeconvnode(left).left.resultdef) =
  2271. is_signed(ttypeconvnode(right).left.resultdef)) or
  2272. (is_signed(ttypeconvnode(left).left.resultdef) and
  2273. (torddef(ttypeconvnode(right).left.resultdef).ordtype in [u8bit,u16bit]))))) then
  2274. begin
  2275. temp := ttypeconvnode(left).left;
  2276. ttypeconvnode(left).left := nil;
  2277. left.free;
  2278. left := temp;
  2279. if (right.nodetype = typeconvn) then
  2280. begin
  2281. temp := ttypeconvnode(right).left;
  2282. ttypeconvnode(right).left := nil;
  2283. right.free;
  2284. right := temp;
  2285. end;
  2286. if (is_signed(left.resultdef)) then
  2287. begin
  2288. inserttypeconv(left,s32inttype);
  2289. inserttypeconv(right,s32inttype);
  2290. end
  2291. else
  2292. begin
  2293. inserttypeconv(left,u32inttype);
  2294. inserttypeconv(right,u32inttype);
  2295. end;
  2296. firstpass(left);
  2297. firstpass(right);
  2298. result := true;
  2299. end;
  2300. end;
  2301. function taddnode.first_add64bitint: tnode;
  2302. var
  2303. procname: string[31];
  2304. temp: tnode;
  2305. power: longint;
  2306. begin
  2307. result := nil;
  2308. { create helper calls mul }
  2309. if nodetype <> muln then
  2310. exit;
  2311. { make sure that if there is a constant, that it's on the right }
  2312. if left.nodetype = ordconstn then
  2313. begin
  2314. temp := right;
  2315. right := left;
  2316. left := temp;
  2317. end;
  2318. { can we use a shift instead of a mul? }
  2319. if not (cs_check_overflow in current_settings.localswitches) and
  2320. (right.nodetype = ordconstn) and
  2321. ispowerof2(tordconstnode(right).value,power) then
  2322. begin
  2323. tordconstnode(right).value := power;
  2324. result := cshlshrnode.create(shln,left,right);
  2325. { left and right are reused }
  2326. left := nil;
  2327. right := nil;
  2328. { return firstpassed new node }
  2329. exit;
  2330. end;
  2331. if not(use_generic_mul32to64) and
  2332. try_make_mul32to64 then
  2333. exit;
  2334. { when currency is used set the result of the
  2335. parameters to s64bit, so they are not converted }
  2336. if is_currency(resultdef) then
  2337. begin
  2338. left.resultdef:=s64inttype;
  2339. right.resultdef:=s64inttype;
  2340. end;
  2341. { otherwise, create the parameters for the helper }
  2342. right := ccallparanode.create(
  2343. cordconstnode.create(ord(cs_check_overflow in current_settings.localswitches),pasbool8type,true),
  2344. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2345. left := nil;
  2346. { only qword needs the unsigned code, the
  2347. signed code is also used for currency }
  2348. if is_signed(resultdef) then
  2349. procname := 'fpc_mul_int64'
  2350. else
  2351. procname := 'fpc_mul_qword';
  2352. result := ccallnode.createintern(procname,right);
  2353. right := nil;
  2354. end;
  2355. function taddnode.first_addfloat : tnode;
  2356. var
  2357. procname: string[31];
  2358. { do we need to reverse the result ? }
  2359. notnode : boolean;
  2360. fdef : tdef;
  2361. begin
  2362. result := nil;
  2363. notnode := false;
  2364. { In non-emulation mode, real opcodes are
  2365. emitted for floating point values.
  2366. }
  2367. if not (cs_fp_emulation in current_settings.moduleswitches) then
  2368. exit;
  2369. if not(target_info.system in systems_wince) then
  2370. begin
  2371. case tfloatdef(left.resultdef).floattype of
  2372. s32real:
  2373. begin
  2374. fdef:=search_system_type('FLOAT32REC').typedef;
  2375. procname:='float32';
  2376. end;
  2377. s64real:
  2378. begin
  2379. fdef:=search_system_type('FLOAT64').typedef;
  2380. procname:='float64';
  2381. end;
  2382. {!!! not yet implemented
  2383. s128real:
  2384. }
  2385. else
  2386. internalerror(2005082601);
  2387. end;
  2388. case nodetype of
  2389. addn:
  2390. procname:=procname+'_add';
  2391. muln:
  2392. procname:=procname+'_mul';
  2393. subn:
  2394. procname:=procname+'_sub';
  2395. slashn:
  2396. procname:=procname+'_div';
  2397. ltn:
  2398. procname:=procname+'_lt';
  2399. lten:
  2400. procname:=procname+'_le';
  2401. gtn:
  2402. begin
  2403. procname:=procname+'_le';
  2404. notnode:=true;
  2405. end;
  2406. gten:
  2407. begin
  2408. procname:=procname+'_lt';
  2409. notnode:=true;
  2410. end;
  2411. equaln:
  2412. procname:=procname+'_eq';
  2413. unequaln:
  2414. begin
  2415. procname:=procname+'_eq';
  2416. notnode:=true;
  2417. end;
  2418. else
  2419. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2420. end;
  2421. end
  2422. else
  2423. begin
  2424. case nodetype of
  2425. addn:
  2426. procname:='ADD';
  2427. muln:
  2428. procname:='MUL';
  2429. subn:
  2430. procname:='SUB';
  2431. slashn:
  2432. procname:='DIV';
  2433. ltn:
  2434. procname:='LT';
  2435. lten:
  2436. procname:='LE';
  2437. gtn:
  2438. procname:='GT';
  2439. gten:
  2440. procname:='GE';
  2441. equaln:
  2442. procname:='EQ';
  2443. unequaln:
  2444. procname:='NE';
  2445. else
  2446. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2447. end;
  2448. case tfloatdef(left.resultdef).floattype of
  2449. s32real:
  2450. begin
  2451. procname:=procname+'S';
  2452. if nodetype in [addn,muln,subn,slashn] then
  2453. procname:=lower(procname);
  2454. end;
  2455. s64real:
  2456. procname:=procname+'D';
  2457. {!!! not yet implemented
  2458. s128real:
  2459. }
  2460. else
  2461. internalerror(2005082602);
  2462. end;
  2463. end;
  2464. { cast softfpu result? }
  2465. if not(target_info.system in systems_wince) then
  2466. begin
  2467. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2468. resultdef:=pasbool8type;
  2469. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  2470. ctypeconvnode.create_internal(right,fdef),
  2471. ccallparanode.create(
  2472. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  2473. end
  2474. else
  2475. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  2476. ccallparanode.create(left,nil)));
  2477. left:=nil;
  2478. right:=nil;
  2479. { do we need to reverse the result }
  2480. if notnode then
  2481. result:=cnotnode.create(result);
  2482. end;
  2483. function taddnode.pass_1 : tnode;
  2484. var
  2485. {$ifdef addstringopt}
  2486. hp : tnode;
  2487. {$endif addstringopt}
  2488. rd,ld : tdef;
  2489. i : longint;
  2490. lt,rt : tnodetype;
  2491. {$ifdef cpuneedsmulhelper}
  2492. procname : string[32];
  2493. {$endif cpuneedsmulhelper}
  2494. begin
  2495. result:=nil;
  2496. { Can we optimize multiple string additions into a single call?
  2497. This need to be done on a complete tree to detect the multiple
  2498. add nodes and is therefor done before the subtrees are processed }
  2499. if canbemultistringadd(self) then
  2500. begin
  2501. result := genmultistringadd(self);
  2502. exit;
  2503. end;
  2504. { first do the two subtrees }
  2505. firstpass(left);
  2506. firstpass(right);
  2507. if codegenerror then
  2508. exit;
  2509. { load easier access variables }
  2510. rd:=right.resultdef;
  2511. ld:=left.resultdef;
  2512. rt:=right.nodetype;
  2513. lt:=left.nodetype;
  2514. { int/int gives real/real! }
  2515. if nodetype=slashn then
  2516. begin
  2517. {$ifdef cpufpemu}
  2518. if (current_settings.fputype=fpu_soft) or (cs_fp_emulation in current_settings.moduleswitches) then
  2519. begin
  2520. result:=first_addfloat;
  2521. if assigned(result) then
  2522. exit;
  2523. end;
  2524. {$endif cpufpemu}
  2525. expectloc:=LOC_FPUREGISTER;
  2526. end
  2527. { if both are orddefs then check sub types }
  2528. else if (ld.typ=orddef) and (rd.typ=orddef) then
  2529. begin
  2530. { optimize multiplacation by a power of 2 }
  2531. if not(cs_check_overflow in current_settings.localswitches) and
  2532. (nodetype = muln) and
  2533. (((left.nodetype = ordconstn) and
  2534. ispowerof2(tordconstnode(left).value,i)) or
  2535. ((right.nodetype = ordconstn) and
  2536. ispowerof2(tordconstnode(right).value,i))) then
  2537. begin
  2538. if left.nodetype = ordconstn then
  2539. begin
  2540. tordconstnode(left).value := i;
  2541. result := cshlshrnode.create(shln,right,left);
  2542. end
  2543. else
  2544. begin
  2545. tordconstnode(right).value := i;
  2546. result := cshlshrnode.create(shln,left,right);
  2547. end;
  2548. left := nil;
  2549. right := nil;
  2550. exit;
  2551. end;
  2552. { 2 booleans ? }
  2553. if is_boolean(ld) and is_boolean(rd) then
  2554. begin
  2555. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  2556. (nf_short_bool in flags)) and
  2557. (nodetype in [andn,orn]) then
  2558. expectloc:=LOC_JUMP
  2559. else
  2560. begin
  2561. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2562. expectloc:=LOC_FLAGS
  2563. else
  2564. expectloc:=LOC_REGISTER;
  2565. end;
  2566. end
  2567. else
  2568. { Both are chars? only convert to shortstrings for addn }
  2569. if is_char(ld) then
  2570. begin
  2571. if nodetype=addn then
  2572. internalerror(200103291);
  2573. expectloc:=LOC_FLAGS;
  2574. end
  2575. {$ifndef cpu64bitaddr}
  2576. { is there a 64 bit type ? }
  2577. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  2578. begin
  2579. result := first_add64bitint;
  2580. if assigned(result) then
  2581. exit;
  2582. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2583. expectloc:=LOC_REGISTER
  2584. else
  2585. expectloc:=LOC_JUMP;
  2586. end
  2587. {$endif cpu64bitaddr}
  2588. {$ifndef cpuneedsmulhelper}
  2589. { is there a cardinal? }
  2590. else if (torddef(ld).ordtype=u32bit) 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. {$endif cpuneedsmulhelper}
  2598. { generic s32bit conversion }
  2599. else
  2600. begin
  2601. {$ifdef cpuneedsmulhelper}
  2602. if (nodetype=muln) and not(torddef(resultdef).ordtype in [u8bit,s8bit]) then
  2603. begin
  2604. result := nil;
  2605. case torddef(resultdef).ordtype of
  2606. s16bit:
  2607. procname := 'fpc_mul_integer';
  2608. u16bit:
  2609. procname := 'fpc_mul_word';
  2610. s32bit:
  2611. procname := 'fpc_mul_longint';
  2612. u32bit:
  2613. procname := 'fpc_mul_dword';
  2614. else
  2615. internalerror(2011022301);
  2616. end;
  2617. result := ccallnode.createintern(procname,
  2618. ccallparanode.create(cordconstnode.create(0,pasbool8type,false),
  2619. ccallparanode.create(right,
  2620. ccallparanode.create(left,nil))));
  2621. left := nil;
  2622. right := nil;
  2623. firstpass(result);
  2624. exit;
  2625. end;
  2626. {$endif cpuneedsmulhelper}
  2627. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2628. expectloc:=LOC_REGISTER
  2629. else
  2630. expectloc:=LOC_FLAGS;
  2631. end;
  2632. end
  2633. { left side a setdef, must be before string processing,
  2634. else array constructor can be seen as array of char (PFV) }
  2635. else if (ld.typ=setdef) then
  2636. begin
  2637. { small sets are handled inline by the compiler.
  2638. small set doesn't have support for adding ranges }
  2639. if is_smallset(ld) and
  2640. not(
  2641. (right.nodetype=setelementn) and
  2642. assigned(tsetelementnode(right).right)
  2643. ) then
  2644. begin
  2645. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2646. expectloc:=LOC_FLAGS
  2647. else
  2648. expectloc:=LOC_REGISTER;
  2649. end
  2650. else
  2651. begin
  2652. result := first_addset;
  2653. if assigned(result) then
  2654. exit;
  2655. expectloc:=LOC_CREFERENCE;
  2656. end;
  2657. end
  2658. { compare pchar by addresses like BP/Delphi }
  2659. else if is_pchar(ld) then
  2660. begin
  2661. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2662. expectloc:=LOC_REGISTER
  2663. else
  2664. expectloc:=LOC_FLAGS;
  2665. end
  2666. { is one of the operands a string }
  2667. else if (ld.typ=stringdef) then
  2668. begin
  2669. if is_widestring(ld) then
  2670. begin
  2671. { this is only for add, the comparisaion is handled later }
  2672. expectloc:=LOC_REGISTER;
  2673. end
  2674. else if is_unicodestring(ld) then
  2675. begin
  2676. { this is only for add, the comparisaion is handled later }
  2677. expectloc:=LOC_REGISTER;
  2678. end
  2679. else if is_ansistring(ld) then
  2680. begin
  2681. { this is only for add, the comparisaion is handled later }
  2682. expectloc:=LOC_REGISTER;
  2683. end
  2684. else if is_longstring(ld) then
  2685. begin
  2686. { this is only for add, the comparisaion is handled later }
  2687. expectloc:=LOC_REFERENCE;
  2688. end
  2689. else
  2690. begin
  2691. {$ifdef addstringopt}
  2692. { can create a call which isn't handled by callparatemp }
  2693. if canbeaddsstringcharoptnode(self) then
  2694. begin
  2695. hp := genaddsstringcharoptnode(self);
  2696. pass_1 := hp;
  2697. exit;
  2698. end
  2699. else
  2700. {$endif addstringopt}
  2701. begin
  2702. { Fix right to be shortstring }
  2703. if is_char(right.resultdef) then
  2704. begin
  2705. inserttypeconv(right,cshortstringtype);
  2706. firstpass(right);
  2707. end;
  2708. end;
  2709. {$ifdef addstringopt}
  2710. { can create a call which isn't handled by callparatemp }
  2711. if canbeaddsstringcsstringoptnode(self) then
  2712. begin
  2713. hp := genaddsstringcsstringoptnode(self);
  2714. pass_1 := hp;
  2715. exit;
  2716. end;
  2717. {$endif addstringopt}
  2718. end;
  2719. { otherwise, let addstring convert everything }
  2720. result := first_addstring;
  2721. exit;
  2722. end
  2723. { is one a real float ? }
  2724. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  2725. begin
  2726. {$ifdef cpufpemu}
  2727. if (current_settings.fputype=fpu_soft) or (cs_fp_emulation in current_settings.moduleswitches) then
  2728. begin
  2729. result:=first_addfloat;
  2730. if assigned(result) then
  2731. exit;
  2732. end;
  2733. {$endif cpufpemu}
  2734. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2735. expectloc:=LOC_FPUREGISTER
  2736. else
  2737. expectloc:=LOC_FLAGS;
  2738. end
  2739. { pointer comperation and subtraction }
  2740. else if (ld.typ=pointerdef) then
  2741. begin
  2742. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2743. expectloc:=LOC_REGISTER
  2744. else
  2745. expectloc:=LOC_FLAGS;
  2746. end
  2747. else if is_implicit_pointer_object_type(ld) then
  2748. begin
  2749. expectloc:=LOC_FLAGS;
  2750. end
  2751. else if (ld.typ=classrefdef) then
  2752. begin
  2753. expectloc:=LOC_FLAGS;
  2754. end
  2755. { support procvar=nil,procvar<>nil }
  2756. else if ((ld.typ=procvardef) and (rt=niln)) or
  2757. ((rd.typ=procvardef) and (lt=niln)) then
  2758. begin
  2759. expectloc:=LOC_FLAGS;
  2760. end
  2761. {$ifdef SUPPORT_MMX}
  2762. { mmx support, this must be before the zero based array
  2763. check }
  2764. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  2765. is_mmx_able_array(rd) then
  2766. begin
  2767. expectloc:=LOC_MMXREGISTER;
  2768. end
  2769. {$endif SUPPORT_MMX}
  2770. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  2771. begin
  2772. expectloc:=LOC_REGISTER;
  2773. end
  2774. else if (rd.typ=procvardef) and
  2775. (ld.typ=procvardef) and
  2776. equal_defs(rd,ld) then
  2777. begin
  2778. expectloc:=LOC_FLAGS;
  2779. end
  2780. else if (ld.typ=enumdef) then
  2781. begin
  2782. expectloc:=LOC_FLAGS;
  2783. end
  2784. {$ifdef SUPPORT_MMX}
  2785. else if (cs_mmx in current_settings.localswitches) and
  2786. is_mmx_able_array(ld) and
  2787. is_mmx_able_array(rd) then
  2788. begin
  2789. expectloc:=LOC_MMXREGISTER;
  2790. end
  2791. {$endif SUPPORT_MMX}
  2792. { the general solution is to convert to 32 bit int }
  2793. else
  2794. begin
  2795. expectloc:=LOC_REGISTER;
  2796. end;
  2797. end;
  2798. {$ifdef state_tracking}
  2799. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2800. var factval:Tnode;
  2801. begin
  2802. track_state_pass:=false;
  2803. if left.track_state_pass(exec_known) then
  2804. begin
  2805. track_state_pass:=true;
  2806. left.resultdef:=nil;
  2807. do_typecheckpass(left);
  2808. end;
  2809. factval:=aktstate.find_fact(left);
  2810. if factval<>nil then
  2811. begin
  2812. track_state_pass:=true;
  2813. left.destroy;
  2814. left:=factval.getcopy;
  2815. end;
  2816. if right.track_state_pass(exec_known) then
  2817. begin
  2818. track_state_pass:=true;
  2819. right.resultdef:=nil;
  2820. do_typecheckpass(right);
  2821. end;
  2822. factval:=aktstate.find_fact(right);
  2823. if factval<>nil then
  2824. begin
  2825. track_state_pass:=true;
  2826. right.destroy;
  2827. right:=factval.getcopy;
  2828. end;
  2829. end;
  2830. {$endif}
  2831. end.