nmat.pas 46 KB

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