nadd.pas 88 KB

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