cgcpu.pas 68 KB

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