nadd.pas 103 KB

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