nadd.pas 113 KB

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