2
0

cgcpu.pas 77 KB

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