nadd.pas 82 KB

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