nadd.pas 104 KB

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