nadd.pas 88 KB

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