cgcpu.pas 69 KB

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