cg64f32.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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,
  29. cginfo, cgobj,
  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_reg_alloc(list : taasmoutput;r : tregister64);override;
  41. procedure a_reg_dealloc(list : taasmoutput;r : tregister64);override;
  42. procedure a_load64_const_ref(list : taasmoutput;value : qword;const ref : treference);override;
  43. procedure a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);override;
  44. procedure a_load64_ref_reg(list : taasmoutput;const ref : treference;reg : tregister64);override;
  45. procedure a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64);override;
  46. procedure a_load64_const_reg(list : taasmoutput;value: qword;reg : tregister64);override;
  47. procedure a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64);override;
  48. procedure a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);override;
  49. procedure a_load64_const_loc(list : taasmoutput;value : qword;const l : tlocation);override;
  50. procedure a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);override;
  51. procedure a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);override;
  52. procedure a_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);override;
  53. procedure a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);override;
  54. procedure a_load64low_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);override;
  55. procedure a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);override;
  56. procedure a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);override;
  57. procedure a_op64_const_loc(list : taasmoutput;op:TOpCG;value : qword;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_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);override;
  61. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);override;
  62. procedure a_param64_reg(list : taasmoutput;reg : tregister64;const locpara : tparalocation);override;
  63. procedure a_param64_const(list : taasmoutput;value : qword;const locpara : tparalocation);override;
  64. procedure a_param64_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  65. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const locpara : tparalocation);override;
  66. {# This routine tries to optimize the a_op64_const_reg operation, by
  67. removing superfluous opcodes. Returns TRUE if normal processing
  68. must continue in op64_const_reg, otherwise, everything is processed
  69. entirely in this routine, by emitting the appropriate 32-bit opcodes.
  70. }
  71. function optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : qword; var reg: tregister64): boolean;override;
  72. procedure g_rangecheck64(list: taasmoutput; const p: tnode;
  73. const todef: tdef); override;
  74. end;
  75. {# Creates a tregister64 record from 2 32 Bit registers. }
  76. function joinreg64(reglo,reghi : tregister) : tregister64;
  77. implementation
  78. uses
  79. globtype,globals,systems,
  80. cgbase,
  81. verbose,
  82. symbase,symconst,symdef,defbase;
  83. function joinreg64(reglo,reghi : tregister) : tregister64;
  84. begin
  85. result.reglo:=reglo;
  86. result.reghi:=reghi;
  87. end;
  88. procedure tcg64f32.a_reg_alloc(list : taasmoutput;r : tregister64);
  89. begin
  90. list.concat(tai_regalloc.alloc(r.reglo));
  91. list.concat(tai_regalloc.alloc(r.reghi));
  92. end;
  93. procedure tcg64f32.a_reg_dealloc(list : taasmoutput;r : tregister64);
  94. begin
  95. list.concat(tai_regalloc.dealloc(r.reglo));
  96. list.concat(tai_regalloc.dealloc(r.reghi));
  97. end;
  98. procedure tcg64f32.a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);
  99. var
  100. tmpreg: tregister;
  101. tmpref: treference;
  102. begin
  103. if target_info.endian = endian_big then
  104. begin
  105. tmpreg:=reg.reglo;
  106. reg.reglo:=reg.reghi;
  107. reg.reghi:=tmpreg;
  108. end;
  109. cg.a_load_reg_ref(list,OS_32,reg.reglo,ref);
  110. tmpref := ref;
  111. inc(tmpref.offset,4);
  112. cg.a_load_reg_ref(list,OS_32,reg.reghi,tmpref);
  113. end;
  114. procedure tcg64f32.a_load64_const_ref(list : taasmoutput;value : qword;const ref : treference);
  115. var
  116. tmpvalue : DWord;
  117. tmpref: treference;
  118. begin
  119. if target_info.endian = endian_big then
  120. swap_qword(value);
  121. cg.a_load_const_ref(list,OS_32,lo(value),ref);
  122. tmpref := ref;
  123. inc(tmpref.offset,4);
  124. cg.a_load_const_ref(list,OS_32,hi(value),tmpref);
  125. end;
  126. procedure tcg64f32.a_load64_ref_reg(list : taasmoutput;const ref : treference;reg : tregister64);
  127. var
  128. tmpreg: tregister;
  129. tmpref: treference;
  130. got_scratch: boolean;
  131. begin
  132. if target_info.endian = endian_big then
  133. begin
  134. tmpreg := reg.reglo;
  135. reg.reglo := reg.reghi;
  136. reg.reghi := tmpreg;
  137. end;
  138. got_scratch:=false;
  139. tmpref := ref;
  140. if (tmpref.base=reg.reglo) then
  141. begin
  142. tmpreg := cg.get_scratch_reg_int(list);
  143. got_scratch:=true;
  144. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.base,tmpreg);
  145. tmpref.base:=tmpreg;
  146. end
  147. else
  148. { this works only for the i386, thus the i386 needs to override }
  149. { this method and this method must be replaced by a more generic }
  150. { implementation FK }
  151. if (tmpref.index=reg.reglo) then
  152. begin
  153. tmpreg:=cg.get_scratch_reg_int(list);
  154. got_scratch:=true;
  155. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.index,tmpreg);
  156. tmpref.index:=tmpreg;
  157. end;
  158. cg.a_load_ref_reg(list,OS_32,tmpref,reg.reglo);
  159. inc(tmpref.offset,4);
  160. cg.a_load_ref_reg(list,OS_32,tmpref,reg.reghi);
  161. if got_scratch then
  162. cg.free_scratch_reg(list,tmpreg);
  163. end;
  164. procedure tcg64f32.a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64);
  165. begin
  166. cg.a_load_reg_reg(list,OS_32,OS_32,regsrc.reglo,regdst.reglo);
  167. cg.a_load_reg_reg(list,OS_32,OS_32,regsrc.reghi,regdst.reghi);
  168. end;
  169. procedure tcg64f32.a_load64_const_reg(list : taasmoutput;value : qword;reg : tregister64);
  170. begin
  171. cg.a_load_const_reg(list,OS_32,lo(value),reg.reglo);
  172. cg.a_load_const_reg(list,OS_32,hi(value),reg.reghi);
  173. end;
  174. procedure tcg64f32.a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64);
  175. begin
  176. case l.loc of
  177. LOC_REFERENCE, LOC_CREFERENCE:
  178. a_load64_ref_reg(list,l.reference,reg);
  179. LOC_REGISTER,LOC_CREGISTER:
  180. a_load64_reg_reg(list,l.register64,reg);
  181. LOC_CONSTANT :
  182. a_load64_const_reg(list,l.valueqword,reg);
  183. else
  184. internalerror(200112292);
  185. end;
  186. end;
  187. procedure tcg64f32.a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);
  188. begin
  189. case l.loc of
  190. LOC_REGISTER,LOC_CREGISTER:
  191. a_load64_reg_ref(list,l.reg64,ref);
  192. LOC_CONSTANT :
  193. a_load64_const_ref(list,l.valueqword,ref);
  194. else
  195. internalerror(200203288);
  196. end;
  197. end;
  198. procedure tcg64f32.a_load64_const_loc(list : taasmoutput;value : qword;const l : tlocation);
  199. begin
  200. case l.loc of
  201. LOC_REFERENCE, LOC_CREFERENCE:
  202. a_load64_const_ref(list,value,l.reference);
  203. LOC_REGISTER,LOC_CREGISTER:
  204. a_load64_const_reg(list,value,l.reg64);
  205. else
  206. internalerror(200112293);
  207. end;
  208. end;
  209. procedure tcg64f32.a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);
  210. begin
  211. case l.loc of
  212. LOC_REFERENCE, LOC_CREFERENCE:
  213. a_load64_reg_ref(list,reg,l.reference);
  214. LOC_REGISTER,LOC_CREGISTER:
  215. a_load64_reg_reg(list,reg,l.register64);
  216. else
  217. internalerror(200112293);
  218. end;
  219. end;
  220. procedure tcg64f32.a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);
  221. var
  222. tmpref: treference;
  223. begin
  224. if target_info.endian = endian_big then
  225. cg.a_load_reg_ref(list,OS_32,reg,ref)
  226. else
  227. begin
  228. tmpref := ref;
  229. inc(tmpref.offset,4);
  230. cg.a_load_reg_ref(list,OS_32,reg,tmpref)
  231. end;
  232. end;
  233. procedure tcg64f32.a_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);
  234. var
  235. tmpref: treference;
  236. begin
  237. if target_info.endian = endian_little then
  238. cg.a_load_reg_ref(list,OS_32,reg,ref)
  239. else
  240. begin
  241. tmpref := ref;
  242. inc(tmpref.offset,4);
  243. cg.a_load_reg_ref(list,OS_32,reg,tmpref)
  244. end;
  245. end;
  246. procedure tcg64f32.a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);
  247. var
  248. tmpref: treference;
  249. begin
  250. if target_info.endian = endian_big then
  251. cg.a_load_ref_reg(list,OS_32,ref,reg)
  252. else
  253. begin
  254. tmpref := ref;
  255. inc(tmpref.offset,4);
  256. cg.a_load_ref_reg(list,OS_32,tmpref,reg)
  257. end;
  258. end;
  259. procedure tcg64f32.a_load64low_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);
  260. var
  261. tmpref: treference;
  262. begin
  263. if target_info.endian = endian_little then
  264. cg.a_load_ref_reg(list,OS_32,ref,reg)
  265. else
  266. begin
  267. tmpref := ref;
  268. inc(tmpref.offset,4);
  269. cg.a_load_ref_reg(list,OS_32,tmpref,reg)
  270. end;
  271. end;
  272. procedure tcg64f32.a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);
  273. begin
  274. case l.loc of
  275. LOC_REFERENCE,
  276. LOC_CREFERENCE :
  277. a_load64low_ref_reg(list,l.reference,reg);
  278. LOC_REGISTER :
  279. cg.a_load_reg_reg(list,OS_32,OS_32,l.registerlow,reg);
  280. LOC_CONSTANT :
  281. cg.a_load_const_reg(list,OS_32,lo(l.valueqword),reg);
  282. else
  283. internalerror(200203244);
  284. end;
  285. end;
  286. procedure tcg64f32.a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);
  287. begin
  288. case l.loc of
  289. LOC_REFERENCE,
  290. LOC_CREFERENCE :
  291. a_load64high_ref_reg(list,l.reference,reg);
  292. LOC_REGISTER :
  293. cg.a_load_reg_reg(list,OS_32,OS_32,l.registerhigh,reg);
  294. LOC_CONSTANT :
  295. cg.a_load_const_reg(list,OS_32,hi(l.valueqword),reg);
  296. else
  297. internalerror(200203244);
  298. end;
  299. end;
  300. procedure tcg64f32.a_op64_const_loc(list : taasmoutput;op:TOpCG;value : qword;const l: tlocation);
  301. begin
  302. case l.loc of
  303. LOC_REFERENCE, LOC_CREFERENCE:
  304. a_op64_const_ref(list,op,value,l.reference);
  305. LOC_REGISTER,LOC_CREGISTER:
  306. a_op64_const_reg(list,op,value,l.register64);
  307. else
  308. internalerror(200203292);
  309. end;
  310. end;
  311. procedure tcg64f32.a_op64_reg_loc(list : taasmoutput;op:TOpCG;reg : tregister64;const l : tlocation);
  312. begin
  313. case l.loc of
  314. LOC_REFERENCE, LOC_CREFERENCE:
  315. a_op64_reg_ref(list,op,reg,l.reference);
  316. LOC_REGISTER,LOC_CREGISTER:
  317. a_op64_reg_reg(list,op,reg,l.register64);
  318. else
  319. internalerror(2002032422);
  320. end;
  321. end;
  322. procedure tcg64f32.a_op64_loc_reg(list : taasmoutput;op:TOpCG;const l : tlocation;reg : tregister64);
  323. begin
  324. case l.loc of
  325. LOC_REFERENCE, LOC_CREFERENCE:
  326. a_op64_ref_reg(list,op,l.reference,reg);
  327. LOC_REGISTER,LOC_CREGISTER:
  328. a_op64_reg_reg(list,op,l.register64,reg);
  329. LOC_CONSTANT :
  330. a_op64_const_reg(list,op,l.valueqword,reg);
  331. else
  332. internalerror(200203242);
  333. end;
  334. end;
  335. procedure tcg64f32.a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);
  336. var
  337. tempreg: tregister64;
  338. begin
  339. tempreg.reghi := cg.get_scratch_reg_int(list);
  340. tempreg.reglo := cg.get_scratch_reg_int(list);
  341. a_load64_ref_reg(list,ref,tempreg);
  342. a_op64_reg_reg(list,op,tempreg,reg);
  343. cg.free_scratch_reg(list,tempreg.reglo);
  344. cg.free_scratch_reg(list,tempreg.reghi);
  345. end;
  346. procedure tcg64f32.a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);
  347. var
  348. tempreg: tregister64;
  349. begin
  350. tempreg.reghi := cg.get_scratch_reg_int(list);
  351. tempreg.reglo := cg.get_scratch_reg_int(list);
  352. a_load64_ref_reg(list,ref,tempreg);
  353. a_op64_const_reg(list,op,value,tempreg);
  354. a_load64_reg_ref(list,tempreg,ref);
  355. cg.free_scratch_reg(list,tempreg.reglo);
  356. cg.free_scratch_reg(list,tempreg.reghi);
  357. end;
  358. procedure tcg64f32.a_param64_reg(list : taasmoutput;reg : tregister64;const locpara : tparalocation);
  359. begin
  360. {$ifdef FPC}
  361. {$warning FIX ME}
  362. {$endif}
  363. cg.a_param_reg(list,OS_32,reg.reghi,locpara);
  364. { the nr+1 needs definitivly a fix FK }
  365. { maybe the parameter numbering needs }
  366. { to take care of this on 32 Bit }
  367. { systems FK }
  368. cg.a_param_reg(list,OS_32,reg.reglo,locpara);
  369. end;
  370. procedure tcg64f32.a_param64_const(list : taasmoutput;value : qword;const locpara : tparalocation);
  371. begin
  372. {$ifdef fpc}
  373. {$warning FIX ME}
  374. {$endif}
  375. if target_info.endian = endian_big then
  376. swap_qword(value);
  377. cg.a_param_const(list,OS_32,hi(value),locpara);
  378. { the nr+1 needs definitivly a fix FK }
  379. { maybe the parameter numbering needs }
  380. { to take care of this on 32 Bit }
  381. { systems FK }
  382. cg.a_param_const(list,OS_32,lo(value),locpara);
  383. end;
  384. procedure tcg64f32.a_param64_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  385. var
  386. tmpref: treference;
  387. tmploc: tparalocation;
  388. begin
  389. tmpref := r;
  390. inc(tmpref.offset,4);
  391. tmploc := locpara;
  392. tmploc.registerlow:=tmploc.registerhigh;
  393. if target_info.endian = endian_big then
  394. begin
  395. cg.a_param_ref(list,OS_32,tmpref,tmploc);
  396. cg.a_param_ref(list,OS_32,r,locpara);
  397. end
  398. else
  399. begin
  400. cg.a_param_ref(list,OS_32,tmpref,locpara);
  401. cg.a_param_ref(list,OS_32,r,locpara);
  402. end;
  403. end;
  404. procedure tcg64f32.a_param64_loc(list : taasmoutput;const l:tlocation;const locpara : tparalocation);
  405. begin
  406. {$ifdef fpc}
  407. {$warning FIX ME}
  408. {$endif}
  409. case l.loc of
  410. LOC_REGISTER,
  411. LOC_CREGISTER :
  412. a_param64_reg(list,l.register64,locpara);
  413. LOC_CONSTANT :
  414. a_param64_const(list,l.valueqword,locpara);
  415. LOC_CREFERENCE,
  416. LOC_REFERENCE :
  417. a_param64_ref(list,l.reference,locpara);
  418. else
  419. internalerror(200203287);
  420. end;
  421. end;
  422. procedure tcg64f32.g_rangecheck64(list : taasmoutput;const p : tnode;const todef : tdef);
  423. var
  424. neglabel,
  425. poslabel,
  426. endlabel: tasmlabel;
  427. hreg : tregister;
  428. hdef : torddef;
  429. fromdef : tdef;
  430. opsize : tcgsize;
  431. oldregisterdef: boolean;
  432. from_signed,to_signed: boolean;
  433. got_scratch: boolean;
  434. begin
  435. fromdef:=p.resulttype.def;
  436. from_signed := is_signed(fromdef);
  437. to_signed := is_signed(todef);
  438. if not is_64bitint(todef) then
  439. begin
  440. oldregisterdef := registerdef;
  441. registerdef := false;
  442. { get the high dword in a register }
  443. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  444. begin
  445. hreg := p.location.registerhigh;
  446. got_scratch := false
  447. end
  448. else
  449. begin
  450. hreg := cg.get_scratch_reg_int(list);
  451. got_scratch := true;
  452. a_load64high_ref_reg(list,p.location.reference,hreg);
  453. end;
  454. objectlibrary.getlabel(poslabel);
  455. { check high dword, must be 0 (for positive numbers) }
  456. cg.a_cmp_const_reg_label(list,OS_32,OC_EQ,0,hreg,poslabel);
  457. { It can also be $ffffffff, but only for negative numbers }
  458. if from_signed and to_signed then
  459. begin
  460. objectlibrary.getlabel(neglabel);
  461. cg.a_cmp_const_reg_label(list,OS_32,OC_EQ,aword(-1),hreg,neglabel);
  462. end;
  463. { !!! freeing of register should happen directly after compare! (JM) }
  464. if got_scratch then
  465. cg.free_scratch_reg(list,hreg);
  466. { For all other values we have a range check error }
  467. cg.a_call_name(list,'FPC_RANGEERROR');
  468. { if the high dword = 0, the low dword can be considered a }
  469. { simple cardinal }
  470. cg.a_label(list,poslabel);
  471. hdef:=torddef.create(u32bit,0,cardinal($ffffffff));
  472. { the real p.resulttype.def is already saved in fromdef }
  473. p.resulttype.def := hdef;
  474. { no use in calling just "g_rangecheck" since that one will }
  475. { simply call the inherited method too (JM) }
  476. cg.g_rangecheck(list,p,todef);
  477. hdef.free;
  478. { restore original resulttype.def }
  479. p.resulttype.def := todef;
  480. if from_signed and to_signed then
  481. begin
  482. objectlibrary.getlabel(endlabel);
  483. cg.a_jmp_always(list,endlabel);
  484. { if the high dword = $ffffffff, then the low dword (when }
  485. { considered as a longint) must be < 0 }
  486. cg.a_label(list,neglabel);
  487. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  488. begin
  489. hreg := p.location.registerlow;
  490. got_scratch := false
  491. end
  492. else
  493. begin
  494. hreg := cg.get_scratch_reg_int(list);
  495. got_scratch := true;
  496. a_load64low_ref_reg(list,p.location.reference,hreg);
  497. end;
  498. { get a new neglabel (JM) }
  499. objectlibrary.getlabel(neglabel);
  500. cg.a_cmp_const_reg_label(list,OS_32,OC_LT,0,hreg,neglabel);
  501. { !!! freeing of register should happen directly after compare! (JM) }
  502. if got_scratch then
  503. cg.free_scratch_reg(list,hreg);
  504. cg.a_call_name(list,'FPC_RANGEERROR');
  505. { if we get here, the 64bit value lies between }
  506. { longint($80000000) and -1 (JM) }
  507. cg.a_label(list,neglabel);
  508. hdef:=torddef.create(s32bit,longint($80000000),-1);
  509. p.resulttype.def := hdef;
  510. cg.g_rangecheck(list,p,todef);
  511. hdef.free;
  512. cg.a_label(list,endlabel);
  513. end;
  514. registerdef := oldregisterdef;
  515. p.resulttype.def := fromdef;
  516. { restore p's resulttype.def }
  517. end
  518. else
  519. { todef = 64bit int }
  520. { no 64bit subranges supported, so only a small check is necessary }
  521. { if both are signed or both are unsigned, no problem! }
  522. if (from_signed xor to_signed) and
  523. { also not if the fromdef is unsigned and < 64bit, since that will }
  524. { always fit in a 64bit int (todef is 64bit) }
  525. (from_signed or
  526. (torddef(fromdef).typ = u64bit)) then
  527. begin
  528. { in all cases, there is only a problem if the higest bit is set }
  529. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  530. begin
  531. if is_64bitint(fromdef) then
  532. begin
  533. hreg := p.location.registerhigh;
  534. opsize := OS_32;
  535. end
  536. else
  537. begin
  538. hreg := p.location.register;
  539. opsize := def_cgsize(p.resulttype.def);
  540. end;
  541. got_scratch := false;
  542. end
  543. else
  544. begin
  545. hreg := cg.get_scratch_reg_int(list);
  546. got_scratch := true;
  547. opsize := def_cgsize(p.resulttype.def);
  548. if opsize in [OS_64,OS_S64] then
  549. a_load64high_ref_reg(list,p.location.reference,hreg)
  550. else
  551. cg.a_load_ref_reg(list,opsize,p.location.reference,hreg);
  552. end;
  553. objectlibrary.getlabel(poslabel);
  554. cg.a_cmp_const_reg_label(list,opsize,OC_GTE,0,hreg,poslabel);
  555. { !!! freeing of register should happen directly after compare! (JM) }
  556. if got_scratch then
  557. cg.free_scratch_reg(list,hreg);
  558. cg.a_call_name(list,'FPC_RANGEERROR');
  559. cg.a_label(list,poslabel);
  560. end;
  561. end;
  562. function tcg64f32.optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : qword; var reg: tregister64): boolean;
  563. var
  564. lowvalue, highvalue : cardinal;
  565. hreg: tregister;
  566. begin
  567. lowvalue := cardinal(a);
  568. highvalue:= a shr 32;
  569. { assume it will be optimized out }
  570. optimize64_op_const_reg := true;
  571. case op of
  572. OP_ADD:
  573. begin
  574. if a = 0 then
  575. exit;
  576. end;
  577. OP_AND:
  578. begin
  579. if lowvalue <> high(cardinal) then
  580. cg.a_op_const_reg(list,op,lowvalue,reg.reglo);
  581. if highvalue <> high(cardinal) then
  582. cg.a_op_const_reg(list,op,highvalue,reg.reghi);
  583. { already emitted correctly }
  584. exit;
  585. end;
  586. OP_OR:
  587. begin
  588. if lowvalue <> 0 then
  589. cg.a_op_const_reg(list,op,lowvalue,reg.reglo);
  590. if highvalue <> 0 then
  591. cg.a_op_const_reg(list,op,highvalue,reg.reghi);
  592. { already emitted correctly }
  593. exit;
  594. end;
  595. OP_SUB:
  596. begin
  597. if a = 0 then
  598. exit;
  599. end;
  600. OP_XOR:
  601. begin
  602. end;
  603. OP_SHL:
  604. begin
  605. if a = 0 then
  606. exit;
  607. { simply clear low-register
  608. and shift the rest and swap
  609. registers.
  610. }
  611. if (a > 31) then
  612. begin
  613. cg.a_load_const_reg(list,OS_32,0,reg.reglo);
  614. cg.a_op_const_reg(list,OP_SHL,a mod 32,reg.reghi);
  615. { swap the registers }
  616. hreg := reg.reghi;
  617. reg.reghi := reg.reglo;
  618. reg.reglo := hreg;
  619. exit;
  620. end;
  621. end;
  622. OP_SHR:
  623. begin
  624. if a = 0 then exit;
  625. { simply clear high-register
  626. and shift the rest and swap
  627. registers.
  628. }
  629. if (a > 31) then
  630. begin
  631. cg.a_load_const_reg(list,OS_32,0,reg.reghi);
  632. cg.a_op_const_reg(list,OP_SHL,a mod 32,reg.reglo);
  633. { swap the registers }
  634. hreg := reg.reghi;
  635. reg.reghi := reg.reglo;
  636. reg.reglo := hreg;
  637. exit;
  638. end;
  639. end;
  640. OP_IMUL,OP_MUL:
  641. begin
  642. if a = 1 then exit;
  643. end;
  644. OP_IDIV,OP_DIV:
  645. begin
  646. if a = 1 then exit;
  647. end;
  648. else
  649. internalerror(20020817);
  650. end;
  651. optimize64_op_const_reg := false;
  652. end;
  653. (*
  654. procedure int64f32_assignment_int64_reg(p : passignmentnode);
  655. begin
  656. end;
  657. begin
  658. p2_assignment:=@int64f32_assignement_int64;
  659. *)
  660. end.
  661. {
  662. $Log$
  663. Revision 1.31 2002-10-05 12:43:23 carl
  664. * fixes for Delphi 6 compilation
  665. (warning : Some features do not work under Delphi)
  666. Revision 1.30 2002/09/17 18:54:01 jonas
  667. * a_load_reg_reg() now has two size parameters: source and dest. This
  668. allows some optimizations on architectures that don't encode the
  669. register size in the register name.
  670. Revision 1.29 2002/09/10 21:24:38 jonas
  671. * fixed a_param64_ref
  672. Revision 1.28 2002/09/07 15:25:00 peter
  673. * old logs removed and tabs fixed
  674. Revision 1.27 2002/08/19 18:17:47 carl
  675. + optimize64_op_const_reg implemented (optimizes 64-bit constant opcodes)
  676. * more fixes to m68k for 64-bit operations
  677. Revision 1.26 2002/08/17 22:09:43 florian
  678. * result type handling in tcgcal.pass_2 overhauled
  679. * better tnode.dowrite
  680. * some ppc stuff fixed
  681. Revision 1.25 2002/08/14 18:41:47 jonas
  682. - remove valuelow/valuehigh fields from tlocation, because they depend
  683. on the endianess of the host operating system -> difficult to get
  684. right. Use lo/hi(location.valueqword) instead (remember to use
  685. valueqword and not value!!)
  686. Revision 1.24 2002/08/11 14:32:26 peter
  687. * renamed current_library to objectlibrary
  688. Revision 1.23 2002/08/11 13:24:11 peter
  689. * saving of asmsymbols in ppu supported
  690. * asmsymbollist global is removed and moved into a new class
  691. tasmlibrarydata that will hold the info of a .a file which
  692. corresponds with a single module. Added librarydata to tmodule
  693. to keep the library info stored for the module. In the future the
  694. objectfiles will also be stored to the tasmlibrarydata class
  695. * all getlabel/newasmsymbol and friends are moved to the new class
  696. Revision 1.22 2002/07/28 15:57:15 jonas
  697. * fixed a_load64_const_reg() for big endian systems
  698. Revision 1.21 2002/07/20 11:57:52 florian
  699. * types.pas renamed to defbase.pas because D6 contains a types
  700. unit so this would conflicts if D6 programms are compiled
  701. + Willamette/SSE2 instructions to assembler added
  702. Revision 1.20 2002/07/12 10:14:26 jonas
  703. * some big-endian fixes
  704. Revision 1.19 2002/07/11 07:23:17 jonas
  705. + generic implementations of a_op64_ref_reg() and a_op64_const_ref()
  706. (only works for processors with >2 scratch registers)
  707. Revision 1.18 2002/07/10 11:12:44 jonas
  708. * fixed a_op64_const_loc()
  709. Revision 1.17 2002/07/07 09:52:32 florian
  710. * powerpc target fixed, very simple units can be compiled
  711. * some basic stuff for better callparanode handling, far from being finished
  712. Revision 1.16 2002/07/01 18:46:21 peter
  713. * internal linker
  714. * reorganized aasm layer
  715. Revision 1.15 2002/07/01 16:23:52 peter
  716. * cg64 patch
  717. * basics for currency
  718. * asnode updates for class and interface (not finished)
  719. Revision 1.14 2002/05/20 13:30:40 carl
  720. * bugfix of hdisponen (base must be set, not index)
  721. * more portability fixes
  722. Revision 1.13 2002/05/18 13:34:05 peter
  723. * readded missing revisions
  724. Revision 1.12 2002/05/16 19:46:35 carl
  725. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  726. + try to fix temp allocation (still in ifdef)
  727. + generic constructor calls
  728. + start of tassembler / tmodulebase class cleanup
  729. Revision 1.10 2002/05/12 16:53:04 peter
  730. * moved entry and exitcode to ncgutil and cgobj
  731. * foreach gets extra argument for passing local data to the
  732. iterator function
  733. * -CR checks also class typecasts at runtime by changing them
  734. into as
  735. * fixed compiler to cycle with the -CR option
  736. * fixed stabs with elf writer, finally the global variables can
  737. be watched
  738. * removed a lot of routines from cga unit and replaced them by
  739. calls to cgobj
  740. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  741. u32bit then the other is typecasted also to u32bit without giving
  742. a rangecheck warning/error.
  743. * fixed pascal calling method with reversing also the high tree in
  744. the parast, detected by tcalcst3 test
  745. Revision 1.9 2002/04/25 20:16:38 peter
  746. * moved more routines from cga/n386util
  747. Revision 1.8 2002/04/21 15:28:51 carl
  748. * a_jmp_cond -> a_jmp_always
  749. Revision 1.7 2002/04/07 13:21:18 carl
  750. + more documentation
  751. }