cgcpu.pas 83 KB

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