nadd.pas 89 KB

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