nadd.pas 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208
  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. const
  1449. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  1450. var
  1451. p: tnode;
  1452. begin
  1453. { when we get here, we are sure that both the left and the right }
  1454. { node are both strings of the same stringtype (JM) }
  1455. case nodetype of
  1456. addn:
  1457. begin
  1458. { create the call to the concat routine both strings as arguments }
  1459. result := ccallnode.createintern('fpc_'+
  1460. tstringdef(resulttype.def).stringtypname+'_concat',
  1461. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1462. { we reused the arguments }
  1463. left := nil;
  1464. right := nil;
  1465. end;
  1466. ltn,lten,gtn,gten,equaln,unequaln :
  1467. begin
  1468. { generate better code for comparison with empty string, we
  1469. only need to compare the length with 0 }
  1470. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1471. (((left.nodetype=stringconstn) and (str_length(left)=0)) or
  1472. ((right.nodetype=stringconstn) and (str_length(right)=0))) then
  1473. begin
  1474. { switch so that the constant is always on the right }
  1475. if left.nodetype = stringconstn then
  1476. begin
  1477. p := left;
  1478. left := right;
  1479. right := p;
  1480. nodetype:=swap_relation[nodetype];
  1481. end;
  1482. if is_shortstring(left.resulttype.def) or
  1483. (nodetype in [gtn,gten,ltn,lten]) then
  1484. { compare the length with 0 }
  1485. result := caddnode.create(nodetype,
  1486. cinlinenode.create(in_length_x,false,left),
  1487. cordconstnode.create(0,s32inttype,false))
  1488. else
  1489. begin
  1490. { compare the pointer with nil (for ansistrings etc), }
  1491. { faster than getting the length (JM) }
  1492. result:= caddnode.create(nodetype,
  1493. ctypeconvnode.create_internal(left,voidpointertype),
  1494. cpointerconstnode.create(0,voidpointertype));
  1495. end;
  1496. { left is reused }
  1497. left := nil;
  1498. { right isn't }
  1499. right.free;
  1500. right := nil;
  1501. exit;
  1502. end;
  1503. { no string constant -> call compare routine }
  1504. result := ccallnode.createintern('fpc_'+
  1505. tstringdef(left.resulttype.def).stringtypname+'_compare',
  1506. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1507. { and compare its result with 0 according to the original operator }
  1508. result := caddnode.create(nodetype,result,
  1509. cordconstnode.create(0,s32inttype,false));
  1510. left := nil;
  1511. right := nil;
  1512. end;
  1513. end;
  1514. end;
  1515. function taddnode.first_addset: tnode;
  1516. var
  1517. procname: string[31];
  1518. tempn: tnode;
  1519. paras: tcallparanode;
  1520. srsym: ttypesym;
  1521. begin
  1522. { get the sym that represents the fpc_normal_set type }
  1523. if not searchsystype('FPC_NORMAL_SET',srsym) then
  1524. internalerror(200108313);
  1525. case nodetype of
  1526. equaln,unequaln,lten,gten:
  1527. begin
  1528. case nodetype of
  1529. equaln,unequaln:
  1530. procname := 'fpc_set_comp_sets';
  1531. lten,gten:
  1532. begin
  1533. procname := 'fpc_set_contains_sets';
  1534. { (left >= right) = (right <= left) }
  1535. if nodetype = gten then
  1536. begin
  1537. tempn := left;
  1538. left := right;
  1539. right := tempn;
  1540. end;
  1541. end;
  1542. end;
  1543. { convert the arguments (explicitely) to fpc_normal_set's }
  1544. left := ctypeconvnode.create_internal(left,srsym.restype);
  1545. right := ctypeconvnode.create_internal(right,srsym.restype);
  1546. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1547. ccallparanode.create(left,nil)));
  1548. { left and right are reused as parameters }
  1549. left := nil;
  1550. right := nil;
  1551. { for an unequaln, we have to negate the result of comp_sets }
  1552. if nodetype = unequaln then
  1553. result := cnotnode.create(result);
  1554. end;
  1555. addn:
  1556. begin
  1557. { optimize first loading of a set }
  1558. if (right.nodetype=setelementn) and
  1559. not(assigned(tsetelementnode(right).right)) and
  1560. is_emptyset(left) then
  1561. begin
  1562. { type cast the value to pass as argument to a byte, }
  1563. { since that's what the helper expects }
  1564. tsetelementnode(right).left :=
  1565. ctypeconvnode.create_internal(tsetelementnode(right).left,u8inttype);
  1566. { set the resulttype to the actual one (otherwise it's }
  1567. { "fpc_normal_set") }
  1568. result := ccallnode.createinternres('fpc_set_create_element',
  1569. ccallparanode.create(tsetelementnode(right).left,nil),
  1570. resulttype);
  1571. { reused }
  1572. tsetelementnode(right).left := nil;
  1573. end
  1574. else
  1575. begin
  1576. if right.nodetype=setelementn then
  1577. begin
  1578. { convert the arguments to bytes, since that's what }
  1579. { the helper expects }
  1580. tsetelementnode(right).left :=
  1581. ctypeconvnode.create_internal(tsetelementnode(right).left,
  1582. u8inttype);
  1583. { convert the original set (explicitely) to an }
  1584. { fpc_normal_set so we can pass it to the helper }
  1585. left := ctypeconvnode.create_internal(left,srsym.restype);
  1586. { add a range or a single element? }
  1587. if assigned(tsetelementnode(right).right) then
  1588. begin
  1589. tsetelementnode(right).right :=
  1590. ctypeconvnode.create_internal(tsetelementnode(right).right,
  1591. u8inttype);
  1592. { create the call }
  1593. result := ccallnode.createinternres('fpc_set_set_range',
  1594. ccallparanode.create(tsetelementnode(right).right,
  1595. ccallparanode.create(tsetelementnode(right).left,
  1596. ccallparanode.create(left,nil))),resulttype);
  1597. end
  1598. else
  1599. begin
  1600. result := ccallnode.createinternres('fpc_set_set_byte',
  1601. ccallparanode.create(tsetelementnode(right).left,
  1602. ccallparanode.create(left,nil)),resulttype);
  1603. end;
  1604. { remove reused parts from original node }
  1605. tsetelementnode(right).right := nil;
  1606. tsetelementnode(right).left := nil;
  1607. left := nil;
  1608. end
  1609. else
  1610. begin
  1611. { add two sets }
  1612. { convert the sets to fpc_normal_set's }
  1613. result := ccallnode.createinternres('fpc_set_add_sets',
  1614. ccallparanode.create(
  1615. ctypeconvnode.create_explicit(right,srsym.restype),
  1616. ccallparanode.create(
  1617. ctypeconvnode.create_internal(left,srsym.restype),nil)),resulttype);
  1618. { remove reused parts from original node }
  1619. left := nil;
  1620. right := nil;
  1621. end;
  1622. end
  1623. end;
  1624. subn,symdifn,muln:
  1625. begin
  1626. { convert the sets to fpc_normal_set's }
  1627. paras := ccallparanode.create(ctypeconvnode.create_internal(right,srsym.restype),
  1628. ccallparanode.create(ctypeconvnode.create_internal(left,srsym.restype),nil));
  1629. case nodetype of
  1630. subn:
  1631. result := ccallnode.createinternres('fpc_set_sub_sets',
  1632. paras,resulttype);
  1633. symdifn:
  1634. result := ccallnode.createinternres('fpc_set_symdif_sets',
  1635. paras,resulttype);
  1636. muln:
  1637. result := ccallnode.createinternres('fpc_set_mul_sets',
  1638. paras,resulttype);
  1639. end;
  1640. { remove reused parts from original node }
  1641. left := nil;
  1642. right := nil;
  1643. end;
  1644. else
  1645. internalerror(200108311);
  1646. end;
  1647. end;
  1648. function taddnode.first_add64bitint: tnode;
  1649. var
  1650. procname: string[31];
  1651. temp: tnode;
  1652. power: longint;
  1653. begin
  1654. result := nil;
  1655. { create helper calls mul }
  1656. if nodetype <> muln then
  1657. exit;
  1658. { make sure that if there is a constant, that it's on the right }
  1659. if left.nodetype = ordconstn then
  1660. begin
  1661. temp := right;
  1662. right := left;
  1663. left := temp;
  1664. end;
  1665. { can we use a shift instead of a mul? }
  1666. if not (cs_check_overflow in aktlocalswitches) and
  1667. (right.nodetype = ordconstn) and
  1668. ispowerof2(tordconstnode(right).value,power) then
  1669. begin
  1670. tordconstnode(right).value := power;
  1671. result := cshlshrnode.create(shln,left,right);
  1672. { left and right are reused }
  1673. left := nil;
  1674. right := nil;
  1675. { return firstpassed new node }
  1676. exit;
  1677. end;
  1678. { when currency is used set the result of the
  1679. parameters to s64bit, so they are not converted }
  1680. if is_currency(resulttype.def) then
  1681. begin
  1682. left.resulttype:=s64inttype;
  1683. right.resulttype:=s64inttype;
  1684. end;
  1685. { otherwise, create the parameters for the helper }
  1686. right := ccallparanode.create(
  1687. cordconstnode.create(ord(cs_check_overflow in aktlocalswitches),booltype,true),
  1688. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1689. left := nil;
  1690. { only qword needs the unsigned code, the
  1691. signed code is also used for currency }
  1692. if is_signed(resulttype.def) then
  1693. procname := 'fpc_mul_int64'
  1694. else
  1695. procname := 'fpc_mul_qword';
  1696. result := ccallnode.createintern(procname,right);
  1697. right := nil;
  1698. end;
  1699. {$ifdef cpufpemu}
  1700. function taddnode.first_addfloat: tnode;
  1701. var
  1702. procname: string[31];
  1703. { do we need to reverse the result ? }
  1704. notnode : boolean;
  1705. begin
  1706. result := nil;
  1707. notnode := false;
  1708. { In non-emulation mode, real opcodes are
  1709. emitted for floating point values.
  1710. }
  1711. if not (cs_fp_emulation in aktmoduleswitches) then
  1712. exit;
  1713. case nodetype of
  1714. addn : procname := 'fpc_single_add';
  1715. muln : procname := 'fpc_single_mul';
  1716. subn : procname := 'fpc_single_sub';
  1717. slashn : procname := 'fpc_single_div';
  1718. ltn : procname := 'fpc_single_lt';
  1719. lten: procname := 'fpc_single_le';
  1720. gtn:
  1721. begin
  1722. procname := 'fpc_single_le';
  1723. notnode := true;
  1724. end;
  1725. gten:
  1726. begin
  1727. procname := 'fpc_single_lt';
  1728. notnode := true;
  1729. end;
  1730. equaln: procname := 'fpc_single_eq';
  1731. unequaln :
  1732. begin
  1733. procname := 'fpc_single_eq';
  1734. notnode := true;
  1735. end;
  1736. else
  1737. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resulttype.def.typename,right.resulttype.def.typename);
  1738. end;
  1739. { convert the arguments (explicitely) to fpc_normal_set's }
  1740. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1741. ccallparanode.create(left,nil)));
  1742. left:=nil;
  1743. right:=nil;
  1744. { do we need to reverse the result }
  1745. if notnode then
  1746. result := cnotnode.create(result);
  1747. end;
  1748. {$endif cpufpemu}
  1749. function taddnode.pass_1 : tnode;
  1750. var
  1751. {$ifdef addstringopt}
  1752. hp : tnode;
  1753. {$endif addstringopt}
  1754. lt,rt : tnodetype;
  1755. rd,ld : tdef;
  1756. begin
  1757. result:=nil;
  1758. { first do the two subtrees }
  1759. firstpass(left);
  1760. firstpass(right);
  1761. if codegenerror then
  1762. exit;
  1763. { load easier access variables }
  1764. rd:=right.resulttype.def;
  1765. ld:=left.resulttype.def;
  1766. rt:=right.nodetype;
  1767. lt:=left.nodetype;
  1768. { int/int gives real/real! }
  1769. if nodetype=slashn then
  1770. begin
  1771. {$ifdef cpufpemu}
  1772. result := first_addfloat;
  1773. if assigned(result) then
  1774. exit;
  1775. {$endif cpufpemu}
  1776. expectloc:=LOC_FPUREGISTER;
  1777. { maybe we need an integer register to save }
  1778. { a reference }
  1779. if ((left.expectloc<>LOC_FPUREGISTER) or
  1780. (right.expectloc<>LOC_FPUREGISTER)) and
  1781. (left.registersint=right.registersint) then
  1782. calcregisters(self,1,1,0)
  1783. else
  1784. calcregisters(self,0,1,0);
  1785. { an add node always first loads both the left and the }
  1786. { right in the fpu before doing the calculation. However, }
  1787. { calcregisters(0,2,0) will overestimate the number of }
  1788. { necessary registers (it will make it 3 in case one of }
  1789. { the operands is already in the fpu) (JM) }
  1790. if ((left.expectloc<>LOC_FPUREGISTER) or
  1791. (right.expectloc<>LOC_FPUREGISTER)) and
  1792. (registersfpu < 2) then
  1793. inc(registersfpu);
  1794. end
  1795. { if both are orddefs then check sub types }
  1796. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  1797. begin
  1798. { 2 booleans ? }
  1799. if is_boolean(ld) and is_boolean(rd) then
  1800. begin
  1801. if not(cs_full_boolean_eval in aktlocalswitches) and
  1802. (nodetype in [andn,orn]) then
  1803. begin
  1804. expectloc:=LOC_JUMP;
  1805. calcregisters(self,0,0,0);
  1806. end
  1807. else
  1808. begin
  1809. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1810. begin
  1811. expectloc:=LOC_FLAGS;
  1812. if (left.expectloc in [LOC_JUMP,LOC_FLAGS]) and
  1813. (left.expectloc in [LOC_JUMP,LOC_FLAGS]) then
  1814. calcregisters(self,2,0,0)
  1815. else
  1816. calcregisters(self,1,0,0);
  1817. end
  1818. else
  1819. begin
  1820. expectloc:=LOC_REGISTER;
  1821. calcregisters(self,0,0,0);
  1822. end;
  1823. end;
  1824. end
  1825. else
  1826. { Both are chars? only convert to shortstrings for addn }
  1827. if is_char(ld) then
  1828. begin
  1829. if nodetype=addn then
  1830. internalerror(200103291);
  1831. expectloc:=LOC_FLAGS;
  1832. calcregisters(self,1,0,0);
  1833. end
  1834. {$ifndef cpu64bit}
  1835. { is there a 64 bit type ? }
  1836. else if (torddef(ld).typ in [s64bit,u64bit,scurrency]) then
  1837. begin
  1838. result := first_add64bitint;
  1839. if assigned(result) then
  1840. exit;
  1841. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1842. expectloc:=LOC_REGISTER
  1843. else
  1844. expectloc:=LOC_JUMP;
  1845. calcregisters(self,2,0,0)
  1846. end
  1847. {$endif cpu64bit}
  1848. { is there a cardinal? }
  1849. else if (torddef(ld).typ=u32bit) then
  1850. begin
  1851. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1852. expectloc:=LOC_REGISTER
  1853. else
  1854. expectloc:=LOC_FLAGS;
  1855. calcregisters(self,1,0,0);
  1856. { for unsigned mul we need an extra register }
  1857. if nodetype=muln then
  1858. inc(registersint);
  1859. end
  1860. { generic s32bit conversion }
  1861. else
  1862. begin
  1863. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1864. expectloc:=LOC_REGISTER
  1865. else
  1866. expectloc:=LOC_FLAGS;
  1867. calcregisters(self,1,0,0);
  1868. end;
  1869. end
  1870. { left side a setdef, must be before string processing,
  1871. else array constructor can be seen as array of char (PFV) }
  1872. else if (ld.deftype=setdef) then
  1873. begin
  1874. if tsetdef(ld).settype=smallset then
  1875. begin
  1876. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  1877. expectloc:=LOC_FLAGS
  1878. else
  1879. expectloc:=LOC_REGISTER;
  1880. { are we adding set elements ? }
  1881. if right.nodetype=setelementn then
  1882. calcregisters(self,2,0,0)
  1883. else
  1884. calcregisters(self,1,0,0);
  1885. end
  1886. else
  1887. {$ifdef MMXSET}
  1888. {$ifdef i386}
  1889. if cs_mmx in aktlocalswitches then
  1890. begin
  1891. expectloc:=LOC_MMXREGISTER;
  1892. calcregisters(self,0,0,4);
  1893. end
  1894. else
  1895. {$endif}
  1896. {$endif MMXSET}
  1897. begin
  1898. result := first_addset;
  1899. if assigned(result) then
  1900. exit;
  1901. expectloc:=LOC_CREFERENCE;
  1902. calcregisters(self,0,0,0);
  1903. { here we call SET... }
  1904. include(current_procinfo.flags,pi_do_call);
  1905. end;
  1906. end
  1907. { compare pchar by addresses like BP/Delphi }
  1908. else if is_pchar(ld) then
  1909. begin
  1910. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1911. expectloc:=LOC_REGISTER
  1912. else
  1913. expectloc:=LOC_FLAGS;
  1914. calcregisters(self,1,0,0);
  1915. end
  1916. { is one of the operands a string }
  1917. else if (ld.deftype=stringdef) then
  1918. begin
  1919. if is_widestring(ld) then
  1920. begin
  1921. { this is only for add, the comparisaion is handled later }
  1922. expectloc:=LOC_REGISTER;
  1923. end
  1924. else if is_ansistring(ld) then
  1925. begin
  1926. { this is only for add, the comparisaion is handled later }
  1927. expectloc:=LOC_REGISTER;
  1928. end
  1929. else if is_longstring(ld) then
  1930. begin
  1931. { this is only for add, the comparisaion is handled later }
  1932. expectloc:=LOC_REFERENCE;
  1933. end
  1934. else
  1935. begin
  1936. {$ifdef addstringopt}
  1937. { can create a call which isn't handled by callparatemp }
  1938. if canbeaddsstringcharoptnode(self) then
  1939. begin
  1940. hp := genaddsstringcharoptnode(self);
  1941. pass_1 := hp;
  1942. exit;
  1943. end
  1944. else
  1945. {$endif addstringopt}
  1946. begin
  1947. { Fix right to be shortstring }
  1948. if is_char(right.resulttype.def) then
  1949. begin
  1950. inserttypeconv(right,cshortstringtype);
  1951. firstpass(right);
  1952. end;
  1953. end;
  1954. {$ifdef addstringopt}
  1955. { can create a call which isn't handled by callparatemp }
  1956. if canbeaddsstringcsstringoptnode(self) then
  1957. begin
  1958. hp := genaddsstringcsstringoptnode(self);
  1959. pass_1 := hp;
  1960. exit;
  1961. end;
  1962. {$endif addstringopt}
  1963. end;
  1964. { otherwise, let addstring convert everything }
  1965. result := first_addstring;
  1966. exit;
  1967. end
  1968. { is one a real float ? }
  1969. else if (rd.deftype=floatdef) or (ld.deftype=floatdef) then
  1970. begin
  1971. {$ifdef cpufpemu}
  1972. result := first_addfloat;
  1973. if assigned(result) then
  1974. exit;
  1975. {$endif cpufpemu}
  1976. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1977. expectloc:=LOC_FPUREGISTER
  1978. else
  1979. expectloc:=LOC_FLAGS;
  1980. calcregisters(self,0,1,0);
  1981. { an add node always first loads both the left and the }
  1982. { right in the fpu before doing the calculation. However, }
  1983. { calcregisters(0,2,0) will overestimate the number of }
  1984. { necessary registers (it will make it 3 in case one of }
  1985. { the operands is already in the fpu) (JM) }
  1986. if ((left.expectloc<>LOC_FPUREGISTER) or
  1987. (right.expectloc<>LOC_FPUREGISTER)) and
  1988. (registersfpu < 2) then
  1989. inc(registersfpu);
  1990. end
  1991. { pointer comperation and subtraction }
  1992. else if (ld.deftype=pointerdef) then
  1993. begin
  1994. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1995. expectloc:=LOC_REGISTER
  1996. else
  1997. expectloc:=LOC_FLAGS;
  1998. calcregisters(self,1,0,0);
  1999. end
  2000. else if is_class_or_interface(ld) then
  2001. begin
  2002. expectloc:=LOC_FLAGS;
  2003. calcregisters(self,1,0,0);
  2004. end
  2005. else if (ld.deftype=classrefdef) then
  2006. begin
  2007. expectloc:=LOC_FLAGS;
  2008. calcregisters(self,1,0,0);
  2009. end
  2010. { support procvar=nil,procvar<>nil }
  2011. else if ((ld.deftype=procvardef) and (rt=niln)) or
  2012. ((rd.deftype=procvardef) and (lt=niln)) then
  2013. begin
  2014. expectloc:=LOC_FLAGS;
  2015. calcregisters(self,1,0,0);
  2016. end
  2017. {$ifdef SUPPORT_MMX}
  2018. { mmx support, this must be before the zero based array
  2019. check }
  2020. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  2021. is_mmx_able_array(rd) then
  2022. begin
  2023. expectloc:=LOC_MMXREGISTER;
  2024. calcregisters(self,0,0,1);
  2025. end
  2026. {$endif SUPPORT_MMX}
  2027. else if (rd.deftype=pointerdef) or (ld.deftype=pointerdef) then
  2028. begin
  2029. expectloc:=LOC_REGISTER;
  2030. calcregisters(self,1,0,0);
  2031. end
  2032. else if (rd.deftype=procvardef) and
  2033. (ld.deftype=procvardef) and
  2034. equal_defs(rd,ld) then
  2035. begin
  2036. expectloc:=LOC_FLAGS;
  2037. calcregisters(self,1,0,0);
  2038. end
  2039. else if (ld.deftype=enumdef) then
  2040. begin
  2041. expectloc:=LOC_FLAGS;
  2042. calcregisters(self,1,0,0);
  2043. end
  2044. {$ifdef SUPPORT_MMX}
  2045. else if (cs_mmx in aktlocalswitches) and
  2046. is_mmx_able_array(ld) and
  2047. is_mmx_able_array(rd) then
  2048. begin
  2049. expectloc:=LOC_MMXREGISTER;
  2050. calcregisters(self,0,0,1);
  2051. end
  2052. {$endif SUPPORT_MMX}
  2053. { the general solution is to convert to 32 bit int }
  2054. else
  2055. begin
  2056. expectloc:=LOC_REGISTER;
  2057. calcregisters(self,1,0,0);
  2058. end;
  2059. end;
  2060. {$ifdef state_tracking}
  2061. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2062. var factval:Tnode;
  2063. begin
  2064. track_state_pass:=false;
  2065. if left.track_state_pass(exec_known) then
  2066. begin
  2067. track_state_pass:=true;
  2068. left.resulttype.def:=nil;
  2069. do_resulttypepass(left);
  2070. end;
  2071. factval:=aktstate.find_fact(left);
  2072. if factval<>nil then
  2073. begin
  2074. track_state_pass:=true;
  2075. left.destroy;
  2076. left:=factval.getcopy;
  2077. end;
  2078. if right.track_state_pass(exec_known) then
  2079. begin
  2080. track_state_pass:=true;
  2081. right.resulttype.def:=nil;
  2082. do_resulttypepass(right);
  2083. end;
  2084. factval:=aktstate.find_fact(right);
  2085. if factval<>nil then
  2086. begin
  2087. track_state_pass:=true;
  2088. right.destroy;
  2089. right:=factval.getcopy;
  2090. end;
  2091. end;
  2092. {$endif}
  2093. begin
  2094. caddnode:=taddnode;
  2095. end.