nadd.pas 81 KB

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