nadd.pas 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420
  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. resultrealtype : ttype;
  26. constructor create(tt : tnodetype;l,r : tnode);override;
  27. function pass_1 : tnode;override;
  28. function det_resulttype: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 resulttype 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 MACOS_USE_FAKE_SYSUTILS}
  69. sysutils,
  70. {$ENDIF MACOS_USE_FAKE_SYSUTILS}
  71. globtype,systems,
  72. cutils,verbose,globals,widestr,
  73. symconst,symdef,symsym,symtable,defutil,defcmp,
  74. cgbase,
  75. htypechk,pass_1,
  76. nbas,nmat,ncnv,ncon,nset,nopt,ncal,ninl,nmem,nutils,
  77. {$ifdef state_tracking}
  78. nstate,
  79. {$endif}
  80. cpuinfo,procinfo;
  81. {*****************************************************************************
  82. TADDNODE
  83. *****************************************************************************}
  84. {$ifdef fpc}
  85. {$maxfpuregisters 0}
  86. {$endif fpc}
  87. function getbestreal(const t1,t2 : ttype) : ttype;
  88. const
  89. floatweight : array[tfloattype] of byte =
  90. (2,3,4,0,1,5);
  91. begin
  92. if t1.def.deftype=floatdef then
  93. begin
  94. result:=t1;
  95. if t2.def.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.def).typ in [s64comp,s64currency]) or
  100. (tfloatdef(t2.def).typ in [s64comp,s64currency]) then
  101. result:=pbestrealtype^
  102. else
  103. if floatweight[tfloatdef(t2.def).typ]>floatweight[tfloatdef(t1.def).typ] then
  104. result:=t2;
  105. end;
  106. end
  107. else if t2.def.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. resultrealtype:=pbestrealtype^;
  135. if (right.resulttype.def.deftype=floatdef) or (left.resulttype.def.deftype=floatdef) then
  136. begin
  137. { when both floattypes are already equal then use that
  138. floattype for results }
  139. if (right.resulttype.def.deftype=floatdef) and
  140. (left.resulttype.def.deftype=floatdef) and
  141. (tfloatdef(left.resulttype.def).typ=tfloatdef(right.resulttype.def).typ) then
  142. resultrealtype:=left.resulttype
  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.resulttype.def) or
  147. is_currency(left.resulttype.def)) and
  148. ((s64currencytype.def.deftype = floatdef) or
  149. (nodetype <> slashn)) then
  150. begin
  151. resultrealtype:=s64currencytype;
  152. inserttypeconv(right,resultrealtype);
  153. inserttypeconv(left,resultrealtype);
  154. end
  155. else
  156. begin
  157. resultrealtype:=getbestreal(left.resulttype,right.resulttype);
  158. inserttypeconv(right,resultrealtype);
  159. inserttypeconv(left,resultrealtype);
  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.resulttype.def) or
  167. is_widestring(left.resulttype.def) or
  168. is_widechar(right.resulttype.def) or
  169. is_widechar(left.resulttype.def)) then
  170. begin
  171. inserttypeconv(right,cwidestringtype);
  172. inserttypeconv(left,cwidestringtype);
  173. end;
  174. { load easier access variables }
  175. rd:=right.resulttype.def;
  176. ld:=left.resulttype.def;
  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 aktlocalswitches) or
  186. (cs_check_overflow in aktlocalswitches) 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 aktmoduleswitches) 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.resulttype)
  232. else if (nodetype <> subn) and
  233. is_voidpointer(ld) then
  234. inserttypeconv(left,right.resulttype)
  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.resulttype);
  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.resulttype.def).pointertype.def.size;
  267. if (rt = pointerconstn) and
  268. (lt <> pointerconstn) then
  269. lv := lv * tpointerdef(right.resulttype.def).pointertype.def.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.resulttype)
  280. else
  281. if is_integer(ld) then
  282. t := genintconstnode(lv+rv)
  283. else
  284. t := cordconstnode.create(lv+rv,left.resulttype,(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).pointertype.def.size)
  310. else
  311. t := cpointerconstnode.create(lv-rv,left.resulttype);
  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.resulttype,(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.resulttype,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.resulttype,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.resulttype,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,resultrealtype);
  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,resultrealtype);
  409. subn :
  410. t:=crealconstnode.create(lvd-rvd,resultrealtype);
  411. muln :
  412. t:=crealconstnode.create(lvd*rvd,resultrealtype);
  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,resultrealtype);
  420. end
  421. else if lvd=0 then
  422. t:=crealconstnode.create(1.0,resultrealtype)
  423. else
  424. t:=crealconstnode.create(exp(ln(lvd)*rvd),resultrealtype);
  425. end;
  426. slashn :
  427. t:=crealconstnode.create(lvd/rvd,resultrealtype);
  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;
  503. s2:=@c2;
  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;
  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;
  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.resulttype.def).settype=smallset) and
  569. (tsetdef(left.resulttype.def).settype<>smallset) then
  570. right.resulttype.setdef(tsetdef.create(tsetdef(right.resulttype.def).elementtype,255));
  571. { check base types }
  572. inserttypeconv(left,right.resulttype);
  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.resulttype);
  585. end;
  586. muln :
  587. begin
  588. resultset:=tsetconstnode(right).value_set^ * tsetconstnode(left).value_set^;
  589. t:=csetconstnode.create(@resultset,left.resulttype);
  590. end;
  591. subn :
  592. begin
  593. resultset:=tsetconstnode(left).value_set^ - tsetconstnode(right).value_set^;
  594. t:=csetconstnode.create(@resultset,left.resulttype);
  595. end;
  596. symdifn :
  597. begin
  598. resultset:=tsetconstnode(right).value_set^ >< tsetconstnode(left).value_set^;
  599. t:=csetconstnode.create(@resultset,left.resulttype);
  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.det_resulttype:tnode;
  632. var
  633. hp : tnode;
  634. lt,rt : tnodetype;
  635. rd,ld : tdef;
  636. htype : ttype;
  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. resulttypepass(left);
  650. resulttypepass(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.resulttype.def) then
  662. begin
  663. arrayconstructor_to_set(left);
  664. resulttypepass(left);
  665. end;
  666. if is_array_constructor(right.resulttype.def) then
  667. begin
  668. arrayconstructor_to_set(right);
  669. resulttypepass(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 aktmodeswitches) and
  688. (blocktype=bt_type) then
  689. begin
  690. if (left.resulttype.def.deftype=enumdef) and
  691. (right.resulttype.def.deftype=orddef) then
  692. begin
  693. { insert explicit typecast to default signed int }
  694. left:=ctypeconvnode.create_internal(left,sinttype);
  695. resulttypepass(left);
  696. end
  697. else
  698. if (left.resulttype.def.deftype=orddef) and
  699. (right.resulttype.def.deftype=enumdef) then
  700. begin
  701. { insert explicit typecast to default signed int }
  702. right:=ctypeconvnode.create_internal(right,sinttype);
  703. resulttypepass(right);
  704. end;
  705. end;
  706. result:=simplify;
  707. if assigned(result) then
  708. exit;
  709. { load easier access variables }
  710. rd:=right.resulttype.def;
  711. ld:=left.resulttype.def;
  712. rt:=right.nodetype;
  713. lt:=left.nodetype;
  714. { but an int/int gives real/real! }
  715. if nodetype=slashn then
  716. begin
  717. if is_currency(left.resulttype.def) and
  718. is_currency(right.resulttype.def) 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.def.deftype = floatdef then
  724. begin
  725. { there's no s64comptype or so, how do we avoid the type conversion?
  726. left.resulttype := s64comptype;
  727. right.resulttype := s64comptype; }
  728. end
  729. else
  730. begin
  731. left.resulttype := s64inttype;
  732. right.resulttype := s64inttype;
  733. end
  734. else if (left.resulttype.def.deftype <> floatdef) and
  735. (right.resulttype.def.deftype <> floatdef) then
  736. CGMessage(type_h_use_div_for_int);
  737. inserttypeconv(right,resultrealtype);
  738. inserttypeconv(left,resultrealtype);
  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 aktlocalswitches) 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.resulttype.def;
  773. end;
  774. if not is_boolean(rd) then
  775. begin
  776. inserttypeconv(right,booltype);
  777. rd := right.resulttype.def;
  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.resulttype.def).size>torddef(right.resulttype.def).size then
  785. begin
  786. right:=ctypeconvnode.create_internal(right,left.resulttype);
  787. ttypeconvnode(right).convtype:=tc_bool_2_bool;
  788. resulttypepass(right);
  789. end
  790. else if torddef(left.resulttype.def).size<torddef(right.resulttype.def).size then
  791. begin
  792. left:=ctypeconvnode.create_internal(left,right.resulttype);
  793. ttypeconvnode(left).convtype:=tc_bool_2_bool;
  794. resulttypepass(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 aktlocalswitches) 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. resulttype:=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. resulttype:=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.resulttype)
  910. else
  911. inserttypeconv_internal(right,left.resulttype);
  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.resulttype.def) or
  947. is_signed(right.resulttype.def) 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.resulttype.def).typ<>u32bit) then
  956. inserttypeconv(left,u32inttype);
  957. if (torddef(right.resulttype.def).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).elementtype.def,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. htype.setdef(tsetdef.create(tsetdef(ld).elementtype,255));
  1051. inserttypeconv(left,htype);
  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.resulttype)
  1062. else
  1063. inserttypeconv(right,left.resulttype);
  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.resulttype.def;
  1085. end
  1086. else if is_chararray(ld) then
  1087. begin
  1088. inserttypeconv(left,charpointertype);
  1089. ld:=left.resulttype.def;
  1090. end;
  1091. case nodetype of
  1092. equaln,unequaln :
  1093. begin
  1094. if is_voidpointer(right.resulttype.def) then
  1095. inserttypeconv(right,left.resulttype)
  1096. else if is_voidpointer(left.resulttype.def) then
  1097. inserttypeconv(left,right.resulttype)
  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 aktmoduleswitches) then
  1113. begin
  1114. if is_voidpointer(right.resulttype.def) then
  1115. inserttypeconv(right,left.resulttype)
  1116. else if is_voidpointer(left.resulttype.def) then
  1117. inserttypeconv(left,right.resulttype)
  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 aktmoduleswitches) then
  1127. begin
  1128. if is_voidpointer(right.resulttype.def) then
  1129. inserttypeconv(right,left.resulttype)
  1130. else if is_voidpointer(left.resulttype.def) then
  1131. inserttypeconv(left,right.resulttype)
  1132. else if not(equal_defs(ld,rd)) then
  1133. IncompatibleTypes(ld,rd);
  1134. end
  1135. else
  1136. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1137. if not(nf_has_pointerdiv in flags) and
  1138. (tpointerdef(rd).pointertype.def.size>1) then
  1139. begin
  1140. hp:=getcopy;
  1141. include(hp.flags,nf_has_pointerdiv);
  1142. result:=cmoddivnode.create(divn,hp,cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,false));
  1143. end;
  1144. resulttype:=sinttype;
  1145. exit;
  1146. end;
  1147. addn:
  1148. begin
  1149. if (cs_extsyntax in aktmoduleswitches) then
  1150. begin
  1151. if is_voidpointer(right.resulttype.def) then
  1152. inserttypeconv(right,left.resulttype)
  1153. else if is_voidpointer(left.resulttype.def) then
  1154. inserttypeconv(left,right.resulttype)
  1155. else if not(equal_defs(ld,rd)) then
  1156. IncompatibleTypes(ld,rd);
  1157. end
  1158. else
  1159. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1160. resulttype:=sinttype;
  1161. exit;
  1162. end;
  1163. else
  1164. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1165. end;
  1166. end
  1167. { is one of the operands a string?,
  1168. chararrays are also handled as strings (after conversion), also take
  1169. care of chararray+chararray and chararray+char.
  1170. Note: Must be done after pointerdef+pointerdef has been checked, else
  1171. pchar is converted to string }
  1172. else if (rd.deftype=stringdef) or
  1173. (ld.deftype=stringdef) or
  1174. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1175. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1176. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1177. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1178. begin
  1179. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1180. begin
  1181. { Is there a widestring? }
  1182. if is_widestring(rd) or is_widestring(ld) or
  1183. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1184. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1185. strtype:= st_widestring
  1186. else
  1187. if is_ansistring(rd) or is_ansistring(ld) or
  1188. ((cs_ansistrings in aktlocalswitches) and
  1189. //todo: Move some of this to longstring's then they are implemented?
  1190. (
  1191. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or
  1192. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld)
  1193. )
  1194. ) then
  1195. strtype:= st_ansistring
  1196. else
  1197. if is_longstring(rd) or is_longstring(ld) then
  1198. strtype:= st_longstring
  1199. else
  1200. begin
  1201. {$warning todo: add a warning/hint here if one converting a too large array}
  1202. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1203. Note: Delphi halts with error if "array [0..xx] of char"
  1204. is assigned to ShortString and string length is less
  1205. then array size }
  1206. strtype:= st_shortstring;
  1207. end;
  1208. // Now convert nodes to common string type
  1209. case strtype of
  1210. st_widestring :
  1211. begin
  1212. if not(is_widestring(rd)) then
  1213. inserttypeconv(right,cwidestringtype);
  1214. if not(is_widestring(ld)) then
  1215. inserttypeconv(left,cwidestringtype);
  1216. end;
  1217. st_ansistring :
  1218. begin
  1219. if not(is_ansistring(rd)) then
  1220. inserttypeconv(right,cansistringtype);
  1221. if not(is_ansistring(ld)) then
  1222. inserttypeconv(left,cansistringtype);
  1223. end;
  1224. st_longstring :
  1225. begin
  1226. if not(is_longstring(rd)) then
  1227. inserttypeconv(right,clongstringtype);
  1228. if not(is_longstring(ld)) then
  1229. inserttypeconv(left,clongstringtype);
  1230. end;
  1231. st_shortstring :
  1232. begin
  1233. if not(is_shortstring(ld)) then
  1234. inserttypeconv(left,cshortstringtype);
  1235. { don't convert char, that can be handled by the optimized node }
  1236. if not(is_shortstring(rd) or is_char(rd)) then
  1237. inserttypeconv(right,cshortstringtype);
  1238. end;
  1239. else
  1240. internalerror(2005101);
  1241. end;
  1242. end
  1243. else
  1244. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1245. end
  1246. { class or interface equation }
  1247. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  1248. begin
  1249. if (nodetype in [equaln,unequaln]) then
  1250. begin
  1251. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  1252. begin
  1253. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1254. inserttypeconv(right,left.resulttype)
  1255. else
  1256. inserttypeconv(left,right.resulttype);
  1257. end
  1258. else if is_class_or_interface(rd) then
  1259. inserttypeconv(left,right.resulttype)
  1260. else
  1261. inserttypeconv(right,left.resulttype);
  1262. end
  1263. else
  1264. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1265. end
  1266. else if (rd.deftype=classrefdef) and (ld.deftype=classrefdef) then
  1267. begin
  1268. if (nodetype in [equaln,unequaln]) then
  1269. begin
  1270. if tobjectdef(tclassrefdef(rd).pointertype.def).is_related(
  1271. tobjectdef(tclassrefdef(ld).pointertype.def)) then
  1272. inserttypeconv(right,left.resulttype)
  1273. else
  1274. inserttypeconv(left,right.resulttype);
  1275. end
  1276. else
  1277. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1278. end
  1279. { allows comperasion with nil pointer }
  1280. else if is_class_or_interface(rd) or (rd.deftype=classrefdef) then
  1281. begin
  1282. if (nodetype in [equaln,unequaln]) then
  1283. inserttypeconv(left,right.resulttype)
  1284. else
  1285. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1286. end
  1287. else if is_class_or_interface(ld) or (ld.deftype=classrefdef) then
  1288. begin
  1289. if (nodetype in [equaln,unequaln]) then
  1290. inserttypeconv(right,left.resulttype)
  1291. else
  1292. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1293. end
  1294. { support procvar=nil,procvar<>nil }
  1295. else if ((ld.deftype=procvardef) and (rt=niln)) or
  1296. ((rd.deftype=procvardef) and (lt=niln)) then
  1297. begin
  1298. if not(nodetype in [equaln,unequaln]) then
  1299. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1300. { find proc field in methodpointer record }
  1301. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('proc'));
  1302. if not assigned(hsym) then
  1303. internalerror(200412043);
  1304. { For methodpointers compare only tmethodpointer.proc }
  1305. if (rd.deftype=procvardef) and
  1306. (not tprocvardef(rd).is_addressonly) then
  1307. begin
  1308. right:=csubscriptnode.create(
  1309. hsym,
  1310. ctypeconvnode.create_internal(right,methodpointertype));
  1311. end;
  1312. if (ld.deftype=procvardef) and
  1313. (not tprocvardef(ld).is_addressonly) then
  1314. begin
  1315. left:=csubscriptnode.create(
  1316. hsym,
  1317. ctypeconvnode.create_internal(left,methodpointertype));
  1318. end;
  1319. end
  1320. { support dynamicarray=nil,dynamicarray<>nil }
  1321. else if (is_dynamic_array(ld) and (rt=niln)) or
  1322. (is_dynamic_array(rd) and (lt=niln)) or
  1323. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1324. begin
  1325. if not(nodetype in [equaln,unequaln]) then
  1326. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1327. end
  1328. {$ifdef SUPPORT_MMX}
  1329. { mmx support, this must be before the zero based array
  1330. check }
  1331. else if (cs_mmx in aktlocalswitches) and
  1332. is_mmx_able_array(ld) and
  1333. is_mmx_able_array(rd) and
  1334. equal_defs(ld,rd) then
  1335. begin
  1336. case nodetype of
  1337. addn,subn,xorn,orn,andn:
  1338. ;
  1339. { mul is a little bit restricted }
  1340. muln:
  1341. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1342. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1343. else
  1344. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1345. end;
  1346. end
  1347. {$endif SUPPORT_MMX}
  1348. { this is a little bit dangerous, also the left type }
  1349. { pointer to should be checked! This broke the mmx support }
  1350. else if (rd.deftype=pointerdef) or
  1351. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  1352. begin
  1353. if is_zero_based_array(rd) then
  1354. begin
  1355. resulttype.setdef(tpointerdef.create(tarraydef(rd).elementtype));
  1356. inserttypeconv(right,resulttype);
  1357. end
  1358. else
  1359. resulttype:=right.resulttype;
  1360. inserttypeconv(left,sinttype);
  1361. if nodetype=addn then
  1362. begin
  1363. if not(cs_extsyntax in aktmoduleswitches) or
  1364. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1365. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1366. if (rd.deftype=pointerdef) and
  1367. (tpointerdef(rd).pointertype.def.size>1) then
  1368. begin
  1369. left:=caddnode.create(muln,left,
  1370. cordconstnode.create(tpointerdef(rd).pointertype.def.size,sinttype,true));
  1371. end;
  1372. end
  1373. else
  1374. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1375. end
  1376. else if (ld.deftype=pointerdef) or
  1377. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  1378. begin
  1379. if is_zero_based_array(ld) then
  1380. begin
  1381. resulttype.setdef(tpointerdef.create(tarraydef(ld).elementtype));
  1382. inserttypeconv(left,resulttype);
  1383. end
  1384. else
  1385. resulttype:=left.resulttype;
  1386. inserttypeconv(right,sinttype);
  1387. if nodetype in [addn,subn] then
  1388. begin
  1389. if not(cs_extsyntax in aktmoduleswitches) or
  1390. (not(is_pchar(ld)) and not(m_add_pointer in aktmodeswitches)) then
  1391. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1392. if (ld.deftype=pointerdef) and
  1393. (tpointerdef(ld).pointertype.def.size>1) then
  1394. begin
  1395. right:=caddnode.create(muln,right,
  1396. cordconstnode.create(tpointerdef(ld).pointertype.def.size,sinttype,true));
  1397. end
  1398. else
  1399. if is_zero_based_array(ld) and
  1400. (tarraydef(ld).elementtype.def.size>1) then
  1401. begin
  1402. right:=caddnode.create(muln,right,
  1403. cordconstnode.create(tarraydef(ld).elementtype.def.size,sinttype,true));
  1404. end;
  1405. end
  1406. else
  1407. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1408. end
  1409. else if (rd.deftype=procvardef) and
  1410. (ld.deftype=procvardef) and
  1411. equal_defs(rd,ld) then
  1412. begin
  1413. if (nodetype in [equaln,unequaln]) then
  1414. begin
  1415. if tprocvardef(rd).is_addressonly then
  1416. begin
  1417. inserttypeconv_internal(right,voidpointertype);
  1418. inserttypeconv_internal(left,voidpointertype);
  1419. end
  1420. else
  1421. begin
  1422. { find proc field in methodpointer record }
  1423. hsym:=tfieldvarsym(trecorddef(methodpointertype.def).symtable.search('proc'));
  1424. if not assigned(hsym) then
  1425. internalerror(200412043);
  1426. { Compare tmehodpointer(left).proc }
  1427. right:=csubscriptnode.create(
  1428. hsym,
  1429. ctypeconvnode.create_internal(right,methodpointertype));
  1430. left:=csubscriptnode.create(
  1431. hsym,
  1432. ctypeconvnode.create_internal(left,methodpointertype));
  1433. end;
  1434. end
  1435. else
  1436. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1437. end
  1438. { enums }
  1439. else if (ld.deftype=enumdef) and (rd.deftype=enumdef) then
  1440. begin
  1441. if allowenumop(nodetype) then
  1442. inserttypeconv(right,left.resulttype)
  1443. else
  1444. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1445. end
  1446. { generic conversion, this is for error recovery }
  1447. else
  1448. begin
  1449. inserttypeconv(left,sinttype);
  1450. inserttypeconv(right,sinttype);
  1451. end;
  1452. { set resulttype if not already done }
  1453. if not assigned(resulttype.def) then
  1454. begin
  1455. case nodetype of
  1456. ltn,lten,gtn,gten,equaln,unequaln :
  1457. resulttype:=booltype;
  1458. slashn :
  1459. resulttype:=resultrealtype;
  1460. addn:
  1461. begin
  1462. { for strings, return is always a 255 char string }
  1463. if is_shortstring(left.resulttype.def) then
  1464. resulttype:=cshortstringtype
  1465. else
  1466. resulttype:=left.resulttype;
  1467. end;
  1468. else
  1469. resulttype:=left.resulttype;
  1470. end;
  1471. end;
  1472. { when the result is currency we need some extra code for
  1473. multiplication and division. this should not be done when
  1474. the muln or slashn node is created internally }
  1475. if not(nf_is_currency in flags) and
  1476. is_currency(resulttype.def) then
  1477. begin
  1478. case nodetype of
  1479. slashn :
  1480. begin
  1481. { slashn will only work with floats }
  1482. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1483. include(hp.flags,nf_is_currency);
  1484. result:=hp;
  1485. end;
  1486. muln :
  1487. begin
  1488. if s64currencytype.def.deftype=floatdef then
  1489. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1490. else
  1491. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1492. include(hp.flags,nf_is_currency);
  1493. result:=hp
  1494. end;
  1495. end;
  1496. end;
  1497. end;
  1498. function taddnode.first_addstring: tnode;
  1499. const
  1500. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  1501. var
  1502. p: tnode;
  1503. begin
  1504. { when we get here, we are sure that both the left and the right }
  1505. { node are both strings of the same stringtype (JM) }
  1506. case nodetype of
  1507. addn:
  1508. begin
  1509. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  1510. begin
  1511. result:=right;
  1512. left:=nil;
  1513. right:=nil;
  1514. exit;
  1515. end;
  1516. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  1517. begin
  1518. result:=left;
  1519. left:=nil;
  1520. right:=nil;
  1521. exit;
  1522. end;
  1523. { create the call to the concat routine both strings as arguments }
  1524. result := ccallnode.createintern('fpc_'+
  1525. tstringdef(resulttype.def).stringtypname+'_concat',
  1526. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1527. { we reused the arguments }
  1528. left := nil;
  1529. right := nil;
  1530. end;
  1531. ltn,lten,gtn,gten,equaln,unequaln :
  1532. begin
  1533. { generate better code for comparison with empty string, we
  1534. only need to compare the length with 0 }
  1535. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1536. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  1537. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  1538. begin
  1539. { switch so that the constant is always on the right }
  1540. if left.nodetype = stringconstn then
  1541. begin
  1542. p := left;
  1543. left := right;
  1544. right := p;
  1545. nodetype:=swap_relation[nodetype];
  1546. end;
  1547. if is_shortstring(left.resulttype.def) or
  1548. (nodetype in [gtn,gten,ltn,lten]) then
  1549. { compare the length with 0 }
  1550. result := caddnode.create(nodetype,
  1551. cinlinenode.create(in_length_x,false,left),
  1552. cordconstnode.create(0,s32inttype,false))
  1553. else
  1554. begin
  1555. { compare the pointer with nil (for ansistrings etc), }
  1556. { faster than getting the length (JM) }
  1557. result:= caddnode.create(nodetype,
  1558. ctypeconvnode.create_internal(left,voidpointertype),
  1559. cpointerconstnode.create(0,voidpointertype));
  1560. end;
  1561. { left is reused }
  1562. left := nil;
  1563. { right isn't }
  1564. right.free;
  1565. right := nil;
  1566. exit;
  1567. end;
  1568. { no string constant -> call compare routine }
  1569. result := ccallnode.createintern('fpc_'+
  1570. tstringdef(left.resulttype.def).stringtypname+'_compare',
  1571. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1572. { and compare its result with 0 according to the original operator }
  1573. result := caddnode.create(nodetype,result,
  1574. cordconstnode.create(0,s32inttype,false));
  1575. left := nil;
  1576. right := nil;
  1577. end;
  1578. end;
  1579. end;
  1580. function taddnode.first_addset: tnode;
  1581. var
  1582. procname: string[31];
  1583. tempn: tnode;
  1584. paras: tcallparanode;
  1585. srsym: ttypesym;
  1586. begin
  1587. { get the sym that represents the fpc_normal_set type }
  1588. srsym:=search_system_type('FPC_NORMAL_SET');
  1589. case nodetype of
  1590. equaln,unequaln,lten,gten:
  1591. begin
  1592. case nodetype of
  1593. equaln,unequaln:
  1594. procname := 'fpc_set_comp_sets';
  1595. lten,gten:
  1596. begin
  1597. procname := 'fpc_set_contains_sets';
  1598. { (left >= right) = (right <= left) }
  1599. if nodetype = gten then
  1600. begin
  1601. tempn := left;
  1602. left := right;
  1603. right := tempn;
  1604. end;
  1605. end;
  1606. end;
  1607. { convert the arguments (explicitely) to fpc_normal_set's }
  1608. left := ctypeconvnode.create_internal(left,srsym.restype);
  1609. right := ctypeconvnode.create_internal(right,srsym.restype);
  1610. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1611. ccallparanode.create(left,nil)));
  1612. { left and right are reused as parameters }
  1613. left := nil;
  1614. right := nil;
  1615. { for an unequaln, we have to negate the result of comp_sets }
  1616. if nodetype = unequaln then
  1617. result := cnotnode.create(result);
  1618. end;
  1619. addn:
  1620. begin
  1621. { optimize first loading of a set }
  1622. if (right.nodetype=setelementn) and
  1623. not(assigned(tsetelementnode(right).right)) and
  1624. is_emptyset(left) then
  1625. begin
  1626. { type cast the value to pass as argument to a byte, }
  1627. { since that's what the helper expects }
  1628. tsetelementnode(right).left :=
  1629. ctypeconvnode.create_internal(tsetelementnode(right).left,u8inttype);
  1630. { set the resulttype to the actual one (otherwise it's }
  1631. { "fpc_normal_set") }
  1632. result := ccallnode.createinternres('fpc_set_create_element',
  1633. ccallparanode.create(tsetelementnode(right).left,nil),
  1634. resulttype);
  1635. { reused }
  1636. tsetelementnode(right).left := nil;
  1637. end
  1638. else
  1639. begin
  1640. if right.nodetype=setelementn then
  1641. begin
  1642. { convert the arguments to bytes, since that's what }
  1643. { the helper expects }
  1644. tsetelementnode(right).left :=
  1645. ctypeconvnode.create_internal(tsetelementnode(right).left,
  1646. u8inttype);
  1647. { convert the original set (explicitely) to an }
  1648. { fpc_normal_set so we can pass it to the helper }
  1649. left := ctypeconvnode.create_internal(left,srsym.restype);
  1650. { add a range or a single element? }
  1651. if assigned(tsetelementnode(right).right) then
  1652. begin
  1653. tsetelementnode(right).right :=
  1654. ctypeconvnode.create_internal(tsetelementnode(right).right,
  1655. u8inttype);
  1656. { create the call }
  1657. result := ccallnode.createinternres('fpc_set_set_range',
  1658. ccallparanode.create(tsetelementnode(right).right,
  1659. ccallparanode.create(tsetelementnode(right).left,
  1660. ccallparanode.create(left,nil))),resulttype);
  1661. end
  1662. else
  1663. begin
  1664. result := ccallnode.createinternres('fpc_set_set_byte',
  1665. ccallparanode.create(tsetelementnode(right).left,
  1666. ccallparanode.create(left,nil)),resulttype);
  1667. end;
  1668. { remove reused parts from original node }
  1669. tsetelementnode(right).right := nil;
  1670. tsetelementnode(right).left := nil;
  1671. left := nil;
  1672. end
  1673. else
  1674. begin
  1675. { add two sets }
  1676. { convert the sets to fpc_normal_set's }
  1677. result := ccallnode.createinternres('fpc_set_add_sets',
  1678. ccallparanode.create(
  1679. ctypeconvnode.create_explicit(right,srsym.restype),
  1680. ccallparanode.create(
  1681. ctypeconvnode.create_internal(left,srsym.restype),nil)),resulttype);
  1682. { remove reused parts from original node }
  1683. left := nil;
  1684. right := nil;
  1685. end;
  1686. end
  1687. end;
  1688. subn,symdifn,muln:
  1689. begin
  1690. { convert the sets to fpc_normal_set's }
  1691. paras := ccallparanode.create(ctypeconvnode.create_internal(right,srsym.restype),
  1692. ccallparanode.create(ctypeconvnode.create_internal(left,srsym.restype),nil));
  1693. case nodetype of
  1694. subn:
  1695. result := ccallnode.createinternres('fpc_set_sub_sets',
  1696. paras,resulttype);
  1697. symdifn:
  1698. result := ccallnode.createinternres('fpc_set_symdif_sets',
  1699. paras,resulttype);
  1700. muln:
  1701. result := ccallnode.createinternres('fpc_set_mul_sets',
  1702. paras,resulttype);
  1703. end;
  1704. { remove reused parts from original node }
  1705. left := nil;
  1706. right := nil;
  1707. end;
  1708. else
  1709. internalerror(200108311);
  1710. end;
  1711. end;
  1712. function taddnode.use_generic_mul32to64: boolean;
  1713. begin
  1714. result := true;
  1715. end;
  1716. function taddnode.try_make_mul32to64: boolean;
  1717. function canbe32bitint(v: tconstexprint; fromdef: torddef; todefsigned: boolean): boolean;
  1718. begin
  1719. if (fromdef.typ <> u64bit) then
  1720. result :=
  1721. ((v >= 0) or
  1722. todefsigned) and
  1723. (v >= low(longint)) and
  1724. (v <= high(longint))
  1725. else
  1726. result :=
  1727. (qword(v) >= low(cardinal)) and
  1728. (qword(v) <= high(cardinal))
  1729. end;
  1730. var
  1731. temp: tnode;
  1732. begin
  1733. result := false;
  1734. if ((left.nodetype = typeconvn) and
  1735. is_integer(ttypeconvnode(left).left.resulttype.def) and
  1736. (not(torddef(ttypeconvnode(left).left.resulttype.def).typ in [u64bit,s64bit])) and
  1737. (((right.nodetype = ordconstn) and
  1738. canbe32bitint(tordconstnode(right).value,torddef(right.resulttype.def),is_signed(left.resulttype.def))) or
  1739. ((right.nodetype = typeconvn) and
  1740. is_integer(ttypeconvnode(right).left.resulttype.def) and
  1741. not(torddef(ttypeconvnode(right).left.resulttype.def).typ in [u64bit,s64bit])) and
  1742. (is_signed(ttypeconvnode(left).left.resulttype.def) =
  1743. is_signed(ttypeconvnode(right).left.resulttype.def)))) then
  1744. begin
  1745. temp := ttypeconvnode(left).left;
  1746. ttypeconvnode(left).left := nil;
  1747. left.free;
  1748. left := temp;
  1749. if (right.nodetype = typeconvn) then
  1750. begin
  1751. temp := ttypeconvnode(right).left;
  1752. ttypeconvnode(right).left := nil;
  1753. right.free;
  1754. right := temp;
  1755. end;
  1756. if (is_signed(left.resulttype.def)) then
  1757. begin
  1758. inserttypeconv(left,s32inttype);
  1759. inserttypeconv(right,s32inttype);
  1760. end
  1761. else
  1762. begin
  1763. inserttypeconv(left,u32inttype);
  1764. inserttypeconv(right,u32inttype);
  1765. end;
  1766. firstpass(left);
  1767. firstpass(right);
  1768. result := true;
  1769. end;
  1770. end;
  1771. function taddnode.first_add64bitint: tnode;
  1772. var
  1773. procname: string[31];
  1774. temp: tnode;
  1775. power: longint;
  1776. begin
  1777. result := nil;
  1778. { create helper calls mul }
  1779. if nodetype <> muln then
  1780. exit;
  1781. { make sure that if there is a constant, that it's on the right }
  1782. if left.nodetype = ordconstn then
  1783. begin
  1784. temp := right;
  1785. right := left;
  1786. left := temp;
  1787. end;
  1788. { can we use a shift instead of a mul? }
  1789. if not (cs_check_overflow in aktlocalswitches) and
  1790. (right.nodetype = ordconstn) and
  1791. ispowerof2(tordconstnode(right).value,power) then
  1792. begin
  1793. tordconstnode(right).value := power;
  1794. result := cshlshrnode.create(shln,left,right);
  1795. { left and right are reused }
  1796. left := nil;
  1797. right := nil;
  1798. { return firstpassed new node }
  1799. exit;
  1800. end;
  1801. if not(use_generic_mul32to64) and
  1802. try_make_mul32to64 then
  1803. exit;
  1804. { when currency is used set the result of the
  1805. parameters to s64bit, so they are not converted }
  1806. if is_currency(resulttype.def) then
  1807. begin
  1808. left.resulttype:=s64inttype;
  1809. right.resulttype:=s64inttype;
  1810. end;
  1811. { otherwise, create the parameters for the helper }
  1812. right := ccallparanode.create(
  1813. cordconstnode.create(ord(cs_check_overflow in aktlocalswitches),booltype,true),
  1814. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1815. left := nil;
  1816. { only qword needs the unsigned code, the
  1817. signed code is also used for currency }
  1818. if is_signed(resulttype.def) then
  1819. procname := 'fpc_mul_int64'
  1820. else
  1821. procname := 'fpc_mul_qword';
  1822. result := ccallnode.createintern(procname,right);
  1823. right := nil;
  1824. end;
  1825. function taddnode.first_addfloat : tnode;
  1826. var
  1827. procname: string[31];
  1828. { do we need to reverse the result ? }
  1829. notnode : boolean;
  1830. begin
  1831. result := nil;
  1832. notnode := false;
  1833. { In non-emulation mode, real opcodes are
  1834. emitted for floating point values.
  1835. }
  1836. if not (cs_fp_emulation in aktmoduleswitches) then
  1837. exit;
  1838. if not(target_info.system in system_wince) then
  1839. begin
  1840. case tfloatdef(left.resulttype.def).typ of
  1841. s32real:
  1842. procname:='float32';
  1843. s64real:
  1844. procname:='float64';
  1845. {!!! not yet implemented
  1846. s128real:
  1847. }
  1848. else
  1849. internalerror(2005082601);
  1850. end;
  1851. case nodetype of
  1852. addn:
  1853. procname:=procname+'_add';
  1854. muln:
  1855. procname:=procname+'_mul';
  1856. subn:
  1857. procname:=procname+'_sub';
  1858. slashn:
  1859. procname:=procname+'_div';
  1860. ltn:
  1861. procname:=procname+'_lt';
  1862. lten:
  1863. procname:=procname+'_le';
  1864. gtn:
  1865. begin
  1866. procname:=procname+'_le';
  1867. notnode:=true;
  1868. end;
  1869. gten:
  1870. begin
  1871. procname:=procname+'_lt';
  1872. notnode:=true;
  1873. end;
  1874. equaln:
  1875. procname:=procname+'_eq';
  1876. unequaln:
  1877. begin
  1878. procname:=procname+'_eq';
  1879. notnode:=true;
  1880. end;
  1881. else
  1882. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resulttype.def.typename,right.resulttype.def.typename);
  1883. end;
  1884. end
  1885. else
  1886. begin
  1887. case nodetype of
  1888. addn:
  1889. procname:='ADD';
  1890. muln:
  1891. procname:='MUL';
  1892. subn:
  1893. procname:='SUB';
  1894. slashn:
  1895. procname:='DIV';
  1896. ltn:
  1897. procname:='LT';
  1898. lten:
  1899. procname:='LE';
  1900. gtn:
  1901. procname:='GT';
  1902. gten:
  1903. procname:='GE';
  1904. equaln:
  1905. procname:='EQ';
  1906. unequaln:
  1907. procname:='NE';
  1908. else
  1909. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resulttype.def.typename,right.resulttype.def.typename);
  1910. end;
  1911. case tfloatdef(left.resulttype.def).typ of
  1912. s32real:
  1913. procname:=procname+'S';
  1914. s64real:
  1915. procname:=procname+'D';
  1916. {!!! not yet implemented
  1917. s128real:
  1918. }
  1919. else
  1920. internalerror(2005082602);
  1921. end;
  1922. end;
  1923. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  1924. ccallparanode.create(left,nil)));
  1925. left:=nil;
  1926. right:=nil;
  1927. { do we need to reverse the result }
  1928. if notnode then
  1929. result:=cnotnode.create(result);
  1930. end;
  1931. function taddnode.pass_1 : tnode;
  1932. var
  1933. {$ifdef addstringopt}
  1934. hp : tnode;
  1935. {$endif addstringopt}
  1936. lt,rt : tnodetype;
  1937. rd,ld : tdef;
  1938. begin
  1939. result:=nil;
  1940. { Can we optimize multiple string additions into a single call?
  1941. This need to be done on a complete tree to detect the multiple
  1942. add nodes and is therefor done before the subtrees are processed }
  1943. if canbemultistringadd(self) then
  1944. begin
  1945. result := genmultistringadd(self);
  1946. exit;
  1947. end;
  1948. { first do the two subtrees }
  1949. firstpass(left);
  1950. firstpass(right);
  1951. if codegenerror then
  1952. exit;
  1953. { load easier access variables }
  1954. rd:=right.resulttype.def;
  1955. ld:=left.resulttype.def;
  1956. rt:=right.nodetype;
  1957. lt:=left.nodetype;
  1958. { int/int gives real/real! }
  1959. if nodetype=slashn then
  1960. begin
  1961. {$ifdef cpufpemu}
  1962. if (aktfputype=fpu_soft) or (cs_fp_emulation in aktmoduleswitches) then
  1963. begin
  1964. result:=first_addfloat;
  1965. if assigned(result) then
  1966. exit;
  1967. end;
  1968. {$endif cpufpemu}
  1969. expectloc:=LOC_FPUREGISTER;
  1970. { maybe we need an integer register to save }
  1971. { a reference }
  1972. if ((left.expectloc<>LOC_FPUREGISTER) or
  1973. (right.expectloc<>LOC_FPUREGISTER)) and
  1974. (left.registersint=right.registersint) then
  1975. calcregisters(self,1,1,0)
  1976. else
  1977. calcregisters(self,0,1,0);
  1978. { an add node always first loads both the left and the }
  1979. { right in the fpu before doing the calculation. However, }
  1980. { calcregisters(0,2,0) will overestimate the number of }
  1981. { necessary registers (it will make it 3 in case one of }
  1982. { the operands is already in the fpu) (JM) }
  1983. if ((left.expectloc<>LOC_FPUREGISTER) or
  1984. (right.expectloc<>LOC_FPUREGISTER)) and
  1985. (registersfpu < 2) then
  1986. inc(registersfpu);
  1987. end
  1988. { if both are orddefs then check sub types }
  1989. else if (ld.deftype=orddef) and (rd.deftype=orddef) then
  1990. begin
  1991. { 2 booleans ? }
  1992. if is_boolean(ld) and is_boolean(rd) then
  1993. begin
  1994. if (not(cs_full_boolean_eval in aktlocalswitches) or
  1995. (nf_short_bool in flags)) and
  1996. (nodetype in [andn,orn]) then
  1997. begin
  1998. expectloc:=LOC_JUMP;
  1999. calcregisters(self,0,0,0);
  2000. end
  2001. else
  2002. begin
  2003. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2004. begin
  2005. expectloc:=LOC_FLAGS;
  2006. if (left.expectloc in [LOC_JUMP,LOC_FLAGS]) and
  2007. (left.expectloc in [LOC_JUMP,LOC_FLAGS]) then
  2008. calcregisters(self,2,0,0)
  2009. else
  2010. calcregisters(self,1,0,0);
  2011. end
  2012. else
  2013. begin
  2014. expectloc:=LOC_REGISTER;
  2015. calcregisters(self,0,0,0);
  2016. end;
  2017. end;
  2018. end
  2019. else
  2020. { Both are chars? only convert to shortstrings for addn }
  2021. if is_char(ld) then
  2022. begin
  2023. if nodetype=addn then
  2024. internalerror(200103291);
  2025. expectloc:=LOC_FLAGS;
  2026. calcregisters(self,1,0,0);
  2027. end
  2028. {$ifndef cpu64bit}
  2029. { is there a 64 bit type ? }
  2030. else if (torddef(ld).typ in [s64bit,u64bit,scurrency]) then
  2031. begin
  2032. result := first_add64bitint;
  2033. if assigned(result) then
  2034. exit;
  2035. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2036. expectloc:=LOC_REGISTER
  2037. else
  2038. expectloc:=LOC_JUMP;
  2039. calcregisters(self,2,0,0)
  2040. end
  2041. {$endif cpu64bit}
  2042. { is there a cardinal? }
  2043. else if (torddef(ld).typ=u32bit) then
  2044. begin
  2045. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2046. expectloc:=LOC_REGISTER
  2047. else
  2048. expectloc:=LOC_FLAGS;
  2049. calcregisters(self,1,0,0);
  2050. { for unsigned mul we need an extra register }
  2051. if nodetype=muln then
  2052. inc(registersint);
  2053. end
  2054. { generic s32bit conversion }
  2055. else
  2056. begin
  2057. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2058. expectloc:=LOC_REGISTER
  2059. else
  2060. expectloc:=LOC_FLAGS;
  2061. calcregisters(self,1,0,0);
  2062. end;
  2063. end
  2064. { left side a setdef, must be before string processing,
  2065. else array constructor can be seen as array of char (PFV) }
  2066. else if (ld.deftype=setdef) then
  2067. begin
  2068. if tsetdef(ld).settype=smallset then
  2069. begin
  2070. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2071. expectloc:=LOC_FLAGS
  2072. else
  2073. expectloc:=LOC_REGISTER;
  2074. { are we adding set elements ? }
  2075. if right.nodetype=setelementn then
  2076. calcregisters(self,2,0,0)
  2077. else
  2078. calcregisters(self,1,0,0);
  2079. end
  2080. else
  2081. {$ifdef MMXSET}
  2082. {$ifdef i386}
  2083. if cs_mmx in aktlocalswitches then
  2084. begin
  2085. expectloc:=LOC_MMXREGISTER;
  2086. calcregisters(self,0,0,4);
  2087. end
  2088. else
  2089. {$endif}
  2090. {$endif MMXSET}
  2091. begin
  2092. result := first_addset;
  2093. if assigned(result) then
  2094. exit;
  2095. expectloc:=LOC_CREFERENCE;
  2096. calcregisters(self,0,0,0);
  2097. { here we call SET... }
  2098. include(current_procinfo.flags,pi_do_call);
  2099. end;
  2100. end
  2101. { compare pchar by addresses like BP/Delphi }
  2102. else if is_pchar(ld) then
  2103. begin
  2104. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2105. expectloc:=LOC_REGISTER
  2106. else
  2107. expectloc:=LOC_FLAGS;
  2108. calcregisters(self,1,0,0);
  2109. end
  2110. { is one of the operands a string }
  2111. else if (ld.deftype=stringdef) then
  2112. begin
  2113. if is_widestring(ld) then
  2114. begin
  2115. { this is only for add, the comparisaion is handled later }
  2116. expectloc:=LOC_REGISTER;
  2117. end
  2118. else if is_ansistring(ld) then
  2119. begin
  2120. { this is only for add, the comparisaion is handled later }
  2121. expectloc:=LOC_REGISTER;
  2122. end
  2123. else if is_longstring(ld) then
  2124. begin
  2125. { this is only for add, the comparisaion is handled later }
  2126. expectloc:=LOC_REFERENCE;
  2127. end
  2128. else
  2129. begin
  2130. {$ifdef addstringopt}
  2131. { can create a call which isn't handled by callparatemp }
  2132. if canbeaddsstringcharoptnode(self) then
  2133. begin
  2134. hp := genaddsstringcharoptnode(self);
  2135. pass_1 := hp;
  2136. exit;
  2137. end
  2138. else
  2139. {$endif addstringopt}
  2140. begin
  2141. { Fix right to be shortstring }
  2142. if is_char(right.resulttype.def) then
  2143. begin
  2144. inserttypeconv(right,cshortstringtype);
  2145. firstpass(right);
  2146. end;
  2147. end;
  2148. {$ifdef addstringopt}
  2149. { can create a call which isn't handled by callparatemp }
  2150. if canbeaddsstringcsstringoptnode(self) then
  2151. begin
  2152. hp := genaddsstringcsstringoptnode(self);
  2153. pass_1 := hp;
  2154. exit;
  2155. end;
  2156. {$endif addstringopt}
  2157. end;
  2158. { otherwise, let addstring convert everything }
  2159. result := first_addstring;
  2160. exit;
  2161. end
  2162. { is one a real float ? }
  2163. else if (rd.deftype=floatdef) or (ld.deftype=floatdef) then
  2164. begin
  2165. {$ifdef cpufpemu}
  2166. if (aktfputype=fpu_soft) or (cs_fp_emulation in aktmoduleswitches) then
  2167. begin
  2168. result:=first_addfloat;
  2169. if assigned(result) then
  2170. exit;
  2171. end;
  2172. {$endif cpufpemu}
  2173. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2174. expectloc:=LOC_FPUREGISTER
  2175. else
  2176. expectloc:=LOC_FLAGS;
  2177. calcregisters(self,0,1,0);
  2178. { an add node always first loads both the left and the }
  2179. { right in the fpu before doing the calculation. However, }
  2180. { calcregisters(0,2,0) will overestimate the number of }
  2181. { necessary registers (it will make it 3 in case one of }
  2182. { the operands is already in the fpu) (JM) }
  2183. if ((left.expectloc<>LOC_FPUREGISTER) or
  2184. (right.expectloc<>LOC_FPUREGISTER)) and
  2185. (registersfpu < 2) then
  2186. inc(registersfpu);
  2187. end
  2188. { pointer comperation and subtraction }
  2189. else if (ld.deftype=pointerdef) then
  2190. begin
  2191. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2192. expectloc:=LOC_REGISTER
  2193. else
  2194. expectloc:=LOC_FLAGS;
  2195. calcregisters(self,1,0,0);
  2196. end
  2197. else if is_class_or_interface(ld) then
  2198. begin
  2199. expectloc:=LOC_FLAGS;
  2200. calcregisters(self,1,0,0);
  2201. end
  2202. else if (ld.deftype=classrefdef) then
  2203. begin
  2204. expectloc:=LOC_FLAGS;
  2205. calcregisters(self,1,0,0);
  2206. end
  2207. { support procvar=nil,procvar<>nil }
  2208. else if ((ld.deftype=procvardef) and (rt=niln)) or
  2209. ((rd.deftype=procvardef) and (lt=niln)) then
  2210. begin
  2211. expectloc:=LOC_FLAGS;
  2212. calcregisters(self,1,0,0);
  2213. end
  2214. {$ifdef SUPPORT_MMX}
  2215. { mmx support, this must be before the zero based array
  2216. check }
  2217. else if (cs_mmx in aktlocalswitches) and is_mmx_able_array(ld) and
  2218. is_mmx_able_array(rd) then
  2219. begin
  2220. expectloc:=LOC_MMXREGISTER;
  2221. calcregisters(self,0,0,1);
  2222. end
  2223. {$endif SUPPORT_MMX}
  2224. else if (rd.deftype=pointerdef) or (ld.deftype=pointerdef) then
  2225. begin
  2226. expectloc:=LOC_REGISTER;
  2227. calcregisters(self,1,0,0);
  2228. end
  2229. else if (rd.deftype=procvardef) and
  2230. (ld.deftype=procvardef) and
  2231. equal_defs(rd,ld) then
  2232. begin
  2233. expectloc:=LOC_FLAGS;
  2234. calcregisters(self,1,0,0);
  2235. end
  2236. else if (ld.deftype=enumdef) then
  2237. begin
  2238. expectloc:=LOC_FLAGS;
  2239. calcregisters(self,1,0,0);
  2240. end
  2241. {$ifdef SUPPORT_MMX}
  2242. else if (cs_mmx in aktlocalswitches) and
  2243. is_mmx_able_array(ld) and
  2244. is_mmx_able_array(rd) then
  2245. begin
  2246. expectloc:=LOC_MMXREGISTER;
  2247. calcregisters(self,0,0,1);
  2248. end
  2249. {$endif SUPPORT_MMX}
  2250. { the general solution is to convert to 32 bit int }
  2251. else
  2252. begin
  2253. expectloc:=LOC_REGISTER;
  2254. calcregisters(self,1,0,0);
  2255. end;
  2256. end;
  2257. {$ifdef state_tracking}
  2258. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2259. var factval:Tnode;
  2260. begin
  2261. track_state_pass:=false;
  2262. if left.track_state_pass(exec_known) then
  2263. begin
  2264. track_state_pass:=true;
  2265. left.resulttype.def:=nil;
  2266. do_resulttypepass(left);
  2267. end;
  2268. factval:=aktstate.find_fact(left);
  2269. if factval<>nil then
  2270. begin
  2271. track_state_pass:=true;
  2272. left.destroy;
  2273. left:=factval.getcopy;
  2274. end;
  2275. if right.track_state_pass(exec_known) then
  2276. begin
  2277. track_state_pass:=true;
  2278. right.resulttype.def:=nil;
  2279. do_resulttypepass(right);
  2280. end;
  2281. factval:=aktstate.find_fact(right);
  2282. if factval<>nil then
  2283. begin
  2284. track_state_pass:=true;
  2285. right.destroy;
  2286. right:=factval.getcopy;
  2287. end;
  2288. end;
  2289. {$endif}
  2290. begin
  2291. caddnode:=taddnode;
  2292. end.