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