nadd.pas 111 KB

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