cgcpu.pas 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  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 cgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,parabase,
  22. cgbase,cgutils,cgobj,cg64f32,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,
  24. cpubase,cpuinfo,
  25. node,symconst,SymType,symdef,
  26. rgcpu;
  27. type
  28. TCgSparc=class(tcg)
  29. protected
  30. function IsSimpleRef(const ref:treference):boolean;
  31. public
  32. procedure init_register_allocators;override;
  33. procedure done_register_allocators;override;
  34. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;override;
  35. { sparc special, needed by cg64 }
  36. procedure make_simple_ref(list:TAsmList;var ref: treference);
  37. procedure make_simple_ref_sparc(list:TAsmList;var ref: treference;loadaddr : boolean;addrreg : tregister);
  38. procedure handle_load_store(list:TAsmList;isstore:boolean;op: tasmop;reg:tregister;ref: treference);
  39. procedure handle_reg_const_reg(list:TAsmList;op:Tasmop;src:tregister;a:tcgint;dst:tregister);
  40. { parameter }
  41. procedure a_load_const_cgpara(list:TAsmList;size:tcgsize;a:tcgint;const paraloc:TCGPara);override;
  42. procedure a_load_ref_cgpara(list:TAsmList;sz:tcgsize;const r:TReference;const paraloc:TCGPara);override;
  43. procedure a_loadaddr_ref_cgpara(list:TAsmList;const r:TReference;const paraloc:TCGPara);override;
  44. procedure a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const paraloc : TCGPara);override;
  45. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);override;
  46. procedure a_call_name(list:TAsmList;const s:string; weak: boolean);override;
  47. procedure a_call_reg(list:TAsmList;Reg:TRegister);override;
  48. { General purpose instructions }
  49. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  50. procedure a_op_const_reg(list:TAsmList;Op:TOpCG;size:tcgsize;a:tcgint;reg:TRegister);override;
  51. procedure a_op_reg_reg(list:TAsmList;Op:TOpCG;size:TCGSize;src, dst:TRegister);override;
  52. procedure a_op_const_reg_reg(list:TAsmList;op:TOpCg;size:tcgsize;a:tcgint;src, dst:tregister);override;
  53. procedure a_op_reg_reg_reg(list:TAsmList;op:TOpCg;size:tcgsize;src1, src2, dst:tregister);override;
  54. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  55. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  56. { move instructions }
  57. procedure a_load_const_reg(list:TAsmList;size:tcgsize;a:tcgint;reg:tregister);override;
  58. procedure a_load_const_ref(list:TAsmList;size:tcgsize;a:tcgint;const ref:TReference);override;
  59. procedure a_load_reg_ref(list:TAsmList;FromSize,ToSize:TCgSize;reg:TRegister;const ref:TReference);override;
  60. procedure a_load_ref_reg(list:TAsmList;FromSize,ToSize:TCgSize;const ref:TReference;reg:tregister);override;
  61. procedure a_load_reg_reg(list:TAsmList;FromSize,ToSize:TCgSize;reg1,reg2:tregister);override;
  62. procedure a_loadaddr_ref_reg(list:TAsmList;const ref:TReference;r:tregister);override;
  63. { fpu move instructions }
  64. procedure a_loadfpu_reg_reg(list:TAsmList;fromsize,tosize:tcgsize;reg1, reg2:tregister);override;
  65. procedure a_loadfpu_ref_reg(list:TAsmList;fromsize,tosize:tcgsize;const ref:TReference;reg:tregister);override;
  66. procedure a_loadfpu_reg_ref(list:TAsmList;fromsize,tosize:tcgsize;reg:tregister;const ref:TReference);override;
  67. { comparison operations }
  68. procedure a_cmp_const_reg_label(list:TAsmList;size:tcgsize;cmp_op:topcmp;a:tcgint;reg:tregister;l:tasmlabel);override;
  69. procedure a_cmp_reg_reg_label(list:TAsmList;size:tcgsize;cmp_op:topcmp;reg1,reg2:tregister;l:tasmlabel);override;
  70. procedure a_jmp_always(List:TAsmList;l:TAsmLabel);override;
  71. procedure a_jmp_name(list : TAsmList;const s : string);override;
  72. procedure a_jmp_cond(list:TAsmList;cond:TOpCmp;l:tasmlabel);{ override;}
  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_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  85. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  86. { Transform unsupported methods into Internal errors }
  87. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: TCGSize; src, dst: TRegister); override;
  88. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  89. private
  90. g1_used : boolean;
  91. use_unlimited_pic_mode : boolean;
  92. end;
  93. TCg64Sparc=class(tcg64f32)
  94. private
  95. procedure get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp;checkoverflow : boolean);
  96. public
  97. procedure a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);override;
  98. procedure a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);override;
  99. procedure a_load64_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);override;
  100. procedure a_op64_reg_reg(list:TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst:TRegister64);override;
  101. procedure a_op64_const_reg(list:TAsmList;op:TOpCG;size : tcgsize;value:int64;regdst:TRegister64);override;
  102. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  103. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  104. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  105. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  106. end;
  107. procedure create_codegen;
  108. const
  109. TOpCG2AsmOp : array[topcg] of TAsmOp=(
  110. 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
  111. );
  112. TOpCG2AsmOpWithFlags : array[topcg] of TAsmOp=(
  113. 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
  114. );
  115. TOpCmp2AsmCond : array[topcmp] of TAsmCond=(C_NONE,
  116. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A
  117. );
  118. implementation
  119. uses
  120. globals,verbose,systems,cutils,
  121. paramgr,fmodule,
  122. tgobj,
  123. procinfo,cpupi;
  124. function TCgSparc.IsSimpleRef(const ref:treference):boolean;
  125. begin
  126. if (ref.base=NR_NO) and (ref.index<>NR_NO) then
  127. InternalError(2002100804);
  128. result :=not(assigned(ref.symbol))and
  129. (((ref.index = NR_NO) and
  130. (ref.offset >= simm13lo) and
  131. (ref.offset <= simm13hi)) or
  132. ((ref.index <> NR_NO) and
  133. (ref.offset = 0)));
  134. end;
  135. procedure tcgsparc.make_simple_ref(list:TAsmList;var ref: treference);
  136. begin
  137. make_simple_ref_sparc(list,ref,false,NR_NO);
  138. end;
  139. procedure tcgsparc.make_simple_ref_sparc(list:TAsmList;var ref: treference;loadaddr : boolean;addrreg : tregister);
  140. var
  141. tmpreg,tmpreg2 : tregister;
  142. tmpref : treference;
  143. need_add_got,need_got_load : boolean;
  144. begin
  145. if loadaddr then
  146. tmpreg:=addrreg
  147. else
  148. tmpreg:=NR_NO;
  149. need_add_got:=false;
  150. need_got_load:=false;
  151. { Be sure to have a base register }
  152. if (ref.base=NR_NO) then
  153. begin
  154. ref.base:=ref.index;
  155. ref.index:=NR_NO;
  156. end;
  157. if (cs_create_pic in current_settings.moduleswitches) and
  158. (tf_pic_uses_got in target_info.flags) and
  159. use_unlimited_pic_mode and
  160. assigned(ref.symbol) then
  161. begin
  162. if not(pi_needs_got in current_procinfo.flags) then
  163. begin
  164. //internalerror(200501161);
  165. include(current_procinfo.flags,pi_needs_got);
  166. end;
  167. if current_procinfo.got=NR_NO then
  168. current_procinfo.got:=NR_L7;
  169. need_got_load:=true;
  170. need_add_got:=true;
  171. end;
  172. if (cs_create_pic in current_settings.moduleswitches) and
  173. (tf_pic_uses_got in target_info.flags) and
  174. not use_unlimited_pic_mode and
  175. assigned(ref.symbol) then
  176. begin
  177. if tmpreg=NR_NO then
  178. tmpreg:=GetIntRegister(list,OS_INT);
  179. reference_reset(tmpref,ref.alignment);
  180. tmpref.symbol:=ref.symbol;
  181. tmpref.refaddr:=addr_pic;
  182. if not(pi_needs_got in current_procinfo.flags) then
  183. begin
  184. //internalerror(200501161);
  185. include(current_procinfo.flags,pi_needs_got);
  186. end;
  187. if current_procinfo.got=NR_NO then
  188. current_procinfo.got:=NR_L7;
  189. tmpref.index:=current_procinfo.got;
  190. list.concat(taicpu.op_ref_reg(A_LD,tmpref,tmpreg));
  191. ref.symbol:=nil;
  192. if (ref.index<>NR_NO) then
  193. begin
  194. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.index,tmpreg));
  195. ref.index:=tmpreg;
  196. end
  197. else
  198. begin
  199. if ref.base<>NR_NO then
  200. ref.index:=tmpreg
  201. else
  202. ref.base:=tmpreg;
  203. end;
  204. end;
  205. { When need to use SETHI, do it first }
  206. if assigned(ref.symbol) or
  207. (ref.offset<simm13lo) or
  208. (ref.offset>simm13hi) then
  209. begin
  210. if tmpreg=NR_NO then
  211. tmpreg:=GetIntRegister(list,OS_INT);
  212. reference_reset(tmpref,ref.alignment);
  213. tmpref.symbol:=ref.symbol;
  214. if not need_got_load then
  215. tmpref.offset:=ref.offset;
  216. tmpref.refaddr:=addr_high;
  217. list.concat(taicpu.op_ref_reg(A_SETHI,tmpref,tmpreg));
  218. if (ref.offset=0) and (ref.index=NR_NO) and
  219. (ref.base=NR_NO) and not need_add_got then
  220. begin
  221. ref.refaddr:=addr_low;
  222. end
  223. else
  224. begin
  225. { Load the low part is left }
  226. tmpref.refaddr:=addr_low;
  227. list.concat(taicpu.op_reg_ref_reg(A_OR,tmpreg,tmpref,tmpreg));
  228. if not need_got_load then
  229. ref.offset:=0;
  230. { symbol is loaded }
  231. ref.symbol:=nil;
  232. end;
  233. if need_add_got then
  234. begin
  235. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,current_procinfo.got,tmpreg));
  236. need_add_got:=false;
  237. end;
  238. if need_got_load then
  239. begin
  240. tmpref.refaddr:=addr_no;
  241. tmpref.base:=tmpreg;
  242. tmpref.symbol:=nil;
  243. list.concat(taicpu.op_ref_reg(A_LD,tmpref,tmpreg));
  244. need_got_load:=false;
  245. if (ref.offset<simm13lo) or
  246. (ref.offset>simm13hi) then
  247. begin
  248. tmpref.symbol:=nil;
  249. tmpref.offset:=ref.offset;
  250. tmpref.base:=tmpreg;
  251. tmpref.refaddr := addr_high;
  252. tmpreg2:=GetIntRegister(list,OS_INT);
  253. a_load_const_reg(list,OS_INT,ref.offset,tmpreg2);
  254. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,tmpreg2,tmpreg));
  255. ref.offset:=0;
  256. end;
  257. end;
  258. if (ref.index<>NR_NO) then
  259. begin
  260. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.index,tmpreg));
  261. ref.index:=tmpreg;
  262. end
  263. else
  264. begin
  265. if ref.base<>NR_NO then
  266. ref.index:=tmpreg
  267. else
  268. ref.base:=tmpreg;
  269. end;
  270. end;
  271. if need_add_got then
  272. begin
  273. if tmpreg=NR_NO then
  274. tmpreg:=GetIntRegister(list,OS_INT);
  275. list.concat(taicpu.op_reg_reg_reg(A_ADD,ref.base,current_procinfo.got,tmpreg));
  276. ref.base:=tmpreg;
  277. ref.index:=NR_NO;
  278. end;
  279. if need_got_load then
  280. begin
  281. if tmpreg=NR_NO then
  282. tmpreg:=GetIntRegister(list,OS_INT);
  283. list.concat(taicpu.op_ref_reg(A_LD,ref,tmpreg));
  284. ref.base:=tmpreg;
  285. ref.index:=NR_NO;
  286. end;
  287. if (ref.base<>NR_NO) or loadaddr then
  288. begin
  289. if loadaddr then
  290. begin
  291. if ref.index<>NR_NO then
  292. list.concat(taicpu.op_reg_reg_reg(A_ADD,ref.base,ref.index,tmpreg));
  293. ref.base:=tmpreg;
  294. ref.index:=NR_NO;
  295. if ref.offset<>0 then
  296. list.concat(taicpu.op_reg_const_reg(A_ADD,ref.base,ref.offset,tmpreg));
  297. end
  298. else if (ref.index<>NR_NO) and
  299. ((ref.offset<>0) or assigned(ref.symbol)) then
  300. begin
  301. if tmpreg=NR_NO then
  302. tmpreg:=GetIntRegister(list,OS_INT);
  303. list.concat(taicpu.op_reg_reg_reg(A_ADD,ref.base,ref.index,tmpreg));
  304. ref.base:=tmpreg;
  305. ref.index:=NR_NO;
  306. end;
  307. end;
  308. end;
  309. procedure tcgsparc.handle_load_store(list:TAsmList;isstore:boolean;op: tasmop;reg:tregister;ref: treference);
  310. begin
  311. make_simple_ref(list,ref);
  312. if isstore then
  313. list.concat(taicpu.op_reg_ref(op,reg,ref))
  314. else
  315. list.concat(taicpu.op_ref_reg(op,ref,reg));
  316. end;
  317. procedure tcgsparc.handle_reg_const_reg(list:TAsmList;op:Tasmop;src:tregister;a:tcgint;dst:tregister);
  318. var
  319. tmpreg : tregister;
  320. begin
  321. if (a<simm13lo) or
  322. (a>simm13hi) then
  323. begin
  324. if g1_used then
  325. GetIntRegister(list,OS_INT)
  326. else
  327. begin
  328. tmpreg:=NR_G1;
  329. g1_used:=true;
  330. end;
  331. a_load_const_reg(list,OS_INT,a,tmpreg);
  332. list.concat(taicpu.op_reg_reg_reg(op,src,tmpreg,dst));
  333. if tmpreg=NR_G1 then
  334. g1_used:=false;
  335. end
  336. else
  337. list.concat(taicpu.op_reg_const_reg(op,src,a,dst));
  338. end;
  339. {****************************************************************************
  340. Assembler code
  341. ****************************************************************************}
  342. procedure Tcgsparc.init_register_allocators;
  343. begin
  344. inherited init_register_allocators;
  345. if (cs_create_pic in current_settings.moduleswitches) and
  346. assigned(current_procinfo) and
  347. (pi_needs_got in current_procinfo.flags) then
  348. begin
  349. current_procinfo.got:=NR_L7;
  350. rg[R_INTREGISTER]:=Trgcpu.create(R_INTREGISTER,R_SUBD,
  351. [RS_O0,RS_O1,RS_O2,RS_O3,RS_O4,RS_O5,
  352. RS_L0,RS_L1,RS_L2,RS_L3,RS_L4,RS_L5,RS_L6],
  353. first_int_imreg,[]);
  354. end
  355. else
  356. rg[R_INTREGISTER]:=Trgcpu.create(R_INTREGISTER,R_SUBD,
  357. [RS_O0,RS_O1,RS_O2,RS_O3,RS_O4,RS_O5,
  358. RS_L0,RS_L1,RS_L2,RS_L3,RS_L4,RS_L5,RS_L6,RS_L7],
  359. first_int_imreg,[]);
  360. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBFS,
  361. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7,
  362. RS_F8,RS_F9,RS_F10,RS_F11,RS_F12,RS_F13,RS_F14,RS_F15,
  363. RS_F16,RS_F17,RS_F18,RS_F19,RS_F20,RS_F21,RS_F22,RS_F23,
  364. RS_F24,RS_F25,RS_F26,RS_F27,RS_F28,RS_F29,RS_F30,RS_F31],
  365. first_fpu_imreg,[]);
  366. { needs at least one element for rgobj not to crash }
  367. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  368. [RS_L0],first_mm_imreg,[]);
  369. end;
  370. procedure Tcgsparc.done_register_allocators;
  371. begin
  372. rg[R_INTREGISTER].free;
  373. rg[R_FPUREGISTER].free;
  374. rg[R_MMREGISTER].free;
  375. inherited done_register_allocators;
  376. end;
  377. function tcgsparc.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  378. begin
  379. if size=OS_F64 then
  380. result:=rg[R_FPUREGISTER].getregister(list,R_SUBFD)
  381. else
  382. result:=rg[R_FPUREGISTER].getregister(list,R_SUBFS);
  383. end;
  384. procedure TCgSparc.a_load_const_cgpara(list:TAsmList;size:tcgsize;a:tcgint;const paraloc:TCGPara);
  385. var
  386. Ref:TReference;
  387. begin
  388. paraloc.check_simple_location;
  389. paramanager.alloccgpara(list,paraloc);
  390. case paraloc.location^.loc of
  391. LOC_REGISTER,LOC_CREGISTER:
  392. a_load_const_reg(list,size,a,paraloc.location^.register);
  393. LOC_REFERENCE:
  394. begin
  395. { Code conventions need the parameters being allocated in %o6+92 }
  396. with paraloc.location^.Reference do
  397. begin
  398. if (Index=NR_SP) and (Offset<Target_info.first_parm_offset) then
  399. InternalError(2002081104);
  400. reference_reset_base(ref,index,offset,paraloc.alignment);
  401. end;
  402. a_load_const_ref(list,size,a,ref);
  403. end;
  404. else
  405. InternalError(2002122200);
  406. end;
  407. end;
  408. procedure TCgSparc.a_load_ref_cgpara(list:TAsmList;sz:TCgSize;const r:TReference;const paraloc:TCGPara);
  409. var
  410. ref: treference;
  411. tmpreg:TRegister;
  412. begin
  413. paraloc.check_simple_location;
  414. paramanager.alloccgpara(list,paraloc);
  415. with paraloc.location^ do
  416. begin
  417. case loc of
  418. LOC_REGISTER,LOC_CREGISTER :
  419. a_load_ref_reg(list,sz,paraloc.location^.size,r,Register);
  420. LOC_REFERENCE:
  421. begin
  422. { Code conventions need the parameters being allocated in %o6+92 }
  423. with Reference do
  424. begin
  425. if (Index=NR_SP) and (Offset<Target_info.first_parm_offset) then
  426. InternalError(2002081104);
  427. reference_reset_base(ref,index,offset,paraloc.alignment);
  428. end;
  429. if g1_used then
  430. GetIntRegister(list,OS_INT)
  431. else
  432. begin
  433. tmpreg:=NR_G1;
  434. g1_used:=true;
  435. end;
  436. a_load_ref_reg(list,sz,sz,r,tmpreg);
  437. a_load_reg_ref(list,sz,sz,tmpreg,ref);
  438. if tmpreg=NR_G1 then
  439. g1_used:=false;
  440. end;
  441. else
  442. internalerror(2002081103);
  443. end;
  444. end;
  445. end;
  446. procedure TCgSparc.a_loadaddr_ref_cgpara(list:TAsmList;const r:TReference;const paraloc:TCGPara);
  447. var
  448. Ref:TReference;
  449. TmpReg:TRegister;
  450. begin
  451. paraloc.check_simple_location;
  452. paramanager.alloccgpara(list,paraloc);
  453. with paraloc.location^ do
  454. begin
  455. case loc of
  456. LOC_REGISTER,LOC_CREGISTER:
  457. a_loadaddr_ref_reg(list,r,register);
  458. LOC_REFERENCE:
  459. begin
  460. reference_reset(ref,paraloc.alignment);
  461. ref.base := reference.index;
  462. ref.offset := reference.offset;
  463. tmpreg:=GetAddressRegister(list);
  464. a_loadaddr_ref_reg(list,r,tmpreg);
  465. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  466. end;
  467. else
  468. internalerror(2002080701);
  469. end;
  470. end;
  471. end;
  472. procedure tcgsparc.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);
  473. var
  474. href,href2 : treference;
  475. hloc : pcgparalocation;
  476. begin
  477. href:=ref;
  478. hloc:=paraloc.location;
  479. while assigned(hloc) do
  480. begin
  481. paramanager.allocparaloc(list,hloc);
  482. case hloc^.loc of
  483. LOC_REGISTER,LOC_CREGISTER :
  484. a_load_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  485. LOC_REFERENCE :
  486. begin
  487. reference_reset_base(href2,hloc^.reference.index,hloc^.reference.offset,paraloc.alignment);
  488. a_load_ref_ref(list,hloc^.size,hloc^.size,href,href2);
  489. end;
  490. LOC_FPUREGISTER,LOC_CFPUREGISTER :
  491. a_loadfpu_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  492. else
  493. internalerror(200408241);
  494. end;
  495. inc(href.offset,tcgsize2size[hloc^.size]);
  496. hloc:=hloc^.next;
  497. end;
  498. end;
  499. procedure tcgsparc.a_loadfpu_reg_cgpara(list : TAsmList;size : tcgsize;const r : tregister;const paraloc : TCGPara);
  500. var
  501. href : treference;
  502. begin
  503. { happens for function result loc }
  504. if paraloc.location^.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  505. begin
  506. paraloc.check_simple_location;
  507. paramanager.allocparaloc(list,paraloc.location);
  508. a_loadfpu_reg_reg(list,size,paraloc.location^.size,r,paraloc.location^.register);
  509. end
  510. else
  511. begin
  512. tg.GetTemp(list,TCGSize2Size[size],TCGSize2Size[size],tt_normal,href);
  513. a_loadfpu_reg_ref(list,size,size,r,href);
  514. a_loadfpu_ref_cgpara(list,size,href,paraloc);
  515. tg.Ungettemp(list,href);
  516. end;
  517. end;
  518. procedure TCgSparc.a_call_name(list:TAsmList;const s:string; weak: boolean);
  519. begin
  520. if not weak then
  521. list.concat(taicpu.op_sym(A_CALL,current_asmdata.RefAsmSymbol(s)))
  522. else
  523. list.concat(taicpu.op_sym(A_CALL,current_asmdata.WeakRefAsmSymbol(s)));
  524. { Delay slot }
  525. list.concat(taicpu.op_none(A_NOP));
  526. end;
  527. procedure TCgSparc.a_call_reg(list:TAsmList;Reg:TRegister);
  528. begin
  529. list.concat(taicpu.op_reg(A_CALL,reg));
  530. { Delay slot }
  531. list.concat(taicpu.op_none(A_NOP));
  532. end;
  533. {********************** load instructions ********************}
  534. procedure TCgSparc.a_load_const_reg(list : TAsmList;size : TCGSize;a : tcgint;reg : TRegister);
  535. begin
  536. { we don't use the set instruction here because it could be evalutated to two
  537. instructions which would cause problems with the delay slot (FK) }
  538. if (a=0) then
  539. list.concat(taicpu.op_reg(A_CLR,reg))
  540. { sethi allows to set the upper 22 bit, so we'll take full advantage of it }
  541. else if (aint(a) and aint($1fff))=0 then
  542. list.concat(taicpu.op_const_reg(A_SETHI,aint(a) shr 10,reg))
  543. else if (a>=simm13lo) and (a<=simm13hi) then
  544. list.concat(taicpu.op_const_reg(A_MOV,a,reg))
  545. else
  546. begin
  547. list.concat(taicpu.op_const_reg(A_SETHI,aint(a) shr 10,reg));
  548. list.concat(taicpu.op_reg_const_reg(A_OR,reg,aint(a) and aint($3ff),reg));
  549. end;
  550. end;
  551. procedure TCgSparc.a_load_const_ref(list : TAsmList;size : tcgsize;a : tcgint;const ref : TReference);
  552. begin
  553. if a=0 then
  554. a_load_reg_ref(list,size,size,NR_G0,ref)
  555. else
  556. inherited a_load_const_ref(list,size,a,ref);
  557. end;
  558. procedure TCgSparc.a_load_reg_ref(list:TAsmList;FromSize,ToSize:TCGSize;reg:tregister;const Ref:TReference);
  559. var
  560. op : tasmop;
  561. begin
  562. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  563. fromsize := tosize;
  564. if (ref.alignment<>0) and
  565. (ref.alignment<tcgsize2size[tosize]) then
  566. begin
  567. a_load_reg_ref_unaligned(list,FromSize,ToSize,reg,ref);
  568. end
  569. else
  570. begin
  571. case tosize of
  572. { signed integer registers }
  573. OS_8,
  574. OS_S8:
  575. Op:=A_STB;
  576. OS_16,
  577. OS_S16:
  578. Op:=A_STH;
  579. OS_32,
  580. OS_S32:
  581. Op:=A_ST;
  582. else
  583. InternalError(2002122100);
  584. end;
  585. handle_load_store(list,true,op,reg,ref);
  586. end;
  587. end;
  588. procedure TCgSparc.a_load_ref_reg(list:TAsmList;FromSize,ToSize:TCgSize;const ref:TReference;reg:tregister);
  589. var
  590. op : tasmop;
  591. begin
  592. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  593. fromsize := tosize;
  594. if (ref.alignment<>0) and
  595. (ref.alignment<tcgsize2size[fromsize]) then
  596. begin
  597. a_load_ref_reg_unaligned(list,FromSize,ToSize,ref,reg);
  598. end
  599. else
  600. begin
  601. case fromsize of
  602. OS_S8:
  603. Op:=A_LDSB;{Load Signed Byte}
  604. OS_8:
  605. Op:=A_LDUB;{Load Unsigned Byte}
  606. OS_S16:
  607. Op:=A_LDSH;{Load Signed Halfword}
  608. OS_16:
  609. Op:=A_LDUH;{Load Unsigned Halfword}
  610. OS_S32,
  611. OS_32:
  612. Op:=A_LD;{Load Word}
  613. OS_S64,
  614. OS_64:
  615. Op:=A_LDD;{Load a Long Word}
  616. else
  617. InternalError(2002122101);
  618. end;
  619. handle_load_store(list,false,op,reg,ref);
  620. if (fromsize=OS_S8) and
  621. (tosize=OS_16) then
  622. a_load_reg_reg(list,fromsize,tosize,reg,reg);
  623. end;
  624. end;
  625. procedure TCgSparc.a_load_reg_reg(list:TAsmList;fromsize,tosize:tcgsize;reg1,reg2:tregister);
  626. var
  627. instr : taicpu;
  628. begin
  629. if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  630. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and
  631. (fromsize <> tosize)) or
  632. { needs to mask out the sign in the top 16 bits }
  633. ((fromsize = OS_S8) and
  634. (tosize = OS_16)) then
  635. case tosize of
  636. OS_8 :
  637. a_op_const_reg_reg(list,OP_AND,tosize,$ff,reg1,reg2);
  638. OS_16 :
  639. a_op_const_reg_reg(list,OP_AND,tosize,$ffff,reg1,reg2);
  640. OS_32,
  641. OS_S32 :
  642. begin
  643. instr:=taicpu.op_reg_reg(A_MOV,reg1,reg2);
  644. list.Concat(instr);
  645. { Notify the register allocator that we have written a move instruction so
  646. it can try to eliminate it. }
  647. add_move_instruction(instr);
  648. end;
  649. OS_S8 :
  650. begin
  651. list.concat(taicpu.op_reg_const_reg(A_SLL,reg1,24,reg2));
  652. list.concat(taicpu.op_reg_const_reg(A_SRA,reg2,24,reg2));
  653. end;
  654. OS_S16 :
  655. begin
  656. list.concat(taicpu.op_reg_const_reg(A_SLL,reg1,16,reg2));
  657. list.concat(taicpu.op_reg_const_reg(A_SRA,reg2,16,reg2));
  658. end;
  659. else
  660. internalerror(2002090901);
  661. end
  662. else
  663. begin
  664. instr:=taicpu.op_reg_reg(A_MOV,reg1,reg2);
  665. list.Concat(instr);
  666. { Notify the register allocator that we have written a move instruction so
  667. it can try to eliminate it. }
  668. add_move_instruction(instr);
  669. end;
  670. end;
  671. procedure TCgSparc.a_loadaddr_ref_reg(list : TAsmList;const ref : TReference;r : tregister);
  672. var
  673. tmpref,href : treference;
  674. hreg,tmpreg,hreg2 : tregister;
  675. need_got,need_got_load : boolean;
  676. begin
  677. href:=ref;
  678. {$ifdef TEST_SIMPLE_SPARC}
  679. make_simple_ref_sparc(list,href,true,r);
  680. {$else}
  681. need_got:=false;
  682. need_got_load:=false;
  683. if (href.base=NR_NO) and (href.index<>NR_NO) then
  684. internalerror(200306171);
  685. if (cs_create_pic in current_settings.moduleswitches) and
  686. (tf_pic_uses_got in target_info.flags) and
  687. use_unlimited_pic_mode and
  688. assigned(ref.symbol) then
  689. begin
  690. if not(pi_needs_got in current_procinfo.flags) then
  691. begin
  692. //internalerror(200501161);
  693. include(current_procinfo.flags,pi_needs_got);
  694. end;
  695. if current_procinfo.got=NR_NO then
  696. current_procinfo.got:=NR_L7;
  697. need_got_load:=true;
  698. need_got:=true;
  699. end;
  700. if (cs_create_pic in current_settings.moduleswitches) and
  701. (tf_pic_uses_got in target_info.flags) and
  702. not use_unlimited_pic_mode and
  703. assigned(href.symbol) then
  704. begin
  705. tmpreg:=GetIntRegister(list,OS_ADDR);
  706. reference_reset(tmpref,href.alignment);
  707. tmpref.symbol:=href.symbol;
  708. tmpref.refaddr:=addr_pic;
  709. if not(pi_needs_got in current_procinfo.flags) then
  710. begin
  711. //internalerror(200501161);
  712. include(current_procinfo.flags,pi_needs_got);
  713. end;
  714. if current_procinfo.got=NR_NO then
  715. current_procinfo.got:=NR_L7;
  716. tmpref.base:=current_procinfo.got;
  717. list.concat(taicpu.op_ref_reg(A_LD,tmpref,tmpreg));
  718. href.symbol:=nil;
  719. if (href.index<>NR_NO) then
  720. begin
  721. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,href.index,tmpreg));
  722. href.index:=tmpreg;
  723. end
  724. else
  725. begin
  726. if href.base<>NR_NO then
  727. href.index:=tmpreg
  728. else
  729. href.base:=tmpreg;
  730. end;
  731. end;
  732. { At least big offset (need SETHI), maybe base and maybe index }
  733. if assigned(href.symbol) or
  734. (href.offset<simm13lo) or
  735. (href.offset>simm13hi) then
  736. begin
  737. hreg:=GetAddressRegister(list);
  738. reference_reset(tmpref,href.alignment);
  739. tmpref.symbol := href.symbol;
  740. if not need_got_load then
  741. tmpref.offset := href.offset;
  742. tmpref.refaddr := addr_high;
  743. list.concat(taicpu.op_ref_reg(A_SETHI,tmpref,hreg));
  744. { Only the low part is left }
  745. tmpref.refaddr:=addr_low;
  746. list.concat(taicpu.op_reg_ref_reg(A_OR,hreg,tmpref,hreg));
  747. if need_got then
  748. begin
  749. list.concat(taicpu.op_reg_reg_reg(A_ADD,hreg,current_procinfo.got,hreg));
  750. need_got:=false;
  751. end;
  752. if need_got_load then
  753. begin
  754. tmpref.symbol:=nil;
  755. tmpref.base:=hreg;
  756. tmpref.refaddr:=addr_no;
  757. list.concat(taicpu.op_ref_reg(A_LD,tmpref,hreg));
  758. need_got_load:=false;
  759. if (href.offset<simm13lo) or
  760. (href.offset>simm13hi) then
  761. begin
  762. tmpref.symbol:=nil;
  763. tmpref.offset:=href.offset;
  764. tmpref.refaddr := addr_high;
  765. hreg2:=GetIntRegister(list,OS_INT);
  766. a_load_const_reg(list,OS_INT,href.offset,hreg2);
  767. { Only the low part is left }
  768. list.concat(taicpu.op_reg_reg_reg(A_ADD,hreg,hreg2,hreg));
  769. end
  770. else if (href.offset<>0) then
  771. begin
  772. list.concat(taicpu.op_reg_const_reg(A_ADD,hreg,href.offset,hreg));
  773. end;
  774. end;
  775. if href.base<>NR_NO then
  776. begin
  777. if href.index<>NR_NO then
  778. begin
  779. list.concat(taicpu.op_reg_reg_reg(A_ADD,hreg,href.base,hreg));
  780. list.concat(taicpu.op_reg_reg_reg(A_ADD,hreg,href.index,r));
  781. end
  782. else
  783. list.concat(taicpu.op_reg_reg_reg(A_ADD,hreg,href.base,r));
  784. end
  785. else
  786. begin
  787. if hreg<>r then
  788. a_load_reg_reg(list,OS_ADDR,OS_ADDR,hreg,r);
  789. end;
  790. end
  791. else
  792. { At least small offset, maybe base and maybe index }
  793. if href.offset<>0 then
  794. begin
  795. if href.base<>NR_NO then
  796. begin
  797. if href.index<>NR_NO then
  798. begin
  799. hreg:=GetAddressRegister(list);
  800. list.concat(taicpu.op_reg_const_reg(A_ADD,href.base,href.offset,hreg));
  801. list.concat(taicpu.op_reg_reg_reg(A_ADD,hreg,href.index,r));
  802. end
  803. else
  804. list.concat(taicpu.op_reg_const_reg(A_ADD,href.base,href.offset,r));
  805. end
  806. else
  807. list.concat(taicpu.op_const_reg(A_MOV,href.offset,r));
  808. end
  809. else
  810. { Both base and index }
  811. if href.index<>NR_NO then
  812. list.concat(taicpu.op_reg_reg_reg(A_ADD,href.base,href.index,r))
  813. else
  814. { Only base }
  815. if href.base<>NR_NO then
  816. a_load_reg_reg(list,OS_ADDR,OS_ADDR,href.base,r)
  817. else
  818. { only offset, can be generated by absolute }
  819. a_load_const_reg(list,OS_ADDR,href.offset,r);
  820. if need_got then
  821. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,current_procinfo.got,r));
  822. if need_got_load then
  823. list.concat(taicpu.op_reg_reg(A_LD,r,r));
  824. {$endif}
  825. end;
  826. procedure TCgSparc.a_loadfpu_reg_reg(list:TAsmList;fromsize,tosize:tcgsize;reg1, reg2:tregister);
  827. const
  828. FpuMovInstr : Array[OS_F32..OS_F64,OS_F32..OS_F64] of TAsmOp =
  829. ((A_FMOVS,A_FSTOD),(A_FDTOS,A_FMOVD));
  830. var
  831. op: TAsmOp;
  832. instr : taicpu;
  833. begin
  834. op:=fpumovinstr[fromsize,tosize];
  835. instr:=taicpu.op_reg_reg(op,reg1,reg2);
  836. list.Concat(instr);
  837. { Notify the register allocator that we have written a move instruction so
  838. it can try to eliminate it. }
  839. if (op = A_FMOVS) or
  840. (op = A_FMOVD) then
  841. add_move_instruction(instr);
  842. end;
  843. procedure TCgSparc.a_loadfpu_ref_reg(list:TAsmList;fromsize,tosize:tcgsize;const ref:TReference;reg:tregister);
  844. const
  845. FpuLoadInstr : Array[OS_F32..OS_F64] of TAsmOp =
  846. (A_LDF,A_LDDF);
  847. var
  848. tmpreg: tregister;
  849. begin
  850. if (fromsize<>tosize) then
  851. begin
  852. tmpreg:=reg;
  853. reg:=getfpuregister(list,fromsize);
  854. end;
  855. handle_load_store(list,false,fpuloadinstr[fromsize],reg,ref);
  856. if (fromsize<>tosize) then
  857. a_loadfpu_reg_reg(list,fromsize,tosize,reg,tmpreg);
  858. end;
  859. procedure TCgSparc.a_loadfpu_reg_ref(list:TAsmList;fromsize,tosize:tcgsize;reg:tregister;const ref:TReference);
  860. const
  861. FpuLoadInstr : Array[OS_F32..OS_F64] of TAsmOp =
  862. (A_STF,A_STDF);
  863. var
  864. tmpreg: tregister;
  865. begin
  866. if (fromsize<>tosize) then
  867. begin
  868. tmpreg:=getfpuregister(list,tosize);
  869. a_loadfpu_reg_reg(list,fromsize,tosize,reg,tmpreg);
  870. reg:=tmpreg;
  871. end;
  872. handle_load_store(list,true,fpuloadinstr[tosize],reg,ref);
  873. end;
  874. procedure tcgsparc.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  875. const
  876. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  877. begin
  878. if (op in overflowops) and
  879. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  880. a_load_reg_reg(list,OS_32,size,dst,dst);
  881. end;
  882. procedure TCgSparc.a_op_const_reg(list:TAsmList;Op:TOpCG;size:tcgsize;a:tcgint;reg:TRegister);
  883. begin
  884. if Op in [OP_NEG,OP_NOT] then
  885. internalerror(200306011);
  886. if (a=0) then
  887. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOp[op],reg,NR_G0,reg))
  888. else
  889. handle_reg_const_reg(list,TOpCG2AsmOp[op],reg,a,reg);
  890. maybeadjustresult(list,op,size,reg);
  891. end;
  892. procedure TCgSparc.a_op_reg_reg(list:TAsmList;Op:TOpCG;size:TCGSize;src, dst:TRegister);
  893. var
  894. a : aint;
  895. begin
  896. Case Op of
  897. OP_NEG :
  898. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],src,dst));
  899. OP_NOT :
  900. begin
  901. case size of
  902. OS_8 :
  903. a:=aint($ffffff00);
  904. OS_16 :
  905. a:=aint($ffff0000);
  906. else
  907. a:=0;
  908. end;
  909. handle_reg_const_reg(list,A_XNOR,src,a,dst);
  910. end;
  911. else
  912. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOp[op],dst,src,dst));
  913. end;
  914. maybeadjustresult(list,op,size,dst);
  915. end;
  916. procedure TCgSparc.a_op_const_reg_reg(list:TAsmList;op:TOpCg;size:tcgsize;a:tcgint;src, dst:tregister);
  917. var
  918. power : longInt;
  919. begin
  920. case op of
  921. OP_MUL,
  922. OP_IMUL:
  923. begin
  924. if ispowerof2(a,power) then
  925. begin
  926. { can be done with a shift }
  927. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  928. exit;
  929. end;
  930. end;
  931. OP_SUB,
  932. OP_ADD :
  933. begin
  934. if (a=0) then
  935. begin
  936. a_load_reg_reg(list,size,size,src,dst);
  937. exit;
  938. end;
  939. end;
  940. end;
  941. handle_reg_const_reg(list,TOpCG2AsmOp[op],src,a,dst);
  942. maybeadjustresult(list,op,size,dst);
  943. end;
  944. procedure TCgSparc.a_op_reg_reg_reg(list:TAsmList;op:TOpCg;size:tcgsize;src1, src2, dst:tregister);
  945. begin
  946. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOp[op],src2,src1,dst));
  947. maybeadjustresult(list,op,size,dst);
  948. end;
  949. procedure tcgsparc.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  950. var
  951. power : longInt;
  952. tmpreg1,tmpreg2 : tregister;
  953. begin
  954. ovloc.loc:=LOC_VOID;
  955. case op of
  956. OP_SUB,
  957. OP_ADD :
  958. begin
  959. if (a=0) then
  960. begin
  961. a_load_reg_reg(list,size,size,src,dst);
  962. exit;
  963. end;
  964. end;
  965. end;
  966. if setflags then
  967. begin
  968. handle_reg_const_reg(list,TOpCG2AsmOpWithFlags[op],src,a,dst);
  969. case op of
  970. OP_MUL:
  971. begin
  972. tmpreg1:=GetIntRegister(list,OS_INT);
  973. list.concat(taicpu.op_reg_reg(A_MOV,NR_Y,tmpreg1));
  974. list.concat(taicpu.op_reg_reg(A_CMP,NR_G0,tmpreg1));
  975. ovloc.loc:=LOC_FLAGS;
  976. ovloc.resflags:=F_NE;
  977. end;
  978. OP_IMUL:
  979. begin
  980. tmpreg1:=GetIntRegister(list,OS_INT);
  981. tmpreg2:=GetIntRegister(list,OS_INT);
  982. list.concat(taicpu.op_reg_reg(A_MOV,NR_Y,tmpreg1));
  983. list.concat(taicpu.op_reg_const_reg(A_SRA,dst,31,tmpreg2));
  984. list.concat(taicpu.op_reg_reg(A_CMP,tmpreg1,tmpreg2));
  985. ovloc.loc:=LOC_FLAGS;
  986. ovloc.resflags:=F_NE;
  987. end;
  988. end;
  989. end
  990. else
  991. handle_reg_const_reg(list,TOpCG2AsmOp[op],src,a,dst);
  992. maybeadjustresult(list,op,size,dst);
  993. end;
  994. procedure tcgsparc.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  995. var
  996. tmpreg1,tmpreg2 : tregister;
  997. begin
  998. ovloc.loc:=LOC_VOID;
  999. if setflags then
  1000. begin
  1001. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOpWithFlags[op],src2,src1,dst));
  1002. case op of
  1003. OP_MUL:
  1004. begin
  1005. tmpreg1:=GetIntRegister(list,OS_INT);
  1006. list.concat(taicpu.op_reg_reg(A_MOV,NR_Y,tmpreg1));
  1007. list.concat(taicpu.op_reg_reg(A_CMP,NR_G0,tmpreg1));
  1008. ovloc.loc:=LOC_FLAGS;
  1009. ovloc.resflags:=F_NE;
  1010. end;
  1011. OP_IMUL:
  1012. begin
  1013. tmpreg1:=GetIntRegister(list,OS_INT);
  1014. tmpreg2:=GetIntRegister(list,OS_INT);
  1015. list.concat(taicpu.op_reg_reg(A_MOV,NR_Y,tmpreg1));
  1016. list.concat(taicpu.op_reg_const_reg(A_SRL,dst,31,tmpreg2));
  1017. list.concat(taicpu.op_reg_reg(A_CMP,tmpreg1,tmpreg2));
  1018. ovloc.loc:=LOC_FLAGS;
  1019. ovloc.resflags:=F_NE;
  1020. end;
  1021. end;
  1022. end
  1023. else
  1024. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOp[op],src2,src1,dst));
  1025. maybeadjustresult(list,op,size,dst);
  1026. end;
  1027. {*************** compare instructructions ****************}
  1028. procedure TCgSparc.a_cmp_const_reg_label(list:TAsmList;size:tcgsize;cmp_op:topcmp;a:tcgint;reg:tregister;l:tasmlabel);
  1029. begin
  1030. if (a=0) then
  1031. list.concat(taicpu.op_reg_reg_reg(A_SUBcc,reg,NR_G0,NR_G0))
  1032. else
  1033. handle_reg_const_reg(list,A_SUBcc,reg,a,NR_G0);
  1034. a_jmp_cond(list,cmp_op,l);
  1035. end;
  1036. procedure TCgSparc.a_cmp_reg_reg_label(list:TAsmList;size:tcgsize;cmp_op:topcmp;reg1,reg2:tregister;l:tasmlabel);
  1037. begin
  1038. list.concat(taicpu.op_reg_reg_reg(A_SUBcc,reg2,reg1,NR_G0));
  1039. a_jmp_cond(list,cmp_op,l);
  1040. end;
  1041. procedure TCgSparc.a_jmp_always(List:TAsmList;l:TAsmLabel);
  1042. begin
  1043. List.Concat(TAiCpu.op_sym(A_BA,current_asmdata.RefAsmSymbol(l.name)));
  1044. { Delay slot }
  1045. list.Concat(TAiCpu.Op_none(A_NOP));
  1046. end;
  1047. procedure tcgsparc.a_jmp_name(list : TAsmList;const s : string);
  1048. begin
  1049. List.Concat(TAiCpu.op_sym(A_BA,current_asmdata.RefAsmSymbol(s)));
  1050. { Delay slot }
  1051. list.Concat(TAiCpu.Op_none(A_NOP));
  1052. end;
  1053. procedure TCgSparc.a_jmp_cond(list:TAsmList;cond:TOpCmp;l:TAsmLabel);
  1054. var
  1055. ai:TAiCpu;
  1056. begin
  1057. ai:=TAiCpu.Op_sym(A_Bxx,l);
  1058. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1059. list.Concat(ai);
  1060. { Delay slot }
  1061. list.Concat(TAiCpu.Op_none(A_NOP));
  1062. end;
  1063. procedure TCgSparc.a_jmp_flags(list:TAsmList;const f:TResFlags;l:tasmlabel);
  1064. var
  1065. ai : taicpu;
  1066. op : tasmop;
  1067. begin
  1068. if f in [F_FE,F_FNE,F_FG,F_FL,F_FGE,F_FLE] then
  1069. op:=A_FBxx
  1070. else
  1071. op:=A_Bxx;
  1072. ai := Taicpu.op_sym(op,l);
  1073. ai.SetCondition(flags_to_cond(f));
  1074. list.Concat(ai);
  1075. { Delay slot }
  1076. list.Concat(TAiCpu.Op_none(A_NOP));
  1077. end;
  1078. procedure TCgSparc.g_flags2reg(list:TAsmList;Size:TCgSize;const f:tresflags;reg:TRegister);
  1079. var
  1080. hl : tasmlabel;
  1081. begin
  1082. current_asmdata.getjumplabel(hl);
  1083. a_load_const_reg(list,size,1,reg);
  1084. a_jmp_flags(list,f,hl);
  1085. a_load_const_reg(list,size,0,reg);
  1086. a_label(list,hl);
  1087. end;
  1088. procedure tcgsparc.g_overflowCheck(List:TAsmList;const Loc:TLocation;def:TDef);
  1089. var
  1090. l : tlocation;
  1091. begin
  1092. l.loc:=LOC_VOID;
  1093. g_overflowCheck_loc(list,loc,def,l);
  1094. end;
  1095. procedure TCgSparc.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  1096. var
  1097. hl : tasmlabel;
  1098. ai:TAiCpu;
  1099. hflags : tresflags;
  1100. begin
  1101. if not(cs_check_overflow in current_settings.localswitches) then
  1102. exit;
  1103. current_asmdata.getjumplabel(hl);
  1104. case ovloc.loc of
  1105. LOC_VOID:
  1106. begin
  1107. if not((def.typ=pointerdef) or
  1108. ((def.typ=orddef) and
  1109. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1110. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1111. begin
  1112. ai:=TAiCpu.Op_sym(A_Bxx,hl);
  1113. ai.SetCondition(C_NO);
  1114. list.Concat(ai);
  1115. { Delay slot }
  1116. list.Concat(TAiCpu.Op_none(A_NOP));
  1117. end
  1118. else
  1119. a_jmp_cond(list,OC_AE,hl);
  1120. end;
  1121. LOC_FLAGS:
  1122. begin
  1123. hflags:=ovloc.resflags;
  1124. inverse_flags(hflags);
  1125. cg.a_jmp_flags(list,hflags,hl);
  1126. end;
  1127. else
  1128. internalerror(200409281);
  1129. end;
  1130. a_call_name(list,'FPC_OVERFLOW',false);
  1131. a_label(list,hl);
  1132. end;
  1133. { *********** entry/exit code and address loading ************ }
  1134. procedure TCgSparc.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1135. begin
  1136. if nostackframe then
  1137. exit;
  1138. { Althogh the SPARC architecture require only word alignment, software
  1139. convention and the operating system require every stack frame to be double word
  1140. aligned }
  1141. LocalSize:=align(LocalSize,8);
  1142. { Execute the SAVE instruction to get a new register window and create a new
  1143. stack frame. In the "SAVE %i6,size,%i6" the first %i6 is related to the state
  1144. before execution of the SAVE instrucion so it is the caller %i6, when the %i6
  1145. after execution of that instruction is the called function stack pointer}
  1146. { constant can be 13 bit signed, since it's negative, size can be max. 4096 }
  1147. if LocalSize>4096 then
  1148. begin
  1149. a_load_const_reg(list,OS_ADDR,-LocalSize,NR_G1);
  1150. g1_used:=true;
  1151. list.concat(Taicpu.Op_reg_reg_reg(A_SAVE,NR_STACK_POINTER_REG,NR_G1,NR_STACK_POINTER_REG));
  1152. g1_used:=false;
  1153. end
  1154. else
  1155. list.concat(Taicpu.Op_reg_const_reg(A_SAVE,NR_STACK_POINTER_REG,-LocalSize,NR_STACK_POINTER_REG));
  1156. end;
  1157. procedure TCgSparc.g_maybe_got_init(list : TAsmList);
  1158. var
  1159. ref : treference;
  1160. begin
  1161. if (cs_create_pic in current_settings.moduleswitches) and
  1162. (pi_needs_got in current_procinfo.flags) then
  1163. begin
  1164. current_procinfo.got:=NR_L7;
  1165. { Set register $l7 to _GLOBAL_OFFSET_TABLE_ at function entry }
  1166. { The offsets -8 for %hi and -4 for %lo correspnod to the
  1167. code distance from the call to FPC_GETGOT inxtruction }
  1168. reference_reset_symbol(ref,current_asmdata.RefAsmSymbol('_GLOBAL_OFFSET_TABLE_'),-8,sizeof(pint));
  1169. ref.refaddr:=addr_high;
  1170. list.concat(taicpu.op_ref_reg(A_SETHI,ref,NR_L7));
  1171. ref.refaddr:=addr_low;
  1172. ref.offset:=-4;
  1173. list.concat(Taicpu.Op_reg_ref_reg(A_OR,NR_L7,ref,NR_L7));
  1174. list.concat(Taicpu.Op_sym(A_CALL,current_asmdata.RefAsmSymbol('FPC_GETGOT')));
  1175. { Delay slot }
  1176. list.concat(Taicpu.Op_none(A_NOP));
  1177. end;
  1178. end;
  1179. procedure TCgSparc.g_restore_registers(list:TAsmList);
  1180. begin
  1181. { The sparc port uses the sparc standard calling convetions so this function has no used }
  1182. end;
  1183. procedure TCgSparc.g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean);
  1184. var
  1185. hr : treference;
  1186. begin
  1187. if paramanager.ret_in_param(current_procinfo.procdef.returndef,current_procinfo.procdef.proccalloption) then
  1188. begin
  1189. reference_reset(hr,sizeof(pint));
  1190. hr.offset:=12;
  1191. hr.refaddr:=addr_full;
  1192. if nostackframe then
  1193. begin
  1194. hr.base:=NR_O7;
  1195. list.concat(taicpu.op_ref_reg(A_JMPL,hr,NR_G0));
  1196. list.concat(Taicpu.op_none(A_NOP))
  1197. end
  1198. else
  1199. begin
  1200. { We use trivial restore in the delay slot of the JMPL instruction, as we
  1201. already set result onto %i0 }
  1202. hr.base:=NR_I7;
  1203. list.concat(taicpu.op_ref_reg(A_JMPL,hr,NR_G0));
  1204. list.concat(Taicpu.op_none(A_RESTORE));
  1205. end;
  1206. end
  1207. else
  1208. begin
  1209. if nostackframe then
  1210. begin
  1211. { Here we need to use RETL instead of RET so it uses %o7 }
  1212. list.concat(Taicpu.op_none(A_RETL));
  1213. list.concat(Taicpu.op_none(A_NOP))
  1214. end
  1215. else
  1216. begin
  1217. { We use trivial restore in the delay slot of the JMPL instruction, as we
  1218. already set result onto %i0 }
  1219. list.concat(Taicpu.op_none(A_RET));
  1220. list.concat(Taicpu.op_none(A_RESTORE));
  1221. end;
  1222. end;
  1223. end;
  1224. procedure TCgSparc.g_save_registers(list : TAsmList);
  1225. begin
  1226. { The sparc port uses the sparc standard calling convetions so this function has no used }
  1227. end;
  1228. { ************* concatcopy ************ }
  1229. procedure tcgsparc.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  1230. var
  1231. paraloc1,paraloc2,paraloc3 : TCGPara;
  1232. begin
  1233. paraloc1.init;
  1234. paraloc2.init;
  1235. paraloc3.init;
  1236. paramanager.getintparaloc(pocall_default,1,voidpointertype,paraloc1);
  1237. paramanager.getintparaloc(pocall_default,2,voidpointertype,paraloc2);
  1238. paramanager.getintparaloc(pocall_default,3,ptrsinttype,paraloc3);
  1239. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  1240. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  1241. a_loadaddr_ref_cgpara(list,source,paraloc1);
  1242. paramanager.freecgpara(list,paraloc3);
  1243. paramanager.freecgpara(list,paraloc2);
  1244. paramanager.freecgpara(list,paraloc1);
  1245. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1246. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1247. a_call_name(list,'FPC_MOVE',false);
  1248. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1249. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1250. paraloc3.done;
  1251. paraloc2.done;
  1252. paraloc1.done;
  1253. end;
  1254. procedure TCgSparc.g_concatcopy(list:TAsmList;const source,dest:treference;len:tcgint);
  1255. var
  1256. tmpreg1,
  1257. hreg,
  1258. countreg: TRegister;
  1259. src, dst: TReference;
  1260. lab: tasmlabel;
  1261. count, count2: aint;
  1262. begin
  1263. if len>high(longint) then
  1264. internalerror(2002072704);
  1265. { anybody wants to determine a good value here :)? }
  1266. if len>100 then
  1267. g_concatcopy_move(list,source,dest,len)
  1268. else
  1269. begin
  1270. reference_reset(src,source.alignment);
  1271. reference_reset(dst,dest.alignment);
  1272. { load the address of source into src.base }
  1273. src.base:=GetAddressRegister(list);
  1274. a_loadaddr_ref_reg(list,source,src.base);
  1275. { load the address of dest into dst.base }
  1276. dst.base:=GetAddressRegister(list);
  1277. a_loadaddr_ref_reg(list,dest,dst.base);
  1278. { generate a loop }
  1279. count:=len div 4;
  1280. if count>4 then
  1281. begin
  1282. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1283. { have to be set to 8. I put an Inc there so debugging may be }
  1284. { easier (should offset be different from zero here, it will be }
  1285. { easy to notice in the generated assembler }
  1286. countreg:=GetIntRegister(list,OS_INT);
  1287. tmpreg1:=GetIntRegister(list,OS_INT);
  1288. a_load_const_reg(list,OS_INT,count,countreg);
  1289. { explicitely allocate R_O0 since it can be used safely here }
  1290. { (for holding date that's being copied) }
  1291. current_asmdata.getjumplabel(lab);
  1292. a_label(list, lab);
  1293. list.concat(taicpu.op_ref_reg(A_LD,src,tmpreg1));
  1294. list.concat(taicpu.op_reg_ref(A_ST,tmpreg1,dst));
  1295. list.concat(taicpu.op_reg_const_reg(A_ADD,src.base,4,src.base));
  1296. list.concat(taicpu.op_reg_const_reg(A_ADD,dst.base,4,dst.base));
  1297. list.concat(taicpu.op_reg_const_reg(A_SUBcc,countreg,1,countreg));
  1298. a_jmp_cond(list,OC_NE,lab);
  1299. list.concat(taicpu.op_none(A_NOP));
  1300. { keep the registers alive }
  1301. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  1302. list.concat(taicpu.op_reg_reg(A_MOV,src.base,src.base));
  1303. list.concat(taicpu.op_reg_reg(A_MOV,dst.base,dst.base));
  1304. len := len mod 4;
  1305. end;
  1306. { unrolled loop }
  1307. count:=len div 4;
  1308. if count>0 then
  1309. begin
  1310. tmpreg1:=GetIntRegister(list,OS_INT);
  1311. for count2 := 1 to count do
  1312. begin
  1313. list.concat(taicpu.op_ref_reg(A_LD,src,tmpreg1));
  1314. list.concat(taicpu.op_reg_ref(A_ST,tmpreg1,dst));
  1315. inc(src.offset,4);
  1316. inc(dst.offset,4);
  1317. end;
  1318. len := len mod 4;
  1319. end;
  1320. if (len and 4) <> 0 then
  1321. begin
  1322. hreg:=GetIntRegister(list,OS_INT);
  1323. a_load_ref_reg(list,OS_32,OS_32,src,hreg);
  1324. a_load_reg_ref(list,OS_32,OS_32,hreg,dst);
  1325. inc(src.offset,4);
  1326. inc(dst.offset,4);
  1327. end;
  1328. { copy the leftovers }
  1329. if (len and 2) <> 0 then
  1330. begin
  1331. hreg:=GetIntRegister(list,OS_INT);
  1332. a_load_ref_reg(list,OS_16,OS_16,src,hreg);
  1333. a_load_reg_ref(list,OS_16,OS_16,hreg,dst);
  1334. inc(src.offset,2);
  1335. inc(dst.offset,2);
  1336. end;
  1337. if (len and 1) <> 0 then
  1338. begin
  1339. hreg:=GetIntRegister(list,OS_INT);
  1340. a_load_ref_reg(list,OS_8,OS_8,src,hreg);
  1341. a_load_reg_ref(list,OS_8,OS_8,hreg,dst);
  1342. end;
  1343. end;
  1344. end;
  1345. procedure tcgsparc.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  1346. var
  1347. src, dst: TReference;
  1348. tmpreg1,
  1349. countreg: TRegister;
  1350. i : aint;
  1351. lab: tasmlabel;
  1352. begin
  1353. if len>31 then
  1354. g_concatcopy_move(list,source,dest,len)
  1355. else
  1356. begin
  1357. reference_reset(src,source.alignment);
  1358. reference_reset(dst,dest.alignment);
  1359. { load the address of source into src.base }
  1360. src.base:=GetAddressRegister(list);
  1361. a_loadaddr_ref_reg(list,source,src.base);
  1362. { load the address of dest into dst.base }
  1363. dst.base:=GetAddressRegister(list);
  1364. a_loadaddr_ref_reg(list,dest,dst.base);
  1365. { generate a loop }
  1366. if len>4 then
  1367. begin
  1368. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1369. { have to be set to 8. I put an Inc there so debugging may be }
  1370. { easier (should offset be different from zero here, it will be }
  1371. { easy to notice in the generated assembler }
  1372. countreg:=GetIntRegister(list,OS_INT);
  1373. tmpreg1:=GetIntRegister(list,OS_INT);
  1374. a_load_const_reg(list,OS_INT,len,countreg);
  1375. { explicitely allocate R_O0 since it can be used safely here }
  1376. { (for holding date that's being copied) }
  1377. current_asmdata.getjumplabel(lab);
  1378. a_label(list, lab);
  1379. list.concat(taicpu.op_ref_reg(A_LDUB,src,tmpreg1));
  1380. list.concat(taicpu.op_reg_ref(A_STB,tmpreg1,dst));
  1381. list.concat(taicpu.op_reg_const_reg(A_ADD,src.base,1,src.base));
  1382. list.concat(taicpu.op_reg_const_reg(A_ADD,dst.base,1,dst.base));
  1383. list.concat(taicpu.op_reg_const_reg(A_SUBcc,countreg,1,countreg));
  1384. a_jmp_cond(list,OC_NE,lab);
  1385. list.concat(taicpu.op_none(A_NOP));
  1386. { keep the registers alive }
  1387. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  1388. list.concat(taicpu.op_reg_reg(A_MOV,src.base,src.base));
  1389. list.concat(taicpu.op_reg_reg(A_MOV,dst.base,dst.base));
  1390. end
  1391. else
  1392. begin
  1393. { unrolled loop }
  1394. tmpreg1:=GetIntRegister(list,OS_INT);
  1395. for i:=1 to len do
  1396. begin
  1397. list.concat(taicpu.op_ref_reg(A_LDUB,src,tmpreg1));
  1398. list.concat(taicpu.op_reg_ref(A_STB,tmpreg1,dst));
  1399. inc(src.offset);
  1400. inc(dst.offset);
  1401. end;
  1402. end;
  1403. end;
  1404. end;
  1405. procedure tcgsparc.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1406. var
  1407. make_global : boolean;
  1408. href : treference;
  1409. begin
  1410. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  1411. Internalerror(200006137);
  1412. if not assigned(procdef.struct) or
  1413. (procdef.procoptions*[po_classmethod, po_staticmethod,
  1414. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  1415. Internalerror(200006138);
  1416. if procdef.owner.symtabletype<>ObjectSymtable then
  1417. Internalerror(200109191);
  1418. make_global:=false;
  1419. if (not current_module.is_unit) or create_smartlink or
  1420. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  1421. make_global:=true;
  1422. if make_global then
  1423. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  1424. else
  1425. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  1426. { set param1 interface to self }
  1427. g_adjust_self_value(list,procdef,ioffset);
  1428. if (po_virtualmethod in procdef.procoptions) and
  1429. not is_objectpascal_helper(procdef.struct) then
  1430. begin
  1431. if (procdef.extnumber=$ffff) then
  1432. Internalerror(200006139);
  1433. { mov 0(%rdi),%rax ; load vmt}
  1434. reference_reset_base(href,NR_O0,0,sizeof(pint));
  1435. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_G1);
  1436. g1_used:=true;
  1437. { jmp *vmtoffs(%eax) ; method offs }
  1438. reference_reset_base(href,NR_G1,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  1439. list.concat(taicpu.op_ref_reg(A_LD,href,NR_G1));
  1440. list.concat(taicpu.op_reg(A_JMP,NR_G1));
  1441. g1_used:=false;
  1442. end
  1443. else
  1444. begin
  1445. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(procdef.mangledname),0,sizeof(pint));
  1446. href.refaddr := addr_high;
  1447. list.concat(taicpu.op_ref_reg(A_SETHI,href,NR_G1));
  1448. g1_used:=true;
  1449. href.refaddr := addr_low;
  1450. list.concat(taicpu.op_reg_ref_reg(A_OR,NR_G1,href,NR_G1));
  1451. { FIXME: this assumes for now that %l7 already has the correct value }
  1452. if (cs_create_pic in current_settings.moduleswitches) then
  1453. begin
  1454. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_G1,NR_L7,NR_G1));
  1455. reference_reset_base(href,NR_G1,0,sizeof(pint));
  1456. list.concat(taicpu.op_ref_reg(A_LD,href,NR_G1));
  1457. end;
  1458. list.concat(taicpu.op_reg(A_JMP,NR_G1));
  1459. g1_used:=false;
  1460. end;
  1461. { Delay slot }
  1462. list.Concat(TAiCpu.Op_none(A_NOP));
  1463. List.concat(Tai_symbol_end.Createname(labelname));
  1464. end;
  1465. procedure tcgsparc.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1466. begin
  1467. Comment(V_Error,'tcgsparc.g_stackpointer_alloc method not implemented');
  1468. end;
  1469. procedure tcgsparc.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: TCGSize; src, dst: TRegister);
  1470. begin
  1471. Comment(V_Error,'tcgsparc.a_bit_scan_reg_reg method not implemented');
  1472. end;
  1473. {****************************************************************************
  1474. TCG64Sparc
  1475. ****************************************************************************}
  1476. procedure tcg64sparc.a_load64_reg_ref(list : TAsmList;reg : tregister64;const ref : treference);
  1477. var
  1478. tmpref: treference;
  1479. begin
  1480. { Override this function to prevent loading the reference twice }
  1481. tmpref:=ref;
  1482. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reghi,tmpref);
  1483. inc(tmpref.offset,4);
  1484. cg.a_load_reg_ref(list,OS_32,OS_32,reg.reglo,tmpref);
  1485. end;
  1486. procedure tcg64sparc.a_load64_ref_reg(list : TAsmList;const ref : treference;reg : tregister64);
  1487. var
  1488. tmpref: treference;
  1489. begin
  1490. { Override this function to prevent loading the reference twice }
  1491. tmpref:=ref;
  1492. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reghi);
  1493. inc(tmpref.offset,4);
  1494. cg.a_load_ref_reg(list,OS_32,OS_32,tmpref,reg.reglo);
  1495. end;
  1496. procedure tcg64sparc.a_load64_ref_cgpara(list : TAsmList;const r : treference;const paraloc : tcgpara);
  1497. var
  1498. hreg64 : tregister64;
  1499. begin
  1500. { Override this function to prevent loading the reference twice.
  1501. Use here some extra registers, but those are optimized away by the RA }
  1502. hreg64.reglo:=cg.GetIntRegister(list,OS_32);
  1503. hreg64.reghi:=cg.GetIntRegister(list,OS_32);
  1504. a_load64_ref_reg(list,r,hreg64);
  1505. a_load64_reg_cgpara(list,hreg64,paraloc);
  1506. end;
  1507. procedure TCg64Sparc.get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp;checkoverflow : boolean);
  1508. begin
  1509. case op of
  1510. OP_ADD :
  1511. begin
  1512. op1:=A_ADDCC;
  1513. if checkoverflow then
  1514. op2:=A_ADDXCC
  1515. else
  1516. op2:=A_ADDX;
  1517. end;
  1518. OP_SUB :
  1519. begin
  1520. op1:=A_SUBCC;
  1521. if checkoverflow then
  1522. op2:=A_SUBXCC
  1523. else
  1524. op2:=A_SUBX;
  1525. end;
  1526. OP_XOR :
  1527. begin
  1528. op1:=A_XOR;
  1529. op2:=A_XOR;
  1530. end;
  1531. OP_OR :
  1532. begin
  1533. op1:=A_OR;
  1534. op2:=A_OR;
  1535. end;
  1536. OP_AND :
  1537. begin
  1538. op1:=A_AND;
  1539. op2:=A_AND;
  1540. end;
  1541. else
  1542. internalerror(200203241);
  1543. end;
  1544. end;
  1545. procedure TCg64Sparc.a_op64_reg_reg(list:TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst:TRegister64);
  1546. var
  1547. op1,op2 : TAsmOp;
  1548. begin
  1549. case op of
  1550. OP_NEG :
  1551. begin
  1552. { Use the simple code: y=0-z }
  1553. list.concat(taicpu.op_reg_reg_reg(A_SUBcc,NR_G0,regsrc.reglo,regdst.reglo));
  1554. list.concat(taicpu.op_reg_reg_reg(A_SUBX,NR_G0,regsrc.reghi,regdst.reghi));
  1555. exit;
  1556. end;
  1557. OP_NOT :
  1558. begin
  1559. list.concat(taicpu.op_reg_reg_reg(A_XNOR,regsrc.reglo,NR_G0,regdst.reglo));
  1560. list.concat(taicpu.op_reg_reg_reg(A_XNOR,regsrc.reghi,NR_G0,regdst.reghi));
  1561. exit;
  1562. end;
  1563. end;
  1564. get_64bit_ops(op,op1,op2,false);
  1565. list.concat(taicpu.op_reg_reg_reg(op1,regdst.reglo,regsrc.reglo,regdst.reglo));
  1566. list.concat(taicpu.op_reg_reg_reg(op2,regdst.reghi,regsrc.reghi,regdst.reghi));
  1567. end;
  1568. procedure TCg64Sparc.a_op64_const_reg(list:TAsmList;op:TOpCG;size : tcgsize;value:int64;regdst:TRegister64);
  1569. var
  1570. op1,op2:TAsmOp;
  1571. begin
  1572. case op of
  1573. OP_NEG,
  1574. OP_NOT :
  1575. internalerror(200306017);
  1576. end;
  1577. get_64bit_ops(op,op1,op2,false);
  1578. tcgsparc(cg).handle_reg_const_reg(list,op1,regdst.reglo,tcgint(lo(value)),regdst.reglo);
  1579. tcgsparc(cg).handle_reg_const_reg(list,op2,regdst.reghi,tcgint(hi(value)),regdst.reghi);
  1580. end;
  1581. procedure tcg64sparc.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64; regsrc,regdst : tregister64);
  1582. var
  1583. l : tlocation;
  1584. begin
  1585. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,l);
  1586. end;
  1587. procedure tcg64sparc.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  1588. var
  1589. l : tlocation;
  1590. begin
  1591. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,l);
  1592. end;
  1593. procedure tcg64sparc.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  1594. var
  1595. op1,op2:TAsmOp;
  1596. begin
  1597. case op of
  1598. OP_NEG,
  1599. OP_NOT :
  1600. internalerror(200306017);
  1601. end;
  1602. get_64bit_ops(op,op1,op2,setflags);
  1603. tcgsparc(cg).handle_reg_const_reg(list,op1,regsrc.reglo,tcgint(lo(value)),regdst.reglo);
  1604. tcgsparc(cg).handle_reg_const_reg(list,op2,regsrc.reghi,tcgint(hi(value)),regdst.reghi);
  1605. end;
  1606. procedure tcg64sparc.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  1607. var
  1608. op1,op2:TAsmOp;
  1609. begin
  1610. case op of
  1611. OP_NEG,
  1612. OP_NOT :
  1613. internalerror(200306017);
  1614. end;
  1615. get_64bit_ops(op,op1,op2,setflags);
  1616. list.concat(taicpu.op_reg_reg_reg(op1,regsrc2.reglo,regsrc1.reglo,regdst.reglo));
  1617. list.concat(taicpu.op_reg_reg_reg(op2,regsrc2.reghi,regsrc1.reghi,regdst.reghi));
  1618. end;
  1619. procedure create_codegen;
  1620. begin
  1621. cg:=TCgSparc.Create;
  1622. if target_info.system=system_sparc_linux then
  1623. TCgSparc(cg).use_unlimited_pic_mode:=true
  1624. else
  1625. TCgSparc(cg).use_unlimited_pic_mode:=false;
  1626. cg64:=TCg64Sparc.Create;
  1627. end;
  1628. end.