nadd.pas 109 KB

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