nadd.pas 114 KB

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