nmat.pas 44 KB

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