cgcpu.pas 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. {******************************************************************************
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  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. UNIT cgcpu;
  17. {This unit implements the code generator for the SPARC architecture}
  18. {$INCLUDE fpcdefs.inc}
  19. INTERFACE
  20. USES
  21. cginfo,cgbase,cgobj,cg64f32,
  22. aasmbase,aasmtai,aasmcpu,
  23. cpubase,cpuinfo,cpupara,
  24. node,symconst;
  25. TYPE
  26. TCgSparc=CLASS(tcg)
  27. {This method is used to pass a parameter, which is located in a register, to a
  28. routine. It should give the parameter to the routine, as required by the
  29. specific processor ABI. It is overriden for each CPU target.
  30. Size : is the size of the operand in the register
  31. r : is the register source of the operand
  32. LocPara : is the location where the parameter will be stored}
  33. procedure a_param_reg(list:TAasmOutput;sz:tcgsize;r:tregister;const LocPara:TParaLocation);override;
  34. {passes a parameter which is a constant to a function}
  35. procedure a_param_const(list:TAasmOutput;size:tcgsize;a:aword;CONST LocPara:TParaLocation);override;
  36. procedure a_param_ref(list:TAasmOutput;sz:tcgsize;CONST r:TReference;CONST LocPara:TParaLocation);override;
  37. procedure a_paramaddr_ref(list:TAasmOutput;CONST r:TReference;CONST LocPara:TParaLocation);override;
  38. procedure a_call_name(list:TAasmOutput;CONST s:string);override;
  39. procedure a_call_ref(list:TAasmOutput;CONST ref:TReference);override;
  40. procedure a_call_reg(list:TAasmOutput;Reg:TRegister);override;
  41. {Branch Instruction}
  42. procedure a_jmp_always(List:TAasmOutput;l:TAsmLabel);override;
  43. {General purpose instyructions}
  44. procedure a_op_const_reg(list:TAasmOutput;Op:TOpCG;a:AWord;reg:TRegister);override;
  45. procedure a_op_const_ref(list:TAasmOutput;Op:TOpCG;size:TCGSize;a:AWord;CONST ref:TReference);override;
  46. procedure a_op_reg_reg(list:TAasmOutput;Op:TOpCG;size:TCGSize;src, dst:TRegister);override;
  47. procedure a_op_ref_reg(list:TAasmOutput;Op:TOpCG;size:TCGSize;CONST ref:TReference;reg:TRegister);override;
  48. procedure a_op_reg_ref(list:TAasmOutput;Op:TOpCG;size:TCGSize;reg:TRegister;CONST ref:TReference);override;
  49. procedure a_op_const_reg_reg(list:TAasmOutput;op:TOpCg;size:tcgsize;a:aword;src, dst:tregister);override;
  50. procedure a_op_reg_reg_reg(list:TAasmOutput;op:TOpCg;size:tcgsize;src1, src2, dst:tregister);override;
  51. { move instructions }
  52. procedure a_load_const_reg(list:TAasmOutput;size:tcgsize;a:aword;reg:tregister);override;
  53. procedure a_load_const_ref(list:TAasmOutput;size:tcgsize;a:aword;CONST ref:TReference);override;
  54. procedure a_load_reg_ref(list:TAasmOutput;size:tcgsize;reg:tregister;CONST ref:TReference);override;
  55. procedure a_load_ref_reg(list:TAasmOutput;size:tcgsize;CONST ref:TReference;reg:tregister);override;
  56. procedure a_load_reg_reg(list:TAasmOutput;fromsize,tosize:tcgsize;reg1,reg2:tregister);override;
  57. procedure a_loadaddr_ref_reg(list:TAasmOutput;CONST ref:TReference;r:tregister);override;
  58. { fpu move instructions }
  59. procedure a_loadfpu_reg_reg(list:TAasmOutput;reg1, reg2:tregister);override;
  60. procedure a_loadfpu_ref_reg(list:TAasmOutput;size:tcgsize;CONST ref:TReference;reg:tregister);override;
  61. procedure a_loadfpu_reg_ref(list:TAasmOutput;size:tcgsize;reg:tregister;CONST ref:TReference);override;
  62. { vector register move instructions }
  63. procedure a_loadmm_reg_reg(list:TAasmOutput;reg1, reg2:tregister);override;
  64. procedure a_loadmm_ref_reg(list:TAasmOutput;CONST ref:TReference;reg:tregister);override;
  65. procedure a_loadmm_reg_ref(list:TAasmOutput;reg:tregister;CONST ref:TReference);override;
  66. procedure a_parammm_reg(list:TAasmOutput;reg:tregister);override;
  67. { comparison operations }
  68. procedure a_cmp_const_reg_label(list:TAasmOutput;size:tcgsize;cmp_op:topcmp;a:aword;reg:tregister;l:tasmlabel);override;
  69. procedure a_cmp_const_ref_label(list:TAasmOutput;size:tcgsize;cmp_op:topcmp;a:aword;CONST ref:TReference;l:tasmlabel);override;
  70. procedure a_cmp_reg_reg_label(list:TAasmOutput;size:tcgsize;cmp_op:topcmp;reg1,reg2:tregister;l:tasmlabel);override;
  71. procedure a_cmp_ref_reg_label(list:TAasmOutput;size:tcgsize;cmp_op:topcmp;CONST ref:TReference;reg:tregister;l:tasmlabel);override;
  72. procedure a_jmp_cond(list:TAasmOutput;cond:TOpCmp;l:tasmlabel);{ override;}
  73. procedure a_jmp_flags(list:TAasmOutput;CONST f:TResFlags;l:tasmlabel);override;
  74. procedure g_flags2reg(list:TAasmOutput;Size:TCgSize;CONST f:tresflags;reg:TRegister);override;
  75. procedure g_overflowCheck(List:TAasmOutput;const p:TNode);override;
  76. procedure g_stackframe_entry(list:TAasmOutput;localsize:LongInt);override;
  77. procedure g_restore_all_registers(list:TAasmOutput;selfused,accused,acchiused:boolean);override;
  78. procedure g_restore_frame_pointer(list:TAasmOutput);override;
  79. procedure g_restore_standard_registers(list:taasmoutput;usedinproc:Tsupregset);override;
  80. procedure g_return_from_proc(list:TAasmOutput;parasize:aword);override;
  81. procedure g_save_all_registers(list : taasmoutput);override;
  82. procedure g_save_standard_registers(list : taasmoutput; usedinproc : Tsupregset);override;
  83. procedure g_concatcopy(list:TAasmOutput;CONST source,dest:TReference;len:aword;delsource,loadref:boolean);override;
  84. class function reg_cgsize(CONST reg:tregister):tcgsize;override;
  85. PRIVATE
  86. function IsSimpleRef(const ref:treference):boolean;
  87. procedure sizes2load(s1:tcgsize;s2:topsize;var op:tasmop;var s3:topsize);
  88. procedure floatload(list:TAasmOutput;t:tcgsize;CONST ref:TReference);
  89. procedure floatstore(list:TAasmOutput;t:tcgsize;CONST ref:TReference);
  90. procedure floatloadops(t:tcgsize;var op:tasmop;var s:topsize);
  91. procedure floatstoreops(t:tcgsize;var op:tasmop;var s:topsize);
  92. END;
  93. TCg64Sparc=class(tcg64f32)
  94. procedure a_op64_ref_reg(list:TAasmOutput;op:TOpCG;CONST ref:TReference;reg:TRegister64);override;
  95. procedure a_op64_reg_reg(list:TAasmOutput;op:TOpCG;regsrc,regdst:TRegister64);override;
  96. procedure a_op64_const_reg(list:TAasmOutput;op:TOpCG;value:qWord;regdst:TRegister64);override;
  97. procedure a_op64_const_ref(list:TAasmOutput;op:TOpCG;value:qWord;CONST ref:TReference);override;
  98. procedure get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  99. END;
  100. CONST
  101. TOpCG2AsmOp:ARRAY[topcg]OF TAsmOp=(A_NONE,A_ADD,A_AND,A_UDIV,A_SDIV,A_UMUL, A_SMUL, A_NEG,A_NOT,A_OR,A_not,A_not,A_not,A_SUB,A_XOR);
  102. TOpCmp2AsmCond:ARRAY[topcmp]OF TAsmCond=(C_NONE,C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  103. TCGSize2OpSize:ARRAY[tcgsize]OF TOpSize=(S_NO,S_B,S_W,S_SW,S_SW,S_B,S_W,S_SW,S_SW,S_FS,S_FD,S_FQ,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO);
  104. IMPLEMENTATION
  105. USES
  106. globtype,globals,verbose,systems,cutils,
  107. symdef,symsym,defutil,paramgr,
  108. rgobj,tgobj,rgcpu,cpupi;
  109. procedure TCgSparc.a_param_reg(list:TAasmOutput;sz:tcgsize;r:tregister;const LocPara:TParaLocation);
  110. var
  111. r2:Tregister;
  112. begin
  113. r2.enum:=R_G0;
  114. with list,LocPara do
  115. case Loc of
  116. LOC_REGISTER:
  117. case Sz of
  118. OS_8,OS_S8:
  119. Concat(taicpu.op_Reg_Const_Reg(A_AND,r,$FF,Register));
  120. OS_16,OS_S16:
  121. begin
  122. Concat(taicpu.op_Reg_Reg_Reg(A_AND,r,r2,Register));
  123. {This will put 00...00111 in the hiest 22 bits of the reg}
  124. Concat(taicpu.op_Reg_Const_Reg(A_SETHI,Register,$7,Register));
  125. end;
  126. OS_32,OS_S32:
  127. if r.enum<>Register.enum
  128. then
  129. Concat(taicpu.op_Reg_Reg_Reg(A_OR,r,r2,Register));
  130. else
  131. InternalError(2002032212);
  132. end;
  133. else
  134. InternalError(2002101002);
  135. end;
  136. end;
  137. procedure TCgSparc.a_param_const(list:TAasmOutput;size:tcgsize;a:aword;CONST LocPara:TParaLocation);
  138. var
  139. Ref:TReference;
  140. begin
  141. with List do
  142. case locpara.loc of
  143. LOC_REGISTER,LOC_CREGISTER:
  144. a_load_const_reg(list,size,a,locpara.register);
  145. LOC_REFERENCE:
  146. begin
  147. reference_reset(ref);
  148. ref.base:=locpara.reference.index;
  149. ref.offset:=locpara.reference.offset;
  150. a_load_const_ref(list,size,a,ref);
  151. end;
  152. else
  153. InternalError(2002122200);
  154. end;
  155. if locpara.sp_fixup<>0
  156. then
  157. InternalError(2002122201);
  158. end;
  159. procedure TCgSparc.a_param_ref(list:TAasmOutput;sz:TCgSize;const r:TReference;const LocPara:TParaLocation);
  160. var
  161. ref: treference;
  162. tmpreg:TRegister;
  163. begin
  164. with LocPara do
  165. case locpara.loc of
  166. LOC_REGISTER,LOC_CREGISTER:
  167. a_load_ref_reg(list,sz,r,Register);
  168. LOC_REFERENCE:
  169. begin
  170. {Code conventions need the parameters being allocated in %o6+92. See
  171. comment on g_stack_frame}
  172. if locpara.sp_fixup<92
  173. then
  174. InternalError(2002081104);
  175. reference_reset(ref);
  176. ref.base:=locpara.reference.index;
  177. ref.offset:=locpara.reference.offset;
  178. tmpreg := get_scratch_reg_int(list,sz);
  179. a_load_ref_reg(list,sz,r,tmpreg);
  180. a_load_reg_ref(list,sz,tmpreg,ref);
  181. free_scratch_reg(list,tmpreg);
  182. end;
  183. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  184. case sz of
  185. OS_32:
  186. a_loadfpu_ref_reg(list,OS_F32,r,locpara.register);
  187. OS_64:
  188. a_loadfpu_ref_reg(list,OS_F64,r,locpara.register);
  189. else
  190. internalerror(2002072801);
  191. end;
  192. else
  193. internalerror(2002081103);
  194. end;
  195. end;
  196. procedure TCgSparc.a_paramaddr_ref(list:TAasmOutput;CONST r:TReference;CONST LocPara:TParaLocation);
  197. VAR
  198. tmpreg:TRegister;
  199. BEGIN
  200. IF r.segment.enum<>R_NO
  201. THEN
  202. CGMessage(cg_e_cant_use_far_pointer_there);
  203. IF(r.base.enum=R_NO)AND(r.index.enum=R_NO)
  204. THEN
  205. list.concat(Taicpu.Op_sym_ofs(A_LD,S_SW,r.symbol,r.offset))
  206. ELSE IF(r.base.enum=R_NO)AND(r.index.enum<>R_NO)AND
  207. (r.offset=0)AND(r.scalefactor=0)AND(r.symbol=nil)
  208. THEN
  209. list.concat(Taicpu.Op_reg(A_LD,r.index))
  210. ELSE IF(r.base.enum<>R_NO)AND(r.index.enum=R_NO)AND
  211. (r.offset=0)AND(r.symbol=nil)
  212. THEN
  213. list.concat(Taicpu.Op_reg(A_LD,r.base))
  214. ELSE
  215. BEGIN
  216. tmpreg:=get_scratch_reg_address(list);
  217. a_loadaddr_ref_reg(list,r,tmpreg);
  218. list.concat(taicpu.op_reg(A_LD,tmpreg));
  219. free_scratch_reg(list,tmpreg);
  220. END;
  221. END;
  222. procedure TCgSparc.a_call_name(list:TAasmOutput;CONST s:string);
  223. BEGIN
  224. WITH List,objectlibrary DO
  225. BEGIN
  226. concat(taicpu.op_sym(A_CALL,S_SW,newasmsymbol(s)));
  227. concat(taicpu.op_none(A_NOP));
  228. END;
  229. END;
  230. procedure TCgSparc.a_call_ref(list:TAasmOutput;CONST ref:TReference);
  231. begin
  232. list.concat(taicpu.op_ref(A_CALL,ref));
  233. list.concat(taicpu.op_none(A_NOP));
  234. end;
  235. procedure TCgSparc.a_call_reg(list:TAasmOutput;Reg:TRegister);
  236. begin
  237. list.concat(taicpu.op_reg(A_JMPL,reg));
  238. if target_info.system=system_sparc_linux
  239. then
  240. list.concat(taicpu.op_none(A_NOP));
  241. procinfo.flags:=procinfo.flags or pi_do_call;
  242. end;
  243. {********************** branch instructions ********************}
  244. procedure TCgSparc.a_jmp_always(List:TAasmOutput;l:TAsmLabel);
  245. begin
  246. List.Concat(TAiCpu.op_sym(A_BA,S_NO,objectlibrary.newasmsymbol(l.name)));
  247. end;
  248. {********************** load instructions ********************}
  249. procedure TCgSparc.a_load_const_reg(list:TAasmOutput;size:TCGSize;a:aword;reg:TRegister);
  250. var r:Tregister;
  251. BEGIN
  252. r.enum:=R_G0;
  253. WITH List DO
  254. IF a<>0
  255. THEN{R_G0 is usually set to zero, so we use it}
  256. Concat(taicpu.op_reg_const_reg(A_OR,r,a,reg))
  257. ELSE{The is no A_MOV in sparc, that's why we use A_OR with help of R_G0}
  258. Concat(taicpu.op_reg_reg_reg(A_OR,r,r,reg));
  259. END;
  260. procedure TCgSparc.a_load_const_ref(list:TAasmOutput;size:tcgsize;a:aword;CONST ref:TReference);
  261. var r:Tregister;
  262. BEGIN
  263. r.enum:=R_G0;
  264. WITH List DO
  265. IF a=0
  266. THEN
  267. Concat(taicpu.op_reg_ref(A_ST,r,Ref))
  268. ELSE
  269. BEGIN
  270. r.enum:=R_G1;
  271. a_load_const_reg(list,size,a,r);
  272. a_load_reg_ref(list,size,r,Ref);
  273. END;
  274. END;
  275. procedure TCgSparc.a_load_reg_ref(list:TAasmOutput;size:TCGSize;reg:tregister;const Ref:TReference);
  276. var
  277. op:tasmop;
  278. begin
  279. case size of
  280. { signed integer registers }
  281. OS_S8:
  282. Op:=A_STB;{Store Signed Byte}
  283. OS_S16:
  284. Op:=A_STH;{Store Signed Halfword}
  285. OS_S32:
  286. Op:=A_ST;{Store Word}
  287. OS_S64:
  288. Op:=A_STD;{Store Double Word}
  289. { unsigned integer registers }
  290. //A_STSTUB;{Store-Store Unsigned Byte}
  291. OS_8:
  292. Op:=A_STB;{Store Unsigned Bye}
  293. OS_16:
  294. Op:=A_STH;{Store Unsigned Halfword}
  295. OS_32:
  296. Op:=A_ST;{Store Word}
  297. OS_64:
  298. Op:=A_STD;{Store Double Word}
  299. { floating-point real registers }
  300. OS_F32:
  301. Op:=A_STF;{Store Floating-point word}
  302. //A_STFSR
  303. OS_F64:
  304. Op:=A_STDF;{Store Double Floating-point word}
  305. //A_STC;{Store Coprocessor}
  306. //A_STCSR;
  307. //A_STDC;{Store Double Coprocessor}
  308. else
  309. InternalError(2002122100);
  310. end;
  311. with list do
  312. concat(taicpu.op_reg_ref(op,reg,ref));
  313. end;
  314. procedure TCgSparc.a_load_ref_reg(list:TAasmOutput;size:TCgSize;const ref:TReference;reg:tregister);
  315. var
  316. op:tasmop;
  317. begin
  318. case size of
  319. { signed integer registers }
  320. OS_S8:
  321. Op:=A_LDSB;{Load Signed Byte}
  322. OS_S16:
  323. Op:=A_LDSH;{Load Signed Halfword}
  324. OS_S32:
  325. Op:=A_LD;{Load Word}
  326. OS_S64:
  327. Op:=A_LDD;{Load Double Word}
  328. { unsigned integer registers }
  329. //A_LDSTUB;{Load-Store Unsigned Byte}
  330. OS_8:
  331. Op:=A_LDUB;{Load Unsigned Bye}
  332. OS_16:
  333. Op:=A_LDUH;{Load Unsigned Halfword}
  334. OS_32:
  335. Op:=A_LD;{Load Word}
  336. OS_64:
  337. Op:=A_LDD;{Load Double Word}
  338. { floating-point real registers }
  339. OS_F32:
  340. Op:=A_LDF;{Load Floating-point word}
  341. //A_LDFSR
  342. OS_F64:
  343. Op:=A_LDDF;{Load Double Floating-point word}
  344. //A_LDC;{Load Coprocessor}
  345. //A_LDCSR;
  346. //A_LDDC;{Load Double Coprocessor}
  347. else
  348. InternalError(2002122100);
  349. end;
  350. with list do
  351. concat(taicpu.op_ref_reg(op,ref,reg));
  352. end;
  353. procedure TCgSparc.a_load_reg_reg(list:TAasmOutput;fromsize,tosize:tcgsize;reg1,reg2:tregister);
  354. var
  355. op:tasmop;
  356. s:topsize;
  357. r:Tregister;
  358. begin
  359. r.enum:=R_G0;
  360. if(reg1.enum<>reg2.enum)or
  361. (tcgsize2size[tosize]<tcgsize2size[fromsize])or
  362. ((tcgsize2size[tosize] = tcgsize2size[fromsize])and
  363. (tosize <> fromsize)and
  364. not(fromsize in [OS_32,OS_S32]))
  365. then
  366. with list do
  367. case fromsize of
  368. OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32:
  369. concat(taicpu.op_reg_reg_reg(A_OR,r,reg1,reg2));
  370. else internalerror(2002090901);
  371. end;
  372. end;
  373. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  374. { R_ST means "the current value at the top of the fpu stack" (JM) }
  375. procedure TCgSparc.a_loadfpu_reg_reg(list:TAasmOutput;reg1, reg2:tregister);
  376. begin
  377. { if NOT (reg1 IN [R_F0..R_F31]) then
  378. begin
  379. list.concat(taicpu.op_reg(A_NONE,S_NO,
  380. trgcpu(rg).correct_fpuregister(reg1,trgcpu(rg).fpuvaroffset)));
  381. inc(trgcpu(rg).fpuvaroffset);
  382. end;
  383. if NOT (reg2 IN [R_F0..R_F31]) then
  384. begin
  385. list.concat(taicpu.op_reg(A_JMPL,S_NO,
  386. trgcpu(rg).correct_fpuregister(reg2,trgcpu(rg).fpuvaroffset)));
  387. dec(trgcpu(rg).fpuvaroffset);
  388. end;}
  389. end;
  390. procedure TCgSparc.a_loadfpu_ref_reg(list:TAasmOutput;size:tcgsize;CONST ref:TReference;reg:tregister);
  391. begin
  392. floatload(list,size,ref);
  393. { if (reg <> R_ST) then
  394. a_loadfpu_reg_reg(list,R_ST,reg);}
  395. end;
  396. procedure TCgSparc.a_loadfpu_reg_ref(list:TAasmOutput;size:tcgsize;reg:tregister;CONST ref:TReference);
  397. begin
  398. { if reg <> R_ST then
  399. a_loadfpu_reg_reg(list,reg,R_ST);}
  400. floatstore(list,size,ref);
  401. end;
  402. procedure TCgSparc.a_loadmm_reg_reg(list:TAasmOutput;reg1, reg2:tregister);
  403. begin
  404. // list.concat(taicpu.op_reg_reg(A_NONEQ,S_NO,reg1,reg2));
  405. end;
  406. procedure TCgSparc.a_loadmm_ref_reg(list:TAasmOutput;CONST ref:TReference;reg:tregister);
  407. begin
  408. // list.concat(taicpu.op_ref_reg(A_NONEQ,S_NO,ref,reg));
  409. end;
  410. procedure TCgSparc.a_loadmm_reg_ref(list:TAasmOutput;reg:tregister;CONST ref:TReference);
  411. begin
  412. // list.concat(taicpu.op_reg_ref(A_NONEQ,S_NO,reg,ref));
  413. end;
  414. procedure TCgSparc.a_parammm_reg(list:TAasmOutput;reg:tregister);
  415. VAR
  416. href:TReference;
  417. BEGIN
  418. // list.concat(taicpu.op_const_reg(A_SUB,S_SW,8,R_RSP));
  419. // reference_reset_base(href,R_ESP,0);
  420. // list.concat(taicpu.op_reg_ref(A_NONEQ,S_NO,reg,href));
  421. END;
  422. procedure TCgSparc.a_op_const_reg(list:TAasmOutput;Op:TOpCG;a:AWord;reg:TRegister);
  423. var
  424. opcode:tasmop;
  425. power:LongInt;
  426. begin
  427. (* Case Op of
  428. OP_DIV, OP_IDIV:
  429. Begin
  430. if ispowerof2(a,power) then
  431. begin
  432. case op of
  433. OP_DIV:
  434. opcode := A_SHR;
  435. OP_IDIV:
  436. opcode := A_SAR;
  437. end;
  438. list.concat(taicpu.op_const_reg(opcode,S_SW,power,
  439. reg));
  440. exit;
  441. end;
  442. { the rest should be handled specifically in the code }
  443. { generator because of the silly register usage restraints }
  444. internalerror(200109224);
  445. End;
  446. OP_MUL,OP_IMUL:
  447. begin
  448. if not(cs_check_overflow in aktlocalswitches) and
  449. ispowerof2(a,power) then
  450. begin
  451. list.concat(taicpu.op_const_reg(A_SHL,S_SW,power,
  452. reg));
  453. exit;
  454. end;
  455. if op = OP_IMUL then
  456. list.concat(taicpu.op_const_reg(A_IMUL,S_SW,
  457. a,reg))
  458. else
  459. { OP_MUL should be handled specifically in the code }
  460. { generator because of the silly register usage restraints }
  461. internalerror(200109225);
  462. end;
  463. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  464. if not(cs_check_overflow in aktlocalswitches) and
  465. (a = 1) and
  466. (op in [OP_ADD,OP_SUB]) then
  467. if op = OP_ADD then
  468. list.concat(taicpu.op_reg(A_INC,S_SW,reg))
  469. else
  470. list.concat(taicpu.op_reg(A_DEC,S_SW,reg))
  471. else if (a = 0) then
  472. if (op <> OP_AND) then
  473. exit
  474. else
  475. list.concat(taicpu.op_const_reg(A_NONE,S_SW,0,reg))
  476. else if (a = high(aword)) and
  477. (op in [OP_AND,OP_OR,OP_XOR]) then
  478. begin
  479. case op of
  480. OP_AND:
  481. exit;
  482. OP_OR:
  483. list.concat(taicpu.op_const_reg(A_NONE,S_SW,high(aword),reg));
  484. OP_XOR:
  485. list.concat(taicpu.op_reg(A_NOT,S_SW,reg));
  486. end
  487. end
  488. else
  489. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],S_SW,
  490. a,reg));
  491. OP_SHL,OP_SHR,OP_SAR:
  492. begin
  493. if (a and 31) <> 0 Then
  494. list.concat(taicpu.op_const_reg(
  495. TOpCG2AsmOp[op],S_SW,a and 31,reg));
  496. if (a shr 5) <> 0 Then
  497. internalerror(68991);
  498. end
  499. else internalerror(68992);
  500. end;*)
  501. end;
  502. procedure TCgSparc.a_op_const_ref(list:TAasmOutput;Op:TOpCG;size:TCGSize;a:AWord;CONST ref:TReference);
  503. var
  504. opcode:tasmop;
  505. power:LongInt;
  506. begin
  507. (* Case Op of
  508. OP_DIV, OP_IDIV:
  509. Begin
  510. if ispowerof2(a,power) then
  511. begin
  512. case op of
  513. OP_DIV:
  514. opcode := A_SHR;
  515. OP_IDIV:
  516. opcode := A_SAR;
  517. end;
  518. list.concat(taicpu.op_const_ref(opcode,
  519. TCgSize2OpSize[size],power,ref));
  520. exit;
  521. end;
  522. { the rest should be handled specifically in the code }
  523. { generator because of the silly register usage restraints }
  524. internalerror(200109231);
  525. End;
  526. OP_MUL,OP_IMUL:
  527. begin
  528. if not(cs_check_overflow in aktlocalswitches) and
  529. ispowerof2(a,power) then
  530. begin
  531. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  532. power,ref));
  533. exit;
  534. end;
  535. { can't multiply a memory location directly with a CONSTant }
  536. if op = OP_IMUL then
  537. inherited a_op_const_ref(list,op,size,a,ref)
  538. else
  539. { OP_MUL should be handled specifically in the code }
  540. { generator because of the silly register usage restraints }
  541. internalerror(200109232);
  542. end;
  543. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  544. if not(cs_check_overflow in aktlocalswitches) and
  545. (a = 1) and
  546. (op in [OP_ADD,OP_SUB]) then
  547. if op = OP_ADD then
  548. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],ref))
  549. else
  550. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],ref))
  551. else if (a = 0) then
  552. if (op <> OP_AND) then
  553. exit
  554. else
  555. a_load_const_ref(list,size,0,ref)
  556. else if (a = high(aword)) and
  557. (op in [OP_AND,OP_OR,OP_XOR]) then
  558. begin
  559. case op of
  560. OP_AND:
  561. exit;
  562. OP_OR:
  563. list.concat(taicpu.op_const_ref(A_NONE,TCgSize2OpSize[size],high(aword),ref));
  564. OP_XOR:
  565. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],ref));
  566. end
  567. end
  568. else
  569. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  570. TCgSize2OpSize[size],a,ref));
  571. OP_SHL,OP_SHR,OP_SAR:
  572. begin
  573. if (a and 31) <> 0 Then
  574. list.concat(taicpu.op_const_ref(
  575. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,ref));
  576. if (a shr 5) <> 0 Then
  577. internalerror(68991);
  578. end
  579. else internalerror(68992);
  580. end;*)
  581. end;
  582. procedure TCgSparc.a_op_reg_reg(list:TAasmOutput;Op:TOpCG;size:TCGSize;src, dst:TRegister);
  583. var
  584. regloadsize:tcgsize;
  585. dstsize:topsize;
  586. tmpreg:tregister;
  587. popecx:boolean;
  588. begin
  589. (* dstsize := S_Q{makeregsize(dst,size)};
  590. case op of
  591. OP_NEG,OP_NOT:
  592. begin
  593. if src <> R_NO then
  594. internalerror(200112291);
  595. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  596. end;
  597. OP_MUL,OP_DIV,OP_IDIV:
  598. { special stuff, needs separate handling inside code }
  599. { generator }
  600. internalerror(200109233);
  601. OP_SHR,OP_SHL,OP_SAR:
  602. begin
  603. tmpreg := R_NO;
  604. { we need cl to hold the shift count, so if the destination }
  605. { is ecx, save it to a temp for now }
  606. if dst in [R_ECX,R_CX,R_CL] then
  607. begin
  608. case S_SW of
  609. S_B:regloadsize := OS_8;
  610. S_W:regloadsize := OS_16;
  611. else regloadsize := OS_32;
  612. end;
  613. tmpreg := get_scratch_reg(list);
  614. a_load_reg_reg(list,regloadsize,OS_32,src,tmpreg);
  615. end;
  616. if not(src in [R_ECX,R_CX,R_CL]) then
  617. begin
  618. { is ecx still free (it's also free if it was allocated }
  619. { to dst, since we've moved dst somewhere else already) }
  620. if not((dst = R_ECX) or
  621. ((R_ECX in rg.unusedregsint) and
  622. { this will always be true, it's just here to }
  623. { allocate ecx }
  624. (rg.getexplicitregisterint(list,R_ECX) = R_ECX))) then
  625. begin
  626. list.concat(taicpu.op_reg(A_NONE,S_SW,R_ECX));
  627. popecx := true;
  628. end;
  629. a_load_reg_reg(list,OS_8,OS_8,(src),R_CL);
  630. end
  631. else
  632. src := R_CL;
  633. { do the shift }
  634. if tmpreg = R_NO then
  635. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,
  636. R_CL,dst))
  637. else
  638. begin
  639. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],S_SW,
  640. R_CL,tmpreg));
  641. { move result back to the destination }
  642. a_load_reg_reg(list,OS_32,OS_32,tmpreg,R_ECX);
  643. free_scratch_reg(list,tmpreg);
  644. end;
  645. if popecx then
  646. list.concat(taicpu.op_reg(A_POP,S_SW,R_ECX))
  647. else if not (dst in [R_ECX,R_CX,R_CL]) then
  648. rg.ungetregisterint(list,R_ECX);
  649. end;
  650. else
  651. begin
  652. if S_SW <> dstsize then
  653. internalerror(200109226);
  654. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,
  655. src,dst));
  656. end;
  657. end;*)
  658. end;
  659. procedure TCgSparc.a_op_ref_reg(list:TAasmOutput;Op:TOpCG;size:TCGSize;CONST ref:TReference;reg:TRegister);
  660. var
  661. opsize:topsize;
  662. begin
  663. (* case op of
  664. OP_NEG,OP_NOT,OP_IMUL:
  665. begin
  666. inherited a_op_ref_reg(list,op,size,ref,reg);
  667. end;
  668. OP_MUL,OP_DIV,OP_IDIV:
  669. { special stuff, needs separate handling inside code }
  670. { generator }
  671. internalerror(200109239);
  672. else
  673. begin
  674. opsize := S_Q{makeregsize(reg,size)};
  675. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],opsize,ref,reg));
  676. end;
  677. end;*)
  678. end;
  679. procedure TCgSparc.a_op_reg_ref(list:TAasmOutput;Op:TOpCG;size:TCGSize;reg:TRegister;CONST ref:TReference);
  680. var
  681. opsize:topsize;
  682. begin
  683. (* case op of
  684. OP_NEG,OP_NOT:
  685. begin
  686. if reg <> R_NO then
  687. internalerror(200109237);
  688. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],ref));
  689. end;
  690. OP_IMUL:
  691. begin
  692. { this one needs a load/imul/store, which is the default }
  693. inherited a_op_ref_reg(list,op,size,ref,reg);
  694. end;
  695. OP_MUL,OP_DIV,OP_IDIV:
  696. { special stuff, needs separate handling inside code }
  697. { generator }
  698. internalerror(200109238);
  699. else
  700. begin
  701. opsize := tcgsize2opsize[size];
  702. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],opsize,reg,ref));
  703. end;
  704. end;*)
  705. end;
  706. procedure TCgSparc.a_op_const_reg_reg(list:TAasmOutput;op:TOpCg;
  707. size:tcgsize;a:aword;src, dst:tregister);
  708. var
  709. tmpref:TReference;
  710. power:LongInt;
  711. opsize:topsize;
  712. begin
  713. opsize := S_SW;
  714. if (opsize <> S_SW) or
  715. not (size in [OS_32,OS_S32]) then
  716. begin
  717. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  718. exit;
  719. end;
  720. { if we get here, we have to do a 32 bit calculation, guaranteed }
  721. Case Op of
  722. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  723. OP_SAR:
  724. { can't do anything special for these }
  725. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  726. OP_IMUL:
  727. begin
  728. if not(cs_check_overflow in aktlocalswitches) and
  729. ispowerof2(a,power) then
  730. { can be done with a shift }
  731. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  732. list.concat(taicpu.op_reg_const_reg(A_SMUL,src,a,dst));
  733. end;
  734. OP_ADD, OP_SUB:
  735. if (a = 0) then
  736. a_load_reg_reg(list,size,size,src,dst)
  737. else
  738. begin
  739. reference_reset(tmpref);
  740. tmpref.base := src;
  741. tmpref.offset := LongInt(a);
  742. if op = OP_SUB then
  743. tmpref.offset := -tmpref.offset;
  744. list.concat(taicpu.op_ref_reg(A_NONE,tmpref,dst));
  745. end
  746. else internalerror(200112302);
  747. end;
  748. end;
  749. procedure TCgSparc.a_op_reg_reg_reg(list:TAasmOutput;op:TOpCg;
  750. size:tcgsize;src1, src2, dst:tregister);
  751. var
  752. tmpref:TReference;
  753. opsize:topsize;
  754. begin
  755. opsize := S_SW;
  756. if (opsize <> S_SW) or
  757. (S_SW <> S_SW) or
  758. not (size in [OS_32,OS_S32]) then
  759. begin
  760. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  761. exit;
  762. end;
  763. { if we get here, we have to do a 32 bit calculation, guaranteed }
  764. Case Op of
  765. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  766. OP_SAR,OP_SUB,OP_NOT,OP_NEG:
  767. { can't do anything special for these }
  768. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  769. OP_IMUL:
  770. list.concat(taicpu.op_reg_reg_reg(A_SMUL,src1,src2,dst));
  771. OP_ADD:
  772. begin
  773. reference_reset(tmpref);
  774. tmpref.base := src1;
  775. tmpref.index := src2;
  776. tmpref.scalefactor := 1;
  777. list.concat(taicpu.op_ref_reg(A_NONE,tmpref,dst));
  778. end
  779. else internalerror(200112303);
  780. end;
  781. end;
  782. {*************** compare instructructions ****************}
  783. procedure TCgSparc.a_cmp_const_reg_label(list:TAasmOutput;size:tcgsize;cmp_op:topcmp;a:aword;reg:tregister;l:tasmlabel);
  784. begin
  785. if(a=0)
  786. then
  787. list.concat(taicpu.op_reg_reg(A_CMP,reg,reg))
  788. else
  789. list.concat(taicpu.op_const_reg(A_CMP,a,reg));
  790. a_jmp_cond(list,cmp_op,l);
  791. end;
  792. procedure TCgSparc.a_cmp_const_ref_label(list:TAasmOutput;size:tcgsize;cmp_op:topcmp;a:aword;const ref:TReference;l:tasmlabel);
  793. begin
  794. with List do
  795. begin
  796. Concat(taicpu.op_const(A_LD,a));
  797. Concat(taicpu.op_ref(A_CMP,ref));
  798. end;
  799. a_jmp_cond(list,cmp_op,l);
  800. end;
  801. procedure TCgSparc.a_cmp_reg_reg_label(list:TAasmOutput;size:tcgsize;cmp_op:topcmp;
  802. reg1,reg2:tregister;l:tasmlabel);
  803. begin
  804. { if regsize(reg1) <> S_SW then
  805. internalerror(200109226);
  806. list.concat(taicpu.op_reg_reg(A_CMP,regsize(reg1),reg1,reg2));
  807. a_jmp_cond(list,cmp_op,l);}
  808. end;
  809. procedure TCgSparc.a_cmp_ref_reg_label(list:TAasmOutput;size:tcgsize;cmp_op:topcmp;CONST ref:TReference;reg:tregister;l:tasmlabel);
  810. var
  811. TempReg:TRegister;
  812. begin
  813. TempReg:=cg.get_scratch_reg_int(List,size);
  814. a_load_ref_reg(list,OS_32,Ref,TempReg);
  815. list.concat(taicpu.op_reg_reg(A_SUBcc,TempReg,Reg));
  816. a_jmp_cond(list,cmp_op,l);
  817. cg.free_scratch_reg(exprasmlist,TempReg);
  818. end;
  819. procedure TCgSparc.a_jmp_cond(list:TAasmOutput;cond:TOpCmp;l:TAsmLabel);
  820. var
  821. ai:TAiCpu;
  822. begin
  823. ai:=TAiCpu.Op_sym(A_BA,S_NO,l);
  824. ai.SetCondition(TOpCmp2AsmCond[cond]);
  825. list.concat(ai);
  826. end;
  827. procedure TCgSparc.a_jmp_flags(list:TAasmOutput;CONST f:TResFlags;l:tasmlabel);
  828. var
  829. ai:taicpu;
  830. begin
  831. ai := Taicpu.op_sym(A_JMPL,S_NO,l);
  832. ai.SetCondition(flags_to_cond(f));
  833. ai.is_jmp := true;
  834. list.concat(ai);
  835. end;
  836. procedure TCgSparc.g_flags2reg(list:TAasmOutput;Size:TCgSize;CONST f:tresflags;reg:TRegister);
  837. VAR
  838. ai:taicpu;
  839. r,hreg:tregister;
  840. BEGIN
  841. r.enum:=R_PSR;
  842. hreg := rg.makeregsize(reg,OS_8);
  843. ai:=Taicpu.Op_reg_reg(A_RDPSR,r,hreg);
  844. ai.SetCondition(flags_to_cond(f));
  845. list.concat(ai);
  846. IF hreg.enum<>reg.enum
  847. THEN
  848. a_load_reg_reg(list,OS_32,OS_32,hreg,reg);
  849. END;
  850. procedure TCgSparc.g_overflowCheck(List:TAasmOutput;const p:TNode);
  851. var
  852. hl:TAsmLabel;
  853. r:Tregister;
  854. begin
  855. r.enum:=R_NONE;
  856. if not(cs_check_overflow in aktlocalswitches)
  857. then
  858. exit;
  859. objectlibrary.getlabel(hl);
  860. if not((p.resulttype.def.deftype=pointerdef) or
  861. ((p.resulttype.def.deftype=orddef) and
  862. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  863. bool8bit,bool16bit,bool32bit])))
  864. then
  865. begin
  866. list.concat(taicpu.op_reg(A_NONE,r));
  867. a_jmp_always(list,hl)
  868. end
  869. else
  870. a_jmp_cond(list,OC_NONE,hl);
  871. a_call_name(list,'FPC_OVERFLOW');
  872. a_label(list,hl);
  873. end;
  874. { *********** entry/exit code and address loading ************ }
  875. procedure TCgSparc.g_stackframe_entry(list:TAasmOutput;LocalSize:LongInt);
  876. var
  877. href:TReference;
  878. r:Tregister;
  879. i:integer;
  880. again:tasmlabel;
  881. begin
  882. {Althogh the SPARC architecture require only word alignment, software
  883. convention and the operating system require every stack frame to be double word
  884. aligned}
  885. LocalSize:=(LocalSize+7)and $FFFFFFF8;
  886. {Execute the SAVE instruction to get a new register window and create a new
  887. stack frame. In the "SAVE %i6,size,%i6" the first %i6 is related to the state
  888. before execution of the SAVE instrucion so it is the caller %i6, when the %i6
  889. after execution of that instruction is the called function stack pointer}
  890. r.enum:=stack_pointer_reg;
  891. with list do
  892. concat(Taicpu.Op_reg_const_reg(A_SAVE,r,-LocalSize,r));
  893. end;
  894. procedure TCgSparc.g_restore_all_registers(list:TaasmOutput;selfused,accused,acchiused:boolean);
  895. begin
  896. {$warning FIX ME TCgSparc.g_restore_all_registers}
  897. end;
  898. procedure TCgSparc.g_restore_frame_pointer(list:TAasmOutput);
  899. begin
  900. {This function intontionally does nothing as frame pointer is restored in the
  901. delay slot of the return instrucion done in g_return_from_proc}
  902. end;
  903. procedure TCgSparc.g_restore_standard_registers(list:taasmoutput;usedinproc:Tsupregset);
  904. begin
  905. {$WARNING FIX ME TCgSparc.g_restore_standard_registers}
  906. end;
  907. procedure TCgSparc.g_return_from_proc(list:TAasmOutput;parasize:aword);
  908. var r,r2:Tregister;
  909. begin
  910. {According to the SPARC ABI, the stack is cleared using the RESTORE instruction
  911. which is genereted in the g_restore_frame_pointer. Notice that SPARC has no
  912. RETURN instruction and that JMPL is used instead. The JMPL instrucion have one
  913. delay slot, so an inversion is possible such as
  914. JMPL %i7+8,%g0
  915. RESTORE %g0,0,%g0
  916. If no inversion we can use just
  917. RESTORE %g0,0,%g0
  918. JMPL %i7+8,%g0
  919. NOP}
  920. with list do
  921. begin
  922. {Return address is computed by adding 8 to the CALL address saved onto %i6}
  923. r.enum:=R_G0;
  924. r2.enum:=R_I7;
  925. concat(Taicpu.Op_caddr_reg(A_JMPL,r,8,r));
  926. {We use trivial restore in the delay slot of the JMPL instruction, as we
  927. already set result onto %i0}
  928. concat(Taicpu.Op_reg_const_reg(A_RESTORE,r,0,r));
  929. end
  930. end;
  931. procedure TCgSparc.g_save_all_registers(list : taasmoutput);
  932. begin
  933. {$warning FIX ME TCgSparc.g_save_all_registers}
  934. end;
  935. procedure TCgSparc.g_save_standard_registers(list : taasmoutput; usedinproc:Tsupregset);
  936. begin
  937. {$warning FIX ME tcgppc.g_save_standard_registers}
  938. end;
  939. procedure TCgSparc.a_loadaddr_ref_reg(list:TAasmOutput;CONST ref:TReference;r:tregister);
  940. begin
  941. // list.concat(taicpu.op_ref_reg(A_LEA,S_SW,ref,r));
  942. end;
  943. { ************* 64bit operations ************ }
  944. procedure TCg64Sparc.get_64bit_ops(op:TOpCG;var op1,op2:TAsmOp);
  945. begin
  946. case op of
  947. OP_ADD :
  948. begin
  949. op1:=A_ADD;
  950. op2:=A_ADD;
  951. end;
  952. OP_SUB :
  953. begin
  954. op1:=A_SUB;
  955. op2:=A_SUB;
  956. end;
  957. OP_XOR :
  958. begin
  959. op1:=A_XOR;
  960. op2:=A_XOR;
  961. end;
  962. OP_OR :
  963. begin
  964. op1:=A_OR;
  965. op2:=A_OR;
  966. end;
  967. OP_AND :
  968. begin
  969. op1:=A_AND;
  970. op2:=A_AND;
  971. end;
  972. else
  973. internalerror(200203241);
  974. end;
  975. end;
  976. procedure TCg64Sparc.a_op64_ref_reg(list:TAasmOutput;op:TOpCG;CONST ref:TReference;reg:TRegister64);
  977. var
  978. op1,op2:TAsmOp;
  979. tempref:TReference;
  980. begin
  981. get_64bit_ops(op,op1,op2);
  982. list.concat(taicpu.op_ref_reg(op1,ref,reg.reglo));
  983. tempref:=ref;
  984. inc(tempref.offset,4);
  985. list.concat(taicpu.op_ref_reg(op2,tempref,reg.reghi));
  986. end;
  987. procedure TCg64Sparc.a_op64_reg_reg(list:TAasmOutput;op:TOpCG;regsrc,regdst:TRegister64);
  988. var
  989. op1,op2:TAsmOp;
  990. begin
  991. get_64bit_ops(op,op1,op2);
  992. list.concat(taicpu.op_reg_reg(op1,regsrc.reglo,regdst.reglo));
  993. list.concat(taicpu.op_reg_reg(op2,regsrc.reghi,regdst.reghi));
  994. end;
  995. procedure TCg64Sparc.a_op64_const_reg(list:TAasmOutput;op:TOpCG;value:qWord;regdst:TRegister64);
  996. var
  997. op1,op2:TAsmOp;
  998. begin
  999. case op of
  1000. OP_AND,OP_OR,OP_XOR:
  1001. WITH cg DO
  1002. begin
  1003. a_op_const_reg(list,op,Lo(Value),regdst.reglo);
  1004. a_op_const_reg(list,op,Hi(Value),regdst.reghi);
  1005. end;
  1006. OP_ADD, OP_SUB:
  1007. begin
  1008. {can't use a_op_const_ref because this may use dec/inc}
  1009. get_64bit_ops(op,op1,op2);
  1010. list.concat(taicpu.op_const_reg(op1,Lo(Value),regdst.reglo));
  1011. list.concat(taicpu.op_const_reg(op2,Hi(Value),regdst.reghi));
  1012. end;
  1013. else
  1014. internalerror(200204021);
  1015. end;
  1016. end;
  1017. procedure TCg64Sparc.a_op64_const_ref(list:TAasmOutput;op:TOpCG;value:qWord;const ref:TReference);
  1018. var
  1019. op1,op2:TAsmOp;
  1020. tempref:TReference;
  1021. begin
  1022. case op of
  1023. OP_AND,OP_OR,OP_XOR:
  1024. with cg do
  1025. begin
  1026. a_op_const_ref(list,op,OS_32,Lo(Value),ref);
  1027. tempref:=ref;
  1028. inc(tempref.offset,4);
  1029. a_op_const_ref(list,op,OS_32,Hi(Value),tempref);
  1030. end;
  1031. OP_ADD, OP_SUB:
  1032. begin
  1033. get_64bit_ops(op,op1,op2);
  1034. { can't use a_op_const_ref because this may use dec/inc}
  1035. { list.concat(taicpu.op_const_ref(op1,Lo(Value),ref));
  1036. tempref:=ref;
  1037. inc(tempref.offset,4);
  1038. list.concat(taicpu.op_const_ref(op2,S_SW,Hi(Value),tempref));}
  1039. InternalError(2002102101);
  1040. end;
  1041. else
  1042. internalerror(200204022);
  1043. end;
  1044. end;
  1045. { ************* concatcopy ************ }
  1046. procedure TCgSparc.g_concatcopy(list:taasmoutput;const source,dest:treference;len:aword;delsource,loadref:boolean);
  1047. var
  1048. countreg: TRegister;
  1049. src, dst: TReference;
  1050. lab: tasmlabel;
  1051. count, count2: aword;
  1052. orgsrc, orgdst: boolean;
  1053. r:Tregister;
  1054. begin
  1055. {$ifdef extdebug}
  1056. if len > high(longint)
  1057. then
  1058. internalerror(2002072704);
  1059. {$endif extdebug}
  1060. { make sure short loads are handled as optimally as possible }
  1061. if not loadref then
  1062. if (len <= 8) and
  1063. (byte(len) in [1,2,4,8]) then
  1064. begin
  1065. if len < 8 then
  1066. begin
  1067. a_load_ref_ref(list,int_cgsize(len),source,dest);
  1068. if delsource then
  1069. reference_release(list,source);
  1070. end
  1071. else
  1072. begin
  1073. r.enum:=R_F0;
  1074. a_reg_alloc(list,r);
  1075. a_loadfpu_ref_reg(list,OS_F64,source,r);
  1076. if delsource then
  1077. reference_release(list,source);
  1078. a_loadfpu_reg_ref(list,OS_F64,r,dest);
  1079. a_reg_dealloc(list,r);
  1080. end;
  1081. exit;
  1082. end;
  1083. reference_reset(src);
  1084. reference_reset(dst);
  1085. { load the address of source into src.base }
  1086. if loadref then
  1087. begin
  1088. src.base := get_scratch_reg_address(list);
  1089. a_load_ref_reg(list,OS_32,source,src.base);
  1090. orgsrc := false;
  1091. end
  1092. else if not issimpleref(source) or
  1093. ((source.index.enum <> R_NO) and
  1094. ((source.offset + longint(len)) > high(smallint))) then
  1095. begin
  1096. src.base := get_scratch_reg_address(list);
  1097. a_loadaddr_ref_reg(list,source,src.base);
  1098. orgsrc := false;
  1099. end
  1100. else
  1101. begin
  1102. src := source;
  1103. orgsrc := true;
  1104. end;
  1105. if not orgsrc and delsource then
  1106. reference_release(list,source);
  1107. { load the address of dest into dst.base }
  1108. if not issimpleref(dest) or
  1109. ((dest.index.enum <> R_NO) and
  1110. ((dest.offset + longint(len)) > high(smallint))) then
  1111. begin
  1112. dst.base := get_scratch_reg_address(list);
  1113. a_loadaddr_ref_reg(list,dest,dst.base);
  1114. orgdst := false;
  1115. end
  1116. else
  1117. begin
  1118. dst := dest;
  1119. orgdst := true;
  1120. end;
  1121. count := len div 8;
  1122. if count > 4 then
  1123. { generate a loop }
  1124. begin
  1125. { the offsets are zero after the a_loadaddress_ref_reg and just }
  1126. { have to be set to 8. I put an Inc there so debugging may be }
  1127. { easier (should offset be different from zero here, it will be }
  1128. { easy to notice in the generated assembler }
  1129. inc(dst.offset,8);
  1130. inc(src.offset,8);
  1131. list.concat(taicpu.op_reg_const_reg(A_SUB,src.base,8,src.base));
  1132. list.concat(taicpu.op_reg_const_reg(A_SUB,dst.base,8,dst.base));
  1133. countreg := get_scratch_reg_int(list,OS_32);
  1134. a_load_const_reg(list,OS_32,count,countreg);
  1135. { explicitely allocate R_O0 since it can be used safely here }
  1136. { (for holding date that's being copied) }
  1137. r.enum:=R_F0;
  1138. a_reg_alloc(list,r);
  1139. objectlibrary.getlabel(lab);
  1140. a_label(list, lab);
  1141. list.concat(taicpu.op_reg_const_reg(A_SUB,countreg,1,countreg));
  1142. list.concat(taicpu.op_reg_ref(A_LDF,r,src));
  1143. list.concat(taicpu.op_reg_ref(A_STD,r,dst));
  1144. //a_jmp(list,A_BC,C_NE,0,lab);
  1145. free_scratch_reg(list,countreg);
  1146. a_reg_dealloc(list,r);
  1147. len := len mod 8;
  1148. end;
  1149. count := len div 8;
  1150. if count > 0 then
  1151. { unrolled loop }
  1152. begin
  1153. r.enum:=R_F0;
  1154. a_reg_alloc(list,r);
  1155. for count2 := 1 to count do
  1156. begin
  1157. a_loadfpu_ref_reg(list,OS_F64,src,r);
  1158. a_loadfpu_reg_ref(list,OS_F64,r,dst);
  1159. inc(src.offset,8);
  1160. inc(dst.offset,8);
  1161. end;
  1162. a_reg_dealloc(list,r);
  1163. len := len mod 8;
  1164. end;
  1165. if (len and 4) <> 0 then
  1166. begin
  1167. r.enum:=R_O0;
  1168. a_reg_alloc(list,r);
  1169. a_load_ref_reg(list,OS_32,src,r);
  1170. a_load_reg_ref(list,OS_32,r,dst);
  1171. inc(src.offset,4);
  1172. inc(dst.offset,4);
  1173. a_reg_dealloc(list,r);
  1174. end;
  1175. { copy the leftovers }
  1176. if (len and 2) <> 0 then
  1177. begin
  1178. r.enum:=R_O0;
  1179. a_reg_alloc(list,r);
  1180. a_load_ref_reg(list,OS_16,src,r);
  1181. a_load_reg_ref(list,OS_16,r,dst);
  1182. inc(src.offset,2);
  1183. inc(dst.offset,2);
  1184. a_reg_dealloc(list,r);
  1185. end;
  1186. if (len and 1) <> 0 then
  1187. begin
  1188. r.enum:=R_O0;
  1189. a_reg_alloc(list,r);
  1190. a_load_ref_reg(list,OS_8,src,r);
  1191. a_load_reg_ref(list,OS_8,r,dst);
  1192. a_reg_dealloc(list,r);
  1193. end;
  1194. if orgsrc then
  1195. begin
  1196. if delsource then
  1197. reference_release(list,source);
  1198. end
  1199. else
  1200. free_scratch_reg(list,src.base);
  1201. if not orgdst then
  1202. free_scratch_reg(list,dst.base);
  1203. end;
  1204. function TCgSparc.reg_cgsize(CONST reg:tregister):tcgsize;
  1205. begin
  1206. result:=OS_32;
  1207. end;
  1208. {***************** This is private property, keep out! :) *****************}
  1209. function TCgSparc.IsSimpleRef(const ref:treference):boolean;
  1210. begin
  1211. if(ref.base.enum=R_NONE)and(ref.index.enum <> R_NO)
  1212. then
  1213. InternalError(2002100804);
  1214. result :=not(assigned(ref.symbol))and
  1215. (((ref.index.enum = R_NO) and
  1216. (ref.offset >= low(smallint)) and
  1217. (ref.offset <= high(smallint))) or
  1218. ((ref.index.enum <> R_NO) and
  1219. (ref.offset = 0)));
  1220. end;
  1221. procedure TCgSparc.sizes2load(s1:tcgsize;s2:topsize;var op:tasmop;var s3:topsize);
  1222. begin
  1223. case s2 of
  1224. S_B:
  1225. if S1 in [OS_8,OS_S8]
  1226. then
  1227. s3 := S_B
  1228. else
  1229. internalerror(200109221);
  1230. S_W:
  1231. case s1 of
  1232. OS_8,OS_S8:
  1233. s3 := S_B;
  1234. OS_16,OS_S16:
  1235. s3 := S_H;
  1236. else
  1237. internalerror(200109222);
  1238. end;
  1239. S_SW:
  1240. case s1 of
  1241. OS_8,OS_S8:
  1242. s3 := S_B;
  1243. OS_16,OS_S16:
  1244. s3 := S_H;
  1245. OS_32,OS_S32:
  1246. s3 := S_W;
  1247. else
  1248. internalerror(200109223);
  1249. end;
  1250. else internalerror(200109227);
  1251. end;
  1252. if s3 in [S_B,S_W,S_SW]
  1253. then
  1254. op := A_LD
  1255. { else if s3=S_DW
  1256. then
  1257. op:=A_LDD
  1258. else if s3 in [OS_8,OS_16,OS_32]
  1259. then
  1260. op := A_NONE}
  1261. else
  1262. op := A_NONE;
  1263. end;
  1264. procedure TCgSparc.floatloadops(t:tcgsize;VAR op:tasmop;VAR s:topsize);
  1265. BEGIN
  1266. (* case t of
  1267. OS_F32:begin
  1268. op:=A_FLD;
  1269. s:=S_FS;
  1270. end;
  1271. OS_F64:begin
  1272. op:=A_FLD;
  1273. { ???? }
  1274. s:=S_FL;
  1275. end;
  1276. OS_F80:begin
  1277. op:=A_FLD;
  1278. s:=S_FX;
  1279. end;
  1280. OS_C64:begin
  1281. op:=A_FILD;
  1282. s:=S_IQ;
  1283. end;
  1284. else internalerror(17);
  1285. end;*)
  1286. END;
  1287. procedure TCgSparc.floatload(list:TAasmOutput;t:tcgsize;CONST ref:TReference);
  1288. VAR
  1289. op:tasmop;
  1290. s:topsize;
  1291. BEGIN
  1292. floatloadops(t,op,s);
  1293. list.concat(Taicpu.Op_ref(op,ref));
  1294. { inc(trgcpu(rg).fpuvaroffset);}
  1295. END;
  1296. procedure TCgSparc.floatstoreops(t:tcgsize;var op:tasmop;var s:topsize);
  1297. BEGIN
  1298. { case t of
  1299. OS_F32:begin
  1300. op:=A_FSTP;
  1301. s:=S_FS;
  1302. end;
  1303. OS_F64:begin
  1304. op:=A_FSTP;
  1305. s:=S_FL;
  1306. end;
  1307. OS_F80:begin
  1308. op:=A_FSTP;
  1309. s:=S_FX;
  1310. end;
  1311. OS_C64:begin
  1312. op:=A_FISTP;
  1313. s:=S_IQ;
  1314. end;
  1315. else
  1316. internalerror(17);
  1317. end;}
  1318. end;
  1319. procedure TCgSparc.floatstore(list:TAasmOutput;t:tcgsize;CONST ref:TReference);
  1320. VAR
  1321. op:tasmop;
  1322. s:topsize;
  1323. BEGIN
  1324. floatstoreops(t,op,s);
  1325. list.concat(Taicpu.Op_ref(op,ref));
  1326. { dec(trgcpu(rg).fpuvaroffset);}
  1327. END;
  1328. BEGIN
  1329. cg:=TCgSparc.Create;
  1330. cg64:=TCg64Sparc.Create;
  1331. END.
  1332. {
  1333. $Log$
  1334. Revision 1.39 2003-02-19 22:00:16 daniel
  1335. * Code generator converted to new register notation
  1336. - Horribily outdated todo.txt removed
  1337. Revision 1.38 2003/02/18 22:00:20 mazen
  1338. * asm condition generation modified by TAiCpu.SetCondition
  1339. Revision 1.37 2003/02/05 21:48:34 mazen
  1340. * fixing run time errors related to unimplemented abstract methods in CG
  1341. + giving empty emplementations for some RTL functions
  1342. Revision 1.36 2003/01/22 22:30:03 mazen
  1343. - internal errors rmoved from a_loar_reg_reg when reg sizes differs from 32
  1344. Revision 1.35 2003/01/20 22:21:36 mazen
  1345. * many stuff related to RTL fixed
  1346. Revision 1.34 2003/01/08 18:43:58 daniel
  1347. * Tregister changed into a record
  1348. Revision 1.33 2003/01/07 22:03:40 mazen
  1349. * adding unequaln node support to sparc compiler
  1350. Revision 1.32 2003/01/06 22:51:47 mazen
  1351. * fixing bugs related to load_reg_ref
  1352. Revision 1.31 2003/01/05 21:32:35 mazen
  1353. * fixing several bugs compiling the RTL
  1354. Revision 1.30 2003/01/05 13:36:53 florian
  1355. * x86-64 compiles
  1356. + very basic support for float128 type (x86-64 only)
  1357. Revision 1.29 2002/12/25 20:59:49 mazen
  1358. - many emitXXX removed from cga.pas in order to remove that file.
  1359. Revision 1.28 2002/12/22 19:26:31 mazen
  1360. * many internal errors related to unimplemented nodes are fixed
  1361. Revision 1.27 2002/12/21 23:21:47 mazen
  1362. + added support for the shift nodes
  1363. + added debug output on screen with -an command line option
  1364. Revision 1.26 2002/11/25 19:21:49 mazen
  1365. * fixed support of nSparcInline
  1366. Revision 1.25 2002/11/25 17:43:28 peter
  1367. * splitted defbase in defutil,symutil,defcmp
  1368. * merged isconvertable and is_equal into compare_defs(_ext)
  1369. * made operator search faster by walking the list only once
  1370. Revision 1.24 2002/11/17 17:49:09 mazen
  1371. + return_result_reg and function_result_reg are now used, in all plateforms, to pass functions result between called function and its caller. See the explanation of each one
  1372. Revision 1.23 2002/11/10 19:07:46 mazen
  1373. * SPARC calling mechanism almost OK (as in GCC./mppcsparc )
  1374. Revision 1.22 2002/11/06 11:31:24 mazen
  1375. * op_reg_reg_reg don't need any more a TOpSize parameter
  1376. Revision 1.21 2002/11/05 16:15:00 mazen
  1377. *** empty log message ***
  1378. Revision 1.20 2002/11/03 20:22:40 mazen
  1379. * parameter handling updated
  1380. Revision 1.19 2002/10/28 20:59:17 mazen
  1381. * TOpSize values changed S_L --> S_SW
  1382. Revision 1.18 2002/10/22 13:43:01 mazen
  1383. - cga.pas redueced to an empty unit
  1384. Revision 1.17 2002/10/20 19:01:38 mazen
  1385. + op_raddr_reg and op_caddr_reg added to fix functions prologue
  1386. Revision 1.16 2002/10/13 21:46:07 mazen
  1387. * assembler output format fixed
  1388. Revision 1.15 2002/10/11 13:35:14 mazen
  1389. *** empty log message ***
  1390. Revision 1.14 2002/10/10 19:57:51 mazen
  1391. * Just to update repsitory
  1392. Revision 1.13 2002/10/10 15:10:39 mazen
  1393. * Internal error fixed, but usually i386 parameter model used
  1394. Revision 1.12 2002/10/08 17:17:03 mazen
  1395. *** empty log message ***
  1396. Revision 1.11 2002/10/07 20:33:04 mazen
  1397. word alignement modified in g_stack_frame
  1398. Revision 1.10 2002/10/04 21:57:42 mazen
  1399. * register allocation for parameters now done in cpupara, but InternalError(200109223) in cgcpu.pas:1053 is still not fixed du to location_force problem in ncgutils.pas:419
  1400. Revision 1.9 2002/10/02 22:20:28 mazen
  1401. + out registers allocator for the first 6 scalar parameters which must be passed into %o0..%o5
  1402. Revision 1.8 2002/10/01 21:35:58 mazen
  1403. + procedures exiting prologue added and stack frame now restored in the delay slot of the return (JMPL) instruction
  1404. Revision 1.7 2002/10/01 21:06:29 mazen
  1405. attinst.inc --> strinst.inc
  1406. Revision 1.6 2002/10/01 17:41:50 florian
  1407. * fixed log and id
  1408. }