cgcpu.pas 73 KB

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