nadd.pas 111 KB

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