cgcpu.pas 68 KB

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