cgcpu.pas 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344
  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. extrdi_startbit : 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. // calculate the correct startbit for the extrdi instruction, do the extraction if required and then
  797. // extend the sign correctly. (The latter is actually required only for signed subsets and if that
  798. // subset is not >= the tosize.
  799. extrdi_startbit := 64 - (tcgsize2size[subsetsize]*8 + startbit);
  800. if (startbit <> 0) then begin
  801. list.concat(taicpu.op_reg_reg_const_const(A_EXTRDI, destreg, subsetreg, tcgsize2size[subsetsize]*8, extrdi_startbit));
  802. a_load_reg_reg(list, subsetsize, tosize, destreg, destreg);
  803. end else
  804. a_load_reg_reg(list, subsetsize, tosize, subsetreg, destreg);
  805. end;
  806. procedure tcgppc.a_load_reg_subsetreg(list : TAsmList; fromsize: tcgsize; subsetregsize,
  807. subsetsize: tcgsize; startbit: byte; fromreg, subsetreg: tregister);
  808. begin
  809. {$ifdef extdebug}
  810. list.concat(tai_comment.create(strpnew('a_load_reg_subsetreg fromsize = ' + cgsize2string(fromsize) + ' subsetregsize = ' + cgsize2string(subsetregsize) + ' subsetsize = ' + cgsize2string(subsetsize) + ' startbit = ' + IntToStr(startbit))));
  811. {$endif}
  812. list.concat(taicpu.op_reg_reg_const_const(A_INSRDI, subsetreg, fromreg, tcgsize2size[subsetsize]*8, (64 - (startbit + tcgsize2size[subsetsize]*8)) and 63));
  813. end;
  814. procedure tcgppc.a_load_const_subsetreg(list: TAsmlist; subsetregsize, subsetsize: tcgsize;
  815. startbit: byte; a: aint; subsetreg: tregister);
  816. var
  817. tmpreg : TRegister;
  818. begin
  819. {$ifdef extdebug}
  820. list.concat(tai_comment.create(strpnew('a_load_const_subsetreg subsetregsize = ' + cgsize2string(subsetregsize) + ' subsetsize = ' + cgsize2string(subsetsize) + ' startbit = ' + intToStr(startbit) + ' a = ' + intToStr(a))));
  821. {$endif}
  822. // simply loading the constant into the lowest bits of a temp register and then inserting is
  823. // better than loading some usually large constants and do some masking and shifting on ppc64
  824. tmpreg := getintregister(list,subsetsize);
  825. a_load_const_reg(list,subsetsize,a,tmpreg);
  826. a_load_reg_subsetreg(list, subsetsize, subsetregsize, subsetsize, startbit, tmpreg, subsetreg);
  827. end;
  828. procedure tcgppc.a_loadfpu_reg_reg(list: TAsmList; size: tcgsize;
  829. reg1, reg2: tregister);
  830. var
  831. instr: taicpu;
  832. begin
  833. instr := taicpu.op_reg_reg(A_FMR, reg2, reg1);
  834. list.concat(instr);
  835. rg[R_FPUREGISTER].add_move_instruction(instr);
  836. end;
  837. procedure tcgppc.a_loadfpu_ref_reg(list: TAsmList; size: tcgsize;
  838. const ref: treference; reg: tregister);
  839. const
  840. FpuLoadInstr: array[OS_F32..OS_F64, boolean, boolean] of TAsmOp =
  841. { indexed? updating?}
  842. (((A_LFS, A_LFSU), (A_LFSX, A_LFSUX)),
  843. ((A_LFD, A_LFDU), (A_LFDX, A_LFDUX)));
  844. var
  845. op: tasmop;
  846. ref2: treference;
  847. begin
  848. { several functions call this procedure with OS_32 or OS_64
  849. so this makes life easier (FK) }
  850. case size of
  851. OS_32, OS_F32:
  852. size := OS_F32;
  853. OS_64, OS_F64, OS_C64:
  854. size := OS_F64;
  855. else
  856. internalerror(200201121);
  857. end;
  858. ref2 := ref;
  859. fixref(list, ref2, size);
  860. op := fpuloadinstr[size, ref2.index <> NR_NO, false];
  861. a_load_store(list, op, reg, ref2);
  862. end;
  863. procedure tcgppc.a_loadfpu_reg_ref(list: TAsmList; size: tcgsize; reg:
  864. tregister; const ref: treference);
  865. const
  866. FpuStoreInstr: array[OS_F32..OS_F64, boolean, boolean] of TAsmOp =
  867. { indexed? updating? }
  868. (((A_STFS, A_STFSU), (A_STFSX, A_STFSUX)),
  869. ((A_STFD, A_STFDU), (A_STFDX, A_STFDUX)));
  870. var
  871. op: tasmop;
  872. ref2: treference;
  873. begin
  874. if not (size in [OS_F32, OS_F64]) then
  875. internalerror(200201122);
  876. ref2 := ref;
  877. fixref(list, ref2, size);
  878. op := fpustoreinstr[size, ref2.index <> NR_NO, false];
  879. a_load_store(list, op, reg, ref2);
  880. end;
  881. procedure tcgppc.a_op_const_reg(list: TAsmList; Op: TOpCG; size: TCGSize; a:
  882. aint; reg: TRegister);
  883. begin
  884. a_op_const_reg_reg(list, op, size, a, reg, reg);
  885. end;
  886. procedure tcgppc.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src,
  887. dst: TRegister);
  888. begin
  889. a_op_reg_reg_reg(list, op, size, src, dst, dst);
  890. end;
  891. procedure tcgppc.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  892. size: tcgsize; a: aint; src, dst: tregister);
  893. var
  894. useReg : boolean;
  895. procedure do_lo_hi(loOp, hiOp : TAsmOp);
  896. begin
  897. { Optimization for logical ops (excluding AND), trying to do this as efficiently
  898. as possible by only generating code for the affected halfwords. Note that all
  899. the instructions handled here must have "X op 0 = X" for every halfword. }
  900. usereg := false;
  901. if (aword(a) > high(dword)) then begin
  902. usereg := true;
  903. end else begin
  904. if (word(a) <> 0) then begin
  905. list.concat(taicpu.op_reg_reg_const(loOp, dst, src, word(a)));
  906. if (word(a shr 16) <> 0) then
  907. list.concat(taicpu.op_reg_reg_const(hiOp, dst, dst, word(a shr 16)));
  908. end else if (word(a shr 16) <> 0) then
  909. list.concat(taicpu.op_reg_reg_const(hiOp, dst, src, word(a shr 16)));
  910. end;
  911. end;
  912. procedure do_lo_hi_and;
  913. begin
  914. { optimization logical and with immediate: only use "andi." for 16 bit
  915. ands, otherwise use register method. Doing this for 32 bit constants
  916. would not give any advantage to the register method (via useReg := true),
  917. requiring a scratch register and three instructions. }
  918. usereg := false;
  919. if (aword(a) > high(word)) then
  920. usereg := true
  921. else
  922. list.concat(taicpu.op_reg_reg_const(A_ANDI_, dst, src, word(a)));
  923. end;
  924. procedure do_constant_div(list : TAsmList; size : TCgSize; a : aint; src, dst : TRegister;
  925. signed : boolean);
  926. const
  927. negops : array[boolean] of tasmop = (A_NEG, A_NEGO);
  928. var
  929. magic, shift : int64;
  930. u_magic : qword;
  931. u_shift : byte;
  932. u_add : boolean;
  933. power : byte;
  934. isNegPower : boolean;
  935. divreg : tregister;
  936. begin
  937. if (a = 0) then begin
  938. internalerror(2005061701);
  939. end else if (a = 1) then begin
  940. cg.a_load_reg_reg(current_asmdata.CurrAsmList, OS_INT, OS_INT, src, dst);
  941. end else if (a = -1) and (signed) then begin
  942. { note: only in the signed case possible..., may overflow }
  943. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(negops[cs_check_overflow in aktlocalswitches], dst, src));
  944. end else if (ispowerof2(a, power, isNegPower)) then begin
  945. if (signed) then begin
  946. { From "The PowerPC Compiler Writer's Guide", pg. 52ff }
  947. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, power,
  948. src, dst);
  949. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_ADDZE, dst, dst));
  950. if (isNegPower) then
  951. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_NEG, dst, dst));
  952. end else begin
  953. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, power, src, dst)
  954. end;
  955. end else begin
  956. { replace division by multiplication, both implementations }
  957. { from "The PowerPC Compiler Writer's Guide" pg. 53ff }
  958. divreg := cg.getintregister(current_asmdata.CurrAsmList, OS_INT);
  959. if (signed) then begin
  960. getmagic_signedN(sizeof(aInt)*8, a, magic, shift);
  961. { load magic value }
  962. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, magic, divreg);
  963. { multiply }
  964. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHD, dst, src, divreg));
  965. { add/subtract numerator }
  966. if (a > 0) and (magic < 0) then begin
  967. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_INT, src, dst, dst);
  968. end else if (a < 0) and (magic > 0) then begin
  969. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT, src, dst, dst);
  970. end;
  971. { shift shift places to the right (arithmetic) }
  972. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SAR, OS_INT, shift, dst, dst);
  973. { extract and add sign bit }
  974. if (a >= 0) then begin
  975. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, 63, src, divreg);
  976. end else begin
  977. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, 63, dst, divreg);
  978. end;
  979. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_INT, dst, divreg, dst);
  980. end else begin
  981. getmagic_unsignedN(sizeof(aWord)*8, a, u_magic, u_add, u_shift);
  982. { load magic in divreg }
  983. cg.a_load_const_reg(current_asmdata.CurrAsmList, OS_INT, u_magic, divreg);
  984. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_MULHDU, dst, src, divreg));
  985. if (u_add) then begin
  986. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_SUB, OS_INT, dst, src, divreg);
  987. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, 1, divreg, divreg);
  988. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList, OP_ADD, OS_INT, divreg, dst, divreg);
  989. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, u_shift-1, divreg, dst);
  990. end else begin
  991. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList, OP_SHR, OS_INT, u_shift, dst, dst);
  992. end;
  993. end;
  994. end;
  995. end;
  996. var
  997. scratchreg: tregister;
  998. shift : byte;
  999. shiftmask : longint;
  1000. isneg : boolean;
  1001. begin
  1002. { subtraction is the same as addition with negative constant }
  1003. if op = OP_SUB then begin
  1004. a_op_const_reg_reg(list, OP_ADD, size, -a, src, dst);
  1005. exit;
  1006. end;
  1007. {$IFDEF EXTDEBUG}
  1008. list.concat(tai_comment.create(strpnew('a_op_const_reg_reg ' + cgop2string(op))));
  1009. {$ENDIF EXTDEBUG}
  1010. { This case includes some peephole optimizations for the various operations,
  1011. (e.g. AND, OR, XOR, ..) - can't this be done at some higher level,
  1012. independent of architecture? }
  1013. { assume that we do not need a scratch register for the operation }
  1014. useReg := false;
  1015. case (op) of
  1016. OP_DIV, OP_IDIV:
  1017. if (cs_opt_level1 in aktoptimizerswitches) then
  1018. do_constant_div(list, size, a, src, dst, op = OP_IDIV)
  1019. else
  1020. usereg := true;
  1021. OP_IMUL, OP_MUL:
  1022. { idea: factorize constant multiplicands and use adds/shifts with few factors;
  1023. however, even a 64 bit multiply is already quite fast on PPC64 }
  1024. if (a = 0) then
  1025. a_load_const_reg(list, size, 0, dst)
  1026. else if (a = -1) then
  1027. list.concat(taicpu.op_reg_reg(A_NEG, dst, dst))
  1028. else if (a = 1) then
  1029. a_load_reg_reg(list, OS_INT, OS_INT, src, dst)
  1030. else if ispowerof2(a, shift, isneg) then begin
  1031. list.concat(taicpu.op_reg_reg_const(A_SLDI, dst, src, shift));
  1032. if (isneg) then
  1033. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_NEG, dst, dst));
  1034. end else if (a >= low(smallint)) and (a <= high(smallint)) then
  1035. list.concat(taicpu.op_reg_reg_const(A_MULLI, dst, src,
  1036. smallint(a)))
  1037. else
  1038. usereg := true;
  1039. OP_ADD:
  1040. if (a = 0) then
  1041. a_load_reg_reg(list, size, size, src, dst)
  1042. else if (a >= low(smallint)) and (a <= high(smallint)) then
  1043. list.concat(taicpu.op_reg_reg_const(A_ADDI, dst, src, smallint(a)))
  1044. else
  1045. useReg := true;
  1046. OP_OR:
  1047. if (a = 0) then
  1048. a_load_reg_reg(list, size, size, src, dst)
  1049. else if (a = -1) then
  1050. a_load_const_reg(list, size, -1, dst)
  1051. else
  1052. do_lo_hi(A_ORI, A_ORIS);
  1053. OP_AND:
  1054. if (a = 0) then
  1055. a_load_const_reg(list, size, 0, dst)
  1056. else if (a = -1) then
  1057. a_load_reg_reg(list, size, size, src, dst)
  1058. else
  1059. do_lo_hi_and;
  1060. OP_XOR:
  1061. if (a = 0) then
  1062. a_load_reg_reg(list, size, size, src, dst)
  1063. else if (a = -1) then
  1064. list.concat(taicpu.op_reg_reg(A_NOT, dst, src))
  1065. else
  1066. do_lo_hi(A_XORI, A_XORIS);
  1067. OP_SHL, OP_SHR, OP_SAR:
  1068. begin
  1069. if (size in [OS_64, OS_S64]) then
  1070. shift := 6
  1071. else
  1072. shift := 5;
  1073. shiftmask := (1 shl shift)-1;
  1074. if (a and shiftmask) <> 0 then
  1075. list.concat(taicpu.op_reg_reg_const(
  1076. TShiftOpCG2AsmOpConst[size in [OS_64, OS_S64], op], dst, src, a and shiftmask))
  1077. else
  1078. a_load_reg_reg(list, size, size, src, dst);
  1079. if ((a shr shift) <> 0) then
  1080. internalError(68991);
  1081. end
  1082. else
  1083. internalerror(200109091);
  1084. end;
  1085. { if all else failed, load the constant in a register and then
  1086. perform the operation }
  1087. if (useReg) then begin
  1088. scratchreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1089. a_load_const_reg(list, size, a, scratchreg);
  1090. a_op_reg_reg_reg(list, op, size, scratchreg, src, dst);
  1091. end;
  1092. end;
  1093. procedure tcgppc.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1094. size: tcgsize; src1, src2, dst: tregister);
  1095. const
  1096. op_reg_reg_opcg2asmop32: array[TOpCG] of tasmop =
  1097. (A_NONE, A_MR, A_ADD, A_AND, A_DIVWU, A_DIVW, A_MULLW, A_MULLW, A_NEG, A_NOT, A_OR,
  1098. A_SRAW, A_SLW, A_SRW, A_SUB, A_XOR);
  1099. op_reg_reg_opcg2asmop64: array[TOpCG] of tasmop =
  1100. (A_NONE, A_MR, A_ADD, A_AND, A_DIVDU, A_DIVD, A_MULLD, A_MULLD, A_NEG, A_NOT, A_OR,
  1101. A_SRAD, A_SLD, A_SRD, A_SUB, A_XOR);
  1102. begin
  1103. case op of
  1104. OP_NEG, OP_NOT:
  1105. begin
  1106. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop64[op], dst, src1));
  1107. if (op = OP_NOT) and not (size in [OS_64, OS_S64]) then
  1108. { zero/sign extend result again, fromsize is not important here }
  1109. a_load_reg_reg(list, OS_S64, size, dst, dst)
  1110. end;
  1111. else
  1112. if (size in [OS_64, OS_S64]) then begin
  1113. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop64[op], dst, src2,
  1114. src1));
  1115. end else begin
  1116. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop32[op], dst, src2,
  1117. src1));
  1118. end;
  1119. end;
  1120. end;
  1121. {*************** compare instructructions ****************}
  1122. procedure tcgppc.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  1123. cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  1124. var
  1125. scratch_register: TRegister;
  1126. signed: boolean;
  1127. begin
  1128. {$IFDEF EXTDEBUG}
  1129. list.concat(tai_comment.create(strpnew('a_cmp_const_reg_label ' + inttostr(ord(size)) + ' ' + inttostr(tcgsize2size[size]))));
  1130. {$ENDIF EXTDEBUG}
  1131. signed := cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE];
  1132. { in the following case, we generate more efficient code when }
  1133. { signed is true }
  1134. if (cmp_op in [OC_EQ, OC_NE]) and
  1135. (aword(a) > $FFFF) then
  1136. signed := true;
  1137. if signed then
  1138. if (a >= low(smallint)) and (a <= high(smallint)) then
  1139. list.concat(taicpu.op_reg_reg_const(A_CMPDI, NR_CR0, reg, 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_CMPD, NR_CR0, reg, scratch_register));
  1144. end
  1145. else if (aword(a) <= $FFFF) then
  1146. list.concat(taicpu.op_reg_reg_const(A_CMPLDI, NR_CR0, reg, aword(a)))
  1147. else begin
  1148. scratch_register := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1149. a_load_const_reg(list, OS_INT, a, scratch_register);
  1150. list.concat(taicpu.op_reg_reg_reg(A_CMPLD, NR_CR0, reg,
  1151. scratch_register));
  1152. end;
  1153. a_jmp(list, A_BC, TOpCmp2AsmCond[cmp_op], 0, l);
  1154. end;
  1155. procedure tcgppc.a_cmp_reg_reg_label(list: TAsmList; size: tcgsize;
  1156. cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1157. var
  1158. op: tasmop;
  1159. begin
  1160. {$IFDEF extdebug}
  1161. list.concat(tai_comment.create(strpnew('a_cmp_reg_reg_label, size ' + cgsize2string(size) + ' op ' + inttostr(ord(cmp_op)))));
  1162. {$ENDIF extdebug}
  1163. if cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE] then
  1164. if (size in [OS_64, OS_S64]) then
  1165. op := A_CMPD
  1166. else
  1167. op := A_CMPW
  1168. else
  1169. if (size in [OS_64, OS_S64]) then
  1170. op := A_CMPLD
  1171. else
  1172. op := A_CMPLW;
  1173. list.concat(taicpu.op_reg_reg_reg(op, NR_CR0, reg2, reg1));
  1174. a_jmp(list, A_BC, TOpCmp2AsmCond[cmp_op], 0, l);
  1175. end;
  1176. procedure tcgppc.a_jmp_cond(list: TAsmList; cond: TOpCmp; l: tasmlabel);
  1177. begin
  1178. a_jmp(list, A_BC, TOpCmp2AsmCond[cond], 0, l);
  1179. end;
  1180. procedure tcgppc.a_jmp_name(list: TAsmList; const s: string);
  1181. var
  1182. p: taicpu;
  1183. begin
  1184. p := taicpu.op_sym(A_B, current_asmdata.RefAsmSymbol(s));
  1185. p.is_jmp := true;
  1186. list.concat(p)
  1187. end;
  1188. procedure tcgppc.a_jmp_always(list: TAsmList; l: tasmlabel);
  1189. begin
  1190. a_jmp(list, A_B, C_None, 0, l);
  1191. end;
  1192. procedure tcgppc.a_jmp_flags(list: TAsmList; const f: TResFlags; l:
  1193. tasmlabel);
  1194. var
  1195. c: tasmcond;
  1196. begin
  1197. c := flags_to_cond(f);
  1198. a_jmp(list, A_BC, c.cond, c.cr - RS_CR0, l);
  1199. end;
  1200. procedure tcgppc.g_flags2reg(list: TAsmList; size: TCgSize; const f:
  1201. TResFlags; reg: TRegister);
  1202. var
  1203. testbit: byte;
  1204. bitvalue: boolean;
  1205. begin
  1206. { get the bit to extract from the conditional register + its requested value (0 or 1) }
  1207. testbit := ((f.cr - RS_CR0) * 4);
  1208. case f.flag of
  1209. F_EQ, F_NE:
  1210. begin
  1211. inc(testbit, 2);
  1212. bitvalue := f.flag = F_EQ;
  1213. end;
  1214. F_LT, F_GE:
  1215. begin
  1216. bitvalue := f.flag = F_LT;
  1217. end;
  1218. F_GT, F_LE:
  1219. begin
  1220. inc(testbit);
  1221. bitvalue := f.flag = F_GT;
  1222. end;
  1223. else
  1224. internalerror(200112261);
  1225. end;
  1226. { load the conditional register in the destination reg }
  1227. list.concat(taicpu.op_reg(A_MFCR, reg));
  1228. { we will move the bit that has to be tested to bit 0 by rotating left }
  1229. testbit := (testbit + 1) and 31;
  1230. { extract bit }
  1231. list.concat(taicpu.op_reg_reg_const_const_const(
  1232. A_RLWINM,reg,reg,testbit,31,31));
  1233. { if we need the inverse, xor with 1 }
  1234. if not bitvalue then
  1235. list.concat(taicpu.op_reg_reg_const(A_XORI, reg, reg, 1));
  1236. end;
  1237. { *********** entry/exit code and address loading ************ }
  1238. procedure tcgppc.g_save_standard_registers(list: TAsmList);
  1239. begin
  1240. { this work is done in g_proc_entry; additionally it is not safe
  1241. to use it because it is called at some weird time }
  1242. end;
  1243. procedure tcgppc.g_restore_standard_registers(list: TAsmList);
  1244. begin
  1245. { this work is done in g_proc_exit; mainly because it is not safe to
  1246. put the register restore code here because it is called at some weird time }
  1247. end;
  1248. procedure tcgppc.calcFirstUsedFPR(out firstfpr : TSuperRegister; out fprcount : aint);
  1249. var
  1250. reg : TSuperRegister;
  1251. begin
  1252. fprcount := 0;
  1253. firstfpr := RS_F31;
  1254. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1255. for reg := RS_F14 to RS_F31 do
  1256. if reg in rg[R_FPUREGISTER].used_in_proc then begin
  1257. fprcount := ord(RS_F31)-ord(reg)+1;
  1258. firstfpr := reg;
  1259. break;
  1260. end;
  1261. end;
  1262. procedure tcgppc.calcFirstUsedGPR(out firstgpr : TSuperRegister; out gprcount : aint);
  1263. var
  1264. reg : TSuperRegister;
  1265. begin
  1266. gprcount := 0;
  1267. firstgpr := RS_R31;
  1268. if not (po_assembler in current_procinfo.procdef.procoptions) then
  1269. for reg := RS_R14 to RS_R31 do
  1270. if reg in rg[R_INTREGISTER].used_in_proc then begin
  1271. gprcount := ord(RS_R31)-ord(reg)+1;
  1272. firstgpr := reg;
  1273. break;
  1274. end;
  1275. end;
  1276. procedure tcgppc.profilecode_savepara(para : tparavarsym; list : TAsmList);
  1277. begin
  1278. case (para.paraloc[calleeside].location^.loc) of
  1279. LOC_REGISTER, LOC_CREGISTER:
  1280. a_load_reg_ref(list, OS_INT, para.paraloc[calleeside].Location^.size,
  1281. para.paraloc[calleeside].Location^.register, para.localloc.reference);
  1282. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1283. a_loadfpu_reg_ref(list, para.paraloc[calleeside].Location^.size,
  1284. para.paraloc[calleeside].Location^.register, para.localloc.reference);
  1285. LOC_MMREGISTER, LOC_CMMREGISTER:
  1286. // not supported
  1287. internalerror(2006041801);
  1288. end;
  1289. end;
  1290. procedure tcgppc.profilecode_restorepara(para : tparavarsym; list : TAsmList);
  1291. begin
  1292. case (para.paraloc[calleeside].Location^.loc) of
  1293. LOC_REGISTER, LOC_CREGISTER:
  1294. a_load_ref_reg(list, para.paraloc[calleeside].Location^.size, OS_INT,
  1295. para.localloc.reference, para.paraloc[calleeside].Location^.register);
  1296. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  1297. a_loadfpu_ref_reg(list, para.paraloc[calleeside].Location^.size,
  1298. para.localloc.reference, para.paraloc[calleeside].Location^.register);
  1299. LOC_MMREGISTER, LOC_CMMREGISTER:
  1300. // not supported
  1301. internalerror(2006041802);
  1302. end;
  1303. end;
  1304. procedure tcgppc.g_profilecode(list: TAsmList);
  1305. begin
  1306. current_procinfo.procdef.paras.ForEachCall(TObjectListCallback(@profilecode_savepara), list);
  1307. a_call_name_direct(list, '_mcount', false, true);
  1308. current_procinfo.procdef.paras.ForEachCall(TObjectListCallback(@profilecode_restorepara), list);
  1309. end;
  1310. { Generates the entry code of a procedure/function.
  1311. This procedure may be called before, as well as after g_return_from_proc
  1312. is called. localsize is the sum of the size necessary for local variables
  1313. and the maximum possible combined size of ALL the parameters of a procedure
  1314. called by the current one
  1315. IMPORTANT: registers are not to be allocated through the register
  1316. allocator here, because the register colouring has already occured !!
  1317. }
  1318. procedure tcgppc.g_proc_entry(list: TAsmList; localsize: longint;
  1319. nostackframe: boolean);
  1320. var
  1321. firstregfpu, firstreggpr: TSuperRegister;
  1322. needslinkreg: boolean;
  1323. fprcount, gprcount : aint;
  1324. { Save standard registers, both FPR and GPR; does not support VMX/Altivec }
  1325. procedure save_standard_registers;
  1326. var
  1327. regcount : TSuperRegister;
  1328. href : TReference;
  1329. mayNeedLRStore : boolean;
  1330. begin
  1331. { there are two ways to do this: manually, by generating a few "std" instructions,
  1332. or via the restore helper functions. The latter are selected by the -Og switch,
  1333. i.e. "optimize for size" }
  1334. if (cs_opt_size in aktoptimizerswitches) then begin
  1335. mayNeedLRStore := false;
  1336. if ((fprcount > 0) and (gprcount > 0)) then begin
  1337. a_op_const_reg_reg(list, OP_SUB, OS_INT, 8 * fprcount, NR_R1, NR_R12);
  1338. a_call_name_direct(list, '_savegpr1_' + intToStr(32-gprcount), false, false, false);
  1339. a_call_name_direct(list, '_savefpr_' + intToStr(32-fprcount), false, false, false);
  1340. end else if (gprcount > 0) then
  1341. a_call_name_direct(list, '_savegpr0_' + intToStr(32-gprcount), false, false, false)
  1342. else if (fprcount > 0) then
  1343. a_call_name_direct(list, '_savefpr_' + intToStr(32-fprcount), false, false, false)
  1344. else
  1345. mayNeedLRStore := true;
  1346. end else begin
  1347. { save registers, FPU first, then GPR }
  1348. reference_reset_base(href, NR_STACK_POINTER_REG, -8);
  1349. if (fprcount > 0) then
  1350. for regcount := RS_F31 downto firstregfpu do begin
  1351. a_loadfpu_reg_ref(list, OS_FLOAT, newreg(R_FPUREGISTER, regcount,
  1352. R_SUBNONE), href);
  1353. dec(href.offset, tcgsize2size[OS_FLOAT]);
  1354. end;
  1355. if (gprcount > 0) then
  1356. for regcount := RS_R31 downto firstreggpr do begin
  1357. a_load_reg_ref(list, OS_INT, OS_INT, newreg(R_INTREGISTER, regcount,
  1358. R_SUBNONE), href);
  1359. dec(href.offset, tcgsize2size[OS_INT]);
  1360. end;
  1361. { VMX registers not supported by FPC atm }
  1362. { in this branch we always need to store LR ourselves}
  1363. mayNeedLRStore := true;
  1364. end;
  1365. { we may need to store R0 (=LR) ourselves }
  1366. if ((cs_profile in initmoduleswitches) or (mayNeedLRStore)) and (needslinkreg) then begin
  1367. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_ELF);
  1368. list.concat(taicpu.op_reg_ref(A_STD, NR_R0, href));
  1369. end;
  1370. end;
  1371. var
  1372. href: treference;
  1373. begin
  1374. calcFirstUsedFPR(firstregfpu, fprcount);
  1375. calcFirstUsedGPR(firstreggpr, gprcount);
  1376. { calculate real stack frame size }
  1377. localsize := tppcprocinfo(current_procinfo).calc_stackframe_size(
  1378. gprcount, fprcount);
  1379. { determine whether we need to save the link register }
  1380. needslinkreg :=
  1381. ((not (po_assembler in current_procinfo.procdef.procoptions)) and
  1382. ((pi_do_call in current_procinfo.flags) or (cs_profile in initmoduleswitches))) or
  1383. ((cs_opt_size in aktoptimizerswitches) and ((fprcount > 0) or (gprcount > 0))) or
  1384. ([cs_lineinfo, cs_debuginfo] * aktmoduleswitches <> []);
  1385. a_reg_alloc(list, NR_STACK_POINTER_REG);
  1386. a_reg_alloc(list, NR_R0);
  1387. { move link register to r0 }
  1388. if (needslinkreg) then
  1389. list.concat(taicpu.op_reg(A_MFLR, NR_R0));
  1390. save_standard_registers;
  1391. { save old stack frame pointer }
  1392. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then begin
  1393. a_reg_alloc(list, NR_OLD_STACK_POINTER_REG);
  1394. list.concat(taicpu.op_reg_reg(A_MR, NR_OLD_STACK_POINTER_REG, NR_STACK_POINTER_REG));
  1395. end;
  1396. { create stack frame }
  1397. if (not nostackframe) and (localsize > 0) then begin
  1398. if (localsize <= high(smallint)) then begin
  1399. reference_reset_base(href, NR_STACK_POINTER_REG, -localsize);
  1400. a_load_store(list, A_STDU, NR_STACK_POINTER_REG, href);
  1401. end else begin
  1402. reference_reset_base(href, NR_NO, -localsize);
  1403. { Use R0 for loading the constant (which is definitely > 32k when entering
  1404. this branch).
  1405. Inlined at this position because it must not use temp registers because
  1406. register allocations have already been done }
  1407. { Code template:
  1408. lis r0,ofs@highest
  1409. ori r0,r0,ofs@higher
  1410. sldi r0,r0,32
  1411. oris r0,r0,ofs@h
  1412. ori r0,r0,ofs@l
  1413. }
  1414. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  1415. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  1416. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  1417. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  1418. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  1419. list.concat(taicpu.op_reg_reg_reg(A_STDUX, NR_R1, NR_R1, NR_R0));
  1420. end;
  1421. end;
  1422. { CR register not used by FPC atm }
  1423. { keep R1 allocated??? }
  1424. a_reg_dealloc(list, NR_R0);
  1425. end;
  1426. { Generates the exit code for a method.
  1427. This procedure may be called before, as well as after g_stackframe_entry
  1428. is called.
  1429. IMPORTANT: registers are not to be allocated through the register
  1430. allocator here, because the register colouring has already occured !!
  1431. }
  1432. procedure tcgppc.g_proc_exit(list: TAsmList; parasize: longint; nostackframe:
  1433. boolean);
  1434. var
  1435. firstregfpu, firstreggpr: TSuperRegister;
  1436. needslinkreg : boolean;
  1437. fprcount, gprcount: aint;
  1438. { Restore standard registers, both FPR and GPR; does not support VMX/Altivec }
  1439. procedure restore_standard_registers;
  1440. var
  1441. { flag indicating whether we need to manually add the exit code (e.g. blr instruction)
  1442. or not }
  1443. needsExitCode : Boolean;
  1444. href : treference;
  1445. regcount : TSuperRegister;
  1446. begin
  1447. { there are two ways to do this: manually, by generating a few "ld" instructions,
  1448. or via the restore helper functions. The latter are selected by the -Og switch,
  1449. i.e. "optimize for size" }
  1450. if (cs_opt_size in aktoptimizerswitches) then begin
  1451. needsExitCode := false;
  1452. if ((fprcount > 0) and (gprcount > 0)) then begin
  1453. a_op_const_reg_reg(list, OP_SUB, OS_INT, 8 * fprcount, NR_R1, NR_R12);
  1454. a_call_name_direct(list, '_restgpr1_' + intToStr(32-gprcount), false, false, false);
  1455. a_jmp_name(list, '_restfpr_' + intToStr(32-fprcount));
  1456. end else if (gprcount > 0) then
  1457. a_jmp_name(list, '_restgpr0_' + intToStr(32-gprcount))
  1458. else if (fprcount > 0) then
  1459. a_jmp_name(list, '_restfpr_' + intToStr(32-fprcount))
  1460. else
  1461. needsExitCode := true;
  1462. end else begin
  1463. needsExitCode := true;
  1464. { restore registers, FPU first, GPR next }
  1465. reference_reset_base(href, NR_STACK_POINTER_REG, -tcgsize2size[OS_FLOAT]);
  1466. if (fprcount > 0) then
  1467. for regcount := RS_F31 downto firstregfpu do begin
  1468. a_loadfpu_ref_reg(list, OS_FLOAT, href, newreg(R_FPUREGISTER, regcount,
  1469. R_SUBNONE));
  1470. dec(href.offset, tcgsize2size[OS_FLOAT]);
  1471. end;
  1472. if (gprcount > 0) then
  1473. for regcount := RS_R31 downto firstreggpr do begin
  1474. a_load_ref_reg(list, OS_INT, OS_INT, href, newreg(R_INTREGISTER, regcount,
  1475. R_SUBNONE));
  1476. dec(href.offset, tcgsize2size[OS_INT]);
  1477. end;
  1478. { VMX not supported by FPC atm }
  1479. end;
  1480. if (needsExitCode) then begin
  1481. { restore LR (if needed) }
  1482. if (needslinkreg) then begin
  1483. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_ELF);
  1484. list.concat(taicpu.op_reg_ref(A_LD, NR_R0, href));
  1485. list.concat(taicpu.op_reg(A_MTLR, NR_R0));
  1486. end;
  1487. { generate return instruction }
  1488. list.concat(taicpu.op_none(A_BLR));
  1489. end;
  1490. end;
  1491. var
  1492. href: treference;
  1493. localsize : aint;
  1494. begin
  1495. calcFirstUsedFPR(firstregfpu, fprcount);
  1496. calcFirstUsedGPR(firstreggpr, gprcount);
  1497. { determine whether we need to restore the link register }
  1498. needslinkreg :=
  1499. ((not (po_assembler in current_procinfo.procdef.procoptions)) and
  1500. ((pi_do_call in current_procinfo.flags) or (cs_profile in initmoduleswitches))) or
  1501. ((cs_opt_size in aktoptimizerswitches) and ((fprcount > 0) or (gprcount > 0))) or
  1502. ([cs_lineinfo, cs_debuginfo] * aktmoduleswitches <> []);
  1503. { calculate stack frame }
  1504. localsize := tppcprocinfo(current_procinfo).calc_stackframe_size(
  1505. gprcount, fprcount);
  1506. { CR register not supported }
  1507. { restore stack pointer }
  1508. if (not nostackframe) and (localsize > 0) then begin
  1509. if (localsize <= high(smallint)) then begin
  1510. list.concat(taicpu.op_reg_reg_const(A_ADDI, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, localsize));
  1511. end else begin
  1512. reference_reset_base(href, NR_NO, localsize);
  1513. { use R0 for loading the constant (which is definitely > 32k when entering
  1514. this branch)
  1515. Inlined because it must not use temp registers because register allocations
  1516. have already been done
  1517. }
  1518. { Code template:
  1519. lis r0,ofs@highest
  1520. ori r0,ofs@higher
  1521. sldi r0,r0,32
  1522. oris r0,r0,ofs@h
  1523. ori r0,r0,ofs@l
  1524. }
  1525. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  1526. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  1527. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  1528. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  1529. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  1530. list.concat(taicpu.op_reg_reg_reg(A_ADD, NR_R1, NR_R1, NR_R0));
  1531. end;
  1532. end;
  1533. restore_standard_registers;
  1534. end;
  1535. procedure tcgppc.a_loadaddr_ref_reg(list: TAsmList; const ref: treference; r:
  1536. tregister);
  1537. var
  1538. ref2, tmpref: treference;
  1539. { register used to construct address }
  1540. tempreg : TRegister;
  1541. begin
  1542. ref2 := ref;
  1543. fixref(list, ref2, OS_64);
  1544. { load a symbol }
  1545. if (assigned(ref2.symbol) or (hasLargeOffset(ref2))) then begin
  1546. { add the symbol's value to the base of the reference, and if the }
  1547. { reference doesn't have a base, create one }
  1548. reference_reset(tmpref);
  1549. tmpref.offset := ref2.offset;
  1550. tmpref.symbol := ref2.symbol;
  1551. tmpref.relsymbol := ref2.relsymbol;
  1552. { load 64 bit reference into r. If the reference already has a base register,
  1553. first load the 64 bit value into a temp register, then add it to the result
  1554. register rD }
  1555. if (ref2.base <> NR_NO) then begin
  1556. { already have a base register, so allocate a new one }
  1557. tempreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1558. end else begin
  1559. tempreg := r;
  1560. end;
  1561. { code for loading a reference from a symbol into a register rD }
  1562. (*
  1563. lis rX,SYM@highest
  1564. ori rX,SYM@higher
  1565. sldi rX,rX,32
  1566. oris rX,rX,SYM@h
  1567. ori rX,rX,SYM@l
  1568. *)
  1569. {$IFDEF EXTDEBUG}
  1570. list.concat(tai_comment.create(strpnew('loadaddr_ref_reg ')));
  1571. {$ENDIF EXTDEBUG}
  1572. if (assigned(tmpref.symbol)) then begin
  1573. tmpref.refaddr := addr_highest;
  1574. list.concat(taicpu.op_reg_ref(A_LIS, tempreg, tmpref));
  1575. tmpref.refaddr := addr_higher;
  1576. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1577. list.concat(taicpu.op_reg_reg_const(A_SLDI, tempreg, tempreg, 32));
  1578. tmpref.refaddr := addr_high;
  1579. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tempreg, tempreg, tmpref));
  1580. tmpref.refaddr := addr_low;
  1581. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1582. end else
  1583. a_load_const_reg(list, OS_ADDR, tmpref.offset, tempreg);
  1584. { if there's already a base register, add the temp register contents to
  1585. the base register }
  1586. if (ref2.base <> NR_NO) then begin
  1587. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, tempreg, ref2.base));
  1588. end;
  1589. end else if (ref2.offset <> 0) then begin
  1590. { no symbol, but offset <> 0 }
  1591. if (ref2.base <> NR_NO) then begin
  1592. a_op_const_reg_reg(list, OP_ADD, OS_64, ref2.offset, ref2.base, r)
  1593. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never
  1594. occurs, so now only ref.offset has to be loaded }
  1595. end else begin
  1596. a_load_const_reg(list, OS_64, ref2.offset, r);
  1597. end;
  1598. end else if (ref2.index <> NR_NO) then begin
  1599. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, ref2.base, ref2.index))
  1600. end else if (ref2.base <> NR_NO) and
  1601. (r <> ref2.base) then begin
  1602. a_load_reg_reg(list, OS_ADDR, OS_ADDR, ref2.base, r)
  1603. //list.concat(taicpu.op_reg_reg(A_MR, ref2.base, r));
  1604. end else begin
  1605. list.concat(taicpu.op_reg_const(A_LI, r, 0));
  1606. end;
  1607. end;
  1608. { ************* concatcopy ************ }
  1609. const
  1610. maxmoveunit = 8;
  1611. procedure tcgppc.g_concatcopy(list: TAsmList; const source, dest: treference;
  1612. len: aint);
  1613. var
  1614. countreg, tempreg: TRegister;
  1615. src, dst: TReference;
  1616. lab: tasmlabel;
  1617. count, count2: longint;
  1618. size: tcgsize;
  1619. begin
  1620. {$IFDEF extdebug}
  1621. if len > high(aint) then
  1622. internalerror(2002072704);
  1623. list.concat(tai_comment.create(strpnew('g_concatcopy1 ' + inttostr(len) + ' bytes left ')));
  1624. {$ENDIF extdebug}
  1625. { if the references are equal, exit, there is no need to copy anything }
  1626. if (references_equal(source, dest)) then
  1627. exit;
  1628. { make sure short loads are handled as optimally as possible;
  1629. note that the data here never overlaps, so we can do a forward
  1630. copy at all times.
  1631. NOTE: maybe use some scratch registers to pair load/store instructions
  1632. }
  1633. if (len <= maxmoveunit) then begin
  1634. src := source; dst := dest;
  1635. {$IFDEF extdebug}
  1636. list.concat(tai_comment.create(strpnew('g_concatcopy3 ' + inttostr(src.offset) + ' ' + inttostr(dst.offset))));
  1637. {$ENDIF extdebug}
  1638. while (len <> 0) do begin
  1639. if (len = 8) then begin
  1640. a_load_ref_ref(list, OS_64, OS_64, src, dst);
  1641. dec(len, 8);
  1642. end else if (len >= 4) then begin
  1643. a_load_ref_ref(list, OS_32, OS_32, src, dst);
  1644. inc(src.offset, 4); inc(dst.offset, 4);
  1645. dec(len, 4);
  1646. end else if (len >= 2) then begin
  1647. a_load_ref_ref(list, OS_16, OS_16, src, dst);
  1648. inc(src.offset, 2); inc(dst.offset, 2);
  1649. dec(len, 2);
  1650. end else begin
  1651. a_load_ref_ref(list, OS_8, OS_8, src, dst);
  1652. inc(src.offset, 1); inc(dst.offset, 1);
  1653. dec(len, 1);
  1654. end;
  1655. end;
  1656. exit;
  1657. end;
  1658. {$IFDEF extdebug}
  1659. list.concat(tai_comment.create(strpnew('g_concatcopy2 ' + inttostr(len) + ' bytes left ')));
  1660. {$ENDIF extdebug}
  1661. count := len div maxmoveunit;
  1662. reference_reset(src);
  1663. reference_reset(dst);
  1664. { load the address of source into src.base }
  1665. if (count > 4) or
  1666. not issimpleref(source) or
  1667. ((source.index <> NR_NO) and
  1668. ((source.offset + len) > high(smallint))) then begin
  1669. src.base := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1670. a_loadaddr_ref_reg(list, source, src.base);
  1671. end else begin
  1672. src := source;
  1673. end;
  1674. { load the address of dest into dst.base }
  1675. if (count > 4) or
  1676. not issimpleref(dest) or
  1677. ((dest.index <> NR_NO) and
  1678. ((dest.offset + len) > high(smallint))) then begin
  1679. dst.base := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1680. a_loadaddr_ref_reg(list, dest, dst.base);
  1681. end else begin
  1682. dst := dest;
  1683. end;
  1684. { generate a loop }
  1685. if count > 4 then begin
  1686. { the offsets are zero after the a_loadaddress_ref_reg and just
  1687. have to be set to 8. I put an Inc there so debugging may be
  1688. easier (should offset be different from zero here, it will be
  1689. easy to notice in the generated assembler }
  1690. inc(dst.offset, 8);
  1691. inc(src.offset, 8);
  1692. list.concat(taicpu.op_reg_reg_const(A_SUBI, src.base, src.base, 8));
  1693. list.concat(taicpu.op_reg_reg_const(A_SUBI, dst.base, dst.base, 8));
  1694. countreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1695. a_load_const_reg(list, OS_64, count, countreg);
  1696. { explicitely allocate F0 since it can be used safely here
  1697. (for holding date that's being copied) }
  1698. a_reg_alloc(list, NR_F0);
  1699. current_asmdata.getjumplabel(lab);
  1700. a_label(list, lab);
  1701. list.concat(taicpu.op_reg_reg_const(A_SUBIC_, countreg, countreg, 1));
  1702. list.concat(taicpu.op_reg_ref(A_LFDU, NR_F0, src));
  1703. list.concat(taicpu.op_reg_ref(A_STFDU, NR_F0, dst));
  1704. a_jmp(list, A_BC, C_NE, 0, lab);
  1705. a_reg_dealloc(list, NR_F0);
  1706. len := len mod 8;
  1707. end;
  1708. count := len div 8;
  1709. { unrolled loop }
  1710. if count > 0 then begin
  1711. a_reg_alloc(list, NR_F0);
  1712. for count2 := 1 to count do begin
  1713. a_loadfpu_ref_reg(list, OS_F64, src, NR_F0);
  1714. a_loadfpu_reg_ref(list, OS_F64, NR_F0, dst);
  1715. inc(src.offset, 8);
  1716. inc(dst.offset, 8);
  1717. end;
  1718. a_reg_dealloc(list, NR_F0);
  1719. len := len mod 8;
  1720. end;
  1721. if (len and 4) <> 0 then begin
  1722. a_reg_alloc(list, NR_R0);
  1723. a_load_ref_reg(list, OS_32, OS_32, src, NR_R0);
  1724. a_load_reg_ref(list, OS_32, OS_32, NR_R0, dst);
  1725. inc(src.offset, 4);
  1726. inc(dst.offset, 4);
  1727. a_reg_dealloc(list, NR_R0);
  1728. end;
  1729. { copy the leftovers }
  1730. if (len and 2) <> 0 then begin
  1731. a_reg_alloc(list, NR_R0);
  1732. a_load_ref_reg(list, OS_16, OS_16, src, NR_R0);
  1733. a_load_reg_ref(list, OS_16, OS_16, NR_R0, dst);
  1734. inc(src.offset, 2);
  1735. inc(dst.offset, 2);
  1736. a_reg_dealloc(list, NR_R0);
  1737. end;
  1738. if (len and 1) <> 0 then begin
  1739. a_reg_alloc(list, NR_R0);
  1740. a_load_ref_reg(list, OS_8, OS_8, src, NR_R0);
  1741. a_load_reg_ref(list, OS_8, OS_8, NR_R0, dst);
  1742. a_reg_dealloc(list, NR_R0);
  1743. end;
  1744. end;
  1745. procedure tcgppc.g_overflowcheck(list: TAsmList; const l: tlocation; def:
  1746. tdef);
  1747. var
  1748. hl: tasmlabel;
  1749. flags : TResFlags;
  1750. begin
  1751. if not (cs_check_overflow in aktlocalswitches) then
  1752. exit;
  1753. current_asmdata.getjumplabel(hl);
  1754. if not ((def.deftype = pointerdef) or
  1755. ((def.deftype = orddef) and
  1756. (torddef(def).typ in [u64bit, u16bit, u32bit, u8bit, uchar,
  1757. bool8bit, bool16bit, bool32bit]))) then
  1758. begin
  1759. { ... instructions setting overflow flag ...
  1760. mfxerf R0
  1761. mtcrf 128, R0
  1762. ble cr0, label }
  1763. list.concat(taicpu.op_reg(A_MFXER, NR_R0));
  1764. list.concat(taicpu.op_const_reg(A_MTCRF, 128, NR_R0));
  1765. flags.cr := RS_CR0;
  1766. flags.flag := F_LE;
  1767. a_jmp_flags(list, flags, hl);
  1768. end else
  1769. a_jmp_cond(list, OC_AE, hl);
  1770. a_call_name(list, 'FPC_OVERFLOW');
  1771. a_label(list, hl);
  1772. end;
  1773. procedure tcgppc.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const
  1774. labelname: string; ioffset: longint);
  1775. procedure loadvmttor11;
  1776. var
  1777. href: treference;
  1778. begin
  1779. reference_reset_base(href, NR_R3, 0);
  1780. cg.a_load_ref_reg(list, OS_ADDR, OS_ADDR, href, NR_R11);
  1781. end;
  1782. procedure op_onr11methodaddr;
  1783. var
  1784. href: treference;
  1785. begin
  1786. if (procdef.extnumber = $FFFF) then
  1787. Internalerror(200006139);
  1788. { call/jmp vmtoffs(%eax) ; method offs }
  1789. reference_reset_base(href, NR_R11,
  1790. procdef._class.vmtmethodoffset(procdef.extnumber));
  1791. if not (hasLargeOffset(href)) then begin
  1792. list.concat(taicpu.op_reg_reg_const(A_ADDIS, NR_R11, NR_R11,
  1793. smallint((href.offset shr 16) + ord(smallint(href.offset and $FFFF) <
  1794. 0))));
  1795. href.offset := smallint(href.offset and $FFFF);
  1796. end else
  1797. { add support for offsets > 16 bit }
  1798. internalerror(200510201);
  1799. list.concat(taicpu.op_reg_ref(A_LD, NR_R11, href));
  1800. { the loaded reference is a function descriptor reference, so deref again
  1801. (at ofs 0 there's the real pointer) }
  1802. {$warning ts:TODO: update GOT reference}
  1803. reference_reset_base(href, NR_R11, 0);
  1804. list.concat(taicpu.op_reg_ref(A_LD, NR_R11, href));
  1805. list.concat(taicpu.op_reg(A_MTCTR, NR_R11));
  1806. list.concat(taicpu.op_none(A_BCTR));
  1807. { NOP needed for the linker...? }
  1808. list.concat(taicpu.op_none(A_NOP));
  1809. end;
  1810. var
  1811. make_global: boolean;
  1812. begin
  1813. if (not (procdef.proctypeoption in [potype_function, potype_procedure])) then
  1814. Internalerror(200006137);
  1815. if not assigned(procdef._class) or
  1816. (procdef.procoptions * [po_classmethod, po_staticmethod,
  1817. po_methodpointer, po_interrupt, po_iocheck] <> []) then
  1818. Internalerror(200006138);
  1819. if procdef.owner.symtabletype <> objectsymtable then
  1820. Internalerror(200109191);
  1821. make_global := false;
  1822. if (not current_module.is_unit) or
  1823. (cs_create_smart in aktmoduleswitches) or
  1824. (procdef.owner.defowner.owner.symtabletype = globalsymtable) then
  1825. make_global := true;
  1826. if make_global then
  1827. List.concat(Tai_symbol.Createname_global(labelname, AT_FUNCTION, 0))
  1828. else
  1829. List.concat(Tai_symbol.Createname(labelname, AT_FUNCTION, 0));
  1830. { set param1 interface to self }
  1831. g_adjust_self_value(list, procdef, ioffset);
  1832. if po_virtualmethod in procdef.procoptions then begin
  1833. loadvmttor11;
  1834. op_onr11methodaddr;
  1835. end else
  1836. {$note ts:todo add GOT change?? - think not needed :) }
  1837. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol('.' + procdef.mangledname)));
  1838. List.concat(Tai_symbol_end.Createname(labelname));
  1839. end;
  1840. {***************** This is private property, keep out! :) *****************}
  1841. function tcgppc.issimpleref(const ref: treference): boolean;
  1842. begin
  1843. if (ref.base = NR_NO) and
  1844. (ref.index <> NR_NO) then
  1845. internalerror(200208101);
  1846. result :=
  1847. not (assigned(ref.symbol)) and
  1848. (((ref.index = NR_NO) and
  1849. (ref.offset >= low(smallint)) and
  1850. (ref.offset <= high(smallint))) or
  1851. ((ref.index <> NR_NO) and
  1852. (ref.offset = 0)));
  1853. end;
  1854. function tcgppc.load_got_symbol(list: TAsmList; symbol : string) : tregister;
  1855. var
  1856. l: tasmsymbol;
  1857. ref: treference;
  1858. symname : string;
  1859. begin
  1860. maybe_new_object_file(current_asmdata.asmlists[al_picdata]);
  1861. symname := '_$' + current_asmdata.name + '$got$' + symbol;
  1862. l:=current_asmdata.getasmsymbol(symname);
  1863. if not(assigned(l)) then begin
  1864. l:=current_asmdata.DefineAsmSymbol(symname, AB_COMMON, AT_DATA);
  1865. current_asmdata.asmlists[al_picdata].concat(tai_section.create(sec_toc, '.toc', 8));
  1866. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create_global(l,0));
  1867. current_asmdata.asmlists[al_picdata].concat(tai_directive.create(asd_toc_entry, symbol + '[TC], ' + symbol));
  1868. end;
  1869. reference_reset_symbol(ref,l,0);
  1870. ref.base := NR_R2;
  1871. ref.refaddr := addr_pic;
  1872. result := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1873. {$IFDEF EXTDEBUG}
  1874. list.concat(tai_comment.create(strpnew('loading got reference for ' + symbol)));
  1875. {$ENDIF EXTDEBUG}
  1876. // cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,result);
  1877. list.concat(taicpu.op_reg_ref(A_LD, result, ref));
  1878. end;
  1879. function tcgppc.fixref(list: TAsmList; var ref: treference; const size : TCgsize): boolean;
  1880. // symbol names must not be larger than this to be able to make a GOT reference out of them,
  1881. // otherwise they get truncated by the compiler resulting in failing of the assembling stage
  1882. const
  1883. MAX_GOT_SYMBOL_NAME_LENGTH_HACK = 120;
  1884. var
  1885. tmpreg: tregister;
  1886. name : string;
  1887. begin
  1888. result := false;
  1889. { Avoids recursion. }
  1890. if (ref.refaddr = addr_pic) then exit;
  1891. {$IFDEF EXTDEBUG}
  1892. list.concat(tai_comment.create(strpnew('fixref0 ' + ref2string(ref))));
  1893. {$ENDIF EXTDEBUG}
  1894. { if we have to create PIC, add the symbol to the TOC/GOT }
  1895. {$WARNING Hack for avoiding too long manglednames enabled!!}
  1896. if (cs_create_pic in aktmoduleswitches) and (assigned(ref.symbol) and
  1897. (length(ref.symbol.name) < MAX_GOT_SYMBOL_NAME_LENGTH_HACK)) then begin
  1898. tmpreg := load_got_symbol(list, ref.symbol.name);
  1899. if (ref.base = NR_NO) then
  1900. ref.base := tmpreg
  1901. else if (ref.index = NR_NO) then
  1902. ref.index := tmpreg
  1903. else begin
  1904. a_op_reg_reg_reg(list, OP_ADD, OS_ADDR, ref.base, tmpreg, tmpreg);
  1905. ref.base := tmpreg;
  1906. end;
  1907. ref.symbol := nil;
  1908. {$IFDEF EXTDEBUG}
  1909. list.concat(tai_comment.create(strpnew('fixref-pic ' + ref2string(ref))));
  1910. {$ENDIF EXTDEBUG}
  1911. end;
  1912. if (ref.base = NR_NO) then begin
  1913. ref.base := ref.index;
  1914. ref.index := NR_NO;
  1915. end;
  1916. if (ref.base <> NR_NO) and (ref.index <> NR_NO) and
  1917. ((ref.offset <> 0) or assigned(ref.symbol)) then begin
  1918. result := true;
  1919. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1920. a_op_reg_reg_reg(list, OP_ADD, size, ref.base, ref.index, tmpreg);
  1921. ref.base := tmpreg;
  1922. ref.index := NR_NO;
  1923. end;
  1924. if (ref.index <> NR_NO) and (assigned(ref.symbol) or (ref.offset <> 0)) then
  1925. internalerror(2006010506);
  1926. {$IFDEF EXTDEBUG}
  1927. list.concat(tai_comment.create(strpnew('fixref1 ' + ref2string(ref))));
  1928. {$ENDIF EXTDEBUG}
  1929. end;
  1930. procedure tcgppc.a_load_store(list: TAsmList; op: tasmop; reg: tregister;
  1931. ref: treference);
  1932. var
  1933. tmpreg, tmpreg2: tregister;
  1934. tmpref: treference;
  1935. largeOffset: Boolean;
  1936. begin
  1937. { at this point there must not be a combination of values in the ref treference
  1938. which is not possible to directly map to instructions of the PowerPC architecture }
  1939. if (ref.index <> NR_NO) and ((ref.offset <> 0) or (assigned(ref.symbol))) then
  1940. internalerror(200310131);
  1941. { if this is a PIC'ed address, handle it and exit }
  1942. if (ref.refaddr = addr_pic) then begin
  1943. if (ref.offset <> 0) then
  1944. internalerror(2006010501);
  1945. if (ref.index <> NR_NO) then
  1946. internalerror(2006010502);
  1947. if (not assigned(ref.symbol)) then
  1948. internalerror(200601050);
  1949. list.concat(taicpu.op_reg_ref(op, reg, ref));
  1950. exit;
  1951. end;
  1952. { for some instructions we need to check that the offset is divisible by at
  1953. least four. If not, add the bytes which are "off" to the base register and
  1954. adjust the offset accordingly }
  1955. case op of
  1956. A_LD, A_LDU, A_STD, A_STDU, A_LWA :
  1957. if ((ref.offset mod 4) <> 0) then begin
  1958. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1959. if (ref.base <> NR_NO) then begin
  1960. a_op_const_reg_reg(list, OP_ADD, OS_ADDR, ref.offset mod 4, ref.base, tmpreg);
  1961. ref.base := tmpreg;
  1962. end else begin
  1963. list.concat(taicpu.op_reg_const(A_LI, tmpreg, ref.offset mod 4));
  1964. ref.base := tmpreg;
  1965. end;
  1966. ref.offset := (ref.offset div 4) * 4;
  1967. end;
  1968. end;
  1969. {$IFDEF EXTDEBUG}
  1970. list.concat(tai_comment.create(strpnew('a_load_store1 ' + BoolToStr(ref.refaddr = addr_pic))));
  1971. {$ENDIF EXTDEBUG}
  1972. { if we have to load/store from a symbol or large addresses, use a temporary register
  1973. containing the address }
  1974. if (assigned(ref.symbol) or (hasLargeOffset(ref))) then begin
  1975. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1976. if (hasLargeOffset(ref) and (ref.base = NR_NO)) then begin
  1977. ref.base := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1978. a_load_const_reg(list, OS_ADDR, ref.offset, ref.base);
  1979. ref.offset := 0;
  1980. end;
  1981. reference_reset(tmpref);
  1982. tmpref.symbol := ref.symbol;
  1983. tmpref.relsymbol := ref.relsymbol;
  1984. tmpref.offset := ref.offset;
  1985. if (ref.base <> NR_NO) then begin
  1986. { As long as the TOC isn't working we try to achieve highest speed (in this
  1987. case by allowing instructions execute in parallel) as possible at the cost
  1988. of using another temporary register. So the code template when there is
  1989. a base register and an offset is the following:
  1990. lis rT1, SYM+offs@highest
  1991. ori rT1, rT1, SYM+offs@higher
  1992. lis rT2, SYM+offs@hi
  1993. ori rT2, SYM+offs@lo
  1994. rldimi rT2, rT1, 32
  1995. <op>X reg, base, rT2
  1996. }
  1997. tmpreg2 := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1998. if (assigned(tmpref.symbol)) then begin
  1999. tmpref.refaddr := addr_highest;
  2000. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  2001. tmpref.refaddr := addr_higher;
  2002. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  2003. tmpref.refaddr := addr_high;
  2004. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg2, tmpref));
  2005. tmpref.refaddr := addr_low;
  2006. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg2, tmpreg2, tmpref));
  2007. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, tmpreg2, tmpreg, 32, 0));
  2008. end else
  2009. a_load_const_reg(list, OS_ADDR, tmpref.offset, tmpreg2);
  2010. reference_reset(tmpref);
  2011. tmpref.base := ref.base;
  2012. tmpref.index := tmpreg2;
  2013. case op of
  2014. { the code generator doesn't generate update instructions anyway, so
  2015. error out on those instructions }
  2016. A_LBZ : op := A_LBZX;
  2017. A_LHZ : op := A_LHZX;
  2018. A_LWZ : op := A_LWZX;
  2019. A_LD : op := A_LDX;
  2020. A_LHA : op := A_LHAX;
  2021. A_LWA : op := A_LWAX;
  2022. A_LFS : op := A_LFSX;
  2023. A_LFD : op := A_LFDX;
  2024. A_STB : op := A_STBX;
  2025. A_STH : op := A_STHX;
  2026. A_STW : op := A_STWX;
  2027. A_STD : op := A_STDX;
  2028. A_STFS : op := A_STFSX;
  2029. A_STFD : op := A_STFDX;
  2030. else
  2031. { unknown load/store opcode }
  2032. internalerror(2005101302);
  2033. end;
  2034. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  2035. end else begin
  2036. { when accessing value from a reference without a base register, use the
  2037. following code template:
  2038. lis rT,SYM+offs@highesta
  2039. ori rT,SYM+offs@highera
  2040. sldi rT,rT,32
  2041. oris rT,rT,SYM+offs@ha
  2042. ld rD,SYM+offs@l(rT)
  2043. }
  2044. tmpref.refaddr := addr_highesta;
  2045. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  2046. tmpref.refaddr := addr_highera;
  2047. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  2048. list.concat(taicpu.op_reg_reg_const(A_SLDI, tmpreg, tmpreg, 32));
  2049. tmpref.refaddr := addr_higha;
  2050. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tmpreg, tmpreg, tmpref));
  2051. tmpref.base := tmpreg;
  2052. tmpref.refaddr := addr_low;
  2053. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  2054. end;
  2055. end else begin
  2056. list.concat(taicpu.op_reg_ref(op, reg, ref));
  2057. end;
  2058. end;
  2059. procedure tcgppc.a_jmp(list: TAsmList; op: tasmop; c: tasmcondflag;
  2060. crval: longint; l: tasmlabel);
  2061. var
  2062. p: taicpu;
  2063. begin
  2064. p := taicpu.op_sym(op, current_asmdata.RefAsmSymbol(l.name));
  2065. if op <> A_B then
  2066. create_cond_norm(c, crval, p.condition);
  2067. p.is_jmp := true;
  2068. list.concat(p)
  2069. end;
  2070. function tcgppc.hasLargeOffset(const ref : TReference) : Boolean; {$ifdef ver2_0}inline;{$endif}
  2071. begin
  2072. { this rather strange calculation is required because offsets of TReferences are unsigned }
  2073. result := aword(ref.offset-low(smallint)) > high(smallint)-low(smallint);
  2074. end;
  2075. procedure tcgppc.loadConstantPIC(list : TAsmList; size : TCGSize; a : aint; reg : TRegister);
  2076. var
  2077. l: tasmsymbol;
  2078. ref: treference;
  2079. symname : string;
  2080. begin
  2081. maybe_new_object_file(current_asmdata.asmlists[al_picdata]);
  2082. symname := '_$' + current_asmdata.name + '$toc$' + hexstr(a, sizeof(a)*2);
  2083. l:=current_asmdata.getasmsymbol(symname);
  2084. if not(assigned(l)) then begin
  2085. l:=current_asmdata.DefineAsmSymbol(symname,AB_GLOBAL, AT_DATA);
  2086. current_asmdata.asmlists[al_picdata].concat(tai_section.create(sec_toc, '.toc', 8));
  2087. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create_global(l,0));
  2088. current_asmdata.asmlists[al_picdata].concat(tai_directive.create(asd_toc_entry, symname + '[TC], ' + inttostr(a)));
  2089. end;
  2090. reference_reset_symbol(ref,l,0);
  2091. ref.base := NR_R2;
  2092. ref.refaddr := addr_pic;
  2093. {$IFDEF EXTDEBUG}
  2094. list.concat(tai_comment.create(strpnew('loading value from TOC reference for ' + symname)));
  2095. {$ENDIF EXTDEBUG}
  2096. cg.a_load_ref_reg(list, OS_INT, OS_INT, ref, reg);
  2097. end;
  2098. begin
  2099. cg := tcgppc.create;
  2100. end.