nmat.pas 41 KB

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