cgcpu.pas 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  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. procedure a_call_name(list: TAsmList; const s: string; weak: boolean); override;
  31. procedure a_call_reg(list: TAsmList; reg: tregister); override;
  32. procedure a_op_const_reg(list: TAsmList; Op: TOpCG; size: TCGSize; a:
  33. aint; reg: TRegister); override;
  34. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src,
  35. dst: TRegister); override;
  36. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  37. size: tcgsize; a: aint; src, dst: tregister); override;
  38. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  39. size: tcgsize; src1, src2, dst: tregister); override;
  40. { move instructions }
  41. procedure a_load_const_reg(list: TAsmList; size: tcgsize; a: aint; reg:
  42. tregister); override;
  43. { loads the memory pointed to by ref into register reg }
  44. procedure a_load_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const
  45. Ref: treference; reg: tregister); override;
  46. procedure a_load_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1,
  47. reg2: tregister); override;
  48. { comparison operations }
  49. procedure a_cmp_const_reg_label(list: TAsmList; size: tcgsize; cmp_op:
  50. topcmp; a: aint; reg: tregister;
  51. l: tasmlabel); override;
  52. procedure a_cmp_reg_reg_label(list: TAsmList; size: tcgsize; cmp_op:
  53. topcmp; reg1, reg2: tregister; l: tasmlabel); override;
  54. procedure a_jmp_name(list: TAsmList; const s: string); override;
  55. procedure a_jmp_always(list: TAsmList; l: tasmlabel); override;
  56. { need to override this for ppc64 to avoid calling CG methods which allocate
  57. registers during creation of the interface wrappers to subtract ioffset from
  58. the self pointer. But register allocation does not take place for them (which
  59. would probably be the generic fix) so we need to have a specialized method
  60. that uses the R11 scratch register in these cases.
  61. At the same time this allows > 32 bit offsets as well.
  62. }
  63. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);override;
  64. procedure g_profilecode(list: TAsmList); override;
  65. procedure g_proc_entry(list: TAsmList; localsize: longint; nostackframe:
  66. boolean); override;
  67. procedure g_proc_exit(list: TAsmList; parasize: longint; nostackframe:
  68. boolean); override;
  69. procedure g_save_registers(list: TAsmList); override;
  70. procedure g_restore_registers(list: TAsmList); override;
  71. procedure a_loadaddr_ref_reg(list: TAsmList; const ref: treference; r:
  72. tregister); override;
  73. procedure g_concatcopy(list: TAsmList; const source, dest: treference;
  74. len: aint); override;
  75. private
  76. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  77. { returns whether a reference can be used immediately in a powerpc }
  78. { instruction }
  79. function issimpleref(const ref: treference): boolean;
  80. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  81. procedure a_load_store(list: TAsmList; op: tasmop; reg: tregister;
  82. ref: treference); override;
  83. { returns the lowest numbered FP register in use, and the number of used FP registers
  84. for the current procedure }
  85. procedure calcFirstUsedFPR(out firstfpr : TSuperRegister; out fprcount : aint);
  86. { returns the lowest numbered GP register in use, and the number of used GP registers
  87. for the current procedure }
  88. procedure calcFirstUsedGPR(out firstgpr : TSuperRegister; out gprcount : aint);
  89. { generates code to call a method with the given string name. The boolean options
  90. control code generation. If prependDot is true, a single dot character is prepended to
  91. the string, if addNOP is true a single NOP instruction is added after the call, and
  92. if includeCall is true, the method is marked as having a call, not if false. This
  93. option is particularly useful to prevent generation of a larger stack frame for the
  94. register save and restore helper functions. }
  95. procedure a_call_name_direct(list: TAsmList; opc: tasmop; s: string; weak: boolean; prependDot : boolean;
  96. addNOP : boolean; includeCall : boolean = true);
  97. procedure a_jmp_name_direct(list : TAsmList; opc: tasmop; s : string; prependDot : boolean);
  98. { emits code to store the given value a into the TOC (if not already in there), and load it from there
  99. as well }
  100. procedure loadConstantPIC(list : TAsmList; size : TCGSize; a : aint; reg : TRegister);
  101. procedure profilecode_savepara(para : tparavarsym; list : TAsmList);
  102. procedure profilecode_restorepara(para : tparavarsym; list : TAsmList);
  103. end;
  104. procedure create_codegen;
  105. const
  106. TShiftOpCG2AsmOpConst : array[boolean, OP_SAR..OP_SHR] of TAsmOp = (
  107. (A_SRAWI, A_SLWI, A_SRWI), (A_SRADI, A_SLDI, A_SRDI)
  108. );
  109. implementation
  110. uses
  111. sysutils, cclasses,
  112. globals, verbose, systems, cutils,
  113. symconst, fmodule,
  114. rgobj, tgobj, cpupi, procinfo, paramgr, cpupara;
  115. function is_signed_cgsize(const size : TCgSize) : Boolean;
  116. begin
  117. case size of
  118. OS_S8,OS_S16,OS_S32,OS_S64 : result := true;
  119. OS_8,OS_16,OS_32,OS_64 : result := false;
  120. else
  121. internalerror(2006050701);
  122. end;
  123. end;
  124. { finds positive and negative powers of two of the given value, returning the
  125. power and whether it's a negative power or not in addition to the actual result
  126. of the function }
  127. function ispowerof2(value : aInt; out power : byte; out neg : boolean) : boolean;
  128. var
  129. i : longint;
  130. hl : aInt;
  131. begin
  132. result := false;
  133. neg := false;
  134. { also try to find negative power of two's by negating if the
  135. value is negative. low(aInt) is special because it can not be
  136. negated. Simply return the appropriate values for it }
  137. if (value < 0) then begin
  138. neg := true;
  139. if (value = low(aInt)) then begin
  140. power := sizeof(aInt)*8-1;
  141. result := true;
  142. exit;
  143. end;
  144. value := -value;
  145. end;
  146. if ((value and (value-1)) <> 0) then begin
  147. result := false;
  148. exit;
  149. end;
  150. hl := 1;
  151. for i := 0 to (sizeof(aInt)*8-1) do begin
  152. if (hl = value) then begin
  153. result := true;
  154. power := i;
  155. exit;
  156. end;
  157. hl := hl shl 1;
  158. end;
  159. end;
  160. { returns the number of instruction required to load the given integer into a register.
  161. This is basically a stripped down version of a_load_const_reg, increasing a counter
  162. instead of emitting instructions. }
  163. function getInstructionLength(a : aint) : longint;
  164. function get32bitlength(a : longint; var length : longint) : boolean; inline;
  165. var
  166. is_half_signed : byte;
  167. begin
  168. { if the lower 16 bits are zero, do a single LIS }
  169. if (smallint(a) = 0) and ((a shr 16) <> 0) then begin
  170. inc(length);
  171. get32bitlength := longint(a) < 0;
  172. end else begin
  173. is_half_signed := ord(smallint(lo(a)) < 0);
  174. inc(length);
  175. if smallint(hi(a) + is_half_signed) <> 0 then
  176. inc(length);
  177. get32bitlength := (smallint(a) < 0) or (a < 0);
  178. end;
  179. end;
  180. var
  181. extendssign : boolean;
  182. begin
  183. result := 0;
  184. if (lo(a) = 0) and (hi(a) <> 0) then begin
  185. get32bitlength(hi(a), result);
  186. inc(result);
  187. end else begin
  188. extendssign := get32bitlength(lo(a), result);
  189. if (extendssign) and (hi(a) = 0) then
  190. inc(result)
  191. else if (not
  192. ((extendssign and (longint(hi(a)) = -1)) or
  193. ((not extendssign) and (hi(a)=0)))
  194. ) then begin
  195. get32bitlength(hi(a), result);
  196. inc(result);
  197. end;
  198. end;
  199. end;
  200. procedure tcgppc.init_register_allocators;
  201. begin
  202. inherited init_register_allocators;
  203. if (target_info.system <> system_powerpc64_darwin) then
  204. // r13 is tls, do not use, r2 is not available
  205. rg[R_INTREGISTER] := trgintcpu.create(R_INTREGISTER, R_SUBWHOLE,
  206. [{$ifdef user0} RS_R0, {$endif} RS_R3, RS_R4, RS_R5, RS_R6, RS_R7, RS_R8,
  207. RS_R9, RS_R10, RS_R11, RS_R12, RS_R31, RS_R30, RS_R29,
  208. RS_R28, RS_R27, RS_R26, RS_R25, RS_R24, RS_R23, RS_R22,
  209. RS_R21, RS_R20, RS_R19, RS_R18, RS_R17, RS_R16, RS_R15,
  210. RS_R14], first_int_imreg, [])
  211. else
  212. { special for darwin/ppc64: r2 available volatile, r13 = tls }
  213. rg[R_INTREGISTER] := trgintcpu.create(R_INTREGISTER, R_SUBWHOLE,
  214. [{$ifdef user0} RS_R0, {$endif} RS_R2, RS_R3, RS_R4, RS_R5, RS_R6, RS_R7, RS_R8,
  215. RS_R9, RS_R10, RS_R11, RS_R12, RS_R31, RS_R30, RS_R29,
  216. RS_R28, RS_R27, RS_R26, RS_R25, RS_R24, RS_R23, RS_R22,
  217. RS_R21, RS_R20, RS_R19, RS_R18, RS_R17, RS_R16, RS_R15,
  218. RS_R14], first_int_imreg, []);
  219. rg[R_FPUREGISTER] := trgcpu.create(R_FPUREGISTER, R_SUBNONE,
  220. [RS_F0, RS_F1, RS_F2, RS_F3, RS_F4, RS_F5, RS_F6, RS_F7, RS_F8, RS_F9,
  221. RS_F10, RS_F11, RS_F12, RS_F13, RS_F31, RS_F30, RS_F29, RS_F28, RS_F27,
  222. RS_F26, RS_F25, RS_F24, RS_F23, RS_F22, RS_F21, RS_F20, RS_F19, RS_F18,
  223. RS_F17, RS_F16, RS_F15, RS_F14], first_fpu_imreg, []);
  224. { TODO: FIX ME}
  225. rg[R_MMREGISTER] := trgcpu.create(R_MMREGISTER, R_SUBNONE,
  226. [RS_M0, RS_M1, RS_M2], first_mm_imreg, []);
  227. end;
  228. procedure tcgppc.done_register_allocators;
  229. begin
  230. rg[R_INTREGISTER].free;
  231. rg[R_FPUREGISTER].free;
  232. rg[R_MMREGISTER].free;
  233. inherited done_register_allocators;
  234. end;
  235. { calling a procedure by name }
  236. procedure tcgppc.a_call_name(list: TAsmList; const s: string; weak: boolean);
  237. begin
  238. if (target_info.system <> system_powerpc64_darwin) then
  239. a_call_name_direct(list, A_BL, s, weak, target_info.system=system_powerpc64_aix, true)
  240. else
  241. begin
  242. list.concat(taicpu.op_sym(A_BL,get_darwin_call_stub(s,weak)));
  243. include(current_procinfo.flags,pi_do_call);
  244. end;
  245. end;
  246. procedure tcgppc.a_call_name_direct(list: TAsmList; opc: tasmop; s: string; weak: boolean; prependDot : boolean; addNOP : boolean; includeCall : boolean);
  247. begin
  248. if (prependDot) then
  249. s := '.' + s;
  250. if not(weak) then
  251. list.concat(taicpu.op_sym(opc, current_asmdata.RefAsmSymbol(s)))
  252. else
  253. list.concat(taicpu.op_sym(opc, current_asmdata.WeakRefAsmSymbol(s)));
  254. if (addNOP) then
  255. list.concat(taicpu.op_none(A_NOP));
  256. if (includeCall) and
  257. assigned(current_procinfo) then
  258. include(current_procinfo.flags, pi_do_call);
  259. end;
  260. { calling a procedure by address }
  261. procedure tcgppc.a_call_reg(list: TAsmList; reg: tregister);
  262. var
  263. tmpref: treference;
  264. tempreg : TRegister;
  265. begin
  266. if (target_info.abi<>abi_powerpc_sysv) then
  267. inherited a_call_reg(list,reg)
  268. else if (not (cs_opt_size in current_settings.optimizerswitches)) then begin
  269. tempreg := getintregister(list, OS_INT);
  270. { load actual function entry (reg contains the reference to the function descriptor)
  271. into tempreg }
  272. reference_reset_base(tmpref, reg, 0, sizeof(pint));
  273. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, tempreg);
  274. { save TOC pointer in stackframe }
  275. reference_reset_base(tmpref, NR_STACK_POINTER_REG, LA_RTOC_SYSV, 8);
  276. a_load_reg_ref(list, OS_ADDR, OS_ADDR, NR_RTOC, tmpref);
  277. { move actual function pointer to CTR register }
  278. list.concat(taicpu.op_reg(A_MTCTR, tempreg));
  279. { load new TOC pointer from function descriptor into RTOC register }
  280. reference_reset_base(tmpref, reg, tcgsize2size[OS_ADDR], 8);
  281. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, NR_RTOC);
  282. { load new environment pointer from function descriptor into R11 register }
  283. reference_reset_base(tmpref, reg, 2*tcgsize2size[OS_ADDR], 8);
  284. a_reg_alloc(list, NR_R11);
  285. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, NR_R11);
  286. { call function }
  287. list.concat(taicpu.op_none(A_BCTRL));
  288. a_reg_dealloc(list, NR_R11);
  289. end else begin
  290. { call ptrgl helper routine which expects the pointer to the function descriptor
  291. in R11 }
  292. a_reg_alloc(list, NR_R11);
  293. a_load_reg_reg(list, OS_ADDR, OS_ADDR, reg, NR_R11);
  294. a_call_name_direct(list, A_BL, '.ptrgl', false, false, false);
  295. a_reg_dealloc(list, NR_R11);
  296. end;
  297. { we need to load the old RTOC from stackframe because we changed it}
  298. reference_reset_base(tmpref, NR_STACK_POINTER_REG, LA_RTOC_SYSV, 8);
  299. a_load_ref_reg(list, OS_ADDR, OS_ADDR, tmpref, NR_RTOC);
  300. include(current_procinfo.flags, pi_do_call);
  301. end;
  302. {********************** load instructions ********************}
  303. procedure tcgppc.a_load_const_reg(list: TAsmList; size: TCGSize; a: aint;
  304. reg: TRegister);
  305. { loads a 32 bit constant into the given register, using an optimal instruction sequence.
  306. This is either LIS, LI or LI+ADDIS.
  307. Returns true if during these operations the upper 32 bits were filled with 1 bits (e.g.
  308. sign extension was performed) }
  309. function load32bitconstant(list : TAsmList; size : TCGSize; a : longint;
  310. reg : TRegister) : boolean;
  311. var
  312. is_half_signed : byte;
  313. begin
  314. { if the lower 16 bits are zero, do a single LIS }
  315. if (smallint(a) = 0) and ((a shr 16) <> 0) then begin
  316. list.concat(taicpu.op_reg_const(A_LIS, reg, smallint(hi(a))));
  317. load32bitconstant := longint(a) < 0;
  318. end else begin
  319. is_half_signed := ord(smallint(lo(a)) < 0);
  320. list.concat(taicpu.op_reg_const(A_LI, reg, smallint(a and $ffff)));
  321. if smallint(hi(a) + is_half_signed) <> 0 then begin
  322. list.concat(taicpu.op_reg_reg_const(A_ADDIS, reg, reg, smallint(hi(a) + is_half_signed)));
  323. end;
  324. load32bitconstant := (smallint(a) < 0) or (a < 0);
  325. end;
  326. end;
  327. { loads a 32 bit constant into R0, using an optimal instruction sequence.
  328. This is either LIS, LI or LI+ORIS.
  329. Returns true if during these operations the upper 32 bits were filled with 1 bits (e.g.
  330. sign extension was performed) }
  331. function load32bitconstantR0(list : TAsmList; size : TCGSize; a : longint) : boolean;
  332. begin
  333. { if it's a value we can load with a single LI, do it }
  334. if (a >= low(smallint)) and (a <= high(smallint)) then begin
  335. list.concat(taicpu.op_reg_const(A_LI, NR_R0, smallint(a)));
  336. end else begin
  337. { if the lower 16 bits are zero, do a single LIS }
  338. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, smallint(a shr 16)));
  339. if (smallint(a) <> 0) then begin
  340. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(a)));
  341. end;
  342. end;
  343. load32bitconstantR0 := a < 0;
  344. end;
  345. { emits the code to load a constant by emitting various instructions into the output
  346. code}
  347. procedure loadConstantNormal(list: TAsmList; size : TCgSize; a: aint; reg: TRegister);
  348. var
  349. extendssign : boolean;
  350. instr : taicpu;
  351. begin
  352. if (lo(a) = 0) and (hi(a) <> 0) then begin
  353. { load only upper 32 bits, and shift }
  354. load32bitconstant(list, size, longint(hi(a)), reg);
  355. list.concat(taicpu.op_reg_reg_const(A_SLDI, reg, reg, 32));
  356. end else begin
  357. { load lower 32 bits }
  358. extendssign := load32bitconstant(list, size, longint(lo(a)), reg);
  359. if (extendssign) and (hi(a) = 0) then
  360. { if upper 32 bits are zero, but loading the lower 32 bit resulted in automatic
  361. sign extension, clear those bits }
  362. list.concat(taicpu.op_reg_reg_const_const(A_RLDICL, reg, reg, 0, 32))
  363. else if (not
  364. ((extendssign and (longint(hi(a)) = -1)) or
  365. ((not extendssign) and (hi(a)=0)))
  366. ) then begin
  367. { only load the upper 32 bits, if the automatic sign extension is not okay,
  368. that is, _not_ if
  369. - loading the lower 32 bits resulted in -1 in the upper 32 bits, and the upper
  370. 32 bits should contain -1
  371. - loading the lower 32 bits resulted in 0 in the upper 32 bits, and the upper
  372. 32 bits should contain 0 }
  373. a_reg_alloc(list, NR_R0);
  374. load32bitconstantR0(list, size, longint(hi(a)));
  375. { combine both registers }
  376. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, reg, NR_R0, 32, 0));
  377. a_reg_dealloc(list, NR_R0);
  378. end;
  379. end;
  380. end;
  381. {$IFDEF EXTDEBUG}
  382. var
  383. astring : string;
  384. {$ENDIF EXTDEBUG}
  385. begin
  386. {$IFDEF EXTDEBUG}
  387. astring := 'a_load_const_reg ' + inttostr(hi(a)) + ' ' + inttostr(lo(a)) + ' ' + inttostr(ord(size)) + ' ' + inttostr(tcgsize2size[size]) + ' ' + hexstr(a, 16);
  388. list.concat(tai_comment.create(strpnew(astring)));
  389. {$ENDIF EXTDEBUG}
  390. if not (size in [OS_8, OS_S8, OS_16, OS_S16, OS_32, OS_S32, OS_64, OS_S64]) then
  391. internalerror(2002090902);
  392. { if PIC or basic optimizations are enabled, and the number of instructions which would be
  393. required to load the value is greater than 2, store (and later load) the value from there }
  394. // if (((cs_opt_peephole in current_settings.optimizerswitches) or (cs_create_pic in current_settings.moduleswitches)) and
  395. // (getInstructionLength(a) > 2)) then
  396. // loadConstantPIC(list, size, a, reg)
  397. // else
  398. loadConstantNormal(list, size, a, reg);
  399. end;
  400. procedure tcgppc.a_load_ref_reg(list: TAsmList; fromsize, tosize: tcgsize;
  401. const ref: treference; reg: tregister);
  402. const
  403. LoadInstr: array[OS_8..OS_S64, boolean, boolean] of TAsmOp =
  404. { indexed? updating? }
  405. (((A_LBZ, A_LBZU), (A_LBZX, A_LBZUX)),
  406. ((A_LHZ, A_LHZU), (A_LHZX, A_LHZUX)),
  407. ((A_LWZ, A_LWZU), (A_LWZX, A_LWZUX)),
  408. ((A_LD, A_LDU), (A_LDX, A_LDUX)),
  409. { 128bit stuff too }
  410. ((A_NONE, A_NONE), (A_NONE, A_NONE)),
  411. { there's no load-byte-with-sign-extend :( }
  412. ((A_LBZ, A_LBZU), (A_LBZX, A_LBZUX)),
  413. ((A_LHA, A_LHAU), (A_LHAX, A_LHAUX)),
  414. { there's no load-word-arithmetic-indexed with update, simulate it in code :( }
  415. ((A_LWA, A_NOP), (A_LWAX, A_LWAUX)),
  416. ((A_LD, A_LDU), (A_LDX, A_LDUX))
  417. );
  418. var
  419. op: tasmop;
  420. ref2: treference;
  421. tmpreg: tregister;
  422. begin
  423. if target_info.system=system_powerpc64_aix then
  424. g_load_check_simple(list,ref,65536);
  425. {$IFDEF EXTDEBUG}
  426. list.concat(tai_comment.create(strpnew('a_load_ref_reg ' + ref2string(ref))));
  427. {$ENDIF EXTDEBUG}
  428. if not (fromsize in [OS_8, OS_S8, OS_16, OS_S16, OS_32, OS_S32, OS_64, OS_S64]) then
  429. internalerror(2002090904);
  430. { the caller is expected to have adjusted the reference already
  431. in this case }
  432. if (TCGSize2Size[fromsize] >= TCGSize2Size[tosize]) then
  433. fromsize := tosize;
  434. ref2 := ref;
  435. fixref(list, ref2);
  436. op := loadinstr[fromsize, ref2.index <> NR_NO, false];
  437. { there is no LWAU instruction, simulate using ADDI and LWA }
  438. if (op = A_NOP) then begin
  439. list.concat(taicpu.op_reg_reg_const(A_ADDI, reg, reg, ref2.offset));
  440. ref2.offset := 0;
  441. op := A_LWA;
  442. end;
  443. a_load_store(list, op, reg, ref2);
  444. { sign extend shortint if necessary (because there is
  445. no load instruction to sign extend an 8 bit value automatically)
  446. and mask out extra sign bits when loading from a smaller
  447. signed to a larger unsigned type (where it matters) }
  448. if (fromsize = OS_S8) then begin
  449. a_load_reg_reg(list, OS_8, OS_S8, reg, reg);
  450. a_load_reg_reg(list, OS_S8, tosize, reg, reg);
  451. end else if (fromsize = OS_S16) and (tosize = OS_32) then
  452. a_load_reg_reg(list, fromsize, tosize, reg, reg);
  453. end;
  454. procedure tcgppc.a_load_reg_reg(list: TAsmList; fromsize, tosize: tcgsize;
  455. reg1, reg2: tregister);
  456. var
  457. instr: TAiCpu;
  458. bytesize : byte;
  459. begin
  460. {$ifdef extdebug}
  461. list.concat(tai_comment.create(strpnew('a_load_reg_reg from : ' + cgsize2string(fromsize) + ' to ' + cgsize2string(tosize))));
  462. {$endif}
  463. if (tcgsize2size[fromsize] > tcgsize2size[tosize]) or
  464. ((tcgsize2size[fromsize] = tcgsize2size[tosize]) and (fromsize <> tosize)) or
  465. { do we need to mask out the sign when loading from smaller signed to larger unsigned type? }
  466. ( is_signed_cgsize(fromsize) and (not is_signed_cgsize(tosize)) and
  467. (tcgsize2size[fromsize] < tcgsize2size[tosize]) and (tcgsize2size[tosize] <> sizeof(pint)) ) then begin
  468. case tosize of
  469. OS_S8:
  470. instr := taicpu.op_reg_reg(A_EXTSB,reg2,reg1);
  471. OS_S16:
  472. instr := taicpu.op_reg_reg(A_EXTSH,reg2,reg1);
  473. OS_S32:
  474. instr := taicpu.op_reg_reg(A_EXTSW,reg2,reg1);
  475. OS_8, OS_16, OS_32:
  476. instr := taicpu.op_reg_reg_const_const(A_RLDICL, reg2, reg1, 0, (8-tcgsize2size[tosize])*8);
  477. OS_S64, OS_64:
  478. instr := taicpu.op_reg_reg(A_MR, reg2, reg1);
  479. else
  480. internalerror(2013113007);
  481. end;
  482. end else
  483. instr := taicpu.op_reg_reg(A_MR, reg2, reg1);
  484. list.concat(instr);
  485. rg[R_INTREGISTER].add_move_instruction(instr);
  486. end;
  487. procedure tcgppc.a_op_const_reg(list: TAsmList; Op: TOpCG; size: TCGSize; a:
  488. aint; reg: TRegister);
  489. begin
  490. a_op_const_reg_reg(list, op, size, a, reg, reg);
  491. end;
  492. procedure tcgppc.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src,
  493. dst: TRegister);
  494. begin
  495. a_op_reg_reg_reg(list, op, size, src, dst, dst);
  496. end;
  497. procedure tcgppc.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  498. size: tcgsize; a: aint; src, dst: tregister);
  499. var
  500. useReg : boolean;
  501. procedure do_lo_hi(loOp, hiOp : TAsmOp);
  502. begin
  503. { Optimization for logical ops (excluding AND), trying to do this as efficiently
  504. as possible by only generating code for the affected halfwords. Note that all
  505. the instructions handled here must have "X op 0 = X" for every halfword. }
  506. usereg := false;
  507. if (aword(a) > high(dword)) then begin
  508. usereg := true;
  509. end else begin
  510. if (word(a) <> 0) then begin
  511. list.concat(taicpu.op_reg_reg_const(loOp, dst, src, word(a)));
  512. if (word(a shr 16) <> 0) then
  513. list.concat(taicpu.op_reg_reg_const(hiOp, dst, dst, word(a shr 16)));
  514. end else if (word(a shr 16) <> 0) then
  515. list.concat(taicpu.op_reg_reg_const(hiOp, dst, src, word(a shr 16)));
  516. end;
  517. end;
  518. procedure do_lo_hi_and;
  519. begin
  520. { optimization logical and with immediate: only use "andi." for 16 bit
  521. ands, otherwise use register method. Doing this for 32 bit constants
  522. would not give any advantage to the register method (via useReg := true),
  523. requiring a scratch register and three instructions. }
  524. usereg := false;
  525. if (aword(a) > high(word)) then
  526. usereg := true
  527. else
  528. list.concat(taicpu.op_reg_reg_const(A_ANDI_, dst, src, word(a)));
  529. end;
  530. procedure do_constant_div(list : TAsmList; size : TCgSize; a : aint; src, dst : TRegister;
  531. signed : boolean);
  532. const
  533. negops : array[boolean] of tasmop = (A_NEG, A_NEGO);
  534. var
  535. magic : int64;
  536. u_magic : qword;
  537. u_shift : byte;
  538. u_add : boolean;
  539. power : byte;
  540. isNegPower : boolean;
  541. divreg : tregister;
  542. begin
  543. if (a = 0) then begin
  544. internalerror(2005061701);
  545. end else if (a = 1) then begin
  546. a_load_reg_reg(list, OS_INT, OS_INT, src, dst);
  547. end else if (a = -1) and (signed) then begin
  548. { note: only in the signed case possible..., may overflow }
  549. list.concat(taicpu.op_reg_reg(negops[cs_check_overflow in current_settings.localswitches], dst, src));
  550. end else if (ispowerof2(a, power, isNegPower)) then begin
  551. if (signed) then begin
  552. { From "The PowerPC Compiler Writer's Guide", pg. 52ff }
  553. a_op_const_reg_reg(list, OP_SAR, OS_INT, power,
  554. src, dst);
  555. list.concat(taicpu.op_reg_reg(A_ADDZE, dst, dst));
  556. if (isNegPower) then
  557. list.concat(taicpu.op_reg_reg(A_NEG, dst, dst));
  558. end else begin
  559. a_op_const_reg_reg(list, OP_SHR, OS_INT, power, src, dst)
  560. end;
  561. end else begin
  562. { replace division by multiplication, both implementations }
  563. { from "The PowerPC Compiler Writer's Guide" pg. 53ff }
  564. divreg := getintregister(list, OS_INT);
  565. if (signed) then begin
  566. calc_divconst_magic_signed(sizeof(aInt)*8, a, magic, u_shift);
  567. { load magic value }
  568. a_load_const_reg(list, OS_INT, magic, divreg);
  569. { multiply }
  570. list.concat(taicpu.op_reg_reg_reg(A_MULHD, dst, src, divreg));
  571. { add/subtract numerator }
  572. if (a > 0) and (magic < 0) then begin
  573. a_op_reg_reg_reg(list, OP_ADD, OS_INT, src, dst, dst);
  574. end else if (a < 0) and (magic > 0) then begin
  575. a_op_reg_reg_reg(list, OP_SUB, OS_INT, src, dst, dst);
  576. end;
  577. { shift shift places to the right (arithmetic) }
  578. a_op_const_reg_reg(list, OP_SAR, OS_INT, u_shift, dst, dst);
  579. { extract and add sign bit }
  580. if (a >= 0) then begin
  581. a_op_const_reg_reg(list, OP_SHR, OS_INT, 63, src, divreg);
  582. end else begin
  583. a_op_const_reg_reg(list, OP_SHR, OS_INT, 63, dst, divreg);
  584. end;
  585. a_op_reg_reg_reg(list, OP_ADD, OS_INT, dst, divreg, dst);
  586. end else begin
  587. calc_divconst_magic_unsigned(sizeof(aWord)*8, a, u_magic, u_add, u_shift);
  588. { load magic in divreg }
  589. a_load_const_reg(list, OS_INT, aint(u_magic), divreg);
  590. list.concat(taicpu.op_reg_reg_reg(A_MULHDU, dst, src, divreg));
  591. if (u_add) then begin
  592. a_op_reg_reg_reg(list, OP_SUB, OS_INT, dst, src, divreg);
  593. a_op_const_reg_reg(list, OP_SHR, OS_INT, 1, divreg, divreg);
  594. a_op_reg_reg_reg(list, OP_ADD, OS_INT, divreg, dst, divreg);
  595. a_op_const_reg_reg(list, OP_SHR, OS_INT, u_shift-1, divreg, dst);
  596. end else begin
  597. a_op_const_reg_reg(list, OP_SHR, OS_INT, u_shift, dst, dst);
  598. end;
  599. end;
  600. end;
  601. end;
  602. var
  603. scratchreg: tregister;
  604. shift : byte;
  605. shiftmask : longint;
  606. isneg : boolean;
  607. begin
  608. { subtraction is the same as addition with negative constant }
  609. if op = OP_SUB then begin
  610. a_op_const_reg_reg(list, OP_ADD, size, -a, src, dst);
  611. exit;
  612. end;
  613. {$IFDEF EXTDEBUG}
  614. list.concat(tai_comment.create(strpnew('a_op_const_reg_reg ' + cgop2string(op))));
  615. {$ENDIF EXTDEBUG}
  616. { This case includes some peephole optimizations for the various operations,
  617. (e.g. AND, OR, XOR, ..) - can't this be done at some higher level,
  618. independent of architecture? }
  619. { assume that we do not need a scratch register for the operation }
  620. useReg := false;
  621. case (op) of
  622. OP_DIV, OP_IDIV:
  623. if (cs_opt_level1 in current_settings.optimizerswitches) then
  624. do_constant_div(list, size, a, src, dst, op = OP_IDIV)
  625. else
  626. usereg := true;
  627. OP_IMUL, OP_MUL:
  628. { idea: factorize constant multiplicands and use adds/shifts with few factors;
  629. however, even a 64 bit multiply is already quite fast on PPC64 }
  630. if (a = 0) then
  631. a_load_const_reg(list, size, 0, dst)
  632. else if (a = -1) then
  633. list.concat(taicpu.op_reg_reg(A_NEG, dst, dst))
  634. else if (a = 1) then
  635. a_load_reg_reg(list, OS_INT, OS_INT, src, dst)
  636. else if ispowerof2(a, shift, isneg) then begin
  637. list.concat(taicpu.op_reg_reg_const(A_SLDI, dst, src, shift));
  638. if (isneg) then
  639. list.concat(taicpu.op_reg_reg(A_NEG, dst, dst));
  640. end else if (a >= low(smallint)) and (a <= high(smallint)) then
  641. list.concat(taicpu.op_reg_reg_const(A_MULLI, dst, src,
  642. smallint(a)))
  643. else
  644. usereg := true;
  645. OP_ADD:
  646. if (a = 0) then
  647. a_load_reg_reg(list, size, size, src, dst)
  648. else if (a >= low(smallint)) and (a <= high(smallint)) then
  649. list.concat(taicpu.op_reg_reg_const(A_ADDI, dst, src, smallint(a)))
  650. else
  651. useReg := true;
  652. OP_OR:
  653. if (a = 0) then
  654. a_load_reg_reg(list, size, size, src, dst)
  655. else if (a = -1) then
  656. a_load_const_reg(list, size, -1, dst)
  657. else
  658. do_lo_hi(A_ORI, A_ORIS);
  659. OP_AND:
  660. if (a = 0) then
  661. a_load_const_reg(list, size, 0, dst)
  662. else if (a = -1) then
  663. a_load_reg_reg(list, size, size, src, dst)
  664. else
  665. do_lo_hi_and;
  666. OP_XOR:
  667. if (a = 0) then
  668. a_load_reg_reg(list, size, size, src, dst)
  669. else if (a = -1) then
  670. list.concat(taicpu.op_reg_reg(A_NOT, dst, src))
  671. else
  672. do_lo_hi(A_XORI, A_XORIS);
  673. OP_ROL:
  674. begin
  675. if (size in [OS_64, OS_S64]) then begin
  676. list.concat(taicpu.op_reg_reg_const_const(A_RLDICL, dst, src, a and 63, 0));
  677. end else if (size in [OS_32, OS_S32]) then begin
  678. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM, dst, src, a and 31, 0, 31));
  679. end else begin
  680. internalerror(2008091303);
  681. end;
  682. end;
  683. OP_ROR:
  684. begin
  685. if (size in [OS_64, OS_S64]) then begin
  686. list.concat(taicpu.op_reg_reg_const_const(A_RLDICL, dst, src, ((64 - a) and 63), 0));
  687. end else if (size in [OS_32, OS_S32]) then begin
  688. list.concat(taicpu.op_reg_reg_const_const_const(A_RLWINM, dst, src, (32 - a) and 31, 0, 31));
  689. end else begin
  690. internalerror(2008091304);
  691. end;
  692. end;
  693. OP_SHL, OP_SHR, OP_SAR:
  694. begin
  695. if (size in [OS_64, OS_S64]) then
  696. shift := 6
  697. else
  698. shift := 5;
  699. shiftmask := (1 shl shift)-1;
  700. if (a and shiftmask) <> 0 then begin
  701. list.concat(taicpu.op_reg_reg_const(
  702. TShiftOpCG2AsmOpConst[size in [OS_64, OS_S64], op], dst, src, a and shiftmask));
  703. end else
  704. a_load_reg_reg(list, size, size, src, dst);
  705. if ((a shr shift) <> 0) then
  706. internalError(68991);
  707. end
  708. else
  709. internalerror(200109091);
  710. end;
  711. { if all else failed, load the constant in a register and then
  712. perform the operation }
  713. if (useReg) then begin
  714. scratchreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  715. a_load_const_reg(list, size, a, scratchreg);
  716. a_op_reg_reg_reg(list, op, size, scratchreg, src, dst);
  717. end else
  718. maybeadjustresult(list, op, size, dst);
  719. end;
  720. procedure tcgppc.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  721. size: tcgsize; src1, src2, dst: tregister);
  722. const
  723. op_reg_reg_opcg2asmop32: array[TOpCG] of tasmop =
  724. (A_NONE, A_MR, A_ADD, A_AND, A_DIVWU, A_DIVW, A_MULLW, A_MULLW, A_NEG, A_NOT, A_OR,
  725. A_SRAW, A_SLW, A_SRW, A_SUB, A_XOR, A_NONE, A_NONE);
  726. op_reg_reg_opcg2asmop64: array[TOpCG] of tasmop =
  727. (A_NONE, A_MR, A_ADD, A_AND, A_DIVDU, A_DIVD, A_MULLD, A_MULLD, A_NEG, A_NOT, A_OR,
  728. A_SRAD, A_SLD, A_SRD, A_SUB, A_XOR, A_NONE, A_NONE);
  729. var
  730. tmpreg : TRegister;
  731. begin
  732. case op of
  733. OP_NEG, OP_NOT:
  734. begin
  735. list.concat(taicpu.op_reg_reg(op_reg_reg_opcg2asmop64[op], dst, src1));
  736. if (op = OP_NOT) and not (size in [OS_64, OS_S64]) then
  737. { zero/sign extend result again, fromsize is not important here }
  738. a_load_reg_reg(list, OS_S64, size, dst, dst)
  739. end;
  740. OP_ROL:
  741. begin
  742. if (size in [OS_64, OS_S64]) then begin
  743. list.concat(taicpu.op_reg_reg_reg_const(A_RLDCL, dst, src2, src1, 0));
  744. end else if (size in [OS_32, OS_S32]) then begin
  745. list.concat(taicpu.op_reg_reg_reg_const_const(A_RLWNM, dst, src2, src1, 0, 31));
  746. end else begin
  747. internalerror(2008091301);
  748. end;
  749. end;
  750. OP_ROR:
  751. begin
  752. tmpreg := getintregister(list, OS_INT);
  753. list.concat(taicpu.op_reg_reg(A_NEG, tmpreg, src1));
  754. if (size in [OS_64, OS_S64]) then begin
  755. list.concat(taicpu.op_reg_reg_reg_const(A_RLDCL, dst, src2, tmpreg, 0));
  756. end else if (size in [OS_32, OS_S32]) then begin
  757. list.concat(taicpu.op_reg_reg_reg_const_const(A_RLWNM, dst, src2, tmpreg, 0, 31));
  758. end else begin
  759. internalerror(2008091302);
  760. end;
  761. end;
  762. else
  763. if (size in [OS_64, OS_S64]) then begin
  764. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop64[op], dst, src2,
  765. src1));
  766. end else begin
  767. list.concat(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop32[op], dst, src2,
  768. src1));
  769. maybeadjustresult(list, op, size, dst);
  770. end;
  771. end;
  772. end;
  773. {*************** compare instructructions ****************}
  774. procedure tcgppc.a_cmp_const_reg_label(list: TAsmList; size: tcgsize;
  775. cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  776. const
  777. { unsigned useconst 32bit-op }
  778. cmpop_table : array[boolean, boolean, boolean] of TAsmOp = (
  779. ((A_CMPD, A_CMPW), (A_CMPDI, A_CMPWI)),
  780. ((A_CMPLD, A_CMPLW), (A_CMPLDI, A_CMPLWI))
  781. );
  782. var
  783. tmpreg : TRegister;
  784. signed, useconst : boolean;
  785. opsize : TCgSize;
  786. op : TAsmOp;
  787. begin
  788. {$IFDEF EXTDEBUG}
  789. 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) )));
  790. {$ENDIF EXTDEBUG}
  791. signed := cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE];
  792. { in the following case, we generate more efficient code when
  793. signed is true }
  794. if (cmp_op in [OC_EQ, OC_NE]) and
  795. (aword(a) > $FFFF) then
  796. signed := true;
  797. opsize := size;
  798. { do we need to change the operand size because ppc64 only supports 32 and
  799. 64 bit compares? }
  800. if (not (size in [OS_32, OS_S32, OS_64, OS_S64])) then begin
  801. if (signed) then
  802. opsize := OS_S32
  803. else
  804. opsize := OS_32;
  805. a_load_reg_reg(list, size, opsize, reg, reg);
  806. end;
  807. { can we use immediate compares? }
  808. useconst := (signed and ( (a >= low(smallint)) and (a <= high(smallint)))) or
  809. ((not signed) and (aword(a) <= $FFFF));
  810. op := cmpop_table[not signed, useconst, opsize in [OS_32, OS_S32]];
  811. if (useconst) then begin
  812. list.concat(taicpu.op_reg_reg_const(op, NR_CR0, reg, a));
  813. end else begin
  814. tmpreg := getintregister(list, OS_INT);
  815. a_load_const_reg(list, opsize, a, tmpreg);
  816. list.concat(taicpu.op_reg_reg_reg(op, NR_CR0, reg, tmpreg));
  817. end;
  818. a_jmp(list, A_BC, TOpCmp2AsmCond[cmp_op], 0, l);
  819. end;
  820. procedure tcgppc.a_cmp_reg_reg_label(list: TAsmList; size: tcgsize;
  821. cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  822. var
  823. op: tasmop;
  824. begin
  825. {$IFDEF extdebug}
  826. list.concat(tai_comment.create(strpnew('a_cmp_reg_reg_label, size ' + cgsize2string(size) + ' op ' + inttostr(ord(cmp_op)))));
  827. {$ENDIF extdebug}
  828. {$note Commented out below check because of compiler weirdness}
  829. {
  830. if (not (size in [OS_32, OS_S32, OS_64, OS_S64])) then
  831. internalerror(200606041);
  832. }
  833. if cmp_op in [OC_GT, OC_LT, OC_GTE, OC_LTE] then
  834. if (size in [OS_64, OS_S64]) then
  835. op := A_CMPD
  836. else
  837. op := A_CMPW
  838. else
  839. if (size in [OS_64, OS_S64]) then
  840. op := A_CMPLD
  841. else
  842. op := A_CMPLW;
  843. list.concat(taicpu.op_reg_reg_reg(op, NR_CR0, reg2, reg1));
  844. a_jmp(list, A_BC, TOpCmp2AsmCond[cmp_op], 0, l);
  845. end;
  846. procedure tcgppc.a_jmp_name_direct(list : TAsmList; opc: tasmop; s : string; prependDot : boolean);
  847. var
  848. p: taicpu;
  849. begin
  850. if (prependDot) then
  851. s := '.' + s;
  852. p := taicpu.op_sym(opc, current_asmdata.RefAsmSymbol(s));
  853. p.is_jmp := true;
  854. list.concat(p)
  855. end;
  856. procedure tcgppc.a_jmp_name(list: TAsmList; const s: string);
  857. var
  858. p: taicpu;
  859. begin
  860. if (target_info.system = system_powerpc64_darwin) then
  861. begin
  862. p := taicpu.op_sym(A_B,get_darwin_call_stub(s,false));
  863. p.is_jmp := true;
  864. list.concat(p)
  865. end
  866. else
  867. a_jmp_name_direct(list, A_B, s, true);
  868. end;
  869. procedure tcgppc.a_jmp_always(list: TAsmList; l: tasmlabel);
  870. begin
  871. a_jmp(list, A_B, C_None, 0, l);
  872. end;
  873. { *********** entry/exit code and address loading ************ }
  874. procedure tcgppc.g_save_registers(list: TAsmList);
  875. begin
  876. { this work is done in g_proc_entry; additionally it is not safe
  877. to use it because it is called at some weird time }
  878. end;
  879. procedure tcgppc.g_restore_registers(list: TAsmList);
  880. begin
  881. { this work is done in g_proc_exit; mainly because it is not safe to
  882. put the register restore code here because it is called at some weird time }
  883. end;
  884. procedure tcgppc.calcFirstUsedFPR(out firstfpr : TSuperRegister; out fprcount : aint);
  885. var
  886. reg : TSuperRegister;
  887. begin
  888. fprcount := 0;
  889. firstfpr := RS_F31;
  890. if not (po_assembler in current_procinfo.procdef.procoptions) then
  891. for reg := RS_F14 to RS_F31 do
  892. if reg in rg[R_FPUREGISTER].used_in_proc then begin
  893. fprcount := ord(RS_F31)-ord(reg)+1;
  894. firstfpr := reg;
  895. break;
  896. end;
  897. end;
  898. procedure tcgppc.calcFirstUsedGPR(out firstgpr : TSuperRegister; out gprcount : aint);
  899. var
  900. reg : TSuperRegister;
  901. begin
  902. gprcount := 0;
  903. firstgpr := RS_R31;
  904. if not (po_assembler in current_procinfo.procdef.procoptions) then
  905. for reg := RS_R14 to RS_R31 do
  906. if reg in rg[R_INTREGISTER].used_in_proc then begin
  907. gprcount := ord(RS_R31)-ord(reg)+1;
  908. firstgpr := reg;
  909. break;
  910. end;
  911. end;
  912. procedure tcgppc.profilecode_savepara(para : tparavarsym; list : TAsmList);
  913. begin
  914. case (para.paraloc[calleeside].location^.loc) of
  915. LOC_REGISTER, LOC_CREGISTER:
  916. a_load_reg_ref(list, OS_INT, para.paraloc[calleeside].Location^.size,
  917. para.paraloc[calleeside].Location^.register, para.localloc.reference);
  918. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  919. a_loadfpu_reg_ref(list, para.paraloc[calleeside].Location^.size,
  920. para.paraloc[calleeside].Location^.size,
  921. para.paraloc[calleeside].Location^.register, para.localloc.reference);
  922. LOC_MMREGISTER, LOC_CMMREGISTER:
  923. { not supported }
  924. internalerror(2006041801);
  925. end;
  926. end;
  927. procedure tcgppc.profilecode_restorepara(para : tparavarsym; list : TAsmList);
  928. begin
  929. case (para.paraloc[calleeside].Location^.loc) of
  930. LOC_REGISTER, LOC_CREGISTER:
  931. a_load_ref_reg(list, para.paraloc[calleeside].Location^.size, OS_INT,
  932. para.localloc.reference, para.paraloc[calleeside].Location^.register);
  933. LOC_FPUREGISTER, LOC_CFPUREGISTER:
  934. a_loadfpu_ref_reg(list, para.paraloc[calleeside].Location^.size,
  935. para.paraloc[calleeside].Location^.size,
  936. para.localloc.reference, para.paraloc[calleeside].Location^.register);
  937. LOC_MMREGISTER, LOC_CMMREGISTER:
  938. { not supported }
  939. internalerror(2006041802);
  940. end;
  941. end;
  942. procedure tcgppc.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: aint);
  943. var
  944. hsym : tsym;
  945. href : treference;
  946. paraloc : Pcgparalocation;
  947. begin
  948. if ((ioffset >= low(smallint)) and (ioffset < high(smallint))) then begin
  949. { the original method can handle this }
  950. inherited g_adjust_self_value(list, procdef, ioffset);
  951. exit;
  952. end;
  953. { calculate the parameter info for the procdef }
  954. procdef.init_paraloc_info(callerside);
  955. hsym:=tsym(procdef.parast.Find('self'));
  956. if not(assigned(hsym) and
  957. (hsym.typ=paravarsym)) then
  958. internalerror(2010103101);
  959. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  960. while paraloc<>nil do
  961. with paraloc^ do begin
  962. case loc of
  963. LOC_REGISTER:
  964. begin
  965. a_load_const_reg(list, size, ioffset, NR_R11);
  966. a_op_reg_reg(list, OP_SUB, size, NR_R11, register);
  967. end else
  968. internalerror(2010103102);
  969. end;
  970. paraloc:=next;
  971. end;
  972. end;
  973. procedure tcgppc.g_profilecode(list: TAsmList);
  974. begin
  975. current_procinfo.procdef.paras.ForEachCall(TObjectListCallback(@profilecode_savepara), list);
  976. a_call_name_direct(list, A_BL, '_mcount', false, false, true);
  977. current_procinfo.procdef.paras.ForEachCall(TObjectListCallback(@profilecode_restorepara), list);
  978. end;
  979. { Generates the entry code of a procedure/function.
  980. This procedure may be called before, as well as after g_return_from_proc
  981. is called. localsize is the sum of the size necessary for local variables
  982. and the maximum possible combined size of ALL the parameters of a procedure
  983. called by the current one
  984. IMPORTANT: registers are not to be allocated through the register
  985. allocator here, because the register colouring has already occured !!
  986. }
  987. procedure tcgppc.g_proc_entry(list: TAsmList; localsize: longint;
  988. nostackframe: boolean);
  989. var
  990. firstregfpu, firstreggpr: TSuperRegister;
  991. needslinkreg: boolean;
  992. fprcount, gprcount : aint;
  993. { Save standard registers, both FPR and GPR; does not support VMX/Altivec }
  994. procedure save_standard_registers;
  995. var
  996. regcount : TSuperRegister;
  997. href : TReference;
  998. mayNeedLRStore : boolean;
  999. opc : tasmop;
  1000. begin
  1001. { there are two ways to do this: manually, by generating a few "std" instructions,
  1002. or via the restore helper functions. The latter are selected by the -Og switch,
  1003. i.e. "optimize for size" }
  1004. if (cs_opt_size in current_settings.optimizerswitches) and
  1005. (target_info.system <> system_powerpc64_darwin) then begin
  1006. mayNeedLRStore := false;
  1007. if target_info.system=system_powerpc64_aix then
  1008. opc:=A_BLA
  1009. else
  1010. opc:=A_BL;
  1011. if ((fprcount > 0) and (gprcount > 0)) then begin
  1012. a_op_const_reg_reg(list, OP_SUB, OS_INT, 8 * fprcount, NR_R1, NR_R12);
  1013. a_call_name_direct(list, opc, '_savegpr1_' + intToStr(32-gprcount), false, false, false, false);
  1014. a_call_name_direct(list, opc, '_savefpr_' + intToStr(32-fprcount), false, false, false, false);
  1015. end else if (gprcount > 0) then
  1016. a_call_name_direct(list, opc, '_savegpr0_' + intToStr(32-gprcount), false, false, false, false)
  1017. else if (fprcount > 0) then
  1018. a_call_name_direct(list, opc, '_savefpr_' + intToStr(32-fprcount), false, false, false, false)
  1019. else
  1020. mayNeedLRStore := true;
  1021. end else begin
  1022. { save registers, FPU first, then GPR }
  1023. reference_reset_base(href, NR_STACK_POINTER_REG, -8, 8);
  1024. if (fprcount > 0) then
  1025. for regcount := RS_F31 downto firstregfpu do begin
  1026. a_loadfpu_reg_ref(list, OS_FLOAT, OS_FLOAT, newreg(R_FPUREGISTER,
  1027. regcount, R_SUBNONE), href);
  1028. dec(href.offset, tcgsize2size[OS_FLOAT]);
  1029. end;
  1030. if (gprcount > 0) then
  1031. for regcount := RS_R31 downto firstreggpr do begin
  1032. a_load_reg_ref(list, OS_INT, OS_INT, newreg(R_INTREGISTER, regcount,
  1033. R_SUBNONE), href);
  1034. dec(href.offset, sizeof(pint));
  1035. end;
  1036. { VMX registers not supported by FPC atm }
  1037. { in this branch we always need to store LR ourselves}
  1038. mayNeedLRStore := true;
  1039. end;
  1040. { we may need to store R0 (=LR) ourselves }
  1041. if ((cs_profile in init_settings.moduleswitches) or (mayNeedLRStore)) and (needslinkreg) then begin
  1042. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_SYSV, 8);
  1043. list.concat(taicpu.op_reg_ref(A_STD, NR_R0, href));
  1044. end;
  1045. end;
  1046. var
  1047. href: treference;
  1048. begin
  1049. calcFirstUsedFPR(firstregfpu, fprcount);
  1050. calcFirstUsedGPR(firstreggpr, gprcount);
  1051. { calculate real stack frame size }
  1052. localsize := tppcprocinfo(current_procinfo).calc_stackframe_size(
  1053. gprcount, fprcount);
  1054. { determine whether we need to save the link register }
  1055. needslinkreg :=
  1056. not(nostackframe) and
  1057. (save_lr_in_prologue or
  1058. ((cs_opt_size in current_settings.optimizerswitches) and
  1059. ((fprcount > 0) or
  1060. (gprcount > 0))));
  1061. a_reg_alloc(list, NR_STACK_POINTER_REG);
  1062. a_reg_alloc(list, NR_R0);
  1063. { move link register to r0 }
  1064. if (needslinkreg) then
  1065. list.concat(taicpu.op_reg(A_MFLR, NR_R0));
  1066. save_standard_registers;
  1067. { save old stack frame pointer }
  1068. if (tppcprocinfo(current_procinfo).needs_frame_pointer) then
  1069. list.concat(taicpu.op_reg_reg(A_MR, NR_OLD_STACK_POINTER_REG, NR_STACK_POINTER_REG));
  1070. { create stack frame }
  1071. if (not nostackframe) and (localsize > 0) and
  1072. tppcprocinfo(current_procinfo).needstackframe then begin
  1073. if (localsize <= high(smallint)) then begin
  1074. reference_reset_base(href, NR_STACK_POINTER_REG, -localsize, 8);
  1075. a_load_store(list, A_STDU, NR_STACK_POINTER_REG, href);
  1076. end else begin
  1077. reference_reset_base(href, NR_NO, -localsize, 8);
  1078. { Use R0 for loading the constant (which is definitely > 32k when entering
  1079. this branch).
  1080. Inlined at this position because it must not use temp registers because
  1081. register allocations have already been done }
  1082. { Code template:
  1083. lis r0,ofs@highest
  1084. ori r0,r0,ofs@higher
  1085. sldi r0,r0,32
  1086. oris r0,r0,ofs@h
  1087. ori r0,r0,ofs@l
  1088. }
  1089. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  1090. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  1091. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  1092. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  1093. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  1094. list.concat(taicpu.op_reg_reg_reg(A_STDUX, NR_R1, NR_R1, NR_R0));
  1095. end;
  1096. end;
  1097. { CR register not used by FPC atm }
  1098. { keep R1 allocated??? }
  1099. a_reg_dealloc(list, NR_R0);
  1100. end;
  1101. { Generates the exit code for a method.
  1102. This procedure may be called before, as well as after g_stackframe_entry
  1103. is called.
  1104. IMPORTANT: registers are not to be allocated through the register
  1105. allocator here, because the register colouring has already occured !!
  1106. }
  1107. procedure tcgppc.g_proc_exit(list: TAsmList; parasize: longint; nostackframe:
  1108. boolean);
  1109. var
  1110. firstregfpu, firstreggpr: TSuperRegister;
  1111. needslinkreg : boolean;
  1112. fprcount, gprcount: aint;
  1113. { Restore standard registers, both FPR and GPR; does not support VMX/Altivec }
  1114. procedure restore_standard_registers;
  1115. var
  1116. { flag indicating whether we need to manually add the exit code (e.g. blr instruction)
  1117. or not }
  1118. needsExitCode : Boolean;
  1119. href : treference;
  1120. regcount : TSuperRegister;
  1121. callopc,
  1122. jmpopc: tasmop;
  1123. begin
  1124. { there are two ways to do this: manually, by generating a few "ld" instructions,
  1125. or via the restore helper functions. The latter are selected by the -Og switch,
  1126. i.e. "optimize for size" }
  1127. if (cs_opt_size in current_settings.optimizerswitches) then begin
  1128. if target_info.system=system_powerpc64_aix then begin
  1129. callopc:=A_BLA;
  1130. jmpopc:=A_BA;
  1131. end
  1132. else begin
  1133. callopc:=A_BL;
  1134. jmpopc:=A_B;
  1135. end;
  1136. needsExitCode := false;
  1137. if ((fprcount > 0) and (gprcount > 0)) then begin
  1138. a_op_const_reg_reg(list, OP_SUB, OS_INT, 8 * fprcount, NR_R1, NR_R12);
  1139. a_call_name_direct(list, callopc, '_restgpr1_' + intToStr(32-gprcount), false, false, false, false);
  1140. a_jmp_name_direct(list, jmpopc, '_restfpr_' + intToStr(32-fprcount), false);
  1141. end else if (gprcount > 0) then
  1142. a_jmp_name_direct(list, jmpopc, '_restgpr0_' + intToStr(32-gprcount), false)
  1143. else if (fprcount > 0) then
  1144. a_jmp_name_direct(list, jmpopc, '_restfpr_' + intToStr(32-fprcount), false)
  1145. else
  1146. needsExitCode := true;
  1147. end else begin
  1148. needsExitCode := true;
  1149. { restore registers, FPU first, GPR next }
  1150. reference_reset_base(href, NR_STACK_POINTER_REG, -tcgsize2size[OS_FLOAT], 8);
  1151. if (fprcount > 0) then
  1152. for regcount := RS_F31 downto firstregfpu do begin
  1153. a_loadfpu_ref_reg(list, OS_FLOAT, OS_FLOAT, href, newreg(R_FPUREGISTER, regcount,
  1154. R_SUBNONE));
  1155. dec(href.offset, tcgsize2size[OS_FLOAT]);
  1156. end;
  1157. if (gprcount > 0) then
  1158. for regcount := RS_R31 downto firstreggpr do begin
  1159. a_load_ref_reg(list, OS_INT, OS_INT, href, newreg(R_INTREGISTER, regcount,
  1160. R_SUBNONE));
  1161. dec(href.offset, sizeof(pint));
  1162. end;
  1163. { VMX not supported by FPC atm }
  1164. end;
  1165. if (needsExitCode) then begin
  1166. { restore LR (if needed) }
  1167. if (needslinkreg) then begin
  1168. reference_reset_base(href, NR_STACK_POINTER_REG, LA_LR_SYSV, 8);
  1169. list.concat(taicpu.op_reg_ref(A_LD, NR_R0, href));
  1170. list.concat(taicpu.op_reg(A_MTLR, NR_R0));
  1171. end;
  1172. { generate return instruction }
  1173. list.concat(taicpu.op_none(A_BLR));
  1174. end;
  1175. end;
  1176. var
  1177. href: treference;
  1178. localsize : aint;
  1179. begin
  1180. calcFirstUsedFPR(firstregfpu, fprcount);
  1181. calcFirstUsedGPR(firstreggpr, gprcount);
  1182. { determine whether we need to restore the link register }
  1183. needslinkreg :=
  1184. not(nostackframe) and
  1185. (((not (po_assembler in current_procinfo.procdef.procoptions)) and
  1186. ((pi_do_call in current_procinfo.flags) or (cs_profile in init_settings.moduleswitches))) or
  1187. ((cs_opt_size in current_settings.optimizerswitches) and ((fprcount > 0) or (gprcount > 0))) or
  1188. ([cs_lineinfo, cs_debuginfo] * current_settings.moduleswitches <> []));
  1189. { calculate stack frame }
  1190. localsize := tppcprocinfo(current_procinfo).calc_stackframe_size(
  1191. gprcount, fprcount);
  1192. { CR register not supported }
  1193. { restore stack pointer }
  1194. if (not nostackframe) and (localsize > 0) and
  1195. tppcprocinfo(current_procinfo).needstackframe then begin
  1196. if (localsize <= high(smallint)) then begin
  1197. list.concat(taicpu.op_reg_reg_const(A_ADDI, NR_STACK_POINTER_REG, NR_STACK_POINTER_REG, localsize));
  1198. end else begin
  1199. reference_reset_base(href, NR_NO, localsize, 8);
  1200. { use R0 for loading the constant (which is definitely > 32k when entering
  1201. this branch)
  1202. Inlined because it must not use temp registers because register allocations
  1203. have already been done
  1204. }
  1205. { Code template:
  1206. lis r0,ofs@highest
  1207. ori r0,ofs@higher
  1208. sldi r0,r0,32
  1209. oris r0,r0,ofs@h
  1210. ori r0,r0,ofs@l
  1211. }
  1212. list.concat(taicpu.op_reg_const(A_LIS, NR_R0, word(href.offset shr 48)));
  1213. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset shr 32)));
  1214. list.concat(taicpu.op_reg_reg_const(A_SLDI, NR_R0, NR_R0, 32));
  1215. list.concat(taicpu.op_reg_reg_const(A_ORIS, NR_R0, NR_R0, word(href.offset shr 16)));
  1216. list.concat(taicpu.op_reg_reg_const(A_ORI, NR_R0, NR_R0, word(href.offset)));
  1217. list.concat(taicpu.op_reg_reg_reg(A_ADD, NR_R1, NR_R1, NR_R0));
  1218. end;
  1219. end;
  1220. restore_standard_registers;
  1221. end;
  1222. procedure tcgppc.a_loadaddr_ref_reg(list: TAsmList; const ref: treference; r:
  1223. tregister);
  1224. var
  1225. ref2, tmpref: treference;
  1226. { register used to construct address }
  1227. tempreg : TRegister;
  1228. begin
  1229. if (target_info.system in [system_powerpc64_darwin,system_powerpc64_aix]) then
  1230. begin
  1231. inherited a_loadaddr_ref_reg(list,ref,r);
  1232. exit;
  1233. end;
  1234. ref2 := ref;
  1235. fixref(list, ref2);
  1236. { load a symbol }
  1237. if (assigned(ref2.symbol) or (hasLargeOffset(ref2))) then begin
  1238. { add the symbol's value to the base of the reference, and if the }
  1239. { reference doesn't have a base, create one }
  1240. reference_reset(tmpref, ref2.alignment);
  1241. tmpref.offset := ref2.offset;
  1242. tmpref.symbol := ref2.symbol;
  1243. tmpref.relsymbol := ref2.relsymbol;
  1244. { load 64 bit reference into r. If the reference already has a base register,
  1245. first load the 64 bit value into a temp register, then add it to the result
  1246. register rD }
  1247. if (ref2.base <> NR_NO) then begin
  1248. { already have a base register, so allocate a new one }
  1249. tempreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1250. end else begin
  1251. tempreg := r;
  1252. end;
  1253. { code for loading a reference from a symbol into a register rD }
  1254. (*
  1255. lis rX,SYM@highest
  1256. ori rX,SYM@higher
  1257. sldi rX,rX,32
  1258. oris rX,rX,SYM@h
  1259. ori rX,rX,SYM@l
  1260. *)
  1261. {$IFDEF EXTDEBUG}
  1262. list.concat(tai_comment.create(strpnew('loadaddr_ref_reg ')));
  1263. {$ENDIF EXTDEBUG}
  1264. if (assigned(tmpref.symbol)) then begin
  1265. tmpref.refaddr := addr_highest;
  1266. list.concat(taicpu.op_reg_ref(A_LIS, tempreg, tmpref));
  1267. tmpref.refaddr := addr_higher;
  1268. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1269. list.concat(taicpu.op_reg_reg_const(A_SLDI, tempreg, tempreg, 32));
  1270. tmpref.refaddr := addr_high;
  1271. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tempreg, tempreg, tmpref));
  1272. tmpref.refaddr := addr_low;
  1273. list.concat(taicpu.op_reg_reg_ref(A_ORI, tempreg, tempreg, tmpref));
  1274. end else
  1275. a_load_const_reg(list, OS_ADDR, tmpref.offset, tempreg);
  1276. { if there's already a base register, add the temp register contents to
  1277. the base register }
  1278. if (ref2.base <> NR_NO) then begin
  1279. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, tempreg, ref2.base));
  1280. end;
  1281. end else if (ref2.offset <> 0) then begin
  1282. { no symbol, but offset <> 0 }
  1283. if (ref2.base <> NR_NO) then begin
  1284. a_op_const_reg_reg(list, OP_ADD, OS_64, ref2.offset, ref2.base, r)
  1285. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never
  1286. occurs, so now only ref.offset has to be loaded }
  1287. end else begin
  1288. a_load_const_reg(list, OS_64, ref2.offset, r);
  1289. end;
  1290. end else if (ref2.index <> NR_NO) then begin
  1291. list.concat(taicpu.op_reg_reg_reg(A_ADD, r, ref2.base, ref2.index))
  1292. end else if (ref2.base <> NR_NO) and
  1293. (r <> ref2.base) then begin
  1294. a_load_reg_reg(list, OS_ADDR, OS_ADDR, ref2.base, r)
  1295. end else begin
  1296. list.concat(taicpu.op_reg_const(A_LI, r, 0));
  1297. end;
  1298. end;
  1299. { ************* concatcopy ************ }
  1300. procedure tcgppc.g_concatcopy(list: TAsmList; const source, dest: treference;
  1301. len: aint);
  1302. var
  1303. countreg, tempreg:TRegister;
  1304. src, dst: TReference;
  1305. lab: tasmlabel;
  1306. count, count2, step: longint;
  1307. size: tcgsize;
  1308. begin
  1309. {$IFDEF extdebug}
  1310. if len > high(aint) then
  1311. internalerror(2002072704);
  1312. list.concat(tai_comment.create(strpnew('g_concatcopy1 ' + inttostr(len) + ' bytes left ')));
  1313. {$ENDIF extdebug}
  1314. { if the references are equal, exit, there is no need to copy anything }
  1315. if references_equal(source, dest) or
  1316. (len=0) then
  1317. exit;
  1318. { make sure short loads are handled as optimally as possible;
  1319. note that the data here never overlaps, so we can do a forward
  1320. copy at all times.
  1321. NOTE: maybe use some scratch registers to pair load/store instructions
  1322. }
  1323. if (len <= 8) then begin
  1324. src := source; dst := dest;
  1325. {$IFDEF extdebug}
  1326. list.concat(tai_comment.create(strpnew('g_concatcopy3 ' + inttostr(src.offset) + ' ' + inttostr(dst.offset))));
  1327. {$ENDIF extdebug}
  1328. while (len <> 0) do begin
  1329. if (len = 8) then begin
  1330. a_load_ref_ref(list, OS_64, OS_64, src, dst);
  1331. dec(len, 8);
  1332. end else if (len >= 4) then begin
  1333. a_load_ref_ref(list, OS_32, OS_32, src, dst);
  1334. inc(src.offset, 4); inc(dst.offset, 4);
  1335. dec(len, 4);
  1336. end else if (len >= 2) then begin
  1337. a_load_ref_ref(list, OS_16, OS_16, src, dst);
  1338. inc(src.offset, 2); inc(dst.offset, 2);
  1339. dec(len, 2);
  1340. end else begin
  1341. a_load_ref_ref(list, OS_8, OS_8, src, dst);
  1342. inc(src.offset, 1); inc(dst.offset, 1);
  1343. dec(len, 1);
  1344. end;
  1345. end;
  1346. exit;
  1347. end;
  1348. {$IFDEF extdebug}
  1349. list.concat(tai_comment.create(strpnew('g_concatcopy2 ' + inttostr(len) + ' bytes left ')));
  1350. {$ENDIF extdebug}
  1351. if not(source.alignment in [1,2]) and
  1352. not(dest.alignment in [1,2]) then
  1353. begin
  1354. count:=len div 8;
  1355. step:=8;
  1356. size:=OS_64;
  1357. end
  1358. else
  1359. begin
  1360. count:=len div 4;
  1361. step:=4;
  1362. size:=OS_32;
  1363. end;
  1364. tempreg:=getintregister(list,size);
  1365. reference_reset(src,source.alignment);
  1366. reference_reset(dst,dest.alignment);
  1367. { load the address of source into src.base }
  1368. if (count > 4) or
  1369. not issimpleref(source) or
  1370. ((source.index <> NR_NO) and
  1371. ((source.offset + len) > high(smallint))) then begin
  1372. src.base := getaddressregister(list);
  1373. a_loadaddr_ref_reg(list, source, src.base);
  1374. end else begin
  1375. src := source;
  1376. end;
  1377. { load the address of dest into dst.base }
  1378. if (count > 4) or
  1379. not issimpleref(dest) or
  1380. ((dest.index <> NR_NO) and
  1381. ((dest.offset + len) > high(smallint))) then begin
  1382. dst.base := getaddressregister(list);
  1383. a_loadaddr_ref_reg(list, dest, dst.base);
  1384. end else begin
  1385. dst := dest;
  1386. end;
  1387. { generate a loop }
  1388. if count > 4 then begin
  1389. { the offsets are zero after the a_loadaddress_ref_reg and just
  1390. have to be set to step. I put an Inc there so debugging may be
  1391. easier (should offset be different from zero here, it will be
  1392. easy to notice in the generated assembler }
  1393. inc(dst.offset, step);
  1394. inc(src.offset, step);
  1395. list.concat(taicpu.op_reg_reg_const(A_SUBI, src.base, src.base, step));
  1396. list.concat(taicpu.op_reg_reg_const(A_SUBI, dst.base, dst.base, step));
  1397. countreg := getintregister(list, OS_INT);
  1398. a_load_const_reg(list, OS_INT, count, countreg);
  1399. current_asmdata.getjumplabel(lab);
  1400. a_label(list, lab);
  1401. list.concat(taicpu.op_reg_reg_const(A_SUBIC_, countreg, countreg, 1));
  1402. if (size=OS_64) then
  1403. begin
  1404. list.concat(taicpu.op_reg_ref(A_LDU, tempreg, src));
  1405. list.concat(taicpu.op_reg_ref(A_STDU, tempreg, dst));
  1406. end
  1407. else
  1408. begin
  1409. list.concat(taicpu.op_reg_ref(A_LWZU, tempreg, src));
  1410. list.concat(taicpu.op_reg_ref(A_STWU, tempreg, dst));
  1411. end;
  1412. a_jmp(list, A_BC, C_NE, 0, lab);
  1413. a_reg_sync(list,src.base);
  1414. a_reg_sync(list,dst.base);
  1415. a_reg_sync(list,countreg);
  1416. len := len mod step;
  1417. count := 0;
  1418. end;
  1419. { unrolled loop }
  1420. if count > 0 then begin
  1421. for count2 := 1 to count do begin
  1422. a_load_ref_reg(list, size, size, src, tempreg);
  1423. a_load_reg_ref(list, size, size, tempreg, dst);
  1424. inc(src.offset, step);
  1425. inc(dst.offset, step);
  1426. end;
  1427. len := len mod step;
  1428. end;
  1429. if (len and 4) <> 0 then begin
  1430. a_load_ref_reg(list, OS_32, OS_32, src, tempreg);
  1431. a_load_reg_ref(list, OS_32, OS_32, tempreg, dst);
  1432. inc(src.offset, 4);
  1433. inc(dst.offset, 4);
  1434. end;
  1435. { copy the leftovers }
  1436. if (len and 2) <> 0 then begin
  1437. a_load_ref_reg(list, OS_16, OS_16, src, tempreg);
  1438. a_load_reg_ref(list, OS_16, OS_16, tempreg, dst);
  1439. inc(src.offset, 2);
  1440. inc(dst.offset, 2);
  1441. end;
  1442. if (len and 1) <> 0 then begin
  1443. a_load_ref_reg(list, OS_8, OS_8, src, tempreg);
  1444. a_load_reg_ref(list, OS_8, OS_8, tempreg, dst);
  1445. end;
  1446. end;
  1447. {***************** This is private property, keep out! :) *****************}
  1448. procedure tcgppc.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  1449. const
  1450. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  1451. begin
  1452. {$IFDEF EXTDEBUG}
  1453. list.concat(tai_comment.create(strpnew('maybeadjustresult op = ' + cgop2string(op) + ' size = ' + cgsize2string(size))));
  1454. {$ENDIF EXTDEBUG}
  1455. if (op in overflowops) and (size in [OS_8, OS_S8, OS_16, OS_S16, OS_32, OS_S32]) then
  1456. a_load_reg_reg(list, OS_64, size, dst, dst);
  1457. end;
  1458. function tcgppc.issimpleref(const ref: treference): boolean;
  1459. begin
  1460. if (ref.base = NR_NO) and
  1461. (ref.index <> NR_NO) then
  1462. internalerror(200208101);
  1463. result :=
  1464. not (assigned(ref.symbol)) and
  1465. (((ref.index = NR_NO) and
  1466. (ref.offset >= low(smallint)) and
  1467. (ref.offset <= high(smallint))) or
  1468. ((ref.index <> NR_NO) and
  1469. (ref.offset = 0)));
  1470. end;
  1471. procedure tcgppc.a_load_store(list: TAsmList; op: tasmop; reg: tregister;
  1472. ref: treference);
  1473. procedure maybefixup64bitoffset;
  1474. var
  1475. tmpreg: tregister;
  1476. begin
  1477. { for some instructions we need to check that the offset is divisible by at
  1478. least four. If not, add the bytes which are "off" to the base register and
  1479. adjust the offset accordingly }
  1480. case op of
  1481. A_LD, A_LDU, A_STD, A_STDU, A_LWA :
  1482. if ((ref.offset mod 4) <> 0) then begin
  1483. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1484. if (ref.base <> NR_NO) then begin
  1485. a_op_const_reg_reg(list, OP_ADD, OS_ADDR, ref.offset mod 4, ref.base, tmpreg);
  1486. ref.base := tmpreg;
  1487. end else begin
  1488. list.concat(taicpu.op_reg_const(A_LI, tmpreg, ref.offset mod 4));
  1489. ref.base := tmpreg;
  1490. end;
  1491. ref.offset := (ref.offset div 4) * 4;
  1492. end;
  1493. end;
  1494. end;
  1495. var
  1496. tmpreg, tmpreg2: tregister;
  1497. tmpref: treference;
  1498. largeOffset: Boolean;
  1499. begin
  1500. if (target_info.system = system_powerpc64_darwin) then
  1501. begin
  1502. { darwin/ppc64 works with 32 bit relocatable symbol addresses }
  1503. maybefixup64bitoffset;
  1504. inherited a_load_store(list,op,reg,ref);
  1505. exit
  1506. end;
  1507. { at this point there must not be a combination of values in the ref treference
  1508. which is not possible to directly map to instructions of the PowerPC architecture }
  1509. if (ref.index <> NR_NO) and ((ref.offset <> 0) or (assigned(ref.symbol))) then
  1510. internalerror(200310131);
  1511. { if this is a PIC'ed address, handle it and exit }
  1512. if (ref.refaddr in [addr_pic,addr_pic_no_got]) then begin
  1513. if (ref.offset <> 0) then
  1514. internalerror(2006010501);
  1515. if (ref.index <> NR_NO) then
  1516. internalerror(2006010502);
  1517. if (not assigned(ref.symbol)) then
  1518. internalerror(200601050);
  1519. list.concat(taicpu.op_reg_ref(op, reg, ref));
  1520. exit;
  1521. end;
  1522. maybefixup64bitoffset;
  1523. {$IFDEF EXTDEBUG}
  1524. list.concat(tai_comment.create(strpnew('a_load_store1 ' + BoolToStr(ref.refaddr = addr_pic))));
  1525. {$ENDIF EXTDEBUG}
  1526. { if we have to load/store from a symbol or large addresses, use a temporary register
  1527. containing the address }
  1528. if (assigned(ref.symbol) or (hasLargeOffset(ref))) then begin
  1529. tmpreg := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1530. if (hasLargeOffset(ref) and (ref.base = NR_NO)) then begin
  1531. ref.base := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1532. a_load_const_reg(list, OS_ADDR, ref.offset, ref.base);
  1533. ref.offset := 0;
  1534. end;
  1535. reference_reset(tmpref, ref.alignment);
  1536. tmpref.symbol := ref.symbol;
  1537. tmpref.relsymbol := ref.relsymbol;
  1538. tmpref.offset := ref.offset;
  1539. if (ref.base <> NR_NO) then begin
  1540. { As long as the TOC isn't working we try to achieve highest speed (in this
  1541. case by allowing instructions execute in parallel) as possible at the cost
  1542. of using another temporary register. So the code template when there is
  1543. a base register and an offset is the following:
  1544. lis rT1, SYM+offs@highest
  1545. ori rT1, rT1, SYM+offs@higher
  1546. lis rT2, SYM+offs@hi
  1547. ori rT2, SYM+offs@lo
  1548. rldimi rT2, rT1, 32
  1549. <op>X reg, base, rT2
  1550. }
  1551. tmpreg2 := rg[R_INTREGISTER].getregister(list, R_SUBWHOLE);
  1552. if (assigned(tmpref.symbol)) then begin
  1553. tmpref.refaddr := addr_highest;
  1554. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  1555. tmpref.refaddr := addr_higher;
  1556. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  1557. tmpref.refaddr := addr_high;
  1558. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg2, tmpref));
  1559. tmpref.refaddr := addr_low;
  1560. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg2, tmpreg2, tmpref));
  1561. list.concat(taicpu.op_reg_reg_const_const(A_RLDIMI, tmpreg2, tmpreg, 32, 0));
  1562. end else
  1563. a_load_const_reg(list, OS_ADDR, tmpref.offset, tmpreg2);
  1564. reference_reset(tmpref, ref.alignment);
  1565. tmpref.base := ref.base;
  1566. tmpref.index := tmpreg2;
  1567. case op of
  1568. { the code generator doesn't generate update instructions anyway, so
  1569. error out on those instructions }
  1570. A_LBZ : op := A_LBZX;
  1571. A_LHZ : op := A_LHZX;
  1572. A_LWZ : op := A_LWZX;
  1573. A_LD : op := A_LDX;
  1574. A_LHA : op := A_LHAX;
  1575. A_LWA : op := A_LWAX;
  1576. A_LFS : op := A_LFSX;
  1577. A_LFD : op := A_LFDX;
  1578. A_STB : op := A_STBX;
  1579. A_STH : op := A_STHX;
  1580. A_STW : op := A_STWX;
  1581. A_STD : op := A_STDX;
  1582. A_STFS : op := A_STFSX;
  1583. A_STFD : op := A_STFDX;
  1584. else
  1585. { unknown load/store opcode }
  1586. internalerror(2005101302);
  1587. end;
  1588. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  1589. end else begin
  1590. { when accessing value from a reference without a base register, use the
  1591. following code template:
  1592. lis rT,SYM+offs@highesta
  1593. ori rT,SYM+offs@highera
  1594. sldi rT,rT,32
  1595. oris rT,rT,SYM+offs@ha
  1596. ld rD,SYM+offs@l(rT)
  1597. }
  1598. tmpref.refaddr := addr_highesta;
  1599. list.concat(taicpu.op_reg_ref(A_LIS, tmpreg, tmpref));
  1600. tmpref.refaddr := addr_highera;
  1601. list.concat(taicpu.op_reg_reg_ref(A_ORI, tmpreg, tmpreg, tmpref));
  1602. list.concat(taicpu.op_reg_reg_const(A_SLDI, tmpreg, tmpreg, 32));
  1603. tmpref.refaddr := addr_higha;
  1604. list.concat(taicpu.op_reg_reg_ref(A_ORIS, tmpreg, tmpreg, tmpref));
  1605. tmpref.base := tmpreg;
  1606. tmpref.refaddr := addr_low;
  1607. list.concat(taicpu.op_reg_ref(op, reg, tmpref));
  1608. end;
  1609. end else begin
  1610. list.concat(taicpu.op_reg_ref(op, reg, ref));
  1611. end;
  1612. end;
  1613. procedure tcgppc.loadConstantPIC(list : TAsmList; size : TCGSize; a : aint; reg : TRegister);
  1614. var
  1615. l: tasmsymbol;
  1616. ref: treference;
  1617. symname : string;
  1618. begin
  1619. maybe_new_object_file(current_asmdata.asmlists[al_picdata]);
  1620. symname := '_$' + current_asmdata.name^ + '$toc$' + hexstr(a, sizeof(a)*2);
  1621. l:=current_asmdata.getasmsymbol(symname);
  1622. if not(assigned(l)) then begin
  1623. l:=current_asmdata.DefineAsmSymbol(symname,AB_GLOBAL, AT_DATA);
  1624. new_section(current_asmdata.asmlists[al_picdata],sec_toc, '.toc', 8);
  1625. current_asmdata.asmlists[al_picdata].concat(tai_symbol.create_global(l,0));
  1626. current_asmdata.asmlists[al_picdata].concat(tai_directive.create(asd_toc_entry, symname + '[TC], ' + inttostr(a)));
  1627. end;
  1628. reference_reset_symbol(ref,l,0, 8);
  1629. ref.base := NR_R2;
  1630. ref.refaddr := addr_no;
  1631. {$IFDEF EXTDEBUG}
  1632. list.concat(tai_comment.create(strpnew('loading value from TOC reference for ' + symname)));
  1633. {$ENDIF EXTDEBUG}
  1634. a_load_ref_reg(list, OS_INT, OS_INT, ref, reg);
  1635. end;
  1636. procedure create_codegen;
  1637. begin
  1638. cg := tcgppc.create;
  1639. cg128:=tcg128.create;
  1640. end;
  1641. end.