nadd.pas 85 KB

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