nadd.pas 109 KB

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