cgcpu.pas 56 KB

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