cgcpu.pas 77 KB

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