cgcpu.pas 50 KB

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