cgcpu.pas 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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. {$i defines.inc}
  20. interface
  21. uses
  22. cgbase,cgobj,aasm,cpuasm,cpubase,cpuinfo,node,cg64f32,cginfo;
  23. type
  24. tcgppc = class(tcg64f32)
  25. { passing parameters, per default the parameter is pushed }
  26. { nr gives the number of the parameter (enumerated from }
  27. { left to right), this allows to move the parameter to }
  28. { register, if the cpu supports register calling }
  29. { conventions }
  30. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;nr : longint);override;
  31. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;nr : longint);override;
  32. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;nr : longint);override;
  33. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;nr : longint);override;
  34. procedure a_call_name(list : taasmoutput;const s : string;
  35. offset : longint);override;
  36. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); override;
  37. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  38. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  39. size: tcgsize; a: aword; src, dst: tregister); override;
  40. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  41. size: tcgsize; src1, src2, dst: tregister); override;
  42. { move instructions }
  43. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  44. procedure a_load_reg_ref(list : taasmoutput; size: tcgsize; reg : tregister;const ref : treference);override;
  45. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const Ref : treference;reg : tregister);override;
  46. procedure a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);override;
  47. procedure a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister); override;
  48. { fpu move instructions }
  49. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); override;
  50. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  51. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  52. { comparison operations }
  53. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  54. l : tasmlabel);override;
  55. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  56. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel); override;
  57. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  58. procedure g_flags2reg(list: taasmoutput; const f: TResFlags; reg: TRegister); override;
  59. procedure g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  60. procedure g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  61. procedure g_stackframe_entry(list : taasmoutput;localsize : longint);override;
  62. procedure g_restore_frame_pointer(list : taasmoutput);override;
  63. procedure g_return_from_proc(list : taasmoutput;parasize : aword); override;
  64. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  65. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);override;
  66. procedure g_overflowcheck(list: taasmoutput; const p: tnode); override;
  67. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  68. { that's the case, we can use rlwinm to do an AND operation }
  69. function get_rlwi_const(a: longint; var l1, l2: longint): boolean;
  70. private
  71. procedure g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  72. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  73. { Make sure ref is a valid reference for the PowerPC and sets the }
  74. { base to the value of the index if (base = R_NO). }
  75. procedure fixref(list: taasmoutput; var ref: treference);
  76. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  77. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  78. ref: treference);
  79. { creates the correct branch instruction for a given combination }
  80. { of asmcondflags and destination addressing mode }
  81. procedure a_jmp(list: taasmoutput; op: tasmop;
  82. c: tasmcondflag; crval: longint; l: tasmlabel);
  83. end;
  84. const
  85. {
  86. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_ADD,A_AND,A_DIVWU,
  87. A_DIVW,A_MULLW, A_MULLW, A_NEG,A_NOT,A_OR,
  88. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  89. }
  90. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  91. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  92. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  93. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  94. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  95. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  96. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  97. C_LT,C_GE,C_LE,C_NE,C_LE,C_NG,C_GE,C_NL);
  98. implementation
  99. uses
  100. globtype,globals,verbose,systems,cutils,symconst,symdef,rgobj;
  101. { parameter passing... Still needs extra support from the processor }
  102. { independent code generator }
  103. procedure tcgppc.a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;nr : longint);
  104. var
  105. ref: treference;
  106. begin
  107. {$ifdef para_sizes_known}
  108. if (nr <= max_param_regs_int) then
  109. a_load_reg_reg(list,size,r,param_regs_int[nr])
  110. else
  111. begin
  112. reset_reference(ref);
  113. ref.base := STACK_POINTER_REG;
  114. ref.offset := LinkageAreaSize+para_size_till_now;
  115. a_load_reg_ref(list,size,reg,ref);
  116. end;
  117. {$endif para_sizes_known}
  118. end;
  119. procedure tcgppc.a_param_const(list : taasmoutput;size : tcgsize;a : aword;nr : longint);
  120. var
  121. ref: treference;
  122. begin
  123. {$ifdef para_sizes_known}
  124. if (nr <= max_param_regs_int) then
  125. a_load_const_reg(list,size,a,param_regs_int[nr])
  126. else
  127. begin
  128. reset_reference(ref);
  129. ref.base := STACK_POINTER_REG;
  130. ref.offset := LinkageAreaSize+para_size_till_now;
  131. a_load_const_ref(list,size,a,ref);
  132. end;
  133. {$endif para_sizes_known}
  134. end;
  135. procedure tcgppc.a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;nr : longint);
  136. var
  137. ref: treference;
  138. tmpreg: tregister;
  139. begin
  140. {$ifdef para_sizes_known}
  141. if (nr <= max_param_regs_int) then
  142. a_load_ref_reg(list,size,r,param_regs_int[nr])
  143. else
  144. begin
  145. reset_reference(ref);
  146. ref.base := STACK_POINTER_REG;
  147. ref.offset := LinkageAreaSize+para_size_till_now;
  148. tmpreg := get_scratch_reg(list);
  149. a_load_ref_reg(list,size,r,tmpreg);
  150. a_load_reg_ref(list,size,tmpreg,ref);
  151. free_scratch_reg(list,tmpreg);
  152. end;
  153. {$endif para_sizes_known}
  154. end;
  155. procedure tcgppc.a_paramaddr_ref(list : taasmoutput;const r : treference;nr : longint);
  156. var
  157. ref: treference;
  158. tmpreg: tregister;
  159. begin
  160. {$ifdef para_sizes_known}
  161. if (nr <= max_param_regs_int) then
  162. a_loadaddr_ref_reg(list,size,r,param_regs_int[nr])
  163. else
  164. begin
  165. reset_reference(ref);
  166. ref.base := STACK_POINTER_REG;
  167. ref.offset := LinkageAreaSize+para_size_till_now;
  168. tmpreg := get_scratch_reg(list);
  169. a_loadaddr_ref_reg(list,size,r,tmpreg);
  170. a_load_reg_ref(list,size,tmpreg,ref);
  171. free_scratch_reg(list,tmpreg);
  172. end;
  173. {$endif para_sizes_known}
  174. end;
  175. { calling a code fragment by name }
  176. procedure tcgppc.a_call_name(list : taasmoutput;const s : string;
  177. offset : longint);
  178. begin
  179. { save our RTOC register value. Only necessary when doing pointer based }
  180. { calls or cross TOC calls, but currently done always }
  181. list.concat(taicpu.op_reg_ref(A_STW,R_RTOC,
  182. new_reference(STACK_POINTER_REG,LA_RTOC)));
  183. list.concat(taicpu.op_sym(A_BL,newasmsymbol(s)));
  184. list.concat(taicpu.op_reg_ref(A_LWZ,R_RTOC,
  185. new_reference(STACK_POINTER_REG,LA_RTOC)));
  186. end;
  187. {********************** load instructions ********************}
  188. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  189. begin
  190. if (a and $ffff) <> 0 Then
  191. begin
  192. list.concat(taicpu.op_reg_const(A_LI,reg,a and $ffff));
  193. if (longint(a) < low(smallint)) or
  194. (longint(a) > high(smallint)) 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 ref : treference);
  202. const
  203. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  204. { indexed? updating?}
  205. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  206. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  207. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  208. var
  209. op: TAsmOp;
  210. ref2: TReference;
  211. begin
  212. ref2 := ref;
  213. FixRef(list,ref2);
  214. if size in [OS_S8..OS_S16] then
  215. { storing is the same for signed and unsigned values }
  216. size := tcgsize(ord(size)-(ord(OS_S8)-ord(OS_8)));
  217. { 64 bit stuff should be handled separately }
  218. if size in [OS_64,OS_S64] then
  219. internalerror(200109236);
  220. op := storeinstr[size,ref2.index<>R_NO,false];
  221. a_load_store(list,op,reg,ref2);
  222. End;
  223. procedure tcgppc.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref: treference;reg : tregister);
  224. const
  225. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  226. { indexed? updating?}
  227. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  228. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  229. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  230. { 64bit stuff should be handled separately }
  231. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  232. { there's no load-byte-with-sign-extend :( }
  233. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  234. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  235. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  236. var
  237. op: tasmop;
  238. tmpreg: tregister;
  239. ref2, tmpref: treference;
  240. begin
  241. if ref.is_immediate then
  242. a_load_const_reg(list,size,ref.offset,reg)
  243. else
  244. begin
  245. ref2 := ref;
  246. fixref(list,ref2);
  247. op := loadinstr[size,ref2.index<>R_NO,false];
  248. a_load_store(list,op,reg,ref2);
  249. { sign extend shortint if necessary, since there is no }
  250. { load instruction that does that automatically (JM) }
  251. if size = OS_S8 then
  252. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  253. end;
  254. end;
  255. procedure tcgppc.a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);
  256. begin
  257. list.concat(taicpu.op_reg_reg(A_MR,reg2,reg1));
  258. end;
  259. procedure tcgppc.a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister);
  260. begin
  261. { can't use op_sym_ofs_reg because sym+ofs can be > 32767!! }
  262. internalerror(200112293);
  263. end;
  264. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  265. begin
  266. list.concat(taicpu.op_reg_reg(A_FMR,reg1,reg2));
  267. end;
  268. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  269. const
  270. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  271. { indexed? updating?}
  272. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  273. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  274. var
  275. op: tasmop;
  276. ref2: treference;
  277. begin
  278. if not(size in [OS_F32,OS_F64]) then
  279. internalerror(200201121);
  280. ref2 := ref;
  281. fixref(list,ref2);
  282. op := fpuloadinstr[size,ref2.index <> R_NO,false];
  283. a_load_store(list,op,reg,ref2);
  284. end;
  285. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  286. const
  287. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  288. { indexed? updating?}
  289. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  290. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  291. var
  292. op: tasmop;
  293. ref2: treference;
  294. begin
  295. if not(size in [OS_F32,OS_F64]) then
  296. internalerror(200201122);
  297. ref2 := ref;
  298. fixref(list,ref2);
  299. op := fpustoreinstr[size,ref2.index <> R_NO,false];
  300. a_load_store(list,op,reg,ref2);
  301. end;
  302. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister);
  303. var
  304. scratch_register: TRegister;
  305. begin
  306. case op of
  307. OP_DIV, OP_IDIV, OP_IMUL, OP_MUL, OP_ADD, OP_AND, OP_OR, OP_SUB,
  308. OP_XOR:
  309. a_op_const_reg_reg(list,op,OS_32,a,reg,reg);
  310. OP_SHL,OP_SHR,OP_SAR:
  311. begin
  312. if (a and 31) <> 0 then
  313. list.concat(taicpu.op_reg_reg_const(
  314. TOpCG2AsmOpConstLo[op],reg,reg,a and 31));
  315. if (a shr 5) <> 0 then
  316. internalError(68991);
  317. end
  318. else internalError(68992);
  319. end;
  320. end;
  321. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  322. begin
  323. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  324. end;
  325. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  326. size: tcgsize; a: aword; src, dst: tregister);
  327. var
  328. l1,l2: longint;
  329. var
  330. oplo, ophi: tasmop;
  331. scratchreg: tregister;
  332. useReg: boolean;
  333. begin
  334. ophi := TOpCG2AsmOpConstHi[op];
  335. oplo := TOpCG2AsmOpConstLo[op];
  336. { constants in a PPC instruction are always interpreted as signed }
  337. { 16bit values, so if the value is between low(smallint) and }
  338. { high(smallint), it's easy }
  339. if (op in [OP_ADD,OP_SUB,OP_AND,OP_OR,OP_XOR]) then
  340. begin
  341. if (longint(a) >= low(smallint)) and
  342. (longint(a) <= high(smallint)) then
  343. begin
  344. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,a));
  345. exit;
  346. end;
  347. { all basic constant instructions also have a shifted form that }
  348. { works only on the highest 16bits, so if low(a) is 0, we can }
  349. { use that one }
  350. if (lo(a) = 0) then
  351. begin
  352. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,hi(a)));
  353. exit;
  354. end;
  355. end;
  356. { otherwise, the instructions we can generate depend on the }
  357. { operation }
  358. useReg := false;
  359. case op of
  360. OP_DIV, OP_IDIV, OP_IMUL, OP_MUL:
  361. if (Op = OP_IMUL) and (longint(a) >= -32768) and
  362. (longint(a) <= 32767) then
  363. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,a))
  364. else
  365. usereg := true;
  366. OP_ADD,OP_SUB:
  367. begin
  368. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,low(a)));
  369. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  370. high(a) + ord(smallint(a) < 0)));
  371. end;
  372. OP_OR:
  373. { try to use rlwimi }
  374. if get_rlwi_const(a,l1,l2) then
  375. begin
  376. if src <> dst then
  377. list.concat(taicpu.op_reg_reg(A_MR,dst,src));
  378. scratchreg := get_scratch_reg(list);
  379. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  380. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  381. scratchreg,0,l1,l2));
  382. free_scratch_reg(list,scratchreg);
  383. end
  384. else
  385. useReg := true;
  386. OP_AND:
  387. { try to use rlwinm }
  388. if get_rlwi_const(a,l1,l2) then
  389. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  390. src,0,l1,l2))
  391. else
  392. useReg := true;
  393. OP_XOR:
  394. useReg := true;
  395. OP_SHL,OP_SHR,OP_SAR:
  396. begin
  397. if (a and 31) <> 0 Then
  398. list.concat(taicpu.op_reg_reg_const(
  399. TOpCG2AsmOpConstLo[Op],dst,src,a and 31));
  400. if (a shr 5) <> 0 then
  401. internalError(68991);
  402. end
  403. else
  404. internalerror(200109091);
  405. end;
  406. { if all else failed, load the constant in a register and then }
  407. { perform the operation }
  408. if useReg then
  409. begin
  410. scratchreg := get_scratch_reg(list);
  411. a_load_const_reg(list,OS_32,a,scratchreg);
  412. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  413. free_scratch_reg(list,scratchreg);
  414. end;
  415. end;
  416. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  417. size: tcgsize; src1, src2, dst: tregister);
  418. const
  419. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  420. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  421. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  422. begin
  423. case op of
  424. OP_NEG,OP_NOT:
  425. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,dst));
  426. else
  427. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  428. end;
  429. end;
  430. {*************** compare instructructions ****************}
  431. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  432. l : tasmlabel);
  433. var
  434. p: taicpu;
  435. scratch_register: TRegister;
  436. signed: boolean;
  437. begin
  438. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  439. if signed then
  440. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  441. list.concat(taicpu.op_reg_reg_const(A_CMPI,R_CR0,reg,a))
  442. else
  443. begin
  444. scratch_register := get_scratch_reg(list);
  445. a_load_const_reg(list,OS_32,a,scratch_register);
  446. list.concat(taicpu.op_reg_reg_reg(A_CMP,R_CR0,reg,scratch_register));
  447. free_scratch_reg(list,scratch_register);
  448. end
  449. else
  450. if (a <= $ffff) then
  451. list.concat(taicpu.op_reg_reg_const(A_CMPLI,R_CR0,reg,a))
  452. else
  453. begin
  454. scratch_register := get_scratch_reg(list);
  455. a_load_const_reg(list,OS_32,a,scratch_register);
  456. list.concat(taicpu.op_reg_reg_reg(A_CMPL,R_CR0,reg,scratch_register));
  457. free_scratch_reg(list,scratch_register);
  458. end;
  459. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  460. end;
  461. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  462. reg1,reg2 : tregister;l : tasmlabel);
  463. var
  464. p: taicpu;
  465. op: tasmop;
  466. begin
  467. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  468. op := A_CMP
  469. else op := A_CMPL;
  470. list.concat(taicpu.op_reg_reg_reg(op,R_CR0,reg1,reg2));
  471. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  472. end;
  473. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  474. begin
  475. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  476. end;
  477. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  478. var
  479. c: tasmcond;
  480. begin
  481. c := flags_to_cond(f);
  482. a_jmp(list,A_BC,c.cond,ord(c.cr)-ord(R_CR0),l);
  483. end;
  484. procedure tcgppc.g_flags2reg(list: taasmoutput; const f: TResFlags; reg: TRegister);
  485. var
  486. testbit: byte;
  487. bitvalue: boolean;
  488. begin
  489. { get the bit to extract from the conditional register + its }
  490. { requested value (0 or 1) }
  491. testbit := (ord(f.cr) * 4);
  492. case f.flag of
  493. F_EQ,F_NE:
  494. bitvalue := f.flag = F_EQ;
  495. F_LT,F_GE:
  496. begin
  497. inc(testbit);
  498. bitvalue := f.flag = F_LT;
  499. end;
  500. F_GT,F_LE:
  501. begin
  502. inc(testbit,2);
  503. bitvalue := f.flag = F_GT;
  504. end;
  505. else
  506. internalerror(200112261);
  507. end;
  508. { load the conditional register in the destination reg }
  509. list.concat(taicpu.op_reg(A_MFCR,reg));
  510. { we will move the bit that has to be tested to bit 31 -> rotate }
  511. { left by bitpos+1 (remember, this is big-endian!) }
  512. testbit := (testbit + 1) and 31;
  513. { extract bit }
  514. list.concat(taicpu.op_reg_reg_const_const_const(
  515. A_RLWINM,reg,reg,testbit,31,31));
  516. { if we need the inverse, xor with 1 }
  517. if not bitvalue then
  518. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  519. end;
  520. (*
  521. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  522. var
  523. testbit: byte;
  524. bitvalue: boolean;
  525. begin
  526. { get the bit to extract from the conditional register + its }
  527. { requested value (0 or 1) }
  528. case f.simple of
  529. false:
  530. begin
  531. { we don't generate this in the compiler }
  532. internalerror(200109062);
  533. end;
  534. true:
  535. case f.cond of
  536. C_None:
  537. internalerror(200109063);
  538. C_LT..C_NU:
  539. begin
  540. testbit := (ord(f.cr) - ord(R_CR0))*4;
  541. inc(testbit,AsmCondFlag2BI[f.cond]);
  542. bitvalue := AsmCondFlagTF[f.cond];
  543. end;
  544. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  545. begin
  546. testbit := f.crbit
  547. bitvalue := AsmCondFlagTF[f.cond];
  548. end;
  549. else
  550. internalerror(200109064);
  551. end;
  552. end;
  553. { load the conditional register in the destination reg }
  554. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  555. { we will move the bit that has to be tested to bit 31 -> rotate }
  556. { left by bitpos+1 (remember, this is big-endian!) }
  557. if bitpos <> 31 then
  558. inc(bitpos)
  559. else
  560. bitpos := 0;
  561. { extract bit }
  562. list.concat(taicpu.op_reg_reg_const_const_const(
  563. A_RLWINM,reg,reg,bitpos,31,31));
  564. { if we need the inverse, xor with 1 }
  565. if not bitvalue then
  566. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  567. end;
  568. *)
  569. { *********** entry/exit code and address loading ************ }
  570. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  571. begin
  572. case target_info.target of
  573. target_powerpc_macos:
  574. g_stackframe_entry_mac(list,localsize);
  575. target_powerpc_linux:
  576. g_stackframe_entry_sysv(list,localsize)
  577. else
  578. internalerror(2204001);
  579. end;
  580. end;
  581. procedure tcgppc.g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  582. { generated the entry code of a procedure/function. Note: localsize is the }
  583. { sum of the size necessary for local variables and the maximum possible }
  584. { combined size of ALL the parameters of a procedure called by the current }
  585. { one }
  586. var regcounter: TRegister;
  587. begin
  588. if (localsize mod 8) <> 0 then internalerror(58991);
  589. { CR and LR only have to be saved in case they are modified by the current }
  590. { procedure, but currently this isn't checked, so save them always }
  591. { following is the entry code as described in "Altivec Programming }
  592. { Interface Manual", bar the saving of AltiVec registers }
  593. a_reg_alloc(list,STACK_POINTER_REG);
  594. a_reg_alloc(list,R_0);
  595. { allocate registers containing reg parameters }
  596. for regcounter := R_3 to R_10 do
  597. a_reg_alloc(list,regcounter);
  598. { save return address... }
  599. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  600. { ... in caller's frame }
  601. list.concat(taicpu.op_reg_ref(A_STW,R_0,new_reference(STACK_POINTER_REG,4)));
  602. a_reg_dealloc(list,R_0);
  603. a_reg_alloc(list,R_11);
  604. { save end of fpr save area }
  605. list.concat(taicpu.op_reg_reg_const(A_ORI,R_11,STACK_POINTER_REG,0));
  606. a_reg_alloc(list,R_12);
  607. { 0 or 8 based on SP alignment }
  608. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  609. R_12,STACK_POINTER_REG,0,28,28));
  610. { add in stack length }
  611. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  612. -localsize));
  613. { establish new alignment }
  614. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER,STACK_POINTER_REG,R_12));
  615. a_reg_dealloc(list,R_12);
  616. { save floating-point registers }
  617. { !!! has to be optimized: only save registers that are used }
  618. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savefpr_14'),0));
  619. { compute end of gpr save area }
  620. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,-144));
  621. { save gprs and fetch GOT pointer }
  622. { !!! has to be optimized: only save registers that are used }
  623. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savegpr_14_go'),0));
  624. a_reg_alloc(list,R_31);
  625. { place GOT ptr in r31 }
  626. list.concat(taicpu.op_reg_reg(A_MFSPR,R_31,R_LR));
  627. { save the CR if necessary ( !!! always done currently ) }
  628. { still need to find out where this has to be done for SystemV
  629. a_reg_alloc(list,R_0);
  630. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  631. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  632. new_reference(STACK_POINTER_REG,LA_CR)));
  633. a_reg_dealloc(list,R_0); }
  634. { save pointer to incoming arguments }
  635. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_30,R_11,144));
  636. { now comes the AltiVec context save, not yet implemented !!! }
  637. end;
  638. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  639. { generated the entry code of a procedure/function. Note: localsize is the }
  640. { sum of the size necessary for local variables and the maximum possible }
  641. { combined size of ALL the parameters of a procedure called by the current }
  642. { one }
  643. var regcounter: TRegister;
  644. begin
  645. if (localsize mod 8) <> 0 then internalerror(58991);
  646. { CR and LR only have to be saved in case they are modified by the current }
  647. { procedure, but currently this isn't checked, so save them always }
  648. { following is the entry code as described in "Altivec Programming }
  649. { Interface Manual", bar the saving of AltiVec registers }
  650. a_reg_alloc(list,STACK_POINTER_REG);
  651. a_reg_alloc(list,R_0);
  652. { allocate registers containing reg parameters }
  653. for regcounter := R_3 to R_10 do
  654. a_reg_alloc(list,regcounter);
  655. { save return address... }
  656. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  657. { ... in caller's frame }
  658. list.concat(taicpu.op_reg_ref(A_STW,R_0,new_reference(STACK_POINTER_REG,8)));
  659. a_reg_dealloc(list,R_0);
  660. { save floating-point registers }
  661. { !!! has to be optimized: only save registers that are used }
  662. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savef14'),0));
  663. { save gprs in gpr save area }
  664. { !!! has to be optimized: only save registers that are used }
  665. list.concat(taicpu.op_reg_ref(A_STMW,R_13,new_reference(STACK_POINTER_REG,-220)));
  666. { save the CR if necessary ( !!! always done currently ) }
  667. a_reg_alloc(list,R_0);
  668. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR));
  669. list.concat(taicpu.op_reg_ref(A_STW,R_0,
  670. new_reference(stack_pointer,LA_CR)));
  671. a_reg_dealloc(list,R_0);
  672. { save pointer to incoming arguments }
  673. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  674. a_reg_alloc(list,R_12);
  675. { 0 or 8 based on SP alignment }
  676. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  677. R_12,STACK_POINTER_REG,0,28,28));
  678. { add in stack length }
  679. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  680. -localsize));
  681. { establish new alignment }
  682. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  683. a_reg_dealloc(list,R_12);
  684. { now comes the AltiVec context save, not yet implemented !!! }
  685. end;
  686. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  687. begin
  688. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  689. end;
  690. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  691. begin
  692. case target_info.target of
  693. target_powerpc_macos:
  694. g_return_from_proc_mac(list,parasize);
  695. target_powerpc_linux:
  696. g_return_from_proc_sysv(list,parasize)
  697. else
  698. internalerror(2204001);
  699. end;
  700. end;
  701. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  702. var tmpreg: tregister;
  703. ref2, tmpref: treference;
  704. begin
  705. ref2 := ref;
  706. FixRef(list,ref2);
  707. if assigned(ref2.symbol) then
  708. { add the symbol's value to the base of the reference, and if the }
  709. { reference doesn't have a base, create one }
  710. begin
  711. tmpreg := get_scratch_reg(list);
  712. reset_reference(tmpref);
  713. tmpref.symbol := ref2.symbol;
  714. tmpref.symaddr := refs_ha;
  715. tmpref.is_immediate := true;
  716. if ref2.base <> R_NO then
  717. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  718. ref2.base,newreference(tmpref)))
  719. else
  720. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,
  721. newreference(tmpref)));
  722. ref2.base := tmpreg;
  723. ref2.symaddr := refs_l;
  724. { can be folded with one of the next instructions by the }
  725. { optimizer probably }
  726. list.concat(taicpu.op_reg_reg_ref(A_ADDI,tmpreg,tmpreg,
  727. newreference(tmpref)));
  728. end;
  729. if ref2.offset <> 0 Then
  730. if ref2.base <> R_NO then
  731. a_op_const_reg_reg(list,OP_ADD,OS_32,ref2.offset,ref2.base,r)
  732. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  733. { occurs, so now only ref.offset has to be loaded }
  734. else a_load_const_reg(list,OS_32,ref2.offset,r)
  735. else
  736. if ref.index <> R_NO Then
  737. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  738. else
  739. if r <> ref2.base then
  740. list.concat(taicpu.op_reg_reg(A_MR,r,ref2.base));
  741. if assigned(ref2.symbol) then
  742. free_scratch_reg(list,tmpreg);
  743. end;
  744. { ************* concatcopy ************ }
  745. procedure tcgppc.g_concatcopy(list : taasmoutput;const source,dest : treference;len : aword; delsource,loadref : boolean);
  746. var
  747. t: taicpu;
  748. countreg, tempreg: TRegister;
  749. src, dst: TReference;
  750. lab: tasmlabel;
  751. count, count2: aword;
  752. begin
  753. { make sure source and dest are valid }
  754. src := source;
  755. fixref(list,src);
  756. dst := dest;
  757. fixref(list,dst);
  758. reset_reference(src);
  759. reset_reference(dst);
  760. { load the address of source into src.base }
  761. src.base := get_scratch_reg(list);
  762. if loadref then
  763. a_load_ref_reg(list,OS_32,source,src.base)
  764. else a_loadaddr_ref_reg(list,source,src.base);
  765. if delsource then
  766. reference_release(exprasmlist,source);
  767. { load the address of dest into dst.base }
  768. dst.base := get_scratch_reg(list);
  769. a_loadaddr_ref_reg(list,dest,dst.base);
  770. count := len div 4;
  771. if count > 3 then
  772. { generate a loop }
  773. begin
  774. { the offsets are zero after the a_loadaddress_ref_reg and just }
  775. { have to be set to 4. I put an Inc there so debugging may be }
  776. { easier (should offset be different from zero here, it will be }
  777. { easy to notice in the genreated assembler }
  778. Inc(dst.offset,4);
  779. Inc(src.offset,4);
  780. list.concat(taicpu.op_reg_reg_const(A_SUBI,src.base,src.base,4));
  781. list.concat(taicpu.op_reg_reg_const(A_SUBI,dst.base,dst.base,4));
  782. countreg := get_scratch_reg(list);
  783. a_load_const_reg(list,OS_32,count-1,countreg);
  784. { explicitely allocate R_0 since it can be used safely here }
  785. { (for holding date that's being copied) }
  786. tempreg := R_0;
  787. a_reg_alloc(list,R_0);
  788. getlabel(lab);
  789. a_label(list, lab);
  790. list.concat(taicpu.op_reg_ref(A_LWZU,tempreg,
  791. newreference(src)));
  792. list.concat(taicpu.op_reg_reg_const(A_CMPI,R_CR0,countreg,0));
  793. list.concat(taicpu.op_reg_ref(A_STWU,tempreg,newreference(dst)));
  794. list.concat(taicpu.op_reg_reg_const(A_SUBI,countreg,countreg,1));
  795. a_jmp(list,A_BC,C_NE,0,lab);
  796. free_scratch_reg(list,countreg);
  797. end
  798. else
  799. { unrolled loop }
  800. begin
  801. tempreg := get_scratch_reg(list);
  802. for count2 := 1 to count do
  803. begin
  804. a_load_ref_reg(list,OS_32,src,tempreg);
  805. a_load_reg_ref(list,OS_32,tempreg,dst);
  806. inc(src.offset,4);
  807. inc(dst.offset,4);
  808. end
  809. end;
  810. { copy the leftovers }
  811. if (len and 2) <> 0 then
  812. begin
  813. a_load_ref_reg(list,OS_16,src,tempreg);
  814. a_load_reg_ref(list,OS_16,tempreg,dst);
  815. inc(src.offset,2);
  816. inc(dst.offset,2);
  817. end;
  818. if (len and 1) <> 0 then
  819. begin
  820. a_load_ref_reg(list,OS_8,src,tempreg);
  821. a_load_reg_ref(list,OS_8,tempreg,dst);
  822. end;
  823. a_reg_dealloc(list,tempreg);
  824. free_scratch_reg(list,src.base);
  825. free_scratch_reg(list,dst.base);
  826. end;
  827. procedure tcgppc.g_overflowcheck(list: taasmoutput; const p: tnode);
  828. var
  829. hl : tasmlabel;
  830. begin
  831. if not(cs_check_overflow in aktlocalswitches) then
  832. exit;
  833. getlabel(hl);
  834. if not ((p.resulttype.def.deftype=pointerdef) or
  835. ((p.resulttype.def.deftype=orddef) and
  836. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  837. bool8bit,bool16bit,bool32bit]))) then
  838. begin
  839. list.concat(taicpu.op_reg(A_MCRXR,R_CR7));
  840. a_jmp(list,A_BC,C_OV,7,hl)
  841. end
  842. else
  843. a_jmp_cond(list,OC_AE,hl);
  844. a_call_name(list,'FPC_OVERFLOW',0);
  845. a_label(list,hl);
  846. end;
  847. {***************** This is private property, keep out! :) *****************}
  848. procedure tcgppc.g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  849. var
  850. regcounter: TRegister;
  851. begin
  852. { release parameter registers }
  853. for regcounter := R_3 to R_10 do
  854. a_reg_dealloc(list,regcounter);
  855. { AltiVec context restore, not yet implemented !!! }
  856. { address of gpr save area to r11 }
  857. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_31,-144));
  858. { restore gprs }
  859. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restgpr_14'),0));
  860. { address of fpr save area to r11 }
  861. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,144));
  862. { restore fprs and return }
  863. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restfpr_14_x'),0));
  864. end;
  865. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  866. var
  867. regcounter: TRegister;
  868. begin
  869. { release parameter registers }
  870. for regcounter := R_3 to R_10 do
  871. a_reg_dealloc(list,regcounter);
  872. { AltiVec context restore, not yet implemented !!! }
  873. { restore SP }
  874. list.concat(taicpu.op_reg_reg_const(A_ORI,STACK_POINTER_REG,R_31,0));
  875. { restore gprs }
  876. list.concat(taicpu.op_reg_ref(A_LMW,R_13,new_reference(STACK_POINTER_REG,-220)));
  877. { restore return address ... }
  878. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,new_reference(STACK_POINTER_REG,8)));
  879. { ... and return from _restf14 }
  880. list.concat(taicpu.op_sym_ofs(A_B,newasmsymbol('_restf14'),0));
  881. end;
  882. procedure tcgppc.fixref(list: taasmoutput; var ref: treference);
  883. begin
  884. If (ref.base <> R_NO) then
  885. begin
  886. if (ref.index <> R_NO) and
  887. ((ref.offset <> 0) or assigned(ref.symbol)) then
  888. begin
  889. if not assigned(ref.symbol) and
  890. (cardinal(ref.offset-low(smallint)) <=
  891. high(smallint)-low(smallint)) then
  892. begin
  893. list.concat(taicpu.op_reg_reg_const(
  894. A_ADDI,ref.base,ref.base,ref.offset));
  895. ref.offset := 0;
  896. end
  897. else
  898. begin
  899. list.concat(taicpu.op_reg_reg_reg(
  900. A_ADD,ref.base,ref.base,ref.index));
  901. ref.index := R_NO;
  902. end;
  903. end
  904. end
  905. else
  906. begin
  907. ref.base := ref.index;
  908. ref.index := R_NO
  909. end
  910. end;
  911. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  912. { that's the case, we can use rlwinm to do an AND operation }
  913. function tcgppc.get_rlwi_const(a: longint; var l1, l2: longint): boolean;
  914. var
  915. temp, testbit: longint;
  916. compare: boolean;
  917. begin
  918. get_rlwi_const := false;
  919. { start with the lowest bit }
  920. testbit := 1;
  921. { check its value }
  922. compare := boolean(a and testbit);
  923. { find out how long the run of bits with this value is }
  924. { (it's impossible that all bits are 1 or 0, because in that case }
  925. { this function wouldn't have been called) }
  926. l1 := 31;
  927. while (((a and testbit) <> 0) = compare) do
  928. begin
  929. testbit := testbit shl 1;
  930. dec(l1);
  931. end;
  932. { check the length of the run of bits that comes next }
  933. compare := not compare;
  934. l2 := l1;
  935. while (((a and testbit) <> 0) = compare) and
  936. (l2 >= 0) do
  937. begin
  938. testbit := testbit shl 1;
  939. dec(l2);
  940. end;
  941. { and finally the check whether the rest of the bits all have the }
  942. { same value }
  943. compare := not compare;
  944. temp := l2;
  945. if temp >= 0 then
  946. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  947. exit;
  948. { we have done "not(not(compare))", so compare is back to its }
  949. { initial value. If the lowest bit was 0, a is of the form }
  950. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  951. { because l2 now contains the position of the last zero of the }
  952. { first run instead of that of the first 1) so switch l1 and l2 }
  953. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  954. if not compare then
  955. begin
  956. temp := l1;
  957. l1 := l2+1;
  958. l2 := temp;
  959. end
  960. else
  961. { otherwise, l1 currently contains the position of the last }
  962. { zero instead of that of the first 1 of the second run -> +1 }
  963. inc(l1);
  964. { the following is the same as "if l1 = -1 then l1 := 31;" }
  965. l1 := l1 and 31;
  966. l2 := l2 and 31;
  967. get_rlwi_const := true;
  968. end;
  969. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  970. ref: treference);
  971. var
  972. tmpreg: tregister;
  973. tmpref: treference;
  974. begin
  975. if assigned(ref.symbol) then
  976. begin
  977. tmpreg := get_scratch_reg(list);
  978. reset_reference(tmpref);
  979. tmpref.symbol := ref.symbol;
  980. tmpref.symaddr := refs_ha;
  981. tmpref.is_immediate := true;
  982. if ref.base <> R_NO then
  983. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  984. ref.base,newreference(tmpref)))
  985. else
  986. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,
  987. newreference(tmpref)));
  988. ref.base := tmpreg;
  989. ref.symaddr := refs_l;
  990. end;
  991. list.concat(taicpu.op_reg_ref(op,reg,newreference(ref)));
  992. if assigned(ref.symbol) then
  993. free_scratch_reg(list,tmpreg);
  994. end;
  995. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  996. crval: longint; l: tasmlabel);
  997. var
  998. p: taicpu;
  999. begin
  1000. p := taicpu.op_sym(op,newasmsymbol(l.name));
  1001. create_cond_norm(c,crval,p.condition);
  1002. p.is_jmp := true;
  1003. list.concat(p)
  1004. end;
  1005. begin
  1006. cg := tcgppc.create;
  1007. end.
  1008. {
  1009. $Log$
  1010. Revision 1.13 2002-04-20 21:41:51 carl
  1011. * renamed some constants
  1012. Revision 1.12 2002/04/06 18:13:01 jonas
  1013. * several powerpc-related additions and fixes
  1014. Revision 1.11 2002/01/02 14:53:04 jonas
  1015. * fixed small bug in a_jmp_flags
  1016. Revision 1.10 2001/12/30 17:24:48 jonas
  1017. * range checking is now processor independent (part in cgobj, part in
  1018. cg64f32) and should work correctly again (it needed some changes after
  1019. the changes of the low and high of tordef's to int64)
  1020. * maketojumpbool() is now processor independent (in ncgutil)
  1021. * getregister32 is now called getregisterint
  1022. Revision 1.9 2001/12/29 15:28:58 jonas
  1023. * powerpc/cgcpu.pas compiles :)
  1024. * several powerpc-related fixes
  1025. * cpuasm unit is now based on common tainst unit
  1026. + nppcmat unit for powerpc (almost complete)
  1027. Revision 1.8 2001/10/28 14:16:49 jonas
  1028. * small fixes
  1029. Revision 1.7 2001/09/29 21:33:30 jonas
  1030. * small optimization
  1031. Revision 1.6 2001/09/28 20:40:05 jonas
  1032. * several additions, almost complete (only some problems with resflags left)
  1033. Revision 1.5 2001/09/16 10:33:21 jonas
  1034. * some fixes to operations with constants
  1035. Revision 1.3 2001/09/06 15:25:55 jonas
  1036. * changed type of tcg from object to class -> abstract methods are now
  1037. a lot cleaner :)
  1038. + more updates: load_*_loc methods, op_*_* methods, g_flags2reg method
  1039. (if possible with generic implementation and necessary ppc
  1040. implementations)
  1041. * worked a bit further on cgflw, now working on exitnode
  1042. Revision 1.2 2001/09/05 20:21:03 jonas
  1043. * new cgflow based on n386flw with all nodes until forn "translated"
  1044. + a_cmp_loc_*_label methods for tcg
  1045. + base implementatino for a_cmp_ref_*_label methods
  1046. * small bugfixes to powerpc cg
  1047. Revision 1.1 2001/08/26 13:31:04 florian
  1048. * some cg reorganisation
  1049. * some PPC updates
  1050. Revision 1.2 2001/08/26 13:29:33 florian
  1051. * some cg reorganisation
  1052. * some PPC updates
  1053. Revision 1.1 2000/07/13 06:30:12 michael
  1054. + Initial import
  1055. Revision 1.12 2000/04/22 14:25:04 jonas
  1056. * aasm.pas: pai_align instead of pai_align_abstract if cpu <> i386
  1057. + systems.pas: info for macos/ppc
  1058. * new/cgobj.pas: compiles again without newst define
  1059. * new/powerpc/cgcpu: generate different entry/exit code depending on
  1060. whether target_os is MacOs or Linux
  1061. Revision 1.11 2000/01/07 01:14:57 peter
  1062. * updated copyright to 2000
  1063. Revision 1.10 1999/12/24 22:48:10 jonas
  1064. * compiles again
  1065. Revision 1.9 1999/11/05 07:05:56 jonas
  1066. + a_jmp_cond()
  1067. Revision 1.8 1999/10/24 09:22:18 jonas
  1068. + entry/exitcode for SystemV (Linux) and AIX/Mac from the Altivec
  1069. PIM (no AltiVec support yet though)
  1070. * small fix to the a_cmp_* methods
  1071. Revision 1.7 1999/10/20 12:23:24 jonas
  1072. * fixed a_loadaddress_ref_reg (mentioned as ToDo in rev. 1.5)
  1073. * small bugfix in a_load_store
  1074. Revision 1.6 1999/09/15 20:35:47 florian
  1075. * small fix to operator overloading when in MMX mode
  1076. + the compiler uses now fldz and fld1 if possible
  1077. + some fixes to floating point registers
  1078. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  1079. * .... ???
  1080. Revision 1.5 1999/09/03 13:14:11 jonas
  1081. + implemented some parameter passing methods, but they require
  1082. some more helper routines
  1083. * fix for loading symbol addresses (still needs to be done in a_loadaddress)
  1084. * several changes to the way conditional branches are handled
  1085. Revision 1.4 1999/08/26 14:53:41 jonas
  1086. * first implementation of concatcopy (requires 4 scratch regs)
  1087. Revision 1.3 1999/08/25 12:00:23 jonas
  1088. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  1089. Revision 1.2 1999/08/18 17:05:57 florian
  1090. + implemented initilizing of data for the new code generator
  1091. so it should compile now simple programs
  1092. Revision 1.1 1999/08/06 16:41:11 jonas
  1093. * PowerPC compiles again, several routines implemented in cgcpu.pas
  1094. * added constant to cpubase of alpha and powerpc for maximum
  1095. number of operands
  1096. }