cgcpu.pas 85 KB

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