nmat.pas 39 KB

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