cgcpu.pas 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit implements the code generator for the PowerPC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. interface
  20. uses
  21. cgbase,cgobj,aasm,cpuasm,cpubase,cpuinfo;
  22. type
  23. tcgppc = class(tcg)
  24. { passing parameters, per default the parameter is pushed }
  25. { nr gives the number of the parameter (enumerated from }
  26. { left to right), this allows to move the parameter to }
  27. { register, if the cpu supports register calling }
  28. { conventions }
  29. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;nr : longint);virtual;
  30. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;nr : longint);virtual;
  31. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;nr : longint);virtual;
  32. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;nr : longint);virtual;
  33. procedure a_call_name(list : taasmoutput;const s : string;
  34. offset : longint);virtual;
  35. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister); virtual;
  36. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister); virtual;
  37. { move instructions }
  38. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);virtual;
  39. procedure a_load_reg_ref(list : taasmoutput; size: tcgsize; reg : tregister;const ref2 : treference);virtual;
  40. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const Ref2 : treference;reg : tregister);virtual;
  41. procedure a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);virtual;
  42. { comparison operations }
  43. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  44. l : pasmlabel);virtual;
  45. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : pasmlabel);
  46. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: pasmlabel);
  47. procedure g_flags2reg(const f: TAsmCond; reg: TRegister); abstract;
  48. procedure g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  49. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  50. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);virtual;
  51. procedure g_restore_frame_pointer(list : taasmoutput);virtual;
  52. procedure g_return_from_proc(list : taasmoutput;parasize : aword); virtual;
  53. procedure g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  54. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  55. procedure a_loadaddress_ref_reg(list : taasmoutput;const ref2 : treference;r : tregister);virtual;
  56. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);virtual;
  57. private
  58. { tries to one immediate instruction to pperform the operation, }
  59. { returns false otherwise (then you have to laod the constant }
  60. procedure a_op_reg_reg_const32(list: taasmoutput; op: TOpCg;
  61. dst, src: tregister; a: aword);
  62. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg; dst, src1,
  63. src2: tregister);
  64. { Make sure ref is a valid reference for the PowerPC and sets the }
  65. { base to the value of the index if (base = R_NO). }
  66. procedure fixref(var ref: treference);
  67. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  68. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  69. var ref: treference);
  70. { creates the correct branch instruction for a given combination }
  71. { of asmcondflags and destination addressing mode }
  72. procedure a_jmp(list: taasmoutput; op: tasmop;
  73. c: tasmcondflags; l: pasmlabel);
  74. end;
  75. const
  76. {
  77. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_ADD,A_AND,A_DIVWU,
  78. A_DIVW,A_MULLW, A_MULLW, A_NEG,A_NOT,A_OR,
  79. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  80. }
  81. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_ADDI,A_ANDI_,A_DIVWU,
  82. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  83. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  84. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_ADDIS,A_ANDIS_,
  85. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  86. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  87. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlags = (CF_NONE,CF_EQ,CF_GT,
  88. CF_LT,CF_GE,CF_LE,CF_NE,CF_LE,CF_NG,CF_GE,CF_NL);
  89. LoadInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  90. { indexed? updating?}
  91. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  92. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  93. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  94. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  95. { indexed? updating?}
  96. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  97. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  98. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  99. implementation
  100. uses
  101. globtype,globals,verbose,systems,cutils;
  102. { parameter passing... Still needs extra support from the processor }
  103. { independent code generator }
  104. procedure tcgppc.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;nr : longint);
  105. var
  106. ref: treference;
  107. begin
  108. {$ifdef para_sizes_known}
  109. if (nr <= max_param_regs_int) then
  110. a_load_reg_reg(list,size,r,param_regs_int[nr])
  111. else
  112. begin
  113. reset_reference(ref);
  114. ref.base := stack_pointer;
  115. ref.offset := LinkageAreaSize+para_size_till_now;
  116. a_load_reg_ref(list,size,reg,ref);
  117. end;
  118. {$endif para_sizes_known}
  119. end;
  120. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;nr : longint);
  121. var
  122. ref: treference;
  123. begin
  124. {$ifdef para_sizes_known}
  125. if (nr <= max_param_regs_int) then
  126. a_load_const_reg(list,size,a,param_regs_int[nr])
  127. else
  128. begin
  129. reset_reference(ref);
  130. ref.base := stack_pointer;
  131. ref.offset := LinkageAreaSize+para_size_till_now;
  132. a_load_const_ref(list,size,a,ref);
  133. end;
  134. {$endif para_sizes_known}
  135. end;
  136. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;nr : longint);
  137. var
  138. ref: treference;
  139. tmpreg: tregister;
  140. begin
  141. {$ifdef para_sizes_known}
  142. if (nr <= max_param_regs_int) then
  143. a_load_ref_reg(list,size,r,param_regs_int[nr])
  144. else
  145. begin
  146. reset_reference(ref);
  147. ref.base := stack_pointer;
  148. ref.offset := LinkageAreaSize+para_size_till_now;
  149. tmpreg := get_scratch_reg(list);
  150. a_load_ref_reg(list,size,r,tmpreg);
  151. a_load_reg_ref(list,size,tmpreg,ref);
  152. free_scratch_reg(list,tmpreg);
  153. end;
  154. {$endif para_sizes_known}
  155. end;
  156. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;nr : longint);
  157. var
  158. ref: treference;
  159. tmpreg: tregister;
  160. begin
  161. {$ifdef para_sizes_known}
  162. if (nr <= max_param_regs_int) then
  163. a_loadaddress_ref_reg(list,size,r,param_regs_int[nr])
  164. else
  165. begin
  166. reset_reference(ref);
  167. ref.base := stack_pointer;
  168. ref.offset := LinkageAreaSize+para_size_till_now;
  169. tmpreg := get_scratch_reg(list);
  170. a_loadaddress_ref_reg(list,size,r,tmpreg);
  171. a_load_reg_ref(list,size,tmpreg,ref);
  172. free_scratch_reg(list,tmpreg);
  173. end;
  174. {$endif para_sizes_known}
  175. end;
  176. { calling a code fragment by name }
  177. procedure tcgppc.a_call_name(list : taasmoutput;const s : string;
  178. offset : longint);
  179. begin
  180. { save our RTOC register value. Only necessary when doing pointer based }
  181. { calls or cross TOC calls, but currently done always }
  182. list.concat(taicpu.op_reg_ref(A_STW,R_RTOC,
  183. new_reference(stack_pointer,LA_RTOC)));
  184. list.concat(taicpu.op_sym(A_BL,newasmsymbol(s)));
  185. list.concat(taicpu.op_reg_ref(A_LWZ,R_RTOC,
  186. new_reference(stack_pointer,LA_RTOC)));
  187. end;
  188. {********************** load instructions ********************}
  189. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  190. begin
  191. If (a and $ffff) <> 0 Then
  192. Begin
  193. list.concat(taicpu.op_reg_const(A_LI,reg,a and $ffff));
  194. If (a shr 16) <> 0 Then
  195. list.concat(taicpu.op_reg_const(A_ADDIS,reg,
  196. (a shr 16)+ord(smallint(a and $ffff) < 0)))
  197. End
  198. Else
  199. list.concat(taicpu.op_reg_const(A_LIS,reg,a shr 16));
  200. end;
  201. procedure tcgppc.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref2 : treference);
  202. var
  203. op: TAsmOp;
  204. ref: TReference;
  205. begin
  206. ref := ref2;
  207. FixRef(ref);
  208. op := storeinstr[size,ref.index<>R_NO,false];
  209. a_load_store(list,op,reg,ref);
  210. End;
  211. procedure tcgppc.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref2: treference;reg : tregister);
  212. var
  213. op: TAsmOp;
  214. tmpreg: tregister;
  215. ref, tmpref: TReference;
  216. begin
  217. ref := ref2;
  218. FixRef(ref);
  219. op := loadinstr[size,ref.index<>R_NO,false];
  220. a_load_store(list,op,reg,ref);
  221. end;
  222. procedure tcgppc.a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);
  223. begin
  224. list.concat(taicpu.op_reg_reg(A_MR,reg2,reg1));
  225. end;
  226. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: AWord; reg: TRegister);
  227. var
  228. scratch_register: TRegister;
  229. begin
  230. Case Op of
  231. OP_DIV, OP_IDIV, OP_IMUL, OP_MUL:
  232. If (Op = OP_IMUL) And (longint(a) >= -32768) And
  233. (longint(a) <= 32767) Then
  234. list.concat(taicpu.op_reg_reg_const(A_MULLI,reg,reg,a))
  235. Else
  236. Begin
  237. scratch_register := get_scratch_reg(list);
  238. a_load_const_reg(list,OS_32,a,scratch_register);
  239. list.concat(taicpu.op_reg_reg_reg(TOpCG2AsmOpConstLo[Op],
  240. reg,scratch_register,reg));
  241. free_scratch_reg(list,scratch_register);
  242. End;
  243. OP_ADD, OP_AND, OP_OR, OP_SUB,OP_XOR:
  244. a_op_reg_reg_const32(list,op,reg,reg,a)
  245. OP_SHL,OP_SHR,OP_SAR:
  246. Begin
  247. if (a and 31) <> 0 Then
  248. list.concat(taicpu.op_reg_reg_const(
  249. TOpCG2AsmOpConstLo[Op],reg,reg,a and 31));
  250. If (a shr 5) <> 0 Then
  251. InternalError(68991);
  252. End
  253. Else InternalError(68992);
  254. end;
  255. end;
  256. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; reg1, reg2: TRegister);
  257. begin
  258. a_op_reg_reg_reg(list,op,reg2,reg1,reg2);
  259. end;
  260. {*************** compare instructructions ****************}
  261. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  262. l : pasmlabel);
  263. var
  264. p: taicpu;
  265. scratch_register: TRegister;
  266. signed: boolean;
  267. begin
  268. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  269. If signed Then
  270. If (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  271. list.concat(taicpu.op_reg_reg_const(A_CMPI,R_CR0,reg,a))
  272. else
  273. begin
  274. scratch_register := get_scratch_reg(list);
  275. a_load_const_reg(list,OS_32,a,scratch_register);
  276. list.concat(taicpu.op_reg_reg_reg(A_CMP,R_CR0,reg,scratch_register));
  277. free_scratch_reg(list,scratch_register);
  278. end
  279. else
  280. if (a <= $ffff) then
  281. list.concat(taicpu.op_reg_reg_const(A_CMPLI,R_CR0,reg,a))
  282. else
  283. begin
  284. scratch_register := get_scratch_reg(list);
  285. a_load_const_reg(list,OS_32,a,scratch_register);
  286. list.concat(taicpu.op_reg_reg_reg(A_CMPL,R_CR0,reg,scratch_register));
  287. free_scratch_reg(list,scratch_register);
  288. end;
  289. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],l);
  290. end;
  291. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  292. reg1,reg2 : tregister;l : pasmlabel);
  293. var p: paicpu;
  294. op: tasmop;
  295. begin
  296. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  297. op := A_CMP
  298. else op := A_CMPL;
  299. list.concat(taicpu.op_reg_reg_reg(op,R_CR0,reg1,reg2));
  300. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],l);
  301. end;
  302. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: pasmlabel);
  303. begin
  304. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],l);
  305. end;
  306. procedure tcgppc.g_flags2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  307. var
  308. testbit: byte;
  309. bitvalue: boolean;
  310. begin
  311. { get the bit to extract from the conditional register + its }
  312. { requested value (0 or 1) }
  313. case simple of
  314. false:
  315. begin
  316. { we don't generate this in the compiler }
  317. internalerror(200109062);
  318. end;
  319. true:
  320. case f.cond of
  321. C_None:
  322. internalerror(200109063);
  323. C_LT..C_NU:
  324. begin
  325. testbit := (ord(f.cr) - ord(R_CR0))*4;
  326. inc(testbit,AsmCondFlag2BI[f.cond]);
  327. bitvalue := AsmCondFlagTF[f.cond];
  328. end;
  329. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  330. begin
  331. testbit := f.crbit
  332. bitvalue := AsmCondFlagTF[f.cond];
  333. end;
  334. else
  335. internalerror(200109064);
  336. end;
  337. end;
  338. { load thge conditional register in the destination reg }
  339. list.concat(taicpu.create(op_reg_reg(A_MFCR,reg)));
  340. { we will move the bit that has to be tested to bit 0 -> rotate }
  341. { left by bitpos+1 (remember, this is big-endian!) }
  342. if bitpos <> 31 then
  343. inc(bitpos)
  344. else
  345. bitpos := 0;
  346. { extract bit }
  347. list.concat(taicpu.create(op_reg_reg_const_const_const(
  348. A_RLWINM,reg,reg,bitpos,31,31)));
  349. { if we need the inverse, xor with 1 }
  350. if not bitvalue then
  351. list.concat(taicpu.create(op_reg_reg_const(A_XORI,reg,reg,1)));
  352. end;
  353. { *********** entry/exit code and address loading ************ }
  354. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  355. begin
  356. case target_os.id of
  357. os_powerpc_macos:
  358. g_stackframe_entry_mac(list,localsize);
  359. os_powerpc_linux:
  360. g_stackframe_entry_sysv(list,localsize)
  361. else
  362. internalerror(2204001);
  363. end;
  364. end;
  365. procedure tcgppc.g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  366. { generated the entry code of a procedure/function. Note: localsize is the }
  367. { sum of the size necessary for local variables and the maximum possible }
  368. { combined size of ALL the parameters of a procedure called by the current }
  369. { one }
  370. var regcounter: TRegister;
  371. begin
  372. if (localsize mod 8) <> 0 then internalerror(58991);
  373. { CR and LR only have to be saved in case they are modified by the current }
  374. { procedure, but currently this isn't checked, so save them always }
  375. { following is the entry code as described in "Altivec Programming }
  376. { Interface Manual", bar the saving of AltiVec registers }
  377. a_reg_alloc(list,stack_pointer);
  378. a_reg_alloc(list,R_0);
  379. { allocate registers containing reg parameters }
  380. for regcounter := R_3 to R_10 do
  381. a_reg_alloc(list,regcounter);
  382. { save return address... }
  383. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  384. { ... in caller's frame }
  385. list.concat(taicpu.op_reg_ref(A_STW,R_0,new_reference(STACK_POINTER,4)));
  386. a_reg_dealloc(list,R_0);
  387. a_reg_alloc(list,R_11);
  388. { save end of fpr save area }
  389. list.concat(taicpu.op_reg_reg_const(A_ORI,R_11,STACK_POINTER,0));
  390. a_reg_alloc(list,R_12);
  391. { 0 or 8 based on SP alignment }
  392. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  393. R_12,STACK_POINTER,0,28,28));
  394. { add in stack length }
  395. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  396. -localsize));
  397. { establish new alignment }
  398. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER,STACK_POINTER,R_12));
  399. a_reg_dealloc(list,R_12);
  400. { save floating-point registers }
  401. { !!! has to be optimized: only save registers that are used }
  402. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savefpr_14'),0));
  403. { compute end of gpr save area }
  404. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,-144));
  405. { save gprs and fetch GOT pointer }
  406. { !!! has to be optimized: only save registers that are used }
  407. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savegpr_14_go'),0));
  408. a_reg_alloc(list,R_31);
  409. { place GOT ptr in r31 }
  410. list.concat(taicpu.op_reg_reg(A_MFSPR,R_31,R_LR));
  411. { save the CR if necessary ( !!! always done currently ) }
  412. { still need to find out where this has to be done for SystemV
  413. a_reg_alloc(list,R_0);
  414. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  415. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  416. new_reference(stack_pointer,LA_CR)));
  417. a_reg_dealloc(list,R_0); }
  418. { save pointer to incoming arguments }
  419. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_30,R_11,144));
  420. { now comes the AltiVec context save, not yet implemented !!! }
  421. end;
  422. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  423. { generated the entry code of a procedure/function. Note: localsize is the }
  424. { sum of the size necessary for local variables and the maximum possible }
  425. { combined size of ALL the parameters of a procedure called by the current }
  426. { one }
  427. var regcounter: TRegister;
  428. begin
  429. if (localsize mod 8) <> 0 then internalerror(58991);
  430. { CR and LR only have to be saved in case they are modified by the current }
  431. { procedure, but currently this isn't checked, so save them always }
  432. { following is the entry code as described in "Altivec Programming }
  433. { Interface Manual", bar the saving of AltiVec registers }
  434. a_reg_alloc(list,STACK_POINTER);
  435. a_reg_alloc(list,R_0);
  436. { allocate registers containing reg parameters }
  437. for regcounter := R_3 to R_10 do
  438. a_reg_alloc(list,regcounter);
  439. { save return address... }
  440. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  441. { ... in caller's frame }
  442. list.concat(taicpu.op_reg_ref(A_STW,R_0,new_reference(STACK_POINTER,8)));
  443. a_reg_dealloc(list,R_0);
  444. { save floating-point registers }
  445. { !!! has to be optimized: only save registers that are used }
  446. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savef14'),0));
  447. { save gprs in gpr save area }
  448. { !!! has to be optimized: only save registers that are used }
  449. list.concat(taicpu.op_reg_ref(A_STMW,R_13,new_reference(STACK_POINTER,-220)));
  450. { save the CR if necessary ( !!! always done currently ) }
  451. a_reg_alloc(list,R_0);
  452. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR));
  453. list.concat(taicpu.op_reg_ref(A_STW,R_0,
  454. new_reference(stack_pointer,LA_CR)));
  455. a_reg_dealloc(list,R_0);
  456. { save pointer to incoming arguments }
  457. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER,0));
  458. a_reg_alloc(list,R_12);
  459. { 0 or 8 based on SP alignment }
  460. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  461. R_12,STACK_POINTER,0,28,28));
  462. { add in stack length }
  463. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  464. -localsize));
  465. { establish new alignment }
  466. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER,STACK_POINTER,R_12));
  467. a_reg_dealloc(list,R_12);
  468. { now comes the AltiVec context save, not yet implemented !!! }
  469. end;
  470. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  471. begin
  472. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  473. end;
  474. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  475. begin
  476. case target_os.id of
  477. os_powerpc_macos:
  478. g_return_from_proc_mac(list,parasize);
  479. os_powerpc_linux:
  480. g_return_from_proc_sysv(list,parasize)
  481. else
  482. internalerror(2204001);
  483. end;
  484. end;
  485. procedure tcgppc.g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  486. var regcounter: TRegister;
  487. begin
  488. { release parameter registers }
  489. for regcounter := R_3 to R_10 do
  490. a_reg_dealloc(list,regcounter);
  491. { AltiVec context restore, not yet implemented !!! }
  492. { address of gpr save area to r11 }
  493. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_31,-144));
  494. { restore gprs }
  495. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restgpr_14'),0));
  496. { address of fpr save area to r11 }
  497. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,144));
  498. { restore fprs and return }
  499. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restfpr_14_x'),0));
  500. end;
  501. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  502. var regcounter: TRegister;
  503. begin
  504. { release parameter registers }
  505. for regcounter := R_3 to R_10 do
  506. a_reg_dealloc(list,regcounter);
  507. { AltiVec context restore, not yet implemented !!! }
  508. { restore SP }
  509. list.concat(taicpu.op_reg_reg_const(A_ORI,STACK_POINTER,R_31,0));
  510. { restore gprs }
  511. list.concat(taicpu.op_reg_ref(A_LMW,R_13,new_reference(STACK_POINTER,-220)));
  512. { restore return address ... }
  513. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,new_reference(STACK_POINTER,8)));
  514. { ... and return from _restf14 }
  515. list.concat(taicpu.op_sym_ofs(A_B,newasmsymbol('_restf14'),0));
  516. end;
  517. procedure tcgppc.a_loadaddress_ref_reg(list : taasmoutput;const ref2 : treference;r : tregister);
  518. var tmpreg: tregister;
  519. ref, tmpref: treference;
  520. begin
  521. ref := ref2;
  522. FixRef(ref);
  523. if assigned(ref.symbol) then
  524. { add the symbol's value to the base of the reference, and if the }
  525. { reference doesn't have a base, create one }
  526. begin
  527. tmpreg := get_scratch_reg(list);
  528. reset_reference(tmpref);
  529. tmpref.symbol := ref.symbol;
  530. tmpref.symaddr := refs_ha;
  531. tmpref.is_immediate := true;
  532. if ref.base <> R_NO then
  533. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  534. ref.base,newreference(tmpref)))
  535. else
  536. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,
  537. newreference(tmpref)));
  538. ref.base := tmpreg;
  539. ref.symaddr := refs_l;
  540. { can be folded with one of the next instructions by the }
  541. { optimizer probably }
  542. list.concat(taicpu.op_reg_reg_ref(A_ADDI,tmpreg,tmpreg,
  543. newreference(tmpref)));
  544. end;
  545. if ref.offset <> 0 Then
  546. if ref.base <> R_NO then
  547. a_op_reg_reg_const32(list,OP_ADD,r,ref.base,ref.offset)
  548. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  549. { occurs, so now only ref.offset has to be loaded }
  550. else a_load_const_reg(list, OS_32, ref.offset, r)
  551. else
  552. if ref.index <> R_NO Then
  553. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref.base,ref.index))
  554. else list.concat(taicpu.op_reg_reg(A_MR,r,ref.base));
  555. if assigned(ref.symbol) then
  556. free_scratch_reg(list,tmpreg);
  557. end;
  558. { ************* concatcopy ************ }
  559. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  560. var
  561. p: paicpu;
  562. countreg, tempreg: TRegister;
  563. src, dst: TReference;
  564. lab: PAsmLabel;
  565. count, count2: aword;
  566. begin
  567. { make sure source and dest are valid }
  568. src := source;
  569. fixref(src);
  570. dst := dest;
  571. fixref(dst);
  572. reset_reference(src);
  573. reset_reference(dst);
  574. { load the address of source into src.base }
  575. src.base := get_scratch_reg(list);
  576. if loadref then
  577. a_load_ref_reg(list,OS_32,source,src.base)
  578. else a_loadaddress_ref_reg(list,source,src.base);
  579. if delsource then
  580. del_reference(list,source);
  581. { load the address of dest into dst.base }
  582. dst.base := get_scratch_reg(list);
  583. a_loadaddress_ref_reg(list,dest,dst.base);
  584. count := len div 4;
  585. if count > 3 then
  586. { generate a loop }
  587. begin
  588. { the offsets are zero after the a_loadaddress_ref_reg and just }
  589. { have to be set to 4. I put an Inc there so debugging may be }
  590. { easier (should offset be different from zero here, it will be }
  591. { easy to notice in the genreated assembler }
  592. Inc(dst.offset,4);
  593. Inc(src.offset,4);
  594. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  595. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  596. countreg := get_scratch_reg(list);
  597. a_load_const_reg(list,OS_32,count-1,countreg);
  598. { explicitely allocate R_0 since it can be used safely here }
  599. { (for holding date that's being copied) }
  600. tempreg := R_0;
  601. a_reg_alloc(list,R_0);
  602. getlabel(lab);
  603. a_label(list, lab);
  604. list.concat(taicpu.op_reg_ref(A_LWZU,tempreg,
  605. newreference(src)));
  606. list.concat(taicpu.op_reg_reg_const(A_CMPI,R_CR0,countreg,0));
  607. list.concat(taicpu.op_reg_ref(A_STWU,tempreg,
  608. newreference(dst)));
  609. list.concat(taicpu.op_reg_reg_const(A_SUBI,countreg,countreg,1));
  610. a_jmp(list,A_BC,CF_NE,lab);
  611. free_scratch_reg(list,countreg);
  612. end
  613. else
  614. { unrolled loop }
  615. begin
  616. tempreg := get_scratch_reg(list);
  617. for count2 := 1 to count do
  618. begin
  619. a_load_ref_reg(list,OS_32,src,tempreg);
  620. a_load_reg_ref(list,OS_32,tempreg,dst);
  621. inc(src.offset,4);
  622. inc(dst.offset,4);
  623. end
  624. end;
  625. { copy the leftovers }
  626. if (len and 2) <> 0 then
  627. begin
  628. a_load_ref_reg(list,OS_16,src,tempreg);
  629. a_load_reg_ref(list,OS_16,tempreg,dst);
  630. inc(src.offset,2);
  631. inc(dst.offset,2);
  632. end;
  633. if (len and 1) <> 0 then
  634. begin
  635. a_load_ref_reg(list,OS_8,src,tempreg);
  636. a_load_reg_ref(list,OS_8,tempreg,dst);
  637. end;
  638. a_reg_dealloc(list,tempreg);
  639. free_scratch_reg(list,src.base);
  640. free_scratch_reg(list,dst.base);
  641. end;
  642. {***************** This is private property, keep out! :) *****************}
  643. procedure tcgppc.fixref(var ref: treference);
  644. begin
  645. If (ref.base <> R_NO) then
  646. begin
  647. if (ref.index <> R_NO) and
  648. ((ref.offset <> 0) or assigned(ref.symbol)) Then
  649. Internalerror(58992)
  650. end
  651. else
  652. begin
  653. ref.base := ref.index;
  654. ref.index := R_NO
  655. end
  656. end;
  657. procedure tcgppc.a_op_reg_reg_const32(list: taasmoutput; op: TOpCg;
  658. dst, src: tregister; a: aword): boolean;
  659. var
  660. l1,l2: longint;
  661. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  662. { that's the case, we can use rlwinm to do an AND operation }
  663. function get_rlwinm_const: boolean;
  664. var
  665. temp, testbit, compare: longint;
  666. begin
  667. get_rlwinm_const := false;
  668. { start with the lowest bit }
  669. testbit := 1;
  670. { check its value }
  671. compare := a and testbit;
  672. { find out how long the run of bits with this value is }
  673. l1 := 31;
  674. while (a and testbit) = compare do
  675. begin
  676. testbit := testbit shl 1;
  677. dec(l1);
  678. end;
  679. { check the length of the run of bits that come next }
  680. compare := compare xor 1;
  681. testbit := testbit shl 1;
  682. l2 := l1 - 1;
  683. while (a and testbit) = compare) and
  684. (l2 > 0) do
  685. begin
  686. testbit := testbit shl 1;
  687. dec(l2);
  688. end;
  689. { and finally the check whether the rest of the bits all have the }
  690. { same value }
  691. compare := compare xor 1;
  692. temp := l2 - 1;
  693. if temp > 0 then
  694. if (a shr (31-temp)) <> ((-compare) shr (31-temp)) then
  695. exit;
  696. { we have done "compare xor 1 xor 1", so compare is back to its }
  697. { initial value. If the lowest bit was 0, a is of the form }
  698. { 00..11..00 and we need "rlwinm reg,reg,0,l2,l1-1", (-1 }
  699. { because l1 then contains the position of the first zero of }
  700. { the second run instead of that of the last 1) so switch l1 }
  701. { and l2 in that case (we will generate }
  702. { "rlwinm reg,reg,0,l1,l2") }
  703. if compare = 0 then
  704. begin
  705. temp := l1-1;
  706. l1 := l2;
  707. l2 := temp;
  708. end
  709. else
  710. { a is of the form 11..00.11 -> l2 contains the position of }
  711. { the first zero instead of of the last 1 of the first run }
  712. dec(l2);
  713. get_rlwinm_const := true;
  714. end;
  715. var
  716. oplo, ophi: tasmop;
  717. scratchreg: tregister;
  718. useReg: boolean;
  719. begin
  720. useReg := true;
  721. ophi := TOpCG2AsmOpConstHi[op];
  722. if (low(a) = 0) then
  723. begin
  724. list.concat(taicpu.op_reg_reg(ophi,reg1,reg2,high(a)));
  725. exit;
  726. end;
  727. oplo := TOpCG2AsmOpConstLo[op];
  728. case op of
  729. OP_ADD,OP_SUB:
  730. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) then
  731. list.concat(taicpu.op_reg_reg_const(oplo,reg1,reg2,a))
  732. else
  733. begin
  734. list.concat(taicpu.op_reg_reg_const(oplo,reg1,reg2,low(a)));
  735. list.concat(taicpu.op_reg_reg_const(ophi,reg1,reg1,
  736. high(a) + ord(smallint(a) < 0)));
  737. end;
  738. OP_OR,OP_XOR:
  739. if (longint(a) >= 0) and (longint(a) <= high(smallint)) then
  740. list.concat(taicpu.op_reg_reg_const(oplo,reg1,reg2,a))
  741. else
  742. useReg := false;
  743. OP_AND:
  744. if (longint(a) >= low(smallint)) and (longint(a) <= 0) then
  745. list.concat(taicpu.op_reg_reg_const(oplo,reg1,reg2,a))
  746. else if get_rlwinm_const then
  747. list.concat(taicpu.op_reg_reg_const_const_const(
  748. a_rlwinm,reg1,reg2,0,l1,l2))
  749. else
  750. useReg := false;
  751. else
  752. internalerror(200109091);
  753. end;
  754. if useReg then
  755. begin
  756. scratchreg := get_scratch_reg(list);
  757. a_load_const_reg(list,OS_32,a,scratchreg);
  758. a_op_reg_reg_reg(list,op,reg1,scratchreg,reg2);
  759. free_scratch_reg(list,scratchreg);
  760. end;
  761. end;
  762. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  763. dest, src1, src2: tregister);
  764. const
  765. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  766. (A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  767. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR)
  768. begin
  769. Case Op of
  770. OP_NEG,OP_NOT:
  771. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],size,dst,dst));
  772. else
  773. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],size,dst,src1,src2));
  774. end;
  775. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  776. var ref: treference);
  777. var
  778. tmpreg: tregister;
  779. tmpref: treference;
  780. begin
  781. if assigned(ref.symbol) then
  782. begin
  783. tmpreg := get_scratch_reg(list);
  784. reset_reference(tmpref);
  785. tmpref.symbol := ref.symbol;
  786. tmpref.symaddr := refs_ha;
  787. tmpref.is_immediate := true;
  788. if ref.base <> R_NO then
  789. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  790. ref.base,newreference(tmpref)))
  791. else
  792. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,
  793. newreference(tmpref)));
  794. ref.base := tmpreg;
  795. ref.symaddr := refs_l;
  796. end;
  797. list.concat(taicpu.op_reg_ref(op,reg,newreference(ref)));
  798. if assigned(ref.symbol) then
  799. free_scratch_reg(list,tmpreg);
  800. end;
  801. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflags;
  802. l: pasmlabel);
  803. var
  804. p: paicpu;
  805. begin
  806. p := taicpu.op_sym(op,newasmsymbol(l^.name));
  807. create_cond_norm(c,0,p^.condition);
  808. list.concat(p)
  809. end;
  810. end.
  811. {
  812. $Log$
  813. Revision 1.4 2001-09-09 17:10:25 jonas
  814. * some more things implemented
  815. Revision 1.3 2001/09/06 15:25:55 jonas
  816. * changed type of tcg from object to class -> abstract methods are now
  817. a lot cleaner :)
  818. + more updates: load_*_loc methods, op_*_* methods, g_flags2reg method
  819. (if possible with geenric implementation and necessary ppc
  820. implementations)
  821. * worked a bit further on cgflw, now working on exitnode
  822. Revision 1.2 2001/09/05 20:21:03 jonas
  823. * new cgflow based on n386flw with all nodes until forn "translated"
  824. + a_cmp_loc_*_label methods for tcg
  825. + base implementatino for a_cmp_ref_*_label methods
  826. * small bugfixes to powerpc cg
  827. Revision 1.1 2001/08/26 13:31:04 florian
  828. * some cg reorganisation
  829. * some PPC updates
  830. Revision 1.2 2001/08/26 13:29:33 florian
  831. * some cg reorganisation
  832. * some PPC updates
  833. Revision 1.1 2000/07/13 06:30:12 michael
  834. + Initial import
  835. Revision 1.12 2000/04/22 14:25:04 jonas
  836. * aasm.pas: pai_align instead of pai_align_abstract if cpu <> i386
  837. + systems.pas: info for macos/ppc
  838. * new/cgobj.pas: compiles again without newst define
  839. * new/powerpc/cgcpu: generate different entry/exit code depending on
  840. whether target_os is MacOs or Linux
  841. Revision 1.11 2000/01/07 01:14:57 peter
  842. * updated copyright to 2000
  843. Revision 1.10 1999/12/24 22:48:10 jonas
  844. * compiles again
  845. Revision 1.9 1999/11/05 07:05:56 jonas
  846. + a_jmp_cond()
  847. Revision 1.8 1999/10/24 09:22:18 jonas
  848. + entry/exitcode for SystemV (Linux) and AIX/Mac from the Altivec
  849. PIM (no AltiVec support yet though)
  850. * small fix to the a_cmp_* methods
  851. Revision 1.7 1999/10/20 12:23:24 jonas
  852. * fixed a_loadaddress_ref_reg (mentioned as ToDo in rev. 1.5)
  853. * small bugfix in a_load_store
  854. Revision 1.6 1999/09/15 20:35:47 florian
  855. * small fix to operator overloading when in MMX mode
  856. + the compiler uses now fldz and fld1 if possible
  857. + some fixes to floating point registers
  858. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  859. * .... ???
  860. Revision 1.5 1999/09/03 13:14:11 jonas
  861. + implemented some parameter passing methods, but they require
  862. some more helper routines
  863. * fix for loading symbol addresses (still needs to be done in a_loadaddress)
  864. * several changes to the way conditional branches are handled
  865. Revision 1.4 1999/08/26 14:53:41 jonas
  866. * first implementation of concatcopy (requires 4 scratch regs)
  867. Revision 1.3 1999/08/25 12:00:23 jonas
  868. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  869. Revision 1.2 1999/08/18 17:05:57 florian
  870. + implemented initilizing of data for the new code generator
  871. so it should compile now simple programs
  872. Revision 1.1 1999/08/06 16:41:11 jonas
  873. * PowerPC compiles again, several routines implemented in cgcpu.pas
  874. * added constant to cpubase of alpha and powerpc for maximum
  875. number of operands
  876. }