cgcpu.pas 85 KB

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