nadd.pas 113 KB

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