cg64f32.pas 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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_reg_ref(list : taasmoutput;op:TOpCG;reg : tregister64; const ref: treference);override;
  59. procedure a_op64_const_loc(list : taasmoutput;op:TOpCG;value : qword;const l: tlocation);override;
  60. procedure a_op64_reg_loc(list : taasmoutput;op:TOpCG;reg : tregister64;const l : tlocation);override;
  61. procedure a_op64_loc_reg(list : taasmoutput;op:TOpCG;const l : tlocation;reg : tregister64);override;
  62. procedure a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);override;
  63. procedure a_param64_reg(list : taasmoutput;reg : tregister64;const locpara : tparalocation);override;
  64. procedure a_param64_const(list : taasmoutput;value : qword;const locpara : tparalocation);override;
  65. procedure a_param64_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  66. procedure a_param64_loc(list : taasmoutput;const l : tlocation;const locpara : tparalocation);override;
  67. {# This routine tries to optimize the a_op64_const_reg operation, by
  68. removing superfluous opcodes. Returns TRUE if normal processing
  69. must continue in op64_const_reg, otherwise, everything is processed
  70. entirely in this routine, by emitting the appropriate 32-bit opcodes.
  71. }
  72. function optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : qword; var reg: tregister64): boolean;override;
  73. procedure g_rangecheck64(list: taasmoutput; const p: tnode;
  74. const todef: tdef); override;
  75. end;
  76. {# Creates a tregister64 record from 2 32 Bit registers. }
  77. function joinreg64(reglo,reghi : tregister) : tregister64;
  78. implementation
  79. uses
  80. globtype,globals,systems,
  81. cgbase,
  82. verbose,
  83. symbase,symconst,symdef,defutil,rgobj;
  84. function joinreg64(reglo,reghi : tregister) : tregister64;
  85. begin
  86. result.reglo:=reglo;
  87. result.reghi:=reghi;
  88. end;
  89. procedure tcg64f32.a_reg_alloc(list : taasmoutput;r : tregister64);
  90. begin
  91. list.concat(tai_regalloc.alloc(r.reglo));
  92. list.concat(tai_regalloc.alloc(r.reghi));
  93. end;
  94. procedure tcg64f32.a_reg_dealloc(list : taasmoutput;r : tregister64);
  95. begin
  96. list.concat(tai_regalloc.dealloc(r.reglo));
  97. list.concat(tai_regalloc.dealloc(r.reghi));
  98. end;
  99. procedure tcg64f32.a_load64_reg_ref(list : taasmoutput;reg : tregister64;const ref : treference);
  100. var
  101. tmpreg: tregister;
  102. tmpref: treference;
  103. begin
  104. if target_info.endian = endian_big then
  105. begin
  106. tmpreg:=reg.reglo;
  107. reg.reglo:=reg.reghi;
  108. reg.reghi:=tmpreg;
  109. end;
  110. cg.a_load_reg_ref(list,OS_32,reg.reglo,ref);
  111. tmpref := ref;
  112. inc(tmpref.offset,4);
  113. cg.a_load_reg_ref(list,OS_32,reg.reghi,tmpref);
  114. end;
  115. procedure tcg64f32.a_load64_const_ref(list : taasmoutput;value : qword;const ref : treference);
  116. var
  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. {$ifdef newra}
  147. tmpreg:=rg.getaddressregister(list);
  148. {$else}
  149. tmpreg := cg.get_scratch_reg_address(list);
  150. {$endif}
  151. got_scratch:=true;
  152. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.base,tmpreg);
  153. tmpref.base:=tmpreg;
  154. end
  155. else
  156. { this works only for the i386, thus the i386 needs to override }
  157. { this method and this method must be replaced by a more generic }
  158. { implementation FK }
  159. if (tmpref.index.number=reg.reglo.number) then
  160. begin
  161. {$ifdef newra}
  162. tmpreg:=rg.getaddressregister(list);
  163. {$else}
  164. tmpreg:=cg.get_scratch_reg_address(list);
  165. {$endif}
  166. got_scratch:=true;
  167. cg.a_load_reg_reg(list,OS_ADDR,OS_ADDR,tmpref.index,tmpreg);
  168. tmpref.index:=tmpreg;
  169. end;
  170. cg.a_load_ref_reg(list,OS_32,tmpref,reg.reglo);
  171. inc(tmpref.offset,4);
  172. cg.a_load_ref_reg(list,OS_32,tmpref,reg.reghi);
  173. {$ifdef newra}
  174. if got_scratch then
  175. rg.ungetregisterint(list,tmpreg);
  176. {$else}
  177. if got_scratch then
  178. cg.free_scratch_reg(list,tmpreg);
  179. {$endif}
  180. end;
  181. procedure tcg64f32.a_load64_reg_reg(list : taasmoutput;regsrc,regdst : tregister64);
  182. begin
  183. cg.a_load_reg_reg(list,OS_32,OS_32,regsrc.reglo,regdst.reglo);
  184. cg.a_load_reg_reg(list,OS_32,OS_32,regsrc.reghi,regdst.reghi);
  185. end;
  186. procedure tcg64f32.a_load64_const_reg(list : taasmoutput;value : qword;reg : tregister64);
  187. begin
  188. cg.a_load_const_reg(list,OS_32,lo(value),reg.reglo);
  189. cg.a_load_const_reg(list,OS_32,hi(value),reg.reghi);
  190. end;
  191. procedure tcg64f32.a_load64_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister64);
  192. begin
  193. case l.loc of
  194. LOC_REFERENCE, LOC_CREFERENCE:
  195. a_load64_ref_reg(list,l.reference,reg);
  196. LOC_REGISTER,LOC_CREGISTER:
  197. a_load64_reg_reg(list,l.register64,reg);
  198. LOC_CONSTANT :
  199. a_load64_const_reg(list,l.valueqword,reg);
  200. else
  201. internalerror(200112292);
  202. end;
  203. end;
  204. procedure tcg64f32.a_load64_loc_ref(list : taasmoutput;const l : tlocation;const ref : treference);
  205. begin
  206. case l.loc of
  207. LOC_REGISTER,LOC_CREGISTER:
  208. a_load64_reg_ref(list,l.reg64,ref);
  209. LOC_CONSTANT :
  210. a_load64_const_ref(list,l.valueqword,ref);
  211. else
  212. internalerror(200203288);
  213. end;
  214. end;
  215. procedure tcg64f32.a_load64_const_loc(list : taasmoutput;value : qword;const l : tlocation);
  216. begin
  217. case l.loc of
  218. LOC_REFERENCE, LOC_CREFERENCE:
  219. a_load64_const_ref(list,value,l.reference);
  220. LOC_REGISTER,LOC_CREGISTER:
  221. a_load64_const_reg(list,value,l.reg64);
  222. else
  223. internalerror(200112293);
  224. end;
  225. end;
  226. procedure tcg64f32.a_load64_reg_loc(list : taasmoutput;reg : tregister64;const l : tlocation);
  227. begin
  228. case l.loc of
  229. LOC_REFERENCE, LOC_CREFERENCE:
  230. a_load64_reg_ref(list,reg,l.reference);
  231. LOC_REGISTER,LOC_CREGISTER:
  232. a_load64_reg_reg(list,reg,l.register64);
  233. else
  234. internalerror(200112293);
  235. end;
  236. end;
  237. procedure tcg64f32.a_load64high_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);
  238. var
  239. tmpref: treference;
  240. begin
  241. if target_info.endian = endian_big 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_load64low_reg_ref(list : taasmoutput;reg : tregister;const ref : treference);
  251. var
  252. tmpref: treference;
  253. begin
  254. if target_info.endian = endian_little then
  255. cg.a_load_reg_ref(list,OS_32,reg,ref)
  256. else
  257. begin
  258. tmpref := ref;
  259. inc(tmpref.offset,4);
  260. cg.a_load_reg_ref(list,OS_32,reg,tmpref)
  261. end;
  262. end;
  263. procedure tcg64f32.a_load64high_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);
  264. var
  265. tmpref: treference;
  266. begin
  267. if target_info.endian = endian_big 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_ref_reg(list : taasmoutput;const ref : treference;reg : tregister);
  277. var
  278. tmpref: treference;
  279. begin
  280. if target_info.endian = endian_little then
  281. cg.a_load_ref_reg(list,OS_32,ref,reg)
  282. else
  283. begin
  284. tmpref := ref;
  285. inc(tmpref.offset,4);
  286. cg.a_load_ref_reg(list,OS_32,tmpref,reg)
  287. end;
  288. end;
  289. procedure tcg64f32.a_load64low_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);
  290. begin
  291. case l.loc of
  292. LOC_REFERENCE,
  293. LOC_CREFERENCE :
  294. a_load64low_ref_reg(list,l.reference,reg);
  295. LOC_REGISTER :
  296. cg.a_load_reg_reg(list,OS_32,OS_32,l.registerlow,reg);
  297. LOC_CONSTANT :
  298. cg.a_load_const_reg(list,OS_32,lo(l.valueqword),reg);
  299. else
  300. internalerror(200203244);
  301. end;
  302. end;
  303. procedure tcg64f32.a_load64high_loc_reg(list : taasmoutput;const l : tlocation;reg : tregister);
  304. begin
  305. case l.loc of
  306. LOC_REFERENCE,
  307. LOC_CREFERENCE :
  308. a_load64high_ref_reg(list,l.reference,reg);
  309. LOC_REGISTER :
  310. cg.a_load_reg_reg(list,OS_32,OS_32,l.registerhigh,reg);
  311. LOC_CONSTANT :
  312. cg.a_load_const_reg(list,OS_32,hi(l.valueqword),reg);
  313. else
  314. internalerror(200203244);
  315. end;
  316. end;
  317. procedure tcg64f32.a_op64_const_loc(list : taasmoutput;op:TOpCG;value : qword;const l: tlocation);
  318. begin
  319. case l.loc of
  320. LOC_REFERENCE, LOC_CREFERENCE:
  321. a_op64_const_ref(list,op,value,l.reference);
  322. LOC_REGISTER,LOC_CREGISTER:
  323. a_op64_const_reg(list,op,value,l.register64);
  324. else
  325. internalerror(200203292);
  326. end;
  327. end;
  328. procedure tcg64f32.a_op64_reg_loc(list : taasmoutput;op:TOpCG;reg : tregister64;const l : tlocation);
  329. begin
  330. case l.loc of
  331. LOC_REFERENCE, LOC_CREFERENCE:
  332. a_op64_reg_ref(list,op,reg,l.reference);
  333. LOC_REGISTER,LOC_CREGISTER:
  334. a_op64_reg_reg(list,op,reg,l.register64);
  335. else
  336. internalerror(2002032422);
  337. end;
  338. end;
  339. procedure tcg64f32.a_op64_loc_reg(list : taasmoutput;op:TOpCG;const l : tlocation;reg : tregister64);
  340. begin
  341. case l.loc of
  342. LOC_REFERENCE, LOC_CREFERENCE:
  343. a_op64_ref_reg(list,op,l.reference,reg);
  344. LOC_REGISTER,LOC_CREGISTER:
  345. a_op64_reg_reg(list,op,l.register64,reg);
  346. LOC_CONSTANT :
  347. a_op64_const_reg(list,op,l.valueqword,reg);
  348. else
  349. internalerror(200203242);
  350. end;
  351. end;
  352. procedure tcg64f32.a_op64_ref_reg(list : taasmoutput;op:TOpCG;const ref : treference;reg : tregister64);
  353. var
  354. tempreg: tregister64;
  355. begin
  356. {$ifdef newra}
  357. tempreg.reghi:=rg.getregisterint(list,OS_INT);
  358. tempreg.reglo:=rg.getregisterint(list,OS_INT);
  359. {$else}
  360. tempreg.reghi := cg.get_scratch_reg_int(list,OS_INT);
  361. tempreg.reglo := cg.get_scratch_reg_int(list,OS_INT);
  362. {$endif}
  363. a_load64_ref_reg(list,ref,tempreg);
  364. a_op64_reg_reg(list,op,tempreg,reg);
  365. {$ifdef newra}
  366. rg.ungetregisterint(list,tempreg.reglo);
  367. rg.ungetregisterint(list,tempreg.reghi);
  368. {$else}
  369. cg.free_scratch_reg(list,tempreg.reglo);
  370. cg.free_scratch_reg(list,tempreg.reghi);
  371. {$endif}
  372. end;
  373. procedure tcg64f32.a_op64_reg_ref(list : taasmoutput;op:TOpCG;reg : tregister64; const ref: treference);
  374. var
  375. tempreg: tregister64;
  376. begin
  377. {$ifdef newra}
  378. tempreg.reghi:=rg.getregisterint(list,OS_INT);
  379. tempreg.reglo:=rg.getregisterint(list,OS_INT);
  380. {$else}
  381. tempreg.reghi := cg.get_scratch_reg_int(list,OS_INT);
  382. tempreg.reglo := cg.get_scratch_reg_int(list,OS_INT);
  383. {$endif}
  384. a_load64_ref_reg(list,ref,tempreg);
  385. a_op64_reg_reg(list,op,reg,tempreg);
  386. a_load64_reg_ref(list,tempreg,ref);
  387. {$ifdef newra}
  388. rg.ungetregisterint(list,tempreg.reglo);
  389. rg.ungetregisterint(list,tempreg.reghi);
  390. {$else}
  391. cg.free_scratch_reg(list,tempreg.reglo);
  392. cg.free_scratch_reg(list,tempreg.reghi);
  393. {$endif}
  394. end;
  395. procedure tcg64f32.a_op64_const_ref(list : taasmoutput;op:TOpCG;value : qword;const ref : treference);
  396. var
  397. tempreg: tregister64;
  398. begin
  399. {$ifdef newra}
  400. tempreg.reghi:=rg.getregisterint(list,OS_INT);
  401. tempreg.reglo:=rg.getregisterint(list,OS_INT);
  402. {$else}
  403. tempreg.reghi := cg.get_scratch_reg_int(list,OS_INT);
  404. tempreg.reglo := cg.get_scratch_reg_int(list,OS_INT);
  405. {$endif}
  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. {$ifdef newra}
  410. rg.ungetregisterint(list,tempreg.reglo);
  411. rg.ungetregisterint(list,tempreg.reghi);
  412. {$else}
  413. cg.free_scratch_reg(list,tempreg.reglo);
  414. cg.free_scratch_reg(list,tempreg.reghi);
  415. {$endif}
  416. end;
  417. procedure tcg64f32.a_param64_reg(list : taasmoutput;reg : tregister64;const locpara : tparalocation);
  418. var
  419. tmplochi,tmploclo: tparalocation;
  420. begin
  421. tmplochi:=locpara;
  422. tmploclo:=locpara;
  423. if locpara.size=OS_S64 then
  424. tmplochi.size:=OS_S32
  425. else
  426. tmplochi.size:=OS_32;
  427. tmploclo.size:=OS_32;
  428. case locpara.loc of
  429. LOC_REGISTER:
  430. tmplochi.register:=tmplochi.registerhigh;
  431. { !!! i386 doesn't pass proper locations here
  432. so always take a loc_reference, since that's what it uses (JM)
  433. LOC_REFERENCE:
  434. }
  435. else
  436. if target_info.endian=endian_big then
  437. inc(tmploclo.reference.offset,4)
  438. else
  439. inc(tmplochi.reference.offset,4);
  440. {
  441. else
  442. internalerror(2003042702);
  443. }
  444. end;
  445. cg.a_param_reg(list,OS_32,reg.reghi,tmplochi);
  446. cg.a_param_reg(list,OS_32,reg.reglo,tmploclo);
  447. end;
  448. procedure tcg64f32.a_param64_const(list : taasmoutput;value : qword;const locpara : tparalocation);
  449. var
  450. tmplochi,tmploclo: tparalocation;
  451. begin
  452. tmplochi:=locpara;
  453. tmploclo:=locpara;
  454. if locpara.size=OS_S64 then
  455. tmplochi.size:=OS_S32
  456. else
  457. tmplochi.size:=OS_32;
  458. tmploclo.size:=OS_32;
  459. case locpara.loc of
  460. LOC_REGISTER:
  461. tmplochi.register:=tmplochi.registerhigh;
  462. { !!! i386 doesn't pass proper locations here
  463. so always take a loc_reference, since that's what it uses (JM)
  464. LOC_REFERENCE:
  465. }
  466. else
  467. if target_info.endian=endian_big then
  468. inc(tmploclo.reference.offset,4)
  469. else
  470. inc(tmplochi.reference.offset,4);
  471. {
  472. else
  473. internalerror(2003042702);
  474. }
  475. end;
  476. cg.a_param_const(list,OS_32,hi(value),tmplochi);
  477. cg.a_param_const(list,OS_32,lo(value),tmploclo);
  478. end;
  479. procedure tcg64f32.a_param64_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  480. var
  481. tmprefhi,tmpreflo : treference;
  482. tmploclo,tmplochi : tparalocation;
  483. begin
  484. tmprefhi:=r;
  485. tmpreflo:=r;
  486. tmplochi:=locpara;
  487. tmploclo:=locpara;
  488. if locpara.size=OS_S64 then
  489. tmplochi.size:=OS_S32
  490. else
  491. tmplochi.size:=OS_32;
  492. tmploclo.size:=OS_32;
  493. case locpara.loc of
  494. LOC_REGISTER:
  495. begin
  496. if target_info.endian=endian_big then
  497. inc(tmpreflo.offset,4)
  498. else
  499. inc(tmprefhi.offset,4);
  500. tmplochi.register:=tmplochi.registerhigh;
  501. end;
  502. { !!! i386 doesn't pass proper locations here
  503. so always take a loc_reference, since that's what it uses (JM)
  504. LOC_REFERENCE:
  505. }
  506. else
  507. begin
  508. if target_info.endian=endian_big then
  509. begin
  510. inc(tmpreflo.offset,4);
  511. inc(tmploclo.reference.offset,4);
  512. end
  513. else
  514. begin
  515. inc(tmprefhi.offset,4);
  516. inc(tmplochi.reference.offset,4);
  517. end;
  518. end
  519. {
  520. else
  521. internalerror(2003042701);
  522. }
  523. end;
  524. cg.a_param_ref(list,OS_32,tmprefhi,tmplochi);
  525. cg.a_param_ref(list,OS_32,tmpreflo,tmploclo);
  526. end;
  527. procedure tcg64f32.a_param64_loc(list : taasmoutput;const l:tlocation;const locpara : tparalocation);
  528. begin
  529. case l.loc of
  530. LOC_REGISTER,
  531. LOC_CREGISTER :
  532. a_param64_reg(list,l.register64,locpara);
  533. LOC_CONSTANT :
  534. a_param64_const(list,l.valueqword,locpara);
  535. LOC_CREFERENCE,
  536. LOC_REFERENCE :
  537. a_param64_ref(list,l.reference,locpara);
  538. else
  539. internalerror(200203287);
  540. end;
  541. end;
  542. procedure tcg64f32.g_rangecheck64(list : taasmoutput;const p : tnode;const todef : tdef);
  543. var
  544. neglabel,
  545. poslabel,
  546. endlabel: tasmlabel;
  547. hreg : tregister;
  548. hdef : torddef;
  549. fromdef : tdef;
  550. opsize : tcgsize;
  551. oldregisterdef: boolean;
  552. from_signed,to_signed: boolean;
  553. got_scratch: boolean;
  554. begin
  555. fromdef:=p.resulttype.def;
  556. from_signed := is_signed(fromdef);
  557. to_signed := is_signed(todef);
  558. if not is_64bit(todef) then
  559. begin
  560. oldregisterdef := registerdef;
  561. registerdef := false;
  562. { get the high dword in a register }
  563. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  564. begin
  565. hreg := p.location.registerhigh;
  566. got_scratch := false
  567. end
  568. else
  569. begin
  570. {$ifdef newra}
  571. hreg:=rg.getregisterint(list,OS_INT);
  572. {$else}
  573. hreg := cg.get_scratch_reg_int(list,OS_INT);
  574. {$endif}
  575. got_scratch := true;
  576. a_load64high_ref_reg(list,p.location.reference,hreg);
  577. end;
  578. objectlibrary.getlabel(poslabel);
  579. { check high dword, must be 0 (for positive numbers) }
  580. cg.a_cmp_const_reg_label(list,OS_32,OC_EQ,0,hreg,poslabel);
  581. { It can also be $ffffffff, but only for negative numbers }
  582. if from_signed and to_signed then
  583. begin
  584. objectlibrary.getlabel(neglabel);
  585. cg.a_cmp_const_reg_label(list,OS_32,OC_EQ,aword(-1),hreg,neglabel);
  586. end;
  587. { !!! freeing of register should happen directly after compare! (JM) }
  588. {$ifdef newra}
  589. if got_scratch then
  590. rg.ungetregisterint(list,hreg);
  591. {$else}
  592. if got_scratch then
  593. cg.free_scratch_reg(list,hreg);
  594. {$endif}
  595. { For all other values we have a range check error }
  596. cg.a_call_name(list,'FPC_RANGEERROR');
  597. { if the high dword = 0, the low dword can be considered a }
  598. { simple cardinal }
  599. cg.a_label(list,poslabel);
  600. hdef:=torddef.create(u32bit,0,cardinal($ffffffff));
  601. { the real p.resulttype.def is already saved in fromdef }
  602. p.resulttype.def := hdef;
  603. { no use in calling just "g_rangecheck" since that one will }
  604. { simply call the inherited method too (JM) }
  605. cg.g_rangecheck(list,p,todef);
  606. hdef.free;
  607. { restore original resulttype.def }
  608. p.resulttype.def := fromdef;
  609. if from_signed and to_signed then
  610. begin
  611. objectlibrary.getlabel(endlabel);
  612. cg.a_jmp_always(list,endlabel);
  613. { if the high dword = $ffffffff, then the low dword (when }
  614. { considered as a longint) must be < 0 }
  615. cg.a_label(list,neglabel);
  616. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  617. begin
  618. hreg := p.location.registerlow;
  619. got_scratch := false
  620. end
  621. else
  622. begin
  623. {$ifdef newra}
  624. hreg:=rg.getregisterint(list,OS_INT);
  625. {$else}
  626. hreg := cg.get_scratch_reg_int(list,OS_INT);
  627. {$endif}
  628. got_scratch := true;
  629. a_load64low_ref_reg(list,p.location.reference,hreg);
  630. end;
  631. { get a new neglabel (JM) }
  632. objectlibrary.getlabel(neglabel);
  633. cg.a_cmp_const_reg_label(list,OS_32,OC_LT,0,hreg,neglabel);
  634. { !!! freeing of register should happen directly after compare! (JM) }
  635. {$ifdef newra}
  636. if got_scratch then
  637. rg.ungetregisterint(list,hreg);
  638. {$else}
  639. if got_scratch then
  640. cg.free_scratch_reg(list,hreg);
  641. {$endif}
  642. cg.a_call_name(list,'FPC_RANGEERROR');
  643. { if we get here, the 64bit value lies between }
  644. { longint($80000000) and -1 (JM) }
  645. cg.a_label(list,neglabel);
  646. hdef:=torddef.create(s32bit,longint($80000000),-1);
  647. p.resulttype.def := hdef;
  648. cg.g_rangecheck(list,p,todef);
  649. hdef.free;
  650. cg.a_label(list,endlabel);
  651. end;
  652. registerdef := oldregisterdef;
  653. p.resulttype.def := fromdef;
  654. { restore p's resulttype.def }
  655. end
  656. else
  657. { todef = 64bit int }
  658. { no 64bit subranges supported, so only a small check is necessary }
  659. { if both are signed or both are unsigned, no problem! }
  660. if (from_signed xor to_signed) and
  661. { also not if the fromdef is unsigned and < 64bit, since that will }
  662. { always fit in a 64bit int (todef is 64bit) }
  663. (from_signed or
  664. (torddef(fromdef).typ = u64bit)) then
  665. begin
  666. { in all cases, there is only a problem if the higest bit is set }
  667. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  668. begin
  669. if is_64bit(fromdef) then
  670. begin
  671. hreg := p.location.registerhigh;
  672. opsize := OS_32;
  673. end
  674. else
  675. begin
  676. hreg := p.location.register;
  677. opsize := def_cgsize(p.resulttype.def);
  678. end;
  679. got_scratch := false;
  680. end
  681. else
  682. begin
  683. {$ifdef newra}
  684. hreg:=rg.getregisterint(list,OS_INT);
  685. {$else}
  686. hreg := cg.get_scratch_reg_int(list,OS_INT);
  687. {$endif}
  688. got_scratch := true;
  689. opsize := def_cgsize(p.resulttype.def);
  690. if opsize in [OS_64,OS_S64] then
  691. a_load64high_ref_reg(list,p.location.reference,hreg)
  692. else
  693. cg.a_load_ref_reg(list,opsize,p.location.reference,hreg);
  694. end;
  695. objectlibrary.getlabel(poslabel);
  696. cg.a_cmp_const_reg_label(list,opsize,OC_GTE,0,hreg,poslabel);
  697. { !!! freeing of register should happen directly after compare! (JM) }
  698. {$ifdef newra}
  699. if got_scratch then
  700. rg.ungetregisterint(list,hreg);
  701. {$else}
  702. if got_scratch then
  703. cg.free_scratch_reg(list,hreg);
  704. {$endif}
  705. cg.a_call_name(list,'FPC_RANGEERROR');
  706. cg.a_label(list,poslabel);
  707. end;
  708. end;
  709. function tcg64f32.optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : qword; var reg: tregister64): boolean;
  710. var
  711. lowvalue, highvalue : cardinal;
  712. hreg: tregister;
  713. begin
  714. lowvalue := cardinal(a);
  715. highvalue:= a shr 32;
  716. { assume it will be optimized out }
  717. optimize64_op_const_reg := true;
  718. case op of
  719. OP_ADD:
  720. begin
  721. if a = 0 then
  722. exit;
  723. end;
  724. OP_AND:
  725. begin
  726. if lowvalue <> high(cardinal) then
  727. cg.a_op_const_reg(list,op,OS_32,lowvalue,reg.reglo);
  728. if highvalue <> high(cardinal) then
  729. cg.a_op_const_reg(list,op,OS_32,highvalue,reg.reghi);
  730. { already emitted correctly }
  731. exit;
  732. end;
  733. OP_OR:
  734. begin
  735. if lowvalue <> 0 then
  736. cg.a_op_const_reg(list,op,OS_32,lowvalue,reg.reglo);
  737. if highvalue <> 0 then
  738. cg.a_op_const_reg(list,op,OS_32,highvalue,reg.reghi);
  739. { already emitted correctly }
  740. exit;
  741. end;
  742. OP_SUB:
  743. begin
  744. if a = 0 then
  745. exit;
  746. end;
  747. OP_XOR:
  748. begin
  749. end;
  750. OP_SHL:
  751. begin
  752. if a = 0 then
  753. exit;
  754. { simply clear low-register
  755. and shift the rest and swap
  756. registers.
  757. }
  758. if (a > 31) then
  759. begin
  760. cg.a_load_const_reg(list,OS_32,0,reg.reglo);
  761. cg.a_op_const_reg(list,OP_SHL,OS_32,a mod 32,reg.reghi);
  762. { swap the registers }
  763. hreg := reg.reghi;
  764. reg.reghi := reg.reglo;
  765. reg.reglo := hreg;
  766. exit;
  767. end;
  768. end;
  769. OP_SHR:
  770. begin
  771. if a = 0 then exit;
  772. { simply clear high-register
  773. and shift the rest and swap
  774. registers.
  775. }
  776. if (a > 31) then
  777. begin
  778. cg.a_load_const_reg(list,OS_32,0,reg.reghi);
  779. cg.a_op_const_reg(list,OP_SHL,OS_32,a mod 32,reg.reglo);
  780. { swap the registers }
  781. hreg := reg.reghi;
  782. reg.reghi := reg.reglo;
  783. reg.reglo := hreg;
  784. exit;
  785. end;
  786. end;
  787. OP_IMUL,OP_MUL:
  788. begin
  789. if a = 1 then exit;
  790. end;
  791. OP_IDIV,OP_DIV:
  792. begin
  793. if a = 1 then exit;
  794. end;
  795. else
  796. internalerror(20020817);
  797. end;
  798. optimize64_op_const_reg := false;
  799. end;
  800. (*
  801. procedure int64f32_assignment_int64_reg(p : passignmentnode);
  802. begin
  803. end;
  804. begin
  805. p2_assignment:=@int64f32_assignement_int64;
  806. *)
  807. end.
  808. {
  809. $Log$
  810. Revision 1.45 2003-06-01 21:38:06 peter
  811. * getregisterfpu size parameter added
  812. * op_const_reg size parameter added
  813. * sparc updates
  814. Revision 1.44 2003/05/14 19:31:37 jonas
  815. * fixed a_param64_reg
  816. Revision 1.43 2003/04/27 14:48:09 jonas
  817. * fixed Florian's quick hack :)
  818. * fixed small bug 64bit range checking code
  819. Revision 1.42 2003/04/27 09:10:49 florian
  820. * quick fix for param64 for intel
  821. Revision 1.41 2003/04/27 08:23:51 florian
  822. * fixed parameter passing for 64 bit ints
  823. Revision 1.40 2003/04/23 20:16:03 peter
  824. + added currency support based on int64
  825. + is_64bit for use in cg units instead of is_64bitint
  826. * removed cgmessage from n386add, replace with internalerrors
  827. Revision 1.39 2003/04/22 10:09:34 daniel
  828. + Implemented the actual register allocator
  829. + Scratch registers unavailable when new register allocator used
  830. + maybe_save/maybe_restore unavailable when new register allocator used
  831. Revision 1.38 2003/04/07 08:52:58 jonas
  832. * fixed compiling error
  833. Revision 1.37 2003/04/07 08:45:09 jonas
  834. + generic a_op64_reg_ref implementation
  835. Revision 1.36 2003/03/28 19:16:56 peter
  836. * generic constructor working for i386
  837. * remove fixed self register
  838. * esi added as address register for i386
  839. Revision 1.35 2003/02/19 22:00:14 daniel
  840. * Code generator converted to new register notation
  841. - Horribily outdated todo.txt removed
  842. Revision 1.34 2003/01/08 18:43:56 daniel
  843. * Tregister changed into a record
  844. Revision 1.33 2003/01/05 13:36:53 florian
  845. * x86-64 compiles
  846. + very basic support for float128 type (x86-64 only)
  847. Revision 1.32 2002/11/25 17:43:16 peter
  848. * splitted defbase in defutil,symutil,defcmp
  849. * merged isconvertable and is_equal into compare_defs(_ext)
  850. * made operator search faster by walking the list only once
  851. Revision 1.31 2002/10/05 12:43:23 carl
  852. * fixes for Delphi 6 compilation
  853. (warning : Some features do not work under Delphi)
  854. Revision 1.30 2002/09/17 18:54:01 jonas
  855. * a_load_reg_reg() now has two size parameters: source and dest. This
  856. allows some optimizations on architectures that don't encode the
  857. register size in the register name.
  858. Revision 1.29 2002/09/10 21:24:38 jonas
  859. * fixed a_param64_ref
  860. Revision 1.28 2002/09/07 15:25:00 peter
  861. * old logs removed and tabs fixed
  862. Revision 1.27 2002/08/19 18:17:47 carl
  863. + optimize64_op_const_reg implemented (optimizes 64-bit constant opcodes)
  864. * more fixes to m68k for 64-bit operations
  865. Revision 1.26 2002/08/17 22:09:43 florian
  866. * result type handling in tcgcal.pass_2 overhauled
  867. * better tnode.dowrite
  868. * some ppc stuff fixed
  869. Revision 1.25 2002/08/14 18:41:47 jonas
  870. - remove valuelow/valuehigh fields from tlocation, because they depend
  871. on the endianess of the host operating system -> difficult to get
  872. right. Use lo/hi(location.valueqword) instead (remember to use
  873. valueqword and not value!!)
  874. Revision 1.24 2002/08/11 14:32:26 peter
  875. * renamed current_library to objectlibrary
  876. Revision 1.23 2002/08/11 13:24:11 peter
  877. * saving of asmsymbols in ppu supported
  878. * asmsymbollist global is removed and moved into a new class
  879. tasmlibrarydata that will hold the info of a .a file which
  880. corresponds with a single module. Added librarydata to tmodule
  881. to keep the library info stored for the module. In the future the
  882. objectfiles will also be stored to the tasmlibrarydata class
  883. * all getlabel/newasmsymbol and friends are moved to the new class
  884. Revision 1.22 2002/07/28 15:57:15 jonas
  885. * fixed a_load64_const_reg() for big endian systems
  886. Revision 1.21 2002/07/20 11:57:52 florian
  887. * types.pas renamed to defbase.pas because D6 contains a types
  888. unit so this would conflicts if D6 programms are compiled
  889. + Willamette/SSE2 instructions to assembler added
  890. Revision 1.20 2002/07/12 10:14:26 jonas
  891. * some big-endian fixes
  892. Revision 1.19 2002/07/11 07:23:17 jonas
  893. + generic implementations of a_op64_ref_reg() and a_op64_const_ref()
  894. (only works for processors with >2 scratch registers)
  895. Revision 1.18 2002/07/10 11:12:44 jonas
  896. * fixed a_op64_const_loc()
  897. Revision 1.17 2002/07/07 09:52:32 florian
  898. * powerpc target fixed, very simple units can be compiled
  899. * some basic stuff for better callparanode handling, far from being finished
  900. Revision 1.16 2002/07/01 18:46:21 peter
  901. * internal linker
  902. * reorganized aasm layer
  903. Revision 1.15 2002/07/01 16:23:52 peter
  904. * cg64 patch
  905. * basics for currency
  906. * asnode updates for class and interface (not finished)
  907. Revision 1.14 2002/05/20 13:30:40 carl
  908. * bugfix of hdisponen (base must be set, not index)
  909. * more portability fixes
  910. Revision 1.13 2002/05/18 13:34:05 peter
  911. * readded missing revisions
  912. Revision 1.12 2002/05/16 19:46:35 carl
  913. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  914. + try to fix temp allocation (still in ifdef)
  915. + generic constructor calls
  916. + start of tassembler / tmodulebase class cleanup
  917. Revision 1.10 2002/05/12 16:53:04 peter
  918. * moved entry and exitcode to ncgutil and cgobj
  919. * foreach gets extra argument for passing local data to the
  920. iterator function
  921. * -CR checks also class typecasts at runtime by changing them
  922. into as
  923. * fixed compiler to cycle with the -CR option
  924. * fixed stabs with elf writer, finally the global variables can
  925. be watched
  926. * removed a lot of routines from cga unit and replaced them by
  927. calls to cgobj
  928. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  929. u32bit then the other is typecasted also to u32bit without giving
  930. a rangecheck warning/error.
  931. * fixed pascal calling method with reversing also the high tree in
  932. the parast, detected by tcalcst3 test
  933. Revision 1.9 2002/04/25 20:16:38 peter
  934. * moved more routines from cga/n386util
  935. Revision 1.8 2002/04/21 15:28:51 carl
  936. * a_jmp_cond -> a_jmp_always
  937. Revision 1.7 2002/04/07 13:21:18 carl
  938. + more documentation
  939. }