nadd.pas 121 KB

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