nadd.pas 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292
  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,st_conststring);
  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. { pointer comparision and subtraction }
  1035. else if (
  1036. (rd.deftype=pointerdef) and (ld.deftype=pointerdef)
  1037. ) or
  1038. { compare pchar to char arrays by addresses like BP/Delphi }
  1039. (
  1040. (nodetype in [equaln,unequaln]) and
  1041. (
  1042. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd)) or
  1043. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld))
  1044. )
  1045. ) then
  1046. begin
  1047. { convert char array to pointer }
  1048. if is_chararray(rd) then
  1049. begin
  1050. inserttypeconv(right,charpointertype);
  1051. rd:=right.resulttype.def;
  1052. end
  1053. else if is_chararray(ld) then
  1054. begin
  1055. inserttypeconv(left,charpointertype);
  1056. ld:=left.resulttype.def;
  1057. end;
  1058. case nodetype of
  1059. equaln,unequaln :
  1060. begin
  1061. if is_voidpointer(right.resulttype.def) then
  1062. inserttypeconv(right,left.resulttype)
  1063. else if is_voidpointer(left.resulttype.def) then
  1064. inserttypeconv(left,right.resulttype)
  1065. else if not(equal_defs(ld,rd)) then
  1066. IncompatibleTypes(ld,rd);
  1067. { now that the type checking is done, convert both to charpointer, }
  1068. { because methodpointers are 8 bytes even though only the first 4 }
  1069. { bytes must be compared. This can happen here if we are in }
  1070. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1071. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1072. { optimized away, since the result already was a voidpointer, so }
  1073. { use a charpointer instead (JM) }
  1074. inserttypeconv_internal(left,charpointertype);
  1075. inserttypeconv_internal(right,charpointertype);
  1076. end;
  1077. ltn,lten,gtn,gten:
  1078. begin
  1079. if (cs_extsyntax in aktmoduleswitches) then
  1080. begin
  1081. if is_voidpointer(right.resulttype.def) then
  1082. inserttypeconv(right,left.resulttype)
  1083. else if is_voidpointer(left.resulttype.def) then
  1084. inserttypeconv(left,right.resulttype)
  1085. else if not(equal_defs(ld,rd)) then
  1086. IncompatibleTypes(ld,rd);
  1087. end
  1088. else
  1089. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1090. end;
  1091. subn:
  1092. begin
  1093. if (cs_extsyntax in aktmoduleswitches) then
  1094. begin
  1095. if is_voidpointer(right.resulttype.def) then
  1096. inserttypeconv(right,left.resulttype)
  1097. else if is_voidpointer(left.resulttype.def) then
  1098. inserttypeconv(left,right.resulttype)
  1099. else if not(equal_defs(ld,rd)) then
  1100. IncompatibleTypes(ld,rd);
  1101. end
  1102. else
  1103. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1104. if not(nf_has_pointerdiv in flags) and
  1105. (tpointerdef(rd).pointertype.def.size>1) then
  1106. begin
  1107. hp:=getcopy;
  1108. include(hp.flags,nf_has_pointerdiv);
  1109. result:=cmoddivnode.create(divn,hp,cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,false));
  1110. end;
  1111. resulttype:=sinttype;
  1112. exit;
  1113. end;
  1114. addn:
  1115. begin
  1116. if (cs_extsyntax in aktmoduleswitches) then
  1117. begin
  1118. if is_voidpointer(right.resulttype.def) then
  1119. inserttypeconv(right,left.resulttype)
  1120. else if is_voidpointer(left.resulttype.def) then
  1121. inserttypeconv(left,right.resulttype)
  1122. else if not(equal_defs(ld,rd)) then
  1123. IncompatibleTypes(ld,rd);
  1124. end
  1125. else
  1126. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1127. resulttype:=sinttype;
  1128. exit;
  1129. end;
  1130. else
  1131. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1132. end;
  1133. end
  1134. { is one of the operands a string?,
  1135. chararrays are also handled as strings (after conversion), also take
  1136. care of chararray+chararray and chararray+char.
  1137. Note: Must be done after pointerdef+pointerdef has been checked, else
  1138. pchar is converted to string }
  1139. else if (rd.deftype=stringdef) or
  1140. (ld.deftype=stringdef) or
  1141. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1142. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1143. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1144. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1145. begin
  1146. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1147. begin
  1148. { Is there a widestring? }
  1149. if is_widestring(rd) or is_widestring(ld) or
  1150. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1151. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1152. strtype:= st_widestring
  1153. else
  1154. if is_ansistring(rd) or is_ansistring(ld) or
  1155. ((cs_ansistrings in aktlocalswitches) and
  1156. //todo: Move some of this to longstring's then they are implemented?
  1157. (
  1158. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or
  1159. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld)
  1160. )
  1161. ) then
  1162. strtype:= st_ansistring
  1163. else
  1164. if is_longstring(rd) or is_longstring(ld) then
  1165. strtype:= st_longstring
  1166. else
  1167. begin
  1168. {$warning todo: add a warning/hint here if one converting a too large array}
  1169. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1170. Note: Delphi halts with error if "array [0..xx] of char"
  1171. is assigned to ShortString and string length is less
  1172. then array size }
  1173. strtype:= st_shortstring;
  1174. end;
  1175. // Now convert nodes to common string type
  1176. case strtype of
  1177. st_widestring :
  1178. begin
  1179. if not(is_widestring(rd)) then
  1180. inserttypeconv(right,cwidestringtype);
  1181. if not(is_widestring(ld)) then
  1182. inserttypeconv(left,cwidestringtype);
  1183. end;
  1184. st_ansistring :
  1185. begin
  1186. if not(is_ansistring(rd)) then
  1187. inserttypeconv(right,cansistringtype);
  1188. if not(is_ansistring(ld)) then
  1189. inserttypeconv(left,cansistringtype);
  1190. end;
  1191. st_longstring :
  1192. begin
  1193. if not(is_longstring(rd)) then
  1194. inserttypeconv(right,clongstringtype);
  1195. if not(is_longstring(ld)) then
  1196. inserttypeconv(left,clongstringtype);
  1197. end;
  1198. st_shortstring :
  1199. begin
  1200. if not(is_shortstring(ld)) then
  1201. inserttypeconv(left,cshortstringtype);
  1202. { don't convert char, that can be handled by the optimized node }
  1203. if not(is_shortstring(rd) or is_char(rd)) then
  1204. inserttypeconv(right,cshortstringtype);
  1205. end;
  1206. else
  1207. internalerror(2005101);
  1208. end;
  1209. end
  1210. else
  1211. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1212. end
  1213. { class or interface equation }
  1214. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  1215. begin
  1216. if (nodetype in [equaln,unequaln]) then
  1217. begin
  1218. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  1219. begin
  1220. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1221. inserttypeconv(right,left.resulttype)
  1222. else
  1223. inserttypeconv(left,right.resulttype);
  1224. end
  1225. else if is_class_or_interface(rd) then
  1226. inserttypeconv(left,right.resulttype)
  1227. else
  1228. inserttypeconv(right,left.resulttype);
  1229. end
  1230. else
  1231. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1232. end
  1233. else if (rd.deftype=classrefdef) and (ld.deftype=classrefdef) then
  1234. begin
  1235. if (nodetype in [equaln,unequaln]) then
  1236. begin
  1237. if tobjectdef(tclassrefdef(rd).pointertype.def).is_related(
  1238. tobjectdef(tclassrefdef(ld).pointertype.def)) then
  1239. inserttypeconv(right,left.resulttype)
  1240. else
  1241. inserttypeconv(left,right.resulttype);
  1242. end
  1243. else
  1244. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1245. end
  1246. { allows comperasion with nil pointer }
  1247. else if is_class_or_interface(rd) or (rd.deftype=classrefdef) then
  1248. begin
  1249. if (nodetype in [equaln,unequaln]) then
  1250. inserttypeconv(left,right.resulttype)
  1251. else
  1252. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1253. end
  1254. else if is_class_or_interface(ld) or (ld.deftype=classrefdef) then
  1255. begin
  1256. if (nodetype in [equaln,unequaln]) then
  1257. inserttypeconv(right,left.resulttype)
  1258. else
  1259. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1260. end
  1261. { support procvar=nil,procvar<>nil }
  1262. else if ((ld.deftype=procvardef) and (rt=niln)) or
  1263. ((rd.deftype=procvardef) and (lt=niln)) then
  1264. begin
  1265. if not(nodetype in [equaln,unequaln]) then
  1266. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1267. { find proc field in methodpointer record }
  1268. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('proc'));
  1269. if not assigned(hsym) then
  1270. internalerror(200412043);
  1271. { For methodpointers compare only tmethodpointer.proc }
  1272. if (rd.deftype=procvardef) and
  1273. (not tprocvardef(rd).is_addressonly) then
  1274. begin
  1275. right:=csubscriptnode.create(
  1276. hsym,
  1277. ctypeconvnode.create_internal(right,methodpointertype));
  1278. end;
  1279. if (ld.deftype=procvardef) and
  1280. (not tprocvardef(ld).is_addressonly) then
  1281. begin
  1282. left:=csubscriptnode.create(
  1283. hsym,
  1284. ctypeconvnode.create_internal(left,methodpointertype));
  1285. end;
  1286. end
  1287. { support dynamicarray=nil,dynamicarray<>nil }
  1288. else if (is_dynamic_array(ld) and (rt=niln)) or
  1289. (is_dynamic_array(rd) and (lt=niln)) or
  1290. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1291. begin
  1292. if not(nodetype in [equaln,unequaln]) then
  1293. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1294. end
  1295. {$ifdef SUPPORT_MMX}
  1296. { mmx support, this must be before the zero based array
  1297. check }
  1298. else if (cs_mmx in aktlocalswitches) and
  1299. is_mmx_able_array(ld) and
  1300. is_mmx_able_array(rd) and
  1301. equal_defs(ld,rd) then
  1302. begin
  1303. case nodetype of
  1304. addn,subn,xorn,orn,andn:
  1305. ;
  1306. { mul is a little bit restricted }
  1307. muln:
  1308. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1309. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1310. else
  1311. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1312. end;
  1313. end
  1314. {$endif SUPPORT_MMX}
  1315. { this is a little bit dangerous, also the left type }
  1316. { pointer to should be checked! This broke the mmx support }
  1317. else if (rd.deftype=pointerdef) or is_zero_based_array(rd) then
  1318. begin
  1319. if is_zero_based_array(rd) then
  1320. begin
  1321. resulttype.setdef(tpointerdef.create(tarraydef(rd).elementtype));
  1322. inserttypeconv(right,resulttype);
  1323. end
  1324. else
  1325. resulttype:=right.resulttype;
  1326. inserttypeconv(left,sinttype);
  1327. if nodetype=addn then
  1328. begin
  1329. if not(cs_extsyntax in aktmoduleswitches) or
  1330. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1331. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1332. if (rd.deftype=pointerdef) and
  1333. (tpointerdef(rd).pointertype.def.size>1) then
  1334. begin
  1335. left:=caddnode.create(muln,left,
  1336. cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,true));
  1337. end;
  1338. end
  1339. else
  1340. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1341. end
  1342. else if (ld.deftype=pointerdef) or is_zero_based_array(ld) then
  1343. begin
  1344. if is_zero_based_array(ld) then
  1345. begin
  1346. resulttype.setdef(tpointerdef.create(tarraydef(ld).elementtype));
  1347. inserttypeconv(left,resulttype);
  1348. end
  1349. else
  1350. resulttype:=left.resulttype;
  1351. inserttypeconv(right,sinttype);
  1352. if nodetype in [addn,subn] then
  1353. begin
  1354. if not(cs_extsyntax in aktmoduleswitches) or
  1355. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1356. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1357. if (ld.deftype=pointerdef) and
  1358. (tpointerdef(ld).pointertype.def.size>1) then
  1359. begin
  1360. right:=caddnode.create(muln,right,
  1361. cordconstnode.create(tpointerdef(ld).pointertype.def.size,sinttype,true));
  1362. end
  1363. else
  1364. if is_zero_based_array(ld) and
  1365. (tarraydef(ld).elementtype.def.size>1) then
  1366. begin
  1367. right:=caddnode.create(muln,right,
  1368. cordconstnode.create(tarraydef(ld).elementtype.def.size,sinttype,true));
  1369. end;
  1370. end
  1371. else
  1372. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1373. end
  1374. else if (rd.deftype=procvardef) and
  1375. (ld.deftype=procvardef) and
  1376. equal_defs(rd,ld) then
  1377. begin
  1378. if (nodetype in [equaln,unequaln]) then
  1379. begin
  1380. if tprocvardef(rd).is_addressonly then
  1381. begin
  1382. inserttypeconv_internal(right,voidpointertype);
  1383. inserttypeconv_internal(left,voidpointertype);
  1384. end
  1385. else
  1386. begin
  1387. { find proc field in methodpointer record }
  1388. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('proc'));
  1389. if not assigned(hsym) then
  1390. internalerror(200412043);
  1391. { Compare tmehodpointer(left).proc }
  1392. right:=csubscriptnode.create(
  1393. hsym,
  1394. ctypeconvnode.create_internal(right,methodpointertype));
  1395. left:=csubscriptnode.create(
  1396. hsym,
  1397. ctypeconvnode.create_internal(left,methodpointertype));
  1398. end;
  1399. end
  1400. else
  1401. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1402. end
  1403. { enums }
  1404. else if (ld.deftype=enumdef) and (rd.deftype=enumdef) then
  1405. begin
  1406. if allowenumop(nodetype) then
  1407. inserttypeconv(right,left.resulttype)
  1408. else
  1409. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1410. end
  1411. { generic conversion, this is for error recovery }
  1412. else
  1413. begin
  1414. inserttypeconv(left,sinttype);
  1415. inserttypeconv(right,sinttype);
  1416. end;
  1417. { set resulttype if not already done }
  1418. if not assigned(resulttype.def) then
  1419. begin
  1420. case nodetype of
  1421. ltn,lten,gtn,gten,equaln,unequaln :
  1422. resulttype:=booltype;
  1423. slashn :
  1424. resulttype:=resultrealtype;
  1425. addn:
  1426. begin
  1427. { for strings, return is always a 255 char string }
  1428. if is_shortstring(left.resulttype.def) then
  1429. resulttype:=cshortstringtype
  1430. else
  1431. resulttype:=left.resulttype;
  1432. end;
  1433. else
  1434. resulttype:=left.resulttype;
  1435. end;
  1436. end;
  1437. { when the result is currency we need some extra code for
  1438. multiplication and division. this should not be done when
  1439. the muln or slashn node is created internally }
  1440. if not(nf_is_currency in flags) and
  1441. is_currency(resulttype.def) then
  1442. begin
  1443. case nodetype of
  1444. slashn :
  1445. begin
  1446. { slashn will only work with floats }
  1447. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1448. include(hp.flags,nf_is_currency);
  1449. result:=hp;
  1450. end;
  1451. muln :
  1452. begin
  1453. if s64currencytype.def.deftype=floatdef then
  1454. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1455. else
  1456. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1457. include(hp.flags,nf_is_currency);
  1458. result:=hp
  1459. end;
  1460. end;
  1461. end;
  1462. end;
  1463. function taddnode.first_addstring: tnode;
  1464. var
  1465. p: tnode;
  1466. begin
  1467. { when we get here, we are sure that both the left and the right }
  1468. { node are both strings of the same stringtype (JM) }
  1469. case nodetype of
  1470. addn:
  1471. begin
  1472. { create the call to the concat routine both strings as arguments }
  1473. result := ccallnode.createintern('fpc_'+
  1474. tstringdef(resulttype.def).stringtypname+'_concat',
  1475. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1476. { we reused the arguments }
  1477. left := nil;
  1478. right := nil;
  1479. end;
  1480. ltn,lten,gtn,gten,equaln,unequaln :
  1481. begin
  1482. { generate better code for comparison with empty string, we
  1483. only need to compare the length with 0 }
  1484. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1485. (((left.nodetype=stringconstn) and (str_length(left)=0)) or
  1486. ((right.nodetype=stringconstn) and (str_length(right)=0))) then
  1487. begin
  1488. { switch so that the constant is always on the right }
  1489. if left.nodetype = stringconstn then
  1490. begin
  1491. p := left;
  1492. left := right;
  1493. right := p;
  1494. end;
  1495. if is_shortstring(left.resulttype.def) or
  1496. (nodetype in [gtn,gten,ltn,lten]) then
  1497. { compare the length with 0 }
  1498. result := caddnode.create(nodetype,
  1499. cinlinenode.create(in_length_x,false,left),
  1500. cordconstnode.create(0,s32inttype,false))
  1501. else
  1502. begin
  1503. { compare the pointer with nil (for ansistrings etc), }
  1504. { faster than getting the length (JM) }
  1505. result:= caddnode.create(nodetype,
  1506. ctypeconvnode.create_internal(left,voidpointertype),
  1507. cpointerconstnode.create(0,voidpointertype));
  1508. end;
  1509. { left is reused }
  1510. left := nil;
  1511. { right isn't }
  1512. right.free;
  1513. right := nil;
  1514. exit;
  1515. end;
  1516. { no string constant -> call compare routine }
  1517. result := ccallnode.createintern('fpc_'+
  1518. tstringdef(left.resulttype.def).stringtypname+'_compare',
  1519. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1520. { and compare its result with 0 according to the original operator }
  1521. result := caddnode.create(nodetype,result,
  1522. cordconstnode.create(0,s32inttype,false));
  1523. left := nil;
  1524. right := nil;
  1525. end;
  1526. end;
  1527. end;
  1528. function taddnode.first_addset: tnode;
  1529. var
  1530. procname: string[31];
  1531. tempn: tnode;
  1532. paras: tcallparanode;
  1533. srsym: ttypesym;
  1534. begin
  1535. { get the sym that represents the fpc_normal_set type }
  1536. if not searchsystype('FPC_NORMAL_SET',srsym) then
  1537. internalerror(200108313);
  1538. case nodetype of
  1539. equaln,unequaln,lten,gten:
  1540. begin
  1541. case nodetype of
  1542. equaln,unequaln:
  1543. procname := 'fpc_set_comp_sets';
  1544. lten,gten:
  1545. begin
  1546. procname := 'fpc_set_contains_sets';
  1547. { (left >= right) = (right <= left) }
  1548. if nodetype = gten then
  1549. begin
  1550. tempn := left;
  1551. left := right;
  1552. right := tempn;
  1553. end;
  1554. end;
  1555. end;
  1556. { convert the arguments (explicitely) to fpc_normal_set's }
  1557. left := ctypeconvnode.create_internal(left,srsym.restype);
  1558. right := ctypeconvnode.create_internal(right,srsym.restype);
  1559. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1560. ccallparanode.create(left,nil)));
  1561. { left and right are reused as parameters }
  1562. left := nil;
  1563. right := nil;
  1564. { for an unequaln, we have to negate the result of comp_sets }
  1565. if nodetype = unequaln then
  1566. result := cnotnode.create(result);
  1567. end;
  1568. addn:
  1569. begin
  1570. { optimize first loading of a set }
  1571. if (right.nodetype=setelementn) and
  1572. not(assigned(tsetelementnode(right).right)) and
  1573. is_emptyset(left) then
  1574. begin
  1575. { type cast the value to pass as argument to a byte, }
  1576. { since that's what the helper expects }
  1577. tsetelementnode(right).left :=
  1578. ctypeconvnode.create_internal(tsetelementnode(right).left,u8inttype);
  1579. { set the resulttype to the actual one (otherwise it's }
  1580. { "fpc_normal_set") }
  1581. result := ccallnode.createinternres('fpc_set_create_element',
  1582. ccallparanode.create(tsetelementnode(right).left,nil),
  1583. resulttype);
  1584. { reused }
  1585. tsetelementnode(right).left := nil;
  1586. end
  1587. else
  1588. begin
  1589. if right.nodetype=setelementn then
  1590. begin
  1591. { convert the arguments to bytes, since that's what }
  1592. { the helper expects }
  1593. tsetelementnode(right).left :=
  1594. ctypeconvnode.create_internal(tsetelementnode(right).left,
  1595. u8inttype);
  1596. { convert the original set (explicitely) to an }
  1597. { fpc_normal_set so we can pass it to the helper }
  1598. left := ctypeconvnode.create_internal(left,srsym.restype);
  1599. { add a range or a single element? }
  1600. if assigned(tsetelementnode(right).right) then
  1601. begin
  1602. tsetelementnode(right).right :=
  1603. ctypeconvnode.create_internal(tsetelementnode(right).right,
  1604. u8inttype);
  1605. { create the call }
  1606. result := ccallnode.createinternres('fpc_set_set_range',
  1607. ccallparanode.create(tsetelementnode(right).right,
  1608. ccallparanode.create(tsetelementnode(right).left,
  1609. ccallparanode.create(left,nil))),resulttype);
  1610. end
  1611. else
  1612. begin
  1613. result := ccallnode.createinternres('fpc_set_set_byte',
  1614. ccallparanode.create(tsetelementnode(right).left,
  1615. ccallparanode.create(left,nil)),resulttype);
  1616. end;
  1617. { remove reused parts from original node }
  1618. tsetelementnode(right).right := nil;
  1619. tsetelementnode(right).left := nil;
  1620. left := nil;
  1621. end
  1622. else
  1623. begin
  1624. { add two sets }
  1625. { convert the sets to fpc_normal_set's }
  1626. result := ccallnode.createinternres('fpc_set_add_sets',
  1627. ccallparanode.create(
  1628. ctypeconvnode.create_explicit(right,srsym.restype),
  1629. ccallparanode.create(
  1630. ctypeconvnode.create_internal(left,srsym.restype),nil)),resulttype);
  1631. { remove reused parts from original node }
  1632. left := nil;
  1633. right := nil;
  1634. end;
  1635. end
  1636. end;
  1637. subn,symdifn,muln:
  1638. begin
  1639. { convert the sets to fpc_normal_set's }
  1640. paras := ccallparanode.create(ctypeconvnode.create_internal(right,srsym.restype),
  1641. ccallparanode.create(ctypeconvnode.create_internal(left,srsym.restype),nil));
  1642. case nodetype of
  1643. subn:
  1644. result := ccallnode.createinternres('fpc_set_sub_sets',
  1645. paras,resulttype);
  1646. symdifn:
  1647. result := ccallnode.createinternres('fpc_set_symdif_sets',
  1648. paras,resulttype);
  1649. muln:
  1650. result := ccallnode.createinternres('fpc_set_mul_sets',
  1651. paras,resulttype);
  1652. end;
  1653. { remove reused parts from original node }
  1654. left := nil;
  1655. right := nil;
  1656. end;
  1657. else
  1658. internalerror(200108311);
  1659. end;
  1660. end;
  1661. function taddnode.first_add64bitint: tnode;
  1662. var
  1663. procname: string[31];
  1664. temp: tnode;
  1665. power: longint;
  1666. begin
  1667. result := nil;
  1668. { create helper calls mul }
  1669. if nodetype <> muln then
  1670. exit;
  1671. { make sure that if there is a constant, that it's on the right }
  1672. if left.nodetype = ordconstn then
  1673. begin
  1674. temp := right;
  1675. right := left;
  1676. left := temp;
  1677. end;
  1678. { can we use a shift instead of a mul? }
  1679. if not (cs_check_overflow in aktlocalswitches) and
  1680. (right.nodetype = ordconstn) and
  1681. ispowerof2(tordconstnode(right).value,power) then
  1682. begin
  1683. tordconstnode(right).value := power;
  1684. result := cshlshrnode.create(shln,left,right);
  1685. { left and right are reused }
  1686. left := nil;
  1687. right := nil;
  1688. { return firstpassed new node }
  1689. exit;
  1690. end;
  1691. { when currency is used set the result of the
  1692. parameters to s64bit, so they are not converted }
  1693. if is_currency(resulttype.def) then
  1694. begin
  1695. left.resulttype:=s64inttype;
  1696. right.resulttype:=s64inttype;
  1697. end;
  1698. { otherwise, create the parameters for the helper }
  1699. right := ccallparanode.create(
  1700. cordconstnode.create(ord(cs_check_overflow in aktlocalswitches),booltype,true),
  1701. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1702. left := nil;
  1703. { only qword needs the unsigned code, the
  1704. signed code is also used for currency }
  1705. if is_signed(resulttype.def) then
  1706. procname := 'fpc_mul_int64'
  1707. else
  1708. procname := 'fpc_mul_qword';
  1709. result := ccallnode.createintern(procname,right);
  1710. right := nil;
  1711. end;
  1712. function taddnode.first_addfloat : tnode;
  1713. var
  1714. procname: string[31];
  1715. temp: tnode;
  1716. power: longint;
  1717. { do we need to reverse the result ? }
  1718. notnode : boolean;
  1719. begin
  1720. result := nil;
  1721. notnode := false;
  1722. { In non-emulation mode, real opcodes are
  1723. emitted for floating point values.
  1724. }
  1725. if not (cs_fp_emulation in aktmoduleswitches) then
  1726. exit;
  1727. if not(target_info.system in system_wince) then
  1728. begin
  1729. case tfloatdef(resulttype.def).typ of
  1730. s32real:
  1731. procname:='float32';
  1732. s64real:
  1733. procname:='float64';
  1734. {!!! not yet implemented
  1735. s128real:
  1736. }
  1737. else
  1738. internalerror(2005082601);
  1739. end;
  1740. case nodetype of
  1741. addn:
  1742. procname:=procname+'_add';
  1743. muln:
  1744. procname:=procname+'_mul';
  1745. subn:
  1746. procname:=procname+'_sub';
  1747. slashn:
  1748. procname:=procname+'_div';
  1749. ltn:
  1750. procname:=procname+'_lt';
  1751. lten:
  1752. procname:=procname+'_le';
  1753. gtn:
  1754. begin
  1755. procname:=procname+'_le';
  1756. notnode:=true;
  1757. end;
  1758. gten:
  1759. begin
  1760. procname:=procname+'_lt';
  1761. notnode:=true;
  1762. end;
  1763. equaln:
  1764. procname:=procname+'_eq';
  1765. unequaln:
  1766. begin
  1767. procname:=procname+'_eq';
  1768. notnode:=true;
  1769. end;
  1770. else
  1771. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resulttype.def.typename,right.resulttype.def.typename);
  1772. end;
  1773. end
  1774. else
  1775. begin
  1776. case nodetype of
  1777. addn:
  1778. procname:='ADD';
  1779. muln:
  1780. procname:='MUL';
  1781. subn:
  1782. procname:='SUB';
  1783. slashn:
  1784. procname:='DIV';
  1785. ltn:
  1786. procname:='LT';
  1787. lten:
  1788. procname:='LE';
  1789. gtn:
  1790. procname:='GT';
  1791. gten:
  1792. procname:='GE';
  1793. equaln:
  1794. procname:='EQ';
  1795. unequaln:
  1796. procname:='NE';
  1797. else
  1798. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resulttype.def.typename,right.resulttype.def.typename);
  1799. end;
  1800. case tfloatdef(resulttype.def).typ of
  1801. s32real:
  1802. procname:=procname+'S';
  1803. s64real:
  1804. procname:=procname+'D';
  1805. {!!! not yet implemented
  1806. s128real:
  1807. }
  1808. else
  1809. internalerror(2005082602);
  1810. end;
  1811. end;
  1812. if procname='MULS' then
  1813. procname:='MULD';
  1814. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  1815. ccallparanode.create(left,nil)));
  1816. left:=nil;
  1817. right:=nil;
  1818. { do we need to reverse the result }
  1819. if notnode then
  1820. result:=cnotnode.create(result);
  1821. end;
  1822. function taddnode.pass_1 : tnode;
  1823. var
  1824. {$ifdef addstringopt}
  1825. hp : tnode;
  1826. {$endif addstringopt}
  1827. lt,rt : tnodetype;
  1828. rd,ld : tdef;
  1829. begin
  1830. result:=nil;
  1831. { first do the two subtrees }
  1832. firstpass(left);
  1833. firstpass(right);
  1834. if codegenerror then
  1835. exit;
  1836. { load easier access variables }
  1837. rd:=right.resulttype.def;
  1838. ld:=left.resulttype.def;
  1839. rt:=right.nodetype;
  1840. lt:=left.nodetype;
  1841. { int/int gives real/real! }
  1842. if nodetype=slashn then
  1843. begin
  1844. {$ifdef cpufpemu}
  1845. if (aktfputype=fpu_soft) or (cs_fp_emulation in aktmoduleswitches) then
  1846. begin
  1847. result:=first_addfloat;
  1848. if assigned(result) then
  1849. exit;
  1850. end;
  1851. {$endif cpufpemu}
  1852. expectloc:=LOC_FPUREGISTER;
  1853. { maybe we need an integer register to save }
  1854. { a reference }
  1855. if ((left.expectloc<>LOC_FPUREGISTER) or
  1856. (right.expectloc<>LOC_FPUREGISTER)) and
  1857. (left.registersint=right.registersint) then
  1858. calcregisters(self,1,1,0)
  1859. else
  1860. calcregisters(self,0,1,0);
  1861. { an add node always first loads both the left and the }
  1862. { right in the fpu before doing the calculation. However, }
  1863. { calcregisters(0,2,0) will overestimate the number of }
  1864. { necessary registers (it will make it 3 in case one of }
  1865. { the operands is already in the fpu) (JM) }
  1866. if ((left.expectloc<>LOC_FPUREGISTER) or
  1867. (right.expectloc<>LOC_FPUREGISTER)) and
  1868. (registersfpu < 2) then
  1869. inc(registersfpu);
  1870. end
  1871. { if both are orddefs then check sub types }
  1872. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  1873. begin
  1874. { 2 booleans ? }
  1875. if is_boolean(ld) and is_boolean(rd) then
  1876. begin
  1877. if not(cs_full_boolean_eval in aktlocalswitches) and
  1878. (nodetype in [andn,orn]) then
  1879. begin
  1880. expectloc:=LOC_JUMP;
  1881. calcregisters(self,0,0,0);
  1882. end
  1883. else
  1884. begin
  1885. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1886. begin
  1887. expectloc:=LOC_FLAGS;
  1888. if (left.expectloc in [LOC_JUMP,LOC_FLAGS]) and
  1889. (left.expectloc in [LOC_JUMP,LOC_FLAGS]) then
  1890. calcregisters(self,2,0,0)
  1891. else
  1892. calcregisters(self,1,0,0);
  1893. end
  1894. else
  1895. begin
  1896. expectloc:=LOC_REGISTER;
  1897. calcregisters(self,0,0,0);
  1898. end;
  1899. end;
  1900. end
  1901. else
  1902. { Both are chars? only convert to shortstrings for addn }
  1903. if is_char(ld) then
  1904. begin
  1905. if nodetype=addn then
  1906. internalerror(200103291);
  1907. expectloc:=LOC_FLAGS;
  1908. calcregisters(self,1,0,0);
  1909. end
  1910. {$ifndef cpu64bit}
  1911. { is there a 64 bit type ? }
  1912. else if (torddef(ld).typ in [s64bit,u64bit,scurrency]) then
  1913. begin
  1914. result := first_add64bitint;
  1915. if assigned(result) then
  1916. exit;
  1917. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1918. expectloc:=LOC_REGISTER
  1919. else
  1920. expectloc:=LOC_JUMP;
  1921. calcregisters(self,2,0,0)
  1922. end
  1923. {$endif cpu64bit}
  1924. { is there a cardinal? }
  1925. else if (torddef(ld).typ=u32bit) then
  1926. begin
  1927. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1928. expectloc:=LOC_REGISTER
  1929. else
  1930. expectloc:=LOC_FLAGS;
  1931. calcregisters(self,1,0,0);
  1932. { for unsigned mul we need an extra register }
  1933. if nodetype=muln then
  1934. inc(registersint);
  1935. end
  1936. { generic s32bit conversion }
  1937. else
  1938. begin
  1939. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1940. expectloc:=LOC_REGISTER
  1941. else
  1942. expectloc:=LOC_FLAGS;
  1943. calcregisters(self,1,0,0);
  1944. end;
  1945. end
  1946. { left side a setdef, must be before string processing,
  1947. else array constructor can be seen as array of char (PFV) }
  1948. else if (ld.deftype=setdef) then
  1949. begin
  1950. if tsetdef(ld).settype=smallset then
  1951. begin
  1952. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1953. expectloc:=LOC_FLAGS
  1954. else
  1955. expectloc:=LOC_REGISTER;
  1956. { are we adding set elements ? }
  1957. if right.nodetype=setelementn then
  1958. calcregisters(self,2,0,0)
  1959. else
  1960. calcregisters(self,1,0,0);
  1961. end
  1962. else
  1963. {$ifdef MMXSET}
  1964. {$ifdef i386}
  1965. if cs_mmx in aktlocalswitches then
  1966. begin
  1967. expectloc:=LOC_MMXREGISTER;
  1968. calcregisters(self,0,0,4);
  1969. end
  1970. else
  1971. {$endif}
  1972. {$endif MMXSET}
  1973. begin
  1974. result := first_addset;
  1975. if assigned(result) then
  1976. exit;
  1977. expectloc:=LOC_CREFERENCE;
  1978. calcregisters(self,0,0,0);
  1979. { here we call SET... }
  1980. include(current_procinfo.flags,pi_do_call);
  1981. end;
  1982. end
  1983. { compare pchar by addresses like BP/Delphi }
  1984. else if is_pchar(ld) then
  1985. begin
  1986. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1987. expectloc:=LOC_REGISTER
  1988. else
  1989. expectloc:=LOC_FLAGS;
  1990. calcregisters(self,1,0,0);
  1991. end
  1992. { is one of the operands a string }
  1993. else if (ld.deftype=stringdef) then
  1994. begin
  1995. if is_widestring(ld) then
  1996. begin
  1997. { this is only for add, the comparisaion is handled later }
  1998. expectloc:=LOC_REGISTER;
  1999. end
  2000. else if is_ansistring(ld) then
  2001. begin
  2002. { this is only for add, the comparisaion is handled later }
  2003. expectloc:=LOC_REGISTER;
  2004. end
  2005. else if is_longstring(ld) then
  2006. begin
  2007. { this is only for add, the comparisaion is handled later }
  2008. expectloc:=LOC_REFERENCE;
  2009. end
  2010. else
  2011. begin
  2012. {$ifdef addstringopt}
  2013. { can create a call which isn't handled by callparatemp }
  2014. if canbeaddsstringcharoptnode(self) then
  2015. begin
  2016. hp := genaddsstringcharoptnode(self);
  2017. pass_1 := hp;
  2018. exit;
  2019. end
  2020. else
  2021. {$endif addstringopt}
  2022. begin
  2023. { Fix right to be shortstring }
  2024. if is_char(right.resulttype.def) then
  2025. begin
  2026. inserttypeconv(right,cshortstringtype);
  2027. firstpass(right);
  2028. end;
  2029. end;
  2030. {$ifdef addstringopt}
  2031. { can create a call which isn't handled by callparatemp }
  2032. if canbeaddsstringcsstringoptnode(self) then
  2033. begin
  2034. hp := genaddsstringcsstringoptnode(self);
  2035. pass_1 := hp;
  2036. exit;
  2037. end;
  2038. {$endif addstringopt}
  2039. end;
  2040. { otherwise, let addstring convert everything }
  2041. result := first_addstring;
  2042. exit;
  2043. end
  2044. { is one a real float ? }
  2045. else if (rd.deftype=floatdef) or (ld.deftype=floatdef) then
  2046. begin
  2047. {$ifdef cpufpemu}
  2048. if (aktfputype=fpu_soft) or (cs_fp_emulation in aktmoduleswitches) then
  2049. begin
  2050. result:=first_addfloat;
  2051. if assigned(result) then
  2052. exit;
  2053. end;
  2054. {$endif cpufpemu}
  2055. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2056. expectloc:=LOC_FPUREGISTER
  2057. else
  2058. expectloc:=LOC_FLAGS;
  2059. calcregisters(self,0,1,0);
  2060. { an add node always first loads both the left and the }
  2061. { right in the fpu before doing the calculation. However, }
  2062. { calcregisters(0,2,0) will overestimate the number of }
  2063. { necessary registers (it will make it 3 in case one of }
  2064. { the operands is already in the fpu) (JM) }
  2065. if ((left.expectloc<>LOC_FPUREGISTER) or
  2066. (right.expectloc<>LOC_FPUREGISTER)) and
  2067. (registersfpu < 2) then
  2068. inc(registersfpu);
  2069. end
  2070. { pointer comperation and subtraction }
  2071. else if (ld.deftype=pointerdef) then
  2072. begin
  2073. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2074. expectloc:=LOC_REGISTER
  2075. else
  2076. expectloc:=LOC_FLAGS;
  2077. calcregisters(self,1,0,0);
  2078. end
  2079. else if is_class_or_interface(ld) then
  2080. begin
  2081. expectloc:=LOC_FLAGS;
  2082. calcregisters(self,1,0,0);
  2083. end
  2084. else if (ld.deftype=classrefdef) then
  2085. begin
  2086. expectloc:=LOC_FLAGS;
  2087. calcregisters(self,1,0,0);
  2088. end
  2089. { support procvar=nil,procvar<>nil }
  2090. else if ((ld.deftype=procvardef) and (rt=niln)) or
  2091. ((rd.deftype=procvardef) and (lt=niln)) then
  2092. begin
  2093. expectloc:=LOC_FLAGS;
  2094. calcregisters(self,1,0,0);
  2095. end
  2096. {$ifdef SUPPORT_MMX}
  2097. { mmx support, this must be before the zero based array
  2098. check }
  2099. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  2100. is_mmx_able_array(rd) then
  2101. begin
  2102. expectloc:=LOC_MMXREGISTER;
  2103. calcregisters(self,0,0,1);
  2104. end
  2105. {$endif SUPPORT_MMX}
  2106. else if (rd.deftype=pointerdef) or (ld.deftype=pointerdef) then
  2107. begin
  2108. expectloc:=LOC_REGISTER;
  2109. calcregisters(self,1,0,0);
  2110. end
  2111. else if (rd.deftype=procvardef) and
  2112. (ld.deftype=procvardef) and
  2113. equal_defs(rd,ld) then
  2114. begin
  2115. expectloc:=LOC_FLAGS;
  2116. calcregisters(self,1,0,0);
  2117. end
  2118. else if (ld.deftype=enumdef) then
  2119. begin
  2120. expectloc:=LOC_FLAGS;
  2121. calcregisters(self,1,0,0);
  2122. end
  2123. {$ifdef SUPPORT_MMX}
  2124. else if (cs_mmx in aktlocalswitches) and
  2125. is_mmx_able_array(ld) and
  2126. is_mmx_able_array(rd) then
  2127. begin
  2128. expectloc:=LOC_MMXREGISTER;
  2129. calcregisters(self,0,0,1);
  2130. end
  2131. {$endif SUPPORT_MMX}
  2132. { the general solution is to convert to 32 bit int }
  2133. else
  2134. begin
  2135. expectloc:=LOC_REGISTER;
  2136. calcregisters(self,1,0,0);
  2137. end;
  2138. end;
  2139. {$ifdef state_tracking}
  2140. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2141. var factval:Tnode;
  2142. begin
  2143. track_state_pass:=false;
  2144. if left.track_state_pass(exec_known) then
  2145. begin
  2146. track_state_pass:=true;
  2147. left.resulttype.def:=nil;
  2148. do_resulttypepass(left);
  2149. end;
  2150. factval:=aktstate.find_fact(left);
  2151. if factval<>nil then
  2152. begin
  2153. track_state_pass:=true;
  2154. left.destroy;
  2155. left:=factval.getcopy;
  2156. end;
  2157. if right.track_state_pass(exec_known) then
  2158. begin
  2159. track_state_pass:=true;
  2160. right.resulttype.def:=nil;
  2161. do_resulttypepass(right);
  2162. end;
  2163. factval:=aktstate.find_fact(right);
  2164. if factval<>nil then
  2165. begin
  2166. track_state_pass:=true;
  2167. right.destroy;
  2168. right:=factval.getcopy;
  2169. end;
  2170. end;
  2171. {$endif}
  2172. begin
  2173. caddnode:=taddnode;
  2174. end.