nadd.pas 111 KB

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