nadd.pas 115 KB

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