nadd.pas 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111
  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) or (lt=niln)) and is_chararray(rd)) or
  902. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld)) then
  903. begin
  904. if is_chararray(rd) then
  905. inserttypeconv(right,charpointertype)
  906. else
  907. inserttypeconv(left,charpointertype);
  908. end
  909. { pointer comparision and subtraction }
  910. else if (rd.deftype=pointerdef) and (ld.deftype=pointerdef) then
  911. begin
  912. case nodetype of
  913. equaln,unequaln :
  914. begin
  915. if is_voidpointer(right.resulttype.def) then
  916. inserttypeconv(right,left.resulttype)
  917. else if is_voidpointer(left.resulttype.def) then
  918. inserttypeconv(left,right.resulttype)
  919. else if not(equal_defs(ld,rd)) then
  920. CGMessage2(type_e_incompatible_types,ld.typename,rd.typename);
  921. end;
  922. ltn,lten,gtn,gten:
  923. begin
  924. if (cs_extsyntax in aktmoduleswitches) then
  925. begin
  926. if is_voidpointer(right.resulttype.def) then
  927. inserttypeconv(right,left.resulttype)
  928. else if is_voidpointer(left.resulttype.def) then
  929. inserttypeconv(left,right.resulttype)
  930. else if not(equal_defs(ld,rd)) then
  931. CGMessage2(type_e_incompatible_types,ld.typename,rd.typename);
  932. end
  933. else
  934. CGMessage(type_e_mismatch);
  935. end;
  936. subn:
  937. begin
  938. if (cs_extsyntax in aktmoduleswitches) then
  939. begin
  940. if is_voidpointer(right.resulttype.def) then
  941. inserttypeconv(right,left.resulttype)
  942. else if is_voidpointer(left.resulttype.def) then
  943. inserttypeconv(left,right.resulttype)
  944. else if not(equal_defs(ld,rd)) then
  945. CGMessage2(type_e_incompatible_types,ld.typename,rd.typename);
  946. end
  947. else
  948. CGMessage(type_e_mismatch);
  949. resulttype:=s32bittype;
  950. exit;
  951. end;
  952. addn:
  953. begin
  954. if (cs_extsyntax in aktmoduleswitches) then
  955. begin
  956. if is_voidpointer(right.resulttype.def) then
  957. inserttypeconv(right,left.resulttype)
  958. else if is_voidpointer(left.resulttype.def) then
  959. inserttypeconv(left,right.resulttype)
  960. else if not(equal_defs(ld,rd)) then
  961. CGMessage2(type_e_incompatible_types,ld.typename,rd.typename);
  962. end
  963. else
  964. CGMessage(type_e_mismatch);
  965. resulttype:=s32bittype;
  966. exit;
  967. end;
  968. else
  969. CGMessage(type_e_mismatch);
  970. end;
  971. end
  972. { is one of the operands a string?,
  973. chararrays are also handled as strings (after conversion), also take
  974. care of chararray+chararray and chararray+char.
  975. Note: Must be done after pointerdef+pointerdef has been checked, else
  976. pchar is converted to string }
  977. else if (rd.deftype=stringdef) or (ld.deftype=stringdef) or
  978. ((is_pchar(rd) or is_chararray(rd) or is_char(rd)) and
  979. (is_pchar(ld) or is_chararray(ld) or is_char(ld))) then
  980. begin
  981. if is_widestring(rd) or is_widestring(ld) then
  982. begin
  983. if not(is_widestring(rd)) then
  984. inserttypeconv(right,cwidestringtype);
  985. if not(is_widestring(ld)) then
  986. inserttypeconv(left,cwidestringtype);
  987. end
  988. else if is_ansistring(rd) or is_ansistring(ld) then
  989. begin
  990. if not(is_ansistring(rd)) then
  991. inserttypeconv(right,cansistringtype);
  992. if not(is_ansistring(ld)) then
  993. inserttypeconv(left,cansistringtype);
  994. end
  995. else if is_longstring(rd) or is_longstring(ld) then
  996. begin
  997. if not(is_longstring(rd)) then
  998. inserttypeconv(right,clongstringtype);
  999. if not(is_longstring(ld)) then
  1000. inserttypeconv(left,clongstringtype);
  1001. location.loc:=LOC_CREFERENCE;
  1002. end
  1003. else
  1004. begin
  1005. if not(is_shortstring(ld)) then
  1006. inserttypeconv(left,cshortstringtype);
  1007. { don't convert char, that can be handled by the optimized node }
  1008. if not(is_shortstring(rd) or is_char(rd)) then
  1009. inserttypeconv(right,cshortstringtype);
  1010. end;
  1011. end
  1012. { class or interface equation }
  1013. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  1014. begin
  1015. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  1016. begin
  1017. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1018. inserttypeconv(right,left.resulttype)
  1019. else
  1020. inserttypeconv(left,right.resulttype);
  1021. end
  1022. else if is_class_or_interface(rd) then
  1023. inserttypeconv(left,right.resulttype)
  1024. else
  1025. inserttypeconv(right,left.resulttype);
  1026. if not(nodetype in [equaln,unequaln]) then
  1027. CGMessage(type_e_mismatch);
  1028. end
  1029. else if (rd.deftype=classrefdef) and (ld.deftype=classrefdef) then
  1030. begin
  1031. if tobjectdef(tclassrefdef(rd).pointertype.def).is_related(
  1032. tobjectdef(tclassrefdef(ld).pointertype.def)) then
  1033. inserttypeconv(right,left.resulttype)
  1034. else
  1035. inserttypeconv(left,right.resulttype);
  1036. if not(nodetype in [equaln,unequaln]) then
  1037. CGMessage(type_e_mismatch);
  1038. end
  1039. { allows comperasion with nil pointer }
  1040. else if is_class_or_interface(rd) or (rd.deftype=classrefdef) then
  1041. begin
  1042. inserttypeconv(left,right.resulttype);
  1043. if not(nodetype in [equaln,unequaln]) then
  1044. CGMessage(type_e_mismatch);
  1045. end
  1046. else if is_class_or_interface(ld) or (ld.deftype=classrefdef) then
  1047. begin
  1048. inserttypeconv(right,left.resulttype);
  1049. if not(nodetype in [equaln,unequaln]) then
  1050. CGMessage(type_e_mismatch);
  1051. end
  1052. { support procvar=nil,procvar<>nil }
  1053. else if ((ld.deftype=procvardef) and (rt=niln)) or
  1054. ((rd.deftype=procvardef) and (lt=niln)) then
  1055. begin
  1056. if not(nodetype in [equaln,unequaln]) then
  1057. CGMessage(type_e_mismatch);
  1058. end
  1059. { support dynamicarray=nil,dynamicarray<>nil }
  1060. else if (is_dynamic_array(ld) and (rt=niln)) or
  1061. (is_dynamic_array(rd) and (lt=niln)) then
  1062. begin
  1063. if not(nodetype in [equaln,unequaln]) then
  1064. CGMessage(type_e_mismatch);
  1065. end
  1066. {$ifdef SUPPORT_MMX}
  1067. { mmx support, this must be before the zero based array
  1068. check }
  1069. else if (cs_mmx in aktlocalswitches) and
  1070. is_mmx_able_array(ld) and
  1071. is_mmx_able_array(rd) and
  1072. equal_defs(ld,rd) then
  1073. begin
  1074. case nodetype of
  1075. addn,subn,xorn,orn,andn:
  1076. ;
  1077. { mul is a little bit restricted }
  1078. muln:
  1079. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1080. CGMessage(type_e_mismatch);
  1081. else
  1082. CGMessage(type_e_mismatch);
  1083. end;
  1084. end
  1085. {$endif SUPPORT_MMX}
  1086. { this is a little bit dangerous, also the left type }
  1087. { pointer to should be checked! This broke the mmx support }
  1088. else if (rd.deftype=pointerdef) or is_zero_based_array(rd) then
  1089. begin
  1090. if is_zero_based_array(rd) then
  1091. begin
  1092. resulttype.setdef(tpointerdef.create(tarraydef(rd).elementtype));
  1093. inserttypeconv(right,resulttype);
  1094. end;
  1095. inserttypeconv(left,s32bittype);
  1096. if nodetype=addn then
  1097. begin
  1098. if not(cs_extsyntax in aktmoduleswitches) or
  1099. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1100. CGMessage(type_e_mismatch);
  1101. if (rd.deftype=pointerdef) and
  1102. (tpointerdef(rd).pointertype.def.size>1) then
  1103. left:=caddnode.create(muln,left,
  1104. cordconstnode.create(tpointerdef(rd).pointertype.def.size,s32bittype,true));
  1105. end
  1106. else
  1107. CGMessage(type_e_mismatch);
  1108. end
  1109. else if (ld.deftype=pointerdef) or is_zero_based_array(ld) then
  1110. begin
  1111. if is_zero_based_array(ld) then
  1112. begin
  1113. resulttype.setdef(tpointerdef.create(tarraydef(ld).elementtype));
  1114. inserttypeconv(left,resulttype);
  1115. end;
  1116. inserttypeconv(right,s32bittype);
  1117. if nodetype in [addn,subn] then
  1118. begin
  1119. if not(cs_extsyntax in aktmoduleswitches) or
  1120. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1121. CGMessage(type_e_mismatch);
  1122. if (ld.deftype=pointerdef) and
  1123. (tpointerdef(ld).pointertype.def.size>1) then
  1124. right:=caddnode.create(muln,right,
  1125. cordconstnode.create(tpointerdef(ld).pointertype.def.size,s32bittype,true));
  1126. end
  1127. else
  1128. CGMessage(type_e_mismatch);
  1129. end
  1130. else if (rd.deftype=procvardef) and
  1131. (ld.deftype=procvardef) and
  1132. equal_defs(rd,ld) then
  1133. begin
  1134. if not (nodetype in [equaln,unequaln]) then
  1135. CGMessage(type_e_mismatch);
  1136. end
  1137. { enums }
  1138. else if (ld.deftype=enumdef) and (rd.deftype=enumdef) then
  1139. begin
  1140. if not(equal_defs(ld,rd)) then
  1141. inserttypeconv(right,left.resulttype);
  1142. if not(nodetype in [equaln,unequaln,ltn,lten,gtn,gten]) then
  1143. CGMessage(type_e_mismatch);
  1144. end
  1145. { generic conversion, this is for error recovery }
  1146. else
  1147. begin
  1148. inserttypeconv(left,s32bittype);
  1149. inserttypeconv(right,s32bittype);
  1150. end;
  1151. { set resulttype if not already done }
  1152. if not assigned(resulttype.def) then
  1153. begin
  1154. case nodetype of
  1155. ltn,lten,gtn,gten,equaln,unequaln :
  1156. resulttype:=booltype;
  1157. slashn :
  1158. resulttype:=resultrealtype;
  1159. addn:
  1160. begin
  1161. { for strings, return is always a 255 char string }
  1162. if is_shortstring(left.resulttype.def) then
  1163. resulttype:=cshortstringtype
  1164. else
  1165. resulttype:=left.resulttype;
  1166. end;
  1167. else
  1168. resulttype:=left.resulttype;
  1169. end;
  1170. end;
  1171. { when the result is currency we need some extra code for
  1172. multiplication and division. this should not be done when
  1173. the muln or slashn node is created internally }
  1174. if not(nf_explizit in flags) and
  1175. is_currency(resulttype.def) then
  1176. begin
  1177. case nodetype of
  1178. slashn :
  1179. begin
  1180. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,resultrealtype));
  1181. include(hp.flags,nf_explizit);
  1182. result:=hp;
  1183. end;
  1184. muln :
  1185. begin
  1186. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,resultrealtype));
  1187. include(hp.flags,nf_explizit);
  1188. result:=hp
  1189. end;
  1190. end;
  1191. end;
  1192. end;
  1193. function taddnode.first_addstring: tnode;
  1194. var
  1195. p: tnode;
  1196. begin
  1197. { when we get here, we are sure that both the left and the right }
  1198. { node are both strings of the same stringtype (JM) }
  1199. case nodetype of
  1200. addn:
  1201. begin
  1202. { note: if you implemented an fpc_shortstr_concat similar to the }
  1203. { one in i386.inc, you have to override first_addstring like in }
  1204. { ti386addnode.first_string and implement the shortstring concat }
  1205. { manually! The generic routine is different from the i386 one (JM) }
  1206. { create the call to the concat routine both strings as arguments }
  1207. result := ccallnode.createintern('fpc_'+
  1208. tstringdef(resulttype.def).stringtypname+'_concat',
  1209. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1210. { we reused the arguments }
  1211. left := nil;
  1212. right := nil;
  1213. firstpass(result);
  1214. end;
  1215. ltn,lten,gtn,gten,equaln,unequaln :
  1216. begin
  1217. { generate better code for s='' and s<>'' }
  1218. if (nodetype in [equaln,unequaln]) and
  1219. (((left.nodetype=stringconstn) and (str_length(left)=0)) or
  1220. ((right.nodetype=stringconstn) and (str_length(right)=0))) then
  1221. begin
  1222. { switch so that the constant is always on the right }
  1223. if left.nodetype = stringconstn then
  1224. begin
  1225. p := left;
  1226. left := right;
  1227. right := p;
  1228. end;
  1229. if is_shortstring(left.resulttype.def) then
  1230. { compare the length with 0 }
  1231. result := caddnode.create(nodetype,
  1232. cinlinenode.create(in_length_x,false,left),
  1233. cordconstnode.create(0,s32bittype,false))
  1234. else
  1235. begin
  1236. { compare the pointer with nil (for ansistrings etc), }
  1237. { faster than getting the length (JM) }
  1238. result:= caddnode.create(nodetype,
  1239. ctypeconvnode.create(left,voidpointertype),
  1240. cpointerconstnode.create(0,voidpointertype));
  1241. taddnode(result).left.toggleflag(nf_explizit);
  1242. end;
  1243. { left is reused }
  1244. left := nil;
  1245. { right isn't }
  1246. right.free;
  1247. right := nil;
  1248. firstpass(result);
  1249. exit;
  1250. end;
  1251. { no string constant -> call compare routine }
  1252. result := ccallnode.createintern('fpc_'+
  1253. tstringdef(left.resulttype.def).stringtypname+'_compare',
  1254. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1255. { and compare its result with 0 according to the original operator }
  1256. result := caddnode.create(nodetype,result,
  1257. cordconstnode.create(0,s32bittype,false));
  1258. left := nil;
  1259. right := nil;
  1260. firstpass(result);
  1261. end;
  1262. end;
  1263. end;
  1264. function taddnode.first_addset: tnode;
  1265. var
  1266. procname: string[31];
  1267. tempn: tnode;
  1268. paras: tcallparanode;
  1269. srsym: ttypesym;
  1270. begin
  1271. { get the sym that represents the fpc_normal_set type }
  1272. if not searchsystype('FPC_NORMAL_SET',srsym) then
  1273. internalerror(200108313);
  1274. case nodetype of
  1275. equaln,unequaln,lten,gten:
  1276. begin
  1277. case nodetype of
  1278. equaln,unequaln:
  1279. procname := 'fpc_set_comp_sets';
  1280. lten,gten:
  1281. begin
  1282. procname := 'fpc_set_contains_sets';
  1283. { (left >= right) = (right <= left) }
  1284. if nodetype = gten then
  1285. begin
  1286. tempn := left;
  1287. left := right;
  1288. right := tempn;
  1289. end;
  1290. end;
  1291. end;
  1292. { convert the arguments (explicitely) to fpc_normal_set's }
  1293. left := ctypeconvnode.create_explicit(left,srsym.restype);
  1294. right := ctypeconvnode.create_explicit(right,srsym.restype);
  1295. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1296. ccallparanode.create(left,nil)));
  1297. { left and right are reused as parameters }
  1298. left := nil;
  1299. right := nil;
  1300. { for an unequaln, we have to negate the result of comp_sets }
  1301. if nodetype = unequaln then
  1302. result := cnotnode.create(result);
  1303. end;
  1304. addn:
  1305. begin
  1306. { optimize first loading of a set }
  1307. if (right.nodetype=setelementn) and
  1308. not(assigned(tsetelementnode(right).right)) and
  1309. is_emptyset(left) then
  1310. begin
  1311. { type cast the value to pass as argument to a byte, }
  1312. { since that's what the helper expects }
  1313. tsetelementnode(right).left :=
  1314. ctypeconvnode.create(tsetelementnode(right).left,u8bittype);
  1315. tsetelementnode(right).left.toggleflag(nf_explizit);
  1316. { set the resulttype to the actual one (otherwise it's }
  1317. { "fpc_normal_set") }
  1318. result := ccallnode.createinternres('fpc_set_create_element',
  1319. ccallparanode.create(tsetelementnode(right).left,nil),
  1320. resulttype);
  1321. { reused }
  1322. tsetelementnode(right).left := nil;
  1323. end
  1324. else
  1325. begin
  1326. if right.nodetype=setelementn then
  1327. begin
  1328. { convert the arguments to bytes, since that's what }
  1329. { the helper expects }
  1330. tsetelementnode(right).left :=
  1331. ctypeconvnode.create(tsetelementnode(right).left,
  1332. u8bittype);
  1333. tsetelementnode(right).left.toggleflag(nf_explizit);
  1334. { convert the original set (explicitely) to an }
  1335. { fpc_normal_set so we can pass it to the helper }
  1336. left := ctypeconvnode.create(left,srsym.restype);
  1337. left.toggleflag(nf_explizit);
  1338. { add a range or a single element? }
  1339. if assigned(tsetelementnode(right).right) then
  1340. begin
  1341. tsetelementnode(right).right :=
  1342. ctypeconvnode.create(tsetelementnode(right).right,
  1343. u8bittype);
  1344. tsetelementnode(right).right.toggleflag(nf_explizit);
  1345. { create the call }
  1346. result := ccallnode.createinternres('fpc_set_set_range',
  1347. ccallparanode.create(tsetelementnode(right).right,
  1348. ccallparanode.create(tsetelementnode(right).left,
  1349. ccallparanode.create(left,nil))),resulttype);
  1350. end
  1351. else
  1352. begin
  1353. result := ccallnode.createinternres('fpc_set_set_byte',
  1354. ccallparanode.create(tsetelementnode(right).left,
  1355. ccallparanode.create(left,nil)),resulttype);
  1356. end;
  1357. { remove reused parts from original node }
  1358. tsetelementnode(right).right := nil;
  1359. tsetelementnode(right).left := nil;
  1360. left := nil;
  1361. end
  1362. else
  1363. begin
  1364. { add two sets }
  1365. { convert the sets to fpc_normal_set's }
  1366. left := ctypeconvnode.create(left,srsym.restype);
  1367. left.toggleflag(nf_explizit);
  1368. right := ctypeconvnode.create(right,srsym.restype);
  1369. right.toggleflag(nf_explizit);
  1370. result := ccallnode.createinternres('fpc_set_add_sets',
  1371. ccallparanode.create(right,
  1372. ccallparanode.create(left,nil)),resulttype);
  1373. { remove reused parts from original node }
  1374. left := nil;
  1375. right := nil;
  1376. end;
  1377. end
  1378. end;
  1379. subn,symdifn,muln:
  1380. begin
  1381. { convert the sets to fpc_normal_set's }
  1382. left := ctypeconvnode.create(left,srsym.restype);
  1383. left.toggleflag(nf_explizit);
  1384. right := ctypeconvnode.create(right,srsym.restype);
  1385. right.toggleflag(nf_explizit);
  1386. paras := ccallparanode.create(right,
  1387. ccallparanode.create(left,nil));
  1388. case nodetype of
  1389. subn:
  1390. result := ccallnode.createinternres('fpc_set_sub_sets',
  1391. paras,resulttype);
  1392. symdifn:
  1393. result := ccallnode.createinternres('fpc_set_symdif_sets',
  1394. paras,resulttype);
  1395. muln:
  1396. result := ccallnode.createinternres('fpc_set_mul_sets',
  1397. paras,resulttype);
  1398. end;
  1399. { remove reused parts from original node }
  1400. left := nil;
  1401. right := nil;
  1402. end;
  1403. else
  1404. internalerror(200108311);
  1405. end;
  1406. firstpass(result);
  1407. end;
  1408. function taddnode.first_add64bitint: tnode;
  1409. var
  1410. procname: string[31];
  1411. temp: tnode;
  1412. power: longint;
  1413. begin
  1414. result := nil;
  1415. { create helper calls mul }
  1416. if nodetype <> muln then
  1417. exit;
  1418. { make sure that if there is a constant, that it's on the right }
  1419. if left.nodetype = ordconstn then
  1420. begin
  1421. temp := right;
  1422. right := left;
  1423. left := temp;
  1424. end;
  1425. { can we use a shift instead of a mul? }
  1426. if (right.nodetype = ordconstn) and
  1427. ispowerof2(tordconstnode(right).value,power) then
  1428. begin
  1429. tordconstnode(right).value := power;
  1430. result := cshlshrnode.create(shln,left,right);
  1431. { left and right are reused }
  1432. left := nil;
  1433. right := nil;
  1434. { return firstpassed new node }
  1435. firstpass(result);
  1436. exit;
  1437. end;
  1438. { otherwise, create the parameters for the helper }
  1439. right := ccallparanode.create(
  1440. cordconstnode.create(ord(cs_check_overflow in aktlocalswitches),booltype,true),
  1441. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1442. left := nil;
  1443. if torddef(resulttype.def).typ = s64bit then
  1444. procname := 'fpc_mul_int64'
  1445. else
  1446. procname := 'fpc_mul_qword';
  1447. result := ccallnode.createintern(procname,right);
  1448. right := nil;
  1449. firstpass(result);
  1450. end;
  1451. {$ifdef cpufpemu}
  1452. function taddnode.first_addfloat: tnode;
  1453. var
  1454. procname: string[31];
  1455. temp: tnode;
  1456. power: longint;
  1457. { do we need to reverse the result ? }
  1458. notnode : boolean;
  1459. begin
  1460. result := nil;
  1461. notnode := false;
  1462. { In non-emulation mode, real opcodes are
  1463. emitted for floating point values.
  1464. }
  1465. if not (cs_fp_emulation in aktmoduleswitches) then
  1466. exit;
  1467. case nodetype of
  1468. addn : procname := 'float32_add';
  1469. muln : procname := 'float32_mul';
  1470. subn : procname := 'float32_sub';
  1471. slashn : procname := 'float32_div';
  1472. ltn : procname := 'float32_lt';
  1473. lten: procname := 'float32_le';
  1474. gtn:
  1475. begin
  1476. procname := 'float32_le';
  1477. notnode := true;
  1478. end;
  1479. gten:
  1480. begin
  1481. procname := 'float32_lt';
  1482. notnode := true;
  1483. end;
  1484. equaln: procname := 'float32_eq';
  1485. unequaln :
  1486. begin
  1487. procname := 'float32_eq';
  1488. notnode := true;
  1489. end;
  1490. else
  1491. CGMessage(type_e_mismatch);
  1492. end;
  1493. { otherwise, create the parameters for the helper }
  1494. right := ccallparanode.create(right,ccallparanode.create(left,nil));
  1495. left := nil;
  1496. { do we need to reverse the result }
  1497. if notnode then
  1498. result := cnotnode.create(ccallnode.createintern(procname,right))
  1499. else
  1500. result := ccallnode.createintern(procname,right);
  1501. right := nil;
  1502. firstpass(result);
  1503. end;
  1504. {$endif cpufpemu}
  1505. function taddnode.pass_1 : tnode;
  1506. var
  1507. hp : tnode;
  1508. lt,rt : tnodetype;
  1509. rd,ld : tdef;
  1510. begin
  1511. result:=nil;
  1512. { first do the two subtrees }
  1513. firstpass(left);
  1514. firstpass(right);
  1515. if codegenerror then
  1516. exit;
  1517. { load easier access variables }
  1518. rd:=right.resulttype.def;
  1519. ld:=left.resulttype.def;
  1520. rt:=right.nodetype;
  1521. lt:=left.nodetype;
  1522. { int/int gives real/real! }
  1523. if nodetype=slashn then
  1524. begin
  1525. {$ifdef cpufpemu}
  1526. result := first_addfloat;
  1527. if assigned(result) then
  1528. exit;
  1529. {$endif cpufpemu}
  1530. location.loc:=LOC_FPUREGISTER;
  1531. { maybe we need an integer register to save }
  1532. { a reference }
  1533. if ((left.location.loc<>LOC_FPUREGISTER) or
  1534. (right.location.loc<>LOC_FPUREGISTER)) and
  1535. (left.registers32=right.registers32) then
  1536. calcregisters(self,1,1,0)
  1537. else
  1538. calcregisters(self,0,1,0);
  1539. { an add node always first loads both the left and the }
  1540. { right in the fpu before doing the calculation. However, }
  1541. { calcregisters(0,2,0) will overestimate the number of }
  1542. { necessary registers (it will make it 3 in case one of }
  1543. { the operands is already in the fpu) (JM) }
  1544. if ((left.location.loc <> LOC_FPUREGISTER) or
  1545. (right.location.loc <> LOC_FPUREGISTER)) and
  1546. (registersfpu < 2) then
  1547. inc(registersfpu);
  1548. end
  1549. { if both are orddefs then check sub types }
  1550. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  1551. begin
  1552. { 2 booleans ? }
  1553. if is_boolean(ld) and is_boolean(rd) then
  1554. begin
  1555. if not(cs_full_boolean_eval in aktlocalswitches) and
  1556. (nodetype in [andn,orn]) then
  1557. begin
  1558. location.loc:=LOC_JUMP;
  1559. calcregisters(self,0,0,0);
  1560. end
  1561. else
  1562. begin
  1563. location.loc := LOC_FLAGS;
  1564. if (left.location.loc in [LOC_JUMP,LOC_FLAGS]) and
  1565. (left.location.loc in [LOC_JUMP,LOC_FLAGS]) then
  1566. calcregisters(self,2,0,0)
  1567. else
  1568. calcregisters(self,1,0,0);
  1569. end;
  1570. end
  1571. else
  1572. { Both are chars? only convert to shortstrings for addn }
  1573. if is_char(ld) then
  1574. begin
  1575. if nodetype=addn then
  1576. internalerror(200103291);
  1577. location.loc := LOC_FLAGS;
  1578. calcregisters(self,1,0,0);
  1579. end
  1580. { is there a 64 bit type ? }
  1581. else if (torddef(ld).typ in [s64bit,u64bit]) then
  1582. begin
  1583. result := first_add64bitint;
  1584. if assigned(result) then
  1585. exit;
  1586. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1587. location.loc := LOC_REGISTER
  1588. else
  1589. location.loc := LOC_JUMP;
  1590. calcregisters(self,2,0,0)
  1591. end
  1592. { is there a cardinal? }
  1593. else if (torddef(ld).typ=u32bit) then
  1594. begin
  1595. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1596. location.loc := LOC_REGISTER
  1597. else
  1598. location.loc := LOC_FLAGS;
  1599. calcregisters(self,1,0,0);
  1600. { for unsigned mul we need an extra register }
  1601. if nodetype=muln then
  1602. inc(registers32);
  1603. end
  1604. { generic s32bit conversion }
  1605. else
  1606. begin
  1607. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  1608. location.loc := LOC_REGISTER
  1609. else
  1610. location.loc := LOC_FLAGS;
  1611. calcregisters(self,1,0,0);
  1612. end;
  1613. end
  1614. { left side a setdef, must be before string processing,
  1615. else array constructor can be seen as array of char (PFV) }
  1616. else if (ld.deftype=setdef) then
  1617. begin
  1618. if tsetdef(ld).settype=smallset then
  1619. begin
  1620. location.loc:=LOC_REGISTER;
  1621. { are we adding set elements ? }
  1622. if right.nodetype=setelementn then
  1623. calcregisters(self,2,0,0)
  1624. else
  1625. calcregisters(self,1,0,0);
  1626. end
  1627. else
  1628. begin
  1629. result := first_addset;
  1630. if assigned(result) then
  1631. exit;
  1632. location.loc:=LOC_CREFERENCE;
  1633. calcregisters(self,0,0,0);
  1634. { here we call SET... }
  1635. if assigned(procinfo) then
  1636. procinfo.flags:=procinfo.flags or pi_do_call;
  1637. end;
  1638. end
  1639. { compare pchar by addresses like BP/Delphi }
  1640. else if is_pchar(ld) then
  1641. begin
  1642. location.loc:=LOC_REGISTER;
  1643. calcregisters(self,1,0,0);
  1644. end
  1645. { is one of the operands a string }
  1646. else if (ld.deftype=stringdef) then
  1647. begin
  1648. if is_widestring(ld) then
  1649. begin
  1650. { we use reference counted widestrings so no fast exit here }
  1651. if assigned(procinfo) then
  1652. procinfo.no_fast_exit:=true;
  1653. { this is only for add, the comparisaion is handled later }
  1654. location.loc:=LOC_REGISTER;
  1655. end
  1656. else if is_ansistring(ld) then
  1657. begin
  1658. { we use ansistrings so no fast exit here }
  1659. if assigned(procinfo) then
  1660. procinfo.no_fast_exit:=true;
  1661. { this is only for add, the comparisaion is handled later }
  1662. location.loc:=LOC_REGISTER;
  1663. end
  1664. else if is_longstring(ld) then
  1665. begin
  1666. { this is only for add, the comparisaion is handled later }
  1667. location.loc:=LOC_CREFERENCE;
  1668. end
  1669. else
  1670. begin
  1671. if canbeaddsstringcharoptnode(self) then
  1672. begin
  1673. hp := genaddsstringcharoptnode(self);
  1674. firstpass(hp);
  1675. pass_1 := hp;
  1676. exit;
  1677. end
  1678. else
  1679. begin
  1680. { Fix right to be shortstring }
  1681. if is_char(right.resulttype.def) then
  1682. begin
  1683. inserttypeconv(right,cshortstringtype);
  1684. firstpass(right);
  1685. end;
  1686. end;
  1687. if canbeaddsstringcsstringoptnode(self) then
  1688. begin
  1689. hp := genaddsstringcsstringoptnode(self);
  1690. firstpass(hp);
  1691. pass_1 := hp;
  1692. exit;
  1693. end;
  1694. end;
  1695. { otherwise, let addstring convert everything }
  1696. result := first_addstring;
  1697. exit;
  1698. end
  1699. { is one a real float ? }
  1700. else if (rd.deftype=floatdef) or (ld.deftype=floatdef) then
  1701. begin
  1702. {$ifdef cpufpemu}
  1703. result := first_addfloat;
  1704. if assigned(result) then
  1705. exit;
  1706. {$endif cpufpemu}
  1707. location.loc:=LOC_FPUREGISTER;
  1708. calcregisters(self,0,1,0);
  1709. { an add node always first loads both the left and the }
  1710. { right in the fpu before doing the calculation. However, }
  1711. { calcregisters(0,2,0) will overestimate the number of }
  1712. { necessary registers (it will make it 3 in case one of }
  1713. { the operands is already in the fpu) (JM) }
  1714. if ((left.location.loc <> LOC_FPUREGISTER) or
  1715. (right.location.loc <> LOC_FPUREGISTER)) and
  1716. (registersfpu < 2) then
  1717. inc(registersfpu);
  1718. end
  1719. { pointer comperation and subtraction }
  1720. else if (ld.deftype=pointerdef) then
  1721. begin
  1722. location.loc:=LOC_REGISTER;
  1723. calcregisters(self,1,0,0);
  1724. end
  1725. else if is_class_or_interface(ld) then
  1726. begin
  1727. location.loc:=LOC_REGISTER;
  1728. calcregisters(self,1,0,0);
  1729. end
  1730. else if (ld.deftype=classrefdef) then
  1731. begin
  1732. location.loc:=LOC_REGISTER;
  1733. calcregisters(self,1,0,0);
  1734. end
  1735. { support procvar=nil,procvar<>nil }
  1736. else if ((ld.deftype=procvardef) and (rt=niln)) or
  1737. ((rd.deftype=procvardef) and (lt=niln)) then
  1738. begin
  1739. location.loc:=LOC_REGISTER;
  1740. calcregisters(self,1,0,0);
  1741. end
  1742. {$ifdef SUPPORT_MMX}
  1743. { mmx support, this must be before the zero based array
  1744. check }
  1745. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  1746. is_mmx_able_array(rd) then
  1747. begin
  1748. location.loc:=LOC_MMXREGISTER;
  1749. calcregisters(self,0,0,1);
  1750. end
  1751. {$endif SUPPORT_MMX}
  1752. else if (rd.deftype=pointerdef) or (ld.deftype=pointerdef) then
  1753. begin
  1754. location.loc:=LOC_REGISTER;
  1755. calcregisters(self,1,0,0);
  1756. end
  1757. else if (rd.deftype=procvardef) and
  1758. (ld.deftype=procvardef) and
  1759. equal_defs(rd,ld) then
  1760. begin
  1761. location.loc:=LOC_REGISTER;
  1762. calcregisters(self,1,0,0);
  1763. end
  1764. else if (ld.deftype=enumdef) then
  1765. begin
  1766. location.loc := LOC_FLAGS;
  1767. calcregisters(self,1,0,0);
  1768. end
  1769. {$ifdef SUPPORT_MMX}
  1770. else if (cs_mmx in aktlocalswitches) and
  1771. is_mmx_able_array(ld) and
  1772. is_mmx_able_array(rd) then
  1773. begin
  1774. location.loc:=LOC_MMXREGISTER;
  1775. calcregisters(self,0,0,1);
  1776. end
  1777. {$endif SUPPORT_MMX}
  1778. { the general solution is to convert to 32 bit int }
  1779. else
  1780. begin
  1781. location.loc:=LOC_REGISTER;
  1782. calcregisters(self,1,0,0);
  1783. end;
  1784. end;
  1785. {$ifdef state_tracking}
  1786. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  1787. var factval:Tnode;
  1788. begin
  1789. track_state_pass:=false;
  1790. if left.track_state_pass(exec_known) then
  1791. begin
  1792. track_state_pass:=true;
  1793. left.resulttype.def:=nil;
  1794. do_resulttypepass(left);
  1795. end;
  1796. factval:=aktstate.find_fact(left);
  1797. if factval<>nil then
  1798. begin
  1799. track_state_pass:=true;
  1800. left.destroy;
  1801. left:=factval.getcopy;
  1802. end;
  1803. if right.track_state_pass(exec_known) then
  1804. begin
  1805. track_state_pass:=true;
  1806. right.resulttype.def:=nil;
  1807. do_resulttypepass(right);
  1808. end;
  1809. factval:=aktstate.find_fact(right);
  1810. if factval<>nil then
  1811. begin
  1812. track_state_pass:=true;
  1813. right.destroy;
  1814. right:=factval.getcopy;
  1815. end;
  1816. end;
  1817. {$endif}
  1818. begin
  1819. caddnode:=taddnode;
  1820. end.
  1821. {
  1822. $Log$
  1823. Revision 1.79 2003-01-02 22:19:54 peter
  1824. * support pchar-char operations converting to string first
  1825. * support chararray-nil
  1826. Revision 1.78 2002/12/11 22:41:03 peter
  1827. * stop processing assignment node when the binaryoverload generates
  1828. a codegenerror
  1829. Revision 1.77 2002/12/06 16:56:57 peter
  1830. * only compile cs_fp_emulation support when cpufpuemu is defined
  1831. * define cpufpuemu for m68k only
  1832. Revision 1.76 2002/11/30 21:32:24 carl
  1833. + Add loading of softfpu in emulation mode
  1834. + Correct routine call for softfpu
  1835. * Extended type must also be defined even with softfpu
  1836. Revision 1.75 2002/11/27 13:11:38 peter
  1837. * more currency fixes, taddcurr runs now successfull
  1838. Revision 1.74 2002/11/27 11:28:40 peter
  1839. * when both flaottypes are the same then handle the addnode using
  1840. that floattype instead of bestrealtype
  1841. Revision 1.73 2002/11/25 18:43:32 carl
  1842. - removed the invalid if <> checking (Delphi is strange on this)
  1843. + implemented abstract warning on instance creation of class with
  1844. abstract methods.
  1845. * some error message cleanups
  1846. Revision 1.72 2002/11/25 17:43:17 peter
  1847. * splitted defbase in defutil,symutil,defcmp
  1848. * merged isconvertable and is_equal into compare_defs(_ext)
  1849. * made operator search faster by walking the list only once
  1850. Revision 1.71 2002/11/23 22:50:06 carl
  1851. * some small speed optimizations
  1852. + added several new warnings/hints
  1853. Revision 1.70 2002/11/16 14:20:22 peter
  1854. * fix tbs0417
  1855. Revision 1.69 2002/11/15 01:58:50 peter
  1856. * merged changes from 1.0.7 up to 04-11
  1857. - -V option for generating bug report tracing
  1858. - more tracing for option parsing
  1859. - errors for cdecl and high()
  1860. - win32 import stabs
  1861. - win32 records<=8 are returned in eax:edx (turned off by default)
  1862. - heaptrc update
  1863. - more info for temp management in .s file with EXTDEBUG
  1864. Revision 1.68 2002/10/08 16:50:43 jonas
  1865. * fixed web bug 2136
  1866. Revision 1.67 2002/10/05 00:47:03 peter
  1867. * support dynamicarray<>nil
  1868. Revision 1.66 2002/10/04 21:19:28 jonas
  1869. * fixed web bug 2139: checking for division by zero fixed
  1870. Revision 1.65 2002/09/07 15:25:02 peter
  1871. * old logs removed and tabs fixed
  1872. Revision 1.64 2002/09/07 12:16:05 carl
  1873. * second part bug report 1996 fix, testrange in cordconstnode
  1874. only called if option is set (also make parsing a tiny faster)
  1875. Revision 1.63 2002/09/04 19:32:56 jonas
  1876. * changed some ctypeconvnode/toggleflag(nf_explizit) combo's to
  1877. ctypeconvnode.create_explicit() statements
  1878. Revision 1.62 2002/08/17 09:23:34 florian
  1879. * first part of procinfo rewrite
  1880. Revision 1.61 2002/08/15 15:15:55 carl
  1881. * jmpbuf size allocation for exceptions is now cpu specific (as it should)
  1882. * more generic nodes for maths
  1883. * several fixes for better m68k support
  1884. Revision 1.60 2002/08/12 15:08:39 carl
  1885. + stab register indexes for powerpc (moved from gdb to cpubase)
  1886. + tprocessor enumeration moved to cpuinfo
  1887. + linker in target_info is now a class
  1888. * many many updates for m68k (will soon start to compile)
  1889. - removed some ifdef or correct them for correct cpu
  1890. Revision 1.59 2002/08/02 07:44:30 jonas
  1891. * made assigned() handling generic
  1892. * add nodes now can also evaluate constant expressions at compile time
  1893. that contain nil nodes
  1894. Revision 1.58 2002/07/26 11:17:52 jonas
  1895. * the optimization of converting a multiplication with a power of two to
  1896. a shl is moved from n386add/secondpass to nadd/resulttypepass
  1897. Revision 1.57 2002/07/23 13:08:16 jonas
  1898. * fixed constant set evaluation of new set handling for non-commutative
  1899. operators
  1900. Revision 1.56 2002/07/23 12:34:29 daniel
  1901. * Readded old set code. To use it define 'oldset'. Activated by default
  1902. for ppc.
  1903. Revision 1.55 2002/07/22 11:48:04 daniel
  1904. * Sets are now internally sets.
  1905. Revision 1.54 2002/07/20 11:57:53 florian
  1906. * types.pas renamed to defbase.pas because D6 contains a types
  1907. unit so this would conflicts if D6 programms are compiled
  1908. + Willamette/SSE2 instructions to assembler added
  1909. Revision 1.53 2002/07/19 11:41:34 daniel
  1910. * State tracker work
  1911. * The whilen and repeatn are now completely unified into whilerepeatn. This
  1912. allows the state tracker to change while nodes automatically into
  1913. repeat nodes.
  1914. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  1915. 'not(a>b)' is optimized into 'a<=b'.
  1916. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  1917. by removing the notn and later switchting the true and falselabels. The
  1918. same is done with 'repeat until not a'.
  1919. Revision 1.52 2002/07/14 18:00:43 daniel
  1920. + Added the beginning of a state tracker. This will track the values of
  1921. variables through procedures and optimize things away.
  1922. Revision 1.51 2002/05/18 13:34:08 peter
  1923. * readded missing revisions
  1924. Revision 1.50 2002/05/16 19:46:37 carl
  1925. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1926. + try to fix temp allocation (still in ifdef)
  1927. + generic constructor calls
  1928. + start of tassembler / tmodulebase class cleanup
  1929. Revision 1.48 2002/05/13 19:54:36 peter
  1930. * removed n386ld and n386util units
  1931. * maybe_save/maybe_restore added instead of the old maybe_push
  1932. Revision 1.47 2002/05/12 16:53:06 peter
  1933. * moved entry and exitcode to ncgutil and cgobj
  1934. * foreach gets extra argument for passing local data to the
  1935. iterator function
  1936. * -CR checks also class typecasts at runtime by changing them
  1937. into as
  1938. * fixed compiler to cycle with the -CR option
  1939. * fixed stabs with elf writer, finally the global variables can
  1940. be watched
  1941. * removed a lot of routines from cga unit and replaced them by
  1942. calls to cgobj
  1943. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1944. u32bit then the other is typecasted also to u32bit without giving
  1945. a rangecheck warning/error.
  1946. * fixed pascal calling method with reversing also the high tree in
  1947. the parast, detected by tcalcst3 test
  1948. Revision 1.46 2002/04/23 19:16:34 peter
  1949. * add pinline unit that inserts compiler supported functions using
  1950. one or more statements
  1951. * moved finalize and setlength from ninl to pinline
  1952. Revision 1.45 2002/04/04 19:05:56 peter
  1953. * removed unused units
  1954. * use tlocation.size in cg.a_*loc*() routines
  1955. Revision 1.44 2002/04/02 17:11:28 peter
  1956. * tlocation,treference update
  1957. * LOC_CONSTANT added for better constant handling
  1958. * secondadd splitted in multiple routines
  1959. * location_force_reg added for loading a location to a register
  1960. of a specified size
  1961. * secondassignment parses now first the right and then the left node
  1962. (this is compatible with Kylix). This saves a lot of push/pop especially
  1963. with string operations
  1964. * adapted some routines to use the new cg methods
  1965. }