cgcpu.pas 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the code generator for the PowerPC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cgcpu;
  18. {$I fpcdefs.inc}
  19. interface
  20. uses
  21. globtype, symtype, symdef, symsym,
  22. cgbase, cgobj,cgppc,
  23. aasmbase, aasmcpu, aasmtai,aasmdata,
  24. cpubase, cpuinfo, cgutils, rgcpu,
  25. parabase;
  26. type
  27. tcgppc = class(tcgppcgen)
  28. procedure init_register_allocators; override;
  29. procedure done_register_allocators; override;
  30. procedure a_call_name(list: TAsmList; const s: string; weak: boolean); override;
  31. procedure a_call_reg(list: TAsmList; reg: tregister); override;
  32. procedure a_op_const_reg(list: TAsmList; Op: TOpCG; size: TCGSize; a:
  33. aint; reg: TRegister); override;
  34. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src,
  35. dst: TRegister); override;
  36. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  37. size: tcgsize; a: aint; src, dst: tregister); override;
  38. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  39. size: tcgsize; src1, src2, dst: tregister); override;
  40. { move instructions }
  41. procedure a_load_const_reg(list: TAsmList; size: tcgsize; a: aint; reg:
  42. tregister); override;
  43. { loads the memory pointed to by ref into register reg }
  44. procedure a_load_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const
  45. Ref: treference; reg: tregister); override;
  46. procedure a_load_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1,
  47. reg2: tregister); override;
  48. { comparison operations }
  49. procedure a_cmp_const_reg_label(list: TAsmList; size: tcgsize; cmp_op:
  50. topcmp; a: aint; reg: tregister;
  51. l: tasmlabel); override;
  52. procedure a_cmp_reg_reg_label(list: TAsmList; size: tcgsize; cmp_op:
  53. topcmp; reg1, reg2: tregister; l: tasmlabel); override;
  54. procedure a_jmp_name(list: TAsmList; const s: string); override;
  55. procedure a_jmp_always(list: TAsmList; l: tasmlabel); override;
  56. { need to override this for ppc64 to avoid calling CG methods which allocate
  57. registers during creation of the interface wrappers to subtract ioffset from
  58. the self pointer. But register allocation does not take place for them (which
  59. would probably be the generic fix) so we need to have a specialized method
  60. that uses the R11 scratch register in these cases.
  61. At the same time this allows > 32 bit offsets as well.
  62. }
  63. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);override;
  64. procedure g_profilecode(list: TAsmList); override;
  65. procedure g_proc_entry(list: TAsmList; localsize: longint; nostackframe:
  66. boolean); override;
  67. procedure g_proc_exit(list: TAsmList; parasize: longint; nostackframe:
  68. boolean); override;
  69. procedure g_save_registers(list: TAsmList); override;
  70. procedure g_restore_registers(list: TAsmList); override;
  71. procedure a_loadaddr_ref_reg(list: TAsmList; const ref: treference; r:
  72. tregister); override;
  73. procedure g_concatcopy(list: TAsmList; const source, dest: treference;
  74. len: aint); override;
  75. private
  76. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  77. { returns whether a reference can be used immediately in a powerpc }
  78. { instruction }
  79. function issimpleref(const ref: treference): boolean;
  80. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  81. procedure a_load_store(list: TAsmList; op: tasmop; reg: tregister;
  82. ref: treference); override;
  83. { returns the lowest numbered FP register in use, and the number of used FP registers
  84. for the current procedure }
  85. procedure calcFirstUsedFPR(out firstfpr : TSuperRegister; out fprcount : aint);
  86. { returns the lowest numbered GP register in use, and the number of used GP registers
  87. for the current procedure }
  88. procedure calcFirstUsedGPR(out firstgpr : TSuperRegister; out gprcount : aint);
  89. { generates code to call a method with the given string name. The boolean options
  90. control code generation. If prependDot is true, a single dot character is prepended to
  91. the string, if addNOP is true a single NOP instruction is added after the call, and
  92. if includeCall is true, the method is marked as having a call, not if false. This
  93. option is particularly useful to prevent generation of a larger stack frame for the
  94. register save and restore helper functions. }
  95. procedure a_call_name_direct(list: TAsmList; opc: tasmop; s: string; weak: boolean; prependDot : boolean;
  96. addNOP : boolean; includeCall : boolean = true);
  97. procedure a_jmp_name_direct(list : TAsmList; opc: tasmop; s : string; prependDot : boolean);
  98. { emits code to store the given value a into the TOC (if not already in there), and load it from there
  99. as well }
  100. procedure loadConstantPIC(list : TAsmList; size : TCGSize; a : aint; reg : TRegister);
  101. procedure profilecode_savepara(para : tparavarsym; list : TAsmList);
  102. procedure profilecode_restorepara(para : tparavarsym; list : TAsmList);
  103. end;
  104. procedure create_codegen;
  105. const
  106. TShiftOpCG2AsmOpConst : array[boolean, OP_SAR..OP_SHR] of TAsmOp = (
  107. (A_SRAWI, A_SLWI, A_SRWI), (A_SRADI, A_SLDI, A_SRDI)
  108. );
  109. implementation
  110. uses
  111. sysutils, cclasses,
  112. globals, verbose, systems, cutils,
  113. symconst, fmodule,
  114. rgobj, tgobj, cpupi, procinfo, paramgr, cpupara;
  115. function is_signed_cgsize(const size : TCgSize) : Boolean;
  116. begin
  117. case size of
  118. OS_S8,OS_S16,OS_S32,OS_S64 : result := true;
  119. OS_8,OS_16,OS_32,OS_64 : result := false;
  120. else
  121. internalerror(2006050701);
  122. end;
  123. end;
  124. { finds positive and negative powers of two of the given value, returning the
  125. power and whether it's a negative power or not in addition to the actual result
  126. of the function }
  127. function ispowerof2(value : aInt; out power : byte; out neg : boolean) : boolean;
  128. var
  129. i : longint;
  130. hl : aInt;
  131. begin
  132. result := false;
  133. neg := false;
  134. { also try to find negative power of two's by negating if the
  135. value is negative. low(aInt) is special because it can not be
  136. negated. Simply return the appropriate values for it }
  137. if (value < 0) then begin
  138. neg := true;
  139. if (value = low(aInt)) then begin
  140. power := sizeof(aInt)*8-1;
  141. result := true;
  142. exit;
  143. end;
  144. value := -value;
  145. end;
  146. if ((value and (value-1)) <> 0) then begin
  147. result := false;
  148. exit;
  149. end;
  150. hl := 1;
  151. for i := 0 to (sizeof(aInt)*8-1) do begin
  152. if (hl = value) then begin
  153. result := true;
  154. power := i;
  155. exit;
  156. end;
  157. hl := hl shl 1;
  158. end;
  159. end;
  160. { returns the number of instruction required to load the given integer into a register.
  161. This is basically a stripped down version of a_load_const_reg, increasing a counter
  162. instead of emitting instructions. }
  163. function getInstructionLength(a : aint) : longint;
  164. function get32bitlength(a : longint; var length : longint) : boolean; inline;
  165. var
  166. is_half_signed : byte;
  167. begin
  168. { if the lower 16 bits are zero, do a single LIS }
  169. if (smallint(a) = 0) and ((a shr 16) <> 0) then begin
  170. inc(length);
  171. get32bitlength := longint(a) < 0;
  172. end else begin
  173. is_half_signed := ord(smallint(lo(a)) < 0);
  174. inc(length);
  175. if smallint(hi(a) + is_half_signed) <> 0 then
  176. inc(length);
  177. get32bitlength := (smallint(a) < 0) or (a < 0);
  178. end;
  179. end;
  180. var
  181. extendssign : boolean;
  182. begin
  183. result := 0;
  184. if (lo(a) = 0) and (hi(a) <> 0) then begin
  185. get32bitlength(hi(a), result);
  186. inc(result);
  187. end else begin
  188. extendssign := get32bitlength(lo(a), result);
  189. if (extendssign) and (hi(a) = 0) then
  190. inc(result)
  191. else if (not
  192. ((extendssign and (longint(hi(a)) = -1)) or
  193. ((not extendssign) and (hi(a)=0)))
  194. ) then begin
  195. get32bitlength(hi(a), result);
  196. inc(result);
  197. end;
  198. end;
  199. end;
  200. procedure tcgppc.init_register_allocators;
  201. begin
  202. inherited init_register_allocators;
  203. if (target_info.system <> system_powerpc64_darwin) then
  204. // r13 is tls, do not use, r2 is not available
  205. rg[R_INTREGISTER] := trgintcpu.create(R_INTREGISTER, R_SUBWHOLE,
  206. [{$ifdef user0} RS_R0, {$endif} RS_R3, RS_R4, RS_R5, RS_R6, RS_R7, RS_R8,
  207. RS_R9, RS_R10, RS_R11, RS_R12, RS_R31, RS_R30, RS_R29,
  208. RS_R28, RS_R27, RS_R26, RS_R25, RS_R24, RS_R23, RS_R22,
  209. RS_R21, RS_R20, RS_R19, RS_R18, RS_R17, RS_R16, RS_R15,
  210. RS_R14], first_int_imreg, [])
  211. else
  212. { special for darwin/ppc64: r2 available volatile, r13 = tls }
  213. rg[R_INTREGISTER] := trgintcpu.create(R_INTREGISTER, R_SUBWHOLE,
  214. [{$ifdef user0} RS_R0, {$endif} RS_R2, RS_R3, RS_R4, RS_R5, RS_R6, RS_R7, RS_R8,
  215. RS_R9, RS_R10, RS_R11, RS_R12, RS_R31, RS_R30, RS_R29,
  216. RS_R28, RS_R27, RS_R26, RS_R25, RS_R24, RS_R23, RS_R22,
  217. RS_R21, RS_R20, RS_R19, RS_R18, RS_R17, RS_R16, RS_R15,
  218. RS_R14], first_int_imreg, []);
  219. rg[R_FPUREGISTER] := trgcpu.create(R_FPUREGISTER, R_SUBNONE,
  220. [RS_F0, RS_F1, RS_F2, RS_F3, RS_F4, RS_F5, RS_F6, RS_F7, RS_F8, RS_F9,
  221. RS_F10, RS_F11, RS_F12, RS_F13, RS_F31, RS_F30, RS_F29, RS_F28, RS_F27,
  222. RS_F26, RS_F25, RS_F24, RS_F23, RS_F22, RS_F21, RS_F20, RS_F19, RS_F18,
  223. RS_F17, RS_F16, RS_F15, RS_F14], first_fpu_imreg, []);
  224. { TODO: FIX ME}
  225. rg[R_MMREGISTER] := trgcpu.create(R_MMREGISTER, R_SUBNONE,
  226. [RS_M0, RS_M1, RS_M2], first_mm_imreg, []);
  227. end;
  228. procedure tcgppc.done_register_allocators;
  229. begin
  230. rg[R_INTREGISTER].free;
  231. rg[R_FPUREGISTER].free;
  232. rg[R_MMREGISTER].free;
  233. inherited done_register_allocators;
  234. end;
  235. { calling a procedure by name }
  236. procedure tcgppc.a_call_name(list: TAsmList; const s: string; weak: boolean);
  237. begin
  238. if (target_info.system <> system_powerpc64_darwin) then
  239. a_call_name_direct(list, A_BL, s, weak, target_info.system=system_powerpc64_aix, true)
  240. else
  241. begin
  242. list.concat(taicpu.op_sym(A_BL,get_darwin_call_stub(s,weak)));
  243. include(current_procinfo.flags,pi_do_call);
  244. end;
  245. end;
  246. procedure tcgppc.a_call_name_direct(list: TAsmList; opc: tasmop; s: string; weak: boolean; prependDot : boolean; addNOP : boolean; includeCall : boolean);
  247. begin
  248. if (prependDot) then
  249. s := '.' + s;
  250. if not(weak) then
  251. list.concat(taicpu.op_sym(opc, current_asmdata.RefAsmSymbol(s,AT_FUNCTION)))
  252. else
  253. list.concat(taicpu.op_sym(opc, current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION)));
  254. if (addNOP) then
  255. list.concat(taicpu.op_none(A_NOP));
  256. if (includeCall) and
  257. assigned(current_procinfo) then
  258. include(current_procinfo.flags, pi_do_call);
  259. end;
  260. function get_rtoc_offset: longint;
  261. begin
  262. result:=0;
  263. case target_info.abi of
  264. abi_powerpc_aix,
  265. abi_powerpc_darwin:
  266. result:=LA_RTOC_AIX;
  267. abi_powerpc_elfv1:
  268. result:=LA_RTOC_SYSV;
  269. abi_powerpc_elfv2:
  270. result:=LA_RTOC_ELFV2;
  271. else
  272. internalerror(2015021001);
  273. end;
  274. end;
  275. { calling a procedure by address }
  276. procedure tcgppc.a_call_reg(list: TAsmList; reg: tregister);
  277. var
  278. tmpref: treference;
  279. tempreg : TRegister;
  280. begin
  281. if (target_info.abi<>abi_powerpc_sysv) then
  282. inherited a_call_reg(list,reg)
  283. else if (not (cs_opt_size in current_settings.optimizerswitches)) then begin
  284. tempreg := getintregister(list, OS_INT);
  285. { load actual function entry (reg contains the reference to the function descriptor)
  286. into tempreg }
  287. reference_reset_base(tmpref, reg, 0, ctempposinvalid, sizeof(pint), []);
  288. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, tempreg);
  289. { save TOC pointer in stackframe }
  290. reference_reset_base(tmpref, NR_STACK_POINTER_REG, get_rtoc_offset, ctempposinvalid, 8, []);
  291. a_load_reg_ref(list, OS_ADDR, OS_ADDR, NR_RTOC, tmpref);
  292. { move actual function pointer to CTR register }
  293. list.concat(taicpu.op_reg(A_MTCTR, tempreg));
  294. { load new TOC pointer from function descriptor into RTOC register }
  295. reference_reset_base(tmpref, reg, tcgsize2size[OS_ADDR], ctempposinvalid, 8, []);
  296. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, NR_RTOC);
  297. { load new environment pointer from function descriptor into R11 register }
  298. reference_reset_base(tmpref, reg, 2*tcgsize2size[OS_ADDR], ctempposinvalid, 8, []);
  299. a_reg_alloc(list, NR_R11);
  300. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, NR_R11);
  301. { call function }
  302. list.concat(taicpu.op_none(A_BCTRL));
  303. a_reg_dealloc(list, NR_R11);
  304. end else begin
  305. { call ptrgl helper routine which expects the pointer to the function descriptor
  306. in R11 }
  307. a_reg_alloc(list, NR_R11);
  308. a_load_reg_reg(list, OS_ADDR, OS_ADDR, reg, NR_R11);
  309. a_call_name_direct(list, A_BL, '.ptrgl', false, false, false);
  310. a_reg_dealloc(list, NR_R11);
  311. end;
  312. { we need to load the old RTOC from stackframe because we changed it}
  313. reference_reset_base(tmpref, NR_STACK_POINTER_REG, get_rtoc_offset, ctempposinvalid, 8, []);
  314. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, NR_RTOC);
  315. include(current_procinfo.flags, pi_do_call);
  316. end;
  317. {********************** load instructions ********************}
  318. procedure tcgppc.a_load_const_reg(list: TAsmList; size: TCGSize; a: aint;
  319. reg: TRegister);
  320. { loads a 32 bit constant into the given register, using an optimal instruction sequence.
  321. This is either LIS, LI or LI+ADDIS.
  322. Returns true if during these operations the upper 32 bits were filled with 1 bits (e.g.
  323. sign extension was performed) }
  324. function load32bitconstant(list : TAsmList; size : TCGSize; a : longint;
  325. reg : TRegister) : boolean;
  326. var
  327. is_half_signed : byte;
  328. begin
  329. { if the lower 16 bits are zero, do a single LIS }
  330. if (smallint(a) = 0) and ((a shr 16) <> 0) then begin
  331. list.concat(taicpu.op_reg_const(A_LIS, reg, smallint(hi(a))));
  332. load32bitconstant := longint(a) < 0;
  333. end else begin
  334. is_half_signed := ord(smallint(lo(a)) < 0);
  335. list.concat(taicpu.op_reg_const(A_LI, reg, smallint(a and $ffff)));
  336. if smallint(hi(a) + is_half_signed) <> 0 then begin
  337. list.concat(taicpu.op_reg_reg_const(A_ADDIS, reg, reg, smallint(hi(a) + is_half_signed)));
  338. end;
  339. load32bitconstant := (smallint(a) < 0) or (a < 0);
  340. end;
  341. end;
  342. { loads a 32 bit constant into R0, using an optimal instruction sequence.
  343. This is either LIS, LI or LI+ORIS.
  344. Returns true if during these operations the upper 32 bits were filled with 1 bits (e.g.
  345. sign extension was performed) }
  346. function load32bitconstantR0(list : TAsmList; size : TCGSize; a : longint) : boolean;
  347. begin
  348. { if it's a value we can load with a single LI, do it }
  349. if (a >= low(smallint)) and (a <= high(smallint)) then begin
  350. list.concat(taicpu.op_reg_const(A_LI, NR_R0, smallint(a)));
  351. end else begin
  352. { if the lower 16 bits are zero, do a single LIS }
  353. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, smallint(a shr 16)));
  354. if (smallint(a) <> 0) then begin
  355. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(a)));
  356. end;
  357. end;
  358. load32bitconstantR0 := a < 0;
  359. end;
  360. { emits the code to load a constant by emitting various instructions into the output
  361. code}
  362. procedure loadConstantNormal(list: TAsmList; size : TCgSize; a: aint; reg: TRegister);
  363. var
  364. extendssign : boolean;
  365. instr : taicpu;
  366. begin
  367. if (lo(a) = 0) and (hi(a) <> 0) then begin
  368. { load only upper 32 bits, and shift }
  369. load32bitconstant(list, size, longint(hi(a)), reg);
  370. list.concat(taicpu.op_reg_reg_const(A_SLDI, reg, reg, 32));
  371. end else begin
  372. { load lower 32 bits }
  373. extendssign := load32bitconstant(list, size, longint(lo(a)), reg);
  374. if (extendssign) and (hi(a) = 0) then
  375. { if upper 32 bits are zero, but loading the lower 32 bit resulted in automatic
  376. sign extension, clear those bits }
  377. list.concat(taicpu.op_reg_reg_const_const(A_RLDICL, reg, reg, 0, 32))
  378. else if (not
  379. ((extendssign and (longint(hi(a)) = -1)) or
  380. ((not extendssign) and (hi(a)=0)))
  381. ) then begin
  382. { only load the upper 32 bits, if the automatic sign extension is not okay,
  383. that is, _not_ if
  384. - loading the lower 32 bits resulted in -1 in the upper 32 bits, and the upper
  385. 32 bits should contain -1
  386. - loading the lower 32 bits resulted in 0 in the upper 32 bits, and the upper
  387. 32 bits should contain 0 }
  388. a_reg_alloc(list, NR_R0);
  389. load32bitconstantR0(list, size, longint(hi(a)));
  390. { combine both registers }
  391. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, reg, NR_R0, 32, 0));
  392. a_reg_dealloc(list, NR_R0);
  393. end;
  394. end;
  395. end;
  396. {$IFDEF EXTDEBUG}
  397. var
  398. astring : string;
  399. {$ENDIF EXTDEBUG}
  400. begin
  401. {$IFDEF EXTDEBUG}
  402. astring := 'a_load_const_reg ' + inttostr(hi(a)) + ' ' + inttostr(lo(a)) + ' ' + inttostr(ord(size)) + ' ' + inttostr(tcgsize2size[size]) + ' ' + hexstr(a, 16);
  403. list.concat(tai_comment.create(strpnew(astring)));
  404. {$ENDIF EXTDEBUG}
  405. if not (size in [OS_8, OS_S8, OS_16, OS_S16, OS_32, OS_S32, OS_64, OS_S64]) then
  406. internalerror(2002090902);
  407. { if PIC or basic optimizations are enabled, and the number of instructions which would be
  408. required to load the value is greater than 2, store (and later load) the value from there }
  409. // if (((cs_opt_peephole in current_settings.optimizerswitches) or (cs_create_pic in current_settings.moduleswitches)) and
  410. // (getInstructionLength(a) > 2)) then
  411. // loadConstantPIC(list, size, a, reg)
  412. // else
  413. loadConstantNormal(list, size, a, reg);
  414. end;
  415. procedure tcgppc.a_load_ref_reg(list: TAsmList; fromsize, tosize: tcgsize;
  416. const ref: treference; reg: tregister);
  417. const
  418. LoadInstr: array[OS_8..OS_S64, boolean, boolean] of TAsmOp =
  419. { indexed? updating? }
  420. (((A_LBZ, A_LBZU), (A_LBZX, A_LBZUX)),
  421. ((A_LHZ, A_LHZU), (A_LHZX, A_LHZUX)),
  422. ((A_LWZ, A_LWZU), (A_LWZX, A_LWZUX)),
  423. ((A_LD, A_LDU), (A_LDX, A_LDUX)),
  424. { 128bit stuff too }
  425. ((A_NONE, A_NONE), (A_NONE, A_NONE)),
  426. { there's no load-byte-with-sign-extend :( }
  427. ((A_LBZ, A_LBZU), (A_LBZX, A_LBZUX)),
  428. ((A_LHA, A_LHAU), (A_LHAX, A_LHAUX)),
  429. { there's no load-word-arithmetic-indexed with update, simulate it in code :( }
  430. ((A_LWA, A_NOP), (A_LWAX, A_LWAUX)),
  431. ((A_LD, A_LDU), (A_LDX, A_LDUX))
  432. );
  433. var
  434. op: tasmop;
  435. ref2: treference;
  436. tmpreg: tregister;
  437. begin
  438. if target_info.system=system_powerpc64_aix then
  439. g_load_check_simple(list,ref,65536);
  440. {$IFDEF EXTDEBUG}
  441. list.concat(tai_comment.create(strpnew('a_load_ref_reg ' + ref2string(ref))));
  442. {$ENDIF EXTDEBUG}
  443. if not (fromsize in [OS_8, OS_S8, OS_16, OS_S16, OS_32, OS_S32, OS_64, OS_S64]) then
  444. internalerror(2002090904);
  445. { the caller is expected to have adjusted the reference already
  446. in this case }
  447. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  448. fromsize := tosize;
  449. ref2 := ref;
  450. fixref(list, ref2);
  451. op := loadinstr[fromsize, ref2.index <> NR_NO, false];
  452. { there is no LWAU instruction, simulate using ADDI and LWA }
  453. if (op = A_NOP) then begin
  454. list.concat(taicpu.op_reg_reg_const(A_ADDI, reg, reg, ref2.offset));
  455. ref2.offset := 0;
  456. op := A_LWA;
  457. end;
  458. a_load_store(list, op, reg, ref2);
  459. { sign extend shortint if necessary (because there is
  460. no load instruction to sign extend an 8 bit value automatically)
  461. and mask out extra sign bits when loading from a smaller
  462. signed to a larger unsigned type (where it matters) }
  463. if (fromsize = OS_S8) then begin
  464. a_load_reg_reg(list, OS_8, OS_S8, reg, reg);
  465. a_load_reg_reg(list, OS_S8, tosize, reg, reg);
  466. end else if (fromsize = OS_S16) and (tosize = OS_32) then
  467. a_load_reg_reg(list, fromsize, tosize, reg, reg);
  468. end;
  469. procedure tcgppc.a_load_reg_reg(list: TAsmList; fromsize, tosize: tcgsize;
  470. reg1, reg2: tregister);
  471. var
  472. instr: TAiCpu;
  473. bytesize : byte;
  474. begin
  475. {$ifdef extdebug}
  476. list.concat(tai_comment.create(strpnew('a_load_reg_reg from : ' + cgsize2string(fromsize) + ' to ' + cgsize2string(tosize))));
  477. {$endif}
  478. if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  479. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and (fromsize <> tosize)) or
  480. { do we need to mask out the sign when loading from smaller signed to larger unsigned type? }
  481. ( is_signed_cgsize(fromsize) and (not is_signed_cgsize(tosize)) and
  482. (tcgsize2size[fromsize] < tcgsize2size[tosize]) and (tcgsize2size[tosize] <> sizeof(pint)) ) then begin
  483. case tosize of
  484. OS_S8:
  485. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  486. OS_S16:
  487. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  488. OS_S32:
  489. instr := taicpu.op_reg_reg(A_EXTSW,reg2,reg1);
  490. OS_8, OS_16, OS_32:
  491. instr := taicpu.op_reg_reg_const_const(A_RLDICL, reg2, reg1, 0, (8-tcgsize2size[tosize])*8);
  492. OS_S64, OS_64:
  493. instr := taicpu.op_reg_reg(A_MR, reg2, reg1);
  494. else
  495. internalerror(2013113007);
  496. end;
  497. end else
  498. instr := taicpu.op_reg_reg(A_MR, reg2, reg1);
  499. list.concat(instr);
  500. rg[R_INTREGISTER].add_move_instruction(instr);
  501. end;
  502. procedure tcgppc.a_op_const_reg(list: TAsmList; Op: TOpCG; size: TCGSize; a:
  503. aint; reg: TRegister);
  504. begin
  505. a_op_const_reg_reg(list, op, size, a, reg, reg);
  506. end;
  507. procedure tcgppc.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src,
  508. dst: TRegister);
  509. begin
  510. a_op_reg_reg_reg(list, op, size, src, dst, dst);
  511. end;
  512. procedure tcgppc.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  513. size: tcgsize; a: aint; src, dst: tregister);
  514. var
  515. useReg : boolean;
  516. procedure do_lo_hi(loOp, hiOp : TAsmOp);
  517. begin
  518. { Optimization for logical ops (excluding AND), trying to do this as efficiently
  519. as possible by only generating code for the affected halfwords. Note that all
  520. the instructions handled here must have "X op 0 = X" for every halfword. }
  521. usereg := false;
  522. if (aword(a) > high(dword)) then begin
  523. usereg := true;
  524. end else begin
  525. if (word(a) <> 0) then begin
  526. list.concat(taicpu.op_reg_reg_const(loOp, dst, src, word(a)));
  527. if (word(a shr 16) <> 0) then
  528. list.concat(taicpu.op_reg_reg_const(hiOp, dst, dst, word(a shr 16)));
  529. end else if (word(a shr 16) <> 0) then
  530. list.concat(taicpu.op_reg_reg_const(hiOp, dst, src, word(a shr 16)));
  531. end;
  532. end;
  533. procedure do_lo_hi_and;
  534. begin
  535. { optimization logical and with immediate: only use "andi." for 16 bit
  536. ands, otherwise use register method. Doing this for 32 bit constants
  537. would not give any advantage to the register method (via useReg := true),
  538. requiring a scratch register and three instructions. }
  539. usereg := false;
  540. if (aword(a) > high(word)) then
  541. usereg := true
  542. else
  543. list.concat(taicpu.op_reg_reg_const(A_ANDI_, dst, src, word(a)));
  544. end;
  545. procedure do_constant_div(list : TAsmList; size : TCgSize; a : aint; src, dst : TRegister;
  546. signed : boolean);
  547. const
  548. negops : array[boolean] of tasmop = (A_NEG, A_NEGO);
  549. var
  550. magic : int64;
  551. u_magic : qword;
  552. u_shift : byte;
  553. u_add : boolean;
  554. power : byte;
  555. isNegPower : boolean;
  556. divreg : tregister;
  557. begin
  558. if (a = 0) then begin
  559. internalerror(2005061701);
  560. end else if (a = 1) then begin
  561. a_load_reg_reg(list, OS_INT, OS_INT, src, dst);
  562. end else if (a = -1) and (signed) then begin
  563. { note: only in the signed case possible..., may overflow }
  564. list.concat(taicpu.op_reg_reg(negops[cs_check_overflow in current_settings.localswitches], dst, src));
  565. end else if (ispowerof2(a, power, isNegPower)) then begin
  566. if (signed) then begin
  567. { From "The PowerPC Compiler Writer's Guide", pg. 52ff }
  568. a_op_const_reg_reg(list, OP_SAR, OS_INT, power,
  569. src, dst);
  570. list.concat(taicpu.op_reg_reg(A_ADDZE, dst, dst));
  571. if (isNegPower) then
  572. list.concat(taicpu.op_reg_reg(A_NEG, dst, dst));
  573. end else begin
  574. a_op_const_reg_reg(list, OP_SHR, OS_INT, power, src, dst)
  575. end;
  576. end else begin
  577. { replace division by multiplication, both implementations }
  578. { from "The PowerPC Compiler Writer's Guide" pg. 53ff }
  579. divreg := getintregister(list, OS_INT);
  580. if (signed) then begin
  581. calc_divconst_magic_signed(sizeof(aInt)*8, a, magic, u_shift);
  582. { load magic value }
  583. a_load_const_reg(list, OS_INT, magic, divreg);
  584. { multiply }
  585. list.concat(taicpu.op_reg_reg_reg(A_MULHD, dst, src, divreg));
  586. { add/subtract numerator }
  587. if (a > 0) and (magic < 0) then begin
  588. a_op_reg_reg_reg(list, OP_ADD, OS_INT, src, dst, dst);
  589. end else if (a < 0) and (magic > 0) then begin
  590. a_op_reg_reg_reg(list, OP_SUB, OS_INT, src, dst, dst);
  591. end;
  592. { shift shift places to the right (arithmetic) }
  593. a_op_const_reg_reg(list, OP_SAR, OS_INT, u_shift, dst, dst);
  594. { extract and add sign bit }
  595. if (a >= 0) then begin
  596. a_op_const_reg_reg(list, OP_SHR, OS_INT, 63, src, divreg);
  597. end else begin
  598. a_op_const_reg_reg(list, OP_SHR, OS_INT, 63, dst, divreg);
  599. end;
  600. a_op_reg_reg_reg(list, OP_ADD, OS_INT, dst, divreg, dst);
  601. end else begin
  602. calc_divconst_magic_unsigned(sizeof(aWord)*8, a, u_magic, u_add, u_shift);
  603. { load magic in divreg }
  604. a_load_const_reg(list, OS_INT, aint(u_magic), divreg);
  605. list.concat(taicpu.op_reg_reg_reg(A_MULHDU, dst, src, divreg));
  606. if (u_add) then begin
  607. a_op_reg_reg_reg(list, OP_SUB, OS_INT, dst, src, divreg);
  608. a_op_const_reg_reg(list, OP_SHR, OS_INT, 1, divreg, divreg);
  609. a_op_reg_reg_reg(list, OP_ADD, OS_INT, divreg, dst, divreg);
  610. a_op_const_reg_reg(list, OP_SHR, OS_INT, u_shift-1, divreg, dst);
  611. end else begin
  612. a_op_const_reg_reg(list, OP_SHR, OS_INT, u_shift, dst, dst);
  613. end;
  614. end;
  615. end;
  616. end;
  617. var
  618. scratchreg: tregister;
  619. shift : byte;
  620. shiftmask : longint;
  621. isneg : boolean;
  622. begin
  623. { subtraction is the same as addition with negative constant }
  624. if op = OP_SUB then begin
  625. a_op_const_reg_reg(list, OP_ADD, size, -a, src, dst);
  626. exit;
  627. end;
  628. {$IFDEF EXTDEBUG}
  629. list.concat(tai_comment.create(strpnew('a_op_const_reg_reg ' + cgop2string(op))));
  630. {$ENDIF EXTDEBUG}
  631. { This case includes some peephole optimizations for the various operations,
  632. (e.g. AND, OR, XOR, ..) - can't this be done at some higher level,
  633. independent of architecture? }
  634. { assume that we do not need a scratch register for the operation }
  635. useReg := false;
  636. case (op) of
  637. OP_DIV, OP_IDIV:
  638. if (cs_opt_level1 in current_settings.optimizerswitches) then
  639. do_constant_div(list, size, a, src, dst, op = OP_IDIV)
  640. else
  641. usereg := true;
  642. OP_IMUL, OP_MUL:
  643. { idea: factorize constant multiplicands and use adds/shifts with few factors;
  644. however, even a 64 bit multiply is already quite fast on PPC64 }
  645. if (a = 0) then
  646. a_load_const_reg(list, size, 0, dst)
  647. else if (a = -1) then
  648. list.concat(taicpu.op_reg_reg(A_NEG, dst, dst))
  649. else if (a = 1) then
  650. a_load_reg_reg(list, OS_INT, OS_INT, src, dst)
  651. else if ispowerof2(a, shift, isneg) then begin
  652. list.concat(taicpu.op_reg_reg_const(A_SLDI, dst, src, shift));
  653. if (isneg) then
  654. list.concat(taicpu.op_reg_reg(A_NEG, dst, dst));
  655. end else if (a >= low(smallint)) and (a <= high(smallint)) then
  656. list.concat(taicpu.op_reg_reg_const(A_MULLI, dst, src,
  657. smallint(a)))
  658. else
  659. usereg := true;
  660. OP_ADD:
  661. if (a = 0) then
  662. a_load_reg_reg(list, size, size, src, dst)
  663. else if (a >= low(smallint)) and (a <= high(smallint)) then
  664. list.concat(taicpu.op_reg_reg_const(A_ADDI, dst, src, smallint(a)))
  665. else
  666. useReg := true;
  667. OP_OR:
  668. if (a = 0) then
  669. a_load_reg_reg(list, size, size, src, dst)
  670. else if (a = -1) then
  671. a_load_const_reg(list, size, -1, dst)
  672. else
  673. do_lo_hi(A_ORI, A_ORIS);
  674. OP_AND:
  675. if (a = 0) then
  676. a_load_const_reg(list, size, 0, dst)
  677. else if (a = -1) then
  678. a_load_reg_reg(list, size, size, src, dst)
  679. else
  680. do_lo_hi_and;
  681. OP_XOR:
  682. if (a = 0) then
  683. a_load_reg_reg(list, size, size, src, dst)
  684. else if (a = -1) then
  685. list.concat(taicpu.op_reg_reg(A_NOT, dst, src))
  686. else
  687. do_lo_hi(A_XORI, A_XORIS);
  688. OP_ROL:
  689. begin
  690. if (size in [OS_64, OS_S64]) then begin
  691. list.concat(taicpu.op_reg_reg_const_const(A_RLDICL, dst, src, a and 63, 0));
  692. end else if (size in [OS_32, OS_S32]) then begin
  693. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM, dst, src, a and 31, 0, 31));
  694. end else begin
  695. internalerror(2008091303);
  696. end;
  697. end;
  698. OP_ROR:
  699. begin
  700. if (size in [OS_64, OS_S64]) then begin
  701. list.concat(taicpu.op_reg_reg_const_const(A_RLDICL, dst, src, ((64 - a) and 63), 0));
  702. end else if (size in [OS_32, OS_S32]) then begin
  703. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM, dst, src, (32 - a) and 31, 0, 31));
  704. end else begin
  705. internalerror(2008091304);
  706. end;
  707. end;
  708. OP_SHL, OP_SHR, OP_SAR:
  709. begin
  710. if (size in [OS_64, OS_S64]) then
  711. shift := 6
  712. else
  713. shift := 5;
  714. shiftmask := (1 shl shift)-1;
  715. if (a and shiftmask) <> 0 then begin
  716. list.concat(taicpu.op_reg_reg_const(
  717. TShiftOpCG2AsmOpConst[size in [OS_64, OS_S64], op], dst, src, a and shiftmask));
  718. end else
  719. a_load_reg_reg(list, size, size, src, dst);
  720. if ((a shr shift) <> 0) then
  721. internalError(68991);
  722. end
  723. else
  724. internalerror(200109091);
  725. end;
  726. { if all else failed, load the constant in a register and then
  727. perform the operation }
  728. if (useReg) then begin
  729. scratchreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  730. a_load_const_reg(list, size, a, scratchreg);
  731. a_op_reg_reg_reg(list, op, size, scratchreg, src, dst);
  732. end else
  733. maybeadjustresult(list, op, size, dst);
  734. end;
  735. procedure tcgppc.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  736. size: tcgsize; src1, src2, dst: tregister);
  737. const
  738. op_reg_reg_opcg2asmop32: array[TOpCG] of tasmop =
  739. (A_NONE, A_MR, A_ADD, A_AND, A_DIVWU, A_DIVW, A_MULLW, A_MULLW, A_NEG, A_NOT, A_OR,
  740. A_SRAW, A_SLW, A_SRW, A_SUB, A_XOR, A_NONE, A_NONE);
  741. op_reg_reg_opcg2asmop64: array[TOpCG] of tasmop =
  742. (A_NONE, A_MR, A_ADD, A_AND, A_DIVDU, A_DIVD, A_MULLD, A_MULLD, A_NEG, A_NOT, A_OR,
  743. A_SRAD, A_SLD, A_SRD, A_SUB, A_XOR, A_NONE, A_NONE);
  744. var
  745. tmpreg : TRegister;
  746. begin
  747. case op of
  748. OP_NEG, OP_NOT:
  749. begin
  750. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop64[op], dst, src1));
  751. if (op = OP_NOT) and not (size in [OS_64, OS_S64]) then
  752. { zero/sign extend result again, fromsize is not important here }
  753. a_load_reg_reg(list, OS_S64, size, dst, dst)
  754. end;
  755. OP_ROL:
  756. begin
  757. if (size in [OS_64, OS_S64]) then begin
  758. list.concat(taicpu.op_reg_reg_reg_const(A_RLDCL, dst, src2, src1, 0));
  759. end else if (size in [OS_32, OS_S32]) then begin
  760. list.concat(taicpu.op_reg_reg_reg_const_const(A_RLWNM, dst, src2, src1, 0, 31));
  761. end else begin
  762. internalerror(2008091301);
  763. end;
  764. end;
  765. OP_ROR:
  766. begin
  767. tmpreg := getintregister(list, OS_INT);
  768. list.concat(taicpu.op_reg_reg(A_NEG, tmpreg, src1));
  769. if (size in [OS_64, OS_S64]) then begin
  770. list.concat(taicpu.op_reg_reg_reg_const(A_RLDCL, dst, src2, tmpreg, 0));
  771. end else if (size in [OS_32, OS_S32]) then begin
  772. list.concat(taicpu.op_reg_reg_reg_const_const(A_RLWNM, dst, src2, tmpreg, 0, 31));
  773. end else begin
  774. internalerror(2008091302);
  775. end;
  776. end;
  777. else
  778. if (size in [OS_64, OS_S64]) then begin
  779. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop64[op], dst, src2,
  780. src1));
  781. end else begin
  782. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop32[op], dst, src2,
  783. src1));
  784. maybeadjustresult(list, op, size, dst);
  785. end;
  786. end;
  787. end;
  788. {*************** compare instructructions ****************}
  789. procedure tcgppc.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  790. cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  791. const
  792. { unsigned useconst 32bit-op }
  793. cmpop_table : array[boolean, boolean, boolean] of TAsmOp = (
  794. ((A_CMPD, A_CMPW), (A_CMPDI, A_CMPWI)),
  795. ((A_CMPLD, A_CMPLW), (A_CMPLDI, A_CMPLWI))
  796. );
  797. var
  798. tmpreg : TRegister;
  799. signed, useconst : boolean;
  800. opsize : TCgSize;
  801. op : TAsmOp;
  802. begin
  803. {$IFDEF EXTDEBUG}
  804. list.concat(tai_comment.create(strpnew('a_cmp_const_reg_label ' + cgsize2string(size) + ' ' + booltostr(cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE]) + ' ' + inttostr(a) )));
  805. {$ENDIF EXTDEBUG}
  806. signed := cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE];
  807. { in the following case, we generate more efficient code when
  808. signed is true }
  809. if (cmp_op in [OC_EQ, OC_NE]) and
  810. (aword(a) > $FFFF) then
  811. signed := true;
  812. opsize := size;
  813. { do we need to change the operand size because ppc64 only supports 32 and
  814. 64 bit compares? }
  815. if (not (size in [OS_32, OS_S32, OS_64, OS_S64])) then begin
  816. if (signed) then
  817. opsize := OS_S32
  818. else
  819. opsize := OS_32;
  820. a_load_reg_reg(list, size, opsize, reg, reg);
  821. end;
  822. { can we use immediate compares? }
  823. useconst := (signed and ( (a >= low(smallint)) and (a <= high(smallint)))) or
  824. ((not signed) and (aword(a) <= $FFFF));
  825. op := cmpop_table[not signed, useconst, opsize in [OS_32, OS_S32]];
  826. if (useconst) then begin
  827. list.concat(taicpu.op_reg_reg_const(op, NR_CR0, reg, a));
  828. end else begin
  829. tmpreg := getintregister(list, OS_INT);
  830. a_load_const_reg(list, opsize, a, tmpreg);
  831. list.concat(taicpu.op_reg_reg_reg(op, NR_CR0, reg, tmpreg));
  832. end;
  833. a_jmp(list, A_BC, TOpCmp2AsmCond[cmp_op], 0, l);
  834. end;
  835. procedure tcgppc.a_cmp_reg_reg_label(list: TAsmList; size: tcgsize;
  836. cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  837. var
  838. op: tasmop;
  839. begin
  840. {$IFDEF extdebug}
  841. list.concat(tai_comment.create(strpnew('a_cmp_reg_reg_label, size ' + cgsize2string(size) + ' op ' + inttostr(ord(cmp_op)))));
  842. {$ENDIF extdebug}
  843. {$note Commented out below check because of compiler weirdness}
  844. {
  845. if (not (size in [OS_32, OS_S32, OS_64, OS_S64])) then
  846. internalerror(200606041);
  847. }
  848. if cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE] then
  849. if (size in [OS_64, OS_S64]) then
  850. op := A_CMPD
  851. else
  852. op := A_CMPW
  853. else
  854. if (size in [OS_64, OS_S64]) then
  855. op := A_CMPLD
  856. else
  857. op := A_CMPLW;
  858. list.concat(taicpu.op_reg_reg_reg(op, NR_CR0, reg2, reg1));
  859. a_jmp(list, A_BC, TOpCmp2AsmCond[cmp_op], 0, l);
  860. end;
  861. procedure tcgppc.a_jmp_name_direct(list : TAsmList; opc: tasmop; s : string; prependDot : boolean);
  862. var
  863. p: taicpu;
  864. begin
  865. if (prependDot) then
  866. s := '.' + s;
  867. p := taicpu.op_sym(opc, current_asmdata.RefAsmSymbol(s,AT_FUNCTION));
  868. p.is_jmp := true;
  869. list.concat(p)
  870. end;
  871. procedure tcgppc.a_jmp_name(list: TAsmList; const s: string);
  872. var
  873. p: taicpu;
  874. begin
  875. if (target_info.system = system_powerpc64_darwin) then
  876. begin
  877. p := taicpu.op_sym(A_B,get_darwin_call_stub(s,false));
  878. p.is_jmp := true;
  879. list.concat(p)
  880. end
  881. else
  882. a_jmp_name_direct(list, A_B, s, true);
  883. end;
  884. procedure tcgppc.a_jmp_always(list: TAsmList; l: tasmlabel);
  885. begin
  886. a_jmp(list, A_B, C_None, 0, l);
  887. end;
  888. { *********** entry/exit code and address loading ************ }
  889. procedure tcgppc.g_save_registers(list: TAsmList);
  890. begin
  891. { this work is done in g_proc_entry; additionally it is not safe
  892. to use it because it is called at some weird time }
  893. end;
  894. procedure tcgppc.g_restore_registers(list: TAsmList);
  895. begin
  896. { this work is done in g_proc_exit; mainly because it is not safe to
  897. put the register restore code here because it is called at some weird time }
  898. end;
  899. procedure tcgppc.calcFirstUsedFPR(out firstfpr : TSuperRegister; out fprcount : aint);
  900. var
  901. reg : TSuperRegister;
  902. begin
  903. fprcount := 0;
  904. firstfpr := RS_F31;
  905. if not (po_assembler in current_procinfo.procdef.procoptions) then
  906. for reg := RS_F14 to RS_F31 do
  907. if reg in rg[R_FPUREGISTER].used_in_proc then begin
  908. fprcount := ord(RS_F31)-ord(reg)+1;
  909. firstfpr := reg;
  910. break;
  911. end;
  912. end;
  913. procedure tcgppc.calcFirstUsedGPR(out firstgpr : TSuperRegister; out gprcount : aint);
  914. var
  915. reg : TSuperRegister;
  916. begin
  917. gprcount := 0;
  918. firstgpr := RS_R31;
  919. if not (po_assembler in current_procinfo.procdef.procoptions) then
  920. for reg := RS_R14 to RS_R31 do
  921. if reg in rg[R_INTREGISTER].used_in_proc then begin
  922. gprcount := ord(RS_R31)-ord(reg)+1;
  923. firstgpr := reg;
  924. break;
  925. end;
  926. end;
  927. procedure tcgppc.profilecode_savepara(para : tparavarsym; list : TAsmList);
  928. begin
  929. case (para.paraloc[calleeside].location^.loc) of
  930. LOC_REGISTER, LOC_CREGISTER:
  931. a_load_reg_ref(list, OS_INT, para.paraloc[calleeside].Location^.size,
  932. para.paraloc[calleeside].Location^.register, para.localloc.reference);
  933. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  934. a_loadfpu_reg_ref(list, para.paraloc[calleeside].Location^.size,
  935. para.paraloc[calleeside].Location^.size,
  936. para.paraloc[calleeside].Location^.register, para.localloc.reference);
  937. LOC_MMREGISTER, LOC_CMMREGISTER:
  938. { not supported }
  939. internalerror(2006041801);
  940. else
  941. ;
  942. end;
  943. end;
  944. procedure tcgppc.profilecode_restorepara(para : tparavarsym; list : TAsmList);
  945. begin
  946. case (para.paraloc[calleeside].Location^.loc) of
  947. LOC_REGISTER, LOC_CREGISTER:
  948. a_load_ref_reg(list, para.paraloc[calleeside].Location^.size, OS_INT,
  949. para.localloc.reference, para.paraloc[calleeside].Location^.register);
  950. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  951. a_loadfpu_ref_reg(list, para.paraloc[calleeside].Location^.size,
  952. para.paraloc[calleeside].Location^.size,
  953. para.localloc.reference, para.paraloc[calleeside].Location^.register);
  954. LOC_MMREGISTER, LOC_CMMREGISTER:
  955. { not supported }
  956. internalerror(2006041802);
  957. else
  958. ;
  959. end;
  960. end;
  961. procedure tcgppc.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);
  962. var
  963. hsym : tsym;
  964. href : treference;
  965. paraloc : Pcgparalocation;
  966. begin
  967. if ((ioffset >= low(smallint)) and (ioffset < high(smallint))) then begin
  968. { the original method can handle this }
  969. inherited g_adjust_self_value(list, procdef, ioffset);
  970. exit;
  971. end;
  972. { calculate the parameter info for the procdef }
  973. procdef.init_paraloc_info(callerside);
  974. hsym:=tsym(procdef.parast.Find('self'));
  975. if not(assigned(hsym) and
  976. (hsym.typ=paravarsym)) then
  977. internalerror(2010103101);
  978. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  979. while paraloc<>nil do
  980. with paraloc^ do begin
  981. case loc of
  982. LOC_REGISTER:
  983. begin
  984. a_load_const_reg(list, size, ioffset, NR_R11);
  985. a_op_reg_reg(list, OP_SUB, size, NR_R11, register);
  986. end else
  987. internalerror(2010103102);
  988. end;
  989. paraloc:=next;
  990. end;
  991. end;
  992. procedure tcgppc.g_profilecode(list: TAsmList);
  993. begin
  994. current_procinfo.procdef.paras.ForEachCall(TObjectListCallback(@profilecode_savepara), list);
  995. a_call_name_direct(list, A_BL, '_mcount', false, false, true);
  996. current_procinfo.procdef.paras.ForEachCall(TObjectListCallback(@profilecode_restorepara), list);
  997. end;
  998. { Generates the entry code of a procedure/function.
  999. This procedure may be called before, as well as after g_return_from_proc
  1000. is called. localsize is the sum of the size necessary for local variables
  1001. and the maximum possible combined size of ALL the parameters of a procedure
  1002. called by the current one
  1003. IMPORTANT: registers are not to be allocated through the register
  1004. allocator here, because the register colouring has already occurred !!
  1005. }
  1006. procedure tcgppc.g_proc_entry(list: TAsmList; localsize: longint;
  1007. nostackframe: boolean);
  1008. var
  1009. firstregfpu, firstreggpr: TSuperRegister;
  1010. needslinkreg: boolean;
  1011. fprcount, gprcount : aint;
  1012. { Save standard registers, both FPR and GPR; does not support VMX/Altivec }
  1013. procedure save_standard_registers;
  1014. var
  1015. regcount : TSuperRegister;
  1016. href : TReference;
  1017. mayNeedLRStore : boolean;
  1018. opc : tasmop;
  1019. begin
  1020. { there are two ways to do this: manually, by generating a few "std" instructions,
  1021. or via the restore helper functions. The latter are selected by the -Og switch,
  1022. i.e. "optimize for size" }
  1023. if (cs_opt_size in current_settings.optimizerswitches) and
  1024. (target_info.system <> system_powerpc64_darwin) then begin
  1025. mayNeedLRStore := false;
  1026. if target_info.system=system_powerpc64_aix then
  1027. opc:=A_BLA
  1028. else
  1029. opc:=A_BL;
  1030. if ((fprcount > 0) and (gprcount > 0)) then begin
  1031. a_op_const_reg_reg(list, OP_SUB, OS_INT, 8 * fprcount, NR_R1, NR_R12);
  1032. a_call_name_direct(list, opc, '_savegpr1_' + intToStr(32-gprcount), false, false, false, false);
  1033. a_call_name_direct(list, opc, '_savefpr_' + intToStr(32-fprcount), false, false, false, false);
  1034. end else if (gprcount > 0) then
  1035. a_call_name_direct(list, opc, '_savegpr0_' + intToStr(32-gprcount), false, false, false, false)
  1036. else if (fprcount > 0) then
  1037. a_call_name_direct(list, opc, '_savefpr_' + intToStr(32-fprcount), false, false, false, false)
  1038. else
  1039. mayNeedLRStore := true;
  1040. end else begin
  1041. { save registers, FPU first, then GPR }
  1042. reference_reset_base(href, NR_STACK_POINTER_REG, -8, ctempposinvalid, 8, []);
  1043. if (fprcount > 0) then
  1044. for regcount := RS_F31 downto firstregfpu do begin
  1045. a_loadfpu_reg_ref(list, OS_FLOAT, OS_FLOAT, newreg(R_FPUREGISTER,
  1046. regcount, R_SUBNONE), href);
  1047. dec(href.offset, tcgsize2size[OS_FLOAT]);
  1048. end;
  1049. if (gprcount > 0) then
  1050. for regcount := RS_R31 downto firstreggpr do begin
  1051. a_load_reg_ref(list, OS_INT, OS_INT, newreg(R_INTREGISTER, regcount,
  1052. R_SUBNONE), href);
  1053. dec(href.offset, sizeof(pint));
  1054. end;
  1055. { VMX registers not supported by FPC atm }
  1056. { in this branch we always need to store LR ourselves}
  1057. mayNeedLRStore := true;
  1058. end;
  1059. { we may need to store R0 (=LR) ourselves }
  1060. if ((cs_profile in init_settings.moduleswitches) or (mayNeedLRStore)) and (needslinkreg) then begin
  1061. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_SYSV, ctempposinvalid, 8, []);
  1062. list.concat(taicpu.op_reg_ref(A_STD, NR_R0, href));
  1063. end;
  1064. end;
  1065. var
  1066. href: treference;
  1067. lab: tasmlabel;
  1068. procmangledname: TSymStr;
  1069. begin
  1070. { In ELFv2 the function is required to initialise the TOC register itself
  1071. if necessary. Additionally, it has to mark the end of this TOC
  1072. initialisation code with a .localfunc directive, which will be used as
  1073. local entry code by the linker (when it knows the TOC value is the same
  1074. for the caller and callee). It must load the TOC in a PIC-way, which it
  1075. can do easily because R12 is guaranteed to hold the address of this function
  1076. on entry. }
  1077. if (target_info.abi=abi_powerpc_elfv2) and
  1078. (pi_needs_got in current_procinfo.flags) and
  1079. not nostackframe then
  1080. begin
  1081. current_asmdata.getlabel(lab,alt_addr);
  1082. getcpuregister(list,NR_R12);
  1083. getcpuregister(list,NR_R2);
  1084. cg.a_label(list,lab);
  1085. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('.TOC.',AT_DATA),0,sizeof(PInt),[]);
  1086. href.relsymbol:=lab;
  1087. href.refaddr:=addr_higha;
  1088. list.concat(taicpu.op_reg_reg_ref(a_addis,NR_R2,NR_R12,href));
  1089. href.refaddr:=addr_low;
  1090. list.concat(taicpu.op_reg_reg_ref(a_addi,NR_R2,NR_R2,href));
  1091. procmangledname:=current_procinfo.procdef.mangledname;
  1092. list.concat(tai_symbolpair.create(spk_localentry,procmangledname,procmangledname));
  1093. end;
  1094. calcFirstUsedFPR(firstregfpu, fprcount);
  1095. calcFirstUsedGPR(firstreggpr, gprcount);
  1096. { calculate real stack frame size }
  1097. localsize := tcpuprocinfo(current_procinfo).calc_stackframe_size(
  1098. gprcount, fprcount);
  1099. { determine whether we need to save the link register }
  1100. needslinkreg :=
  1101. not(nostackframe) and
  1102. (save_lr_in_prologue or
  1103. ((cs_opt_size in current_settings.optimizerswitches) and
  1104. ((fprcount > 0) or
  1105. (gprcount > 0))));
  1106. a_reg_alloc(list, NR_STACK_POINTER_REG);
  1107. a_reg_alloc(list, NR_R0);
  1108. { move link register to r0 }
  1109. if (needslinkreg) then
  1110. list.concat(taicpu.op_reg(A_MFLR, NR_R0));
  1111. save_standard_registers;
  1112. { save old stack frame pointer }
  1113. if (tcpuprocinfo(current_procinfo).needs_frame_pointer) then
  1114. list.concat(taicpu.op_reg_reg(A_MR, NR_OLD_STACK_POINTER_REG, NR_STACK_POINTER_REG));
  1115. { create stack frame }
  1116. if (not nostackframe) and (localsize > 0) and
  1117. tcpuprocinfo(current_procinfo).needstackframe then begin
  1118. if (localsize <= high(smallint)) then begin
  1119. reference_reset_base(href, NR_STACK_POINTER_REG, -localsize, ctempposinvalid, 8, []);
  1120. a_load_store(list, A_STDU, NR_STACK_POINTER_REG, href);
  1121. end else begin
  1122. reference_reset_base(href, NR_NO, -localsize, ctempposinvalid, 8, []);
  1123. { Use R0 for loading the constant (which is definitely > 32k when entering
  1124. this branch).
  1125. Inlined at this position because it must not use temp registers because
  1126. register allocations have already been done }
  1127. { Code template:
  1128. lis r0,ofs@highest
  1129. ori r0,r0,ofs@higher
  1130. sldi r0,r0,32
  1131. oris r0,r0,ofs@h
  1132. ori r0,r0,ofs@l
  1133. }
  1134. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  1135. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  1136. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  1137. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  1138. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  1139. list.concat(taicpu.op_reg_reg_reg(A_STDUX, NR_R1, NR_R1, NR_R0));
  1140. end;
  1141. end;
  1142. { CR register not used by FPC atm }
  1143. { keep R1 allocated??? }
  1144. a_reg_dealloc(list, NR_R0);
  1145. end;
  1146. { Generates the exit code for a method.
  1147. This procedure may be called before, as well as after g_stackframe_entry
  1148. is called.
  1149. IMPORTANT: registers are not to be allocated through the register
  1150. allocator here, because the register colouring has already occurred !!
  1151. }
  1152. procedure tcgppc.g_proc_exit(list: TAsmList; parasize: longint; nostackframe:
  1153. boolean);
  1154. var
  1155. firstregfpu, firstreggpr: TSuperRegister;
  1156. needslinkreg : boolean;
  1157. fprcount, gprcount: aint;
  1158. { Restore standard registers, both FPR and GPR; does not support VMX/Altivec }
  1159. procedure restore_standard_registers;
  1160. var
  1161. { flag indicating whether we need to manually add the exit code (e.g. blr instruction)
  1162. or not }
  1163. needsExitCode : Boolean;
  1164. href : treference;
  1165. regcount : TSuperRegister;
  1166. callopc,
  1167. jmpopc: tasmop;
  1168. begin
  1169. { there are two ways to do this: manually, by generating a few "ld" instructions,
  1170. or via the restore helper functions. The latter are selected by the -Og switch,
  1171. i.e. "optimize for size" }
  1172. if (cs_opt_size in current_settings.optimizerswitches) then begin
  1173. if target_info.system=system_powerpc64_aix then begin
  1174. callopc:=A_BLA;
  1175. jmpopc:=A_BA;
  1176. end
  1177. else begin
  1178. callopc:=A_BL;
  1179. jmpopc:=A_B;
  1180. end;
  1181. needsExitCode := false;
  1182. if ((fprcount > 0) and (gprcount > 0)) then begin
  1183. a_op_const_reg_reg(list, OP_SUB, OS_INT, 8 * fprcount, NR_R1, NR_R12);
  1184. a_call_name_direct(list, callopc, '_restgpr1_' + intToStr(32-gprcount), false, false, false, false);
  1185. a_jmp_name_direct(list, jmpopc, '_restfpr_' + intToStr(32-fprcount), false);
  1186. end else if (gprcount > 0) then
  1187. a_jmp_name_direct(list, jmpopc, '_restgpr0_' + intToStr(32-gprcount), false)
  1188. else if (fprcount > 0) then
  1189. a_jmp_name_direct(list, jmpopc, '_restfpr_' + intToStr(32-fprcount), false)
  1190. else
  1191. needsExitCode := true;
  1192. end else begin
  1193. needsExitCode := true;
  1194. { restore registers, FPU first, GPR next }
  1195. reference_reset_base(href, NR_STACK_POINTER_REG, -tcgsize2size[OS_FLOAT], ctempposinvalid, 8, []);
  1196. if (fprcount > 0) then
  1197. for regcount := RS_F31 downto firstregfpu do begin
  1198. a_loadfpu_ref_reg(list, OS_FLOAT, OS_FLOAT, href, newreg(R_FPUREGISTER, regcount,
  1199. R_SUBNONE));
  1200. dec(href.offset, tcgsize2size[OS_FLOAT]);
  1201. end;
  1202. if (gprcount > 0) then
  1203. for regcount := RS_R31 downto firstreggpr do begin
  1204. a_load_ref_reg(list, OS_INT, OS_INT, href, newreg(R_INTREGISTER, regcount,
  1205. R_SUBNONE));
  1206. dec(href.offset, sizeof(pint));
  1207. end;
  1208. { VMX not supported by FPC atm }
  1209. end;
  1210. if (needsExitCode) then begin
  1211. { restore LR (if needed) }
  1212. if (needslinkreg) then begin
  1213. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_SYSV, ctempposinvalid, 8, []);
  1214. list.concat(taicpu.op_reg_ref(A_LD, NR_R0, href));
  1215. list.concat(taicpu.op_reg(A_MTLR, NR_R0));
  1216. end;
  1217. { generate return instruction }
  1218. list.concat(taicpu.op_none(A_BLR));
  1219. end;
  1220. end;
  1221. var
  1222. href: treference;
  1223. localsize : aint;
  1224. begin
  1225. calcFirstUsedFPR(firstregfpu, fprcount);
  1226. calcFirstUsedGPR(firstreggpr, gprcount);
  1227. { determine whether we need to restore the link register }
  1228. needslinkreg :=
  1229. not(nostackframe) and
  1230. (((not (po_assembler in current_procinfo.procdef.procoptions)) and
  1231. ((pi_do_call in current_procinfo.flags) or (cs_profile in init_settings.moduleswitches))) or
  1232. ((cs_opt_size in current_settings.optimizerswitches) and ((fprcount > 0) or (gprcount > 0))) or
  1233. ([cs_lineinfo, cs_debuginfo] * current_settings.moduleswitches <> []));
  1234. { calculate stack frame }
  1235. localsize := tcpuprocinfo(current_procinfo).calc_stackframe_size(
  1236. gprcount, fprcount);
  1237. { CR register not supported }
  1238. { restore stack pointer }
  1239. if (not nostackframe) and (localsize > 0) and
  1240. tcpuprocinfo(current_procinfo).needstackframe then begin
  1241. if (localsize <= high(smallint)) then begin
  1242. list.concat(taicpu.op_reg_reg_const(A_ADDI, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, localsize));
  1243. end else begin
  1244. reference_reset_base(href, NR_NO, localsize, ctempposinvalid, 8, []);
  1245. { use R0 for loading the constant (which is definitely > 32k when entering
  1246. this branch)
  1247. Inlined because it must not use temp registers because register allocations
  1248. have already been done
  1249. }
  1250. { Code template:
  1251. lis r0,ofs@highest
  1252. ori r0,ofs@higher
  1253. sldi r0,r0,32
  1254. oris r0,r0,ofs@h
  1255. ori r0,r0,ofs@l
  1256. }
  1257. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  1258. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  1259. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  1260. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  1261. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  1262. list.concat(taicpu.op_reg_reg_reg(A_ADD, NR_R1, NR_R1, NR_R0));
  1263. end;
  1264. end;
  1265. restore_standard_registers;
  1266. end;
  1267. procedure tcgppc.a_loadaddr_ref_reg(list: TAsmList; const ref: treference; r:
  1268. tregister);
  1269. var
  1270. ref2, tmpref: treference;
  1271. { register used to construct address }
  1272. tempreg : TRegister;
  1273. begin
  1274. if (target_info.system in [system_powerpc64_darwin,system_powerpc64_aix]) then
  1275. begin
  1276. inherited a_loadaddr_ref_reg(list,ref,r);
  1277. exit;
  1278. end;
  1279. ref2 := ref;
  1280. fixref(list, ref2);
  1281. { load a symbol }
  1282. if (assigned(ref2.symbol) or (hasLargeOffset(ref2))) then begin
  1283. { add the symbol's value to the base of the reference, and if the }
  1284. { reference doesn't have a base, create one }
  1285. reference_reset(tmpref, ref2.alignment, ref2.volatility);
  1286. tmpref.offset := ref2.offset;
  1287. tmpref.symbol := ref2.symbol;
  1288. tmpref.relsymbol := ref2.relsymbol;
  1289. { load 64 bit reference into r. If the reference already has a base register,
  1290. first load the 64 bit value into a temp register, then add it to the result
  1291. register rD }
  1292. if (ref2.base <> NR_NO) then begin
  1293. { already have a base register, so allocate a new one }
  1294. tempreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1295. end else begin
  1296. tempreg := r;
  1297. end;
  1298. { code for loading a reference from a symbol into a register rD }
  1299. (*
  1300. lis rX,SYM@highest
  1301. ori rX,SYM@higher
  1302. sldi rX,rX,32
  1303. oris rX,rX,SYM@h
  1304. ori rX,rX,SYM@l
  1305. *)
  1306. {$IFDEF EXTDEBUG}
  1307. list.concat(tai_comment.create(strpnew('loadaddr_ref_reg ')));
  1308. {$ENDIF EXTDEBUG}
  1309. if (assigned(tmpref.symbol)) then begin
  1310. tmpref.refaddr := addr_highest;
  1311. list.concat(taicpu.op_reg_ref(A_LIS, tempreg, tmpref));
  1312. tmpref.refaddr := addr_higher;
  1313. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1314. list.concat(taicpu.op_reg_reg_const(A_SLDI, tempreg, tempreg, 32));
  1315. tmpref.refaddr := addr_high;
  1316. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tempreg, tempreg, tmpref));
  1317. tmpref.refaddr := addr_low;
  1318. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1319. end else
  1320. a_load_const_reg(list, OS_ADDR, tmpref.offset, tempreg);
  1321. { if there's already a base register, add the temp register contents to
  1322. the base register }
  1323. if (ref2.base <> NR_NO) then begin
  1324. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, tempreg, ref2.base));
  1325. end;
  1326. end else if (ref2.offset <> 0) then begin
  1327. { no symbol, but offset <> 0 }
  1328. if (ref2.base <> NR_NO) then begin
  1329. a_op_const_reg_reg(list, OP_ADD, OS_64, ref2.offset, ref2.base, r)
  1330. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never
  1331. occurs, so now only ref.offset has to be loaded }
  1332. end else begin
  1333. a_load_const_reg(list, OS_64, ref2.offset, r);
  1334. end;
  1335. end else if (ref2.index <> NR_NO) then begin
  1336. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, ref2.base, ref2.index))
  1337. end else if (ref2.base <> NR_NO) and
  1338. (r <> ref2.base) then begin
  1339. a_load_reg_reg(list, OS_ADDR, OS_ADDR, ref2.base, r)
  1340. end else begin
  1341. list.concat(taicpu.op_reg_const(A_LI, r, 0));
  1342. end;
  1343. end;
  1344. { ************* concatcopy ************ }
  1345. procedure tcgppc.g_concatcopy(list: TAsmList; const source, dest: treference;
  1346. len: aint);
  1347. var
  1348. countreg, tempreg:TRegister;
  1349. src, dst: TReference;
  1350. lab: tasmlabel;
  1351. count, count2, step: longint;
  1352. size: tcgsize;
  1353. begin
  1354. {$IFDEF extdebug}
  1355. list.concat(tai_comment.create(strpnew('g_concatcopy1 ' + inttostr(len) + ' bytes left ')));
  1356. {$ENDIF extdebug}
  1357. { if the references are equal, exit, there is no need to copy anything }
  1358. if references_equal(source, dest) or
  1359. (len=0) then
  1360. exit;
  1361. { make sure short loads are handled as optimally as possible;
  1362. note that the data here never overlaps, so we can do a forward
  1363. copy at all times.
  1364. NOTE: maybe use some scratch registers to pair load/store instructions
  1365. }
  1366. if (len <= 8) then begin
  1367. src := source; dst := dest;
  1368. {$IFDEF extdebug}
  1369. list.concat(tai_comment.create(strpnew('g_concatcopy3 ' + inttostr(src.offset) + ' ' + inttostr(dst.offset))));
  1370. {$ENDIF extdebug}
  1371. while (len <> 0) do begin
  1372. if (len = 8) then begin
  1373. a_load_ref_ref(list, OS_64, OS_64, src, dst);
  1374. dec(len, 8);
  1375. end else if (len >= 4) then begin
  1376. a_load_ref_ref(list, OS_32, OS_32, src, dst);
  1377. inc(src.offset, 4); inc(dst.offset, 4);
  1378. dec(len, 4);
  1379. end else if (len >= 2) then begin
  1380. a_load_ref_ref(list, OS_16, OS_16, src, dst);
  1381. inc(src.offset, 2); inc(dst.offset, 2);
  1382. dec(len, 2);
  1383. end else begin
  1384. a_load_ref_ref(list, OS_8, OS_8, src, dst);
  1385. inc(src.offset, 1); inc(dst.offset, 1);
  1386. dec(len, 1);
  1387. end;
  1388. end;
  1389. exit;
  1390. end;
  1391. {$IFDEF extdebug}
  1392. list.concat(tai_comment.create(strpnew('g_concatcopy2 ' + inttostr(len) + ' bytes left ')));
  1393. {$ENDIF extdebug}
  1394. if not(source.alignment in [1,2]) and
  1395. not(dest.alignment in [1,2]) then
  1396. begin
  1397. count:=len div 8;
  1398. step:=8;
  1399. size:=OS_64;
  1400. end
  1401. else
  1402. begin
  1403. count:=len div 4;
  1404. step:=4;
  1405. size:=OS_32;
  1406. end;
  1407. tempreg:=getintregister(list,size);
  1408. reference_reset(src,source.alignment,source.volatility);
  1409. reference_reset(dst,dest.alignment,dest.volatility);
  1410. { load the address of source into src.base }
  1411. if (count > 4) or
  1412. not issimpleref(source) or
  1413. ((source.index <> NR_NO) and
  1414. ((source.offset + len) > high(smallint))) then begin
  1415. src.base := getaddressregister(list);
  1416. a_loadaddr_ref_reg(list, source, src.base);
  1417. end else begin
  1418. src := source;
  1419. end;
  1420. { load the address of dest into dst.base }
  1421. if (count > 4) or
  1422. not issimpleref(dest) or
  1423. ((dest.index <> NR_NO) and
  1424. ((dest.offset + len) > high(smallint))) then begin
  1425. dst.base := getaddressregister(list);
  1426. a_loadaddr_ref_reg(list, dest, dst.base);
  1427. end else begin
  1428. dst := dest;
  1429. end;
  1430. { generate a loop }
  1431. if count > 4 then begin
  1432. { the offsets are zero after the a_loadaddress_ref_reg and just
  1433. have to be set to step. I put an Inc there so debugging may be
  1434. easier (should offset be different from zero here, it will be
  1435. easy to notice in the generated assembler }
  1436. inc(dst.offset, step);
  1437. inc(src.offset, step);
  1438. list.concat(taicpu.op_reg_reg_const(A_SUBI, src.base, src.base, step));
  1439. list.concat(taicpu.op_reg_reg_const(A_SUBI, dst.base, dst.base, step));
  1440. countreg := getintregister(list, OS_INT);
  1441. a_load_const_reg(list, OS_INT, count, countreg);
  1442. current_asmdata.getjumplabel(lab);
  1443. a_label(list, lab);
  1444. list.concat(taicpu.op_reg_reg_const(A_SUBIC_, countreg, countreg, 1));
  1445. if (size=OS_64) then
  1446. begin
  1447. list.concat(taicpu.op_reg_ref(A_LDU, tempreg, src));
  1448. list.concat(taicpu.op_reg_ref(A_STDU, tempreg, dst));
  1449. end
  1450. else
  1451. begin
  1452. list.concat(taicpu.op_reg_ref(A_LWZU, tempreg, src));
  1453. list.concat(taicpu.op_reg_ref(A_STWU, tempreg, dst));
  1454. end;
  1455. a_jmp(list, A_BC, C_NE, 0, lab);
  1456. a_reg_sync(list,src.base);
  1457. a_reg_sync(list,dst.base);
  1458. a_reg_sync(list,countreg);
  1459. len := len mod step;
  1460. count := 0;
  1461. end;
  1462. { unrolled loop }
  1463. if count > 0 then begin
  1464. for count2 := 1 to count do begin
  1465. a_load_ref_reg(list, size, size, src, tempreg);
  1466. a_load_reg_ref(list, size, size, tempreg, dst);
  1467. inc(src.offset, step);
  1468. inc(dst.offset, step);
  1469. end;
  1470. len := len mod step;
  1471. end;
  1472. if (len and 4) <> 0 then begin
  1473. a_load_ref_reg(list, OS_32, OS_32, src, tempreg);
  1474. a_load_reg_ref(list, OS_32, OS_32, tempreg, dst);
  1475. inc(src.offset, 4);
  1476. inc(dst.offset, 4);
  1477. end;
  1478. { copy the leftovers }
  1479. if (len and 2) <> 0 then begin
  1480. a_load_ref_reg(list, OS_16, OS_16, src, tempreg);
  1481. a_load_reg_ref(list, OS_16, OS_16, tempreg, dst);
  1482. inc(src.offset, 2);
  1483. inc(dst.offset, 2);
  1484. end;
  1485. if (len and 1) <> 0 then begin
  1486. a_load_ref_reg(list, OS_8, OS_8, src, tempreg);
  1487. a_load_reg_ref(list, OS_8, OS_8, tempreg, dst);
  1488. end;
  1489. end;
  1490. {***************** This is private property, keep out! :) *****************}
  1491. procedure tcgppc.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  1492. const
  1493. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  1494. begin
  1495. {$IFDEF EXTDEBUG}
  1496. list.concat(tai_comment.create(strpnew('maybeadjustresult op = ' + cgop2string(op) + ' size = ' + cgsize2string(size))));
  1497. {$ENDIF EXTDEBUG}
  1498. if (op in overflowops) and (size in [OS_8, OS_S8, OS_16, OS_S16, OS_32, OS_S32]) then
  1499. a_load_reg_reg(list, OS_64, size, dst, dst);
  1500. end;
  1501. function tcgppc.issimpleref(const ref: treference): boolean;
  1502. begin
  1503. if (ref.base = NR_NO) and
  1504. (ref.index <> NR_NO) then
  1505. internalerror(200208101);
  1506. result :=
  1507. not (assigned(ref.symbol)) and
  1508. (((ref.index = NR_NO) and
  1509. (ref.offset >= low(smallint)) and
  1510. (ref.offset <= high(smallint))) or
  1511. ((ref.index <> NR_NO) and
  1512. (ref.offset = 0)));
  1513. end;
  1514. procedure tcgppc.a_load_store(list: TAsmList; op: tasmop; reg: tregister;
  1515. ref: treference);
  1516. procedure maybefixup64bitoffset;
  1517. var
  1518. tmpreg: tregister;
  1519. begin
  1520. { for some instructions we need to check that the offset is divisible by at
  1521. least four. If not, add the bytes which are "off" to the base register and
  1522. adjust the offset accordingly }
  1523. case op of
  1524. A_LD, A_LDU, A_STD, A_STDU, A_LWA :
  1525. if ((ref.offset mod 4) <> 0) then begin
  1526. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1527. if (ref.base <> NR_NO) then begin
  1528. a_op_const_reg_reg(list, OP_ADD, OS_ADDR, ref.offset mod 4, ref.base, tmpreg);
  1529. ref.base := tmpreg;
  1530. end else begin
  1531. list.concat(taicpu.op_reg_const(A_LI, tmpreg, ref.offset mod 4));
  1532. ref.base := tmpreg;
  1533. end;
  1534. ref.offset := (ref.offset div 4) * 4;
  1535. end;
  1536. else
  1537. ;
  1538. end;
  1539. end;
  1540. var
  1541. tmpreg, tmpreg2: tregister;
  1542. tmpref: treference;
  1543. largeOffset: Boolean;
  1544. begin
  1545. if (target_info.system = system_powerpc64_darwin) then
  1546. begin
  1547. { darwin/ppc64 works with 32 bit relocatable symbol addresses }
  1548. maybefixup64bitoffset;
  1549. inherited a_load_store(list,op,reg,ref);
  1550. exit
  1551. end;
  1552. { at this point there must not be a combination of values in the ref treference
  1553. which is not possible to directly map to instructions of the PowerPC architecture }
  1554. if (ref.index <> NR_NO) and ((ref.offset <> 0) or (assigned(ref.symbol))) then
  1555. internalerror(200310131);
  1556. { if this is a PIC'ed address, handle it and exit }
  1557. if (ref.refaddr in [addr_pic,addr_pic_no_got]) then begin
  1558. if (ref.offset <> 0) then
  1559. internalerror(2006010501);
  1560. if (ref.index <> NR_NO) then
  1561. internalerror(2006010502);
  1562. if (not assigned(ref.symbol)) then
  1563. internalerror(200601050);
  1564. list.concat(taicpu.op_reg_ref(op, reg, ref));
  1565. exit;
  1566. end;
  1567. maybefixup64bitoffset;
  1568. {$IFDEF EXTDEBUG}
  1569. list.concat(tai_comment.create(strpnew('a_load_store1 ' + BoolToStr(ref.refaddr = addr_pic))));
  1570. {$ENDIF EXTDEBUG}
  1571. { if we have to load/store from a symbol or large addresses, use a temporary register
  1572. containing the address }
  1573. if (assigned(ref.symbol) or (hasLargeOffset(ref))) then begin
  1574. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1575. if (hasLargeOffset(ref) and (ref.base = NR_NO)) then begin
  1576. ref.base := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1577. a_load_const_reg(list, OS_ADDR, ref.offset, ref.base);
  1578. ref.offset := 0;
  1579. end;
  1580. reference_reset(tmpref, ref.alignment, ref.volatility);
  1581. tmpref.symbol := ref.symbol;
  1582. tmpref.relsymbol := ref.relsymbol;
  1583. tmpref.offset := ref.offset;
  1584. if (ref.base <> NR_NO) then begin
  1585. { As long as the TOC isn't working we try to achieve highest speed (in this
  1586. case by allowing instructions execute in parallel) as possible at the cost
  1587. of using another temporary register. So the code template when there is
  1588. a base register and an offset is the following:
  1589. lis rT1, SYM+offs@highest
  1590. ori rT1, rT1, SYM+offs@higher
  1591. lis rT2, SYM+offs@hi
  1592. ori rT2, SYM+offs@lo
  1593. rldimi rT2, rT1, 32
  1594. <op>X reg, base, rT2
  1595. }
  1596. tmpreg2 := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1597. if (assigned(tmpref.symbol)) then begin
  1598. tmpref.refaddr := addr_highest;
  1599. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  1600. tmpref.refaddr := addr_higher;
  1601. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  1602. tmpref.refaddr := addr_high;
  1603. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg2, tmpref));
  1604. tmpref.refaddr := addr_low;
  1605. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg2, tmpreg2, tmpref));
  1606. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, tmpreg2, tmpreg, 32, 0));
  1607. end else
  1608. a_load_const_reg(list, OS_ADDR, tmpref.offset, tmpreg2);
  1609. reference_reset(tmpref, ref.alignment, ref.volatility);
  1610. tmpref.base := ref.base;
  1611. tmpref.index := tmpreg2;
  1612. case op of
  1613. { the code generator doesn't generate update instructions anyway, so
  1614. error out on those instructions }
  1615. A_LBZ : op := A_LBZX;
  1616. A_LHZ : op := A_LHZX;
  1617. A_LWZ : op := A_LWZX;
  1618. A_LD : op := A_LDX;
  1619. A_LHA : op := A_LHAX;
  1620. A_LWA : op := A_LWAX;
  1621. A_LFS : op := A_LFSX;
  1622. A_LFD : op := A_LFDX;
  1623. A_STB : op := A_STBX;
  1624. A_STH : op := A_STHX;
  1625. A_STW : op := A_STWX;
  1626. A_STD : op := A_STDX;
  1627. A_STFS : op := A_STFSX;
  1628. A_STFD : op := A_STFDX;
  1629. else
  1630. { unknown load/store opcode }
  1631. internalerror(2005101302);
  1632. end;
  1633. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  1634. end else begin
  1635. { when accessing value from a reference without a base register, use the
  1636. following code template:
  1637. lis rT,SYM+offs@highesta
  1638. ori rT,SYM+offs@highera
  1639. sldi rT,rT,32
  1640. oris rT,rT,SYM+offs@ha
  1641. ld rD,SYM+offs@l(rT)
  1642. }
  1643. tmpref.refaddr := addr_highesta;
  1644. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  1645. tmpref.refaddr := addr_highera;
  1646. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  1647. list.concat(taicpu.op_reg_reg_const(A_SLDI, tmpreg, tmpreg, 32));
  1648. tmpref.refaddr := addr_higha;
  1649. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tmpreg, tmpreg, tmpref));
  1650. tmpref.base := tmpreg;
  1651. tmpref.refaddr := addr_low;
  1652. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  1653. end;
  1654. end else begin
  1655. list.concat(taicpu.op_reg_ref(op, reg, ref));
  1656. end;
  1657. end;
  1658. procedure tcgppc.loadConstantPIC(list : TAsmList; size : TCGSize; a : aint; reg : TRegister);
  1659. var
  1660. l: tasmsymbol;
  1661. ref: treference;
  1662. symname : string;
  1663. begin
  1664. maybe_new_object_file(current_asmdata.asmlists[al_picdata]);
  1665. symname := '_$' + current_asmdata.name^ + '$toc$' + hexstr(a, sizeof(a)*2);
  1666. l:=current_asmdata.getasmsymbol(symname);
  1667. if not(assigned(l)) then begin
  1668. l:=current_asmdata.DefineAsmSymbol(symname,AB_GLOBAL, AT_METADATA, voidpointertype);
  1669. new_section(current_asmdata.asmlists[al_picdata],sec_toc, '.toc', 8);
  1670. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create_global(l,0));
  1671. current_asmdata.asmlists[al_picdata].concat(tai_directive.create(asd_toc_entry, symname + '[TC], ' + inttostr(a)));
  1672. end;
  1673. reference_reset_symbol(ref,l,0,8,[]);
  1674. ref.base := NR_R2;
  1675. ref.refaddr := addr_no;
  1676. {$IFDEF EXTDEBUG}
  1677. list.concat(tai_comment.create(strpnew('loading value from TOC reference for ' + symname)));
  1678. {$ENDIF EXTDEBUG}
  1679. a_load_ref_reg(list, OS_INT, OS_INT, ref, reg);
  1680. end;
  1681. procedure create_codegen;
  1682. begin
  1683. cg := tcgppc.create;
  1684. cg128:=tcg128.create;
  1685. end;
  1686. end.