cgcpu.pas 67 KB

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