nadd.pas 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759
  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. else if not(nodetype in [ltn,lten,gtn,gten,unequaln,equaln]) then
  916. begin
  917. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  918. result:=cnothingnode.create;
  919. exit;
  920. end;
  921. end
  922. { There is a widechar? }
  923. else if is_widechar(rd) or is_widechar(ld) then
  924. begin
  925. { widechar+widechar gives widestring }
  926. if nodetype=addn then
  927. begin
  928. inserttypeconv(left,cwidestringtype);
  929. if (torddef(rd).ordtype<>uwidechar) then
  930. inserttypeconv(right,cwidechartype);
  931. resultdef:=cwidestringtype;
  932. end
  933. else
  934. begin
  935. if (torddef(ld).ordtype<>uwidechar) then
  936. inserttypeconv(left,cwidechartype);
  937. if (torddef(rd).ordtype<>uwidechar) then
  938. inserttypeconv(right,cwidechartype);
  939. end;
  940. end
  941. { is there a currency type ? }
  942. else if ((torddef(rd).ordtype=scurrency) or (torddef(ld).ordtype=scurrency)) then
  943. begin
  944. if (torddef(ld).ordtype<>scurrency) then
  945. inserttypeconv(left,s64currencytype);
  946. if (torddef(rd).ordtype<>scurrency) then
  947. inserttypeconv(right,s64currencytype);
  948. end
  949. { and,or,xor work on bit patterns and don't care
  950. about the sign of integers }
  951. { compares don't need extension to native int size either }
  952. { as long as both values are signed or unsigned }
  953. else if is_integer(ld) and is_integer(rd) and
  954. ((nodetype in [andn,orn,xorn]) or
  955. ((nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  956. not(is_signed(ld) xor is_signed(rd)))) then
  957. begin
  958. if rd.size>ld.size then
  959. inserttypeconv_internal(left,right.resultdef)
  960. else
  961. inserttypeconv_internal(right,left.resultdef);
  962. end
  963. { is there a signed 64 bit type ? }
  964. else if ((torddef(rd).ordtype=s64bit) or (torddef(ld).ordtype=s64bit)) then
  965. begin
  966. if (torddef(ld).ordtype<>s64bit) then
  967. inserttypeconv(left,s64inttype);
  968. if (torddef(rd).ordtype<>s64bit) then
  969. inserttypeconv(right,s64inttype);
  970. end
  971. { is there a unsigned 64 bit type ? }
  972. else if ((torddef(rd).ordtype=u64bit) or (torddef(ld).ordtype=u64bit)) then
  973. begin
  974. if (torddef(ld).ordtype<>u64bit) then
  975. inserttypeconv(left,u64inttype);
  976. if (torddef(rd).ordtype<>u64bit) then
  977. inserttypeconv(right,u64inttype);
  978. end
  979. { 64 bit cpus do calculations always in 64 bit }
  980. {$ifndef cpu64bit}
  981. { is there a cardinal? }
  982. else if ((torddef(rd).ordtype=u32bit) or (torddef(ld).ordtype=u32bit)) then
  983. begin
  984. { convert positive constants to u32bit }
  985. if (torddef(ld).ordtype<>u32bit) and
  986. is_constintnode(left) and
  987. (tordconstnode(left).value >= 0) then
  988. inserttypeconv(left,u32inttype);
  989. if (torddef(rd).ordtype<>u32bit) and
  990. is_constintnode(right) and
  991. (tordconstnode(right).value >= 0) then
  992. inserttypeconv(right,u32inttype);
  993. { when one of the operand is signed or the operation is subn then perform
  994. the operation in 64bit, can't use rd/ld here because there
  995. could be already typeconvs inserted.
  996. This is compatible with the code below for other unsigned types (PFV) }
  997. if is_signed(left.resultdef) or
  998. is_signed(right.resultdef) or
  999. (nodetype=subn) then
  1000. begin
  1001. if nodetype<>subn then
  1002. CGMessage(type_w_mixed_signed_unsigned);
  1003. inserttypeconv(left,s64inttype);
  1004. inserttypeconv(right,s64inttype);
  1005. end
  1006. else
  1007. begin
  1008. if (torddef(left.resultdef).ordtype<>u32bit) then
  1009. inserttypeconv(left,u32inttype);
  1010. if (torddef(right.resultdef).ordtype<>u32bit) then
  1011. inserttypeconv(right,u32inttype);
  1012. end;
  1013. end
  1014. {$endif cpu64bit}
  1015. { generic ord conversion is sinttype }
  1016. else
  1017. begin
  1018. { if the left or right value is smaller than the normal
  1019. type sinttype and is unsigned, and the other value
  1020. is a constant < 0, the result will always be false/true
  1021. for equal / unequal nodes.
  1022. }
  1023. if (
  1024. { left : unsigned ordinal var, right : < 0 constant }
  1025. (
  1026. ((is_signed(ld)=false) and (is_constintnode(left) =false)) and
  1027. ((is_constintnode(right)) and (tordconstnode(right).value < 0))
  1028. ) or
  1029. { right : unsigned ordinal var, left : < 0 constant }
  1030. (
  1031. ((is_signed(rd)=false) and (is_constintnode(right) =false)) and
  1032. ((is_constintnode(left)) and (tordconstnode(left).value < 0))
  1033. )
  1034. ) then
  1035. begin
  1036. if nodetype = equaln then
  1037. CGMessage(type_w_signed_unsigned_always_false)
  1038. else
  1039. if nodetype = unequaln then
  1040. CGMessage(type_w_signed_unsigned_always_true)
  1041. else
  1042. if (is_constintnode(left) and (nodetype in [ltn,lten])) or
  1043. (is_constintnode(right) and (nodetype in [gtn,gten])) then
  1044. CGMessage(type_w_signed_unsigned_always_true)
  1045. else
  1046. if (is_constintnode(right) and (nodetype in [ltn,lten])) or
  1047. (is_constintnode(left) and (nodetype in [gtn,gten])) then
  1048. CGMessage(type_w_signed_unsigned_always_false);
  1049. end;
  1050. { When there is a signed type or there is a minus operation
  1051. we convert to signed int. Otherwise (both are unsigned) we keep
  1052. the result also unsigned. This is compatible with Delphi (PFV) }
  1053. if is_signed(ld) or
  1054. is_signed(rd) or
  1055. (nodetype=subn) then
  1056. begin
  1057. inserttypeconv(right,sinttype);
  1058. inserttypeconv(left,sinttype);
  1059. end
  1060. else
  1061. begin
  1062. inserttypeconv(right,uinttype);
  1063. inserttypeconv(left,uinttype);
  1064. end;
  1065. end;
  1066. end
  1067. { if both are floatdefs, conversion is already done before constant folding }
  1068. else if (ld.typ=floatdef) then
  1069. begin
  1070. if not(nodetype in [addn,subn,muln,slashn,equaln,unequaln,ltn,lten,gtn,gten]) then
  1071. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1072. end
  1073. { left side a setdef, must be before string processing,
  1074. else array constructor can be seen as array of char (PFV) }
  1075. else if (ld.typ=setdef) then
  1076. begin
  1077. { trying to add a set element? }
  1078. if (nodetype=addn) and (rd.typ<>setdef) then
  1079. begin
  1080. if (rt=setelementn) then
  1081. begin
  1082. if not(equal_defs(tsetdef(ld).elementdef,rd)) then
  1083. inserttypeconv(right,tsetdef(ld).elementdef);
  1084. end
  1085. else
  1086. CGMessage(type_e_mismatch);
  1087. { ranges require normsets on big endian system }
  1088. if (target_info.endian=endian_big) and
  1089. (tsetdef(ld).size<>32) and
  1090. (rt=setelementn) and
  1091. assigned(tsetelementnode(right).right) then
  1092. begin
  1093. { generate a temporary normset def, it'll be destroyed
  1094. when the symtable is unloaded }
  1095. inserttypeconv(left,tsetdef.create(tsetdef(ld).elementdef,255));
  1096. end;
  1097. end
  1098. else
  1099. begin
  1100. if not(nodetype in [addn,subn,symdifn,muln,equaln,unequaln,lten,gten]) then
  1101. CGMessage(type_e_set_operation_unknown);
  1102. { make operands the same setdef, if right is a normalset or varset then
  1103. force the left side to be the same. General fallback also for non-set nodes
  1104. is to convert right to a set }
  1105. if not(equal_defs(ld,rd)) then
  1106. begin
  1107. if is_varset(rd) or is_normalset(rd) then
  1108. inserttypeconv(left,right.resultdef)
  1109. else
  1110. inserttypeconv(right,left.resultdef);
  1111. end;
  1112. end;
  1113. end
  1114. { pointer comparision and subtraction }
  1115. else if (
  1116. (rd.typ=pointerdef) and (ld.typ=pointerdef)
  1117. ) or
  1118. { compare/add pchar to variable (not stringconst) char arrays
  1119. by addresses like BP/Delphi }
  1120. (
  1121. (nodetype in [equaln,unequaln,subn,addn]) and
  1122. (
  1123. ((is_pchar(ld) or (lt=niln)) and is_chararray(rd) and (rt<>stringconstn)) or
  1124. ((is_pchar(rd) or (rt=niln)) and is_chararray(ld) and (lt<>stringconstn))
  1125. )
  1126. ) then
  1127. begin
  1128. { convert char array to pointer }
  1129. if is_chararray(rd) then
  1130. begin
  1131. inserttypeconv(right,charpointertype);
  1132. rd:=right.resultdef;
  1133. end
  1134. else if is_chararray(ld) then
  1135. begin
  1136. inserttypeconv(left,charpointertype);
  1137. ld:=left.resultdef;
  1138. end;
  1139. case nodetype of
  1140. equaln,unequaln :
  1141. begin
  1142. if is_voidpointer(right.resultdef) then
  1143. inserttypeconv(right,left.resultdef)
  1144. else if is_voidpointer(left.resultdef) then
  1145. inserttypeconv(left,right.resultdef)
  1146. else if not(equal_defs(ld,rd)) then
  1147. IncompatibleTypes(ld,rd);
  1148. { now that the type checking is done, convert both to charpointer, }
  1149. { because methodpointers are 8 bytes even though only the first 4 }
  1150. { bytes must be compared. This can happen here if we are in }
  1151. { TP/Delphi mode, because there @methodpointer = voidpointer (but }
  1152. { a voidpointer of 8 bytes). A conversion to voidpointer would be }
  1153. { optimized away, since the result already was a voidpointer, so }
  1154. { use a charpointer instead (JM) }
  1155. inserttypeconv_internal(left,charpointertype);
  1156. inserttypeconv_internal(right,charpointertype);
  1157. end;
  1158. ltn,lten,gtn,gten:
  1159. begin
  1160. if (cs_extsyntax in current_settings.moduleswitches) then
  1161. begin
  1162. if is_voidpointer(right.resultdef) then
  1163. inserttypeconv(right,left.resultdef)
  1164. else if is_voidpointer(left.resultdef) then
  1165. inserttypeconv(left,right.resultdef)
  1166. else if not(equal_defs(ld,rd)) then
  1167. IncompatibleTypes(ld,rd);
  1168. end
  1169. else
  1170. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1171. end;
  1172. subn:
  1173. begin
  1174. if (cs_extsyntax in current_settings.moduleswitches) then
  1175. begin
  1176. if is_voidpointer(right.resultdef) then
  1177. begin
  1178. if is_big_untyped_addrnode(right) then
  1179. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1180. inserttypeconv(right,left.resultdef)
  1181. end
  1182. else if is_voidpointer(left.resultdef) then
  1183. inserttypeconv(left,right.resultdef)
  1184. else if not(equal_defs(ld,rd)) then
  1185. IncompatibleTypes(ld,rd);
  1186. end
  1187. else
  1188. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1189. if not(nf_has_pointerdiv in flags) and
  1190. (tpointerdef(rd).pointeddef.size>1) then
  1191. begin
  1192. hp:=getcopy;
  1193. include(hp.flags,nf_has_pointerdiv);
  1194. result:=cmoddivnode.create(divn,hp,cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,false));
  1195. end;
  1196. resultdef:=sinttype;
  1197. exit;
  1198. end;
  1199. else
  1200. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1201. end;
  1202. end
  1203. { is one of the operands a string?,
  1204. chararrays are also handled as strings (after conversion), also take
  1205. care of chararray+chararray and chararray+char.
  1206. Note: Must be done after pointerdef+pointerdef has been checked, else
  1207. pchar is converted to string }
  1208. else if (rd.typ=stringdef) or
  1209. (ld.typ=stringdef) or
  1210. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1211. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1212. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1213. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1214. begin
  1215. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1216. begin
  1217. { Is there a widestring? }
  1218. if is_widestring(rd) or is_widestring(ld) or
  1219. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1220. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1221. strtype:= st_widestring
  1222. else
  1223. if is_ansistring(rd) or is_ansistring(ld) or
  1224. ((cs_ansistrings in current_settings.localswitches) and
  1225. //todo: Move some of this to longstring's then they are implemented?
  1226. (
  1227. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or
  1228. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld)
  1229. )
  1230. ) then
  1231. strtype:= st_ansistring
  1232. else
  1233. if is_longstring(rd) or is_longstring(ld) then
  1234. strtype:= st_longstring
  1235. else
  1236. begin
  1237. {$warning todo: add a warning/hint here if one converting a too large array}
  1238. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1239. Note: Delphi halts with error if "array [0..xx] of char"
  1240. is assigned to ShortString and string length is less
  1241. then array size }
  1242. strtype:= st_shortstring;
  1243. end;
  1244. // Now convert nodes to common string type
  1245. case strtype of
  1246. st_widestring :
  1247. begin
  1248. if not(is_widestring(rd)) then
  1249. inserttypeconv(right,cwidestringtype);
  1250. if not(is_widestring(ld)) then
  1251. inserttypeconv(left,cwidestringtype);
  1252. end;
  1253. st_ansistring :
  1254. begin
  1255. if not(is_ansistring(rd)) then
  1256. inserttypeconv(right,cansistringtype);
  1257. if not(is_ansistring(ld)) then
  1258. inserttypeconv(left,cansistringtype);
  1259. end;
  1260. st_longstring :
  1261. begin
  1262. if not(is_longstring(rd)) then
  1263. inserttypeconv(right,clongstringtype);
  1264. if not(is_longstring(ld)) then
  1265. inserttypeconv(left,clongstringtype);
  1266. end;
  1267. st_shortstring :
  1268. begin
  1269. if not(is_shortstring(ld)) then
  1270. inserttypeconv(left,cshortstringtype);
  1271. { don't convert char, that can be handled by the optimized node }
  1272. if not(is_shortstring(rd) or is_char(rd)) then
  1273. inserttypeconv(right,cshortstringtype);
  1274. end;
  1275. else
  1276. internalerror(2005101);
  1277. end;
  1278. end
  1279. else
  1280. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1281. end
  1282. { class or interface equation }
  1283. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  1284. begin
  1285. if (nodetype in [equaln,unequaln]) then
  1286. begin
  1287. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  1288. begin
  1289. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1290. inserttypeconv(right,left.resultdef)
  1291. else
  1292. inserttypeconv(left,right.resultdef);
  1293. end
  1294. else if is_class_or_interface(rd) then
  1295. inserttypeconv(left,right.resultdef)
  1296. else
  1297. inserttypeconv(right,left.resultdef);
  1298. end
  1299. else
  1300. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1301. end
  1302. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  1303. begin
  1304. if (nodetype in [equaln,unequaln]) then
  1305. begin
  1306. if tobjectdef(tclassrefdef(rd).pointeddef).is_related(
  1307. tobjectdef(tclassrefdef(ld).pointeddef)) then
  1308. inserttypeconv(right,left.resultdef)
  1309. else
  1310. inserttypeconv(left,right.resultdef);
  1311. end
  1312. else
  1313. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1314. end
  1315. { allows comperasion with nil pointer }
  1316. else if is_class_or_interface(rd) or (rd.typ=classrefdef) then
  1317. begin
  1318. if (nodetype in [equaln,unequaln]) then
  1319. inserttypeconv(left,right.resultdef)
  1320. else
  1321. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1322. end
  1323. else if is_class_or_interface(ld) or (ld.typ=classrefdef) then
  1324. begin
  1325. if (nodetype in [equaln,unequaln]) then
  1326. inserttypeconv(right,left.resultdef)
  1327. else
  1328. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1329. end
  1330. { support procvar=nil,procvar<>nil }
  1331. else if ((ld.typ=procvardef) and (rt=niln)) or
  1332. ((rd.typ=procvardef) and (lt=niln)) then
  1333. begin
  1334. if not(nodetype in [equaln,unequaln]) then
  1335. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1336. { find proc field in methodpointer record }
  1337. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1338. if not assigned(hsym) then
  1339. internalerror(200412043);
  1340. { For methodpointers compare only tmethodpointer.proc }
  1341. if (rd.typ=procvardef) and
  1342. (not tprocvardef(rd).is_addressonly) then
  1343. begin
  1344. right:=csubscriptnode.create(
  1345. hsym,
  1346. ctypeconvnode.create_internal(right,methodpointertype));
  1347. typecheckpass(right);
  1348. end;
  1349. if (ld.typ=procvardef) and
  1350. (not tprocvardef(ld).is_addressonly) then
  1351. begin
  1352. left:=csubscriptnode.create(
  1353. hsym,
  1354. ctypeconvnode.create_internal(left,methodpointertype));
  1355. typecheckpass(left);
  1356. end;
  1357. end
  1358. { support dynamicarray=nil,dynamicarray<>nil }
  1359. else if (is_dynamic_array(ld) and (rt=niln)) or
  1360. (is_dynamic_array(rd) and (lt=niln)) or
  1361. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1362. begin
  1363. if not(nodetype in [equaln,unequaln]) then
  1364. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1365. end
  1366. {$ifdef SUPPORT_MMX}
  1367. { mmx support, this must be before the zero based array
  1368. check }
  1369. else if (cs_mmx in current_settings.localswitches) and
  1370. is_mmx_able_array(ld) and
  1371. is_mmx_able_array(rd) and
  1372. equal_defs(ld,rd) then
  1373. begin
  1374. case nodetype of
  1375. addn,subn,xorn,orn,andn:
  1376. ;
  1377. { mul is a little bit restricted }
  1378. muln:
  1379. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1380. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1381. else
  1382. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1383. end;
  1384. end
  1385. {$endif SUPPORT_MMX}
  1386. { vector support, this must be before the zero based array
  1387. check }
  1388. else if (cs_support_vectors in current_settings.globalswitches) and
  1389. is_vector(ld) and
  1390. is_vector(rd) and
  1391. equal_defs(ld,rd) then
  1392. begin
  1393. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  1394. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1395. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  1396. resultdef:=left.resultdef;
  1397. end
  1398. { this is a little bit dangerous, also the left type }
  1399. { pointer to should be checked! This broke the mmx support }
  1400. else if (rd.typ=pointerdef) or
  1401. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  1402. begin
  1403. if is_zero_based_array(rd) then
  1404. begin
  1405. resultdef:=tpointerdef.create(tarraydef(rd).elementdef);
  1406. inserttypeconv(right,resultdef);
  1407. end
  1408. else
  1409. resultdef:=right.resultdef;
  1410. inserttypeconv(left,sinttype);
  1411. if nodetype=addn then
  1412. begin
  1413. if not(cs_extsyntax in current_settings.moduleswitches) or
  1414. (not(is_pchar(ld)) and not(m_add_pointer in current_settings.modeswitches)) then
  1415. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1416. if (rd.typ=pointerdef) and
  1417. (tpointerdef(rd).pointeddef.size>1) then
  1418. begin
  1419. left:=caddnode.create(muln,left,
  1420. cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,true));
  1421. typecheckpass(left);
  1422. end;
  1423. end
  1424. else
  1425. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1426. end
  1427. else if (ld.typ=pointerdef) or
  1428. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  1429. begin
  1430. if is_zero_based_array(ld) then
  1431. begin
  1432. resultdef:=tpointerdef.create(tarraydef(ld).elementdef);
  1433. inserttypeconv(left,resultdef);
  1434. end
  1435. else
  1436. resultdef:=left.resultdef;
  1437. inserttypeconv(right,sinttype);
  1438. if nodetype in [addn,subn] then
  1439. begin
  1440. if not(cs_extsyntax in current_settings.moduleswitches) or
  1441. (not(is_pchar(ld)) and not(m_add_pointer in current_settings.modeswitches)) then
  1442. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1443. if (ld.typ=pointerdef) then
  1444. begin
  1445. if is_big_untyped_addrnode(left) then
  1446. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1447. if (tpointerdef(ld).pointeddef.size>1) then
  1448. begin
  1449. right:=caddnode.create(muln,right,
  1450. cordconstnode.create(tpointerdef(ld).pointeddef.size,sinttype,true));
  1451. typecheckpass(right);
  1452. end
  1453. end else
  1454. if is_zero_based_array(ld) and
  1455. (tarraydef(ld).elementdef.size>1) then
  1456. begin
  1457. right:=caddnode.create(muln,right,
  1458. cordconstnode.create(tarraydef(ld).elementdef.size,sinttype,true));
  1459. typecheckpass(right);
  1460. end;
  1461. end
  1462. else
  1463. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1464. end
  1465. else if (rd.typ=procvardef) and
  1466. (ld.typ=procvardef) and
  1467. equal_defs(rd,ld) then
  1468. begin
  1469. if (nodetype in [equaln,unequaln]) then
  1470. begin
  1471. if tprocvardef(rd).is_addressonly then
  1472. begin
  1473. inserttypeconv_internal(right,voidpointertype);
  1474. inserttypeconv_internal(left,voidpointertype);
  1475. end
  1476. else
  1477. begin
  1478. { find proc field in methodpointer record }
  1479. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1480. if not assigned(hsym) then
  1481. internalerror(200412043);
  1482. { Compare tmehodpointer(left).proc }
  1483. right:=csubscriptnode.create(
  1484. hsym,
  1485. ctypeconvnode.create_internal(right,methodpointertype));
  1486. typecheckpass(right);
  1487. left:=csubscriptnode.create(
  1488. hsym,
  1489. ctypeconvnode.create_internal(left,methodpointertype));
  1490. typecheckpass(left);
  1491. end;
  1492. end
  1493. else
  1494. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1495. end
  1496. { enums }
  1497. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  1498. begin
  1499. if allowenumop(nodetype) then
  1500. inserttypeconv(right,left.resultdef)
  1501. else
  1502. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1503. end
  1504. { generic conversion, this is for error recovery }
  1505. else
  1506. begin
  1507. inserttypeconv(left,sinttype);
  1508. inserttypeconv(right,sinttype);
  1509. end;
  1510. { set resultdef if not already done }
  1511. if not assigned(resultdef) then
  1512. begin
  1513. case nodetype of
  1514. ltn,lten,gtn,gten,equaln,unequaln :
  1515. resultdef:=booltype;
  1516. slashn :
  1517. resultdef:=resultrealdef;
  1518. addn:
  1519. begin
  1520. { for strings, return is always a 255 char string }
  1521. if is_shortstring(left.resultdef) then
  1522. resultdef:=cshortstringtype
  1523. else
  1524. resultdef:=left.resultdef;
  1525. end;
  1526. else
  1527. resultdef:=left.resultdef;
  1528. end;
  1529. end;
  1530. { when the result is currency we need some extra code for
  1531. multiplication and division. this should not be done when
  1532. the muln or slashn node is created internally }
  1533. if not(nf_is_currency in flags) and
  1534. is_currency(resultdef) then
  1535. begin
  1536. case nodetype of
  1537. slashn :
  1538. begin
  1539. { slashn will only work with floats }
  1540. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1541. include(hp.flags,nf_is_currency);
  1542. result:=hp;
  1543. end;
  1544. muln :
  1545. begin
  1546. if s64currencytype.typ=floatdef then
  1547. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1548. else
  1549. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1550. include(hp.flags,nf_is_currency);
  1551. result:=hp
  1552. end;
  1553. end;
  1554. end;
  1555. end;
  1556. function taddnode.first_addstring: tnode;
  1557. const
  1558. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  1559. var
  1560. p: tnode;
  1561. newstatement : tstatementnode;
  1562. tempnode,tempnode2 : ttempcreatenode;
  1563. cmpfuncname: string;
  1564. begin
  1565. { when we get here, we are sure that both the left and the right }
  1566. { node are both strings of the same stringtype (JM) }
  1567. case nodetype of
  1568. addn:
  1569. begin
  1570. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  1571. begin
  1572. result:=right;
  1573. left.free;
  1574. left:=nil;
  1575. right:=nil;
  1576. exit;
  1577. end;
  1578. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  1579. begin
  1580. result:=left;
  1581. left:=nil;
  1582. right.free;
  1583. right:=nil;
  1584. exit;
  1585. end;
  1586. { create the call to the concat routine both strings as arguments }
  1587. if assigned(aktassignmentnode) and
  1588. (aktassignmentnode.right=self) and
  1589. (aktassignmentnode.left.resultdef=resultdef) and
  1590. valid_for_var(aktassignmentnode.left,false) then
  1591. begin
  1592. result:=ccallnode.createintern('fpc_'+
  1593. tstringdef(resultdef).stringtypname+'_concat',
  1594. ccallparanode.create(right,
  1595. ccallparanode.create(left,
  1596. ccallparanode.create(aktassignmentnode.left.getcopy,nil))));
  1597. include(aktassignmentnode.flags,nf_assign_done_in_right);
  1598. firstpass(result);
  1599. end
  1600. else
  1601. begin
  1602. result:=internalstatements(newstatement);
  1603. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1604. addstatement(newstatement,tempnode);
  1605. addstatement(newstatement,ccallnode.createintern('fpc_'+
  1606. tstringdef(resultdef).stringtypname+'_concat',
  1607. ccallparanode.create(right,
  1608. ccallparanode.create(left,
  1609. ccallparanode.create(ctemprefnode.create(tempnode),nil)))));
  1610. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1611. addstatement(newstatement,ctemprefnode.create(tempnode));
  1612. end;
  1613. { we reused the arguments }
  1614. left := nil;
  1615. right := nil;
  1616. end;
  1617. ltn,lten,gtn,gten,equaln,unequaln :
  1618. begin
  1619. { generate better code for comparison with empty string, we
  1620. only need to compare the length with 0 }
  1621. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1622. { windows widestrings are too complicated to be handled optimized }
  1623. not(is_widestring(left.resultdef) and (target_info.system in system_windows)) and
  1624. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  1625. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  1626. begin
  1627. { switch so that the constant is always on the right }
  1628. if left.nodetype = stringconstn then
  1629. begin
  1630. p := left;
  1631. left := right;
  1632. right := p;
  1633. nodetype:=swap_relation[nodetype];
  1634. end;
  1635. if is_shortstring(left.resultdef) or
  1636. (nodetype in [gtn,gten,ltn,lten]) then
  1637. { compare the length with 0 }
  1638. result := caddnode.create(nodetype,
  1639. cinlinenode.create(in_length_x,false,left),
  1640. cordconstnode.create(0,s32inttype,false))
  1641. else
  1642. begin
  1643. (*
  1644. if is_widestring(left.resultdef) and
  1645. (target_info.system in system_windows) then
  1646. begin
  1647. { windows like widestrings requires that we also check the length }
  1648. result:=internalstatements(newstatement);
  1649. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1650. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1651. addstatement(newstatement,tempnode);
  1652. addstatement(newstatement,tempnode2);
  1653. { poor man's cse }
  1654. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1655. ctypeconvnode.create_internal(left,voidpointertype))
  1656. );
  1657. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  1658. caddnode.create(orn,
  1659. caddnode.create(nodetype,
  1660. ctemprefnode.create(tempnode),
  1661. cpointerconstnode.create(0,voidpointertype)
  1662. ),
  1663. caddnode.create(nodetype,
  1664. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  1665. cordconstnode.create(0,s32inttype,false)
  1666. )
  1667. )
  1668. ));
  1669. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1670. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  1671. addstatement(newstatement,ctemprefnode.create(tempnode2));
  1672. end
  1673. else
  1674. *)
  1675. begin
  1676. { compare the pointer with nil (for ansistrings etc), }
  1677. { faster than getting the length (JM) }
  1678. result:= caddnode.create(nodetype,
  1679. ctypeconvnode.create_internal(left,voidpointertype),
  1680. cpointerconstnode.create(0,voidpointertype));
  1681. end;
  1682. end;
  1683. { left is reused }
  1684. left := nil;
  1685. { right isn't }
  1686. right.free;
  1687. right := nil;
  1688. exit;
  1689. end;
  1690. { no string constant -> call compare routine }
  1691. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  1692. { for equality checks use optimized version }
  1693. if nodetype in [equaln,unequaln] then
  1694. cmpfuncname := cmpfuncname + '_equal';
  1695. result := ccallnode.createintern(cmpfuncname,
  1696. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1697. { and compare its result with 0 according to the original operator }
  1698. result := caddnode.create(nodetype,result,
  1699. cordconstnode.create(0,s32inttype,false));
  1700. left := nil;
  1701. right := nil;
  1702. end;
  1703. end;
  1704. end;
  1705. function taddnode.first_addset : tnode;
  1706. procedure call_varset_helper(const n : string);
  1707. var
  1708. newstatement : tstatementnode;
  1709. temp : ttempcreatenode;
  1710. begin
  1711. { add two var sets }
  1712. result:=internalstatements(newstatement);
  1713. { create temp for result }
  1714. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1715. addstatement(newstatement,temp);
  1716. addstatement(newstatement,ccallnode.createintern(n,
  1717. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1718. ccallparanode.create(ctemprefnode.create(temp),
  1719. ccallparanode.create(right,
  1720. ccallparanode.create(left,nil)))))
  1721. );
  1722. { remove reused parts from original node }
  1723. left:=nil;
  1724. right:=nil;
  1725. { the last statement should return the value as
  1726. location and type, this is done be referencing the
  1727. temp and converting it first from a persistent temp to
  1728. normal temp }
  1729. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1730. addstatement(newstatement,ctemprefnode.create(temp));
  1731. end;
  1732. var
  1733. procname: string[31];
  1734. tempn: tnode;
  1735. paras: tcallparanode;
  1736. srsym: ttypesym;
  1737. newstatement : tstatementnode;
  1738. temp : ttempcreatenode;
  1739. begin
  1740. if (is_varset(left.resultdef) or is_varset(right.resultdef)) and
  1741. not(is_normalset(left.resultdef)) and
  1742. not(is_normalset(right.resultdef)) then
  1743. begin
  1744. case nodetype of
  1745. equaln,unequaln,lten,gten:
  1746. begin
  1747. case nodetype of
  1748. equaln,unequaln:
  1749. procname := 'fpc_varset_comp_sets';
  1750. lten,gten:
  1751. begin
  1752. procname := 'fpc_varset_contains_sets';
  1753. { (left >= right) = (right <= left) }
  1754. if nodetype = gten then
  1755. begin
  1756. tempn := left;
  1757. left := right;
  1758. right := tempn;
  1759. end;
  1760. end;
  1761. end;
  1762. result := ccallnode.createinternres(procname,
  1763. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  1764. ccallparanode.create(right,
  1765. ccallparanode.create(left,nil))),resultdef);
  1766. { left and right are reused as parameters }
  1767. left := nil;
  1768. right := nil;
  1769. { for an unequaln, we have to negate the result of comp_sets }
  1770. if nodetype = unequaln then
  1771. result := cnotnode.create(result);
  1772. end;
  1773. addn:
  1774. begin
  1775. { optimize first loading of a set }
  1776. if (right.nodetype=setelementn) and
  1777. not(assigned(tsetelementnode(right).right)) and
  1778. is_emptyset(left) then
  1779. begin
  1780. result:=internalstatements(newstatement);
  1781. { create temp for result }
  1782. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1783. addstatement(newstatement,temp);
  1784. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  1785. ccallparanode.create(ctemprefnode.create(temp),
  1786. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1787. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),nil))))
  1788. );
  1789. { the last statement should return the value as
  1790. location and type, this is done be referencing the
  1791. temp and converting it first from a persistent temp to
  1792. normal temp }
  1793. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1794. addstatement(newstatement,ctemprefnode.create(temp));
  1795. tsetelementnode(right).left := nil;
  1796. end
  1797. else
  1798. begin
  1799. if right.nodetype=setelementn then
  1800. begin
  1801. result:=internalstatements(newstatement);
  1802. { create temp for result }
  1803. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1804. addstatement(newstatement,temp);
  1805. { add a range or a single element? }
  1806. if assigned(tsetelementnode(right).right) then
  1807. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  1808. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1809. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  1810. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  1811. ccallparanode.create(ctemprefnode.create(temp),
  1812. ccallparanode.create(left,nil))))))
  1813. )
  1814. else
  1815. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  1816. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1817. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  1818. ccallparanode.create(ctemprefnode.create(temp),
  1819. ccallparanode.create(left,nil)))))
  1820. );
  1821. { remove reused parts from original node }
  1822. tsetelementnode(right).right:=nil;
  1823. tsetelementnode(right).left:=nil;
  1824. left:=nil;
  1825. { the last statement should return the value as
  1826. location and type, this is done be referencing the
  1827. temp and converting it first from a persistent temp to
  1828. normal temp }
  1829. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1830. addstatement(newstatement,ctemprefnode.create(temp));
  1831. end
  1832. else
  1833. call_varset_helper('fpc_varset_add_sets');
  1834. end
  1835. end;
  1836. subn:
  1837. call_varset_helper('fpc_varset_sub_sets');
  1838. symdifn:
  1839. call_varset_helper('fpc_varset_symdif_sets');
  1840. muln:
  1841. call_varset_helper('fpc_varset_mul_sets');
  1842. else
  1843. internalerror(200609241);
  1844. end;
  1845. end
  1846. else
  1847. begin
  1848. { get the sym that represents the fpc_normal_set type }
  1849. srsym:=search_system_type('FPC_NORMAL_SET');
  1850. case nodetype of
  1851. equaln,unequaln,lten,gten:
  1852. begin
  1853. case nodetype of
  1854. equaln,unequaln:
  1855. procname := 'fpc_set_comp_sets';
  1856. lten,gten:
  1857. begin
  1858. procname := 'fpc_set_contains_sets';
  1859. { (left >= right) = (right <= left) }
  1860. if nodetype = gten then
  1861. begin
  1862. tempn := left;
  1863. left := right;
  1864. right := tempn;
  1865. end;
  1866. end;
  1867. end;
  1868. { convert the arguments (explicitely) to fpc_normal_set's }
  1869. left := ctypeconvnode.create_internal(left,srsym.typedef);
  1870. right := ctypeconvnode.create_internal(right,srsym.typedef);
  1871. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1872. ccallparanode.create(left,nil)));
  1873. { left and right are reused as parameters }
  1874. left := nil;
  1875. right := nil;
  1876. { for an unequaln, we have to negate the result of comp_sets }
  1877. if nodetype = unequaln then
  1878. result := cnotnode.create(result);
  1879. end;
  1880. addn:
  1881. begin
  1882. { optimize first loading of a set }
  1883. if (right.nodetype=setelementn) and
  1884. not(assigned(tsetelementnode(right).right)) and
  1885. is_emptyset(left) then
  1886. begin
  1887. { type cast the value to pass as argument to a byte, }
  1888. { since that's what the helper expects }
  1889. tsetelementnode(right).left :=
  1890. ctypeconvnode.create_internal(tsetelementnode(right).left,u8inttype);
  1891. { set the resultdef to the actual one (otherwise it's }
  1892. { "fpc_normal_set") }
  1893. result := ccallnode.createinternres('fpc_set_create_element',
  1894. ccallparanode.create(tsetelementnode(right).left,nil),
  1895. resultdef);
  1896. { reused }
  1897. tsetelementnode(right).left := nil;
  1898. end
  1899. else
  1900. begin
  1901. if right.nodetype=setelementn then
  1902. begin
  1903. { convert the arguments to bytes, since that's what }
  1904. { the helper expects }
  1905. tsetelementnode(right).left :=
  1906. ctypeconvnode.create_internal(tsetelementnode(right).left,
  1907. u8inttype);
  1908. { convert the original set (explicitely) to an }
  1909. { fpc_normal_set so we can pass it to the helper }
  1910. left := ctypeconvnode.create_internal(left,srsym.typedef);
  1911. { add a range or a single element? }
  1912. if assigned(tsetelementnode(right).right) then
  1913. begin
  1914. tsetelementnode(right).right :=
  1915. ctypeconvnode.create_internal(tsetelementnode(right).right,
  1916. u8inttype);
  1917. { create the call }
  1918. result := ccallnode.createinternres('fpc_set_set_range',
  1919. ccallparanode.create(tsetelementnode(right).right,
  1920. ccallparanode.create(tsetelementnode(right).left,
  1921. ccallparanode.create(left,nil))),resultdef);
  1922. end
  1923. else
  1924. begin
  1925. result := ccallnode.createinternres('fpc_set_set_byte',
  1926. ccallparanode.create(tsetelementnode(right).left,
  1927. ccallparanode.create(left,nil)),resultdef);
  1928. end;
  1929. { remove reused parts from original node }
  1930. tsetelementnode(right).right := nil;
  1931. tsetelementnode(right).left := nil;
  1932. left := nil;
  1933. end
  1934. else
  1935. begin
  1936. { add two sets }
  1937. { convert the sets to fpc_normal_set's }
  1938. result := ccallnode.createinternres('fpc_set_add_sets',
  1939. ccallparanode.create(
  1940. ctypeconvnode.create_explicit(right,srsym.typedef),
  1941. ccallparanode.create(
  1942. ctypeconvnode.create_internal(left,srsym.typedef),nil)),resultdef);
  1943. { remove reused parts from original node }
  1944. left := nil;
  1945. right := nil;
  1946. end;
  1947. end
  1948. end;
  1949. subn,symdifn,muln:
  1950. begin
  1951. { convert the sets to fpc_normal_set's }
  1952. paras := ccallparanode.create(ctypeconvnode.create_internal(right,srsym.typedef),
  1953. ccallparanode.create(ctypeconvnode.create_internal(left,srsym.typedef),nil));
  1954. case nodetype of
  1955. subn:
  1956. result := ccallnode.createinternres('fpc_set_sub_sets',
  1957. paras,resultdef);
  1958. symdifn:
  1959. result := ccallnode.createinternres('fpc_set_symdif_sets',
  1960. paras,resultdef);
  1961. muln:
  1962. result := ccallnode.createinternres('fpc_set_mul_sets',
  1963. paras,resultdef);
  1964. end;
  1965. { remove reused parts from original node }
  1966. left := nil;
  1967. right := nil;
  1968. end;
  1969. else
  1970. internalerror(200108311);
  1971. end;
  1972. end;
  1973. end;
  1974. function taddnode.use_generic_mul32to64: boolean;
  1975. begin
  1976. result := true;
  1977. end;
  1978. function taddnode.try_make_mul32to64: boolean;
  1979. function canbe32bitint(v: tconstexprint; fromdef: torddef; todefsigned: boolean): boolean;
  1980. begin
  1981. if (fromdef.ordtype <> u64bit) then
  1982. result :=
  1983. ((v >= 0) or
  1984. todefsigned) and
  1985. (v >= low(longint)) and
  1986. (v <= high(longint))
  1987. else
  1988. result :=
  1989. (qword(v) >= low(cardinal)) and
  1990. (qword(v) <= high(cardinal))
  1991. end;
  1992. var
  1993. temp: tnode;
  1994. begin
  1995. result := false;
  1996. if ((left.nodetype = typeconvn) and
  1997. is_integer(ttypeconvnode(left).left.resultdef) and
  1998. (not(torddef(ttypeconvnode(left).left.resultdef).ordtype in [u64bit,s64bit])) and
  1999. (((right.nodetype = ordconstn) and
  2000. canbe32bitint(tordconstnode(right).value,torddef(right.resultdef),is_signed(left.resultdef))) or
  2001. ((right.nodetype = typeconvn) and
  2002. is_integer(ttypeconvnode(right).left.resultdef) and
  2003. not(torddef(ttypeconvnode(right).left.resultdef).ordtype in [u64bit,s64bit])) and
  2004. (is_signed(ttypeconvnode(left).left.resultdef) =
  2005. is_signed(ttypeconvnode(right).left.resultdef)))) then
  2006. begin
  2007. temp := ttypeconvnode(left).left;
  2008. ttypeconvnode(left).left := nil;
  2009. left.free;
  2010. left := temp;
  2011. if (right.nodetype = typeconvn) then
  2012. begin
  2013. temp := ttypeconvnode(right).left;
  2014. ttypeconvnode(right).left := nil;
  2015. right.free;
  2016. right := temp;
  2017. end;
  2018. if (is_signed(left.resultdef)) then
  2019. begin
  2020. inserttypeconv(left,s32inttype);
  2021. inserttypeconv(right,s32inttype);
  2022. end
  2023. else
  2024. begin
  2025. inserttypeconv(left,u32inttype);
  2026. inserttypeconv(right,u32inttype);
  2027. end;
  2028. firstpass(left);
  2029. firstpass(right);
  2030. result := true;
  2031. end;
  2032. end;
  2033. function taddnode.first_add64bitint: tnode;
  2034. var
  2035. procname: string[31];
  2036. temp: tnode;
  2037. power: longint;
  2038. begin
  2039. result := nil;
  2040. { create helper calls mul }
  2041. if nodetype <> muln then
  2042. exit;
  2043. { make sure that if there is a constant, that it's on the right }
  2044. if left.nodetype = ordconstn then
  2045. begin
  2046. temp := right;
  2047. right := left;
  2048. left := temp;
  2049. end;
  2050. { can we use a shift instead of a mul? }
  2051. if not (cs_check_overflow in current_settings.localswitches) and
  2052. (right.nodetype = ordconstn) and
  2053. ispowerof2(tordconstnode(right).value,power) then
  2054. begin
  2055. tordconstnode(right).value := power;
  2056. result := cshlshrnode.create(shln,left,right);
  2057. { left and right are reused }
  2058. left := nil;
  2059. right := nil;
  2060. { return firstpassed new node }
  2061. exit;
  2062. end;
  2063. if not(use_generic_mul32to64) and
  2064. try_make_mul32to64 then
  2065. exit;
  2066. { when currency is used set the result of the
  2067. parameters to s64bit, so they are not converted }
  2068. if is_currency(resultdef) then
  2069. begin
  2070. left.resultdef:=s64inttype;
  2071. right.resultdef:=s64inttype;
  2072. end;
  2073. { otherwise, create the parameters for the helper }
  2074. right := ccallparanode.create(
  2075. cordconstnode.create(ord(cs_check_overflow in current_settings.localswitches),booltype,true),
  2076. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2077. left := nil;
  2078. { only qword needs the unsigned code, the
  2079. signed code is also used for currency }
  2080. if is_signed(resultdef) then
  2081. procname := 'fpc_mul_int64'
  2082. else
  2083. procname := 'fpc_mul_qword';
  2084. result := ccallnode.createintern(procname,right);
  2085. right := nil;
  2086. end;
  2087. function taddnode.first_addfloat : tnode;
  2088. var
  2089. procname: string[31];
  2090. { do we need to reverse the result ? }
  2091. notnode : boolean;
  2092. fdef : tdef;
  2093. begin
  2094. result := nil;
  2095. notnode := false;
  2096. { In non-emulation mode, real opcodes are
  2097. emitted for floating point values.
  2098. }
  2099. if not (cs_fp_emulation in current_settings.moduleswitches) then
  2100. exit;
  2101. if not(target_info.system in system_wince) then
  2102. begin
  2103. case tfloatdef(left.resultdef).floattype of
  2104. s32real:
  2105. begin
  2106. fdef:=search_system_type('FLOAT32REC').typedef;
  2107. procname:='float32';
  2108. end;
  2109. s64real:
  2110. begin
  2111. fdef:=search_system_type('FLOAT64').typedef;
  2112. procname:='float64';
  2113. end;
  2114. {!!! not yet implemented
  2115. s128real:
  2116. }
  2117. else
  2118. internalerror(2005082601);
  2119. end;
  2120. case nodetype of
  2121. addn:
  2122. procname:=procname+'_add';
  2123. muln:
  2124. procname:=procname+'_mul';
  2125. subn:
  2126. procname:=procname+'_sub';
  2127. slashn:
  2128. procname:=procname+'_div';
  2129. ltn:
  2130. procname:=procname+'_lt';
  2131. lten:
  2132. procname:=procname+'_le';
  2133. gtn:
  2134. begin
  2135. procname:=procname+'_le';
  2136. notnode:=true;
  2137. end;
  2138. gten:
  2139. begin
  2140. procname:=procname+'_lt';
  2141. notnode:=true;
  2142. end;
  2143. equaln:
  2144. procname:=procname+'_eq';
  2145. unequaln:
  2146. begin
  2147. procname:=procname+'_eq';
  2148. notnode:=true;
  2149. end;
  2150. else
  2151. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2152. end;
  2153. end
  2154. else
  2155. begin
  2156. case nodetype of
  2157. addn:
  2158. procname:='ADD';
  2159. muln:
  2160. procname:='MUL';
  2161. subn:
  2162. procname:='SUB';
  2163. slashn:
  2164. procname:='DIV';
  2165. ltn:
  2166. procname:='LT';
  2167. lten:
  2168. procname:='LE';
  2169. gtn:
  2170. procname:='GT';
  2171. gten:
  2172. procname:='GE';
  2173. equaln:
  2174. procname:='EQ';
  2175. unequaln:
  2176. procname:='NE';
  2177. else
  2178. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2179. end;
  2180. case tfloatdef(left.resultdef).floattype of
  2181. s32real:
  2182. begin
  2183. procname:=procname+'S';
  2184. if nodetype in [addn,muln,subn,slashn] then
  2185. procname:=lower(procname);
  2186. end;
  2187. s64real:
  2188. procname:=procname+'D';
  2189. {!!! not yet implemented
  2190. s128real:
  2191. }
  2192. else
  2193. internalerror(2005082602);
  2194. end;
  2195. end;
  2196. { cast softfpu result? }
  2197. if not(target_info.system in system_wince) then
  2198. begin
  2199. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2200. resultdef:=booltype;
  2201. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  2202. ctypeconvnode.create_internal(right,fdef),
  2203. ccallparanode.create(
  2204. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  2205. end
  2206. else
  2207. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  2208. ccallparanode.create(left,nil)));
  2209. left:=nil;
  2210. right:=nil;
  2211. { do we need to reverse the result }
  2212. if notnode then
  2213. result:=cnotnode.create(result);
  2214. end;
  2215. function taddnode.pass_1 : tnode;
  2216. var
  2217. {$ifdef addstringopt}
  2218. hp : tnode;
  2219. {$endif addstringopt}
  2220. lt,rt : tnodetype;
  2221. rd,ld : tdef;
  2222. newstatement : tstatementnode;
  2223. temp : ttempcreatenode;
  2224. begin
  2225. result:=nil;
  2226. { Can we optimize multiple string additions into a single call?
  2227. This need to be done on a complete tree to detect the multiple
  2228. add nodes and is therefor done before the subtrees are processed }
  2229. if canbemultistringadd(self) then
  2230. begin
  2231. result := genmultistringadd(self);
  2232. exit;
  2233. end;
  2234. { first do the two subtrees }
  2235. firstpass(left);
  2236. firstpass(right);
  2237. if codegenerror then
  2238. exit;
  2239. { load easier access variables }
  2240. rd:=right.resultdef;
  2241. ld:=left.resultdef;
  2242. rt:=right.nodetype;
  2243. lt:=left.nodetype;
  2244. { int/int gives real/real! }
  2245. if nodetype=slashn then
  2246. begin
  2247. {$ifdef cpufpemu}
  2248. if (current_settings.fputype=fpu_soft) or (cs_fp_emulation in current_settings.moduleswitches) then
  2249. begin
  2250. result:=first_addfloat;
  2251. if assigned(result) then
  2252. exit;
  2253. end;
  2254. {$endif cpufpemu}
  2255. expectloc:=LOC_FPUREGISTER;
  2256. { maybe we need an integer register to save }
  2257. { a reference }
  2258. if ((left.expectloc<>LOC_FPUREGISTER) or
  2259. (right.expectloc<>LOC_FPUREGISTER)) and
  2260. (left.registersint=right.registersint) then
  2261. calcregisters(self,1,1,0)
  2262. else
  2263. calcregisters(self,0,1,0);
  2264. { an add node always first loads both the left and the }
  2265. { right in the fpu before doing the calculation. However, }
  2266. { calcregisters(0,2,0) will overestimate the number of }
  2267. { necessary registers (it will make it 3 in case one of }
  2268. { the operands is already in the fpu) (JM) }
  2269. if ((left.expectloc<>LOC_FPUREGISTER) or
  2270. (right.expectloc<>LOC_FPUREGISTER)) and
  2271. (registersfpu < 2) then
  2272. inc(registersfpu);
  2273. end
  2274. { if both are orddefs then check sub types }
  2275. else if (ld.typ=orddef) and (rd.typ=orddef) then
  2276. begin
  2277. { 2 booleans ? }
  2278. if is_boolean(ld) and is_boolean(rd) then
  2279. begin
  2280. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  2281. (nf_short_bool in flags)) and
  2282. (nodetype in [andn,orn]) then
  2283. begin
  2284. expectloc:=LOC_JUMP;
  2285. calcregisters(self,0,0,0);
  2286. end
  2287. else
  2288. begin
  2289. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2290. begin
  2291. expectloc:=LOC_FLAGS;
  2292. if (left.expectloc in [LOC_JUMP,LOC_FLAGS]) and
  2293. (left.expectloc in [LOC_JUMP,LOC_FLAGS]) then
  2294. calcregisters(self,2,0,0)
  2295. else
  2296. calcregisters(self,1,0,0);
  2297. end
  2298. else
  2299. begin
  2300. expectloc:=LOC_REGISTER;
  2301. calcregisters(self,0,0,0);
  2302. end;
  2303. end;
  2304. end
  2305. else
  2306. { Both are chars? only convert to shortstrings for addn }
  2307. if is_char(ld) then
  2308. begin
  2309. if nodetype=addn then
  2310. internalerror(200103291);
  2311. expectloc:=LOC_FLAGS;
  2312. calcregisters(self,1,0,0);
  2313. end
  2314. {$ifndef cpu64bit}
  2315. { is there a 64 bit type ? }
  2316. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  2317. begin
  2318. result := first_add64bitint;
  2319. if assigned(result) then
  2320. exit;
  2321. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2322. expectloc:=LOC_REGISTER
  2323. else
  2324. expectloc:=LOC_JUMP;
  2325. calcregisters(self,2,0,0)
  2326. end
  2327. {$endif cpu64bit}
  2328. { is there a cardinal? }
  2329. else if (torddef(ld).ordtype=u32bit) then
  2330. begin
  2331. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2332. expectloc:=LOC_REGISTER
  2333. else
  2334. expectloc:=LOC_FLAGS;
  2335. calcregisters(self,1,0,0);
  2336. { for unsigned mul we need an extra register }
  2337. if nodetype=muln then
  2338. inc(registersint);
  2339. end
  2340. { generic s32bit conversion }
  2341. else
  2342. begin
  2343. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2344. expectloc:=LOC_REGISTER
  2345. else
  2346. expectloc:=LOC_FLAGS;
  2347. calcregisters(self,1,0,0);
  2348. end;
  2349. end
  2350. { left side a setdef, must be before string processing,
  2351. else array constructor can be seen as array of char (PFV) }
  2352. else if (ld.typ=setdef) then
  2353. begin
  2354. if not(is_varset(ld)) and not(is_normalset(ld)) then
  2355. begin
  2356. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2357. expectloc:=LOC_FLAGS
  2358. else
  2359. expectloc:=LOC_REGISTER;
  2360. { are we adding set elements ? }
  2361. if right.nodetype=setelementn then
  2362. begin
  2363. { add range?
  2364. the smallset code can't handle set ranges }
  2365. if assigned(tsetelementnode(right).right) then
  2366. begin
  2367. result:=internalstatements(newstatement);
  2368. { create temp for result }
  2369. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2370. addstatement(newstatement,temp);
  2371. { add a range or a single element? }
  2372. if assigned(tsetelementnode(right).right) then
  2373. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  2374. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2375. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  2376. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2377. ccallparanode.create(ctemprefnode.create(temp),
  2378. ccallparanode.create(left,nil))))))
  2379. )
  2380. else
  2381. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  2382. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2383. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2384. ccallparanode.create(ctemprefnode.create(temp),
  2385. ccallparanode.create(left,nil)))))
  2386. );
  2387. { remove reused parts from original node }
  2388. tsetelementnode(right).right:=nil;
  2389. tsetelementnode(right).left:=nil;
  2390. left:=nil;
  2391. { the last statement should return the value as
  2392. location and type, this is done be referencing the
  2393. temp and converting it first from a persistent temp to
  2394. normal temp }
  2395. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2396. addstatement(newstatement,ctemprefnode.create(temp));
  2397. end
  2398. else
  2399. calcregisters(self,2,0,0)
  2400. end
  2401. else
  2402. calcregisters(self,1,0,0);
  2403. end
  2404. else
  2405. {$ifdef MMXSET}
  2406. {$ifdef i386}
  2407. if cs_mmx in current_settings.localswitches then
  2408. begin
  2409. expectloc:=LOC_MMXREGISTER;
  2410. calcregisters(self,0,0,4);
  2411. end
  2412. else
  2413. {$endif}
  2414. {$endif MMXSET}
  2415. begin
  2416. result := first_addset;
  2417. if assigned(result) then
  2418. exit;
  2419. expectloc:=LOC_CREFERENCE;
  2420. calcregisters(self,0,0,0);
  2421. { here we call SET... }
  2422. include(current_procinfo.flags,pi_do_call);
  2423. end;
  2424. end
  2425. { compare pchar by addresses like BP/Delphi }
  2426. else if is_pchar(ld) then
  2427. begin
  2428. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2429. expectloc:=LOC_REGISTER
  2430. else
  2431. expectloc:=LOC_FLAGS;
  2432. calcregisters(self,1,0,0);
  2433. end
  2434. { is one of the operands a string }
  2435. else if (ld.typ=stringdef) then
  2436. begin
  2437. if is_widestring(ld) then
  2438. begin
  2439. { this is only for add, the comparisaion is handled later }
  2440. expectloc:=LOC_REGISTER;
  2441. end
  2442. else if is_ansistring(ld) then
  2443. begin
  2444. { this is only for add, the comparisaion is handled later }
  2445. expectloc:=LOC_REGISTER;
  2446. end
  2447. else if is_longstring(ld) then
  2448. begin
  2449. { this is only for add, the comparisaion is handled later }
  2450. expectloc:=LOC_REFERENCE;
  2451. end
  2452. else
  2453. begin
  2454. {$ifdef addstringopt}
  2455. { can create a call which isn't handled by callparatemp }
  2456. if canbeaddsstringcharoptnode(self) then
  2457. begin
  2458. hp := genaddsstringcharoptnode(self);
  2459. pass_1 := hp;
  2460. exit;
  2461. end
  2462. else
  2463. {$endif addstringopt}
  2464. begin
  2465. { Fix right to be shortstring }
  2466. if is_char(right.resultdef) then
  2467. begin
  2468. inserttypeconv(right,cshortstringtype);
  2469. firstpass(right);
  2470. end;
  2471. end;
  2472. {$ifdef addstringopt}
  2473. { can create a call which isn't handled by callparatemp }
  2474. if canbeaddsstringcsstringoptnode(self) then
  2475. begin
  2476. hp := genaddsstringcsstringoptnode(self);
  2477. pass_1 := hp;
  2478. exit;
  2479. end;
  2480. {$endif addstringopt}
  2481. end;
  2482. { otherwise, let addstring convert everything }
  2483. result := first_addstring;
  2484. exit;
  2485. end
  2486. { is one a real float ? }
  2487. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  2488. begin
  2489. {$ifdef cpufpemu}
  2490. if (current_settings.fputype=fpu_soft) or (cs_fp_emulation in current_settings.moduleswitches) then
  2491. begin
  2492. result:=first_addfloat;
  2493. if assigned(result) then
  2494. exit;
  2495. end;
  2496. {$endif cpufpemu}
  2497. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2498. expectloc:=LOC_FPUREGISTER
  2499. else
  2500. expectloc:=LOC_FLAGS;
  2501. calcregisters(self,0,1,0);
  2502. { an add node always first loads both the left and the }
  2503. { right in the fpu before doing the calculation. However, }
  2504. { calcregisters(0,2,0) will overestimate the number of }
  2505. { necessary registers (it will make it 3 in case one of }
  2506. { the operands is already in the fpu) (JM) }
  2507. if ((left.expectloc<>LOC_FPUREGISTER) or
  2508. (right.expectloc<>LOC_FPUREGISTER)) and
  2509. (registersfpu < 2) then
  2510. inc(registersfpu);
  2511. end
  2512. { pointer comperation and subtraction }
  2513. else if (ld.typ=pointerdef) then
  2514. begin
  2515. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2516. expectloc:=LOC_REGISTER
  2517. else
  2518. expectloc:=LOC_FLAGS;
  2519. calcregisters(self,1,0,0);
  2520. end
  2521. else if is_class_or_interface(ld) then
  2522. begin
  2523. expectloc:=LOC_FLAGS;
  2524. calcregisters(self,1,0,0);
  2525. end
  2526. else if (ld.typ=classrefdef) then
  2527. begin
  2528. expectloc:=LOC_FLAGS;
  2529. calcregisters(self,1,0,0);
  2530. end
  2531. { support procvar=nil,procvar<>nil }
  2532. else if ((ld.typ=procvardef) and (rt=niln)) or
  2533. ((rd.typ=procvardef) and (lt=niln)) then
  2534. begin
  2535. expectloc:=LOC_FLAGS;
  2536. calcregisters(self,1,0,0);
  2537. end
  2538. {$ifdef SUPPORT_MMX}
  2539. { mmx support, this must be before the zero based array
  2540. check }
  2541. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  2542. is_mmx_able_array(rd) then
  2543. begin
  2544. expectloc:=LOC_MMXREGISTER;
  2545. calcregisters(self,0,0,1);
  2546. end
  2547. {$endif SUPPORT_MMX}
  2548. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  2549. begin
  2550. expectloc:=LOC_REGISTER;
  2551. calcregisters(self,1,0,0);
  2552. end
  2553. else if (rd.typ=procvardef) and
  2554. (ld.typ=procvardef) and
  2555. equal_defs(rd,ld) then
  2556. begin
  2557. expectloc:=LOC_FLAGS;
  2558. calcregisters(self,1,0,0);
  2559. end
  2560. else if (ld.typ=enumdef) then
  2561. begin
  2562. expectloc:=LOC_FLAGS;
  2563. calcregisters(self,1,0,0);
  2564. end
  2565. {$ifdef SUPPORT_MMX}
  2566. else if (cs_mmx in current_settings.localswitches) and
  2567. is_mmx_able_array(ld) and
  2568. is_mmx_able_array(rd) then
  2569. begin
  2570. expectloc:=LOC_MMXREGISTER;
  2571. calcregisters(self,0,0,1);
  2572. end
  2573. {$endif SUPPORT_MMX}
  2574. { the general solution is to convert to 32 bit int }
  2575. else
  2576. begin
  2577. expectloc:=LOC_REGISTER;
  2578. calcregisters(self,1,0,0);
  2579. end;
  2580. end;
  2581. {$ifdef state_tracking}
  2582. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2583. var factval:Tnode;
  2584. begin
  2585. track_state_pass:=false;
  2586. if left.track_state_pass(exec_known) then
  2587. begin
  2588. track_state_pass:=true;
  2589. left.resultdef:=nil;
  2590. do_typecheckpass(left);
  2591. end;
  2592. factval:=aktstate.find_fact(left);
  2593. if factval<>nil then
  2594. begin
  2595. track_state_pass:=true;
  2596. left.destroy;
  2597. left:=factval.getcopy;
  2598. end;
  2599. if right.track_state_pass(exec_known) then
  2600. begin
  2601. track_state_pass:=true;
  2602. right.resultdef:=nil;
  2603. do_typecheckpass(right);
  2604. end;
  2605. factval:=aktstate.find_fact(right);
  2606. if factval<>nil then
  2607. begin
  2608. track_state_pass:=true;
  2609. right.destroy;
  2610. right:=factval.getcopy;
  2611. end;
  2612. end;
  2613. {$endif}
  2614. begin
  2615. caddnode:=taddnode;
  2616. end.