cgcpu.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.inc}
  20. interface
  21. uses
  22. cgbase,cgobj,
  23. aasmbase,aasmcpu,aasmtai,
  24. cpubase,cpuinfo,node,cg64f32,cginfo;
  25. type
  26. tcgppc = class(tcg)
  27. { passing parameters, per default the parameter is pushed }
  28. { nr gives the number of the parameter (enumerated from }
  29. { left to right), this allows to move the parameter to }
  30. { register, if the cpu supports register calling }
  31. { conventions }
  32. procedure a_param_reg(list : taasmoutput;size : tcgsize;r : tregister;const locpara : tparalocation);override;
  33. procedure a_param_const(list : taasmoutput;size : tcgsize;a : aword;const locpara : tparalocation);override;
  34. procedure a_param_ref(list : taasmoutput;size : tcgsize;const r : treference;const locpara : tparalocation);override;
  35. procedure a_paramaddr_ref(list : taasmoutput;const r : treference;const locpara : tparalocation);override;
  36. procedure a_call_name(list : taasmoutput;const s : string);override;
  37. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister); override;
  38. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  39. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  40. size: tcgsize; a: aword; src, dst: tregister); override;
  41. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  42. size: tcgsize; src1, src2, dst: tregister); override;
  43. { move instructions }
  44. procedure a_load_const_reg(list : taasmoutput; size: tcgsize; a : aword;reg : tregister);override;
  45. procedure a_load_reg_ref(list : taasmoutput; size: tcgsize; reg : tregister;const ref : treference);override;
  46. procedure a_load_ref_reg(list : taasmoutput;size : tcgsize;const Ref : treference;reg : tregister);override;
  47. procedure a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);override;
  48. procedure a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister); override;
  49. { fpu move instructions }
  50. procedure a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister); override;
  51. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  52. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  53. { comparison operations }
  54. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  55. l : tasmlabel);override;
  56. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;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 a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  72. procedure g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  73. procedure g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  74. { Make sure ref is a valid reference for the PowerPC and sets the }
  75. { base to the value of the index if (base = R_NO). }
  76. procedure fixref(list: taasmoutput; var ref: treference);
  77. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  78. procedure a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  79. ref: treference);
  80. { creates the correct branch instruction for a given combination }
  81. { of asmcondflags and destination addressing mode }
  82. procedure a_jmp(list: taasmoutput; op: tasmop;
  83. c: tasmcondflag; crval: longint; l: tasmlabel);
  84. end;
  85. const
  86. {
  87. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_ADD,A_AND,A_DIVWU,
  88. A_DIVW,A_MULLW, A_MULLW, A_NEG,A_NOT,A_OR,
  89. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  90. }
  91. TOpCG2AsmOpConstLo: Array[topcg] of TAsmOp = (A_NONE,A_ADDI,A_ANDI_,A_DIVWU,
  92. A_DIVW,A_MULLW, A_MULLW, A_NONE,A_NONE,A_ORI,
  93. A_SRAWI,A_SLWI,A_SRWI,A_SUBI,A_XORI);
  94. TOpCG2AsmOpConstHi: Array[topcg] of TAsmOp = (A_NONE,A_ADDIS,A_ANDIS_,
  95. A_DIVWU,A_DIVW, A_MULLW,A_MULLW,A_NONE,A_NONE,
  96. A_ORIS,A_NONE, A_NONE,A_NONE,A_SUBIS,A_XORIS);
  97. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  98. C_LT,C_GE,C_LE,C_NE,C_LE,C_NG,C_GE,C_NL);
  99. implementation
  100. uses
  101. globtype,globals,verbose,systems,cutils,symconst,symdef,rgobj;
  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;const locpara : tparalocation);
  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_REG;
  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;const locpara : tparalocation);
  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_REG;
  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;const locpara : tparalocation);
  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_REG;
  148. ref.offset := LinkageAreaSize+para_size_till_now;
  149. tmpreg := get_scratch_reg_int(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;const locpara : tparalocation);
  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_loadaddr_ref_reg(list,size,r,param_regs_int[nr])
  164. else
  165. begin
  166. reset_reference(ref);
  167. ref.base := STACK_POINTER_REG;
  168. ref.offset := LinkageAreaSize+para_size_till_now;
  169. tmpreg := get_scratch_reg_address(list);
  170. a_loadaddr_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. var
  179. href : treference;
  180. begin
  181. { save our RTOC register value. Only necessary when doing pointer based }
  182. { calls or cross TOC calls, but currently done always }
  183. reference_reset_base(href,STACK_POINTER_REG,LA_RTOC);
  184. list.concat(taicpu.op_reg_ref(A_STW,R_TOC,href));
  185. list.concat(taicpu.op_sym(A_BL,newasmsymbol(s)));
  186. reference_reset_base(href,STACK_POINTER_REG,LA_RTOC);
  187. list.concat(taicpu.op_reg_ref(A_LWZ,R_TOC,href));
  188. end;
  189. {********************** load instructions ********************}
  190. procedure tcgppc.a_load_const_reg(list : taasmoutput; size: TCGSize; a : aword; reg : TRegister);
  191. begin
  192. if (a and $ffff) <> 0 Then
  193. begin
  194. list.concat(taicpu.op_reg_const(A_LI,reg,a and $ffff));
  195. if (longint(a) < low(smallint)) or
  196. (longint(a) > high(smallint)) then
  197. list.concat(taicpu.op_reg_const(A_ADDIS,reg,
  198. (a shr 16)+ord(smallint(a and $ffff) < 0)))
  199. end
  200. else
  201. list.concat(taicpu.op_reg_const(A_LIS,reg,a shr 16));
  202. end;
  203. procedure tcgppc.a_load_reg_ref(list : taasmoutput; size: TCGSize; reg : tregister;const ref : treference);
  204. const
  205. StoreInstr: Array[OS_8..OS_32,boolean, boolean] of TAsmOp =
  206. { indexed? updating?}
  207. (((A_STB,A_STBU),(A_STBX,A_STBUX)),
  208. ((A_STH,A_STHU),(A_STHX,A_STHUX)),
  209. ((A_STW,A_STWU),(A_STWX,A_STWUX)));
  210. var
  211. op: TAsmOp;
  212. ref2: TReference;
  213. begin
  214. ref2 := ref;
  215. FixRef(list,ref2);
  216. if size in [OS_S8..OS_S16] then
  217. { storing is the same for signed and unsigned values }
  218. size := tcgsize(ord(size)-(ord(OS_S8)-ord(OS_8)));
  219. { 64 bit stuff should be handled separately }
  220. if size in [OS_64,OS_S64] then
  221. internalerror(200109236);
  222. op := storeinstr[tcgsize2unsigned[size],ref2.index<>R_NO,false];
  223. a_load_store(list,op,reg,ref2);
  224. End;
  225. procedure tcgppc.a_load_ref_reg(list : taasmoutput;size : tcgsize;const ref: treference;reg : tregister);
  226. const
  227. LoadInstr: Array[OS_8..OS_S32,boolean, boolean] of TAsmOp =
  228. { indexed? updating?}
  229. (((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  230. ((A_LHZ,A_LHZU),(A_LHZX,A_LHZUX)),
  231. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)),
  232. { 64bit stuff should be handled separately }
  233. ((A_NONE,A_NONE),(A_NONE,A_NONE)),
  234. { there's no load-byte-with-sign-extend :( }
  235. ((A_LBZ,A_LBZU),(A_LBZX,A_LBZUX)),
  236. ((A_LHA,A_LHAU),(A_LHAX,A_LHAUX)),
  237. ((A_LWZ,A_LWZU),(A_LWZX,A_LWZUX)));
  238. var
  239. op: tasmop;
  240. tmpreg: tregister;
  241. ref2, tmpref: treference;
  242. begin
  243. ref2 := ref;
  244. fixref(list,ref2);
  245. op := loadinstr[size,ref2.index<>R_NO,false];
  246. a_load_store(list,op,reg,ref2);
  247. { sign extend shortint if necessary, since there is no }
  248. { load instruction that does that automatically (JM) }
  249. if size = OS_S8 then
  250. list.concat(taicpu.op_reg_reg(A_EXTSB,reg,reg));
  251. end;
  252. procedure tcgppc.a_load_reg_reg(list : taasmoutput;size : tcgsize;reg1,reg2 : tregister);
  253. begin
  254. list.concat(taicpu.op_reg_reg(A_MR,reg2,reg1));
  255. end;
  256. procedure tcgppc.a_load_sym_ofs_reg(list: taasmoutput; const sym: tasmsymbol; ofs: longint; reg: tregister);
  257. begin
  258. { can't use op_sym_ofs_reg because sym+ofs can be > 32767!! }
  259. internalerror(200112293);
  260. end;
  261. procedure tcgppc.a_loadfpu_reg_reg(list: taasmoutput; reg1, reg2: tregister);
  262. begin
  263. list.concat(taicpu.op_reg_reg(A_FMR,reg1,reg2));
  264. end;
  265. procedure tcgppc.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  266. const
  267. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  268. { indexed? updating?}
  269. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  270. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  271. var
  272. op: tasmop;
  273. ref2: treference;
  274. begin
  275. if not(size in [OS_F32,OS_F64]) then
  276. internalerror(200201121);
  277. ref2 := ref;
  278. fixref(list,ref2);
  279. op := fpuloadinstr[size,ref2.index <> R_NO,false];
  280. a_load_store(list,op,reg,ref2);
  281. end;
  282. procedure tcgppc.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  283. const
  284. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  285. { indexed? updating?}
  286. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  287. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  288. var
  289. op: tasmop;
  290. ref2: treference;
  291. begin
  292. if not(size in [OS_F32,OS_F64]) then
  293. internalerror(200201122);
  294. ref2 := ref;
  295. fixref(list,ref2);
  296. op := fpustoreinstr[size,ref2.index <> R_NO,false];
  297. a_load_store(list,op,reg,ref2);
  298. end;
  299. procedure tcgppc.a_op_const_reg(list : taasmoutput; Op: TOpCG; a: AWord; reg: TRegister);
  300. var
  301. scratch_register: TRegister;
  302. begin
  303. case op of
  304. OP_DIV, OP_IDIV, OP_IMUL, OP_MUL, OP_ADD, OP_AND, OP_OR, OP_SUB,
  305. OP_XOR:
  306. a_op_const_reg_reg(list,op,OS_32,a,reg,reg);
  307. OP_SHL,OP_SHR,OP_SAR:
  308. begin
  309. if (a and 31) <> 0 then
  310. list.concat(taicpu.op_reg_reg_const(
  311. TOpCG2AsmOpConstLo[op],reg,reg,a and 31));
  312. if (a shr 5) <> 0 then
  313. internalError(68991);
  314. end
  315. else internalError(68992);
  316. end;
  317. end;
  318. procedure tcgppc.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  319. begin
  320. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  321. end;
  322. procedure tcgppc.a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  323. size: tcgsize; a: aword; src, dst: tregister);
  324. var
  325. l1,l2: longint;
  326. var
  327. oplo, ophi: tasmop;
  328. scratchreg: tregister;
  329. useReg: boolean;
  330. begin
  331. ophi := TOpCG2AsmOpConstHi[op];
  332. oplo := TOpCG2AsmOpConstLo[op];
  333. { constants in a PPC instruction are always interpreted as signed }
  334. { 16bit values, so if the value is between low(smallint) and }
  335. { high(smallint), it's easy }
  336. if (op in [OP_ADD,OP_SUB,OP_AND,OP_OR,OP_XOR]) then
  337. begin
  338. if (longint(a) >= low(smallint)) and
  339. (longint(a) <= high(smallint)) then
  340. begin
  341. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,a));
  342. exit;
  343. end;
  344. { all basic constant instructions also have a shifted form that }
  345. { works only on the highest 16bits, so if low(a) is 0, we can }
  346. { use that one }
  347. if (lo(a) = 0) then
  348. begin
  349. list.concat(taicpu.op_reg_reg_const(ophi,dst,src,hi(a)));
  350. exit;
  351. end;
  352. end;
  353. { otherwise, the instructions we can generate depend on the }
  354. { operation }
  355. useReg := false;
  356. case op of
  357. OP_DIV, OP_IDIV, OP_IMUL, OP_MUL:
  358. if (Op = OP_IMUL) and (longint(a) >= -32768) and
  359. (longint(a) <= 32767) then
  360. list.concat(taicpu.op_reg_reg_const(A_MULLI,dst,src,a))
  361. else
  362. usereg := true;
  363. OP_ADD,OP_SUB:
  364. begin
  365. list.concat(taicpu.op_reg_reg_const(oplo,dst,src,low(a)));
  366. list.concat(taicpu.op_reg_reg_const(ophi,dst,dst,
  367. high(a) + ord(smallint(a) < 0)));
  368. end;
  369. OP_OR:
  370. { try to use rlwimi }
  371. if get_rlwi_const(a,l1,l2) then
  372. begin
  373. if src <> dst then
  374. list.concat(taicpu.op_reg_reg(A_MR,dst,src));
  375. scratchreg := get_scratch_reg_int(list);
  376. list.concat(taicpu.op_reg_const(A_LI,scratchreg,-1));
  377. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWIMI,dst,
  378. scratchreg,0,l1,l2));
  379. free_scratch_reg(list,scratchreg);
  380. end
  381. else
  382. useReg := true;
  383. OP_AND:
  384. { try to use rlwinm }
  385. if get_rlwi_const(a,l1,l2) then
  386. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,dst,
  387. src,0,l1,l2))
  388. else
  389. useReg := true;
  390. OP_XOR:
  391. useReg := true;
  392. OP_SHL,OP_SHR,OP_SAR:
  393. begin
  394. if (a and 31) <> 0 Then
  395. list.concat(taicpu.op_reg_reg_const(
  396. TOpCG2AsmOpConstLo[Op],dst,src,a and 31));
  397. if (a shr 5) <> 0 then
  398. internalError(68991);
  399. end
  400. else
  401. internalerror(200109091);
  402. end;
  403. { if all else failed, load the constant in a register and then }
  404. { perform the operation }
  405. if useReg then
  406. begin
  407. scratchreg := get_scratch_reg_int(list);
  408. a_load_const_reg(list,OS_32,a,scratchreg);
  409. a_op_reg_reg_reg(list,op,OS_32,scratchreg,src,dst);
  410. free_scratch_reg(list,scratchreg);
  411. end;
  412. end;
  413. procedure tcgppc.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  414. size: tcgsize; src1, src2, dst: tregister);
  415. const
  416. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  417. (A_NONE,A_ADD,A_AND,A_DIVWU,A_DIVW,A_MULLW,A_MULLW,A_NEG,A_NOT,A_OR,
  418. A_SRAW,A_SLW,A_SRW,A_SUB,A_XOR);
  419. begin
  420. case op of
  421. OP_NEG,OP_NOT:
  422. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop[op],dst,dst));
  423. else
  424. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1));
  425. end;
  426. end;
  427. {*************** compare instructructions ****************}
  428. procedure tcgppc.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aword;reg : tregister;
  429. l : tasmlabel);
  430. var
  431. p: taicpu;
  432. scratch_register: TRegister;
  433. signed: boolean;
  434. begin
  435. signed := cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE];
  436. if signed then
  437. if (longint(a) >= low(smallint)) and (longint(a) <= high(smallint)) Then
  438. list.concat(taicpu.op_reg_reg_const(A_CMPI,R_CR0,reg,a))
  439. else
  440. begin
  441. scratch_register := get_scratch_reg_int(list);
  442. a_load_const_reg(list,OS_32,a,scratch_register);
  443. list.concat(taicpu.op_reg_reg_reg(A_CMP,R_CR0,reg,scratch_register));
  444. free_scratch_reg(list,scratch_register);
  445. end
  446. else
  447. if (a <= $ffff) then
  448. list.concat(taicpu.op_reg_reg_const(A_CMPLI,R_CR0,reg,a))
  449. else
  450. begin
  451. scratch_register := get_scratch_reg_int(list);
  452. a_load_const_reg(list,OS_32,a,scratch_register);
  453. list.concat(taicpu.op_reg_reg_reg(A_CMPL,R_CR0,reg,scratch_register));
  454. free_scratch_reg(list,scratch_register);
  455. end;
  456. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  457. end;
  458. procedure tcgppc.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  459. reg1,reg2 : tregister;l : tasmlabel);
  460. var
  461. p: taicpu;
  462. op: tasmop;
  463. begin
  464. if cmp_op in [OC_GT,OC_LT,OC_GTE,OC_LTE] then
  465. op := A_CMP
  466. else op := A_CMPL;
  467. list.concat(taicpu.op_reg_reg_reg(op,R_CR0,reg1,reg2));
  468. a_jmp(list,A_BC,TOpCmp2AsmCond[cmp_op],0,l);
  469. end;
  470. procedure tcgppc.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  471. begin
  472. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  473. end;
  474. procedure tcgppc.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  475. var
  476. c: tasmcond;
  477. begin
  478. c := flags_to_cond(f);
  479. a_jmp(list,A_BC,c.cond,ord(c.cr)-ord(R_CR0),l);
  480. end;
  481. procedure tcgppc.g_flags2reg(list: taasmoutput; const f: TResFlags; reg: TRegister);
  482. var
  483. testbit: byte;
  484. bitvalue: boolean;
  485. begin
  486. { get the bit to extract from the conditional register + its }
  487. { requested value (0 or 1) }
  488. testbit := (ord(f.cr) * 4);
  489. case f.flag of
  490. F_EQ,F_NE:
  491. bitvalue := f.flag = F_EQ;
  492. F_LT,F_GE:
  493. begin
  494. inc(testbit);
  495. bitvalue := f.flag = F_LT;
  496. end;
  497. F_GT,F_LE:
  498. begin
  499. inc(testbit,2);
  500. bitvalue := f.flag = F_GT;
  501. end;
  502. else
  503. internalerror(200112261);
  504. end;
  505. { load the conditional register in the destination reg }
  506. list.concat(taicpu.op_reg(A_MFCR,reg));
  507. { we will move the bit that has to be tested to bit 31 -> rotate }
  508. { left by bitpos+1 (remember, this is big-endian!) }
  509. testbit := (testbit + 1) and 31;
  510. { extract bit }
  511. list.concat(taicpu.op_reg_reg_const_const_const(
  512. A_RLWINM,reg,reg,testbit,31,31));
  513. { if we need the inverse, xor with 1 }
  514. if not bitvalue then
  515. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  516. end;
  517. (*
  518. procedure tcgppc.g_cond2reg(list: taasmoutput; const f: TAsmCond; reg: TRegister);
  519. var
  520. testbit: byte;
  521. bitvalue: boolean;
  522. begin
  523. { get the bit to extract from the conditional register + its }
  524. { requested value (0 or 1) }
  525. case f.simple of
  526. false:
  527. begin
  528. { we don't generate this in the compiler }
  529. internalerror(200109062);
  530. end;
  531. true:
  532. case f.cond of
  533. C_None:
  534. internalerror(200109063);
  535. C_LT..C_NU:
  536. begin
  537. testbit := (ord(f.cr) - ord(R_CR0))*4;
  538. inc(testbit,AsmCondFlag2BI[f.cond]);
  539. bitvalue := AsmCondFlagTF[f.cond];
  540. end;
  541. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  542. begin
  543. testbit := f.crbit
  544. bitvalue := AsmCondFlagTF[f.cond];
  545. end;
  546. else
  547. internalerror(200109064);
  548. end;
  549. end;
  550. { load the conditional register in the destination reg }
  551. list.concat(taicpu.op_reg_reg(A_MFCR,reg));
  552. { we will move the bit that has to be tested to bit 31 -> rotate }
  553. { left by bitpos+1 (remember, this is big-endian!) }
  554. if bitpos <> 31 then
  555. inc(bitpos)
  556. else
  557. bitpos := 0;
  558. { extract bit }
  559. list.concat(taicpu.op_reg_reg_const_const_const(
  560. A_RLWINM,reg,reg,bitpos,31,31));
  561. { if we need the inverse, xor with 1 }
  562. if not bitvalue then
  563. list.concat(taicpu.op_reg_reg_const(A_XORI,reg,reg,1));
  564. end;
  565. *)
  566. { *********** entry/exit code and address loading ************ }
  567. procedure tcgppc.g_stackframe_entry(list : taasmoutput;localsize : longint);
  568. begin
  569. case target_info.target of
  570. target_powerpc_macos:
  571. g_stackframe_entry_mac(list,localsize);
  572. target_powerpc_linux:
  573. g_stackframe_entry_sysv(list,localsize)
  574. else
  575. internalerror(2204001);
  576. end;
  577. end;
  578. procedure tcgppc.g_stackframe_entry_sysv(list : taasmoutput;localsize : longint);
  579. { generated the entry code of a procedure/function. Note: localsize is the }
  580. { sum of the size necessary for local variables and the maximum possible }
  581. { combined size of ALL the parameters of a procedure called by the current }
  582. { one }
  583. var regcounter: TRegister;
  584. href : treference;
  585. begin
  586. if (localsize mod 8) <> 0 then internalerror(58991);
  587. { CR and LR only have to be saved in case they are modified by the current }
  588. { procedure, but currently this isn't checked, so save them always }
  589. { following is the entry code as described in "Altivec Programming }
  590. { Interface Manual", bar the saving of AltiVec registers }
  591. a_reg_alloc(list,STACK_POINTER_REG);
  592. a_reg_alloc(list,R_0);
  593. { allocate registers containing reg parameters }
  594. for regcounter := R_3 to R_10 do
  595. a_reg_alloc(list,regcounter);
  596. { save return address... }
  597. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_LR));
  598. { ... in caller's frame }
  599. reference_reset_base(href,STACK_POINTER_REG,4);
  600. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  601. a_reg_dealloc(list,R_0);
  602. a_reg_alloc(list,R_11);
  603. { save end of fpr save area }
  604. list.concat(taicpu.op_reg_reg_const(A_ORI,R_11,STACK_POINTER_REG,0));
  605. a_reg_alloc(list,R_12);
  606. { 0 or 8 based on SP alignment }
  607. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  608. R_12,STACK_POINTER_REG,0,28,28));
  609. { add in stack length }
  610. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  611. -localsize));
  612. { establish new alignment }
  613. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  614. a_reg_dealloc(list,R_12);
  615. { save floating-point registers }
  616. { !!! has to be optimized: only save registers that are used }
  617. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savefpr_14'),0));
  618. { compute end of gpr save area }
  619. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,-144));
  620. { save gprs and fetch GOT pointer }
  621. { !!! has to be optimized: only save registers that are used }
  622. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savegpr_14_go'),0));
  623. a_reg_alloc(list,R_31);
  624. { place GOT ptr in r31 }
  625. list.concat(taicpu.op_reg_reg(A_MFSPR,R_31,R_LR));
  626. { save the CR if necessary ( !!! always done currently ) }
  627. { still need to find out where this has to be done for SystemV
  628. a_reg_alloc(list,R_0);
  629. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR);
  630. list.concat(taicpu.op_reg_ref(A_STW,scratch_register,
  631. new_reference(STACK_POINTER_REG,LA_CR)));
  632. a_reg_dealloc(list,R_0); }
  633. { save pointer to incoming arguments }
  634. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_30,R_11,144));
  635. { now comes the AltiVec context save, not yet implemented !!! }
  636. end;
  637. procedure tcgppc.g_stackframe_entry_mac(list : taasmoutput;localsize : longint);
  638. { generated the entry code of a procedure/function. Note: localsize is the }
  639. { sum of the size necessary for local variables and the maximum possible }
  640. { combined size of ALL the parameters of a procedure called by the current }
  641. { one }
  642. var regcounter: TRegister;
  643. href : treference;
  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. reference_reset_base(href,STACK_POINTER_REG,8);
  659. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  660. a_reg_dealloc(list,R_0);
  661. { save floating-point registers }
  662. { !!! has to be optimized: only save registers that are used }
  663. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_savef14'),0));
  664. { save gprs in gpr save area }
  665. { !!! has to be optimized: only save registers that are used }
  666. reference_reset_base(href,STACK_POINTER_REG,-220);
  667. list.concat(taicpu.op_reg_ref(A_STMW,R_13,href));
  668. { save the CR if necessary ( !!! always done currently ) }
  669. a_reg_alloc(list,R_0);
  670. list.concat(taicpu.op_reg_reg(A_MFSPR,R_0,R_CR));
  671. reference_reset_base(href,stack_pointer_reg,LA_CR);
  672. list.concat(taicpu.op_reg_ref(A_STW,R_0,href));
  673. a_reg_dealloc(list,R_0);
  674. { save pointer to incoming arguments }
  675. list.concat(taicpu.op_reg_reg_const(A_ORI,R_31,STACK_POINTER_REG,0));
  676. a_reg_alloc(list,R_12);
  677. { 0 or 8 based on SP alignment }
  678. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM,
  679. R_12,STACK_POINTER_REG,0,28,28));
  680. { add in stack length }
  681. list.concat(taicpu.op_reg_reg_const(A_SUBFIC,R_12,R_12,
  682. -localsize));
  683. { establish new alignment }
  684. list.concat(taicpu.op_reg_reg_reg(A_STWUX,STACK_POINTER_REG,STACK_POINTER_REG,R_12));
  685. a_reg_dealloc(list,R_12);
  686. { now comes the AltiVec context save, not yet implemented !!! }
  687. end;
  688. procedure tcgppc.g_restore_frame_pointer(list : taasmoutput);
  689. begin
  690. { no frame pointer on the PowerPC (maybe there is one in the SystemV ABI?)}
  691. end;
  692. procedure tcgppc.g_return_from_proc(list : taasmoutput;parasize : aword);
  693. begin
  694. case target_info.target of
  695. target_powerpc_macos:
  696. g_return_from_proc_mac(list,parasize);
  697. target_powerpc_linux:
  698. g_return_from_proc_sysv(list,parasize)
  699. else
  700. internalerror(2204001);
  701. end;
  702. end;
  703. procedure tcgppc.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  704. var tmpreg: tregister;
  705. ref2, tmpref: treference;
  706. begin
  707. ref2 := ref;
  708. FixRef(list,ref2);
  709. if assigned(ref2.symbol) then
  710. { add the symbol's value to the base of the reference, and if the }
  711. { reference doesn't have a base, create one }
  712. begin
  713. tmpreg := get_scratch_reg_address(list);
  714. reference_reset(tmpref);
  715. tmpref.symbol := ref2.symbol;
  716. tmpref.symaddr := refs_ha;
  717. // tmpref.is_immediate := true;
  718. if ref2.base <> R_NO then
  719. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  720. ref2.base,tmpref))
  721. else
  722. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  723. ref2.base := tmpreg;
  724. ref2.symaddr := refs_l;
  725. { can be folded with one of the next instructions by the }
  726. { optimizer probably }
  727. list.concat(taicpu.op_reg_reg_ref(A_ADDI,tmpreg,tmpreg,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. reference_reset(src);
  759. reference_reset(dst);
  760. { load the address of source into src.base }
  761. src.base := get_scratch_reg_address(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_address(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_int(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,src));
  791. list.concat(taicpu.op_reg_reg_const(A_CMPI,R_CR0,countreg,0));
  792. list.concat(taicpu.op_reg_ref(A_STWU,tempreg,dst));
  793. list.concat(taicpu.op_reg_reg_const(A_SUBI,countreg,countreg,1));
  794. a_jmp(list,A_BC,C_NE,0,lab);
  795. free_scratch_reg(list,countreg);
  796. end
  797. else
  798. { unrolled loop }
  799. begin
  800. tempreg := get_scratch_reg_int(list);
  801. for count2 := 1 to count do
  802. begin
  803. a_load_ref_reg(list,OS_32,src,tempreg);
  804. a_load_reg_ref(list,OS_32,tempreg,dst);
  805. inc(src.offset,4);
  806. inc(dst.offset,4);
  807. end
  808. end;
  809. { copy the leftovers }
  810. if (len and 2) <> 0 then
  811. begin
  812. a_load_ref_reg(list,OS_16,src,tempreg);
  813. a_load_reg_ref(list,OS_16,tempreg,dst);
  814. inc(src.offset,2);
  815. inc(dst.offset,2);
  816. end;
  817. if (len and 1) <> 0 then
  818. begin
  819. a_load_ref_reg(list,OS_8,src,tempreg);
  820. a_load_reg_ref(list,OS_8,tempreg,dst);
  821. end;
  822. a_reg_dealloc(list,tempreg);
  823. free_scratch_reg(list,src.base);
  824. free_scratch_reg(list,dst.base);
  825. end;
  826. procedure tcgppc.g_overflowcheck(list: taasmoutput; const p: tnode);
  827. var
  828. hl : tasmlabel;
  829. begin
  830. if not(cs_check_overflow in aktlocalswitches) then
  831. exit;
  832. getlabel(hl);
  833. if not ((p.resulttype.def.deftype=pointerdef) or
  834. ((p.resulttype.def.deftype=orddef) and
  835. (torddef(p.resulttype.def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  836. bool8bit,bool16bit,bool32bit]))) then
  837. begin
  838. list.concat(taicpu.op_reg(A_MCRXR,R_CR7));
  839. a_jmp(list,A_BC,C_OV,7,hl)
  840. end
  841. else
  842. a_jmp_cond(list,OC_AE,hl);
  843. a_call_name(list,'FPC_OVERFLOW');
  844. a_label(list,hl);
  845. end;
  846. {***************** This is private property, keep out! :) *****************}
  847. procedure tcgppc.g_return_from_proc_sysv(list : taasmoutput;parasize : aword);
  848. var
  849. regcounter: TRegister;
  850. begin
  851. { release parameter registers }
  852. for regcounter := R_3 to R_10 do
  853. a_reg_dealloc(list,regcounter);
  854. { AltiVec context restore, not yet implemented !!! }
  855. { address of gpr save area to r11 }
  856. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_31,-144));
  857. { restore gprs }
  858. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restgpr_14'),0));
  859. { address of fpr save area to r11 }
  860. list.concat(taicpu.op_reg_reg_const(A_ADDI,R_11,R_11,144));
  861. { restore fprs and return }
  862. list.concat(taicpu.op_sym_ofs(A_BL,newasmsymbol('_restfpr_14_x'),0));
  863. end;
  864. procedure tcgppc.g_return_from_proc_mac(list : taasmoutput;parasize : aword);
  865. var
  866. regcounter: TRegister;
  867. href : treference;
  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. reference_reset_base(href,STACK_POINTER_REG,-220);
  877. list.concat(taicpu.op_reg_ref(A_LMW,R_13,href));
  878. { restore return address ... }
  879. reference_reset_base(href,STACK_POINTER_REG,8);
  880. list.concat(taicpu.op_reg_ref(A_LWZ,R_0,href));
  881. { ... and return from _restf14 }
  882. list.concat(taicpu.op_sym_ofs(A_B,newasmsymbol('_restf14'),0));
  883. end;
  884. procedure tcgppc.fixref(list: taasmoutput; var ref: treference);
  885. begin
  886. If (ref.base <> R_NO) then
  887. begin
  888. if (ref.index <> R_NO) and
  889. ((ref.offset <> 0) or assigned(ref.symbol)) then
  890. begin
  891. if not assigned(ref.symbol) and
  892. (cardinal(ref.offset-low(smallint)) <=
  893. high(smallint)-low(smallint)) then
  894. begin
  895. list.concat(taicpu.op_reg_reg_const(
  896. A_ADDI,ref.base,ref.base,ref.offset));
  897. ref.offset := 0;
  898. end
  899. else
  900. begin
  901. list.concat(taicpu.op_reg_reg_reg(
  902. A_ADD,ref.base,ref.base,ref.index));
  903. ref.index := R_NO;
  904. end;
  905. end
  906. end
  907. else
  908. begin
  909. ref.base := ref.index;
  910. ref.index := R_NO
  911. end
  912. end;
  913. { find out whether a is of the form 11..00..11b or 00..11...00. If }
  914. { that's the case, we can use rlwinm to do an AND operation }
  915. function tcgppc.get_rlwi_const(a: longint; var l1, l2: longint): boolean;
  916. var
  917. temp, testbit: longint;
  918. compare: boolean;
  919. begin
  920. get_rlwi_const := false;
  921. { start with the lowest bit }
  922. testbit := 1;
  923. { check its value }
  924. compare := boolean(a and testbit);
  925. { find out how long the run of bits with this value is }
  926. { (it's impossible that all bits are 1 or 0, because in that case }
  927. { this function wouldn't have been called) }
  928. l1 := 31;
  929. while (((a and testbit) <> 0) = compare) do
  930. begin
  931. testbit := testbit shl 1;
  932. dec(l1);
  933. end;
  934. { check the length of the run of bits that comes next }
  935. compare := not compare;
  936. l2 := l1;
  937. while (((a and testbit) <> 0) = compare) and
  938. (l2 >= 0) do
  939. begin
  940. testbit := testbit shl 1;
  941. dec(l2);
  942. end;
  943. { and finally the check whether the rest of the bits all have the }
  944. { same value }
  945. compare := not compare;
  946. temp := l2;
  947. if temp >= 0 then
  948. if (a shr (31-temp)) <> ((-ord(compare)) shr (31-temp)) then
  949. exit;
  950. { we have done "not(not(compare))", so compare is back to its }
  951. { initial value. If the lowest bit was 0, a is of the form }
  952. { 00..11..00 and we need "rlwinm reg,reg,0,l2+1,l1", (+1 }
  953. { because l2 now contains the position of the last zero of the }
  954. { first run instead of that of the first 1) so switch l1 and l2 }
  955. { in that case (we will generate "rlwinm reg,reg,0,l1,l2") }
  956. if not compare then
  957. begin
  958. temp := l1;
  959. l1 := l2+1;
  960. l2 := temp;
  961. end
  962. else
  963. { otherwise, l1 currently contains the position of the last }
  964. { zero instead of that of the first 1 of the second run -> +1 }
  965. inc(l1);
  966. { the following is the same as "if l1 = -1 then l1 := 31;" }
  967. l1 := l1 and 31;
  968. l2 := l2 and 31;
  969. get_rlwi_const := true;
  970. end;
  971. procedure tcgppc.a_load_store(list:taasmoutput;op: tasmop;reg:tregister;
  972. ref: treference);
  973. var
  974. tmpreg: tregister;
  975. tmpref: treference;
  976. begin
  977. if assigned(ref.symbol) then
  978. begin
  979. tmpreg := get_scratch_reg_address(list);
  980. reference_reset(tmpref);
  981. tmpref.symbol := ref.symbol;
  982. tmpref.symaddr := refs_ha;
  983. // tmpref.is_immediate := true;
  984. if ref.base <> R_NO then
  985. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  986. ref.base,tmpref))
  987. else
  988. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  989. ref.base := tmpreg;
  990. ref.symaddr := refs_l;
  991. end;
  992. list.concat(taicpu.op_reg_ref(op,reg,ref));
  993. if assigned(ref.symbol) then
  994. free_scratch_reg(list,tmpreg);
  995. end;
  996. procedure tcgppc.a_jmp(list: taasmoutput; op: tasmop; c: tasmcondflag;
  997. crval: longint; l: tasmlabel);
  998. var
  999. p: taicpu;
  1000. begin
  1001. p := taicpu.op_sym(op,newasmsymbol(l.name));
  1002. create_cond_norm(c,crval,p.condition);
  1003. p.is_jmp := true;
  1004. list.concat(p)
  1005. end;
  1006. begin
  1007. cg := tcgppc.create;
  1008. end.
  1009. {
  1010. $Log$
  1011. Revision 1.20 2002-07-07 09:44:31 florian
  1012. * powerpc target fixed, very simple units can be compiled
  1013. Revision 1.19 2002/05/20 13:30:41 carl
  1014. * bugfix of hdisponen (base must be set, not index)
  1015. * more portability fixes
  1016. Revision 1.18 2002/05/18 13:34:26 peter
  1017. * readded missing revisions
  1018. Revision 1.17 2002/05/16 19:46:53 carl
  1019. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1020. + try to fix temp allocation (still in ifdef)
  1021. + generic constructor calls
  1022. + start of tassembler / tmodulebase class cleanup
  1023. Revision 1.14 2002/05/13 19:52:46 peter
  1024. * a ppcppc can be build again
  1025. Revision 1.13 2002/04/20 21:41:51 carl
  1026. * renamed some constants
  1027. Revision 1.12 2002/04/06 18:13:01 jonas
  1028. * several powerpc-related additions and fixes
  1029. Revision 1.11 2002/01/02 14:53:04 jonas
  1030. * fixed small bug in a_jmp_flags
  1031. }