cg64f32.pas 31 KB

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