nmat.pas 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. {
  2. Copyright (c) 2000-2005 by Florian Klaempfl
  3. Type checking and register allocation for math 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 nmat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node;
  22. type
  23. tmoddivnode = class(tbinopnode)
  24. function pass_1 : tnode;override;
  25. function pass_typecheck:tnode;override;
  26. function simplify(forinline : boolean) : tnode;override;
  27. protected
  28. { override the following if you want to implement }
  29. { parts explicitely in the code generator (JM) }
  30. function use_moddiv64bitint_helper: boolean; virtual;
  31. function first_moddiv64bitint: tnode; virtual;
  32. function firstoptimize: tnode; virtual;
  33. function first_moddivint: tnode; virtual;
  34. end;
  35. tmoddivnodeclass = class of tmoddivnode;
  36. tshlshrnode = class(tbinopnode)
  37. function pass_1 : tnode;override;
  38. function pass_typecheck:tnode;override;
  39. function simplify(forinline : boolean) : tnode;override;
  40. {$ifndef cpu64bitalu}
  41. { override the following if you want to implement }
  42. { parts explicitely in the code generator (CEC)
  43. Should return nil, if everything will be handled
  44. in the code generator
  45. }
  46. function first_shlshr64bitint: tnode; virtual;
  47. {$endif not cpu64bitalu}
  48. end;
  49. tshlshrnodeclass = class of tshlshrnode;
  50. tunaryminusnode = class(tunarynode)
  51. constructor create(expr : tnode);virtual;
  52. function pass_1 : tnode;override;
  53. function pass_typecheck:tnode;override;
  54. function simplify(forinline : boolean) : tnode;override;
  55. end;
  56. tunaryminusnodeclass = class of tunaryminusnode;
  57. tunaryplusnode = class(tunarynode)
  58. constructor create(expr : tnode);virtual;
  59. function pass_1 : tnode;override;
  60. function pass_typecheck:tnode;override;
  61. end;
  62. tunaryplusnodeclass = class of tunaryplusnode;
  63. tnotnode = class(tunarynode)
  64. constructor create(expr : tnode);virtual;
  65. function pass_1 : tnode;override;
  66. function pass_typecheck:tnode;override;
  67. function simplify(forinline : boolean) : tnode;override;
  68. {$ifdef state_tracking}
  69. function track_state_pass(exec_known:boolean):boolean;override;
  70. {$endif}
  71. end;
  72. tnotnodeclass = class of tnotnode;
  73. var
  74. cmoddivnode : tmoddivnodeclass = tmoddivnode;
  75. cshlshrnode : tshlshrnodeclass = tshlshrnode;
  76. cunaryminusnode : tunaryminusnodeclass = tunaryminusnode;
  77. cunaryplusnode : tunaryplusnodeclass = tunaryplusnode;
  78. cnotnode : tnotnodeclass = tnotnode;
  79. implementation
  80. uses
  81. systems,
  82. verbose,globals,cutils,compinnr,
  83. globtype,constexp,
  84. symconst,symtype,symdef,
  85. defcmp,defutil,
  86. htypechk,pass_1,
  87. cgbase,
  88. ncon,ncnv,ncal,nadd,nld,nbas,nflw,ninl,
  89. nutils;
  90. {****************************************************************************
  91. TMODDIVNODE
  92. ****************************************************************************}
  93. function tmoddivnode.simplify(forinline : boolean):tnode;
  94. var
  95. rv,lv : tconstexprint;
  96. begin
  97. result:=nil;
  98. if is_constintnode(right) then
  99. begin
  100. rv:=tordconstnode(right).value;
  101. if rv = 1 then
  102. begin
  103. case nodetype of
  104. modn:
  105. result := cordconstnode.create(0,left.resultdef,true);
  106. divn:
  107. result := left.getcopy;
  108. end;
  109. exit;
  110. end;
  111. if rv = 0 then
  112. begin
  113. Message(parser_e_division_by_zero);
  114. { recover }
  115. tordconstnode(right).value := 1;
  116. end;
  117. { the following simplification is also required for correctness
  118. on x86, as its transformation of divisions by constants to
  119. multiplications and shifts does not handle -1 correctly }
  120. if (rv=-1) and
  121. (nodetype=divn) then
  122. begin
  123. result:=cunaryminusnode.create(left);
  124. left:=nil;
  125. exit;
  126. end;
  127. if (nf_isomod in flags) and
  128. (rv<=0) then
  129. begin
  130. Message(cg_e_mod_only_defined_for_pos_quotient);
  131. { recover }
  132. tordconstnode(right).value := 1;
  133. end;
  134. end;
  135. if is_constintnode(right) and is_constintnode(left) then
  136. begin
  137. rv:=tordconstnode(right).value;
  138. lv:=tordconstnode(left).value;
  139. case nodetype of
  140. modn:
  141. if nf_isomod in flags then
  142. begin
  143. if lv>=0 then
  144. result:=create_simplified_ord_const(lv mod rv,resultdef,forinline,false)
  145. else
  146. if ((-lv) mod rv)=0 then
  147. result:=create_simplified_ord_const((-lv) mod rv,resultdef,forinline,false)
  148. else
  149. result:=create_simplified_ord_const(rv-((-lv) mod rv),resultdef,forinline,false);
  150. end
  151. else
  152. result:=create_simplified_ord_const(lv mod rv,resultdef,forinline,false);
  153. divn:
  154. result:=create_simplified_ord_const(lv div rv,resultdef,forinline,cs_check_overflow in localswitches);
  155. end;
  156. end;
  157. end;
  158. function tmoddivnode.use_moddiv64bitint_helper: boolean;
  159. begin
  160. { not with an ifdef around the call to this routine, because e.g. the
  161. Java VM has a signed 64 bit division opcode, but not an unsigned
  162. one }
  163. {$ifdef cpu64bitalu}
  164. result:=false;
  165. {$else cpu64bitalu}
  166. result:=
  167. (left.resultdef.typ=orddef) and
  168. (right.resultdef.typ=orddef) and
  169. { include currency as well }
  170. (is_64bit(left.resultdef) or is_64bit(right.resultdef));
  171. {$endif cpu64bitaly}
  172. end;
  173. function tmoddivnode.pass_typecheck:tnode;
  174. var
  175. else_block,
  176. hp,t : tnode;
  177. rd,ld : torddef;
  178. else_statements,
  179. statements : tstatementnode;
  180. result_data : ttempcreatenode;
  181. nd : torddef;
  182. begin
  183. result:=nil;
  184. typecheckpass(left);
  185. typecheckpass(right);
  186. { avoid any problems with type parameters later on }
  187. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  188. begin
  189. resultdef:=cundefinedtype;
  190. exit;
  191. end;
  192. set_varstate(left,vs_read,[vsf_must_be_valid]);
  193. set_varstate(right,vs_read,[vsf_must_be_valid]);
  194. if codegenerror then
  195. exit;
  196. { tp procvar support }
  197. maybe_call_procvar(left,true);
  198. maybe_call_procvar(right,true);
  199. { allow operator overloading }
  200. t:=self;
  201. if isbinaryoverloaded(t,[]) then
  202. begin
  203. result:=t;
  204. exit;
  205. end;
  206. { we need 2 orddefs always }
  207. if (left.resultdef.typ<>orddef) then
  208. inserttypeconv(left,sinttype);
  209. if (right.resultdef.typ<>orddef) then
  210. inserttypeconv(right,sinttype);
  211. if codegenerror then
  212. exit;
  213. { Try only now to simply constant
  214. as otherwise you might create
  215. tconstnode with return type that are
  216. not compatible with tconst node
  217. as in bug report 21566 PM }
  218. result:=simplify(false);
  219. if assigned(result) then
  220. exit;
  221. rd:=torddef(right.resultdef);
  222. ld:=torddef(left.resultdef);
  223. { if one operand is a cardinal and the other is a positive constant, convert the }
  224. { constant to a cardinal as well so we don't have to do a 64bit division (JM) }
  225. { Do the same for qwords and positive constants as well, otherwise things like }
  226. { "qword mod 10" are evaluated with int64 as result, which is wrong if the }
  227. { "qword" was > high(int64) (JM) }
  228. { Additionally, do the same for cardinal/qwords and other positive types, but }
  229. { always in a way that a smaller type is converted to a bigger type }
  230. { (webtbs/tw8870) }
  231. if (rd.ordtype in [u8bit,u16bit,u32bit,u64bit]) and
  232. ((is_constintnode(left) and
  233. (tordconstnode(left).value >= 0) and
  234. (tordconstnode(left).value <= get_max_value(rd))) or
  235. (not is_signed(ld) and
  236. (rd.size >= ld.size))) then
  237. begin
  238. inserttypeconv(left,right.resultdef);
  239. ld:=torddef(left.resultdef);
  240. end;
  241. if (ld.ordtype in [u8bit,u16bit,u32bit,u64bit]) and
  242. ((is_constintnode(right) and
  243. (tordconstnode(right).value >= 0) and
  244. (tordconstnode(right).value <= get_max_value(ld))) or
  245. (not is_signed(rd) and
  246. (ld.size >= rd.size))) then
  247. begin
  248. inserttypeconv(right,left.resultdef);
  249. rd:=torddef(right.resultdef);
  250. end;
  251. { when there is one currency value, everything is done
  252. using currency }
  253. if (ld.ordtype=scurrency) or
  254. (rd.ordtype=scurrency) then
  255. begin
  256. if (ld.ordtype<>scurrency) then
  257. inserttypeconv(left,s64currencytype);
  258. if (rd.ordtype<>scurrency) then
  259. inserttypeconv(right,s64currencytype);
  260. resultdef:=left.resultdef;
  261. end
  262. else
  263. { when there is one 64bit value, everything is done
  264. in 64bit }
  265. if (is_64bitint(left.resultdef) or
  266. is_64bitint(right.resultdef)) then
  267. begin
  268. if is_signed(rd) or is_signed(ld) then
  269. begin
  270. if (ld.ordtype<>s64bit) then
  271. inserttypeconv(left,s64inttype);
  272. if (rd.ordtype<>s64bit) then
  273. inserttypeconv(right,s64inttype);
  274. end
  275. else
  276. begin
  277. if (ld.ordtype<>u64bit) then
  278. inserttypeconv(left,u64inttype);
  279. if (rd.ordtype<>u64bit) then
  280. inserttypeconv(right,u64inttype);
  281. end;
  282. resultdef:=left.resultdef;
  283. end
  284. else
  285. { is there a larger than the native int? }
  286. if is_oversizedint(ld) or is_oversizedint(rd) then
  287. begin
  288. nd:=get_common_intdef(ld,rd,false);
  289. if (ld.ordtype<>nd.ordtype) then
  290. inserttypeconv(left,nd);
  291. if (rd.ordtype<>nd.ordtype) then
  292. inserttypeconv(right,nd);
  293. resultdef:=left.resultdef;
  294. end
  295. else
  296. { when mixing unsigned and signed native ints, convert everything to a larger signed type (JM) }
  297. if (is_nativeuint(rd) and
  298. is_signed(ld)) or
  299. (is_nativeuint(ld) and
  300. is_signed(rd)) then
  301. begin
  302. CGMessage(type_h_mixed_signed_unsigned);
  303. { get a signed int, larger than the native int }
  304. nd:=get_common_intdef(torddef(sinttype),torddef(uinttype),false);
  305. if (ld.ordtype<>nd.ordtype) then
  306. inserttypeconv(left,nd);
  307. if (rd.ordtype<>nd.ordtype) then
  308. inserttypeconv(right,nd);
  309. resultdef:=left.resultdef;
  310. end
  311. else
  312. begin
  313. { Make everything always default singed int }
  314. if not(rd.ordtype in [torddef(sinttype).ordtype,torddef(uinttype).ordtype]) then
  315. inserttypeconv(right,sinttype);
  316. if not(ld.ordtype in [torddef(sinttype).ordtype,torddef(uinttype).ordtype]) then
  317. inserttypeconv(left,sinttype);
  318. resultdef:=right.resultdef;
  319. end;
  320. { when the result is currency we need some extra code for
  321. division. this should not be done when the divn node is
  322. created internally }
  323. if (nodetype=divn) and
  324. not(nf_is_currency in flags) and
  325. is_currency(resultdef) then
  326. begin
  327. hp:=caddnode.create(muln,getcopy,cordconstnode.create(10000,s64currencytype,false));
  328. include(hp.flags,nf_is_currency);
  329. result:=hp;
  330. end;
  331. if (nodetype=modn) and (nf_isomod in flags) then
  332. begin
  333. result:=internalstatements(statements);
  334. else_block:=internalstatements(else_statements);
  335. result_data:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  336. { right <=0? }
  337. addstatement(statements,cifnode.create_internal(caddnode.create_internal(lten,right.getcopy,cordconstnode.create(0,resultdef,false)),
  338. { then: result:=left mod right }
  339. ccallnode.createintern('fpc_divbyzero',nil),
  340. nil
  341. ));
  342. { prepare else block }
  343. { result:=(-left) mod right }
  344. addstatement(else_statements,cassignmentnode.create(ctemprefnode.create(result_data),cmoddivnode.create(modn,cunaryminusnode.create(left.getcopy),right.getcopy)));
  345. { result<>0? }
  346. addstatement(else_statements,cifnode.create_internal(caddnode.create_internal(unequaln,ctemprefnode.create(result_data),cordconstnode.create(0,resultdef,false)),
  347. { then: result:=right-result }
  348. cassignmentnode.create_internal(ctemprefnode.create(result_data),caddnode.create_internal(subn,right.getcopy,ctemprefnode.create(result_data))),
  349. nil
  350. ));
  351. addstatement(statements,result_data);
  352. { if left>=0 }
  353. addstatement(statements,cifnode.create_internal(caddnode.create_internal(gten,left.getcopy,cordconstnode.create(0,resultdef,false)),
  354. { then: result:=left mod right }
  355. cassignmentnode.create_internal(ctemprefnode.create(result_data),cmoddivnode.create(modn,left.getcopy,right.getcopy)),
  356. { else block }
  357. else_block
  358. ));
  359. addstatement(statements,ctempdeletenode.create_normal_temp(result_data));
  360. addstatement(statements,ctemprefnode.create(result_data));
  361. end;
  362. end;
  363. function tmoddivnode.first_moddivint: tnode;
  364. {$ifdef cpuneedsdivhelper}
  365. var
  366. procname: string[31];
  367. begin
  368. result := nil;
  369. { otherwise create a call to a helper }
  370. if nodetype = divn then
  371. procname := 'fpc_div_'
  372. else
  373. procname := 'fpc_mod_';
  374. { only qword needs the unsigned code, the
  375. signed code is also used for currency }
  376. case torddef(resultdef).ordtype of
  377. u8bit:
  378. procname := procname + 'byte';
  379. s8bit:
  380. procname := procname + 'shortint';
  381. u16bit:
  382. procname := procname + 'word';
  383. s16bit:
  384. procname := procname + 'smallint';
  385. u32bit:
  386. procname := procname + 'dword';
  387. s32bit:
  388. procname := procname + 'longint';
  389. scurrency:
  390. procname := procname + 'currency';
  391. else
  392. internalerror(2015070501);
  393. end;
  394. result := ccallnode.createintern(procname,ccallparanode.create(left,
  395. ccallparanode.create(right,nil)));
  396. left := nil;
  397. right := nil;
  398. firstpass(result);
  399. if result.resultdef.typ<>orddef then
  400. internalerror(2013031701);
  401. if resultdef.typ<>orddef then
  402. internalerror(2013031701);
  403. if torddef(result.resultdef).ordtype <> torddef(resultdef).ordtype then
  404. inserttypeconv(result,resultdef);
  405. end;
  406. {$else cpuneedsdivhelper}
  407. begin
  408. result:=nil;
  409. end;
  410. {$endif cpuneedsdiv32helper}
  411. function tmoddivnode.first_moddiv64bitint: tnode;
  412. var
  413. procname: string[31];
  414. begin
  415. result := nil;
  416. { when currency is used set the result of the
  417. parameters to s64bit, so they are not converted }
  418. if is_currency(resultdef) then
  419. begin
  420. left.resultdef:=s64inttype;
  421. right.resultdef:=s64inttype;
  422. end;
  423. { otherwise create a call to a helper }
  424. if nodetype = divn then
  425. procname := 'fpc_div_'
  426. else
  427. procname := 'fpc_mod_';
  428. { only qword needs the unsigned code, the
  429. signed code is also used for currency }
  430. if is_signed(resultdef) then
  431. procname := procname + 'int64'
  432. else
  433. procname := procname + 'qword';
  434. result := ccallnode.createintern(procname,ccallparanode.create(left,
  435. ccallparanode.create(right,nil)));
  436. left := nil;
  437. right := nil;
  438. firstpass(result);
  439. end;
  440. function tmoddivnode.firstoptimize: tnode;
  441. var
  442. power,shiftval : longint;
  443. statements : tstatementnode;
  444. temp,resulttemp : ttempcreatenode;
  445. masknode : tnode;
  446. invertsign: Boolean;
  447. begin
  448. result := nil;
  449. { divide/mod a number by a constant which is a power of 2? }
  450. if (right.nodetype = ordconstn) and
  451. isabspowerof2(tordconstnode(right).value,power) and
  452. {$ifdef cpu64bitalu}
  453. { for 64 bit, we leave the optimization to the cg }
  454. (not is_signed(resultdef)) then
  455. {$else cpu64bitalu}
  456. (((nodetype=divn) and is_oversizedord(resultdef)) or
  457. (nodetype=modn) or
  458. not is_signed(resultdef)) then
  459. {$endif cpu64bitalu}
  460. begin
  461. if nodetype=divn then
  462. begin
  463. if is_signed(resultdef) then
  464. begin
  465. invertsign:=tordconstnode(right).value<0;
  466. if is_64bitint(left.resultdef) then
  467. if not (cs_opt_size in current_settings.optimizerswitches) then
  468. shiftval:=63
  469. else
  470. { the shift code is a lot bigger than the call to }
  471. { the divide helper }
  472. exit
  473. else
  474. shiftval:=left.resultdef.size*8-1;
  475. result:=internalstatements(statements);
  476. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  477. resulttemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  478. addstatement(statements,resulttemp);
  479. addstatement(statements,temp);
  480. addstatement(statements,cassignmentnode.create(ctemprefnode.create(temp),
  481. left));
  482. left:=nil;
  483. { masknode is (sar(temp,shiftval) and ((1 shl power)-1))
  484. for power=1 (i.e. division by 2), masknode is simply (temp shr shiftval)}
  485. if power=1 then
  486. masknode:=
  487. cshlshrnode.create(shrn,
  488. ctemprefnode.create(temp),
  489. cordconstnode.create(shiftval,u8inttype,false)
  490. )
  491. else
  492. masknode:=
  493. caddnode.create(andn,
  494. cinlinenode.create(in_sar_x_y,false,
  495. ccallparanode.create(cordconstnode.create(shiftval,u8inttype,false),
  496. ccallparanode.create(ctemprefnode.create(temp),nil))
  497. ),
  498. cordconstnode.create(tcgint((qword(1) shl power)-1),
  499. right.resultdef,false)
  500. );
  501. if invertsign then
  502. addstatement(statements,cassignmentnode.create(ctemprefnode.create(resulttemp),
  503. cunaryminusnode.create(
  504. cinlinenode.create(in_sar_x_y,false,
  505. ccallparanode.create(cordconstnode.create(power,u8inttype,false),
  506. ccallparanode.create(caddnode.create(addn,ctemprefnode.create(temp),
  507. masknode),nil
  508. )))))
  509. )
  510. else
  511. addstatement(statements,cassignmentnode.create(ctemprefnode.create(resulttemp),
  512. cinlinenode.create(in_sar_x_y,false,
  513. ccallparanode.create(cordconstnode.create(power,u8inttype,false),
  514. ccallparanode.create(caddnode.create(addn,ctemprefnode.create(temp),
  515. masknode),nil
  516. ))))
  517. );
  518. addstatement(statements,ctempdeletenode.create(temp));
  519. addstatement(statements,ctempdeletenode.create_normal_temp(resulttemp));
  520. addstatement(statements,ctemprefnode.create(resulttemp));
  521. right.Free;
  522. end
  523. else
  524. begin
  525. tordconstnode(right).value:=power;
  526. result:=cshlshrnode.create(shrn,left,right)
  527. end;
  528. end
  529. else if is_signed(resultdef) then { signed modulus }
  530. begin
  531. if (cs_opt_size in current_settings.optimizerswitches) then
  532. exit;
  533. shiftval:=left.resultdef.size*8-1;
  534. tordconstnode(right).value.uvalue:=qword((qword(1) shl power)-1);
  535. result:=internalstatements(statements);
  536. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  537. resulttemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  538. addstatement(statements,resulttemp);
  539. addstatement(statements,temp);
  540. addstatement(statements,cassignmentnode.create(ctemprefnode.create(temp),left));
  541. { mask:=sar(left,sizeof(left)*8-1) and ((1 shl power)-1); }
  542. if power=1 then
  543. masknode:=
  544. cshlshrnode.create(shrn,
  545. ctemprefnode.create(temp),
  546. cordconstnode.create(shiftval,u8inttype,false)
  547. )
  548. else
  549. masknode:=
  550. caddnode.create(andn,
  551. cinlinenode.create(in_sar_x_y,false,
  552. ccallparanode.create(cordconstnode.create(shiftval,u8inttype,false),
  553. ccallparanode.create(ctemprefnode.create(temp),nil))
  554. ),
  555. cordconstnode.create(tcgint((qword(1) shl power)-1),
  556. right.resultdef,false)
  557. );
  558. addstatement(statements,cassignmentnode.create(ctemprefnode.create(resulttemp),masknode));
  559. { result:=((left+mask) and right)-mask; }
  560. addstatement(statements,cassignmentnode.create(ctemprefnode.create(resulttemp),
  561. caddnode.create(subn,
  562. caddnode.create(andn,
  563. right,
  564. caddnode.create(addn,
  565. ctemprefnode.create(temp),
  566. ctemprefnode.create(resulttemp))),
  567. ctemprefnode.create(resulttemp))
  568. ));
  569. addstatement(statements,ctempdeletenode.create(temp));
  570. addstatement(statements,ctempdeletenode.create_normal_temp(resulttemp));
  571. addstatement(statements,ctemprefnode.create(resulttemp));
  572. end
  573. else
  574. begin
  575. tordconstnode(right).value.uvalue:=qword((qword(1) shl power)-1);
  576. result := caddnode.create(andn,left,right);
  577. end;
  578. { left and right are reused }
  579. left := nil;
  580. right := nil;
  581. firstpass(result);
  582. exit;
  583. end;
  584. end;
  585. function tmoddivnode.pass_1 : tnode;
  586. begin
  587. result:=nil;
  588. firstpass(left);
  589. firstpass(right);
  590. if codegenerror then
  591. exit;
  592. { Try to optimize mod/div }
  593. result := firstoptimize;
  594. if assigned(result) then
  595. exit;
  596. { 64bit }
  597. if use_moddiv64bitint_helper then
  598. begin
  599. result := first_moddiv64bitint;
  600. if assigned(result) then
  601. exit;
  602. expectloc:=LOC_REGISTER;
  603. end
  604. else
  605. begin
  606. result := first_moddivint;
  607. if assigned(result) then
  608. exit;
  609. end;
  610. expectloc:=LOC_REGISTER;
  611. end;
  612. {****************************************************************************
  613. TSHLSHRNODE
  614. ****************************************************************************}
  615. function tshlshrnode.simplify(forinline : boolean):tnode;
  616. var
  617. lvalue, rvalue, mask : Tconstexprint;
  618. rangedef: tdef;
  619. size: longint;
  620. begin
  621. result:=nil;
  622. { constant folding }
  623. if is_constintnode(right) then
  624. begin
  625. if forinline then
  626. begin
  627. case resultdef.size of
  628. 1,2,4:
  629. rvalue:=tordconstnode(right).value and byte($1f);
  630. 8:
  631. rvalue:=tordconstnode(right).value and byte($3f);
  632. else
  633. internalerror(2013122302);
  634. end;
  635. end
  636. else
  637. rvalue:=tordconstnode(right).value;
  638. if is_constintnode(left) then
  639. begin
  640. lvalue:=tordconstnode(left).value;
  641. getrangedefmasksize(resultdef, rangedef, mask, size);
  642. { shr is an unsigned operation, so cut off upper bits }
  643. if forinline then
  644. lvalue:=lvalue and mask;
  645. case nodetype of
  646. shrn:
  647. lvalue:=lvalue shr rvalue;
  648. shln:
  649. lvalue:=lvalue shl rvalue;
  650. else
  651. internalerror(2019050517);
  652. end;
  653. { discard shifted-out bits (shl never triggers overflow/range errors) }
  654. if forinline and
  655. (nodetype=shln) then
  656. lvalue:=lvalue and mask;
  657. result:=create_simplified_ord_const(lvalue,resultdef,forinline,false);
  658. end
  659. else if rvalue=0 then
  660. begin
  661. result:=left;
  662. left:=nil;
  663. end;
  664. end
  665. else if is_constintnode(left) then
  666. begin
  667. lvalue:=tordconstnode(left).value;
  668. if forinline then
  669. begin
  670. getrangedefmasksize(resultdef, rangedef, mask, size);
  671. lvalue:=lvalue and mask;
  672. end;
  673. { '0 shl x' and '0 shr x' are 0 }
  674. if (lvalue=0) and
  675. ((cs_opt_level4 in current_settings.optimizerswitches) or
  676. not might_have_sideeffects(right)) then
  677. result:=cordconstnode.create(0,resultdef,true);
  678. end;
  679. end;
  680. function tshlshrnode.pass_typecheck:tnode;
  681. var
  682. t : tnode;
  683. begin
  684. result:=nil;
  685. typecheckpass(left);
  686. typecheckpass(right);
  687. { avoid any problems with type parameters later on }
  688. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  689. begin
  690. resultdef:=cundefinedtype;
  691. exit;
  692. end;
  693. set_varstate(right,vs_read,[vsf_must_be_valid]);
  694. set_varstate(left,vs_read,[vsf_must_be_valid]);
  695. if codegenerror then
  696. exit;
  697. { tp procvar support }
  698. maybe_call_procvar(left,true);
  699. maybe_call_procvar(right,true);
  700. { allow operator overloading }
  701. t:=self;
  702. if isbinaryoverloaded(t,[]) then
  703. begin
  704. result:=t;
  705. exit;
  706. end;
  707. {$ifdef SUPPORT_MMX}
  708. if (cs_mmx in current_settings.localswitches) and
  709. is_mmx_able_array(left.resultdef) and
  710. ((is_mmx_able_array(right.resultdef) and
  711. equal_defs(left.resultdef,right.resultdef)
  712. ) or is_constintnode(right)) then
  713. begin
  714. if not(mmx_type(left.resultdef) in [mmxu16bit,mmxs16bit,mmxfixed16,mmxu32bit,mmxs32bit,mmxu64bit,mmxs64bit]) then
  715. CGMessage3(type_e_operator_not_supported_for_types,node2opstr(nodetype),left.resultdef.typename,right.resultdef.typename);
  716. if not(is_mmx_able_array(right.resultdef)) then
  717. inserttypeconv(right,sinttype);
  718. end
  719. else
  720. {$endif SUPPORT_MMX}
  721. begin
  722. { calculations for ordinals < 32 bit have to be done in
  723. 32 bit for backwards compatibility. That way 'shl 33' is
  724. the same as 'shl 1'. It's ugly but compatible with delphi/tp/gcc }
  725. if (not is_64bit(left.resultdef)) and
  726. (torddef(left.resultdef).ordtype<>u32bit) then
  727. begin
  728. { keep singness of orignal type }
  729. if is_signed(left.resultdef) then
  730. begin
  731. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  732. inserttypeconv(left,s32inttype)
  733. {$elseif defined(cpu16bitalu) or defined(cpu8bitalu)}
  734. inserttypeconv(left,get_common_intdef(torddef(left.resultdef),torddef(sinttype),true));
  735. {$else}
  736. internalerror(2013031301);
  737. {$endif}
  738. end
  739. else
  740. begin
  741. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  742. inserttypeconv(left,u32inttype);
  743. {$elseif defined(cpu16bitalu) or defined(cpu8bitalu)}
  744. inserttypeconv(left,get_common_intdef(torddef(left.resultdef),torddef(uinttype),true));
  745. {$else}
  746. internalerror(2013031301);
  747. {$endif}
  748. end
  749. end;
  750. inserttypeconv(right,sinttype);
  751. end;
  752. resultdef:=left.resultdef;
  753. result:=simplify(false);
  754. if assigned(result) then
  755. exit;
  756. end;
  757. {$ifndef cpu64bitalu}
  758. function tshlshrnode.first_shlshr64bitint: tnode;
  759. var
  760. procname: string[31];
  761. begin
  762. result := nil;
  763. { Normally already done below, but called again,
  764. just in case it is called directly }
  765. firstpass(left);
  766. { otherwise create a call to a helper }
  767. if is_signed(left.resultdef) then
  768. procname:='int64'
  769. else
  770. procname:='qword';
  771. if nodetype = shln then
  772. procname := 'fpc_shl_'+procname
  773. else
  774. procname := 'fpc_shr_'+procname;
  775. { this order of parameters works at least for the arm,
  776. however it should work for any calling conventions (FK) }
  777. result := ccallnode.createintern(procname,ccallparanode.create(right,
  778. ccallparanode.create(left,nil)));
  779. left := nil;
  780. right := nil;
  781. firstpass(result);
  782. end;
  783. {$endif not cpu64bitalu}
  784. function tshlshrnode.pass_1 : tnode;
  785. begin
  786. result:=nil;
  787. firstpass(left);
  788. firstpass(right);
  789. if codegenerror then
  790. exit;
  791. {$ifndef cpu64bitalu}
  792. expectloc:=LOC_REGISTER;
  793. { 64 bit ints have their own shift handling }
  794. if is_64bit(left.resultdef) then
  795. result := first_shlshr64bitint;
  796. {$endif not cpu64bitalu}
  797. end;
  798. {****************************************************************************
  799. TUNARYMINUSNODE
  800. ****************************************************************************}
  801. constructor tunaryminusnode.create(expr : tnode);
  802. begin
  803. inherited create(unaryminusn,expr);
  804. end;
  805. function tunaryminusnode.simplify(forinline : boolean):tnode;
  806. begin
  807. result:=nil;
  808. { constant folding }
  809. if is_constintnode(left) then
  810. begin
  811. result:=create_simplified_ord_const(-tordconstnode(left).value,resultdef,forinline,cs_check_overflow in localswitches);
  812. exit;
  813. end;
  814. if is_constrealnode(left) then
  815. begin
  816. trealconstnode(left).value_real:=-trealconstnode(left).value_real;
  817. { Avoid integer overflow on x86_64 CPU for currency value }
  818. { i386 uses fildll/fchs/fistll instructions which never seem
  819. to raise any coprocessor flags .. }
  820. {$push}{$Q-}
  821. trealconstnode(left).value_currency:=-trealconstnode(left).value_currency;
  822. result:=left;
  823. {$pop}
  824. left:=nil;
  825. exit;
  826. end;
  827. end;
  828. function tunaryminusnode.pass_typecheck : tnode;
  829. var
  830. t : tnode;
  831. begin
  832. result:=nil;
  833. typecheckpass(left);
  834. { avoid any problems with type parameters later on }
  835. if is_typeparam(left.resultdef) then
  836. begin
  837. resultdef:=cundefinedtype;
  838. exit;
  839. end;
  840. set_varstate(left,vs_read,[vsf_must_be_valid]);
  841. if codegenerror then
  842. exit;
  843. result:=simplify(false);
  844. if assigned(result) then
  845. exit;
  846. resultdef:=left.resultdef;
  847. if (left.resultdef.typ=floatdef) or
  848. is_currency(left.resultdef) then
  849. begin
  850. end
  851. {$ifdef SUPPORT_MMX}
  852. else if (cs_mmx in current_settings.localswitches) and
  853. is_mmx_able_array(left.resultdef) then
  854. begin
  855. { if saturation is on, left.resultdef isn't
  856. "mmx able" (FK)
  857. if (cs_mmx_saturation in current_settings.localswitches^) and
  858. (torddef(tarraydef(resultdef).definition).typ in
  859. [s32bit,u32bit]) then
  860. CGMessage(type_e_mismatch);
  861. }
  862. end
  863. {$endif SUPPORT_MMX}
  864. else if is_oversizedord(left.resultdef) then
  865. begin
  866. if is_64bit(left.resultdef) then
  867. inserttypeconv(left,s64inttype)
  868. else if is_32bit(left.resultdef) then
  869. inserttypeconv(left,s32inttype)
  870. else if is_16bit(left.resultdef) then
  871. inserttypeconv(left,s16inttype)
  872. else
  873. internalerror(2013040701);
  874. resultdef:=left.resultdef;
  875. end
  876. else if (left.resultdef.typ=orddef) then
  877. begin
  878. inserttypeconv(left,sinttype);
  879. resultdef:=left.resultdef
  880. end
  881. else
  882. begin
  883. { allow operator overloading }
  884. t:=self;
  885. if isunaryoverloaded(t,[]) then
  886. begin
  887. result:=t;
  888. exit;
  889. end;
  890. CGMessage(type_e_mismatch);
  891. end;
  892. end;
  893. { generic code }
  894. { overridden by: }
  895. { i386 }
  896. function tunaryminusnode.pass_1 : tnode;
  897. var
  898. procname: string[31];
  899. begin
  900. result:=nil;
  901. firstpass(left);
  902. if codegenerror then
  903. exit;
  904. if (cs_fp_emulation in current_settings.moduleswitches) and (left.resultdef.typ=floatdef) then
  905. begin
  906. if not(target_info.system in systems_wince) then
  907. begin
  908. expectloc:=LOC_REGISTER;
  909. exit;
  910. end
  911. else
  912. begin
  913. case tfloatdef(resultdef).floattype of
  914. s32real:
  915. procname:='negs';
  916. s64real:
  917. procname:='negd';
  918. {!!! not yet implemented
  919. s128real:
  920. }
  921. else
  922. internalerror(2005082802);
  923. end;
  924. result:=ccallnode.createintern(procname,ccallparanode.create(left,nil));
  925. end;
  926. left:=nil;
  927. end
  928. else
  929. begin
  930. if (left.resultdef.typ=floatdef) then
  931. expectloc:=LOC_FPUREGISTER
  932. {$ifdef SUPPORT_MMX}
  933. else if (cs_mmx in current_settings.localswitches) and
  934. is_mmx_able_array(left.resultdef) then
  935. expectloc:=LOC_MMXREGISTER
  936. {$endif SUPPORT_MMX}
  937. else if (left.resultdef.typ=orddef) then
  938. expectloc:=LOC_REGISTER;
  939. end;
  940. end;
  941. {****************************************************************************
  942. TUNARYPLUSNODE
  943. ****************************************************************************}
  944. constructor tunaryplusnode.create(expr: tnode);
  945. begin
  946. inherited create(unaryplusn,expr);
  947. end;
  948. function tunaryplusnode.pass_1: tnode;
  949. begin
  950. result:=nil;
  951. { can never happen because all the conversions happen
  952. in pass_typecheck }
  953. internalerror(201012250);
  954. end;
  955. function tunaryplusnode.pass_typecheck: tnode;
  956. var
  957. t:tnode;
  958. begin
  959. result:=nil;
  960. typecheckpass(left);
  961. { avoid any problems with type parameters later on }
  962. if is_typeparam(left.resultdef) then
  963. begin
  964. resultdef:=cundefinedtype;
  965. exit;
  966. end;
  967. set_varstate(left,vs_read,[vsf_must_be_valid]);
  968. if codegenerror then
  969. exit;
  970. if is_constintnode(left) or
  971. is_constrealnode(left) or
  972. (left.resultdef.typ=floatdef) or
  973. is_currency(left.resultdef)
  974. {$ifdef SUPPORT_MMX}
  975. or ((cs_mmx in current_settings.localswitches) and
  976. is_mmx_able_array(left.resultdef))
  977. {$endif SUPPORT_MMX}
  978. then
  979. begin
  980. result:=left;
  981. left:=nil;
  982. end
  983. else if is_oversizedord(left.resultdef) then
  984. begin
  985. if is_64bit(left.resultdef) then
  986. inserttypeconv(left,s64inttype)
  987. else if is_32bit(left.resultdef) then
  988. inserttypeconv(left,s32inttype)
  989. else if is_16bit(left.resultdef) then
  990. inserttypeconv(left,s16inttype)
  991. else
  992. internalerror(2013040702);
  993. result:=left;
  994. left:=nil;
  995. end
  996. else if (left.resultdef.typ=orddef) then
  997. begin
  998. inserttypeconv(left,sinttype);
  999. result:=left;
  1000. left:=nil;
  1001. end
  1002. else
  1003. begin
  1004. { allow operator overloading }
  1005. t:=self;
  1006. if isunaryoverloaded(t,[]) then
  1007. begin
  1008. result:=t;
  1009. exit;
  1010. end;
  1011. CGMessage(type_e_mismatch);
  1012. end;
  1013. end;
  1014. {****************************************************************************
  1015. TNOTNODE
  1016. ****************************************************************************}
  1017. const
  1018. boolean_reverse:array[ltn..unequaln] of Tnodetype=(
  1019. gten,gtn,lten,ltn,unequaln,equaln
  1020. );
  1021. constructor tnotnode.create(expr : tnode);
  1022. begin
  1023. inherited create(notn,expr);
  1024. end;
  1025. function tnotnode.simplify(forinline : boolean):tnode;
  1026. var
  1027. v : tconstexprint;
  1028. t : tnode;
  1029. def : tdef;
  1030. begin
  1031. result:=nil;
  1032. { Try optmimizing ourself away }
  1033. if left.nodetype=notn then
  1034. begin
  1035. { Double not. Remove both }
  1036. result:=Tnotnode(left).left;
  1037. tnotnode(left).left:=nil;
  1038. exit;
  1039. end;
  1040. if (left.nodetype in [ltn,lten,equaln,unequaln,gtn,gten]) then
  1041. begin
  1042. { Not of boolean expression. Turn around the operator and remove
  1043. the not. This is not allowed for sets with the gten/lten,
  1044. because there is no ltn/gtn support }
  1045. if (taddnode(left).left.resultdef.typ<>setdef) or
  1046. (left.nodetype in [equaln,unequaln]) then
  1047. begin
  1048. result:=left;
  1049. left.nodetype:=boolean_reverse[left.nodetype];
  1050. left:=nil;
  1051. exit;
  1052. end;
  1053. end;
  1054. { constant folding }
  1055. if (left.nodetype=ordconstn) and
  1056. (left.resultdef.typ=orddef) then
  1057. begin
  1058. v:=tordconstnode(left).value;
  1059. def:=left.resultdef;
  1060. if not calc_not_ordvalue(v,def) then
  1061. CGMessage(type_e_mismatch);
  1062. { not-nodes are not range checked by the code generator -> also
  1063. don't range check while inlining; the resultdef is a bit tricky
  1064. though: the node's resultdef gets changed in most cases compared
  1065. to left, but the not-operation itself is caried out in the code
  1066. generator using the size of left
  1067. }
  1068. if not(forinline) then
  1069. t:=cordconstnode.create(v,def,false)
  1070. else
  1071. begin
  1072. { cut off the value if necessary }
  1073. t:=cordconstnode.create(v,left.resultdef,false);
  1074. { now convert to node's resultdef }
  1075. inserttypeconv_explicit(t,def);
  1076. end;
  1077. result:=t;
  1078. exit;
  1079. end;
  1080. end;
  1081. function tnotnode.pass_typecheck : tnode;
  1082. var
  1083. t : tnode;
  1084. begin
  1085. result:=nil;
  1086. typecheckpass(left);
  1087. { avoid any problems with type parameters later on }
  1088. if is_typeparam(left.resultdef) then
  1089. begin
  1090. resultdef:=cundefinedtype;
  1091. exit;
  1092. end;
  1093. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1094. if codegenerror then
  1095. exit;
  1096. { tp procvar support }
  1097. maybe_call_procvar(left,true);
  1098. resultdef:=left.resultdef;
  1099. result:=simplify(false);
  1100. if assigned(result) then
  1101. exit;
  1102. if is_boolean(resultdef) then
  1103. begin
  1104. end
  1105. else
  1106. {$ifdef SUPPORT_MMX}
  1107. if (cs_mmx in current_settings.localswitches) and
  1108. is_mmx_able_array(left.resultdef) then
  1109. begin
  1110. end
  1111. else
  1112. {$endif SUPPORT_MMX}
  1113. {$ifndef cpu64bitaddr}
  1114. if is_64bitint(left.resultdef) then
  1115. begin
  1116. end
  1117. else
  1118. {$endif not cpu64bitaddr}
  1119. if is_integer(left.resultdef) then
  1120. begin
  1121. end
  1122. else
  1123. begin
  1124. { allow operator overloading }
  1125. t:=self;
  1126. if isunaryoverloaded(t,[]) then
  1127. begin
  1128. result:=t;
  1129. exit;
  1130. end;
  1131. CGMessage(type_e_mismatch);
  1132. end;
  1133. end;
  1134. function tnotnode.pass_1 : tnode;
  1135. begin
  1136. result:=nil;
  1137. firstpass(left);
  1138. if codegenerror then
  1139. exit;
  1140. expectloc:=left.expectloc;
  1141. if is_boolean(resultdef) then
  1142. begin
  1143. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1144. expectloc:=LOC_REGISTER;
  1145. { before loading it into flags we need to load it into
  1146. a register thus 1 register is need PM }
  1147. {$ifdef cpuflags}
  1148. if left.expectloc<>LOC_JUMP then
  1149. expectloc:=LOC_FLAGS;
  1150. {$endif def cpuflags}
  1151. end
  1152. else
  1153. {$ifdef SUPPORT_MMX}
  1154. if (cs_mmx in current_settings.localswitches) and
  1155. is_mmx_able_array(left.resultdef) then
  1156. expectloc:=LOC_MMXREGISTER
  1157. else
  1158. {$endif SUPPORT_MMX}
  1159. {$ifndef cpu64bitalu}
  1160. if is_64bit(left.resultdef) then
  1161. begin
  1162. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1163. expectloc:=LOC_REGISTER;
  1164. end
  1165. else
  1166. {$endif not cpu64bitalu}
  1167. if is_integer(left.resultdef) then
  1168. expectloc:=LOC_REGISTER;
  1169. end;
  1170. {$ifdef state_tracking}
  1171. function Tnotnode.track_state_pass(exec_known:boolean):boolean;
  1172. begin
  1173. track_state_pass:=true;
  1174. if left.track_state_pass(exec_known) then
  1175. begin
  1176. left.resultdef:=nil;
  1177. do_typecheckpass(left);
  1178. end;
  1179. end;
  1180. {$endif}
  1181. end.