cgppc.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. {
  2. Copyright (c) 2006 by Florian Klaempfl
  3. This unit implements the common part of 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 cgppc;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,symtype,symdef,
  22. cgbase,cgobj,
  23. aasmbase,aasmcpu,aasmtai,aasmdata,
  24. cpubase,cpuinfo,cgutils,rgcpu,
  25. parabase;
  26. type
  27. tcgppcgen = class(tcg)
  28. procedure a_param_const(list: TAsmList; size: tcgsize; a: aint; const paraloc : tcgpara); override;
  29. procedure a_paramaddr_ref(list : TAsmList;const r : treference;const paraloc : tcgpara); override;
  30. procedure a_call_reg(list : TAsmList;reg: tregister); override;
  31. procedure a_call_ref(list : TAsmList;ref: treference); override;
  32. { stores the contents of register reg to the memory location described by
  33. ref }
  34. procedure a_load_reg_ref(list: TAsmList; fromsize, tosize: TCGSize;
  35. reg: tregister; const ref: treference); override;
  36. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  37. { fpu move instructions }
  38. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  39. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  40. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  41. { overflow checking }
  42. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef);override;
  43. { entry code }
  44. procedure g_profilecode(list: TAsmList); override;
  45. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  46. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  47. protected
  48. function get_darwin_call_stub(const s: string): tasmsymbol;
  49. procedure a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister); override;
  50. function fixref(list: TAsmList; var ref: treference): boolean; virtual; abstract;
  51. { contains the common code of a_load_reg_ref and a_load_ref_reg }
  52. procedure a_load_store(list:TAsmList;op: tasmop;reg:tregister;ref: treference);virtual;
  53. { creates the correct branch instruction for a given combination }
  54. { of asmcondflags and destination addressing mode }
  55. procedure a_jmp(list: TAsmList; op: tasmop;
  56. c: tasmcondflag; crval: longint; l: tasmlabel);
  57. { returns true if the offset of the given reference can not be }
  58. { represented by a 16 bit immediate as required by some PowerPC }
  59. { instructions }
  60. function hasLargeOffset(const ref : TReference) : Boolean; inline;
  61. end;
  62. const
  63. TOpCmp2AsmCond: Array[topcmp] of TAsmCondFlag = (C_NONE,C_EQ,C_GT,
  64. C_LT,C_GE,C_LE,C_NE,C_LE,C_LT,C_GE,C_GT);
  65. implementation
  66. uses
  67. globals,verbose,systems,cutils,
  68. symconst,symsym,fmodule,
  69. rgobj,tgobj,cpupi,procinfo,paramgr;
  70. function tcgppcgen.hasLargeOffset(const ref : TReference) : Boolean;
  71. begin
  72. result := aword(ref.offset-low(smallint)) > high(smallint)-low(smallint);
  73. end;
  74. procedure tcgppcgen.a_param_const(list: TAsmList; size: tcgsize; a: aint; const
  75. paraloc: tcgpara);
  76. var
  77. ref: treference;
  78. begin
  79. paraloc.check_simple_location;
  80. case paraloc.location^.loc of
  81. LOC_REGISTER, LOC_CREGISTER:
  82. a_load_const_reg(list, size, a, paraloc.location^.register);
  83. LOC_REFERENCE:
  84. begin
  85. reference_reset(ref);
  86. ref.base := paraloc.location^.reference.index;
  87. ref.offset := paraloc.location^.reference.offset;
  88. a_load_const_ref(list, size, a, ref);
  89. end;
  90. else
  91. internalerror(2002081101);
  92. end;
  93. end;
  94. procedure tcgppcgen.a_paramaddr_ref(list : TAsmList;const r : treference;const paraloc : tcgpara);
  95. var
  96. ref: treference;
  97. tmpreg: tregister;
  98. begin
  99. paraloc.check_simple_location;
  100. case paraloc.location^.loc of
  101. LOC_REGISTER,LOC_CREGISTER:
  102. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  103. LOC_REFERENCE:
  104. begin
  105. reference_reset(ref);
  106. ref.base := paraloc.location^.reference.index;
  107. ref.offset := paraloc.location^.reference.offset;
  108. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  109. a_loadaddr_ref_reg(list,r,tmpreg);
  110. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  111. end;
  112. else
  113. internalerror(2002080701);
  114. end;
  115. end;
  116. function tcgppcgen.get_darwin_call_stub(const s: string): tasmsymbol;
  117. var
  118. stubname: string;
  119. href: treference;
  120. l1: tasmsymbol;
  121. begin
  122. { function declared in the current unit? }
  123. { doesn't work correctly, because this will also return a hit if we }
  124. { previously took the address of an external procedure. It doesn't }
  125. { really matter, the linker will remove all unnecessary stubs. }
  126. stubname := 'L'+s+'$stub';
  127. result := current_asmdata.getasmsymbol(stubname);
  128. if assigned(result) then
  129. exit;
  130. if current_asmdata.asmlists[al_imports]=nil then
  131. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  132. current_asmdata.asmlists[al_imports].concat(Tai_section.create(sec_stub,'',0));
  133. current_asmdata.asmlists[al_imports].concat(Tai_align.Create(16));
  134. result := current_asmdata.RefAsmSymbol(stubname);
  135. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  136. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  137. l1 := current_asmdata.RefAsmSymbol('L'+s+'$lazy_ptr');
  138. reference_reset_symbol(href,l1,0);
  139. href.refaddr := addr_hi;
  140. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LIS,NR_R11,href));
  141. href.refaddr := addr_lo;
  142. href.base := NR_R11;
  143. {$ifndef cpu64bit}
  144. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LWZU,NR_R12,href));
  145. {$else cpu64bit}
  146. { darwin/ppc64 uses a 32 bit absolute address here, strange... }
  147. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LDU,NR_R12,href));
  148. {$endif cpu64bit}
  149. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg(A_MTCTR,NR_R12));
  150. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_BCTR));
  151. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_lazy_symbol_pointer,''));
  152. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(l1,0));
  153. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  154. current_asmdata.asmlists[al_imports].concat(tai_const.createname('dyld_stub_binding_helper',0));
  155. end;
  156. procedure tcgppcgen.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  157. var
  158. ref2, tmpref: treference;
  159. begin
  160. ref2 := ref;
  161. fixref(list,ref2);
  162. if assigned(ref2.symbol) then
  163. begin
  164. if target_info.system = system_powerpc_macos then
  165. begin
  166. if macos_direct_globals then
  167. begin
  168. reference_reset(tmpref);
  169. tmpref.offset := ref2.offset;
  170. tmpref.symbol := ref2.symbol;
  171. tmpref.base := NR_NO;
  172. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,NR_RTOC,tmpref));
  173. end
  174. else
  175. begin
  176. reference_reset(tmpref);
  177. tmpref.symbol := ref2.symbol;
  178. tmpref.offset := 0;
  179. tmpref.base := NR_RTOC;
  180. list.concat(taicpu.op_reg_ref(A_LWZ,r,tmpref));
  181. if ref2.offset <> 0 then
  182. begin
  183. reference_reset(tmpref);
  184. tmpref.offset := ref2.offset;
  185. tmpref.base:= r;
  186. list.concat(taicpu.op_reg_ref(A_LA,r,tmpref));
  187. end;
  188. end;
  189. if ref2.base <> NR_NO then
  190. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,r,ref2.base));
  191. //list.concat(tai_comment.create(strpnew('*** a_loadaddr_ref_reg')));
  192. end
  193. else
  194. begin
  195. { add the symbol's value to the base of the reference, and if the }
  196. { reference doesn't have a base, create one }
  197. reference_reset(tmpref);
  198. tmpref.offset := ref2.offset;
  199. tmpref.symbol := ref2.symbol;
  200. tmpref.relsymbol := ref2.relsymbol;
  201. tmpref.refaddr := addr_hi;
  202. if ref2.base<> NR_NO then
  203. begin
  204. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,r,
  205. ref2.base,tmpref));
  206. end
  207. else
  208. list.concat(taicpu.op_reg_ref(A_LIS,r,tmpref));
  209. tmpref.base := NR_NO;
  210. tmpref.refaddr := addr_lo;
  211. { can be folded with one of the next instructions by the }
  212. { optimizer probably }
  213. list.concat(taicpu.op_reg_reg_ref(A_ADDI,r,r,tmpref));
  214. end
  215. end
  216. else if ref2.offset <> 0 Then
  217. if ref2.base <> NR_NO then
  218. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref2.offset,ref2.base,r)
  219. { FixRef makes sure that "(ref.index <> R_NO) and (ref.offset <> 0)" never}
  220. { occurs, so now only ref.offset has to be loaded }
  221. else
  222. a_load_const_reg(list,OS_ADDR,ref2.offset,r)
  223. else if ref2.index <> NR_NO Then
  224. list.concat(taicpu.op_reg_reg_reg(A_ADD,r,ref2.base,ref2.index))
  225. else if (ref2.base <> NR_NO) and
  226. (r <> ref2.base) then
  227. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref2.base,r)
  228. else
  229. list.concat(taicpu.op_reg_const(A_LI,r,0));
  230. end;
  231. { calling a procedure by address }
  232. procedure tcgppcgen.a_call_reg(list : TAsmList;reg: tregister);
  233. begin
  234. list.concat(taicpu.op_reg(A_MTCTR,reg));
  235. list.concat(taicpu.op_none(A_BCTRL));
  236. include(current_procinfo.flags,pi_do_call);
  237. end;
  238. procedure tcgppcgen.a_call_ref(list : TAsmList;ref: treference);
  239. var
  240. tempreg : TRegister;
  241. begin
  242. tempreg := getintregister(list, OS_ADDR);
  243. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,tempreg);
  244. a_call_reg(list,tempreg);
  245. end;
  246. procedure tcgppcgen.a_load_reg_ref(list: TAsmList; fromsize, tosize: TCGSize;
  247. reg: tregister; const ref: treference);
  248. const
  249. StoreInstr: array[OS_8..OS_INT, boolean, boolean] of TAsmOp =
  250. { indexed? updating?}
  251. (((A_STB, A_STBU), (A_STBX, A_STBUX)),
  252. ((A_STH, A_STHU), (A_STHX, A_STHUX)),
  253. ((A_STW, A_STWU), (A_STWX, A_STWUX))
  254. {$ifdef cpu64bit}
  255. ,
  256. ((A_STD, A_STDU), (A_STDX, A_STDUX))
  257. {$endif cpu64bit}
  258. );
  259. var
  260. op: TAsmOp;
  261. ref2: TReference;
  262. begin
  263. if not (fromsize in [OS_8..OS_INT,OS_S8..OS_SINT]) then
  264. internalerror(2002090903);
  265. if not (tosize in [OS_8..OS_INT,OS_S8..OS_SINT]) then
  266. internalerror(2002090905);
  267. ref2 := ref;
  268. fixref(list, ref2);
  269. if tosize in [OS_S8..OS_SINT] then
  270. { storing is the same for signed and unsigned values }
  271. tosize := tcgsize(ord(tosize) - (ord(OS_S8) - ord(OS_8)));
  272. op := storeinstr[tcgsize2unsigned[tosize], ref2.index <> NR_NO, false];
  273. a_load_store(list, op, reg, ref2);
  274. end;
  275. procedure tcgppcgen.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  276. var
  277. op: tasmop;
  278. instr: taicpu;
  279. begin
  280. if not(fromsize in [OS_F32,OS_F64]) or
  281. not(tosize in [OS_F32,OS_F64]) then
  282. internalerror(2006123110);
  283. if (tosize < fromsize) then
  284. op:=A_FRSP
  285. else
  286. op:=A_FMR;
  287. instr := taicpu.op_reg_reg(op,reg2,reg1);
  288. list.concat(instr);
  289. if (op = A_FMR) then
  290. rg[R_FPUREGISTER].add_move_instruction(instr);
  291. end;
  292. procedure tcgppcgen.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  293. const
  294. FpuLoadInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  295. { indexed? updating?}
  296. (((A_LFS,A_LFSU),(A_LFSX,A_LFSUX)),
  297. ((A_LFD,A_LFDU),(A_LFDX,A_LFDUX)));
  298. var
  299. op: tasmop;
  300. ref2: treference;
  301. begin
  302. if not(fromsize in [OS_F32,OS_F64]) or
  303. not(tosize in [OS_F32,OS_F64]) then
  304. internalerror(200201121);
  305. ref2 := ref;
  306. fixref(list,ref2);
  307. op := fpuloadinstr[fromsize,ref2.index <> NR_NO,false];
  308. a_load_store(list,op,reg,ref2);
  309. if (fromsize > tosize) then
  310. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  311. end;
  312. procedure tcgppcgen.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  313. const
  314. FpuStoreInstr: Array[OS_F32..OS_F64,boolean, boolean] of TAsmOp =
  315. { indexed? updating?}
  316. (((A_STFS,A_STFSU),(A_STFSX,A_STFSUX)),
  317. ((A_STFD,A_STFDU),(A_STFDX,A_STFDUX)));
  318. var
  319. op: tasmop;
  320. ref2: treference;
  321. {$ifndef cpu64bit}
  322. reg2: tregister;
  323. {$endif cpu64bit}
  324. begin
  325. if not(fromsize in [OS_F32,OS_F64]) or
  326. not(tosize in [OS_F32,OS_F64]) then
  327. internalerror(200201122);
  328. ref2 := ref;
  329. fixref(list,ref2);
  330. op := fpustoreinstr[tosize,ref2.index <> NR_NO,false];
  331. {$ifndef cpu64bit}
  332. { some ppc's have a bug whereby storing a double to memory }
  333. { as single corrupts the value -> convert double to single }
  334. { first }
  335. if (tosize < fromsize) then
  336. begin
  337. reg2:=getfpuregister(list,tosize);
  338. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg2);
  339. reg:=reg2;
  340. end;
  341. {$endif not cpu64bit}
  342. a_load_store(list,op,reg,ref2);
  343. end;
  344. procedure tcgppcgen.a_load_subsetref_regs_noindex(list: TAsmList; subsetsize: tcgsize; loadbitsize: byte; const sref: tsubsetreference; valuereg, extra_value_reg: tregister);
  345. var
  346. fromsreg, tosreg: tsubsetregister;
  347. restbits: byte;
  348. begin
  349. restbits := (sref.bitlen - (loadbitsize - sref.startbit));
  350. if (subsetsize in [OS_S8..OS_S128]) then
  351. begin
  352. { sign extend }
  353. a_op_const_reg(list,OP_SHL,OS_INT,AIntBits-loadbitsize+sref.startbit,valuereg);
  354. a_op_const_reg(list,OP_SAR,OS_INT,AIntBits-sref.bitlen,valuereg);
  355. end
  356. else
  357. begin
  358. a_op_const_reg(list,OP_SHL,OS_INT,restbits,valuereg);
  359. { mask other bits }
  360. if (sref.bitlen <> AIntBits) then
  361. a_op_const_reg(list,OP_AND,OS_INT,(aword(1) shl sref.bitlen)-1,valuereg);
  362. end;
  363. { use subsetreg routine, it may have been overridden with an optimized version }
  364. fromsreg.subsetreg := extra_value_reg;
  365. fromsreg.subsetregsize := OS_INT;
  366. { subsetregs always count bits from right to left }
  367. fromsreg.startbit := loadbitsize-restbits;
  368. fromsreg.bitlen := restbits;
  369. tosreg.subsetreg := valuereg;
  370. tosreg.subsetregsize := OS_INT;
  371. tosreg.startbit := 0;
  372. tosreg.bitlen := restbits;
  373. a_load_subsetreg_subsetreg(list,subsetsize,subsetsize,fromsreg,tosreg);
  374. end;
  375. procedure tcgppcgen.g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef);
  376. var
  377. hl : tasmlabel;
  378. flags : TResFlags;
  379. begin
  380. if not(cs_check_overflow in current_settings.localswitches) then
  381. exit;
  382. current_asmdata.getjumplabel(hl);
  383. if not ((def.typ=pointerdef) or
  384. ((def.typ=orddef) and
  385. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  386. bool8bit,bool16bit,bool32bit,bool64bit]))) then
  387. begin
  388. if (current_settings.optimizecputype >= cpu_ppc970) or
  389. (current_settings.cputype >= cpu_ppc970) then
  390. begin
  391. { ... instructions setting overflow flag ...
  392. mfxerf R0
  393. mtcrf 128, R0
  394. ble cr0, label }
  395. list.concat(taicpu.op_reg(A_MFXER, NR_R0));
  396. list.concat(taicpu.op_const_reg(A_MTCRF, 128, NR_R0));
  397. flags.cr := RS_CR0;
  398. flags.flag := F_LE;
  399. a_jmp_flags(list, flags, hl);
  400. end
  401. else
  402. begin
  403. list.concat(taicpu.op_reg(A_MCRXR,NR_CR7));
  404. a_jmp(list,A_BC,C_NO,7,hl)
  405. end;
  406. end
  407. else
  408. a_jmp_cond(list,OC_AE,hl);
  409. a_call_name(list,'FPC_OVERFLOW');
  410. a_label(list,hl);
  411. end;
  412. procedure tcgppcgen.g_profilecode(list: TAsmList);
  413. var
  414. paraloc1 : tcgpara;
  415. reg: tregister;
  416. begin
  417. if (target_info.system in [system_powerpc_darwin]) then
  418. begin
  419. paraloc1.init;
  420. paramanager.getintparaloc(pocall_cdecl,1,paraloc1);
  421. a_param_reg(list,OS_ADDR,NR_R0,paraloc1);
  422. paramanager.freeparaloc(list,paraloc1);
  423. paraloc1.done;
  424. allocallcpuregisters(list);
  425. a_call_name(list,'mcount');
  426. deallocallcpuregisters(list);
  427. a_reg_dealloc(list,NR_R0);
  428. end;
  429. end;
  430. procedure tcgppcgen.a_jmp_cond(list : TAsmList;cond : TOpCmp; l: tasmlabel);
  431. begin
  432. a_jmp(list,A_BC,TOpCmp2AsmCond[cond],0,l);
  433. end;
  434. procedure tcgppcgen.a_jmp(list: TAsmList; op: tasmop; c: tasmcondflag;
  435. crval: longint; l: tasmlabel);
  436. var
  437. p: taicpu;
  438. begin
  439. p := taicpu.op_sym(op,l);
  440. if op <> A_B then
  441. create_cond_norm(c,crval,p.condition);
  442. p.is_jmp := true;
  443. list.concat(p)
  444. end;
  445. procedure tcgppcgen.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  446. procedure loadvmttor11;
  447. var
  448. href : treference;
  449. begin
  450. reference_reset_base(href,NR_R3,0);
  451. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R11);
  452. end;
  453. procedure op_onr11methodaddr;
  454. var
  455. href : treference;
  456. begin
  457. if (procdef.extnumber=$ffff) then
  458. Internalerror(200006139);
  459. { call/jmp vmtoffs(%eax) ; method offs }
  460. reference_reset_base(href,NR_R11,procdef._class.vmtmethodoffset(procdef.extnumber));
  461. if hasLargeOffset(href) then
  462. begin
  463. {$ifdef cpu64}
  464. if (longint(href.offset) <> href.offset) then
  465. { add support for offsets > 32 bit }
  466. internalerror(200510201);
  467. {$endif cpu64}
  468. list.concat(taicpu.op_reg_reg_const(A_ADDIS,NR_R11,NR_R11,
  469. smallint((href.offset shr 16)+ord(smallint(href.offset and $ffff) < 0))));
  470. href.offset := smallint(href.offset and $ffff);
  471. end;
  472. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R11);
  473. if (target_info.system = system_powerpc64_linux) then
  474. begin
  475. reference_reset_base(href, NR_R11, 0);
  476. a_load_ref_reg(list, OS_ADDR, OS_ADDR, href, NR_R11);
  477. end;
  478. list.concat(taicpu.op_reg(A_MTCTR,NR_R11));
  479. list.concat(taicpu.op_none(A_BCTR));
  480. if (target_info.system = system_powerpc64_linux) then
  481. list.concat(taicpu.op_none(A_NOP));
  482. end;
  483. var
  484. make_global : boolean;
  485. begin
  486. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  487. Internalerror(200006137);
  488. if not assigned(procdef._class) or
  489. (procdef.procoptions*[po_classmethod, po_staticmethod,
  490. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  491. Internalerror(200006138);
  492. if procdef.owner.symtabletype<>ObjectSymtable then
  493. Internalerror(200109191);
  494. make_global:=false;
  495. if (not current_module.is_unit) or
  496. (cs_create_smart in current_settings.moduleswitches) or
  497. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  498. make_global:=true;
  499. if make_global then
  500. List.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  501. else
  502. List.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  503. { set param1 interface to self }
  504. g_adjust_self_value(list,procdef,ioffset);
  505. { case 4 }
  506. if po_virtualmethod in procdef.procoptions then
  507. begin
  508. loadvmttor11;
  509. op_onr11methodaddr;
  510. end
  511. { case 0 }
  512. else
  513. case target_info.system of
  514. system_powerpc_darwin,
  515. system_powerpc64_darwin:
  516. list.concat(taicpu.op_sym(A_B,get_darwin_call_stub(procdef.mangledname)));
  517. system_powerpc64_linux:
  518. {$note ts:todo add GOT change?? - think not needed :) }
  519. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol('.' + procdef.mangledname)));
  520. else
  521. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)))
  522. end;
  523. List.concat(Tai_symbol_end.Createname(labelname));
  524. end;
  525. procedure tcgppcgen.a_load_store(list:TAsmList;op: tasmop;reg:tregister;
  526. ref: treference);
  527. var
  528. tmpreg: tregister;
  529. tmpref: treference;
  530. largeOffset: Boolean;
  531. begin
  532. tmpreg := NR_NO;
  533. largeOffset:= hasLargeOffset(ref);
  534. if target_info.system = system_powerpc_macos then
  535. begin
  536. if assigned(ref.symbol) then
  537. begin {Load symbol's value}
  538. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  539. reference_reset(tmpref);
  540. tmpref.symbol := ref.symbol;
  541. tmpref.base := NR_RTOC;
  542. if macos_direct_globals then
  543. list.concat(taicpu.op_reg_ref(A_LA,tmpreg,tmpref))
  544. else
  545. list.concat(taicpu.op_reg_ref(A_LWZ,tmpreg,tmpref));
  546. end;
  547. if largeOffset then
  548. begin {Add hi part of offset}
  549. reference_reset(tmpref);
  550. if Smallint(Lo(ref.offset)) < 0 then
  551. tmpref.offset := Hi(ref.offset) + 1 {Compensate when lo part is negative}
  552. else
  553. tmpref.offset := Hi(ref.offset);
  554. if (tmpreg <> NR_NO) then
  555. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg, tmpreg,tmpref))
  556. else
  557. begin
  558. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  559. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  560. end;
  561. end;
  562. if (tmpreg <> NR_NO) then
  563. begin
  564. {Add content of base register}
  565. if ref.base <> NR_NO then
  566. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,
  567. ref.base,tmpreg));
  568. {Make ref ready to be used by op}
  569. ref.symbol:= nil;
  570. ref.base:= tmpreg;
  571. if largeOffset then
  572. ref.offset := Smallint(Lo(ref.offset));
  573. list.concat(taicpu.op_reg_ref(op,reg,ref));
  574. //list.concat(tai_comment.create(strpnew('*** a_load_store indirect global')));
  575. end
  576. else
  577. list.concat(taicpu.op_reg_ref(op,reg,ref));
  578. end
  579. else {if target_info.system <> system_powerpc_macos}
  580. begin
  581. if assigned(ref.symbol) or
  582. largeOffset then
  583. begin
  584. tmpreg := rg[R_INTREGISTER].getregister(list,R_SUBWHOLE);
  585. reference_reset(tmpref);
  586. tmpref.symbol := ref.symbol;
  587. tmpref.relsymbol := ref.relsymbol;
  588. tmpref.offset := ref.offset;
  589. tmpref.refaddr := addr_hi;
  590. if ref.base <> NR_NO then
  591. list.concat(taicpu.op_reg_reg_ref(A_ADDIS,tmpreg,
  592. ref.base,tmpref))
  593. else
  594. list.concat(taicpu.op_reg_ref(A_LIS,tmpreg,tmpref));
  595. ref.base := tmpreg;
  596. ref.refaddr := addr_lo;
  597. list.concat(taicpu.op_reg_ref(op,reg,ref));
  598. end
  599. else
  600. list.concat(taicpu.op_reg_ref(op,reg,ref));
  601. end;
  602. end;
  603. end.