nadd.pas 110 KB

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