nadd.pas 87 KB

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