cgcpu.pas 82 KB

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