nadd.pas 80 KB

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