nadd.pas 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Type checking and register allocation for add nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nadd;
  18. {$i fpcdefs.inc}
  19. { define addstringopt}
  20. interface
  21. uses
  22. node,symtype;
  23. type
  24. taddnode = class(tbinopnode)
  25. resultrealdef : tdef;
  26. constructor create(tt : tnodetype;l,r : tnode);override;
  27. function pass_1 : tnode;override;
  28. function pass_typecheck:tnode;override;
  29. function simplify : tnode;override;
  30. {$ifdef state_tracking}
  31. function track_state_pass(exec_known:boolean):boolean;override;
  32. {$endif}
  33. protected
  34. { override the following if you want to implement }
  35. { parts explicitely in the code generator (JM) }
  36. function first_addstring: tnode; virtual;
  37. function first_addset: tnode; virtual;
  38. { only implements "muln" nodes, the rest always has to be done in }
  39. { the code generator for performance reasons (JM) }
  40. function first_add64bitint: tnode; virtual;
  41. { override and return false if you can handle 32x32->64 }
  42. { bit multiplies directly in your code generator. If }
  43. { this function is overridden to return false, you can }
  44. { get multiplies with left/right both s32bit or u32bit, }
  45. { and resultdef of the muln s64bit or u64bit }
  46. function use_generic_mul32to64: boolean; virtual;
  47. { This routine calls internal runtime library helpers
  48. for all floating point arithmetic in the case
  49. where the emulation switches is on. Otherwise
  50. returns nil, and everything must be done in
  51. the code generation phase.
  52. }
  53. function first_addfloat : tnode; virtual;
  54. private
  55. { checks whether a muln can be calculated as a 32bit }
  56. { * 32bit -> 64 bit }
  57. function try_make_mul32to64: boolean;
  58. end;
  59. taddnodeclass = class of taddnode;
  60. var
  61. { caddnode is used to create nodes of the add type }
  62. { the virtual constructor allows to assign }
  63. { another class type to caddnode => processor }
  64. { specific node types can be created }
  65. caddnode : taddnodeclass;
  66. implementation
  67. uses
  68. {$IFNDEF USE_FAKE_SYSUTILS}
  69. sysutils,
  70. {$ELSE}
  71. fksysutl,
  72. {$ENDIF}
  73. globtype,systems,
  74. cutils,verbose,globals,widestr,
  75. symconst,symdef,symsym,symtable,defutil,defcmp,
  76. cgbase,
  77. htypechk,pass_1,
  78. nld,nbas,nmat,ncnv,ncon,nset,nopt,ncal,ninl,nmem,nutils,
  79. {$ifdef state_tracking}
  80. nstate,
  81. {$endif}
  82. cpuinfo,procinfo;
  83. {*****************************************************************************
  84. TADDNODE
  85. *****************************************************************************}
  86. {$maxfpuregisters 0}
  87. function getbestreal(t1,t2 : tdef) : tdef;
  88. const
  89. floatweight : array[tfloattype] of byte =
  90. (2,3,4,0,1,5);
  91. begin
  92. if t1.typ=floatdef then
  93. begin
  94. result:=t1;
  95. if t2.typ=floatdef then
  96. begin
  97. { when a comp or currency is used, use always the
  98. best float type to calculate the result }
  99. if (tfloatdef(t2).floattype in [s64comp,s64currency]) or
  100. (tfloatdef(t2).floattype in [s64comp,s64currency]) then
  101. result:=pbestrealtype^
  102. else
  103. if floatweight[tfloatdef(t2).floattype]>floatweight[tfloatdef(t1).floattype] then
  104. result:=t2;
  105. end;
  106. end
  107. else if t2.typ=floatdef then
  108. result:=t2
  109. else internalerror(200508061);
  110. end;
  111. constructor taddnode.create(tt : tnodetype;l,r : tnode);
  112. begin
  113. inherited create(tt,l,r);
  114. end;
  115. function taddnode.simplify : tnode;
  116. var
  117. t : tnode;
  118. lt,rt : tnodetype;
  119. rd,ld : tdef;
  120. rv,lv : tconstexprint;
  121. rvd,lvd : bestreal;
  122. ws1,ws2 : pcompilerwidestring;
  123. concatstrings : boolean;
  124. c1,c2 : array[0..1] of char;
  125. s1,s2 : pchar;
  126. l1,l2 : longint;
  127. resultset : Tconstset;
  128. b : boolean;
  129. begin
  130. result:=nil;
  131. { load easier access variables }
  132. rd:=right.resultdef;
  133. ld:=left.resultdef;
  134. rt:=right.nodetype;
  135. lt:=left.nodetype;
  136. if (nodetype = slashn) and
  137. (((rt = ordconstn) and
  138. (tordconstnode(right).value = 0)) or
  139. ((rt = realconstn) and
  140. (trealconstnode(right).value_real = 0.0))) then
  141. begin
  142. if (cs_check_range in current_settings.localswitches) or
  143. (cs_check_overflow in current_settings.localswitches) then
  144. begin
  145. result:=crealconstnode.create(1,pbestrealtype^);
  146. Message(parser_e_division_by_zero);
  147. exit;
  148. end;
  149. end;
  150. { both are int constants }
  151. if (
  152. (
  153. is_constintnode(left) and
  154. is_constintnode(right)
  155. ) or
  156. (
  157. is_constboolnode(left) and
  158. is_constboolnode(right) and
  159. (nodetype in [slashn,ltn,lten,gtn,gten,equaln,unequaln,andn,xorn,orn])
  160. ) or
  161. (
  162. is_constenumnode(left) and
  163. is_constenumnode(right) and
  164. allowenumop(nodetype))
  165. ) or
  166. (
  167. (lt = pointerconstn) and
  168. is_constintnode(right) and
  169. (nodetype in [addn,subn])
  170. ) or
  171. (
  172. (lt in [pointerconstn,niln]) and
  173. (rt in [pointerconstn,niln]) and
  174. (nodetype in [ltn,lten,gtn,gten,equaln,unequaln,subn])
  175. ) then
  176. begin
  177. t:=nil;
  178. { when comparing/substracting pointers, make sure they are }
  179. { of the same type (JM) }
  180. if (lt = pointerconstn) and (rt = pointerconstn) then
  181. begin
  182. if not(cs_extsyntax in current_settings.moduleswitches) and
  183. not(nodetype in [equaln,unequaln]) then
  184. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename)
  185. else
  186. if (nodetype <> subn) and
  187. is_voidpointer(rd) then
  188. inserttypeconv(right,left.resultdef)
  189. else if (nodetype <> subn) and
  190. is_voidpointer(ld) then
  191. inserttypeconv(left,right.resultdef)
  192. else if not(equal_defs(ld,rd)) then
  193. IncompatibleTypes(ld,rd);
  194. end
  195. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  196. begin
  197. if not(equal_defs(ld,rd)) then
  198. inserttypeconv(right,left.resultdef);
  199. end;
  200. { load values }
  201. case lt of
  202. ordconstn:
  203. lv:=tordconstnode(left).value;
  204. pointerconstn:
  205. lv:=tpointerconstnode(left).value;
  206. niln:
  207. lv:=0;
  208. else
  209. internalerror(2002080202);
  210. end;
  211. case rt of
  212. ordconstn:
  213. rv:=tordconstnode(right).value;
  214. pointerconstn:
  215. rv:=tpointerconstnode(right).value;
  216. niln:
  217. rv:=0;
  218. else
  219. internalerror(2002080203);
  220. end;
  221. if (lt = pointerconstn) and
  222. (rt <> pointerconstn) then
  223. rv := rv * tpointerdef(left.resultdef).pointeddef.size;
  224. if (rt = pointerconstn) and
  225. (lt <> pointerconstn) then
  226. lv := lv * tpointerdef(right.resultdef).pointeddef.size;
  227. case nodetype of
  228. addn :
  229. begin
  230. {$ifopt Q-}
  231. {$define OVERFLOW_OFF}
  232. {$Q+}
  233. {$endif}
  234. try
  235. if (lt=pointerconstn) then
  236. t := cpointerconstnode.create(lv+rv,left.resultdef)
  237. else
  238. if is_integer(ld) then
  239. t := genintconstnode(lv+rv)
  240. else
  241. t := cordconstnode.create(lv+rv,left.resultdef,(ld.typ<>enumdef));
  242. except
  243. on E:EIntOverflow do
  244. begin
  245. Message(parser_e_arithmetic_operation_overflow);
  246. { Recover }
  247. t:=genintconstnode(0)
  248. end;
  249. end;
  250. {$ifdef OVERFLOW_OFF}
  251. {$Q-}
  252. {$undef OVERFLOW_OFF}
  253. {$endif}
  254. end;
  255. subn :
  256. begin
  257. {$ifopt Q-}
  258. {$define OVERFLOW_OFF}
  259. {$Q+}
  260. {$endif}
  261. try
  262. if (lt=pointerconstn) then
  263. begin
  264. { pointer-pointer results in an integer }
  265. if (rt=pointerconstn) then
  266. t := genintconstnode((lv-rv) div tpointerdef(ld).pointeddef.size)
  267. else
  268. t := cpointerconstnode.create(lv-rv,left.resultdef);
  269. end
  270. else
  271. begin
  272. if is_integer(ld) then
  273. t:=genintconstnode(lv-rv)
  274. else
  275. t:=cordconstnode.create(lv-rv,left.resultdef,(ld.typ<>enumdef));
  276. end;
  277. except
  278. on E:EIntOverflow do
  279. begin
  280. Message(parser_e_arithmetic_operation_overflow);
  281. { Recover }
  282. t:=genintconstnode(0)
  283. end;
  284. end;
  285. {$ifdef OVERFLOW_OFF}
  286. {$Q-}
  287. {$undef OVERFLOW_OFF}
  288. {$endif}
  289. end;
  290. muln :
  291. begin
  292. {$ifopt Q-}
  293. {$define OVERFLOW_OFF}
  294. {$Q+}
  295. {$endif}
  296. try
  297. if (torddef(ld).ordtype <> u64bit) or
  298. (torddef(rd).ordtype <> u64bit) then
  299. t:=genintconstnode(lv*rv)
  300. else
  301. t:=genintconstnode(int64(qword(lv)*qword(rv)));
  302. except
  303. on E:EIntOverflow do
  304. begin
  305. Message(parser_e_arithmetic_operation_overflow);
  306. { Recover }
  307. t:=genintconstnode(0)
  308. end;
  309. end;
  310. {$ifdef OVERFLOW_OFF}
  311. {$Q-}
  312. {$undef OVERFLOW_OFF}
  313. {$endif}
  314. end;
  315. xorn :
  316. if is_integer(ld) then
  317. t:=genintconstnode(lv xor rv)
  318. else
  319. t:=cordconstnode.create(lv xor rv,left.resultdef,true);
  320. orn :
  321. if is_integer(ld) then
  322. t:=genintconstnode(lv or rv)
  323. else
  324. t:=cordconstnode.create(lv or rv,left.resultdef,true);
  325. andn :
  326. if is_integer(ld) then
  327. t:=genintconstnode(lv and rv)
  328. else
  329. t:=cordconstnode.create(lv and rv,left.resultdef,true);
  330. ltn :
  331. t:=cordconstnode.create(ord(lv<rv),booltype,true);
  332. lten :
  333. t:=cordconstnode.create(ord(lv<=rv),booltype,true);
  334. gtn :
  335. t:=cordconstnode.create(ord(lv>rv),booltype,true);
  336. gten :
  337. t:=cordconstnode.create(ord(lv>=rv),booltype,true);
  338. equaln :
  339. t:=cordconstnode.create(ord(lv=rv),booltype,true);
  340. unequaln :
  341. t:=cordconstnode.create(ord(lv<>rv),booltype,true);
  342. slashn :
  343. begin
  344. { int/int becomes a real }
  345. rvd:=rv;
  346. lvd:=lv;
  347. t:=crealconstnode.create(lvd/rvd,resultrealdef);
  348. end;
  349. else
  350. begin
  351. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  352. t:=cnothingnode.create;
  353. end;
  354. end;
  355. result:=t;
  356. exit;
  357. end
  358. {Match against the ranges, i.e.:
  359. var a:1..10;
  360. begin
  361. if a>0 then
  362. ... always evaluates to true. (DM)}
  363. else if is_constintnode(left) and (right.resultdef.typ=orddef)
  364. {workaround for u64bit bugs} and (Torddef(right.resultdef).ordtype<>u64bit) then
  365. begin
  366. t:=nil;
  367. lv:=Tordconstnode(left).value;
  368. with Torddef(right.resultdef) do
  369. case nodetype of
  370. ltn:
  371. if lv<low then
  372. t:=Cordconstnode.create(1,booltype,true)
  373. else if lv>=high then
  374. t:=Cordconstnode.create(0,booltype,true);
  375. lten:
  376. if lv<=low then
  377. t:=Cordconstnode.create(1,booltype,true)
  378. else if lv>high then
  379. t:=Cordconstnode.create(0,booltype,true);
  380. gtn:
  381. if lv<=low then
  382. t:=Cordconstnode.create(0,booltype,true)
  383. else if lv>high then
  384. t:=Cordconstnode.create(1,booltype,true);
  385. gten :
  386. if lv<low then
  387. t:=Cordconstnode.create(0,booltype,true)
  388. else if lv>=high then
  389. t:=Cordconstnode.create(1,booltype,true);
  390. equaln:
  391. if (lv<low) or (lv>high) then
  392. t:=Cordconstnode.create(0,booltype,true);
  393. unequaln:
  394. if (lv<low) or (lv>high) then
  395. t:=Cordconstnode.create(1,booltype,true);
  396. end;
  397. if t<>nil then
  398. begin
  399. result:=t;
  400. exit;
  401. end
  402. end
  403. else if (left.resultdef.typ=orddef) and is_constintnode(right)
  404. {workaround for u64bit bugs} and (Torddef(left.resultdef).ordtype<>u64bit) then
  405. begin
  406. t:=nil;
  407. rv:=Tordconstnode(right).value;
  408. with Torddef(left.resultdef) do
  409. case nodetype of
  410. ltn:
  411. if high<rv then
  412. t:=Cordconstnode.create(1,booltype,true)
  413. else if low>=rv then
  414. t:=Cordconstnode.create(0,booltype,true);
  415. lten:
  416. if high<=rv then
  417. t:=Cordconstnode.create(1,booltype,true)
  418. else if low>rv then
  419. t:=Cordconstnode.create(0,booltype,true);
  420. gtn:
  421. if high<=rv then
  422. t:=Cordconstnode.create(0,booltype,true)
  423. else if low>rv then
  424. t:=Cordconstnode.create(1,booltype,true);
  425. gten:
  426. if high<rv then
  427. t:=Cordconstnode.create(0,booltype,true)
  428. else if low>=rv then
  429. t:=Cordconstnode.create(1,booltype,true);
  430. equaln:
  431. if (rv<low) or (rv>high) then
  432. t:=Cordconstnode.create(0,booltype,true);
  433. unequaln:
  434. if (rv<low) or (rv>high) then
  435. t:=Cordconstnode.create(1,booltype,true);
  436. end;
  437. if t<>nil then
  438. begin
  439. result:=t;
  440. exit;
  441. end
  442. end;
  443. { Add,Sub,Mul with constant 0 or 1? }
  444. if is_constintnode(right) and is_integer(left.resultdef) then
  445. begin
  446. if tordconstnode(right).value = 0 then
  447. begin
  448. case nodetype of
  449. addn,subn:
  450. result := left.getcopy;
  451. muln:
  452. result:=cordconstnode.create(0,left.resultdef,true);
  453. end;
  454. end
  455. else if tordconstnode(right).value = 1 then
  456. begin
  457. case nodetype of
  458. muln:
  459. result := left.getcopy;
  460. end;
  461. end;
  462. if assigned(result) then
  463. exit;
  464. end;
  465. if is_constintnode(left) and is_integer(right.resultdef) then
  466. begin
  467. if tordconstnode(left).value = 0 then
  468. begin
  469. case nodetype of
  470. addn:
  471. result := right.getcopy;
  472. subn:
  473. result := cunaryminusnode.create(right.getcopy);
  474. muln:
  475. result:=cordconstnode.create(0,right.resultdef,true);
  476. end;
  477. end
  478. else if tordconstnode(left).value = 1 then
  479. begin
  480. case nodetype of
  481. muln:
  482. result := right.getcopy;
  483. end;
  484. end;
  485. if assigned(result) then
  486. exit;
  487. end;
  488. { both real constants ? }
  489. if (lt=realconstn) and (rt=realconstn) then
  490. begin
  491. lvd:=trealconstnode(left).value_real;
  492. rvd:=trealconstnode(right).value_real;
  493. case nodetype of
  494. addn :
  495. t:=crealconstnode.create(lvd+rvd,resultrealdef);
  496. subn :
  497. t:=crealconstnode.create(lvd-rvd,resultrealdef);
  498. muln :
  499. t:=crealconstnode.create(lvd*rvd,resultrealdef);
  500. starstarn:
  501. begin
  502. if lvd<0 then
  503. begin
  504. Message(parser_e_invalid_float_operation);
  505. t:=crealconstnode.create(0,resultrealdef);
  506. end
  507. else if lvd=0 then
  508. t:=crealconstnode.create(1.0,resultrealdef)
  509. else
  510. t:=crealconstnode.create(exp(ln(lvd)*rvd),resultrealdef);
  511. end;
  512. slashn :
  513. t:=crealconstnode.create(lvd/rvd,resultrealdef);
  514. ltn :
  515. t:=cordconstnode.create(ord(lvd<rvd),booltype,true);
  516. lten :
  517. t:=cordconstnode.create(ord(lvd<=rvd),booltype,true);
  518. gtn :
  519. t:=cordconstnode.create(ord(lvd>rvd),booltype,true);
  520. gten :
  521. t:=cordconstnode.create(ord(lvd>=rvd),booltype,true);
  522. equaln :
  523. t:=cordconstnode.create(ord(lvd=rvd),booltype,true);
  524. unequaln :
  525. t:=cordconstnode.create(ord(lvd<>rvd),booltype,true);
  526. else
  527. begin
  528. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  529. t:=cnothingnode.create;
  530. end;
  531. end;
  532. result:=t;
  533. exit;
  534. end;
  535. { first, we handle widestrings, so we can check later for }
  536. { stringconstn only }
  537. { widechars are converted above to widestrings too }
  538. { this isn't veryy efficient, but I don't think }
  539. { that it does matter that much (FK) }
  540. if (lt=stringconstn) and (rt=stringconstn) and
  541. (tstringconstnode(left).cst_type=cst_widestring) and
  542. (tstringconstnode(right).cst_type=cst_widestring) then
  543. begin
  544. initwidestring(ws1);
  545. initwidestring(ws2);
  546. copywidestring(pcompilerwidestring(tstringconstnode(left).value_str),ws1);
  547. copywidestring(pcompilerwidestring(tstringconstnode(right).value_str),ws2);
  548. case nodetype of
  549. addn :
  550. begin
  551. concatwidestrings(ws1,ws2);
  552. t:=cstringconstnode.createwstr(ws1);
  553. end;
  554. ltn :
  555. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<0),booltype,true);
  556. lten :
  557. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<=0),booltype,true);
  558. gtn :
  559. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>0),booltype,true);
  560. gten :
  561. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)>=0),booltype,true);
  562. equaln :
  563. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)=0),booltype,true);
  564. unequaln :
  565. t:=cordconstnode.create(byte(comparewidestrings(ws1,ws2)<>0),booltype,true);
  566. else
  567. begin
  568. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  569. t:=cnothingnode.create;
  570. end;
  571. end;
  572. donewidestring(ws1);
  573. donewidestring(ws2);
  574. result:=t;
  575. exit;
  576. end;
  577. { concating strings ? }
  578. concatstrings:=false;
  579. if (lt=ordconstn) and (rt=ordconstn) and
  580. is_char(ld) and is_char(rd) then
  581. begin
  582. c1[0]:=char(byte(tordconstnode(left).value));
  583. c1[1]:=#0;
  584. l1:=1;
  585. c2[0]:=char(byte(tordconstnode(right).value));
  586. c2[1]:=#0;
  587. l2:=1;
  588. s1:=@c1[0];
  589. s2:=@c2[0];
  590. concatstrings:=true;
  591. end
  592. else if (lt=stringconstn) and (rt=ordconstn) and is_char(rd) then
  593. begin
  594. s1:=tstringconstnode(left).value_str;
  595. l1:=tstringconstnode(left).len;
  596. c2[0]:=char(byte(tordconstnode(right).value));
  597. c2[1]:=#0;
  598. s2:=@c2[0];
  599. l2:=1;
  600. concatstrings:=true;
  601. end
  602. else if (lt=ordconstn) and (rt=stringconstn) and is_char(ld) then
  603. begin
  604. c1[0]:=char(byte(tordconstnode(left).value));
  605. c1[1]:=#0;
  606. l1:=1;
  607. s1:=@c1[0];
  608. s2:=tstringconstnode(right).value_str;
  609. l2:=tstringconstnode(right).len;
  610. concatstrings:=true;
  611. end
  612. else if (lt=stringconstn) and (rt=stringconstn) then
  613. begin
  614. s1:=tstringconstnode(left).value_str;
  615. l1:=tstringconstnode(left).len;
  616. s2:=tstringconstnode(right).value_str;
  617. l2:=tstringconstnode(right).len;
  618. concatstrings:=true;
  619. end;
  620. if concatstrings then
  621. begin
  622. case nodetype of
  623. addn :
  624. t:=cstringconstnode.createpchar(concatansistrings(s1,s2,l1,l2),l1+l2);
  625. ltn :
  626. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<0),booltype,true);
  627. lten :
  628. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<=0),booltype,true);
  629. gtn :
  630. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>0),booltype,true);
  631. gten :
  632. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)>=0),booltype,true);
  633. equaln :
  634. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)=0),booltype,true);
  635. unequaln :
  636. t:=cordconstnode.create(byte(compareansistrings(s1,s2,l1,l2)<>0),booltype,true);
  637. else
  638. begin
  639. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  640. t:=cnothingnode.create;
  641. end;
  642. end;
  643. result:=t;
  644. exit;
  645. end;
  646. { set constant evaluation }
  647. if (right.nodetype=setconstn) and
  648. not assigned(tsetconstnode(right).left) and
  649. (left.nodetype=setconstn) and
  650. not assigned(tsetconstnode(left).left) then
  651. begin
  652. { check if size adjusting is needed, only for left
  653. to right as the other way is checked in the typeconv }
  654. if (tsetdef(right.resultdef).settype=smallset) and
  655. (tsetdef(left.resultdef).settype<>smallset) then
  656. right.resultdef:=tsetdef.create(tsetdef(right.resultdef).elementdef,255);
  657. { check base types }
  658. inserttypeconv(left,right.resultdef);
  659. if codegenerror then
  660. begin
  661. { recover by only returning the left part }
  662. result:=left;
  663. left:=nil;
  664. exit;
  665. end;
  666. case nodetype of
  667. addn :
  668. begin
  669. resultset:=tsetconstnode(right).value_set^ + tsetconstnode(left).value_set^;
  670. t:=csetconstnode.create(@resultset,left.resultdef);
  671. end;
  672. muln :
  673. begin
  674. resultset:=tsetconstnode(right).value_set^ * tsetconstnode(left).value_set^;
  675. t:=csetconstnode.create(@resultset,left.resultdef);
  676. end;
  677. subn :
  678. begin
  679. resultset:=tsetconstnode(left).value_set^ - tsetconstnode(right).value_set^;
  680. t:=csetconstnode.create(@resultset,left.resultdef);
  681. end;
  682. symdifn :
  683. begin
  684. resultset:=tsetconstnode(right).value_set^ >< tsetconstnode(left).value_set^;
  685. t:=csetconstnode.create(@resultset,left.resultdef);
  686. end;
  687. unequaln :
  688. begin
  689. b:=tsetconstnode(right).value_set^ <> tsetconstnode(left).value_set^;
  690. t:=cordconstnode.create(byte(b),booltype,true);
  691. end;
  692. equaln :
  693. begin
  694. b:=tsetconstnode(right).value_set^ = tsetconstnode(left).value_set^;
  695. t:=cordconstnode.create(byte(b),booltype,true);
  696. end;
  697. lten :
  698. begin
  699. b:=tsetconstnode(left).value_set^ <= tsetconstnode(right).value_set^;
  700. t:=cordconstnode.create(byte(b),booltype,true);
  701. end;
  702. gten :
  703. begin
  704. b:=tsetconstnode(left).value_set^ >= tsetconstnode(right).value_set^;
  705. t:=cordconstnode.create(byte(b),booltype,true);
  706. end;
  707. else
  708. begin
  709. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  710. t:=cnothingnode.create;
  711. end;
  712. end;
  713. result:=t;
  714. exit;
  715. end;
  716. end;
  717. function taddnode.pass_typecheck:tnode;
  718. var
  719. hp : tnode;
  720. lt,rt : tnodetype;
  721. rd,ld : tdef;
  722. ot : tnodetype;
  723. hsym : tfieldvarsym;
  724. i : longint;
  725. strtype : tstringtype;
  726. b : boolean;
  727. {$ifdef state_tracking}
  728. factval : Tnode;
  729. change : boolean;
  730. {$endif}
  731. begin
  732. result:=nil;
  733. { first do the two subtrees }
  734. typecheckpass(left);
  735. typecheckpass(right);
  736. { both left and right need to be valid }
  737. set_varstate(left,vs_read,[vsf_must_be_valid]);
  738. set_varstate(right,vs_read,[vsf_must_be_valid]);
  739. if codegenerror then
  740. exit;
  741. { tp procvar support }
  742. maybe_call_procvar(left,true);
  743. maybe_call_procvar(right,true);
  744. { convert array constructors to sets, because there is no other operator
  745. possible for array constructors }
  746. if is_array_constructor(left.resultdef) then
  747. begin
  748. arrayconstructor_to_set(left);
  749. typecheckpass(left);
  750. end;
  751. if is_array_constructor(right.resultdef) then
  752. begin
  753. arrayconstructor_to_set(right);
  754. typecheckpass(right);
  755. end;
  756. { allow operator overloading }
  757. hp:=self;
  758. if isbinaryoverloaded(hp) then
  759. begin
  760. result:=hp;
  761. exit;
  762. end;
  763. { Stop checking when an error was found in the operator checking }
  764. if codegenerror then
  765. begin
  766. result:=cerrornode.create;
  767. exit;
  768. end;
  769. { Kylix allows enum+ordconstn in an enum declaration (blocktype
  770. is bt_type), we need to do the conversion here before the
  771. constant folding }
  772. if (m_delphi in current_settings.modeswitches) and
  773. (blocktype=bt_type) then
  774. begin
  775. if (left.resultdef.typ=enumdef) and
  776. (right.resultdef.typ=orddef) then
  777. begin
  778. { insert explicit typecast to default signed int }
  779. left:=ctypeconvnode.create_internal(left,sinttype);
  780. typecheckpass(left);
  781. end
  782. else
  783. if (left.resultdef.typ=orddef) and
  784. (right.resultdef.typ=enumdef) then
  785. begin
  786. { insert explicit typecast to default signed int }
  787. right:=ctypeconvnode.create_internal(right,sinttype);
  788. typecheckpass(right);
  789. end;
  790. end;
  791. { is one a real float, then both need to be floats, this
  792. need to be done before the constant folding so constant
  793. operation on a float and int are also handled }
  794. resultrealdef:=pbestrealtype^;
  795. if (right.resultdef.typ=floatdef) or (left.resultdef.typ=floatdef) then
  796. begin
  797. { when both floattypes are already equal then use that
  798. floattype for results }
  799. if (right.resultdef.typ=floatdef) and
  800. (left.resultdef.typ=floatdef) and
  801. (tfloatdef(left.resultdef).floattype=tfloatdef(right.resultdef).floattype) then
  802. resultrealdef:=left.resultdef
  803. { when there is a currency type then use currency, but
  804. only when currency is defined as float }
  805. else
  806. if (is_currency(right.resultdef) or
  807. is_currency(left.resultdef)) and
  808. ((s64currencytype.typ = floatdef) or
  809. (nodetype <> slashn)) then
  810. begin
  811. resultrealdef:=s64currencytype;
  812. inserttypeconv(right,resultrealdef);
  813. inserttypeconv(left,resultrealdef);
  814. end
  815. else
  816. begin
  817. resultrealdef:=getbestreal(left.resultdef,right.resultdef);
  818. inserttypeconv(right,resultrealdef);
  819. inserttypeconv(left,resultrealdef);
  820. end;
  821. end;
  822. { If both operands are constant and there is a widechar
  823. or widestring then convert everything to widestring. This
  824. allows constant folding like char+widechar }
  825. if is_constnode(right) and is_constnode(left) and
  826. (is_widestring(right.resultdef) or
  827. is_widestring(left.resultdef) or
  828. is_widechar(right.resultdef) or
  829. is_widechar(left.resultdef)) then
  830. begin
  831. inserttypeconv(right,cwidestringtype);
  832. inserttypeconv(left,cwidestringtype);
  833. end;
  834. result:=simplify;
  835. if assigned(result) then
  836. exit;
  837. { load easier access variables }
  838. rd:=right.resultdef;
  839. ld:=left.resultdef;
  840. rt:=right.nodetype;
  841. lt:=left.nodetype;
  842. { but an int/int gives real/real! }
  843. if (nodetype=slashn) and not(is_vector(left.resultdef)) and not(is_vector(right.resultdef)) then
  844. begin
  845. if is_currency(left.resultdef) and
  846. is_currency(right.resultdef) then
  847. { In case of currency, converting to float means dividing by 10000 }
  848. { However, since this is already a division, both divisions by }
  849. { 10000 are eliminated when we divide the results -> we can skip }
  850. { them. }
  851. if s64currencytype.typ = floatdef then
  852. begin
  853. { there's no s64comptype or so, how do we avoid the type conversion?
  854. left.resultdef := s64comptype;
  855. right.resultdef := s64comptype; }
  856. end
  857. else
  858. begin
  859. left.resultdef := s64inttype;
  860. right.resultdef := s64inttype;
  861. end;
  862. inserttypeconv(right,resultrealdef);
  863. inserttypeconv(left,resultrealdef);
  864. end
  865. { if both are orddefs then check sub types }
  866. else if (ld.typ=orddef) and (rd.typ=orddef) then
  867. begin
  868. { optimize multiplacation by a power of 2 }
  869. if not(cs_check_overflow in current_settings.localswitches) and
  870. (nodetype = muln) and
  871. (((left.nodetype = ordconstn) and
  872. ispowerof2(tordconstnode(left).value,i)) or
  873. ((right.nodetype = ordconstn) and
  874. ispowerof2(tordconstnode(right).value,i))) then
  875. begin
  876. if left.nodetype = ordconstn then
  877. begin
  878. tordconstnode(left).value := i;
  879. result := cshlshrnode.create(shln,right,left);
  880. end
  881. else
  882. begin
  883. tordconstnode(right).value := i;
  884. result := cshlshrnode.create(shln,left,right);
  885. end;
  886. left := nil;
  887. right := nil;
  888. exit;
  889. end;
  890. { set for & and | operations in macpas mode: they only work on }
  891. { booleans, and always short circuit evaluation }
  892. if (nf_short_bool in flags) then
  893. begin
  894. if not is_boolean(ld) then
  895. begin
  896. inserttypeconv(left,booltype);
  897. ld := left.resultdef;
  898. end;
  899. if not is_boolean(rd) then
  900. begin
  901. inserttypeconv(right,booltype);
  902. rd := right.resultdef;
  903. end;
  904. end;
  905. { 2 booleans? Make them equal to the largest boolean }
  906. if (is_boolean(ld) and is_boolean(rd)) or
  907. (nf_short_bool in flags) then
  908. begin
  909. if torddef(left.resultdef).size>torddef(right.resultdef).size then
  910. begin
  911. right:=ctypeconvnode.create_internal(right,left.resultdef);
  912. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  913. typecheckpass(right);
  914. end
  915. else if torddef(left.resultdef).size<torddef(right.resultdef).size then
  916. begin
  917. left:=ctypeconvnode.create_internal(left,right.resultdef);
  918. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  919. typecheckpass(left);
  920. end;
  921. case nodetype of
  922. xorn,
  923. ltn,
  924. lten,
  925. gtn,
  926. gten,
  927. andn,
  928. orn:
  929. begin
  930. end;
  931. unequaln,
  932. equaln:
  933. begin
  934. if not(cs_full_boolean_eval in current_settings.localswitches) or
  935. (nf_short_bool in flags) then
  936. begin
  937. { Remove any compares with constants }
  938. if (left.nodetype=ordconstn) then
  939. begin
  940. hp:=right;
  941. b:=(tordconstnode(left).value<>0);
  942. ot:=nodetype;
  943. left.free;
  944. left:=nil;
  945. right:=nil;
  946. if (not(b) and (ot=equaln)) or
  947. (b and (ot=unequaln)) then
  948. begin
  949. hp:=cnotnode.create(hp);
  950. end;
  951. result:=hp;
  952. exit;
  953. end;
  954. if (right.nodetype=ordconstn) then
  955. begin
  956. hp:=left;
  957. b:=(tordconstnode(right).value<>0);
  958. ot:=nodetype;
  959. right.free;
  960. right:=nil;
  961. left:=nil;
  962. if (not(b) and (ot=equaln)) or
  963. (b and (ot=unequaln)) then
  964. begin
  965. hp:=cnotnode.create(hp);
  966. end;
  967. result:=hp;
  968. exit;
  969. end;
  970. end;
  971. end;
  972. else
  973. begin
  974. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  975. result:=cnothingnode.create;
  976. exit;
  977. end;
  978. end;
  979. end
  980. { Both are chars? }
  981. else if is_char(rd) and is_char(ld) then
  982. begin
  983. if nodetype=addn then
  984. begin
  985. resultdef:=cshortstringtype;
  986. if not(is_constcharnode(left) and is_constcharnode(right)) then
  987. begin
  988. inserttypeconv(left,cshortstringtype);
  989. {$ifdef addstringopt}
  990. hp := genaddsstringcharoptnode(self);
  991. result := hp;
  992. exit;
  993. {$endif addstringopt}
  994. end
  995. end
  996. else if not(nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) then
  997. begin
  998. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  999. result:=cnothingnode.create;
  1000. exit;
  1001. end;
  1002. end
  1003. { There is a widechar? }
  1004. else if is_widechar(rd) or is_widechar(ld) then
  1005. begin
  1006. { widechar+widechar gives widestring }
  1007. if nodetype=addn then
  1008. begin
  1009. inserttypeconv(left,cwidestringtype);
  1010. if (torddef(rd).ordtype<>uwidechar) then
  1011. inserttypeconv(right,cwidechartype);
  1012. resultdef:=cwidestringtype;
  1013. end
  1014. else
  1015. begin
  1016. if (torddef(ld).ordtype<>uwidechar) then
  1017. inserttypeconv(left,cwidechartype);
  1018. if (torddef(rd).ordtype<>uwidechar) then
  1019. inserttypeconv(right,cwidechartype);
  1020. end;
  1021. end
  1022. { is there a currency type ? }
  1023. else if ((torddef(rd).ordtype=scurrency) or (torddef(ld).ordtype=scurrency)) then
  1024. begin
  1025. if (torddef(ld).ordtype<>scurrency) then
  1026. inserttypeconv(left,s64currencytype);
  1027. if (torddef(rd).ordtype<>scurrency) then
  1028. inserttypeconv(right,s64currencytype);
  1029. end
  1030. { and,or,xor work on bit patterns and don't care
  1031. about the sign of integers }
  1032. { compares don't need extension to native int size either }
  1033. { as long as both values are signed or unsigned }
  1034. else if is_integer(ld) and is_integer(rd) and
  1035. ((nodetype in [andn,orn,xorn]) or
  1036. ((nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1037. not(is_signed(ld) xor is_signed(rd)))) then
  1038. begin
  1039. if rd.size>ld.size then
  1040. inserttypeconv_internal(left,right.resultdef)
  1041. else
  1042. inserttypeconv_internal(right,left.resultdef);
  1043. end
  1044. { is there a signed 64 bit type ? }
  1045. else if ((torddef(rd).ordtype=s64bit) or (torddef(ld).ordtype=s64bit)) then
  1046. begin
  1047. if (torddef(ld).ordtype<>s64bit) then
  1048. inserttypeconv(left,s64inttype);
  1049. if (torddef(rd).ordtype<>s64bit) then
  1050. inserttypeconv(right,s64inttype);
  1051. end
  1052. { is there a unsigned 64 bit type ? }
  1053. else if ((torddef(rd).ordtype=u64bit) or (torddef(ld).ordtype=u64bit)) then
  1054. begin
  1055. if (torddef(ld).ordtype<>u64bit) then
  1056. inserttypeconv(left,u64inttype);
  1057. if (torddef(rd).ordtype<>u64bit) then
  1058. inserttypeconv(right,u64inttype);
  1059. end
  1060. { 64 bit cpus do calculations always in 64 bit }
  1061. {$ifndef cpu64bit}
  1062. { is there a cardinal? }
  1063. else if ((torddef(rd).ordtype=u32bit) or (torddef(ld).ordtype=u32bit)) then
  1064. begin
  1065. { convert positive constants to u32bit }
  1066. if (torddef(ld).ordtype<>u32bit) and
  1067. is_constintnode(left) and
  1068. (tordconstnode(left).value >= 0) then
  1069. inserttypeconv(left,u32inttype);
  1070. if (torddef(rd).ordtype<>u32bit) and
  1071. is_constintnode(right) and
  1072. (tordconstnode(right).value >= 0) then
  1073. inserttypeconv(right,u32inttype);
  1074. { when one of the operand is signed or the operation is subn then perform
  1075. the operation in 64bit, can't use rd/ld here because there
  1076. could be already typeconvs inserted.
  1077. This is compatible with the code below for other unsigned types (PFV) }
  1078. if is_signed(left.resultdef) or
  1079. is_signed(right.resultdef) or
  1080. (nodetype=subn) then
  1081. begin
  1082. if nodetype<>subn then
  1083. CGMessage(type_w_mixed_signed_unsigned);
  1084. inserttypeconv(left,s64inttype);
  1085. inserttypeconv(right,s64inttype);
  1086. end
  1087. else
  1088. begin
  1089. if (torddef(left.resultdef).ordtype<>u32bit) then
  1090. inserttypeconv(left,u32inttype);
  1091. if (torddef(right.resultdef).ordtype<>u32bit) then
  1092. inserttypeconv(right,u32inttype);
  1093. end;
  1094. end
  1095. {$endif cpu64bit}
  1096. { generic ord conversion is sinttype }
  1097. else
  1098. begin
  1099. { if the left or right value is smaller than the normal
  1100. type sinttype and is unsigned, and the other value
  1101. is a constant < 0, the result will always be false/true
  1102. for equal / unequal nodes.
  1103. }
  1104. if (
  1105. { left : unsigned ordinal var, right : < 0 constant }
  1106. (
  1107. ((is_signed(ld)=false) and (is_constintnode(left) =false)) and
  1108. ((is_constintnode(right)) and (tordconstnode(right).value < 0))
  1109. ) or
  1110. { right : unsigned ordinal var, left : < 0 constant }
  1111. (
  1112. ((is_signed(rd)=false) and (is_constintnode(right) =false)) and
  1113. ((is_constintnode(left)) and (tordconstnode(left).value < 0))
  1114. )
  1115. ) then
  1116. begin
  1117. if nodetype = equaln then
  1118. CGMessage(type_w_signed_unsigned_always_false)
  1119. else
  1120. if nodetype = unequaln then
  1121. CGMessage(type_w_signed_unsigned_always_true)
  1122. else
  1123. if (is_constintnode(left) and (nodetype in [ltn,lten])) or
  1124. (is_constintnode(right) and (nodetype in [gtn,gten])) then
  1125. CGMessage(type_w_signed_unsigned_always_true)
  1126. else
  1127. if (is_constintnode(right) and (nodetype in [ltn,lten])) or
  1128. (is_constintnode(left) and (nodetype in [gtn,gten])) then
  1129. CGMessage(type_w_signed_unsigned_always_false);
  1130. end;
  1131. { When there is a signed type or there is a minus operation
  1132. we convert to signed int. Otherwise (both are unsigned) we keep
  1133. the result also unsigned. This is compatible with Delphi (PFV) }
  1134. if is_signed(ld) or
  1135. is_signed(rd) or
  1136. (nodetype=subn) then
  1137. begin
  1138. inserttypeconv(right,sinttype);
  1139. inserttypeconv(left,sinttype);
  1140. end
  1141. else
  1142. begin
  1143. inserttypeconv(right,uinttype);
  1144. inserttypeconv(left,uinttype);
  1145. end;
  1146. end;
  1147. end
  1148. { if both are floatdefs, conversion is already done before constant folding }
  1149. else if (ld.typ=floatdef) then
  1150. begin
  1151. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  1152. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1153. end
  1154. { left side a setdef, must be before string processing,
  1155. else array constructor can be seen as array of char (PFV) }
  1156. else if (ld.typ=setdef) then
  1157. begin
  1158. { trying to add a set element? }
  1159. if (nodetype=addn) and (rd.typ<>setdef) then
  1160. begin
  1161. if (rt=setelementn) then
  1162. begin
  1163. if not(equal_defs(tsetdef(ld).elementdef,rd)) then
  1164. inserttypeconv(right,tsetdef(ld).elementdef);
  1165. end
  1166. else
  1167. CGMessage(type_e_mismatch);
  1168. { ranges require normsets on big endian system }
  1169. if (target_info.endian=endian_big) and
  1170. (tsetdef(ld).size<>32) and
  1171. (rt=setelementn) and
  1172. assigned(tsetelementnode(right).right) then
  1173. begin
  1174. { generate a temporary normset def, it'll be destroyed
  1175. when the symtable is unloaded }
  1176. inserttypeconv(left,tsetdef.create(tsetdef(ld).elementdef,255));
  1177. end;
  1178. end
  1179. else
  1180. begin
  1181. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1182. CGMessage(type_e_set_operation_unknown);
  1183. { make operands the same setdef, if right is a normalset or varset then
  1184. force the left side to be the same. General fallback also for non-set nodes
  1185. is to convert right to a set }
  1186. if not(equal_defs(ld,rd)) then
  1187. begin
  1188. if is_varset(rd) then
  1189. inserttypeconv(left,right.resultdef)
  1190. else
  1191. inserttypeconv(right,left.resultdef);
  1192. end;
  1193. end;
  1194. end
  1195. { pointer comparision and subtraction }
  1196. else if (
  1197. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  1198. ) or
  1199. { compare/add pchar to variable (not stringconst) char arrays
  1200. by addresses like BP/Delphi }
  1201. (
  1202. (nodetype in [equaln,unequaln,subn,addn]) and
  1203. (
  1204. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1205. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1206. )
  1207. ) then
  1208. begin
  1209. { convert char array to pointer }
  1210. if is_chararray(rd) then
  1211. begin
  1212. inserttypeconv(right,charpointertype);
  1213. rd:=right.resultdef;
  1214. end
  1215. else if is_chararray(ld) then
  1216. begin
  1217. inserttypeconv(left,charpointertype);
  1218. ld:=left.resultdef;
  1219. end;
  1220. case nodetype of
  1221. equaln,unequaln :
  1222. begin
  1223. if is_voidpointer(right.resultdef) then
  1224. inserttypeconv(right,left.resultdef)
  1225. else if is_voidpointer(left.resultdef) then
  1226. inserttypeconv(left,right.resultdef)
  1227. else if not(equal_defs(ld,rd)) then
  1228. IncompatibleTypes(ld,rd);
  1229. { now that the type checking is done, convert both to charpointer, }
  1230. { because methodpointers are 8 bytes even though only the first 4 }
  1231. { bytes must be compared. This can happen here if we are in }
  1232. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1233. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1234. { optimized away, since the result already was a voidpointer, so }
  1235. { use a charpointer instead (JM) }
  1236. inserttypeconv_internal(left,charpointertype);
  1237. inserttypeconv_internal(right,charpointertype);
  1238. end;
  1239. ltn,lten,gtn,gten:
  1240. begin
  1241. if (cs_extsyntax in current_settings.moduleswitches) then
  1242. begin
  1243. if is_voidpointer(right.resultdef) then
  1244. inserttypeconv(right,left.resultdef)
  1245. else if is_voidpointer(left.resultdef) then
  1246. inserttypeconv(left,right.resultdef)
  1247. else if not(equal_defs(ld,rd)) then
  1248. IncompatibleTypes(ld,rd);
  1249. end
  1250. else
  1251. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1252. end;
  1253. subn:
  1254. begin
  1255. if (cs_extsyntax in current_settings.moduleswitches) then
  1256. begin
  1257. if is_voidpointer(right.resultdef) then
  1258. begin
  1259. if is_big_untyped_addrnode(right) then
  1260. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1261. inserttypeconv(right,left.resultdef)
  1262. end
  1263. else if is_voidpointer(left.resultdef) then
  1264. inserttypeconv(left,right.resultdef)
  1265. else if not(equal_defs(ld,rd)) then
  1266. IncompatibleTypes(ld,rd);
  1267. end
  1268. else
  1269. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1270. if not(nf_has_pointerdiv in flags) and
  1271. (tpointerdef(rd).pointeddef.size>1) then
  1272. begin
  1273. hp:=getcopy;
  1274. include(hp.flags,nf_has_pointerdiv);
  1275. result:=cmoddivnode.create(divn,hp,cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,false));
  1276. end;
  1277. resultdef:=sinttype;
  1278. exit;
  1279. end;
  1280. else
  1281. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1282. end;
  1283. end
  1284. { is one of the operands a string?,
  1285. chararrays are also handled as strings (after conversion), also take
  1286. care of chararray+chararray and chararray+char.
  1287. Note: Must be done after pointerdef+pointerdef has been checked, else
  1288. pchar is converted to string }
  1289. else if (rd.typ=stringdef) or
  1290. (ld.typ=stringdef) or
  1291. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1292. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1293. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1294. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1295. begin
  1296. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1297. begin
  1298. { Is there a widestring? }
  1299. if is_widestring(rd) or is_widestring(ld) or
  1300. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1301. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1302. strtype:= st_widestring
  1303. else
  1304. if is_ansistring(rd) or is_ansistring(ld) or
  1305. ((cs_ansistrings in current_settings.localswitches) and
  1306. //todo: Move some of this to longstring's then they are implemented?
  1307. (
  1308. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or
  1309. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld)
  1310. )
  1311. ) then
  1312. strtype:= st_ansistring
  1313. else
  1314. if is_longstring(rd) or is_longstring(ld) then
  1315. strtype:= st_longstring
  1316. else
  1317. begin
  1318. {$warning todo: add a warning/hint here if one converting a too large array}
  1319. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1320. Note: Delphi halts with error if "array [0..xx] of char"
  1321. is assigned to ShortString and string length is less
  1322. then array size }
  1323. strtype:= st_shortstring;
  1324. end;
  1325. // Now convert nodes to common string type
  1326. case strtype of
  1327. st_widestring :
  1328. begin
  1329. if not(is_widestring(rd)) then
  1330. inserttypeconv(right,cwidestringtype);
  1331. if not(is_widestring(ld)) then
  1332. inserttypeconv(left,cwidestringtype);
  1333. end;
  1334. st_ansistring :
  1335. begin
  1336. if not(is_ansistring(rd)) then
  1337. inserttypeconv(right,cansistringtype);
  1338. if not(is_ansistring(ld)) then
  1339. inserttypeconv(left,cansistringtype);
  1340. end;
  1341. st_longstring :
  1342. begin
  1343. if not(is_longstring(rd)) then
  1344. inserttypeconv(right,clongstringtype);
  1345. if not(is_longstring(ld)) then
  1346. inserttypeconv(left,clongstringtype);
  1347. end;
  1348. st_shortstring :
  1349. begin
  1350. if not(is_shortstring(ld)) then
  1351. inserttypeconv(left,cshortstringtype);
  1352. { don't convert char, that can be handled by the optimized node }
  1353. if not(is_shortstring(rd) or is_char(rd)) then
  1354. inserttypeconv(right,cshortstringtype);
  1355. end;
  1356. else
  1357. internalerror(2005101);
  1358. end;
  1359. end
  1360. else
  1361. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1362. end
  1363. { class or interface equation }
  1364. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  1365. begin
  1366. if (nodetype in [equaln,unequaln]) then
  1367. begin
  1368. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  1369. begin
  1370. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1371. inserttypeconv(right,left.resultdef)
  1372. else
  1373. inserttypeconv(left,right.resultdef);
  1374. end
  1375. else if is_class_or_interface(rd) then
  1376. inserttypeconv(left,right.resultdef)
  1377. else
  1378. inserttypeconv(right,left.resultdef);
  1379. end
  1380. else
  1381. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1382. end
  1383. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  1384. begin
  1385. if (nodetype in [equaln,unequaln]) then
  1386. begin
  1387. if tobjectdef(tclassrefdef(rd).pointeddef).is_related(
  1388. tobjectdef(tclassrefdef(ld).pointeddef)) then
  1389. inserttypeconv(right,left.resultdef)
  1390. else
  1391. inserttypeconv(left,right.resultdef);
  1392. end
  1393. else
  1394. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1395. end
  1396. { allows comperasion with nil pointer }
  1397. else if is_class_or_interface(rd) or (rd.typ=classrefdef) then
  1398. begin
  1399. if (nodetype in [equaln,unequaln]) then
  1400. inserttypeconv(left,right.resultdef)
  1401. else
  1402. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1403. end
  1404. else if is_class_or_interface(ld) or (ld.typ=classrefdef) then
  1405. begin
  1406. if (nodetype in [equaln,unequaln]) then
  1407. inserttypeconv(right,left.resultdef)
  1408. else
  1409. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1410. end
  1411. { support procvar=nil,procvar<>nil }
  1412. else if ((ld.typ=procvardef) and (rt=niln)) or
  1413. ((rd.typ=procvardef) and (lt=niln)) then
  1414. begin
  1415. if not(nodetype in [equaln,unequaln]) then
  1416. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1417. { find proc field in methodpointer record }
  1418. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1419. if not assigned(hsym) then
  1420. internalerror(200412043);
  1421. { For methodpointers compare only tmethodpointer.proc }
  1422. if (rd.typ=procvardef) and
  1423. (not tprocvardef(rd).is_addressonly) then
  1424. begin
  1425. right:=csubscriptnode.create(
  1426. hsym,
  1427. ctypeconvnode.create_internal(right,methodpointertype));
  1428. typecheckpass(right);
  1429. end;
  1430. if (ld.typ=procvardef) and
  1431. (not tprocvardef(ld).is_addressonly) then
  1432. begin
  1433. left:=csubscriptnode.create(
  1434. hsym,
  1435. ctypeconvnode.create_internal(left,methodpointertype));
  1436. typecheckpass(left);
  1437. end;
  1438. end
  1439. { support dynamicarray=nil,dynamicarray<>nil }
  1440. else if (is_dynamic_array(ld) and (rt=niln)) or
  1441. (is_dynamic_array(rd) and (lt=niln)) or
  1442. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1443. begin
  1444. if not(nodetype in [equaln,unequaln]) then
  1445. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1446. end
  1447. {$ifdef SUPPORT_MMX}
  1448. { mmx support, this must be before the zero based array
  1449. check }
  1450. else if (cs_mmx in current_settings.localswitches) and
  1451. is_mmx_able_array(ld) and
  1452. is_mmx_able_array(rd) and
  1453. equal_defs(ld,rd) then
  1454. begin
  1455. case nodetype of
  1456. addn,subn,xorn,orn,andn:
  1457. ;
  1458. { mul is a little bit restricted }
  1459. muln:
  1460. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1461. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1462. else
  1463. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1464. end;
  1465. end
  1466. {$endif SUPPORT_MMX}
  1467. { vector support, this must be before the zero based array
  1468. check }
  1469. else if (cs_support_vectors in current_settings.globalswitches) and
  1470. is_vector(ld) and
  1471. is_vector(rd) and
  1472. equal_defs(ld,rd) then
  1473. begin
  1474. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  1475. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1476. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  1477. resultdef:=left.resultdef;
  1478. end
  1479. { this is a little bit dangerous, also the left type }
  1480. { pointer to should be checked! This broke the mmx support }
  1481. else if (rd.typ=pointerdef) or
  1482. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  1483. begin
  1484. if is_zero_based_array(rd) then
  1485. begin
  1486. resultdef:=tpointerdef.create(tarraydef(rd).elementdef);
  1487. inserttypeconv(right,resultdef);
  1488. end
  1489. else
  1490. resultdef:=right.resultdef;
  1491. inserttypeconv(left,sinttype);
  1492. if nodetype=addn then
  1493. begin
  1494. if not(cs_extsyntax in current_settings.moduleswitches) or
  1495. (not(is_pchar(ld)) and not(m_add_pointer in current_settings.modeswitches)) then
  1496. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1497. if (rd.typ=pointerdef) and
  1498. (tpointerdef(rd).pointeddef.size>1) then
  1499. begin
  1500. left:=caddnode.create(muln,left,
  1501. cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,true));
  1502. typecheckpass(left);
  1503. end;
  1504. end
  1505. else
  1506. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1507. end
  1508. else if (ld.typ=pointerdef) or
  1509. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  1510. begin
  1511. if is_zero_based_array(ld) then
  1512. begin
  1513. resultdef:=tpointerdef.create(tarraydef(ld).elementdef);
  1514. inserttypeconv(left,resultdef);
  1515. end
  1516. else
  1517. resultdef:=left.resultdef;
  1518. inserttypeconv(right,sinttype);
  1519. if nodetype in [addn,subn] then
  1520. begin
  1521. if not(cs_extsyntax in current_settings.moduleswitches) or
  1522. (not(is_pchar(ld)) and not(m_add_pointer in current_settings.modeswitches)) then
  1523. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1524. if (ld.typ=pointerdef) then
  1525. begin
  1526. if is_big_untyped_addrnode(left) then
  1527. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1528. if (tpointerdef(ld).pointeddef.size>1) then
  1529. begin
  1530. right:=caddnode.create(muln,right,
  1531. cordconstnode.create(tpointerdef(ld).pointeddef.size,sinttype,true));
  1532. typecheckpass(right);
  1533. end
  1534. end else
  1535. if is_zero_based_array(ld) and
  1536. (tarraydef(ld).elementdef.size>1) then
  1537. begin
  1538. right:=caddnode.create(muln,right,
  1539. cordconstnode.create(tarraydef(ld).elementdef.size,sinttype,true));
  1540. typecheckpass(right);
  1541. end;
  1542. end
  1543. else
  1544. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1545. end
  1546. else if (rd.typ=procvardef) and
  1547. (ld.typ=procvardef) and
  1548. equal_defs(rd,ld) then
  1549. begin
  1550. if (nodetype in [equaln,unequaln]) then
  1551. begin
  1552. if tprocvardef(rd).is_addressonly then
  1553. begin
  1554. inserttypeconv_internal(right,voidpointertype);
  1555. inserttypeconv_internal(left,voidpointertype);
  1556. end
  1557. else
  1558. begin
  1559. { find proc field in methodpointer record }
  1560. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1561. if not assigned(hsym) then
  1562. internalerror(200412043);
  1563. { Compare tmehodpointer(left).proc }
  1564. right:=csubscriptnode.create(
  1565. hsym,
  1566. ctypeconvnode.create_internal(right,methodpointertype));
  1567. typecheckpass(right);
  1568. left:=csubscriptnode.create(
  1569. hsym,
  1570. ctypeconvnode.create_internal(left,methodpointertype));
  1571. typecheckpass(left);
  1572. end;
  1573. end
  1574. else
  1575. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1576. end
  1577. { enums }
  1578. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  1579. begin
  1580. if allowenumop(nodetype) then
  1581. inserttypeconv(right,left.resultdef)
  1582. else
  1583. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1584. end
  1585. { generic conversion, this is for error recovery }
  1586. else
  1587. begin
  1588. inserttypeconv(left,sinttype);
  1589. inserttypeconv(right,sinttype);
  1590. end;
  1591. { set resultdef if not already done }
  1592. if not assigned(resultdef) then
  1593. begin
  1594. case nodetype of
  1595. ltn,lten,gtn,gten,equaln,unequaln :
  1596. resultdef:=booltype;
  1597. slashn :
  1598. resultdef:=resultrealdef;
  1599. addn:
  1600. begin
  1601. { for strings, return is always a 255 char string }
  1602. if is_shortstring(left.resultdef) then
  1603. resultdef:=cshortstringtype
  1604. else
  1605. resultdef:=left.resultdef;
  1606. end;
  1607. else
  1608. resultdef:=left.resultdef;
  1609. end;
  1610. end;
  1611. { when the result is currency we need some extra code for
  1612. multiplication and division. this should not be done when
  1613. the muln or slashn node is created internally }
  1614. if not(nf_is_currency in flags) and
  1615. is_currency(resultdef) then
  1616. begin
  1617. case nodetype of
  1618. slashn :
  1619. begin
  1620. { slashn will only work with floats }
  1621. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1622. include(hp.flags,nf_is_currency);
  1623. result:=hp;
  1624. end;
  1625. muln :
  1626. begin
  1627. if s64currencytype.typ=floatdef then
  1628. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1629. else
  1630. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1631. include(hp.flags,nf_is_currency);
  1632. result:=hp
  1633. end;
  1634. end;
  1635. end;
  1636. end;
  1637. function taddnode.first_addstring: tnode;
  1638. const
  1639. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  1640. var
  1641. p: tnode;
  1642. newstatement : tstatementnode;
  1643. tempnode,tempnode2 : ttempcreatenode;
  1644. cmpfuncname: string;
  1645. begin
  1646. { when we get here, we are sure that both the left and the right }
  1647. { node are both strings of the same stringtype (JM) }
  1648. case nodetype of
  1649. addn:
  1650. begin
  1651. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  1652. begin
  1653. result:=right;
  1654. left.free;
  1655. left:=nil;
  1656. right:=nil;
  1657. exit;
  1658. end;
  1659. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  1660. begin
  1661. result:=left;
  1662. left:=nil;
  1663. right.free;
  1664. right:=nil;
  1665. exit;
  1666. end;
  1667. { create the call to the concat routine both strings as arguments }
  1668. if assigned(aktassignmentnode) and
  1669. (aktassignmentnode.right=self) and
  1670. (aktassignmentnode.left.resultdef=resultdef) and
  1671. valid_for_var(aktassignmentnode.left,false) then
  1672. begin
  1673. result:=ccallnode.createintern('fpc_'+
  1674. tstringdef(resultdef).stringtypname+'_concat',
  1675. ccallparanode.create(right,
  1676. ccallparanode.create(left,
  1677. ccallparanode.create(aktassignmentnode.left.getcopy,nil))));
  1678. include(aktassignmentnode.flags,nf_assign_done_in_right);
  1679. firstpass(result);
  1680. end
  1681. else
  1682. begin
  1683. result:=internalstatements(newstatement);
  1684. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1685. addstatement(newstatement,tempnode);
  1686. addstatement(newstatement,ccallnode.createintern('fpc_'+
  1687. tstringdef(resultdef).stringtypname+'_concat',
  1688. ccallparanode.create(right,
  1689. ccallparanode.create(left,
  1690. ccallparanode.create(ctemprefnode.create(tempnode),nil)))));
  1691. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1692. addstatement(newstatement,ctemprefnode.create(tempnode));
  1693. end;
  1694. { we reused the arguments }
  1695. left := nil;
  1696. right := nil;
  1697. end;
  1698. ltn,lten,gtn,gten,equaln,unequaln :
  1699. begin
  1700. { generate better code for comparison with empty string, we
  1701. only need to compare the length with 0 }
  1702. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1703. { windows widestrings are too complicated to be handled optimized }
  1704. not(is_widestring(left.resultdef) and (target_info.system in system_windows)) and
  1705. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  1706. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  1707. begin
  1708. { switch so that the constant is always on the right }
  1709. if left.nodetype = stringconstn then
  1710. begin
  1711. p := left;
  1712. left := right;
  1713. right := p;
  1714. nodetype:=swap_relation[nodetype];
  1715. end;
  1716. if is_shortstring(left.resultdef) or
  1717. (nodetype in [gtn,gten,ltn,lten]) then
  1718. { compare the length with 0 }
  1719. result := caddnode.create(nodetype,
  1720. cinlinenode.create(in_length_x,false,left),
  1721. cordconstnode.create(0,s32inttype,false))
  1722. else
  1723. begin
  1724. (*
  1725. if is_widestring(left.resultdef) and
  1726. (target_info.system in system_windows) then
  1727. begin
  1728. { windows like widestrings requires that we also check the length }
  1729. result:=internalstatements(newstatement);
  1730. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1731. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1732. addstatement(newstatement,tempnode);
  1733. addstatement(newstatement,tempnode2);
  1734. { poor man's cse }
  1735. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1736. ctypeconvnode.create_internal(left,voidpointertype))
  1737. );
  1738. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  1739. caddnode.create(orn,
  1740. caddnode.create(nodetype,
  1741. ctemprefnode.create(tempnode),
  1742. cpointerconstnode.create(0,voidpointertype)
  1743. ),
  1744. caddnode.create(nodetype,
  1745. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  1746. cordconstnode.create(0,s32inttype,false)
  1747. )
  1748. )
  1749. ));
  1750. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1751. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  1752. addstatement(newstatement,ctemprefnode.create(tempnode2));
  1753. end
  1754. else
  1755. *)
  1756. begin
  1757. { compare the pointer with nil (for ansistrings etc), }
  1758. { faster than getting the length (JM) }
  1759. result:= caddnode.create(nodetype,
  1760. ctypeconvnode.create_internal(left,voidpointertype),
  1761. cpointerconstnode.create(0,voidpointertype));
  1762. end;
  1763. end;
  1764. { left is reused }
  1765. left := nil;
  1766. { right isn't }
  1767. right.free;
  1768. right := nil;
  1769. exit;
  1770. end;
  1771. { no string constant -> call compare routine }
  1772. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  1773. { for equality checks use optimized version }
  1774. if nodetype in [equaln,unequaln] then
  1775. cmpfuncname := cmpfuncname + '_equal';
  1776. result := ccallnode.createintern(cmpfuncname,
  1777. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1778. { and compare its result with 0 according to the original operator }
  1779. result := caddnode.create(nodetype,result,
  1780. cordconstnode.create(0,s32inttype,false));
  1781. left := nil;
  1782. right := nil;
  1783. end;
  1784. end;
  1785. end;
  1786. function taddnode.first_addset : tnode;
  1787. procedure call_varset_helper(const n : string);
  1788. var
  1789. newstatement : tstatementnode;
  1790. temp : ttempcreatenode;
  1791. begin
  1792. { add two var sets }
  1793. result:=internalstatements(newstatement);
  1794. { create temp for result }
  1795. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1796. addstatement(newstatement,temp);
  1797. addstatement(newstatement,ccallnode.createintern(n,
  1798. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1799. ccallparanode.create(ctemprefnode.create(temp),
  1800. ccallparanode.create(right,
  1801. ccallparanode.create(left,nil)))))
  1802. );
  1803. { remove reused parts from original node }
  1804. left:=nil;
  1805. right:=nil;
  1806. { the last statement should return the value as
  1807. location and type, this is done be referencing the
  1808. temp and converting it first from a persistent temp to
  1809. normal temp }
  1810. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1811. addstatement(newstatement,ctemprefnode.create(temp));
  1812. end;
  1813. var
  1814. procname: string[31];
  1815. tempn: tnode;
  1816. paras: tcallparanode;
  1817. srsym: ttypesym;
  1818. newstatement : tstatementnode;
  1819. temp : ttempcreatenode;
  1820. begin
  1821. if (is_varset(left.resultdef) or is_varset(right.resultdef)) then
  1822. begin
  1823. case nodetype of
  1824. equaln,unequaln,lten,gten:
  1825. begin
  1826. case nodetype of
  1827. equaln,unequaln:
  1828. procname := 'fpc_varset_comp_sets';
  1829. lten,gten:
  1830. begin
  1831. procname := 'fpc_varset_contains_sets';
  1832. { (left >= right) = (right <= left) }
  1833. if nodetype = gten then
  1834. begin
  1835. tempn := left;
  1836. left := right;
  1837. right := tempn;
  1838. end;
  1839. end;
  1840. end;
  1841. result := ccallnode.createinternres(procname,
  1842. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  1843. ccallparanode.create(right,
  1844. ccallparanode.create(left,nil))),resultdef);
  1845. { left and right are reused as parameters }
  1846. left := nil;
  1847. right := nil;
  1848. { for an unequaln, we have to negate the result of comp_sets }
  1849. if nodetype = unequaln then
  1850. result := cnotnode.create(result);
  1851. end;
  1852. addn:
  1853. begin
  1854. { optimize first loading of a set }
  1855. if (right.nodetype=setelementn) and
  1856. not(assigned(tsetelementnode(right).right)) and
  1857. is_emptyset(left) then
  1858. begin
  1859. result:=internalstatements(newstatement);
  1860. { create temp for result }
  1861. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1862. addstatement(newstatement,temp);
  1863. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  1864. ccallparanode.create(ctemprefnode.create(temp),
  1865. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1866. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),nil))))
  1867. );
  1868. { the last statement should return the value as
  1869. location and type, this is done be referencing the
  1870. temp and converting it first from a persistent temp to
  1871. normal temp }
  1872. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1873. addstatement(newstatement,ctemprefnode.create(temp));
  1874. tsetelementnode(right).left := nil;
  1875. end
  1876. else
  1877. begin
  1878. if right.nodetype=setelementn then
  1879. begin
  1880. result:=internalstatements(newstatement);
  1881. { create temp for result }
  1882. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1883. addstatement(newstatement,temp);
  1884. { add a range or a single element? }
  1885. if assigned(tsetelementnode(right).right) then
  1886. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  1887. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1888. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  1889. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  1890. ccallparanode.create(ctemprefnode.create(temp),
  1891. ccallparanode.create(left,nil))))))
  1892. )
  1893. else
  1894. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  1895. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1896. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  1897. ccallparanode.create(ctemprefnode.create(temp),
  1898. ccallparanode.create(left,nil)))))
  1899. );
  1900. { remove reused parts from original node }
  1901. tsetelementnode(right).right:=nil;
  1902. tsetelementnode(right).left:=nil;
  1903. left:=nil;
  1904. { the last statement should return the value as
  1905. location and type, this is done be referencing the
  1906. temp and converting it first from a persistent temp to
  1907. normal temp }
  1908. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1909. addstatement(newstatement,ctemprefnode.create(temp));
  1910. end
  1911. else
  1912. call_varset_helper('fpc_varset_add_sets');
  1913. end
  1914. end;
  1915. subn:
  1916. call_varset_helper('fpc_varset_sub_sets');
  1917. symdifn:
  1918. call_varset_helper('fpc_varset_symdif_sets');
  1919. muln:
  1920. call_varset_helper('fpc_varset_mul_sets');
  1921. else
  1922. internalerror(200609241);
  1923. end;
  1924. end
  1925. else
  1926. begin
  1927. { get the sym that represents the fpc_normal_set type }
  1928. srsym:=search_system_type('FPC_NORMAL_SET');
  1929. case nodetype of
  1930. equaln,unequaln,lten,gten:
  1931. begin
  1932. case nodetype of
  1933. equaln,unequaln:
  1934. procname := 'fpc_set_comp_sets';
  1935. lten,gten:
  1936. begin
  1937. procname := 'fpc_set_contains_sets';
  1938. { (left >= right) = (right <= left) }
  1939. if nodetype = gten then
  1940. begin
  1941. tempn := left;
  1942. left := right;
  1943. right := tempn;
  1944. end;
  1945. end;
  1946. end;
  1947. { convert the arguments (explicitely) to fpc_normal_set's }
  1948. left := ctypeconvnode.create_internal(left,srsym.typedef);
  1949. right := ctypeconvnode.create_internal(right,srsym.typedef);
  1950. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1951. ccallparanode.create(left,nil)));
  1952. { left and right are reused as parameters }
  1953. left := nil;
  1954. right := nil;
  1955. { for an unequaln, we have to negate the result of comp_sets }
  1956. if nodetype = unequaln then
  1957. result := cnotnode.create(result);
  1958. end;
  1959. addn:
  1960. begin
  1961. { optimize first loading of a set }
  1962. if (right.nodetype=setelementn) and
  1963. not(assigned(tsetelementnode(right).right)) and
  1964. is_emptyset(left) then
  1965. begin
  1966. { type cast the value to pass as argument to a byte, }
  1967. { since that's what the helper expects }
  1968. tsetelementnode(right).left :=
  1969. ctypeconvnode.create_internal(tsetelementnode(right).left,u8inttype);
  1970. { set the resultdef to the actual one (otherwise it's }
  1971. { "fpc_normal_set") }
  1972. result := ccallnode.createinternres('fpc_set_create_element',
  1973. ccallparanode.create(tsetelementnode(right).left,nil),
  1974. resultdef);
  1975. { reused }
  1976. tsetelementnode(right).left := nil;
  1977. end
  1978. else
  1979. begin
  1980. if right.nodetype=setelementn then
  1981. begin
  1982. { convert the arguments to bytes, since that's what }
  1983. { the helper expects }
  1984. tsetelementnode(right).left :=
  1985. ctypeconvnode.create_internal(tsetelementnode(right).left,
  1986. u8inttype);
  1987. { convert the original set (explicitely) to an }
  1988. { fpc_normal_set so we can pass it to the helper }
  1989. left := ctypeconvnode.create_internal(left,srsym.typedef);
  1990. { add a range or a single element? }
  1991. if assigned(tsetelementnode(right).right) then
  1992. begin
  1993. tsetelementnode(right).right :=
  1994. ctypeconvnode.create_internal(tsetelementnode(right).right,
  1995. u8inttype);
  1996. { create the call }
  1997. result := ccallnode.createinternres('fpc_set_set_range',
  1998. ccallparanode.create(tsetelementnode(right).right,
  1999. ccallparanode.create(tsetelementnode(right).left,
  2000. ccallparanode.create(left,nil))),resultdef);
  2001. end
  2002. else
  2003. begin
  2004. result := ccallnode.createinternres('fpc_set_set_byte',
  2005. ccallparanode.create(tsetelementnode(right).left,
  2006. ccallparanode.create(left,nil)),resultdef);
  2007. end;
  2008. { remove reused parts from original node }
  2009. tsetelementnode(right).right := nil;
  2010. tsetelementnode(right).left := nil;
  2011. left := nil;
  2012. end
  2013. else
  2014. begin
  2015. { add two sets }
  2016. { convert the sets to fpc_normal_set's }
  2017. result := ccallnode.createinternres('fpc_set_add_sets',
  2018. ccallparanode.create(
  2019. ctypeconvnode.create_explicit(right,srsym.typedef),
  2020. ccallparanode.create(
  2021. ctypeconvnode.create_internal(left,srsym.typedef),nil)),resultdef);
  2022. { remove reused parts from original node }
  2023. left := nil;
  2024. right := nil;
  2025. end;
  2026. end
  2027. end;
  2028. subn,symdifn,muln:
  2029. begin
  2030. { convert the sets to fpc_normal_set's }
  2031. paras := ccallparanode.create(ctypeconvnode.create_internal(right,srsym.typedef),
  2032. ccallparanode.create(ctypeconvnode.create_internal(left,srsym.typedef),nil));
  2033. case nodetype of
  2034. subn:
  2035. result := ccallnode.createinternres('fpc_set_sub_sets',
  2036. paras,resultdef);
  2037. symdifn:
  2038. result := ccallnode.createinternres('fpc_set_symdif_sets',
  2039. paras,resultdef);
  2040. muln:
  2041. result := ccallnode.createinternres('fpc_set_mul_sets',
  2042. paras,resultdef);
  2043. end;
  2044. { remove reused parts from original node }
  2045. left := nil;
  2046. right := nil;
  2047. end;
  2048. else
  2049. internalerror(200108311);
  2050. end;
  2051. end;
  2052. end;
  2053. function taddnode.use_generic_mul32to64: boolean;
  2054. begin
  2055. result := true;
  2056. end;
  2057. function taddnode.try_make_mul32to64: boolean;
  2058. function canbe32bitint(v: tconstexprint; fromdef: torddef; todefsigned: boolean): boolean;
  2059. begin
  2060. if (fromdef.ordtype <> u64bit) then
  2061. result :=
  2062. ((v >= 0) or
  2063. todefsigned) and
  2064. (v >= low(longint)) and
  2065. (v <= high(longint))
  2066. else
  2067. result :=
  2068. (qword(v) >= low(cardinal)) and
  2069. (qword(v) <= high(cardinal))
  2070. end;
  2071. var
  2072. temp: tnode;
  2073. begin
  2074. result := false;
  2075. if ((left.nodetype = typeconvn) and
  2076. is_integer(ttypeconvnode(left).left.resultdef) and
  2077. (not(torddef(ttypeconvnode(left).left.resultdef).ordtype in [u64bit,s64bit])) and
  2078. (((right.nodetype = ordconstn) and
  2079. canbe32bitint(tordconstnode(right).value,torddef(right.resultdef),is_signed(left.resultdef))) or
  2080. ((right.nodetype = typeconvn) and
  2081. is_integer(ttypeconvnode(right).left.resultdef) and
  2082. not(torddef(ttypeconvnode(right).left.resultdef).ordtype in [u64bit,s64bit])) and
  2083. (is_signed(ttypeconvnode(left).left.resultdef) =
  2084. is_signed(ttypeconvnode(right).left.resultdef)))) then
  2085. begin
  2086. temp := ttypeconvnode(left).left;
  2087. ttypeconvnode(left).left := nil;
  2088. left.free;
  2089. left := temp;
  2090. if (right.nodetype = typeconvn) then
  2091. begin
  2092. temp := ttypeconvnode(right).left;
  2093. ttypeconvnode(right).left := nil;
  2094. right.free;
  2095. right := temp;
  2096. end;
  2097. if (is_signed(left.resultdef)) then
  2098. begin
  2099. inserttypeconv(left,s32inttype);
  2100. inserttypeconv(right,s32inttype);
  2101. end
  2102. else
  2103. begin
  2104. inserttypeconv(left,u32inttype);
  2105. inserttypeconv(right,u32inttype);
  2106. end;
  2107. firstpass(left);
  2108. firstpass(right);
  2109. result := true;
  2110. end;
  2111. end;
  2112. function taddnode.first_add64bitint: tnode;
  2113. var
  2114. procname: string[31];
  2115. temp: tnode;
  2116. power: longint;
  2117. begin
  2118. result := nil;
  2119. { create helper calls mul }
  2120. if nodetype <> muln then
  2121. exit;
  2122. { make sure that if there is a constant, that it's on the right }
  2123. if left.nodetype = ordconstn then
  2124. begin
  2125. temp := right;
  2126. right := left;
  2127. left := temp;
  2128. end;
  2129. { can we use a shift instead of a mul? }
  2130. if not (cs_check_overflow in current_settings.localswitches) and
  2131. (right.nodetype = ordconstn) and
  2132. ispowerof2(tordconstnode(right).value,power) then
  2133. begin
  2134. tordconstnode(right).value := power;
  2135. result := cshlshrnode.create(shln,left,right);
  2136. { left and right are reused }
  2137. left := nil;
  2138. right := nil;
  2139. { return firstpassed new node }
  2140. exit;
  2141. end;
  2142. if not(use_generic_mul32to64) and
  2143. try_make_mul32to64 then
  2144. exit;
  2145. { when currency is used set the result of the
  2146. parameters to s64bit, so they are not converted }
  2147. if is_currency(resultdef) then
  2148. begin
  2149. left.resultdef:=s64inttype;
  2150. right.resultdef:=s64inttype;
  2151. end;
  2152. { otherwise, create the parameters for the helper }
  2153. right := ccallparanode.create(
  2154. cordconstnode.create(ord(cs_check_overflow in current_settings.localswitches),booltype,true),
  2155. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2156. left := nil;
  2157. { only qword needs the unsigned code, the
  2158. signed code is also used for currency }
  2159. if is_signed(resultdef) then
  2160. procname := 'fpc_mul_int64'
  2161. else
  2162. procname := 'fpc_mul_qword';
  2163. result := ccallnode.createintern(procname,right);
  2164. right := nil;
  2165. end;
  2166. function taddnode.first_addfloat : tnode;
  2167. var
  2168. procname: string[31];
  2169. { do we need to reverse the result ? }
  2170. notnode : boolean;
  2171. fdef : tdef;
  2172. begin
  2173. result := nil;
  2174. notnode := false;
  2175. { In non-emulation mode, real opcodes are
  2176. emitted for floating point values.
  2177. }
  2178. if not (cs_fp_emulation in current_settings.moduleswitches) then
  2179. exit;
  2180. if not(target_info.system in system_wince) then
  2181. begin
  2182. case tfloatdef(left.resultdef).floattype of
  2183. s32real:
  2184. begin
  2185. fdef:=search_system_type('FLOAT32REC').typedef;
  2186. procname:='float32';
  2187. end;
  2188. s64real:
  2189. begin
  2190. fdef:=search_system_type('FLOAT64').typedef;
  2191. procname:='float64';
  2192. end;
  2193. {!!! not yet implemented
  2194. s128real:
  2195. }
  2196. else
  2197. internalerror(2005082601);
  2198. end;
  2199. case nodetype of
  2200. addn:
  2201. procname:=procname+'_add';
  2202. muln:
  2203. procname:=procname+'_mul';
  2204. subn:
  2205. procname:=procname+'_sub';
  2206. slashn:
  2207. procname:=procname+'_div';
  2208. ltn:
  2209. procname:=procname+'_lt';
  2210. lten:
  2211. procname:=procname+'_le';
  2212. gtn:
  2213. begin
  2214. procname:=procname+'_le';
  2215. notnode:=true;
  2216. end;
  2217. gten:
  2218. begin
  2219. procname:=procname+'_lt';
  2220. notnode:=true;
  2221. end;
  2222. equaln:
  2223. procname:=procname+'_eq';
  2224. unequaln:
  2225. begin
  2226. procname:=procname+'_eq';
  2227. notnode:=true;
  2228. end;
  2229. else
  2230. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2231. end;
  2232. end
  2233. else
  2234. begin
  2235. case nodetype of
  2236. addn:
  2237. procname:='ADD';
  2238. muln:
  2239. procname:='MUL';
  2240. subn:
  2241. procname:='SUB';
  2242. slashn:
  2243. procname:='DIV';
  2244. ltn:
  2245. procname:='LT';
  2246. lten:
  2247. procname:='LE';
  2248. gtn:
  2249. procname:='GT';
  2250. gten:
  2251. procname:='GE';
  2252. equaln:
  2253. procname:='EQ';
  2254. unequaln:
  2255. procname:='NE';
  2256. else
  2257. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2258. end;
  2259. case tfloatdef(left.resultdef).floattype of
  2260. s32real:
  2261. begin
  2262. procname:=procname+'S';
  2263. if nodetype in [addn,muln,subn,slashn] then
  2264. procname:=lower(procname);
  2265. end;
  2266. s64real:
  2267. procname:=procname+'D';
  2268. {!!! not yet implemented
  2269. s128real:
  2270. }
  2271. else
  2272. internalerror(2005082602);
  2273. end;
  2274. end;
  2275. { cast softfpu result? }
  2276. if not(target_info.system in system_wince) then
  2277. begin
  2278. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2279. resultdef:=booltype;
  2280. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  2281. ctypeconvnode.create_internal(right,fdef),
  2282. ccallparanode.create(
  2283. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  2284. end
  2285. else
  2286. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  2287. ccallparanode.create(left,nil)));
  2288. left:=nil;
  2289. right:=nil;
  2290. { do we need to reverse the result }
  2291. if notnode then
  2292. result:=cnotnode.create(result);
  2293. end;
  2294. function taddnode.pass_1 : tnode;
  2295. var
  2296. {$ifdef addstringopt}
  2297. hp : tnode;
  2298. {$endif addstringopt}
  2299. lt,rt : tnodetype;
  2300. rd,ld : tdef;
  2301. newstatement : tstatementnode;
  2302. temp : ttempcreatenode;
  2303. begin
  2304. result:=nil;
  2305. { Can we optimize multiple string additions into a single call?
  2306. This need to be done on a complete tree to detect the multiple
  2307. add nodes and is therefor done before the subtrees are processed }
  2308. if canbemultistringadd(self) then
  2309. begin
  2310. result := genmultistringadd(self);
  2311. exit;
  2312. end;
  2313. { first do the two subtrees }
  2314. firstpass(left);
  2315. firstpass(right);
  2316. if codegenerror then
  2317. exit;
  2318. { load easier access variables }
  2319. rd:=right.resultdef;
  2320. ld:=left.resultdef;
  2321. rt:=right.nodetype;
  2322. lt:=left.nodetype;
  2323. { int/int gives real/real! }
  2324. if nodetype=slashn then
  2325. begin
  2326. {$ifdef cpufpemu}
  2327. if (current_settings.fputype=fpu_soft) or (cs_fp_emulation in current_settings.moduleswitches) then
  2328. begin
  2329. result:=first_addfloat;
  2330. if assigned(result) then
  2331. exit;
  2332. end;
  2333. {$endif cpufpemu}
  2334. expectloc:=LOC_FPUREGISTER;
  2335. { maybe we need an integer register to save }
  2336. { a reference }
  2337. if ((left.expectloc<>LOC_FPUREGISTER) or
  2338. (right.expectloc<>LOC_FPUREGISTER)) and
  2339. (left.registersint=right.registersint) then
  2340. calcregisters(self,1,1,0)
  2341. else
  2342. calcregisters(self,0,1,0);
  2343. { an add node always first loads both the left and the }
  2344. { right in the fpu before doing the calculation. However, }
  2345. { calcregisters(0,2,0) will overestimate the number of }
  2346. { necessary registers (it will make it 3 in case one of }
  2347. { the operands is already in the fpu) (JM) }
  2348. if ((left.expectloc<>LOC_FPUREGISTER) or
  2349. (right.expectloc<>LOC_FPUREGISTER)) and
  2350. (registersfpu < 2) then
  2351. inc(registersfpu);
  2352. end
  2353. { if both are orddefs then check sub types }
  2354. else if (ld.typ=orddef) and (rd.typ=orddef) then
  2355. begin
  2356. { 2 booleans ? }
  2357. if is_boolean(ld) and is_boolean(rd) then
  2358. begin
  2359. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  2360. (nf_short_bool in flags)) and
  2361. (nodetype in [andn,orn]) then
  2362. begin
  2363. expectloc:=LOC_JUMP;
  2364. calcregisters(self,0,0,0);
  2365. end
  2366. else
  2367. begin
  2368. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2369. begin
  2370. expectloc:=LOC_FLAGS;
  2371. if (left.expectloc in [LOC_JUMP,LOC_FLAGS]) and
  2372. (left.expectloc in [LOC_JUMP,LOC_FLAGS]) then
  2373. calcregisters(self,2,0,0)
  2374. else
  2375. calcregisters(self,1,0,0);
  2376. end
  2377. else
  2378. begin
  2379. expectloc:=LOC_REGISTER;
  2380. calcregisters(self,0,0,0);
  2381. end;
  2382. end;
  2383. end
  2384. else
  2385. { Both are chars? only convert to shortstrings for addn }
  2386. if is_char(ld) then
  2387. begin
  2388. if nodetype=addn then
  2389. internalerror(200103291);
  2390. expectloc:=LOC_FLAGS;
  2391. calcregisters(self,1,0,0);
  2392. end
  2393. {$ifndef cpu64bit}
  2394. { is there a 64 bit type ? }
  2395. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  2396. begin
  2397. result := first_add64bitint;
  2398. if assigned(result) then
  2399. exit;
  2400. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2401. expectloc:=LOC_REGISTER
  2402. else
  2403. expectloc:=LOC_JUMP;
  2404. calcregisters(self,2,0,0)
  2405. end
  2406. {$endif cpu64bit}
  2407. { is there a cardinal? }
  2408. else if (torddef(ld).ordtype=u32bit) then
  2409. begin
  2410. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2411. expectloc:=LOC_REGISTER
  2412. else
  2413. expectloc:=LOC_FLAGS;
  2414. calcregisters(self,1,0,0);
  2415. { for unsigned mul we need an extra register }
  2416. if nodetype=muln then
  2417. inc(registersint);
  2418. end
  2419. { generic s32bit conversion }
  2420. else
  2421. begin
  2422. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2423. expectloc:=LOC_REGISTER
  2424. else
  2425. expectloc:=LOC_FLAGS;
  2426. calcregisters(self,1,0,0);
  2427. end;
  2428. end
  2429. { left side a setdef, must be before string processing,
  2430. else array constructor can be seen as array of char (PFV) }
  2431. else if (ld.typ=setdef) then
  2432. begin
  2433. if not(is_varset(ld)) then
  2434. begin
  2435. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2436. expectloc:=LOC_FLAGS
  2437. else
  2438. expectloc:=LOC_REGISTER;
  2439. { are we adding set elements ? }
  2440. if right.nodetype=setelementn then
  2441. begin
  2442. { add range?
  2443. the smallset code can't handle set ranges }
  2444. if assigned(tsetelementnode(right).right) then
  2445. begin
  2446. result:=internalstatements(newstatement);
  2447. { create temp for result }
  2448. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2449. addstatement(newstatement,temp);
  2450. { add a range or a single element? }
  2451. if assigned(tsetelementnode(right).right) then
  2452. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  2453. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2454. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  2455. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2456. ccallparanode.create(ctemprefnode.create(temp),
  2457. ccallparanode.create(left,nil))))))
  2458. )
  2459. else
  2460. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  2461. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2462. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2463. ccallparanode.create(ctemprefnode.create(temp),
  2464. ccallparanode.create(left,nil)))))
  2465. );
  2466. { remove reused parts from original node }
  2467. tsetelementnode(right).right:=nil;
  2468. tsetelementnode(right).left:=nil;
  2469. left:=nil;
  2470. { the last statement should return the value as
  2471. location and type, this is done be referencing the
  2472. temp and converting it first from a persistent temp to
  2473. normal temp }
  2474. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2475. addstatement(newstatement,ctemprefnode.create(temp));
  2476. end
  2477. else
  2478. calcregisters(self,2,0,0)
  2479. end
  2480. else
  2481. calcregisters(self,1,0,0);
  2482. end
  2483. else
  2484. {$ifdef MMXSET}
  2485. {$ifdef i386}
  2486. if cs_mmx in current_settings.localswitches then
  2487. begin
  2488. expectloc:=LOC_MMXREGISTER;
  2489. calcregisters(self,0,0,4);
  2490. end
  2491. else
  2492. {$endif}
  2493. {$endif MMXSET}
  2494. begin
  2495. result := first_addset;
  2496. if assigned(result) then
  2497. exit;
  2498. expectloc:=LOC_CREFERENCE;
  2499. calcregisters(self,0,0,0);
  2500. { here we call SET... }
  2501. include(current_procinfo.flags,pi_do_call);
  2502. end;
  2503. end
  2504. { compare pchar by addresses like BP/Delphi }
  2505. else if is_pchar(ld) then
  2506. begin
  2507. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2508. expectloc:=LOC_REGISTER
  2509. else
  2510. expectloc:=LOC_FLAGS;
  2511. calcregisters(self,1,0,0);
  2512. end
  2513. { is one of the operands a string }
  2514. else if (ld.typ=stringdef) then
  2515. begin
  2516. if is_widestring(ld) then
  2517. begin
  2518. { this is only for add, the comparisaion is handled later }
  2519. expectloc:=LOC_REGISTER;
  2520. end
  2521. else if is_ansistring(ld) then
  2522. begin
  2523. { this is only for add, the comparisaion is handled later }
  2524. expectloc:=LOC_REGISTER;
  2525. end
  2526. else if is_longstring(ld) then
  2527. begin
  2528. { this is only for add, the comparisaion is handled later }
  2529. expectloc:=LOC_REFERENCE;
  2530. end
  2531. else
  2532. begin
  2533. {$ifdef addstringopt}
  2534. { can create a call which isn't handled by callparatemp }
  2535. if canbeaddsstringcharoptnode(self) then
  2536. begin
  2537. hp := genaddsstringcharoptnode(self);
  2538. pass_1 := hp;
  2539. exit;
  2540. end
  2541. else
  2542. {$endif addstringopt}
  2543. begin
  2544. { Fix right to be shortstring }
  2545. if is_char(right.resultdef) then
  2546. begin
  2547. inserttypeconv(right,cshortstringtype);
  2548. firstpass(right);
  2549. end;
  2550. end;
  2551. {$ifdef addstringopt}
  2552. { can create a call which isn't handled by callparatemp }
  2553. if canbeaddsstringcsstringoptnode(self) then
  2554. begin
  2555. hp := genaddsstringcsstringoptnode(self);
  2556. pass_1 := hp;
  2557. exit;
  2558. end;
  2559. {$endif addstringopt}
  2560. end;
  2561. { otherwise, let addstring convert everything }
  2562. result := first_addstring;
  2563. exit;
  2564. end
  2565. { is one a real float ? }
  2566. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  2567. begin
  2568. {$ifdef cpufpemu}
  2569. if (current_settings.fputype=fpu_soft) or (cs_fp_emulation in current_settings.moduleswitches) then
  2570. begin
  2571. result:=first_addfloat;
  2572. if assigned(result) then
  2573. exit;
  2574. end;
  2575. {$endif cpufpemu}
  2576. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2577. expectloc:=LOC_FPUREGISTER
  2578. else
  2579. expectloc:=LOC_FLAGS;
  2580. calcregisters(self,0,1,0);
  2581. { an add node always first loads both the left and the }
  2582. { right in the fpu before doing the calculation. However, }
  2583. { calcregisters(0,2,0) will overestimate the number of }
  2584. { necessary registers (it will make it 3 in case one of }
  2585. { the operands is already in the fpu) (JM) }
  2586. if ((left.expectloc<>LOC_FPUREGISTER) or
  2587. (right.expectloc<>LOC_FPUREGISTER)) and
  2588. (registersfpu < 2) then
  2589. inc(registersfpu);
  2590. end
  2591. { pointer comperation and subtraction }
  2592. else if (ld.typ=pointerdef) then
  2593. begin
  2594. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2595. expectloc:=LOC_REGISTER
  2596. else
  2597. expectloc:=LOC_FLAGS;
  2598. calcregisters(self,1,0,0);
  2599. end
  2600. else if is_class_or_interface(ld) then
  2601. begin
  2602. expectloc:=LOC_FLAGS;
  2603. calcregisters(self,1,0,0);
  2604. end
  2605. else if (ld.typ=classrefdef) then
  2606. begin
  2607. expectloc:=LOC_FLAGS;
  2608. calcregisters(self,1,0,0);
  2609. end
  2610. { support procvar=nil,procvar<>nil }
  2611. else if ((ld.typ=procvardef) and (rt=niln)) or
  2612. ((rd.typ=procvardef) and (lt=niln)) then
  2613. begin
  2614. expectloc:=LOC_FLAGS;
  2615. calcregisters(self,1,0,0);
  2616. end
  2617. {$ifdef SUPPORT_MMX}
  2618. { mmx support, this must be before the zero based array
  2619. check }
  2620. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  2621. is_mmx_able_array(rd) then
  2622. begin
  2623. expectloc:=LOC_MMXREGISTER;
  2624. calcregisters(self,0,0,1);
  2625. end
  2626. {$endif SUPPORT_MMX}
  2627. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  2628. begin
  2629. expectloc:=LOC_REGISTER;
  2630. calcregisters(self,1,0,0);
  2631. end
  2632. else if (rd.typ=procvardef) and
  2633. (ld.typ=procvardef) and
  2634. equal_defs(rd,ld) then
  2635. begin
  2636. expectloc:=LOC_FLAGS;
  2637. calcregisters(self,1,0,0);
  2638. end
  2639. else if (ld.typ=enumdef) then
  2640. begin
  2641. expectloc:=LOC_FLAGS;
  2642. calcregisters(self,1,0,0);
  2643. end
  2644. {$ifdef SUPPORT_MMX}
  2645. else if (cs_mmx in current_settings.localswitches) and
  2646. is_mmx_able_array(ld) and
  2647. is_mmx_able_array(rd) then
  2648. begin
  2649. expectloc:=LOC_MMXREGISTER;
  2650. calcregisters(self,0,0,1);
  2651. end
  2652. {$endif SUPPORT_MMX}
  2653. { the general solution is to convert to 32 bit int }
  2654. else
  2655. begin
  2656. expectloc:=LOC_REGISTER;
  2657. calcregisters(self,1,0,0);
  2658. end;
  2659. end;
  2660. {$ifdef state_tracking}
  2661. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2662. var factval:Tnode;
  2663. begin
  2664. track_state_pass:=false;
  2665. if left.track_state_pass(exec_known) then
  2666. begin
  2667. track_state_pass:=true;
  2668. left.resultdef:=nil;
  2669. do_typecheckpass(left);
  2670. end;
  2671. factval:=aktstate.find_fact(left);
  2672. if factval<>nil then
  2673. begin
  2674. track_state_pass:=true;
  2675. left.destroy;
  2676. left:=factval.getcopy;
  2677. end;
  2678. if right.track_state_pass(exec_known) then
  2679. begin
  2680. track_state_pass:=true;
  2681. right.resultdef:=nil;
  2682. do_typecheckpass(right);
  2683. end;
  2684. factval:=aktstate.find_fact(right);
  2685. if factval<>nil then
  2686. begin
  2687. track_state_pass:=true;
  2688. right.destroy;
  2689. right:=factval.getcopy;
  2690. end;
  2691. end;
  2692. {$endif}
  2693. begin
  2694. caddnode:=taddnode;
  2695. end.