nadd.pas 114 KB

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