cgcpu.pas 79 KB

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