nadd.pas 111 KB

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