cgcpu.pas 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  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. end;
  941. end;
  942. procedure tcgppc.profilecode_restorepara(para : tparavarsym; list : TAsmList);
  943. begin
  944. case (para.paraloc[calleeside].Location^.loc) of
  945. LOC_REGISTER, LOC_CREGISTER:
  946. a_load_ref_reg(list, para.paraloc[calleeside].Location^.size, OS_INT,
  947. para.localloc.reference, para.paraloc[calleeside].Location^.register);
  948. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  949. a_loadfpu_ref_reg(list, para.paraloc[calleeside].Location^.size,
  950. para.paraloc[calleeside].Location^.size,
  951. para.localloc.reference, para.paraloc[calleeside].Location^.register);
  952. LOC_MMREGISTER, LOC_CMMREGISTER:
  953. { not supported }
  954. internalerror(2006041802);
  955. end;
  956. end;
  957. procedure tcgppc.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);
  958. var
  959. hsym : tsym;
  960. href : treference;
  961. paraloc : Pcgparalocation;
  962. begin
  963. if ((ioffset >= low(smallint)) and (ioffset < high(smallint))) then begin
  964. { the original method can handle this }
  965. inherited g_adjust_self_value(list, procdef, ioffset);
  966. exit;
  967. end;
  968. { calculate the parameter info for the procdef }
  969. procdef.init_paraloc_info(callerside);
  970. hsym:=tsym(procdef.parast.Find('self'));
  971. if not(assigned(hsym) and
  972. (hsym.typ=paravarsym)) then
  973. internalerror(2010103101);
  974. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  975. while paraloc<>nil do
  976. with paraloc^ do begin
  977. case loc of
  978. LOC_REGISTER:
  979. begin
  980. a_load_const_reg(list, size, ioffset, NR_R11);
  981. a_op_reg_reg(list, OP_SUB, size, NR_R11, register);
  982. end else
  983. internalerror(2010103102);
  984. end;
  985. paraloc:=next;
  986. end;
  987. end;
  988. procedure tcgppc.g_profilecode(list: TAsmList);
  989. begin
  990. current_procinfo.procdef.paras.ForEachCall(TObjectListCallback(@profilecode_savepara), list);
  991. a_call_name_direct(list, A_BL, '_mcount', false, false, true);
  992. current_procinfo.procdef.paras.ForEachCall(TObjectListCallback(@profilecode_restorepara), list);
  993. end;
  994. { Generates the entry code of a procedure/function.
  995. This procedure may be called before, as well as after g_return_from_proc
  996. is called. localsize is the sum of the size necessary for local variables
  997. and the maximum possible combined size of ALL the parameters of a procedure
  998. called by the current one
  999. IMPORTANT: registers are not to be allocated through the register
  1000. allocator here, because the register colouring has already occurred !!
  1001. }
  1002. procedure tcgppc.g_proc_entry(list: TAsmList; localsize: longint;
  1003. nostackframe: boolean);
  1004. var
  1005. firstregfpu, firstreggpr: TSuperRegister;
  1006. needslinkreg: boolean;
  1007. fprcount, gprcount : aint;
  1008. { Save standard registers, both FPR and GPR; does not support VMX/Altivec }
  1009. procedure save_standard_registers;
  1010. var
  1011. regcount : TSuperRegister;
  1012. href : TReference;
  1013. mayNeedLRStore : boolean;
  1014. opc : tasmop;
  1015. begin
  1016. { there are two ways to do this: manually, by generating a few "std" instructions,
  1017. or via the restore helper functions. The latter are selected by the -Og switch,
  1018. i.e. "optimize for size" }
  1019. if (cs_opt_size in current_settings.optimizerswitches) and
  1020. (target_info.system <> system_powerpc64_darwin) then begin
  1021. mayNeedLRStore := false;
  1022. if target_info.system=system_powerpc64_aix then
  1023. opc:=A_BLA
  1024. else
  1025. opc:=A_BL;
  1026. if ((fprcount > 0) and (gprcount > 0)) then begin
  1027. a_op_const_reg_reg(list, OP_SUB, OS_INT, 8 * fprcount, NR_R1, NR_R12);
  1028. a_call_name_direct(list, opc, '_savegpr1_' + intToStr(32-gprcount), false, false, false, false);
  1029. a_call_name_direct(list, opc, '_savefpr_' + intToStr(32-fprcount), false, false, false, false);
  1030. end else if (gprcount > 0) then
  1031. a_call_name_direct(list, opc, '_savegpr0_' + intToStr(32-gprcount), false, false, false, false)
  1032. else if (fprcount > 0) then
  1033. a_call_name_direct(list, opc, '_savefpr_' + intToStr(32-fprcount), false, false, false, false)
  1034. else
  1035. mayNeedLRStore := true;
  1036. end else begin
  1037. { save registers, FPU first, then GPR }
  1038. reference_reset_base(href, NR_STACK_POINTER_REG, -8, ctempposinvalid, 8, []);
  1039. if (fprcount > 0) then
  1040. for regcount := RS_F31 downto firstregfpu do begin
  1041. a_loadfpu_reg_ref(list, OS_FLOAT, OS_FLOAT, newreg(R_FPUREGISTER,
  1042. regcount, R_SUBNONE), href);
  1043. dec(href.offset, tcgsize2size[OS_FLOAT]);
  1044. end;
  1045. if (gprcount > 0) then
  1046. for regcount := RS_R31 downto firstreggpr do begin
  1047. a_load_reg_ref(list, OS_INT, OS_INT, newreg(R_INTREGISTER, regcount,
  1048. R_SUBNONE), href);
  1049. dec(href.offset, sizeof(pint));
  1050. end;
  1051. { VMX registers not supported by FPC atm }
  1052. { in this branch we always need to store LR ourselves}
  1053. mayNeedLRStore := true;
  1054. end;
  1055. { we may need to store R0 (=LR) ourselves }
  1056. if ((cs_profile in init_settings.moduleswitches) or (mayNeedLRStore)) and (needslinkreg) then begin
  1057. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_SYSV, ctempposinvalid, 8, []);
  1058. list.concat(taicpu.op_reg_ref(A_STD, NR_R0, href));
  1059. end;
  1060. end;
  1061. var
  1062. href: treference;
  1063. lab: tasmlabel;
  1064. procmangledname: TSymStr;
  1065. begin
  1066. { In ELFv2 the function is required to initialise the TOC register itself
  1067. if necessary. Additionally, it has to mark the end of this TOC
  1068. initialisation code with a .localfunc directive, which will be used as
  1069. local entry code by the linker (when it knows the TOC value is the same
  1070. for the caller and callee). It must load the TOC in a PIC-way, which it
  1071. can do easily because R12 is guaranteed to hold the address of this function
  1072. on entry. }
  1073. if (target_info.abi=abi_powerpc_elfv2) and
  1074. (pi_needs_got in current_procinfo.flags) and
  1075. not nostackframe then
  1076. begin
  1077. current_asmdata.getlabel(lab,alt_addr);
  1078. getcpuregister(list,NR_R12);
  1079. getcpuregister(list,NR_R2);
  1080. cg.a_label(list,lab);
  1081. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('.TOC.',AT_DATA),0,sizeof(PInt),[]);
  1082. href.relsymbol:=lab;
  1083. href.refaddr:=addr_higha;
  1084. list.concat(taicpu.op_reg_reg_ref(a_addis,NR_R2,NR_R12,href));
  1085. href.refaddr:=addr_low;
  1086. list.concat(taicpu.op_reg_reg_ref(a_addi,NR_R2,NR_R2,href));
  1087. procmangledname:=current_procinfo.procdef.mangledname;
  1088. list.concat(tai_symbolpair.create(spk_localentry,procmangledname,procmangledname));
  1089. end;
  1090. calcFirstUsedFPR(firstregfpu, fprcount);
  1091. calcFirstUsedGPR(firstreggpr, gprcount);
  1092. { calculate real stack frame size }
  1093. localsize := tcpuprocinfo(current_procinfo).calc_stackframe_size(
  1094. gprcount, fprcount);
  1095. { determine whether we need to save the link register }
  1096. needslinkreg :=
  1097. not(nostackframe) and
  1098. (save_lr_in_prologue or
  1099. ((cs_opt_size in current_settings.optimizerswitches) and
  1100. ((fprcount > 0) or
  1101. (gprcount > 0))));
  1102. a_reg_alloc(list, NR_STACK_POINTER_REG);
  1103. a_reg_alloc(list, NR_R0);
  1104. { move link register to r0 }
  1105. if (needslinkreg) then
  1106. list.concat(taicpu.op_reg(A_MFLR, NR_R0));
  1107. save_standard_registers;
  1108. { save old stack frame pointer }
  1109. if (tcpuprocinfo(current_procinfo).needs_frame_pointer) then
  1110. list.concat(taicpu.op_reg_reg(A_MR, NR_OLD_STACK_POINTER_REG, NR_STACK_POINTER_REG));
  1111. { create stack frame }
  1112. if (not nostackframe) and (localsize > 0) and
  1113. tcpuprocinfo(current_procinfo).needstackframe then begin
  1114. if (localsize <= high(smallint)) then begin
  1115. reference_reset_base(href, NR_STACK_POINTER_REG, -localsize, ctempposinvalid, 8, []);
  1116. a_load_store(list, A_STDU, NR_STACK_POINTER_REG, href);
  1117. end else begin
  1118. reference_reset_base(href, NR_NO, -localsize, ctempposinvalid, 8, []);
  1119. { Use R0 for loading the constant (which is definitely > 32k when entering
  1120. this branch).
  1121. Inlined at this position because it must not use temp registers because
  1122. register allocations have already been done }
  1123. { Code template:
  1124. lis r0,ofs@highest
  1125. ori r0,r0,ofs@higher
  1126. sldi r0,r0,32
  1127. oris r0,r0,ofs@h
  1128. ori r0,r0,ofs@l
  1129. }
  1130. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  1131. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  1132. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  1133. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  1134. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  1135. list.concat(taicpu.op_reg_reg_reg(A_STDUX, NR_R1, NR_R1, NR_R0));
  1136. end;
  1137. end;
  1138. { CR register not used by FPC atm }
  1139. { keep R1 allocated??? }
  1140. a_reg_dealloc(list, NR_R0);
  1141. end;
  1142. { Generates the exit code for a method.
  1143. This procedure may be called before, as well as after g_stackframe_entry
  1144. is called.
  1145. IMPORTANT: registers are not to be allocated through the register
  1146. allocator here, because the register colouring has already occurred !!
  1147. }
  1148. procedure tcgppc.g_proc_exit(list: TAsmList; parasize: longint; nostackframe:
  1149. boolean);
  1150. var
  1151. firstregfpu, firstreggpr: TSuperRegister;
  1152. needslinkreg : boolean;
  1153. fprcount, gprcount: aint;
  1154. { Restore standard registers, both FPR and GPR; does not support VMX/Altivec }
  1155. procedure restore_standard_registers;
  1156. var
  1157. { flag indicating whether we need to manually add the exit code (e.g. blr instruction)
  1158. or not }
  1159. needsExitCode : Boolean;
  1160. href : treference;
  1161. regcount : TSuperRegister;
  1162. callopc,
  1163. jmpopc: tasmop;
  1164. begin
  1165. { there are two ways to do this: manually, by generating a few "ld" instructions,
  1166. or via the restore helper functions. The latter are selected by the -Og switch,
  1167. i.e. "optimize for size" }
  1168. if (cs_opt_size in current_settings.optimizerswitches) then begin
  1169. if target_info.system=system_powerpc64_aix then begin
  1170. callopc:=A_BLA;
  1171. jmpopc:=A_BA;
  1172. end
  1173. else begin
  1174. callopc:=A_BL;
  1175. jmpopc:=A_B;
  1176. end;
  1177. needsExitCode := false;
  1178. if ((fprcount > 0) and (gprcount > 0)) then begin
  1179. a_op_const_reg_reg(list, OP_SUB, OS_INT, 8 * fprcount, NR_R1, NR_R12);
  1180. a_call_name_direct(list, callopc, '_restgpr1_' + intToStr(32-gprcount), false, false, false, false);
  1181. a_jmp_name_direct(list, jmpopc, '_restfpr_' + intToStr(32-fprcount), false);
  1182. end else if (gprcount > 0) then
  1183. a_jmp_name_direct(list, jmpopc, '_restgpr0_' + intToStr(32-gprcount), false)
  1184. else if (fprcount > 0) then
  1185. a_jmp_name_direct(list, jmpopc, '_restfpr_' + intToStr(32-fprcount), false)
  1186. else
  1187. needsExitCode := true;
  1188. end else begin
  1189. needsExitCode := true;
  1190. { restore registers, FPU first, GPR next }
  1191. reference_reset_base(href, NR_STACK_POINTER_REG, -tcgsize2size[OS_FLOAT], ctempposinvalid, 8, []);
  1192. if (fprcount > 0) then
  1193. for regcount := RS_F31 downto firstregfpu do begin
  1194. a_loadfpu_ref_reg(list, OS_FLOAT, OS_FLOAT, href, newreg(R_FPUREGISTER, regcount,
  1195. R_SUBNONE));
  1196. dec(href.offset, tcgsize2size[OS_FLOAT]);
  1197. end;
  1198. if (gprcount > 0) then
  1199. for regcount := RS_R31 downto firstreggpr do begin
  1200. a_load_ref_reg(list, OS_INT, OS_INT, href, newreg(R_INTREGISTER, regcount,
  1201. R_SUBNONE));
  1202. dec(href.offset, sizeof(pint));
  1203. end;
  1204. { VMX not supported by FPC atm }
  1205. end;
  1206. if (needsExitCode) then begin
  1207. { restore LR (if needed) }
  1208. if (needslinkreg) then begin
  1209. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_SYSV, ctempposinvalid, 8, []);
  1210. list.concat(taicpu.op_reg_ref(A_LD, NR_R0, href));
  1211. list.concat(taicpu.op_reg(A_MTLR, NR_R0));
  1212. end;
  1213. { generate return instruction }
  1214. list.concat(taicpu.op_none(A_BLR));
  1215. end;
  1216. end;
  1217. var
  1218. href: treference;
  1219. localsize : aint;
  1220. begin
  1221. calcFirstUsedFPR(firstregfpu, fprcount);
  1222. calcFirstUsedGPR(firstreggpr, gprcount);
  1223. { determine whether we need to restore the link register }
  1224. needslinkreg :=
  1225. not(nostackframe) and
  1226. (((not (po_assembler in current_procinfo.procdef.procoptions)) and
  1227. ((pi_do_call in current_procinfo.flags) or (cs_profile in init_settings.moduleswitches))) or
  1228. ((cs_opt_size in current_settings.optimizerswitches) and ((fprcount > 0) or (gprcount > 0))) or
  1229. ([cs_lineinfo, cs_debuginfo] * current_settings.moduleswitches <> []));
  1230. { calculate stack frame }
  1231. localsize := tcpuprocinfo(current_procinfo).calc_stackframe_size(
  1232. gprcount, fprcount);
  1233. { CR register not supported }
  1234. { restore stack pointer }
  1235. if (not nostackframe) and (localsize > 0) and
  1236. tcpuprocinfo(current_procinfo).needstackframe then begin
  1237. if (localsize <= high(smallint)) then begin
  1238. list.concat(taicpu.op_reg_reg_const(A_ADDI, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, localsize));
  1239. end else begin
  1240. reference_reset_base(href, NR_NO, localsize, ctempposinvalid, 8, []);
  1241. { use R0 for loading the constant (which is definitely > 32k when entering
  1242. this branch)
  1243. Inlined because it must not use temp registers because register allocations
  1244. have already been done
  1245. }
  1246. { Code template:
  1247. lis r0,ofs@highest
  1248. ori r0,ofs@higher
  1249. sldi r0,r0,32
  1250. oris r0,r0,ofs@h
  1251. ori r0,r0,ofs@l
  1252. }
  1253. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  1254. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  1255. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  1256. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  1257. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  1258. list.concat(taicpu.op_reg_reg_reg(A_ADD, NR_R1, NR_R1, NR_R0));
  1259. end;
  1260. end;
  1261. restore_standard_registers;
  1262. end;
  1263. procedure tcgppc.a_loadaddr_ref_reg(list: TAsmList; const ref: treference; r:
  1264. tregister);
  1265. var
  1266. ref2, tmpref: treference;
  1267. { register used to construct address }
  1268. tempreg : TRegister;
  1269. begin
  1270. if (target_info.system in [system_powerpc64_darwin,system_powerpc64_aix]) then
  1271. begin
  1272. inherited a_loadaddr_ref_reg(list,ref,r);
  1273. exit;
  1274. end;
  1275. ref2 := ref;
  1276. fixref(list, ref2);
  1277. { load a symbol }
  1278. if (assigned(ref2.symbol) or (hasLargeOffset(ref2))) then begin
  1279. { add the symbol's value to the base of the reference, and if the }
  1280. { reference doesn't have a base, create one }
  1281. reference_reset(tmpref, ref2.alignment, ref2.volatility);
  1282. tmpref.offset := ref2.offset;
  1283. tmpref.symbol := ref2.symbol;
  1284. tmpref.relsymbol := ref2.relsymbol;
  1285. { load 64 bit reference into r. If the reference already has a base register,
  1286. first load the 64 bit value into a temp register, then add it to the result
  1287. register rD }
  1288. if (ref2.base <> NR_NO) then begin
  1289. { already have a base register, so allocate a new one }
  1290. tempreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1291. end else begin
  1292. tempreg := r;
  1293. end;
  1294. { code for loading a reference from a symbol into a register rD }
  1295. (*
  1296. lis rX,SYM@highest
  1297. ori rX,SYM@higher
  1298. sldi rX,rX,32
  1299. oris rX,rX,SYM@h
  1300. ori rX,rX,SYM@l
  1301. *)
  1302. {$IFDEF EXTDEBUG}
  1303. list.concat(tai_comment.create(strpnew('loadaddr_ref_reg ')));
  1304. {$ENDIF EXTDEBUG}
  1305. if (assigned(tmpref.symbol)) then begin
  1306. tmpref.refaddr := addr_highest;
  1307. list.concat(taicpu.op_reg_ref(A_LIS, tempreg, tmpref));
  1308. tmpref.refaddr := addr_higher;
  1309. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1310. list.concat(taicpu.op_reg_reg_const(A_SLDI, tempreg, tempreg, 32));
  1311. tmpref.refaddr := addr_high;
  1312. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tempreg, tempreg, tmpref));
  1313. tmpref.refaddr := addr_low;
  1314. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1315. end else
  1316. a_load_const_reg(list, OS_ADDR, tmpref.offset, tempreg);
  1317. { if there's already a base register, add the temp register contents to
  1318. the base register }
  1319. if (ref2.base <> NR_NO) then begin
  1320. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, tempreg, ref2.base));
  1321. end;
  1322. end else if (ref2.offset <> 0) then begin
  1323. { no symbol, but offset <> 0 }
  1324. if (ref2.base <> NR_NO) then begin
  1325. a_op_const_reg_reg(list, OP_ADD, OS_64, ref2.offset, ref2.base, r)
  1326. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never
  1327. occurs, so now only ref.offset has to be loaded }
  1328. end else begin
  1329. a_load_const_reg(list, OS_64, ref2.offset, r);
  1330. end;
  1331. end else if (ref2.index <> NR_NO) then begin
  1332. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, ref2.base, ref2.index))
  1333. end else if (ref2.base <> NR_NO) and
  1334. (r <> ref2.base) then begin
  1335. a_load_reg_reg(list, OS_ADDR, OS_ADDR, ref2.base, r)
  1336. end else begin
  1337. list.concat(taicpu.op_reg_const(A_LI, r, 0));
  1338. end;
  1339. end;
  1340. { ************* concatcopy ************ }
  1341. procedure tcgppc.g_concatcopy(list: TAsmList; const source, dest: treference;
  1342. len: aint);
  1343. var
  1344. countreg, tempreg:TRegister;
  1345. src, dst: TReference;
  1346. lab: tasmlabel;
  1347. count, count2, step: longint;
  1348. size: tcgsize;
  1349. begin
  1350. {$IFDEF extdebug}
  1351. list.concat(tai_comment.create(strpnew('g_concatcopy1 ' + inttostr(len) + ' bytes left ')));
  1352. {$ENDIF extdebug}
  1353. { if the references are equal, exit, there is no need to copy anything }
  1354. if references_equal(source, dest) or
  1355. (len=0) then
  1356. exit;
  1357. { make sure short loads are handled as optimally as possible;
  1358. note that the data here never overlaps, so we can do a forward
  1359. copy at all times.
  1360. NOTE: maybe use some scratch registers to pair load/store instructions
  1361. }
  1362. if (len <= 8) then begin
  1363. src := source; dst := dest;
  1364. {$IFDEF extdebug}
  1365. list.concat(tai_comment.create(strpnew('g_concatcopy3 ' + inttostr(src.offset) + ' ' + inttostr(dst.offset))));
  1366. {$ENDIF extdebug}
  1367. while (len <> 0) do begin
  1368. if (len = 8) then begin
  1369. a_load_ref_ref(list, OS_64, OS_64, src, dst);
  1370. dec(len, 8);
  1371. end else if (len >= 4) then begin
  1372. a_load_ref_ref(list, OS_32, OS_32, src, dst);
  1373. inc(src.offset, 4); inc(dst.offset, 4);
  1374. dec(len, 4);
  1375. end else if (len >= 2) then begin
  1376. a_load_ref_ref(list, OS_16, OS_16, src, dst);
  1377. inc(src.offset, 2); inc(dst.offset, 2);
  1378. dec(len, 2);
  1379. end else begin
  1380. a_load_ref_ref(list, OS_8, OS_8, src, dst);
  1381. inc(src.offset, 1); inc(dst.offset, 1);
  1382. dec(len, 1);
  1383. end;
  1384. end;
  1385. exit;
  1386. end;
  1387. {$IFDEF extdebug}
  1388. list.concat(tai_comment.create(strpnew('g_concatcopy2 ' + inttostr(len) + ' bytes left ')));
  1389. {$ENDIF extdebug}
  1390. if not(source.alignment in [1,2]) and
  1391. not(dest.alignment in [1,2]) then
  1392. begin
  1393. count:=len div 8;
  1394. step:=8;
  1395. size:=OS_64;
  1396. end
  1397. else
  1398. begin
  1399. count:=len div 4;
  1400. step:=4;
  1401. size:=OS_32;
  1402. end;
  1403. tempreg:=getintregister(list,size);
  1404. reference_reset(src,source.alignment,source.volatility);
  1405. reference_reset(dst,dest.alignment,dest.volatility);
  1406. { load the address of source into src.base }
  1407. if (count > 4) or
  1408. not issimpleref(source) or
  1409. ((source.index <> NR_NO) and
  1410. ((source.offset + len) > high(smallint))) then begin
  1411. src.base := getaddressregister(list);
  1412. a_loadaddr_ref_reg(list, source, src.base);
  1413. end else begin
  1414. src := source;
  1415. end;
  1416. { load the address of dest into dst.base }
  1417. if (count > 4) or
  1418. not issimpleref(dest) or
  1419. ((dest.index <> NR_NO) and
  1420. ((dest.offset + len) > high(smallint))) then begin
  1421. dst.base := getaddressregister(list);
  1422. a_loadaddr_ref_reg(list, dest, dst.base);
  1423. end else begin
  1424. dst := dest;
  1425. end;
  1426. { generate a loop }
  1427. if count > 4 then begin
  1428. { the offsets are zero after the a_loadaddress_ref_reg and just
  1429. have to be set to step. I put an Inc there so debugging may be
  1430. easier (should offset be different from zero here, it will be
  1431. easy to notice in the generated assembler }
  1432. inc(dst.offset, step);
  1433. inc(src.offset, step);
  1434. list.concat(taicpu.op_reg_reg_const(A_SUBI, src.base, src.base, step));
  1435. list.concat(taicpu.op_reg_reg_const(A_SUBI, dst.base, dst.base, step));
  1436. countreg := getintregister(list, OS_INT);
  1437. a_load_const_reg(list, OS_INT, count, countreg);
  1438. current_asmdata.getjumplabel(lab);
  1439. a_label(list, lab);
  1440. list.concat(taicpu.op_reg_reg_const(A_SUBIC_, countreg, countreg, 1));
  1441. if (size=OS_64) then
  1442. begin
  1443. list.concat(taicpu.op_reg_ref(A_LDU, tempreg, src));
  1444. list.concat(taicpu.op_reg_ref(A_STDU, tempreg, dst));
  1445. end
  1446. else
  1447. begin
  1448. list.concat(taicpu.op_reg_ref(A_LWZU, tempreg, src));
  1449. list.concat(taicpu.op_reg_ref(A_STWU, tempreg, dst));
  1450. end;
  1451. a_jmp(list, A_BC, C_NE, 0, lab);
  1452. a_reg_sync(list,src.base);
  1453. a_reg_sync(list,dst.base);
  1454. a_reg_sync(list,countreg);
  1455. len := len mod step;
  1456. count := 0;
  1457. end;
  1458. { unrolled loop }
  1459. if count > 0 then begin
  1460. for count2 := 1 to count do begin
  1461. a_load_ref_reg(list, size, size, src, tempreg);
  1462. a_load_reg_ref(list, size, size, tempreg, dst);
  1463. inc(src.offset, step);
  1464. inc(dst.offset, step);
  1465. end;
  1466. len := len mod step;
  1467. end;
  1468. if (len and 4) <> 0 then begin
  1469. a_load_ref_reg(list, OS_32, OS_32, src, tempreg);
  1470. a_load_reg_ref(list, OS_32, OS_32, tempreg, dst);
  1471. inc(src.offset, 4);
  1472. inc(dst.offset, 4);
  1473. end;
  1474. { copy the leftovers }
  1475. if (len and 2) <> 0 then begin
  1476. a_load_ref_reg(list, OS_16, OS_16, src, tempreg);
  1477. a_load_reg_ref(list, OS_16, OS_16, tempreg, dst);
  1478. inc(src.offset, 2);
  1479. inc(dst.offset, 2);
  1480. end;
  1481. if (len and 1) <> 0 then begin
  1482. a_load_ref_reg(list, OS_8, OS_8, src, tempreg);
  1483. a_load_reg_ref(list, OS_8, OS_8, tempreg, dst);
  1484. end;
  1485. end;
  1486. {***************** This is private property, keep out! :) *****************}
  1487. procedure tcgppc.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  1488. const
  1489. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  1490. begin
  1491. {$IFDEF EXTDEBUG}
  1492. list.concat(tai_comment.create(strpnew('maybeadjustresult op = ' + cgop2string(op) + ' size = ' + cgsize2string(size))));
  1493. {$ENDIF EXTDEBUG}
  1494. if (op in overflowops) and (size in [OS_8, OS_S8, OS_16, OS_S16, OS_32, OS_S32]) then
  1495. a_load_reg_reg(list, OS_64, size, dst, dst);
  1496. end;
  1497. function tcgppc.issimpleref(const ref: treference): boolean;
  1498. begin
  1499. if (ref.base = NR_NO) and
  1500. (ref.index <> NR_NO) then
  1501. internalerror(200208101);
  1502. result :=
  1503. not (assigned(ref.symbol)) and
  1504. (((ref.index = NR_NO) and
  1505. (ref.offset >= low(smallint)) and
  1506. (ref.offset <= high(smallint))) or
  1507. ((ref.index <> NR_NO) and
  1508. (ref.offset = 0)));
  1509. end;
  1510. procedure tcgppc.a_load_store(list: TAsmList; op: tasmop; reg: tregister;
  1511. ref: treference);
  1512. procedure maybefixup64bitoffset;
  1513. var
  1514. tmpreg: tregister;
  1515. begin
  1516. { for some instructions we need to check that the offset is divisible by at
  1517. least four. If not, add the bytes which are "off" to the base register and
  1518. adjust the offset accordingly }
  1519. case op of
  1520. A_LD, A_LDU, A_STD, A_STDU, A_LWA :
  1521. if ((ref.offset mod 4) <> 0) then begin
  1522. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1523. if (ref.base <> NR_NO) then begin
  1524. a_op_const_reg_reg(list, OP_ADD, OS_ADDR, ref.offset mod 4, ref.base, tmpreg);
  1525. ref.base := tmpreg;
  1526. end else begin
  1527. list.concat(taicpu.op_reg_const(A_LI, tmpreg, ref.offset mod 4));
  1528. ref.base := tmpreg;
  1529. end;
  1530. ref.offset := (ref.offset div 4) * 4;
  1531. end;
  1532. end;
  1533. end;
  1534. var
  1535. tmpreg, tmpreg2: tregister;
  1536. tmpref: treference;
  1537. largeOffset: Boolean;
  1538. begin
  1539. if (target_info.system = system_powerpc64_darwin) then
  1540. begin
  1541. { darwin/ppc64 works with 32 bit relocatable symbol addresses }
  1542. maybefixup64bitoffset;
  1543. inherited a_load_store(list,op,reg,ref);
  1544. exit
  1545. end;
  1546. { at this point there must not be a combination of values in the ref treference
  1547. which is not possible to directly map to instructions of the PowerPC architecture }
  1548. if (ref.index <> NR_NO) and ((ref.offset <> 0) or (assigned(ref.symbol))) then
  1549. internalerror(200310131);
  1550. { if this is a PIC'ed address, handle it and exit }
  1551. if (ref.refaddr in [addr_pic,addr_pic_no_got]) then begin
  1552. if (ref.offset <> 0) then
  1553. internalerror(2006010501);
  1554. if (ref.index <> NR_NO) then
  1555. internalerror(2006010502);
  1556. if (not assigned(ref.symbol)) then
  1557. internalerror(200601050);
  1558. list.concat(taicpu.op_reg_ref(op, reg, ref));
  1559. exit;
  1560. end;
  1561. maybefixup64bitoffset;
  1562. {$IFDEF EXTDEBUG}
  1563. list.concat(tai_comment.create(strpnew('a_load_store1 ' + BoolToStr(ref.refaddr = addr_pic))));
  1564. {$ENDIF EXTDEBUG}
  1565. { if we have to load/store from a symbol or large addresses, use a temporary register
  1566. containing the address }
  1567. if (assigned(ref.symbol) or (hasLargeOffset(ref))) then begin
  1568. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1569. if (hasLargeOffset(ref) and (ref.base = NR_NO)) then begin
  1570. ref.base := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1571. a_load_const_reg(list, OS_ADDR, ref.offset, ref.base);
  1572. ref.offset := 0;
  1573. end;
  1574. reference_reset(tmpref, ref.alignment, ref.volatility);
  1575. tmpref.symbol := ref.symbol;
  1576. tmpref.relsymbol := ref.relsymbol;
  1577. tmpref.offset := ref.offset;
  1578. if (ref.base <> NR_NO) then begin
  1579. { As long as the TOC isn't working we try to achieve highest speed (in this
  1580. case by allowing instructions execute in parallel) as possible at the cost
  1581. of using another temporary register. So the code template when there is
  1582. a base register and an offset is the following:
  1583. lis rT1, SYM+offs@highest
  1584. ori rT1, rT1, SYM+offs@higher
  1585. lis rT2, SYM+offs@hi
  1586. ori rT2, SYM+offs@lo
  1587. rldimi rT2, rT1, 32
  1588. <op>X reg, base, rT2
  1589. }
  1590. tmpreg2 := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1591. if (assigned(tmpref.symbol)) then begin
  1592. tmpref.refaddr := addr_highest;
  1593. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  1594. tmpref.refaddr := addr_higher;
  1595. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  1596. tmpref.refaddr := addr_high;
  1597. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg2, tmpref));
  1598. tmpref.refaddr := addr_low;
  1599. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg2, tmpreg2, tmpref));
  1600. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, tmpreg2, tmpreg, 32, 0));
  1601. end else
  1602. a_load_const_reg(list, OS_ADDR, tmpref.offset, tmpreg2);
  1603. reference_reset(tmpref, ref.alignment, ref.volatility);
  1604. tmpref.base := ref.base;
  1605. tmpref.index := tmpreg2;
  1606. case op of
  1607. { the code generator doesn't generate update instructions anyway, so
  1608. error out on those instructions }
  1609. A_LBZ : op := A_LBZX;
  1610. A_LHZ : op := A_LHZX;
  1611. A_LWZ : op := A_LWZX;
  1612. A_LD : op := A_LDX;
  1613. A_LHA : op := A_LHAX;
  1614. A_LWA : op := A_LWAX;
  1615. A_LFS : op := A_LFSX;
  1616. A_LFD : op := A_LFDX;
  1617. A_STB : op := A_STBX;
  1618. A_STH : op := A_STHX;
  1619. A_STW : op := A_STWX;
  1620. A_STD : op := A_STDX;
  1621. A_STFS : op := A_STFSX;
  1622. A_STFD : op := A_STFDX;
  1623. else
  1624. { unknown load/store opcode }
  1625. internalerror(2005101302);
  1626. end;
  1627. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  1628. end else begin
  1629. { when accessing value from a reference without a base register, use the
  1630. following code template:
  1631. lis rT,SYM+offs@highesta
  1632. ori rT,SYM+offs@highera
  1633. sldi rT,rT,32
  1634. oris rT,rT,SYM+offs@ha
  1635. ld rD,SYM+offs@l(rT)
  1636. }
  1637. tmpref.refaddr := addr_highesta;
  1638. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  1639. tmpref.refaddr := addr_highera;
  1640. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  1641. list.concat(taicpu.op_reg_reg_const(A_SLDI, tmpreg, tmpreg, 32));
  1642. tmpref.refaddr := addr_higha;
  1643. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tmpreg, tmpreg, tmpref));
  1644. tmpref.base := tmpreg;
  1645. tmpref.refaddr := addr_low;
  1646. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  1647. end;
  1648. end else begin
  1649. list.concat(taicpu.op_reg_ref(op, reg, ref));
  1650. end;
  1651. end;
  1652. procedure tcgppc.loadConstantPIC(list : TAsmList; size : TCGSize; a : aint; reg : TRegister);
  1653. var
  1654. l: tasmsymbol;
  1655. ref: treference;
  1656. symname : string;
  1657. begin
  1658. maybe_new_object_file(current_asmdata.asmlists[al_picdata]);
  1659. symname := '_$' + current_asmdata.name^ + '$toc$' + hexstr(a, sizeof(a)*2);
  1660. l:=current_asmdata.getasmsymbol(symname);
  1661. if not(assigned(l)) then begin
  1662. l:=current_asmdata.DefineAsmSymbol(symname,AB_GLOBAL, AT_METADATA, voidpointertype);
  1663. new_section(current_asmdata.asmlists[al_picdata],sec_toc, '.toc', 8);
  1664. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create_global(l,0));
  1665. current_asmdata.asmlists[al_picdata].concat(tai_directive.create(asd_toc_entry, symname + '[TC], ' + inttostr(a)));
  1666. end;
  1667. reference_reset_symbol(ref,l,0,8,[]);
  1668. ref.base := NR_R2;
  1669. ref.refaddr := addr_no;
  1670. {$IFDEF EXTDEBUG}
  1671. list.concat(tai_comment.create(strpnew('loading value from TOC reference for ' + symname)));
  1672. {$ENDIF EXTDEBUG}
  1673. a_load_ref_reg(list, OS_INT, OS_INT, ref, reg);
  1674. end;
  1675. procedure create_codegen;
  1676. begin
  1677. cg := tcgppc.create;
  1678. cg128:=tcg128.create;
  1679. end;
  1680. end.