cgcpu.pas 79 KB

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