cg64f32.pas 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. begin
  419. {$ifdef FPC}
  420. {$warning FIX ME}
  421. {$endif}
  422. cg.a_param_reg(list,OS_32,reg.reghi,locpara);
  423. { the nr+1 needs definitivly a fix FK }
  424. { maybe the parameter numbering needs }
  425. { to take care of this on 32 Bit }
  426. { systems FK }
  427. cg.a_param_reg(list,OS_32,reg.reglo,locpara);
  428. end;
  429. procedure tcg64f32.a_param64_const(list : taasmoutput;value : qword;const locpara : tparalocation);
  430. var
  431. tmplochi,tmploclo: tparalocation;
  432. begin
  433. tmplochi:=locpara;
  434. tmploclo:=locpara;
  435. if locpara.size=OS_S64 then
  436. tmplochi.size:=OS_S32
  437. else
  438. tmplochi.size:=OS_32;
  439. tmploclo.size:=OS_32;
  440. case locpara.loc of
  441. LOC_REGISTER:
  442. tmplochi.register:=tmplochi.registerhigh;
  443. LOC_REFERENCE:
  444. if target_info.endian=endian_big then
  445. inc(tmploclo.reference.offset,4)
  446. else
  447. inc(tmplochi.reference.offset,4);
  448. else
  449. internalerror(2003042702);
  450. end;
  451. cg.a_param_const(list,OS_32,lo(value),tmploclo);
  452. cg.a_param_const(list,OS_32,hi(value),tmplochi);
  453. end;
  454. procedure tcg64f32.a_param64_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);
  455. var
  456. tmprefhi,tmpreflo : treference;
  457. tmploclo,tmplochi : tparalocation;
  458. begin
  459. tmprefhi:=r;
  460. tmpreflo:=r;
  461. tmplochi:=locpara;
  462. tmploclo:=locpara;
  463. if locpara.size=OS_S64 then
  464. tmplochi.size:=OS_S32
  465. else
  466. tmplochi.size:=OS_32;
  467. tmploclo.size:=OS_32;
  468. case locpara.loc of
  469. LOC_REGISTER:
  470. begin
  471. if target_info.endian=endian_big then
  472. inc(tmpreflo.offset,4)
  473. else
  474. inc(tmprefhi.offset,4);
  475. tmplochi.register:=tmplochi.registerhigh;
  476. end;
  477. LOC_REFERENCE:
  478. begin
  479. inc(tmprefhi.offset,4);
  480. inc(tmplochi.reference.offset,4);
  481. end
  482. else
  483. internalerror(2003042701);
  484. end;
  485. cg.a_param_ref(list,OS_32,tmpreflo,tmploclo);
  486. cg.a_param_ref(list,OS_32,tmprefhi,tmplochi);
  487. end;
  488. procedure tcg64f32.a_param64_loc(list : taasmoutput;const l:tlocation;const locpara : tparalocation);
  489. begin
  490. {$ifdef fpc}
  491. {$warning FIX ME}
  492. {$endif}
  493. case l.loc of
  494. LOC_REGISTER,
  495. LOC_CREGISTER :
  496. a_param64_reg(list,l.register64,locpara);
  497. LOC_CONSTANT :
  498. a_param64_const(list,l.valueqword,locpara);
  499. LOC_CREFERENCE,
  500. LOC_REFERENCE :
  501. a_param64_ref(list,l.reference,locpara);
  502. else
  503. internalerror(200203287);
  504. end;
  505. end;
  506. procedure tcg64f32.g_rangecheck64(list : taasmoutput;const p : tnode;const todef : tdef);
  507. var
  508. neglabel,
  509. poslabel,
  510. endlabel: tasmlabel;
  511. hreg : tregister;
  512. hdef : torddef;
  513. fromdef : tdef;
  514. opsize : tcgsize;
  515. oldregisterdef: boolean;
  516. from_signed,to_signed: boolean;
  517. got_scratch: boolean;
  518. begin
  519. fromdef:=p.resulttype.def;
  520. from_signed := is_signed(fromdef);
  521. to_signed := is_signed(todef);
  522. if not is_64bit(todef) then
  523. begin
  524. oldregisterdef := registerdef;
  525. registerdef := false;
  526. { get the high dword in a register }
  527. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  528. begin
  529. hreg := p.location.registerhigh;
  530. got_scratch := false
  531. end
  532. else
  533. begin
  534. {$ifdef newra}
  535. hreg:=rg.getregisterint(list,OS_INT);
  536. {$else}
  537. hreg := cg.get_scratch_reg_int(list,OS_INT);
  538. {$endif}
  539. got_scratch := true;
  540. a_load64high_ref_reg(list,p.location.reference,hreg);
  541. end;
  542. objectlibrary.getlabel(poslabel);
  543. { check high dword, must be 0 (for positive numbers) }
  544. cg.a_cmp_const_reg_label(list,OS_32,OC_EQ,0,hreg,poslabel);
  545. { It can also be $ffffffff, but only for negative numbers }
  546. if from_signed and to_signed then
  547. begin
  548. objectlibrary.getlabel(neglabel);
  549. cg.a_cmp_const_reg_label(list,OS_32,OC_EQ,aword(-1),hreg,neglabel);
  550. end;
  551. { !!! freeing of register should happen directly after compare! (JM) }
  552. {$ifdef newra}
  553. if got_scratch then
  554. rg.ungetregisterint(list,hreg);
  555. {$else}
  556. if got_scratch then
  557. cg.free_scratch_reg(list,hreg);
  558. {$endif}
  559. { For all other values we have a range check error }
  560. cg.a_call_name(list,'FPC_RANGEERROR');
  561. { if the high dword = 0, the low dword can be considered a }
  562. { simple cardinal }
  563. cg.a_label(list,poslabel);
  564. hdef:=torddef.create(u32bit,0,cardinal($ffffffff));
  565. { the real p.resulttype.def is already saved in fromdef }
  566. p.resulttype.def := hdef;
  567. { no use in calling just "g_rangecheck" since that one will }
  568. { simply call the inherited method too (JM) }
  569. cg.g_rangecheck(list,p,todef);
  570. hdef.free;
  571. { restore original resulttype.def }
  572. p.resulttype.def := todef;
  573. if from_signed and to_signed then
  574. begin
  575. objectlibrary.getlabel(endlabel);
  576. cg.a_jmp_always(list,endlabel);
  577. { if the high dword = $ffffffff, then the low dword (when }
  578. { considered as a longint) must be < 0 }
  579. cg.a_label(list,neglabel);
  580. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  581. begin
  582. hreg := p.location.registerlow;
  583. got_scratch := false
  584. end
  585. else
  586. begin
  587. {$ifdef newra}
  588. hreg:=rg.getregisterint(list,OS_INT);
  589. {$else}
  590. hreg := cg.get_scratch_reg_int(list,OS_INT);
  591. {$endif}
  592. got_scratch := true;
  593. a_load64low_ref_reg(list,p.location.reference,hreg);
  594. end;
  595. { get a new neglabel (JM) }
  596. objectlibrary.getlabel(neglabel);
  597. cg.a_cmp_const_reg_label(list,OS_32,OC_LT,0,hreg,neglabel);
  598. { !!! freeing of register should happen directly after compare! (JM) }
  599. {$ifdef newra}
  600. if got_scratch then
  601. rg.ungetregisterint(list,hreg);
  602. {$else}
  603. if got_scratch then
  604. cg.free_scratch_reg(list,hreg);
  605. {$endif}
  606. cg.a_call_name(list,'FPC_RANGEERROR');
  607. { if we get here, the 64bit value lies between }
  608. { longint($80000000) and -1 (JM) }
  609. cg.a_label(list,neglabel);
  610. hdef:=torddef.create(s32bit,longint($80000000),-1);
  611. p.resulttype.def := hdef;
  612. cg.g_rangecheck(list,p,todef);
  613. hdef.free;
  614. cg.a_label(list,endlabel);
  615. end;
  616. registerdef := oldregisterdef;
  617. p.resulttype.def := fromdef;
  618. { restore p's resulttype.def }
  619. end
  620. else
  621. { todef = 64bit int }
  622. { no 64bit subranges supported, so only a small check is necessary }
  623. { if both are signed or both are unsigned, no problem! }
  624. if (from_signed xor to_signed) and
  625. { also not if the fromdef is unsigned and < 64bit, since that will }
  626. { always fit in a 64bit int (todef is 64bit) }
  627. (from_signed or
  628. (torddef(fromdef).typ = u64bit)) then
  629. begin
  630. { in all cases, there is only a problem if the higest bit is set }
  631. if p.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  632. begin
  633. if is_64bit(fromdef) then
  634. begin
  635. hreg := p.location.registerhigh;
  636. opsize := OS_32;
  637. end
  638. else
  639. begin
  640. hreg := p.location.register;
  641. opsize := def_cgsize(p.resulttype.def);
  642. end;
  643. got_scratch := false;
  644. end
  645. else
  646. begin
  647. {$ifdef newra}
  648. hreg:=rg.getregisterint(list,OS_INT);
  649. {$else}
  650. hreg := cg.get_scratch_reg_int(list,OS_INT);
  651. {$endif}
  652. got_scratch := true;
  653. opsize := def_cgsize(p.resulttype.def);
  654. if opsize in [OS_64,OS_S64] then
  655. a_load64high_ref_reg(list,p.location.reference,hreg)
  656. else
  657. cg.a_load_ref_reg(list,opsize,p.location.reference,hreg);
  658. end;
  659. objectlibrary.getlabel(poslabel);
  660. cg.a_cmp_const_reg_label(list,opsize,OC_GTE,0,hreg,poslabel);
  661. { !!! freeing of register should happen directly after compare! (JM) }
  662. {$ifdef newra}
  663. if got_scratch then
  664. rg.ungetregisterint(list,hreg);
  665. {$else}
  666. if got_scratch then
  667. cg.free_scratch_reg(list,hreg);
  668. {$endif}
  669. cg.a_call_name(list,'FPC_RANGEERROR');
  670. cg.a_label(list,poslabel);
  671. end;
  672. end;
  673. function tcg64f32.optimize64_op_const_reg(list: taasmoutput; var op: topcg; var a : qword; var reg: tregister64): boolean;
  674. var
  675. lowvalue, highvalue : cardinal;
  676. hreg: tregister;
  677. begin
  678. lowvalue := cardinal(a);
  679. highvalue:= a shr 32;
  680. { assume it will be optimized out }
  681. optimize64_op_const_reg := true;
  682. case op of
  683. OP_ADD:
  684. begin
  685. if a = 0 then
  686. exit;
  687. end;
  688. OP_AND:
  689. begin
  690. if lowvalue <> high(cardinal) then
  691. cg.a_op_const_reg(list,op,lowvalue,reg.reglo);
  692. if highvalue <> high(cardinal) then
  693. cg.a_op_const_reg(list,op,highvalue,reg.reghi);
  694. { already emitted correctly }
  695. exit;
  696. end;
  697. OP_OR:
  698. begin
  699. if lowvalue <> 0 then
  700. cg.a_op_const_reg(list,op,lowvalue,reg.reglo);
  701. if highvalue <> 0 then
  702. cg.a_op_const_reg(list,op,highvalue,reg.reghi);
  703. { already emitted correctly }
  704. exit;
  705. end;
  706. OP_SUB:
  707. begin
  708. if a = 0 then
  709. exit;
  710. end;
  711. OP_XOR:
  712. begin
  713. end;
  714. OP_SHL:
  715. begin
  716. if a = 0 then
  717. exit;
  718. { simply clear low-register
  719. and shift the rest and swap
  720. registers.
  721. }
  722. if (a > 31) then
  723. begin
  724. cg.a_load_const_reg(list,OS_32,0,reg.reglo);
  725. cg.a_op_const_reg(list,OP_SHL,a mod 32,reg.reghi);
  726. { swap the registers }
  727. hreg := reg.reghi;
  728. reg.reghi := reg.reglo;
  729. reg.reglo := hreg;
  730. exit;
  731. end;
  732. end;
  733. OP_SHR:
  734. begin
  735. if a = 0 then exit;
  736. { simply clear high-register
  737. and shift the rest and swap
  738. registers.
  739. }
  740. if (a > 31) then
  741. begin
  742. cg.a_load_const_reg(list,OS_32,0,reg.reghi);
  743. cg.a_op_const_reg(list,OP_SHL,a mod 32,reg.reglo);
  744. { swap the registers }
  745. hreg := reg.reghi;
  746. reg.reghi := reg.reglo;
  747. reg.reglo := hreg;
  748. exit;
  749. end;
  750. end;
  751. OP_IMUL,OP_MUL:
  752. begin
  753. if a = 1 then exit;
  754. end;
  755. OP_IDIV,OP_DIV:
  756. begin
  757. if a = 1 then exit;
  758. end;
  759. else
  760. internalerror(20020817);
  761. end;
  762. optimize64_op_const_reg := false;
  763. end;
  764. (*
  765. procedure int64f32_assignment_int64_reg(p : passignmentnode);
  766. begin
  767. end;
  768. begin
  769. p2_assignment:=@int64f32_assignement_int64;
  770. *)
  771. end.
  772. {
  773. $Log$
  774. Revision 1.41 2003-04-27 08:23:51 florian
  775. * fixed parameter passing for 64 bit ints
  776. Revision 1.40 2003/04/23 20:16:03 peter
  777. + added currency support based on int64
  778. + is_64bit for use in cg units instead of is_64bitint
  779. * removed cgmessage from n386add, replace with internalerrors
  780. Revision 1.39 2003/04/22 10:09:34 daniel
  781. + Implemented the actual register allocator
  782. + Scratch registers unavailable when new register allocator used
  783. + maybe_save/maybe_restore unavailable when new register allocator used
  784. Revision 1.38 2003/04/07 08:52:58 jonas
  785. * fixed compiling error
  786. Revision 1.37 2003/04/07 08:45:09 jonas
  787. + generic a_op64_reg_ref implementation
  788. Revision 1.36 2003/03/28 19:16:56 peter
  789. * generic constructor working for i386
  790. * remove fixed self register
  791. * esi added as address register for i386
  792. Revision 1.35 2003/02/19 22:00:14 daniel
  793. * Code generator converted to new register notation
  794. - Horribily outdated todo.txt removed
  795. Revision 1.34 2003/01/08 18:43:56 daniel
  796. * Tregister changed into a record
  797. Revision 1.33 2003/01/05 13:36:53 florian
  798. * x86-64 compiles
  799. + very basic support for float128 type (x86-64 only)
  800. Revision 1.32 2002/11/25 17:43:16 peter
  801. * splitted defbase in defutil,symutil,defcmp
  802. * merged isconvertable and is_equal into compare_defs(_ext)
  803. * made operator search faster by walking the list only once
  804. Revision 1.31 2002/10/05 12:43:23 carl
  805. * fixes for Delphi 6 compilation
  806. (warning : Some features do not work under Delphi)
  807. Revision 1.30 2002/09/17 18:54:01 jonas
  808. * a_load_reg_reg() now has two size parameters: source and dest. This
  809. allows some optimizations on architectures that don't encode the
  810. register size in the register name.
  811. Revision 1.29 2002/09/10 21:24:38 jonas
  812. * fixed a_param64_ref
  813. Revision 1.28 2002/09/07 15:25:00 peter
  814. * old logs removed and tabs fixed
  815. Revision 1.27 2002/08/19 18:17:47 carl
  816. + optimize64_op_const_reg implemented (optimizes 64-bit constant opcodes)
  817. * more fixes to m68k for 64-bit operations
  818. Revision 1.26 2002/08/17 22:09:43 florian
  819. * result type handling in tcgcal.pass_2 overhauled
  820. * better tnode.dowrite
  821. * some ppc stuff fixed
  822. Revision 1.25 2002/08/14 18:41:47 jonas
  823. - remove valuelow/valuehigh fields from tlocation, because they depend
  824. on the endianess of the host operating system -> difficult to get
  825. right. Use lo/hi(location.valueqword) instead (remember to use
  826. valueqword and not value!!)
  827. Revision 1.24 2002/08/11 14:32:26 peter
  828. * renamed current_library to objectlibrary
  829. Revision 1.23 2002/08/11 13:24:11 peter
  830. * saving of asmsymbols in ppu supported
  831. * asmsymbollist global is removed and moved into a new class
  832. tasmlibrarydata that will hold the info of a .a file which
  833. corresponds with a single module. Added librarydata to tmodule
  834. to keep the library info stored for the module. In the future the
  835. objectfiles will also be stored to the tasmlibrarydata class
  836. * all getlabel/newasmsymbol and friends are moved to the new class
  837. Revision 1.22 2002/07/28 15:57:15 jonas
  838. * fixed a_load64_const_reg() for big endian systems
  839. Revision 1.21 2002/07/20 11:57:52 florian
  840. * types.pas renamed to defbase.pas because D6 contains a types
  841. unit so this would conflicts if D6 programms are compiled
  842. + Willamette/SSE2 instructions to assembler added
  843. Revision 1.20 2002/07/12 10:14:26 jonas
  844. * some big-endian fixes
  845. Revision 1.19 2002/07/11 07:23:17 jonas
  846. + generic implementations of a_op64_ref_reg() and a_op64_const_ref()
  847. (only works for processors with >2 scratch registers)
  848. Revision 1.18 2002/07/10 11:12:44 jonas
  849. * fixed a_op64_const_loc()
  850. Revision 1.17 2002/07/07 09:52:32 florian
  851. * powerpc target fixed, very simple units can be compiled
  852. * some basic stuff for better callparanode handling, far from being finished
  853. Revision 1.16 2002/07/01 18:46:21 peter
  854. * internal linker
  855. * reorganized aasm layer
  856. Revision 1.15 2002/07/01 16:23:52 peter
  857. * cg64 patch
  858. * basics for currency
  859. * asnode updates for class and interface (not finished)
  860. Revision 1.14 2002/05/20 13:30:40 carl
  861. * bugfix of hdisponen (base must be set, not index)
  862. * more portability fixes
  863. Revision 1.13 2002/05/18 13:34:05 peter
  864. * readded missing revisions
  865. Revision 1.12 2002/05/16 19:46:35 carl
  866. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  867. + try to fix temp allocation (still in ifdef)
  868. + generic constructor calls
  869. + start of tassembler / tmodulebase class cleanup
  870. Revision 1.10 2002/05/12 16:53:04 peter
  871. * moved entry and exitcode to ncgutil and cgobj
  872. * foreach gets extra argument for passing local data to the
  873. iterator function
  874. * -CR checks also class typecasts at runtime by changing them
  875. into as
  876. * fixed compiler to cycle with the -CR option
  877. * fixed stabs with elf writer, finally the global variables can
  878. be watched
  879. * removed a lot of routines from cga unit and replaced them by
  880. calls to cgobj
  881. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  882. u32bit then the other is typecasted also to u32bit without giving
  883. a rangecheck warning/error.
  884. * fixed pascal calling method with reversing also the high tree in
  885. the parast, detected by tcalcst3 test
  886. Revision 1.9 2002/04/25 20:16:38 peter
  887. * moved more routines from cga/n386util
  888. Revision 1.8 2002/04/21 15:28:51 carl
  889. * a_jmp_cond -> a_jmp_always
  890. Revision 1.7 2002/04/07 13:21:18 carl
  891. + more documentation
  892. }