nadd.pas 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397
  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 MACOS_USE_FAKE_SYSUTILS}
  69. sysutils,
  70. {$ENDIF MACOS_USE_FAKE_SYSUTILS}
  71. globtype,systems,
  72. cutils,verbose,globals,widestr,
  73. symconst,symdef,symsym,symtable,defutil,defcmp,
  74. cgbase,
  75. htypechk,pass_1,
  76. nbas,nmat,ncnv,ncon,nset,nopt,ncal,ninl,nmem,nutils,
  77. {$ifdef state_tracking}
  78. nstate,
  79. {$endif}
  80. cpuinfo,procinfo;
  81. {*****************************************************************************
  82. TADDNODE
  83. *****************************************************************************}
  84. {$ifdef fpc}
  85. {$maxfpuregisters 0}
  86. {$endif fpc}
  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;
  503. s2:=@c2;
  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;
  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;
  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. { 2 booleans? Make them equal to the largest boolean }
  766. if is_boolean(ld) and is_boolean(rd) then
  767. begin
  768. if torddef(left.resulttype.def).size>torddef(right.resulttype.def).size then
  769. begin
  770. right:=ctypeconvnode.create_internal(right,left.resulttype);
  771. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  772. resulttypepass(right);
  773. end
  774. else if torddef(left.resulttype.def).size<torddef(right.resulttype.def).size then
  775. begin
  776. left:=ctypeconvnode.create_internal(left,right.resulttype);
  777. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  778. resulttypepass(left);
  779. end;
  780. case nodetype of
  781. xorn,
  782. ltn,
  783. lten,
  784. gtn,
  785. gten,
  786. andn,
  787. orn:
  788. begin
  789. end;
  790. unequaln,
  791. equaln:
  792. begin
  793. if not(cs_full_boolean_eval in aktlocalswitches) then
  794. begin
  795. { Remove any compares with constants }
  796. if (left.nodetype=ordconstn) then
  797. begin
  798. hp:=right;
  799. b:=(tordconstnode(left).value<>0);
  800. ot:=nodetype;
  801. left.free;
  802. left:=nil;
  803. right:=nil;
  804. if (not(b) and (ot=equaln)) or
  805. (b and (ot=unequaln)) then
  806. begin
  807. hp:=cnotnode.create(hp);
  808. end;
  809. result:=hp;
  810. exit;
  811. end;
  812. if (right.nodetype=ordconstn) then
  813. begin
  814. hp:=left;
  815. b:=(tordconstnode(right).value<>0);
  816. ot:=nodetype;
  817. right.free;
  818. right:=nil;
  819. left:=nil;
  820. if (not(b) and (ot=equaln)) or
  821. (b and (ot=unequaln)) then
  822. begin
  823. hp:=cnotnode.create(hp);
  824. end;
  825. result:=hp;
  826. exit;
  827. end;
  828. end;
  829. end;
  830. else
  831. begin
  832. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  833. result:=cnothingnode.create;
  834. exit;
  835. end;
  836. end;
  837. end
  838. { Both are chars? }
  839. else if is_char(rd) and is_char(ld) then
  840. begin
  841. if nodetype=addn then
  842. begin
  843. resulttype:=cshortstringtype;
  844. if not(is_constcharnode(left) and is_constcharnode(right)) then
  845. begin
  846. inserttypeconv(left,cshortstringtype);
  847. {$ifdef addstringopt}
  848. hp := genaddsstringcharoptnode(self);
  849. result := hp;
  850. exit;
  851. {$endif addstringopt}
  852. end;
  853. end;
  854. end
  855. { There is a widechar? }
  856. else if is_widechar(rd) or is_widechar(ld) then
  857. begin
  858. { widechar+widechar gives widestring }
  859. if nodetype=addn then
  860. begin
  861. inserttypeconv(left,cwidestringtype);
  862. if (torddef(rd).typ<>uwidechar) then
  863. inserttypeconv(right,cwidechartype);
  864. resulttype:=cwidestringtype;
  865. end
  866. else
  867. begin
  868. if (torddef(ld).typ<>uwidechar) then
  869. inserttypeconv(left,cwidechartype);
  870. if (torddef(rd).typ<>uwidechar) then
  871. inserttypeconv(right,cwidechartype);
  872. end;
  873. end
  874. { is there a currency type ? }
  875. else if ((torddef(rd).typ=scurrency) or (torddef(ld).typ=scurrency)) then
  876. begin
  877. if (torddef(ld).typ<>scurrency) then
  878. inserttypeconv(left,s64currencytype);
  879. if (torddef(rd).typ<>scurrency) then
  880. inserttypeconv(right,s64currencytype);
  881. end
  882. { and,or,xor work on bit patterns and don't care
  883. about the sign of integers }
  884. else if (nodetype in [andn,orn,xorn]) and
  885. is_integer(ld) and is_integer(rd) then
  886. begin
  887. if rd.size>ld.size then
  888. inserttypeconv_internal(left,right.resulttype)
  889. else
  890. inserttypeconv_internal(right,left.resulttype);
  891. end
  892. { is there a signed 64 bit type ? }
  893. else if ((torddef(rd).typ=s64bit) or (torddef(ld).typ=s64bit)) then
  894. begin
  895. if (torddef(ld).typ<>s64bit) then
  896. inserttypeconv(left,s64inttype);
  897. if (torddef(rd).typ<>s64bit) then
  898. inserttypeconv(right,s64inttype);
  899. end
  900. { is there a unsigned 64 bit type ? }
  901. else if ((torddef(rd).typ=u64bit) or (torddef(ld).typ=u64bit)) then
  902. begin
  903. if (torddef(ld).typ<>u64bit) then
  904. inserttypeconv(left,u64inttype);
  905. if (torddef(rd).typ<>u64bit) then
  906. inserttypeconv(right,u64inttype);
  907. end
  908. { 64 bit cpus do calculations always in 64 bit }
  909. {$ifndef cpu64bit}
  910. { is there a cardinal? }
  911. else if ((torddef(rd).typ=u32bit) or (torddef(ld).typ=u32bit)) then
  912. begin
  913. { convert positive constants to u32bit }
  914. if (torddef(ld).typ<>u32bit) and
  915. is_constintnode(left) and
  916. (tordconstnode(left).value >= 0) then
  917. inserttypeconv(left,u32inttype);
  918. if (torddef(rd).typ<>u32bit) and
  919. is_constintnode(right) and
  920. (tordconstnode(right).value >= 0) then
  921. inserttypeconv(right,u32inttype);
  922. { when one of the operand is signed perform
  923. the operation in 64bit, can't use rd/ld here because there
  924. could be already typeconvs inserted }
  925. if is_signed(left.resulttype.def) or
  926. is_signed(right.resulttype.def) then
  927. begin
  928. CGMessage(type_w_mixed_signed_unsigned);
  929. inserttypeconv(left,s64inttype);
  930. inserttypeconv(right,s64inttype);
  931. end
  932. else
  933. begin
  934. if (torddef(left.resulttype.def).typ<>u32bit) then
  935. inserttypeconv(left,u32inttype);
  936. if (torddef(right.resulttype.def).typ<>u32bit) then
  937. inserttypeconv(right,u32inttype);
  938. end;
  939. end
  940. {$endif cpu64bit}
  941. { generic ord conversion is sinttype }
  942. else
  943. begin
  944. { if the left or right value is smaller than the normal
  945. type sinttype and is unsigned, and the other value
  946. is a constant < 0, the result will always be false/true
  947. for equal / unequal nodes.
  948. }
  949. if (
  950. { left : unsigned ordinal var, right : < 0 constant }
  951. (
  952. ((is_signed(ld)=false) and (is_constintnode(left) =false)) and
  953. ((is_constintnode(right)) and (tordconstnode(right).value < 0))
  954. ) or
  955. { right : unsigned ordinal var, left : < 0 constant }
  956. (
  957. ((is_signed(rd)=false) and (is_constintnode(right) =false)) and
  958. ((is_constintnode(left)) and (tordconstnode(left).value < 0))
  959. )
  960. ) then
  961. begin
  962. if nodetype = equaln then
  963. CGMessage(type_w_signed_unsigned_always_false)
  964. else
  965. if nodetype = unequaln then
  966. CGMessage(type_w_signed_unsigned_always_true)
  967. else
  968. if (is_constintnode(left) and (nodetype in [ltn,lten])) or
  969. (is_constintnode(right) and (nodetype in [gtn,gten])) then
  970. CGMessage(type_w_signed_unsigned_always_true)
  971. else
  972. if (is_constintnode(right) and (nodetype in [ltn,lten])) or
  973. (is_constintnode(left) and (nodetype in [gtn,gten])) then
  974. CGMessage(type_w_signed_unsigned_always_false);
  975. end;
  976. { When there is a signed type or there is a minus operation
  977. we convert to signed int. Otherwise (both are unsigned) we keep
  978. the result also unsigned. This is compatible with Delphi (PFV) }
  979. if is_signed(ld) or
  980. is_signed(rd) or
  981. (nodetype=subn) then
  982. begin
  983. inserttypeconv(right,sinttype);
  984. inserttypeconv(left,sinttype);
  985. end
  986. else
  987. begin
  988. inserttypeconv(right,uinttype);
  989. inserttypeconv(left,uinttype);
  990. end;
  991. end;
  992. end
  993. { if both are floatdefs, conversion is already done before constant folding }
  994. else if (ld.deftype=floatdef) then
  995. begin
  996. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  997. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  998. end
  999. { left side a setdef, must be before string processing,
  1000. else array constructor can be seen as array of char (PFV) }
  1001. else if (ld.deftype=setdef) then
  1002. begin
  1003. { trying to add a set element? }
  1004. if (nodetype=addn) and (rd.deftype<>setdef) then
  1005. begin
  1006. if (rt=setelementn) then
  1007. begin
  1008. if not(equal_defs(tsetdef(ld).elementtype.def,rd)) then
  1009. CGMessage(type_e_set_element_are_not_comp);
  1010. end
  1011. else
  1012. CGMessage(type_e_mismatch)
  1013. end
  1014. else
  1015. begin
  1016. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1017. CGMessage(type_e_set_operation_unknown);
  1018. { right def must be a also be set }
  1019. if (rd.deftype<>setdef) or not(equal_defs(rd,ld)) then
  1020. CGMessage(type_e_set_element_are_not_comp);
  1021. end;
  1022. { ranges require normsets }
  1023. if (tsetdef(ld).settype=smallset) and
  1024. (rt=setelementn) and
  1025. assigned(tsetelementnode(right).right) then
  1026. begin
  1027. { generate a temporary normset def, it'll be destroyed
  1028. when the symtable is unloaded }
  1029. htype.setdef(tsetdef.create(tsetdef(ld).elementtype,255));
  1030. inserttypeconv(left,htype);
  1031. end;
  1032. { if the right side is also a setdef then the settype must
  1033. be the same as the left setdef }
  1034. if (rd.deftype=setdef) and
  1035. (tsetdef(ld).settype<>tsetdef(rd).settype) then
  1036. begin
  1037. { when right is a normset we need to typecast both
  1038. to normsets }
  1039. if (tsetdef(rd).settype=normset) then
  1040. inserttypeconv(left,right.resulttype)
  1041. else
  1042. inserttypeconv(right,left.resulttype);
  1043. end;
  1044. end
  1045. { pointer comparision and subtraction }
  1046. else if (
  1047. (rd.deftype=pointerdef) and (ld.deftype=pointerdef)
  1048. ) or
  1049. { compare/add pchar to variable (not stringconst) char arrays
  1050. by addresses like BP/Delphi }
  1051. (
  1052. (nodetype in [equaln,unequaln,subn,addn]) and
  1053. (
  1054. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1055. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1056. )
  1057. ) then
  1058. begin
  1059. { convert char array to pointer }
  1060. if is_chararray(rd) then
  1061. begin
  1062. inserttypeconv(right,charpointertype);
  1063. rd:=right.resulttype.def;
  1064. end
  1065. else if is_chararray(ld) then
  1066. begin
  1067. inserttypeconv(left,charpointertype);
  1068. ld:=left.resulttype.def;
  1069. end;
  1070. case nodetype of
  1071. equaln,unequaln :
  1072. begin
  1073. if is_voidpointer(right.resulttype.def) then
  1074. inserttypeconv(right,left.resulttype)
  1075. else if is_voidpointer(left.resulttype.def) then
  1076. inserttypeconv(left,right.resulttype)
  1077. else if not(equal_defs(ld,rd)) then
  1078. IncompatibleTypes(ld,rd);
  1079. { now that the type checking is done, convert both to charpointer, }
  1080. { because methodpointers are 8 bytes even though only the first 4 }
  1081. { bytes must be compared. This can happen here if we are in }
  1082. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1083. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1084. { optimized away, since the result already was a voidpointer, so }
  1085. { use a charpointer instead (JM) }
  1086. inserttypeconv_internal(left,charpointertype);
  1087. inserttypeconv_internal(right,charpointertype);
  1088. end;
  1089. ltn,lten,gtn,gten:
  1090. begin
  1091. if (cs_extsyntax in aktmoduleswitches) then
  1092. begin
  1093. if is_voidpointer(right.resulttype.def) then
  1094. inserttypeconv(right,left.resulttype)
  1095. else if is_voidpointer(left.resulttype.def) then
  1096. inserttypeconv(left,right.resulttype)
  1097. else if not(equal_defs(ld,rd)) then
  1098. IncompatibleTypes(ld,rd);
  1099. end
  1100. else
  1101. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1102. end;
  1103. subn:
  1104. begin
  1105. if (cs_extsyntax in aktmoduleswitches) then
  1106. begin
  1107. if is_voidpointer(right.resulttype.def) then
  1108. inserttypeconv(right,left.resulttype)
  1109. else if is_voidpointer(left.resulttype.def) then
  1110. inserttypeconv(left,right.resulttype)
  1111. else if not(equal_defs(ld,rd)) then
  1112. IncompatibleTypes(ld,rd);
  1113. end
  1114. else
  1115. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1116. if not(nf_has_pointerdiv in flags) and
  1117. (tpointerdef(rd).pointertype.def.size>1) then
  1118. begin
  1119. hp:=getcopy;
  1120. include(hp.flags,nf_has_pointerdiv);
  1121. result:=cmoddivnode.create(divn,hp,cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,false));
  1122. end;
  1123. resulttype:=sinttype;
  1124. exit;
  1125. end;
  1126. addn:
  1127. begin
  1128. if (cs_extsyntax in aktmoduleswitches) then
  1129. begin
  1130. if is_voidpointer(right.resulttype.def) then
  1131. inserttypeconv(right,left.resulttype)
  1132. else if is_voidpointer(left.resulttype.def) then
  1133. inserttypeconv(left,right.resulttype)
  1134. else if not(equal_defs(ld,rd)) then
  1135. IncompatibleTypes(ld,rd);
  1136. end
  1137. else
  1138. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1139. resulttype:=sinttype;
  1140. exit;
  1141. end;
  1142. else
  1143. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1144. end;
  1145. end
  1146. { is one of the operands a string?,
  1147. chararrays are also handled as strings (after conversion), also take
  1148. care of chararray+chararray and chararray+char.
  1149. Note: Must be done after pointerdef+pointerdef has been checked, else
  1150. pchar is converted to string }
  1151. else if (rd.deftype=stringdef) or
  1152. (ld.deftype=stringdef) or
  1153. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1154. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1155. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1156. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1157. begin
  1158. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1159. begin
  1160. { Is there a widestring? }
  1161. if is_widestring(rd) or is_widestring(ld) or
  1162. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1163. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1164. strtype:= st_widestring
  1165. else
  1166. if is_ansistring(rd) or is_ansistring(ld) or
  1167. ((cs_ansistrings in aktlocalswitches) and
  1168. //todo: Move some of this to longstring's then they are implemented?
  1169. (
  1170. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or
  1171. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld)
  1172. )
  1173. ) then
  1174. strtype:= st_ansistring
  1175. else
  1176. if is_longstring(rd) or is_longstring(ld) then
  1177. strtype:= st_longstring
  1178. else
  1179. begin
  1180. {$warning todo: add a warning/hint here if one converting a too large array}
  1181. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1182. Note: Delphi halts with error if "array [0..xx] of char"
  1183. is assigned to ShortString and string length is less
  1184. then array size }
  1185. strtype:= st_shortstring;
  1186. end;
  1187. // Now convert nodes to common string type
  1188. case strtype of
  1189. st_widestring :
  1190. begin
  1191. if not(is_widestring(rd)) then
  1192. inserttypeconv(right,cwidestringtype);
  1193. if not(is_widestring(ld)) then
  1194. inserttypeconv(left,cwidestringtype);
  1195. end;
  1196. st_ansistring :
  1197. begin
  1198. if not(is_ansistring(rd)) then
  1199. inserttypeconv(right,cansistringtype);
  1200. if not(is_ansistring(ld)) then
  1201. inserttypeconv(left,cansistringtype);
  1202. end;
  1203. st_longstring :
  1204. begin
  1205. if not(is_longstring(rd)) then
  1206. inserttypeconv(right,clongstringtype);
  1207. if not(is_longstring(ld)) then
  1208. inserttypeconv(left,clongstringtype);
  1209. end;
  1210. st_shortstring :
  1211. begin
  1212. if not(is_shortstring(ld)) then
  1213. inserttypeconv(left,cshortstringtype);
  1214. { don't convert char, that can be handled by the optimized node }
  1215. if not(is_shortstring(rd) or is_char(rd)) then
  1216. inserttypeconv(right,cshortstringtype);
  1217. end;
  1218. else
  1219. internalerror(2005101);
  1220. end;
  1221. end
  1222. else
  1223. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1224. end
  1225. { class or interface equation }
  1226. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  1227. begin
  1228. if (nodetype in [equaln,unequaln]) then
  1229. begin
  1230. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  1231. begin
  1232. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1233. inserttypeconv(right,left.resulttype)
  1234. else
  1235. inserttypeconv(left,right.resulttype);
  1236. end
  1237. else if is_class_or_interface(rd) then
  1238. inserttypeconv(left,right.resulttype)
  1239. else
  1240. inserttypeconv(right,left.resulttype);
  1241. end
  1242. else
  1243. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1244. end
  1245. else if (rd.deftype=classrefdef) and (ld.deftype=classrefdef) then
  1246. begin
  1247. if (nodetype in [equaln,unequaln]) then
  1248. begin
  1249. if tobjectdef(tclassrefdef(rd).pointertype.def).is_related(
  1250. tobjectdef(tclassrefdef(ld).pointertype.def)) then
  1251. inserttypeconv(right,left.resulttype)
  1252. else
  1253. inserttypeconv(left,right.resulttype);
  1254. end
  1255. else
  1256. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1257. end
  1258. { allows comperasion with nil pointer }
  1259. else if is_class_or_interface(rd) or (rd.deftype=classrefdef) then
  1260. begin
  1261. if (nodetype in [equaln,unequaln]) then
  1262. inserttypeconv(left,right.resulttype)
  1263. else
  1264. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1265. end
  1266. else if is_class_or_interface(ld) or (ld.deftype=classrefdef) then
  1267. begin
  1268. if (nodetype in [equaln,unequaln]) then
  1269. inserttypeconv(right,left.resulttype)
  1270. else
  1271. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1272. end
  1273. { support procvar=nil,procvar<>nil }
  1274. else if ((ld.deftype=procvardef) and (rt=niln)) or
  1275. ((rd.deftype=procvardef) and (lt=niln)) then
  1276. begin
  1277. if not(nodetype in [equaln,unequaln]) then
  1278. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1279. { find proc field in methodpointer record }
  1280. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('proc'));
  1281. if not assigned(hsym) then
  1282. internalerror(200412043);
  1283. { For methodpointers compare only tmethodpointer.proc }
  1284. if (rd.deftype=procvardef) and
  1285. (not tprocvardef(rd).is_addressonly) then
  1286. begin
  1287. right:=csubscriptnode.create(
  1288. hsym,
  1289. ctypeconvnode.create_internal(right,methodpointertype));
  1290. end;
  1291. if (ld.deftype=procvardef) and
  1292. (not tprocvardef(ld).is_addressonly) then
  1293. begin
  1294. left:=csubscriptnode.create(
  1295. hsym,
  1296. ctypeconvnode.create_internal(left,methodpointertype));
  1297. end;
  1298. end
  1299. { support dynamicarray=nil,dynamicarray<>nil }
  1300. else if (is_dynamic_array(ld) and (rt=niln)) or
  1301. (is_dynamic_array(rd) and (lt=niln)) or
  1302. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1303. begin
  1304. if not(nodetype in [equaln,unequaln]) then
  1305. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1306. end
  1307. {$ifdef SUPPORT_MMX}
  1308. { mmx support, this must be before the zero based array
  1309. check }
  1310. else if (cs_mmx in aktlocalswitches) and
  1311. is_mmx_able_array(ld) and
  1312. is_mmx_able_array(rd) and
  1313. equal_defs(ld,rd) then
  1314. begin
  1315. case nodetype of
  1316. addn,subn,xorn,orn,andn:
  1317. ;
  1318. { mul is a little bit restricted }
  1319. muln:
  1320. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1321. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1322. else
  1323. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1324. end;
  1325. end
  1326. {$endif SUPPORT_MMX}
  1327. { this is a little bit dangerous, also the left type }
  1328. { pointer to should be checked! This broke the mmx support }
  1329. else if (rd.deftype=pointerdef) or
  1330. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  1331. begin
  1332. if is_zero_based_array(rd) then
  1333. begin
  1334. resulttype.setdef(tpointerdef.create(tarraydef(rd).elementtype));
  1335. inserttypeconv(right,resulttype);
  1336. end
  1337. else
  1338. resulttype:=right.resulttype;
  1339. inserttypeconv(left,sinttype);
  1340. if nodetype=addn then
  1341. begin
  1342. if not(cs_extsyntax in aktmoduleswitches) or
  1343. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1344. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1345. if (rd.deftype=pointerdef) and
  1346. (tpointerdef(rd).pointertype.def.size>1) then
  1347. begin
  1348. left:=caddnode.create(muln,left,
  1349. cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,true));
  1350. end;
  1351. end
  1352. else
  1353. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1354. end
  1355. else if (ld.deftype=pointerdef) or
  1356. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  1357. begin
  1358. if is_zero_based_array(ld) then
  1359. begin
  1360. resulttype.setdef(tpointerdef.create(tarraydef(ld).elementtype));
  1361. inserttypeconv(left,resulttype);
  1362. end
  1363. else
  1364. resulttype:=left.resulttype;
  1365. inserttypeconv(right,sinttype);
  1366. if nodetype in [addn,subn] then
  1367. begin
  1368. if not(cs_extsyntax in aktmoduleswitches) or
  1369. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1370. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1371. if (ld.deftype=pointerdef) and
  1372. (tpointerdef(ld).pointertype.def.size>1) then
  1373. begin
  1374. right:=caddnode.create(muln,right,
  1375. cordconstnode.create(tpointerdef(ld).pointertype.def.size,sinttype,true));
  1376. end
  1377. else
  1378. if is_zero_based_array(ld) and
  1379. (tarraydef(ld).elementtype.def.size>1) then
  1380. begin
  1381. right:=caddnode.create(muln,right,
  1382. cordconstnode.create(tarraydef(ld).elementtype.def.size,sinttype,true));
  1383. end;
  1384. end
  1385. else
  1386. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1387. end
  1388. else if (rd.deftype=procvardef) and
  1389. (ld.deftype=procvardef) and
  1390. equal_defs(rd,ld) then
  1391. begin
  1392. if (nodetype in [equaln,unequaln]) then
  1393. begin
  1394. if tprocvardef(rd).is_addressonly then
  1395. begin
  1396. inserttypeconv_internal(right,voidpointertype);
  1397. inserttypeconv_internal(left,voidpointertype);
  1398. end
  1399. else
  1400. begin
  1401. { find proc field in methodpointer record }
  1402. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('proc'));
  1403. if not assigned(hsym) then
  1404. internalerror(200412043);
  1405. { Compare tmehodpointer(left).proc }
  1406. right:=csubscriptnode.create(
  1407. hsym,
  1408. ctypeconvnode.create_internal(right,methodpointertype));
  1409. left:=csubscriptnode.create(
  1410. hsym,
  1411. ctypeconvnode.create_internal(left,methodpointertype));
  1412. end;
  1413. end
  1414. else
  1415. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1416. end
  1417. { enums }
  1418. else if (ld.deftype=enumdef) and (rd.deftype=enumdef) then
  1419. begin
  1420. if allowenumop(nodetype) then
  1421. inserttypeconv(right,left.resulttype)
  1422. else
  1423. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1424. end
  1425. { generic conversion, this is for error recovery }
  1426. else
  1427. begin
  1428. inserttypeconv(left,sinttype);
  1429. inserttypeconv(right,sinttype);
  1430. end;
  1431. { set resulttype if not already done }
  1432. if not assigned(resulttype.def) then
  1433. begin
  1434. case nodetype of
  1435. ltn,lten,gtn,gten,equaln,unequaln :
  1436. resulttype:=booltype;
  1437. slashn :
  1438. resulttype:=resultrealtype;
  1439. addn:
  1440. begin
  1441. { for strings, return is always a 255 char string }
  1442. if is_shortstring(left.resulttype.def) then
  1443. resulttype:=cshortstringtype
  1444. else
  1445. resulttype:=left.resulttype;
  1446. end;
  1447. else
  1448. resulttype:=left.resulttype;
  1449. end;
  1450. end;
  1451. { when the result is currency we need some extra code for
  1452. multiplication and division. this should not be done when
  1453. the muln or slashn node is created internally }
  1454. if not(nf_is_currency in flags) and
  1455. is_currency(resulttype.def) then
  1456. begin
  1457. case nodetype of
  1458. slashn :
  1459. begin
  1460. { slashn will only work with floats }
  1461. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1462. include(hp.flags,nf_is_currency);
  1463. result:=hp;
  1464. end;
  1465. muln :
  1466. begin
  1467. if s64currencytype.def.deftype=floatdef then
  1468. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1469. else
  1470. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1471. include(hp.flags,nf_is_currency);
  1472. result:=hp
  1473. end;
  1474. end;
  1475. end;
  1476. end;
  1477. function taddnode.first_addstring: tnode;
  1478. const
  1479. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  1480. var
  1481. p: tnode;
  1482. begin
  1483. { when we get here, we are sure that both the left and the right }
  1484. { node are both strings of the same stringtype (JM) }
  1485. case nodetype of
  1486. addn:
  1487. begin
  1488. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  1489. begin
  1490. result:=right;
  1491. left:=nil;
  1492. right:=nil;
  1493. exit;
  1494. end;
  1495. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  1496. begin
  1497. result:=left;
  1498. left:=nil;
  1499. right:=nil;
  1500. exit;
  1501. end;
  1502. { create the call to the concat routine both strings as arguments }
  1503. result := ccallnode.createintern('fpc_'+
  1504. tstringdef(resulttype.def).stringtypname+'_concat',
  1505. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1506. { we reused the arguments }
  1507. left := nil;
  1508. right := nil;
  1509. end;
  1510. ltn,lten,gtn,gten,equaln,unequaln :
  1511. begin
  1512. { generate better code for comparison with empty string, we
  1513. only need to compare the length with 0 }
  1514. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1515. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  1516. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  1517. begin
  1518. { switch so that the constant is always on the right }
  1519. if left.nodetype = stringconstn then
  1520. begin
  1521. p := left;
  1522. left := right;
  1523. right := p;
  1524. nodetype:=swap_relation[nodetype];
  1525. end;
  1526. if is_shortstring(left.resulttype.def) or
  1527. (nodetype in [gtn,gten,ltn,lten]) then
  1528. { compare the length with 0 }
  1529. result := caddnode.create(nodetype,
  1530. cinlinenode.create(in_length_x,false,left),
  1531. cordconstnode.create(0,s32inttype,false))
  1532. else
  1533. begin
  1534. { compare the pointer with nil (for ansistrings etc), }
  1535. { faster than getting the length (JM) }
  1536. result:= caddnode.create(nodetype,
  1537. ctypeconvnode.create_internal(left,voidpointertype),
  1538. cpointerconstnode.create(0,voidpointertype));
  1539. end;
  1540. { left is reused }
  1541. left := nil;
  1542. { right isn't }
  1543. right.free;
  1544. right := nil;
  1545. exit;
  1546. end;
  1547. { no string constant -> call compare routine }
  1548. result := ccallnode.createintern('fpc_'+
  1549. tstringdef(left.resulttype.def).stringtypname+'_compare',
  1550. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1551. { and compare its result with 0 according to the original operator }
  1552. result := caddnode.create(nodetype,result,
  1553. cordconstnode.create(0,s32inttype,false));
  1554. left := nil;
  1555. right := nil;
  1556. end;
  1557. end;
  1558. end;
  1559. function taddnode.first_addset: tnode;
  1560. var
  1561. procname: string[31];
  1562. tempn: tnode;
  1563. paras: tcallparanode;
  1564. srsym: ttypesym;
  1565. begin
  1566. { get the sym that represents the fpc_normal_set type }
  1567. srsym:=search_system_type('FPC_NORMAL_SET');
  1568. case nodetype of
  1569. equaln,unequaln,lten,gten:
  1570. begin
  1571. case nodetype of
  1572. equaln,unequaln:
  1573. procname := 'fpc_set_comp_sets';
  1574. lten,gten:
  1575. begin
  1576. procname := 'fpc_set_contains_sets';
  1577. { (left >= right) = (right <= left) }
  1578. if nodetype = gten then
  1579. begin
  1580. tempn := left;
  1581. left := right;
  1582. right := tempn;
  1583. end;
  1584. end;
  1585. end;
  1586. { convert the arguments (explicitely) to fpc_normal_set's }
  1587. left := ctypeconvnode.create_internal(left,srsym.restype);
  1588. right := ctypeconvnode.create_internal(right,srsym.restype);
  1589. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1590. ccallparanode.create(left,nil)));
  1591. { left and right are reused as parameters }
  1592. left := nil;
  1593. right := nil;
  1594. { for an unequaln, we have to negate the result of comp_sets }
  1595. if nodetype = unequaln then
  1596. result := cnotnode.create(result);
  1597. end;
  1598. addn:
  1599. begin
  1600. { optimize first loading of a set }
  1601. if (right.nodetype=setelementn) and
  1602. not(assigned(tsetelementnode(right).right)) and
  1603. is_emptyset(left) then
  1604. begin
  1605. { type cast the value to pass as argument to a byte, }
  1606. { since that's what the helper expects }
  1607. tsetelementnode(right).left :=
  1608. ctypeconvnode.create_internal(tsetelementnode(right).left,u8inttype);
  1609. { set the resulttype to the actual one (otherwise it's }
  1610. { "fpc_normal_set") }
  1611. result := ccallnode.createinternres('fpc_set_create_element',
  1612. ccallparanode.create(tsetelementnode(right).left,nil),
  1613. resulttype);
  1614. { reused }
  1615. tsetelementnode(right).left := nil;
  1616. end
  1617. else
  1618. begin
  1619. if right.nodetype=setelementn then
  1620. begin
  1621. { convert the arguments to bytes, since that's what }
  1622. { the helper expects }
  1623. tsetelementnode(right).left :=
  1624. ctypeconvnode.create_internal(tsetelementnode(right).left,
  1625. u8inttype);
  1626. { convert the original set (explicitely) to an }
  1627. { fpc_normal_set so we can pass it to the helper }
  1628. left := ctypeconvnode.create_internal(left,srsym.restype);
  1629. { add a range or a single element? }
  1630. if assigned(tsetelementnode(right).right) then
  1631. begin
  1632. tsetelementnode(right).right :=
  1633. ctypeconvnode.create_internal(tsetelementnode(right).right,
  1634. u8inttype);
  1635. { create the call }
  1636. result := ccallnode.createinternres('fpc_set_set_range',
  1637. ccallparanode.create(tsetelementnode(right).right,
  1638. ccallparanode.create(tsetelementnode(right).left,
  1639. ccallparanode.create(left,nil))),resulttype);
  1640. end
  1641. else
  1642. begin
  1643. result := ccallnode.createinternres('fpc_set_set_byte',
  1644. ccallparanode.create(tsetelementnode(right).left,
  1645. ccallparanode.create(left,nil)),resulttype);
  1646. end;
  1647. { remove reused parts from original node }
  1648. tsetelementnode(right).right := nil;
  1649. tsetelementnode(right).left := nil;
  1650. left := nil;
  1651. end
  1652. else
  1653. begin
  1654. { add two sets }
  1655. { convert the sets to fpc_normal_set's }
  1656. result := ccallnode.createinternres('fpc_set_add_sets',
  1657. ccallparanode.create(
  1658. ctypeconvnode.create_explicit(right,srsym.restype),
  1659. ccallparanode.create(
  1660. ctypeconvnode.create_internal(left,srsym.restype),nil)),resulttype);
  1661. { remove reused parts from original node }
  1662. left := nil;
  1663. right := nil;
  1664. end;
  1665. end
  1666. end;
  1667. subn,symdifn,muln:
  1668. begin
  1669. { convert the sets to fpc_normal_set's }
  1670. paras := ccallparanode.create(ctypeconvnode.create_internal(right,srsym.restype),
  1671. ccallparanode.create(ctypeconvnode.create_internal(left,srsym.restype),nil));
  1672. case nodetype of
  1673. subn:
  1674. result := ccallnode.createinternres('fpc_set_sub_sets',
  1675. paras,resulttype);
  1676. symdifn:
  1677. result := ccallnode.createinternres('fpc_set_symdif_sets',
  1678. paras,resulttype);
  1679. muln:
  1680. result := ccallnode.createinternres('fpc_set_mul_sets',
  1681. paras,resulttype);
  1682. end;
  1683. { remove reused parts from original node }
  1684. left := nil;
  1685. right := nil;
  1686. end;
  1687. else
  1688. internalerror(200108311);
  1689. end;
  1690. end;
  1691. function taddnode.use_generic_mul32to64: boolean;
  1692. begin
  1693. result := true;
  1694. end;
  1695. function taddnode.try_make_mul32to64: boolean;
  1696. function canbe32bitint(v: tconstexprint; fromdef: torddef; todefsigned: boolean): boolean;
  1697. begin
  1698. if (fromdef.typ <> u64bit) then
  1699. result :=
  1700. ((v >= 0) or
  1701. todefsigned) and
  1702. (v >= low(longint)) and
  1703. (v <= high(longint))
  1704. else
  1705. result :=
  1706. (qword(v) >= low(cardinal)) and
  1707. (qword(v) <= high(cardinal))
  1708. end;
  1709. var
  1710. temp: tnode;
  1711. begin
  1712. result := false;
  1713. if ((left.nodetype = typeconvn) and
  1714. is_integer(ttypeconvnode(left).left.resulttype.def) and
  1715. (not(torddef(ttypeconvnode(left).left.resulttype.def).typ in [u64bit,s64bit])) and
  1716. (((right.nodetype = ordconstn) and
  1717. canbe32bitint(tordconstnode(right).value,torddef(right.resulttype.def),is_signed(left.resulttype.def))) or
  1718. ((right.nodetype = typeconvn) and
  1719. is_integer(ttypeconvnode(right).left.resulttype.def) and
  1720. not(torddef(ttypeconvnode(right).left.resulttype.def).typ in [u64bit,s64bit])) and
  1721. (is_signed(ttypeconvnode(left).left.resulttype.def) =
  1722. is_signed(ttypeconvnode(right).left.resulttype.def)))) then
  1723. begin
  1724. temp := ttypeconvnode(left).left;
  1725. ttypeconvnode(left).left := nil;
  1726. left.free;
  1727. left := temp;
  1728. if (right.nodetype = typeconvn) then
  1729. begin
  1730. temp := ttypeconvnode(right).left;
  1731. ttypeconvnode(right).left := nil;
  1732. right.free;
  1733. right := temp;
  1734. end;
  1735. if (is_signed(left.resulttype.def)) then
  1736. begin
  1737. inserttypeconv(left,s32inttype);
  1738. inserttypeconv(right,s32inttype);
  1739. end
  1740. else
  1741. begin
  1742. inserttypeconv(left,u32inttype);
  1743. inserttypeconv(right,u32inttype);
  1744. end;
  1745. firstpass(left);
  1746. firstpass(right);
  1747. result := true;
  1748. end;
  1749. end;
  1750. function taddnode.first_add64bitint: tnode;
  1751. var
  1752. procname: string[31];
  1753. temp: tnode;
  1754. power: longint;
  1755. begin
  1756. result := nil;
  1757. { create helper calls mul }
  1758. if nodetype <> muln then
  1759. exit;
  1760. { make sure that if there is a constant, that it's on the right }
  1761. if left.nodetype = ordconstn then
  1762. begin
  1763. temp := right;
  1764. right := left;
  1765. left := temp;
  1766. end;
  1767. { can we use a shift instead of a mul? }
  1768. if not (cs_check_overflow in aktlocalswitches) and
  1769. (right.nodetype = ordconstn) and
  1770. ispowerof2(tordconstnode(right).value,power) then
  1771. begin
  1772. tordconstnode(right).value := power;
  1773. result := cshlshrnode.create(shln,left,right);
  1774. { left and right are reused }
  1775. left := nil;
  1776. right := nil;
  1777. { return firstpassed new node }
  1778. exit;
  1779. end;
  1780. if not(use_generic_mul32to64) and
  1781. try_make_mul32to64 then
  1782. exit;
  1783. { when currency is used set the result of the
  1784. parameters to s64bit, so they are not converted }
  1785. if is_currency(resulttype.def) then
  1786. begin
  1787. left.resulttype:=s64inttype;
  1788. right.resulttype:=s64inttype;
  1789. end;
  1790. { otherwise, create the parameters for the helper }
  1791. right := ccallparanode.create(
  1792. cordconstnode.create(ord(cs_check_overflow in aktlocalswitches),booltype,true),
  1793. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1794. left := nil;
  1795. { only qword needs the unsigned code, the
  1796. signed code is also used for currency }
  1797. if is_signed(resulttype.def) then
  1798. procname := 'fpc_mul_int64'
  1799. else
  1800. procname := 'fpc_mul_qword';
  1801. result := ccallnode.createintern(procname,right);
  1802. right := nil;
  1803. end;
  1804. function taddnode.first_addfloat : tnode;
  1805. var
  1806. procname: string[31];
  1807. { do we need to reverse the result ? }
  1808. notnode : boolean;
  1809. begin
  1810. result := nil;
  1811. notnode := false;
  1812. { In non-emulation mode, real opcodes are
  1813. emitted for floating point values.
  1814. }
  1815. if not (cs_fp_emulation in aktmoduleswitches) then
  1816. exit;
  1817. if not(target_info.system in system_wince) then
  1818. begin
  1819. case tfloatdef(left.resulttype.def).typ of
  1820. s32real:
  1821. procname:='float32';
  1822. s64real:
  1823. procname:='float64';
  1824. {!!! not yet implemented
  1825. s128real:
  1826. }
  1827. else
  1828. internalerror(2005082601);
  1829. end;
  1830. case nodetype of
  1831. addn:
  1832. procname:=procname+'_add';
  1833. muln:
  1834. procname:=procname+'_mul';
  1835. subn:
  1836. procname:=procname+'_sub';
  1837. slashn:
  1838. procname:=procname+'_div';
  1839. ltn:
  1840. procname:=procname+'_lt';
  1841. lten:
  1842. procname:=procname+'_le';
  1843. gtn:
  1844. begin
  1845. procname:=procname+'_le';
  1846. notnode:=true;
  1847. end;
  1848. gten:
  1849. begin
  1850. procname:=procname+'_lt';
  1851. notnode:=true;
  1852. end;
  1853. equaln:
  1854. procname:=procname+'_eq';
  1855. unequaln:
  1856. begin
  1857. procname:=procname+'_eq';
  1858. notnode:=true;
  1859. end;
  1860. else
  1861. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resulttype.def.typename,right.resulttype.def.typename);
  1862. end;
  1863. end
  1864. else
  1865. begin
  1866. case nodetype of
  1867. addn:
  1868. procname:='ADD';
  1869. muln:
  1870. procname:='MUL';
  1871. subn:
  1872. procname:='SUB';
  1873. slashn:
  1874. procname:='DIV';
  1875. ltn:
  1876. procname:='LT';
  1877. lten:
  1878. procname:='LE';
  1879. gtn:
  1880. procname:='GT';
  1881. gten:
  1882. procname:='GE';
  1883. equaln:
  1884. procname:='EQ';
  1885. unequaln:
  1886. procname:='NE';
  1887. else
  1888. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resulttype.def.typename,right.resulttype.def.typename);
  1889. end;
  1890. case tfloatdef(left.resulttype.def).typ of
  1891. s32real:
  1892. procname:=procname+'S';
  1893. s64real:
  1894. procname:=procname+'D';
  1895. {!!! not yet implemented
  1896. s128real:
  1897. }
  1898. else
  1899. internalerror(2005082602);
  1900. end;
  1901. end;
  1902. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  1903. ccallparanode.create(left,nil)));
  1904. left:=nil;
  1905. right:=nil;
  1906. { do we need to reverse the result }
  1907. if notnode then
  1908. result:=cnotnode.create(result);
  1909. end;
  1910. function taddnode.pass_1 : tnode;
  1911. var
  1912. {$ifdef addstringopt}
  1913. hp : tnode;
  1914. {$endif addstringopt}
  1915. lt,rt : tnodetype;
  1916. rd,ld : tdef;
  1917. begin
  1918. result:=nil;
  1919. { Can we optimize multiple string additions into a single call?
  1920. This need to be done on a complete tree to detect the multiple
  1921. add nodes and is therefor done before the subtrees are processed }
  1922. if canbemultistringadd(self) then
  1923. begin
  1924. result := genmultistringadd(self);
  1925. exit;
  1926. end;
  1927. { first do the two subtrees }
  1928. firstpass(left);
  1929. firstpass(right);
  1930. if codegenerror then
  1931. exit;
  1932. { load easier access variables }
  1933. rd:=right.resulttype.def;
  1934. ld:=left.resulttype.def;
  1935. rt:=right.nodetype;
  1936. lt:=left.nodetype;
  1937. { int/int gives real/real! }
  1938. if nodetype=slashn then
  1939. begin
  1940. {$ifdef cpufpemu}
  1941. if (aktfputype=fpu_soft) or (cs_fp_emulation in aktmoduleswitches) then
  1942. begin
  1943. result:=first_addfloat;
  1944. if assigned(result) then
  1945. exit;
  1946. end;
  1947. {$endif cpufpemu}
  1948. expectloc:=LOC_FPUREGISTER;
  1949. { maybe we need an integer register to save }
  1950. { a reference }
  1951. if ((left.expectloc<>LOC_FPUREGISTER) or
  1952. (right.expectloc<>LOC_FPUREGISTER)) and
  1953. (left.registersint=right.registersint) then
  1954. calcregisters(self,1,1,0)
  1955. else
  1956. calcregisters(self,0,1,0);
  1957. { an add node always first loads both the left and the }
  1958. { right in the fpu before doing the calculation. However, }
  1959. { calcregisters(0,2,0) will overestimate the number of }
  1960. { necessary registers (it will make it 3 in case one of }
  1961. { the operands is already in the fpu) (JM) }
  1962. if ((left.expectloc<>LOC_FPUREGISTER) or
  1963. (right.expectloc<>LOC_FPUREGISTER)) and
  1964. (registersfpu < 2) then
  1965. inc(registersfpu);
  1966. end
  1967. { if both are orddefs then check sub types }
  1968. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  1969. begin
  1970. { 2 booleans ? }
  1971. if is_boolean(ld) and is_boolean(rd) then
  1972. begin
  1973. if not(cs_full_boolean_eval in aktlocalswitches) and
  1974. (nodetype in [andn,orn]) then
  1975. begin
  1976. expectloc:=LOC_JUMP;
  1977. calcregisters(self,0,0,0);
  1978. end
  1979. else
  1980. begin
  1981. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1982. begin
  1983. expectloc:=LOC_FLAGS;
  1984. if (left.expectloc in [LOC_JUMP,LOC_FLAGS]) and
  1985. (left.expectloc in [LOC_JUMP,LOC_FLAGS]) then
  1986. calcregisters(self,2,0,0)
  1987. else
  1988. calcregisters(self,1,0,0);
  1989. end
  1990. else
  1991. begin
  1992. expectloc:=LOC_REGISTER;
  1993. calcregisters(self,0,0,0);
  1994. end;
  1995. end;
  1996. end
  1997. else
  1998. { Both are chars? only convert to shortstrings for addn }
  1999. if is_char(ld) then
  2000. begin
  2001. if nodetype=addn then
  2002. internalerror(200103291);
  2003. expectloc:=LOC_FLAGS;
  2004. calcregisters(self,1,0,0);
  2005. end
  2006. {$ifndef cpu64bit}
  2007. { is there a 64 bit type ? }
  2008. else if (torddef(ld).typ in [s64bit,u64bit,scurrency]) then
  2009. begin
  2010. result := first_add64bitint;
  2011. if assigned(result) then
  2012. exit;
  2013. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2014. expectloc:=LOC_REGISTER
  2015. else
  2016. expectloc:=LOC_JUMP;
  2017. calcregisters(self,2,0,0)
  2018. end
  2019. {$endif cpu64bit}
  2020. { is there a cardinal? }
  2021. else if (torddef(ld).typ=u32bit) then
  2022. begin
  2023. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2024. expectloc:=LOC_REGISTER
  2025. else
  2026. expectloc:=LOC_FLAGS;
  2027. calcregisters(self,1,0,0);
  2028. { for unsigned mul we need an extra register }
  2029. if nodetype=muln then
  2030. inc(registersint);
  2031. end
  2032. { generic s32bit conversion }
  2033. else
  2034. begin
  2035. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2036. expectloc:=LOC_REGISTER
  2037. else
  2038. expectloc:=LOC_FLAGS;
  2039. calcregisters(self,1,0,0);
  2040. end;
  2041. end
  2042. { left side a setdef, must be before string processing,
  2043. else array constructor can be seen as array of char (PFV) }
  2044. else if (ld.deftype=setdef) then
  2045. begin
  2046. if tsetdef(ld).settype=smallset then
  2047. begin
  2048. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2049. expectloc:=LOC_FLAGS
  2050. else
  2051. expectloc:=LOC_REGISTER;
  2052. { are we adding set elements ? }
  2053. if right.nodetype=setelementn then
  2054. calcregisters(self,2,0,0)
  2055. else
  2056. calcregisters(self,1,0,0);
  2057. end
  2058. else
  2059. {$ifdef MMXSET}
  2060. {$ifdef i386}
  2061. if cs_mmx in aktlocalswitches then
  2062. begin
  2063. expectloc:=LOC_MMXREGISTER;
  2064. calcregisters(self,0,0,4);
  2065. end
  2066. else
  2067. {$endif}
  2068. {$endif MMXSET}
  2069. begin
  2070. result := first_addset;
  2071. if assigned(result) then
  2072. exit;
  2073. expectloc:=LOC_CREFERENCE;
  2074. calcregisters(self,0,0,0);
  2075. { here we call SET... }
  2076. include(current_procinfo.flags,pi_do_call);
  2077. end;
  2078. end
  2079. { compare pchar by addresses like BP/Delphi }
  2080. else if is_pchar(ld) then
  2081. begin
  2082. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2083. expectloc:=LOC_REGISTER
  2084. else
  2085. expectloc:=LOC_FLAGS;
  2086. calcregisters(self,1,0,0);
  2087. end
  2088. { is one of the operands a string }
  2089. else if (ld.deftype=stringdef) then
  2090. begin
  2091. if is_widestring(ld) then
  2092. begin
  2093. { this is only for add, the comparisaion is handled later }
  2094. expectloc:=LOC_REGISTER;
  2095. end
  2096. else if is_ansistring(ld) then
  2097. begin
  2098. { this is only for add, the comparisaion is handled later }
  2099. expectloc:=LOC_REGISTER;
  2100. end
  2101. else if is_longstring(ld) then
  2102. begin
  2103. { this is only for add, the comparisaion is handled later }
  2104. expectloc:=LOC_REFERENCE;
  2105. end
  2106. else
  2107. begin
  2108. {$ifdef addstringopt}
  2109. { can create a call which isn't handled by callparatemp }
  2110. if canbeaddsstringcharoptnode(self) then
  2111. begin
  2112. hp := genaddsstringcharoptnode(self);
  2113. pass_1 := hp;
  2114. exit;
  2115. end
  2116. else
  2117. {$endif addstringopt}
  2118. begin
  2119. { Fix right to be shortstring }
  2120. if is_char(right.resulttype.def) then
  2121. begin
  2122. inserttypeconv(right,cshortstringtype);
  2123. firstpass(right);
  2124. end;
  2125. end;
  2126. {$ifdef addstringopt}
  2127. { can create a call which isn't handled by callparatemp }
  2128. if canbeaddsstringcsstringoptnode(self) then
  2129. begin
  2130. hp := genaddsstringcsstringoptnode(self);
  2131. pass_1 := hp;
  2132. exit;
  2133. end;
  2134. {$endif addstringopt}
  2135. end;
  2136. { otherwise, let addstring convert everything }
  2137. result := first_addstring;
  2138. exit;
  2139. end
  2140. { is one a real float ? }
  2141. else if (rd.deftype=floatdef) or (ld.deftype=floatdef) then
  2142. begin
  2143. {$ifdef cpufpemu}
  2144. if (aktfputype=fpu_soft) or (cs_fp_emulation in aktmoduleswitches) then
  2145. begin
  2146. result:=first_addfloat;
  2147. if assigned(result) then
  2148. exit;
  2149. end;
  2150. {$endif cpufpemu}
  2151. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2152. expectloc:=LOC_FPUREGISTER
  2153. else
  2154. expectloc:=LOC_FLAGS;
  2155. calcregisters(self,0,1,0);
  2156. { an add node always first loads both the left and the }
  2157. { right in the fpu before doing the calculation. However, }
  2158. { calcregisters(0,2,0) will overestimate the number of }
  2159. { necessary registers (it will make it 3 in case one of }
  2160. { the operands is already in the fpu) (JM) }
  2161. if ((left.expectloc<>LOC_FPUREGISTER) or
  2162. (right.expectloc<>LOC_FPUREGISTER)) and
  2163. (registersfpu < 2) then
  2164. inc(registersfpu);
  2165. end
  2166. { pointer comperation and subtraction }
  2167. else if (ld.deftype=pointerdef) then
  2168. begin
  2169. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2170. expectloc:=LOC_REGISTER
  2171. else
  2172. expectloc:=LOC_FLAGS;
  2173. calcregisters(self,1,0,0);
  2174. end
  2175. else if is_class_or_interface(ld) then
  2176. begin
  2177. expectloc:=LOC_FLAGS;
  2178. calcregisters(self,1,0,0);
  2179. end
  2180. else if (ld.deftype=classrefdef) then
  2181. begin
  2182. expectloc:=LOC_FLAGS;
  2183. calcregisters(self,1,0,0);
  2184. end
  2185. { support procvar=nil,procvar<>nil }
  2186. else if ((ld.deftype=procvardef) and (rt=niln)) or
  2187. ((rd.deftype=procvardef) and (lt=niln)) then
  2188. begin
  2189. expectloc:=LOC_FLAGS;
  2190. calcregisters(self,1,0,0);
  2191. end
  2192. {$ifdef SUPPORT_MMX}
  2193. { mmx support, this must be before the zero based array
  2194. check }
  2195. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  2196. is_mmx_able_array(rd) then
  2197. begin
  2198. expectloc:=LOC_MMXREGISTER;
  2199. calcregisters(self,0,0,1);
  2200. end
  2201. {$endif SUPPORT_MMX}
  2202. else if (rd.deftype=pointerdef) or (ld.deftype=pointerdef) then
  2203. begin
  2204. expectloc:=LOC_REGISTER;
  2205. calcregisters(self,1,0,0);
  2206. end
  2207. else if (rd.deftype=procvardef) and
  2208. (ld.deftype=procvardef) and
  2209. equal_defs(rd,ld) then
  2210. begin
  2211. expectloc:=LOC_FLAGS;
  2212. calcregisters(self,1,0,0);
  2213. end
  2214. else if (ld.deftype=enumdef) then
  2215. begin
  2216. expectloc:=LOC_FLAGS;
  2217. calcregisters(self,1,0,0);
  2218. end
  2219. {$ifdef SUPPORT_MMX}
  2220. else if (cs_mmx in aktlocalswitches) and
  2221. is_mmx_able_array(ld) and
  2222. is_mmx_able_array(rd) then
  2223. begin
  2224. expectloc:=LOC_MMXREGISTER;
  2225. calcregisters(self,0,0,1);
  2226. end
  2227. {$endif SUPPORT_MMX}
  2228. { the general solution is to convert to 32 bit int }
  2229. else
  2230. begin
  2231. expectloc:=LOC_REGISTER;
  2232. calcregisters(self,1,0,0);
  2233. end;
  2234. end;
  2235. {$ifdef state_tracking}
  2236. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2237. var factval:Tnode;
  2238. begin
  2239. track_state_pass:=false;
  2240. if left.track_state_pass(exec_known) then
  2241. begin
  2242. track_state_pass:=true;
  2243. left.resulttype.def:=nil;
  2244. do_resulttypepass(left);
  2245. end;
  2246. factval:=aktstate.find_fact(left);
  2247. if factval<>nil then
  2248. begin
  2249. track_state_pass:=true;
  2250. left.destroy;
  2251. left:=factval.getcopy;
  2252. end;
  2253. if right.track_state_pass(exec_known) then
  2254. begin
  2255. track_state_pass:=true;
  2256. right.resulttype.def:=nil;
  2257. do_resulttypepass(right);
  2258. end;
  2259. factval:=aktstate.find_fact(right);
  2260. if factval<>nil then
  2261. begin
  2262. track_state_pass:=true;
  2263. right.destroy;
  2264. right:=factval.getcopy;
  2265. end;
  2266. end;
  2267. {$endif}
  2268. begin
  2269. caddnode:=taddnode;
  2270. end.