nadd.pas 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  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. protected
  29. { override the following if you want to implement }
  30. { parts explicitely in the code generator (JM) }
  31. function first_addstring: tnode; virtual;
  32. function first_addset: tnode; virtual;
  33. { only implements "muln" nodes, the rest always has to be done in }
  34. { the code generator for performance reasons (JM) }
  35. function first_add64bitint: tnode; virtual;
  36. end;
  37. taddnodeclass = class of taddnode;
  38. var
  39. { caddnode is used to create nodes of the add type }
  40. { the virtual constructor allows to assign }
  41. { another class type to caddnode => processor }
  42. { specific node types can be created }
  43. caddnode : taddnodeclass;
  44. implementation
  45. uses
  46. globtype,systems,
  47. cutils,verbose,globals,widestr,
  48. symconst,symtype,symbase,symdef,symsym,symtable,types,
  49. cpuinfo,
  50. cgbase,
  51. htypechk,pass_1,
  52. nmat,ncnv,nld,ncon,nset,nopt,ncal,ninl,
  53. cpubase;
  54. {*****************************************************************************
  55. TADDNODE
  56. *****************************************************************************}
  57. {$ifdef fpc}
  58. {$maxfpuregisters 0}
  59. {$endif fpc}
  60. constructor taddnode.create(tt : tnodetype;l,r : tnode);
  61. begin
  62. inherited create(tt,l,r);
  63. end;
  64. function taddnode.det_resulttype:tnode;
  65. var
  66. hp,t : tnode;
  67. lt,rt : tnodetype;
  68. rd,ld : tdef;
  69. htype : ttype;
  70. ot : tnodetype;
  71. concatstrings : boolean;
  72. resultset : pconstset;
  73. i : longint;
  74. b : boolean;
  75. s1,s2 : pchar;
  76. ws1,ws2 : pcompilerwidestring;
  77. l1,l2 : longint;
  78. rv,lv : tconstexprint;
  79. rvd,lvd : bestreal;
  80. begin
  81. result:=nil;
  82. { first do the two subtrees }
  83. resulttypepass(left);
  84. resulttypepass(right);
  85. { both left and right need to be valid }
  86. set_varstate(left,true);
  87. set_varstate(right,true);
  88. if codegenerror then
  89. exit;
  90. { convert array constructors to sets, because there is no other operator
  91. possible for array constructors }
  92. if is_array_constructor(left.resulttype.def) then
  93. begin
  94. arrayconstructor_to_set(tarrayconstructornode(left));
  95. resulttypepass(left);
  96. end;
  97. if is_array_constructor(right.resulttype.def) then
  98. begin
  99. arrayconstructor_to_set(tarrayconstructornode(right));
  100. resulttypepass(right);
  101. end;
  102. { allow operator overloading }
  103. hp:=self;
  104. if isbinaryoverloaded(hp) then
  105. begin
  106. result:=hp;
  107. exit;
  108. end;
  109. { Kylix allows enum+ordconstn in an enum declaration (blocktype
  110. is bt_type), we need to do the conversion here before the
  111. constant folding }
  112. if (m_delphi in aktmodeswitches) and
  113. (blocktype=bt_type) then
  114. begin
  115. if (left.resulttype.def.deftype=enumdef) and
  116. (right.resulttype.def.deftype=orddef) then
  117. begin
  118. { insert explicit typecast to s32bit }
  119. left:=ctypeconvnode.create(left,s32bittype);
  120. include(left.flags,nf_explizit);
  121. resulttypepass(left);
  122. end
  123. else
  124. if (left.resulttype.def.deftype=orddef) and
  125. (right.resulttype.def.deftype=enumdef) then
  126. begin
  127. { insert explicit typecast to s32bit }
  128. right:=ctypeconvnode.create(right,s32bittype);
  129. include(right.flags,nf_explizit);
  130. resulttypepass(right);
  131. end;
  132. end;
  133. { is one a real float, then both need to be floats, this
  134. need to be done before the constant folding so constant
  135. operation on a float and int are also handled }
  136. if (right.resulttype.def.deftype=floatdef) or (left.resulttype.def.deftype=floatdef) then
  137. begin
  138. inserttypeconv(right,pbestrealtype^);
  139. inserttypeconv(left,pbestrealtype^);
  140. end;
  141. { if one operand is a widechar or a widestring, both operands }
  142. { are converted to widestring. This must be done before constant }
  143. { folding to allow char+widechar etc. }
  144. if is_widestring(right.resulttype.def) or
  145. is_widestring(left.resulttype.def) or
  146. is_widechar(right.resulttype.def) or
  147. is_widechar(left.resulttype.def) then
  148. begin
  149. inserttypeconv(right,cwidestringtype);
  150. inserttypeconv(left,cwidestringtype);
  151. end;
  152. { load easier access variables }
  153. rd:=right.resulttype.def;
  154. ld:=left.resulttype.def;
  155. rt:=right.nodetype;
  156. lt:=left.nodetype;
  157. { both are int constants }
  158. if (((is_constintnode(left) and is_constintnode(right)) or
  159. (is_constboolnode(left) and is_constboolnode(right) and
  160. (nodetype in [ltn,lten,gtn,gten,equaln,unequaln,andn,xorn,orn])))) or
  161. { support pointer arithmetics on constants (JM) }
  162. ((lt = pointerconstn) and is_constintnode(right) and
  163. (nodetype in [addn,subn])) or
  164. ((lt = pointerconstn) and (rt = pointerconstn) and
  165. (nodetype in [ltn,lten,gtn,gten,equaln,unequaln,subn])) then
  166. begin
  167. { when comparing/substracting pointers, make sure they are }
  168. { of the same type (JM) }
  169. if (lt = pointerconstn) and (rt = pointerconstn) then
  170. begin
  171. if not(cs_extsyntax in aktmoduleswitches) and
  172. not(nodetype in [equaln,unequaln]) then
  173. CGMessage(type_e_mismatch)
  174. else
  175. if (nodetype <> subn) and
  176. is_voidpointer(rd) then
  177. inserttypeconv(right,left.resulttype)
  178. else if (nodetype <> subn) and
  179. is_voidpointer(ld) then
  180. inserttypeconv(left,right.resulttype)
  181. else if not(is_equal(ld,rd)) then
  182. CGMessage(type_e_mismatch);
  183. end
  184. else if (lt=ordconstn) and (rt=ordconstn) then
  185. begin
  186. { make left const type the biggest, this type will be used
  187. for orn,andn,xorn }
  188. if rd.size>ld.size then
  189. inserttypeconv(left,right.resulttype);
  190. end;
  191. { load values }
  192. if (lt = ordconstn) then
  193. lv:=tordconstnode(left).value
  194. else
  195. lv:=tpointerconstnode(left).value;
  196. if (rt = ordconstn) then
  197. rv:=tordconstnode(right).value
  198. else
  199. rv:=tpointerconstnode(right).value;
  200. if (lt = pointerconstn) and
  201. (rt <> pointerconstn) then
  202. rv := rv * tpointerdef(left.resulttype.def).pointertype.def.size;
  203. if (rt = pointerconstn) and
  204. (lt <> pointerconstn) then
  205. lv := lv * tpointerdef(right.resulttype.def).pointertype.def.size;
  206. case nodetype of
  207. addn :
  208. if (lt <> pointerconstn) then
  209. t := genintconstnode(lv+rv)
  210. else
  211. t := cpointerconstnode.create(lv+rv,left.resulttype);
  212. subn :
  213. if (lt <> pointerconstn) or (rt = pointerconstn) then
  214. t := genintconstnode(lv-rv)
  215. else
  216. t := cpointerconstnode.create(lv-rv,left.resulttype);
  217. muln :
  218. t:=genintconstnode(lv*rv);
  219. xorn :
  220. t:=cordconstnode.create(lv xor rv,left.resulttype);
  221. orn :
  222. t:=cordconstnode.create(lv or rv,left.resulttype);
  223. andn :
  224. t:=cordconstnode.create(lv and rv,left.resulttype);
  225. ltn :
  226. t:=cordconstnode.create(ord(lv<rv),booltype);
  227. lten :
  228. t:=cordconstnode.create(ord(lv<=rv),booltype);
  229. gtn :
  230. t:=cordconstnode.create(ord(lv>rv),booltype);
  231. gten :
  232. t:=cordconstnode.create(ord(lv>=rv),booltype);
  233. equaln :
  234. t:=cordconstnode.create(ord(lv=rv),booltype);
  235. unequaln :
  236. t:=cordconstnode.create(ord(lv<>rv),booltype);
  237. slashn :
  238. begin
  239. { int/int becomes a real }
  240. rvd:=rv;
  241. lvd:=lv;
  242. if int(rvd)=0 then
  243. begin
  244. Message(parser_e_invalid_float_operation);
  245. t:=crealconstnode.create(0,pbestrealtype^);
  246. end
  247. else
  248. t:=crealconstnode.create(int(lvd)/int(rvd),pbestrealtype^);
  249. end;
  250. else
  251. CGMessage(type_e_mismatch);
  252. end;
  253. result:=t;
  254. exit;
  255. end;
  256. { both real constants ? }
  257. if (lt=realconstn) and (rt=realconstn) then
  258. begin
  259. lvd:=trealconstnode(left).value_real;
  260. rvd:=trealconstnode(right).value_real;
  261. case nodetype of
  262. addn :
  263. t:=crealconstnode.create(lvd+rvd,pbestrealtype^);
  264. subn :
  265. t:=crealconstnode.create(lvd-rvd,pbestrealtype^);
  266. muln :
  267. t:=crealconstnode.create(lvd*rvd,pbestrealtype^);
  268. starstarn,
  269. caretn :
  270. begin
  271. if lvd<0 then
  272. begin
  273. Message(parser_e_invalid_float_operation);
  274. t:=crealconstnode.create(0,pbestrealtype^);
  275. end
  276. else if lvd=0 then
  277. t:=crealconstnode.create(1.0,pbestrealtype^)
  278. else
  279. t:=crealconstnode.create(exp(ln(lvd)*rvd),pbestrealtype^);
  280. end;
  281. slashn :
  282. begin
  283. if rvd=0 then
  284. begin
  285. Message(parser_e_invalid_float_operation);
  286. t:=crealconstnode.create(0,pbestrealtype^);
  287. end
  288. else
  289. t:=crealconstnode.create(lvd/rvd,pbestrealtype^);
  290. end;
  291. ltn :
  292. t:=cordconstnode.create(ord(lvd<rvd),booltype);
  293. lten :
  294. t:=cordconstnode.create(ord(lvd<=rvd),booltype);
  295. gtn :
  296. t:=cordconstnode.create(ord(lvd>rvd),booltype);
  297. gten :
  298. t:=cordconstnode.create(ord(lvd>=rvd),booltype);
  299. equaln :
  300. t:=cordconstnode.create(ord(lvd=rvd),booltype);
  301. unequaln :
  302. t:=cordconstnode.create(ord(lvd<>rvd),booltype);
  303. else
  304. CGMessage(type_e_mismatch);
  305. end;
  306. result:=t;
  307. exit;
  308. end;
  309. { first, we handle widestrings, so we can check later for }
  310. { stringconstn only }
  311. { widechars are converted above to widestrings too }
  312. { this isn't veryy efficient, but I don't think }
  313. { that it does matter that much (FK) }
  314. if (lt=stringconstn) and (rt=stringconstn) and
  315. (tstringconstnode(left).st_type=st_widestring) and
  316. (tstringconstnode(right).st_type=st_widestring) then
  317. begin
  318. initwidestring(ws1);
  319. initwidestring(ws2);
  320. copywidestring(pcompilerwidestring(tstringconstnode(left).value_str),ws1);
  321. copywidestring(pcompilerwidestring(tstringconstnode(right).value_str),ws2);
  322. case nodetype of
  323. addn :
  324. begin
  325. concatwidestrings(ws1,ws2);
  326. t:=cstringconstnode.createwstr(ws1);
  327. end;
  328. ltn :
  329. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<0),booltype);
  330. lten :
  331. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<=0),booltype);
  332. gtn :
  333. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>0),booltype);
  334. gten :
  335. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>=0),booltype);
  336. equaln :
  337. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)=0),booltype);
  338. unequaln :
  339. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<>0),booltype);
  340. end;
  341. donewidestring(ws1);
  342. donewidestring(ws2);
  343. result:=t;
  344. exit;
  345. end;
  346. { concating strings ? }
  347. concatstrings:=false;
  348. s1:=nil;
  349. s2:=nil;
  350. if (lt=ordconstn) and (rt=ordconstn) and
  351. is_char(ld) and is_char(rd) then
  352. begin
  353. s1:=strpnew(char(byte(tordconstnode(left).value)));
  354. s2:=strpnew(char(byte(tordconstnode(right).value)));
  355. l1:=1;
  356. l2:=1;
  357. concatstrings:=true;
  358. end
  359. else
  360. if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  361. begin
  362. s1:=tstringconstnode(left).getpcharcopy;
  363. l1:=tstringconstnode(left).len;
  364. s2:=strpnew(char(byte(tordconstnode(right).value)));
  365. l2:=1;
  366. concatstrings:=true;
  367. end
  368. else
  369. if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  370. begin
  371. s1:=strpnew(char(byte(tordconstnode(left).value)));
  372. l1:=1;
  373. s2:=tstringconstnode(right).getpcharcopy;
  374. l2:=tstringconstnode(right).len;
  375. concatstrings:=true;
  376. end
  377. else if (lt=stringconstn) and (rt=stringconstn) then
  378. begin
  379. s1:=tstringconstnode(left).getpcharcopy;
  380. l1:=tstringconstnode(left).len;
  381. s2:=tstringconstnode(right).getpcharcopy;
  382. l2:=tstringconstnode(right).len;
  383. concatstrings:=true;
  384. end;
  385. if concatstrings then
  386. begin
  387. case nodetype of
  388. addn :
  389. t:=cstringconstnode.createpchar(concatansistrings(s1,s2,l1,l2),l1+l2);
  390. ltn :
  391. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<0),booltype);
  392. lten :
  393. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<=0),booltype);
  394. gtn :
  395. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>0),booltype);
  396. gten :
  397. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>=0),booltype);
  398. equaln :
  399. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)=0),booltype);
  400. unequaln :
  401. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<>0),booltype);
  402. end;
  403. ansistringdispose(s1,l1);
  404. ansistringdispose(s2,l2);
  405. result:=t;
  406. exit;
  407. end;
  408. { set constant evaluation }
  409. if (right.nodetype=setconstn) and
  410. not assigned(tsetconstnode(right).left) and
  411. (left.nodetype=setconstn) and
  412. not assigned(tsetconstnode(left).left) then
  413. begin
  414. { check if size adjusting is needed, only for left
  415. to right as the other way is checked in the typeconv }
  416. if (tsetdef(right.resulttype.def).settype=smallset) and
  417. (tsetdef(left.resulttype.def).settype<>smallset) then
  418. tsetdef(right.resulttype.def).changesettype(normset);
  419. { check base types }
  420. inserttypeconv(left,right.resulttype);
  421. if codegenerror then
  422. begin
  423. { recover by only returning the left part }
  424. result:=left;
  425. left:=nil;
  426. exit;
  427. end;
  428. new(resultset);
  429. case nodetype of
  430. addn :
  431. begin
  432. for i:=0 to 31 do
  433. resultset^[i]:=tsetconstnode(right).value_set^[i] or tsetconstnode(left).value_set^[i];
  434. t:=csetconstnode.create(resultset,left.resulttype);
  435. end;
  436. muln :
  437. begin
  438. for i:=0 to 31 do
  439. resultset^[i]:=tsetconstnode(right).value_set^[i] and tsetconstnode(left).value_set^[i];
  440. t:=csetconstnode.create(resultset,left.resulttype);
  441. end;
  442. subn :
  443. begin
  444. for i:=0 to 31 do
  445. resultset^[i]:=tsetconstnode(left).value_set^[i] and not(tsetconstnode(right).value_set^[i]);
  446. t:=csetconstnode.create(resultset,left.resulttype);
  447. end;
  448. symdifn :
  449. begin
  450. for i:=0 to 31 do
  451. resultset^[i]:=tsetconstnode(left).value_set^[i] xor tsetconstnode(right).value_set^[i];
  452. t:=csetconstnode.create(resultset,left.resulttype);
  453. end;
  454. unequaln :
  455. begin
  456. b:=true;
  457. for i:=0 to 31 do
  458. if tsetconstnode(right).value_set^[i]=tsetconstnode(left).value_set^[i] then
  459. begin
  460. b:=false;
  461. break;
  462. end;
  463. t:=cordconstnode.create(ord(b),booltype);
  464. end;
  465. equaln :
  466. begin
  467. b:=true;
  468. for i:=0 to 31 do
  469. if tsetconstnode(right).value_set^[i]<>tsetconstnode(left).value_set^[i] then
  470. begin
  471. b:=false;
  472. break;
  473. end;
  474. t:=cordconstnode.create(ord(b),booltype);
  475. end;
  476. lten :
  477. begin
  478. b := true;
  479. For i := 0 to 31 Do
  480. If (tsetconstnode(right).value_set^[i] And tsetconstnode(left).value_set^[i]) <>
  481. tsetconstnode(left).value_set^[i] Then
  482. Begin
  483. b := false;
  484. Break
  485. End;
  486. t := cordconstnode.create(ord(b),booltype);
  487. End;
  488. gten :
  489. Begin
  490. b := true;
  491. For i := 0 to 31 Do
  492. If (tsetconstnode(left).value_set^[i] And tsetconstnode(right).value_set^[i]) <>
  493. tsetconstnode(right).value_set^[i] Then
  494. Begin
  495. b := false;
  496. Break
  497. End;
  498. t := cordconstnode.create(ord(b),booltype);
  499. End;
  500. end;
  501. dispose(resultset);
  502. result:=t;
  503. exit;
  504. end;
  505. { but an int/int gives real/real! }
  506. if nodetype=slashn then
  507. begin
  508. CGMessage(type_h_use_div_for_int);
  509. inserttypeconv(right,pbestrealtype^);
  510. inserttypeconv(left,pbestrealtype^);
  511. end
  512. { if both are orddefs then check sub types }
  513. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  514. begin
  515. { 2 booleans? Make them equal to the largest boolean }
  516. if is_boolean(ld) and is_boolean(rd) then
  517. begin
  518. if torddef(left.resulttype.def).size>torddef(right.resulttype.def).size then
  519. begin
  520. inserttypeconv(right,left.resulttype);
  521. ttypeconvnode(right).convtype:=tc_bool_2_int;
  522. include(right.flags,nf_explizit);
  523. end
  524. else if torddef(left.resulttype.def).size<torddef(right.resulttype.def).size then
  525. begin
  526. inserttypeconv(left,right.resulttype);
  527. ttypeconvnode(left).convtype:=tc_bool_2_int;
  528. include(left.flags,nf_explizit);
  529. end;
  530. case nodetype of
  531. xorn,
  532. ltn,
  533. lten,
  534. gtn,
  535. gten,
  536. andn,
  537. orn:
  538. begin
  539. end;
  540. unequaln,
  541. equaln:
  542. begin
  543. if not(cs_full_boolean_eval in aktlocalswitches) then
  544. begin
  545. { Remove any compares with constants }
  546. if (left.nodetype=ordconstn) then
  547. begin
  548. hp:=right;
  549. b:=(tordconstnode(left).value<>0);
  550. ot:=nodetype;
  551. left.free;
  552. left:=nil;
  553. right:=nil;
  554. if (not(b) and (ot=equaln)) or
  555. (b and (ot=unequaln)) then
  556. begin
  557. hp:=cnotnode.create(hp);
  558. end;
  559. result:=hp;
  560. exit;
  561. end;
  562. if (right.nodetype=ordconstn) then
  563. begin
  564. hp:=left;
  565. b:=(tordconstnode(right).value<>0);
  566. ot:=nodetype;
  567. right.free;
  568. right:=nil;
  569. left:=nil;
  570. if (not(b) and (ot=equaln)) or
  571. (b and (ot=unequaln)) then
  572. begin
  573. hp:=cnotnode.create(hp);
  574. end;
  575. result:=hp;
  576. exit;
  577. end;
  578. end;
  579. end;
  580. else
  581. CGMessage(type_e_mismatch);
  582. end;
  583. end
  584. { Both are chars? }
  585. else if is_char(rd) and is_char(ld) then
  586. begin
  587. if nodetype=addn then
  588. begin
  589. resulttype:=cshortstringtype;
  590. if not(is_constcharnode(left) and is_constcharnode(right)) then
  591. begin
  592. inserttypeconv(left,cshortstringtype);
  593. hp := genaddsstringcharoptnode(self);
  594. result := hp;
  595. exit;
  596. end;
  597. end;
  598. end
  599. { is there a signed 64 bit type ? }
  600. else if ((torddef(rd).typ=s64bit) or (torddef(ld).typ=s64bit)) then
  601. begin
  602. if (torddef(ld).typ<>s64bit) then
  603. inserttypeconv(left,cs64bittype);
  604. if (torddef(rd).typ<>s64bit) then
  605. inserttypeconv(right,cs64bittype);
  606. end
  607. { is there a unsigned 64 bit type ? }
  608. else if ((torddef(rd).typ=u64bit) or (torddef(ld).typ=u64bit)) then
  609. begin
  610. if (torddef(ld).typ<>u64bit) then
  611. inserttypeconv(left,cu64bittype);
  612. if (torddef(rd).typ<>u64bit) then
  613. inserttypeconv(right,cu64bittype);
  614. end
  615. { is there a cardinal? }
  616. else if ((torddef(rd).typ=u32bit) or (torddef(ld).typ=u32bit)) then
  617. begin
  618. if is_signed(ld) and
  619. { then rd = u32bit }
  620. { convert positive constants to u32bit }
  621. not(is_constintnode(left) and
  622. (tordconstnode(left).value >= 0)) and
  623. { range/overflow checking on mixed signed/cardinal expressions }
  624. { is only possible if you convert everything to 64bit (JM) }
  625. ((aktlocalswitches * [cs_check_overflow,cs_check_range] <> []) and
  626. (nodetype in [addn,subn,muln])) then
  627. begin
  628. { perform the operation in 64bit }
  629. CGMessage(type_w_mixed_signed_unsigned);
  630. inserttypeconv(left,cs64bittype);
  631. inserttypeconv(right,cs64bittype);
  632. end
  633. else
  634. begin
  635. if is_signed(ld) and
  636. not(is_constintnode(left) and
  637. (tordconstnode(left).value >= 0)) and
  638. (cs_check_range in aktlocalswitches) then
  639. CGMessage(type_w_mixed_signed_unsigned2);
  640. inserttypeconv(left,u32bittype);
  641. if is_signed(rd) and
  642. { then ld = u32bit }
  643. { convert positive constants to u32bit }
  644. not(is_constintnode(right) and
  645. (tordconstnode(right).value >= 0)) and
  646. ((aktlocalswitches * [cs_check_overflow,cs_check_range] <> []) and
  647. (nodetype in [addn,subn,muln])) then
  648. begin
  649. { perform the operation in 64bit }
  650. CGMessage(type_w_mixed_signed_unsigned);
  651. inserttypeconv(left,cs64bittype);
  652. inserttypeconv(right,cs64bittype);
  653. end
  654. else
  655. begin
  656. if is_signed(rd) and
  657. not(is_constintnode(right) and
  658. (tordconstnode(right).value >= 0)) and
  659. (cs_check_range in aktlocalswitches) then
  660. CGMessage(type_w_mixed_signed_unsigned2);
  661. inserttypeconv(right,u32bittype);
  662. end;
  663. end;
  664. end
  665. { generic ord conversion is s32bit }
  666. else
  667. begin
  668. inserttypeconv(right,s32bittype);
  669. inserttypeconv(left,s32bittype);
  670. end;
  671. end
  672. { if both are floatdefs, conversion is already done before constant folding }
  673. else if (ld.deftype=floatdef) then
  674. begin
  675. { already converted }
  676. end
  677. { left side a setdef, must be before string processing,
  678. else array constructor can be seen as array of char (PFV) }
  679. else if (ld.deftype=setdef) then
  680. begin
  681. { trying to add a set element? }
  682. if (nodetype=addn) and (rd.deftype<>setdef) then
  683. begin
  684. if (rt=setelementn) then
  685. begin
  686. if not(is_equal(tsetdef(ld).elementtype.def,rd)) then
  687. CGMessage(type_e_set_element_are_not_comp);
  688. end
  689. else
  690. CGMessage(type_e_mismatch)
  691. end
  692. else
  693. begin
  694. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  695. CGMessage(type_e_set_operation_unknown);
  696. { right def must be a also be set }
  697. if (rd.deftype<>setdef) or not(is_equal(rd,ld)) then
  698. CGMessage(type_e_set_element_are_not_comp);
  699. end;
  700. { ranges require normsets }
  701. if (tsetdef(ld).settype=smallset) and
  702. (rt=setelementn) and
  703. assigned(tsetelementnode(right).right) then
  704. begin
  705. { generate a temporary normset def, it'll be destroyed
  706. when the symtable is unloaded }
  707. htype.setdef(tsetdef.create(tsetdef(ld).elementtype,255));
  708. inserttypeconv(left,htype);
  709. end;
  710. { if the destination is not a smallset then insert a typeconv
  711. which loads a smallset into a normal set }
  712. if (tsetdef(ld).settype<>smallset) and
  713. (tsetdef(rd).settype=smallset) then
  714. begin
  715. if (right.nodetype=setconstn) then
  716. begin
  717. t:=csetconstnode.create(tsetconstnode(right).value_set,left.resulttype);
  718. tsetconstnode(t).left:=tsetconstnode(right).left;
  719. tsetconstnode(right).left := nil;
  720. right.free;
  721. right:=t;
  722. end
  723. else
  724. inserttypeconv(right,left.resulttype);
  725. resulttypepass(right);
  726. end;
  727. end
  728. { compare pchar to char arrays by addresses like BP/Delphi }
  729. else if (is_pchar(ld) and is_chararray(rd)) or
  730. (is_pchar(rd) and is_chararray(ld)) then
  731. begin
  732. if is_chararray(rd) then
  733. inserttypeconv(right,left.resulttype)
  734. else
  735. inserttypeconv(left,right.resulttype);
  736. end
  737. { is one of the operands a string?,
  738. chararrays are also handled as strings (after conversion), also take
  739. care of chararray+chararray and chararray+char }
  740. else if (rd.deftype=stringdef) or (ld.deftype=stringdef) or
  741. ((is_chararray(rd) or is_char(rd)) and
  742. (is_chararray(ld) or is_char(ld))) then
  743. begin
  744. if is_widestring(rd) or is_widestring(ld) then
  745. begin
  746. if not(is_widestring(rd)) then
  747. inserttypeconv(right,cwidestringtype);
  748. if not(is_widestring(ld)) then
  749. inserttypeconv(left,cwidestringtype);
  750. end
  751. else if is_ansistring(rd) or is_ansistring(ld) then
  752. begin
  753. if not(is_ansistring(rd)) then
  754. inserttypeconv(right,cansistringtype);
  755. if not(is_ansistring(ld)) then
  756. inserttypeconv(left,cansistringtype);
  757. end
  758. else if is_longstring(rd) or is_longstring(ld) then
  759. begin
  760. if not(is_longstring(rd)) then
  761. inserttypeconv(right,clongstringtype);
  762. if not(is_longstring(ld)) then
  763. inserttypeconv(left,clongstringtype);
  764. location.loc:=LOC_MEM;
  765. end
  766. else
  767. begin
  768. if not(is_shortstring(ld)) then
  769. inserttypeconv(left,cshortstringtype);
  770. { don't convert char, that can be handled by the optimized node }
  771. if not(is_shortstring(rd) or is_char(rd)) then
  772. inserttypeconv(right,cshortstringtype);
  773. end;
  774. end
  775. { pointer comparision and subtraction }
  776. else if (rd.deftype=pointerdef) and (ld.deftype=pointerdef) then
  777. begin
  778. case nodetype of
  779. equaln,unequaln :
  780. begin
  781. if is_voidpointer(right.resulttype.def) then
  782. inserttypeconv(right,left.resulttype)
  783. else if is_voidpointer(left.resulttype.def) then
  784. inserttypeconv(left,right.resulttype)
  785. else if not(is_equal(ld,rd)) then
  786. CGMessage(type_e_mismatch);
  787. end;
  788. ltn,lten,gtn,gten:
  789. begin
  790. if (cs_extsyntax in aktmoduleswitches) then
  791. begin
  792. if is_voidpointer(right.resulttype.def) then
  793. inserttypeconv(right,left.resulttype)
  794. else if is_voidpointer(left.resulttype.def) then
  795. inserttypeconv(left,right.resulttype)
  796. else if not(is_equal(ld,rd)) then
  797. CGMessage(type_e_mismatch);
  798. end
  799. else
  800. CGMessage(type_e_mismatch);
  801. end;
  802. subn:
  803. begin
  804. if (cs_extsyntax in aktmoduleswitches) then
  805. begin
  806. if is_voidpointer(right.resulttype.def) then
  807. inserttypeconv(right,left.resulttype)
  808. else if is_voidpointer(left.resulttype.def) then
  809. inserttypeconv(left,right.resulttype)
  810. else if not(is_equal(ld,rd)) then
  811. CGMessage(type_e_mismatch);
  812. end
  813. else
  814. CGMessage(type_e_mismatch);
  815. resulttype:=s32bittype;
  816. exit;
  817. end;
  818. addn:
  819. begin
  820. if (cs_extsyntax in aktmoduleswitches) then
  821. begin
  822. if is_voidpointer(right.resulttype.def) then
  823. inserttypeconv(right,left.resulttype)
  824. else if is_voidpointer(left.resulttype.def) then
  825. inserttypeconv(left,right.resulttype)
  826. else if not(is_equal(ld,rd)) then
  827. CGMessage(type_e_mismatch);
  828. end
  829. else
  830. CGMessage(type_e_mismatch);
  831. resulttype:=s32bittype;
  832. exit;
  833. end;
  834. else
  835. CGMessage(type_e_mismatch);
  836. end;
  837. end
  838. { class or interface equation }
  839. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  840. begin
  841. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  842. begin
  843. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  844. inserttypeconv(right,left.resulttype)
  845. else
  846. inserttypeconv(left,right.resulttype);
  847. end
  848. else if is_class_or_interface(rd) then
  849. inserttypeconv(left,right.resulttype)
  850. else
  851. inserttypeconv(right,left.resulttype);
  852. if not(nodetype in [equaln,unequaln]) then
  853. CGMessage(type_e_mismatch);
  854. end
  855. else if (rd.deftype=classrefdef) and (ld.deftype=classrefdef) then
  856. begin
  857. if tobjectdef(tclassrefdef(rd).pointertype.def).is_related(
  858. tobjectdef(tclassrefdef(ld).pointertype.def)) then
  859. inserttypeconv(right,left.resulttype)
  860. else
  861. inserttypeconv(left,right.resulttype);
  862. if not(nodetype in [equaln,unequaln]) then
  863. CGMessage(type_e_mismatch);
  864. end
  865. { allows comperasion with nil pointer }
  866. else if is_class_or_interface(rd) or (rd.deftype=classrefdef) then
  867. begin
  868. inserttypeconv(left,right.resulttype);
  869. if not(nodetype in [equaln,unequaln]) then
  870. CGMessage(type_e_mismatch);
  871. end
  872. else if is_class_or_interface(ld) or (ld.deftype=classrefdef) then
  873. begin
  874. inserttypeconv(right,left.resulttype);
  875. if not(nodetype in [equaln,unequaln]) then
  876. CGMessage(type_e_mismatch);
  877. end
  878. { support procvar=nil,procvar<>nil }
  879. else if ((ld.deftype=procvardef) and (rt=niln)) or
  880. ((rd.deftype=procvardef) and (lt=niln)) then
  881. begin
  882. if not(nodetype in [equaln,unequaln]) then
  883. CGMessage(type_e_mismatch);
  884. end
  885. {$ifdef SUPPORT_MMX}
  886. { mmx support, this must be before the zero based array
  887. check }
  888. else if (cs_mmx in aktlocalswitches) and
  889. is_mmx_able_array(ld) and
  890. is_mmx_able_array(rd) and
  891. is_equal(ld,rd) then
  892. begin
  893. case nodetype of
  894. addn,subn,xorn,orn,andn:
  895. ;
  896. { mul is a little bit restricted }
  897. muln:
  898. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  899. CGMessage(type_e_mismatch);
  900. else
  901. CGMessage(type_e_mismatch);
  902. end;
  903. end
  904. {$endif SUPPORT_MMX}
  905. { this is a little bit dangerous, also the left type }
  906. { pointer to should be checked! This broke the mmx support }
  907. else if (rd.deftype=pointerdef) or is_zero_based_array(rd) then
  908. begin
  909. if is_zero_based_array(rd) then
  910. begin
  911. resulttype.setdef(tpointerdef.create(tarraydef(rd).elementtype));
  912. inserttypeconv(right,resulttype);
  913. end;
  914. inserttypeconv(left,s32bittype);
  915. if nodetype=addn then
  916. begin
  917. if not(cs_extsyntax in aktmoduleswitches) or
  918. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  919. CGMessage(type_e_mismatch);
  920. if (rd.deftype=pointerdef) and
  921. (tpointerdef(rd).pointertype.def.size>1) then
  922. left:=caddnode.create(muln,left,cordconstnode.create(tpointerdef(rd).pointertype.def.size,s32bittype));
  923. end
  924. else
  925. CGMessage(type_e_mismatch);
  926. end
  927. else if (ld.deftype=pointerdef) or is_zero_based_array(ld) then
  928. begin
  929. if is_zero_based_array(ld) then
  930. begin
  931. resulttype.setdef(tpointerdef.create(tarraydef(ld).elementtype));
  932. inserttypeconv(left,resulttype);
  933. end;
  934. inserttypeconv(right,s32bittype);
  935. if nodetype in [addn,subn] then
  936. begin
  937. if not(cs_extsyntax in aktmoduleswitches) or
  938. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  939. CGMessage(type_e_mismatch);
  940. if (ld.deftype=pointerdef) and
  941. (tpointerdef(ld).pointertype.def.size>1) then
  942. right:=caddnode.create(muln,right,cordconstnode.create(tpointerdef(ld).pointertype.def.size,s32bittype));
  943. end
  944. else
  945. CGMessage(type_e_mismatch);
  946. end
  947. else if (rd.deftype=procvardef) and (ld.deftype=procvardef) and is_equal(rd,ld) then
  948. begin
  949. if not (nodetype in [equaln,unequaln]) then
  950. CGMessage(type_e_mismatch);
  951. end
  952. { enums }
  953. else if (ld.deftype=enumdef) and (rd.deftype=enumdef) then
  954. begin
  955. if not(is_equal(ld,rd)) then
  956. inserttypeconv(right,left.resulttype);
  957. if not(nodetype in [equaln,unequaln,ltn,lten,gtn,gten]) then
  958. CGMessage(type_e_mismatch);
  959. end
  960. { generic conversion, this is for error recovery }
  961. else
  962. begin
  963. inserttypeconv(left,s32bittype);
  964. inserttypeconv(right,s32bittype);
  965. end;
  966. { set resulttype if not already done }
  967. if not assigned(resulttype.def) then
  968. begin
  969. case nodetype of
  970. ltn,lten,gtn,gten,equaln,unequaln :
  971. resulttype:=booltype;
  972. slashn :
  973. resulttype:=pbestrealtype^;
  974. addn:
  975. begin
  976. { for strings, return is always a 255 char string }
  977. if is_shortstring(left.resulttype.def) then
  978. resulttype:=cshortstringtype
  979. else
  980. resulttype:=left.resulttype;
  981. end;
  982. else
  983. resulttype:=left.resulttype;
  984. end;
  985. end;
  986. end;
  987. function taddnode.first_addstring: tnode;
  988. var
  989. p: tnode;
  990. begin
  991. { when we get here, we are sure that both the left and the right }
  992. { node are both strings of the same stringtype (JM) }
  993. case nodetype of
  994. addn:
  995. begin
  996. { note: if you implemented an fpc_shortstr_concat similar to the }
  997. { one in i386.inc, you have to override first_addstring like in }
  998. { ti386addnode.first_string and implement the shortstring concat }
  999. { manually! The generic routine is different from the i386 one (JM) }
  1000. { create the call to the concat routine both strings as arguments }
  1001. result := ccallnode.createintern('fpc_'+
  1002. lower(tstringdef(resulttype.def).stringtypname)+'_concat',
  1003. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1004. { we reused the arguments }
  1005. left := nil;
  1006. right := nil;
  1007. firstpass(result);
  1008. end;
  1009. ltn,lten,gtn,gten,equaln,unequaln :
  1010. begin
  1011. { generate better code for s='' and s<>'' }
  1012. if (nodetype in [equaln,unequaln]) and
  1013. (((left.nodetype=stringconstn) and (str_length(left)=0)) or
  1014. ((right.nodetype=stringconstn) and (str_length(right)=0))) then
  1015. begin
  1016. { switch so that the constant is always on the right }
  1017. if left.nodetype = stringconstn then
  1018. begin
  1019. p := left;
  1020. left := right;
  1021. right := p;
  1022. end;
  1023. if is_shortstring(left.resulttype.def) then
  1024. { compare the length with 0 }
  1025. result := caddnode.create(nodetype,
  1026. cinlinenode.create(in_length_x,false,left),
  1027. cordconstnode.create(0,s32bittype))
  1028. else
  1029. begin
  1030. { compare the pointer with nil (for ansistrings etc), }
  1031. { faster than getting the length (JM) }
  1032. result:= caddnode.create(nodetype,
  1033. ctypeconvnode.create(left,voidpointertype),
  1034. cpointerconstnode.create(0,voidpointertype));
  1035. taddnode(result).left.toggleflag(nf_explizit);
  1036. end;
  1037. { left is reused }
  1038. left := nil;
  1039. { right isn't }
  1040. right.free;
  1041. right := nil;
  1042. firstpass(result);
  1043. exit;
  1044. end;
  1045. { no string constant -> call compare routine }
  1046. result := ccallnode.createintern('fpc_'+
  1047. lower(tstringdef(left.resulttype.def).stringtypname)+'_compare',
  1048. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1049. { and compare its result with 0 according to the original operator }
  1050. result := caddnode.create(nodetype,result,
  1051. cordconstnode.create(0,s32bittype));
  1052. left := nil;
  1053. right := nil;
  1054. firstpass(result);
  1055. end;
  1056. end;
  1057. end;
  1058. function taddnode.first_addset: tnode;
  1059. var
  1060. procname: string[31];
  1061. tempn: tnode;
  1062. paras: tcallparanode;
  1063. srsym: ttypesym;
  1064. begin
  1065. { get the sym that represents the fpc_normal_set type }
  1066. if not searchsystype('FPC_NORMAL_SET',srsym) then
  1067. internalerror(200108313);
  1068. case nodetype of
  1069. equaln,unequaln,lten,gten:
  1070. begin
  1071. case nodetype of
  1072. equaln,unequaln:
  1073. procname := 'fpc_set_comp_sets';
  1074. lten,gten:
  1075. begin
  1076. procname := 'fpc_set_contains_sets';
  1077. { (left >= right) = (right <= left) }
  1078. if nodetype = gten then
  1079. begin
  1080. tempn := left;
  1081. left := right;
  1082. right := tempn;
  1083. end;
  1084. end;
  1085. end;
  1086. { convert the arguments (explicitely) to fpc_normal_set's }
  1087. left := ctypeconvnode.create(left,srsym.restype);
  1088. left.toggleflag(nf_explizit);
  1089. right := ctypeconvnode.create(right,srsym.restype);
  1090. right.toggleflag(nf_explizit);
  1091. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1092. ccallparanode.create(left,nil)));
  1093. { left and right are reused as parameters }
  1094. left := nil;
  1095. right := nil;
  1096. { for an unequaln, we have to negate the result of comp_sets }
  1097. if nodetype = unequaln then
  1098. result := cnotnode.create(result);
  1099. end;
  1100. addn:
  1101. begin
  1102. { optimize first loading of a set }
  1103. if (right.nodetype=setelementn) and
  1104. not(assigned(tsetelementnode(right).right)) and
  1105. is_emptyset(left) then
  1106. begin
  1107. { type cast the value to pass as argument to a byte, }
  1108. { since that's what the helper expects }
  1109. tsetelementnode(right).left :=
  1110. ctypeconvnode.create(tsetelementnode(right).left,u8bittype);
  1111. tsetelementnode(right).left.toggleflag(nf_explizit);
  1112. { set the resulttype to the actual one (otherwise it's }
  1113. { "fpc_normal_set") }
  1114. result := ccallnode.createinternres('fpc_set_create_element',
  1115. ccallparanode.create(tsetelementnode(right).left,nil),
  1116. resulttype);
  1117. { reused }
  1118. tsetelementnode(right).left := nil;
  1119. end
  1120. else
  1121. begin
  1122. if right.nodetype=setelementn then
  1123. begin
  1124. { convert the arguments to bytes, since that's what }
  1125. { the helper expects }
  1126. tsetelementnode(right).left :=
  1127. ctypeconvnode.create(tsetelementnode(right).left,
  1128. u8bittype);
  1129. tsetelementnode(right).left.toggleflag(nf_explizit);
  1130. { convert the original set (explicitely) to an }
  1131. { fpc_normal_set so we can pass it to the helper }
  1132. left := ctypeconvnode.create(left,srsym.restype);
  1133. left.toggleflag(nf_explizit);
  1134. { add a range or a single element? }
  1135. if assigned(tsetelementnode(right).right) then
  1136. begin
  1137. tsetelementnode(right).right :=
  1138. ctypeconvnode.create(tsetelementnode(right).right,
  1139. u8bittype);
  1140. tsetelementnode(right).right.toggleflag(nf_explizit);
  1141. { create the call }
  1142. result := ccallnode.createinternres('fpc_set_set_range',
  1143. ccallparanode.create(tsetelementnode(right).right,
  1144. ccallparanode.create(tsetelementnode(right).left,
  1145. ccallparanode.create(left,nil))),resulttype);
  1146. end
  1147. else
  1148. begin
  1149. result := ccallnode.createinternres('fpc_set_set_byte',
  1150. ccallparanode.create(tsetelementnode(right).left,
  1151. ccallparanode.create(left,nil)),resulttype);
  1152. end;
  1153. { remove reused parts from original node }
  1154. tsetelementnode(right).right := nil;
  1155. tsetelementnode(right).left := nil;
  1156. left := nil;
  1157. end
  1158. else
  1159. begin
  1160. { add two sets }
  1161. { convert the sets to fpc_normal_set's }
  1162. left := ctypeconvnode.create(left,srsym.restype);
  1163. left.toggleflag(nf_explizit);
  1164. right := ctypeconvnode.create(right,srsym.restype);
  1165. right.toggleflag(nf_explizit);
  1166. result := ccallnode.createinternres('fpc_set_add_sets',
  1167. ccallparanode.create(right,
  1168. ccallparanode.create(left,nil)),resulttype);
  1169. { remove reused parts from original node }
  1170. left := nil;
  1171. right := nil;
  1172. end;
  1173. end
  1174. end;
  1175. subn,symdifn,muln:
  1176. begin
  1177. { convert the sets to fpc_normal_set's }
  1178. left := ctypeconvnode.create(left,srsym.restype);
  1179. left.toggleflag(nf_explizit);
  1180. right := ctypeconvnode.create(right,srsym.restype);
  1181. right.toggleflag(nf_explizit);
  1182. paras := ccallparanode.create(right,
  1183. ccallparanode.create(left,nil));
  1184. case nodetype of
  1185. subn:
  1186. result := ccallnode.createinternres('fpc_set_sub_sets',
  1187. paras,resulttype);
  1188. symdifn:
  1189. result := ccallnode.createinternres('fpc_set_symdif_sets',
  1190. paras,resulttype);
  1191. muln:
  1192. result := ccallnode.createinternres('fpc_set_mul_sets',
  1193. paras,resulttype);
  1194. end;
  1195. { remove reused parts from original node }
  1196. left := nil;
  1197. right := nil;
  1198. end;
  1199. else
  1200. internalerror(200108311);
  1201. end;
  1202. firstpass(result);
  1203. end;
  1204. function taddnode.first_add64bitint: tnode;
  1205. var
  1206. procname: string[31];
  1207. temp: tnode;
  1208. power: longint;
  1209. begin
  1210. result := nil;
  1211. { create helper calls mul }
  1212. if nodetype <> muln then
  1213. exit;
  1214. { make sure that if there is a constant, that it's on the right }
  1215. if left.nodetype = ordconstn then
  1216. begin
  1217. temp := right;
  1218. right := left;
  1219. left := temp;
  1220. end;
  1221. { can we use a shift instead of a mul? }
  1222. if (right.nodetype = ordconstn) and
  1223. ispowerof2(tordconstnode(right).value,power) then
  1224. begin
  1225. tordconstnode(right).value := power;
  1226. result := cshlshrnode.create(shln,left,right);
  1227. { left and right are reused }
  1228. left := nil;
  1229. right := nil;
  1230. { return firstpassed new node }
  1231. firstpass(result);
  1232. exit;
  1233. end;
  1234. { otherwise, create the parameters for the helper }
  1235. right := ccallparanode.create(
  1236. cordconstnode.create(ord(cs_check_overflow in aktlocalswitches),booltype),
  1237. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1238. left := nil;
  1239. if torddef(resulttype.def).typ = s64bit then
  1240. procname := 'fpc_mul_int64'
  1241. else
  1242. procname := 'fpc_mul_qword';
  1243. result := ccallnode.createintern(procname,right);
  1244. right := nil;
  1245. firstpass(result);
  1246. end;
  1247. function taddnode.pass_1 : tnode;
  1248. var
  1249. hp : tnode;
  1250. lt,rt : tnodetype;
  1251. rd,ld : tdef;
  1252. begin
  1253. result:=nil;
  1254. { first do the two subtrees }
  1255. firstpass(left);
  1256. firstpass(right);
  1257. if codegenerror then
  1258. exit;
  1259. { load easier access variables }
  1260. rd:=right.resulttype.def;
  1261. ld:=left.resulttype.def;
  1262. rt:=right.nodetype;
  1263. lt:=left.nodetype;
  1264. { int/int gives real/real! }
  1265. if nodetype=slashn then
  1266. begin
  1267. location.loc:=LOC_FPU;
  1268. { maybe we need an integer register to save }
  1269. { a reference }
  1270. if ((left.location.loc<>LOC_FPU) or
  1271. (right.location.loc<>LOC_FPU)) and
  1272. (left.registers32=right.registers32) then
  1273. calcregisters(self,1,1,0)
  1274. else
  1275. calcregisters(self,0,1,0);
  1276. { an add node always first loads both the left and the }
  1277. { right in the fpu before doing the calculation. However, }
  1278. { calcregisters(0,2,0) will overestimate the number of }
  1279. { necessary registers (it will make it 3 in case one of }
  1280. { the operands is already in the fpu) (JM) }
  1281. if ((left.location.loc <> LOC_FPU) or
  1282. (right.location.loc <> LOC_FPU)) and
  1283. (registersfpu < 2) then
  1284. inc(registersfpu);
  1285. end
  1286. { if both are orddefs then check sub types }
  1287. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  1288. begin
  1289. { 2 booleans ? }
  1290. if is_boolean(ld) and is_boolean(rd) then
  1291. begin
  1292. if not(cs_full_boolean_eval in aktlocalswitches) and
  1293. (nodetype in [andn,orn]) then
  1294. begin
  1295. location.loc:=LOC_JUMP;
  1296. calcregisters(self,0,0,0);
  1297. end
  1298. else
  1299. begin
  1300. location.loc := LOC_FLAGS;
  1301. if (left.location.loc in [LOC_JUMP,LOC_FLAGS]) and
  1302. (left.location.loc in [LOC_JUMP,LOC_FLAGS]) then
  1303. calcregisters(self,2,0,0)
  1304. else
  1305. calcregisters(self,1,0,0);
  1306. end;
  1307. end
  1308. else
  1309. { Both are chars? only convert to shortstrings for addn }
  1310. if is_char(ld) then
  1311. begin
  1312. if nodetype=addn then
  1313. internalerror(200103291);
  1314. location.loc := LOC_FLAGS;
  1315. calcregisters(self,1,0,0);
  1316. end
  1317. { is there a 64 bit type ? }
  1318. else if (torddef(ld).typ in [s64bit,u64bit]) then
  1319. begin
  1320. result := first_add64bitint;
  1321. if assigned(result) then
  1322. exit;
  1323. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1324. location.loc := LOC_REGISTER
  1325. else
  1326. location.loc := LOC_JUMP;
  1327. calcregisters(self,2,0,0)
  1328. end
  1329. { is there a cardinal? }
  1330. else if (torddef(ld).typ=u32bit) then
  1331. begin
  1332. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1333. location.loc := LOC_REGISTER
  1334. else
  1335. location.loc := LOC_FLAGS;
  1336. calcregisters(self,1,0,0);
  1337. { for unsigned mul we need an extra register }
  1338. if nodetype=muln then
  1339. inc(registers32);
  1340. end
  1341. { generic s32bit conversion }
  1342. else
  1343. begin
  1344. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1345. location.loc := LOC_REGISTER
  1346. else
  1347. location.loc := LOC_FLAGS;
  1348. calcregisters(self,1,0,0);
  1349. end;
  1350. end
  1351. { left side a setdef, must be before string processing,
  1352. else array constructor can be seen as array of char (PFV) }
  1353. else if (ld.deftype=setdef) then
  1354. begin
  1355. if tsetdef(ld).settype=smallset then
  1356. begin
  1357. location.loc:=LOC_REGISTER;
  1358. { are we adding set elements ? }
  1359. if right.nodetype=setelementn then
  1360. calcregisters(self,2,0,0)
  1361. else
  1362. calcregisters(self,1,0,0);
  1363. end
  1364. else
  1365. begin
  1366. result := first_addset;
  1367. if assigned(result) then
  1368. exit;
  1369. location.loc:=LOC_MEM;
  1370. calcregisters(self,0,0,0);
  1371. { here we call SET... }
  1372. procinfo^.flags:=procinfo^.flags or pi_do_call;
  1373. end;
  1374. end
  1375. { compare pchar by addresses like BP/Delphi }
  1376. else if is_pchar(ld) then
  1377. begin
  1378. location.loc:=LOC_REGISTER;
  1379. calcregisters(self,1,0,0);
  1380. end
  1381. { is one of the operands a string }
  1382. else if (ld.deftype=stringdef) then
  1383. begin
  1384. if is_widestring(ld) then
  1385. begin
  1386. { we use reference counted widestrings so no fast exit here }
  1387. procinfo^.no_fast_exit:=true;
  1388. { this is only for add, the comparisaion is handled later }
  1389. location.loc:=LOC_REGISTER;
  1390. end
  1391. else if is_ansistring(ld) then
  1392. begin
  1393. { we use ansistrings so no fast exit here }
  1394. procinfo^.no_fast_exit:=true;
  1395. { this is only for add, the comparisaion is handled later }
  1396. location.loc:=LOC_REGISTER;
  1397. end
  1398. else if is_longstring(ld) then
  1399. begin
  1400. { this is only for add, the comparisaion is handled later }
  1401. location.loc:=LOC_MEM;
  1402. end
  1403. else
  1404. begin
  1405. if canbeaddsstringcharoptnode(self) then
  1406. begin
  1407. hp := genaddsstringcharoptnode(self);
  1408. firstpass(hp);
  1409. pass_1 := hp;
  1410. exit;
  1411. end
  1412. else
  1413. begin
  1414. { Fix right to be shortstring }
  1415. if is_char(right.resulttype.def) then
  1416. begin
  1417. inserttypeconv(right,cshortstringtype);
  1418. firstpass(right);
  1419. end;
  1420. end;
  1421. if canbeaddsstringcsstringoptnode(self) then
  1422. begin
  1423. hp := genaddsstringcsstringoptnode(self);
  1424. firstpass(hp);
  1425. pass_1 := hp;
  1426. exit;
  1427. end;
  1428. end;
  1429. { otherwise, let addstring convert everything }
  1430. result := first_addstring;
  1431. exit;
  1432. end
  1433. { is one a real float ? }
  1434. else if (rd.deftype=floatdef) or (ld.deftype=floatdef) then
  1435. begin
  1436. location.loc:=LOC_FPU;
  1437. calcregisters(self,0,1,0);
  1438. { an add node always first loads both the left and the }
  1439. { right in the fpu before doing the calculation. However, }
  1440. { calcregisters(0,2,0) will overestimate the number of }
  1441. { necessary registers (it will make it 3 in case one of }
  1442. { the operands is already in the fpu) (JM) }
  1443. if ((left.location.loc <> LOC_FPU) or
  1444. (right.location.loc <> LOC_FPU)) and
  1445. (registersfpu < 2) then
  1446. inc(registersfpu);
  1447. end
  1448. { pointer comperation and subtraction }
  1449. else if (ld.deftype=pointerdef) then
  1450. begin
  1451. location.loc:=LOC_REGISTER;
  1452. calcregisters(self,1,0,0);
  1453. end
  1454. else if is_class_or_interface(ld) then
  1455. begin
  1456. location.loc:=LOC_REGISTER;
  1457. calcregisters(self,1,0,0);
  1458. end
  1459. else if (ld.deftype=classrefdef) then
  1460. begin
  1461. location.loc:=LOC_REGISTER;
  1462. calcregisters(self,1,0,0);
  1463. end
  1464. { support procvar=nil,procvar<>nil }
  1465. else if ((ld.deftype=procvardef) and (rt=niln)) or
  1466. ((rd.deftype=procvardef) and (lt=niln)) then
  1467. begin
  1468. location.loc:=LOC_REGISTER;
  1469. calcregisters(self,1,0,0);
  1470. end
  1471. {$ifdef SUPPORT_MMX}
  1472. { mmx support, this must be before the zero based array
  1473. check }
  1474. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  1475. is_mmx_able_array(rd) then
  1476. begin
  1477. location.loc:=LOC_MMXREGISTER;
  1478. calcregisters(self,0,0,1);
  1479. end
  1480. {$endif SUPPORT_MMX}
  1481. else if (rd.deftype=pointerdef) or (ld.deftype=pointerdef) then
  1482. begin
  1483. location.loc:=LOC_REGISTER;
  1484. calcregisters(self,1,0,0);
  1485. end
  1486. else if (rd.deftype=procvardef) and (ld.deftype=procvardef) and is_equal(rd,ld) then
  1487. begin
  1488. location.loc:=LOC_REGISTER;
  1489. calcregisters(self,1,0,0);
  1490. end
  1491. else if (ld.deftype=enumdef) then
  1492. begin
  1493. location.loc := LOC_FLAGS;
  1494. calcregisters(self,1,0,0);
  1495. end
  1496. {$ifdef SUPPORT_MMX}
  1497. else if (cs_mmx in aktlocalswitches) and
  1498. is_mmx_able_array(ld) and
  1499. is_mmx_able_array(rd) then
  1500. begin
  1501. location.loc:=LOC_MMXREGISTER;
  1502. calcregisters(self,0,0,1);
  1503. end
  1504. {$endif SUPPORT_MMX}
  1505. { the general solution is to convert to 32 bit int }
  1506. else
  1507. begin
  1508. location.loc:=LOC_REGISTER;
  1509. calcregisters(self,1,0,0);
  1510. end;
  1511. end;
  1512. begin
  1513. caddnode:=taddnode;
  1514. end.
  1515. {
  1516. $Log$
  1517. Revision 1.42 2001-12-27 15:33:58 jonas
  1518. * fixed fpuregister counting errors ("merged")
  1519. Revision 1.41 2001/10/20 19:28:37 peter
  1520. * interface 2 guid support
  1521. * guid constants support
  1522. Revision 1.40 2001/10/12 13:51:51 jonas
  1523. * fixed internalerror(10) due to previous fpu overflow fixes ("merged")
  1524. * fixed bug in n386add (introduced after compilerproc changes for string
  1525. operations) where calcregisters wasn't called for shortstring addnodes
  1526. * NOTE: from now on, the location of a binary node must now always be set
  1527. before you call calcregisters() for it
  1528. Revision 1.39 2001/09/05 15:22:09 jonas
  1529. * made multiplying, dividing and mod'ing of int64 and qword processor
  1530. independent with compilerprocs (+ small optimizations by using shift/and
  1531. where possible)
  1532. Revision 1.38 2001/09/04 11:38:54 jonas
  1533. + searchsystype() and searchsystype() functions in symtable
  1534. * changed ninl and nadd to use these functions
  1535. * i386 set comparison functions now return their results in al instead
  1536. of in the flags so that they can be sued as compilerprocs
  1537. - removed all processor specific code from n386add.pas that has to do
  1538. with set handling, it's now all done in nadd.pas
  1539. * fixed fpc_set_contains_sets in genset.inc
  1540. * fpc_set_in_byte is now coded inline in n386set.pas and doesn't use a
  1541. helper anymore
  1542. * some small fixes in compproc.inc/set.inc regarding the declaration of
  1543. internal helper types (fpc_small_set and fpc_normal_set)
  1544. Revision 1.37 2001/09/03 13:27:42 jonas
  1545. * compilerproc implementation of set addition/substraction/...
  1546. * changed the declaration of some set helpers somewhat to accomodate the
  1547. above change
  1548. * i386 still uses the old code for comparisons of sets, because its
  1549. helpers return the results in the flags
  1550. * dummy tc_normal_2_small_set type conversion because I need the original
  1551. resulttype of the set add nodes
  1552. NOTE: you have to start a cycle with 1.0.5!
  1553. Revision 1.36 2001/09/02 21:12:06 peter
  1554. * move class of definitions into type section for delphi
  1555. Revision 1.35 2001/08/31 15:42:15 jonas
  1556. * added missing type conversion from small to normal sets
  1557. Revision 1.34 2001/08/30 15:43:14 jonas
  1558. * converted adding/comparing of strings to compileproc. Note that due
  1559. to the way the shortstring helpers for i386 are written, they are
  1560. still handled by the old code (reason: fpc_shortstr_compare returns
  1561. results in the flags instead of in eax and fpc_shortstr_concat
  1562. has wierd parameter conventions). The compilerproc stuff should work
  1563. fine with the generic implementations though.
  1564. * removed some nested comments warnings
  1565. Revision 1.33 2001/08/26 13:36:38 florian
  1566. * some cg reorganisation
  1567. * some PPC updates
  1568. Revision 1.32 2001/08/06 21:40:46 peter
  1569. * funcret moved from tprocinfo to tprocdef
  1570. Revision 1.31 2001/07/08 21:00:14 peter
  1571. * various widestring updates, it works now mostly without charset
  1572. mapping supported
  1573. Revision 1.30 2001/06/04 21:41:26 peter
  1574. * readded generic conversion to s32bit that i removed yesterday. It
  1575. is still used for error recovery, added a small note about that
  1576. Revision 1.29 2001/06/04 18:13:53 peter
  1577. * Support kylix hack of having enum+integer in a enum declaration.
  1578. Revision 1.28 2001/05/27 14:30:55 florian
  1579. + some widestring stuff added
  1580. Revision 1.27 2001/05/19 21:11:50 peter
  1581. * first check for overloaded operator before doing inserting any
  1582. typeconvs
  1583. Revision 1.26 2001/05/19 12:53:52 peter
  1584. * check set types when doing constant set evaluation
  1585. Revision 1.25 2001/04/13 01:22:08 peter
  1586. * symtable change to classes
  1587. * range check generation and errors fixed, make cycle DEBUG=1 works
  1588. * memory leaks fixed
  1589. Revision 1.24 2001/04/04 22:42:39 peter
  1590. * move constant folding into det_resulttype
  1591. Revision 1.23 2001/04/02 21:20:30 peter
  1592. * resulttype rewrite
  1593. Revision 1.22 2001/02/04 11:12:17 jonas
  1594. * fixed web bug 1377 & const pointer arithmtic
  1595. Revision 1.21 2001/01/14 22:13:13 peter
  1596. * constant calculation fixed. The type of the new constant is now
  1597. defined after the calculation is done. This should remove a lot
  1598. of wrong warnings (and errors with -Cr).
  1599. Revision 1.20 2000/12/31 11:14:10 jonas
  1600. + implemented/fixed docompare() mathods for all nodes (not tested)
  1601. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  1602. and constant strings/chars together
  1603. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  1604. when adding
  1605. Revision 1.19 2000/12/16 15:55:32 jonas
  1606. + warning when there is a chance to get a range check error because of
  1607. automatic type conversion to u32bit
  1608. * arithmetic operations with a cardinal and a signed operand are carried
  1609. out in 64bit when range checking is on ("merged" from fixes branch)
  1610. Revision 1.18 2000/11/29 00:30:31 florian
  1611. * unused units removed from uses clause
  1612. * some changes for widestrings
  1613. Revision 1.17 2000/11/20 15:30:42 jonas
  1614. * changed types of values used for constant expression evaluation to
  1615. tconstexprint
  1616. Revision 1.16 2000/11/13 11:30:55 florian
  1617. * some bugs with interfaces and NIL fixed
  1618. Revision 1.15 2000/11/04 14:25:20 florian
  1619. + merged Attila's changes for interfaces, not tested yet
  1620. Revision 1.14 2000/10/31 22:02:47 peter
  1621. * symtable splitted, no real code changes
  1622. Revision 1.13 2000/10/14 10:14:50 peter
  1623. * moehrendorf oct 2000 rewrite
  1624. Revision 1.12 2000/10/01 19:48:23 peter
  1625. * lot of compile updates for cg11
  1626. Revision 1.11 2000/09/30 16:08:45 peter
  1627. * more cg11 updates
  1628. Revision 1.10 2000/09/28 19:49:52 florian
  1629. *** empty log message ***
  1630. Revision 1.9 2000/09/27 21:33:22 florian
  1631. * finally nadd.pas compiles
  1632. Revision 1.8 2000/09/27 20:25:44 florian
  1633. * more stuff fixed
  1634. Revision 1.7 2000/09/27 18:14:31 florian
  1635. * fixed a lot of syntax errors in the n*.pas stuff
  1636. Revision 1.6 2000/09/24 15:06:19 peter
  1637. * use defines.inc
  1638. Revision 1.5 2000/09/22 22:42:52 florian
  1639. * more fixes
  1640. Revision 1.4 2000/09/21 12:22:42 jonas
  1641. * put piece of code between -dnewoptimizations2 since it wasn't
  1642. necessary otherwise
  1643. + support for full boolean evaluation (from tcadd)
  1644. Revision 1.3 2000/09/20 21:50:59 florian
  1645. * updated
  1646. Revision 1.2 2000/08/29 08:24:45 jonas
  1647. * some modifications to -dcardinalmulfix code
  1648. Revision 1.1 2000/08/26 12:24:20 florian
  1649. * initial release
  1650. }