nadd.pas 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799
  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. { stringconstn's can be arraydefs }
  1260. (lt=stringconstn) or
  1261. (rt=stringconstn) or
  1262. ((is_pchar(rd) or is_chararray(rd) or is_char(rd) or is_open_chararray(rd) or
  1263. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd)) and
  1264. (is_pchar(ld) or is_chararray(ld) or is_char(ld) or is_open_chararray(ld) or
  1265. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld))) then
  1266. begin
  1267. if (nodetype in [addn,equaln,unequaln,lten,gten,ltn,gtn]) then
  1268. begin
  1269. { Is there a widestring? }
  1270. if is_widestring(rd) or is_widestring(ld) or
  1271. is_pwidechar(rd) or is_widechararray(rd) or is_widechar(rd) or is_open_widechararray(rd) or
  1272. is_pwidechar(ld) or is_widechararray(ld) or is_widechar(ld) or is_open_widechararray(ld) then
  1273. strtype:= st_widestring
  1274. else
  1275. if is_ansistring(rd) or is_ansistring(ld) or
  1276. ((cs_ansistrings in current_settings.localswitches) and
  1277. //todo: Move some of this to longstring's then they are implemented?
  1278. (
  1279. is_pchar(rd) or (is_chararray(rd) and (rd.size > 255)) or is_open_chararray(rd) or
  1280. is_pchar(ld) or (is_chararray(ld) and (ld.size > 255)) or is_open_chararray(ld)
  1281. )
  1282. ) then
  1283. strtype:= st_ansistring
  1284. else
  1285. if is_longstring(rd) or is_longstring(ld) then
  1286. strtype:= st_longstring
  1287. else
  1288. begin
  1289. {$warning todo: add a warning/hint here if one converting a too large array}
  1290. { nodes is PChar, array [with size > 255] or OpenArrayOfChar.
  1291. Note: Delphi halts with error if "array [0..xx] of char"
  1292. is assigned to ShortString and string length is less
  1293. then array size }
  1294. strtype:= st_shortstring;
  1295. end;
  1296. // Now convert nodes to common string type
  1297. case strtype of
  1298. st_widestring :
  1299. begin
  1300. if not(is_widestring(rd)) then
  1301. inserttypeconv(right,cwidestringtype);
  1302. if not(is_widestring(ld)) then
  1303. inserttypeconv(left,cwidestringtype);
  1304. end;
  1305. st_ansistring :
  1306. begin
  1307. if not(is_ansistring(rd)) then
  1308. inserttypeconv(right,cansistringtype);
  1309. if not(is_ansistring(ld)) then
  1310. inserttypeconv(left,cansistringtype);
  1311. end;
  1312. st_longstring :
  1313. begin
  1314. if not(is_longstring(rd)) then
  1315. inserttypeconv(right,clongstringtype);
  1316. if not(is_longstring(ld)) then
  1317. inserttypeconv(left,clongstringtype);
  1318. end;
  1319. st_shortstring :
  1320. begin
  1321. if not(is_shortstring(ld)) then
  1322. inserttypeconv(left,cshortstringtype);
  1323. { don't convert char, that can be handled by the optimized node }
  1324. if not(is_shortstring(rd) or is_char(rd)) then
  1325. inserttypeconv(right,cshortstringtype);
  1326. end;
  1327. else
  1328. internalerror(2005101);
  1329. end;
  1330. end
  1331. else
  1332. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1333. end
  1334. { class or interface equation }
  1335. else if is_class_or_interface(rd) or is_class_or_interface(ld) then
  1336. begin
  1337. if (nodetype in [equaln,unequaln]) then
  1338. begin
  1339. if is_class_or_interface(rd) and is_class_or_interface(ld) then
  1340. begin
  1341. if tobjectdef(rd).is_related(tobjectdef(ld)) then
  1342. inserttypeconv(right,left.resultdef)
  1343. else
  1344. inserttypeconv(left,right.resultdef);
  1345. end
  1346. else if is_class_or_interface(rd) then
  1347. inserttypeconv(left,right.resultdef)
  1348. else
  1349. inserttypeconv(right,left.resultdef);
  1350. end
  1351. else
  1352. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1353. end
  1354. else if (rd.typ=classrefdef) and (ld.typ=classrefdef) then
  1355. begin
  1356. if (nodetype in [equaln,unequaln]) then
  1357. begin
  1358. if tobjectdef(tclassrefdef(rd).pointeddef).is_related(
  1359. tobjectdef(tclassrefdef(ld).pointeddef)) then
  1360. inserttypeconv(right,left.resultdef)
  1361. else
  1362. inserttypeconv(left,right.resultdef);
  1363. end
  1364. else
  1365. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1366. end
  1367. { allows comperasion with nil pointer }
  1368. else if is_class_or_interface(rd) or (rd.typ=classrefdef) then
  1369. begin
  1370. if (nodetype in [equaln,unequaln]) then
  1371. inserttypeconv(left,right.resultdef)
  1372. else
  1373. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1374. end
  1375. else if is_class_or_interface(ld) or (ld.typ=classrefdef) then
  1376. begin
  1377. if (nodetype in [equaln,unequaln]) then
  1378. inserttypeconv(right,left.resultdef)
  1379. else
  1380. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1381. end
  1382. { support procvar=nil,procvar<>nil }
  1383. else if ((ld.typ=procvardef) and (rt=niln)) or
  1384. ((rd.typ=procvardef) and (lt=niln)) then
  1385. begin
  1386. if not(nodetype in [equaln,unequaln]) then
  1387. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1388. { find proc field in methodpointer record }
  1389. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1390. if not assigned(hsym) then
  1391. internalerror(200412043);
  1392. { For methodpointers compare only tmethodpointer.proc }
  1393. if (rd.typ=procvardef) and
  1394. (not tprocvardef(rd).is_addressonly) then
  1395. begin
  1396. right:=csubscriptnode.create(
  1397. hsym,
  1398. ctypeconvnode.create_internal(right,methodpointertype));
  1399. typecheckpass(right);
  1400. end;
  1401. if (ld.typ=procvardef) and
  1402. (not tprocvardef(ld).is_addressonly) then
  1403. begin
  1404. left:=csubscriptnode.create(
  1405. hsym,
  1406. ctypeconvnode.create_internal(left,methodpointertype));
  1407. typecheckpass(left);
  1408. end;
  1409. end
  1410. { support dynamicarray=nil,dynamicarray<>nil }
  1411. else if (is_dynamic_array(ld) and (rt=niln)) or
  1412. (is_dynamic_array(rd) and (lt=niln)) or
  1413. (is_dynamic_array(ld) and is_dynamic_array(rd)) then
  1414. begin
  1415. if not(nodetype in [equaln,unequaln]) then
  1416. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1417. end
  1418. {$ifdef SUPPORT_MMX}
  1419. { mmx support, this must be before the zero based array
  1420. check }
  1421. else if (cs_mmx in current_settings.localswitches) and
  1422. is_mmx_able_array(ld) and
  1423. is_mmx_able_array(rd) and
  1424. equal_defs(ld,rd) then
  1425. begin
  1426. case nodetype of
  1427. addn,subn,xorn,orn,andn:
  1428. ;
  1429. { mul is a little bit restricted }
  1430. muln:
  1431. if not(mmx_type(ld) in [mmxu16bit,mmxs16bit,mmxfixed16]) then
  1432. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1433. else
  1434. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1435. end;
  1436. end
  1437. {$endif SUPPORT_MMX}
  1438. { vector support, this must be before the zero based array
  1439. check }
  1440. else if (cs_support_vectors in current_settings.globalswitches) and
  1441. is_vector(ld) and
  1442. is_vector(rd) and
  1443. equal_defs(ld,rd) then
  1444. begin
  1445. if not(nodetype in [addn,subn,xorn,orn,andn,muln,slashn]) then
  1446. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1447. { both defs must be equal, so taking left or right as resultdef doesn't matter }
  1448. resultdef:=left.resultdef;
  1449. end
  1450. { this is a little bit dangerous, also the left type }
  1451. { pointer to should be checked! This broke the mmx support }
  1452. else if (rd.typ=pointerdef) or
  1453. (is_zero_based_array(rd) and (rt<>stringconstn)) then
  1454. begin
  1455. if is_zero_based_array(rd) then
  1456. begin
  1457. resultdef:=tpointerdef.create(tarraydef(rd).elementdef);
  1458. inserttypeconv(right,resultdef);
  1459. end
  1460. else
  1461. resultdef:=right.resultdef;
  1462. inserttypeconv(left,sinttype);
  1463. if nodetype=addn then
  1464. begin
  1465. if not(cs_extsyntax in current_settings.moduleswitches) or
  1466. (not(is_pchar(ld)) and not(m_add_pointer in current_settings.modeswitches)) then
  1467. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1468. if (rd.typ=pointerdef) and
  1469. (tpointerdef(rd).pointeddef.size>1) then
  1470. begin
  1471. left:=caddnode.create(muln,left,
  1472. cordconstnode.create(tpointerdef(rd).pointeddef.size,sinttype,true));
  1473. typecheckpass(left);
  1474. end;
  1475. end
  1476. else
  1477. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1478. end
  1479. else if (ld.typ=pointerdef) or
  1480. (is_zero_based_array(ld) and (lt<>stringconstn)) then
  1481. begin
  1482. if is_zero_based_array(ld) then
  1483. begin
  1484. resultdef:=tpointerdef.create(tarraydef(ld).elementdef);
  1485. inserttypeconv(left,resultdef);
  1486. end
  1487. else
  1488. resultdef:=left.resultdef;
  1489. inserttypeconv(right,sinttype);
  1490. if nodetype in [addn,subn] then
  1491. begin
  1492. if not(cs_extsyntax in current_settings.moduleswitches) or
  1493. (not(is_pchar(ld)) and not(m_add_pointer in current_settings.modeswitches)) then
  1494. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1495. if (ld.typ=pointerdef) then
  1496. begin
  1497. if is_big_untyped_addrnode(left) then
  1498. CGMessage1(type_w_untyped_arithmetic_unportable,node2opstr(nodetype));
  1499. if (tpointerdef(ld).pointeddef.size>1) then
  1500. begin
  1501. right:=caddnode.create(muln,right,
  1502. cordconstnode.create(tpointerdef(ld).pointeddef.size,sinttype,true));
  1503. typecheckpass(right);
  1504. end
  1505. end else
  1506. if is_zero_based_array(ld) and
  1507. (tarraydef(ld).elementdef.size>1) then
  1508. begin
  1509. right:=caddnode.create(muln,right,
  1510. cordconstnode.create(tarraydef(ld).elementdef.size,sinttype,true));
  1511. typecheckpass(right);
  1512. end;
  1513. end
  1514. else
  1515. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1516. end
  1517. else if (rd.typ=procvardef) and
  1518. (ld.typ=procvardef) and
  1519. equal_defs(rd,ld) then
  1520. begin
  1521. if (nodetype in [equaln,unequaln]) then
  1522. begin
  1523. if tprocvardef(rd).is_addressonly then
  1524. begin
  1525. inserttypeconv_internal(right,voidpointertype);
  1526. inserttypeconv_internal(left,voidpointertype);
  1527. end
  1528. else
  1529. begin
  1530. { find proc field in methodpointer record }
  1531. hsym:=tfieldvarsym(trecorddef(methodpointertype).symtable.Find('proc'));
  1532. if not assigned(hsym) then
  1533. internalerror(200412043);
  1534. { Compare tmehodpointer(left).proc }
  1535. right:=csubscriptnode.create(
  1536. hsym,
  1537. ctypeconvnode.create_internal(right,methodpointertype));
  1538. typecheckpass(right);
  1539. left:=csubscriptnode.create(
  1540. hsym,
  1541. ctypeconvnode.create_internal(left,methodpointertype));
  1542. typecheckpass(left);
  1543. end;
  1544. end
  1545. else
  1546. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1547. end
  1548. { enums }
  1549. else if (ld.typ=enumdef) and (rd.typ=enumdef) then
  1550. begin
  1551. if allowenumop(nodetype) then
  1552. inserttypeconv(right,left.resultdef)
  1553. else
  1554. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),ld.typename,rd.typename);
  1555. end
  1556. { generic conversion, this is for error recovery }
  1557. else
  1558. begin
  1559. inserttypeconv(left,sinttype);
  1560. inserttypeconv(right,sinttype);
  1561. end;
  1562. { set resultdef if not already done }
  1563. if not assigned(resultdef) then
  1564. begin
  1565. case nodetype of
  1566. ltn,lten,gtn,gten,equaln,unequaln :
  1567. resultdef:=booltype;
  1568. slashn :
  1569. resultdef:=resultrealdef;
  1570. addn:
  1571. begin
  1572. { for strings, return is always a 255 char string }
  1573. if is_shortstring(left.resultdef) then
  1574. resultdef:=cshortstringtype
  1575. else
  1576. resultdef:=left.resultdef;
  1577. end;
  1578. else
  1579. resultdef:=left.resultdef;
  1580. end;
  1581. end;
  1582. { when the result is currency we need some extra code for
  1583. multiplication and division. this should not be done when
  1584. the muln or slashn node is created internally }
  1585. if not(nf_is_currency in flags) and
  1586. is_currency(resultdef) then
  1587. begin
  1588. case nodetype of
  1589. slashn :
  1590. begin
  1591. { slashn will only work with floats }
  1592. hp:=caddnode.create(muln,getcopy,crealconstnode.create(10000.0,s64currencytype));
  1593. include(hp.flags,nf_is_currency);
  1594. result:=hp;
  1595. end;
  1596. muln :
  1597. begin
  1598. if s64currencytype.typ=floatdef then
  1599. hp:=caddnode.create(slashn,getcopy,crealconstnode.create(10000.0,s64currencytype))
  1600. else
  1601. hp:=cmoddivnode.create(divn,getcopy,cordconstnode.create(10000,s64currencytype,false));
  1602. include(hp.flags,nf_is_currency);
  1603. result:=hp
  1604. end;
  1605. end;
  1606. end;
  1607. end;
  1608. function taddnode.first_addstring: tnode;
  1609. const
  1610. swap_relation: array [ltn..unequaln] of Tnodetype=(gtn, gten, ltn, lten, equaln, unequaln);
  1611. var
  1612. p: tnode;
  1613. newstatement : tstatementnode;
  1614. tempnode (*,tempnode2*) : ttempcreatenode;
  1615. cmpfuncname: string;
  1616. begin
  1617. { when we get here, we are sure that both the left and the right }
  1618. { node are both strings of the same stringtype (JM) }
  1619. case nodetype of
  1620. addn:
  1621. begin
  1622. if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
  1623. begin
  1624. result:=right;
  1625. left.free;
  1626. left:=nil;
  1627. right:=nil;
  1628. exit;
  1629. end;
  1630. if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
  1631. begin
  1632. result:=left;
  1633. left:=nil;
  1634. right.free;
  1635. right:=nil;
  1636. exit;
  1637. end;
  1638. { create the call to the concat routine both strings as arguments }
  1639. if assigned(aktassignmentnode) and
  1640. (aktassignmentnode.right=self) and
  1641. (aktassignmentnode.left.resultdef=resultdef) and
  1642. valid_for_var(aktassignmentnode.left,false) then
  1643. begin
  1644. result:=ccallnode.createintern('fpc_'+
  1645. tstringdef(resultdef).stringtypname+'_concat',
  1646. ccallparanode.create(right,
  1647. ccallparanode.create(left,
  1648. ccallparanode.create(aktassignmentnode.left.getcopy,nil))));
  1649. include(aktassignmentnode.flags,nf_assign_done_in_right);
  1650. firstpass(result);
  1651. end
  1652. else
  1653. begin
  1654. result:=internalstatements(newstatement);
  1655. tempnode:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1656. addstatement(newstatement,tempnode);
  1657. addstatement(newstatement,ccallnode.createintern('fpc_'+
  1658. tstringdef(resultdef).stringtypname+'_concat',
  1659. ccallparanode.create(right,
  1660. ccallparanode.create(left,
  1661. ccallparanode.create(ctemprefnode.create(tempnode),nil)))));
  1662. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1663. addstatement(newstatement,ctemprefnode.create(tempnode));
  1664. end;
  1665. { we reused the arguments }
  1666. left := nil;
  1667. right := nil;
  1668. end;
  1669. ltn,lten,gtn,gten,equaln,unequaln :
  1670. begin
  1671. { generate better code for comparison with empty string, we
  1672. only need to compare the length with 0 }
  1673. if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
  1674. { windows widestrings are too complicated to be handled optimized }
  1675. not(is_widestring(left.resultdef) and (target_info.system in system_windows)) and
  1676. (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
  1677. ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
  1678. begin
  1679. { switch so that the constant is always on the right }
  1680. if left.nodetype = stringconstn then
  1681. begin
  1682. p := left;
  1683. left := right;
  1684. right := p;
  1685. nodetype:=swap_relation[nodetype];
  1686. end;
  1687. if is_shortstring(left.resultdef) or
  1688. (nodetype in [gtn,gten,ltn,lten]) then
  1689. { compare the length with 0 }
  1690. result := caddnode.create(nodetype,
  1691. cinlinenode.create(in_length_x,false,left),
  1692. cordconstnode.create(0,s32inttype,false))
  1693. else
  1694. begin
  1695. (*
  1696. if is_widestring(left.resultdef) and
  1697. (target_info.system in system_windows) then
  1698. begin
  1699. { windows like widestrings requires that we also check the length }
  1700. result:=internalstatements(newstatement);
  1701. tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
  1702. tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1703. addstatement(newstatement,tempnode);
  1704. addstatement(newstatement,tempnode2);
  1705. { poor man's cse }
  1706. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
  1707. ctypeconvnode.create_internal(left,voidpointertype))
  1708. );
  1709. addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
  1710. caddnode.create(orn,
  1711. caddnode.create(nodetype,
  1712. ctemprefnode.create(tempnode),
  1713. cpointerconstnode.create(0,voidpointertype)
  1714. ),
  1715. caddnode.create(nodetype,
  1716. ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
  1717. cordconstnode.create(0,s32inttype,false)
  1718. )
  1719. )
  1720. ));
  1721. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  1722. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
  1723. addstatement(newstatement,ctemprefnode.create(tempnode2));
  1724. end
  1725. else
  1726. *)
  1727. begin
  1728. { compare the pointer with nil (for ansistrings etc), }
  1729. { faster than getting the length (JM) }
  1730. result:= caddnode.create(nodetype,
  1731. ctypeconvnode.create_internal(left,voidpointertype),
  1732. cpointerconstnode.create(0,voidpointertype));
  1733. end;
  1734. end;
  1735. { left is reused }
  1736. left := nil;
  1737. { right isn't }
  1738. right.free;
  1739. right := nil;
  1740. exit;
  1741. end;
  1742. { no string constant -> call compare routine }
  1743. cmpfuncname := 'fpc_'+tstringdef(left.resultdef).stringtypname+'_compare';
  1744. { for equality checks use optimized version }
  1745. if nodetype in [equaln,unequaln] then
  1746. cmpfuncname := cmpfuncname + '_equal';
  1747. result := ccallnode.createintern(cmpfuncname,
  1748. ccallparanode.create(right,ccallparanode.create(left,nil)));
  1749. { and compare its result with 0 according to the original operator }
  1750. result := caddnode.create(nodetype,result,
  1751. cordconstnode.create(0,s32inttype,false));
  1752. left := nil;
  1753. right := nil;
  1754. end;
  1755. end;
  1756. end;
  1757. function taddnode.first_addset : tnode;
  1758. procedure call_varset_helper(const n : string);
  1759. var
  1760. newstatement : tstatementnode;
  1761. temp : ttempcreatenode;
  1762. begin
  1763. { add two var sets }
  1764. result:=internalstatements(newstatement);
  1765. { create temp for result }
  1766. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1767. addstatement(newstatement,temp);
  1768. addstatement(newstatement,ccallnode.createintern(n,
  1769. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1770. ccallparanode.create(ctemprefnode.create(temp),
  1771. ccallparanode.create(right,
  1772. ccallparanode.create(left,nil)))))
  1773. );
  1774. { remove reused parts from original node }
  1775. left:=nil;
  1776. right:=nil;
  1777. { the last statement should return the value as
  1778. location and type, this is done be referencing the
  1779. temp and converting it first from a persistent temp to
  1780. normal temp }
  1781. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1782. addstatement(newstatement,ctemprefnode.create(temp));
  1783. end;
  1784. var
  1785. procname: string[31];
  1786. tempn: tnode;
  1787. paras: tcallparanode;
  1788. srsym: ttypesym;
  1789. newstatement : tstatementnode;
  1790. temp : ttempcreatenode;
  1791. begin
  1792. if (is_varset(left.resultdef) or is_varset(right.resultdef)) then
  1793. begin
  1794. case nodetype of
  1795. equaln,unequaln,lten,gten:
  1796. begin
  1797. case nodetype of
  1798. equaln,unequaln:
  1799. procname := 'fpc_varset_comp_sets';
  1800. lten,gten:
  1801. begin
  1802. procname := 'fpc_varset_contains_sets';
  1803. { (left >= right) = (right <= left) }
  1804. if nodetype = gten then
  1805. begin
  1806. tempn := left;
  1807. left := right;
  1808. right := tempn;
  1809. end;
  1810. end;
  1811. end;
  1812. result := ccallnode.createinternres(procname,
  1813. ccallparanode.create(cordconstnode.create(left.resultdef.size,sinttype,false),
  1814. ccallparanode.create(right,
  1815. ccallparanode.create(left,nil))),resultdef);
  1816. { left and right are reused as parameters }
  1817. left := nil;
  1818. right := nil;
  1819. { for an unequaln, we have to negate the result of comp_sets }
  1820. if nodetype = unequaln then
  1821. result := cnotnode.create(result);
  1822. end;
  1823. addn:
  1824. begin
  1825. { optimize first loading of a set }
  1826. if (right.nodetype=setelementn) and
  1827. not(assigned(tsetelementnode(right).right)) and
  1828. is_emptyset(left) then
  1829. begin
  1830. result:=internalstatements(newstatement);
  1831. { create temp for result }
  1832. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1833. addstatement(newstatement,temp);
  1834. addstatement(newstatement,ccallnode.createintern('fpc_varset_create_element',
  1835. ccallparanode.create(ctemprefnode.create(temp),
  1836. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1837. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),nil))))
  1838. );
  1839. { the last statement should return the value as
  1840. location and type, this is done be referencing the
  1841. temp and converting it first from a persistent temp to
  1842. normal temp }
  1843. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1844. addstatement(newstatement,ctemprefnode.create(temp));
  1845. tsetelementnode(right).left := nil;
  1846. end
  1847. else
  1848. begin
  1849. if right.nodetype=setelementn then
  1850. begin
  1851. result:=internalstatements(newstatement);
  1852. { create temp for result }
  1853. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  1854. addstatement(newstatement,temp);
  1855. { add a range or a single element? }
  1856. if assigned(tsetelementnode(right).right) then
  1857. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  1858. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1859. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  1860. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  1861. ccallparanode.create(ctemprefnode.create(temp),
  1862. ccallparanode.create(left,nil))))))
  1863. )
  1864. else
  1865. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  1866. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  1867. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  1868. ccallparanode.create(ctemprefnode.create(temp),
  1869. ccallparanode.create(left,nil)))))
  1870. );
  1871. { remove reused parts from original node }
  1872. tsetelementnode(right).right:=nil;
  1873. tsetelementnode(right).left:=nil;
  1874. left:=nil;
  1875. { the last statement should return the value as
  1876. location and type, this is done be referencing the
  1877. temp and converting it first from a persistent temp to
  1878. normal temp }
  1879. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  1880. addstatement(newstatement,ctemprefnode.create(temp));
  1881. end
  1882. else
  1883. call_varset_helper('fpc_varset_add_sets');
  1884. end
  1885. end;
  1886. subn:
  1887. call_varset_helper('fpc_varset_sub_sets');
  1888. symdifn:
  1889. call_varset_helper('fpc_varset_symdif_sets');
  1890. muln:
  1891. call_varset_helper('fpc_varset_mul_sets');
  1892. else
  1893. internalerror(200609241);
  1894. end;
  1895. end
  1896. else
  1897. begin
  1898. { get the sym that represents the fpc_normal_set type }
  1899. srsym:=search_system_type('FPC_NORMAL_SET');
  1900. case nodetype of
  1901. equaln,unequaln,lten,gten:
  1902. begin
  1903. case nodetype of
  1904. equaln,unequaln:
  1905. procname := 'fpc_set_comp_sets';
  1906. lten,gten:
  1907. begin
  1908. procname := 'fpc_set_contains_sets';
  1909. { (left >= right) = (right <= left) }
  1910. if nodetype = gten then
  1911. begin
  1912. tempn := left;
  1913. left := right;
  1914. right := tempn;
  1915. end;
  1916. end;
  1917. end;
  1918. { convert the arguments (explicitely) to fpc_normal_set's }
  1919. left := ctypeconvnode.create_internal(left,srsym.typedef);
  1920. right := ctypeconvnode.create_internal(right,srsym.typedef);
  1921. result := ccallnode.createintern(procname,ccallparanode.create(right,
  1922. ccallparanode.create(left,nil)));
  1923. { left and right are reused as parameters }
  1924. left := nil;
  1925. right := nil;
  1926. { for an unequaln, we have to negate the result of comp_sets }
  1927. if nodetype = unequaln then
  1928. result := cnotnode.create(result);
  1929. end;
  1930. addn:
  1931. begin
  1932. { optimize first loading of a set }
  1933. if (right.nodetype=setelementn) and
  1934. not(assigned(tsetelementnode(right).right)) and
  1935. is_emptyset(left) then
  1936. begin
  1937. { type cast the value to pass as argument to a byte, }
  1938. { since that's what the helper expects }
  1939. tsetelementnode(right).left :=
  1940. ctypeconvnode.create_internal(tsetelementnode(right).left,u8inttype);
  1941. { set the resultdef to the actual one (otherwise it's }
  1942. { "fpc_normal_set") }
  1943. result := ccallnode.createinternres('fpc_set_create_element',
  1944. ccallparanode.create(tsetelementnode(right).left,nil),
  1945. resultdef);
  1946. { reused }
  1947. tsetelementnode(right).left := nil;
  1948. end
  1949. else
  1950. begin
  1951. if right.nodetype=setelementn then
  1952. begin
  1953. { convert the arguments to bytes, since that's what }
  1954. { the helper expects }
  1955. tsetelementnode(right).left :=
  1956. ctypeconvnode.create_internal(tsetelementnode(right).left,
  1957. u8inttype);
  1958. { convert the original set (explicitely) to an }
  1959. { fpc_normal_set so we can pass it to the helper }
  1960. left := ctypeconvnode.create_internal(left,srsym.typedef);
  1961. { add a range or a single element? }
  1962. if assigned(tsetelementnode(right).right) then
  1963. begin
  1964. tsetelementnode(right).right :=
  1965. ctypeconvnode.create_internal(tsetelementnode(right).right,
  1966. u8inttype);
  1967. { create the call }
  1968. result := ccallnode.createinternres('fpc_set_set_range',
  1969. ccallparanode.create(tsetelementnode(right).right,
  1970. ccallparanode.create(tsetelementnode(right).left,
  1971. ccallparanode.create(left,nil))),resultdef);
  1972. end
  1973. else
  1974. begin
  1975. result := ccallnode.createinternres('fpc_set_set_byte',
  1976. ccallparanode.create(tsetelementnode(right).left,
  1977. ccallparanode.create(left,nil)),resultdef);
  1978. end;
  1979. { remove reused parts from original node }
  1980. tsetelementnode(right).right := nil;
  1981. tsetelementnode(right).left := nil;
  1982. left := nil;
  1983. end
  1984. else
  1985. begin
  1986. { add two sets }
  1987. { convert the sets to fpc_normal_set's }
  1988. result := ccallnode.createinternres('fpc_set_add_sets',
  1989. ccallparanode.create(
  1990. ctypeconvnode.create_explicit(right,srsym.typedef),
  1991. ccallparanode.create(
  1992. ctypeconvnode.create_internal(left,srsym.typedef),nil)),resultdef);
  1993. { remove reused parts from original node }
  1994. left := nil;
  1995. right := nil;
  1996. end;
  1997. end
  1998. end;
  1999. subn,symdifn,muln:
  2000. begin
  2001. { convert the sets to fpc_normal_set's }
  2002. paras := ccallparanode.create(ctypeconvnode.create_internal(right,srsym.typedef),
  2003. ccallparanode.create(ctypeconvnode.create_internal(left,srsym.typedef),nil));
  2004. case nodetype of
  2005. subn:
  2006. result := ccallnode.createinternres('fpc_set_sub_sets',
  2007. paras,resultdef);
  2008. symdifn:
  2009. result := ccallnode.createinternres('fpc_set_symdif_sets',
  2010. paras,resultdef);
  2011. muln:
  2012. result := ccallnode.createinternres('fpc_set_mul_sets',
  2013. paras,resultdef);
  2014. end;
  2015. { remove reused parts from original node }
  2016. left := nil;
  2017. right := nil;
  2018. end;
  2019. else
  2020. internalerror(200108311);
  2021. end;
  2022. end;
  2023. end;
  2024. function taddnode.use_generic_mul32to64: boolean;
  2025. begin
  2026. result := true;
  2027. end;
  2028. function taddnode.try_make_mul32to64: boolean;
  2029. function canbe32bitint(v: tconstexprint): boolean;
  2030. begin
  2031. result := ((v >= int64(low(longint))) and (v <= int64(high(longint)))) or
  2032. ((v >= qword(low(cardinal))) and (v <= qword(high(cardinal))))
  2033. end;
  2034. var
  2035. temp: tnode;
  2036. begin
  2037. result := false;
  2038. if ((left.nodetype = typeconvn) and
  2039. is_integer(ttypeconvnode(left).left.resultdef) and
  2040. (not(torddef(ttypeconvnode(left).left.resultdef).ordtype in [u64bit,s64bit])) and
  2041. (((right.nodetype = ordconstn) and canbe32bitint(tordconstnode(right).value)) or
  2042. ((right.nodetype = typeconvn) and
  2043. is_integer(ttypeconvnode(right).left.resultdef) and
  2044. not(torddef(ttypeconvnode(right).left.resultdef).ordtype in [u64bit,s64bit])) and
  2045. (is_signed(ttypeconvnode(left).left.resultdef) =
  2046. is_signed(ttypeconvnode(right).left.resultdef)))) then
  2047. begin
  2048. temp := ttypeconvnode(left).left;
  2049. ttypeconvnode(left).left := nil;
  2050. left.free;
  2051. left := temp;
  2052. if (right.nodetype = typeconvn) then
  2053. begin
  2054. temp := ttypeconvnode(right).left;
  2055. ttypeconvnode(right).left := nil;
  2056. right.free;
  2057. right := temp;
  2058. end;
  2059. if (is_signed(left.resultdef)) then
  2060. begin
  2061. inserttypeconv(left,s32inttype);
  2062. inserttypeconv(right,s32inttype);
  2063. end
  2064. else
  2065. begin
  2066. inserttypeconv(left,u32inttype);
  2067. inserttypeconv(right,u32inttype);
  2068. end;
  2069. firstpass(left);
  2070. firstpass(right);
  2071. result := true;
  2072. end;
  2073. end;
  2074. function taddnode.first_add64bitint: tnode;
  2075. var
  2076. procname: string[31];
  2077. temp: tnode;
  2078. power: longint;
  2079. begin
  2080. result := nil;
  2081. { create helper calls mul }
  2082. if nodetype <> muln then
  2083. exit;
  2084. { make sure that if there is a constant, that it's on the right }
  2085. if left.nodetype = ordconstn then
  2086. begin
  2087. temp := right;
  2088. right := left;
  2089. left := temp;
  2090. end;
  2091. { can we use a shift instead of a mul? }
  2092. if not (cs_check_overflow in current_settings.localswitches) and
  2093. (right.nodetype = ordconstn) and
  2094. ispowerof2(tordconstnode(right).value,power) then
  2095. begin
  2096. tordconstnode(right).value := power;
  2097. result := cshlshrnode.create(shln,left,right);
  2098. { left and right are reused }
  2099. left := nil;
  2100. right := nil;
  2101. { return firstpassed new node }
  2102. exit;
  2103. end;
  2104. if not(use_generic_mul32to64) and
  2105. try_make_mul32to64 then
  2106. exit;
  2107. { when currency is used set the result of the
  2108. parameters to s64bit, so they are not converted }
  2109. if is_currency(resultdef) then
  2110. begin
  2111. left.resultdef:=s64inttype;
  2112. right.resultdef:=s64inttype;
  2113. end;
  2114. { otherwise, create the parameters for the helper }
  2115. right := ccallparanode.create(
  2116. cordconstnode.create(ord(cs_check_overflow in current_settings.localswitches),booltype,true),
  2117. ccallparanode.create(right,ccallparanode.create(left,nil)));
  2118. left := nil;
  2119. { only qword needs the unsigned code, the
  2120. signed code is also used for currency }
  2121. if is_signed(resultdef) then
  2122. procname := 'fpc_mul_int64'
  2123. else
  2124. procname := 'fpc_mul_qword';
  2125. result := ccallnode.createintern(procname,right);
  2126. right := nil;
  2127. end;
  2128. function taddnode.first_addfloat : tnode;
  2129. var
  2130. procname: string[31];
  2131. { do we need to reverse the result ? }
  2132. notnode : boolean;
  2133. fdef : tdef;
  2134. begin
  2135. result := nil;
  2136. notnode := false;
  2137. { In non-emulation mode, real opcodes are
  2138. emitted for floating point values.
  2139. }
  2140. if not (cs_fp_emulation in current_settings.moduleswitches) then
  2141. exit;
  2142. if not(target_info.system in system_wince) then
  2143. begin
  2144. case tfloatdef(left.resultdef).floattype of
  2145. s32real:
  2146. begin
  2147. fdef:=search_system_type('FLOAT32REC').typedef;
  2148. procname:='float32';
  2149. end;
  2150. s64real:
  2151. begin
  2152. fdef:=search_system_type('FLOAT64').typedef;
  2153. procname:='float64';
  2154. end;
  2155. {!!! not yet implemented
  2156. s128real:
  2157. }
  2158. else
  2159. internalerror(2005082601);
  2160. end;
  2161. case nodetype of
  2162. addn:
  2163. procname:=procname+'_add';
  2164. muln:
  2165. procname:=procname+'_mul';
  2166. subn:
  2167. procname:=procname+'_sub';
  2168. slashn:
  2169. procname:=procname+'_div';
  2170. ltn:
  2171. procname:=procname+'_lt';
  2172. lten:
  2173. procname:=procname+'_le';
  2174. gtn:
  2175. begin
  2176. procname:=procname+'_le';
  2177. notnode:=true;
  2178. end;
  2179. gten:
  2180. begin
  2181. procname:=procname+'_lt';
  2182. notnode:=true;
  2183. end;
  2184. equaln:
  2185. procname:=procname+'_eq';
  2186. unequaln:
  2187. begin
  2188. procname:=procname+'_eq';
  2189. notnode:=true;
  2190. end;
  2191. else
  2192. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2193. end;
  2194. end
  2195. else
  2196. begin
  2197. case nodetype of
  2198. addn:
  2199. procname:='ADD';
  2200. muln:
  2201. procname:='MUL';
  2202. subn:
  2203. procname:='SUB';
  2204. slashn:
  2205. procname:='DIV';
  2206. ltn:
  2207. procname:='LT';
  2208. lten:
  2209. procname:='LE';
  2210. gtn:
  2211. procname:='GT';
  2212. gten:
  2213. procname:='GE';
  2214. equaln:
  2215. procname:='EQ';
  2216. unequaln:
  2217. procname:='NE';
  2218. else
  2219. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  2220. end;
  2221. case tfloatdef(left.resultdef).floattype of
  2222. s32real:
  2223. begin
  2224. procname:=procname+'S';
  2225. if nodetype in [addn,muln,subn,slashn] then
  2226. procname:=lower(procname);
  2227. end;
  2228. s64real:
  2229. procname:=procname+'D';
  2230. {!!! not yet implemented
  2231. s128real:
  2232. }
  2233. else
  2234. internalerror(2005082602);
  2235. end;
  2236. end;
  2237. { cast softfpu result? }
  2238. if not(target_info.system in system_wince) then
  2239. begin
  2240. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2241. resultdef:=booltype;
  2242. result:=ctypeconvnode.create_internal(ccallnode.createintern(procname,ccallparanode.create(
  2243. ctypeconvnode.create_internal(right,fdef),
  2244. ccallparanode.create(
  2245. ctypeconvnode.create_internal(left,fdef),nil))),resultdef);
  2246. end
  2247. else
  2248. result:=ccallnode.createintern(procname,ccallparanode.create(right,
  2249. ccallparanode.create(left,nil)));
  2250. left:=nil;
  2251. right:=nil;
  2252. { do we need to reverse the result }
  2253. if notnode then
  2254. result:=cnotnode.create(result);
  2255. end;
  2256. function taddnode.pass_1 : tnode;
  2257. var
  2258. {$ifdef addstringopt}
  2259. hp : tnode;
  2260. {$endif addstringopt}
  2261. lt,rt : tnodetype;
  2262. rd,ld : tdef;
  2263. newstatement : tstatementnode;
  2264. temp : ttempcreatenode;
  2265. begin
  2266. result:=nil;
  2267. { Can we optimize multiple string additions into a single call?
  2268. This need to be done on a complete tree to detect the multiple
  2269. add nodes and is therefor done before the subtrees are processed }
  2270. if canbemultistringadd(self) then
  2271. begin
  2272. result := genmultistringadd(self);
  2273. exit;
  2274. end;
  2275. { first do the two subtrees }
  2276. firstpass(left);
  2277. firstpass(right);
  2278. if codegenerror then
  2279. exit;
  2280. { load easier access variables }
  2281. rd:=right.resultdef;
  2282. ld:=left.resultdef;
  2283. rt:=right.nodetype;
  2284. lt:=left.nodetype;
  2285. { int/int gives real/real! }
  2286. if nodetype=slashn then
  2287. begin
  2288. {$ifdef cpufpemu}
  2289. if (current_settings.fputype=fpu_soft) or (cs_fp_emulation in current_settings.moduleswitches) then
  2290. begin
  2291. result:=first_addfloat;
  2292. if assigned(result) then
  2293. exit;
  2294. end;
  2295. {$endif cpufpemu}
  2296. expectloc:=LOC_FPUREGISTER;
  2297. { maybe we need an integer register to save }
  2298. { a reference }
  2299. if ((left.expectloc<>LOC_FPUREGISTER) or
  2300. (right.expectloc<>LOC_FPUREGISTER)) and
  2301. (left.registersint=right.registersint) then
  2302. calcregisters(self,1,1,0)
  2303. else
  2304. calcregisters(self,0,1,0);
  2305. { an add node always first loads both the left and the }
  2306. { right in the fpu before doing the calculation. However, }
  2307. { calcregisters(0,2,0) will overestimate the number of }
  2308. { necessary registers (it will make it 3 in case one of }
  2309. { the operands is already in the fpu) (JM) }
  2310. if ((left.expectloc<>LOC_FPUREGISTER) or
  2311. (right.expectloc<>LOC_FPUREGISTER)) and
  2312. (registersfpu < 2) then
  2313. inc(registersfpu);
  2314. end
  2315. { if both are orddefs then check sub types }
  2316. else if (ld.typ=orddef) and (rd.typ=orddef) then
  2317. begin
  2318. { 2 booleans ? }
  2319. if is_boolean(ld) and is_boolean(rd) then
  2320. begin
  2321. if (not(cs_full_boolean_eval in current_settings.localswitches) or
  2322. (nf_short_bool in flags)) and
  2323. (nodetype in [andn,orn]) then
  2324. begin
  2325. expectloc:=LOC_JUMP;
  2326. calcregisters(self,0,0,0);
  2327. end
  2328. else
  2329. begin
  2330. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2331. begin
  2332. expectloc:=LOC_FLAGS;
  2333. if (left.expectloc in [LOC_JUMP,LOC_FLAGS]) and
  2334. (left.expectloc in [LOC_JUMP,LOC_FLAGS]) then
  2335. calcregisters(self,2,0,0)
  2336. else
  2337. calcregisters(self,1,0,0);
  2338. end
  2339. else
  2340. begin
  2341. expectloc:=LOC_REGISTER;
  2342. calcregisters(self,0,0,0);
  2343. end;
  2344. end;
  2345. end
  2346. else
  2347. { Both are chars? only convert to shortstrings for addn }
  2348. if is_char(ld) then
  2349. begin
  2350. if nodetype=addn then
  2351. internalerror(200103291);
  2352. expectloc:=LOC_FLAGS;
  2353. calcregisters(self,1,0,0);
  2354. end
  2355. {$ifndef cpu64bit}
  2356. { is there a 64 bit type ? }
  2357. else if (torddef(ld).ordtype in [s64bit,u64bit,scurrency]) then
  2358. begin
  2359. result := first_add64bitint;
  2360. if assigned(result) then
  2361. exit;
  2362. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2363. expectloc:=LOC_REGISTER
  2364. else
  2365. expectloc:=LOC_JUMP;
  2366. calcregisters(self,2,0,0)
  2367. end
  2368. {$endif cpu64bit}
  2369. { is there a cardinal? }
  2370. else if (torddef(ld).ordtype=u32bit) then
  2371. begin
  2372. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2373. expectloc:=LOC_REGISTER
  2374. else
  2375. expectloc:=LOC_FLAGS;
  2376. calcregisters(self,1,0,0);
  2377. { for unsigned mul we need an extra register }
  2378. if nodetype=muln then
  2379. inc(registersint);
  2380. end
  2381. { generic s32bit conversion }
  2382. else
  2383. begin
  2384. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2385. expectloc:=LOC_REGISTER
  2386. else
  2387. expectloc:=LOC_FLAGS;
  2388. calcregisters(self,1,0,0);
  2389. end;
  2390. end
  2391. { left side a setdef, must be before string processing,
  2392. else array constructor can be seen as array of char (PFV) }
  2393. else if (ld.typ=setdef) then
  2394. begin
  2395. if not(is_varset(ld)) then
  2396. begin
  2397. if nodetype in [ltn,lten,gtn,gten,equaln,unequaln] then
  2398. expectloc:=LOC_FLAGS
  2399. else
  2400. expectloc:=LOC_REGISTER;
  2401. { are we adding set elements ? }
  2402. if right.nodetype=setelementn then
  2403. begin
  2404. { add range?
  2405. the smallset code can't handle set ranges }
  2406. if assigned(tsetelementnode(right).right) then
  2407. begin
  2408. result:=internalstatements(newstatement);
  2409. { create temp for result }
  2410. temp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  2411. addstatement(newstatement,temp);
  2412. { add a range or a single element? }
  2413. if assigned(tsetelementnode(right).right) then
  2414. addstatement(newstatement,ccallnode.createintern('fpc_varset_set_range',
  2415. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2416. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).right,sinttype),
  2417. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2418. ccallparanode.create(ctemprefnode.create(temp),
  2419. ccallparanode.create(left,nil))))))
  2420. )
  2421. else
  2422. addstatement(newstatement,ccallnode.createintern('fpc_varset_set',
  2423. ccallparanode.create(cordconstnode.create(resultdef.size,sinttype,false),
  2424. ccallparanode.create(ctypeconvnode.create_internal(tsetelementnode(right).left,sinttype),
  2425. ccallparanode.create(ctemprefnode.create(temp),
  2426. ccallparanode.create(left,nil)))))
  2427. );
  2428. { remove reused parts from original node }
  2429. tsetelementnode(right).right:=nil;
  2430. tsetelementnode(right).left:=nil;
  2431. left:=nil;
  2432. { the last statement should return the value as
  2433. location and type, this is done be referencing the
  2434. temp and converting it first from a persistent temp to
  2435. normal temp }
  2436. addstatement(newstatement,ctempdeletenode.create_normal_temp(temp));
  2437. addstatement(newstatement,ctemprefnode.create(temp));
  2438. end
  2439. else
  2440. calcregisters(self,2,0,0)
  2441. end
  2442. else
  2443. calcregisters(self,1,0,0);
  2444. end
  2445. else
  2446. {$ifdef MMXSET}
  2447. {$ifdef i386}
  2448. if cs_mmx in current_settings.localswitches then
  2449. begin
  2450. expectloc:=LOC_MMXREGISTER;
  2451. calcregisters(self,0,0,4);
  2452. end
  2453. else
  2454. {$endif}
  2455. {$endif MMXSET}
  2456. begin
  2457. result := first_addset;
  2458. if assigned(result) then
  2459. exit;
  2460. expectloc:=LOC_CREFERENCE;
  2461. calcregisters(self,0,0,0);
  2462. { here we call SET... }
  2463. include(current_procinfo.flags,pi_do_call);
  2464. end;
  2465. end
  2466. { compare pchar by addresses like BP/Delphi }
  2467. else if is_pchar(ld) then
  2468. begin
  2469. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2470. expectloc:=LOC_REGISTER
  2471. else
  2472. expectloc:=LOC_FLAGS;
  2473. calcregisters(self,1,0,0);
  2474. end
  2475. { is one of the operands a string }
  2476. else if (ld.typ=stringdef) then
  2477. begin
  2478. if is_widestring(ld) then
  2479. begin
  2480. { this is only for add, the comparisaion is handled later }
  2481. expectloc:=LOC_REGISTER;
  2482. end
  2483. else if is_ansistring(ld) then
  2484. begin
  2485. { this is only for add, the comparisaion is handled later }
  2486. expectloc:=LOC_REGISTER;
  2487. end
  2488. else if is_longstring(ld) then
  2489. begin
  2490. { this is only for add, the comparisaion is handled later }
  2491. expectloc:=LOC_REFERENCE;
  2492. end
  2493. else
  2494. begin
  2495. {$ifdef addstringopt}
  2496. { can create a call which isn't handled by callparatemp }
  2497. if canbeaddsstringcharoptnode(self) then
  2498. begin
  2499. hp := genaddsstringcharoptnode(self);
  2500. pass_1 := hp;
  2501. exit;
  2502. end
  2503. else
  2504. {$endif addstringopt}
  2505. begin
  2506. { Fix right to be shortstring }
  2507. if is_char(right.resultdef) then
  2508. begin
  2509. inserttypeconv(right,cshortstringtype);
  2510. firstpass(right);
  2511. end;
  2512. end;
  2513. {$ifdef addstringopt}
  2514. { can create a call which isn't handled by callparatemp }
  2515. if canbeaddsstringcsstringoptnode(self) then
  2516. begin
  2517. hp := genaddsstringcsstringoptnode(self);
  2518. pass_1 := hp;
  2519. exit;
  2520. end;
  2521. {$endif addstringopt}
  2522. end;
  2523. { otherwise, let addstring convert everything }
  2524. result := first_addstring;
  2525. exit;
  2526. end
  2527. { is one a real float ? }
  2528. else if (rd.typ=floatdef) or (ld.typ=floatdef) then
  2529. begin
  2530. {$ifdef cpufpemu}
  2531. if (current_settings.fputype=fpu_soft) or (cs_fp_emulation in current_settings.moduleswitches) then
  2532. begin
  2533. result:=first_addfloat;
  2534. if assigned(result) then
  2535. exit;
  2536. end;
  2537. {$endif cpufpemu}
  2538. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2539. expectloc:=LOC_FPUREGISTER
  2540. else
  2541. expectloc:=LOC_FLAGS;
  2542. calcregisters(self,0,1,0);
  2543. { an add node always first loads both the left and the }
  2544. { right in the fpu before doing the calculation. However, }
  2545. { calcregisters(0,2,0) will overestimate the number of }
  2546. { necessary registers (it will make it 3 in case one of }
  2547. { the operands is already in the fpu) (JM) }
  2548. if ((left.expectloc<>LOC_FPUREGISTER) or
  2549. (right.expectloc<>LOC_FPUREGISTER)) and
  2550. (registersfpu < 2) then
  2551. inc(registersfpu);
  2552. end
  2553. { pointer comperation and subtraction }
  2554. else if (ld.typ=pointerdef) then
  2555. begin
  2556. if nodetype in [addn,subn,muln,andn,orn,xorn] then
  2557. expectloc:=LOC_REGISTER
  2558. else
  2559. expectloc:=LOC_FLAGS;
  2560. calcregisters(self,1,0,0);
  2561. end
  2562. else if is_class_or_interface(ld) then
  2563. begin
  2564. expectloc:=LOC_FLAGS;
  2565. calcregisters(self,1,0,0);
  2566. end
  2567. else if (ld.typ=classrefdef) then
  2568. begin
  2569. expectloc:=LOC_FLAGS;
  2570. calcregisters(self,1,0,0);
  2571. end
  2572. { support procvar=nil,procvar<>nil }
  2573. else if ((ld.typ=procvardef) and (rt=niln)) or
  2574. ((rd.typ=procvardef) and (lt=niln)) then
  2575. begin
  2576. expectloc:=LOC_FLAGS;
  2577. calcregisters(self,1,0,0);
  2578. end
  2579. {$ifdef SUPPORT_MMX}
  2580. { mmx support, this must be before the zero based array
  2581. check }
  2582. else if (cs_mmx in current_settings.localswitches) and is_mmx_able_array(ld) and
  2583. is_mmx_able_array(rd) then
  2584. begin
  2585. expectloc:=LOC_MMXREGISTER;
  2586. calcregisters(self,0,0,1);
  2587. end
  2588. {$endif SUPPORT_MMX}
  2589. else if (rd.typ=pointerdef) or (ld.typ=pointerdef) then
  2590. begin
  2591. expectloc:=LOC_REGISTER;
  2592. calcregisters(self,1,0,0);
  2593. end
  2594. else if (rd.typ=procvardef) and
  2595. (ld.typ=procvardef) and
  2596. equal_defs(rd,ld) then
  2597. begin
  2598. expectloc:=LOC_FLAGS;
  2599. calcregisters(self,1,0,0);
  2600. end
  2601. else if (ld.typ=enumdef) then
  2602. begin
  2603. expectloc:=LOC_FLAGS;
  2604. calcregisters(self,1,0,0);
  2605. end
  2606. {$ifdef SUPPORT_MMX}
  2607. else if (cs_mmx in current_settings.localswitches) and
  2608. is_mmx_able_array(ld) and
  2609. is_mmx_able_array(rd) then
  2610. begin
  2611. expectloc:=LOC_MMXREGISTER;
  2612. calcregisters(self,0,0,1);
  2613. end
  2614. {$endif SUPPORT_MMX}
  2615. { the general solution is to convert to 32 bit int }
  2616. else
  2617. begin
  2618. expectloc:=LOC_REGISTER;
  2619. calcregisters(self,1,0,0);
  2620. end;
  2621. end;
  2622. {$ifdef state_tracking}
  2623. function Taddnode.track_state_pass(exec_known:boolean):boolean;
  2624. var factval:Tnode;
  2625. begin
  2626. track_state_pass:=false;
  2627. if left.track_state_pass(exec_known) then
  2628. begin
  2629. track_state_pass:=true;
  2630. left.resultdef:=nil;
  2631. do_typecheckpass(left);
  2632. end;
  2633. factval:=aktstate.find_fact(left);
  2634. if factval<>nil then
  2635. begin
  2636. track_state_pass:=true;
  2637. left.destroy;
  2638. left:=factval.getcopy;
  2639. end;
  2640. if right.track_state_pass(exec_known) then
  2641. begin
  2642. track_state_pass:=true;
  2643. right.resultdef:=nil;
  2644. do_typecheckpass(right);
  2645. end;
  2646. factval:=aktstate.find_fact(right);
  2647. if factval<>nil then
  2648. begin
  2649. track_state_pass:=true;
  2650. right.destroy;
  2651. right:=factval.getcopy;
  2652. end;
  2653. end;
  2654. {$endif}
  2655. begin
  2656. caddnode:=taddnode;
  2657. end.