cgsparc.pas 47 KB

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