nadd.pas 81 KB

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