nadd.pas 114 KB

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