nadd.pas 111 KB

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