nadd.pas 87 KB

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