nadd.pas 87 KB

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