cgcpu.pas 71 KB

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