cgcpu.pas 83 KB

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