nadd.pas 67 KB

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