cgsparc.pas 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the SPARC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgsparc;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,parabase,
  22. cgbase,cgutils,cgobj,
  23. {$ifndef SPARC64}
  24. cg64f32,
  25. {$endif SPARC64}
  26. aasmbase,aasmtai,aasmdata,aasmcpu,
  27. cpubase,cpuinfo,
  28. node,symconst,SymType,symdef,
  29. rgcpu;
  30. type
  31. TCGSparcGen=class(tcg)
  32. protected
  33. function IsSimpleRef(const ref:treference):boolean;
  34. public
  35. procedure init_register_allocators;override;
  36. procedure done_register_allocators;override;
  37. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;override;
  38. { sparc special, needed by cg64 }
  39. procedure make_simple_ref(list:TAsmList;var ref: treference);
  40. procedure handle_load_store(list:TAsmList;isstore:boolean;op: tasmop;reg:tregister;ref: treference);
  41. procedure handle_reg_const_reg(list:TAsmList;op:Tasmop;src:tregister;a:tcgint;dst:tregister);
  42. { parameter }
  43. procedure a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const paraloc : TCGPara);override;
  44. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);override;
  45. procedure a_call_name(list:TAsmList;const s:string; weak: boolean);override;
  46. procedure a_call_reg(list:TAsmList;Reg:TRegister);override;
  47. { General purpose instructions }
  48. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  49. procedure a_op_const_reg(list:TAsmList;Op:TOpCG;size:tcgsize;a:tcgint;reg:TRegister);override;
  50. procedure a_op_reg_reg(list:TAsmList;Op:TOpCG;size:TCGSize;src, dst:TRegister);override;
  51. procedure a_op_const_reg_reg(list:TAsmList;op:TOpCg;size:tcgsize;a:tcgint;src, dst:tregister);override;
  52. procedure a_op_reg_reg_reg(list:TAsmList;op:TOpCg;size:tcgsize;src1, src2, dst:tregister);override;
  53. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  54. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  55. { move instructions }
  56. procedure a_load_const_ref(list:TAsmList;size:tcgsize;a:tcgint;const ref:TReference);override;
  57. procedure a_load_reg_ref(list:TAsmList;FromSize,ToSize:TCgSize;reg:TRegister;const ref:TReference);override;
  58. procedure a_load_ref_reg(list:TAsmList;FromSize,ToSize:TCgSize;const ref:TReference;reg:tregister);override;
  59. procedure a_loadaddr_ref_reg(list:TAsmList;const ref:TReference;r:tregister);override;
  60. { fpu move instructions }
  61. procedure a_loadfpu_reg_reg(list:TAsmList;fromsize,tosize:tcgsize;reg1, reg2:tregister);override;
  62. procedure a_loadfpu_ref_reg(list:TAsmList;fromsize,tosize:tcgsize;const ref:TReference;reg:tregister);override;
  63. procedure a_loadfpu_reg_ref(list:TAsmList;fromsize,tosize:tcgsize;reg:tregister;const ref:TReference);override;
  64. { comparison operations }
  65. procedure a_cmp_const_reg_label(list:TAsmList;size:tcgsize;cmp_op:topcmp;a:tcgint;reg:tregister;l:tasmlabel);override;
  66. procedure a_cmp_reg_reg_label(list:TAsmList;size:tcgsize;cmp_op:topcmp;reg1,reg2:tregister;l:tasmlabel);override;
  67. procedure a_jmp_always(List:TAsmList;l:TAsmLabel);override;
  68. procedure a_jmp_name(list : TAsmList;const s : string);override;
  69. procedure a_jmp_cond(list:TAsmList;cond:TOpCmp;l:tasmlabel);{ override;}
  70. procedure a_jmp_flags(list:TAsmList;const f:TResFlags;l:tasmlabel);override;
  71. procedure g_flags2reg(list:TAsmList;Size:TCgSize;const f:tresflags;reg:TRegister);override;
  72. procedure g_overflowCheck(List:TAsmList;const Loc:TLocation;def:TDef);override;
  73. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);override;
  74. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  75. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);override;
  76. procedure g_maybe_got_init(list: TAsmList); override;
  77. procedure g_restore_registers(list:TAsmList);override;
  78. procedure g_save_registers(list : TAsmList);override;
  79. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  80. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);override;
  81. procedure g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  82. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);override;
  83. protected
  84. use_unlimited_pic_mode : boolean;
  85. end;
  86. const
  87. TOpCG2AsmOp : array[boolean,topcg] of TAsmOp=(
  88. (
  89. A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_SMUL,A_UMUL,A_NEG,A_NOT,A_OR,A_SRA,A_SLL,A_SRL,A_SUB,A_XOR,A_NONE,A_NONE
  90. ),
  91. (
  92. A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_SMUL,A_UMUL,A_NEG,A_NOT,A_OR,A_SRAX,A_SLLX,A_SRLX,A_SUB,A_XOR,A_NONE,A_NONE
  93. )
  94. );
  95. TOpCG2AsmOpWithFlags : array[boolean,topcg] of TAsmOp=(
  96. (
  97. A_NONE,A_MOV,A_ADDcc,A_ANDcc,A_UDIVcc,A_SDIVcc,A_SMULcc,A_UMULcc,A_NEG,A_NOT,A_ORcc,A_SRA,A_SLL,A_SRL,A_SUBcc,A_XORcc,A_NONE,A_NONE
  98. ),
  99. (
  100. A_NONE,A_MOV,A_ADDcc,A_ANDcc,A_UDIVcc,A_SDIVcc,A_SMULcc,A_UMULcc,A_NEG,A_NOT,A_ORcc,A_SRAX,A_SLLX,A_SRLX,A_SUBcc,A_XORcc,A_NONE,A_NONE
  101. )
  102. );
  103. TOpCmp2AsmCond : array[topcmp] of TAsmCond=(C_NONE,
  104. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A
  105. );
  106. implementation
  107. uses
  108. globals,verbose,systems,cutils,
  109. paramgr,fmodule,
  110. symtable,symsym,
  111. tgobj,
  112. procinfo,cpupi;
  113. function TCGSparcGen.IsSimpleRef(const ref:treference):boolean;
  114. begin
  115. result :=not(assigned(ref.symbol))and
  116. (((ref.index = NR_NO) and
  117. (ref.offset >= simm13lo) and
  118. (ref.offset <= simm13hi)) or
  119. ((ref.index <> NR_NO) and
  120. (ref.offset = 0)));
  121. end;
  122. procedure TCGSparcGen.make_simple_ref(list:TAsmList;var ref: treference);
  123. var
  124. href: treference;
  125. hreg,hreg2: tregister;
  126. begin
  127. if (ref.refaddr<>addr_no) then
  128. InternalError(2013022802);
  129. if (ref.base=NR_NO) then
  130. begin
  131. ref.base:=ref.index;
  132. ref.index:=NR_NO;
  133. end;
  134. if IsSimpleRef(ref) then
  135. exit;
  136. if (ref.symbol=nil) then
  137. begin
  138. hreg:=getintregister(list,OS_ADDR);
  139. if (ref.index=NR_NO) then
  140. a_load_const_reg(list,OS_ADDR,ref.offset,hreg)
  141. else
  142. begin
  143. if (ref.offset<simm13lo) or (ref.offset>simm13hi-sizeof(pint)) then
  144. begin
  145. a_load_const_reg(list,OS_ADDR,ref.offset,hreg);
  146. list.concat(taicpu.op_reg_reg_reg(A_ADD,hreg,ref.index,hreg));
  147. end
  148. else
  149. list.concat(taicpu.op_reg_const_reg(A_ADD,ref.index,ref.offset,hreg));
  150. end;
  151. if (ref.base=NR_NO) then
  152. ref.base:=hreg
  153. else
  154. ref.index:=hreg;
  155. ref.offset:=0;
  156. exit;
  157. end;
  158. reference_reset_symbol(href,ref.symbol,ref.offset,ref.alignment,ref.volatility);
  159. hreg:=getintregister(list,OS_ADDR);
  160. if not (cs_create_pic in current_settings.moduleswitches) then
  161. begin
  162. { absolute loads allow any offset to be encoded into relocation }
  163. href.refaddr:=addr_high;
  164. list.concat(taicpu.op_ref_reg(A_SETHI,href,hreg));
  165. if (ref.base=NR_NO) and (ref.index=NR_NO) then
  166. begin
  167. ref.base:=hreg;
  168. ref.refaddr:=addr_low;
  169. exit;
  170. end;
  171. { base present -> load the entire address and use it as index }
  172. href.refaddr:=addr_low;
  173. list.concat(taicpu.op_reg_ref_reg(A_OR,hreg,href,hreg));
  174. ref.symbol:=nil;
  175. ref.offset:=0;
  176. if (ref.index<>NR_NO) then
  177. list.concat(taicpu.op_reg_reg_reg(A_ADD,ref.index,hreg,hreg));
  178. ref.index:=hreg;
  179. end
  180. else
  181. begin
  182. include(current_procinfo.flags,pi_needs_got);
  183. href.offset:=0;
  184. if use_unlimited_pic_mode then
  185. begin
  186. href.refaddr:=addr_high;
  187. list.concat(taicpu.op_ref_reg(A_SETHI,href,hreg));
  188. href.refaddr:=addr_low;
  189. list.concat(taicpu.op_reg_ref_reg(A_OR,hreg,href,hreg));
  190. reference_reset_base(href,hreg,0,sizeof(pint),[]);
  191. href.index:=current_procinfo.got;
  192. end
  193. else
  194. begin
  195. href.base:=current_procinfo.got;
  196. href.refaddr:=addr_pic;
  197. end;
  198. list.concat(taicpu.op_ref_reg(A_LD,href,hreg));
  199. ref.symbol:=nil;
  200. { hreg now holds symbol address. Add remaining members. }
  201. if (ref.offset>=simm13lo) and (ref.offset<=simm13hi-sizeof(pint)) then
  202. begin
  203. if (ref.base=NR_NO) then
  204. ref.base:=hreg
  205. else
  206. begin
  207. if (ref.offset<>0) then
  208. list.concat(taicpu.op_reg_const_reg(A_ADD,hreg,ref.offset,hreg));
  209. if (ref.index<>NR_NO) then
  210. list.concat(taicpu.op_reg_reg_reg(A_ADD,hreg,ref.index,hreg));
  211. ref.index:=hreg;
  212. ref.offset:=0;
  213. end;
  214. end
  215. else { large offset, need another register to deal with it }
  216. begin
  217. hreg2:=getintregister(list,OS_ADDR);
  218. a_load_const_reg(list,OS_ADDR,ref.offset,hreg2);
  219. if (ref.index<>NR_NO) then
  220. list.concat(taicpu.op_reg_reg_reg(A_ADD,hreg2,ref.index,hreg2));
  221. if (ref.base<>NR_NO) then
  222. list.concat(taicpu.op_reg_reg_reg(A_ADD,hreg2,ref.base,hreg2));
  223. ref.base:=hreg;
  224. ref.index:=hreg2;
  225. ref.offset:=0;
  226. end;
  227. end;
  228. end;
  229. procedure TCGSparcGen.handle_load_store(list:TAsmList;isstore:boolean;op: tasmop;reg:tregister;ref: treference);
  230. begin
  231. make_simple_ref(list,ref);
  232. if isstore then
  233. list.concat(taicpu.op_reg_ref(op,reg,ref))
  234. else
  235. list.concat(taicpu.op_ref_reg(op,ref,reg));
  236. end;
  237. procedure TCGSparcGen.handle_reg_const_reg(list:TAsmList;op:Tasmop;src:tregister;a:tcgint;dst:tregister);
  238. var
  239. tmpreg : tregister;
  240. begin
  241. if (a<simm13lo) or
  242. (a>simm13hi) then
  243. begin
  244. tmpreg:=GetIntRegister(list,OS_INT);
  245. a_load_const_reg(list,OS_INT,a,tmpreg);
  246. list.concat(taicpu.op_reg_reg_reg(op,src,tmpreg,dst));
  247. end
  248. else
  249. list.concat(taicpu.op_reg_const_reg(op,src,a,dst));
  250. end;
  251. {****************************************************************************
  252. Assembler code
  253. ****************************************************************************}
  254. procedure TCGSparcGen.init_register_allocators;
  255. begin
  256. inherited init_register_allocators;
  257. rg[R_INTREGISTER]:=Trgcpu.create(R_INTREGISTER,R_SUBWHOLE,
  258. [RS_O0,RS_O1,RS_O2,RS_O3,RS_O4,RS_O5,RS_O7,
  259. RS_L0,RS_L1,RS_L2,RS_L3,RS_L4,RS_L5,RS_L6,RS_L7,
  260. RS_I0,RS_I1,RS_I2,RS_I3,RS_I4,RS_I5],
  261. first_int_imreg,[]);
  262. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBFS,
  263. [RS_F0,{RS_F1,}RS_F2,{RS_F3,}RS_F4,{RS_F5,}RS_F6,{RS_F7,}
  264. RS_F8,{RS_F9,}RS_F10,{RS_F11,}RS_F12,{RS_F13,}RS_F14,{RS_F15,}
  265. RS_F16,{RS_F17,}RS_F18,{RS_F19,}RS_F20,{RS_F21,}RS_F22,{RS_F23,}
  266. RS_F24,{RS_F25,}RS_F26,{RS_F27,}RS_F28,{RS_F29,}RS_F30{,RS_F31}],
  267. first_fpu_imreg,[]);
  268. { needs at least one element for rgobj not to crash }
  269. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  270. [RS_L0],first_mm_imreg,[]);
  271. end;
  272. procedure TCGSparcGen.done_register_allocators;
  273. begin
  274. rg[R_INTREGISTER].free;
  275. rg[R_FPUREGISTER].free;
  276. rg[R_MMREGISTER].free;
  277. inherited done_register_allocators;
  278. end;
  279. function TCGSparcGen.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  280. begin
  281. if size=OS_F64 then
  282. result:=rg[R_FPUREGISTER].getregister(list,R_SUBFD)
  283. else
  284. result:=rg[R_FPUREGISTER].getregister(list,R_SUBFS);
  285. end;
  286. procedure TCGSparcGen.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);
  287. var
  288. href,href2 : treference;
  289. hloc : pcgparalocation;
  290. begin
  291. href:=ref;
  292. hloc:=paraloc.location;
  293. while assigned(hloc) do
  294. begin
  295. paramanager.allocparaloc(list,hloc);
  296. case hloc^.loc of
  297. LOC_REGISTER,LOC_CREGISTER :
  298. a_load_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  299. LOC_REFERENCE :
  300. begin
  301. reference_reset_base(href2,hloc^.reference.index,hloc^.reference.offset,paraloc.alignment,[]);
  302. a_load_ref_ref(list,hloc^.size,hloc^.size,href,href2);
  303. end;
  304. LOC_FPUREGISTER,LOC_CFPUREGISTER :
  305. a_loadfpu_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  306. else
  307. internalerror(200408241);
  308. end;
  309. inc(href.offset,tcgsize2size[hloc^.size]);
  310. hloc:=hloc^.next;
  311. end;
  312. end;
  313. procedure TCGSparcGen.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const paraloc : TCGPara);
  314. var
  315. href : treference;
  316. begin
  317. { happens for function result loc }
  318. if paraloc.location^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  319. begin
  320. paraloc.check_simple_location;
  321. paramanager.allocparaloc(list,paraloc.location);
  322. a_loadfpu_reg_reg(list,size,paraloc.location^.size,r,paraloc.location^.register);
  323. end
  324. else
  325. begin
  326. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,href);
  327. a_loadfpu_reg_ref(list,size,size,r,href);
  328. a_loadfpu_ref_cgpara(list,size,href,paraloc);
  329. tg.Ungettemp(list,href);
  330. end;
  331. end;
  332. procedure TCGSparcGen.a_call_name(list:TAsmList;const s:string; weak: boolean);
  333. begin
  334. if not weak then
  335. list.concat(taicpu.op_sym(A_CALL,current_asmdata.RefAsmSymbol(s,AT_FUNCTION)))
  336. else
  337. list.concat(taicpu.op_sym(A_CALL,current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION)));
  338. { Delay slot }
  339. list.concat(taicpu.op_none(A_NOP));
  340. end;
  341. procedure TCGSparcGen.a_call_reg(list:TAsmList;Reg:TRegister);
  342. begin
  343. list.concat(taicpu.op_reg(A_CALL,reg));
  344. { Delay slot }
  345. list.concat(taicpu.op_none(A_NOP));
  346. end;
  347. {********************** load instructions ********************}
  348. procedure TCGSparcGen.a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : TReference);
  349. begin
  350. if a=0 then
  351. a_load_reg_ref(list,size,size,NR_G0,ref)
  352. else
  353. inherited a_load_const_ref(list,size,a,ref);
  354. end;
  355. procedure TCGSparcGen.a_load_reg_ref(list:TAsmList;FromSize,ToSize:TCGSize;reg:tregister;const Ref:TReference);
  356. var
  357. op : tasmop;
  358. begin
  359. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  360. fromsize := tosize;
  361. if (ref.alignment<>0) and
  362. (ref.alignment<tcgsize2size[tosize]) then
  363. begin
  364. a_load_reg_ref_unaligned(list,FromSize,ToSize,reg,ref);
  365. end
  366. else
  367. begin
  368. case tosize of
  369. { signed integer registers }
  370. OS_8,
  371. OS_S8:
  372. Op:=A_STB;
  373. OS_16,
  374. OS_S16:
  375. Op:=A_STH;
  376. OS_32,
  377. OS_S32:
  378. Op:=A_ST;
  379. {$ifdef SPARC64}
  380. OS_64,
  381. OS_S64:
  382. Op:=A_STX;
  383. {$endif SPARC64}
  384. else
  385. InternalError(2002122100);
  386. end;
  387. handle_load_store(list,true,op,reg,ref);
  388. end;
  389. end;
  390. procedure TCGSparcGen.a_load_ref_reg(list:TAsmList;FromSize,ToSize:TCgSize;const ref:TReference;reg:tregister);
  391. var
  392. op : tasmop;
  393. begin
  394. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  395. fromsize := tosize;
  396. if (ref.alignment<>0) and
  397. (ref.alignment<tcgsize2size[fromsize]) then
  398. begin
  399. a_load_ref_reg_unaligned(list,FromSize,ToSize,ref,reg);
  400. end
  401. else
  402. begin
  403. case fromsize of
  404. OS_S8:
  405. Op:=A_LDSB;{Load Signed Byte}
  406. OS_8:
  407. Op:=A_LDUB;{Load Unsigned Byte}
  408. OS_S16:
  409. Op:=A_LDSH;{Load Signed Halfword}
  410. OS_16:
  411. Op:=A_LDUH;{Load Unsigned Halfword}
  412. {$ifdef SPARC64}
  413. OS_S32:
  414. Op:=A_LDSW;{Load Signed Word}
  415. OS_32:
  416. Op:=A_LDUW;{Load Unsigned Word}
  417. OS_64,
  418. OS_S64:
  419. Op:=A_LDX;
  420. {$else SPARC64}
  421. OS_S32,
  422. OS_32:
  423. Op:=A_LD;{Load Word}
  424. OS_S64,
  425. OS_64:
  426. Op:=A_LDD;{Load a Long Word}
  427. {$endif SPARC64}
  428. else
  429. InternalError(2002122101);
  430. end;
  431. handle_load_store(list,false,op,reg,ref);
  432. if (fromsize=OS_S8) and
  433. (tosize=OS_16) then
  434. a_load_reg_reg(list,fromsize,tosize,reg,reg);
  435. end;
  436. end;
  437. procedure TCGSparcGen.a_loadaddr_ref_reg(list : TAsmList;const ref : TReference;r : tregister);
  438. var
  439. href: treference;
  440. hreg: tregister;
  441. begin
  442. if (ref.base=NR_NO) and (ref.index<>NR_NO) then
  443. internalerror(200306171);
  444. if (ref.symbol=nil) then
  445. begin
  446. if (ref.base<>NR_NO) then
  447. begin
  448. if (ref.offset<simm13lo) or (ref.offset>simm13hi) then
  449. begin
  450. hreg:=getintregister(list,OS_ADDR);
  451. a_load_const_reg(list,OS_ADDR,ref.offset,hreg);
  452. list.concat(taicpu.op_reg_reg_reg(A_ADD,hreg,ref.base,r));
  453. if (ref.index<>NR_NO) then
  454. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref.index,r));
  455. end
  456. else if (ref.offset<>0) then
  457. begin
  458. list.concat(taicpu.op_reg_const_reg(A_ADD,ref.base,ref.offset,r));
  459. if (ref.index<>NR_NO) then
  460. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref.index,r));
  461. end
  462. else if (ref.index<>NR_NO) then
  463. list.concat(taicpu.op_reg_reg_reg(A_ADD,ref.base,ref.index,r))
  464. else
  465. a_load_reg_reg(list,OS_ADDR,OS_INT,ref.base,r); { (try to) emit optimizable move }
  466. end
  467. else
  468. a_load_const_reg(list,OS_ADDR,ref.offset,r);
  469. exit;
  470. end;
  471. reference_reset_symbol(href,ref.symbol,ref.offset,ref.alignment,ref.volatility);
  472. if (cs_create_pic in current_settings.moduleswitches) then
  473. begin
  474. include(current_procinfo.flags,pi_needs_got);
  475. href.offset:=0;
  476. if use_unlimited_pic_mode then
  477. begin
  478. href.refaddr:=addr_high;
  479. list.concat(taicpu.op_ref_reg(A_SETHI,href,r));
  480. href.refaddr:=addr_low;
  481. list.concat(taicpu.op_reg_ref_reg(A_OR,r,href,r));
  482. reference_reset_base(href,r,0,sizeof(pint),[]);
  483. href.index:=current_procinfo.got;
  484. end
  485. else
  486. begin
  487. href.base:=current_procinfo.got;
  488. href.refaddr:=addr_pic; { should it be done THAT way?? }
  489. end;
  490. { load contents of GOT slot }
  491. list.concat(taicpu.op_ref_reg(A_LD,href,r));
  492. { add original base/index, if any }
  493. if (ref.base<>NR_NO) then
  494. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref.base,r));
  495. if (ref.index<>NR_NO) then
  496. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref.index,r));
  497. { finally, add offset }
  498. if (ref.offset<simm13lo) or (ref.offset>simm13hi) then
  499. begin
  500. hreg:=getintregister(list,OS_ADDR);
  501. a_load_const_reg(list,OS_ADDR,ref.offset,hreg);
  502. list.concat(taicpu.op_reg_reg_reg(A_ADD,hreg,r,r));
  503. end
  504. else if (ref.offset<>0) then
  505. list.concat(taicpu.op_reg_const_reg(A_ADD,r,ref.offset,r));
  506. end
  507. else
  508. begin
  509. { load symbol+offset }
  510. href.refaddr:=addr_high;
  511. list.concat(taicpu.op_ref_reg(A_SETHI,href,r));
  512. href.refaddr:=addr_low;
  513. list.concat(taicpu.op_reg_ref_reg(A_OR,r,href,r));
  514. { add original base/index, if any }
  515. if (ref.base<>NR_NO) then
  516. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref.base,r));
  517. if (ref.index<>NR_NO) then
  518. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref.index,r));
  519. end;
  520. end;
  521. procedure TCGSparcGen.a_loadfpu_reg_reg(list:TAsmList;fromsize,tosize:tcgsize;reg1, reg2:tregister);
  522. const
  523. FpuMovInstr : Array[OS_F32..OS_F64,OS_F32..OS_F64] of TAsmOp =
  524. ((A_FMOVS,A_FSTOD),(A_FDTOS,A_FMOVD));
  525. var
  526. op: TAsmOp;
  527. instr : taicpu;
  528. begin
  529. op:=fpumovinstr[fromsize,tosize];
  530. instr:=taicpu.op_reg_reg(op,reg1,reg2);
  531. list.Concat(instr);
  532. { Notify the register allocator that we have written a move instruction so
  533. it can try to eliminate it. }
  534. if (op = A_FMOVS) or
  535. (op = A_FMOVD) then
  536. add_move_instruction(instr);
  537. end;
  538. procedure TCGSparcGen.a_loadfpu_ref_reg(list:TAsmList;fromsize,tosize:tcgsize;const ref:TReference;reg:tregister);
  539. const
  540. FpuLoadInstr : Array[OS_F32..OS_F64] of TAsmOp =
  541. (A_LDF,A_LDDF);
  542. var
  543. tmpreg: tregister;
  544. begin
  545. tmpreg:=NR_NO;
  546. if (fromsize<>tosize) then
  547. begin
  548. tmpreg:=reg;
  549. reg:=getfpuregister(list,fromsize);
  550. end;
  551. handle_load_store(list,false,fpuloadinstr[fromsize],reg,ref);
  552. if (fromsize<>tosize) then
  553. a_loadfpu_reg_reg(list,fromsize,tosize,reg,tmpreg);
  554. end;
  555. procedure TCGSparcGen.a_loadfpu_reg_ref(list:TAsmList;fromsize,tosize:tcgsize;reg:tregister;const ref:TReference);
  556. const
  557. FpuLoadInstr : Array[OS_F32..OS_F64] of TAsmOp =
  558. (A_STF,A_STDF);
  559. var
  560. tmpreg: tregister;
  561. begin
  562. if (fromsize<>tosize) then
  563. begin
  564. tmpreg:=getfpuregister(list,tosize);
  565. a_loadfpu_reg_reg(list,fromsize,tosize,reg,tmpreg);
  566. reg:=tmpreg;
  567. end;
  568. handle_load_store(list,true,fpuloadinstr[tosize],reg,ref);
  569. end;
  570. procedure TCGSparcGen.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  571. const
  572. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  573. begin
  574. if (op in overflowops) and
  575. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  576. a_load_reg_reg(list,OS_32,size,dst,dst);
  577. end;
  578. procedure TCGSparcGen.a_op_const_reg(list:TAsmList;Op:TOpCG;size:tcgsize;a:tcgint;reg:TRegister);
  579. begin
  580. optimize_op_const(size,op,a);
  581. case op of
  582. OP_NONE:
  583. exit;
  584. OP_MOVE:
  585. a_load_const_reg(list,size,a,reg);
  586. OP_NEG,OP_NOT:
  587. internalerror(200306011);
  588. else
  589. a_op_const_reg_reg(list,op,size,a,reg,reg);
  590. end;
  591. end;
  592. procedure TCGSparcGen.a_op_reg_reg(list:TAsmList;Op:TOpCG;size:TCGSize;src, dst:TRegister);
  593. begin
  594. Case Op of
  595. OP_NEG :
  596. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[size in [OS_64,OS_S64],op],src,dst));
  597. OP_NOT :
  598. list.concat(taicpu.op_reg_reg_reg(A_XNOR,src,NR_G0,dst));
  599. else
  600. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOp[size in [OS_64,OS_S64],op],dst,src,dst));
  601. end;
  602. maybeadjustresult(list,op,size,dst);
  603. end;
  604. procedure TCGSparcGen.a_op_const_reg_reg(list:TAsmList;op:TOpCg;size:tcgsize;a:tcgint;src, dst:tregister);
  605. var
  606. l: TLocation;
  607. begin
  608. a_op_const_reg_reg_checkoverflow(list,op,size,a,src,dst,false,l);
  609. end;
  610. procedure TCGSparcGen.a_op_reg_reg_reg(list:TAsmList;op:TOpCg;size:tcgsize;src1, src2, dst:tregister);
  611. begin
  612. if (TOpcg2AsmOp[size in [OS_64,OS_S64],op]=A_NONE) then
  613. InternalError(2013070305);
  614. if (op=OP_SAR) then
  615. begin
  616. if (size in [OS_S8,OS_S16]) then
  617. begin
  618. { Sign-extend before shifting }
  619. list.concat(taicpu.op_reg_const_reg(A_SLL,src2,32-(tcgsize2size[size]*8),dst));
  620. list.concat(taicpu.op_reg_const_reg(A_SRA,dst,32-(tcgsize2size[size]*8),dst));
  621. src2:=dst;
  622. end
  623. else if not (size in [OS_32,OS_S32]) then
  624. InternalError(2013070306);
  625. end;
  626. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOp[size in [OS_64,OS_S64],op],src2,src1,dst));
  627. maybeadjustresult(list,op,size,dst);
  628. end;
  629. procedure TCGSparcGen.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  630. var
  631. tmpreg1,tmpreg2 : tregister;
  632. begin
  633. ovloc.loc:=LOC_VOID;
  634. optimize_op_const(size,op,a);
  635. case op of
  636. OP_NONE:
  637. begin
  638. a_load_reg_reg(list,size,size,src,dst);
  639. exit;
  640. end;
  641. OP_MOVE:
  642. begin
  643. a_load_const_reg(list,size,a,dst);
  644. exit;
  645. end;
  646. OP_SAR:
  647. begin
  648. if (size in [OS_S8,OS_S16]) then
  649. begin
  650. list.concat(taicpu.op_reg_const_reg(A_SLL,src,32-(tcgsize2size[size]*8),dst));
  651. inc(a,32-tcgsize2size[size]*8);
  652. src:=dst;
  653. end
  654. {$ifndef SPARC64}
  655. else if not (size in [OS_32,OS_S32]) then
  656. InternalError(2013070303)
  657. {$endif SPARC64}
  658. ;
  659. end;
  660. end;
  661. if setflags then
  662. begin
  663. handle_reg_const_reg(list,TOpCG2AsmOpWithFlags[size in [OS_64,OS_S64],op],src,a,dst);
  664. case op of
  665. OP_MUL:
  666. begin
  667. tmpreg1:=GetIntRegister(list,OS_INT);
  668. list.concat(taicpu.op_reg_reg(A_MOV,NR_Y,tmpreg1));
  669. list.concat(taicpu.op_reg_reg(A_CMP,NR_G0,tmpreg1));
  670. ovloc.loc:=LOC_FLAGS;
  671. ovloc.resflags.Init(NR_ICC,F_NE);
  672. end;
  673. OP_IMUL:
  674. begin
  675. tmpreg1:=GetIntRegister(list,OS_INT);
  676. tmpreg2:=GetIntRegister(list,OS_INT);
  677. list.concat(taicpu.op_reg_reg(A_MOV,NR_Y,tmpreg1));
  678. list.concat(taicpu.op_reg_const_reg(A_SRA,dst,31,tmpreg2));
  679. list.concat(taicpu.op_reg_reg(A_CMP,tmpreg1,tmpreg2));
  680. ovloc.loc:=LOC_FLAGS;
  681. ovloc.resflags.Init(NR_ICC,F_NE);
  682. end;
  683. end;
  684. end
  685. else
  686. handle_reg_const_reg(list,TOpCG2AsmOp[size in [OS_64,OS_S64],op],src,a,dst);
  687. maybeadjustresult(list,op,size,dst);
  688. end;
  689. procedure TCGSparcGen.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  690. var
  691. tmpreg1,tmpreg2 : tregister;
  692. begin
  693. ovloc.loc:=LOC_VOID;
  694. if setflags then
  695. begin
  696. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOpWithFlags[size in [OS_64,OS_S64],op],src2,src1,dst));
  697. case op of
  698. OP_MUL:
  699. begin
  700. tmpreg1:=GetIntRegister(list,OS_INT);
  701. list.concat(taicpu.op_reg_reg(A_MOV,NR_Y,tmpreg1));
  702. list.concat(taicpu.op_reg_reg(A_CMP,NR_G0,tmpreg1));
  703. ovloc.loc:=LOC_FLAGS;
  704. ovloc.resflags.Init(NR_ICC,F_NE);
  705. end;
  706. OP_IMUL:
  707. begin
  708. tmpreg1:=GetIntRegister(list,OS_INT);
  709. tmpreg2:=GetIntRegister(list,OS_INT);
  710. list.concat(taicpu.op_reg_reg(A_MOV,NR_Y,tmpreg1));
  711. list.concat(taicpu.op_reg_const_reg(A_SRL,dst,31,tmpreg2));
  712. list.concat(taicpu.op_reg_reg(A_CMP,tmpreg1,tmpreg2));
  713. ovloc.loc:=LOC_FLAGS;
  714. ovloc.resflags.Init(NR_ICC,F_NE);
  715. end;
  716. end;
  717. end
  718. else
  719. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOp[size in [OS_64,OS_S64],op],src2,src1,dst));
  720. maybeadjustresult(list,op,size,dst);
  721. end;
  722. {*************** compare instructructions ****************}
  723. procedure TCGSparcGen.a_cmp_const_reg_label(list:TAsmList;size:tcgsize;cmp_op:topcmp;a:tcgint;reg:tregister;l:tasmlabel);
  724. begin
  725. if (a=0) then
  726. list.concat(taicpu.op_reg_reg_reg(A_SUBcc,reg,NR_G0,NR_G0))
  727. else
  728. handle_reg_const_reg(list,A_SUBcc,reg,a,NR_G0);
  729. a_jmp_cond(list,cmp_op,l);
  730. end;
  731. procedure TCGSparcGen.a_cmp_reg_reg_label(list:TAsmList;size:tcgsize;cmp_op:topcmp;reg1,reg2:tregister;l:tasmlabel);
  732. begin
  733. list.concat(taicpu.op_reg_reg_reg(A_SUBcc,reg2,reg1,NR_G0));
  734. a_jmp_cond(list,cmp_op,l);
  735. end;
  736. procedure TCGSparcGen.a_jmp_always(List:TAsmList;l:TAsmLabel);
  737. begin
  738. List.Concat(TAiCpu.op_sym(A_BA,current_asmdata.RefAsmSymbol(l.name,AT_FUNCTION)));
  739. { Delay slot }
  740. list.Concat(TAiCpu.Op_none(A_NOP));
  741. end;
  742. procedure TCGSparcGen.a_jmp_name(list : TAsmList;const s : string);
  743. begin
  744. List.Concat(TAiCpu.op_sym(A_BA,current_asmdata.RefAsmSymbol(s,AT_FUNCTION)));
  745. { Delay slot }
  746. list.Concat(TAiCpu.Op_none(A_NOP));
  747. end;
  748. procedure TCGSparcGen.a_jmp_cond(list:TAsmList;cond:TOpCmp;l:TAsmLabel);
  749. var
  750. ai:TAiCpu;
  751. begin
  752. ai:=TAiCpu.Op_sym(A_Bxx,l);
  753. ai.SetCondition(TOpCmp2AsmCond[cond]);
  754. list.Concat(ai);
  755. { Delay slot }
  756. list.Concat(TAiCpu.Op_none(A_NOP));
  757. end;
  758. procedure TCGSparcGen.a_jmp_flags(list:TAsmList;const f:TResFlags;l:tasmlabel);
  759. var
  760. ai : taicpu;
  761. begin
  762. case f.FlagReg of
  763. {$ifdef SPARC64}
  764. NR_XCC:
  765. ai:=Taicpu.op_reg_sym(A_Bxx,f.FlagReg,l);
  766. {$endif SPARC64}
  767. NR_ICC:
  768. ai:=Taicpu.op_sym(A_Bxx,l);
  769. NR_FCC0:
  770. ai:=Taicpu.op_sym(A_FBxx,l);
  771. NR_FCC1,NR_FCC2,NR_FCC3:
  772. ai:=Taicpu.op_reg_sym(A_FBxx,f.FlagReg,l);
  773. end;
  774. ai.SetCondition(flags_to_cond(f));
  775. list.Concat(ai);
  776. { Delay slot }
  777. list.Concat(TAiCpu.Op_none(A_NOP));
  778. end;
  779. procedure TCGSparcGen.g_flags2reg(list:TAsmList;Size:TCgSize;const f:tresflags;reg:TRegister);
  780. var
  781. hl : tasmlabel;
  782. ai : taicpu;
  783. begin
  784. if (f.FlagReg=NR_ICC) and (f.Flags in [F_B]) then
  785. list.concat(taicpu.op_reg_reg_reg(A_ADDX,NR_G0,NR_G0,reg))
  786. else if (f.FlagReg=NR_ICC) and (f.Flags in [F_AE]) then
  787. list.concat(taicpu.op_reg_const_reg(A_SUBX,NR_G0,-1,reg))
  788. else
  789. begin
  790. if current_settings.cputype in [cpu_SPARC_V9] then
  791. begin
  792. ai:=Taicpu.op_reg_const_reg(A_MOVcc,f.FlagReg,0,reg);
  793. ai.SetCondition(inverse_cond(flags_to_cond(f)));
  794. list.Concat(ai);
  795. ai:=Taicpu.op_reg_const_reg(A_MOVcc,f.FlagReg,1,reg);
  796. ai.SetCondition(flags_to_cond(f));
  797. list.Concat(ai);
  798. end
  799. else
  800. begin
  801. current_asmdata.getjumplabel(hl);
  802. a_load_const_reg(list,size,1,reg);
  803. a_jmp_flags(list,f,hl);
  804. a_load_const_reg(list,size,0,reg);
  805. a_label(list,hl);
  806. end;
  807. end;
  808. end;
  809. procedure TCGSparcGen.g_overflowCheck(List:TAsmList;const Loc:TLocation;def:TDef);
  810. var
  811. l : tlocation;
  812. begin
  813. l.loc:=LOC_VOID;
  814. g_overflowCheck_loc(list,loc,def,l);
  815. end;
  816. procedure TCGSparcGen.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  817. var
  818. hl : tasmlabel;
  819. ai:TAiCpu;
  820. hflags : tresflags;
  821. begin
  822. if not(cs_check_overflow in current_settings.localswitches) then
  823. exit;
  824. current_asmdata.getjumplabel(hl);
  825. case ovloc.loc of
  826. LOC_VOID:
  827. begin
  828. if not((def.typ=pointerdef) or
  829. ((def.typ=orddef) and
  830. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  831. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  832. begin
  833. ai:=TAiCpu.Op_sym(A_Bxx,hl);
  834. ai.SetCondition(C_VC);
  835. list.Concat(ai);
  836. { Delay slot }
  837. list.Concat(TAiCpu.Op_none(A_NOP));
  838. end
  839. else
  840. a_jmp_cond(list,OC_AE,hl);
  841. end;
  842. LOC_FLAGS:
  843. begin
  844. hflags:=ovloc.resflags;
  845. inverse_flags(hflags);
  846. cg.a_jmp_flags(list,hflags,hl);
  847. end;
  848. else
  849. internalerror(200409281);
  850. end;
  851. a_call_name(list,'FPC_OVERFLOW',false);
  852. a_label(list,hl);
  853. end;
  854. { *********** entry/exit code and address loading ************ }
  855. procedure TCGSparcGen.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  856. begin
  857. if nostackframe then
  858. exit;
  859. { Althogh the SPARC architecture require only word alignment, software
  860. convention and the operating system require every stack frame to be double word
  861. aligned }
  862. LocalSize:=align(LocalSize,8);
  863. { Execute the SAVE instruction to get a new register window and create a new
  864. stack frame. In the "SAVE %i6,size,%i6" the first %i6 is related to the state
  865. before execution of the SAVE instrucion so it is the caller %i6, when the %i6
  866. after execution of that instruction is the called function stack pointer}
  867. { constant can be 13 bit signed, since it's negative, size can be max. 4096 }
  868. if LocalSize>4096 then
  869. begin
  870. a_load_const_reg(list,OS_ADDR,-LocalSize,NR_G1);
  871. list.concat(Taicpu.Op_reg_reg_reg(A_SAVE,NR_STACK_POINTER_REG,NR_G1,NR_STACK_POINTER_REG));
  872. end
  873. else
  874. list.concat(Taicpu.Op_reg_const_reg(A_SAVE,NR_STACK_POINTER_REG,-LocalSize,NR_STACK_POINTER_REG));
  875. end;
  876. procedure TCGSparcGen.g_maybe_got_init(list : TAsmList);
  877. var
  878. ref : treference;
  879. hl : tasmlabel;
  880. begin
  881. if (cs_create_pic in current_settings.moduleswitches) and
  882. ((pi_needs_got in current_procinfo.flags) or
  883. (current_procinfo.procdef.proctypeoption=potype_unitfinalize)) then
  884. begin
  885. current_asmdata.getjumplabel(hl);
  886. list.concat(taicpu.op_sym(A_CALL,hl));
  887. { ABI recommends the following sequence:
  888. 1: call 2f
  889. sethi %hi(_GLOBAL_OFFSET_TABLE_+(.-1b)), %l7
  890. 2: or %l7, %lo(_GLOBAL_OFFSET_TABLE_+(.-1b)), %l7
  891. add %l7, %o7, %l7 }
  892. reference_reset_symbol(ref,current_asmdata.RefAsmSymbol('_GLOBAL_OFFSET_TABLE_',AT_DATA),4,sizeof(pint),[]);
  893. ref.refaddr:=addr_high;
  894. list.concat(taicpu.op_ref_reg(A_SETHI,ref,NR_L7));
  895. cg.a_label(list,hl);
  896. ref.refaddr:=addr_low;
  897. ref.offset:=8;
  898. list.concat(Taicpu.Op_reg_ref_reg(A_OR,NR_L7,ref,NR_L7));
  899. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_L7,NR_O7,NR_L7));
  900. { allocate NR_L7, so reg.allocator does not see it as available }
  901. list.concat(tai_regalloc.alloc(NR_L7,nil));
  902. end;
  903. end;
  904. procedure TCGSparcGen.g_restore_registers(list:TAsmList);
  905. begin
  906. { The sparc port uses the sparc standard calling convetions so this function has no used }
  907. end;
  908. procedure TCGSparcGen.g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);
  909. var
  910. hr : treference;
  911. begin
  912. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef) then
  913. begin
  914. reference_reset(hr,sizeof(pint),[]);
  915. hr.offset:=12;
  916. hr.refaddr:=addr_full;
  917. if nostackframe then
  918. begin
  919. hr.base:=NR_O7;
  920. list.concat(taicpu.op_ref_reg(A_JMPL,hr,NR_G0));
  921. list.concat(Taicpu.op_none(A_NOP))
  922. end
  923. else
  924. begin
  925. { We use trivial restore in the delay slot of the JMPL instruction, as we
  926. already set result onto %i0 }
  927. hr.base:=NR_I7;
  928. list.concat(taicpu.op_ref_reg(A_JMPL,hr,NR_G0));
  929. list.concat(Taicpu.op_none(A_RESTORE));
  930. end;
  931. end
  932. else
  933. begin
  934. if nostackframe then
  935. begin
  936. { Here we need to use RETL instead of RET so it uses %o7 }
  937. list.concat(Taicpu.op_none(A_RETL));
  938. list.concat(Taicpu.op_none(A_NOP))
  939. end
  940. else
  941. begin
  942. { We use trivial restore in the delay slot of the JMPL instruction, as we
  943. already set result onto %i0 }
  944. list.concat(Taicpu.op_none(A_RET));
  945. list.concat(Taicpu.op_none(A_RESTORE));
  946. end;
  947. end;
  948. end;
  949. procedure TCGSparcGen.g_save_registers(list : TAsmList);
  950. begin
  951. { The sparc port uses the sparc standard calling convetions so this function has no used }
  952. end;
  953. { ************* concatcopy ************ }
  954. procedure TCGSparcGen.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  955. var
  956. paraloc1,paraloc2,paraloc3 : TCGPara;
  957. pd : tprocdef;
  958. begin
  959. pd:=search_system_proc('MOVE');
  960. paraloc1.init;
  961. paraloc2.init;
  962. paraloc3.init;
  963. paramanager.getintparaloc(list,pd,1,paraloc1);
  964. paramanager.getintparaloc(list,pd,2,paraloc2);
  965. paramanager.getintparaloc(list,pd,3,paraloc3);
  966. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  967. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  968. a_loadaddr_ref_cgpara(list,source,paraloc1);
  969. paramanager.freecgpara(list,paraloc3);
  970. paramanager.freecgpara(list,paraloc2);
  971. paramanager.freecgpara(list,paraloc1);
  972. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  973. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  974. a_call_name(list,'FPC_MOVE',false);
  975. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  976. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  977. paraloc3.done;
  978. paraloc2.done;
  979. paraloc1.done;
  980. end;
  981. procedure TCGSparcGen.g_concatcopy(list:TAsmList;const source,dest:treference;len:tcgint);
  982. var
  983. tmpreg1,
  984. hreg,
  985. countreg: TRegister;
  986. src, dst: TReference;
  987. lab: tasmlabel;
  988. count, count2: longint;
  989. function reference_is_reusable(const ref: treference): boolean;
  990. begin
  991. result:=(ref.base<>NR_NO) and (ref.index=NR_NO) and
  992. (ref.symbol=nil) and
  993. (ref.offset>=simm13lo) and (ref.offset+len<=simm13hi);
  994. end;
  995. begin
  996. if len>high(longint) then
  997. internalerror(2002072704);
  998. { anybody wants to determine a good value here :)? }
  999. if len>100 then
  1000. g_concatcopy_move(list,source,dest,len)
  1001. else
  1002. begin
  1003. count:=len div 4;
  1004. if (count<=4) and reference_is_reusable(source) then
  1005. src:=source
  1006. else
  1007. begin
  1008. reference_reset_base(src,getintregister(list,OS_ADDR),0,sizeof(aint),source.volatility);
  1009. a_loadaddr_ref_reg(list,source,src.base);
  1010. end;
  1011. if (count<=4) and reference_is_reusable(dest) then
  1012. dst:=dest
  1013. else
  1014. begin
  1015. reference_reset_base(dst,getintregister(list,OS_ADDR),0,sizeof(aint),dest.volatility);
  1016. a_loadaddr_ref_reg(list,dest,dst.base);
  1017. end;
  1018. { generate a loop }
  1019. if count>4 then
  1020. begin
  1021. countreg:=GetIntRegister(list,OS_INT);
  1022. tmpreg1:=GetIntRegister(list,OS_INT);
  1023. a_load_const_reg(list,OS_ADDR,count,countreg);
  1024. current_asmdata.getjumplabel(lab);
  1025. a_label(list, lab);
  1026. list.concat(taicpu.op_ref_reg(A_LD,src,tmpreg1));
  1027. list.concat(taicpu.op_reg_ref(A_ST,tmpreg1,dst));
  1028. list.concat(taicpu.op_reg_const_reg(A_ADD,src.base,4,src.base));
  1029. list.concat(taicpu.op_reg_const_reg(A_ADD,dst.base,4,dst.base));
  1030. list.concat(taicpu.op_reg_const_reg(A_SUBcc,countreg,1,countreg));
  1031. a_jmp_cond(list,OC_NE,lab);
  1032. len := len mod 4;
  1033. end;
  1034. { unrolled loop }
  1035. count:=len div 4;
  1036. if count>0 then
  1037. begin
  1038. tmpreg1:=GetIntRegister(list,OS_INT);
  1039. for count2 := 1 to count do
  1040. begin
  1041. list.concat(taicpu.op_ref_reg(A_LD,src,tmpreg1));
  1042. list.concat(taicpu.op_reg_ref(A_ST,tmpreg1,dst));
  1043. inc(src.offset,4);
  1044. inc(dst.offset,4);
  1045. end;
  1046. len := len mod 4;
  1047. end;
  1048. if (len and 4) <> 0 then
  1049. begin
  1050. hreg:=GetIntRegister(list,OS_INT);
  1051. a_load_ref_reg(list,OS_32,OS_32,src,hreg);
  1052. a_load_reg_ref(list,OS_32,OS_32,hreg,dst);
  1053. inc(src.offset,4);
  1054. inc(dst.offset,4);
  1055. end;
  1056. { copy the leftovers }
  1057. if (len and 2) <> 0 then
  1058. begin
  1059. hreg:=GetIntRegister(list,OS_INT);
  1060. a_load_ref_reg(list,OS_16,OS_16,src,hreg);
  1061. a_load_reg_ref(list,OS_16,OS_16,hreg,dst);
  1062. inc(src.offset,2);
  1063. inc(dst.offset,2);
  1064. end;
  1065. if (len and 1) <> 0 then
  1066. begin
  1067. hreg:=GetIntRegister(list,OS_INT);
  1068. a_load_ref_reg(list,OS_8,OS_8,src,hreg);
  1069. a_load_reg_ref(list,OS_8,OS_8,hreg,dst);
  1070. end;
  1071. end;
  1072. end;
  1073. procedure TCGSparcGen.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  1074. var
  1075. src, dst: TReference;
  1076. tmpreg1,
  1077. countreg: TRegister;
  1078. i : longint;
  1079. lab: tasmlabel;
  1080. begin
  1081. if len>31 then
  1082. g_concatcopy_move(list,source,dest,len)
  1083. else
  1084. begin
  1085. reference_reset(src,source.alignment,source.volatility);
  1086. reference_reset(dst,dest.alignment,dest.volatility);
  1087. { load the address of source into src.base }
  1088. src.base:=GetAddressRegister(list);
  1089. a_loadaddr_ref_reg(list,source,src.base);
  1090. { load the address of dest into dst.base }
  1091. dst.base:=GetAddressRegister(list);
  1092. a_loadaddr_ref_reg(list,dest,dst.base);
  1093. { generate a loop }
  1094. if len>4 then
  1095. begin
  1096. countreg:=GetIntRegister(list,OS_ADDR);
  1097. tmpreg1:=GetIntRegister(list,OS_ADDR);
  1098. a_load_const_reg(list,OS_ADDR,len,countreg);
  1099. current_asmdata.getjumplabel(lab);
  1100. a_label(list, lab);
  1101. list.concat(taicpu.op_ref_reg(A_LDUB,src,tmpreg1));
  1102. list.concat(taicpu.op_reg_ref(A_STB,tmpreg1,dst));
  1103. list.concat(taicpu.op_reg_const_reg(A_ADD,src.base,1,src.base));
  1104. list.concat(taicpu.op_reg_const_reg(A_ADD,dst.base,1,dst.base));
  1105. list.concat(taicpu.op_reg_const_reg(A_SUBcc,countreg,1,countreg));
  1106. a_jmp_cond(list,OC_NE,lab);
  1107. end
  1108. else
  1109. begin
  1110. { unrolled loop }
  1111. tmpreg1:=GetIntRegister(list,OS_ADDR);
  1112. for i:=1 to len do
  1113. begin
  1114. list.concat(taicpu.op_ref_reg(A_LDUB,src,tmpreg1));
  1115. list.concat(taicpu.op_reg_ref(A_STB,tmpreg1,dst));
  1116. inc(src.offset);
  1117. inc(dst.offset);
  1118. end;
  1119. end;
  1120. end;
  1121. end;
  1122. procedure TCGSparcGen.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  1123. begin
  1124. { This method is integrated into g_intf_wrapper and shouldn't be called separately }
  1125. InternalError(2013020102);
  1126. end;
  1127. end.