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