nadd.pas 109 KB

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