nadd.pas 89 KB

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