nadd.pas 115 KB

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