nmat.pas 43 KB

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