nadd.pas 89 KB

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