nadd.pas 87 KB

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