2
0

nmat.pas 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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. trealconstnode(left).value_currency:=-trealconstnode(left).value_currency;
  773. result:=left;
  774. left:=nil;
  775. exit;
  776. end;
  777. end;
  778. function tunaryminusnode.pass_typecheck : tnode;
  779. var
  780. t : tnode;
  781. begin
  782. result:=nil;
  783. typecheckpass(left);
  784. { avoid any problems with type parameters later on }
  785. if is_typeparam(left.resultdef) then
  786. begin
  787. resultdef:=cundefinedtype;
  788. exit;
  789. end;
  790. set_varstate(left,vs_read,[vsf_must_be_valid]);
  791. if codegenerror then
  792. exit;
  793. result:=simplify(false);
  794. if assigned(result) then
  795. exit;
  796. resultdef:=left.resultdef;
  797. if (left.resultdef.typ=floatdef) or
  798. is_currency(left.resultdef) then
  799. begin
  800. end
  801. {$ifdef SUPPORT_MMX}
  802. else if (cs_mmx in current_settings.localswitches) and
  803. is_mmx_able_array(left.resultdef) then
  804. begin
  805. { if saturation is on, left.resultdef isn't
  806. "mmx able" (FK)
  807. if (cs_mmx_saturation in current_settings.localswitches^) and
  808. (torddef(tarraydef(resultdef).definition).typ in
  809. [s32bit,u32bit]) then
  810. CGMessage(type_e_mismatch);
  811. }
  812. end
  813. {$endif SUPPORT_MMX}
  814. else if is_oversizedord(left.resultdef) then
  815. begin
  816. if is_64bit(left.resultdef) then
  817. inserttypeconv(left,s64inttype)
  818. else if is_32bit(left.resultdef) then
  819. inserttypeconv(left,s32inttype)
  820. else if is_16bit(left.resultdef) then
  821. inserttypeconv(left,s16inttype)
  822. else
  823. internalerror(2013040701);
  824. resultdef:=left.resultdef;
  825. end
  826. else if (left.resultdef.typ=orddef) then
  827. begin
  828. inserttypeconv(left,sinttype);
  829. resultdef:=left.resultdef
  830. end
  831. else
  832. begin
  833. { allow operator overloading }
  834. t:=self;
  835. if isunaryoverloaded(t) then
  836. begin
  837. result:=t;
  838. exit;
  839. end;
  840. CGMessage(type_e_mismatch);
  841. end;
  842. end;
  843. { generic code }
  844. { overridden by: }
  845. { i386 }
  846. function tunaryminusnode.pass_1 : tnode;
  847. var
  848. procname: string[31];
  849. begin
  850. result:=nil;
  851. firstpass(left);
  852. if codegenerror then
  853. exit;
  854. if (cs_fp_emulation in current_settings.moduleswitches) and (left.resultdef.typ=floatdef) then
  855. begin
  856. if not(target_info.system in systems_wince) then
  857. begin
  858. expectloc:=LOC_REGISTER;
  859. exit;
  860. end
  861. else
  862. begin
  863. case tfloatdef(resultdef).floattype of
  864. s32real:
  865. procname:='negs';
  866. s64real:
  867. procname:='negd';
  868. {!!! not yet implemented
  869. s128real:
  870. }
  871. else
  872. internalerror(2005082802);
  873. end;
  874. result:=ccallnode.createintern(procname,ccallparanode.create(left,nil));
  875. end;
  876. left:=nil;
  877. end
  878. else
  879. begin
  880. if (left.resultdef.typ=floatdef) then
  881. expectloc:=LOC_FPUREGISTER
  882. {$ifdef SUPPORT_MMX}
  883. else if (cs_mmx in current_settings.localswitches) and
  884. is_mmx_able_array(left.resultdef) then
  885. expectloc:=LOC_MMXREGISTER
  886. {$endif SUPPORT_MMX}
  887. else if (left.resultdef.typ=orddef) then
  888. expectloc:=LOC_REGISTER;
  889. end;
  890. end;
  891. {****************************************************************************
  892. TUNARYPLUSNODE
  893. ****************************************************************************}
  894. constructor tunaryplusnode.create(expr: tnode);
  895. begin
  896. inherited create(unaryplusn,expr);
  897. end;
  898. function tunaryplusnode.pass_1: tnode;
  899. begin
  900. result:=nil;
  901. { can never happen because all the conversions happen
  902. in pass_typecheck }
  903. internalerror(201012250);
  904. end;
  905. function tunaryplusnode.pass_typecheck: tnode;
  906. var
  907. t:tnode;
  908. begin
  909. result:=nil;
  910. typecheckpass(left);
  911. { avoid any problems with type parameters later on }
  912. if is_typeparam(left.resultdef) then
  913. begin
  914. resultdef:=cundefinedtype;
  915. exit;
  916. end;
  917. set_varstate(left,vs_read,[vsf_must_be_valid]);
  918. if codegenerror then
  919. exit;
  920. if is_constintnode(left) or
  921. is_constrealnode(left) or
  922. (left.resultdef.typ=floatdef) or
  923. is_currency(left.resultdef)
  924. {$ifdef SUPPORT_MMX}
  925. or ((cs_mmx in current_settings.localswitches) and
  926. is_mmx_able_array(left.resultdef))
  927. {$endif SUPPORT_MMX}
  928. then
  929. begin
  930. result:=left;
  931. left:=nil;
  932. end
  933. else if is_oversizedord(left.resultdef) then
  934. begin
  935. if is_64bit(left.resultdef) then
  936. inserttypeconv(left,s64inttype)
  937. else if is_32bit(left.resultdef) then
  938. inserttypeconv(left,s32inttype)
  939. else if is_16bit(left.resultdef) then
  940. inserttypeconv(left,s16inttype)
  941. else
  942. internalerror(2013040702);
  943. result:=left;
  944. left:=nil;
  945. end
  946. else if (left.resultdef.typ=orddef) then
  947. begin
  948. inserttypeconv(left,sinttype);
  949. result:=left;
  950. left:=nil;
  951. end
  952. else
  953. begin
  954. { allow operator overloading }
  955. t:=self;
  956. if isunaryoverloaded(t) then
  957. begin
  958. result:=t;
  959. exit;
  960. end;
  961. CGMessage(type_e_mismatch);
  962. end;
  963. end;
  964. {****************************************************************************
  965. TNOTNODE
  966. ****************************************************************************}
  967. const
  968. boolean_reverse:array[ltn..unequaln] of Tnodetype=(
  969. gten,gtn,lten,ltn,unequaln,equaln
  970. );
  971. constructor tnotnode.create(expr : tnode);
  972. begin
  973. inherited create(notn,expr);
  974. end;
  975. function tnotnode.simplify(forinline : boolean):tnode;
  976. var
  977. v : tconstexprint;
  978. t : tnode;
  979. def : tdef;
  980. begin
  981. result:=nil;
  982. { Try optmimizing ourself away }
  983. if left.nodetype=notn then
  984. begin
  985. { Double not. Remove both }
  986. result:=Tnotnode(left).left;
  987. tnotnode(left).left:=nil;
  988. exit;
  989. end;
  990. if (left.nodetype in [ltn,lten,equaln,unequaln,gtn,gten]) then
  991. begin
  992. { Not of boolean expression. Turn around the operator and remove
  993. the not. This is not allowed for sets with the gten/lten,
  994. because there is no ltn/gtn support }
  995. if (taddnode(left).left.resultdef.typ<>setdef) or
  996. (left.nodetype in [equaln,unequaln]) then
  997. begin
  998. result:=left;
  999. left.nodetype:=boolean_reverse[left.nodetype];
  1000. left:=nil;
  1001. exit;
  1002. end;
  1003. end;
  1004. { constant folding }
  1005. if (left.nodetype=ordconstn) then
  1006. begin
  1007. v:=tordconstnode(left).value;
  1008. def:=left.resultdef;
  1009. case torddef(left.resultdef).ordtype of
  1010. pasbool8,
  1011. pasbool16,
  1012. pasbool32,
  1013. pasbool64:
  1014. v:=byte(not(boolean(int64(v))));
  1015. bool8bit,
  1016. bool16bit,
  1017. bool32bit,
  1018. bool64bit:
  1019. begin
  1020. if v=0 then
  1021. v:=-1
  1022. else
  1023. v:=0;
  1024. end;
  1025. uchar,
  1026. uwidechar,
  1027. u8bit,
  1028. s8bit,
  1029. u16bit,
  1030. s16bit,
  1031. s32bit,
  1032. u32bit,
  1033. s64bit,
  1034. u64bit:
  1035. begin
  1036. { unsigned, equal or bigger than the native int size? }
  1037. if (torddef(left.resultdef).ordtype in [u64bit,u32bit,u16bit,u8bit,uchar,uwidechar]) and
  1038. (is_nativeord(left.resultdef) or is_oversizedord(left.resultdef)) then
  1039. begin
  1040. { Delphi-compatible: not dword = dword (not word = longint) }
  1041. { Extension: not qword = qword }
  1042. v:=qword(not qword(v));
  1043. { will be truncated by the ordconstnode for u32bit }
  1044. end
  1045. else
  1046. begin
  1047. v:=int64(not int64(v));
  1048. def:=get_common_intdef(torddef(left.resultdef),torddef(sinttype),false);
  1049. end;
  1050. end;
  1051. else
  1052. CGMessage(type_e_mismatch);
  1053. end;
  1054. { not-nodes are not range checked by the code generator -> also
  1055. don't range check while inlining; the resultdef is a bit tricky
  1056. though: the node's resultdef gets changed in most cases compared
  1057. to left, but the not-operation itself is caried out in the code
  1058. generator using the size of left
  1059. }
  1060. if not(forinline) then
  1061. t:=cordconstnode.create(v,def,false)
  1062. else
  1063. begin
  1064. { cut off the value if necessary }
  1065. t:=cordconstnode.create(v,left.resultdef,false);
  1066. { now convert to node's resultdef }
  1067. inserttypeconv_explicit(t,def);
  1068. end;
  1069. result:=t;
  1070. exit;
  1071. end;
  1072. end;
  1073. function tnotnode.pass_typecheck : tnode;
  1074. var
  1075. t : tnode;
  1076. begin
  1077. result:=nil;
  1078. typecheckpass(left);
  1079. { avoid any problems with type parameters later on }
  1080. if is_typeparam(left.resultdef) then
  1081. begin
  1082. resultdef:=cundefinedtype;
  1083. exit;
  1084. end;
  1085. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1086. if codegenerror then
  1087. exit;
  1088. { tp procvar support }
  1089. maybe_call_procvar(left,true);
  1090. resultdef:=left.resultdef;
  1091. result:=simplify(false);
  1092. if assigned(result) then
  1093. exit;
  1094. if is_boolean(resultdef) then
  1095. begin
  1096. end
  1097. else
  1098. {$ifdef SUPPORT_MMX}
  1099. if (cs_mmx in current_settings.localswitches) and
  1100. is_mmx_able_array(left.resultdef) then
  1101. begin
  1102. end
  1103. else
  1104. {$endif SUPPORT_MMX}
  1105. {$ifndef cpu64bitaddr}
  1106. if is_64bitint(left.resultdef) then
  1107. begin
  1108. end
  1109. else
  1110. {$endif not cpu64bitaddr}
  1111. if is_integer(left.resultdef) then
  1112. begin
  1113. end
  1114. else
  1115. begin
  1116. { allow operator overloading }
  1117. t:=self;
  1118. if isunaryoverloaded(t) then
  1119. begin
  1120. result:=t;
  1121. exit;
  1122. end;
  1123. CGMessage(type_e_mismatch);
  1124. end;
  1125. end;
  1126. function tnotnode.pass_1 : tnode;
  1127. begin
  1128. result:=nil;
  1129. firstpass(left);
  1130. if codegenerror then
  1131. exit;
  1132. expectloc:=left.expectloc;
  1133. if is_boolean(resultdef) then
  1134. begin
  1135. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1136. expectloc:=LOC_REGISTER;
  1137. { before loading it into flags we need to load it into
  1138. a register thus 1 register is need PM }
  1139. {$ifdef cpuflags}
  1140. if left.expectloc<>LOC_JUMP then
  1141. expectloc:=LOC_FLAGS;
  1142. {$endif def cpuflags}
  1143. end
  1144. else
  1145. {$ifdef SUPPORT_MMX}
  1146. if (cs_mmx in current_settings.localswitches) and
  1147. is_mmx_able_array(left.resultdef) then
  1148. expectloc:=LOC_MMXREGISTER
  1149. else
  1150. {$endif SUPPORT_MMX}
  1151. {$ifndef cpu64bitalu}
  1152. if is_64bit(left.resultdef) then
  1153. begin
  1154. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1155. expectloc:=LOC_REGISTER;
  1156. end
  1157. else
  1158. {$endif not cpu64bitalu}
  1159. if is_integer(left.resultdef) then
  1160. expectloc:=LOC_REGISTER;
  1161. end;
  1162. {$ifdef state_tracking}
  1163. function Tnotnode.track_state_pass(exec_known:boolean):boolean;
  1164. begin
  1165. track_state_pass:=true;
  1166. if left.track_state_pass(exec_known) then
  1167. begin
  1168. left.resultdef:=nil;
  1169. do_typecheckpass(left);
  1170. end;
  1171. end;
  1172. {$endif}
  1173. end.