cgcpu.pas 75 KB

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