cg64f32.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Member of the Free Pascal development team
  5. This unit implements the code generation for 64 bit int
  6. arithmethics on 32 bit processors
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. {# This unit implements the code generation for 64 bit int arithmethics on
  21. 32 bit processors.
  22. }
  23. unit cg64f32;
  24. {$i fpcdefs.inc}
  25. interface
  26. uses
  27. aasmbase,aasmtai,aasmcpu,
  28. cpuinfo,cpubase,cpupara,
  29. cgbase,cgobj,parabase,
  30. node,symtype
  31. {$ifdef delphi}
  32. ,dmisc
  33. {$endif}
  34. ;
  35. type
  36. {# Defines all the methods required on 32-bit processors
  37. to handle 64-bit integers.
  38. }
  39. tcg64f32 = class(tcg64)
  40. procedure a_load64_const_ref(list : taasmoutput;value : int64;const ref : treference);override;
  41. procedure a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);override;
  42. procedure a_load64_ref_reg(list : taasmoutput;const ref : treference;reg : tregister64);override;
  43. procedure a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64);override;
  44. procedure a_load64_const_reg(list : taasmoutput;value: int64;reg : tregister64);override;
  45. procedure a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64);override;
  46. procedure a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);override;
  47. procedure a_load64_const_loc(list : taasmoutput;value : int64;const l : tlocation);override;
  48. procedure a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);override;
  49. procedure a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);override;
  50. procedure a_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);override;
  51. procedure a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);override;
  52. procedure a_load64low_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);override;
  53. procedure a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);override;
  54. procedure a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);override;
  55. procedure a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);override;
  56. procedure a_op64_reg_ref(list : taasmoutput;op:TOpCG;reg : tregister64; const ref: treference);override;
  57. procedure a_op64_const_loc(list : taasmoutput;op:TOpCG;value : int64;const l: tlocation);override;
  58. procedure a_op64_reg_loc(list : taasmoutput;op:TOpCG;reg : tregister64;const l : tlocation);override;
  59. procedure a_op64_loc_reg(list : taasmoutput;op:TOpCG;const l : tlocation;reg : tregister64);override;
  60. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;value : int64;const ref : treference);override;
  61. procedure a_param64_reg(list : taasmoutput;reg : tregister64;const paraloc : tcgpara);override;
  62. procedure a_param64_const(list : taasmoutput;value : int64;const paraloc : tcgpara);override;
  63. procedure a_param64_ref(list : taasmoutput;const r : treference;const paraloc : tcgpara);override;
  64. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const paraloc : tcgpara);override;
  65. {# This routine tries to optimize the a_op64_const_reg operation, by
  66. removing superfluous opcodes. Returns TRUE if normal processing
  67. must continue in op64_const_reg, otherwise, everything is processed
  68. entirely in this routine, by emitting the appropriate 32-bit opcodes.
  69. }
  70. function optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : int64; var reg: tregister64): boolean;override;
  71. procedure g_rangecheck64(list: taasmoutput; const l:tlocation;fromdef,todef: tdef); override;
  72. end;
  73. {# Creates a tregister64 record from 2 32 Bit registers. }
  74. function joinreg64(reglo,reghi : tregister) : tregister64;
  75. implementation
  76. uses
  77. globtype,systems,
  78. verbose,
  79. symbase,symconst,symdef,defutil,paramgr;
  80. {****************************************************************************
  81. Helpers
  82. ****************************************************************************}
  83. function joinreg64(reglo,reghi : tregister) : tregister64;
  84. begin
  85. result.reglo:=reglo;
  86. result.reghi:=reghi;
  87. end;
  88. procedure swap64(var q : int64);
  89. begin
  90. q:=(int64(lo(q)) shl 32) or hi(q);
  91. end;
  92. procedure splitparaloc64(const cgpara:tcgpara;var cgparalo,cgparahi:tcgpara);
  93. var
  94. paraloclo,
  95. paralochi : pcgparalocation;
  96. begin
  97. if not(cgpara.size in [OS_64,OS_S64]) then
  98. internalerror(200408231);
  99. if not assigned(cgpara.location) then
  100. internalerror(200408201);
  101. { init lo/hi para }
  102. cgparahi.reset;
  103. if cgpara.size=OS_S64 then
  104. cgparahi.size:=OS_S32
  105. else
  106. cgparahi.size:=OS_32;
  107. cgparahi.alignment:=cgpara.alignment;
  108. paralochi:=cgparahi.add_location;
  109. cgparalo.reset;
  110. cgparalo.size:=OS_32;
  111. cgparalo.alignment:=cgpara.alignment;
  112. paraloclo:=cgparalo.add_location;
  113. { 2 parameter fields? }
  114. if assigned(cgpara.location^.next) then
  115. begin
  116. if target_info.endian = endian_big then
  117. begin
  118. { low is in second location }
  119. move(cgpara.location^.next^,paraloclo^,sizeof(paraloclo^));
  120. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  121. end
  122. else
  123. begin
  124. { low is in first location }
  125. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  126. move(cgpara.location^.next^,paralochi^,sizeof(paralochi^));
  127. end;
  128. end
  129. else
  130. begin
  131. { single parameter, this can only be in memory }
  132. if cgpara.location^.loc<>LOC_REFERENCE then
  133. internalerror(200408282);
  134. move(cgpara.location^,paraloclo^,sizeof(paraloclo^));
  135. move(cgpara.location^,paralochi^,sizeof(paralochi^));
  136. { for big endian low is at +4, for little endian high }
  137. if target_info.endian = endian_big then
  138. inc(cgparalo.location^.reference.offset,tcgsize2size[cgparahi.size])
  139. else
  140. inc(cgparahi.location^.reference.offset,tcgsize2size[cgparalo.size]);
  141. end;
  142. { fix size }
  143. paraloclo^.size:=cgparalo.size;
  144. paraloclo^.next:=nil;
  145. paralochi^.size:=cgparahi.size;
  146. paralochi^.next:=nil;
  147. end;
  148. {****************************************************************************
  149. TCG64F32
  150. ****************************************************************************}
  151. procedure tcg64f32.a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);
  152. var
  153. tmpreg: tregister;
  154. tmpref: treference;
  155. begin
  156. if target_info.endian = endian_big then
  157. begin
  158. tmpreg:=reg.reglo;
  159. reg.reglo:=reg.reghi;
  160. reg.reghi:=tmpreg;
  161. end;
  162. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reglo,ref);
  163. tmpref := ref;
  164. inc(tmpref.offset,4);
  165. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reghi,tmpref);
  166. end;
  167. procedure tcg64f32.a_load64_const_ref(list : taasmoutput;value : int64;const ref : treference);
  168. var
  169. tmpref: treference;
  170. begin
  171. if target_info.endian = endian_big then
  172. swap64(value);
  173. cg.a_load_const_ref(list,OS_32,aint(lo(value)),ref);
  174. tmpref := ref;
  175. inc(tmpref.offset,4);
  176. cg.a_load_const_ref(list,OS_32,aint(hi(value)),tmpref);
  177. end;
  178. procedure tcg64f32.a_load64_ref_reg(list : taasmoutput;const ref : treference;reg : tregister64);
  179. var
  180. tmpreg: tregister;
  181. tmpref: treference;
  182. begin
  183. if target_info.endian = endian_big then
  184. begin
  185. tmpreg := reg.reglo;
  186. reg.reglo := reg.reghi;
  187. reg.reghi := tmpreg;
  188. end;
  189. tmpref := ref;
  190. if (tmpref.base=reg.reglo) then
  191. begin
  192. tmpreg:=cg.getaddressregister(list);
  193. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.base,tmpreg);
  194. tmpref.base:=tmpreg;
  195. end
  196. else
  197. { this works only for the i386, thus the i386 needs to override }
  198. { this method and this method must be replaced by a more generic }
  199. { implementation FK }
  200. if (tmpref.index=reg.reglo) then
  201. begin
  202. tmpreg:=cg.getaddressregister(list);
  203. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.index,tmpreg);
  204. tmpref.index:=tmpreg;
  205. end;
  206. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reglo);
  207. inc(tmpref.offset,4);
  208. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reghi);
  209. end;
  210. procedure tcg64f32.a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64);
  211. begin
  212. cg.a_load_reg_reg(list,OS_32,OS_32,regsrc.reglo,regdst.reglo);
  213. cg.a_load_reg_reg(list,OS_32,OS_32,regsrc.reghi,regdst.reghi);
  214. end;
  215. procedure tcg64f32.a_load64_const_reg(list : taasmoutput;value : int64;reg : tregister64);
  216. begin
  217. cg.a_load_const_reg(list,OS_32,aint(lo(value)),reg.reglo);
  218. cg.a_load_const_reg(list,OS_32,aint(hi(value)),reg.reghi);
  219. end;
  220. procedure tcg64f32.a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64);
  221. begin
  222. case l.loc of
  223. LOC_REFERENCE, LOC_CREFERENCE:
  224. a_load64_ref_reg(list,l.reference,reg);
  225. LOC_REGISTER,LOC_CREGISTER:
  226. a_load64_reg_reg(list,l.register64,reg);
  227. LOC_CONSTANT :
  228. a_load64_const_reg(list,l.value64,reg);
  229. else
  230. internalerror(200112292);
  231. end;
  232. end;
  233. procedure tcg64f32.a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);
  234. begin
  235. case l.loc of
  236. LOC_REGISTER,LOC_CREGISTER:
  237. a_load64_reg_ref(list,l.register64,ref);
  238. LOC_CONSTANT :
  239. a_load64_const_ref(list,l.value64,ref);
  240. else
  241. internalerror(200203288);
  242. end;
  243. end;
  244. procedure tcg64f32.a_load64_const_loc(list : taasmoutput;value : int64;const l : tlocation);
  245. begin
  246. case l.loc of
  247. LOC_REFERENCE, LOC_CREFERENCE:
  248. a_load64_const_ref(list,value,l.reference);
  249. LOC_REGISTER,LOC_CREGISTER:
  250. a_load64_const_reg(list,value,l.register64);
  251. else
  252. internalerror(200112293);
  253. end;
  254. end;
  255. procedure tcg64f32.a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);
  256. begin
  257. case l.loc of
  258. LOC_REFERENCE, LOC_CREFERENCE:
  259. a_load64_reg_ref(list,reg,l.reference);
  260. LOC_REGISTER,LOC_CREGISTER:
  261. a_load64_reg_reg(list,reg,l.register64);
  262. else
  263. internalerror(200112293);
  264. end;
  265. end;
  266. procedure tcg64f32.a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);
  267. var
  268. tmpref: treference;
  269. begin
  270. if target_info.endian = endian_big then
  271. cg.a_load_reg_ref(list,OS_32,OS_32,reg,ref)
  272. else
  273. begin
  274. tmpref := ref;
  275. inc(tmpref.offset,4);
  276. cg.a_load_reg_ref(list,OS_32,OS_32,reg,tmpref)
  277. end;
  278. end;
  279. procedure tcg64f32.a_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);
  280. var
  281. tmpref: treference;
  282. begin
  283. if target_info.endian = endian_little then
  284. cg.a_load_reg_ref(list,OS_32,OS_32,reg,ref)
  285. else
  286. begin
  287. tmpref := ref;
  288. inc(tmpref.offset,4);
  289. cg.a_load_reg_ref(list,OS_32,OS_32,reg,tmpref)
  290. end;
  291. end;
  292. procedure tcg64f32.a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);
  293. var
  294. tmpref: treference;
  295. begin
  296. if target_info.endian = endian_big then
  297. cg.a_load_ref_reg(list,OS_32,OS_32,ref,reg)
  298. else
  299. begin
  300. tmpref := ref;
  301. inc(tmpref.offset,4);
  302. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg)
  303. end;
  304. end;
  305. procedure tcg64f32.a_load64low_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);
  306. var
  307. tmpref: treference;
  308. begin
  309. if target_info.endian = endian_little then
  310. cg.a_load_ref_reg(list,OS_32,OS_32,ref,reg)
  311. else
  312. begin
  313. tmpref := ref;
  314. inc(tmpref.offset,4);
  315. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg)
  316. end;
  317. end;
  318. procedure tcg64f32.a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);
  319. begin
  320. case l.loc of
  321. LOC_REFERENCE,
  322. LOC_CREFERENCE :
  323. a_load64low_ref_reg(list,l.reference,reg);
  324. LOC_REGISTER :
  325. cg.a_load_reg_reg(list,OS_32,OS_32,l.registerlow,reg);
  326. LOC_CONSTANT :
  327. cg.a_load_const_reg(list,OS_32,aint(lo(l.value64)),reg);
  328. else
  329. internalerror(200203244);
  330. end;
  331. end;
  332. procedure tcg64f32.a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);
  333. begin
  334. case l.loc of
  335. LOC_REFERENCE,
  336. LOC_CREFERENCE :
  337. a_load64high_ref_reg(list,l.reference,reg);
  338. LOC_REGISTER :
  339. cg.a_load_reg_reg(list,OS_32,OS_32,l.registerhigh,reg);
  340. LOC_CONSTANT :
  341. cg.a_load_const_reg(list,OS_32,hi(l.value64),reg);
  342. else
  343. internalerror(200203244);
  344. end;
  345. end;
  346. procedure tcg64f32.a_op64_const_loc(list : taasmoutput;op:TOpCG;value : int64;const l: tlocation);
  347. begin
  348. case l.loc of
  349. LOC_REFERENCE, LOC_CREFERENCE:
  350. a_op64_const_ref(list,op,value,l.reference);
  351. LOC_REGISTER,LOC_CREGISTER:
  352. a_op64_const_reg(list,op,value,l.register64);
  353. else
  354. internalerror(200203292);
  355. end;
  356. end;
  357. procedure tcg64f32.a_op64_reg_loc(list : taasmoutput;op:TOpCG;reg : tregister64;const l : tlocation);
  358. begin
  359. case l.loc of
  360. LOC_REFERENCE, LOC_CREFERENCE:
  361. a_op64_reg_ref(list,op,reg,l.reference);
  362. LOC_REGISTER,LOC_CREGISTER:
  363. a_op64_reg_reg(list,op,reg,l.register64);
  364. else
  365. internalerror(2002032422);
  366. end;
  367. end;
  368. procedure tcg64f32.a_op64_loc_reg(list : taasmoutput;op:TOpCG;const l : tlocation;reg : tregister64);
  369. begin
  370. case l.loc of
  371. LOC_REFERENCE, LOC_CREFERENCE:
  372. a_op64_ref_reg(list,op,l.reference,reg);
  373. LOC_REGISTER,LOC_CREGISTER:
  374. a_op64_reg_reg(list,op,l.register64,reg);
  375. LOC_CONSTANT :
  376. a_op64_const_reg(list,op,l.value64,reg);
  377. else
  378. internalerror(200203242);
  379. end;
  380. end;
  381. procedure tcg64f32.a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);
  382. var
  383. tempreg: tregister64;
  384. begin
  385. tempreg.reghi:=cg.getintregister(list,OS_32);
  386. tempreg.reglo:=cg.getintregister(list,OS_32);
  387. a_load64_ref_reg(list,ref,tempreg);
  388. a_op64_reg_reg(list,op,tempreg,reg);
  389. end;
  390. procedure tcg64f32.a_op64_reg_ref(list : taasmoutput;op:TOpCG;reg : tregister64; const ref: treference);
  391. var
  392. tempreg: tregister64;
  393. begin
  394. tempreg.reghi:=cg.getintregister(list,OS_32);
  395. tempreg.reglo:=cg.getintregister(list,OS_32);
  396. a_load64_ref_reg(list,ref,tempreg);
  397. a_op64_reg_reg(list,op,reg,tempreg);
  398. a_load64_reg_ref(list,tempreg,ref);
  399. end;
  400. procedure tcg64f32.a_op64_const_ref(list : taasmoutput;op:TOpCG;value : int64;const ref : treference);
  401. var
  402. tempreg: tregister64;
  403. begin
  404. tempreg.reghi:=cg.getintregister(list,OS_32);
  405. tempreg.reglo:=cg.getintregister(list,OS_32);
  406. a_load64_ref_reg(list,ref,tempreg);
  407. a_op64_const_reg(list,op,value,tempreg);
  408. a_load64_reg_ref(list,tempreg,ref);
  409. end;
  410. procedure tcg64f32.a_param64_reg(list : taasmoutput;reg : tregister64;const paraloc : tcgpara);
  411. var
  412. tmplochi,tmploclo: tcgpara;
  413. begin
  414. tmploclo.init;
  415. tmplochi.init;
  416. splitparaloc64(paraloc,tmploclo,tmplochi);
  417. cg.a_param_reg(list,OS_32,reg.reghi,tmplochi);
  418. cg.a_param_reg(list,OS_32,reg.reglo,tmploclo);
  419. tmploclo.done;
  420. tmplochi.done;
  421. end;
  422. procedure tcg64f32.a_param64_const(list : taasmoutput;value : int64;const paraloc : tcgpara);
  423. var
  424. tmplochi,tmploclo: tcgpara;
  425. begin
  426. tmploclo.init;
  427. tmplochi.init;
  428. splitparaloc64(paraloc,tmploclo,tmplochi);
  429. cg.a_param_const(list,OS_32,aint(hi(value)),tmplochi);
  430. cg.a_param_const(list,OS_32,aint(lo(value)),tmploclo);
  431. tmploclo.done;
  432. tmplochi.done;
  433. end;
  434. procedure tcg64f32.a_param64_ref(list : taasmoutput;const r : treference;const paraloc : tcgpara);
  435. var
  436. tmprefhi,tmpreflo : treference;
  437. tmploclo,tmplochi : tcgpara;
  438. begin
  439. tmploclo.init;
  440. tmplochi.init;
  441. splitparaloc64(paraloc,tmploclo,tmplochi);
  442. tmprefhi:=r;
  443. tmpreflo:=r;
  444. if target_info.endian=endian_big then
  445. inc(tmpreflo.offset,4)
  446. else
  447. inc(tmprefhi.offset,4);
  448. cg.a_param_ref(list,OS_32,tmprefhi,tmplochi);
  449. cg.a_param_ref(list,OS_32,tmpreflo,tmploclo);
  450. tmploclo.done;
  451. tmplochi.done;
  452. end;
  453. procedure tcg64f32.a_param64_loc(list : taasmoutput;const l:tlocation;const paraloc : tcgpara);
  454. begin
  455. case l.loc of
  456. LOC_REGISTER,
  457. LOC_CREGISTER :
  458. a_param64_reg(list,l.register64,paraloc);
  459. LOC_CONSTANT :
  460. a_param64_const(list,l.value64,paraloc);
  461. LOC_CREFERENCE,
  462. LOC_REFERENCE :
  463. a_param64_ref(list,l.reference,paraloc);
  464. else
  465. internalerror(200203287);
  466. end;
  467. end;
  468. procedure tcg64f32.g_rangecheck64(list : taasmoutput;const l:tlocation;fromdef,todef:tdef);
  469. var
  470. neglabel,
  471. poslabel,
  472. endlabel: tasmlabel;
  473. hreg : tregister;
  474. hdef : torddef;
  475. opsize : tcgsize;
  476. oldregisterdef: boolean;
  477. from_signed,to_signed: boolean;
  478. temploc : tlocation;
  479. begin
  480. from_signed := is_signed(fromdef);
  481. to_signed := is_signed(todef);
  482. if not is_64bit(todef) then
  483. begin
  484. oldregisterdef := registerdef;
  485. registerdef := false;
  486. { get the high dword in a register }
  487. if l.loc in [LOC_REGISTER,LOC_CREGISTER] then
  488. begin
  489. hreg := l.registerhigh;
  490. end
  491. else
  492. begin
  493. hreg:=cg.getintregister(list,OS_32);
  494. a_load64high_ref_reg(list,l.reference,hreg);
  495. end;
  496. objectlibrary.getlabel(poslabel);
  497. { check high dword, must be 0 (for positive numbers) }
  498. cg.a_cmp_const_reg_label(list,OS_32,OC_EQ,0,hreg,poslabel);
  499. { It can also be $ffffffff, but only for negative numbers }
  500. if from_signed and to_signed then
  501. begin
  502. objectlibrary.getlabel(neglabel);
  503. cg.a_cmp_const_reg_label(list,OS_32,OC_EQ,-1,hreg,neglabel);
  504. end;
  505. { For all other values we have a range check error }
  506. cg.a_call_name(list,'FPC_RANGEERROR');
  507. { if the high dword = 0, the low dword can be considered a }
  508. { simple cardinal }
  509. cg.a_label(list,poslabel);
  510. hdef:=torddef.create(u32bit,0,$ffffffff);
  511. location_copy(temploc,l);
  512. temploc.size:=OS_32;
  513. if (temploc.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  514. (target_info.endian = endian_big) then
  515. inc(temploc.reference.offset,4);
  516. cg.g_rangecheck(list,temploc,hdef,todef);
  517. hdef.free;
  518. if from_signed and to_signed then
  519. begin
  520. objectlibrary.getlabel(endlabel);
  521. cg.a_jmp_always(list,endlabel);
  522. { if the high dword = $ffffffff, then the low dword (when }
  523. { considered as a longint) must be < 0 }
  524. cg.a_label(list,neglabel);
  525. if l.loc in [LOC_REGISTER,LOC_CREGISTER] then
  526. begin
  527. hreg := l.registerlow;
  528. end
  529. else
  530. begin
  531. hreg:=cg.getintregister(list,OS_32);
  532. a_load64low_ref_reg(list,l.reference,hreg);
  533. end;
  534. { get a new neglabel (JM) }
  535. objectlibrary.getlabel(neglabel);
  536. cg.a_cmp_const_reg_label(list,OS_32,OC_LT,0,hreg,neglabel);
  537. cg.a_call_name(list,'FPC_RANGEERROR');
  538. { if we get here, the 64bit value lies between }
  539. { longint($80000000) and -1 (JM) }
  540. cg.a_label(list,neglabel);
  541. hdef:=torddef.create(s32bit,longint($80000000),-1);
  542. location_copy(temploc,l);
  543. temploc.size:=OS_32;
  544. cg.g_rangecheck(list,temploc,hdef,todef);
  545. hdef.free;
  546. cg.a_label(list,endlabel);
  547. end;
  548. registerdef := oldregisterdef;
  549. end
  550. else
  551. { todef = 64bit int }
  552. { no 64bit subranges supported, so only a small check is necessary }
  553. { if both are signed or both are unsigned, no problem! }
  554. if (from_signed xor to_signed) and
  555. { also not if the fromdef is unsigned and < 64bit, since that will }
  556. { always fit in a 64bit int (todef is 64bit) }
  557. (from_signed or
  558. (torddef(fromdef).typ = u64bit)) then
  559. begin
  560. { in all cases, there is only a problem if the higest bit is set }
  561. if l.loc in [LOC_REGISTER,LOC_CREGISTER] then
  562. begin
  563. if is_64bit(fromdef) then
  564. begin
  565. hreg := l.registerhigh;
  566. opsize := OS_32;
  567. end
  568. else
  569. begin
  570. hreg := l.register;
  571. opsize := def_cgsize(fromdef);
  572. end;
  573. end
  574. else
  575. begin
  576. hreg:=cg.getintregister(list,OS_32);
  577. opsize := def_cgsize(fromdef);
  578. if opsize in [OS_64,OS_S64] then
  579. a_load64high_ref_reg(list,l.reference,hreg)
  580. else
  581. cg.a_load_ref_reg(list,opsize,OS_32,l.reference,hreg);
  582. end;
  583. objectlibrary.getlabel(poslabel);
  584. cg.a_cmp_const_reg_label(list,opsize,OC_GTE,0,hreg,poslabel);
  585. cg.a_call_name(list,'FPC_RANGEERROR');
  586. cg.a_label(list,poslabel);
  587. end;
  588. end;
  589. function tcg64f32.optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : int64; var reg: tregister64): boolean;
  590. var
  591. lowvalue, highvalue : longint;
  592. hreg: tregister;
  593. begin
  594. lowvalue := longint(a);
  595. highvalue:= longint(a shr 32);
  596. { assume it will be optimized out }
  597. optimize64_op_const_reg := true;
  598. case op of
  599. OP_ADD:
  600. begin
  601. if a = 0 then
  602. exit;
  603. end;
  604. OP_AND:
  605. begin
  606. if lowvalue <> -1 then
  607. cg.a_op_const_reg(list,op,OS_32,lowvalue,reg.reglo);
  608. if highvalue <> -1 then
  609. cg.a_op_const_reg(list,op,OS_32,highvalue,reg.reghi);
  610. { already emitted correctly }
  611. exit;
  612. end;
  613. OP_OR:
  614. begin
  615. if lowvalue <> 0 then
  616. cg.a_op_const_reg(list,op,OS_32,lowvalue,reg.reglo);
  617. if highvalue <> 0 then
  618. cg.a_op_const_reg(list,op,OS_32,highvalue,reg.reghi);
  619. { already emitted correctly }
  620. exit;
  621. end;
  622. OP_SUB:
  623. begin
  624. if a = 0 then
  625. exit;
  626. end;
  627. OP_XOR:
  628. begin
  629. end;
  630. OP_SHL:
  631. begin
  632. if a = 0 then
  633. exit;
  634. { simply clear low-register
  635. and shift the rest and swap
  636. registers.
  637. }
  638. if (a > 31) then
  639. begin
  640. cg.a_load_const_reg(list,OS_32,0,reg.reglo);
  641. cg.a_op_const_reg(list,OP_SHL,OS_32,a mod 32,reg.reghi);
  642. { swap the registers }
  643. hreg := reg.reghi;
  644. reg.reghi := reg.reglo;
  645. reg.reglo := hreg;
  646. exit;
  647. end;
  648. end;
  649. OP_SHR:
  650. begin
  651. if a = 0 then exit;
  652. { simply clear high-register
  653. and shift the rest and swap
  654. registers.
  655. }
  656. if (a > 31) then
  657. begin
  658. cg.a_load_const_reg(list,OS_32,0,reg.reghi);
  659. cg.a_op_const_reg(list,OP_SHL,OS_32,a mod 32,reg.reglo);
  660. { swap the registers }
  661. hreg := reg.reghi;
  662. reg.reghi := reg.reglo;
  663. reg.reglo := hreg;
  664. exit;
  665. end;
  666. end;
  667. OP_IMUL,OP_MUL:
  668. begin
  669. if a = 1 then exit;
  670. end;
  671. OP_IDIV,OP_DIV:
  672. begin
  673. if a = 1 then exit;
  674. end;
  675. else
  676. internalerror(20020817);
  677. end;
  678. optimize64_op_const_reg := false;
  679. end;
  680. end.
  681. {
  682. $Log$
  683. Revision 1.63 2004-09-25 14:23:54 peter
  684. * ungetregister is now only used for cpuregisters, renamed to
  685. ungetcpuregister
  686. * renamed (get|unget)explicitregister(s) to ..cpuregister
  687. * removed location-release/reference_release
  688. Revision 1.62 2004/09/21 17:25:12 peter
  689. * paraloc branch merged
  690. Revision 1.61.4.2 2004/09/20 20:46:34 peter
  691. * register allocation optimized for 64bit loading of parameters
  692. and return values
  693. Revision 1.61.4.1 2004/08/31 20:43:06 peter
  694. * paraloc patch
  695. Revision 1.61 2004/06/20 08:55:28 florian
  696. * logs truncated
  697. Revision 1.60 2004/06/18 15:16:46 peter
  698. * remove obsolete cardinal() typecasts
  699. Revision 1.59 2004/06/17 16:55:46 peter
  700. * powerpc compiles again
  701. Revision 1.58 2004/06/16 20:07:07 florian
  702. * dwarf branch merged
  703. Revision 1.57.2.5 2004/06/13 10:51:16 florian
  704. * fixed several register allocator problems (sparc/arm)
  705. Revision 1.57.2.4 2004/06/12 17:01:01 florian
  706. * fixed compilation of arm compiler
  707. }