cgcpu.pas 83 KB

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