cgcpu.pas 69 KB

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