cg64f32.pas 36 KB

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