nmat.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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. right.Free;
  495. end
  496. else
  497. begin
  498. tordconstnode(right).value:=power;
  499. result:=cshlshrnode.create(shrn,left,right)
  500. end;
  501. end
  502. else if is_signed(resultdef) then { signed modulus }
  503. begin
  504. if (cs_opt_size in current_settings.optimizerswitches) then
  505. exit;
  506. shiftval:=left.resultdef.size*8-1;
  507. dec(tordconstnode(right).value.uvalue);
  508. result:=internalstatements(statements);
  509. temp:=ctempcreatenode.create(left.resultdef,left.resultdef.size,tt_persistent,true);
  510. resulttemp:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
  511. addstatement(statements,resulttemp);
  512. addstatement(statements,temp);
  513. addstatement(statements,cassignmentnode.create(ctemprefnode.create(temp),left));
  514. { sign:=sar(left,sizeof(left)*8-1); }
  515. addstatement(statements,cassignmentnode.create(ctemprefnode.create(resulttemp),
  516. cinlinenode.create(in_sar_x_y,false,
  517. ccallparanode.create(cordconstnode.create(shiftval,u8inttype,false),
  518. ccallparanode.create(ctemprefnode.create(temp),nil)
  519. )
  520. )));
  521. { result:=((((left xor sign)-sign) and right) xor sign)-sign; }
  522. addstatement(statements,cassignmentnode.create(ctemprefnode.create(resulttemp),
  523. caddnode.create(subn,
  524. caddnode.create(xorn,
  525. caddnode.create(andn,
  526. right,
  527. caddnode.create(subn,
  528. caddnode.create(xorn,
  529. ctemprefnode.create(resulttemp),
  530. ctemprefnode.create(temp)),
  531. ctemprefnode.create(resulttemp))
  532. ),
  533. ctemprefnode.create(resulttemp)
  534. ),
  535. ctemprefnode.create(resulttemp))
  536. ));
  537. addstatement(statements,ctempdeletenode.create(temp));
  538. addstatement(statements,ctempdeletenode.create_normal_temp(resulttemp));
  539. addstatement(statements,ctemprefnode.create(resulttemp));
  540. end
  541. else
  542. begin
  543. dec(tordconstnode(right).value.uvalue);
  544. result := caddnode.create(andn,left,right);
  545. end;
  546. { left and right are reused }
  547. left := nil;
  548. right := nil;
  549. firstpass(result);
  550. exit;
  551. end;
  552. end;
  553. function tmoddivnode.pass_1 : tnode;
  554. begin
  555. result:=nil;
  556. firstpass(left);
  557. firstpass(right);
  558. if codegenerror then
  559. exit;
  560. { Try to optimize mod/div }
  561. result := firstoptimize;
  562. if assigned(result) then
  563. exit;
  564. { 64bit }
  565. if use_moddiv64bitint_helper then
  566. begin
  567. result := first_moddiv64bitint;
  568. if assigned(result) then
  569. exit;
  570. expectloc:=LOC_REGISTER;
  571. end
  572. else
  573. begin
  574. result := first_moddivint;
  575. if assigned(result) then
  576. exit;
  577. end;
  578. expectloc:=LOC_REGISTER;
  579. end;
  580. {****************************************************************************
  581. TSHLSHRNODE
  582. ****************************************************************************}
  583. function tshlshrnode.simplify(forinline : boolean):tnode;
  584. var
  585. lvalue,rvalue : Tconstexprint;
  586. begin
  587. result:=nil;
  588. { constant folding }
  589. if is_constintnode(right) then
  590. begin
  591. if forinline then
  592. begin
  593. { shl/shr are unsigned operations, so cut off upper bits }
  594. case resultdef.size of
  595. 1,2,4:
  596. rvalue:=tordconstnode(right).value and byte($1f);
  597. 8:
  598. rvalue:=tordconstnode(right).value and byte($3f);
  599. else
  600. internalerror(2013122302);
  601. end;
  602. end
  603. else
  604. rvalue:=tordconstnode(right).value;
  605. if is_constintnode(left) then
  606. begin
  607. if forinline then
  608. begin
  609. { shl/shr are unsigned operations, so cut off upper bits }
  610. case resultdef.size of
  611. 1:
  612. lvalue:=tordconstnode(left).value and byte($ff);
  613. 2:
  614. lvalue:=tordconstnode(left).value and word($ffff);
  615. 4:
  616. lvalue:=tordconstnode(left).value and dword($ffffffff);
  617. 8:
  618. lvalue:=tordconstnode(left).value and qword($ffffffffffffffff);
  619. else
  620. internalerror(2013122301);
  621. end;
  622. end
  623. else
  624. lvalue:=tordconstnode(left).value;
  625. case nodetype of
  626. shrn:
  627. result:=create_simplified_ord_const(lvalue shr rvalue,resultdef,forinline);
  628. shln:
  629. result:=create_simplified_ord_const(lvalue shl rvalue,resultdef,forinline);
  630. end;
  631. end
  632. else if rvalue=0 then
  633. begin
  634. result:=left;
  635. left:=nil;
  636. end;
  637. end;
  638. end;
  639. function tshlshrnode.pass_typecheck:tnode;
  640. var
  641. t : tnode;
  642. begin
  643. result:=nil;
  644. typecheckpass(left);
  645. typecheckpass(right);
  646. { avoid any problems with type parameters later on }
  647. if is_typeparam(left.resultdef) or is_typeparam(right.resultdef) then
  648. begin
  649. resultdef:=cundefinedtype;
  650. exit;
  651. end;
  652. set_varstate(right,vs_read,[vsf_must_be_valid]);
  653. set_varstate(left,vs_read,[vsf_must_be_valid]);
  654. if codegenerror then
  655. exit;
  656. { tp procvar support }
  657. maybe_call_procvar(left,true);
  658. maybe_call_procvar(right,true);
  659. { allow operator overloading }
  660. t:=self;
  661. if isbinaryoverloaded(t) then
  662. begin
  663. result:=t;
  664. exit;
  665. end;
  666. { calculations for ordinals < 32 bit have to be done in
  667. 32 bit for backwards compatibility. That way 'shl 33' is
  668. the same as 'shl 1'. It's ugly but compatible with delphi/tp/gcc }
  669. if (not is_64bit(left.resultdef)) and
  670. (torddef(left.resultdef).ordtype<>u32bit) then
  671. begin
  672. { keep singness of orignal type }
  673. if is_signed(left.resultdef) then
  674. begin
  675. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  676. inserttypeconv(left,s32inttype)
  677. {$elseif defined(cpu16bitalu) or defined(cpu8bitalu)}
  678. inserttypeconv(left,get_common_intdef(torddef(left.resultdef),torddef(sinttype),true));
  679. {$else}
  680. internalerror(2013031301);
  681. {$endif}
  682. end
  683. else
  684. begin
  685. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  686. inserttypeconv(left,u32inttype);
  687. {$elseif defined(cpu16bitalu) or defined(cpu8bitalu)}
  688. inserttypeconv(left,get_common_intdef(torddef(left.resultdef),torddef(uinttype),true));
  689. {$else}
  690. internalerror(2013031301);
  691. {$endif}
  692. end
  693. end;
  694. inserttypeconv(right,sinttype);
  695. resultdef:=left.resultdef;
  696. result:=simplify(false);
  697. if assigned(result) then
  698. exit;
  699. end;
  700. {$ifndef cpu64bitalu}
  701. function tshlshrnode.first_shlshr64bitint: tnode;
  702. var
  703. procname: string[31];
  704. begin
  705. result := nil;
  706. { Normally already done below, but called again,
  707. just in case it is called directly }
  708. firstpass(left);
  709. { otherwise create a call to a helper }
  710. if is_signed(left.resultdef) then
  711. procname:='int64'
  712. else
  713. procname:='qword';
  714. if nodetype = shln then
  715. procname := 'fpc_shl_'+procname
  716. else
  717. procname := 'fpc_shr_'+procname;
  718. { this order of parameters works at least for the arm,
  719. however it should work for any calling conventions (FK) }
  720. result := ccallnode.createintern(procname,ccallparanode.create(right,
  721. ccallparanode.create(left,nil)));
  722. left := nil;
  723. right := nil;
  724. firstpass(result);
  725. end;
  726. {$endif not cpu64bitalu}
  727. function tshlshrnode.pass_1 : tnode;
  728. var
  729. regs : longint;
  730. begin
  731. result:=nil;
  732. firstpass(left);
  733. firstpass(right);
  734. if codegenerror then
  735. exit;
  736. {$ifndef cpu64bitalu}
  737. { 64 bit ints have their own shift handling }
  738. if is_64bit(left.resultdef) then
  739. begin
  740. result := first_shlshr64bitint;
  741. if assigned(result) then
  742. exit;
  743. regs:=2;
  744. end
  745. else
  746. {$endif not cpu64bitalu}
  747. begin
  748. regs:=1
  749. end;
  750. if (right.nodetype<>ordconstn) then
  751. inc(regs);
  752. expectloc:=LOC_REGISTER;
  753. end;
  754. {****************************************************************************
  755. TUNARYMINUSNODE
  756. ****************************************************************************}
  757. constructor tunaryminusnode.create(expr : tnode);
  758. begin
  759. inherited create(unaryminusn,expr);
  760. end;
  761. function tunaryminusnode.simplify(forinline : boolean):tnode;
  762. begin
  763. result:=nil;
  764. { constant folding }
  765. if is_constintnode(left) then
  766. begin
  767. result:=create_simplified_ord_const(-tordconstnode(left).value,resultdef,forinline);
  768. exit;
  769. end;
  770. if is_constrealnode(left) then
  771. begin
  772. trealconstnode(left).value_real:=-trealconstnode(left).value_real;
  773. { Avoid integer overflow on x86_64 CPU for currency value }
  774. { i386 uses fildll/fchs/fistll instructions which never seem
  775. to raise any coprocessor flags .. }
  776. {$push}{$Q-}
  777. trealconstnode(left).value_currency:=-trealconstnode(left).value_currency;
  778. result:=left;
  779. {$pop}
  780. left:=nil;
  781. exit;
  782. end;
  783. end;
  784. function tunaryminusnode.pass_typecheck : tnode;
  785. var
  786. t : tnode;
  787. begin
  788. result:=nil;
  789. typecheckpass(left);
  790. { avoid any problems with type parameters later on }
  791. if is_typeparam(left.resultdef) then
  792. begin
  793. resultdef:=cundefinedtype;
  794. exit;
  795. end;
  796. set_varstate(left,vs_read,[vsf_must_be_valid]);
  797. if codegenerror then
  798. exit;
  799. result:=simplify(false);
  800. if assigned(result) then
  801. exit;
  802. resultdef:=left.resultdef;
  803. if (left.resultdef.typ=floatdef) or
  804. is_currency(left.resultdef) then
  805. begin
  806. end
  807. {$ifdef SUPPORT_MMX}
  808. else if (cs_mmx in current_settings.localswitches) and
  809. is_mmx_able_array(left.resultdef) then
  810. begin
  811. { if saturation is on, left.resultdef isn't
  812. "mmx able" (FK)
  813. if (cs_mmx_saturation in current_settings.localswitches^) and
  814. (torddef(tarraydef(resultdef).definition).typ in
  815. [s32bit,u32bit]) then
  816. CGMessage(type_e_mismatch);
  817. }
  818. end
  819. {$endif SUPPORT_MMX}
  820. else if is_oversizedord(left.resultdef) then
  821. begin
  822. if is_64bit(left.resultdef) then
  823. inserttypeconv(left,s64inttype)
  824. else if is_32bit(left.resultdef) then
  825. inserttypeconv(left,s32inttype)
  826. else if is_16bit(left.resultdef) then
  827. inserttypeconv(left,s16inttype)
  828. else
  829. internalerror(2013040701);
  830. resultdef:=left.resultdef;
  831. end
  832. else if (left.resultdef.typ=orddef) then
  833. begin
  834. inserttypeconv(left,sinttype);
  835. resultdef:=left.resultdef
  836. end
  837. else
  838. begin
  839. { allow operator overloading }
  840. t:=self;
  841. if isunaryoverloaded(t) then
  842. begin
  843. result:=t;
  844. exit;
  845. end;
  846. CGMessage(type_e_mismatch);
  847. end;
  848. end;
  849. { generic code }
  850. { overridden by: }
  851. { i386 }
  852. function tunaryminusnode.pass_1 : tnode;
  853. var
  854. procname: string[31];
  855. begin
  856. result:=nil;
  857. firstpass(left);
  858. if codegenerror then
  859. exit;
  860. if (cs_fp_emulation in current_settings.moduleswitches) and (left.resultdef.typ=floatdef) then
  861. begin
  862. if not(target_info.system in systems_wince) then
  863. begin
  864. expectloc:=LOC_REGISTER;
  865. exit;
  866. end
  867. else
  868. begin
  869. case tfloatdef(resultdef).floattype of
  870. s32real:
  871. procname:='negs';
  872. s64real:
  873. procname:='negd';
  874. {!!! not yet implemented
  875. s128real:
  876. }
  877. else
  878. internalerror(2005082802);
  879. end;
  880. result:=ccallnode.createintern(procname,ccallparanode.create(left,nil));
  881. end;
  882. left:=nil;
  883. end
  884. else
  885. begin
  886. if (left.resultdef.typ=floatdef) then
  887. expectloc:=LOC_FPUREGISTER
  888. {$ifdef SUPPORT_MMX}
  889. else if (cs_mmx in current_settings.localswitches) and
  890. is_mmx_able_array(left.resultdef) then
  891. expectloc:=LOC_MMXREGISTER
  892. {$endif SUPPORT_MMX}
  893. else if (left.resultdef.typ=orddef) then
  894. expectloc:=LOC_REGISTER;
  895. end;
  896. end;
  897. {****************************************************************************
  898. TUNARYPLUSNODE
  899. ****************************************************************************}
  900. constructor tunaryplusnode.create(expr: tnode);
  901. begin
  902. inherited create(unaryplusn,expr);
  903. end;
  904. function tunaryplusnode.pass_1: tnode;
  905. begin
  906. result:=nil;
  907. { can never happen because all the conversions happen
  908. in pass_typecheck }
  909. internalerror(201012250);
  910. end;
  911. function tunaryplusnode.pass_typecheck: tnode;
  912. var
  913. t:tnode;
  914. begin
  915. result:=nil;
  916. typecheckpass(left);
  917. { avoid any problems with type parameters later on }
  918. if is_typeparam(left.resultdef) then
  919. begin
  920. resultdef:=cundefinedtype;
  921. exit;
  922. end;
  923. set_varstate(left,vs_read,[vsf_must_be_valid]);
  924. if codegenerror then
  925. exit;
  926. if is_constintnode(left) or
  927. is_constrealnode(left) or
  928. (left.resultdef.typ=floatdef) or
  929. is_currency(left.resultdef)
  930. {$ifdef SUPPORT_MMX}
  931. or ((cs_mmx in current_settings.localswitches) and
  932. is_mmx_able_array(left.resultdef))
  933. {$endif SUPPORT_MMX}
  934. then
  935. begin
  936. result:=left;
  937. left:=nil;
  938. end
  939. else if is_oversizedord(left.resultdef) then
  940. begin
  941. if is_64bit(left.resultdef) then
  942. inserttypeconv(left,s64inttype)
  943. else if is_32bit(left.resultdef) then
  944. inserttypeconv(left,s32inttype)
  945. else if is_16bit(left.resultdef) then
  946. inserttypeconv(left,s16inttype)
  947. else
  948. internalerror(2013040702);
  949. result:=left;
  950. left:=nil;
  951. end
  952. else if (left.resultdef.typ=orddef) then
  953. begin
  954. inserttypeconv(left,sinttype);
  955. result:=left;
  956. left:=nil;
  957. end
  958. else
  959. begin
  960. { allow operator overloading }
  961. t:=self;
  962. if isunaryoverloaded(t) then
  963. begin
  964. result:=t;
  965. exit;
  966. end;
  967. CGMessage(type_e_mismatch);
  968. end;
  969. end;
  970. {****************************************************************************
  971. TNOTNODE
  972. ****************************************************************************}
  973. const
  974. boolean_reverse:array[ltn..unequaln] of Tnodetype=(
  975. gten,gtn,lten,ltn,unequaln,equaln
  976. );
  977. constructor tnotnode.create(expr : tnode);
  978. begin
  979. inherited create(notn,expr);
  980. end;
  981. function tnotnode.simplify(forinline : boolean):tnode;
  982. var
  983. v : tconstexprint;
  984. t : tnode;
  985. def : tdef;
  986. begin
  987. result:=nil;
  988. { Try optmimizing ourself away }
  989. if left.nodetype=notn then
  990. begin
  991. { Double not. Remove both }
  992. result:=Tnotnode(left).left;
  993. tnotnode(left).left:=nil;
  994. exit;
  995. end;
  996. if (left.nodetype in [ltn,lten,equaln,unequaln,gtn,gten]) then
  997. begin
  998. { Not of boolean expression. Turn around the operator and remove
  999. the not. This is not allowed for sets with the gten/lten,
  1000. because there is no ltn/gtn support }
  1001. if (taddnode(left).left.resultdef.typ<>setdef) or
  1002. (left.nodetype in [equaln,unequaln]) then
  1003. begin
  1004. result:=left;
  1005. left.nodetype:=boolean_reverse[left.nodetype];
  1006. left:=nil;
  1007. exit;
  1008. end;
  1009. end;
  1010. { constant folding }
  1011. if (left.nodetype=ordconstn) then
  1012. begin
  1013. v:=tordconstnode(left).value;
  1014. def:=left.resultdef;
  1015. case torddef(left.resultdef).ordtype of
  1016. pasbool8,
  1017. pasbool16,
  1018. pasbool32,
  1019. pasbool64:
  1020. v:=byte(not(boolean(int64(v))));
  1021. bool8bit,
  1022. bool16bit,
  1023. bool32bit,
  1024. bool64bit:
  1025. begin
  1026. if v=0 then
  1027. v:=-1
  1028. else
  1029. v:=0;
  1030. end;
  1031. uchar,
  1032. uwidechar,
  1033. u8bit,
  1034. s8bit,
  1035. u16bit,
  1036. s16bit,
  1037. s32bit,
  1038. u32bit,
  1039. s64bit,
  1040. u64bit:
  1041. begin
  1042. { unsigned, equal or bigger than the native int size? }
  1043. if (torddef(left.resultdef).ordtype in [u64bit,u32bit,u16bit,u8bit,uchar,uwidechar]) and
  1044. (is_nativeord(left.resultdef) or is_oversizedord(left.resultdef)) then
  1045. begin
  1046. { Delphi-compatible: not dword = dword (not word = longint) }
  1047. { Extension: not qword = qword }
  1048. v:=qword(not qword(v));
  1049. { will be truncated by the ordconstnode for u32bit }
  1050. end
  1051. else
  1052. begin
  1053. v:=int64(not int64(v));
  1054. def:=get_common_intdef(torddef(left.resultdef),torddef(sinttype),false);
  1055. end;
  1056. end;
  1057. else
  1058. CGMessage(type_e_mismatch);
  1059. end;
  1060. { not-nodes are not range checked by the code generator -> also
  1061. don't range check while inlining; the resultdef is a bit tricky
  1062. though: the node's resultdef gets changed in most cases compared
  1063. to left, but the not-operation itself is caried out in the code
  1064. generator using the size of left
  1065. }
  1066. if not(forinline) then
  1067. t:=cordconstnode.create(v,def,false)
  1068. else
  1069. begin
  1070. { cut off the value if necessary }
  1071. t:=cordconstnode.create(v,left.resultdef,false);
  1072. { now convert to node's resultdef }
  1073. inserttypeconv_explicit(t,def);
  1074. end;
  1075. result:=t;
  1076. exit;
  1077. end;
  1078. end;
  1079. function tnotnode.pass_typecheck : tnode;
  1080. var
  1081. t : tnode;
  1082. begin
  1083. result:=nil;
  1084. typecheckpass(left);
  1085. { avoid any problems with type parameters later on }
  1086. if is_typeparam(left.resultdef) then
  1087. begin
  1088. resultdef:=cundefinedtype;
  1089. exit;
  1090. end;
  1091. set_varstate(left,vs_read,[vsf_must_be_valid]);
  1092. if codegenerror then
  1093. exit;
  1094. { tp procvar support }
  1095. maybe_call_procvar(left,true);
  1096. resultdef:=left.resultdef;
  1097. result:=simplify(false);
  1098. if assigned(result) then
  1099. exit;
  1100. if is_boolean(resultdef) then
  1101. begin
  1102. end
  1103. else
  1104. {$ifdef SUPPORT_MMX}
  1105. if (cs_mmx in current_settings.localswitches) and
  1106. is_mmx_able_array(left.resultdef) then
  1107. begin
  1108. end
  1109. else
  1110. {$endif SUPPORT_MMX}
  1111. {$ifndef cpu64bitaddr}
  1112. if is_64bitint(left.resultdef) then
  1113. begin
  1114. end
  1115. else
  1116. {$endif not cpu64bitaddr}
  1117. if is_integer(left.resultdef) then
  1118. begin
  1119. end
  1120. else
  1121. begin
  1122. { allow operator overloading }
  1123. t:=self;
  1124. if isunaryoverloaded(t) then
  1125. begin
  1126. result:=t;
  1127. exit;
  1128. end;
  1129. CGMessage(type_e_mismatch);
  1130. end;
  1131. end;
  1132. function tnotnode.pass_1 : tnode;
  1133. begin
  1134. result:=nil;
  1135. firstpass(left);
  1136. if codegenerror then
  1137. exit;
  1138. expectloc:=left.expectloc;
  1139. if is_boolean(resultdef) then
  1140. begin
  1141. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1142. expectloc:=LOC_REGISTER;
  1143. { before loading it into flags we need to load it into
  1144. a register thus 1 register is need PM }
  1145. {$ifdef cpuflags}
  1146. if left.expectloc<>LOC_JUMP then
  1147. expectloc:=LOC_FLAGS;
  1148. {$endif def cpuflags}
  1149. end
  1150. else
  1151. {$ifdef SUPPORT_MMX}
  1152. if (cs_mmx in current_settings.localswitches) and
  1153. is_mmx_able_array(left.resultdef) then
  1154. expectloc:=LOC_MMXREGISTER
  1155. else
  1156. {$endif SUPPORT_MMX}
  1157. {$ifndef cpu64bitalu}
  1158. if is_64bit(left.resultdef) then
  1159. begin
  1160. if (expectloc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  1161. expectloc:=LOC_REGISTER;
  1162. end
  1163. else
  1164. {$endif not cpu64bitalu}
  1165. if is_integer(left.resultdef) then
  1166. expectloc:=LOC_REGISTER;
  1167. end;
  1168. {$ifdef state_tracking}
  1169. function Tnotnode.track_state_pass(exec_known:boolean):boolean;
  1170. begin
  1171. track_state_pass:=true;
  1172. if left.track_state_pass(exec_known) then
  1173. begin
  1174. left.resultdef:=nil;
  1175. do_typecheckpass(left);
  1176. end;
  1177. end;
  1178. {$endif}
  1179. end.